#define IMAPS_PORT 993
#endif
-#define QUOTE_IF_REQUIRED(out, str) \
-{ \
- if (*str != '"' && strchr(str, ' ')) { \
- gchar *__tmp; \
- gint len; \
- \
- len = strlen(str) + 3; \
- Xalloca(__tmp, len, return IMAP_ERROR); \
- g_snprintf(__tmp, len, "\"%s\"", str); \
- out = __tmp; \
- } else { \
- Xstrdup_a(out, str, return IMAP_ERROR); \
- } \
+#define QUOTE_IF_REQUIRED(out, str) \
+{ \
+ if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
+ gchar *__tmp; \
+ gint len; \
+ \
+ len = strlen(str) + 3; \
+ Xalloca(__tmp, len, return IMAP_ERROR); \
+ g_snprintf(__tmp, len, "\"%s\"", str); \
+ out = __tmp; \
+ } else { \
+ Xstrdup_a(out, str, return IMAP_ERROR); \
+ } \
}
static GList *session_list = NULL;
const gchar *path,
gint *messages,
gint *recent,
- gint *unseen,
- guint32 *uid_validity);
+ guint32 *uid_next,
+ guint32 *uid_validity,
+ gint *unseen);
static void imap_parse_namespace (IMAPSession *session,
IMAPFolder *folder);
return NULL;
}
+ ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+ NULL, NULL, NULL, NULL);
+ statusbar_pop_all();
+ if (ok != IMAP_SUCCESS) {
+ g_warning(_("can't select mailbox %s\n"), item->path);
+ g_free(filename);
+ return NULL;
+ }
+
debug_print(_("getting message %d...\n"), uid);
ok = imap_cmd_fetch(SESSION(session)->sock, (guint32)uid, filename);
{
gchar *destdir;
IMAPSession *session;
+ gint messages, recent, unseen;
+ guint32 uid_next, uid_validity;
gint ok;
g_return_val_if_fail(folder != NULL, -1);
session = imap_session_get(folder);
if (!session) return -1;
+ ok = imap_status(session, IMAP_FOLDER(folder), dest->path,
+ &messages, &recent, &uid_next, &uid_validity, &unseen);
+ statusbar_pop_all();
+ if (ok != IMAP_SUCCESS) {
+ g_warning(_("can't append message %s\n"), file);
+ return -1;
+ }
+
destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
ok = imap_cmd_append(SESSION(session)->sock, destdir, file);
g_free(destdir);
FILE_OP_ERROR(file, "unlink");
}
- return 0;
+ return uid_next;
}
static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
return FALSE;
}
-void imap_scan_folder(Folder *folder, FolderItem *item)
+gint imap_scan_folder(Folder *folder, FolderItem *item)
{
IMAPSession *session;
gint messages, recent, unseen;
- guint32 uid_validity;
+ guint32 uid_next, uid_validity;
gint ok;
- g_return_if_fail(folder != NULL);
- g_return_if_fail(item != NULL);
+ g_return_val_if_fail(folder != NULL, -1);
+ g_return_val_if_fail(item != NULL, -1);
session = imap_session_get(folder);
- if (!session) return;
+ if (!session) return -1;
ok = imap_status(session, IMAP_FOLDER(folder), item->path,
- &messages, &recent, &unseen, &uid_validity);
+ &messages, &recent, &uid_next, &uid_validity, &unseen);
statusbar_pop_all();
- if (ok != IMAP_SUCCESS) return;
+ if (ok != IMAP_SUCCESS) return -1;
item->new = unseen > 0 ? recent : 0;
item->unread = unseen;
item->total = messages;
+ item->last_num = (messages > 0 && uid_next > 0) ? uid_next - 1 : 0;
/* item->mtime = uid_validity; */
+
+ return 0;
}
void imap_scan_tree(Folder *folder)
session = imap_session_get(folder);
if (!session) return;
- if (imapfolder->namespace && imapfolder->namespace->data)
- namespace = (IMAPNameSpace *)imapfolder->namespace->data;
+ if (imapfolder->ns_personal && imapfolder->ns_personal->data)
+ namespace = (IMAPNameSpace *)imapfolder->ns_personal->data;
if (folder->account->imap_dir && *folder->account->imap_dir) {
gchar *imap_dir;
if (!folder->inbox)
folder->inbox = imap_create_special_folder
(folder, F_INBOX, "INBOX");
+#if 0
if (!folder->outbox)
folder->outbox = imap_create_special_folder
(folder, F_OUTBOX, "Sent");
if (!folder->queue)
folder->queue = imap_create_special_folder
(folder, F_QUEUE, "Queue");
+#endif
if (!folder->trash)
folder->trash = imap_create_special_folder
(folder, F_TRASH, "Trash");
session = imap_session_get(folder);
if (!session) return NULL;
- if (parent->path)
+ if (!parent->parent && strcmp(name, "INBOX") == 0)
+ dirpath = g_strdup(name);
+ else if (parent->path)
dirpath = g_strconcat(parent->path, "/", name, NULL);
else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
dirpath = g_strdup(name);
real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
+ g_free(session->mbox);
+ session->mbox = NULL;
ok = imap_cmd_examine(SESSION(session)->sock, "INBOX",
&exists, &recent, &unseen, &uid_validity);
statusbar_pop_all();
return newlist;
}
strretchomp(tmp);
- log_print("IMAP4< %s\n", tmp);
if (tmp[0] != '*' || tmp[1] != ' ') {
+ log_print("IMAP4< %s\n", tmp);
g_free(tmp);
break;
}
if (strstr(tmp, "FETCH") == NULL) {
+ log_print("IMAP4< %s\n", tmp);
g_free(tmp);
continue;
}
+ /* log_print("IMAP4< %s\n", tmp); */
+ debug_print("IMAP4< %s\n", tmp);
g_string_assign(str, tmp);
g_free(tmp);
return sock;
}
-#define THROW goto catch
-
-static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
+static GList *imap_parse_namespace_str(gchar *str)
{
- gchar *ns_str;
+ gchar *p = str;
gchar *name;
gchar *separator;
- gchar *p;
IMAPNameSpace *namespace;
GList *ns_list = NULL;
- g_return_if_fail(session != NULL);
- g_return_if_fail(folder != NULL);
-
- if (folder->namespace != NULL) return;
-
- if (imap_cmd_namespace(SESSION(session)->sock, &ns_str)
- != IMAP_SUCCESS) {
- log_warning(_("can't get namespace\n"));
- return;
- }
-
- /* get the first element */
- extract_one_parenthesis_with_skip_quote(ns_str, '"', '(', ')');
- g_strstrip(ns_str);
- p = ns_str;
-
while (*p != '\0') {
/* parse ("#foo" "/") */
while (*p && *p != '(') p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
p++;
while (*p && *p != '"') p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
p++;
name = p;
while (*p && *p != '"') p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
*p = '\0';
p++;
while (*p && isspace(*p)) p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
if (strncmp(p, "NIL", 3) == 0)
separator = NULL;
else if (*p == '"') {
p++;
separator = p;
while (*p && *p != '"') p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
*p = '\0';
p++;
- } else THROW;
+ } else break;
while (*p && *p != ')') p++;
- if (*p == '\0') THROW;
+ if (*p == '\0') break;
p++;
namespace = g_new(IMAPNameSpace, 1);
namespace->name = g_strdup(name);
namespace->separator = separator ? separator[0] : '\0';
ns_list = g_list_append(ns_list, namespace);
- IMAP_FOLDER(folder)->namespace = ns_list;
}
-catch:
- g_free(ns_str);
- return;
+ return ns_list;
}
-#undef THROW
+static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
+{
+ gchar *ns_str;
+ gchar **str_array;
-static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
- const gchar *path)
+ g_return_if_fail(session != NULL);
+ g_return_if_fail(folder != NULL);
+
+ if (folder->ns_personal != NULL ||
+ folder->ns_others != NULL ||
+ folder->ns_shared != NULL)
+ return;
+
+ if (imap_cmd_namespace(SESSION(session)->sock, &ns_str)
+ != IMAP_SUCCESS) {
+ log_warning(_("can't get namespace\n"));
+ return;
+ }
+
+ str_array = strsplit_parenthesis(ns_str, '(', ')', 3);
+ if (str_array[0])
+ folder->ns_personal = imap_parse_namespace_str(str_array[0]);
+ if (str_array[0] && str_array[1])
+ folder->ns_others = imap_parse_namespace_str(str_array[1]);
+ if (str_array[0] && str_array[1] && str_array[2])
+ folder->ns_shared = imap_parse_namespace_str(str_array[2]);
+ g_strfreev(str_array);
+ return;
+}
+
+static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
+ const gchar *path)
{
IMAPNameSpace *namespace = NULL;
- GList *ns_list;
gchar *name;
- g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(path != NULL, NULL);
- ns_list = folder->namespace;
-
for (; ns_list != NULL; ns_list = ns_list->next) {
IMAPNameSpace *tmp_ns = ns_list->data;
return namespace;
}
+static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
+ const gchar *path)
+{
+ IMAPNameSpace *namespace;
+
+ namespace = imap_find_namespace_from_list(folder->ns_personal, path);
+ if (namespace) return namespace;
+ namespace = imap_find_namespace_from_list(folder->ns_others, path);
+ if (namespace) return namespace;
+ namespace = imap_find_namespace_from_list(folder->ns_shared, path);
+ if (namespace) return namespace;
+
+ return NULL;
+}
+
static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
{
gchar *real_path;
g_string_assign(str, nextline);
cur_pos = str->str;
strretchomp(nextline);
- log_print("IMAP4< %s\n", nextline);
+ /* log_print("IMAP4< %s\n", nextline); */
+ debug_print("IMAP4< %s\n", nextline);
g_free(nextline);
while (isspace(*cur_pos)) cur_pos++;
g_string_append(str, nextline);
cur_pos = str->str;
strretchomp(nextline);
- log_print("IMAP4< %s\n", nextline);
+ /* log_print("IMAP4< %s\n", nextline); */
+ debug_print("IMAP4< %s\n", nextline);
g_free(nextline);
} while (line_len < len);
{
gchar *real_path;
gint ok;
+ gint exists_, recent_, unseen_, uid_validity_;
+
+ if (!exists || !recent || !unseen || !uid_validity) {
+ if (session->mbox && strcmp(session->mbox, path) == 0)
+ return IMAP_SUCCESS;
+ exists = &exists_;
+ recent = &recent_;
+ unseen = &unseen_;
+ uid_validity = &uid_validity_;
+ }
+
+ g_free(session->mbox);
+ session->mbox = NULL;
real_path = imap_get_real_path(folder, path);
ok = imap_cmd_select(SESSION(session)->sock, real_path,
exists, recent, unseen, uid_validity);
if (ok != IMAP_SUCCESS)
log_warning(_("can't select folder: %s\n"), real_path);
+ else
+ session->mbox = g_strdup(path);
g_free(real_path);
return ok;
static gint imap_status(IMAPSession *session, IMAPFolder *folder,
const gchar *path,
- gint *messages, gint *recent, gint *unseen,
- guint32 *uid_validity)
+ gint *messages, gint *recent,
+ guint32 *uid_next, guint32 *uid_validity,
+ gint *unseen)
{
gchar *real_path;
gchar *real_path_;
GPtrArray *argbuf;
gchar *str;
- *messages = *recent = *unseen = *uid_validity = 0;
+ *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);
imap_cmd_gen_send(SESSION(session)->sock, "STATUS %s "
- "(MESSAGES RECENT UNSEEN UIDVALIDITY)", real_path_);
+ "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
+ real_path_);
ok = imap_cmd_ok(SESSION(session)->sock, argbuf);
if (ok != IMAP_SUCCESS) THROW(ok);
} else if (!strncmp(str, "RECENT ", 7)) {
str += 7;
*recent = strtol(str, &str, 10);
- } else if (!strncmp(str, "UNSEEN ", 7)) {
- str += 7;
- *unseen = strtol(str, &str, 10);
+ } else if (!strncmp(str, "UIDNEXT ", 8)) {
+ str += 8;
+ *uid_next = strtoul(str, &str, 10);
} else if (!strncmp(str, "UIDVALIDITY ", 12)) {
str += 12;
*uid_validity = strtoul(str, &str, 10);
+ } else if (!strncmp(str, "UNSEEN ", 7)) {
+ str += 7;
+ *unseen = strtol(str, &str, 10);
} else {
g_warning("invalid STATUS response: %s\n", str);
break;