2006-05-15 [colin] 2.2.0cvs18
[claws.git] / src / procmsg.c
index 788bda55988c076a9dc7a97037365ccc0e47dc73..40d247a16cdf715f5e5ec55159170f6c2372e254 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include "defs.h"
@@ -42,6 +42,8 @@
 #include "hooks.h"
 #include "msgcache.h"
 #include "partial_download.h"
+#include "mainwindow.h"
+#include "summaryview.h"
 
 static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session);
 
@@ -332,39 +334,48 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
        return root;
 }
 
-void procmsg_move_messages(GSList *mlist)
+gint procmsg_move_messages(GSList *mlist)
 {
        GSList *cur, *movelist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-
-       if (!mlist) return;
+       gint retval = 0;
+       gboolean finished = TRUE;
+       if (!mlist) return 0;
 
        folder_item_update_freeze();
 
+next_folder:
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
+               if (!msginfo->to_folder) {
+                       continue;
+               } else {
+                       finished = FALSE;
+               }
                if (!dest) {
                        dest = msginfo->to_folder;
                        movelist = g_slist_append(movelist, msginfo);
                } else if (dest == msginfo->to_folder) {
                        movelist = g_slist_append(movelist, msginfo);
                } else {
-                       folder_item_move_msgs(dest, movelist);
-                       g_slist_free(movelist);
-                       movelist = NULL;
-                       dest = msginfo->to_folder;
-                       movelist = g_slist_append(movelist, msginfo);
+                       continue;
                }
                procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
-
        if (movelist) {
-               folder_item_move_msgs(dest, movelist);
+               retval |= folder_item_move_msgs(dest, movelist);
                g_slist_free(movelist);
+               movelist = NULL;
+       }
+       if (finished == FALSE) {
+               finished = TRUE;
+               dest = NULL;
+               goto next_folder;
        }
 
        folder_item_update_thaw();
+       return retval;
 }
 
 void procmsg_copy_messages(GSList *mlist)
@@ -372,31 +383,38 @@ void procmsg_copy_messages(GSList *mlist)
        GSList *cur, *copylist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-
+       gboolean finished = TRUE;
        if (!mlist) return;
 
        folder_item_update_freeze();
 
+next_folder:
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
+               if (!msginfo->to_folder) {
+                       continue;
+               } else {
+                       finished = FALSE;
+               }
                if (!dest) {
                        dest = msginfo->to_folder;
                        copylist = g_slist_append(copylist, msginfo);
                } else if (dest == msginfo->to_folder) {
                        copylist = g_slist_append(copylist, msginfo);
                } else {
-                       folder_item_copy_msgs(dest, copylist);
-                       g_slist_free(copylist);
-                       copylist = NULL;
-                       dest = msginfo->to_folder;
-                       copylist = g_slist_append(copylist, msginfo);
+                       continue;
                }
                procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
-
        if (copylist) {
                folder_item_copy_msgs(dest, copylist);
                g_slist_free(copylist);
+               copylist = NULL;
+       }
+       if (finished == FALSE) {
+               finished = TRUE;
+               dest = NULL;
+               goto next_folder;
        }
 
        folder_item_update_thaw();
@@ -547,6 +565,8 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                                       {"X-Mailing-list:", NULL, TRUE},
                                       {"List-Id:",        NULL, TRUE},
                                       {"X-Sequence:",     NULL, TRUE},
+                                      {"Sender:",         NULL, TRUE},
+                                      {"List-Post:",      NULL, TRUE},
                                       {NULL,              NULL, FALSE}};
        enum
        {
@@ -555,7 +575,9 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                H_X_LIST         = 2,
                H_X_MAILING_LIST = 3,
                H_LIST_ID        = 4,
-               H_X_SEQUENCE     = 5
+               H_X_SEQUENCE     = 5,
+               H_SENDER         = 6,
+               H_LIST_POST      = 7
        };
 
        FILE *fp;
@@ -608,6 +630,13 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                                }
                        }
                        g_strstrip(*key);
