get attachment names
[claws.git] / src / compose.c
index 691e5f994776cfcbdf7cdc9e4b34d6d05394cf8b..3755a636c13f04685256a3aafcca13902f07b5fc 100644 (file)
@@ -62,6 +62,7 @@
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include <libgen.h>
 
 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
 #  include <wchar.h>
@@ -149,6 +150,14 @@ typedef enum
        PRIORITY_LOWEST
 } PriorityLevel;
 
+typedef enum
+{
+       COMPOSE_INSERT_SUCCESS,
+       COMPOSE_INSERT_READ_ERROR,
+       COMPOSE_INSERT_INVALID_CHARACTER,
+       COMPOSE_INSERT_NO_FILE
+} ComposeInsertResult;
+
 #define B64_LINE_SIZE          57
 #define B64_BUFFSIZE           77
 
@@ -199,7 +208,7 @@ static void compose_reedit_set_entry                (Compose        *compose,
 static void compose_insert_sig                 (Compose        *compose,
                                                 gboolean        replace);
 static gchar *compose_get_signature_str                (Compose        *compose);
-static void compose_insert_file                        (Compose        *compose,
+static ComposeInsertResult compose_insert_file (Compose        *compose,
                                                 const gchar    *file);
 static void compose_attach_append              (Compose        *compose,
                                                 const gchar    *file,
@@ -873,7 +882,7 @@ void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
        case COMPOSE_FORWARD_INLINE:
                /* check if we reply to more than one Message */
                if (list_len == 1) {
-                       compose_forward(NULL, msginfo, FALSE, body);
+                       compose_forward(NULL, msginfo, FALSE, body, FALSE);
                        break;
                } 
                /* more messages FALL THROUGH */
@@ -1012,7 +1021,8 @@ if (msginfo->var && *msginfo->var) { \
 }
 
 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
-                        gboolean as_attach, const gchar *body)
+                        gboolean as_attach, const gchar *body,
+                        gboolean no_extedit)
 {
        Compose *compose;
        GtkSText *text;
@@ -1033,7 +1043,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
        if (!account && prefs_common.forward_account_autosel) {
                gchar cc[BUFFSIZE];
-               if (!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
+               if (!procheader_get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
                        extract_address(cc);
                        account = account_find_from_address(cc);
                 }
@@ -1122,7 +1132,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 #endif
        gtk_widget_grab_focus(compose->header_last->entry);
 
-       if (prefs_common.auto_exteditor)
+       if (!no_extedit && prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
        
        /*save folder*/
@@ -1221,22 +1231,22 @@ void compose_reedit(MsgInfo *msginfo)
                gint id;
 
                /* Select Account from queue headers */
-               if (!get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
                        id = atoi(&queueheader_buf[22]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "NAID:")) {
                        id = atoi(&queueheader_buf[5]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                    sizeof(queueheader_buf), "MAID:")) {
                        id = atoi(&queueheader_buf[5]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                                sizeof(queueheader_buf), "S:")) {
                        account = account_find_from_address(queueheader_buf);
                }
@@ -1245,7 +1255,7 @@ void compose_reedit(MsgInfo *msginfo)
 
        if (!account && prefs_common.reedit_account_autosel) {
                        gchar from[BUFFSIZE];
-               if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
+               if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
                        extract_address(from);
                        account = account_find_from_address(from);
                 }
@@ -1261,7 +1271,7 @@ void compose_reedit(MsgInfo *msginfo)
                gchar queueheader_buf[BUFFSIZE];
 
                /* Set message save folder */
-               if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
                        gint startpos = 0;
 
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
@@ -1854,6 +1864,10 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
 
        if (replyto && from)
                cc_list = address_list_append_with_comments(cc_list, from);
+       if (to_all && msginfo->folder && 
+           msginfo->folder->prefs->enable_default_reply_to)
+               cc_list = address_list_append_with_comments(cc_list,
+                               msginfo->folder->prefs->default_reply_to);
        cc_list = address_list_append_with_comments(cc_list, msginfo->to);
        cc_list = address_list_append_with_comments(cc_list, compose->cc);
 
@@ -2018,18 +2032,19 @@ static gchar *compose_get_signature_str(Compose *compose)
        return sig_str;
 }
 
-static void compose_insert_file(Compose *compose, const gchar *file)
+static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        gchar buf[BUFFSIZE];
        gint len;
        FILE *fp;
+       gboolean badtxt = FALSE;
 
-       g_return_if_fail(file != NULL);
+       g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
 
        if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
-               return;
+               return COMPOSE_INSERT_READ_ERROR;
        }
 
        gtk_stext_freeze(text);
@@ -2043,12 +2058,19 @@ static void compose_insert_file(Compose *compose, const gchar *file)
                        while (--len >= 0)
                                if (buf[len] == '\r') buf[len] = '\n';
                }
+               if (mbstowcs(NULL, buf, 0) == -1)
+                       badtxt = TRUE;
                gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
        }
 
        gtk_stext_thaw(text);
 
        fclose(fp);
+
+       if (badtxt)
+               return COMPOSE_INSERT_INVALID_CHARACTER;
+       else 
+               return COMPOSE_INSERT_SUCCESS;
 }
 
 static void compose_attach_append(Compose *compose, const gchar *file,
@@ -2150,8 +2172,8 @@ static void compose_attach_append(Compose *compose, const gchar *file,
        ((info->type == MIMETYPE_TEXT) || \
         (info->type == MIMETYPE_MULTIPART && info->subtype && \
          !strcasecmp(info->subtype, "alternative") && \
-         (info->children && \
-          (info->children->type == MIMETYPE_TEXT))))
+         (info->node->children && \
+          (((MimeInfo *) info->node->children->data)->type == MIMETYPE_TEXT))))
 
 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
 {
@@ -2159,30 +2181,31 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
        MimeInfo *child;
        gchar *infile;
        gchar *outfile;
+       const gchar *partname = NULL;
 
        mimeinfo = procmime_scan_message(msginfo);
        if (!mimeinfo) return;
 
        /* skip first text (presumably message body) */
-       child = mimeinfo->children;
+       child = (MimeInfo *) mimeinfo->node->children->data;
        if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
                procmime_mimeinfo_free_all(mimeinfo);
                return;
        }
 
        if (IS_FIRST_PART_TEXT(child))
