#include "procheader.h"
#include "prefs_account.h"
#include "codeconv.h"
+#include "md5.h"
+#include "base64.h"
#include "utils.h"
#include "inputdialog.h"
#include "log.h"
{
Session session;
+ gboolean authenticated;
+
gchar **capability;
+ gboolean uidplus;
+
gchar *mbox;
- time_t last_access_time;
- gboolean authenticated;
guint cmd_count;
+
+ /* CLAWS */
+ time_t last_access_time;
gboolean folder_content_changed;
guint exists;
};
#define IMAP_ERROR 7
#define IMAPBUFSIZE 8192
-#define IMAPCMDLIMIT 1000
typedef enum
{
#define IMAPS_PORT 993
#endif
+#define IMAP_CMD_LIMIT 1000
+
#define QUOTE_IF_REQUIRED(out, str) \
{ \
if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
static gchar *imap_fetch_msg(Folder * folder, FolderItem * item, gint uid);
static gint imap_add_msg(Folder * folder,
FolderItem * dest,
- const gchar * file, gboolean remove_source);
+ const gchar * file, MsgFlags * flags);
+static gint imap_add_msgs(Folder * folder, FolderItem * dest,
+ GSList * file_list,
+ GRelation *relation);
static gint imap_copy_msg(Folder * folder,
FolderItem * dest, MsgInfo * msginfo);
+static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
+ MsgInfoList *msglist, GRelation *relation);
static gint imap_remove_msg(Folder * folder, FolderItem * item, gint uid);
static gint imap_remove_all_msg(Folder * folder, FolderItem * item);
static gboolean imap_is_msg_changed(Folder * folder,
FolderItem * item, MsgInfo * msginfo);
+static gint imap_close(Folder * folder, FolderItem * item);
+
static void imap_scan_tree(Folder * folder);
static gint imap_create_tree(Folder * folder);
static IMAPSession *imap_session_get (Folder *folder);
+static gint imap_greeting (IMAPSession *session);
+static gint imap_auth (IMAPSession *session,
+ const gchar *user,
+ const gchar *pass,
+ IMAPAuthType type);
+
static gint imap_scan_tree_recursive (IMAPSession *session,
FolderItem *item);
static GSList *imap_parse_list (IMAPFolder *folder,
SpecialFolderItemType stype,
const gchar *name);
-static gint imap_do_copy (Folder *folder,
+static gint imap_do_copy_msgs (Folder *folder,
FolderItem *dest,
- MsgInfo *msginfo,
- gboolean remove_source);
+ MsgInfoList *msglist,
+ GRelation *relation);
static void imap_delete_all_cached_messages (FolderItem *item);
static SockInfo *imap_init_sock(SockInfo *sock);
#endif
+static gchar *imap_get_flag_str (IMAPFlags flags);
static gint imap_set_message_flags (IMAPSession *session,
MsgNumberList *numlist,
- IMAPFlags flag,
+ IMAPFlags flags,
gboolean is_set);
static gint imap_select (IMAPSession *session,
IMAPFolder *folder,
static MsgInfo *imap_parse_envelope (SockInfo *sock,
FolderItem *item,
GString *line_str);
-static gint imap_greeting (IMAPSession *session);
+
static gboolean imap_has_capability (IMAPSession *session,
const gchar *cap);
-void imap_free_capabilities (IMAPSession *session);
-static const IMAPSet numberlist_to_imapset
- (MsgNumberList *list);
+static void imap_free_capabilities (IMAPSession *session);
/* low-level IMAP4rev1 commands */
+static gint imap_cmd_authenticate
+ (IMAPSession *session,
+ const gchar *user,
+ const gchar *pass,
+ IMAPAuthType type);
static gint imap_cmd_login (IMAPSession *sock,
const gchar *user,
const gchar *pass);
static gint imap_cmd_append (IMAPSession *session,
const gchar *destfolder,
const gchar *file,
- gint32 *newuid);
-static gint imap_cmd_copy (IMAPSession *session,
- gint32 msgnum,
- const gchar *destfolder,
- gint32 *new_uid);
+ IMAPFlags flags,
+ guint32 *new_uid);
+static gint imap_cmd_copy (IMAPSession *session,
+ const gchar *seq_set,
+ const gchar *destfolder,
+ GRelation *uid_mapping);
static gint imap_cmd_store (IMAPSession *sock,
IMAPSet set,
gchar *sub_cmd);
static gint imap_cmd_expunge (IMAPSession *sock);
+static gint imap_cmd_close (IMAPSession *session);
static gint imap_cmd_ok (IMAPSession *session,
GPtrArray *argbuf);
static void imap_gen_send (IMAPSession *sock,
const gchar *format, ...);
static gint imap_gen_recv (IMAPSession *sock,
- gchar **buf);
+ gchar **ret);
/* misc utility functions */
static gchar *strchr_cpy (const gchar *src,
static void imap_path_separator_subst (gchar *str,
gchar separator);
-static gchar *imap_modified_utf7_to_locale (const gchar *mutf7_str);
-static gchar *imap_locale_to_modified_utf7 (const gchar *from);
+static gchar *imap_utf8_to_modified_utf7 (const gchar *from);
+static gchar *imap_modified_utf7_to_utf8 (const gchar *mutf7_str);
+
+static GSList *imap_get_seq_set_from_numlist (MsgNumberList *msglist);
+static GSList *imap_get_seq_set_from_msglist (MsgInfoList *msglist);
+static void imap_seq_set_free (GSList *seq_list);
static gboolean imap_rename_folder_func (GNode *node,
gpointer data);
static gint imap_get_num_list (Folder *folder,
FolderItem *item,
- GSList **list);
+ GSList **list,
+ gboolean *old_uids_valid);
static GSList *imap_get_msginfos (Folder *folder,
FolderItem *item,
GSList *msgnum_list);
static MsgInfo *imap_get_msginfo (Folder *folder,
FolderItem *item,
gint num);
-static gboolean imap_check_msgnum_validity (Folder *folder,
+static gboolean imap_scan_required (Folder *folder,
FolderItem *item);
static void imap_change_flags (Folder *folder,
FolderItem *item,
imap_create_folder,
imap_rename_folder,
imap_remove_folder,
+ imap_close,
imap_get_num_list,
NULL,
NULL,
NULL,
- imap_check_msgnum_validity,
+ imap_scan_required,
/* Message functions */
imap_get_msginfo,
imap_get_msginfos,
imap_fetch_msg,
imap_add_msg,
+ imap_add_msgs,
imap_copy_msg,
+ imap_copy_msgs,
imap_remove_msg,
imap_remove_all_msg,
imap_is_msg_changed,
g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
}
+/* Send CAPABILITY, and examine the server's response to see whether this
+ * connection is pre-authenticated or not and build a list of CAPABILITIES. */
+static gint imap_greeting(IMAPSession *session)
+{
+ gchar *capstr;
+ GPtrArray *argbuf;
+
+ imap_gen_send(session, "CAPABILITY");
+
+ argbuf = g_ptr_array_new();
+
+ if (imap_cmd_ok(session, argbuf) != IMAP_SUCCESS ||
+ ((capstr = search_array_str(argbuf, "CAPABILITY ")) == NULL)) {
+ ptr_array_free_strings(argbuf);
+ g_ptr_array_free(argbuf, TRUE);
+ return -1;
+ }
+
+ session->authenticated = search_array_str(argbuf, "PREAUTH") != NULL;
+
+ capstr += strlen("CAPABILITY ");
+
+ IMAP_SESSION(session)->capability = g_strsplit(capstr, " ", 0);
+
+ ptr_array_free_strings(argbuf);
+ g_ptr_array_free(argbuf, TRUE);
+
+ if (imap_has_capability(session, "UIDPLUS"))
+ session->uidplus = TRUE;
+
+ return 0;
+}
+
+static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
+ IMAPAuthType type)
+{
+ gint ok;
+
+ if (type == 0 || type == IMAP_AUTH_LOGIN)
+ ok = imap_cmd_login(session, user, pass);
+ else
+ ok = imap_cmd_authenticate(session, user, pass, type);
+
+ if (ok == IMAP_SUCCESS)
+ session->authenticated = TRUE;
+
+ return ok;
+}
+
static IMAPSession *imap_session_get(Folder *folder)
{
RemoteFolder *rfolder = REMOTE_FOLDER(folder);
IMAPSession *session = NULL;
- gushort port;
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
g_return_val_if_fail(folder->account != NULL, NULL);
-#if USE_OPENSSL
- port = folder->account->set_imapport ? folder->account->imapport
- : folder->account->ssl_imap == SSL_TUNNEL
- ? IMAPS_PORT : IMAP4_PORT;
-#else
- port = folder->account->set_imapport ? folder->account->imapport
- : IMAP4_PORT;
-#endif
-
/* Make sure we have a session */
if (rfolder->session != NULL) {
session = IMAP_SESSION(rfolder->session);
/* Check if this is the first try to establish a
connection, if yes we don't try to reconnect */
if (rfolder->session == NULL) {
- log_warning(_("Connecting %s:%d failed"),
- folder->account->recv_server, port);
+ log_warning(_("Connecting %s failed"),
+ folder->account->recv_server);
session_destroy(SESSION(session));
session = NULL;
} else {
- log_warning(_("IMAP4 connection to %s:%d has been"
+ log_warning(_("IMAP4 connection to %s has been"
" disconnected. Reconnecting...\n"),
- folder->account->recv_server, port);
+ folder->account->recv_server);
session_destroy(SESSION(session));
/* Clear folders session to make imap_session_get create
a new session, because of rfolder->session == NULL
session->capability = NULL;
- session->mbox = NULL;
session->authenticated = FALSE;
+ session->mbox = NULL;
session->cmd_count = 0;
/* Only need to log in if the connection was not PREAUTH */
}
#if USE_OPENSSL
- if (account->ssl_imap == SSL_STARTTLS && imap_has_capability(session, "STARTTLS")) {
+ if (account->ssl_imap == SSL_STARTTLS &&
+ imap_has_capability(session, "STARTTLS")) {
gint ok;
ok = imap_cmd_starttls(session);
imap_free_capabilities(session);
session->authenticated = FALSE;
+ session->uidplus = FALSE;
session->cmd_count = 1;
if (imap_greeting(session) != IMAP_SUCCESS) {
g_free(tmp_pass);
}
- if (imap_cmd_login(session, account->userid, pass) != IMAP_SUCCESS) {
+ if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
imap_cmd_logout(session);
return;
}
void imap_session_destroy(Session *session)
{
+ imap_free_capabilities(IMAP_SESSION(session));
+ g_free(IMAP_SESSION(session)->mbox);
sock_close(session->sock);
session->sock = NULL;
-
- g_free(IMAP_SESSION(session)->mbox);
- imap_free_capabilities(IMAP_SESSION(session));
}
gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
return filename;
}
-gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
- gboolean remove_source)
+gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
+{
+ gint ret;
+ GSList file_list;
+ MsgFileInfo fileinfo;
+
+ g_return_val_if_fail(file != NULL, -1);
+
+ fileinfo.msginfo = NULL;
+ fileinfo.file = (gchar *)file;
+ fileinfo.flags = flags;
+ file_list.data = &fileinfo;
+ file_list.next = NULL;
+
+ ret = imap_add_msgs(folder, dest, &file_list, NULL);
+ return ret;
+}
+
+gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
+ GRelation *relation)
{
gchar *destdir;
IMAPSession *session;
- gint ok, newuid;
+ guint32 last_uid = 0;
+ GSList *cur;
+ MsgFileInfo *fileinfo;
+ gint ok;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(dest != NULL, -1);
- g_return_val_if_fail(file != NULL, -1);
+ g_return_val_if_fail(file_list != NULL, -1);
session = imap_session_get(folder);
if (!session) return -1;
destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
- ok = imap_cmd_append(session, destdir, file, &newuid);
- g_free(destdir);
- if (ok != IMAP_SUCCESS) {
- g_warning("can't append message %s\n", file);
- return -1;
- }
+ for (cur = file_list; cur != NULL; cur = cur->next) {
+ IMAPFlags iflags = 0;
+ guint32 new_uid = 0;
+
+ fileinfo = (MsgFileInfo *)cur->data;
+
+ if (fileinfo->flags) {
+ if (MSG_IS_MARKED(*fileinfo->flags))
+ iflags |= IMAP_FLAG_FLAGGED;
+ if (MSG_IS_REPLIED(*fileinfo->flags))
+ iflags |= IMAP_FLAG_ANSWERED;
+ if (!MSG_IS_UNREAD(*fileinfo->flags))
+ iflags |= IMAP_FLAG_SEEN;
+ }
+
+ if (dest->stype == F_OUTBOX ||
+ dest->stype == F_QUEUE ||
+ dest->stype == F_DRAFT ||
+ dest->stype == F_TRASH)
+ iflags |= IMAP_FLAG_SEEN;
+
+ ok = imap_cmd_append(session, destdir, fileinfo->file, iflags,
+ &new_uid);
+
+ if (ok != IMAP_SUCCESS) {
+ g_warning("can't append message %s\n", fileinfo->file);
+ g_free(destdir);
+ return -1;
+ }
- if (remove_source) {
- if (unlink(file) < 0)
- FILE_OP_ERROR(file, "unlink");
+ if (relation != NULL)
+ g_relation_insert(relation, fileinfo->msginfo != NULL ?
+ (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
+ GINT_TO_POINTER(dest->last_num + 1));
+ if (last_uid < new_uid)
+ last_uid = new_uid;
}
- return newuid;
+ g_free(destdir);
+
+ return last_uid;
}
-static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
- gboolean remove_source)
+static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
+ MsgInfoList *msglist, GRelation *relation)
{
+ FolderItem *src;
gchar *destdir;
+ GSList *seq_list, *cur;
+ MsgInfo *msginfo;
IMAPSession *session;
- guint32 newuid = 0;
- gint ok;
+ gint ok = IMAP_SUCCESS;
+ GRelation *uid_mapping;
+ gint last_num = 0;
g_return_val_if_fail(folder != NULL, -1);
- g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
g_return_val_if_fail(dest != NULL, -1);
- g_return_val_if_fail(msginfo != NULL, -1);
+ g_return_val_if_fail(msglist != NULL, -1);
session = imap_session_get(folder);
if (!session) return -1;
- if (msginfo->folder == dest) {
+ msginfo = (MsgInfo *)msglist->data;
+
+ src = msginfo->folder;
+ if (src == dest) {
g_warning("the src folder is identical to the dest.\n");
return -1;
}
- destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
-
- /* ensure source folder selected */
ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
NULL, NULL, NULL, NULL);
if (ok != IMAP_SUCCESS)
- return -1;
-
- if (remove_source)
- debug_print("Moving message %s%c%d to %s ...\n",
- msginfo->folder->path, G_DIR_SEPARATOR,
- msginfo->msgnum, destdir);
- else
- debug_print("Copying message %s%c%d to %s ...\n",
- msginfo->folder->path, G_DIR_SEPARATOR,
- msginfo->msgnum, destdir);
+ return ok;
- ok = imap_cmd_copy(session, msginfo->msgnum, destdir, &newuid);
+ destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+ seq_list = imap_get_seq_set_from_msglist(msglist);
+ uid_mapping = g_relation_new(2);
+ g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
+
+ for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+ gchar *seq_set = (gchar *)cur->data;
- if (ok == IMAP_SUCCESS && remove_source) {
- MsgNumberList numlist;
+ debug_print("Copying message %s%c[%s] to %s ...\n",
+ src->path, G_DIR_SEPARATOR,
+ seq_set, destdir);
+
+ ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
+ if (ok != IMAP_SUCCESS) {
+ g_relation_destroy(uid_mapping);
+ imap_seq_set_free(seq_list);
+ return -1;
+ }
+ }
- numlist.next = NULL;
- numlist.data = GINT_TO_POINTER(msginfo->msgnum);
+ for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+ MsgInfo *msginfo = (MsgInfo *)cur->data;
+ GTuples *tuples;
- imap_set_message_flags(session, &numlist,
- IMAP_FLAG_DELETED, TRUE);
- ok = imap_cmd_expunge(session);
+ tuples = g_relation_select(uid_mapping,
+ GINT_TO_POINTER(msginfo->msgnum),
+ 0);
+ if (tuples->len > 0) {
+ gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
+ g_relation_insert(relation, msginfo,
+ GPOINTER_TO_INT(num));
+ if (num > last_num)
+ last_num = num;
+ } else
+ g_relation_insert(relation, msginfo,
+ GPOINTER_TO_INT(0));
+ g_tuples_destroy(tuples);
}
+
+ imap_seq_set_free(seq_list);
g_free(destdir);
if (ok == IMAP_SUCCESS)
- return newuid;
+ return last_num;
else
return -1;
}
gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
{
- gchar *srcfile;
- gint ret = 0;
+ GSList msglist;
+
+ g_return_val_if_fail(msginfo != NULL, -1);
+
+ msglist.data = msginfo;
+ msglist.next = NULL;
+
+ return imap_copy_msgs(folder, dest, &msglist, NULL);
+}
+
+gint imap_copy_msgs(Folder *folder, FolderItem *dest,
+ MsgInfoList *msglist, GRelation *relation)
+{
+ MsgInfo *msginfo;
+ GSList *file_list;
+ gint ret;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(dest != NULL, -1);
- g_return_val_if_fail(msginfo != NULL, -1);
+ g_return_val_if_fail(msglist != NULL, -1);
+
+ msginfo = (MsgInfo *)msglist->data;
g_return_val_if_fail(msginfo->folder != NULL, -1);
if (folder == msginfo->folder->folder)
- return imap_do_copy(folder, dest, msginfo, FALSE);
+ return imap_do_copy_msgs(folder, dest, msglist, relation);
- srcfile = procmsg_get_message_file(msginfo);
- if (!srcfile) return -1;
+ file_list = procmsg_get_message_file_list(msglist);
+ g_return_val_if_fail(file_list != NULL, -1);
- ret = imap_add_msg(folder, dest, srcfile, FALSE);
+ ret = imap_add_msgs(folder, dest, file_list, relation);
- g_free(srcfile);
+ procmsg_message_file_list_free(file_list);
return ret;
}
gint imap_remove_all_msg(Folder *folder, FolderItem *item)
{
- gint exists, recent, unseen;
- guint32 uid_validity;
gint ok;
IMAPSession *session;
gchar *dir;
if (!session) return -1;
ok = imap_select(session, IMAP_FOLDER(folder), item->path,
- &exists, &recent, &unseen, &uid_validity);
+ NULL, NULL, NULL, NULL);
if (ok != IMAP_SUCCESS)
return ok;
- if (exists == 0)
- return IMAP_SUCCESS;
- imap_gen_send(session,
- "STORE 1:%d +FLAGS (\\Deleted)", exists);
+ imap_gen_send(session, "STORE 1:* +FLAGS.SILENT (\\Deleted)");
ok = imap_cmd_ok(session, NULL);
if (ok != IMAP_SUCCESS) {
- log_warning(_("can't set deleted flags: 1:%d\n"), exists);
+ log_warning(_("can't set deleted flags: 1:*\n"));
return ok;
}
return FALSE;
}
+gint imap_close(Folder *folder, FolderItem *item)
+{
+ gint ok;
+ IMAPSession *session;
+
+ g_return_val_if_fail(folder != NULL, -1);
+ g_return_val_if_fail(item != NULL, -1);
+ g_return_val_if_fail(item->path != NULL, -1);
+
+ session = imap_session_get(folder);
+ if (!session) return -1;
+
+ if (session->mbox) {
+ if (strcmp(item->path, session->mbox))
+ return -1;
+
+ ok = imap_cmd_close(session);
+ if (ok != IMAP_SUCCESS)
+ log_warning(_("can't close folder\n"));
+
+ g_free(session->mbox);
+ session->mbox = NULL;
+
+ return ok;
+ }
+
+ return 0;
+}
+
void imap_scan_tree(Folder *folder)
{
- FolderItem *item;
+ FolderItem *item = NULL;
IMAPSession *session;
gchar *root_folder = NULL;
folder_tree_destroy(folder);
item = folder_item_new(folder, folder->name, NULL);
item->folder = folder;
- folder->node = g_node_new(item);
+ folder->node = item->node = g_node_new(item);
}
return;
}
debug_print("IMAP root directory: %s\n", root_folder);
}
- item = folder_item_new(folder, folder->name, root_folder);
- item->folder = folder;
- item->no_select = TRUE;
- folder->node = g_node_new(item);
-
- imap_scan_tree_recursive(session, item);
+ if (folder->node)
+ item = FOLDER_ITEM(folder->node->data);
+ if (!item || ((item->path || root_folder) &&
+ strcmp2(item->path, root_folder) != 0)) {
+ folder_tree_destroy(folder);
+ item = folder_item_new(folder, folder->name, root_folder);
+ item->folder = folder;
+ folder->node = item->node = g_node_new(item);
+ }
+ imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
imap_create_missing_folders(folder);
}
IMAPFolder *imapfolder;
FolderItem *new_item;
GSList *item_list, *cur;
+ GNode *node;
gchar *real_path;
gchar *wildcard_path;
gchar separator;
g_return_val_if_fail(item->folder != NULL, -1);
g_return_val_if_fail(item->no_sub == FALSE, -1);
- folder = FOLDER(item->folder);
+ folder = item->folder;
imapfolder = IMAP_FOLDER(folder);
separator = imap_get_path_separator(imapfolder, item->path);
- if (item->folder->ui_func)
- item->folder->ui_func(folder, item, folder->ui_func_data);
+ if (folder->ui_func)
+ folder->ui_func(folder, item, folder->ui_func_data);
if (item->path) {
wildcard[0] = separator;
item_list = imap_parse_list(imapfolder, session, real_path, NULL);
g_free(real_path);
+ node = item->node->children;
+ while (node != NULL) {
+ FolderItem *old_item = FOLDER_ITEM(node->data);
+ GNode *next = node->next;
+
+ new_item = NULL;
+ for (cur = item_list; cur != NULL; cur = cur->next) {
+ FolderItem *cur_item = FOLDER_ITEM(cur->data);
+ if (!strcmp2(old_item->path, cur_item->path)) {
+ new_item = cur_item;
+ break;
+ }
+ }
+ if (!new_item) {
+ debug_print("folder '%s' not found. removing...\n",
+ old_item->path);
+ folder_item_remove(old_item);
+ } else {
+ old_item->no_sub = new_item->no_sub;
+ old_item->no_select = new_item->no_select;
+ if (old_item->no_sub == TRUE && node->children) {
+ debug_print("folder '%s' doesn't have "
+ "subfolders. removing...\n",
+ old_item->path);
+ folder_item_remove_children(old_item);
+ }
+ }
+
+ node = next;
+ }
+
for (cur = item_list; cur != NULL; cur = cur->next) {
- new_item = cur->data;
- if (!strcmp(new_item->path, "INBOX")) {
- if (!folder->inbox) {
- new_item->stype = F_INBOX;
- item->folder->inbox = new_item;
- } else {
- folder_item_destroy(new_item);
- continue;
+ FolderItem *cur_item = FOLDER_ITEM(cur->data);
+ new_item = NULL;
+ for (node = item->node->children; node != NULL;
+ node = node->next) {
+ if (!strcmp2(FOLDER_ITEM(node->data)->path,
+ cur_item->path)) {
+ new_item = FOLDER_ITEM(node->data);
+ folder_item_destroy(cur_item);
+ cur_item = NULL;
+ break;
}
+ }
+ if (!new_item) {
+ new_item = cur_item;
+ debug_print("new folder '%s' found.\n", new_item->path);
+ folder_item_append(item, new_item);
+ }
+
+ if (!strcmp(new_item->path, "INBOX")) {
+ new_item->stype = F_INBOX;
+ folder->inbox = new_item;
} else if (!item->parent || item->stype == F_INBOX) {
- gchar *base;
+ const gchar *base;
base = g_basename(new_item->path);
folder->trash = new_item;
}
}
- folder_item_append(item, new_item);
+
if (new_item->no_sub == FALSE)
imap_scan_tree_recursive(session, new_item);
}
+ g_slist_free(item_list);
+
return IMAP_SUCCESS;
}
gchar flags[256];
gchar separator_str[16];
gchar *p;
- gchar *name;
+ const gchar *name;
gchar *loc_name, *loc_path;
GSList *item_list = NULL;
GString *str;
name = g_basename(buf);
if (name[0] == '.') continue;
- loc_name = imap_modified_utf7_to_locale(name);
- loc_path = imap_modified_utf7_to_locale(buf);
+ loc_name = imap_modified_utf7_to_utf8(name);
+ loc_path = imap_modified_utf7_to_utf8(buf);
new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
if (strcasestr(flags, "\\Noinferiors") != NULL)
new_item->no_sub = TRUE;
item_list = g_slist_append(item_list, new_item);
- debug_print("folder %s has been added.\n", loc_path);
+ debug_print("folder '%s' found.\n", loc_path);
g_free(loc_path);
g_free(loc_name);
}
/* keep trailing directory separator to create a folder that contains
sub folder */
- imap_path = imap_locale_to_modified_utf7(dirpath);
+ imap_path = imap_utf8_to_modified_utf7(dirpath);
strtailchomp(dirpath, '/');
Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
strtailchomp(new_name, '/');
gchar *newpath;
gchar *real_oldpath;
gchar *real_newpath;
- GNode *node;
gchar *paths[2];
gchar *old_cache_dir;
gchar *new_cache_dir;
} else
newpath = g_strdup(name);
- real_newpath = imap_locale_to_modified_utf7(newpath);
+ real_newpath = imap_utf8_to_modified_utf7(newpath);
imap_path_separator_subst(real_newpath, separator);
ok = imap_cmd_rename(session, real_oldpath, real_newpath);
old_cache_dir = folder_item_get_path(item);
- node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
- item);
paths[0] = g_strdup(item->path);
paths[1] = newpath;
- g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+ g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
imap_rename_folder_func, paths);
if (is_dir_exist(old_cache_dir)) {
GSList *llast = NULL;
GString *str;
MsgInfo *msginfo;
+ GSList *seq_list, *cur;
IMAPSet imapset;
g_return_val_if_fail(session != NULL, NULL);
g_return_val_if_fail(item->folder != NULL, NULL);
g_return_val_if_fail(FOLDER_CLASS(item->folder) == &imap_class, NULL);
- imapset = numberlist_to_imapset(numlist);
- while (imapset != NULL) {
+ seq_list = imap_get_seq_set_from_numlist(numlist);
+ for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+ imapset = cur->data;
+
if (imap_cmd_envelope(session, imapset)
!= IMAP_SUCCESS) {
log_warning(_("can't get envelope\n"));
}
g_string_free(str, TRUE);
- imapset = numberlist_to_imapset(NULL);
}
+ imap_seq_set_free(seq_list);
return newlist;
}
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(path != NULL, NULL);
- real_path = imap_locale_to_modified_utf7(path);
+ real_path = imap_utf8_to_modified_utf7(path);
separator = imap_get_path_separator(folder, path);
imap_path_separator_subst(real_path, separator);
return msginfo;
}
+static gchar *imap_get_flag_str(IMAPFlags flags)
+{
+ GString *str;
+ gchar *ret;
+
+ str = g_string_new(NULL);
+
+ if (IMAP_IS_SEEN(flags)) g_string_append(str, "\\Seen ");
+ if (IMAP_IS_ANSWERED(flags)) g_string_append(str, "\\Answered ");
+ if (IMAP_IS_FLAGGED(flags)) g_string_append(str, "\\Flagged ");
+ if (IMAP_IS_DELETED(flags)) g_string_append(str, "\\Deleted ");
+ if (IMAP_IS_DRAFT(flags)) g_string_append(str, "\\Draft");
+
+ if (str->len > 0 && str->str[str->len - 1] == ' ')
+ g_string_truncate(str, str->len - 1);
+
+ ret = str->str;
+ g_string_free(str, FALSE);
+
+ return ret;
+}
+
static gint imap_set_message_flags(IMAPSession *session,
MsgNumberList *numlist,
IMAPFlags flags,
gboolean is_set)
{
- GString *buf;
+ gchar *cmd;
+ gchar *flag_str;
gint ok;
+ GSList *seq_list, *cur;
IMAPSet imapset;
- buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
-
- if (IMAP_IS_SEEN(flags)) g_string_append(buf, "\\Seen ");
- if (IMAP_IS_ANSWERED(flags)) g_string_append(buf, "\\Answered ");
- if (IMAP_IS_FLAGGED(flags)) g_string_append(buf, "\\Flagged ");
- if (IMAP_IS_DELETED(flags)) g_string_append(buf, "\\Deleted ");
- if (IMAP_IS_DRAFT(flags)) g_string_append(buf, "\\Draft");
-
- if (buf->str[buf->len - 1] == ' ')
- g_string_truncate(buf, buf->len - 1);
+ flag_str = imap_get_flag_str(flags);
+ cmd = g_strconcat(is_set ? "+FLAGS.SILENT (" : "-FLAGS.SILENT (",
+ flag_str, ")", NULL);
+ g_free(flag_str);
- g_string_append_c(buf, ')');
+ seq_list = imap_get_seq_set_from_numlist(numlist);
+ for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+ imapset = cur->data;
- imapset = numberlist_to_imapset(numlist);
- while (imapset != NULL) {
- ok = imap_cmd_store(session, imapset,
- buf->str);
- imapset = numberlist_to_imapset(NULL);
+ ok = imap_cmd_store(session, imapset, cmd);
}
- g_string_free(buf, TRUE);
+ imap_seq_set_free(seq_list);
+ g_free(cmd);
return ok;
}
*messages = *recent = *uid_next = *uid_validity = *unseen = 0;
+ if (path == NULL)
+ return -1;
+
argbuf = g_ptr_array_new();
real_path = imap_get_real_path(folder, path);
#undef THROW
-
-/* low-level IMAP4rev1 commands */
-
-static gint imap_cmd_login(IMAPSession *session,
- const gchar *user, const gchar *pass)
-{
- gchar *user_, *pass_;
- gint ok;
-
- QUOTE_IF_REQUIRED(user_, user);
- QUOTE_IF_REQUIRED(pass_, pass);
- imap_gen_send(session, "LOGIN %s %s", user_, pass_);
-
- ok = imap_cmd_ok(session, NULL);
- if (ok != IMAP_SUCCESS)
- log_warning(_("IMAP4 login failed.\n"));
-
- return ok;
-}
-
-static gint imap_cmd_logout(IMAPSession *session)
-{
- imap_gen_send(session, "LOGOUT");
- return imap_cmd_ok(session, NULL);
-}
-
-/* Send CAPABILITY, and examine the server's response to see whether this
- * connection is pre-authenticated or not and build a list of CAPABILITIES. */
-static gint imap_greeting(IMAPSession *session)
-{
- gchar *capstr;
- GPtrArray *argbuf;
-
- imap_gen_send(session, "CAPABILITY");
-
- argbuf = g_ptr_array_new();
-
- if (imap_cmd_ok(session, argbuf) != IMAP_SUCCESS ||
- ((capstr = search_array_str(argbuf, "CAPABILITY ")) == NULL)) {
- ptr_array_free_strings(argbuf);
- g_ptr_array_free(argbuf, TRUE);
- return -1;
- }
-
- session->authenticated = search_array_str(argbuf, "PREAUTH") != NULL;
-
- capstr += strlen("CAPABILITY ");
-
- IMAP_SESSION(session)->capability = g_strsplit(capstr, " ", 0);
-
- ptr_array_free_strings(argbuf);
- g_ptr_array_free(argbuf, TRUE);
-
- return 0;
-}
-
static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
{
gchar **p;
- for (p = session->capability; *p != NULL; ++p)
- if (g_strcasecmp(*p, cap) == 0)
+ for (p = session->capability; *p != NULL; ++p) {
+ if (!g_strcasecmp(*p, cap))
return TRUE;
+ }
return FALSE;
}
session->capability = NULL;
}
-static const IMAPSet numberlist_to_imapset(MsgNumberList *list)
+/* low-level IMAP4rev1 commands */
+
+static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
+ const gchar *pass, IMAPAuthType type)
{
- static GString *imapset = NULL;
- static MsgNumberList *numlist, *elem;
- guint first, last, next;
+ gchar *auth_type;
+ gint ok;
+ gchar *buf = NULL;
+ gchar *challenge;
+ gint challenge_len;
+ gchar hexdigest[33];
+ gchar *response;
+ gchar *response64;
+
+ auth_type = "CRAM-MD5";
+
+ imap_gen_send(session, "AUTHENTICATE %s", auth_type);
+ ok = imap_gen_recv(session, &buf);
+ if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
+ g_free(buf);
+ return IMAP_ERROR;
+ }
- if (imapset == NULL)
- imapset = g_string_sized_new(256);
- else
- g_string_truncate(imapset, 0);
+ challenge = g_malloc(strlen(buf + 2) + 1);
+ challenge_len = base64_decode(challenge, buf + 2, -1);
+ challenge[challenge_len] = '\0';
+ g_free(buf);
+ log_print("IMAP< [Decoded: %s]\n", challenge);
- if (list != NULL) {
- g_slist_free(numlist);
- numlist = g_slist_copy(list);
- numlist = g_slist_sort(numlist, g_int_compare);
- } else if (numlist == NULL) {
- return NULL;
- }
+ md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
+ g_free(challenge);
- first = GPOINTER_TO_INT(numlist->data);
- last = first;
- for(elem = g_slist_next(numlist); elem != NULL; elem = g_slist_next(elem)) {
- next = GPOINTER_TO_INT(elem->data);
+ response = g_strdup_printf("%s %s", user, hexdigest);
+ log_print("IMAP> [Encoded: %s]\n", response);
+ response64 = g_malloc((strlen(response) + 3) * 2 + 1);
+ base64_encode(response64, response, strlen(response));
+ g_free(response);
- if(next != (last + 1)) {
- if (imapset->len > 0)
- g_string_append(imapset, ",");
- if (first == last)
- g_string_sprintfa(imapset, "%d", first);
- else
- g_string_sprintfa(imapset, "%d:%d", first, last);
+ log_print("IMAP> %s\n", response64);
+ sock_puts(SESSION(session)->sock, response64);
+ ok = imap_cmd_ok(session, NULL);
+ if (ok != IMAP_SUCCESS)
+ log_warning(_("IMAP4 authentication failed.\n"));
- if (imapset->len > IMAPCMDLIMIT) {
- last = 0;
- break;
- }
+ return ok;
+}
- first = next;
- }
- last = next;
- }
- if (last != 0) {
- if (imapset->len > 0)
- g_string_append(imapset, ",");
- if (first == last)
- g_string_sprintfa(imapset, "%d", first);
- else
- g_string_sprintfa(imapset, "%d:%d", first, last);
+static gint imap_cmd_login(IMAPSession *session,
+ const gchar *user, const gchar *pass)
+{
+ gchar *user_, *pass_;
+ gint ok;
- g_slist_free(numlist);
- numlist = NULL;
- } else {
- MsgNumberList *remaining;
+ QUOTE_IF_REQUIRED(user_, user);
+ QUOTE_IF_REQUIRED(pass_, pass);
+ imap_gen_send(session, "LOGIN %s %s", user_, pass_);
- remaining = elem->next;
- remaining = g_slist_prepend(remaining, elem->data);
- elem->next = NULL;
- g_slist_free(numlist);
- numlist = remaining;
- }
+ ok = imap_cmd_ok(session, NULL);
+ if (ok != IMAP_SUCCESS)
+ log_warning(_("IMAP4 login failed.\n"));
- return imapset->str;
+ return ok;
+}
+
+static gint imap_cmd_logout(IMAPSession *session)
+{
+ imap_gen_send(session, "LOGOUT");
+ return imap_cmd_ok(session, NULL);
}
static gint imap_cmd_noop(IMAPSession *session)
static gint imap_cmd_fetch(IMAPSession *session, guint32 uid, const gchar *filename)
{
gint ok;
- gchar *buf;
+ gchar *buf = NULL;
gchar *cur_pos;
gchar size_str[32];
glong size_num;
imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
- while ((ok = imap_gen_recv(session, &buf))
- == IMAP_SUCCESS) {
+ while ((ok = imap_gen_recv(session, &buf)) == IMAP_SUCCESS) {
if (buf[0] != '*' || buf[1] != ' ') {
g_free(buf);
return IMAP_ERROR;
}
- if (strstr(buf, "FETCH") != NULL)
- break;
+ if (strstr(buf, "FETCH") != NULL) break;
g_free(buf);
}
if (ok != IMAP_SUCCESS) {
return ok;
}
- cur_pos = strchr(buf, '{');
- if (cur_pos == NULL) {
- g_free(buf);
- return IMAP_ERROR;
+#define RETURN_ERROR_IF_FAIL(cond) \
+ if (!(cond)) { \
+ g_free(buf); \
+ return IMAP_ERROR; \
}
+ cur_pos = strchr(buf, '{');
+ RETURN_ERROR_IF_FAIL(cur_pos != NULL);
cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
- if (cur_pos == NULL) {
- g_free(buf);
- return IMAP_ERROR;
- }
+ RETURN_ERROR_IF_FAIL(cur_pos != NULL);
size_num = atol(size_str);
- g_return_val_if_fail(size_num > 0, IMAP_ERROR);
+ RETURN_ERROR_IF_FAIL(size_num > 0);
- if (*cur_pos != '\0') {
- g_free(buf);
- return IMAP_ERROR;
- }
+ RETURN_ERROR_IF_FAIL(*cur_pos == '\0');
- if (recv_bytes_write_to_file(SESSION(session)->sock, size_num, filename) != 0) {
- g_free(buf);
+#undef RETURN_ERROR_IF_FAIL
+
+ g_free(buf);
+
+ if (recv_bytes_write_to_file(SESSION(session)->sock, size_num, filename) != 0)
return IMAP_ERROR;
- }
if (imap_gen_recv(session, &buf) != IMAP_SUCCESS) {
g_free(buf);
g_free(buf);
return IMAP_ERROR;
}
-
g_free(buf);
+
ok = imap_cmd_ok(session, NULL);
return ok;
}
static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
- const gchar *file, gint32 *new_uid)
+ const gchar *file, IMAPFlags flags, guint32 *new_uid)
{
gint ok;
- gint size, newuid;
+ gint size;
gchar *destfolder_;
- gchar buf[BUFFSIZE], *imapbuf;
+ gchar *flag_str;
+ guint32 new_uid_;
+ gchar *ret = NULL;
+ gchar buf[BUFFSIZE];
FILE *fp;
- GPtrArray *reply;
- gchar *okmsginfo;
+ GPtrArray *argbuf;
+ gchar *resp_str;
g_return_val_if_fail(file != NULL, IMAP_ERROR);
return -1;
}
QUOTE_IF_REQUIRED(destfolder_, destfolder);
- imap_gen_send(session, "APPEND %s (\\Seen) {%d}", destfolder_, size);
+ flag_str = imap_get_flag_str(flags);
+ imap_gen_send(session, "APPEND %s (%s) {%d}",
+ destfolder_, flag_str, size);
+ g_free(flag_str);
- ok = imap_gen_recv(session, &imapbuf);
- if (ok != IMAP_SUCCESS || imapbuf[0] != '+' || imapbuf[1] != ' ') {
+ ok = imap_gen_recv(session, &ret);
+ if (ok != IMAP_SUCCESS || ret[0] != '+' || ret[1] != ' ') {
log_warning(_("can't append %s to %s\n"), file, destfolder_);
- g_free(imapbuf);
+ g_free(ret);
fclose(fp);
return IMAP_ERROR;
}
- g_free(imapbuf);
+ g_free(ret);
log_print("IMAP4> %s\n", _("(sending file...)"));
fclose(fp);
- reply = g_ptr_array_new();
+ if (new_uid != NULL)
+ *new_uid = 0;
- *new_uid = 0;
- ok = imap_cmd_ok(session, reply);
- if (ok != IMAP_SUCCESS)
- log_warning(_("can't append message to %s\n"), destfolder_);
- else if (
- (new_uid != NULL) &&
- (imap_has_capability(session, "UIDPLUS") && reply->len > 0) &&
- ((okmsginfo = g_ptr_array_index(reply, reply->len - 1)) != NULL) &&
- (sscanf(okmsginfo, "%*u OK [APPENDUID %*u %u]", &newuid) == 1)) {
- *new_uid = newuid;
- }
+ if (new_uid != NULL && session->uidplus) {
+ argbuf = g_ptr_array_new();
+
+ ok = imap_cmd_ok(session, argbuf);
+ if (ok != IMAP_SUCCESS)
+ log_warning(_("can't append message to %s\n"),
+ destfolder_);
+ else if (argbuf->len > 0) {
+ resp_str = g_ptr_array_index(argbuf, argbuf->len - 1);
+ if (resp_str &&
+ sscanf(resp_str, "%*u OK [APPENDUID %*u %u]",
+ &new_uid_) == 1) {
+ *new_uid = new_uid_;
+ }
+ }
+
+ ptr_array_free_strings(argbuf);
+ g_ptr_array_free(argbuf, TRUE);
+ } else
+ ok = imap_cmd_ok(session, NULL);
- ptr_array_free_strings(reply);
- g_ptr_array_free(reply, TRUE);
return ok;
}
-
-static gint imap_cmd_copy(IMAPSession * session,
- gint32 msgnum,
- const gchar * destfolder, gint32 * new_uid)
+static gint imap_cmd_copy(IMAPSession *session, const gchar *seq_set,
+ const gchar *destfolder, GRelation *uid_mapping)
{
gint ok;
- gint32 olduid, newuid;
- gchar *okmsginfo;
gchar *destfolder_;
GPtrArray *reply;
-
- g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
+
g_return_val_if_fail(session != NULL, IMAP_ERROR);
- g_return_val_if_fail(new_uid != NULL, IMAP_ERROR);
+ g_return_val_if_fail(seq_set != NULL, IMAP_ERROR);
+ g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
QUOTE_IF_REQUIRED(destfolder_, destfolder);
- imap_gen_send(session, "UID COPY %d %s", msgnum, destfolder_);
+ imap_gen_send(session, "UID COPY %s %s", seq_set, destfolder_);
reply = g_ptr_array_new();
- *new_uid = 0;
ok = imap_cmd_ok(session, reply);
if (ok != IMAP_SUCCESS)
- log_warning(_("can't copy %d to %s\n"), msgnum, destfolder_);
+ log_warning(_("can't copy %s to %s\n"), seq_set, destfolder_);
+/*
+ TODO: UIDPLUS
+
+ - split IMAPSets into uids
+ - g_relation_insert(uid_mapping, olduid, newuid);
+
else if (imap_has_capability(session, "UIDPLUS") && reply->len > 0)
if ((okmsginfo = g_ptr_array_index(reply, reply->len - 1)) != NULL &&
sscanf(okmsginfo, "%*u OK [COPYUID %*u %u %u]", &olduid, &newuid) == 2 &&
olduid == msgnum)
*new_uid = newuid;
-
+*/
ptr_array_free_strings(reply);
g_ptr_array_free(reply, TRUE);
return ok;
return IMAP_SUCCESS;
}
-static gint imap_cmd_store(IMAPSession *session, IMAPSet set,
+static gint imap_cmd_store(IMAPSession *session, IMAPSet seq_set,
gchar *sub_cmd)
{
gint ok;
- imap_gen_send(session, "UID STORE %s %s",
- set, sub_cmd);
+ imap_gen_send(session, "UID STORE %s %s", seq_set, sub_cmd);
if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
log_warning(_("error while imap command: STORE %s %s\n"),
- set, sub_cmd);
+ seq_set, sub_cmd);
return ok;
}
return IMAP_SUCCESS;
}
+static gint imap_cmd_close(IMAPSession *session)
+{
+ gint ok;
+
+ imap_gen_send(session, "CLOSE");
+ if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS)
+ log_warning(_("error while imap command: CLOSE\n"));
+
+ return ok;
+}
+
static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
{
gint ok = IMAP_SUCCESS;
g_free(buf);
}
-static gint imap_gen_recv(IMAPSession *session, gchar **buf)
+static gint imap_gen_recv(IMAPSession *session, gchar **ret)
{
- if ((*buf = sock_getline(SESSION(session)->sock)) == NULL)
+ if ((*ret = sock_getline(SESSION(session)->sock)) == NULL)
return IMAP_SOCKET;
- strretchomp(*buf);
+ strretchomp(*ret);
- log_print("IMAP4< %s\n", *buf);
+ log_print("IMAP4< %s\n", *ret);
return IMAP_SUCCESS;
}
}
}
-static gchar *imap_modified_utf7_to_locale(const gchar *mutf7_str)
+static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
{
#if !HAVE_ICONV
const gchar *from_p;
if (!iconv_ok) return g_strdup(mutf7_str);
if (cd == (iconv_t)-1) {
- cd = iconv_open(conv_get_current_charset_str(), "UTF-7");
+ cd = iconv_open(conv_get_current_charset_str(), CS_UTF_8);
if (cd == (iconv_t)-1) {
- g_warning("iconv cannot convert UTF-7 to %s\n",
- conv_get_current_charset_str());
+ g_warning(_("iconv cannot convert UTF-7 to UTF-8\n"));
iconv_ok = FALSE;
return g_strdup(mutf7_str);
}
if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
&to_p, &to_len) == -1) {
- g_warning(_("iconv cannot convert UTF-7 to %s\n"),
- conv_get_current_charset_str());
+ g_warning(_("iconv cannot convert UTF-7 to UTF-8\n"));
g_string_free(norm_utf7, TRUE);
g_free(to_str);
return g_strdup(mutf7_str);
#endif /* !HAVE_ICONV */
}
-static gchar *imap_locale_to_modified_utf7(const gchar *from)
+static gchar *imap_utf8_to_modified_utf7(const gchar *from)
{
#if !HAVE_ICONV
const gchar *from_p;
if (!iconv_ok) return g_strdup(from);
if (cd == (iconv_t)-1) {
- cd = iconv_open("UTF-7", conv_get_current_charset_str());
+ cd = iconv_open("UTF-7", CS_UTF_8);
if (cd == (iconv_t)-1) {
- g_warning("iconv cannot convert %s to UTF-7\n",
- conv_get_current_charset_str());
+ g_warning("iconv cannot convert UTF-8 to UTF-7\n");
iconv_ok = FALSE;
return g_strdup(from);
}
if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
&conv_len,
&norm_utf7_p, &norm_utf7_len) == -1) {
- g_warning("iconv cannot convert %s to UTF-7\n",
- conv_get_current_charset_str());
+ g_warning(_("iconv cannot convert UTF-8 to UTF-7\n"));
return g_strdup(from);
}
#endif /* !HAVE_ICONV */
}
+static GSList *imap_get_seq_set_from_numlist(MsgNumberList *numlist)
+{
+ GString *str;
+ GSList *sorted_list, *cur;
+ guint first, last, next;
+ gchar *ret_str;
+ GSList *ret_list = NULL;
+
+ if (numlist == NULL)
+ return NULL;
+
+ str = g_string_sized_new(256);
+
+ sorted_list = g_slist_copy(numlist);
+ sorted_list = g_slist_sort(sorted_list, g_int_compare);
+
+ first = GPOINTER_TO_INT(sorted_list->data);
+
+ for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
+ last = GPOINTER_TO_INT(cur->data);
+ if (cur->next)
+ next = GPOINTER_TO_INT(cur->next->data);
+ else
+ next = 0;
+
+ if (last + 1 != next || next == 0) {
+ if (str->len > 0)
+ g_string_append_c(str, ',');
+ if (first == last)
+ g_string_sprintfa(str, "%u", first);
+ else
+ g_string_sprintfa(str, "%u:%u", first, last);
+
+ first = next;
+
+ if (str->len > IMAP_CMD_LIMIT) {
+ ret_str = g_strdup(str->str);
+ ret_list = g_slist_append(ret_list, ret_str);
+ g_string_truncate(str, 0);
+ }
+ }
+ }
+
+ if (str->len > 0) {
+ ret_str = g_strdup(str->str);
+ ret_list = g_slist_append(ret_list, ret_str);
+ }
+
+ g_slist_free(sorted_list);
+ g_string_free(str, TRUE);
+
+ return ret_list;
+}
+
+static GSList *imap_get_seq_set_from_msglist(MsgInfoList *msglist)
+{
+ MsgNumberList *numlist = NULL;
+ MsgInfoList *cur;
+ GSList *seq_list;
+
+ for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+ MsgInfo *msginfo = (MsgInfo *) cur->data;
+
+ numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+ }
+ seq_list = imap_get_seq_set_from_numlist(numlist);
+ g_slist_free(numlist);
+
+ return seq_list;
+}
+
+static void imap_seq_set_free(GSList *seq_list)
+{
+ slist_free_strings(seq_list);
+ g_slist_free(seq_list);
+}
+
+
static gboolean imap_rename_folder_func(GNode *node, gpointer data)
{
FolderItem *item = node->data;
return nummsgs;
}
-gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list)
+gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
{
IMAPFolderItem *item = (IMAPFolderItem *)_item;
IMAPSession *session;
- gint ok, nummsgs = 0, exists, recent, unseen, uid_val, uid_next;
+ gint ok, nummsgs = 0, exists, recent, uid_val, uid_next, unseen;
GSList *uidlist;
gchar *dir;
gboolean selected_folder;
if (ok != IMAP_SUCCESS)
return -1;
exists = session->exists;
+
+ *old_uids_valid = TRUE;
} else {
ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
&exists, &recent, &uid_next, &uid_val, &unseen);
if (ok != IMAP_SUCCESS)
return -1;
+
+ if(item->item.mtime == uid_val)
+ *old_uids_valid = TRUE;
+ else {
+ *old_uids_valid = FALSE;
+
+ debug_print("Freeing imap uid cache\n");
+ item->lastuid = 0;
+ g_slist_free(item->uid_list);
+ item->uid_list = NULL;
+
+ item->item.mtime = uid_val;
+
+ imap_delete_all_cached_messages((FolderItem *)item);
+ }
}
+ if (!selected_folder)
+ item->uid_next = uid_next;
+
/* If old uid_next matches new uid_next we can be sure no message
was added to the folder */
if (( selected_folder && !session->folder_content_changed) ||
item->uid_list = NULL;
}
}
- if (!selected_folder)
- item->uid_next = uid_next;
if (exists == 0) {
*msgnum_list = NULL;
return msginfo;
}
-gboolean imap_check_msgnum_validity(Folder *folder, FolderItem *_item)
+gboolean imap_scan_required(Folder *folder, FolderItem *_item)
{
IMAPSession *session;
IMAPFolderItem *item = (IMAPFolderItem *)_item;
gint ok, exists = 0, recent = 0, unseen = 0;
- guint32 uid_next, uid_validity = 0;
+ guint32 uid_next, uid_val = 0;
+ gboolean selected_folder;
g_return_val_if_fail(folder != NULL, FALSE);
g_return_val_if_fail(item != NULL, FALSE);
g_return_val_if_fail(item->item.folder != NULL, FALSE);
g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
+ if (item->item.path == NULL)
+ return FALSE;
+
session = imap_session_get(folder);
g_return_val_if_fail(session != NULL, FALSE);
- ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
- &exists, &recent, &uid_next, &uid_validity, &unseen);
- if (ok != IMAP_SUCCESS)
- return FALSE;
-
- if(item->item.mtime == uid_validity)
- return TRUE;
+ selected_folder = (session->mbox != NULL) &&
+ (!strcmp(session->mbox, item->item.path));
+ if (selected_folder) {
+ ok = imap_cmd_noop(session);
+ if (ok != IMAP_SUCCESS)
+ return FALSE;
- debug_print("Freeing imap uid cache\n");
- item->lastuid = 0;
- g_slist_free(item->uid_list);
- item->uid_list = NULL;
-
- item->item.mtime = uid_validity;
+ if (session->folder_content_changed)
+ return TRUE;
+ } else {
+ ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
+ &exists, &recent, &uid_next, &uid_val, &unseen);
+ if (ok != IMAP_SUCCESS)
+ return FALSE;
- imap_delete_all_cached_messages((FolderItem *)item);
+ if ((uid_next != item->uid_next) || (exists < item->item.total_msgs))
+ return TRUE;
+ }
return FALSE;
}