/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
*
* 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
#include "defs.h"
#include <glib.h>
+#include <glib/gi18n.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
-#include "intl.h"
#include "main.h"
#include "utils.h"
#include "procmsg.h"
#include "msgcache.h"
#include "partial_download.h"
+static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session);
+
+enum
+{
+ Q_SENDER = 0,
+ Q_SMTPSERVER = 1,
+ Q_RECIPIENTS = 2,
+ Q_NEWSGROUPS = 3,
+ Q_MAIL_ACCOUNT_ID = 4,
+ Q_NEWS_ACCOUNT_ID = 5,
+ Q_SAVE_COPY_FOLDER = 6,
+ Q_REPLY_MESSAGE_ID = 7,
+ Q_FWD_MESSAGE_ID = 8,
+ Q_PRIVACY_SYSTEM = 9,
+ Q_ENCRYPT = 10,
+ Q_ENCRYPT_DATA = 11,
+};
+
GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
{
GHashTable *msg_table;
GRelation *subject_relation;
MsgInfo *msginfo;
const gchar *msgid;
+ GSList *reflist;
root = g_node_new(NULL);
msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
/* complete the unfinished threads */
for (node = root->children; node != NULL; ) {
- parent = NULL;
next = node->next;
msginfo = (MsgInfo *)node->data;
- if (msginfo->inreplyto) {
+ parent = NULL;
+
+ if (msginfo->inreplyto)
parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
- /* node should not be the parent, and node should not
- be an ancestor of parent (circular reference) */
- if (parent && parent != node &&
- !g_node_is_ancestor(node, parent)) {
- g_node_unlink(node);
- g_node_insert_before
- (parent, parent->children, node);
- }
+
+ /* try looking for the indirect parent */
+ if (!parent && msginfo->references) {
+ for (reflist = msginfo->references;
+ reflist != NULL; reflist = reflist->next)
+ if ((parent = g_hash_table_lookup
+ (msgid_table, reflist->data)) != NULL)
+ break;
+ }
+
+ /* node should not be the parent, and node should not
+ be an ancestor of parent (circular reference) */
+ if (parent && parent != node &&
+ !g_node_is_ancestor(node, parent)) {
+ g_node_unlink(node);
+ g_node_insert_before
+ (parent, parent->children, node);
}
+
node = next;
}
void procmsg_empty_trash(FolderItem *trash)
{
- FILE *fp;
-
if (trash && trash->total_msgs > 0) {
GSList *mlist = folder_item_get_msg_list(trash);
GSList *cur;
}
}
+static PrefsAccount *procmsg_get_account_from_file(const gchar *file)
+{
+ PrefsAccount *mailac = NULL;
+ FILE *fp;
+ int hnum;
+ gchar buf[BUFFSIZE];
+ static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
+ {"SSV:", NULL, FALSE},
+ {"R:", NULL, FALSE},
+ {"NG:", NULL, FALSE},
+ {"MAID:", NULL, FALSE},
+ {"NAID:", NULL, FALSE},
+ {"SCF:", NULL, FALSE},
+ {"RMID:", NULL, FALSE},
+ {"FMID:", NULL, FALSE},
+ {"X-Sylpheed-Privacy-System:", NULL, FALSE},
+ {"X-Sylpheed-Encrypt:", NULL, FALSE},
+ {"X-Sylpheed-Encrypt-Data:", NULL, FALSE},
+ {NULL, NULL, FALSE}};
+
+ g_return_val_if_fail(file != NULL, NULL);
+
+ if ((fp = fopen(file, "rb")) == NULL) {
+ FILE_OP_ERROR(file, "fopen");
+ return NULL;
+ }
+
+ while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
+ != -1) {
+ gchar *p = buf + strlen(qentry[hnum].name);
+
+ if (hnum == Q_MAIL_ACCOUNT_ID) {
+ mailac = account_find_from_id(atoi(p));
+ break;
+ }
+ }
+ fclose(fp);
+ return mailac;
+}
+
+static GSList *procmsg_list_sort_by_account(FolderItem *queue, GSList *list)
+{
+ GSList *result = NULL;
+ GSList *orig = NULL;
+ PrefsAccount *last_account = NULL;
+ MsgInfo *msg = NULL;
+ GSList *cur = NULL;
+ gboolean nothing_to_sort = TRUE;
+
+ if (!list)
+ return NULL;
+
+ orig = g_slist_copy(list);
+
+ msg = (MsgInfo *)orig->data;
+
+ for (cur = orig; cur; cur = cur->next)
+ debug_print("sort before %s\n", ((MsgInfo *)cur->data)->from);
+
+ debug_print("\n");
+
+parse_again:
+ nothing_to_sort = TRUE;
+ cur = orig;
+ while (cur) {
+ gchar *file;
+ PrefsAccount *ac = procmsg_get_account_from_file(file);
+ msg = (MsgInfo *)cur->data;
+ file = folder_item_fetch_msg(queue, msg->msgnum);
+ g_free(file);
+
+ if (last_account == NULL || (ac != NULL && ac == last_account)) {
+ result = g_slist_append(result, msg);
+ orig = g_slist_remove(orig, msg);
+ last_account = ac;
+ nothing_to_sort = FALSE;
+ goto parse_again;
+ }
+ cur = cur->next;
+ }
+
+ if (orig || g_slist_length(orig)) {
+ if (!last_account && nothing_to_sort) {
+ /* can't find an account for the rest of the list */
+ cur = orig;
+ while (cur) {
+ result = g_slist_append(result, cur->data);
+ cur = cur->next;
+ }
+ } else {
+ last_account = NULL;
+ goto parse_again;
+ }
+ }
+
+ g_slist_free(orig);
+
+ for (cur = result; cur; cur = cur->next)
+ debug_print("sort after %s\n", ((MsgInfo *)cur->data)->from);
+
+ debug_print("\n");
+
+ return result;
+}
+
+static gboolean procmsg_is_last_for_account(FolderItem *queue, MsgInfo *msginfo, GSList *elem)
+{
+ gchar *file = folder_item_fetch_msg(queue, msginfo->msgnum);
+ PrefsAccount *ac = procmsg_get_account_from_file(file);
+ GSList *cur = elem;
+ g_free(file);
+ for (cur = elem; cur; cur = cur->next) {
+ MsgInfo *cur_msginfo = (MsgInfo *)cur->data;
+ PrefsAccount *cur_ac = NULL;
+ file = folder_item_fetch_msg(queue, cur_msginfo->msgnum);
+
+ if (cur_msginfo != msginfo && !MSG_IS_LOCKED(cur_msginfo->flags)) {
+ if (procmsg_get_account_from_file(file) == ac) {
+ g_free(file);
+ return FALSE;
+ }
+ }
+
+ g_free(file);
+ }
+ return TRUE;
+}
+
/*!
*\brief Send messages in queue
*
*/
gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
{
- gint ret = 1, count = 0;
+ gint sent = 0, err = 0;
GSList *list, *elem;
+ GSList *sorted_list = NULL;
if (!queue)
queue = folder_get_default_queue();
folder_item_scan(queue);
list = folder_item_get_msg_list(queue);
- for (elem = list; elem != NULL; elem = elem->next) {
+ /* sort the list per sender account; this helps reusing the same SMTP server */
+ sorted_list = procmsg_list_sort_by_account(queue, list);
+
+ for (elem = sorted_list; elem != NULL; elem = elem->next) {
gchar *file;
MsgInfo *msginfo;
-
+
msginfo = (MsgInfo *)(elem->data);
if (!MSG_IS_LOCKED(msginfo->flags)) {
file = folder_item_fetch_msg(queue, msginfo->msgnum);
if (file) {
- if (procmsg_send_message_queue(file) < 0) {
+ if (procmsg_send_message_queue_full(file,
+ !procmsg_is_last_for_account(queue, msginfo, elem)) < 0) {
g_warning("Sending queued message %d failed.\n",
msginfo->msgnum);
- ret = -1;
+ err++;
} else {
/* CLAWS:
* We save in procmsg_send_message_queue because
(queue->folder->outbox,
file, TRUE);
*/
- count++;
+ sent++;
folder_item_remove_msg(queue, msginfo->msgnum);
}
g_free(file);
procmsg_msginfo_free(msginfo);
}
- return ret * count;
+ g_slist_free(sorted_list);
+
+ return (err != 0 ? -err : sent);
}
gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
{
MsgInfo *newmsginfo;
+ GSList *refs;
if (msginfo == NULL) return NULL;
MEMBDUP(xface);
MEMBDUP(dispositionnotificationto);
MEMBDUP(returnreceiptto);
- MEMBDUP(references);
+
+ refs = msginfo->references;
+ for (refs = msginfo->references; refs != NULL; refs = refs->next) {
+ newmsginfo->references = g_slist_prepend
+ (newmsginfo->references, g_strdup(refs->data));
+ }
+ newmsginfo->references = g_slist_reverse(newmsginfo->references);
MEMBCOPY(score);
MEMBCOPY(threadscore);
if (msginfo == NULL) return NULL;
- file = procmsg_get_message_file(msginfo);
- if (!file) {
+ file = procmsg_get_message_file_path(msginfo);
+ if (!file || !is_file_exist(file)) {
+ g_free(file);
+ file = procmsg_get_message_file(msginfo);
+ }
+ if (!file || !is_file_exist(file)) {
g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
return NULL;
}
}
g_free(msginfo->fromspace);
- g_free(msginfo->references);
g_free(msginfo->returnreceiptto);
g_free(msginfo->dispositionnotificationto);
g_free(msginfo->xface);
g_free(msginfo->account_server);
g_free(msginfo->account_login);
+ slist_free_strings(msginfo->references);
+ g_slist_free(msginfo->references);
+
g_free(msginfo->plaintext_file);
g_free(msginfo);
memusage += strlen(msginfo->dispositionnotificationto);
if (msginfo->returnreceiptto)
memusage += strlen(msginfo->returnreceiptto);
+#warning FIXME: Calculate size of references list
+#if 0
if (msginfo->references)
memusage += strlen(msginfo->references);
+#endif
if (msginfo->fromspace)
memusage += strlen(msginfo->fromspace);
return msginfo1->msgnum - msginfo2->msgnum;
}
-enum
-{
- Q_SENDER = 0,
- Q_SMTPSERVER = 1,
- Q_RECIPIENTS = 2,
- Q_NEWSGROUPS = 3,
- Q_MAIL_ACCOUNT_ID = 4,
- Q_NEWS_ACCOUNT_ID = 5,
- Q_SAVE_COPY_FOLDER = 6,
- Q_REPLY_MESSAGE_ID = 7,
- Q_FWD_MESSAGE_ID = 8,
- Q_PRIVACY_SYSTEM = 9,
- Q_ENCRYPT = 10,
- Q_ENCRYPT_DATA = 11,
-};
-
-gint procmsg_send_message_queue(const gchar *file)
+static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session)
{
static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
{"SSV:", NULL, FALSE},
gchar buf[BUFFSIZE];
gint hnum;
PrefsAccount *mailac = NULL, *newsac = NULL;
+ gboolean save_clear_text = TRUE;
+ gchar *tmp_enc_file = NULL;
+
int local = 0;
g_return_val_if_fail(file != NULL, -1);
if (encrypt) {
MimeInfo *mimeinfo;
+ save_clear_text = (mailac != NULL && mailac->save_encrypted_as_clear_text);
+
fclose(fp);
-
+ fp = NULL;
+
mimeinfo = procmime_scan_queue_file(file);
if (!privacy_encrypt(privacy_system, mimeinfo, encrypt_data)
|| (fp = my_tmpfile()) == NULL
g_free(encrypt_data);
return -1;
}
+
+ rewind(fp);
+ if (!save_clear_text) {
+ gchar *content = NULL;
+ FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
+ if (tmpfp) {
+ fclose(tmpfp);
+
+ content = file_read_stream_to_str(fp);
+ rewind(fp);
+ str_write_to_file(content, tmp_enc_file);
+ g_free(content);
+ } else {
+ g_warning("couldn't get tempfile\n");
+ }
+ }
+
procmime_mimeinfo_free_all(mimeinfo);
- rewind(fp);
+
filepos = 0;
}
}
if (mailac)
- mailval = send_message_smtp(mailac, to_list, fp);
+ mailval = send_message_smtp_full(mailac, to_list, fp, keep_session);
else {
PrefsAccount tmp_ac;
outbox = folder_find_item_from_identifier(savecopyfolder);
if (!outbox)
outbox = folder_get_default_outbox();
+
+ if (save_clear_text || tmp_enc_file == NULL) {
+ procmsg_save_to_outbox(outbox, file, TRUE);
+ } else {
+ procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE);
+ }
+ }
- procmsg_save_to_outbox(outbox, file, TRUE);
+ if (tmp_enc_file != NULL) {
+ unlink(tmp_enc_file);
+ free(tmp_enc_file);
+ tmp_enc_file = NULL;
}
if (replymessageid != NULL || fwdmessageid != NULL) {
return (newsval != 0 ? newsval : mailval);
}
+gint procmsg_send_message_queue(const gchar *file)
+{
+ return procmsg_send_message_queue_full(file, FALSE);
+}
+
static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
{
MsgPermFlags new_flags = msginfo->flags.perm_flags;
return FALSE;
}
+
+MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimeinfo)
+{
+ MsgInfo *tmp_msginfo = NULL;
+ MsgFlags flags = {0, 0};
+
+
+ if (!mimeinfo || mimeinfo->type != MIMETYPE_MESSAGE ||
+ g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
+ g_warning("procmsg_msginfo_new_from_mimeinfo(): unsuitable mimeinfo");
+ 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");
+ }
+
+ return tmp_msginfo;
+}