+               } else if (hentry[H_SENDER].body != NULL) {
+                       SET_FILTER_KEY("header \"Sender\"", H_SENDER);
+               } else if (hentry[H_LIST_POST].body != NULL) {
+                       SET_FILTER_KEY("header \"List-Post\"", H_LIST_POST);
+               } else if (msginfo->to) {
+                       *header = g_strdup("to");
+                       *key = g_strdup(msginfo->to);
                } else if (msginfo->subject) {
                        *header = g_strdup("subject");
                        *key = g_strdup(msginfo->subject);
@@ -625,6 +654,10 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                hentry[H_X_MAILING_LIST].body = NULL;
                g_free(hentry[H_LIST_ID].body);
                hentry[H_LIST_ID].body = NULL;
+               g_free(hentry[H_SENDER].body);
+               hentry[H_SENDER].body = NULL;
+               g_free(hentry[H_LIST_POST].body);
+               hentry[H_LIST_POST].body = NULL;
 
                break;
        case FILTER_BY_FROM:
@@ -646,6 +679,13 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
 
 void procmsg_empty_trash(FolderItem *trash)
 {
+       GNode *node, *next;
+
+       if (!trash || 
+           (trash->stype != F_TRASH && 
+            !folder_has_parent_of_type(trash, F_TRASH)))
+               return;
+
        if (trash && trash->total_msgs > 0) {
                GSList *mlist = folder_item_get_msg_list(trash);
                GSList *cur;
@@ -662,6 +702,16 @@ void procmsg_empty_trash(FolderItem *trash)
                g_slist_free(mlist);
                folder_item_remove_all_msg(trash);
        }
+
+       if (!trash->node || !trash->node->children)
+               return;
+
+       node = trash->node->children;
+       while (node != NULL) {
+               next = node->next;
+               procmsg_empty_trash(FOLDER_ITEM(node->data));
+               node = next;
+       }
 }
 
 void procmsg_empty_all_trash(void)
@@ -746,9 +796,10 @@ parse_again:
        cur = orig;
        while (cur) {
                gchar *file = NULL;
-               PrefsAccount *ac = procmsg_get_account_from_file(file);
+               PrefsAccount *ac = NULL;
                msg = (MsgInfo *)cur->data;
                file = folder_item_fetch_msg(queue, msg->msgnum);
+               ac = procmsg_get_account_from_file(file);
                g_free(file);
 
                if (last_account == NULL || (ac != NULL && ac == last_account)) {
@@ -821,6 +872,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        gint sent = 0, err = 0;
        GSList *list, *elem;
        GSList *sorted_list = NULL;
+       GNode *node, *next;
 
        if (!queue)
                queue = folder_get_default_queue();
@@ -869,6 +921,21 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        }
 
        g_slist_free(sorted_list);
+       folder_item_scan(queue);
+
+       if (queue->node && queue->node->children) {
+               node = queue->node->children;
+               while (node != NULL) {
+                       int res = 0;
+                       next = node->next;
+                       res = procmsg_send_queue(FOLDER_ITEM(node->data), save_msgs);
+                       if (res < 0) 
+                               err = -res;
+                       else
+                               sent += res;
+                       node = next;
+               }
+       }
 
        return (err != 0 ? -err : sent);
 }
@@ -892,6 +959,19 @@ gboolean procmsg_queue_is_empty(FolderItem *queue)
        list = folder_item_get_msg_list(queue);
        res = (list == NULL);
        procmsg_msg_list_free(list);
+
+       if (res == TRUE) {
+               GNode *node, *next;
+               if (queue->node && queue->node->children) {
+                       node = queue->node->children;
+                       while (node != NULL) {
+                               next = node->next;
+                               if (!procmsg_queue_is_empty(FOLDER_ITEM(node->data)))
+                                       return FALSE;
+                               node = next;
+                       }
+               }
+       }
        return res;
 }
 
@@ -916,8 +996,8 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
        fclose(outfp);
        fclose(fp);
        return 0;
-
 }
