2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 #include "procheader.h"
46 #include "prefs_account.h"
49 #include "inputdialog.h"
52 #define IMAP4_PORT 143
54 #define IMAPS_PORT 993
57 #define QUOTE_IF_REQUIRED(out, str) \
59 if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
63 len = strlen(str) + 3; \
64 Xalloca(__tmp, len, return IMAP_ERROR); \
65 g_snprintf(__tmp, len, "\"%s\"", str); \
68 Xstrdup_a(out, str, return IMAP_ERROR); \
72 struct _IMAPFolderItem
80 static GList *session_list = NULL;
82 static gint imap_cmd_count = 0;
84 static void imap_folder_init (Folder *folder,
88 static FolderItem *imap_folder_item_new (Folder *folder);
89 static void imap_folder_item_destroy (Folder *folder,
92 static IMAPSession *imap_session_get (Folder *folder);
94 static gint imap_scan_tree_recursive (IMAPSession *session,
96 static GSList *imap_parse_list (Folder *folder,
98 const gchar *real_path,
101 static void imap_create_missing_folders (Folder *folder);
102 static FolderItem *imap_create_special_folder
104 SpecialFolderItemType stype,
107 static gint imap_do_copy (Folder *folder,
110 gboolean remove_source);
112 static gint imap_do_copy_msgs_with_dest (Folder *folder,
115 gboolean remove_source);
118 static GSList *imap_get_uncached_messages (IMAPSession *session,
122 static GSList *imap_delete_cached_messages (GSList *mlist,
126 static void imap_delete_all_cached_messages (FolderItem *item);
129 static SockInfo *imap_open (const gchar *server,
133 static SockInfo *imap_open (const gchar *server,
138 static SockInfo *imap_open_tunnel(const gchar *server,
139 const gchar *tunnelcmd,
142 static SockInfo *imap_open_tunnel(const gchar *server,
143 const gchar *tunnelcmd);
147 static SockInfo *imap_init_sock(SockInfo *sock, SSLType ssl_type);
149 static SockInfo *imap_init_sock(SockInfo *sock);
152 static gint imap_set_message_flags (IMAPSession *session,
157 static gint imap_select (IMAPSession *session,
163 guint32 *uid_validity);
164 static gint imap_get_uid (IMAPSession *session,
167 static gint imap_status (IMAPSession *session,
173 guint32 *uid_validity,
176 static void imap_parse_namespace (IMAPSession *session,
178 static void imap_get_namespace_by_list (IMAPSession *session,
180 static IMAPNameSpace *imap_find_namespace (IMAPFolder *folder,
182 static gchar imap_get_path_separator (IMAPFolder *folder,
184 static gchar *imap_get_real_path (IMAPFolder *folder,
187 static gchar *imap_parse_atom (SockInfo *sock,
192 static MsgFlags imap_parse_flags (const gchar *flag_str);
193 static MsgInfo *imap_parse_envelope (SockInfo *sock,
196 static gint imap_greeting (SockInfo *sock,
197 gboolean *is_preauth);
198 static void imap_get_capability (Session *session);
199 static gboolean imap_has_capability (IMAPSession *session,
202 /* low-level IMAP4rev1 commands */
203 static gint imap_cmd_login (SockInfo *sock,
206 static gint imap_cmd_logout (SockInfo *sock);
207 static gint imap_cmd_noop (SockInfo *sock);
208 static gint imap_cmd_starttls (SockInfo *sock);
209 static gint imap_cmd_namespace (SockInfo *sock,
211 static gint imap_cmd_list (SockInfo *sock,
213 const gchar *mailbox,
215 static gint imap_cmd_do_select (SockInfo *sock,
221 guint32 *uid_validity);
222 static gint imap_cmd_select (SockInfo *sock,
227 guint32 *uid_validity);
228 static gint imap_cmd_examine (SockInfo *sock,
233 guint32 *uid_validity);
234 static gint imap_cmd_create (SockInfo *sock,
235 const gchar *folder);
236 static gint imap_cmd_rename (SockInfo *sock,
237 const gchar *oldfolder,
238 const gchar *newfolder);
239 static gint imap_cmd_delete (SockInfo *sock,
240 const gchar *folder);
241 static gint imap_cmd_envelope (SockInfo *sock,
245 static gint imap_cmd_search (SockInfo *sock,
248 static gint imap_cmd_fetch (SockInfo *sock,
250 const gchar *filename);
251 static gint imap_cmd_append (SockInfo *sock,
252 const gchar *destfolder,
254 static gint imap_cmd_copy (IMAPSession *session,
256 const gchar *destfolder,
258 static gint imap_cmd_store (SockInfo *sock,
262 static gint imap_cmd_expunge (SockInfo *sock);
264 static gint imap_cmd_ok (SockInfo *sock,
266 static void imap_cmd_gen_send (SockInfo *sock,
267 const gchar *format, ...);
268 static gint imap_cmd_gen_recv (SockInfo *sock,
271 /* misc utility functions */
272 static gchar *strchr_cpy (const gchar *src,
276 static gchar *get_quoted (const gchar *src,
280 static gchar *search_array_contain_str (GPtrArray *array,
282 static gchar *search_array_str (GPtrArray *array,
284 static void imap_path_separator_subst (gchar *str,
287 static gchar *imap_modified_utf7_to_locale (const gchar *mutf7_str);
288 static gchar *imap_locale_to_modified_utf7 (const gchar *from);
290 static gboolean imap_rename_folder_func (GNode *node,
292 gint imap_get_num_list (Folder *folder,
295 GSList *imap_get_msginfos (Folder *folder,
297 GSList *msgnum_list);
298 MsgInfo *imap_get_msginfo (Folder *folder,
301 gboolean imap_check_msgnum_validity (Folder *folder,
304 Folder *imap_folder_new(const gchar *name, const gchar *path)
308 folder = (Folder *)g_new0(IMAPFolder, 1);
309 imap_folder_init(folder, name, path);
314 void imap_folder_destroy(Folder *folder)
318 dir = folder_get_path(folder);
319 if (is_dir_exist(dir))
320 remove_dir_recursive(dir);
323 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
326 static void imap_folder_init(Folder *folder, const gchar *name,
329 folder->type = F_IMAP;
331 folder_remote_folder_init((Folder *)folder, name, path);
334 folder->get_msg_list = imap_get_msg_list;
336 folder->item_new = imap_folder_item_new;
337 folder->item_destroy = imap_folder_item_destroy;
338 folder->fetch_msg = imap_fetch_msg;
339 folder->add_msg = imap_add_msg;
340 folder->move_msg = imap_move_msg;
342 folder->move_msgs_with_dest = imap_move_msgs_with_dest;
344 folder->copy_msg = imap_copy_msg;
346 folder->copy_msgs_with_dest = imap_copy_msgs_with_dest;
348 folder->remove_msg = imap_remove_msg;
349 folder->remove_msgs = imap_remove_msgs;
350 folder->remove_all_msg = imap_remove_all_msg;
351 folder->is_msg_changed = imap_is_msg_changed;
353 folder->scan = imap_scan_folder;
355 folder->scan_tree = imap_scan_tree;
356 folder->create_tree = imap_create_tree;
357 folder->create_folder = imap_create_folder;
358 folder->rename_folder = imap_rename_folder;
359 folder->remove_folder = imap_remove_folder;
360 folder->destroy = imap_folder_destroy;
361 folder->check_msgnum_validity = imap_check_msgnum_validity;
363 folder->get_num_list = imap_get_num_list;
364 folder->get_msginfo = imap_get_msginfo;
365 folder->get_msginfos = imap_get_msginfos;
368 static FolderItem *imap_folder_item_new(Folder *folder)
370 IMAPFolderItem *item;
372 item = g_new0(IMAPFolderItem, 1);
374 item->uid_list = NULL;
376 return (FolderItem *)item;
379 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
381 IMAPFolderItem *item = (IMAPFolderItem *)_item;
383 g_return_if_fail(item != NULL);
384 g_slist_free(item->uid_list);
389 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
391 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
394 g_slist_free(item->uid_list);
395 item->uid_list = NULL;
400 static void imap_reset_uid_lists(Folder *folder)
402 if(folder->node == NULL)
405 /* Destroy all uid lists and rest last uid */
406 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
409 static IMAPSession *imap_session_get(Folder *folder)
411 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
412 Session *session = NULL;
415 g_return_val_if_fail(folder != NULL, NULL);
416 g_return_val_if_fail(folder->type == F_IMAP, NULL);
417 g_return_val_if_fail(folder->account != NULL, NULL);
420 port = folder->account->set_imapport ? folder->account->imapport
421 : folder->account->ssl_imap == SSL_TUNNEL
422 ? IMAPS_PORT : IMAP4_PORT;
424 port = folder->account->set_imapport ? folder->account->imapport
428 /* Make sure we have a session */
429 if (rfolder->session != NULL) {
430 session = rfolder->session;
432 imap_reset_uid_lists(folder);
433 session = imap_session_new(folder->account);
439 imap_get_capability(session);
441 /* Make sure session is authenticated */
442 if (!IMAP_SESSION(session)->authenticated)
443 imap_session_authenticate(IMAP_SESSION(session), folder->account);
444 if (!IMAP_SESSION(session)->authenticated) {
445 session_destroy(session);
446 rfolder->session = NULL;
450 /* Make sure we have parsed the IMAP namespace */
451 imap_parse_namespace(IMAP_SESSION(session),
452 IMAP_FOLDER(folder));
454 /* I think the point of this code is to avoid sending a
455 * keepalive if we've used the session recently and therefore
456 * think it's still alive. Unfortunately, most of the code
457 * does not yet check for errors on the socket, and so if the
458 * connection drops we don't notice until the timeout expires.
459 * A better solution than sending a NOOP every time would be
460 * for every command to be prepared to retry until it is
461 * successfully sent. -- mbp */
462 if (time(NULL) - session->last_access_time > SESSION_TIMEOUT) {
463 /* verify that the session is still alive */
464 if (imap_cmd_noop(session->sock) != IMAP_SUCCESS) {
465 /* Check if this is the first try to establish a
466 connection, if yes we don't try to reconnect */
467 if (rfolder->session == NULL) {
468 log_warning(_("Connecting %s:%d failed"),
469 folder->account->recv_server, port);
470 session_destroy(session);
473 log_warning(_("IMAP4 connection to %s:%d has been"
474 " disconnected. Reconnecting...\n"),
475 folder->account->recv_server, port);
476 session_destroy(session);
477 /* Clear folders session to make imap_session_get create
478 a new session, because of rfolder->session == NULL
479 it will not try to reconnect again and so avoid an
481 rfolder->session = NULL;
482 session = SESSION(imap_session_get(folder));
487 rfolder->session = session;
489 session->last_access_time = time(NULL);
491 return IMAP_SESSION(session);
494 Session *imap_session_new(const PrefsAccount *account)
496 IMAPSession *session;
502 /* FIXME: IMAP over SSL only... */
505 port = account->set_imapport ? account->imapport
506 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
507 ssl_type = account->ssl_imap;
509 port = account->set_imapport ? account->imapport
513 if (account->set_tunnelcmd) {
514 log_message(_("creating tunneled IMAP4 connection\n"));
516 if ((imap_sock = imap_open_tunnel(account->recv_server,
520 if ((imap_sock = imap_open_tunnel(account->recv_server,
521 account->tunnelcmd)) == NULL)
525 g_return_val_if_fail(account->recv_server != NULL, NULL);
527 log_message(_("creating IMAP4 connection to %s:%d ...\n"),
528 account->recv_server, port);
531 if ((imap_sock = imap_open(account->recv_server, port,
534 if ((imap_sock = imap_open(account->recv_server, port)) == NULL)
539 /* Only need to log in if the connection was not PREAUTH */
540 if (imap_greeting(imap_sock, &is_preauth) != IMAP_SUCCESS) {
541 sock_close(imap_sock);
544 log_message("IMAP connection is %s-authenticated\n",
545 (is_preauth) ? "pre" : "un");
547 session = g_new(IMAPSession, 1);
548 SESSION(session)->type = SESSION_IMAP;
549 SESSION(session)->server = g_strdup(account->recv_server);
550 SESSION(session)->sock = imap_sock;
551 SESSION(session)->connected = TRUE;
552 SESSION(session)->phase = SESSION_READY;
553 SESSION(session)->last_access_time = time(NULL);
554 SESSION(session)->data = NULL;
556 SESSION(session)->destroy = imap_session_destroy;
558 session->capability = NULL;
560 session->mbox = NULL;
561 session->authenticated = is_preauth;
563 session_list = g_list_append(session_list, session);
565 return SESSION(session);
568 void imap_session_authenticate(IMAPSession *session, const PrefsAccount *account)
572 g_return_if_fail(account->userid != NULL);
574 pass = account->passwd;
577 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
580 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
584 if (imap_cmd_login(SESSION(session)->sock, account->userid, pass) != IMAP_SUCCESS) {
585 imap_cmd_logout(SESSION(session)->sock);
589 session->authenticated = TRUE;
592 void imap_session_destroy(Session *session)
594 sock_close(session->sock);
595 session->sock = NULL;
597 g_free(IMAP_SESSION(session)->mbox);
599 g_strfreev(IMAP_SESSION(session)->capability);
601 session_list = g_list_remove(session_list, session);
604 void imap_session_destroy_all(void)
606 while (session_list != NULL) {
607 IMAPSession *session = (IMAPSession *)session_list->data;
609 imap_cmd_logout(SESSION(session)->sock);
610 session_destroy(SESSION(session));
614 #define THROW goto catch
616 GSList *imap_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
618 GSList *mlist = NULL;
619 IMAPSession *session;
620 gint ok, exists = 0, recent = 0, unseen = 0;
621 guint32 uid_validity = 0;
622 guint32 first_uid = 0, last_uid = 0, begin;
624 g_return_val_if_fail(folder != NULL, NULL);
625 g_return_val_if_fail(item != NULL, NULL);
626 g_return_val_if_fail(folder->type == F_IMAP, NULL);
627 g_return_val_if_fail(folder->account != NULL, NULL);
629 session = imap_session_get(folder);
632 mlist = procmsg_read_cache(item, FALSE);
633 item->last_num = procmsg_get_last_num_in_msg_list(mlist);
634 procmsg_set_flags(mlist, item);
638 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
639 &exists, &recent, &unseen, &uid_validity);
640 if (ok != IMAP_SUCCESS) THROW;
642 ok = imap_get_uid(session, 1, &first_uid);
643 if (ok != IMAP_SUCCESS) THROW;
645 ok = imap_get_uid(session, exists, &last_uid);
646 if (ok != IMAP_SUCCESS) THROW;
648 last_uid = first_uid;
650 imap_delete_all_cached_messages(item);
657 mlist = procmsg_read_cache(item, FALSE);
658 procmsg_set_flags(mlist, item);
659 cache_last = procmsg_get_last_num_in_msg_list(mlist);
661 /* calculating the range of envelope to get */
662 if (item->mtime != uid_validity) {
663 /* mailbox is changed (get all) */
665 } else if (last_uid < cache_last) {
666 /* mailbox is changed (get all) */
668 } else if (last_uid == cache_last) {
669 /* mailbox unchanged (get none)*/
672 begin = cache_last + 1;
675 item->mtime = uid_validity;
677 if (first_uid > 0 && last_uid > 0) {
678 mlist = imap_delete_cached_messages(mlist, item,
680 mlist = imap_delete_cached_messages(mlist, item,
685 mlist = imap_delete_cached_messages(mlist, item,
688 imap_delete_all_cached_messages(item);
692 if (begin > 0 && begin <= last_uid) {
694 newlist = imap_get_uncached_messages(session, item,
696 mlist = g_slist_concat(mlist, newlist);
699 item->last_num = last_uid;
707 gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
709 gchar *path, *filename;
710 IMAPSession *session;
713 g_return_val_if_fail(folder != NULL, NULL);
714 g_return_val_if_fail(item != NULL, NULL);
716 path = folder_item_get_path(item);
717 if (!is_dir_exist(path))
719 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
722 if (is_file_exist(filename)) {
723 debug_print("message %d has been already cached.\n", uid);
727 session = imap_session_get(folder);
733 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
734 NULL, NULL, NULL, NULL);
735 if (ok != IMAP_SUCCESS) {
736 g_warning("can't select mailbox %s\n", item->path);
741 debug_print("getting message %d...\n", uid);
742 ok = imap_cmd_fetch(SESSION(session)->sock, (guint32)uid, filename);
744 if (ok != IMAP_SUCCESS) {
745 g_warning("can't fetch message %d\n", uid);
753 gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
754 gboolean remove_source)
757 IMAPSession *session;
760 g_return_val_if_fail(folder != NULL, -1);
761 g_return_val_if_fail(dest != NULL, -1);
762 g_return_val_if_fail(file != NULL, -1);
764 session = imap_session_get(folder);
765 if (!session) return -1;
767 destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
768 ok = imap_cmd_append(SESSION(session)->sock, destdir, file);
771 if (ok != IMAP_SUCCESS) {
772 g_warning("can't append message %s\n", file);
777 if (unlink(file) < 0)
778 FILE_OP_ERROR(file, "unlink");
784 static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
785 gboolean remove_source)
788 IMAPSession *session;
789 IMAPFlags iflags = 0;
790 gint messages, recent, unseen;
791 guint32 newuid = 0, uid_validity;
794 g_return_val_if_fail(folder != NULL, -1);
795 g_return_val_if_fail(folder->type == F_IMAP, -1);
796 g_return_val_if_fail(dest != NULL, -1);
797 g_return_val_if_fail(msginfo != NULL, -1);
799 session = imap_session_get(folder);
800 if (!session) return -1;
802 if (msginfo->folder == dest) {
803 g_warning("the src folder is identical to the dest.\n");
807 /* if we don't have UIDPLUS we get UID-NEXT and hope that the
808 message really gets this uid */
809 if (!imap_has_capability(session, "UIDPLUS")) {
810 ok = imap_status(session, IMAP_FOLDER(folder), dest->path,
811 &messages, &recent, &newuid, &uid_validity, &unseen);
812 if (ok != IMAP_SUCCESS) {
813 g_warning("can't copy message\n");
818 destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
820 /* ensure source folder selected */
821 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
822 NULL, NULL, NULL, NULL);
823 if (ok != IMAP_SUCCESS)
827 debug_print("Moving message %s%c%d to %s ...\n",
828 msginfo->folder->path, G_DIR_SEPARATOR,
829 msginfo->msgnum, destdir);
831 debug_print("Copying message %s%c%d to %s ...\n",
832 msginfo->folder->path, G_DIR_SEPARATOR,
833 msginfo->msgnum, destdir);
835 ok = imap_cmd_copy(session, msginfo->msgnum, destdir, &newuid);
837 if (ok == IMAP_SUCCESS && remove_source) {
838 imap_set_message_flags(session, msginfo->msgnum, msginfo->msgnum,
839 IMAP_FLAG_DELETED, TRUE);
840 ok = imap_cmd_expunge(SESSION(session)->sock);
843 /* get the dest folder to set the flags */
845 ok = imap_select(session, IMAP_FOLDER(folder), dest->path,
846 NULL, NULL, NULL, NULL);
847 if (ok != IMAP_SUCCESS) /* the folder disappeared? */
850 if (msginfo->flags.perm_flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
851 if (msginfo->flags.perm_flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
853 if (imap_set_message_flags(session, newuid, newuid, iflags, TRUE)
860 if (ok == IMAP_SUCCESS)
867 static gint imap_do_copy_msgs_with_dest(Folder *folder, FolderItem *dest,
869 gboolean remove_source)
874 IMAPSession *session;
875 gint ok = IMAP_SUCCESS;
877 g_return_val_if_fail(folder != NULL, -1);
878 g_return_val_if_fail(dest != NULL, -1);
879 g_return_val_if_fail(msglist != NULL, -1);
881 session = imap_session_get(folder);
882 if (!session) return -1;
884 destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
886 for (cur = msglist; cur != NULL; cur = cur->next) {
887 msginfo = (MsgInfo *)cur->data;
889 if (msginfo->folder == dest) {
890 g_warning("the src folder is identical to the dest.\n");
894 /* ensure source folder selected */
895 ok = imap_select(session, IMAP_FOLDER(folder),
896 msginfo->folder->path, NULL, NULL, NULL, NULL);
899 debug_print("Moving message %s%c%d to %s ...\n",
900 msginfo->folder->path, G_DIR_SEPARATOR,
901 msginfo->msgnum, destdir);
903 debug_print("Copying message %s%c%d to %s ...\n",
904 msginfo->folder->path, G_DIR_SEPARATOR,
905 msginfo->msgnum, destdir);
907 ok = imap_cmd_copy(session, msginfo, destdir);
909 if (ok == IMAP_SUCCESS && remove_source) {
910 imap_set_message_flags
911 (session, msginfo->msgnum, msginfo->msgnum,
912 IMAP_FLAG_DELETED, TRUE);
917 ok = imap_cmd_expunge(SESSION(session)->sock);
921 if (ok == IMAP_SUCCESS)
928 gint imap_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
933 g_return_val_if_fail(folder != NULL, -1);
934 g_return_val_if_fail(dest != NULL, -1);
935 g_return_val_if_fail(msginfo != NULL, -1);
936 g_return_val_if_fail(msginfo->folder != NULL, -1);
938 if (folder == msginfo->folder->folder)
939 return imap_do_copy(folder, dest, msginfo, TRUE);
941 srcfile = procmsg_get_message_file(msginfo);
942 if (!srcfile) return -1;
944 ret = imap_add_msg(folder, dest, srcfile, FALSE);
948 if(folder_item_remove_msg(msginfo->folder, msginfo->msgnum)) {
957 gint imap_move_msgs_with_dest(Folder *folder, FolderItem *dest,
964 g_return_val_if_fail(folder != NULL, -1);
965 g_return_val_if_fail(dest != NULL, -1);
966 g_return_val_if_fail(msglist != NULL, -1);
968 msginfo = (MsgInfo *)msglist->data;
969 if (folder == msginfo->folder->folder)
970 return imap_do_copy_msgs_with_dest(folder, dest, msglist, TRUE);
972 for (cur = msglist; cur != NULL; cur = cur->next) {
973 msginfo = (MsgInfo *)cur->data;
974 ret = imap_move_msg(folder, dest, msginfo);
975 if (ret == -1) break;
982 gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
987 g_return_val_if_fail(folder != NULL, -1);
988 g_return_val_if_fail(dest != NULL, -1);
989 g_return_val_if_fail(msginfo != NULL, -1);
990 g_return_val_if_fail(msginfo->folder != NULL, -1);
992 if (folder == msginfo->folder->folder)
993 return imap_do_copy(folder, dest, msginfo, FALSE);
995 srcfile = procmsg_get_message_file(msginfo);
996 if (!srcfile) return -1;
998 ret = imap_add_msg(folder, dest, srcfile, FALSE);
1006 gint imap_copy_msgs_with_dest(Folder *folder, FolderItem *dest,
1013 g_return_val_if_fail(folder != NULL, -1);
1014 g_return_val_if_fail(dest != NULL, -1);
1015 g_return_val_if_fail(msglist != NULL, -1);
1017 msginfo = (MsgInfo *)msglist->data;
1018 if (folder == msginfo->folder->folder)
1019 return imap_do_copy_msgs_with_dest
1020 (folder, dest, msglist, FALSE);
1022 for (cur = msglist; cur != NULL; cur = cur->next) {
1023 msginfo = (MsgInfo *)cur->data;
1024 ret = imap_copy_msg(folder, dest, msginfo);
1025 if (ret == -1) break;
1032 gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
1035 IMAPSession *session;
1038 g_return_val_if_fail(folder != NULL, -1);
1039 g_return_val_if_fail(folder->type == F_IMAP, -1);
1040 g_return_val_if_fail(item != NULL, -1);
1042 session = imap_session_get(folder);
1043 if (!session) return -1;
1045 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1046 NULL, NULL, NULL, NULL);
1047 if (ok != IMAP_SUCCESS)
1050 ok = imap_set_message_flags
1051 (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
1052 (guint32)uid, (guint32)uid, IMAP_FLAG_DELETED, TRUE);
1053 if (ok != IMAP_SUCCESS) {
1054 log_warning(_("can't set deleted flags: %d\n"), uid);
1058 ok = imap_cmd_expunge(SESSION(session)->sock);
1059 if (ok != IMAP_SUCCESS) {
1060 log_warning(_("can't expunge\n"));
1064 dir = folder_item_get_path(item);
1065 if (is_dir_exist(dir))
1066 remove_numbered_files(dir, uid, uid);
1069 return IMAP_SUCCESS;
1072 gint imap_remove_msgs(Folder *folder, FolderItem *item, GSList *msglist)
1075 IMAPSession *session;
1081 g_return_val_if_fail(folder != NULL, -1);
1082 g_return_val_if_fail(folder->type == F_IMAP, -1);
1083 g_return_val_if_fail(item != NULL, -1);
1084 g_return_val_if_fail(msglist != NULL, -1);
1086 session = imap_session_get(folder);
1087 if (!session) return -1;
1089 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1090 NULL, NULL, NULL, NULL);
1091 if (ok != IMAP_SUCCESS)
1094 for (cur = msglist; cur != NULL; cur = cur->next) {
1095 msginfo = (MsgInfo *)cur->data;
1096 uid = msginfo->msgnum;
1097 ok = imap_set_message_flags
1098 (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
1099 uid, uid, IMAP_FLAG_DELETED, TRUE);
1100 if (ok != IMAP_SUCCESS) {
1101 log_warning(_("can't set deleted flags: %d\n"), uid);
1106 ok = imap_cmd_expunge(SESSION(session)->sock);
1107 if (ok != IMAP_SUCCESS) {
1108 log_warning(_("can't expunge\n"));
1112 dir = folder_item_get_path(item);
1113 if (is_dir_exist(dir)) {
1114 for (cur = msglist; cur != NULL; cur = cur->next) {
1115 msginfo = (MsgInfo *)cur->data;
1116 uid = msginfo->msgnum;
1117 remove_numbered_files(dir, uid, uid);
1122 return IMAP_SUCCESS;
1125 gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1127 gint exists, recent, unseen;
1128 guint32 uid_validity;
1130 IMAPSession *session;
1133 g_return_val_if_fail(folder != NULL, -1);
1134 g_return_val_if_fail(item != NULL, -1);
1136 session = imap_session_get(folder);
1137 if (!session) return -1;
1139 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1140 &exists, &recent, &unseen, &uid_validity);
1141 if (ok != IMAP_SUCCESS)
1144 return IMAP_SUCCESS;
1146 imap_cmd_gen_send(SESSION(session)->sock,
1147 "STORE 1:%d +FLAGS (\\Deleted)", exists);
1148 ok = imap_cmd_ok(SESSION(session)->sock, NULL);
1149 if (ok != IMAP_SUCCESS) {
1150 log_warning(_("can't set deleted flags: 1:%d\n"), exists);
1154 ok = imap_cmd_expunge(SESSION(session)->sock);
1155 if (ok != IMAP_SUCCESS) {
1156 log_warning(_("can't expunge\n"));
1160 dir = folder_item_get_path(item);
1161 if (is_dir_exist(dir))
1162 remove_all_numbered_files(dir);
1165 return IMAP_SUCCESS;
1168 gboolean imap_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1170 /* TODO: properly implement this method */
1174 gint imap_scan_folder(Folder *folder, FolderItem *item)
1176 IMAPSession *session;
1177 gint messages, recent, unseen;
1178 guint32 uid_next, uid_validity;
1181 g_return_val_if_fail(folder != NULL, -1);
1182 g_return_val_if_fail(item != NULL, -1);
1184 session = imap_session_get(folder);
1185 if (!session) return -1;
1187 ok = imap_status(session, IMAP_FOLDER(folder), item->path,
1188 &messages, &recent, &uid_next, &uid_validity, &unseen);
1189 if (ok != IMAP_SUCCESS) return -1;
1191 item->new = unseen > 0 ? recent : 0;
1192 item->unread = unseen;
1193 item->total = messages;
1194 item->last_num = (messages > 0 && uid_next > 0) ? uid_next - 1 : 0;
1195 /* item->mtime = uid_validity; */
1200 void imap_scan_tree(Folder *folder)
1203 IMAPSession *session;
1204 gchar *root_folder = NULL;
1206 g_return_if_fail(folder != NULL);
1207 g_return_if_fail(folder->account != NULL);
1209 session = imap_session_get(folder);
1211 if (!folder->node) {
1212 folder_tree_destroy(folder);
1213 item = folder_item_new(folder, folder->name, NULL);
1214 item->folder = folder;
1215 folder->node = g_node_new(item);
1220 if (folder->account->imap_dir && *folder->account->imap_dir) {
1221 Xstrdup_a(root_folder, folder->account->imap_dir, return);
1222 strtailchomp(root_folder, '/');
1223 debug_print("IMAP root directory: %s\n", root_folder);
1226 item = folder_item_new(folder, folder->name, root_folder);
1227 item->folder = folder;
1228 item->no_select = TRUE;
1229 folder->node = g_node_new(item);
1231 imap_scan_tree_recursive(session, item);
1233 imap_create_missing_folders(folder);
1236 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
1239 IMAPFolder *imapfolder;
1240 FolderItem *new_item;
1241 GSList *item_list, *cur;
1243 gchar *wildcard_path;
1247 g_return_val_if_fail(item != NULL, -1);
1248 g_return_val_if_fail(item->folder != NULL, -1);
1249 g_return_val_if_fail(item->no_sub == FALSE, -1);
1251 folder = FOLDER(item->folder);
1252 imapfolder = IMAP_FOLDER(folder);
1254 separator = imap_get_path_separator(imapfolder, item->path);
1256 if (item->folder->ui_func)
1257 item->folder->ui_func(folder, item, folder->ui_func_data);
1260 wildcard[0] = separator;
1263 real_path = imap_get_real_path(imapfolder, item->path);
1267 real_path = g_strdup("");
1270 Xstrcat_a(wildcard_path, real_path, wildcard,
1271 {g_free(real_path); return IMAP_ERROR;});
1272 QUOTE_IF_REQUIRED(wildcard_path, wildcard_path);
1274 imap_cmd_gen_send(SESSION(session)->sock, "LIST \"\" %s",
1277 strtailchomp(real_path, separator);
1278 item_list = imap_parse_list(folder, session, real_path, NULL);
1281 for (cur = item_list; cur != NULL; cur = cur->next) {
1282 new_item = cur->data;
1283 if (!strcmp(new_item->path, "INBOX")) {
1284 if (!folder->inbox) {
1285 new_item->stype = F_INBOX;
1286 item->folder->inbox = new_item;
1288 folder_item_destroy(new_item);
1291 } else if (!item->parent || item->stype == F_INBOX) {
1294 base = g_basename(new_item->path);
1296 if (!folder->outbox && !strcasecmp(base, "Sent")) {
1297 new_item->stype = F_OUTBOX;
1298 folder->outbox = new_item;
1299 } else if (!folder->draft && !strcasecmp(base, "Drafts")) {
1300 new_item->stype = F_DRAFT;
1301 folder->draft = new_item;
1302 } else if (!folder->queue && !strcasecmp(base, "Queue")) {
1303 new_item->stype = F_QUEUE;
1304 folder->queue = new_item;
1305 } else if (!folder->trash && !strcasecmp(base, "Trash")) {
1306 new_item->stype = F_TRASH;
1307 folder->trash = new_item;
1310 folder_item_append(item, new_item);
1311 if (new_item->no_select == FALSE)
1312 imap_scan_folder(folder, new_item);
1313 if (new_item->no_sub == FALSE)
1314 imap_scan_tree_recursive(session, new_item);
1317 return IMAP_SUCCESS;
1320 static GSList *imap_parse_list(Folder *folder, IMAPSession *session,
1321 const gchar *real_path, gchar *separator)
1323 gchar buf[IMAPBUFSIZE];
1325 gchar separator_str[16];
1328 gchar *loc_name, *loc_path;
1329 GSList *item_list = NULL;
1331 FolderItem *new_item;
1333 debug_print("getting list of %s ...\n",
1334 *real_path ? real_path : "\"\"");
1336 str = g_string_new(NULL);
1339 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) <= 0) {
1340 log_warning(_("error occured while getting LIST.\n"));
1344 if (buf[0] != '*' || buf[1] != ' ') {
1345 log_print("IMAP4< %s\n", buf);
1348 debug_print("IMAP4< %s\n", buf);
1350 g_string_assign(str, buf);
1352 if (strncmp(p, "LIST ", 5) != 0) continue;
1355 if (*p != '(') continue;
1357 p = strchr_cpy(p, ')', flags, sizeof(flags));
1359 while (*p == ' ') p++;
1361 p = strchr_cpy(p, ' ', separator_str, sizeof(separator_str));
1363 extract_quote(separator_str, '"');
1364 if (!strcmp(separator_str, "NIL"))
1365 separator_str[0] = '\0';
1367 *separator = separator_str[0];
1370 while (*p == ' ') p++;
1371 if (*p == '{' || *p == '"')
1372 p = imap_parse_atom(SESSION(session)->sock, p,
1373 buf, sizeof(buf), str);
1375 strncpy2(buf, p, sizeof(buf));
1376 strtailchomp(buf, separator_str[0]);
1377 if (buf[0] == '\0') continue;
1378 if (!strcmp(buf, real_path)) continue;
1380 if (separator_str[0] != '\0')
1381 subst_char(buf, separator_str[0], '/');
1382 name = g_basename(buf);
1383 if (name[0] == '.') continue;
1385 loc_name = imap_modified_utf7_to_locale(name);
1386 loc_path = imap_modified_utf7_to_locale(buf);
1387 new_item = folder_item_new(folder, loc_name, loc_path);
1388 if (strcasestr(flags, "\\Noinferiors") != NULL)
1389 new_item->no_sub = TRUE;
1390 if (strcmp(buf, "INBOX") != 0 &&
1391 strcasestr(flags, "\\Noselect") != NULL)
1392 new_item->no_select = TRUE;
1394 item_list = g_slist_append(item_list, new_item);
1396 debug_print("folder %s has been added.\n", loc_path);
1401 g_string_free(str, TRUE);
1406 gint imap_create_tree(Folder *folder)
1408 g_return_val_if_fail(folder != NULL, -1);
1409 g_return_val_if_fail(folder->node != NULL, -1);
1410 g_return_val_if_fail(folder->node->data != NULL, -1);
1411 g_return_val_if_fail(folder->account != NULL, -1);
1413 imap_scan_tree(folder);
1414 imap_create_missing_folders(folder);
1419 static void imap_create_missing_folders(Folder *folder)
1421 g_return_if_fail(folder != NULL);
1424 folder->inbox = imap_create_special_folder
1425 (folder, F_INBOX, "INBOX");
1427 if (!folder->outbox)
1428 folder->outbox = imap_create_special_folder
1429 (folder, F_OUTBOX, "Sent");
1431 folder->draft = imap_create_special_folder
1432 (folder, F_DRAFT, "Drafts");
1434 folder->queue = imap_create_special_folder
1435 (folder, F_QUEUE, "Queue");
1438 folder->trash = imap_create_special_folder
1439 (folder, F_TRASH, "Trash");
1442 static FolderItem *imap_create_special_folder(Folder *folder,
1443 SpecialFolderItemType stype,
1447 FolderItem *new_item;
1449 g_return_val_if_fail(folder != NULL, NULL);
1450 g_return_val_if_fail(folder->node != NULL, NULL);
1451 g_return_val_if_fail(folder->node->data != NULL, NULL);
1452 g_return_val_if_fail(folder->account != NULL, NULL);
1453 g_return_val_if_fail(name != NULL, NULL);
1455 item = FOLDER_ITEM(folder->node->data);
1456 new_item = imap_create_folder(folder, item, name);
1459 g_warning("Can't create '%s'\n", name);
1460 if (!folder->inbox) return NULL;
1462 new_item = imap_create_folder(folder, folder->inbox, name);
1464 g_warning("Can't create '%s' under INBOX\n", name);
1466 new_item->stype = stype;
1468 new_item->stype = stype;
1473 FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
1476 gchar *dirpath, *imap_path;
1477 IMAPSession *session;
1478 FolderItem *new_item;
1484 g_return_val_if_fail(folder != NULL, NULL);
1485 g_return_val_if_fail(folder->account != NULL, NULL);
1486 g_return_val_if_fail(parent != NULL, NULL);
1487 g_return_val_if_fail(name != NULL, NULL);
1489 session = imap_session_get(folder);
1490 if (!session) return NULL;
1492 if (!parent->parent && strcmp(name, "INBOX") == 0)
1493 dirpath = g_strdup(name);
1494 else if (parent->path)
1495 dirpath = g_strconcat(parent->path, "/", name, NULL);
1496 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
1497 dirpath = g_strdup(name);
1498 else if (folder->account->imap_dir && *folder->account->imap_dir) {
1501 Xstrdup_a(imap_dir, folder->account->imap_dir, return NULL);
1502 strtailchomp(imap_dir, '/');
1503 dirpath = g_strconcat(imap_dir, "/", name, NULL);
1505 dirpath = g_strdup(name);
1507 /* keep trailing directory separator to create a folder that contains
1509 imap_path = imap_locale_to_modified_utf7(dirpath);
1510 strtailchomp(dirpath, '/');
1511 Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
1512 strtailchomp(new_name, '/');
1513 separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
1514 imap_path_separator_subst(imap_path, separator);
1515 subst_char(new_name, '/', separator);
1517 if (strcmp(name, "INBOX") != 0) {
1520 gboolean exist = FALSE;
1522 argbuf = g_ptr_array_new();
1523 ok = imap_cmd_list(SESSION(session)->sock, NULL, imap_path,
1525 if (ok != IMAP_SUCCESS) {
1526 log_warning(_("can't create mailbox: LIST failed\n"));
1529 ptr_array_free_strings(argbuf);
1530 g_ptr_array_free(argbuf, TRUE);
1534 for (i = 0; i < argbuf->len; i++) {
1536 str = g_ptr_array_index(argbuf, i);
1537 if (!strncmp(str, "LIST ", 5)) {
1542 ptr_array_free_strings(argbuf);
1543 g_ptr_array_free(argbuf, TRUE);
1546 ok = imap_cmd_create(SESSION(session)->sock, imap_path);
1547 if (ok != IMAP_SUCCESS) {
1548 log_warning(_("can't create mailbox\n"));
1556 new_item = folder_item_new(folder, new_name, dirpath);
1557 folder_item_append(parent, new_item);
1561 dirpath = folder_item_get_path(new_item);
1562 if (!is_dir_exist(dirpath))
1563 make_dir_hier(dirpath);
1569 gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
1573 gchar *real_oldpath;
1574 gchar *real_newpath;
1577 gchar *old_cache_dir;
1578 gchar *new_cache_dir;
1579 IMAPSession *session;
1582 gint exists, recent, unseen;
1583 guint32 uid_validity;
1585 g_return_val_if_fail(folder != NULL, -1);
1586 g_return_val_if_fail(item != NULL, -1);
1587 g_return_val_if_fail(item->path != NULL, -1);
1588 g_return_val_if_fail(name != NULL, -1);
1590 session = imap_session_get(folder);
1591 if (!session) return -1;
1593 real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
1595 g_free(session->mbox);
1596 session->mbox = NULL;
1597 ok = imap_cmd_examine(SESSION(session)->sock, "INBOX",
1598 &exists, &recent, &unseen, &uid_validity);
1599 if (ok != IMAP_SUCCESS) {
1600 g_free(real_oldpath);
1604 separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path);
1605 if (strchr(item->path, G_DIR_SEPARATOR)) {
1606 dirpath = g_dirname(item->path);
1607 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
1610 newpath = g_strdup(name);
1612 real_newpath = imap_locale_to_modified_utf7(newpath);
1613 imap_path_separator_subst(real_newpath, separator);
1615 ok = imap_cmd_rename(SESSION(session)->sock, real_oldpath, real_newpath);
1616 if (ok != IMAP_SUCCESS) {
1617 log_warning(_("can't rename mailbox: %s to %s\n"),
1618 real_oldpath, real_newpath);
1619 g_free(real_oldpath);
1621 g_free(real_newpath);
1626 item->name = g_strdup(name);
1628 old_cache_dir = folder_item_get_path(item);
1630 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
1632 paths[0] = g_strdup(item->path);
1634 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1635 imap_rename_folder_func, paths);
1637 if (is_dir_exist(old_cache_dir)) {
1638 new_cache_dir = folder_item_get_path(item);
1639 if (rename(old_cache_dir, new_cache_dir) < 0) {
1640 FILE_OP_ERROR(old_cache_dir, "rename");
1642 g_free(new_cache_dir);
1645 g_free(old_cache_dir);
1648 g_free(real_oldpath);
1649 g_free(real_newpath);
1654 gint imap_remove_folder(Folder *folder, FolderItem *item)
1657 IMAPSession *session;
1660 gint exists, recent, unseen;
1661 guint32 uid_validity;
1663 g_return_val_if_fail(folder != NULL, -1);
1664 g_return_val_if_fail(item != NULL, -1);
1665 g_return_val_if_fail(item->path != NULL, -1);
1667 session = imap_session_get(folder);
1668 if (!session) return -1;
1670 path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
1672 ok = imap_cmd_examine(SESSION(session)->sock, "INBOX",
1673 &exists, &recent, &unseen, &uid_validity);
1674 if (ok != IMAP_SUCCESS) {
1679 ok = imap_cmd_delete(SESSION(session)->sock, path);
1680 if (ok != IMAP_SUCCESS) {
1681 log_warning(_("can't delete mailbox\n"));
1687 cache_dir = folder_item_get_path(item);
1688 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
1689 g_warning("can't remove directory '%s'\n", cache_dir);
1691 folder_item_remove(item);
1696 static GSList *imap_get_uncached_messages(IMAPSession *session,
1698 guint32 first_uid, guint32 last_uid)
1701 GSList *newlist = NULL;
1702 GSList *llast = NULL;
1706 g_return_val_if_fail(session != NULL, NULL);
1707 g_return_val_if_fail(item != NULL, NULL);
1708 g_return_val_if_fail(item->folder != NULL, NULL);
1709 g_return_val_if_fail(item->folder->type == F_IMAP, NULL);
1710 g_return_val_if_fail(first_uid <= last_uid, NULL);
1712 if (imap_cmd_envelope(SESSION(session)->sock, first_uid, last_uid)
1714 log_warning(_("can't get envelope\n"));
1718 str = g_string_new(NULL);
1721 if ((tmp = sock_getline(SESSION(session)->sock)) == NULL) {
1722 log_warning(_("error occurred while getting envelope.\n"));
1723 g_string_free(str, TRUE);
1727 if (tmp[0] != '*' || tmp[1] != ' ') {
1728 log_print("IMAP4< %s\n", tmp);
1732 if (strstr(tmp, "FETCH") == NULL) {
1733 log_print("IMAP4< %s\n", tmp);
1737 log_print("IMAP4< %s\n", tmp);
1738 g_string_assign(str, tmp);
1741 msginfo = imap_parse_envelope
1742 (SESSION(session)->sock, item, str);
1744 log_warning(_("can't parse envelope: %s\n"), str->str);
1747 if (item->stype == F_QUEUE) {
1748 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED);
1749 } else if (item->stype == F_DRAFT) {
1750 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT);
1753 msginfo->folder = item;
1756 llast = newlist = g_slist_append(newlist, msginfo);
1758 llast = g_slist_append(llast, msginfo);
1759 llast = llast->next;
1763 g_string_free(str, TRUE);
1768 static GSList *imap_delete_cached_messages(GSList *mlist, FolderItem *item,
1769 guint32 first_uid, guint32 last_uid)
1775 g_return_val_if_fail(item != NULL, mlist);
1776 g_return_val_if_fail(item->folder != NULL, mlist);
1777 g_return_val_if_fail(item->folder->type == F_IMAP, mlist);
1779 debug_print("Deleting cached messages %u - %u ... ",
1780 first_uid, last_uid);
1782 dir = folder_item_get_path(item);
1783 if (is_dir_exist(dir))
1784 remove_numbered_files(dir, first_uid, last_uid);
1787 for (cur = mlist; cur != NULL; ) {
1790 msginfo = (MsgInfo *)cur->data;
1791 if (msginfo != NULL && first_uid <= msginfo->msgnum &&
1792 msginfo->msgnum <= last_uid) {
1793 procmsg_msginfo_free(msginfo);
1794 mlist = g_slist_remove(mlist, msginfo);
1800 debug_print("done.\n");
1805 static void imap_delete_all_cached_messages(FolderItem *item)
1809 g_return_if_fail(item != NULL);
1810 g_return_if_fail(item->folder != NULL);
1811 g_return_if_fail(item->folder->type == F_IMAP);
1813 debug_print("Deleting all cached messages...\n");
1815 dir = folder_item_get_path(item);
1816 if (is_dir_exist(dir))
1817 remove_all_numbered_files(dir);
1820 debug_print("done.\n");
1824 static SockInfo *imap_open_tunnel(const gchar *server,
1825 const gchar *tunnelcmd,
1828 static SockInfo *imap_open_tunnel(const gchar *server,
1829 const gchar *tunnelcmd)
1834 if ((sock = sock_connect_cmd(server, tunnelcmd)) == NULL)
1835 log_warning(_("Can't establish IMAP4 session with: %s\n"),
1839 return imap_init_sock(sock, ssl_type);
1841 return imap_init_sock(sock);
1847 static SockInfo *imap_open(const gchar *server, gushort port,
1850 static SockInfo *imap_open(const gchar *server, gushort port)
1855 if ((sock = sock_connect(server, port)) == NULL) {
1856 log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
1862 if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
1863 log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
1868 return imap_init_sock(sock, ssl_type);
1870 return imap_init_sock(sock);
1875 static SockInfo *imap_init_sock(SockInfo *sock, SSLType ssl_type)
1877 static SockInfo *imap_init_sock(SockInfo *sock)
1882 if (ssl_type == SSL_STARTTLS) {
1885 ok = imap_cmd_starttls(sock);
1886 if (ok != IMAP_SUCCESS) {
1887 log_warning(_("Can't start TLS session.\n"));
1891 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
1898 if (imap_cmd_noop(sock) != IMAP_SUCCESS) {
1899 log_warning(_("Can't establish IMAP4 session.\n"));
1907 static GList *imap_parse_namespace_str(gchar *str)
1912 IMAPNameSpace *namespace;
1913 GList *ns_list = NULL;
1915 while (*p != '\0') {
1916 /* parse ("#foo" "/") */
1918 while (*p && *p != '(') p++;
1919 if (*p == '\0') break;
1922 while (*p && *p != '"') p++;
1923 if (*p == '\0') break;
1927 while (*p && *p != '"') p++;
1928 if (*p == '\0') break;
1932 while (*p && isspace(*p)) p++;
1933 if (*p == '\0') break;
1934 if (strncmp(p, "NIL", 3) == 0)
1936 else if (*p == '"') {
1939 while (*p && *p != '"') p++;
1940 if (*p == '\0') break;
1945 while (*p && *p != ')') p++;
1946 if (*p == '\0') break;
1949 namespace = g_new(IMAPNameSpace, 1);
1950 namespace->name = g_strdup(name);
1951 namespace->separator = separator ? separator[0] : '\0';
1952 ns_list = g_list_append(ns_list, namespace);
1958 static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
1963 g_return_if_fail(session != NULL);
1964 g_return_if_fail(folder != NULL);
1966 if (folder->ns_personal != NULL ||
1967 folder->ns_others != NULL ||
1968 folder->ns_shared != NULL)
1971 if (!imap_has_capability(session, "NAMESPACE")) {
1972 imap_get_namespace_by_list(session, folder);
1976 if (imap_cmd_namespace(SESSION(session)->sock, &ns_str)
1978 log_warning(_("can't get namespace\n"));
1982 str_array = strsplit_parenthesis(ns_str, '(', ')', 3);
1983 if (str_array == NULL) {
1985 imap_get_namespace_by_list(session, folder);
1989 folder->ns_personal = imap_parse_namespace_str(str_array[0]);
1990 if (str_array[0] && str_array[1])
1991 folder->ns_others = imap_parse_namespace_str(str_array[1]);
1992 if (str_array[0] && str_array[1] && str_array[2])
1993 folder->ns_shared = imap_parse_namespace_str(str_array[2]);
1994 g_strfreev(str_array);
1998 static void imap_get_namespace_by_list(IMAPSession *session, IMAPFolder *folder)
2000 GSList *item_list, *cur;
2001 gchar separator = '\0';
2002 IMAPNameSpace *namespace;
2004 g_return_if_fail(session != NULL);
2005 g_return_if_fail(folder != NULL);
2007 if (folder->ns_personal != NULL ||
2008 folder->ns_others != NULL ||
2009 folder->ns_shared != NULL)
2012 imap_cmd_gen_send(SESSION(session)->sock, "LIST \"\" \"\"");
2013 item_list = imap_parse_list(NULL, session, "", &separator);
2014 for (cur = item_list; cur != NULL; cur = cur->next)
2015 folder_item_destroy(FOLDER_ITEM(cur->data));
2016 g_slist_free(item_list);
2018 namespace = g_new(IMAPNameSpace, 1);
2019 namespace->name = g_strdup("");
2020 namespace->separator = separator;
2021 folder->ns_personal = g_list_append(NULL, namespace);
2024 static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
2027 IMAPNameSpace *namespace = NULL;
2028 gchar *tmp_path, *name;
2030 if (!path) path = "";
2032 Xstrcat_a(tmp_path, path, "/", return NULL);
2034 for (; ns_list != NULL; ns_list = ns_list->next) {
2035 IMAPNameSpace *tmp_ns = ns_list->data;
2037 Xstrdup_a(name, tmp_ns->name, return namespace);
2038 if (tmp_ns->separator && tmp_ns->separator != '/')
2039 subst_char(name, tmp_ns->separator, '/');
2040 if (strncmp(tmp_path, name, strlen(name)) == 0)
2047 static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
2050 IMAPNameSpace *namespace;
2052 g_return_val_if_fail(folder != NULL, NULL);
2054 namespace = imap_find_namespace_from_list(folder->ns_personal, path);
2055 if (namespace) return namespace;
2056 namespace = imap_find_namespace_from_list(folder->ns_others, path);
2057 if (namespace) return namespace;
2058 namespace = imap_find_namespace_from_list(folder->ns_shared, path);
2059 if (namespace) return namespace;
2064 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
2066 IMAPNameSpace *namespace;
2067 gchar separator = '/';
2069 namespace = imap_find_namespace(folder, path);
2070 if (namespace && namespace->separator)
2071 separator = namespace->separator;
2076 static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
2081 g_return_val_if_fail(folder != NULL, NULL);
2082 g_return_val_if_fail(path != NULL, NULL);
2084 real_path = imap_locale_to_modified_utf7(path);
2085 separator = imap_get_path_separator(folder, path);
2086 imap_path_separator_subst(real_path, separator);
2091 static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
2092 gchar *dest, gint dest_len, GString *str)
2094 gchar *cur_pos = src;
2097 g_return_val_if_fail(str != NULL, cur_pos);
2099 /* read the next line if the current response buffer is empty */
2100 while (isspace(*cur_pos)) cur_pos++;
2101 while (*cur_pos == '\0') {
2102 if ((nextline = sock_getline(sock)) == NULL)
2104 g_string_assign(str, nextline);
2106 strretchomp(nextline);
2107 /* log_print("IMAP4< %s\n", nextline); */
2108 debug_print("IMAP4< %s\n", nextline);
2111 while (isspace(*cur_pos)) cur_pos++;
2114 if (!strncmp(cur_pos, "NIL", 3)) {
2117 } else if (*cur_pos == '\"') {
2120 p = get_quoted(cur_pos, '\"', dest, dest_len);
2121 cur_pos = p ? p : cur_pos + 2;
2122 } else if (*cur_pos == '{') {
2127 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
2130 g_string_truncate(str, 0);
2134 if ((nextline = sock_getline(sock)) == NULL)
2136 line_len += strlen(nextline);
2137 g_string_append(str, nextline);
2139 strretchomp(nextline);
2140 /* log_print("IMAP4< %s\n", nextline); */
2141 debug_print("IMAP4< %s\n", nextline);
2143 } while (line_len < len);
2145 memcpy(dest, cur_pos, MIN(len, dest_len - 1));
2146 dest[MIN(len, dest_len - 1)] = '\0';
2153 static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
2163 g_return_val_if_fail(str != NULL, cur_pos);
2165 while (isspace(*cur_pos)) cur_pos++;
2167 g_return_val_if_fail(*cur_pos == '{', cur_pos);
2169 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
2172 g_string_truncate(str, 0);
2176 if ((nextline = sock_getline(sock)) == NULL)
2178 block_len += strlen(nextline);
2179 g_string_append(str, nextline);
2181 strretchomp(nextline);
2182 /* debug_print("IMAP4< %s\n", nextline); */
2184 } while (block_len < len);
2186 debug_print("IMAP4< [contents of BODY.PEEK[HEADER.FIELDS (...)]]\n");
2188 *headers = g_strndup(cur_pos, len);
2191 while (isspace(*cur_pos)) cur_pos++;
2192 while (*cur_pos == '\0') {
2193 if ((nextline = sock_getline(sock)) == NULL)
2195 g_string_assign(str, nextline);
2197 strretchomp(nextline);
2198 debug_print("IMAP4< %s\n", nextline);
2201 while (isspace(*cur_pos)) cur_pos++;
2207 static MsgFlags imap_parse_flags(const gchar *flag_str)
2209 const gchar *p = flag_str;
2210 MsgFlags flags = {0, 0};
2212 flags.perm_flags = MSG_UNREAD;
2214 while ((p = strchr(p, '\\')) != NULL) {
2217 if (g_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
2218 MSG_SET_PERM_FLAGS(flags, MSG_NEW);
2219 } else if (g_strncasecmp(p, "Seen", 4) == 0) {
2220 MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
2221 } else if (g_strncasecmp(p, "Deleted", 7) == 0) {
2222 MSG_SET_PERM_FLAGS(flags, MSG_DELETED);
2223 } else if (g_strncasecmp(p, "Flagged", 7) == 0) {
2224 MSG_SET_PERM_FLAGS(flags, MSG_MARKED);
2225 } else if (g_strncasecmp(p, "Answered", 8) == 0) {
2226 MSG_SET_PERM_FLAGS(flags, MSG_REPLIED);
2233 static MsgInfo *imap_parse_envelope(SockInfo *sock, FolderItem *item,
2236 gchar buf[IMAPBUFSIZE];
2237 MsgInfo *msginfo = NULL;
2242 MsgFlags flags = {0, 0}, imap_flags = {0, 0};
2244 g_return_val_if_fail(line_str != NULL, NULL);
2245 g_return_val_if_fail(line_str->str[0] == '*' &&
2246 line_str->str[1] == ' ', NULL);
2248 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
2249 if (item->stype == F_QUEUE) {
2250 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
2251 } else if (item->stype == F_DRAFT) {
2252 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
2255 cur_pos = line_str->str + 2;
2257 #define PARSE_ONE_ELEMENT(ch) \
2259 cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
2260 if (cur_pos == NULL) { \
2261 g_warning("cur_pos == NULL\n"); \
2262 procmsg_msginfo_free(msginfo); \
2267 PARSE_ONE_ELEMENT(' ');
2270 PARSE_ONE_ELEMENT(' ');
2271 g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
2273 g_return_val_if_fail(*cur_pos == '(', NULL);
2276 while (*cur_pos != '\0' && *cur_pos != ')') {
2277 while (*cur_pos == ' ') cur_pos++;
2279 if (!strncmp(cur_pos, "UID ", 4)) {
2281 uid = strtoul(cur_pos, &cur_pos, 10);
2282 } else if (!strncmp(cur_pos, "FLAGS ", 6)) {
2284 if (*cur_pos != '(') {
2285 g_warning("*cur_pos != '('\n");
2286 procmsg_msginfo_free(msginfo);
2290 PARSE_ONE_ELEMENT(')');
2291 imap_flags = imap_parse_flags(buf);
2292 } else if (!strncmp(cur_pos, "RFC822.SIZE ", 12)) {
2294 size = strtol(cur_pos, &cur_pos, 10);
2295 } else if (!strncmp(cur_pos, "BODY[HEADER.FIELDS ", 19)) {
2299 if (*cur_pos != '(') {
2300 g_warning("*cur_pos != '('\n");
2301 procmsg_msginfo_free(msginfo);
2305 PARSE_ONE_ELEMENT(')');
2306 if (*cur_pos != ']') {
2307 g_warning("*cur_pos != ']'\n");
2308 procmsg_msginfo_free(msginfo);
2313 cur_pos = imap_get_header(sock, cur_pos, &headers,
2315 msginfo = procheader_parse_str(headers, flags, FALSE, FALSE);
2318 g_warning("invalid FETCH response: %s\n", cur_pos);
2324 msginfo->msgnum = uid;
2325 msginfo->size = size;
2326 msginfo->flags.tmp_flags |= imap_flags.tmp_flags;
2327 msginfo->flags.perm_flags = imap_flags.perm_flags;
2333 gint imap_msg_set_perm_flags(MsgInfo *msginfo, MsgPermFlags flags)
2336 IMAPSession *session;
2337 IMAPFlags iflags = 0;
2338 gint ok = IMAP_SUCCESS;
2340 g_return_val_if_fail(msginfo != NULL, -1);
2341 g_return_val_if_fail(msginfo->folder != NULL, -1);
2342 g_return_val_if_fail(msginfo->folder->folder != NULL, -1);
2344 folder = msginfo->folder->folder;
2345 g_return_val_if_fail(folder->type == F_IMAP, -1);
2347 session = imap_session_get(folder);
2348 if (!session) return -1;
2350 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
2351 NULL, NULL, NULL, NULL)) != IMAP_SUCCESS)
2354 if (flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
2355 if (flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
2357 ok = imap_set_message_flags(session, msginfo->msgnum,
2358 msginfo->msgnum, iflags, TRUE);
2359 if (ok != IMAP_SUCCESS) return ok;
2362 if (flags & MSG_UNREAD)
2363 ok = imap_set_message_flags(session, msginfo->msgnum,
2364 msginfo->msgnum, IMAP_FLAG_SEEN,
2369 gint imap_msg_unset_perm_flags(MsgInfo *msginfo, MsgPermFlags flags)
2372 IMAPSession *session;
2373 IMAPFlags iflags = 0;
2374 gint ok = IMAP_SUCCESS;
2376 g_return_val_if_fail(msginfo != NULL, -1);
2377 g_return_val_if_fail(msginfo->folder != NULL, -1);
2378 g_return_val_if_fail(msginfo->folder->folder != NULL, -1);
2380 folder = msginfo->folder->folder;
2381 g_return_val_if_fail(folder->type == F_IMAP, -1);
2383 session = imap_session_get(folder);
2384 if (!session) return -1;
2386 if (flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
2387 if (flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
2389 ok = imap_set_message_flags(session, msginfo->msgnum,
2390 msginfo->msgnum, iflags, FALSE);
2391 if (ok != IMAP_SUCCESS) return ok;
2394 if (flags & MSG_UNREAD)
2395 ok = imap_set_message_flags(session, msginfo->msgnum,
2396 msginfo->msgnum, IMAP_FLAG_SEEN,
2401 static gint imap_set_message_flags(IMAPSession *session,
2410 buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
2412 if (IMAP_IS_SEEN(flags)) g_string_append(buf, "\\Seen ");
2413 if (IMAP_IS_ANSWERED(flags)) g_string_append(buf, "\\Answered ");
2414 if (IMAP_IS_FLAGGED(flags)) g_string_append(buf, "\\Flagged ");
2415 if (IMAP_IS_DELETED(flags)) g_string_append(buf, "\\Deleted ");
2416 if (IMAP_IS_DRAFT(flags)) g_string_append(buf, "\\Draft");
2418 if (buf->str[buf->len - 1] == ' ')
2419 g_string_truncate(buf, buf->len - 1);
2421 g_string_append_c(buf, ')');
2423 ok = imap_cmd_store(SESSION(session)->sock, first_uid, last_uid,
2425 g_string_free(buf, TRUE);
2430 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
2432 gint *exists, gint *recent, gint *unseen,
2433 guint32 *uid_validity)
2437 gint exists_, recent_, unseen_, uid_validity_;
2439 if (!exists || !recent || !unseen || !uid_validity) {
2440 if (session->mbox && strcmp(session->mbox, path) == 0)
2441 return IMAP_SUCCESS;
2445 uid_validity = &uid_validity_;
2448 g_free(session->mbox);
2449 session->mbox = NULL;
2451 real_path = imap_get_real_path(folder, path);
2452 ok = imap_cmd_select(SESSION(session)->sock, real_path,
2453 exists, recent, unseen, uid_validity);
2454 if (ok != IMAP_SUCCESS)
2455 log_warning(_("can't select folder: %s\n"), real_path);
2457 session->mbox = g_strdup(path);
2463 #define THROW(err) { ok = err; goto catch; }
2465 static gint imap_get_uid(IMAPSession *session, gint msgnum, guint32 *uid)
2473 argbuf = g_ptr_array_new();
2475 imap_cmd_gen_send(SESSION(session)->sock, "FETCH %d (UID)", msgnum);
2476 if ((ok = imap_cmd_ok(SESSION(session)->sock, argbuf)) != IMAP_SUCCESS)
2479 str = search_array_contain_str(argbuf, "FETCH");
2480 if (!str) THROW(IMAP_ERROR);
2482 if (sscanf(str, "%d FETCH (UID %d)", &num, uid) != 2 ||
2484 g_warning("imap_get_uid(): invalid FETCH line.\n");
2489 ptr_array_free_strings(argbuf);
2490 g_ptr_array_free(argbuf, TRUE);
2495 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
2497 gint *messages, gint *recent,
2498 guint32 *uid_next, guint32 *uid_validity,
2507 *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
2509 argbuf = g_ptr_array_new();
2511 real_path = imap_get_real_path(folder, path);
2512 QUOTE_IF_REQUIRED(real_path_, real_path);
2513 imap_cmd_gen_send(SESSION(session)->sock, "STATUS %s "
2514 "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
2517 ok = imap_cmd_ok(SESSION(session)->sock, argbuf);
2518 if (ok != IMAP_SUCCESS) THROW(ok);
2520 str = search_array_str(argbuf, "STATUS");
2521 if (!str) THROW(IMAP_ERROR);
2523 str = strchr(str, '(');
2524 if (!str) THROW(IMAP_ERROR);
2526 while (*str != '\0' && *str != ')') {
2527 while (*str == ' ') str++;
2529 if (!strncmp(str, "MESSAGES ", 9)) {
2531 *messages = strtol(str, &str, 10);
2532 } else if (!strncmp(str, "RECENT ", 7)) {
2534 *recent = strtol(str, &str, 10);
2535 } else if (!strncmp(str, "UIDNEXT ", 8)) {
2537 *uid_next = strtoul(str, &str, 10);
2538 } else if (!strncmp(str, "UIDVALIDITY ", 12)) {
2540 *uid_validity = strtoul(str, &str, 10);
2541 } else if (!strncmp(str, "UNSEEN ", 7)) {
2543 *unseen = strtol(str, &str, 10);
2545 g_warning("invalid STATUS response: %s\n", str);
2552 ptr_array_free_strings(argbuf);
2553 g_ptr_array_free(argbuf, TRUE);
2561 /* low-level IMAP4rev1 commands */
2563 static gint imap_cmd_login(SockInfo *sock,
2564 const gchar *user, const gchar *pass)
2566 gchar *user_, *pass_;
2569 QUOTE_IF_REQUIRED(user_, user);
2570 QUOTE_IF_REQUIRED(pass_, pass);
2571 imap_cmd_gen_send(sock, "LOGIN %s %s", user_, pass_);
2573 ok = imap_cmd_ok(sock, NULL);
2574 if (ok != IMAP_SUCCESS)
2575 log_warning(_("IMAP4 login failed.\n"));
2580 static gint imap_cmd_logout(SockInfo *sock)
2582 imap_cmd_gen_send(sock, "LOGOUT");
2583 return imap_cmd_ok(sock, NULL);
2586 /* Send a NOOP, and examine the server's response to see whether this
2587 * connection is pre-authenticated or not. */
2588 static gint imap_greeting(SockInfo *sock, gboolean *is_preauth)
2593 imap_cmd_gen_send(sock, "NOOP");
2594 argbuf = g_ptr_array_new(); /* will hold messages sent back */
2595 r = imap_cmd_ok(sock, argbuf);
2596 *is_preauth = search_array_str(argbuf, "PREAUTH") != NULL;
2598 ptr_array_free_strings(argbuf);
2599 g_ptr_array_free(argbuf, TRUE);
2604 static void imap_get_capability(Session *session)
2609 if (IMAP_SESSION(session)->capability != NULL)
2612 imap_cmd_gen_send(session->sock, "CAPABILITY");
2614 argbuf = g_ptr_array_new();
2616 if (imap_cmd_ok(session->sock, argbuf) != IMAP_SUCCESS ||
2617 ((capstr = search_array_str(argbuf, "CAPABILITY ")) == NULL)) {
2618 ptr_array_free_strings(argbuf);
2619 g_ptr_array_free(argbuf, TRUE);
2623 capstr += strlen("CAPABILITY ");
2625 IMAP_SESSION(session)->capability = g_strsplit(capstr, " ", 0);
2627 ptr_array_free_strings(argbuf);
2628 g_ptr_array_free(argbuf, TRUE);
2631 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
2635 for (p = session->capability; *p != NULL; ++p)
2636 if (g_strcasecmp(*p, cap) == 0)
2642 static gint imap_cmd_noop(SockInfo *sock)
2644 imap_cmd_gen_send(sock, "NOOP");
2645 return imap_cmd_ok(sock, NULL);
2648 static gint imap_cmd_starttls(SockInfo *sock)
2650 imap_cmd_gen_send(sock, "STARTTLS");
2651 return imap_cmd_ok(sock, NULL);
2654 #define THROW(err) { ok = err; goto catch; }
2656 static gint imap_cmd_namespace(SockInfo *sock, gchar **ns_str)
2662 argbuf = g_ptr_array_new();
2664 imap_cmd_gen_send(sock, "NAMESPACE");
2665 if ((ok = imap_cmd_ok(sock, argbuf)) != IMAP_SUCCESS) THROW(ok);
2667 str = search_array_str(argbuf, "NAMESPACE");
2668 if (!str) THROW(IMAP_ERROR);
2670 *ns_str = g_strdup(str);
2673 ptr_array_free_strings(argbuf);
2674 g_ptr_array_free(argbuf, TRUE);
2681 static gint imap_cmd_list(SockInfo *sock, const gchar *ref,
2682 const gchar *mailbox, GPtrArray *argbuf)
2684 gchar *ref_, *mailbox_;
2686 if (!ref) ref = "\"\"";
2687 if (!mailbox) mailbox = "\"\"";
2689 QUOTE_IF_REQUIRED(ref_, ref);
2690 QUOTE_IF_REQUIRED(mailbox_, mailbox);
2691 imap_cmd_gen_send(sock, "LIST %s %s", ref_, mailbox_);
2693 return imap_cmd_ok(sock, argbuf);
2696 #define THROW goto catch
2698 static gint imap_cmd_do_select(SockInfo *sock, const gchar *folder,
2700 gint *exists, gint *recent, gint *unseen,
2701 guint32 *uid_validity)
2709 *exists = *recent = *unseen = *uid_validity = 0;
2710 argbuf = g_ptr_array_new();
2713 select_cmd = "EXAMINE";
2715 select_cmd = "SELECT";
2717 QUOTE_IF_REQUIRED(folder_, folder);
2718 imap_cmd_gen_send(sock, "%s %s", select_cmd, folder_);
2720 if ((ok = imap_cmd_ok(sock, argbuf)) != IMAP_SUCCESS) THROW;
2722 resp_str = search_array_contain_str(argbuf, "EXISTS");
2724 if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
2725 g_warning("imap_cmd_select(): invalid EXISTS line.\n");
2730 resp_str = search_array_contain_str(argbuf, "RECENT");
2732 if (sscanf(resp_str, "%d RECENT", recent) != 1) {
2733 g_warning("imap_cmd_select(): invalid RECENT line.\n");
2738 resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
2740 if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", uid_validity)
2742 g_warning("imap_cmd_select(): invalid UIDVALIDITY line.\n");
2747 resp_str = search_array_contain_str(argbuf, "UNSEEN");
2749 if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
2750 g_warning("imap_cmd_select(): invalid UNSEEN line.\n");
2756 ptr_array_free_strings(argbuf);
2757 g_ptr_array_free(argbuf, TRUE);
2762 static gint imap_cmd_select(SockInfo *sock, const gchar *folder,
2763 gint *exists, gint *recent, gint *unseen,
2764 guint32 *uid_validity)
2766 return imap_cmd_do_select(sock, folder, FALSE,
2767 exists, recent, unseen, uid_validity);
2770 static gint imap_cmd_examine(SockInfo *sock, const gchar *folder,
2771 gint *exists, gint *recent, gint *unseen,
2772 guint32 *uid_validity)
2774 return imap_cmd_do_select(sock, folder, TRUE,
2775 exists, recent, unseen, uid_validity);
2780 static gint imap_cmd_create(SockInfo *sock, const gchar *folder)
2784 QUOTE_IF_REQUIRED(folder_, folder);
2785 imap_cmd_gen_send(sock, "CREATE %s", folder_);
2787 return imap_cmd_ok(sock, NULL);
2790 static gint imap_cmd_rename(SockInfo *sock, const gchar *old_folder,
2791 const gchar *new_folder)
2793 gchar *old_folder_, *new_folder_;
2795 QUOTE_IF_REQUIRED(old_folder_, old_folder);
2796 QUOTE_IF_REQUIRED(new_folder_, new_folder);
2797 imap_cmd_gen_send(sock, "RENAME %s %s", old_folder_, new_folder_);
2799 return imap_cmd_ok(sock, NULL);
2802 static gint imap_cmd_delete(SockInfo *sock, const gchar *folder)
2806 QUOTE_IF_REQUIRED(folder_, folder);
2807 imap_cmd_gen_send(sock, "DELETE %s", folder_);
2809 return imap_cmd_ok(sock, NULL);
2812 static gint imap_cmd_fetch(SockInfo *sock, guint32 uid, const gchar *filename)
2820 g_return_val_if_fail(filename != NULL, IMAP_ERROR);
2822 imap_cmd_gen_send(sock, "UID FETCH %d BODY[]", uid);
2824 while ((ok = imap_cmd_gen_recv(sock, &buf))
2826 if (buf[0] != '*' || buf[1] != ' ') {
2830 if (strstr(buf, "FETCH") != NULL)
2833 if (ok != IMAP_SUCCESS)
2836 cur_pos = strchr(buf, '{');
2837 if (cur_pos == NULL) {
2841 cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
2842 if (cur_pos == NULL) {
2846 size_num = atol(size_str);
2848 if (*cur_pos != '\0') {
2853 if (recv_bytes_write_to_file(sock, size_num, filename) != 0) {
2858 if (imap_cmd_gen_recv(sock, &buf) != IMAP_SUCCESS) {
2863 if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') {
2869 ok = imap_cmd_ok(sock, NULL);
2874 static gint imap_cmd_append(SockInfo *sock, const gchar *destfolder,
2880 gchar buf[BUFFSIZE], *imapbuf;
2883 g_return_val_if_fail(file != NULL, IMAP_ERROR);
2885 size = get_file_size_as_crlf(file);
2886 if ((fp = fopen(file, "rb")) == NULL) {
2887 FILE_OP_ERROR(file, "fopen");
2890 QUOTE_IF_REQUIRED(destfolder_, destfolder);
2891 imap_cmd_gen_send(sock, "APPEND %s (\\Seen) {%d}", destfolder_, size);
2893 ok = imap_cmd_gen_recv(sock, &imapbuf);
2894 if (ok != IMAP_SUCCESS || imapbuf[0] != '+' || imapbuf[1] != ' ') {
2895 log_warning(_("can't append %s to %s\n"), file, destfolder_);
2902 log_print("IMAP4> %s\n", _("(sending file...)"));
2904 while (fgets(buf, sizeof(buf), fp) != NULL) {
2906 if (sock_puts(sock, buf) < 0) {
2914 FILE_OP_ERROR(file, "fgets");
2920 sock_puts(sock, "");
2923 return imap_cmd_ok(sock, NULL);
2927 static void imap_uidplus_copy(IMAPSession *session, MsgInfo *msginfo,
2928 const gchar *destfolder, const gchar *okmsginfo)
2930 unsigned int olduid, newuid;
2931 IMAPFlags iflags = 0;
2933 if (okmsginfo == NULL)
2936 if (sscanf(okmsginfo, "%*u OK [COPYUID %*llu %u %u]", &olduid, &newuid) != 2)
2939 if (olduid != msginfo->msgnum) /* this should NEVER happen */
2942 if (imap_select(session, IMAP_FOLDER(msginfo->folder->folder), destfolder,
2943 NULL, NULL, NULL, NULL) != IMAP_SUCCESS)
2946 if (msginfo->flags.perm_flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
2947 if (msginfo->flags.perm_flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
2949 imap_set_message_flags(session, newuid, newuid, iflags, TRUE);
2951 if (msginfo->flags.perm_flags & MSG_UNREAD)
2952 imap_set_message_flags(session, newuid, newuid, IMAP_FLAG_SEEN, FALSE);
2954 imap_select(session, IMAP_FOLDER(msginfo->folder->folder), msginfo->folder->path,
2955 NULL, NULL, NULL, NULL);
2959 static gint imap_cmd_copy(IMAPSession *session,
2960 gint32 msgnum, const gchar *destfolder, gint32 *new_uid)
2963 gint32 olduid, newuid;
2968 g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
2969 g_return_val_if_fail(session != NULL, IMAP_ERROR);
2970 g_return_val_if_fail(new_uid != NULL, IMAP_ERROR);
2972 QUOTE_IF_REQUIRED(destfolder_, destfolder);
2973 imap_cmd_gen_send(SESSION(session)->sock, "UID COPY %d %s", msgnum, destfolder_);
2975 reply = g_ptr_array_new();
2977 ok = imap_cmd_ok(SESSION(session)->sock, reply);
2978 if (ok != IMAP_SUCCESS)
2979 log_warning(_("can't copy %d to %s\n"), msgnum, destfolder_);
2980 else if (imap_has_capability(session, "UIDPLUS") && reply->len > 0)
2981 if ((okmsginfo = g_ptr_array_index(reply, reply->len - 1)) != NULL &&
2982 sscanf(okmsginfo, "%*u OK [COPYUID %*llu %u %u]", &olduid, &newuid) == 2 &&
2986 ptr_array_free_strings(reply);
2987 g_ptr_array_free(reply, TRUE);
2991 gint imap_cmd_envelope(SockInfo *sock, guint32 first_uid, guint32 last_uid)
2993 static GString *header_fields = NULL;
2995 if (header_fields == NULL) {
2996 const HeaderEntry *headers, *elem;
2998 headers = procheader_get_headernames(FALSE);
2999 header_fields = g_string_new("");
3001 for (elem = headers; elem->name != NULL; ++elem) {
3002 gint namelen = strlen(elem->name);
3004 while(namelen && (elem->name[namelen - 1] == ' ' ||
3005 elem->name[namelen - 1] == ':'))
3011 g_string_sprintfa(header_fields, "%s%.*s",
3012 header_fields->str[0] != '\0' ? " " : "",
3013 namelen, elem->name);
3018 (sock, "UID FETCH %d:%d (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])",
3019 first_uid, last_uid,
3020 header_fields->str);
3022 return IMAP_SUCCESS;
3025 static gint imap_cmd_store(SockInfo *sock, guint32 first_uid, guint32 last_uid,
3030 imap_cmd_gen_send(sock, "UID STORE %d:%d %s",
3031 first_uid, last_uid, sub_cmd);
3033 if ((ok = imap_cmd_ok(sock, NULL)) != IMAP_SUCCESS) {
3034 log_warning(_("error while imap command: STORE %d:%d %s\n"),
3035 first_uid, last_uid, sub_cmd);
3039 return IMAP_SUCCESS;
3042 static gint imap_cmd_expunge(SockInfo *sock)
3046 imap_cmd_gen_send(sock, "EXPUNGE");
3047 if ((ok = imap_cmd_ok(sock, NULL)) != IMAP_SUCCESS) {
3048 log_warning(_("error while imap command: EXPUNGE\n"));
3052 return IMAP_SUCCESS;
3055 static gint imap_cmd_ok(SockInfo *sock, GPtrArray *argbuf)
3060 gchar cmd_status[IMAPBUFSIZE];
3062 while ((ok = imap_cmd_gen_recv(sock, &buf))
3064 if (buf[0] == '*' && buf[1] == ' ') {
3066 g_ptr_array_add(argbuf, g_strdup(buf + 2));
3070 if (sscanf(buf, "%d %s", &cmd_num, cmd_status) < 2)
3072 else if (cmd_num == imap_cmd_count &&
3073 !strcmp(cmd_status, "OK")) {
3075 g_ptr_array_add(argbuf, g_strdup(buf));
3077 return IMAP_SUCCESS;
3088 static void imap_cmd_gen_send(SockInfo *sock, const gchar *format, ...)
3090 gchar buf[IMAPBUFSIZE];
3091 gchar tmp[IMAPBUFSIZE];
3095 va_start(args, format);
3096 g_vsnprintf(tmp, sizeof(tmp), format, args);
3101 g_snprintf(buf, sizeof(buf), "%d %s\r\n", imap_cmd_count, tmp);
3102 if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
3104 log_print("IMAP4> %d %s ********\n", imap_cmd_count, tmp);
3106 log_print("IMAP4> %d %s\n", imap_cmd_count, tmp);
3108 sock_write(sock, buf, strlen(buf));
3111 static gint imap_cmd_gen_recv(SockInfo *sock, gchar **buf)
3113 if ((*buf = sock_getline(sock)) == NULL)
3118 log_print("IMAP4< %s\n", *buf);
3120 return IMAP_SUCCESS;
3124 /* misc utility functions */
3126 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
3131 tmp = strchr(src, ch);
3135 memcpy(dest, src, MIN(tmp - src, len - 1));
3136 dest[MIN(tmp - src, len - 1)] = '\0';
3141 static gchar *get_quoted(const gchar *src, gchar ch, gchar *dest, gint len)
3143 const gchar *p = src;
3146 g_return_val_if_fail(*p == ch, NULL);
3151 while (*p != '\0' && *p != ch) {
3153 if (*p == '\\' && *(p + 1) != '\0')
3162 return (gchar *)(*p == ch ? p + 1 : p);
3165 static gchar *search_array_contain_str(GPtrArray *array, const gchar *str)
3169 for (i = 0; i < array->len; i++) {
3172 tmp = g_ptr_array_index(array, i);
3173 if (strstr(tmp, str) != NULL)
3180 static gchar *search_array_str(GPtrArray *array, const gchar *str)
3187 for (i = 0; i < array->len; i++) {
3190 tmp = g_ptr_array_index(array, i);
3191 if (!strncmp(tmp, str, len))
3198 static void imap_path_separator_subst(gchar *str, gchar separator)
3201 gboolean in_escape = FALSE;
3203 if (!separator || separator == '/') return;
3205 for (p = str; *p != '\0'; p++) {
3206 if (*p == '/' && !in_escape)
3208 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3210 else if (*p == '-' && in_escape)
3215 static gchar *imap_modified_utf7_to_locale(const gchar *mutf7_str)
3218 return g_strdup(mutf7_str);
3220 static iconv_t cd = (iconv_t)-1;
3221 static gboolean iconv_ok = TRUE;
3224 size_t norm_utf7_len;
3226 gchar *to_str, *to_p;
3228 gboolean in_escape = FALSE;
3230 if (!iconv_ok) return g_strdup(mutf7_str);
3232 if (cd == (iconv_t)-1) {
3233 cd = iconv_open(conv_get_current_charset_str(), "UTF-7");
3234 if (cd == (iconv_t)-1) {
3235 g_warning("iconv cannot convert UTF-7 to %s\n",
3236 conv_get_current_charset_str());
3238 return g_strdup(mutf7_str);
3242 norm_utf7 = g_string_new(NULL);
3244 for (p = mutf7_str; *p != '\0'; p++) {
3245 /* replace: '&' -> '+',
3247 escaped ',' -> '/' */
3248 if (!in_escape && *p == '&') {
3249 if (*(p + 1) != '-') {
3250 g_string_append_c(norm_utf7, '+');
3253 g_string_append_c(norm_utf7, '&');
3256 } else if (in_escape && *p == ',') {
3257 g_string_append_c(norm_utf7, '/');
3258 } else if (in_escape && *p == '-') {
3259 g_string_append_c(norm_utf7, '-');
3262 g_string_append_c(norm_utf7, *p);
3266 norm_utf7_p = norm_utf7->str;
3267 norm_utf7_len = norm_utf7->len;
3268 to_len = strlen(mutf7_str) * 5;
3269 to_p = to_str = g_malloc(to_len + 1);
3271 if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
3272 &to_p, &to_len) == -1) {
3273 g_warning(_("iconv cannot convert UTF-7 to %s\n"),
3274 conv_get_current_charset_str());
3275 g_string_free(norm_utf7, TRUE);
3277 return g_strdup(mutf7_str);
3280 /* second iconv() call for flushing */
3281 iconv(cd, NULL, NULL, &to_p, &to_len);
3282 g_string_free(norm_utf7, TRUE);
3286 #endif /* !HAVE_ICONV */
3289 static gchar *imap_locale_to_modified_utf7(const gchar *from)
3292 return g_strdup(from);
3294 static iconv_t cd = (iconv_t)-1;
3295 static gboolean iconv_ok = TRUE;
3296 gchar *norm_utf7, *norm_utf7_p;
3297 size_t from_len, norm_utf7_len;
3299 gchar *from_tmp, *to, *p;
3300 gboolean in_escape = FALSE;
3302 if (!iconv_ok) return g_strdup(from);
3304 if (cd == (iconv_t)-1) {
3305 cd = iconv_open("UTF-7", conv_get_current_charset_str());
3306 if (cd == (iconv_t)-1) {
3307 g_warning("iconv cannot convert %s to UTF-7\n",
3308 conv_get_current_charset_str());
3310 return g_strdup(from);
3314 Xstrdup_a(from_tmp, from, return g_strdup(from));
3315 from_len = strlen(from);
3316 norm_utf7_len = from_len * 5;
3317 Xalloca(norm_utf7, norm_utf7_len + 1, return g_strdup(from));
3318 norm_utf7_p = norm_utf7;
3320 #define IS_PRINT(ch) (isprint(ch) && isascii(ch))
3322 while (from_len > 0) {
3323 if (IS_PRINT(*from_tmp)) {
3324 /* printable ascii char */
3325 *norm_utf7_p = *from_tmp;
3332 /* unprintable char: convert to UTF-7 */
3334 !IS_PRINT(from_tmp[mblen]) && mblen < from_len;
3338 if (iconv(cd, (ICONV_CONST gchar **)&from_tmp, &mblen,
3339 &norm_utf7_p, &norm_utf7_len) == -1) {
3340 g_warning("iconv cannot convert %s to UTF-7\n",
3341 conv_get_current_charset_str());
3342 return g_strdup(from);
3345 /* second iconv() call for flushing */
3346 iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
3352 *norm_utf7_p = '\0';
3353 to_str = g_string_new(NULL);
3354 for (p = norm_utf7; p < norm_utf7_p; p++) {
3355 /* replace: '&' -> "&-",
3357 escaped '/' -> ',' */
3358 if (!in_escape && *p == '&') {
3359 g_string_append(to_str, "&-");
3360 } else if (!in_escape && *p == '+') {
3361 g_string_append_c(to_str, '&');
3363 } else if (in_escape && *p == '/') {
3364 g_string_append_c(to_str, ',');
3365 } else if (in_escape && *p == '-') {
3367 g_string_append_c(to_str, '-');
3369 g_string_append_c(to_str, *p);
3375 g_string_append_c(to_str, '-');
3379 g_string_free(to_str, FALSE);
3382 #endif /* !HAVE_ICONV */
3385 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3387 FolderItem *item = node->data;
3388 gchar **paths = data;
3389 const gchar *oldpath = paths[0];
3390 const gchar *newpath = paths[1];
3392 gchar *new_itempath;
3395 oldpathlen = strlen(oldpath);
3396 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3397 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3401 base = item->path + oldpathlen;
3402 while (*base == G_DIR_SEPARATOR) base++;
3404 new_itempath = g_strdup(newpath);
3406 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3409 item->path = new_itempath;
3414 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list)
3416 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3417 IMAPSession *session;
3418 gint ok, i, lastuid_old, nummsgs = 0, exists, resent, unseen, uid_val;
3420 gchar *cmdbuf = NULL;
3423 g_return_val_if_fail(folder != NULL, -1);
3424 g_return_val_if_fail(item != NULL, -1);
3425 g_return_val_if_fail(item->item.path != NULL, -1);
3426 g_return_val_if_fail(folder->type == F_IMAP, -1);
3427 g_return_val_if_fail(folder->account != NULL, -1);
3429 session = imap_session_get(folder);
3430 g_return_val_if_fail(session != NULL, -1);
3432 ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
3433 &exists, &resent, &unseen, &uid_val);
3434 if (ok != IMAP_SUCCESS)
3440 argbuf = g_ptr_array_new();
3442 cmdbuf = g_strdup_printf("UID FETCH %d:* (UID)", (item->lastuid + 1));
3444 cmdbuf = g_strdup("UID SEARCH ALL");
3446 imap_cmd_gen_send(SESSION(session)->sock, cmdbuf);
3448 ok = imap_cmd_ok(SESSION(session)->sock, argbuf);
3449 if (ok != IMAP_SUCCESS) {
3450 ptr_array_free_strings(argbuf);
3451 g_ptr_array_free(argbuf, TRUE);
3455 lastuid_old = item->lastuid;
3456 *msgnum_list = g_slist_copy(item->uid_list);
3457 debug_print("Got %d uids from cache\n", g_slist_length(item->uid_list));
3458 for(i = 0; i < argbuf->len; i++) {
3459 int ret, msgidx, msgnum;
3461 if (!strncmp(g_ptr_array_index(argbuf, i), "SEARCH ", 7)) {
3462 gchar **p, **list = g_strsplit(g_ptr_array_index(argbuf, i) + 7, " ", 0);
3464 for (p = list; *p != NULL; ++p)
3465 if (sscanf(*p, "%d", &msgnum) == 1 && msgnum > lastuid_old) {
3466 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3467 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3470 if(msgnum > item->lastuid)
3471 item->lastuid = msgnum;
3476 } else if((ret = sscanf(g_ptr_array_index(argbuf, i),
3477 "%d FETCH (UID %d)", &msgidx, &msgnum)) == 2 &&
3478 msgnum > lastuid_old) {
3479 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3480 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3483 if(msgnum > item->lastuid)
3484 item->lastuid = msgnum;
3489 dir = folder_item_get_path((FolderItem *)item);
3490 debug_print("removing old messages from %s\n", dir);
3491 remove_numbered_files_not_in_list(dir, *msgnum_list);
3494 ptr_array_free_strings(argbuf);
3495 g_ptr_array_free(argbuf, TRUE);
3500 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3505 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3506 flags.tmp_flags = 0;
3508 g_return_val_if_fail(item != NULL, NULL);
3509 g_return_val_if_fail(file != NULL, NULL);
3511 if (item->stype == F_QUEUE) {
3512 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3513 } else if (item->stype == F_DRAFT) {
3514 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3517 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3518 if (!msginfo) return NULL;
3520 msginfo->folder = item;
3525 static int compare_uint(gconstpointer a, gconstpointer b)
3530 GSList *imap_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
3532 IMAPSession *session;
3533 GSList *sorted_list, *elem, *ret = NULL;
3534 gint ok, startnum, lastnum;
3536 g_return_val_if_fail(folder != NULL, NULL);
3537 g_return_val_if_fail(item != NULL, NULL);
3538 g_return_val_if_fail(msgnum_list != NULL, NULL);
3540 session = imap_session_get(folder);
3541 g_return_val_if_fail(session != NULL, NULL);
3543 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
3544 NULL, NULL, NULL, NULL);
3545 if (ok != IMAP_SUCCESS)
3548 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), compare_uint);
3550 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
3552 for (elem = sorted_list;; elem = g_slist_next(elem)) {
3556 num = GPOINTER_TO_INT(elem->data);
3558 if (num > lastnum + 1 || elem == NULL) {
3559 if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
3560 ret = g_slist_concat(ret,
3561 imap_get_uncached_messages(
3562 session, item, startnum, lastnum));
3565 for (i = startnum; i <= lastnum; ++i) {
3568 file = imap_fetch_msg(folder, item, i);
3570 MsgInfo *msginfo = imap_parse_msg(file, item);
3571 if (msginfo != NULL) {
3572 msginfo->msgnum = i;
3573 ret = g_slist_append(ret, msginfo);
3588 g_slist_free(sorted_list);
3593 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
3595 IMAPSession *session;
3596 MsgInfo *msginfo = NULL;
3599 g_return_val_if_fail(folder != NULL, NULL);
3600 g_return_val_if_fail(item != NULL, NULL);
3602 session = imap_session_get(folder);
3603 g_return_val_if_fail(session != NULL, NULL);
3605 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
3606 NULL, NULL, NULL, NULL);
3607 if (ok != IMAP_SUCCESS)
3610 if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
3613 list = imap_get_uncached_messages(session, item, uid, uid);
3615 msginfo = (MsgInfo *)list->data;
3618 procmsg_msg_list_free(list);
3622 file = imap_fetch_msg(folder, item, uid);
3624 msginfo = imap_parse_msg(file, item);
3625 if (msginfo != NULL)
3626 msginfo->msgnum = uid;
3634 gboolean imap_check_msgnum_validity(Folder *folder, FolderItem *_item)
3636 IMAPSession *session;
3637 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3638 gint ok, exists = 0, recent = 0, unseen = 0;
3639 guint32 uid_next, uid_validity = 0;
3641 g_return_val_if_fail(folder != NULL, FALSE);
3642 g_return_val_if_fail(item != NULL, FALSE);
3643 g_return_val_if_fail(item->item.folder != NULL, FALSE);
3644 g_return_val_if_fail(item->item.folder->type == F_IMAP, FALSE);
3646 session = imap_session_get(folder);
3647 g_return_val_if_fail(session != NULL, FALSE);
3649 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
3650 &exists, &recent, &uid_next, &uid_validity, &unseen);
3651 if (ok != IMAP_SUCCESS)
3654 if(item->item.mtime == uid_validity)
3657 debug_print("Freeing imap uid cache");
3659 g_slist_free(item->uid_list);
3660 item->uid_list = NULL;
3662 item->item.mtime = uid_validity;
3664 imap_delete_all_cached_messages((FolderItem *)item);