GSList *procmsg_get_message_file_list(GSList *mlist) { GSList *file_list = NULL; MsgInfo *msginfo; MsgFileInfo *fileinfo; gchar *file; while (mlist != NULL) { msginfo = (MsgInfo *)mlist->data; file = procmsg_get_message_file(msginfo); if (!file) { procmsg_message_file_list_free(file_list); return NULL; } fileinfo = g_new(MsgFileInfo, 1); fileinfo->msginfo = procmsg_msginfo_new_ref(msginfo); fileinfo->file = file; fileinfo->flags = g_new(MsgFlags, 1); *fileinfo->flags = msginfo->flags; file_list = g_slist_prepend(file_list, fileinfo); mlist = mlist->next; } file_list = g_slist_reverse(file_list); return file_list; }
gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter, PrefsAccount *account) /* return values: -1 error, >=0 number of msgs added */ { FILE *mbox_fp; gchar buf[MESSAGEBUFSIZE]; gchar *tmp_file; gint msgs = 0; gint lines; MsgInfo *msginfo; gboolean more; GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL; gboolean printed = FALSE; FolderItem *dropfolder; cm_return_val_if_fail(dest != NULL, -1); cm_return_val_if_fail(mbox != NULL, -1); debug_print("Getting messages from %s into %s...\n", mbox, dest->path); if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) { FILE_OP_ERROR(mbox, "fopen"); alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox); return -1; } /* ignore empty lines on the head */ do { if (fgets(buf, sizeof(buf), mbox_fp) == NULL) { g_warning("can't read mbox file.\n"); fclose(mbox_fp); return -1; } } while (buf[0] == '\n' || buf[0] == '\r'); if (strncmp(buf, "From ", 5) != 0) { g_warning("invalid mbox format: %s\n", mbox); fclose(mbox_fp); return -1; } tmp_file = get_tmp_file(); folder_item_update_freeze(); if (apply_filter) dropfolder = folder_get_default_processing(); else dropfolder = dest; do { FILE *tmp_fp; gint empty_lines; gint msgnum; if (msgs > 0 && msgs%500 == 0) { if (printed) statusbar_pop_all(); statusbar_print_all( ngettext("Importing from mbox... (%d mail imported)", "Importing from mbox... (%d mails imported)", msgs), msgs); printed=TRUE; GTK_EVENTS_FLUSH(); } if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) { FILE_OP_ERROR(tmp_file, "fopen"); g_warning("can't open temporary file\n"); fclose(mbox_fp); g_free(tmp_file); return -1; } if (change_file_mode_rw(tmp_fp, tmp_file) < 0) { FILE_OP_ERROR(tmp_file, "chmod"); } empty_lines = 0; lines = 0; /* process all lines from mboxrc file */ while (fgets(buf, sizeof(buf), mbox_fp) != NULL) { int offset; /* eat empty lines */ if (buf[0] == '\n' || buf[0] == '\r') { empty_lines++; continue; } /* From separator or quoted From */ offset = 0; /* detect leading '>' char(s) */ while ((buf[offset] == '>')) { offset++; } if (!strncmp(buf+offset, "From ", 5)) { /* From separator: */ if (offset == 0) { /* expect next mbox item */ break; } /* quoted From: */ /* flush any eaten empty line */ if (empty_lines > 0) { while (empty_lines-- > 0) { FPUTS_TO_TMP_ABORT_IF_FAIL("\n"); } empty_lines = 0; } /* store the unquoted line */ FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1); continue; } /* other line */ /* flush any eaten empty line */ if (empty_lines > 0) { while (empty_lines-- > 0) { FPUTS_TO_TMP_ABORT_IF_FAIL("\n"); } empty_lines = 0; } /* store the line itself */ FPUTS_TO_TMP_ABORT_IF_FAIL(buf); } /* end of mbox item or end of mbox */ /* flush any eaten empty line (but the last one) */ if (empty_lines > 0) { while (--empty_lines > 0) { FPUTS_TO_TMP_ABORT_IF_FAIL("\n"); } } /* more emails to expect? */ more = !feof(mbox_fp); /* warn if email part is empty (it's the minimum check we can do */ if (lines == 0) { g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs); fclose(tmp_fp); fclose(mbox_fp); claws_unlink(tmp_file); return -1; } if (fclose(tmp_fp) == EOF) { FILE_OP_ERROR(tmp_file, "fclose"); g_warning("can't write to temporary file\n"); fclose(mbox_fp); claws_unlink(tmp_file); g_free(tmp_file); return -1; } if (apply_filter) { if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) { fclose(mbox_fp); claws_unlink(tmp_file); g_free(tmp_file); return -1; } msginfo = folder_item_get_msginfo(dropfolder, msgnum); to_filter = g_slist_prepend(to_filter, msginfo); } else { MsgFileInfo *finfo = g_new0(MsgFileInfo, 1); finfo->file = tmp_file; to_add = g_slist_prepend(to_add, finfo); tmp_file = get_tmp_file(); /* flush every 500 */ if (msgs > 0 && msgs % 500 == 0) { folder_item_add_msgs(dropfolder, to_add, TRUE); procmsg_message_file_list_free(to_add); to_add = NULL; } } msgs++; } while (more); if (printed) statusbar_pop_all(); if (apply_filter) { folder_item_set_batch(dropfolder, FALSE); procmsg_msglist_filter(to_filter, account, &filtered, &unfiltered, TRUE); folder_item_set_batch(dropfolder, TRUE); filtering_move_and_copy_msgs(to_filter); for (cur = filtered; cur; cur = g_slist_next(cur)) { MsgInfo *info = (MsgInfo *)cur->data; procmsg_msginfo_free(info); } unfiltered = g_slist_reverse(unfiltered); if (unfiltered) { folder_item_move_msgs(dest, unfiltered); for (cur = unfiltered; cur; cur = g_slist_next(cur)) { MsgInfo *info = (MsgInfo *)cur->data; procmsg_msginfo_free(info); } } g_slist_free(unfiltered); g_slist_free(filtered); g_slist_free(to_filter); } else if (to_add) { folder_item_add_msgs(dropfolder, to_add, TRUE); procmsg_message_file_list_free(to_add); to_add = NULL; } folder_item_update_thaw(); g_free(tmp_file); fclose(mbox_fp); debug_print("%d messages found.\n", msgs); return msgs; }