+
 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                            gboolean is_queued)
 {
@@ -1088,6 +1168,7 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        MEMBCOPY(folder);
        MEMBCOPY(to_folder);
 
+       MEMBDUP(face);
        MEMBDUP(xface);
        MEMBDUP(dispositionnotificationto);
        MEMBDUP(returnreceiptto);
@@ -1100,7 +1181,6 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
         newmsginfo->references = g_slist_reverse(newmsginfo->references);
 
        MEMBCOPY(score);
-       MEMBCOPY(threadscore);
        MEMBDUP(plaintext_file);
 
        return newmsginfo;
@@ -1129,8 +1209,22 @@ MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
 
        /* CLAWS: make sure we add the missing members; see: 
         * procheader.c::procheader_get_headernames() */
+       if (!msginfo->list_post)
+               msginfo->list_post = g_strdup(full_msginfo->list_post);
+       if (!msginfo->list_subscribe)
+               msginfo->list_subscribe = g_strdup(full_msginfo->list_subscribe);
+       if (!msginfo->list_unsubscribe)
+               msginfo->list_unsubscribe = g_strdup(full_msginfo->list_unsubscribe);
+       if (!msginfo->list_help)
+               msginfo->list_help = g_strdup(full_msginfo->list_help);
+       if (!msginfo->list_archive)
+               msginfo->list_archive= g_strdup(full_msginfo->list_archive);
+       if (!msginfo->list_owner)
+               msginfo->list_owner = g_strdup(full_msginfo->list_owner);
        if (!msginfo->xface)
                msginfo->xface = g_strdup(full_msginfo->xface);
+       if (!msginfo->face)
+               msginfo->face = g_strdup(full_msginfo->face);
        if (!msginfo->dispositionnotificationto)
                msginfo->dispositionnotificationto = 
                        g_strdup(full_msginfo->dispositionnotificationto);
@@ -1170,6 +1264,7 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        g_free(msginfo->returnreceiptto);
        g_free(msginfo->dispositionnotificationto);
        g_free(msginfo->xface);
+       g_free(msginfo->face);
 
        g_free(msginfo->fromname);
 
@@ -1183,6 +1278,13 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        g_free(msginfo->inreplyto);
        g_free(msginfo->xref);
 
+       g_free(msginfo->list_post);
+       g_free(msginfo->list_subscribe);
+       g_free(msginfo->list_unsubscribe);
+       g_free(msginfo->list_help);
+       g_free(msginfo->list_archive);
+       g_free(msginfo->list_owner);
+
        g_free(msginfo->partial_recv);
        g_free(msginfo->account_server);
        g_free(msginfo->account_login);
@@ -1221,6 +1323,8 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
                memusage += strlen(msginfo->inreplyto);
        if (msginfo->xface)
                memusage += strlen(msginfo->xface);
+       if (msginfo->face)
+               memusage += strlen(msginfo->face);
        if (msginfo->dispositionnotificationto)
                memusage += strlen(msginfo->dispositionnotificationto);
        if (msginfo->returnreceiptto)
@@ -1347,7 +1451,11 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
        if (encrypt) {
                MimeInfo *mimeinfo;
 
-               save_clear_text = (mailac != NULL && mailac->save_encrypted_as_clear_text);
+               if (mailac && mailac->save_encrypted_as_clear_text 
+               &&  !mailac->encrypt_to_self)
+                       save_clear_text = TRUE;
+               else
+                       save_clear_text = FALSE;
 
                fclose(fp);
                fp = NULL;
@@ -1428,7 +1536,9 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
                                mailval = send_message_smtp(&tmp_ac, to_list, fp);
                        }
                }
-       }
+       } else if (!to_list && !newsgroup_list) 
+               mailval = -1;
+
 
        fseek(fp, filepos, SEEK_SET);
        if (newsgroup_list && (mailval == 0)) {
@@ -1504,9 +1614,9 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
                FolderItem *item;
                
                if (replymessageid != NULL)
-                       tokens = g_strsplit(replymessageid, "\x7f", 0);
+                       tokens = g_strsplit(replymessageid, "\t", 0);
                else
-                       tokens = g_strsplit(fwdmessageid, "\x7f", 0);
+                       tokens = g_strsplit(fwdmessageid, "\t", 0);
                item = folder_find_item_from_identifier(tokens[0]);
 
                /* check if queued message has valid folder and message id */
@@ -1897,16 +2007,16 @@ gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
        MailFilteringData mail_filtering_data;
                        
        mail_filtering_data.msginfo = msginfo;                  
-       if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
+       if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data)) {
                return TRUE;
+       }
 
        /* filter if enabled in prefs or move to inbox if not */
        if((filtering_rules != NULL) &&
-          filter_message_by_msginfo(filtering_rules, msginfo))
+          filter_message_by_msginfo(filtering_rules, msginfo)) {
                return TRUE;
+       }
                
-       hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
-
        return FALSE;
 }
 
