2007-01-21 [colin] 2.7.1cvs47
[claws.git] / src / procmsg.c
index 8e0bb358834033f8ba674fe19ceedf141edc74a3..e1c7e1f0b0d6c6cd32e922f083fbaa76a4bcfa69 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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
@@ -49,8 +49,9 @@
 #include "inc.h"
 
 static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session, gchar **errstr,
-                                           FolderItem *queue, gint msgnum);
-
+                                           FolderItem *queue, gint msgnum, gboolean *queued_removed);
+static void procmsg_update_unread_children     (MsgInfo        *info,
+                                        gboolean        newly_marked);
 enum
 {
        Q_SENDER           = 0,
@@ -65,70 +66,13 @@ enum
        Q_PRIVACY_SYSTEM   = 9,
        Q_ENCRYPT          = 10,
        Q_ENCRYPT_DATA     = 11,
-       Q_SYLPHEED_HDRS    = 12,
+       Q_CLAWS_HDRS       = 12,
+       Q_PRIVACY_SYSTEM_OLD = 13,
+       Q_ENCRYPT_OLD        = 14,
+       Q_ENCRYPT_DATA_OLD   = 15,
+       Q_CLAWS_HDRS_OLD     = 16,
 };
 
-GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
-{
-       GHashTable *msg_table;
-
-       if (mlist == NULL) return NULL;
-
-       msg_table = g_hash_table_new(NULL, g_direct_equal);
-       procmsg_msg_hash_table_append(msg_table, mlist);
-
-       return msg_table;
-}
-
-void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
-{
-       GSList *cur;
-       MsgInfo *msginfo;
-
-       if (msg_table == NULL || mlist == NULL) return;
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-
-               g_hash_table_insert(msg_table,
-                                   GUINT_TO_POINTER(msginfo->msgnum),
-                                   msginfo);
-       }
-}
-
-GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
-{
-       GHashTable *msg_table;
-       GSList *cur;
-       MsgInfo *msginfo;
-
-       if (mlist == NULL) return NULL;
-
-       msg_table = g_hash_table_new(NULL, g_direct_equal);
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-               g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
-       }
-
-       return msg_table;
-}
-
-gint procmsg_get_last_num_in_msg_list(GSList *mlist)
-{
-       GSList *cur;
-       MsgInfo *msginfo;
-       gint last = 0;
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-               if (msginfo && msginfo->msgnum > last)
-                       last = msginfo->msgnum;
-       }
-
-       return last;
-}
-
 void procmsg_msg_list_free(GSList *mlist)
 {
        GSList *cur;
@@ -550,8 +494,10 @@ FILE *procmsg_open_message(MsgInfo *msginfo)
 
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        /* new way */
-                       if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
-                               strlen("X-Sylpheed-End-Special-Headers:")))
+                       if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
+                               strlen("X-Claws-End-Special-Headers:"))) ||
+                           (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
+                               strlen("X-Sylpheed-End-Special-Headers:"))))
                                break;
                        /* old way */
                        if (buf[0] == '\r' || buf[0] == '\n') break;
@@ -705,7 +651,7 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
        }
 }
 
-void procmsg_empty_trash(FolderItem *trash)
+static void procmsg_empty_trash(FolderItem *trash)
 {
        GNode *node, *next;
 
@@ -773,6 +719,10 @@ static PrefsAccount *procmsg_get_account_from_file(const gchar *file)
                                       {"SCF:",  NULL, FALSE},
                                       {"RMID:", NULL, FALSE},
                                       {"FMID:", NULL, FALSE},
+                                      {"X-Claws-Privacy-System:", NULL, FALSE},
+                                      {"X-Claws-Encrypt:", NULL, FALSE},
+                                      {"X-Claws-Encrypt-Data:", NULL, FALSE},
+                                      {"X-Claws-End-Special-Headers",    NULL, FALSE},
                                       {"X-Sylpheed-Privacy-System:", NULL, FALSE},
                                       {"X-Sylpheed-Encrypt:", NULL, FALSE},
                                       {"X-Sylpheed-Encrypt-Data:", NULL, FALSE},
@@ -940,15 +890,17 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs, gchar **errstr)
                if (!MSG_IS_LOCKED(msginfo->flags)) {
                        file = folder_item_fetch_msg(queue, msginfo->msgnum);
                        if (file) {
+                               gboolean queued_removed = FALSE;
                                if (procmsg_send_message_queue_full(file, 
                                                !procmsg_is_last_for_account(queue, msginfo, elem),
-                                               errstr, queue, msginfo->msgnum) < 0) {
+                                               errstr, queue, msginfo->msgnum, &queued_removed) < 0) {
                                        g_warning("Sending queued message %d failed.\n", 
                                                  msginfo->msgnum);
                                        err++;
                                } else {
                                        sent++; 
-                                       folder_item_remove_msg(queue, msginfo->msgnum);
+                                       if (!queued_removed)
+                                               folder_item_remove_msg(queue, msginfo->msgnum);
                                }
                                g_free(file);
                        }
