sync with sylpheed 0.7.0cvs38
[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(perm_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                 if (msginfo->inreplyto) 
551                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
552                 if (parent && parent != node) {
553                         g_node_unlink(node);
554                         g_node_insert_before
555                                 (parent, parent->children, node);
556                         /* CLAWS: ignore thread */
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                                 /* CLAWS: ignore thread */
588                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
589                                         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
590                                 }
591                         }
592                 }                                       
593                 node = next;
594         }               
595                 
596         g_hash_table_destroy(subject_table);
597         g_hash_table_destroy(msgid_table);
598
599         return root;
600 }
601
602 void procmsg_move_messages(GSList *mlist)
603 {
604         GSList *cur, *movelist = NULL;
605         MsgInfo *msginfo;
606         FolderItem *dest = NULL;
607         GHashTable *hash;
608
609         if (!mlist) return;
610
611         hash = procmsg_to_folder_hash_table_create(mlist);
612         folder_item_scan_foreach(hash);
613         g_hash_table_destroy(hash);
614
615         for (cur = mlist; cur != NULL; cur = cur->next) {
616                 msginfo = (MsgInfo *)cur->data;
617                 if (!dest) {
618                         dest = msginfo->to_folder;
619                         movelist = g_slist_append(movelist, msginfo);
620                 } else if (dest == msginfo->to_folder) {
621                         movelist = g_slist_append(movelist, msginfo);
622                 } else {
623                         folder_item_move_msgs_with_dest(dest, movelist);
624                         g_slist_free(movelist);
625                         movelist = NULL;
626                         dest = msginfo->to_folder;
627                         movelist = g_slist_append(movelist, msginfo);
628                 }
629         }
630
631         if (movelist) {
632                 folder_item_move_msgs_with_dest(dest, movelist);
633                 g_slist_free(movelist);
634         }
635 }
636
637 void procmsg_copy_messages(GSList *mlist)
638 {
639         GSList *cur, *copylist = NULL;
640         MsgInfo *msginfo;
641         FolderItem *dest = NULL;
642         GHashTable *hash;
643
644         if (!mlist) return;
645
646         hash = procmsg_to_folder_hash_table_create(mlist);
647         folder_item_scan_foreach(hash);
648         g_hash_table_destroy(hash);
649
650         for (cur = mlist; cur != NULL; cur = cur->next) {
651                 msginfo = (MsgInfo *)cur->data;
652                 if (!dest) {
653                         dest = msginfo->to_folder;
654                         copylist = g_slist_append(copylist, msginfo);
655                 } else if (dest == msginfo->to_folder) {
656                         copylist = g_slist_append(copylist, msginfo);
657                 } else {
658                         folder_item_copy_msgs_with_dest(dest, copylist);
659                         g_slist_free(copylist);
660                         copylist = NULL;
661                         dest = msginfo->to_folder;
662                         copylist = g_slist_append(copylist, msginfo);
663                 }
664         }
665
666         if (copylist) {
667                 folder_item_copy_msgs_with_dest(dest, copylist);
668                 g_slist_free(copylist);
669         }
670 }
671
672 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
673 {
674         gchar *path, *file;
675
676         g_return_val_if_fail(msginfo != NULL, NULL);
677
678         if (msginfo->plaintext_file)
679                 file = g_strdup(msginfo->plaintext_file);
680         else {
681                 path = folder_item_get_path(msginfo->folder);
682                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
683                                    itos(msginfo->msgnum), NULL);
684                 g_free(path);
685         }
686
687         return file;
688 }
689
690 gchar *procmsg_get_message_file(MsgInfo *msginfo)
691 {
692         gchar *filename = NULL;
693
694         g_return_val_if_fail(msginfo != NULL, NULL);
695
696         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
697         if (!filename)
698                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
699
700         return filename;
701 }
702
703 FILE *procmsg_open_message(MsgInfo *msginfo)
704 {
705         FILE *fp;
706         gchar *file;
707
708         g_return_val_if_fail(msginfo != NULL, NULL);
709
710         file = procmsg_get_message_file(msginfo);
711         g_return_val_if_fail(file != NULL, NULL);
712
713         if ((fp = fopen(file, "r")) == NULL) {
714                 FILE_OP_ERROR(file, "fopen");
715                 g_free(file);
716                 return NULL;
717         }
718
719         g_free(file);
720
721         if (MSG_IS_QUEUED(msginfo->flags)) {
722                 gchar buf[BUFFSIZE];
723
724                 while (fgets(buf, sizeof(buf), fp) != NULL)
725                         if (buf[0] == '\r' || buf[0] == '\n') break;
726         }
727
728         return fp;
729 }
730
731 gboolean procmsg_msg_exist(MsgInfo *msginfo)
732 {
733         gchar *path;
734         gboolean ret;
735
736         if (!msginfo) return FALSE;
737
738         path = folder_item_get_path(msginfo->folder);
739         change_dir(path);
740         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
741         g_free(path);
742
743         return ret;
744 }
745
746 void procmsg_empty_trash(void)
747 {
748         FolderItem *trash;
749         GList *cur;
750
751         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
752                 trash = FOLDER(cur->data)->trash;
753                 if (trash && trash->total > 0)
754                         folder_item_remove_all_msg(trash);
755         }
756 }
757
758 gint procmsg_send_queue(void)
759 {
760         FolderItem *queue;
761         gint i;
762         gint ret = 0;
763
764         queue = folder_get_default_queue();
765         g_return_val_if_fail(queue != NULL, -1);
766         folder_item_scan(queue);
767         if (queue->last_num < 0) return -1;
768         else if (queue->last_num == 0) return 0;
769
770         for (i = 1; i <= queue->last_num; i++) {
771                 gchar *file;
772
773                 file = folder_item_fetch_msg(queue, i);
774                 if (file) {
775                         if (procmsg_send_message_queue(file) < 0) {
776                                 g_warning(_("Sending queued message %d failed.\n"), i);
777                                 ret = -1;
778                         } else
779                                 folder_item_remove_msg(queue, i);
780                         g_free(file);
781                 }
782         }
783
784         return ret;
785 }
786
787 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
788 {
789         static const gchar *def_cmd = "lpr %s";
790         static guint id = 0;
791         gchar *prtmp;
792         FILE *tmpfp, *prfp;
793         gchar buf[1024];
794         gchar *p;
795
796         g_return_if_fail(msginfo);
797
798         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
799                 g_warning(_("Can't get text part\n"));
800                 return;
801         }
802
803         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
804                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
805
806         if ((prfp = fopen(prtmp, "w")) == NULL) {
807                 FILE_OP_ERROR(prtmp, "fopen");
808                 g_free(prtmp);
809                 fclose(tmpfp);
810                 return;
811         }
812
813         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
814         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
815         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
816         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
817         if (msginfo->newsgroups)
818                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
819         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
820         fputc('\n', prfp);
821
822         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
823                 fputs(buf, prfp);
824
825         fclose(prfp);
826         fclose(tmpfp);
827
828         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
829             !strchr(p + 2, '%'))
830                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
831         else {
832                 if (cmdline)
833                         g_warning(_("Print command line is invalid: `%s'\n"),
834                                   cmdline);
835                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
836         }
837
838         g_free(prtmp);
839
840         g_strchomp(buf);
841         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
842         system(buf);
843 }
844
845 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
846 {
847         MsgInfo *newmsginfo;
848
849         if (msginfo == NULL) return NULL;
850
851         newmsginfo = g_new0(MsgInfo, 1);
852
853 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
854 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
855                         g_strdup(msginfo->mmb) : NULL
856
857         MEMBCOPY(msgnum);
858         MEMBCOPY(size);
859         MEMBCOPY(mtime);
860         MEMBCOPY(date_t);
861         MEMBCOPY(flags);
862
863         MEMBDUP(fromname);
864
865         MEMBDUP(date);
866         MEMBDUP(from);
867         MEMBDUP(to);
868         MEMBDUP(cc);
869         MEMBDUP(newsgroups);
870         MEMBDUP(subject);
871         MEMBDUP(msgid);
872         MEMBDUP(inreplyto);
873
874         MEMBCOPY(folder);
875         MEMBCOPY(to_folder);
876
877         MEMBDUP(xface);
878         MEMBDUP(dispositionnotificationto);
879         MEMBDUP(returnreceiptto);
880         MEMBDUP(references);
881
882         MEMBCOPY(score);
883         MEMBCOPY(threadscore);
884
885         return newmsginfo;
886 }
887
888 void procmsg_msginfo_free(MsgInfo *msginfo)
889 {
890         if (msginfo == NULL) return;
891
892         g_free(msginfo->fromspace);
893         g_free(msginfo->references);
894         g_free(msginfo->returnreceiptto);
895         g_free(msginfo->dispositionnotificationto);
896         g_free(msginfo->xface);
897
898         g_free(msginfo->fromname);
899
900         g_free(msginfo->date);
901         g_free(msginfo->from);
902         g_free(msginfo->to);
903         g_free(msginfo->cc);
904         g_free(msginfo->newsgroups);
905         g_free(msginfo->subject);
906         g_free(msginfo->msgid);
907         g_free(msginfo->inreplyto);
908
909         g_free(msginfo);
910 }
911
912 static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
913 {
914         const MsgInfo *msginfo = a;
915         const guint msgnum = GPOINTER_TO_UINT(b);
916
917         if (!msginfo)
918                 return -1;
919
920         return msginfo->msgnum - msgnum;
921 }
922
923 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
924 {
925         const MsgInfo *msginfo1 = a;
926         const MsgInfo *msginfo2 = b;
927
928         if (!msginfo1)
929                 return -1;
930         if (!msginfo2)
931                 return -1;
932
933         return msginfo1->msgnum - msginfo2->msgnum;
934 }
935
936 static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
937 {
938         const FlagInfo *finfo = a;
939         const guint msgnum = GPOINTER_TO_UINT(b);
940
941         if (!finfo)
942                 return -1;
943
944         return finfo->msgnum - msgnum;
945 }
946
947 enum
948 {
949         Q_SENDER           = 0,
950         Q_SMTPSERVER       = 1,
951         Q_RECIPIENTS       = 2,
952         Q_NEWSGROUPS       = 3,
953         Q_MAIL_ACCOUNT_ID  = 4,
954         Q_NEWS_ACCOUNT_ID  = 5,
955         Q_SAVE_COPY_FOLDER = 6
956 };
957
958 gint procmsg_send_message_queue(const gchar *file)
959 {
960         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
961                                        {"SSV:",  NULL, FALSE},
962                                        {"R:",    NULL, FALSE},
963                                        {"NG:",   NULL, FALSE},
964                                        {"MAID:", NULL, FALSE},
965                                        {"NAID:", NULL, FALSE},
966                                        {"SCF:",  NULL, FALSE},
967                                        {NULL,    NULL, FALSE}};
968         FILE *fp;
969         gint filepos;
970         gint mailval = 0, newsval = 0;
971         gchar *from = NULL;
972         gchar *smtpserver = NULL;
973         GSList *to_list = NULL;
974         GSList *newsgroup_list = NULL;
975         gchar *savecopyfolder = NULL;
976         gchar buf[BUFFSIZE];
977         gint hnum;
978         PrefsAccount *mailac = NULL, *newsac = NULL;
979         gchar *tmp = NULL;
980
981         g_return_val_if_fail(file != NULL, -1);
982
983         if ((fp = fopen(file, "r")) == NULL) {
984                 FILE_OP_ERROR(file, "fopen");
985                 return -1;
986         }
987
988         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
989                != -1) {
990                 gchar *p = buf + strlen(qentry[hnum].name);
991
992                 switch (hnum) {
993                 case Q_SENDER:
994                         if (!from) from = g_strdup(p);
995                         break;
996                 case Q_SMTPSERVER:
997                         if (!smtpserver) smtpserver = g_strdup(p);
998                         break;
999                 case Q_RECIPIENTS:
1000                         to_list = address_list_append(to_list, p);
1001                         break;
1002                 case Q_NEWSGROUPS:
1003                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1004                         break;
1005                 case Q_MAIL_ACCOUNT_ID:
1006                         mailac = account_find_from_id(atoi(p));
1007                         break;
1008                 case Q_NEWS_ACCOUNT_ID:
1009                         newsac = account_find_from_id(atoi(p));
1010                         break;
1011                 case Q_SAVE_COPY_FOLDER:
1012                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1013                         break;
1014                 default:
1015                 }
1016         }
1017         filepos = ftell(fp);
1018
1019         if(newsgroup_list || prefs_common.savemsg) {
1020                 FILE *tmpfp;
1021
1022                 /* write to temporary file */
1023                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1024                             G_DIR_SEPARATOR, (gint)file);
1025                 if ((tmpfp = fopen(tmp, "w")) == NULL) {
1026                         FILE_OP_ERROR(tmp, "fopen");
1027                         newsval = -1;
1028                 }
1029                 if (change_file_mode_rw(tmpfp, tmp) < 0) {
1030                         FILE_OP_ERROR(tmp, "chmod");
1031                         g_warning(_("can't change file mode\n"));
1032                 }
1033
1034                 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1035                         if (fputs(buf, tmpfp) == EOF) {
1036                                 FILE_OP_ERROR(tmp, "fputs");
1037                                 newsval = -1;
1038                         }
1039                 }
1040                 fclose(tmpfp);
1041         }
1042
1043         fseek(fp, filepos, SEEK_SET);
1044         if (to_list) {
1045                 debug_print(_("Sending message by mail\n"));
1046                 if(!from) {
1047                         g_warning(_("Queued message header is broken.\n"));
1048                         mailval = -1;
1049                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1050                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1051                 } else {
1052                         if (!mailac) {
1053                                 mailac = account_find_from_smtp_server(from, smtpserver);
1054                                 if (!mailac) {
1055                                         g_warning(_("Account not found. "
1056                                                     "Using current account...\n"));
1057                                         mailac = cur_account;
1058                                 }
1059                         }
1060
1061                         if (mailac)
1062                                 mailval = send_message_smtp(mailac, to_list, fp);
1063                         else {
1064                                 PrefsAccount tmp_ac;
1065
1066                                 g_warning(_("Account not found.\n"));
1067
1068                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1069                                 tmp_ac.address = from;
1070                                 tmp_ac.smtp_server = smtpserver;
1071                                 tmp_ac.smtpport = SMTP_PORT;
1072                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1073                         }
1074                 }
1075                 if (mailval < 0) {
1076                         alertpanel_error(_("Error occurred while sending the message to %s ."),
1077                                  mailac ? mailac->smtp_server : smtpserver);
1078                 }
1079         }
1080
1081         if(newsgroup_list && (newsval == 0)) {
1082                 Folder *folder;
1083
1084                 debug_print(_("Sending message by news\n"));
1085
1086                 folder = FOLDER(newsac->folder);
1087
1088                 newsval = news_post(folder, tmp);
1089                 if (newsval < 0) {
1090                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1091                                  newsac->nntp_server);
1092                 }
1093         }
1094
1095         /* save message to outbox */
1096         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1097                 FolderItem *folder;
1098                 gchar *path;
1099                 gint num;
1100                 FILE *fp;
1101
1102                 debug_print(_("saving sent message...\n"));
1103
1104                 folder = folder_find_item_from_identifier(savecopyfolder);
1105                 if(!folder)
1106                         folder = folder_get_default_outbox();
1107                 path = folder_item_get_path(folder);
1108                 if (!is_dir_exist(path))
1109                         make_dir_hier(path);
1110
1111                 folder_item_scan(folder);
1112                 if ((num = folder_item_add_msg(folder, tmp, FALSE)) < 0) {
1113                         g_warning(_("can't save message\n"));
1114                 }
1115
1116                 if(num) {
1117                         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
1118                                 g_warning(_("can't open mark file\n"));
1119                         else {
1120                                 MsgInfo newmsginfo;
1121
1122                                 newmsginfo.msgnum = num;
1123                                 newmsginfo.flags.perm_flags = 0;
1124                                 newmsginfo.flags.tmp_flags = 0;
1125                                 procmsg_write_flags(&newmsginfo, fp);
1126                                 fclose(fp);
1127                         }
1128                 }
1129                 g_free(path);
1130         }
1131
1132         slist_free_strings(to_list);
1133         g_slist_free(to_list);
1134         slist_free_strings(newsgroup_list);
1135         g_slist_free(newsgroup_list);
1136         g_free(from);
1137         g_free(smtpserver);
1138         fclose(fp);
1139         if(tmp) {
1140                 unlink(tmp);
1141                 g_free(tmp);
1142         }
1143
1144         return (newsval != 0 ? newsval : mailval);
1145 }