2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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.
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.
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.
31 #include "procheader.h"
32 #include "send_message.h"
34 #include "statusbar.h"
35 #include "prefs_filtering.h"
36 #include "filtering.h"
38 #include "prefs_common.h"
43 #include "alertpanel.h"
48 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
50 GHashTable *msg_table;
52 if (mlist == NULL) return NULL;
54 msg_table = g_hash_table_new(NULL, g_direct_equal);
55 procmsg_msg_hash_table_append(msg_table, mlist);
60 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
65 if (msg_table == NULL || mlist == NULL) return;
67 for (cur = mlist; cur != NULL; cur = cur->next) {
68 msginfo = (MsgInfo *)cur->data;
70 g_hash_table_insert(msg_table,
71 GUINT_TO_POINTER(msginfo->msgnum),
76 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
78 GHashTable *msg_table;
82 if (mlist == NULL) return NULL;
84 msg_table = g_hash_table_new(NULL, g_direct_equal);
86 for (cur = mlist; cur != NULL; cur = cur->next) {
87 msginfo = (MsgInfo *)cur->data;
88 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
94 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
100 for (cur = mlist; cur != NULL; cur = cur->next) {
101 msginfo = (MsgInfo *)cur->data;
102 if (msginfo && msginfo->msgnum > last)
103 last = msginfo->msgnum;
109 void procmsg_msg_list_free(GSList *mlist)
114 for (cur = mlist; cur != NULL; cur = cur->next) {
115 msginfo = (MsgInfo *)cur->data;
116 procmsg_msginfo_free(msginfo);
130 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
132 MsgInfo *msginfo = (MsgInfo *)node->data;
134 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
135 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
140 /* return the reversed thread tree */
141 GNode *procmsg_get_thread_tree(GSList *mlist)
143 GNode *root, *parent, *node, *next;
144 GHashTable *msgid_table;
145 GHashTable *subject_table;
148 const gchar *subject;
149 GNode *found_subject;
151 root = g_node_new(NULL);
152 msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
153 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
155 for (; mlist != NULL; mlist = mlist->next) {
156 msginfo = (MsgInfo *)mlist->data;
159 if (msginfo->inreplyto) {
160 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
161 if (parent == NULL) {
164 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
165 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
166 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
170 node = g_node_insert_data_before
171 (parent, parent == root ? parent->children : NULL,
173 if ((msgid = msginfo->msgid) &&
174 g_hash_table_lookup(msgid_table, msgid) == NULL)
175 g_hash_table_insert(msgid_table, (gchar *)msgid, node);
177 if (prefs_common.thread_by_subject) {
178 subject = msginfo->subject;
179 subject += subject_get_reply_prefix_length(subject);
180 found_subject = subject_table_lookup_clean(subject_table,
182 if (found_subject == NULL)
183 subject_table_insert_clean(subject_table, (gchar *) subject,
186 /* replace if msg in table is older than current one
187 * can add here more stuff. */
188 if ( ((MsgInfo*)(found_subject->data))->date_t >
189 ((MsgInfo*)(node->data))->date_t ) {
190 subject_table_remove_clean(subject_table, (gchar *) subject);
191 subject_table_insert_clean(subject_table, (gchar *) subject, node);
197 /* complete the unfinished threads */
198 for (node = root->children; node != NULL; ) {
200 msginfo = (MsgInfo *)node->data;
202 if (msginfo->inreplyto)
203 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
204 if (parent && parent != node) {
207 (parent, parent->children, node);
208 /* CLAWS: ignore thread */
209 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
210 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
216 /* CLAWS: now see if the first level (below root) still has some nodes that can be
217 * threaded by subject line. we need to handle this in a special way to prevent
218 * circular reference from a node that has already been threaded by IN-REPLY-TO
219 * but is also in the subject line hash table */
220 if (prefs_common.thread_by_subject) {
221 for (node = root->children; node != NULL; ) {
223 msginfo = (MsgInfo *) node->data;
224 parent = subject_table_lookup(subject_table, msginfo->subject);
225 /* the node may already be threaded by IN-REPLY-TO,
226 so go up in the tree to find the parent node */
227 if (parent != NULL) {
228 if (g_node_is_ancestor(node, parent))
236 g_node_append(parent, node);
237 /* CLAWS: ignore thread */
238 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
239 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
246 g_hash_table_destroy(subject_table);
247 g_hash_table_destroy(msgid_table);
252 void procmsg_move_messages(GSList *mlist)
254 GSList *cur, *movelist = NULL;
256 FolderItem *dest = NULL;
260 folder_item_update_freeze();
262 for (cur = mlist; cur != NULL; cur = cur->next) {
263 msginfo = (MsgInfo *)cur->data;
265 dest = msginfo->to_folder;
266 movelist = g_slist_append(movelist, msginfo);
267 } else if (dest == msginfo->to_folder) {
268 movelist = g_slist_append(movelist, msginfo);
270 folder_item_move_msgs_with_dest(dest, movelist);
271 g_slist_free(movelist);
273 dest = msginfo->to_folder;
274 movelist = g_slist_append(movelist, msginfo);
276 procmsg_msginfo_set_to_folder(msginfo, NULL);
280 folder_item_move_msgs_with_dest(dest, movelist);
281 g_slist_free(movelist);
284 folder_item_update_thaw();
287 void procmsg_copy_messages(GSList *mlist)
289 GSList *cur, *copylist = NULL;
291 FolderItem *dest = NULL;
295 folder_item_update_freeze();
297 for (cur = mlist; cur != NULL; cur = cur->next) {
298 msginfo = (MsgInfo *)cur->data;
300 dest = msginfo->to_folder;
301 copylist = g_slist_append(copylist, msginfo);
302 } else if (dest == msginfo->to_folder) {
303 copylist = g_slist_append(copylist, msginfo);
305 folder_item_copy_msgs_with_dest(dest, copylist);
306 g_slist_free(copylist);
308 dest = msginfo->to_folder;
309 copylist = g_slist_append(copylist, msginfo);
311 procmsg_msginfo_set_to_folder(msginfo, NULL);
315 folder_item_copy_msgs_with_dest(dest, copylist);
316 g_slist_free(copylist);
319 folder_item_update_thaw();
322 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
326 g_return_val_if_fail(msginfo != NULL, NULL);
328 if (msginfo->plaintext_file)
329 file = g_strdup(msginfo->plaintext_file);
331 path = folder_item_get_path(msginfo->folder);
332 file = g_strconcat(path, G_DIR_SEPARATOR_S,
333 itos(msginfo->msgnum), NULL);
340 gchar *procmsg_get_message_file(MsgInfo *msginfo)
342 gchar *filename = NULL;
344 g_return_val_if_fail(msginfo != NULL, NULL);
346 filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
348 g_warning("can't fetch message %d\n", msginfo->msgnum);
353 FILE *procmsg_open_message(MsgInfo *msginfo)
358 g_return_val_if_fail(msginfo != NULL, NULL);
360 file = procmsg_get_message_file_path(msginfo);
361 g_return_val_if_fail(file != NULL, NULL);
363 if (!is_file_exist(file)) {
365 file = procmsg_get_message_file(msginfo);
366 g_return_val_if_fail(file != NULL, NULL);
369 if ((fp = fopen(file, "rb")) == NULL) {
370 FILE_OP_ERROR(file, "fopen");
377 if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
380 while (fgets(buf, sizeof(buf), fp) != NULL)
381 if (buf[0] == '\r' || buf[0] == '\n') break;
388 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
394 g_return_val_if_fail(msginfo != NULL, NULL);
396 if (mimeinfo) *mimeinfo = NULL;
398 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
400 mimeinfo_ = procmime_scan_mime_header(fp);
406 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
407 rfc2015_is_encrypted(mimeinfo_)) {
408 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
411 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
412 !msginfo->plaintext_file &&
413 !msginfo->decryption_failed) {
415 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
416 if (msginfo->plaintext_file &&
417 !msginfo->decryption_failed) {
419 procmime_mimeinfo_free_all(mimeinfo_);
420 if ((fp = procmsg_open_message(msginfo)) == NULL)
422 mimeinfo_ = procmime_scan_mime_header(fp);
428 if (fseek(fp, fpos, SEEK_SET) < 0)
433 if (mimeinfo) *mimeinfo = mimeinfo_;
438 gboolean procmsg_msg_exist(MsgInfo *msginfo)
443 if (!msginfo) return FALSE;
445 path = folder_item_get_path(msginfo->folder);
447 ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
453 void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
454 PrefsFilterType type)
456 static HeaderEntry hentry[] = {{"X-BeenThere:", NULL, TRUE},
457 {"X-ML-Name:", NULL, TRUE},
458 {"X-List:", NULL, TRUE},
459 {"X-Mailing-list:", NULL, TRUE},
460 {"List-Id:", NULL, TRUE},
461 {"X-Sequence:", NULL, TRUE},
462 {NULL, NULL, FALSE}};
468 H_X_MAILING_LIST = 3,
475 g_return_if_fail(msginfo != NULL);
476 g_return_if_fail(header != NULL);
477 g_return_if_fail(key != NULL);
486 if ((fp = procmsg_open_message(msginfo)) == NULL)
488 procheader_get_header_fields(fp, hentry);
491 #define SET_FILTER_KEY(hstr, idx) \
493 *header = g_strdup(hstr); \
494 *key = hentry[idx].body; \
495 hentry[idx].body = NULL; \
498 if (hentry[H_X_BEENTHERE].body != NULL) {
499 SET_FILTER_KEY("header \"X-BeenThere\"", H_X_BEENTHERE);
500 } else if (hentry[H_X_ML_NAME].body != NULL) {
501 SET_FILTER_KEY("header \"X-ML-Name\"", H_X_ML_NAME);
502 } else if (hentry[H_X_LIST].body != NULL) {
503 SET_FILTER_KEY("header \"X-List\"", H_X_LIST);
504 } else if (hentry[H_X_MAILING_LIST].body != NULL) {
505 SET_FILTER_KEY("header \"X-Mailing-List\"", H_X_MAILING_LIST);
506 } else if (hentry[H_LIST_ID].body != NULL) {
507 SET_FILTER_KEY("header \"List-Id\"", H_LIST_ID);
508 extract_list_id_str(*key);
509 } else if (hentry[H_X_SEQUENCE].body != NULL) {
512 SET_FILTER_KEY("X-Sequence", H_X_SEQUENCE);
515 while (*p != '\0' && !isspace(*p)) p++;
516 while (isspace(*p)) p++;
523 } else if (msginfo->subject) {
524 *header = g_strdup("subject");
525 *key = g_strdup(msginfo->subject);
528 #undef SET_FILTER_KEY
530 g_free(hentry[H_X_BEENTHERE].body);
531 hentry[H_X_BEENTHERE].body = NULL;
532 g_free(hentry[H_X_ML_NAME].body);
533 hentry[H_X_ML_NAME].body = NULL;
534 g_free(hentry[H_X_LIST].body);
535 hentry[H_X_LIST].body = NULL;
536 g_free(hentry[H_X_MAILING_LIST].body);
537 hentry[H_X_MAILING_LIST].body = NULL;
538 g_free(hentry[H_LIST_ID].body);
539 hentry[H_LIST_ID].body = NULL;
543 *header = g_strdup("from");
544 *key = g_strdup(msginfo->from);
547 *header = g_strdup("to");
548 *key = g_strdup(msginfo->to);
550 case FILTER_BY_SUBJECT:
551 *header = g_strdup("subject");
552 *key = g_strdup(msginfo->subject);
559 void procmsg_empty_trash(void)
564 for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
565 trash = FOLDER(cur->data)->trash;
566 if (trash && trash->total_msgs > 0)
567 folder_item_remove_all_msg(trash);
572 *\brief Send messages in queue
574 *\param queue Queue folder to process
575 *\param save_msgs Unused
577 *\return Number of messages sent, negative if an error occurred
578 * positive if no error occurred
580 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
582 gint ret = 1, count = 0;
586 queue = folder_get_default_queue();
587 g_return_val_if_fail(queue != NULL, -1);
589 folder_item_scan(queue);
590 list = folder_item_get_msg_list(queue);
592 for (elem = list; elem != NULL; elem = elem->next) {
596 msginfo = (MsgInfo *)(elem->data);
597 if (!MSG_IS_LOCKED(msginfo->flags)) {
598 file = folder_item_fetch_msg(queue, msginfo->msgnum);
600 if (procmsg_send_message_queue(file) < 0) {
601 g_warning("Sending queued message %d failed.\n",
606 * We save in procmsg_send_message_queue because
607 * we need the destination folder from the queue
611 procmsg_save_to_outbox
612 (queue->folder->outbox,
616 folder_item_remove_msg(queue, msginfo->msgnum);
621 /* FIXME: supposedly if only one message is locked, and queue
622 * is being flushed, the following free says something like
623 * "freeing msg ## in folder (nil)". */
624 procmsg_msginfo_free(msginfo);
630 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
635 if ((fp = fopen(in, "rb")) == NULL) {
636 FILE_OP_ERROR(in, "fopen");
639 if ((outfp = fopen(out, "wb")) == NULL) {
640 FILE_OP_ERROR(out, "fopen");
644 while (fgets(buf, sizeof(buf), fp) != NULL)
645 if (buf[0] == '\r' || buf[0] == '\n') break;
646 while (fgets(buf, sizeof(buf), fp) != NULL)
653 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
659 debug_print("saving sent message...\n");
662 outbox = folder_get_default_outbox();
663 g_return_val_if_fail(outbox != NULL, -1);
665 /* remove queueing headers */
667 gchar tmp[MAXPATHLEN + 1];
669 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
670 get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
672 if (procmsg_remove_special_headers(file, tmp) !=0)
675 folder_item_scan(outbox);
676 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
677 g_warning("can't save message\n");
682 folder_item_scan(outbox);
683 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
684 g_warning("can't save message\n");
689 msginfo = folder_item_get_msginfo(outbox, num);
690 if (msginfo != NULL) {
691 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
692 procmsg_msginfo_free(msginfo);
694 folder_item_update(outbox, TRUE);
699 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
701 static const gchar *def_cmd = "lpr %s";
708 g_return_if_fail(msginfo);
710 if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
711 g_warning("Can't get text part\n");
715 prtmp = g_strdup_printf("%s%cprinttmp.%08x",
716 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
718 if ((prfp = fopen(prtmp, "wb")) == NULL) {
719 FILE_OP_ERROR(prtmp, "fopen");
725 if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
726 if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
727 if (msginfo->to) fprintf(prfp, "To: %s\n", msginfo->to);
728 if (msginfo->cc) fprintf(prfp, "Cc: %s\n", msginfo->cc);
729 if (msginfo->newsgroups)
730 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
731 if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
734 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
740 if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
742 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
745 g_warning("Print command line is invalid: `%s'\n",
747 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
753 if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
757 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
764 MsgInfo *procmsg_msginfo_new(void)
768 newmsginfo = g_new0(MsgInfo, 1);
769 newmsginfo->refcnt = 1;
774 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
778 if (msginfo == NULL) return NULL;
780 newmsginfo = g_new0(MsgInfo, 1);
782 newmsginfo->refcnt = 1;
784 #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb
785 #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \
786 g_strdup(msginfo->mmb) : NULL
810 MEMBDUP(dispositionnotificationto);
811 MEMBDUP(returnreceiptto);
815 MEMBCOPY(threadscore);
820 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
822 MsgInfo *full_msginfo;
825 if (msginfo == NULL) return NULL;
827 file = procmsg_get_message_file(msginfo);
829 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
833 full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
835 if (!full_msginfo) return NULL;
837 full_msginfo->msgnum = msginfo->msgnum;
838 full_msginfo->size = msginfo->size;
839 full_msginfo->mtime = msginfo->mtime;
840 full_msginfo->folder = msginfo->folder;
842 full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
843 full_msginfo->decryption_failed = msginfo->decryption_failed;
845 procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
850 void procmsg_msginfo_free(MsgInfo *msginfo)
852 if (msginfo == NULL) return;
855 if (msginfo->refcnt > 0)
858 debug_print("freeing msginfo %d in %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
860 if (msginfo->to_folder) {
861 msginfo->to_folder->op_count--;
862 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
865 g_free(msginfo->fromspace);
866 g_free(msginfo->references);
867 g_free(msginfo->returnreceiptto);
868 g_free(msginfo->dispositionnotificationto);
869 g_free(msginfo->xface);
871 g_free(msginfo->fromname);
873 g_free(msginfo->date);
874 g_free(msginfo->from);
877 g_free(msginfo->newsgroups);
878 g_free(msginfo->subject);
879 g_free(msginfo->msgid);
880 g_free(msginfo->inreplyto);
881 g_free(msginfo->xref);
886 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
890 memusage += sizeof(MsgInfo);
891 if (msginfo->fromname)
892 memusage += strlen(msginfo->fromname);
894 memusage += strlen(msginfo->date);
896 memusage += strlen(msginfo->from);
898 memusage += strlen(msginfo->to);
900 memusage += strlen(msginfo->cc);
901 if (msginfo->newsgroups)
902 memusage += strlen(msginfo->newsgroups);
903 if (msginfo->subject)
904 memusage += strlen(msginfo->subject);
906 memusage += strlen(msginfo->msgid);
907 if (msginfo->inreplyto)
908 memusage += strlen(msginfo->inreplyto);
910 memusage += strlen(msginfo->xface);
911 if (msginfo->dispositionnotificationto)
912 memusage += strlen(msginfo->dispositionnotificationto);
913 if (msginfo->returnreceiptto)
914 memusage += strlen(msginfo->returnreceiptto);
915 if (msginfo->references)
916 memusage += strlen(msginfo->references);
917 if (msginfo->fromspace)
918 memusage += strlen(msginfo->fromspace);
923 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
925 const MsgInfo *msginfo1 = a;
926 const MsgInfo *msginfo2 = b;
933 return msginfo1->msgnum - msginfo2->msgnum;
942 Q_MAIL_ACCOUNT_ID = 4,
943 Q_NEWS_ACCOUNT_ID = 5,
944 Q_SAVE_COPY_FOLDER = 6,
945 Q_REPLY_MESSAGE_ID = 7,
949 gint procmsg_send_message_queue(const gchar *file)
951 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
952 {"SSV:", NULL, FALSE},
954 {"NG:", NULL, FALSE},
955 {"MAID:", NULL, FALSE},
956 {"NAID:", NULL, FALSE},
957 {"SCF:", NULL, FALSE},
958 {"RMID:", NULL, FALSE},
959 {"FMID:", NULL, FALSE},
960 {NULL, NULL, FALSE}};
963 gint mailval = 0, newsval = 0;
965 gchar *smtpserver = NULL;
966 GSList *to_list = NULL;
967 GSList *newsgroup_list = NULL;
968 gchar *savecopyfolder = NULL;
969 gchar *replymessageid = NULL;
970 gchar *fwdmessageid = NULL;
973 PrefsAccount *mailac = NULL, *newsac = NULL;
976 g_return_val_if_fail(file != NULL, -1);
978 if ((fp = fopen(file, "rb")) == NULL) {
979 FILE_OP_ERROR(file, "fopen");
983 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
985 gchar *p = buf + strlen(qentry[hnum].name);
989 if (!from) from = g_strdup(p);
992 if (!smtpserver) smtpserver = g_strdup(p);
995 to_list = address_list_append(to_list, p);
998 newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1000 case Q_MAIL_ACCOUNT_ID:
1001 mailac = account_find_from_id(atoi(p));
1003 case Q_NEWS_ACCOUNT_ID:
1004 newsac = account_find_from_id(atoi(p));
1006 case Q_SAVE_COPY_FOLDER:
1007 if (!savecopyfolder) savecopyfolder = g_strdup(p);
1009 case Q_REPLY_MESSAGE_ID:
1010 if (!replymessageid) replymessageid = g_strdup(p);
1012 case Q_FWD_MESSAGE_ID:
1013 if (!fwdmessageid) fwdmessageid = g_strdup(p);
1017 filepos = ftell(fp);
1020 debug_print("Sending message by mail\n");
1022 g_warning("Queued message header is broken.\n");
1024 } else if (mailac && mailac->use_mail_command &&
1025 mailac->mail_command && (* mailac->mail_command)) {
1026 mailval = send_message_local(mailac->mail_command, fp);
1028 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1029 mailval = send_message_local(prefs_common.extsend_cmd, fp);
1033 mailac = account_find_from_smtp_server(from, smtpserver);
1035 g_warning("Account not found. "
1036 "Using current account...\n");
1037 mailac = cur_account;
1042 mailval = send_message_smtp(mailac, to_list, fp);
1044 PrefsAccount tmp_ac;
1046 g_warning("Account not found.\n");
1048 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1049 tmp_ac.address = from;
1050 tmp_ac.smtp_server = smtpserver;
1051 tmp_ac.smtpport = SMTP_PORT;
1052 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1057 fseek(fp, filepos, SEEK_SET);
1058 if (newsgroup_list && (newsval == 0)) {
1063 /* write to temporary file */
1064 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1065 G_DIR_SEPARATOR, (gint)file);
1066 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1067 FILE_OP_ERROR(tmp, "fopen");
1069 alertpanel_error(_("Could not create temporary file for news sending."));
1071 if (change_file_mode_rw(tmpfp, tmp) < 0) {
1072 FILE_OP_ERROR(tmp, "chmod");
1073 g_warning("can't change file mode\n");
1076 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1077 if (fputs(buf, tmpfp) == EOF) {
1078 FILE_OP_ERROR(tmp, "fputs");
1080 alertpanel_error(_("Error when writing temporary file for news sending."));
1086 debug_print("Sending message by news\n");
1088 folder = FOLDER(newsac->folder);
1090 newsval = news_post(folder, tmp);
1092 alertpanel_error(_("Error occurred while posting the message to %s ."),
1093 newsac->nntp_server);
1101 slist_free_strings(to_list);
1102 g_slist_free(to_list);
1103 slist_free_strings(newsgroup_list);
1104 g_slist_free(newsgroup_list);
1109 /* save message to outbox */
1110 if (mailval == 0 && newsval == 0 && savecopyfolder) {
1113 debug_print("saving sent message...\n");
1115 outbox = folder_find_item_from_identifier(savecopyfolder);
1117 outbox = folder_get_default_outbox();
1119 procmsg_save_to_outbox(outbox, file, TRUE);
1122 if (replymessageid != NULL || fwdmessageid != NULL) {
1126 if (replymessageid != NULL)
1127 tokens = g_strsplit(replymessageid, "\x7f", 0);
1129 tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1130 item = folder_find_item_from_identifier(tokens[0]);
1134 msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1136 /*!< note that if the message has no msgid (maybe it was invalid),
1137 * we also refuse to do something with the reply to flag */
1138 if ((msginfo != NULL) &&
1139 (msginfo->msgid != NULL) &&
1140 (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1141 procmsg_msginfo_free(msginfo);
1145 if (msginfo == NULL) {
1146 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1149 if (msginfo != NULL) {
1150 if (replymessageid != NULL) {
1151 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1152 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1155 procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1156 procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1158 procmsg_msginfo_free(msginfo);
1164 g_free(savecopyfolder);
1165 g_free(replymessageid);
1166 g_free(fwdmessageid);
1168 return (newsval != 0 ? newsval : mailval);
1171 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
1173 MsgPermFlags new_flags = msginfo->flags.perm_flags;
1176 if (!(old_flags & MSG_NEW) && (new_flags & MSG_NEW)) {
1180 if ((old_flags & MSG_NEW) && !(new_flags & MSG_NEW)) {
1185 if (!(old_flags & MSG_UNREAD) && (new_flags & MSG_UNREAD)) {
1186 item->unread_msgs++;
1187 if (procmsg_msg_has_marked_parent(msginfo))
1188 item->unreadmarked_msgs++;
1191 if ((old_flags & MSG_UNREAD) && !(new_flags & MSG_UNREAD)) {
1192 item->unread_msgs--;
1193 if (procmsg_msg_has_marked_parent(msginfo))
1194 item->unreadmarked_msgs--;
1198 if (!(old_flags & MSG_MARKED) && (new_flags & MSG_MARKED)) {
1199 procmsg_update_unread_children(msginfo, TRUE);
1202 if ((old_flags & MSG_MARKED) && !(new_flags & MSG_MARKED)) {
1203 procmsg_update_unread_children(msginfo, FALSE);
1207 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1210 MsgInfoUpdate msginfo_update;
1211 MsgPermFlags perm_flags_new, perm_flags_old;
1213 g_return_if_fail(msginfo != NULL);
1214 item = msginfo->folder;
1215 g_return_if_fail(item != NULL);
1217 debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1219 /* Perm Flags handling */
1220 perm_flags_old = msginfo->flags.perm_flags;
1221 perm_flags_new = msginfo->flags.perm_flags | perm_flags;
1222 if ((perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
1223 perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
1226 if (perm_flags_old != perm_flags_new) {
1227 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1229 update_folder_msg_counts(item, msginfo, perm_flags_old);
1231 msginfo_update.msginfo = msginfo;
1232 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1233 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1236 /* Tmp flags hanlding */
1237 msginfo->flags.tmp_flags |= tmp_flags;
1240 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1243 MsgInfoUpdate msginfo_update;
1244 MsgPermFlags perm_flags_new, perm_flags_old;
1246 g_return_if_fail(msginfo != NULL);
1247 item = msginfo->folder;
1248 g_return_if_fail(item != NULL);
1250 debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1252 /* Perm Flags handling */
1253 perm_flags_old = msginfo->flags.perm_flags;
1254 perm_flags_new = msginfo->flags.perm_flags & ~perm_flags;
1256 if (perm_flags_old != perm_flags_new) {
1257 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1259 update_folder_msg_counts(item, msginfo, perm_flags_old);
1261 msginfo_update.msginfo = msginfo;
1262 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1263 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1266 /* Tmp flags hanlding */
1267 msginfo->flags.tmp_flags &= ~tmp_flags;
1271 *\brief check for flags (e.g. mark) in prior msgs of current thread
1273 *\param info Current message
1274 *\param perm_flags Flags to be checked
1275 *\param parentmsgs Hash of prior msgs to avoid loops
1277 *\return gboolean TRUE if perm_flags are found
1279 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1280 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1284 g_return_val_if_fail(info != NULL, FALSE);
1286 if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1287 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1289 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1290 procmsg_msginfo_free(tmp);
1292 } else if (tmp != NULL) {
1295 if (g_hash_table_lookup(parentmsgs, info)) {
1296 debug_print("loop detected: %s%c%d\n",
1297 folder_item_get_path(info->folder),
1298 G_DIR_SEPARATOR, info->msgnum);
1301 g_hash_table_insert(parentmsgs, info, "1");
1302 result = procmsg_msg_has_flagged_parent_real(
1303 tmp, perm_flags, parentmsgs);
1305 procmsg_msginfo_free(tmp);
1315 *\brief Callback for cleaning up hash of parentmsgs
1317 gboolean parentmsgs_hash_remove(gpointer key,
1325 *\brief Set up list of parentmsgs
1326 * See procmsg_msg_has_flagged_parent_real()
1328 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1331 GHashTable *parentmsgs = g_hash_table_new(NULL, NULL);
1333 result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1334 g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1335 g_hash_table_destroy(parentmsgs);
1340 *\brief Check if msgs prior in thread are marked
1341 * See procmsg_msg_has_flagged_parent_real()
1343 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1345 return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1349 GSList *procmsg_find_children_func(MsgInfo *info,
1350 GSList *children, GSList *all)
1354 g_return_val_if_fail(info!=NULL, children);
1355 if (info->msgid == NULL)
1358 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1359 MsgInfo *tmp = (MsgInfo *)cur->data;
1360 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1361 /* Check if message is already in the list */
1362 if ((children == NULL) ||
1363 (g_slist_index(children, tmp) == -1)) {
1364 children = g_slist_prepend(children,
1365 procmsg_msginfo_new_ref(tmp));
1366 children = procmsg_find_children_func(tmp,
1375 GSList *procmsg_find_children (MsgInfo *info)
1380 g_return_val_if_fail(info!=NULL, NULL);
1381 all = folder_item_get_msg_list(info->folder);
1382 children = procmsg_find_children_func(info, NULL, all);
1383 if (children != NULL) {
1384 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1385 /* this will not free the used pointers
1386 created with procmsg_msginfo_new_ref */
1387 procmsg_msginfo_free((MsgInfo *)cur->data);
1395 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1397 GSList *children = procmsg_find_children(info);
1399 for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1400 MsgInfo *tmp = (MsgInfo *)cur->data;
1401 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1403 info->folder->unreadmarked_msgs++;
1405 info->folder->unreadmarked_msgs--;
1406 folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1408 procmsg_msginfo_free(tmp);
1410 g_slist_free(children);
1414 * Set the destination folder for a copy or move operation
1416 * \param msginfo The message which's destination folder is changed
1417 * \param to_folder The destination folder for the operation
1419 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1421 if(msginfo->to_folder != NULL) {
1422 msginfo->to_folder->op_count--;
1423 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1425 msginfo->to_folder = to_folder;
1426 if(to_folder != NULL) {
1427 to_folder->op_count++;
1428 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1433 * Apply filtering actions to the msginfo
1435 * \param msginfo The MsgInfo describing the message that should be filtered
1436 * \return TRUE if the message was moved and MsgInfo is now invalid,
1439 gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
1441 MailFilteringData mail_filtering_data;
1443 mail_filtering_data.msginfo = msginfo;
1444 if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
1447 /* filter if enabled in prefs or move to inbox if not */
1448 if((global_processing != NULL) &&
1449 filter_message_by_msginfo(global_processing, msginfo))