@@ -1038,8 +990,10 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
        }
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                /* new way */
-               if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
-                       strlen("X-Sylpheed-End-Special-Headers:")))
+               if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
+                       strlen("X-Claws-End-Special-Headers:"))) ||
+                   (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
+                       strlen("X-Sylpheed-End-Special-Headers:"))))
                        break;
                /* old way */
                if (buf[0] == '\r' || buf[0] == '\n') break;
@@ -1059,7 +1013,7 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
        return 0;
 }
 
-gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
+static gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                            gboolean is_queued)
 {
        gint num;
@@ -1439,21 +1393,8 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
        return memusage;
 }
 
-gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
-{
-       const MsgInfo *msginfo1 = a;
-       const MsgInfo *msginfo2 = b;
-
-       if (!msginfo1)
-               return -1;
-       if (!msginfo2)
-               return -1;
-
-       return msginfo1->msgnum - msginfo2->msgnum;
-}
-
 static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session, gchar **errstr,
-                                           FolderItem *queue, gint msgnum)
+                                           FolderItem *queue, gint msgnum, gboolean *queued_removed)
 {
        static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
                                       {"SSV:",  NULL, FALSE},
@@ -1464,6 +1405,10 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
                                       {"SCF:",  NULL, FALSE},
                                       {"RMID:", NULL, FALSE},
                                       {"FMID:", NULL, FALSE},
+                                      {"X-Claws-Privacy-System:", NULL, FALSE},
+                                      {"X-Claws-Encrypt:", NULL, FALSE},
+                                      {"X-Claws-Encrypt-Data:", NULL, FALSE},
+                                      {"X-Claws-End-Special-Headers:", NULL, FALSE},
                                       {"X-Sylpheed-Privacy-System:", NULL, FALSE},
                                       {"X-Sylpheed-Encrypt:", NULL, FALSE},
                                       {"X-Sylpheed-Encrypt-Data:", NULL, FALSE},
@@ -1539,18 +1484,22 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
                                fwdmessageid = g_strdup(p);
                        break;
                case Q_PRIVACY_SYSTEM:
+               case Q_PRIVACY_SYSTEM_OLD:
                        if (privacy_system == NULL) 
                                privacy_system = g_strdup(p);
                        break;
                case Q_ENCRYPT:
+               case Q_ENCRYPT_OLD:
                        if (p[0] == '1') 
                                encrypt = TRUE;
                        break;
                case Q_ENCRYPT_DATA:
+               case Q_ENCRYPT_DATA_OLD:
                        if (encrypt_data == NULL) 
                                encrypt_data = g_strdup(p);
                        break;
-               case Q_SYLPHEED_HDRS:
+               case Q_CLAWS_HDRS:
+               case Q_CLAWS_HDRS_OLD:
                        /* end of special headers reached */
                        goto send_mail; /* can't "break;break;" */
                }
@@ -1667,7 +1616,7 @@ send_mail:
                if (errstr) {
                        if (*errstr) g_free(*errstr);
                        *errstr = g_strdup(_("Couldn't determine sending informations. "
-                               "Maybe the email hasn't been generated by Sylpheed-Claws."));
+                               "Maybe the email hasn't been generated by Claws Mail."));
                }
                mailval = -1;
        }
@@ -1679,8 +1628,8 @@ send_mail:
                FILE *tmpfp;
 
                /* write to temporary file */
