/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 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 "config.h"
#endif
+#include "defs.h"
+
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
+#include <time.h>
+#if HAVE_LIBJCONV
+# include <jconv.h>
+#endif
#include "intl.h"
#include "imap.h"
#include "socket.h"
+#include "ssl.h"
#include "recv.h"
#include "procmsg.h"
#include "procheader.h"
#include "inputdialog.h"
#define IMAP4_PORT 143
+#if USE_SSL
+#define IMAPS_PORT 993
+#endif
+
+#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;
static gint imap_cmd_count = 0;
+static void imap_folder_init (Folder *folder,
+ const gchar *name,
+ const gchar *path);
+
static IMAPSession *imap_session_get (Folder *folder);
-static gchar *imap_query_password (const gchar *server,
- const gchar *user);
-static void imap_scan_tree_recursive (IMAPSession *session,
- FolderItem *item,
- IMAPNameSpace *namespace);
+static gint imap_scan_tree_recursive (IMAPSession *session,
+ FolderItem *item);
static GSList *imap_parse_list (IMAPSession *session,
- const gchar *path);
-static gint imap_create_trash (Folder *folder);
+ const gchar *real_path);
+
+static void imap_create_missing_folders (Folder *folder);
+static FolderItem *imap_create_special_folder
+ (Folder *folder,
+ SpecialFolderItemType stype,
+ const gchar *name);
static gint imap_do_copy (Folder *folder,
FolderItem *dest,
guint32 last_uid);
static void imap_delete_all_cached_messages (FolderItem *item);
+#if USE_SSL
static SockInfo *imap_open (const gchar *server,
gushort port,
- gchar *buf);
+ gboolean use_ssl);
+#else
+static SockInfo *imap_open (const gchar *server,
+ gushort port);
+#endif
+
+static SockInfo *imap_open_tunnel(const gchar *server,
+ const gchar *tunnelcmd);
+
+static SockInfo *imap_init_sock(SockInfo *sock);
static gint imap_set_message_flags (IMAPSession *session,
guint32 first_uid,
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);
static IMAPNameSpace *imap_find_namespace (IMAPFolder *folder,
const gchar *path);
+static gchar imap_get_path_separator (IMAPFolder *folder,
+ const gchar *path);
static gchar *imap_get_real_path (IMAPFolder *folder,
const gchar *path);
static MsgFlags imap_parse_flags (const gchar *flag_str);
static MsgInfo *imap_parse_envelope (SockInfo *sock,
GString *line_str);
+static gint imap_greeting (SockInfo *sock,
+ gboolean *is_preauth);
/* low-level IMAP4rev1 commands */
static gint imap_cmd_login (SockInfo *sock,
guint32 *uid_validity);
static gint imap_cmd_create (SockInfo *sock,
const gchar *folder);
+static gint imap_cmd_rename (SockInfo *sock,
+ const gchar *oldfolder,
+ const gchar *newfolder);
static gint imap_cmd_delete (SockInfo *sock,
const gchar *folder);
static gint imap_cmd_envelope (SockInfo *sock,
gchar ch,
gchar *dest,
gint len);
+static gchar *get_quoted (const gchar *src,
+ gchar ch,
+ gchar *dest,
+ gint len);
static gchar *search_array_contain_str (GPtrArray *array,
gchar *str);
+static gchar *search_array_str (GPtrArray *array,
+ gchar *str);
+static gchar *search_array_starts (GPtrArray *array,
+ const gchar *str);
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 gboolean imap_rename_folder_func (GNode *node,
+ gpointer data);
+
+
+Folder *imap_folder_new(const gchar *name, const gchar *path)
+{
+ Folder *folder;
+
+ folder = (Folder *)g_new0(IMAPFolder, 1);
+ imap_folder_init(folder, name, path);
+
+ return folder;
+}
+
+void imap_folder_destroy(IMAPFolder *folder)
+{
+ folder_remote_folder_destroy(REMOTE_FOLDER(folder));
+}
+
+static void imap_folder_init(Folder *folder, const gchar *name,
+ const gchar *path)
+{
+ folder_remote_folder_init(folder, name, path);
+
+ folder->type = F_IMAP;
+
+ folder->get_msg_list = imap_get_msg_list;
+ folder->fetch_msg = imap_fetch_msg;
+ folder->add_msg = imap_add_msg;
+ folder->move_msg = imap_move_msg;
+ folder->move_msgs_with_dest = imap_move_msgs_with_dest;
+ folder->copy_msg = imap_copy_msg;
+ folder->copy_msgs_with_dest = imap_copy_msgs_with_dest;
+ folder->remove_msg = imap_remove_msg;
+ folder->remove_all_msg = imap_remove_all_msg;
+ folder->is_msg_changed = imap_is_msg_changed;
+ folder->scan = imap_scan_folder;
+ folder->scan_tree = imap_scan_tree;
+ folder->create_tree = imap_create_tree;
+ folder->create_folder = imap_create_folder;
+ folder->rename_folder = imap_rename_folder;
+ folder->remove_folder = imap_remove_folder;
+}
+
static IMAPSession *imap_session_get(Folder *folder)
{
RemoteFolder *rfolder = REMOTE_FOLDER(folder);
g_return_val_if_fail(folder->type == F_IMAP, NULL);
g_return_val_if_fail(folder->account != NULL, NULL);
+#if USE_SSL
+ port = folder->account->set_imapport ? folder->account->imapport
+ : folder->account->ssl_imap ? IMAPS_PORT : IMAP4_PORT;
+#else
port = folder->account->set_imapport ? folder->account->imapport
: IMAP4_PORT;
+#endif
if (!rfolder->session) {
rfolder->session =
- imap_session_new(folder->account->recv_server, port,
- folder->account->userid,
- folder->account->passwd);
- if (rfolder->session)
+ imap_session_new(folder->account);
+ if (rfolder->session) {
imap_parse_namespace(IMAP_SESSION(rfolder->session),
IMAP_FOLDER(folder));
+ rfolder->session->last_access_time = time(NULL);
+ }
+ statusbar_pop_all();
+ return IMAP_SESSION(rfolder->session);
+ }
+
+ /* I think the point of this code is to avoid sending a
+ * keepalive if we've used the session recently and therefore
+ * think it's still alive. Unfortunately, most of the code
+ * does not yet check for errors on the socket, and so if the
+ * connection drops we don't notice until the timeout expires.
+ * A better solution than sending a NOOP every time would be
+ * for every command to be prepared to retry until it is
+ * successfully sent. -- mbp */
+ if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
+ rfolder->session->last_access_time = time(NULL);
statusbar_pop_all();
return IMAP_SESSION(rfolder->session);
}
folder->account->recv_server, port);
session_destroy(rfolder->session);
rfolder->session =
- imap_session_new(folder->account->recv_server, port,
- folder->account->userid,
- folder->account->passwd);
+ imap_session_new(folder->account);
if (rfolder->session)
imap_parse_namespace(IMAP_SESSION(rfolder->session),
IMAP_FOLDER(folder));
}
+ if (rfolder->session)
+ rfolder->session->last_access_time = time(NULL);
statusbar_pop_all();
return IMAP_SESSION(rfolder->session);
}
-static gchar *imap_query_password(const gchar *server, const gchar *user)
-{
- gchar *message;
- gchar *pass;
-
- message = g_strdup_printf(_("Input password for %s on %s:"),
- user, server);
- pass = input_dialog_with_invisible(_("Input password"), message, NULL);
- g_free(message);
-
- return pass;
-}
-
-Session *imap_session_new(const gchar *server, gushort port,
- const gchar *user, const gchar *pass)
+Session *imap_session_new(const PrefsAccount *account)
{
- gchar buf[IMAPBUFSIZE];
IMAPSession *session;
SockInfo *imap_sock;
+ gushort port;
+ gchar *pass;
+ gboolean is_preauth;
+
+#ifdef USE_SSL
+ /* FIXME: IMAP over SSL only... */
+ gboolean use_ssl;
- g_return_val_if_fail(server != NULL, NULL);
- g_return_val_if_fail(user != NULL, NULL);
+ port = account->set_imapport ? account->imapport
+ : account->ssl_imap ? IMAPS_PORT : IMAP4_PORT;
+ use_ssl = account->ssl_imap ? TRUE : FALSE;
+#else
+ port = account->set_imapport ? account->imapport
+ : IMAP4_PORT;
+#endif
- if (!pass) {
- gchar *tmp_pass;
- tmp_pass = imap_query_password(server, user);
- if (!tmp_pass)
+ if (account->set_tunnelcmd) {
+ log_message(_("creating tunneled IMAP4 connection\n"));
+ if ((imap_sock = imap_open_tunnel(account->recv_server,
+ account->tunnelcmd)) == NULL)
+ return NULL;
+ } else {
+ g_return_val_if_fail(account->recv_server != NULL, NULL);
+
+ log_message(_("creating IMAP4 connection to %s:%d ...\n"),
+ account->recv_server, port);
+
+#if USE_SSL
+ if ((imap_sock = imap_open(account->recv_server, port,
+ use_ssl)) == NULL)
+#else
+ if ((imap_sock = imap_open(account->recv_server, port)) == NULL)
+#endif
return NULL;
- Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return NULL;});
- g_free(tmp_pass);
}
- log_message(_("creating IMAP4 connection to %s:%d ...\n"),
- server, port);
+ /* Only need to log in if the connection was not PREAUTH */
+ imap_greeting(imap_sock, &is_preauth);
+ log_message("IMAP connection is %s-authenticated\n",
+ (is_preauth) ? "pre" : "un");
+ if (!is_preauth) {
+ g_return_val_if_fail(account->userid != NULL, NULL);
+
+ pass = account->passwd;
+ if (!pass) {
+ gchar *tmp_pass;
+ tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
+ if (!tmp_pass)
+ return NULL;
+ Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return NULL;});
+ g_free(tmp_pass);
+ }
- if ((imap_sock = imap_open(server, port, buf)) == NULL)
- return NULL;
- if (imap_cmd_login(imap_sock, user, pass) != IMAP_SUCCESS) {
- imap_cmd_logout(imap_sock);
- sock_close(imap_sock);
- return NULL;
+ if (imap_cmd_login(imap_sock, account->userid, pass) != IMAP_SUCCESS) {
+ imap_cmd_logout(imap_sock);
+ sock_close(imap_sock);
+ return NULL;
+ }
}
session = g_new(IMAPSession, 1);
- SESSION(session)->type = SESSION_IMAP;
- SESSION(session)->server = g_strdup(server);
- SESSION(session)->sock = imap_sock;
- SESSION(session)->connected = TRUE;
- SESSION(session)->phase = SESSION_READY;
- SESSION(session)->data = NULL;
+ SESSION(session)->type = SESSION_IMAP;
+ SESSION(session)->server = g_strdup(account->recv_server);
+ SESSION(session)->sock = imap_sock;
+ SESSION(session)->connected = TRUE;
+ SESSION(session)->phase = SESSION_READY;
+ SESSION(session)->last_access_time = time(NULL);
+ SESSION(session)->data = NULL;
session->mbox = NULL;
session_list = g_list_append(session_list, session);
IMAPSession *session = (IMAPSession *)session_list->data;
imap_cmd_logout(SESSION(session)->sock);
- imap_session_destroy(session);
+ session_destroy(SESSION(session));
}
}
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);
gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
gboolean remove_source)
{
+ gchar *destdir;
IMAPSession *session;
+ gint messages, recent, unseen;
+ guint32 uid_next, uid_validity;
gint ok;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(file != NULL, -1);
session = imap_session_get(folder);
- if (!session)
+ 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);
- ok = imap_cmd_append(SESSION(session)->sock, dest->path, file);
if (ok != IMAP_SUCCESS) {
g_warning(_("can't append message %s\n"), file);
return -1;
FILE_OP_ERROR(file, "unlink");
}
- return dest->last_num;
+ return uid_next;
}
static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
g_free(destdir);
statusbar_pop_all();
- return ok;
+ if (ok == IMAP_SUCCESS)
+ return 0;
+ else
+ return -1;
}
static gint imap_do_copy_msgs_with_dest(Folder *folder, FolderItem *dest,
GSList *cur;
MsgInfo *msginfo;
IMAPSession *session;
- gint ok;
+ gint ok = IMAP_SUCCESS;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(dest != NULL, -1);
g_free(destdir);
statusbar_pop_all();
- return IMAP_SUCCESS;
+ if (ok == IMAP_SUCCESS)
+ return 0;
+ else
+ return -1;
}
gint imap_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
{
- return imap_do_copy(folder, dest, msginfo, TRUE);
+ gchar *srcfile;
+ gint ret = 0;
+
+ 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(msginfo->folder != NULL, -1);
+
+ if (folder == msginfo->folder->folder)
+ return imap_do_copy(folder, dest, msginfo, TRUE);
+
+ srcfile = procmsg_get_message_file(msginfo);
+ if (!srcfile) return -1;
+
+ ret = imap_add_msg(folder, dest, srcfile, FALSE);
+ g_free(srcfile);
+
+ if (ret == 0)
+ ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
+
+ return ret;
}
gint imap_move_msgs_with_dest(Folder *folder, FolderItem *dest,
GSList *msglist)
{
- return imap_do_copy_msgs_with_dest(folder, dest, msglist, TRUE);
+ MsgInfo *msginfo;
+ GSList *cur;
+ gint ret = 0;
+
+ g_return_val_if_fail(folder != NULL, -1);
+ g_return_val_if_fail(dest != NULL, -1);
+ g_return_val_if_fail(msglist != NULL, -1);
+
+ msginfo = (MsgInfo *)msglist->data;
+ if (folder == msginfo->folder->folder)
+ return imap_do_copy_msgs_with_dest(folder, dest, msglist, TRUE);
+
+ for (cur = msglist; cur != NULL; cur = cur->next) {
+ msginfo = (MsgInfo *)cur->data;
+ ret = imap_move_msg(folder, dest, msginfo);
+ if (ret != 0) break;
+ }
+
+ return ret;
}
gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
{
- return imap_do_copy(folder, dest, msginfo, FALSE);
+ gchar *srcfile;
+ gint ret = 0;
+
+ 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(msginfo->folder != NULL, -1);
+
+ if (folder == msginfo->folder->folder)
+ return imap_do_copy(folder, dest, msginfo, FALSE);
+
+ srcfile = procmsg_get_message_file(msginfo);
+ if (!srcfile) return -1;
+
+ ret = imap_add_msg(folder, dest, srcfile, FALSE);
+
+ g_free(srcfile);
+
+ return ret;
}
gint imap_copy_msgs_with_dest(Folder *folder, FolderItem *dest,
GSList *msglist)
{
- return imap_do_copy_msgs_with_dest(folder, dest, msglist, FALSE);
+ MsgInfo *msginfo;
+ GSList *cur;
+ gint ret = 0;
+
+ g_return_val_if_fail(folder != NULL, -1);
+ g_return_val_if_fail(dest != NULL, -1);
+ g_return_val_if_fail(msglist != NULL, -1);
+
+ msginfo = (MsgInfo *)msglist->data;
+ if (folder == msginfo->folder->folder)
+ return imap_do_copy_msgs_with_dest
+ (folder, dest, msglist, FALSE);
+
+ for (cur = msglist; cur != NULL; cur = cur->next) {
+ msginfo = (MsgInfo *)cur->data;
+ ret = imap_copy_msg(folder, dest, msginfo);
+ if (ret != 0) break;
+ }
+
+ return ret;
}
gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
return IMAP_SUCCESS;
}
-void imap_scan_folder(Folder *folder, FolderItem *item)
+gboolean imap_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+{
+ /* TODO: properly implement this method */
+ return FALSE;
+}
+
+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 = recent;
+ 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)
{
- IMAPFolder *imapfolder = IMAP_FOLDER(folder);
- FolderItem *item, *inbox;
+ FolderItem *item;
IMAPSession *session;
- IMAPNameSpace *namespace = NULL;
gchar *root_folder = NULL;
g_return_if_fail(folder != NULL);
session = imap_session_get(folder);
if (!session) return;
- if (imapfolder->namespace && imapfolder->namespace->data)
- namespace = (IMAPNameSpace *)imapfolder->namespace->data;
-
if (folder->account->imap_dir && *folder->account->imap_dir) {
- gchar *imap_dir;
- Xstrdup_a(imap_dir, folder->account->imap_dir, return);
- strtailchomp(imap_dir, '/');
- root_folder = g_strconcat
- (namespace && namespace->name ? namespace->name : "",
- imap_dir, NULL);
- if (namespace && namespace->separator)
- subst_char(root_folder, namespace->separator, '/');
- }
-
- if (root_folder)
+ Xstrdup_a(root_folder, folder->account->imap_dir, return);
+ strtailchomp(root_folder, '/');
debug_print("IMAP root directory: %s\n", root_folder);
+ }
folder_tree_destroy(folder);
item = folder_item_new(folder->name, root_folder);
item->folder = folder;
folder->node = g_node_new(item);
- g_free(root_folder);
- imap_scan_tree_recursive(session, item, namespace);
+ imap_scan_tree_recursive(session, item);
- if (!folder->inbox) {
- inbox = folder_item_new("INBOX", "INBOX");
- inbox->stype = F_INBOX;
- folder_item_append(item, inbox);
- folder->inbox = inbox;
- }
- if (!folder->trash)
- imap_create_trash(folder);
+ imap_create_missing_folders(folder);
}
-static void imap_scan_tree_recursive(IMAPSession *session,
- FolderItem *item,
- IMAPNameSpace *namespace)
+static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
{
+ Folder *folder;
IMAPFolder *imapfolder;
FolderItem *new_item;
GSList *item_list, *cur;
gchar *real_path;
+ gchar *wildcard_path;
+ gchar separator;
+ gchar wildcard[3];
- g_return_if_fail(item != NULL);
- g_return_if_fail(item->folder != NULL);
- g_return_if_fail(item->no_sub == FALSE);
+ g_return_val_if_fail(item != NULL, -1);
+ g_return_val_if_fail(item->folder != NULL, -1);
+ g_return_val_if_fail(item->no_sub == FALSE, -1);
+
+ folder = FOLDER(item->folder);
+ imapfolder = IMAP_FOLDER(folder);
- imapfolder = IMAP_FOLDER(item->folder);
+ separator = imap_get_path_separator(imapfolder, item->path);
if (item->folder->ui_func)
- item->folder->ui_func(item->folder, item,
- item->folder->ui_func_data);
+ item->folder->ui_func(folder, item, folder->ui_func_data);
if (item->path) {
+ wildcard[0] = separator;
+ wildcard[1] = '%';
+ wildcard[2] = '\0';
real_path = imap_get_real_path(imapfolder, item->path);
- imap_cmd_gen_send(SESSION(session)->sock, "LIST \"\" %s%c%%",
- real_path,
- namespace && namespace->separator
- ? namespace->separator : '/');
} else {
- real_path = g_strdup(namespace && namespace->name
- ? namespace->name : "");
- imap_cmd_gen_send(SESSION(session)->sock, "LIST \"\" %s%%",
- real_path);
+ wildcard[0] = '%';
+ wildcard[1] = '\0';
+ real_path = g_strdup("");
}
- strtailchomp(real_path, namespace && namespace->separator
- ? namespace->separator : '/');
+ Xstrcat_a(wildcard_path, real_path, wildcard,
+ {g_free(real_path); return IMAP_ERROR;});
+ QUOTE_IF_REQUIRED(wildcard_path, wildcard_path);
+ imap_cmd_gen_send(SESSION(session)->sock, "LIST \"\" %s",
+ wildcard_path);
+
+ strtailchomp(real_path, separator);
item_list = imap_parse_list(session, real_path);
+ g_free(real_path);
+
for (cur = item_list; cur != NULL; cur = cur->next) {
new_item = cur->data;
if (!strcmp(new_item->path, "INBOX")) {
- if (!item->folder->inbox) {
+ if (!folder->inbox) {
new_item->stype = F_INBOX;
item->folder->inbox = new_item;
} else {
folder_item_destroy(new_item);
continue;
}
- } else if (!item->parent && !item->folder->trash) {
- if (!strcasecmp(g_basename(new_item->path), "Trash")) {
+ } else if (!item->parent || item->stype == F_INBOX) {
+ gchar *base;
+
+ base = g_basename(new_item->path);
+
+ if (!folder->outbox && !strcasecmp(base, "Sent")) {
+ new_item->stype = F_OUTBOX;
+ folder->outbox = new_item;
+ } else if (!folder->draft && !strcasecmp(base, "Drafts")) {
+ new_item->stype = F_DRAFT;
+ folder->draft = new_item;
+ } else if (!folder->queue && !strcasecmp(base, "Queue")) {
+ new_item->stype = F_QUEUE;
+ folder->queue = new_item;
+ } else if (!folder->trash && !strcasecmp(base, "Trash")) {
new_item->stype = F_TRASH;
- item->folder->trash = new_item;
+ folder->trash = new_item;
}
}
folder_item_append(item, new_item);
if (new_item->no_select == FALSE)
- imap_scan_folder(new_item->folder, new_item);
+ imap_scan_folder(folder, new_item);
if (new_item->no_sub == FALSE)
- imap_scan_tree_recursive(session, new_item, namespace);
+ imap_scan_tree_recursive(session, new_item);
}
+
+ return IMAP_SUCCESS;
}
-static GSList *imap_parse_list(IMAPSession *session, const gchar *path)
+static GSList *imap_parse_list(IMAPSession *session, const gchar *real_path)
{
gchar buf[IMAPBUFSIZE];
gchar flags[256];
gchar separator[16];
gchar *p;
gchar *name;
+ gchar *loc_name, *loc_path;
GSList *item_list = NULL;
GString *str;
FolderItem *new_item;
- debug_print("getting list of %s ...\n", *path ? path : "\"\"");
+ debug_print("getting list of %s ...\n",
+ *real_path ? real_path : "\"\"");
str = g_string_new(NULL);
strncpy2(buf, p, sizeof(buf));
strtailchomp(buf, separator[0]);
if (buf[0] == '\0') continue;
- if (!strcmp(buf, path)) continue;
+ if (!strcmp(buf, real_path)) continue;
if (separator[0] != '\0')
subst_char(buf, separator[0], '/');
name = g_basename(buf);
if (name[0] == '.') continue;
- new_item = folder_item_new(name, buf);
+ loc_name = imap_modified_utf7_to_locale(name);
+ loc_path = imap_modified_utf7_to_locale(buf);
+ new_item = folder_item_new(loc_name, loc_path);
if (strcasestr(flags, "\\Noinferiors") != NULL)
new_item->no_sub = TRUE;
- if (strcasestr(flags, "\\Noselect") != NULL)
+ if (strcmp(buf, "INBOX") != 0 &&
+ strcasestr(flags, "\\Noselect") != NULL)
new_item->no_select = TRUE;
item_list = g_slist_append(item_list, new_item);
- debug_print("folder %s has been added.\n", buf);
+ debug_print("folder %s has been added.\n", loc_path);
+ g_free(loc_path);
+ g_free(loc_name);
}
g_string_free(str, TRUE);
gint imap_create_tree(Folder *folder)
{
- FolderItem *item;
-
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(folder->node != NULL, -1);
g_return_val_if_fail(folder->node->data != NULL, -1);
g_return_val_if_fail(folder->account != NULL, -1);
imap_scan_tree(folder);
+ imap_create_missing_folders(folder);
- item = FOLDER_ITEM(folder->node->data);
+ return 0;
+}
- if (!folder->inbox) {
- FolderItem *inbox;
+static void imap_create_missing_folders(Folder *folder)
+{
+ g_return_if_fail(folder != NULL);
- inbox = folder_item_new("INBOX", "INBOX");
- inbox->stype = F_INBOX;
- folder_item_append(item, inbox);
- folder->inbox = inbox;
- }
+ 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->draft)
+ folder->draft = imap_create_special_folder
+ (folder, F_DRAFT, "Drafts");
+ if (!folder->queue)
+ folder->queue = imap_create_special_folder
+ (folder, F_QUEUE, "Queue");
+#endif
if (!folder->trash)
- imap_create_trash(folder);
-
- return 0;
+ folder->trash = imap_create_special_folder
+ (folder, F_TRASH, "Trash");
}
-static gint imap_create_trash(Folder *folder)
+static FolderItem *imap_create_special_folder(Folder *folder,
+ SpecialFolderItemType stype,
+ const gchar *name)
{
- IMAPFolder *imapfolder = IMAP_FOLDER(folder);
FolderItem *item;
FolderItem *new_item;
- gchar *trash_path;
- gchar *imap_dir = "";
-
- g_return_val_if_fail(folder != NULL, -1);
- g_return_val_if_fail(folder->node != NULL, -1);
- g_return_val_if_fail(folder->node->data != NULL, -1);
- g_return_val_if_fail(folder->account != NULL, -1);
-
- if (folder->account->imap_dir && *folder->account->imap_dir) {
- gchar *tmpdir;
-
- Xstrdup_a(tmpdir, folder->account->imap_dir, return -1);
- strtailchomp(tmpdir, '/');
- Xalloca(imap_dir, strlen(tmpdir) + 2, return -1);
- g_snprintf(imap_dir, strlen(tmpdir) + 2, "%s%c", tmpdir, '/');
- }
-
- if (imapfolder->namespace && imapfolder->namespace->data) {
- IMAPNameSpace *namespace =
- (IMAPNameSpace *)imapfolder->namespace->data;
-
- if (*namespace->name != '\0') {
- gchar *name;
- Xstrdup_a(name, namespace->name, return -1);
- subst_char(name, namespace->separator, '/');
- trash_path = g_strconcat(name, imap_dir, "Trash", NULL);
- } else
- trash_path = g_strconcat(imap_dir, "Trash", NULL);
- } else
- trash_path = g_strconcat(imap_dir, "Trash", NULL);
+ g_return_val_if_fail(folder != NULL, NULL);
+ g_return_val_if_fail(folder->node != NULL, NULL);
+ g_return_val_if_fail(folder->node->data != NULL, NULL);
+ g_return_val_if_fail(folder->account != NULL, NULL);
+ g_return_val_if_fail(name != NULL, NULL);
item = FOLDER_ITEM(folder->node->data);
- new_item = imap_create_folder(folder, item, trash_path);
+ new_item = imap_create_folder(folder, item, name);
if (!new_item) {
- gchar *path;
-
- new_item = folder_item_new("Trash", trash_path);
- folder_item_append(item, new_item);
-
- path = folder_item_get_path(new_item);
- if (!is_dir_exist(path))
- make_dir_hier(path);
- g_free(path);
- } else {
- g_free(new_item->name);
- new_item->name = g_strdup("Trash");
- }
- new_item->stype = F_TRASH;
- folder->trash = new_item;
+ g_warning(_("Can't create '%s'\n"), name);
+ if (!folder->inbox) return NULL;
- g_free(trash_path);
+ new_item = imap_create_folder(folder, folder->inbox, name);
+ if (!new_item)
+ g_warning(_("Can't create '%s' under INBOX\n"), name);
+ else
+ new_item->stype = stype;
+ } else
+ new_item->stype = stype;
- return 0;
+ return new_item;
}
FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
const gchar *name)
{
- gchar *dirpath, *imappath;
+ gchar *dirpath, *imap_path;
IMAPSession *session;
- IMAPNameSpace *namespace;
FolderItem *new_item;
+ gchar separator;
gchar *new_name;
const gchar *p;
gint ok;
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);
} else
dirpath = g_strdup(name);
- Xstrdup_a(imappath, dirpath, {g_free(dirpath); return NULL;});
- Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
- namespace = imap_find_namespace(IMAP_FOLDER(folder), imappath);
- if (namespace && namespace->separator) {
- imap_path_separator_subst(imappath, namespace->separator);
- imap_path_separator_subst(new_name, namespace->separator);
- strtailchomp(new_name, namespace->separator);
- }
+ /* keep trailing directory separator to create a folder that contains
+ sub folder */
+ imap_path = imap_locale_to_modified_utf7(dirpath);
strtailchomp(dirpath, '/');
+ Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
+ strtailchomp(new_name, '/');
+ separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
+ imap_path_separator_subst(imap_path, separator);
+ subst_char(new_name, '/', separator);
if (strcmp(name, "INBOX") != 0) {
GPtrArray *argbuf;
gboolean exist = FALSE;
argbuf = g_ptr_array_new();
- ok = imap_cmd_list(SESSION(session)->sock, NULL, imappath,
+ ok = imap_cmd_list(SESSION(session)->sock, NULL, imap_path,
argbuf);
statusbar_pop_all();
if (ok != IMAP_SUCCESS) {
log_warning(_("can't create mailbox: LIST failed\n"));
+ g_free(imap_path);
g_free(dirpath);
g_ptr_array_free(argbuf, TRUE);
return NULL;
g_ptr_array_free(argbuf, TRUE);
if (!exist) {
- ok = imap_cmd_create(SESSION(session)->sock, imappath);
+ ok = imap_cmd_create(SESSION(session)->sock, imap_path);
statusbar_pop_all();
if (ok != IMAP_SUCCESS) {
log_warning(_("can't create mailbox\n"));
+ g_free(imap_path);
g_free(dirpath);
return NULL;
}
new_item = folder_item_new(new_name, dirpath);
folder_item_append(parent, new_item);
+ g_free(imap_path);
g_free(dirpath);
dirpath = folder_item_get_path(new_item);
return new_item;
}
+gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
+{
+ gchar *dirpath;
+ gchar *newpath;
+ gchar *real_oldpath;
+ gchar *real_newpath;
+ GNode *node;
+ gchar *paths[2];
+ gchar *old_cache_dir;
+ gchar *new_cache_dir;
+ IMAPSession *session;
+ gchar separator;
+ gint ok;
+ gint exists, recent, unseen;
+ guint32 uid_validity;
+
+ 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);
+ g_return_val_if_fail(name != NULL, -1);
+
+ session = imap_session_get(folder);
+ if (!session) return -1;
+
+ 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();
+ if (ok != IMAP_SUCCESS) {
+ g_free(real_oldpath);
+ return -1;
+ }
+
+ separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path);
+ if (strchr(item->path, G_DIR_SEPARATOR)) {
+ dirpath = g_dirname(item->path);
+ newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
+ g_free(dirpath);
+ } else
+ newpath = g_strdup(name);
+
+ real_newpath = imap_locale_to_modified_utf7(newpath);
+ imap_path_separator_subst(real_newpath, separator);
+
+ ok = imap_cmd_rename(SESSION(session)->sock, real_oldpath, real_newpath);
+ statusbar_pop_all();
+ if (ok != IMAP_SUCCESS) {
+ log_warning(_("can't rename mailbox: %s to %s\n"),
+ real_oldpath, real_newpath);
+ g_free(real_oldpath);
+ g_free(newpath);
+ g_free(real_newpath);
+ return -1;
+ }
+
+ g_free(item->name);
+ item->name = g_strdup(name);
+
+ 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,
+ imap_rename_folder_func, paths);
+
+ if (is_dir_exist(old_cache_dir)) {
+ new_cache_dir = folder_item_get_path(item);
+ if (rename(old_cache_dir, new_cache_dir) < 0) {
+ FILE_OP_ERROR(old_cache_dir, "rename");
+ }
+ g_free(new_cache_dir);
+ }
+
+ g_free(old_cache_dir);
+ g_free(paths[0]);
+ g_free(newpath);
+ g_free(real_oldpath);
+ g_free(real_newpath);
+
+ return 0;
+}
+
gint imap_remove_folder(Folder *folder, FolderItem *item)
{
gint ok;
IMAPSession *session;
gchar *path;
+ gchar *cache_dir;
gint exists, recent, unseen;
guint32 uid_validity;
}
g_free(path);
+ cache_dir = folder_item_get_path(item);
+ if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
+ g_warning("can't remove directory '%s'\n", cache_dir);
+ g_free(cache_dir);
folder_item_remove(item);
return 0;
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);
log_warning(_("can't parse envelope: %s\n"), str->str);
continue;
}
+ if (item->stype == F_QUEUE) {
+ MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED);
+ } else if (item->stype == F_DRAFT) {
+ MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT);
+ }
msginfo->folder = item;
debug_print(_("done.\n"));
}
-static SockInfo *imap_open(const gchar *server, gushort port, gchar *buf)
+
+static SockInfo *imap_open_tunnel(const gchar *server,
+ const gchar *tunnelcmd)
{
SockInfo *sock;
- if ((sock = sock_connect(server, port)) == NULL) {
- log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
- server, port);
+ if ((sock = sock_connect_cmd(server, tunnelcmd)) == NULL)
+ log_warning(_("Can't establish IMAP4 session with: %s\n"),
+ server);
return NULL;
- }
- imap_cmd_count = 0;
+ return imap_init_sock(sock);
+}
+
+
+#if USE_SSL
+static SockInfo *imap_open(const gchar *server, gushort port,
+ gboolean use_ssl)
+#else
+static SockInfo *imap_open(const gchar *server, gushort port)
+#endif
+{
+ SockInfo *sock;
+
+ if ((sock = sock_connect(server, port)) == NULL) {
+ log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
+ server, port);
+ return NULL;
+ }
+
+#if USE_SSL
+ if (use_ssl && !ssl_init_socket(sock)) {
+ log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
+ server, port);
+ sock_close(sock);
+ return NULL;
+ }
+#endif
+
+ return imap_init_sock(sock);
+}
+
+
+static SockInfo *imap_init_sock(SockInfo *sock)
+{
+ imap_cmd_count = 0;
if (imap_cmd_noop(sock) != IMAP_SUCCESS) {
+ log_warning(_("Can't establish IMAP4 session\n"));
sock_close(sock);
return NULL;
}
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;
+ gchar *tmp_path, *name;
- g_return_val_if_fail(folder != NULL, NULL);
- g_return_val_if_fail(path != NULL, NULL);
+ if (!path) path = "";
- ns_list = folder->namespace;
+ Xstrcat_a(tmp_path, path, "/", return NULL);
for (; ns_list != NULL; ns_list = ns_list->next) {
IMAPNameSpace *tmp_ns = ns_list->data;
Xstrdup_a(name, tmp_ns->name, return namespace);
if (tmp_ns->separator && tmp_ns->separator != '/')
subst_char(name, tmp_ns->separator, '/');
- if (strncmp(path, name, strlen(name)) == 0)
+ if (strncmp(tmp_path, name, strlen(name)) == 0)
namespace = tmp_ns;
}
return namespace;
}
-static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
+static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
+ const gchar *path)
{
- gchar *real_path;
IMAPNameSpace *namespace;
g_return_val_if_fail(folder != NULL, NULL);
- g_return_val_if_fail(path != NULL, NULL);
- real_path = g_strdup(path);
+ 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_path_separator(IMAPFolder *folder, const gchar *path)
+{
+ IMAPNameSpace *namespace;
+ gchar separator = '/';
+
namespace = imap_find_namespace(folder, path);
if (namespace && namespace->separator)
- imap_path_separator_subst(real_path, namespace->separator);
+ separator = namespace->separator;
+
+ return separator;
+}
+
+static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
+{
+ gchar *real_path;
+ gchar separator;
+
+ g_return_val_if_fail(folder != NULL, NULL);
+ g_return_val_if_fail(path != NULL, NULL);
+
+ real_path = imap_locale_to_modified_utf7(path);
+ separator = imap_get_path_separator(folder, path);
+ imap_path_separator_subst(real_path, separator);
return real_path;
}
gchar *dest, gint dest_len, GString *str)
{
gchar *cur_pos = src;
+ gchar *nextline;
g_return_val_if_fail(str != NULL, cur_pos);
- while (*cur_pos == ' ') cur_pos++;
+ /* read the next line if the current response buffer is empty */
+ while (isspace(*cur_pos)) cur_pos++;
+ while (*cur_pos == '\0') {
+ if ((nextline = sock_getline(sock)) == NULL)
+ return cur_pos;
+ g_string_assign(str, nextline);
+ cur_pos = str->str;
+ strretchomp(nextline);
+ /* log_print("IMAP4< %s\n", nextline); */
+ debug_print("IMAP4< %s\n", nextline);
+ g_free(nextline);
+
+ while (isspace(*cur_pos)) cur_pos++;
+ }
if (!strncmp(cur_pos, "NIL", 3)) {
*dest = '\0';
} else if (*cur_pos == '\"') {
gchar *p;
- p = strchr_cpy(cur_pos + 1, '\"', dest, dest_len);
+ p = get_quoted(cur_pos, '\"', dest, dest_len);
cur_pos = p ? p : cur_pos + 2;
} else if (*cur_pos == '{') {
gchar buf[32];
gint len;
- gchar *nextline;
+ gint line_len = 0;
cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
len = atoi(buf);
- if ((nextline = sock_getline(sock)) == NULL)
- return cur_pos;
- strretchomp(nextline);
- log_print("IMAP4< %s\n", nextline);
- g_string_assign(str, nextline);
-
- len = MIN(len, strlen(nextline));
- memcpy(dest, nextline, MIN(len, dest_len - 1));
+ g_string_truncate(str, 0);
+ cur_pos = str->str;
+
+ do {
+ if ((nextline = sock_getline(sock)) == NULL)
+ return cur_pos;
+ line_len += strlen(nextline);
+ g_string_append(str, nextline);
+ cur_pos = str->str;
+ strretchomp(nextline);
+ /* log_print("IMAP4< %s\n", nextline); */
+ debug_print("IMAP4< %s\n", nextline);
+ g_free(nextline);
+ } while (line_len < len);
+
+ memcpy(dest, cur_pos, MIN(len, dest_len - 1));
dest[MIN(len, dest_len - 1)] = '\0';
- cur_pos = str->str + len;
+ cur_pos += len;
}
return cur_pos;
while ((p = strchr(p, '\\')) != NULL) {
p++;
- if (g_strncasecmp(p, "Recent", 6) == 0) {
- MSG_SET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
+ if (g_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
+ MSG_SET_PERM_FLAGS(flags, MSG_NEW);
} else if (g_strncasecmp(p, "Seen", 4) == 0) {
MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
} else if (g_strncasecmp(p, "Deleted", 7) == 0) {
MSG_SET_PERM_FLAGS(flags, MSG_DELETED);
} else if (g_strncasecmp(p, "Flagged", 7) == 0) {
MSG_SET_PERM_FLAGS(flags, MSG_MARKED);
+ } else if (g_strncasecmp(p, "Answered", 8) == 0) {
+ MSG_SET_PERM_FLAGS(flags, MSG_REPLIED);
}
}
g_return_val_if_fail(*cur_pos == '(', NULL);
cur_pos = imap_parse_atom
(sock, cur_pos + 1, buf, sizeof(buf), line_str);
- Xstrdup_a(date, buf, return NULL);
- date_t = procheader_date_parse(NULL, date, 0);
+ if (buf[0] != '\0') {
+ Xstrdup_a(date, buf, return NULL);
+ date_t = procheader_date_parse(NULL, date, 0);
+ }
cur_pos = imap_parse_atom
(sock, cur_pos, buf, sizeof(buf), line_str);
cur_pos = imap_parse_address(sock, cur_pos + 1,
&tmp_from, &tmp_fromname,
line_str);
- Xstrdup_a(from, tmp_from,
- {g_free(tmp_from); g_free(tmp_fromname);
- return NULL;});
- Xstrdup_a(fromname, tmp_fromname,
- {g_free(tmp_from); g_free(tmp_fromname);
- return NULL;});
+ g_return_val_if_fail(cur_pos != NULL, NULL);
+ if (tmp_from && *tmp_from != '\0')
+ Xstrdup_a(from, tmp_from,
+ {g_free(tmp_from);
+ g_free(tmp_fromname);
+ return NULL;});
+ if (tmp_fromname && *tmp_fromname != '\0')
+ Xstrdup_a(fromname, tmp_fromname,
+ {g_free(tmp_from);
+ g_free(tmp_fromname);
+ return NULL;});
g_free(tmp_from);
g_free(tmp_fromname);
g_return_val_if_fail(*cur_pos == ' ', NULL); \
cur_pos = imap_parse_address(sock, cur_pos + 1, NULL, NULL, \
line_str); \
+ g_return_val_if_fail(cur_pos != NULL, NULL); \
}
/* skip sender and reply-to */
g_return_val_if_fail(*cur_pos == ' ', NULL);
cur_pos = imap_parse_address(sock, cur_pos + 1,
&tmp_to, NULL, line_str);
+ g_return_val_if_fail(cur_pos != NULL, NULL);
Xstrdup_a(to, tmp_to, {g_free(tmp_to); return NULL;});
g_free(tmp_to);
g_return_val_if_fail(*cur_pos == ' ', NULL);
cur_pos = imap_parse_atom
(sock, cur_pos, buf, sizeof(buf), line_str);
+ g_return_val_if_fail(cur_pos != NULL, NULL);
if (buf[0] != '\0') {
eliminate_parenthesis(buf, '(', ')');
extract_parenthesis(buf, '<', '>');
g_return_val_if_fail(*cur_pos == ' ', NULL);
cur_pos = imap_parse_atom
(sock, cur_pos, buf, sizeof(buf), line_str);
+ g_return_val_if_fail(cur_pos != NULL, NULL);
if (buf[0] != '\0') {
extract_parenthesis(buf, '<', '>');
remove_space(buf);
return msginfo;
}
+gint imap_msg_set_perm_flags(MsgInfo *msginfo, MsgPermFlags flags)
+{
+ Folder *folder;
+ IMAPSession *session;
+ IMAPFlags iflags = 0;
+ gint ok = IMAP_SUCCESS;
+
+ g_return_val_if_fail(msginfo != NULL, -1);
+ g_return_val_if_fail(MSG_IS_IMAP(msginfo->flags), -1);
+ g_return_val_if_fail(msginfo->folder != NULL, -1);
+ g_return_val_if_fail(msginfo->folder->folder != NULL, -1);
+
+ folder = msginfo->folder->folder;
+ g_return_val_if_fail(folder->type == F_IMAP, -1);
+
+ session = imap_session_get(folder);
+ if (!session) return -1;
+
+ if (flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
+ if (flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
+ if (iflags) {
+ ok = imap_set_message_flags(session, msginfo->msgnum,
+ msginfo->msgnum, iflags, TRUE);
+ if (ok != IMAP_SUCCESS) return ok;
+ }
+
+ if (flags & MSG_UNREAD)
+ ok = imap_set_message_flags(session, msginfo->msgnum,
+ msginfo->msgnum, IMAP_FLAG_SEEN,
+ FALSE);
+ return ok;
+}
+
+gint imap_msg_unset_perm_flags(MsgInfo *msginfo, MsgPermFlags flags)
+{
+ Folder *folder;
+ IMAPSession *session;
+ IMAPFlags iflags = 0;
+ gint ok = IMAP_SUCCESS;
+
+ g_return_val_if_fail(msginfo != NULL, -1);
+ g_return_val_if_fail(MSG_IS_IMAP(msginfo->flags), -1);
+ g_return_val_if_fail(msginfo->folder != NULL, -1);
+ g_return_val_if_fail(msginfo->folder->folder != NULL, -1);
+
+ folder = msginfo->folder->folder;
+ g_return_val_if_fail(folder->type == F_IMAP, -1);
+
+ session = imap_session_get(folder);
+ if (!session) return -1;
+
+ if (flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
+ if (flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
+ if (iflags) {
+ ok = imap_set_message_flags(session, msginfo->msgnum,
+ msginfo->msgnum, iflags, FALSE);
+ if (ok != IMAP_SUCCESS) return ok;
+ }
+
+ if (flags & MSG_UNREAD)
+ ok = imap_set_message_flags(session, msginfo->msgnum,
+ msginfo->msgnum, IMAP_FLAG_SEEN,
+ TRUE);
+ return ok;
+}
+
static gint imap_set_message_flags(IMAPSession *session,
guint32 first_uid,
guint32 last_uid,
{
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_;
gint ok;
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);
- if (strchr(real_path, ' ') != NULL)
- imap_cmd_gen_send(SESSION(session)->sock, "STATUS \"%s\" "
- "(MESSAGES RECENT UNSEEN UIDVALIDITY)",
- real_path);
- else
- imap_cmd_gen_send(SESSION(session)->sock, "STATUS %s "
- "(MESSAGES RECENT UNSEEN UIDVALIDITY)",
- real_path);
+ QUOTE_IF_REQUIRED(real_path_, real_path);
+ imap_cmd_gen_send(SESSION(session)->sock, "STATUS %s "
+ "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
+ real_path_);
ok = imap_cmd_ok(SESSION(session)->sock, argbuf);
if (ok != IMAP_SUCCESS) THROW(ok);
- str = search_array_contain_str(argbuf, "STATUS");
+ str = search_array_str(argbuf, "STATUS");
if (!str) THROW(IMAP_ERROR);
str = strchr(str, '(');
} 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;
static gint imap_cmd_login(SockInfo *sock,
const gchar *user, const gchar *pass)
{
+ gchar *user_, *pass_;
gint ok;
- if (strchr(user, ' ') != NULL)
- imap_cmd_gen_send(sock, "LOGIN \"%s\" %s", user, pass);
- else
- imap_cmd_gen_send(sock, "LOGIN %s %s", user, pass);
+ QUOTE_IF_REQUIRED(user_, user);
+ QUOTE_IF_REQUIRED(pass_, pass);
+ imap_cmd_gen_send(sock, "LOGIN %s %s", user_, pass_);
ok = imap_cmd_ok(sock, NULL);
if (ok != IMAP_SUCCESS)
return imap_cmd_ok(sock, NULL);
}
+/* Send a NOOP, and examine the server's response to see whether this
+ * connection is pre-authenticated or not. */
+static gint imap_greeting(SockInfo *sock, gboolean *is_preauth)
+{
+ GPtrArray *argbuf;
+ gint r;
+
+ imap_cmd_gen_send(sock, "NOOP");
+ argbuf = g_ptr_array_new(); /* will hold messages sent back */
+ r = imap_cmd_ok(sock, argbuf);
+ *is_preauth = search_array_starts(argbuf, "PREAUTH") != NULL;
+
+ return r;
+}
+
static gint imap_cmd_noop(SockInfo *sock)
{
imap_cmd_gen_send(sock, "NOOP");
imap_cmd_gen_send(sock, "NAMESPACE");
if ((ok = imap_cmd_ok(sock, argbuf)) != IMAP_SUCCESS) THROW(ok);
- str = search_array_contain_str(argbuf, "NAMESPACE");
+ str = search_array_str(argbuf, "NAMESPACE");
if (!str) THROW(IMAP_ERROR);
*ns_str = g_strdup(str);
static gint imap_cmd_list(SockInfo *sock, const gchar *ref,
const gchar *mailbox, GPtrArray *argbuf)
{
- gchar *new_ref;
- gchar *new_mailbox;
+ gchar *ref_, *mailbox_;
if (!ref) ref = "\"\"";
if (!mailbox) mailbox = "\"\"";
- if (*ref != '"' && strchr(ref, ' ') != NULL)
- new_ref = g_strdup_printf("\"%s\"", ref);
- else
- new_ref = g_strdup(ref);
- if (*mailbox != '"' && strchr(mailbox, ' ') != NULL)
- new_mailbox = g_strdup_printf("\"%s\"", mailbox);
- else
- new_mailbox = g_strdup(mailbox);
-
- imap_cmd_gen_send(sock, "LIST %s %s", new_ref, new_mailbox);
-
- g_free(new_ref);
- g_free(new_mailbox);
+ QUOTE_IF_REQUIRED(ref_, ref);
+ QUOTE_IF_REQUIRED(mailbox_, mailbox);
+ imap_cmd_gen_send(sock, "LIST %s %s", ref_, mailbox_);
return imap_cmd_ok(sock, argbuf);
}
gchar *resp_str;
GPtrArray *argbuf;
gchar *select_cmd;
+ gchar *folder_;
*exists = *recent = *unseen = *uid_validity = 0;
argbuf = g_ptr_array_new();
else
select_cmd = "SELECT";
- if (strchr(folder, ' ') != NULL)
- imap_cmd_gen_send(sock, "%s \"%s\"", select_cmd, folder);
- else
- imap_cmd_gen_send(sock, "%s %s", select_cmd, folder);
+ QUOTE_IF_REQUIRED(folder_, folder);
+ imap_cmd_gen_send(sock, "%s %s", select_cmd, folder_);
if ((ok = imap_cmd_ok(sock, argbuf)) != IMAP_SUCCESS) THROW;
static gint imap_cmd_create(SockInfo *sock, const gchar *folder)
{
- if (strchr(folder, ' ') != NULL)
- imap_cmd_gen_send(sock, "CREATE \"%s\"", folder);
- else
- imap_cmd_gen_send(sock, "CREATE %s", folder);
+ gchar *folder_;
+
+ QUOTE_IF_REQUIRED(folder_, folder);
+ imap_cmd_gen_send(sock, "CREATE %s", folder_);
+
+ return imap_cmd_ok(sock, NULL);
+}
+
+static gint imap_cmd_rename(SockInfo *sock, const gchar *old_folder,
+ const gchar *new_folder)
+{
+ gchar *old_folder_, *new_folder_;
+
+ QUOTE_IF_REQUIRED(old_folder_, old_folder);
+ QUOTE_IF_REQUIRED(new_folder_, new_folder);
+ imap_cmd_gen_send(sock, "RENAME %s %s", old_folder_, new_folder_);
return imap_cmd_ok(sock, NULL);
}
static gint imap_cmd_delete(SockInfo *sock, const gchar *folder)
{
- if (strchr(folder, ' ') != NULL)
- imap_cmd_gen_send(sock, "DELETE \"%s\"", folder);
- else
- imap_cmd_gen_send(sock, "DELETE %s", folder);
+ gchar *folder_;
+
+ QUOTE_IF_REQUIRED(folder_, folder);
+ imap_cmd_gen_send(sock, "DELETE %s", folder_);
return imap_cmd_ok(sock, NULL);
}
imap_cmd_gen_send(sock, "UID FETCH %d BODY[]", uid);
- if (sock_gets(sock, buf, sizeof(buf)) < 0)
- return IMAP_ERROR;
- strretchomp(buf);
- if (buf[0] != '*' || buf[1] != ' ')
- return IMAP_ERROR;
- log_print("IMAP4< %s\n", buf);
+ while ((ok = imap_cmd_gen_recv(sock, buf, sizeof(buf)))
+ == IMAP_SUCCESS) {
+ if (buf[0] != '*' || buf[1] != ' ')
+ return IMAP_ERROR;
+ if (strstr(buf, "FETCH") != NULL)
+ break;
+ }
+ if (ok != IMAP_SUCCESS)
+ return ok;
cur_pos = strchr(buf, '{');
g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
{
gint ok;
gint size;
+ gchar *destfolder_;
+ gchar buf[BUFFSIZE];
+ FILE *fp;
g_return_val_if_fail(file != NULL, IMAP_ERROR);
- size = get_file_size(file);
- imap_cmd_gen_send(sock, "APPEND %s {%d}", destfolder, size);
- ok = imap_cmd_ok(sock, NULL);
- if (ok != IMAP_SUCCESS) {
- log_warning(_("can't append %s to %s\n"), file, destfolder);
+ size = get_file_size_as_crlf(file);
+ if ((fp = fopen(file, "rb")) == NULL) {
+ FILE_OP_ERROR(file, "fopen");
return -1;
}
+ QUOTE_IF_REQUIRED(destfolder_, destfolder);
+ imap_cmd_gen_send(sock, "APPEND %s (\\Seen) {%d}", destfolder_, size);
- return ok;
+ ok = imap_cmd_gen_recv(sock, buf, sizeof(buf));
+ if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
+ log_warning(_("can't append %s to %s\n"), file, destfolder_);
+ fclose(fp);
+ return IMAP_ERROR;
+ }
+
+ log_print("IMAP4> %s\n", _("(sending file...)"));
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ strretchomp(buf);
+ if (sock_puts(sock, buf) < 0) {
+ fclose(fp);
+ sock_close(sock);
+ return -1;
+ }
+ }
+
+ if (ferror(fp)) {
+ FILE_OP_ERROR(file, "fgets");
+ fclose(fp);
+ sock_close(sock);
+ return -1;
+ }
+
+ sock_puts(sock, "");
+
+ fclose(fp);
+ return imap_cmd_ok(sock, NULL);
}
static gint imap_cmd_copy(SockInfo *sock, guint32 uid, const gchar *destfolder)
{
gint ok;
+ gchar *destfolder_;
g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
- if (strchr(destfolder, ' ') != NULL)
- imap_cmd_gen_send(sock, "UID COPY %d \"%s\"", uid, destfolder);
- else
- imap_cmd_gen_send(sock, "UID COPY %d %s", uid, destfolder);
+ QUOTE_IF_REQUIRED(destfolder_, destfolder);
+ imap_cmd_gen_send(sock, "UID COPY %d %s", uid, destfolder_);
ok = imap_cmd_ok(sock, NULL);
if (ok != IMAP_SUCCESS) {
- log_warning(_("can't copy %d to %s\n"), uid, destfolder);
+ log_warning(_("can't copy %d to %s\n"), uid, destfolder_);
return -1;
}
return tmp + 1;
}
+static gchar *get_quoted(const gchar *src, gchar ch, gchar *dest, gint len)
+{
+ const gchar *p = src;
+ gint n = 0;
+
+ g_return_val_if_fail(*p == ch, NULL);
+
+ *dest = '\0';
+ p++;
+
+ while (*p != '\0' && *p != ch) {
+ if (n < len - 1) {
+ if (*p == '\\' && *(p + 1) != '\0')
+ p++;
+ *dest++ = *p++;
+ } else
+ p++;
+ n++;
+ }
+
+ *dest = '\0';
+ return (gchar *)(*p == ch ? p + 1 : p);
+}
+
+static gchar *search_array_starts(GPtrArray *array, const gchar *str)
+{
+ gint i;
+ size_t len;
+
+ g_return_val_if_fail(str != NULL, NULL);
+ len = strlen(str);
+ for (i = 0; i < array->len; i++) {
+ gchar *tmp;
+ tmp = g_ptr_array_index(array, i);
+ if (strncmp(tmp, str, len) == 0)
+ return tmp;
+ }
+ return NULL;
+}
+
static gchar *search_array_contain_str(GPtrArray *array, gchar *str)
{
gint i;
return NULL;
}
+static gchar *search_array_str(GPtrArray *array, gchar *str)
+{
+ gint i;
+ gint len;
+
+ len = strlen(str);
+
+ for (i = 0; i < array->len; i++) {
+ gchar *tmp;
+
+ tmp = g_ptr_array_index(array, i);
+ if (!strncmp(tmp, str, len))
+ return tmp;
+ }
+
+ return NULL;
+}
+
static void imap_path_separator_subst(gchar *str, gchar separator)
{
- if (separator && separator != '/')
- subst_char(str, '/', separator);
+ gchar *p;
+ gboolean in_escape = FALSE;
+
+ if (!separator || separator == '/') return;
+
+ for (p = str; *p != '\0'; p++) {
+ if (*p == '/' && !in_escape)
+ *p = separator;
+ else if (*p == '&' && *(p + 1) != '-' && !in_escape)
+ in_escape = TRUE;
+ else if (*p == '-' && in_escape)
+ in_escape = FALSE;
+ }
+}
+
+static gchar *imap_modified_utf7_to_locale(const gchar *mutf7_str)
+{
+#if !HAVE_LIBJCONV
+ return g_strdup(mutf7_str);
+#else
+ static iconv_t cd = (iconv_t)-1;
+ static gboolean iconv_ok = TRUE;
+ GString *norm_utf7;
+ gchar *norm_utf7_p;
+ size_t norm_utf7_len;
+ const gchar *p;
+ gchar *to_str, *to_p;
+ size_t to_len;
+ gboolean in_escape = FALSE;
+
+ if (!iconv_ok) return g_strdup(mutf7_str);
+
+ if (cd == (iconv_t)-1) {
+ cd = iconv_open(conv_get_current_charset_str(), "UTF-7");
+ if (cd == (iconv_t)-1) {
+ g_warning(_("iconv cannot convert UTF-7 to %s\n"),
+ conv_get_current_charset_str());
+ iconv_ok = FALSE;
+ return g_strdup(mutf7_str);
+ }
+ }
+
+ norm_utf7 = g_string_new(NULL);
+
+ for (p = mutf7_str; *p != '\0'; p++) {
+ /* replace: '&' -> '+',
+ "&-" -> '&',
+ escaped ',' -> '/' */
+ if (!in_escape && *p == '&') {
+ if (*(p + 1) != '-') {
+ g_string_append_c(norm_utf7, '+');
+ in_escape = TRUE;
+ } else {
+ g_string_append_c(norm_utf7, '&');
+ p++;
+ }
+ } else if (in_escape && *p == ',') {
+ g_string_append_c(norm_utf7, '/');
+ } else if (in_escape && *p == '-') {
+ g_string_append_c(norm_utf7, '-');
+ in_escape = FALSE;
+ } else {
+ g_string_append_c(norm_utf7, *p);
+ }
+ }
+
+ norm_utf7_p = norm_utf7->str;
+ norm_utf7_len = norm_utf7->len;
+ to_len = strlen(mutf7_str) * 5;
+ to_p = to_str = g_malloc(to_len + 1);
+
+ if (iconv(cd, &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_string_free(norm_utf7, TRUE);
+ g_free(to_str);
+ return g_strdup(mutf7_str);
+ }
+
+ /* second iconv() call for flushing */
+ iconv(cd, NULL, NULL, &to_p, &to_len);
+ g_string_free(norm_utf7, TRUE);
+ *to_p = '\0';
+
+ return to_str;
+#endif /* !HAVE_LIBJCONV */
+}
+
+static gchar *imap_locale_to_modified_utf7(const gchar *from)
+{
+#if !HAVE_LIBJCONV
+ return g_strdup(from);
+#else
+ static iconv_t cd = (iconv_t)-1;
+ static gboolean iconv_ok = TRUE;
+ gchar *norm_utf7, *norm_utf7_p;
+ size_t from_len, norm_utf7_len;
+ GString *to_str;
+ gchar *from_tmp, *to, *p;
+ gboolean in_escape = FALSE;
+
+ if (!iconv_ok) return g_strdup(from);
+
+ if (cd == (iconv_t)-1) {
+ cd = iconv_open("UTF-7", conv_get_current_charset_str());
+ if (cd == (iconv_t)-1) {
+ g_warning(_("iconv cannot convert %s to UTF-7\n"),
+ conv_get_current_charset_str());
+ iconv_ok = FALSE;
+ return g_strdup(from);
+ }
+ }
+
+ Xstrdup_a(from_tmp, from, return g_strdup(from));
+ from_len = strlen(from);
+ norm_utf7_len = from_len * 5;
+ Xalloca(norm_utf7, norm_utf7_len + 1, return g_strdup(from));
+ norm_utf7_p = norm_utf7;
+
+#define IS_PRINT(ch) (isprint(ch) && isascii(ch))
+
+ while (from_len > 0) {
+ if (IS_PRINT(*from_tmp)) {
+ /* printable ascii char */
+ *norm_utf7_p = *from_tmp;
+ norm_utf7_p++;
+ from_tmp++;
+ from_len--;
+ } else {
+ size_t mblen;
+
+ /* unprintable char: convert to UTF-7 */
+ for (mblen = 0;
+ !IS_PRINT(from_tmp[mblen]) && mblen < from_len;
+ mblen++)
+ ;
+ from_len -= mblen;
+ if (iconv(cd, &from_tmp, &mblen,
+ &norm_utf7_p, &norm_utf7_len) == -1) {
+ g_warning(_("iconv cannot convert %s to UTF-7\n"),
+ conv_get_current_charset_str());
+ return g_strdup(from);
+ }
+
+ /* second iconv() call for flushing */
+ iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
+ }
+ }
+
+#undef IS_PRINT
+
+ *norm_utf7_p = '\0';
+ to_str = g_string_new(NULL);
+ for (p = norm_utf7; p < norm_utf7_p; p++) {
+ /* replace: '&' -> "&-",
+ '+' -> '&',
+ escaped '/' -> ',' */
+ if (!in_escape && *p == '&') {
+ g_string_append(to_str, "&-");
+ } else if (!in_escape && *p == '+') {
+ g_string_append_c(to_str, '&');
+ in_escape = TRUE;
+ } else if (in_escape && *p == '/') {
+ g_string_append_c(to_str, ',');
+ } else if (in_escape && *p == '-') {
+ in_escape = FALSE;
+ g_string_append_c(to_str, '-');
+ } else {
+ g_string_append_c(to_str, *p);
+ }
+ }
+
+ if (in_escape) {
+ in_escape = FALSE;
+ g_string_append_c(to_str, '-');
+ }
+
+ to = to_str->str;
+ g_string_free(to_str, FALSE);
+
+ return to;
+#endif
+}
+
+static gboolean imap_rename_folder_func(GNode *node, gpointer data)
+{
+ FolderItem *item = node->data;
+ gchar **paths = data;
+ const gchar *oldpath = paths[0];
+ const gchar *newpath = paths[1];
+ gchar *base;
+ gchar *new_itempath;
+ gint oldpathlen;
+
+ oldpathlen = strlen(oldpath);
+ if (strncmp(oldpath, item->path, oldpathlen) != 0) {
+ g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+ return TRUE;
+ }
+
+ base = item->path + oldpathlen;
+ while (*base == G_DIR_SEPARATOR) base++;
+ if (*base == '\0')
+ new_itempath = g_strdup(newpath);
+ else
+ new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
+ NULL);
+ g_free(item->path);
+ item->path = new_itempath;
+
+ return FALSE;
}