-               child = child->next;
+               child = (MimeInfo *) child->node->next;
 
        infile = procmsg_get_message_file_path(msginfo);
 
        while (child != NULL) {
-               if (child->children || child->type == MIMETYPE_MULTIPART) {
+               if (child->node->children || child->type == MIMETYPE_MULTIPART) {
                        child = procmime_mimeinfo_next(child);
                        continue;
                }
-               if (child->parent && child->parent->parent
-               && (child->parent->parent->type == MIMETYPE_MULTIPART)
-               && !strcasecmp(child->parent->parent->subtype, "signed")
+               if (child->node->parent && child->node->parent->parent
+               && (((MimeInfo *) child->node->parent->parent->data)->type == MIMETYPE_MULTIPART)
+               && !strcasecmp(((MimeInfo *) child->node->parent->parent->data)->subtype, "signed")
                && child->type == MIMETYPE_TEXT) {
                        /* this is the main text part of a signed message */
                        child = procmime_mimeinfo_next(child);
@@ -2196,14 +2219,14 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
                        gchar *content_type;
 
                        content_type = g_strdup_printf("%s/%s", procmime_get_type_str(child->type), child->subtype);
-                       compose_attach_append
-                               (compose, outfile,
-                                child->filename ? child->filename : child->name,
-                                content_type);
+                       partname = procmime_mimeinfo_get_parameter(child, "name");
+                       
+                       compose_attach_append(compose, outfile, 
+                                             partname, content_type);
                        g_free(content_type);
                }
 
-               child = child->next;
+               child = child->node->next != NULL ? (MimeInfo *) child->node->next->data : NULL;
        }
 
        g_free(infile);
@@ -3280,7 +3303,7 @@ static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
                g_warning("can't change file mode\n");
        }
 
-       while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
+       while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
                /* should filter returnpath, delivered-to */
                if (g_strncasecmp(buf, "Return-Path:",
                                   strlen("Return-Path:")) == 0 ||
@@ -6418,7 +6441,19 @@ static void compose_insert_file_cb(gpointer data, guint action,
 
                for ( tmp = file_list; tmp; tmp = tmp->next) {
                        gchar *file = (gchar *) tmp->data;
-                       compose_insert_file(compose, file);
+                       gchar *filedup = g_strdup(file);
+                       gchar *shortfile;
+                       ComposeInsertResult res;
+
+                       res = compose_insert_file(compose, file);
+                       shortfile = g_basename(filedup);
+                       if (res == COMPOSE_INSERT_READ_ERROR) {
+                               alertpanel_error(_("File '%s' could not be read."), shortfile);
+                       } else if (res == COMPOSE_INSERT_INVALID_CHARACTER) {
+                               alertpanel_error(_("File '%s' contained invalid characters\n"
+                                                  "for the current encoding, insertion may be incorrect."), shortfile);
+                       }
+                       g_free(filedup);
                        g_free(file);
                }
                g_list_free(file_list);