Changed two string with the same german translation
[claws.git] / src / procmsg.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "intl.h"
27 #include "main.h"
28 #include "utils.h"
29 #include "procmsg.h"
30 #include "procheader.h"
31 #include "send.h"
32 #include "procmime.h"
33 #include "statusbar.h"
34 #include "folder.h"
35 #include "prefs_common.h"
36 #include "account.h"
37
38 typedef struct _FlagInfo        FlagInfo;
39
40 struct _FlagInfo
41 {
42         guint    msgnum;
43         MsgFlags flags;
44 };
45
46 static void mark_sum_func                       (gpointer        key,
47                                                  gpointer        value,
48                                                  gpointer        data);
49
50 static GHashTable *procmsg_read_mark_file       (const gchar    *folder);
51 static gint procmsg_cmp_msgnum                  (gconstpointer   a,
52                                                  gconstpointer   b);
53 static gint procmsg_cmp_flag_msgnum             (gconstpointer   a,
54                                                  gconstpointer   b);
55
56
57 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
58 {
59         GHashTable *msg_table;
60
61         if (mlist == NULL) return NULL;
62
63         msg_table = g_hash_table_new(NULL, g_direct_equal);
64         procmsg_msg_hash_table_append(msg_table, mlist);
65
66         return msg_table;
67 }
68
69 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
70 {
71         GSList *cur;
72         MsgInfo *msginfo;
73
74         if (msg_table == NULL || mlist == NULL) return;
75
76         for (cur = mlist; cur != NULL; cur = cur->next) {
77                 msginfo = (MsgInfo *)cur->data;
78
79                 g_hash_table_insert(msg_table,
80                                     GUINT_TO_POINTER(msginfo->msgnum),
81                                     msginfo);
82         }
83 }
84
85 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
86 {
87         GHashTable *msg_table;
88         GSList *cur;
89         MsgInfo *msginfo;
90
91         if (mlist == NULL) return NULL;
92
93         msg_table = g_hash_table_new(NULL, g_direct_equal);
94
95         for (cur = mlist; cur != NULL; cur = cur->next) {
96                 msginfo = (MsgInfo *)cur->data;
97                 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
98         }
99
100         return msg_table;
101 }
102
103 static gint procmsg_read_cache_data_str(FILE *fp, gchar **str)
104 {
105         gchar buf[BUFFSIZE];
106         gint ret = 0;
107         size_t len;
108
109         if (fread(&len, sizeof(len), 1, fp) == 1) {
110                 if (len < 0)
111                         ret = -1;
112                 else {
113                         gchar *tmp = NULL;
114
115                         while (len > 0) {
116                                 size_t size = MIN(len, BUFFSIZE - 1);
117
118                                 if (fread(buf, size, 1, fp) != 1) {
119                                         ret = -1;
120                                         if (tmp) g_free(tmp);
121                                         *str = NULL;
122                                         break;
123                                 }
124
125                                 buf[size] = '\0';
126                                 if (tmp) {
127                                         *str = g_strconcat(tmp, buf, NULL);
128                                         g_free(tmp);
129                                         tmp = *str;
130                                 } else
131                                         tmp = *str = g_strdup(buf);
132
133                                 len -= size;
134                         }
135                 }
136         } else
137                 ret = -1;
138
139         if (ret < 0)
140                 g_warning(_("Cache data is corrupted\n"));
141
142         return ret;
143 }
144
145 #define READ_CACHE_DATA(data, fp) \
146 { \
147         if (procmsg_read_cache_data_str(fp, &data) < 0) { \
148                 procmsg_msginfo_free(msginfo); \
149                 break; \
150         } \
151 }
152
153 #define READ_CACHE_DATA_INT(n, fp) \
154 { \
155         if (fread(&n, sizeof(n), 1, fp) != 1) { \
156                 g_warning(_("Cache data is corrupted\n")); \
157                 procmsg_msginfo_free(msginfo); \
158                 break; \
159         } \
160 }
161
162 GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
163 {
164         GSList *mlist = NULL;
165         GSList *last = NULL;
166         gchar *cache_file;
167         FILE *fp;
168         MsgInfo *msginfo;
169         MsgFlags default_flags;
170         gchar file_buf[BUFFSIZE];
171         gint ver;
172         guint num;
173         FolderType type;
174
175         g_return_val_if_fail(item != NULL, NULL);
176         g_return_val_if_fail(item->folder != NULL, NULL);
177         type = item->folder->type;
178
179         default_flags.perm_flags = MSG_NEW|MSG_UNREAD;
180         default_flags.tmp_flags = MSG_CACHED;
181         if (type == F_MH) {
182                 if (item->stype == F_QUEUE) {
183                         MSG_SET_TMP_FLAGS(default_flags, MSG_QUEUED);
184                 } else if (item->stype == F_DRAFT) {
185                         MSG_SET_TMP_FLAGS(default_flags, MSG_DRAFT);
186                 }
187         } else if (type == F_IMAP) {
188                 MSG_SET_TMP_FLAGS(default_flags, MSG_IMAP);
189         } else if (type == F_NEWS) {
190                 MSG_SET_TMP_FLAGS(default_flags, MSG_NEWS);
191         }
192
193         if (type == F_MH) {
194                 gchar *path;
195
196                 path = folder_item_get_path(item);
197                 if (change_dir(path) < 0) {
198                         g_free(path);
199                         return NULL;
200                 }
201                 g_free(path);
202         }
203         cache_file = folder_item_get_cache_file(item);
204         if ((fp = fopen(cache_file, "r")) == NULL) {
205                 debug_print(_("\tNo cache file\n"));
206                 g_free(cache_file);
207                 return NULL;
208         }
209         setvbuf(fp, file_buf, _IOFBF, sizeof(file_buf));
210         g_free(cache_file);
211
212         debug_print(_("\tReading summary cache...\n"));
213
214         /* compare cache version */
215         if (fread(&ver, sizeof(ver), 1, fp) != 1 ||
216             CACHE_VERSION != ver) {
217                 debug_print(_("Cache version is different. Discarding it.\n"));
218                 fclose(fp);
219                 return NULL;
220         }
221
222         while (fread(&num, sizeof(num), 1, fp) == 1) {
223                 msginfo = g_new0(MsgInfo, 1);
224                 msginfo->msgnum = num;
225                 READ_CACHE_DATA_INT(msginfo->size, fp);
226                 READ_CACHE_DATA_INT(msginfo->mtime, fp);
227                 READ_CACHE_DATA_INT(msginfo->date_t, fp);
228                 READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
229
230                 READ_CACHE_DATA(msginfo->fromname, fp);
231
232                 READ_CACHE_DATA(msginfo->date, fp);
233                 READ_CACHE_DATA(msginfo->from, fp);
234                 READ_CACHE_DATA(msginfo->to, fp);
235                 READ_CACHE_DATA(msginfo->cc, fp);
236                 READ_CACHE_DATA(msginfo->newsgroups, fp);
237                 READ_CACHE_DATA(msginfo->subject, fp);
238                 READ_CACHE_DATA(msginfo->msgid, fp);
239                 READ_CACHE_DATA(msginfo->inreplyto, fp);
240                 READ_CACHE_DATA(msginfo->references, fp);
241
242                 MSG_SET_PERM_FLAGS(msginfo->flags, default_flags.perm_flags);
243                 MSG_SET_TMP_FLAGS(msginfo->flags, default_flags.tmp_flags);
244
245                 /* if the message file doesn't exist or is changed,
246                    don't add the data */
247                 if (type == F_MH && scan_file &&
248                     folder_item_is_msg_changed(item, msginfo))
249                         procmsg_msginfo_free(msginfo);
250                 else {
251                         msginfo->folder = item;
252
253                         if (!mlist)
254                                 last = mlist = g_slist_append(NULL, msginfo);
255                         else {
256                                 last = g_slist_append(last, msginfo);
257                                 last = last->next;
258                         }
259                 }
260         }
261
262         fclose(fp);
263         debug_print(_("done.\n"));
264
265         return mlist;
266 }
267
268 #undef READ_CACHE_DATA
269 #undef READ_CACHE_DATA_INT
270
271 void procmsg_set_flags(GSList *mlist, FolderItem *item)
272 {
273         GSList *cur, *tmp;
274         gint newmsg = 0;
275         gint lastnum = 0;
276         gchar *markdir;
277         MsgInfo *msginfo;
278         GHashTable *mark_table;
279         MsgFlags *flags;
280
281         if (!mlist) return;
282         g_return_if_fail(item != NULL);
283         g_return_if_fail(item->folder != NULL);
284
285         debug_print(_("\tMarking the messages...\n"));
286
287         markdir = folder_item_get_path(item);
288         if (!is_dir_exist(markdir))
289                 make_dir_hier(markdir);
290
291         mark_table = procmsg_read_mark_file(markdir);
292         g_free(markdir);
293
294         if (!mark_table) return;
295
296         for (cur = mlist; cur != NULL; cur = cur->next) {
297                 msginfo = (MsgInfo *)cur->data;
298
299                 if (lastnum < msginfo->msgnum)
300                         lastnum = msginfo->msgnum;
301
302                 flags = g_hash_table_lookup
303                         (mark_table, GUINT_TO_POINTER(msginfo->msgnum));
304
305                 if (flags != NULL) {
306                         /* add the permanent flags only */
307                         msginfo->flags.perm_flags = flags->perm_flags;
308                         if (item->folder->type == F_IMAP) {
309                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_IMAP);
310                         } else if (item->folder->type == F_NEWS) {
311                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_NEWS);
312                         }
313                 } else {
314                         /* not found (new message) */
315                         if (newmsg == 0) {
316                                 for (tmp = mlist; tmp != cur; tmp = tmp->next)
317                                         MSG_UNSET_PERM_FLAGS
318                                                 (((MsgInfo *)tmp->data)->flags,
319                                                  MSG_NEW);
320                         }
321                         newmsg++;
322                 }
323         }
324
325         item->last_num = lastnum;
326
327         debug_print(_("done.\n"));
328         if (newmsg)
329                 debug_print(_("\t%d new message(s)\n"), newmsg);
330
331         hash_free_value_mem(mark_table);
332         g_hash_table_destroy(mark_table);
333 }
334
335 gint procmsg_get_last_num_in_cache(GSList *mlist)
336 {
337         GSList *cur;
338         MsgInfo *msginfo;
339         gint last = 0;
340
341         if (mlist == NULL) return 0;
342
343         for (cur = mlist; cur != NULL; cur = cur->next) {
344                 msginfo = (MsgInfo *)cur->data;
345                 if (msginfo && msginfo->msgnum > last)
346                         last = msginfo->msgnum;
347         }
348
349         return last;
350 }
351
352 void procmsg_msg_list_free(GSList *mlist)
353 {
354         GSList *cur;
355         MsgInfo *msginfo;
356
357         for (cur = mlist; cur != NULL; cur = cur->next) {
358                 msginfo = (MsgInfo *)cur->data;
359                 procmsg_msginfo_free(msginfo);
360         }
361         g_slist_free(mlist);
362 }
363
364 void procmsg_write_cache(MsgInfo *msginfo, FILE *fp)
365 {
366         MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
367
368         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
369         WRITE_CACHE_DATA_INT(msginfo->size, fp);
370         WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
371         WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
372         WRITE_CACHE_DATA_INT(flags, fp);
373
374         WRITE_CACHE_DATA(msginfo->fromname, fp);
375
376         WRITE_CACHE_DATA(msginfo->date, fp);
377         WRITE_CACHE_DATA(msginfo->from, fp);
378         WRITE_CACHE_DATA(msginfo->to, fp);
379         WRITE_CACHE_DATA(msginfo->cc, fp);
380         WRITE_CACHE_DATA(msginfo->newsgroups, fp);
381         WRITE_CACHE_DATA(msginfo->subject, fp);
382         WRITE_CACHE_DATA(msginfo->msgid, fp);
383         WRITE_CACHE_DATA(msginfo->inreplyto, fp);
384         WRITE_CACHE_DATA(msginfo->references, fp);
385 }
386
387 void procmsg_write_flags(MsgInfo *msginfo, FILE *fp)
388 {
389         MsgPermFlags flags = msginfo->flags.perm_flags;
390
391         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
392         WRITE_CACHE_DATA_INT(flags, fp);
393 }
394
395 struct MarkSum {
396         gint *new;
397         gint *unread;
398         gint *total;
399 };
400
401 static void mark_sum_func(gpointer key, gpointer value, gpointer data)
402 {
403         MsgFlags *flags = value;
404         struct MarkSum *marksum = data;
405
406         if (MSG_IS_NEW(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->new)++;
407         if (MSG_IS_UNREAD(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->unread)++;
408         (*marksum->total)++;
409
410         g_free(flags);
411 }
412
413 void procmsg_get_mark_sum(const gchar *folder,
414                           gint *new, gint *unread, gint *total)
415 {
416         GHashTable *mark_table;
417         struct MarkSum marksum;
418
419         *new = *unread = *total = 0;
420         marksum.new    = new;
421         marksum.unread = unread;
422         marksum.total  = total;
423
424         mark_table = procmsg_read_mark_file(folder);
425
426         if (mark_table) {
427                 g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
428                 g_hash_table_destroy(mark_table);
429         }
430         debug_print("mark->new = %d, mark->unread = %d, mark->total = %d\n",
431                     *(marksum.new), *(marksum.unread), *(marksum.total));
432 }
433
434 static GHashTable *procmsg_read_mark_file(const gchar *folder)
435 {
436         FILE *fp;
437         GHashTable *mark_table = NULL;
438         gint num;
439         MsgFlags *flags;
440         MsgPermFlags perm_flags;
441
442         if ((fp = procmsg_open_mark_file(folder, FALSE)) == NULL)
443                 return NULL;
444
445         mark_table = g_hash_table_new(NULL, g_direct_equal);
446
447         while (fread(&num, sizeof(num), 1, fp) == 1) {
448                 if (fread(&perm_flags, sizeof(flags), 1, fp) != 1) break;
449
450                 flags = g_new0(MsgFlags, 1);
451                 flags->perm_flags = perm_flags;
452
453                 g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
454         }
455
456         fclose(fp);
457         return mark_table;
458 }
459
460 FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
461 {
462         gchar *markfile;
463         FILE *fp;
464         gint ver;
465
466         markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
467
468         if ((fp = fopen(markfile, "r")) == NULL)
469                 debug_print(_("Mark file not found.\n"));
470         else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
471                 debug_print(_("Mark version is different (%d != %d). "
472                               "Discarding it.\n"), ver, MARK_VERSION);
473                 fclose(fp);
474                 fp = NULL;
475         }
476
477         /* read mode */
478         if (append == FALSE) {
479                 g_free(markfile);
480                 return fp;
481         }
482
483         if (fp) {
484                 /* reopen with append mode */
485                 fclose(fp);
486                 if ((fp = fopen(markfile, "a")) == NULL)
487                         g_warning(_("Can't open mark file with append mode.\n"));
488         } else {
489                 /* open with overwrite mode if mark file doesn't exist or
490                    version is different */
491                 if ((fp = fopen(markfile, "w")) == NULL)
492                         g_warning(_("Can't open mark file with write mode.\n"));
493                 else {
494                         ver = MARK_VERSION;
495                         WRITE_CACHE_DATA_INT(ver, fp);
496                 }
497         }
498
499         g_free(markfile);
500         return fp;
501 }
502
503 /* return the reversed thread tree */
504 GNode *procmsg_get_thread_tree(GSList *mlist)
505 {
506         GNode *root, *parent, *node, *next;
507         GHashTable *msgid_table;
508         GHashTable *subject_table;
509         MsgInfo *msginfo;
510         const gchar *msgid;
511         const gchar *subject;
512
513         root = g_node_new(NULL);
514         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
515         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
516
517         for (; mlist != NULL; mlist = mlist->next) {
518                 msginfo = (MsgInfo *)mlist->data;
519                 parent = root;
520
521                 if (msginfo->inreplyto) {
522                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
523                         if (parent == NULL) {
524                                 parent = root;
525                         } else {
526                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
527                                         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
528                                 }
529                         }
530                 }
531                 node = g_node_insert_data_before
532                         (parent, parent == root ? parent->children : NULL,
533                          msginfo);
534                 if ((msgid = msginfo->msgid) &&
535                     g_hash_table_lookup(msgid_table, msgid) == NULL)
536                         g_hash_table_insert(msgid_table, (gchar *)msgid, node);
537
538                 subject = msginfo->subject;
539                 if (subject_table_lookup(subject_table,
540                                          (gchar *) subject) == NULL)
541                         subject_table_insert(subject_table, (gchar *)subject,
542                                              node);
543         }
544
545         /* complete the unfinished threads */
546         for (node = root->children; node != NULL; ) {
547                 next = node->next;
548                 msginfo = (MsgInfo *)node->data;
549                 parent = NULL;
550                 /* CLAWS: ignore thread */
551                 if (msginfo->inreplyto) 
552                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
553                 if (parent && parent != node) {
554                         g_node_unlink(node);
555                         g_node_insert_before
556                                 (parent, parent->children, node);
557                         if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
558                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
559                         }
560                 }
561                 node = next;
562         }
563
564         /* CLAWS: now see if the first level (below root) still has some nodes that can be
565          * threaded by subject line. we need to handle this in a special way to prevent
566          * circular reference from a node that has already been threaded by IN-REPLY-TO
567          * but is also in the subject line hash table */
568         for (node = root->children; node != NULL; ) {
569                 next = node->next;
570                 msginfo = (MsgInfo *) node->data;
571                 parent = NULL;
572                 if (subject_is_reply(msginfo->subject)) {
573                         parent = subject_table_lookup(subject_table,
574                                                       msginfo->subject);
575                         /* the node may already be threaded by IN-REPLY-TO,
576                            so go up in the tree to find the parent node */
577                         if (parent != NULL) {
578                                 if (g_node_is_ancestor(node, parent))
579                                         parent = NULL;
580                                 if (parent == node)
581                                         parent = NULL;
582                         }
583
584                         if (parent) {
585                                 g_node_unlink(node);
586                                 g_node_append(parent, node);
587                         }
588                 }                                       
589                 node = next;
590         }               
591                 
592         g_hash_table_destroy(subject_table);
593         g_hash_table_destroy(msgid_table);
594
595         return root;
596 }
597
598 void procmsg_move_messages(GSList *mlist)
599 {
600         GSList *cur, *movelist = NULL;
601         MsgInfo *msginfo;
602         FolderItem *dest = NULL;
603         GHashTable *hash;
604
605         if (!mlist) return;
606
607         hash = procmsg_to_folder_hash_table_create(mlist);
608         folder_item_scan_foreach(hash);
609         g_hash_table_destroy(hash);
610
611         for (cur = mlist; cur != NULL; cur = cur->next) {
612                 msginfo = (MsgInfo *)cur->data;
613                 if (!dest) {
614                         dest = msginfo->to_folder;
615                         movelist = g_slist_append(movelist, msginfo);
616                 } else if (dest == msginfo->to_folder) {
617                         movelist = g_slist_append(movelist, msginfo);
618                 } else {
619                         folder_item_move_msgs_with_dest(dest, movelist);
620                         g_slist_free(movelist);
621                         movelist = NULL;
622                         dest = msginfo->to_folder;
623                         movelist = g_slist_append(movelist, msginfo);
624                 }
625         }
626
627         if (movelist) {
628                 folder_item_move_msgs_with_dest(dest, movelist);
629                 g_slist_free(movelist);
630         }
631 }
632
633 void procmsg_copy_messages(GSList *mlist)
634 {
635         GSList *cur, *copylist = NULL;
636         MsgInfo *msginfo;
637         FolderItem *dest = NULL;
638         GHashTable *hash;
639
640         if (!mlist) return;
641
642         hash = procmsg_to_folder_hash_table_create(mlist);
643         folder_item_scan_foreach(hash);
644         g_hash_table_destroy(hash);
645
646         for (cur = mlist; cur != NULL; cur = cur->next) {
647                 msginfo = (MsgInfo *)cur->data;
648                 if (!dest) {
649                         dest = msginfo->to_folder;
650                         copylist = g_slist_append(copylist, msginfo);
651                 } else if (dest == msginfo->to_folder) {
652                         copylist = g_slist_append(copylist, msginfo);
653                 } else {
654                         folder_item_copy_msgs_with_dest(dest, copylist);
655                         g_slist_free(copylist);
656                         copylist = NULL;
657                         dest = msginfo->to_folder;
658                         copylist = g_slist_append(copylist, msginfo);
659                 }
660         }
661
662         if (copylist) {
663                 folder_item_copy_msgs_with_dest(dest, copylist);
664                 g_slist_free(copylist);
665         }
666 }
667
668 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
669 {
670         gchar *path, *file;
671
672         g_return_val_if_fail(msginfo != NULL, NULL);
673
674         if (msginfo->plaintext_file)
675                 file = g_strdup(msginfo->plaintext_file);
676         else {
677                 path = folder_item_get_path(msginfo->folder);
678                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
679                                    itos(msginfo->msgnum), NULL);
680                 g_free(path);
681         }
682
683         return file;
684 }
685
686 gchar *procmsg_get_message_file(MsgInfo *msginfo)
687 {
688         gchar *filename = NULL;
689
690         g_return_val_if_fail(msginfo != NULL, NULL);
691
692         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
693         if (!filename)
694                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
695
696         return filename;
697 }
698
699 FILE *procmsg_open_message(MsgInfo *msginfo)
700 {
701         FILE *fp;
702         gchar *file;
703
704         g_return_val_if_fail(msginfo != NULL, NULL);
705
706         file = procmsg_get_message_file_path(msginfo);
707         g_return_val_if_fail(file != NULL, NULL);
708
709         if ((fp = fopen(file, "r")) == NULL) {
710                 FILE_OP_ERROR(file, "fopen");
711                 g_free(file);
712                 return NULL;
713         }
714
715         g_free(file);
716
717         if (MSG_IS_QUEUED(msginfo->flags)) {
718                 gchar buf[BUFFSIZE];
719
720                 while (fgets(buf, sizeof(buf), fp) != NULL)
721                         if (buf[0] == '\r' || buf[0] == '\n') break;
722         }
723
724         return fp;
725 }
726
727 gboolean procmsg_msg_exist(MsgInfo *msginfo)
728 {
729         gchar *path;
730         gboolean ret;
731
732         if (!msginfo) return FALSE;
733
734         path = folder_item_get_path(msginfo->folder);
735         change_dir(path);
736         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
737         g_free(path);
738
739         return ret;
740 }
741
742 void procmsg_empty_trash(void)
743 {
744         FolderItem *trash;
745         GList *cur;
746
747         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
748                 trash = FOLDER(cur->data)->trash;
749                 if (trash) folder_item_remove_all_msg(trash);
750         }
751 }
752
753 gint procmsg_send_queue(void)
754 {
755         FolderItem *queue;
756         gint i;
757         gint ret = 0;
758
759         queue = folder_get_default_queue();
760         g_return_val_if_fail(queue != NULL, -1);
761         folder_item_scan(queue);
762         if (queue->last_num < 0) return -1;
763         else if (queue->last_num == 0) return 0;
764
765         for (i = 1; i <= queue->last_num; i++) {
766                 gchar *file;
767
768                 file = folder_item_fetch_msg(queue, i);
769                 if (file) {
770                         if (procmsg_send_message_queue(file) < 0) {
771                                 g_warning(_("Sending queued message %d failed.\n"), i);
772                                 ret = -1;
773                         } else
774                                 folder_item_remove_msg(queue, i);
775                         g_free(file);
776                 }
777         }
778
779         return ret;
780 }
781
782 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
783 {
784         static const gchar *def_cmd = "lpr %s";
785         static guint id = 0;
786         gchar *prtmp;
787         FILE *tmpfp, *prfp;
788         gchar buf[1024];
789         gchar *p;
790
791         g_return_if_fail(msginfo);
792
793         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
794                 g_warning(_("Can't get text part\n"));
795                 return;
796         }
797
798         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
799                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
800
801         if ((prfp = fopen(prtmp, "w")) == NULL) {
802                 FILE_OP_ERROR(prtmp, "fopen");
803                 g_free(prtmp);
804                 fclose(tmpfp);
805                 return;
806         }
807
808         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
809         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
810         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
811         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
812         if (msginfo->newsgroups)
813                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
814         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
815         fputc('\n', prfp);
816
817         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
818                 fputs(buf, prfp);
819
820         fclose(prfp);
821         fclose(tmpfp);
822
823         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
824             !strchr(p + 2, '%'))
825                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
826         else {
827                 if (cmdline)
828                         g_warning(_("Print command line is invalid: `%s'\n"),
829                                   cmdline);
830                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
831         }
832
833         g_free(prtmp);
834
835         g_strchomp(buf);
836         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
837         system(buf);
838 }
839
840 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
841 {
842         MsgInfo *newmsginfo;
843
844         if (msginfo == NULL) return NULL;
845
846         newmsginfo = g_new0(MsgInfo, 1);
847
848 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
849 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
850                         g_strdup(msginfo->mmb) : NULL
851
852         MEMBCOPY(msgnum);
853         MEMBCOPY(size);
854         MEMBCOPY(mtime);
855         MEMBCOPY(date_t);
856         MEMBCOPY(flags);
857
858         MEMBDUP(fromname);
859
860         MEMBDUP(date);
861         MEMBDUP(from);
862         MEMBDUP(to);
863         MEMBDUP(cc);
864         MEMBDUP(newsgroups);
865         MEMBDUP(subject);
866         MEMBDUP(msgid);
867         MEMBDUP(inreplyto);
868
869         MEMBCOPY(folder);
870         MEMBCOPY(to_folder);
871
872         MEMBDUP(xface);
873         MEMBDUP(dispositionnotificationto);
874         MEMBDUP(returnreceiptto);
875         MEMBDUP(references);
876
877         MEMBCOPY(score);
878         MEMBCOPY(threadscore);
879
880         return newmsginfo;
881 }
882
883 void procmsg_msginfo_free(MsgInfo *msginfo)
884 {
885         if (msginfo == NULL) return;
886
887         g_free(msginfo->fromspace);
888         g_free(msginfo->references);
889         g_free(msginfo->returnreceiptto);
890         g_free(msginfo->dispositionnotificationto);
891         g_free(msginfo->xface);
892
893         g_free(msginfo->fromname);
894
895         g_free(msginfo->date);
896         g_free(msginfo->from);
897         g_free(msginfo->to);
898         g_free(msginfo->cc);
899         g_free(msginfo->newsgroups);
900         g_free(msginfo->subject);
901         g_free(msginfo->msgid);
902         g_free(msginfo->inreplyto);
903
904         g_free(msginfo);
905 }
906
907 static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
908 {
909         const MsgInfo *msginfo = a;
910         const guint msgnum = GPOINTER_TO_UINT(b);
911
912         if (!msginfo)
913                 return -1;
914
915         return msginfo->msgnum - msgnum;
916 }
917
918 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
919 {
920         const MsgInfo *msginfo1 = a;
921         const MsgInfo *msginfo2 = b;
922
923         if (!msginfo1)
924                 return -1;
925         if (!msginfo2)
926                 return -1;
927
928         return msginfo1->msgnum - msginfo2->msgnum;
929 }
930
931 static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
932 {
933         const FlagInfo *finfo = a;
934         const guint msgnum = GPOINTER_TO_UINT(b);
935
936         if (!finfo)
937                 return -1;
938
939         return finfo->msgnum - msgnum;
940 }
941
942 enum
943 {
944         Q_SENDER          = 0,
945         Q_SMTPSERVER      = 1,
946         Q_RECIPIENTS      = 2,
947         Q_NEWSGROUPS      = 3,
948         Q_MAIL_ACCOUNT_ID = 4,
949         Q_NEWS_ACCOUNT_ID = 5
950 };
951
952 gint procmsg_send_message_queue(const gchar *file)
953 {
954         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
955                                        {"SSV:",  NULL, FALSE},
956                                        {"R:",    NULL, FALSE},
957                                        {"NG:",   NULL, FALSE},
958                                        {"MAID:", NULL, FALSE},
959                                        {"NAID:", NULL, FALSE},
960                                        {NULL,    NULL, FALSE}};
961         FILE *fp;
962         gint filepos;
963         gint mailval = 0, newsval = 0;
964         gchar *from = NULL;
965         gchar *smtpserver = NULL;
966         GSList *to_list = NULL;
967         GSList *newsgroup_list = NULL;
968         gchar buf[BUFFSIZE];
969         gint hnum;
970         PrefsAccount *mailac = NULL, *newsac = NULL;
971         gchar *tmp = NULL;
972
973         g_return_val_if_fail(file != NULL, -1);
974
975         if ((fp = fopen(file, "r")) == NULL) {
976                 FILE_OP_ERROR(file, "fopen");
977                 return -1;
978         }
979
980         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
981                != -1) {
982                 gchar *p = buf + strlen(qentry[hnum].name);
983
984                 switch (hnum) {
985                 case Q_SENDER:
986                         if (!from) from = g_strdup(p);
987                         break;
988                 case Q_SMTPSERVER:
989                         if (!smtpserver) smtpserver = g_strdup(p);
990                         break;
991                 case Q_RECIPIENTS:
992                         to_list = address_list_append(to_list, p);
993                         break;
994                 case Q_NEWSGROUPS:
995                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
996                         break;
997                 case Q_MAIL_ACCOUNT_ID:
998                         mailac = account_find_from_id(atoi(p));
999                         break;
1000                 case Q_NEWS_ACCOUNT_ID:
1001                         newsac = account_find_from_id(atoi(p));
1002                         break;
1003                 default:
1004                 }
1005         }
1006         filepos = ftell(fp);
1007
1008         if(newsgroup_list || prefs_common.savemsg) {
1009                 FILE *tmpfp;
1010
1011                 /* write to temporary file */
1012                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1013                             G_DIR_SEPARATOR, (gint)file);
1014                 if ((tmpfp = fopen(tmp, "w")) == NULL) {
1015                         FILE_OP_ERROR(tmp, "fopen");
1016                         newsval = -1;
1017                 }
1018                 if (change_file_mode_rw(tmpfp, tmp) < 0) {
1019                         FILE_OP_ERROR(tmp, "chmod");
1020                         g_warning(_("can't change file mode\n"));
1021                 }
1022
1023                 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1024                         if (fputs(buf, tmpfp) == EOF) {
1025                                 FILE_OP_ERROR(tmp, "fputs");
1026                                 newsval = -1;
1027                         }
1028                 }
1029                 fclose(tmpfp);
1030         }
1031
1032         fseek(fp, filepos, SEEK_SET);
1033         if (to_list) {
1034                 debug_print(_("Sending message by mail\n"));
1035                 if(!from) {
1036                         g_warning(_("Queued message header is broken.\n"));
1037                         mailval = -1;
1038                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1039                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1040                 } else {
1041                         if (!mailac) {
1042                                 mailac = account_find_from_smtp_server(from, smtpserver);
1043                                 if (!mailac) {
1044                                         g_warning(_("Account not found. "
1045                                                     "Using current account...\n"));
1046                                         mailac = cur_account;
1047                                 }
1048                         }
1049
1050                         if (mailac)
1051                                 mailval = send_message_smtp(mailac, to_list, fp);
1052                         else {
1053                                 PrefsAccount tmp_ac;
1054
1055                                 g_warning(_("Account not found.\n"));
1056
1057                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1058                                 tmp_ac.address = from;
1059                                 tmp_ac.smtp_server = smtpserver;
1060                                 tmp_ac.smtpport = SMTP_PORT;
1061                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1062                         }
1063                 }
1064         }
1065
1066         if(newsgroup_list) {
1067                 Folder *folder;
1068
1069                 debug_print(_("Sending message by news\n"));
1070
1071                 folder = FOLDER(newsac->folder);
1072
1073                 if(newsval == 0) {
1074                         newsval = news_post(folder, tmp);
1075                         if (newsval < 0) {
1076                                 alertpanel_error(_("Error occurred while posting the message to %s ."),
1077                                          newsac->nntp_server);
1078                         }
1079                 }
1080
1081         }
1082
1083         /* save message to outbox */
1084         if (mailval == 0 && newsval == 0 && prefs_common.savemsg) {
1085                 FolderItem *outbox;
1086                 gchar *path;
1087                 gint num;
1088                 FILE *fp;
1089
1090                 debug_print(_("saving sent message...\n"));
1091
1092                 outbox = folder_get_default_outbox();
1093                 path = folder_item_get_path(outbox);
1094                 if (!is_dir_exist(path))
1095                         make_dir_hier(path);
1096
1097                 folder_item_scan(outbox);
1098                 if ((num = folder_item_add_msg(outbox, tmp, FALSE)) < 0) {
1099                         g_warning(_("can't save message\n"));
1100                 }
1101
1102                 if(num) {
1103                         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
1104                                 g_warning(_("can't open mark file\n"));
1105                         else {
1106                                 MsgInfo newmsginfo;
1107
1108                                 newmsginfo.msgnum = num;
1109                                 newmsginfo.flags.perm_flags = 0;
1110                                 newmsginfo.flags.tmp_flags = 0;
1111                                 procmsg_write_flags(&newmsginfo, fp);
1112                                 fclose(fp);
1113                         }
1114                 }
1115                 g_free(path);
1116         }
1117
1118         slist_free_strings(to_list);
1119         g_slist_free(to_list);
1120         slist_free_strings(newsgroup_list);
1121         g_slist_free(newsgroup_list);
1122         g_free(from);
1123         g_free(smtpserver);
1124         fclose(fp);
1125         if(tmp) {
1126                 unlink(tmp);
1127                 g_free(tmp);
1128         }
1129
1130         return (newsval != 0 ? newsval : mailval);
1131 }