@@ -1914,53 +2024,94 @@ MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimei
 {
        MsgInfo *tmp_msginfo = NULL;
        MsgFlags flags = {0, 0};
-       
+       gchar *tmpfile = get_tmp_file();
+       FILE *fp = g_fopen(tmpfile, "wb");
        
        if (!mimeinfo || mimeinfo->type != MIMETYPE_MESSAGE ||
            g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                g_warning("procmsg_msginfo_new_from_mimeinfo(): unsuitable mimeinfo");
+               if (fp) 
+                       fclose(fp);
+               g_free(tmpfile);
                return NULL;
        }
        
-       
-       if (mimeinfo->content == MIMECONTENT_MEM) {
-               gchar *tmpfile = get_tmp_file();
-               str_write_to_file(mimeinfo->data.mem, tmpfile);
-               g_free(mimeinfo->data.mem);
-               mimeinfo->content = MIMECONTENT_FILE;
-               mimeinfo->data.filename = g_strdup(tmpfile);
-               g_free(tmpfile);
-               tmp_msginfo = procheader_parse_file(mimeinfo->data.filename,
-                                       flags, TRUE, FALSE);
-               if (tmp_msginfo != NULL) {
-                       tmp_msginfo->folder = src_msginfo->folder;
-                       tmp_msginfo->plaintext_file = g_strdup(mimeinfo->data.filename);
-               } else {
-                       g_warning("procmsg_msginfo_new_from_mimeinfo(): Can't generate new msginfo");
-               }
-       } else {
-               gchar *tmpfile = get_tmp_file();
-               FILE *fp = g_fopen(tmpfile, "wb");
-               if (fp && procmime_write_mimeinfo(mimeinfo, fp) >= 0) {
-                       if (fp)
-                               fclose(fp);
-                       fp = NULL;
-                       tmp_msginfo = procheader_parse_file(
-                               tmpfile, flags, 
-                               TRUE, FALSE);
-               }
-               if (fp)
-                       fclose(fp);
+       if (fp && procmime_write_mimeinfo(mimeinfo, fp) >= 0) {
+               fclose(fp);
+               fp = NULL;
+               tmp_msginfo = procheader_parse_file(
+                       tmpfile, flags, 
+                       TRUE, FALSE);
+       }
+       if (fp)
+               fclose(fp);
 
-               if (tmp_msginfo != NULL) {
+       if (tmp_msginfo != NULL) {
+               if (src_msginfo)
                        tmp_msginfo->folder = src_msginfo->folder;
-                       tmp_msginfo->plaintext_file = g_strdup(tmpfile);
-               } else {
-                       g_warning("procmsg_msginfo_new_from_mimeinfo(): Can't generate new msginfo");
-               }
-               g_free(tmpfile);
-               
+               tmp_msginfo->plaintext_file = g_strdup(tmpfile);
+       } else {
+               g_warning("procmsg_msginfo_new_from_mimeinfo(): Can't generate new msginfo");
        }
-       
+
+       g_free(tmpfile);
+
        return tmp_msginfo;
 }
+
+static GSList *spam_learners = NULL;
+
+void procmsg_register_spam_learner (int (*learn_func)(MsgInfo *info, GSList *list, gboolean spam))
+{
+       if (!g_slist_find(spam_learners, learn_func))
+               spam_learners = g_slist_append(spam_learners, learn_func);
+       if (mainwindow_get_mainwindow()) {
+               main_window_set_menu_sensitive(mainwindow_get_mainwindow());
+               summary_set_menu_sensitive(
+                       mainwindow_get_mainwindow()->summaryview);
+               toolbar_main_set_sensitive(mainwindow_get_mainwindow());
+       }
+}
+
+void procmsg_unregister_spam_learner (int (*learn_func)(MsgInfo *info, GSList *list, gboolean spam))
+{
+       spam_learners = g_slist_remove(spam_learners, learn_func);
+       if (mainwindow_get_mainwindow()) {
+               main_window_set_menu_sensitive(mainwindow_get_mainwindow());
+               summary_set_menu_sensitive(
+                       mainwindow_get_mainwindow()->summaryview);
+               toolbar_main_set_sensitive(mainwindow_get_mainwindow());
+       }
+}
+
+gboolean procmsg_spam_can_learn(void)
+{
+       return g_slist_length(spam_learners) > 0;
+}
+
+int procmsg_spam_learner_learn (MsgInfo *info, GSList *list, gboolean spam)
+{
+       GSList *cur = spam_learners;
+       int ret = 0;
+       for (; cur; cur = cur->next) {
+               int ((*func)(MsgInfo *info, GSList *list, gboolean spam)) = cur->data;
+               ret |= func(info, list, spam);
+       }
+       return ret;
+}
+
+static gchar *spam_folder_item = NULL;
+void procmsg_spam_set_folder (const char *item_identifier)
+{
+       g_free(spam_folder_item);
+       if (item_identifier)
+               spam_folder_item = g_strdup(item_identifier);
+       else
+               spam_folder_item = NULL;
+}
+
+FolderItem *procmsg_spam_get_folder (void)
+{
+       FolderItem *item = spam_folder_item ? folder_find_item_from_identifier(spam_folder_item) : NULL;
+       return item ? item : folder_get_default_trash();
+}