#include "prefs_account.h"
#define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
+#define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
#define IMAP_SESSION(obj) ((IMAPSession *)obj)
struct _IMAPFolder
static gint imap_close(Folder * folder, FolderItem * item);
-static void imap_scan_tree(Folder * folder);
+static gint imap_scan_tree(Folder * folder);
static gint imap_create_tree(Folder * folder);
const gchar *user,
const gchar *pass,
IMAPAuthType type);
-static gint imap_cmd_login (IMAPSession *sock,
+static gint imap_cmd_login (IMAPSession *session,
const gchar *user,
const gchar *pass);
-static gint imap_cmd_logout (IMAPSession *sock);
-static gint imap_cmd_noop (IMAPSession *sock);
-static gint imap_cmd_starttls (IMAPSession *sock);
-static gint imap_cmd_namespace (IMAPSession *sock,
+static gint imap_cmd_logout (IMAPSession *session);
+static gint imap_cmd_noop (IMAPSession *session);
+static gint imap_cmd_starttls (IMAPSession *session);
+static gint imap_cmd_namespace (IMAPSession *session,
gchar **ns_str);
static gint imap_cmd_list (IMAPSession *session,
const gchar *ref,
const gchar *mailbox,
GPtrArray *argbuf);
-static gint imap_cmd_do_select (IMAPSession *sock,
+static gint imap_cmd_do_select (IMAPSession *session,
const gchar *folder,
gboolean examine,
gint *exists,
gint *recent,
gint *unseen,
guint32 *uid_validity);
-static gint imap_cmd_select (IMAPSession *sock,
+static gint imap_cmd_select (IMAPSession *session,
const gchar *folder,
gint *exists,
gint *recent,
gint *unseen,
guint32 *uid_validity);
-static gint imap_cmd_examine (IMAPSession *sock,
+static gint imap_cmd_examine (IMAPSession *session,
const gchar *folder,
gint *exists,
gint *recent,
static gint imap_cmd_rename (IMAPSession *sock,
const gchar *oldfolder,
const gchar *newfolder);
-static gint imap_cmd_delete (IMAPSession *sock,
+static gint imap_cmd_delete (IMAPSession *session,
const gchar *folder);
-static gint imap_cmd_envelope (IMAPSession *sock,
+static gint imap_cmd_envelope (IMAPSession *session,
IMAPSet set);
static gint imap_cmd_fetch (IMAPSession *sock,
guint32 uid,
const gchar *seq_set,
const gchar *destfolder,
GRelation *uid_mapping);
-static gint imap_cmd_store (IMAPSession *sock,
+static gint imap_cmd_store (IMAPSession *session,
IMAPSet set,
gchar *sub_cmd);
-static gint imap_cmd_expunge (IMAPSession *sock);
-static gint imap_cmd_close (IMAPSession *session);
+static gint imap_cmd_expunge (IMAPSession *session,
+ IMAPSet seq_set);
+static gint imap_cmd_close (IMAPSession *session);
static gint imap_cmd_ok (IMAPSession *session,
GPtrArray *argbuf);
-static void imap_gen_send (IMAPSession *sock,
+static void imap_gen_send (IMAPSession *session,
const gchar *format, ...);
-static gint imap_gen_recv (IMAPSession *sock,
+static gint imap_gen_recv (IMAPSession *session,
gchar **ret);
/* misc utility functions */
for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
MsgInfo *msginfo = (MsgInfo *)cur->data;
GTuples *tuples;
-
+
tuples = g_relation_select(uid_mapping,
GINT_TO_POINTER(msginfo->msgnum),
0);
return ok;
}
- ok = imap_cmd_expunge(session);
+ if (!session->uidplus) {
+ ok = imap_cmd_expunge(session, NULL);
+ } else {
+ gchar *uidstr;
+
+ uidstr = g_strdup_printf("%u", uid);
+ ok = imap_cmd_expunge(session, uidstr);
+ g_free(uidstr);
+ }
if (ok != IMAP_SUCCESS) {
log_warning(_("can't expunge\n"));
return ok;
}
+ IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
+ IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
dir = folder_item_get_path(item);
if (is_dir_exist(dir))
remove_numbered_files(dir, uid, uid);
return ok;
}
- ok = imap_cmd_expunge(session);
+ ok = imap_cmd_expunge(session, NULL);
if (ok != IMAP_SUCCESS) {
log_warning(_("can't expunge\n"));
return ok;
if (!session) return -1;
if (session->mbox) {
- if (strcmp(item->path, session->mbox))
- return -1;
+ if (strcmp2(session->mbox, item->path) != 0) return -1;
ok = imap_cmd_close(session);
if (ok != IMAP_SUCCESS)
return 0;
}
-void imap_scan_tree(Folder *folder)
+gint imap_scan_tree(Folder *folder)
{
FolderItem *item = NULL;
IMAPSession *session;
gchar *root_folder = NULL;
- g_return_if_fail(folder != NULL);
- g_return_if_fail(folder->account != NULL);
+ g_return_val_if_fail(folder != NULL, -1);
+ g_return_val_if_fail(folder->account != NULL, -1);
session = imap_session_get(folder);
if (!session) {
item->folder = folder;
folder->node = item->node = g_node_new(item);
}
- return;
+ return -1;
}
if (folder->account->imap_dir && *folder->account->imap_dir) {
- Xstrdup_a(root_folder, folder->account->imap_dir, return);
+ gchar *real_path;
+
+ Xstrdup_a(root_folder, folder->account->imap_dir, return -1);
strtailchomp(root_folder, '/');
- debug_print("IMAP root directory: %s\n", root_folder);
+ extract_quote(root_folder, '"');
+ real_path = imap_get_real_path
+ (IMAP_FOLDER(folder), root_folder);
+ debug_print("IMAP root directory: %s\n", real_path);
+ if (imap_status(session, IMAP_FOLDER(folder), root_folder,
+ NULL, NULL, NULL, NULL, NULL)
+ != IMAP_SUCCESS) {
+ if (imap_cmd_create(session, real_path)
+ != IMAP_SUCCESS) {
+ log_warning(_("can't create root folder %s\n"),
+ real_path);
+ g_free(real_path);
+ return -1;
+ }
+ }
+ g_free(real_path);
}
if (folder->node)
imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
imap_create_missing_folders(folder);
+
+ return 0;
}
static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
strretchomp(buf);
if (buf[0] != '*' || buf[1] != ' ') {
log_print("IMAP4< %s\n", buf);
+ if (sscanf(buf, "%*d %16s", buf) < 1 ||
+ strcmp(buf, "OK") != 0)
+ log_warning(_("error occurred while getting LIST.\n"));
+
break;
}
debug_print("IMAP4< %s\n", buf);
if (!path) path = "";
- Xstrcat_a(tmp_path, path, "/", return NULL);
-
for (; ns_list != NULL; ns_list = ns_list->next) {
IMAPNameSpace *tmp_ns = ns_list->data;
+ Xstrcat_a(tmp_path, path, "/", return namespace);
Xstrdup_a(name, tmp_ns->name, return namespace);
- if (tmp_ns->separator && tmp_ns->separator != '/')
+ if (tmp_ns->separator && tmp_ns->separator != '/') {
+ subst_char(tmp_path, tmp_ns->separator, '/');
subst_char(name, tmp_ns->separator, '/');
+ }
if (strncmp(tmp_path, name, strlen(name)) == 0)
namespace = tmp_ns;
}
cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
len = atoi(buf);
- g_return_val_if_fail(len > 0, cur_pos);
+ g_return_val_if_fail(len >= 0, cur_pos);
g_string_truncate(str, 0);
cur_pos = str->str;
cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
len = atoi(buf);
- g_return_val_if_fail(len > 0, cur_pos);
+ g_return_val_if_fail(len >= 0, cur_pos);
g_string_truncate(str, 0);
cur_pos = str->str;
gchar *real_path;
gchar *real_path_;
gint ok;
- GPtrArray *argbuf;
+ GPtrArray *argbuf = NULL;
gchar *str;
- *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
-
- if (path == NULL)
- return -1;
-
- argbuf = g_ptr_array_new();
+ if (messages && recent && uid_next && uid_validity && unseen) {
+ *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
+ argbuf = g_ptr_array_new();
+ }
real_path = imap_get_real_path(folder, path);
QUOTE_IF_REQUIRED(real_path_, real_path);
real_path_);
ok = imap_cmd_ok(session, argbuf);
- if (ok != IMAP_SUCCESS) THROW(ok);
+ if (ok != IMAP_SUCCESS || !argbuf) THROW(ok);
str = search_array_str(argbuf, "STATUS");
if (!str) THROW(IMAP_ERROR);
catch:
g_free(real_path);
- ptr_array_free_strings(argbuf);
- g_ptr_array_free(argbuf, TRUE);
+ if (argbuf) {
+ ptr_array_free_strings(argbuf);
+ g_ptr_array_free(argbuf, TRUE);
+ }
return ok;
}
cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
RETURN_ERROR_IF_FAIL(cur_pos != NULL);
size_num = atol(size_str);
- RETURN_ERROR_IF_FAIL(size_num > 0);
+ RETURN_ERROR_IF_FAIL(size_num >= 0);
RETURN_ERROR_IF_FAIL(*cur_pos == '\0');
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) {
+ if ((ok == IMAP_SUCCESS) && (argbuf->len > 0)) {
resp_str = g_ptr_array_index(argbuf, argbuf->len - 1);
if (resp_str &&
sscanf(resp_str, "%*u OK [APPENDUID %*u %u]",
} else
ok = imap_cmd_ok(session, NULL);
+ if (ok != IMAP_SUCCESS)
+ log_warning(_("can't append message to %s\n"),
+ destfolder_);
+
return ok;
}
+static MsgNumberList *imapset_to_numlist(IMAPSet imapset)
+{
+ gchar **ranges, **range;
+ guint32 low, high;
+ MsgNumberList *uids = NULL;
+
+ ranges = g_strsplit(imapset, ",", 0);
+ for (range = ranges; *range != NULL; range++) {
+ printf("%s\n", *range);
+ if(sscanf(*range, "%u:%u", &low, &high) == 1)
+ uids = g_slist_prepend(uids, GINT_TO_POINTER(low));
+ else {
+ int i;
+ for (i = low; i <= high; i++)
+ uids = g_slist_prepend(uids, GINT_TO_POINTER(i));
+ }
+ }
+ uids = g_slist_reverse(uids);
+ g_strfreev(ranges);
+
+ return uids;
+}
+
static gint imap_cmd_copy(IMAPSession *session, const gchar *seq_set,
const gchar *destfolder, GRelation *uid_mapping)
{
gint ok;
gchar *destfolder_;
- GPtrArray *reply;
g_return_val_if_fail(session != NULL, IMAP_ERROR);
g_return_val_if_fail(seq_set != NULL, IMAP_ERROR);
QUOTE_IF_REQUIRED(destfolder_, destfolder);
imap_gen_send(session, "UID COPY %s %s", seq_set, destfolder_);
- reply = g_ptr_array_new();
+ if (uid_mapping != NULL && session->uidplus) {
+ GPtrArray *reply;
+ gchar *resp_str = NULL, *olduids_str, *newuids_str;
+ MsgNumberList *olduids, *old_cur, *newuids, *new_cur;
+
+ reply = g_ptr_array_new();
+ ok = imap_cmd_ok(session, reply);
+ if ((ok == IMAP_SUCCESS) && (reply->len > 0)) {
+ resp_str = g_ptr_array_index(reply, reply->len - 1);
+ if (resp_str) {
+ olduids_str = g_new0(gchar, strlen(resp_str));
+ newuids_str = g_new0(gchar, strlen(resp_str));
+ if (sscanf(resp_str, "%*s OK [COPYUID %*u %[0-9,:] %[0-9,:]]",
+ olduids_str, newuids_str) == 2) {
+ olduids = imapset_to_numlist(olduids_str);
+ newuids = imapset_to_numlist(newuids_str);
+
+ old_cur = olduids;
+ new_cur = newuids;
+ while(old_cur != NULL && new_cur != NULL) {
+ g_relation_insert(uid_mapping,
+ GPOINTER_TO_INT(old_cur->data),
+ GPOINTER_TO_INT(new_cur->data));
+ old_cur = g_slist_next(old_cur);
+ new_cur = g_slist_next(new_cur);
+ }
+
+ g_slist_free(olduids);
+ g_slist_free(newuids);
+ }
+ g_free(olduids_str);
+ g_free(newuids_str);
+ }
+ }
+ ptr_array_free_strings(reply);
+ g_ptr_array_free(reply, TRUE);
+ } else
+ ok = imap_cmd_ok(session, NULL);
- ok = imap_cmd_ok(session, reply);
if (ok != IMAP_SUCCESS)
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_expunge(IMAPSession *session)
+static gint imap_cmd_expunge(IMAPSession *session, IMAPSet seq_set)
{
gint ok;
- imap_gen_send(session, "EXPUNGE");
+ if (seq_set && session->uidplus)
+ imap_gen_send(session, "UID EXPUNGE %s", seq_set);
+ else
+ imap_gen_send(session, "EXPUNGE");
if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
log_warning(_("error while imap command: EXPUNGE\n"));
return ok;