-               tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
-                           G_DIR_SEPARATOR, (gint)file);
+               tmp = g_strdup_printf("%s%ctmp%p", g_get_tmp_dir(),
+                           G_DIR_SEPARATOR, file);
                if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
                        FILE_OP_ERROR(tmp, "fopen");
                        newsval = -1;
@@ -1734,15 +1683,25 @@ send_mail:
                        
                if (save_clear_text || tmp_enc_file == NULL) {
                        gboolean saved = FALSE;
+                       *queued_removed = FALSE;
                        if (queue && msgnum > 0) {
                                MsgInfo *queued_mail = folder_item_get_msginfo(queue, msgnum);
-                               if (folder_item_copy_msg(outbox, queued_mail) >= 0)
+                               if (folder_item_move_msg(outbox, queued_mail) >= 0) {
+                                       debug_print("moved queued mail %d to sent folder\n", msgnum);
                                        saved = TRUE;
+                                       *queued_removed = TRUE;
+                               } else if (folder_item_copy_msg(outbox, queued_mail) >= 0) {
+                                       debug_print("copied queued mail %d to sent folder\n", msgnum);
+                                       saved = TRUE;
+                               }
                                procmsg_msginfo_free(queued_mail);
                        }
-                       if (!saved)
+                       if (!saved) {
+                               debug_print("resaving clear text queued mail to sent folder\n");
                                procmsg_save_to_outbox(outbox, file, TRUE);
+                       }
                } else {
+                       debug_print("saving encrpyted queued mail to sent folder\n");
                        procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE);
                }
        }
@@ -1810,9 +1769,9 @@ send_mail:
        return (newsval != 0 ? newsval : mailval);
 }
 
-gint procmsg_send_message_queue(const gchar *file, gchar **errstr, FolderItem *queue, gint msgnum)
+gint procmsg_send_message_queue(const gchar *file, gchar **errstr, FolderItem *queue, gint msgnum, gboolean *queued_removed)
 {
-       gint result = procmsg_send_message_queue_full(file, FALSE, errstr, queue, msgnum);
+       gint result = procmsg_send_message_queue_full(file, FALSE, errstr, queue, msgnum, queued_removed);
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
        return result;
 }
@@ -1983,7 +1942,7 @@ void procmsg_msginfo_change_flags(MsgInfo *msginfo,
  *
  *\return      gboolean TRUE if perm_flags are found
  */
-gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
+static gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
                MsgPermFlags perm_flags, GHashTable *parentmsgs)
 {
        MsgInfo *tmp;
@@ -2020,7 +1979,7 @@ gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
 /*!
  *\brief       Callback for cleaning up hash of parentmsgs
  */
-gboolean parentmsgs_hash_remove(gpointer key,
+static gboolean parentmsgs_hash_remove(gpointer key,
                             gpointer value,
                             gpointer user_data)
 {
@@ -2055,7 +2014,7 @@ gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
 }
 
 
-GSList *procmsg_find_children_func(MsgInfo *info, 
+static GSList *procmsg_find_children_func(MsgInfo *info, 
                                   GSList *children, GSList *all)
 {
        GSList *cur;
@@ -2081,7 +2040,7 @@ GSList *procmsg_find_children_func(MsgInfo *info,
        return children;
 }
 
-GSList *procmsg_find_children (MsgInfo *info)
+static GSList *procmsg_find_children (MsgInfo *info)
 {
        GSList *children;
        GSList *all, *cur;
@@ -2101,7 +2060,7 @@ GSList *procmsg_find_children (MsgInfo *info)
        return children;
 }
 
-void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
+static void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
 {
        GSList *children = procmsg_find_children(info);
        GSList *cur;
@@ -2145,7 +2104,7 @@ void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
  * \return TRUE if the message was moved and MsgInfo is now invalid,
  *         FALSE otherwise
  */
-gboolean procmsg_msginfo_filter(MsgInfo *msginfo, PrefsAccount* ac_prefs)
+static gboolean procmsg_msginfo_filter(MsgInfo *msginfo, PrefsAccount* ac_prefs)
 {
        MailFilteringData mail_filtering_data;