{
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;
};
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,
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 void imap_free_capabilities (IMAPSession *session);
/* low-level IMAP4rev1 commands */
static gint imap_cmd_authenticate
const gchar *destfolder,
const gchar *file,
IMAPFlags flags,
- gint32 *newuid);
+ guint32 *new_uid);
static gint imap_cmd_copy (IMAPSession *session,
const gchar *seq_set,
const gchar *destfolder,
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,
NULL,
NULL,
NULL,
- imap_check_msgnum_validity,
+ imap_scan_required,
/* Message functions */
imap_get_msginfo,
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)
- return imap_cmd_login(session, user, pass);
+ ok = imap_cmd_login(session, user, pass);
else
- return imap_cmd_authenticate(session, user, pass, type);
+ ok = imap_cmd_authenticate(session, user, pass, type);
+
+ if (ok == IMAP_SUCCESS)
+ session->authenticated = TRUE;
+
+ return ok;
}
static IMAPSession *imap_session_get(Folder *folder)
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) {
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)
guint32 last_uid = 0;
GSList *cur;
MsgFileInfo *fileinfo;
- gint ok, newnum;
+ gint ok;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(dest != NULL, -1);
for (cur = file_list; cur != NULL; cur = cur->next) {
IMAPFlags iflags = 0;
+ guint32 new_uid = 0;
fileinfo = (MsgFileInfo *)cur->data;
dest->stype == F_TRASH)
iflags |= IMAP_FLAG_SEEN;
- ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, &newnum);
+ 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);
if (relation != NULL)
g_relation_insert(relation, fileinfo->msginfo != NULL ?
- fileinfo->msginfo : fileinfo,
+ (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
GINT_TO_POINTER(dest->last_num + 1));
- if (newnum > last_uid)
- last_uid = newnum;
+ if (last_uid < new_uid)
+ last_uid = new_uid;
}
g_free(destdir);
static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
MsgInfoList *msglist, GRelation *relation)
{
+ FolderItem *src;
gchar *destdir;
GSList *seq_list, *cur;
MsgInfo *msginfo;
if (!session) return -1;
msginfo = (MsgInfo *)msglist->data;
- if (msginfo->folder == dest) {
+
+ src = msginfo->folder;
+ if (src == dest) {
g_warning("the src folder is identical to the dest.\n");
return -1;
}
+ ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
+ NULL, NULL, NULL, NULL);
+ if (ok != IMAP_SUCCESS)
+ return ok;
+
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);
gchar *seq_set = (gchar *)cur->data;
debug_print("Copying message %s%c[%s] to %s ...\n",
- msginfo->folder->path, G_DIR_SEPARATOR,
+ src->path, G_DIR_SEPARATOR,
seq_set, destdir);
ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
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.SILENT (\\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;
}
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"));
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;
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;
}
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);
}
gchar *newpath;
gchar *real_oldpath;
gchar *real_newpath;
- GNode *node;
gchar *paths[2];
gchar *old_cache_dir;
gchar *new_cache_dir;
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)) {
*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
+static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
+{
+ gchar **p;
+
+ for (p = session->capability; *p != NULL; ++p) {
+ if (!g_strcasecmp(*p, cap))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+void imap_free_capabilities(IMAPSession *session)
+{
+ g_strfreev(session->capability);
+ session->capability = NULL;
+}
/* low-level IMAP4rev1 commands */
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)
- return TRUE;
-
- return FALSE;
-}
-
-void imap_free_capabilities(IMAPSession *session)
-{
- g_strfreev(session->capability);
- session->capability = NULL;
-}
-
static gint imap_cmd_noop(IMAPSession *session)
{
imap_gen_send(session, "NOOP");
}
static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
- const gchar *file, IMAPFlags flags, gint32 *new_uid)
+ const gchar *file, IMAPFlags flags, guint32 *new_uid)
{
gint ok;
- gint size, newuid;
+ gint size;
gchar *destfolder_;
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);
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;
}
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;
}