2006-02-05 [colin] 2.0.0cvs13
[claws.git] / src / procmsg.c
index db63e6ad5ee909370fddcf4fc0681f0d85fe27e3..0e0b4895cc86f96822c88db5e35a9d99503dea99 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"
@@ -332,13 +332,14 @@ 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;
+       gint retval = 0;
 
-       if (!mlist) return;
+       if (!mlist) return 0;
 
        folder_item_update_freeze();
 
@@ -360,11 +361,12 @@ void procmsg_move_messages(GSList *mlist)
        }
 
        if (movelist) {
-               folder_item_move_msgs(dest, movelist);
+               retval = folder_item_move_msgs(dest, movelist);
                g_slist_free(movelist);
        }
 
        folder_item_update_thaw();
+       return retval;
 }
 
 void procmsg_copy_messages(GSList *mlist)
@@ -547,6 +549,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 +559,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 +614,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 \"Sender\"", 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 +638,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 +663,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 +686,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 +780,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 +856,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 +905,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 +943,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;
 }
 
@@ -1088,6 +1152,7 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        MEMBCOPY(folder);
        MEMBCOPY(to_folder);
 
+       MEMBDUP(face);
        MEMBDUP(xface);
        MEMBDUP(dispositionnotificationto);
        MEMBDUP(returnreceiptto);
@@ -1100,7 +1165,6 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
         newmsginfo->references = g_slist_reverse(newmsginfo->references);
 
        MEMBCOPY(score);
-       MEMBCOPY(threadscore);
        MEMBDUP(plaintext_file);
 
        return newmsginfo;
@@ -1131,6 +1195,8 @@ MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
         * procheader.c::procheader_get_headernames() */
        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 +1236,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);
 
@@ -1221,6 +1288,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)
@@ -1697,7 +1766,7 @@ void procmsg_msginfo_change_flags(MsgInfo *msginfo,
 
        /* Perm Flags handling */
        perm_flags_old = msginfo->flags.perm_flags;
-       perm_flags_new = (msginfo->flags.perm_flags | add_perm_flags) & ~rem_perm_flags;
+       perm_flags_new = (msginfo->flags.perm_flags & ~rem_perm_flags) | add_perm_flags;
        if ((add_perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
                perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
        }
@@ -1711,8 +1780,8 @@ void procmsg_msginfo_change_flags(MsgInfo *msginfo,
 
        /* Tmp flags handling */
        tmp_flags_old = msginfo->flags.tmp_flags;
-       msginfo->flags.tmp_flags |= add_tmp_flags;
        msginfo->flags.tmp_flags &= ~rem_tmp_flags;
+       msginfo->flags.tmp_flags |= add_tmp_flags;
 
        /* update notification */
        if ((perm_flags_old != perm_flags_new) || (tmp_flags_old != msginfo->flags.tmp_flags)) {
@@ -1897,16 +1966,19 @@ 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)) {
+               hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
                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)) {
+               hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
                return TRUE;
+       }
                
        hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
-
        return FALSE;
 }
 
@@ -1914,53 +1986,37 @@ 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;
 }