2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
34 #include "alertpanel.h"
57 #include "procheader.h"
58 #include "prefs_account.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
66 #include "remotefolder.h"
68 #include "statusbar.h"
70 #include "imap-thread.h"
73 typedef struct _IMAPFolder IMAPFolder;
74 typedef struct _IMAPSession IMAPSession;
75 typedef struct _IMAPNameSpace IMAPNameSpace;
76 typedef struct _IMAPFolderItem IMAPFolderItem;
78 #include "prefs_account.h"
80 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
81 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
82 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
88 /* list of IMAPNameSpace */
92 gchar last_seen_separator;
100 gboolean authenticated;
109 gboolean folder_content_changed;
120 gboolean sens_update_block;
123 struct _IMAPNameSpace
129 #define IMAP_SUCCESS 0
130 #define IMAP_SOCKET 2
131 #define IMAP_AUTHFAIL 3
132 #define IMAP_PROTOCOL 4
133 #define IMAP_SYNTAX 5
137 #define IMAPBUFSIZE 8192
141 IMAP_FLAG_SEEN = 1 << 0,
142 IMAP_FLAG_ANSWERED = 1 << 1,
143 IMAP_FLAG_FLAGGED = 1 << 2,
144 IMAP_FLAG_DELETED = 1 << 3,
145 IMAP_FLAG_DRAFT = 1 << 4
148 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
149 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
150 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
151 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
152 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
155 #define IMAP4_PORT 143
157 #define IMAPS_PORT 993
160 #define IMAP_CMD_LIMIT 1000
162 struct _IMAPFolderItem
171 GHashTable *flags_set_table;
172 GHashTable *flags_unset_table;
175 gboolean should_update;
176 gboolean should_trash_cache;
179 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
180 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
182 static void imap_folder_init (Folder *folder,
186 static Folder *imap_folder_new (const gchar *name,
188 static void imap_folder_destroy (Folder *folder);
190 static IMAPSession *imap_session_new (Folder *folder,
191 const PrefsAccount *account);
192 static void imap_session_authenticate(IMAPSession *session,
193 const PrefsAccount *account);
194 static void imap_session_destroy (Session *session);
196 static gchar *imap_fetch_msg (Folder *folder,
199 static gchar *imap_fetch_msg_full (Folder *folder,
204 static void imap_remove_cached_msg (Folder *folder,
207 static gint imap_add_msg (Folder *folder,
211 static gint imap_add_msgs (Folder *folder,
214 GRelation *relation);
216 static gint imap_copy_msg (Folder *folder,
219 static gint imap_copy_msgs (Folder *folder,
221 MsgInfoList *msglist,
222 GRelation *relation);
224 static gint imap_remove_msg (Folder *folder,
227 static gint imap_remove_msgs (Folder *folder,
229 MsgInfoList *msglist,
230 GRelation *relation);
231 static gint imap_remove_all_msg (Folder *folder,
234 static gboolean imap_is_msg_changed (Folder *folder,
238 static gint imap_close (Folder *folder,
241 static gint imap_scan_tree (Folder *folder);
243 static gint imap_create_tree (Folder *folder);
245 static FolderItem *imap_create_folder (Folder *folder,
248 static gint imap_rename_folder (Folder *folder,
251 static gint imap_remove_folder (Folder *folder,
254 static FolderItem *imap_folder_item_new (Folder *folder);
255 static void imap_folder_item_destroy (Folder *folder,
258 static IMAPSession *imap_session_get (Folder *folder);
260 static gint imap_auth (IMAPSession *session,
265 static gint imap_scan_tree_recursive (IMAPSession *session,
269 static void imap_create_missing_folders (Folder *folder);
270 static FolderItem *imap_create_special_folder
272 SpecialFolderItemType stype,
275 static gint imap_do_copy_msgs (Folder *folder,
277 MsgInfoList *msglist,
278 GRelation *relation);
280 static void imap_delete_all_cached_messages (FolderItem *item);
281 static void imap_set_batch (Folder *folder,
284 static gint imap_set_message_flags (IMAPSession *session,
285 MsgNumberList *numlist,
288 static gint imap_select (IMAPSession *session,
294 guint32 *uid_validity,
296 static gint imap_status (IMAPSession *session,
299 IMAPFolderItem *item,
302 guint32 *uid_validity,
306 static gchar imap_get_path_separator (IMAPSession *session,
309 static gchar *imap_get_real_path (IMAPSession *session,
312 static void imap_synchronise (FolderItem *item, gint days);
313 static gboolean imap_is_busy (Folder *folder);
315 static void imap_free_capabilities (IMAPSession *session);
317 /* low-level IMAP4rev1 commands */
318 static gint imap_cmd_login (IMAPSession *session,
322 static gint imap_cmd_noop (IMAPSession *session);
324 static gint imap_cmd_starttls (IMAPSession *session);
326 static gint imap_cmd_select (IMAPSession *session,
331 guint32 *uid_validity,
333 static gint imap_cmd_close (IMAPSession *session);
334 static gint imap_cmd_examine (IMAPSession *session,
339 guint32 *uid_validity,
341 static gint imap_cmd_create (IMAPSession *sock,
342 const gchar *folder);
343 static gint imap_cmd_rename (IMAPSession *sock,
344 const gchar *oldfolder,
345 const gchar *newfolder);
346 static gint imap_cmd_delete (IMAPSession *session,
347 const gchar *folder);
348 static gint imap_cmd_fetch (IMAPSession *sock,
350 const gchar *filename,
353 static gint imap_cmd_append (IMAPSession *session,
354 const gchar *destfolder,
358 static gint imap_cmd_copy (IMAPSession *session,
359 struct mailimap_set * set,
360 const gchar *destfolder,
361 GRelation *uid_mapping,
362 struct mailimap_set ** source,
363 struct mailimap_set ** dest);
364 static gint imap_cmd_store (IMAPSession *session,
365 struct mailimap_set * set,
368 static gint imap_cmd_expunge (IMAPSession *session);
370 static void imap_path_separator_subst (gchar *str,
373 static gchar *imap_utf8_to_modified_utf7 (const gchar *from);
374 static gchar *imap_modified_utf7_to_utf8 (const gchar *mutf7_str);
376 static gboolean imap_rename_folder_func (GNode *node,
378 static gint imap_get_num_list (Folder *folder,
381 gboolean *old_uids_valid);
382 static GSList *imap_get_msginfos (Folder *folder,
384 GSList *msgnum_list);
385 static MsgInfo *imap_get_msginfo (Folder *folder,
388 static gboolean imap_scan_required (Folder *folder,
390 static void imap_change_flags (Folder *folder,
393 MsgPermFlags newflags);
394 static gint imap_get_flags (Folder *folder,
396 MsgInfoList *msglist,
397 GRelation *msgflags);
398 static gchar *imap_folder_get_path (Folder *folder);
399 static gchar *imap_item_get_path (Folder *folder,
401 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
404 /* data types conversion libetpan <-> claws */
405 static GSList * imap_list_from_lep(IMAPFolder * folder,
406 clist * list, const gchar * real_path, gboolean all);
407 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist);
408 static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist);
409 static GSList * imap_uid_list_from_lep(clist * list);
410 static GSList * imap_uid_list_from_lep_tab(carray * list);
411 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
413 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
415 static void imap_lep_set_free(GSList *seq_list);
416 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags);
418 typedef struct _hashtable_data {
419 IMAPSession *session;
421 IMAPFolderItem *item;
424 static FolderClass imap_class;
426 typedef struct _thread_data {
436 FolderClass *imap_get_class(void)
438 if (imap_class.idstr == NULL) {
439 imap_class.type = F_IMAP;
440 imap_class.idstr = "imap";
441 imap_class.uistr = "IMAP4";
443 /* Folder functions */
444 imap_class.new_folder = imap_folder_new;
445 imap_class.destroy_folder = imap_folder_destroy;
446 imap_class.scan_tree = imap_scan_tree;
447 imap_class.create_tree = imap_create_tree;
449 /* FolderItem functions */
450 imap_class.item_new = imap_folder_item_new;
451 imap_class.item_destroy = imap_folder_item_destroy;
452 imap_class.item_get_path = imap_item_get_path;
453 imap_class.create_folder = imap_create_folder;
454 imap_class.rename_folder = imap_rename_folder;
455 imap_class.remove_folder = imap_remove_folder;
456 imap_class.close = imap_close;
457 imap_class.get_num_list = imap_get_num_list;
458 imap_class.scan_required = imap_scan_required;
459 imap_class.set_xml = folder_set_xml;
460 imap_class.get_xml = folder_get_xml;
461 imap_class.item_set_xml = imap_item_set_xml;
462 imap_class.item_get_xml = imap_item_get_xml;
464 /* Message functions */
465 imap_class.get_msginfo = imap_get_msginfo;
466 imap_class.get_msginfos = imap_get_msginfos;
467 imap_class.fetch_msg = imap_fetch_msg;
468 imap_class.fetch_msg_full = imap_fetch_msg_full;
469 imap_class.add_msg = imap_add_msg;
470 imap_class.add_msgs = imap_add_msgs;
471 imap_class.copy_msg = imap_copy_msg;
472 imap_class.copy_msgs = imap_copy_msgs;
473 imap_class.remove_msg = imap_remove_msg;
474 imap_class.remove_msgs = imap_remove_msgs;
475 imap_class.remove_all_msg = imap_remove_all_msg;
476 imap_class.is_msg_changed = imap_is_msg_changed;
477 imap_class.change_flags = imap_change_flags;
478 imap_class.get_flags = imap_get_flags;
479 imap_class.set_batch = imap_set_batch;
480 imap_class.synchronise = imap_synchronise;
481 imap_class.remove_cached_msg = imap_remove_cached_msg;
483 pthread_mutex_init(&imap_mutex, NULL);
490 static Folder *imap_folder_new(const gchar *name, const gchar *path)
494 folder = (Folder *)g_new0(IMAPFolder, 1);
495 folder->klass = &imap_class;
496 imap_folder_init(folder, name, path);
501 static void imap_folder_destroy(Folder *folder)
503 while (imap_folder_get_refcnt(folder) > 0)
504 gtk_main_iteration();
506 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
510 static void imap_folder_init(Folder *folder, const gchar *name,
513 folder_remote_folder_init((Folder *)folder, name, path);
516 static FolderItem *imap_folder_item_new(Folder *folder)
518 IMAPFolderItem *item;
520 item = g_new0(IMAPFolderItem, 1);
523 item->uid_list = NULL;
525 return (FolderItem *)item;
528 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
530 IMAPFolderItem *item = (IMAPFolderItem *)_item;
532 g_return_if_fail(item != NULL);
533 g_slist_free(item->uid_list);
538 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
540 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
543 g_slist_free(item->uid_list);
544 item->uid_list = NULL;
549 static void imap_reset_uid_lists(Folder *folder)
551 if(folder->node == NULL)
554 /* Destroy all uid lists and rest last uid */
555 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
558 static int imap_get_capabilities(IMAPSession *session)
560 struct mailimap_capability_data *capabilities = NULL;
564 if (session->capability != NULL)
565 return MAILIMAP_NO_ERROR;
567 capabilities = imap_threaded_capability(session->folder, &result);
569 if (result != MAILIMAP_NO_ERROR) {
570 return MAILIMAP_ERROR_CAPABILITY;
573 if (capabilities == NULL) {
574 return MAILIMAP_NO_ERROR;
577 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
578 cur = clist_next(cur)) {
579 struct mailimap_capability * cap =
581 if (!cap || cap->cap_data.cap_name == NULL)
583 session->capability = g_slist_append
584 (session->capability,
585 g_strdup(cap->cap_data.cap_name));
586 debug_print("got capa %s\n", cap->cap_data.cap_name);
588 mailimap_capability_data_free(capabilities);
589 return MAILIMAP_NO_ERROR;
592 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
595 for (cur = session->capability; cur; cur = cur->next) {
596 if (!g_ascii_strcasecmp(cur->data, cap))
602 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
605 gint ok = IMAP_ERROR;
606 static time_t last_login_err = 0;
607 gchar *ext_info = "";
609 if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR)
614 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
616 case IMAP_AUTH_CRAM_MD5:
617 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
619 case IMAP_AUTH_LOGIN:
620 ok = imap_cmd_login(session, user, pass, "LOGIN");
622 case IMAP_AUTH_GSSAPI:
623 ok = imap_cmd_login(session, user, pass, "GSSAPI");
626 debug_print("capabilities:\n"
631 imap_has_capability(session, "ANONYMOUS"),
632 imap_has_capability(session, "CRAM-MD5"),
633 imap_has_capability(session, "LOGIN"),
634 imap_has_capability(session, "GSSAPI"));
635 if (imap_has_capability(session, "CRAM-MD5"))
636 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
637 if (ok == IMAP_ERROR && imap_has_capability(session, "GSSAPI"))
638 ok = imap_cmd_login(session, user, pass, "GSSAPI");
639 if (ok == IMAP_ERROR) /* we always try LOGIN before giving up */
640 ok = imap_cmd_login(session, user, pass, "LOGIN");
643 if (ok == IMAP_SUCCESS)
644 session->authenticated = TRUE;
646 if (type == IMAP_AUTH_CRAM_MD5) {
647 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
648 "compiled with SASL support and the "
649 "CRAM-MD5 SASL plugin is installed.");
652 if (time(NULL) - last_login_err > 10) {
653 if (!prefs_common.no_recv_err_panel) {
654 alertpanel_error(_("Connection to %s failed: "
656 SESSION(session)->server, ext_info);
658 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
659 "login refused.%s\n"),
660 SESSION(session)->server, ext_info);
663 last_login_err = time(NULL);
668 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
670 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
671 /* Check if this is the first try to establish a
672 connection, if yes we don't try to reconnect */
673 debug_print("reconnecting\n");
674 if (rfolder->session == NULL) {
675 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
676 folder->account->recv_server);
677 session_destroy(SESSION(session));
680 rfolder->session = NULL;
681 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
682 " disconnected. Reconnecting...\n"),
683 folder->account->recv_server);
684 statusbar_print_all(_("IMAP4 connection to %s has been"
685 " disconnected. Reconnecting...\n"),
686 folder->account->recv_server);
687 SESSION(session)->state = SESSION_DISCONNECTED;
688 session_destroy(SESSION(session));
689 /* Clear folders session to make imap_session_get create
690 a new session, because of rfolder->session == NULL
691 it will not try to reconnect again and so avoid an
693 debug_print("getting session...\n");
694 session = imap_session_get(folder);
695 rfolder->session = SESSION(session);
701 static void imap_refresh_sensitivity (IMAPSession *session)
705 if (session->sens_update_block)
707 mainwin = mainwindow_get_mainwindow();
709 toolbar_main_set_sensitive(mainwin);
710 main_window_set_menu_sensitive(mainwin);
714 static void lock_session(IMAPSession *session)
717 debug_print("locking session %p (%d)\n", session, session->busy);
719 debug_print(" SESSION WAS LOCKED !! \n");
720 session->busy = TRUE;
721 imap_refresh_sensitivity(session);
723 debug_print("can't lock null session\n");
727 static void unlock_session(IMAPSession *session)
730 debug_print("unlocking session %p\n", session);
731 session->busy = FALSE;
732 imap_refresh_sensitivity(session);
734 debug_print("can't unlock null session\n");
738 static void imap_disc_session_destroy(Folder *folder)
740 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
741 IMAPSession *session = NULL;
745 session = IMAP_SESSION(rfolder->session);
748 rfolder->session = NULL;
749 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
750 SESSION(session)->state = SESSION_DISCONNECTED;
751 session_destroy(SESSION(session));
754 static IMAPSession *imap_session_get(Folder *folder)
756 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
757 IMAPSession *session = NULL;
759 g_return_val_if_fail(folder != NULL, NULL);
760 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
761 g_return_val_if_fail(folder->account != NULL, NULL);
763 if (prefs_common.work_offline &&
764 !inc_offline_should_override(FALSE,
765 _("Claws Mail needs network access in order "
766 "to access the IMAP server."))) {
770 /* Make sure we have a session */
771 if (rfolder->session != NULL) {
772 session = IMAP_SESSION(rfolder->session);
773 } else if (rfolder->connecting) {
774 debug_print("already connecting\n");
777 imap_reset_uid_lists(folder);
778 if (time(NULL) - rfolder->last_failure <= 2)
780 rfolder->connecting = TRUE;
781 session = imap_session_new(folder, folder->account);
783 if(session == NULL) {
784 rfolder->last_failure = time(NULL);
785 rfolder->connecting = FALSE;
789 /* Make sure session is authenticated */
790 if (!IMAP_SESSION(session)->authenticated)
791 imap_session_authenticate(IMAP_SESSION(session), folder->account);
793 if (!IMAP_SESSION(session)->authenticated) {
794 imap_threaded_disconnect(session->folder);
795 rfolder->session = NULL;
796 SESSION(session)->state = SESSION_DISCONNECTED;
797 session_destroy(SESSION(session));
798 rfolder->last_failure = time(NULL);
799 rfolder->connecting = FALSE;
803 lock_session(session);
805 /* I think the point of this code is to avoid sending a
806 * keepalive if we've used the session recently and therefore
807 * think it's still alive. Unfortunately, most of the code
808 * does not yet check for errors on the socket, and so if the
809 * connection drops we don't notice until the timeout expires.
810 * A better solution than sending a NOOP every time would be
811 * for every command to be prepared to retry until it is
812 * successfully sent. -- mbp */
813 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
814 /* verify that the session is still alive */
815 if (imap_cmd_noop(session) != IMAP_SUCCESS) {
816 debug_print("disconnected!\n");
817 session = imap_reconnect_if_possible(folder, session);
820 session->cancelled = FALSE;
823 rfolder->session = SESSION(session);
824 rfolder->connecting = FALSE;
826 return IMAP_SESSION(session);
829 static IMAPSession *imap_session_new(Folder * folder,
830 const PrefsAccount *account)
832 IMAPSession *session;
835 int authenticated = FALSE;
838 /* FIXME: IMAP over SSL only... */
841 port = account->set_imapport ? account->imapport
842 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
843 ssl_type = account->ssl_imap;
845 if (account->ssl_imap != SSL_NONE) {
846 if (alertpanel_full(_("Insecure connection"),
847 _("This connection is configured to be secured "
848 "using SSL, but SSL is not available in this "
849 "build of Claws Mail. \n\n"
850 "Do you want to continue connecting to this "
851 "server? The communication would not be "
853 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
854 NULL, FALSE, NULL, ALERT_WARNING,
855 G_ALERTDEFAULT) != G_ALERTALTERNATE)
858 port = account->set_imapport ? account->imapport
863 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
864 if (account->set_tunnelcmd) {
865 r = imap_threaded_connect_cmd(folder,
867 account->recv_server,
872 if (ssl_type == SSL_TUNNEL) {
873 r = imap_threaded_connect_ssl(folder,
874 account->recv_server,
880 r = imap_threaded_connect(folder,
881 account->recv_server,
886 statuswindow_pop_all();
887 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
888 authenticated = TRUE;
890 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
891 authenticated = FALSE;
894 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
896 if (r == MAILIMAP_ERROR_SSL)
897 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
900 if(!prefs_common.no_recv_err_panel) {
901 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
902 account->recv_server, port);
904 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
905 account->recv_server, port);
911 session = g_new0(IMAPSession, 1);
912 session_init(SESSION(session));
913 SESSION(session)->type = SESSION_IMAP;
914 SESSION(session)->server = g_strdup(account->recv_server);
915 SESSION(session)->sock = NULL;
917 SESSION(session)->destroy = imap_session_destroy;
919 session->capability = NULL;
921 session->authenticated = authenticated;
922 session->mbox = NULL;
925 session->expunge = 0;
926 session->cmd_count = 0;
927 session->folder = folder;
928 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
931 if (account->ssl_imap == SSL_STARTTLS) {
934 ok = imap_cmd_starttls(session);
935 if (ok != IMAP_SUCCESS) {
936 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
937 session_destroy(SESSION(session));
941 imap_free_capabilities(session);
942 session->authenticated = FALSE;
943 session->uidplus = FALSE;
944 session->cmd_count = 1;
947 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
948 (session->authenticated) ? "pre" : "un");
953 static void imap_session_authenticate(IMAPSession *session,
954 const PrefsAccount *account)
956 gchar *pass, *acc_pass;
957 gboolean failed = FALSE;
959 g_return_if_fail(account->userid != NULL);
960 acc_pass = account->passwd;
963 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
965 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
968 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
970 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
973 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
974 account->recv_server);
975 if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
983 if (prefs_common.no_recv_err_panel) {
984 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
985 mainwindow_show_error();
987 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
993 statuswindow_pop_all();
994 session->authenticated = TRUE;
998 static void imap_session_destroy(Session *session)
1000 if (session->state != SESSION_DISCONNECTED)
1001 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1003 imap_free_capabilities(IMAP_SESSION(session));
1004 g_free(IMAP_SESSION(session)->mbox);
1005 sock_close(session->sock);
1006 session->sock = NULL;
1009 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1011 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1014 static guint get_file_size_with_crs(const gchar *filename)
1020 if (filename == NULL)
1023 fp = fopen(filename, "rb");
1027 while (fgets(buf, sizeof (buf), fp) != NULL) {
1029 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1037 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1039 gchar *path, *filename;
1041 path = folder_item_get_path(item);
1043 if (!is_dir_exist(path)) {
1048 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1051 if (is_file_exist(filename)) {
1057 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1058 gboolean headers, gboolean body)
1060 gchar *path, *filename;
1061 IMAPSession *session;
1064 g_return_val_if_fail(folder != NULL, NULL);
1065 g_return_val_if_fail(item != NULL, NULL);
1070 path = folder_item_get_path(item);
1071 if (!is_dir_exist(path))
1072 make_dir_hier(path);
1073 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1075 debug_print("trying to fetch cached %s\n", filename);
1076 if (is_file_exist(filename)) {
1077 /* see whether the local file represents the whole message
1078 * or not. As the IMAP server reports size with \r chars,
1079 * we have to update the local file (UNIX \n only) size */
1080 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1081 guint have_size = -1;
1084 debug_print("message %d has been already %scached.\n", uid,
1085 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1087 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1088 have_size = get_file_size_with_crs(filename);
1089 if (cached && (cached->size <= have_size || !body)) {
1090 procmsg_msginfo_free(cached);
1091 ok = file_strip_crs(filename);
1092 if (ok == 0 && cached && cached->size <= have_size) {
1093 /* we have it all and stripped */
1094 debug_print("...fully cached in fact; setting flag.\n");
1095 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1098 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1099 debug_print("message not cached and file recent, considering file complete\n");
1100 ok = file_strip_crs(filename);
1104 procmsg_msginfo_free(cached);
1107 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1108 procmsg_msginfo_free(cached);
1112 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1114 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1115 procmsg_msginfo_free(cached);
1119 debug_print("getting session...\n");
1120 session = imap_session_get(folder);
1127 debug_print("IMAP fetching messages\n");
1128 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1129 NULL, NULL, NULL, NULL, FALSE);
1130 if (ok != IMAP_SUCCESS) {
1131 g_warning("can't select mailbox %s\n", item->path);
1133 unlock_session(session);
1134 if (ok == MAILIMAP_ERROR_STREAM) {
1135 imap_disc_session_destroy(folder);
1141 debug_print("getting message %d...\n", uid);
1142 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1144 if (ok != IMAP_SUCCESS) {
1145 g_warning("can't fetch message %d\n", uid);
1147 unlock_session(session);
1148 if (ok == MAILIMAP_ERROR_STREAM) {
1149 imap_disc_session_destroy(folder);
1155 unlock_session(session);
1156 ok = file_strip_crs(filename);
1158 if (ok == 0 && headers && body) {
1159 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1161 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1162 procmsg_msginfo_free(cached);
1164 } else if (ok == -1) {
1165 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1167 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1168 procmsg_msginfo_free(cached);
1174 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1176 gchar *path, *filename;
1178 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1183 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1184 procmsg_msginfo_free(cached);
1187 path = folder_item_get_path(item);
1188 if (!is_dir_exist(path))
1191 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1193 if (is_file_exist(filename)) {
1194 if (cached && cached->total_size == cached->size) {
1197 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1200 size = get_file_size_with_crs(filename);
1203 if (cached && size >= cached->size) {
1204 cached->total_size = cached->size;
1205 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1206 procmsg_msginfo_free(cached);
1210 procmsg_msginfo_free(cached);
1214 void imap_cache_msg(FolderItem *item, gint msgnum)
1216 Folder *folder = NULL;
1220 folder = item->folder;
1222 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1223 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1224 debug_print("fetched %s\n", tmp);
1229 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1230 const gchar *file, MsgFlags *flags)
1234 MsgFileInfo fileinfo;
1236 g_return_val_if_fail(file != NULL, -1);
1238 fileinfo.msginfo = NULL;
1239 fileinfo.file = (gchar *)file;
1240 fileinfo.flags = flags;
1241 file_list.data = &fileinfo;
1242 file_list.next = NULL;
1244 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1248 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1249 GRelation *relation)
1252 IMAPSession *session;
1253 guint32 last_uid = 0;
1255 MsgFileInfo *fileinfo;
1257 gint curnum = 0, total = 0;
1258 gboolean missing_uids = FALSE;
1260 g_return_val_if_fail(folder != NULL, -1);
1261 g_return_val_if_fail(dest != NULL, -1);
1262 g_return_val_if_fail(file_list != NULL, -1);
1264 debug_print("getting session...\n");
1265 session = imap_session_get(folder);
1269 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1271 statusbar_print_all(_("Adding messages..."));
1272 total = g_slist_length(file_list);
1273 for (cur = file_list; cur != NULL; cur = cur->next) {
1274 IMAPFlags iflags = 0;
1275 guint32 new_uid = 0;
1276 gchar *real_file = NULL;
1277 fileinfo = (MsgFileInfo *)cur->data;
1279 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1282 if (fileinfo->flags) {
1283 if (MSG_IS_MARKED(*fileinfo->flags))
1284 iflags |= IMAP_FLAG_FLAGGED;
1285 if (MSG_IS_REPLIED(*fileinfo->flags))
1286 iflags |= IMAP_FLAG_ANSWERED;
1287 if (!MSG_IS_UNREAD(*fileinfo->flags))
1288 iflags |= IMAP_FLAG_SEEN;
1291 if (real_file == NULL)
1292 real_file = g_strdup(fileinfo->file);
1294 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1295 folder_has_parent_of_type(dest, F_OUTBOX) ||
1296 folder_has_parent_of_type(dest, F_DRAFT) ||
1297 folder_has_parent_of_type(dest, F_TRASH))
1298 iflags |= IMAP_FLAG_SEEN;
1300 ok = imap_cmd_append(session, destdir, real_file, iflags,
1303 if (ok != IMAP_SUCCESS) {
1304 g_warning("can't append message %s\n", real_file);
1307 unlock_session(session);
1308 statusbar_progress_all(0,0,0);
1309 statusbar_pop_all();
1310 if (ok == MAILIMAP_ERROR_STREAM) {
1311 imap_disc_session_destroy(folder);
1316 debug_print("appended new message as %d\n", new_uid);
1317 /* put the local file in the imapcache, so that we don't
1318 * have to fetch it back later. */
1321 missing_uids = TRUE;
1322 debug_print("Missing UID (0)\n");
1325 gchar *cache_path = folder_item_get_path(dest);
1326 if (!is_dir_exist(cache_path))
1327 make_dir_hier(cache_path);
1328 if (is_dir_exist(cache_path)) {
1329 gchar *cache_file = g_strconcat(
1330 cache_path, G_DIR_SEPARATOR_S,
1331 itos(new_uid), NULL);
1332 copy_file(real_file, cache_file, TRUE);
1333 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1340 if (relation != NULL)
1341 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1342 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1343 GINT_TO_POINTER(new_uid));
1344 if (last_uid < new_uid) {
1351 statusbar_progress_all(0,0,0);
1352 statusbar_pop_all();
1354 unlock_session(session);
1358 imap_scan_required(folder, dest);
1360 session = imap_session_get(folder);
1364 unlock_session(session);
1368 ok = imap_select(session, IMAP_FOLDER(folder), dest->path,
1369 &a, NULL, NULL, NULL, FALSE);
1370 if (ok == MAILIMAP_ERROR_STREAM) {
1371 imap_disc_session_destroy(folder);
1378 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1380 GSList *result = NULL;
1385 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1386 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1387 start = item->set_first;
1388 end = item->set_last;
1389 for (t = start; t <= end; t++) {
1390 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1393 result = g_slist_reverse(result);
1394 if (debug_get_mode()) {
1395 debug_print("flat imap set: ");
1396 for (cur = result; cur; cur = cur->next) {
1397 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1404 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1405 MsgInfoList *msglist, GRelation *relation)
1409 GSList *seq_list, *cur;
1411 IMAPSession *session;
1412 gint ok = IMAP_SUCCESS;
1413 GRelation *uid_mapping;
1415 gboolean single = FALSE;
1417 g_return_val_if_fail(folder != NULL, -1);
1418 g_return_val_if_fail(dest != NULL, -1);
1419 g_return_val_if_fail(msglist != NULL, -1);
1421 debug_print("getting session...\n");
1422 session = imap_session_get(folder);
1428 msginfo = (MsgInfo *)msglist->data;
1429 if (msglist->next == NULL)
1431 src = msginfo->folder;
1433 g_warning("the src folder is identical to the dest.\n");
1434 unlock_session(session);
1438 if (src->folder != dest->folder) {
1439 GSList *infolist = NULL, *cur;
1441 for (cur = msglist; cur; cur = cur->next) {
1442 msginfo = (MsgInfo *)cur->data;
1443 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1444 fileinfo->file = procmsg_get_message_file(msginfo);
1445 fileinfo->flags = &(msginfo->flags);
1446 infolist = g_slist_prepend(infolist, fileinfo);
1448 infolist = g_slist_reverse(infolist);
1449 unlock_session(session);
1450 res = folder_item_add_msgs(dest, infolist, FALSE);
1451 for (cur = infolist; cur; cur = cur->next) {
1452 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1456 g_slist_free(infolist);
1460 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1461 NULL, NULL, NULL, NULL, FALSE);
1462 if (ok != IMAP_SUCCESS) {
1463 unlock_session(session);
1464 if (ok == MAILIMAP_ERROR_STREAM) {
1465 imap_disc_session_destroy(folder);
1471 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1472 seq_list = imap_get_lep_set_from_msglist(msglist);
1473 uid_mapping = g_relation_new(2);
1474 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1476 statusbar_print_all(_("Copying messages..."));
1477 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1478 struct mailimap_set * seq_set;
1479 struct mailimap_set * source = NULL;
1480 struct mailimap_set * dest = NULL;
1481 seq_set = cur->data;
1483 debug_print("Copying messages from %s to %s ...\n",
1484 src->path, destdir);
1486 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1489 if (ok == MAILIMAP_ERROR_STREAM) {
1490 imap_disc_session_destroy(folder);
1493 if (ok == IMAP_SUCCESS) {
1494 if (relation && source && dest) {
1495 GSList *s_list = flatten_mailimap_set(source);
1496 GSList *d_list = flatten_mailimap_set(dest);
1497 GSList *s_cur, *d_cur;
1498 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1500 for (s_cur = s_list, d_cur = d_list;
1502 s_cur = s_cur->next, d_cur = d_cur->next) {
1503 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1507 debug_print("hhhmm, source list length != dest list length.\n");
1509 g_slist_free(s_list);
1510 g_slist_free(d_list);
1516 mailimap_set_free(source);
1518 mailimap_set_free(dest);
1520 if (ok != IMAP_SUCCESS) {
1521 g_relation_destroy(uid_mapping);
1522 imap_lep_set_free(seq_list);
1523 unlock_session(session);
1524 statusbar_pop_all();
1529 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1530 MsgInfo *msginfo = (MsgInfo *)cur->data;
1533 tuples = g_relation_select(uid_mapping,
1534 GINT_TO_POINTER(msginfo->msgnum),
1536 if (tuples->len > 0) {
1537 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1538 g_relation_insert(relation, msginfo,
1539 GINT_TO_POINTER(num));
1542 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1543 /* put the local file in the imapcache, so that we don't
1544 * have to fetch it back later. */
1546 gchar *cache_path = folder_item_get_path(msginfo->folder);
1547 gchar *real_file = g_strconcat(
1548 cache_path, G_DIR_SEPARATOR_S,
1549 itos(msginfo->msgnum), NULL);
1550 gchar *cache_file = NULL;
1552 cache_path = folder_item_get_path(dest);
1553 cache_file = g_strconcat(
1554 cache_path, G_DIR_SEPARATOR_S,
1556 if (!is_dir_exist(cache_path))
1557 make_dir_hier(cache_path);
1558 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1559 copy_file(real_file, cache_file, TRUE);
1560 debug_print("copied to cache: %s\n", cache_file);
1567 g_relation_insert(relation, msginfo,
1568 GINT_TO_POINTER(0));
1569 g_tuples_destroy(tuples);
1571 statusbar_pop_all();
1573 g_relation_destroy(uid_mapping);
1574 imap_lep_set_free(seq_list);
1578 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1579 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1580 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1581 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1583 unlock_session(session);
1584 imap_scan_required(folder, dest);
1585 if (ok == IMAP_SUCCESS)
1591 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1595 g_return_val_if_fail(msginfo != NULL, -1);
1597 msglist.data = msginfo;
1598 msglist.next = NULL;
1600 return imap_copy_msgs(folder, dest, &msglist, NULL);
1603 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1604 MsgInfoList *msglist, GRelation *relation)
1609 g_return_val_if_fail(folder != NULL, -1);
1610 g_return_val_if_fail(dest != NULL, -1);
1611 g_return_val_if_fail(msglist != NULL, -1);
1613 msginfo = (MsgInfo *)msglist->data;
1614 g_return_val_if_fail(msginfo->folder != NULL, -1);
1616 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1621 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1622 MsgInfoList *msglist, GRelation *relation)
1624 gchar *destdir, *dir;
1625 GSList *numlist = NULL, *cur;
1627 IMAPSession *session;
1628 gint ok = IMAP_SUCCESS;
1629 GRelation *uid_mapping;
1631 g_return_val_if_fail(folder != NULL, -1);
1632 g_return_val_if_fail(dest != NULL, -1);
1633 g_return_val_if_fail(msglist != NULL, -1);
1635 debug_print("getting session...\n");
1636 session = imap_session_get(folder);
1641 msginfo = (MsgInfo *)msglist->data;
1643 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1644 NULL, NULL, NULL, NULL, FALSE);
1645 if (ok != IMAP_SUCCESS) {
1646 unlock_session(session);
1647 if (ok == MAILIMAP_ERROR_STREAM) {
1648 imap_disc_session_destroy(folder);
1654 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1655 for (cur = msglist; cur; cur = cur->next) {
1656 msginfo = (MsgInfo *)cur->data;
1657 if (!MSG_IS_DELETED(msginfo->flags))
1658 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1660 numlist = g_slist_reverse(numlist);
1662 uid_mapping = g_relation_new(2);
1663 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1665 ok = imap_set_message_flags
1666 (session, numlist, IMAP_FLAG_DELETED, TRUE);
1667 if (ok != IMAP_SUCCESS) {
1668 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1669 unlock_session(session);
1670 if (ok == MAILIMAP_ERROR_STREAM) {
1671 imap_disc_session_destroy(folder);
1676 ok = imap_cmd_expunge(session);
1677 if (ok != IMAP_SUCCESS) {
1678 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1679 unlock_session(session);
1680 if (ok == MAILIMAP_ERROR_STREAM) {
1681 imap_disc_session_destroy(folder);
1687 dir = folder_item_get_path(msginfo->folder);
1688 if (is_dir_exist(dir)) {
1689 for (cur = msglist; cur; cur = cur->next) {
1690 msginfo = (MsgInfo *)cur->data;
1691 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1696 g_relation_destroy(uid_mapping);
1697 g_slist_free(numlist);
1700 unlock_session(session);
1701 if (ok == IMAP_SUCCESS)
1707 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
1708 MsgInfoList *msglist, GRelation *relation)
1712 g_return_val_if_fail(folder != NULL, -1);
1713 g_return_val_if_fail(dest != NULL, -1);
1714 if (msglist == NULL)
1717 msginfo = (MsgInfo *)msglist->data;
1718 g_return_val_if_fail(msginfo->folder != NULL, -1);
1720 return imap_do_remove_msgs(folder, dest, msglist, relation);
1723 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1725 GSList *list = folder_item_get_msg_list(item);
1726 gint res = imap_remove_msgs(folder, item, list, NULL);
1727 procmsg_msg_list_free(list);
1731 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
1734 /* TODO: properly implement this method */
1738 static gint imap_close(Folder *folder, FolderItem *item)
1743 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
1745 FolderItem *item = NULL;
1746 IMAPSession *session;
1747 gchar *root_folder = NULL;
1749 g_return_val_if_fail(folder != NULL, -1);
1750 g_return_val_if_fail(folder->account != NULL, -1);
1752 debug_print("getting session...\n");
1753 session = imap_session_get(folder);
1755 if (!folder->node) {
1756 folder_tree_destroy(folder);
1757 item = folder_item_new(folder, folder->name, NULL);
1758 item->folder = folder;
1759 folder->node = item->node = g_node_new(item);
1764 if (folder->account->imap_dir && *folder->account->imap_dir) {
1769 Xstrdup_a(root_folder, folder->account->imap_dir, {unlock_session(session);return -1;});
1770 extract_quote(root_folder, '"');
1771 subst_char(root_folder,
1772 imap_get_path_separator(session, IMAP_FOLDER(folder),
1775 strtailchomp(root_folder, '/');
1776 real_path = imap_get_real_path
1777 (session, IMAP_FOLDER(folder), root_folder);
1778 debug_print("IMAP root directory: %s\n", real_path);
1780 /* check if root directory exist */
1782 r = imap_threaded_list(session->folder, "", real_path,
1784 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
1785 if (!folder->node) {
1786 item = folder_item_new(folder, folder->name, NULL);
1787 item->folder = folder;
1788 folder->node = item->node = g_node_new(item);
1790 unlock_session(session);
1793 mailimap_list_result_free(lep_list);
1799 item = FOLDER_ITEM(folder->node->data);
1801 if (item && !item->path && root_folder) {
1802 item->path = g_strdup(root_folder);
1805 if (!item || ((item->path || root_folder) &&
1806 strcmp2(item->path, root_folder) != 0)) {
1807 folder_tree_destroy(folder);
1808 item = folder_item_new(folder, folder->name, root_folder);
1809 item->folder = folder;
1810 folder->node = item->node = g_node_new(item);
1813 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
1814 imap_create_missing_folders(folder);
1815 unlock_session(session);
1820 static gint imap_scan_tree(Folder *folder)
1822 gboolean subs_only = FALSE;
1823 if (folder->account) {
1824 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
1825 subs_only = folder->account->imap_subsonly;
1827 return imap_scan_tree_real(folder, subs_only);
1830 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
1833 IMAPFolder *imapfolder;
1834 FolderItem *new_item;
1835 GSList *item_list, *cur;
1838 gchar *wildcard_path;
1844 g_return_val_if_fail(item != NULL, -1);
1845 g_return_val_if_fail(item->folder != NULL, -1);
1846 g_return_val_if_fail(item->no_sub == FALSE, -1);
1848 folder = item->folder;
1849 imapfolder = IMAP_FOLDER(folder);
1851 separator = imap_get_path_separator(session, imapfolder, item->path);
1853 if (folder->ui_func)
1854 folder->ui_func(folder, item, folder->ui_func_data);
1857 wildcard[0] = separator;
1860 real_path = imap_get_real_path(session, imapfolder, item->path);
1864 real_path = g_strdup("");
1867 Xstrcat_a(wildcard_path, real_path, wildcard,
1868 {g_free(real_path); return IMAP_ERROR;});
1872 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
1874 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
1876 if (r != MAILIMAP_NO_ERROR) {
1880 item_list = imap_list_from_lep(imapfolder,
1881 lep_list, real_path, FALSE);
1882 mailimap_list_result_free(lep_list);
1887 node = item->node->children;
1888 while (node != NULL) {
1889 FolderItem *old_item = FOLDER_ITEM(node->data);
1890 GNode *next = node->next;
1893 for (cur = item_list; cur != NULL; cur = cur->next) {
1894 FolderItem *cur_item = FOLDER_ITEM(cur->data);
1895 if (!strcmp2(old_item->path, cur_item->path)) {
1896 new_item = cur_item;
1901 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
1902 debug_print("not removing INBOX\n");
1904 debug_print("folder '%s' not found. removing...\n",
1906 folder_item_remove(old_item);
1909 old_item->no_sub = new_item->no_sub;
1910 old_item->no_select = new_item->no_select;
1911 if (old_item->no_sub == TRUE && node->children) {
1912 debug_print("folder '%s' doesn't have "
1913 "subfolders. removing...\n",
1915 folder_item_remove_children(old_item);
1922 for (cur = item_list; cur != NULL; cur = cur->next) {
1923 FolderItem *cur_item = FOLDER_ITEM(cur->data);
1926 for (node = item->node->children; node != NULL;
1927 node = node->next) {
1928 if (!strcmp2(FOLDER_ITEM(node->data)->path,
1930 new_item = FOLDER_ITEM(node->data);
1931 folder_item_destroy(cur_item);
1937 new_item = cur_item;
1938 debug_print("new folder '%s' found.\n", new_item->path);
1939 folder_item_append(item, new_item);
1942 if (!strcmp(new_item->path, "INBOX")) {
1943 new_item->stype = F_INBOX;
1944 folder->inbox = new_item;
1945 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
1948 base = g_path_get_basename(new_item->path);
1950 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
1951 new_item->stype = F_OUTBOX;
1952 folder->outbox = new_item;
1953 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
1954 new_item->stype = F_DRAFT;
1955 folder->draft = new_item;
1956 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
1957 new_item->stype = F_QUEUE;
1958 folder->queue = new_item;
1959 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
1960 new_item->stype = F_TRASH;
1961 folder->trash = new_item;
1966 if (new_item->no_sub == FALSE)
1967 imap_scan_tree_recursive(session, new_item, subs_only);
1970 g_slist_free(item_list);
1972 return IMAP_SUCCESS;
1975 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
1977 IMAPSession *session = imap_session_get(folder);
1979 gchar *wildcard_path;
1983 GSList *item_list = NULL, *cur;
1984 GList *child_list = NULL, *tmplist = NULL;
1985 GSList *sub_list = NULL;
1991 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
1994 wildcard[0] = separator;
1997 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2001 real_path = g_strdup("");
2004 Xstrcat_a(wildcard_path, real_path, wildcard,
2005 {g_free(real_path); return NULL;});
2009 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2010 item->path?item->path:item->name);
2012 statusbar_print_all(_("Looking for subfolders of %s..."),
2013 item->path?item->path:item->name);
2015 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2017 statusbar_pop_all();
2020 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2021 lep_list, real_path, FALSE);
2022 mailimap_list_result_free(lep_list);
2024 for (cur = item_list; cur != NULL; cur = cur->next) {
2025 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2027 tmplist = imap_scan_subtree(folder, cur_item,
2028 unsubs_only, recursive);
2030 child_list = g_list_concat(child_list, tmplist);
2032 child_list = g_list_prepend(child_list,
2033 imap_get_real_path(session,
2034 IMAP_FOLDER(folder), cur_item->path));
2036 folder_item_destroy(cur_item);
2038 child_list = g_list_reverse(child_list);
2039 g_slist_free(item_list);
2042 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2044 statusbar_pop_all();
2047 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2048 lep_list, real_path, FALSE);
2049 mailimap_list_result_free(lep_list);
2051 for (cur = sub_list; cur != NULL; cur = cur->next) {
2052 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2053 GList *oldlitem = NULL;
2054 gchar *tmp = imap_get_real_path(session,
2055 IMAP_FOLDER(folder), cur_item->path);
2056 folder_item_destroy(cur_item);
2057 oldlitem = g_list_find_custom(
2058 child_list, tmp, (GCompareFunc)strcmp2);
2060 child_list = g_list_remove_link(child_list, oldlitem);
2061 g_free(oldlitem->data);
2062 g_list_free(oldlitem);
2068 statusbar_pop_all();
2073 static gint imap_create_tree(Folder *folder)
2075 g_return_val_if_fail(folder != NULL, -1);
2076 g_return_val_if_fail(folder->node != NULL, -1);
2077 g_return_val_if_fail(folder->node->data != NULL, -1);
2078 g_return_val_if_fail(folder->account != NULL, -1);
2080 imap_scan_tree(folder);
2081 imap_create_missing_folders(folder);
2086 static void imap_create_missing_folders(Folder *folder)
2088 g_return_if_fail(folder != NULL);
2091 folder->inbox = imap_create_special_folder
2092 (folder, F_INBOX, "INBOX");
2094 folder->trash = imap_create_special_folder
2095 (folder, F_TRASH, "Trash");
2097 folder->queue = imap_create_special_folder
2098 (folder, F_QUEUE, "Queue");
2099 if (!folder->outbox)
2100 folder->outbox = imap_create_special_folder
2101 (folder, F_OUTBOX, "Sent");
2103 folder->draft = imap_create_special_folder
2104 (folder, F_DRAFT, "Drafts");
2107 static FolderItem *imap_create_special_folder(Folder *folder,
2108 SpecialFolderItemType stype,
2112 FolderItem *new_item;
2114 g_return_val_if_fail(folder != NULL, NULL);
2115 g_return_val_if_fail(folder->node != NULL, NULL);
2116 g_return_val_if_fail(folder->node->data != NULL, NULL);
2117 g_return_val_if_fail(folder->account != NULL, NULL);
2118 g_return_val_if_fail(name != NULL, NULL);
2120 item = FOLDER_ITEM(folder->node->data);
2121 new_item = imap_create_folder(folder, item, name);
2124 g_warning("Can't create '%s'\n", name);
2125 if (!folder->inbox) return NULL;
2127 new_item = imap_create_folder(folder, folder->inbox, name);
2129 g_warning("Can't create '%s' under INBOX\n", name);
2131 new_item->stype = stype;
2133 new_item->stype = stype;
2138 static gchar *imap_folder_get_path(Folder *folder)
2142 g_return_val_if_fail(folder != NULL, NULL);
2143 g_return_val_if_fail(folder->account != NULL, NULL);
2145 folder_path = g_strconcat(get_imap_cache_dir(),
2147 folder->account->recv_server,
2149 folder->account->userid,
2155 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2157 gchar *folder_path, *path;
2159 g_return_val_if_fail(folder != NULL, NULL);
2160 g_return_val_if_fail(item != NULL, NULL);
2161 folder_path = imap_folder_get_path(folder);
2163 g_return_val_if_fail(folder_path != NULL, NULL);
2164 if (folder_path[0] == G_DIR_SEPARATOR) {
2166 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2169 path = g_strdup(folder_path);
2172 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2173 folder_path, G_DIR_SEPARATOR_S,
2176 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2179 g_free(folder_path);
2184 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2187 gchar *dirpath, *imap_path;
2188 IMAPSession *session;
2189 FolderItem *new_item;
2194 gboolean no_select = FALSE, no_sub = FALSE;
2195 gboolean exist = FALSE;
2197 g_return_val_if_fail(folder != NULL, NULL);
2198 g_return_val_if_fail(folder->account != NULL, NULL);
2199 g_return_val_if_fail(parent != NULL, NULL);
2200 g_return_val_if_fail(name != NULL, NULL);
2202 debug_print("getting session...\n");
2203 session = imap_session_get(folder);
2208 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2209 dirpath = g_strdup(name);
2210 }else if (parent->path)
2211 dirpath = g_strconcat(parent->path, "/", name, NULL);
2212 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2213 dirpath = g_strdup(name);
2214 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2217 Xstrdup_a(imap_dir, folder->account->imap_dir, {unlock_session(session);return NULL;});
2218 strtailchomp(imap_dir, '/');
2219 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2221 dirpath = g_strdup(name);
2225 /* keep trailing directory separator to create a folder that contains
2227 imap_path = imap_utf8_to_modified_utf7(dirpath);
2229 strtailchomp(dirpath, '/');
2230 Xstrdup_a(new_name, name, {
2232 unlock_session(session);
2235 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2236 imap_path_separator_subst(imap_path, separator);
2237 /* remove trailing / for display */
2238 strtailchomp(new_name, '/');
2240 if (strcmp(dirpath, "INBOX") != 0) {
2245 argbuf = g_ptr_array_new();
2246 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2247 if (r != MAILIMAP_NO_ERROR) {
2248 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2251 ptr_array_free_strings(argbuf);
2252 g_ptr_array_free(argbuf, TRUE);
2253 unlock_session(session);
2257 if (clist_count(lep_list) > 0)
2259 mailimap_list_result_free(lep_list);
2262 ok = imap_cmd_create(session, imap_path);
2263 if (ok != IMAP_SUCCESS) {
2264 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2267 unlock_session(session);
2268 if (ok == MAILIMAP_ERROR_STREAM) {
2269 imap_disc_session_destroy(folder);
2274 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2275 if (r == MAILIMAP_NO_ERROR) {
2276 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2277 lep_list, dirpath, TRUE);
2279 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2280 no_select = cur_item->no_select;
2281 no_sub = cur_item->no_sub;
2282 g_slist_free(item_list);
2284 mailimap_list_result_free(lep_list);
2287 imap_threaded_subscribe(folder, imap_path, TRUE);
2291 /* just get flags */
2292 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2293 if (r == MAILIMAP_NO_ERROR) {
2294 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2295 lep_list, dirpath, TRUE);
2297 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2298 no_select = cur_item->no_select;
2299 no_sub = cur_item->no_sub;
2300 g_slist_free(item_list);
2302 mailimap_list_result_free(lep_list);
2306 new_item = folder_item_new(folder, new_name, dirpath);
2307 new_item->no_select = no_select;
2308 new_item->no_sub = no_sub;
2309 folder_item_append(parent, new_item);
2313 dirpath = folder_item_get_path(new_item);
2314 if (!is_dir_exist(dirpath))
2315 make_dir_hier(dirpath);
2317 unlock_session(session);
2320 /* folder existed, scan it */
2321 imap_scan_required(folder, new_item);
2322 folder_item_scan_full(new_item, FALSE);
2328 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2333 gchar *real_oldpath;
2334 gchar *real_newpath;
2336 gchar *old_cache_dir;
2337 gchar *new_cache_dir;
2338 IMAPSession *session;
2341 gint exists, recent, unseen;
2342 guint32 uid_validity;
2344 g_return_val_if_fail(folder != NULL, -1);
2345 g_return_val_if_fail(item != NULL, -1);
2346 g_return_val_if_fail(item->path != NULL, -1);
2347 g_return_val_if_fail(name != NULL, -1);
2349 debug_print("getting session...\n");
2350 session = imap_session_get(folder);
2355 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2356 g_warning(_("New folder name must not contain the namespace "
2358 unlock_session(session);
2362 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2364 g_free(session->mbox);
2365 session->mbox = NULL;
2366 session->exists = 0;
2367 session->recent = 0;
2368 session->expunge = 0;
2369 ok = imap_cmd_examine(session, "INBOX",
2370 &exists, &recent, &unseen, &uid_validity, FALSE);
2371 if (ok != IMAP_SUCCESS) {
2372 g_free(real_oldpath);
2373 unlock_session(session);
2374 if (ok == MAILIMAP_ERROR_STREAM) {
2375 imap_disc_session_destroy(folder);
2381 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2382 if (strchr(item->path, G_DIR_SEPARATOR)) {
2383 dirpath = g_path_get_dirname(item->path);
2384 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2387 newpath = g_strdup(name);
2389 real_newpath = imap_utf8_to_modified_utf7(newpath);
2390 imap_path_separator_subst(real_newpath, separator);
2392 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2393 if (ok != IMAP_SUCCESS) {
2394 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2395 real_oldpath, real_newpath);
2396 g_free(real_oldpath);
2398 g_free(real_newpath);
2399 unlock_session(session);
2400 if (ok == MAILIMAP_ERROR_STREAM) {
2401 imap_disc_session_destroy(folder);
2407 item->name = g_strdup(name);
2409 old_cache_dir = folder_item_get_path(item);
2411 paths[0] = g_strdup(item->path);
2413 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2414 imap_rename_folder_func, paths);
2416 if (is_dir_exist(old_cache_dir)) {
2417 new_cache_dir = folder_item_get_path(item);
2418 if (rename(old_cache_dir, new_cache_dir) < 0) {
2419 FILE_OP_ERROR(old_cache_dir, "rename");
2421 g_free(new_cache_dir);
2424 g_free(old_cache_dir);
2427 g_free(real_oldpath);
2428 g_free(real_newpath);
2429 unlock_session(session);
2433 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2437 IMAPSession *session;
2438 debug_print("getting session...\n");
2440 session = imap_session_get(folder);
2444 if (item && item->path) {
2445 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2448 if (!strcmp(path, "INBOX") && sub == FALSE)
2450 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2451 r = imap_threaded_subscribe(folder, path, sub);
2454 r = imap_threaded_subscribe(folder, rpath, sub);
2460 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2463 IMAPSession *session;
2466 gboolean selected_folder;
2468 g_return_val_if_fail(folder != NULL, -1);
2469 g_return_val_if_fail(item != NULL, -1);
2470 g_return_val_if_fail(item->path != NULL, -1);
2472 debug_print("getting session...\n");
2473 session = imap_session_get(folder);
2477 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2479 imap_threaded_subscribe(folder, path, FALSE);
2481 selected_folder = (session->mbox != NULL) &&
2482 (!strcmp(session->mbox, item->path));
2483 if (selected_folder) {
2484 ok = imap_cmd_close(session);
2485 if (ok != MAILIMAP_NO_ERROR) {
2486 debug_print("close err %d\n", ok);
2487 if (ok == MAILIMAP_ERROR_STREAM) {
2488 imap_disc_session_destroy(folder);
2494 ok = imap_cmd_delete(session, path);
2495 if (ok != IMAP_SUCCESS) {
2496 gchar *tmp = g_strdup_printf("%s%c", path,
2497 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2500 ok = imap_cmd_delete(session, path);
2503 if (ok != IMAP_SUCCESS) {
2504 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2506 unlock_session(session);
2507 if (ok == MAILIMAP_ERROR_STREAM) {
2508 imap_disc_session_destroy(folder);
2515 cache_dir = folder_item_get_path(item);
2516 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2517 g_warning("can't remove directory '%s'\n", cache_dir);
2519 folder_item_remove(item);
2520 unlock_session(session);
2524 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2528 g_return_val_if_fail(item != NULL, -1);
2529 g_return_val_if_fail(item->folder != NULL, -1);
2530 g_return_val_if_fail(item->node != NULL, -1);
2532 node = item->node->children;
2533 while (node != NULL) {
2535 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2539 debug_print("IMAP removing %s\n", item->path);
2541 if (imap_remove_all_msg(folder, item) < 0)
2543 return imap_remove_folder_real(folder, item);
2546 typedef struct _uncached_data {
2547 IMAPSession *session;
2549 MsgNumberList *numlist;
2555 static void *imap_get_uncached_messages_thread(void *data)
2557 uncached_data *stuff = (uncached_data *)data;
2558 IMAPSession *session = stuff->session;
2559 FolderItem *item = stuff->item;
2560 MsgNumberList *numlist = stuff->numlist;
2562 GSList *newlist = NULL;
2563 GSList *llast = NULL;
2564 GSList *seq_list, *cur;
2566 debug_print("uncached_messages\n");
2568 if (session == NULL || item == NULL || item->folder == NULL
2569 || FOLDER_CLASS(item->folder) != &imap_class) {
2574 seq_list = imap_get_lep_set_from_numlist(numlist);
2575 debug_print("get msgs info\n");
2576 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2577 struct mailimap_set * imapset;
2583 if (session->cancelled)
2586 imapset = cur->data;
2588 r = imap_threaded_fetch_env(session->folder,
2589 imapset, &env_list);
2590 if (r != MAILIMAP_NO_ERROR)
2593 session_set_access_time(SESSION(session));
2596 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2597 struct imap_fetch_env_info * info;
2600 info = carray_get(env_list, i);
2601 msginfo = imap_envelope_from_lep(info, item);
2602 if (msginfo == NULL)
2604 msginfo->folder = item;
2606 llast = newlist = g_slist_append(newlist, msginfo);
2608 llast = g_slist_append(llast, msginfo);
2609 llast = llast->next;
2614 imap_fetch_env_free(env_list);
2617 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2618 struct mailimap_set * imapset;
2620 imapset = cur->data;
2621 mailimap_set_free(imapset);
2624 session_set_access_time(SESSION(session));
2629 #define MAX_MSG_NUM 50
2631 static GSList *imap_get_uncached_messages(IMAPSession *session,
2633 MsgNumberList *numlist)
2635 GSList *result = NULL;
2637 uncached_data *data = g_new0(uncached_data, 1);
2642 data->total = g_slist_length(numlist);
2643 debug_print("messages list : %i\n", data->total);
2645 while (cur != NULL) {
2646 GSList * partial_result;
2654 while (count < MAX_MSG_NUM) {
2659 if (newlist == NULL)
2660 llast = newlist = g_slist_append(newlist, p);
2662 llast = g_slist_append(llast, p);
2663 llast = llast->next;
2673 data->session = session;
2675 data->numlist = newlist;
2678 if (prefs_common.work_offline &&
2679 !inc_offline_should_override(FALSE,
2680 _("Claws Mail needs network access in order "
2681 "to access the IMAP server."))) {
2687 (GSList *)imap_get_uncached_messages_thread(data);
2689 statusbar_progress_all(data->cur,data->total, 1);
2691 g_slist_free(newlist);
2693 result = g_slist_concat(result, partial_result);
2697 statusbar_progress_all(0,0,0);
2698 statusbar_pop_all();
2703 static void imap_delete_all_cached_messages(FolderItem *item)
2707 g_return_if_fail(item != NULL);
2708 g_return_if_fail(item->folder != NULL);
2709 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
2711 debug_print("Deleting all cached messages...\n");
2713 dir = folder_item_get_path(item);
2714 if (is_dir_exist(dir))
2715 remove_all_numbered_files(dir);
2718 debug_print("done.\n");
2721 gchar imap_get_path_separator_for_item(FolderItem *item)
2723 Folder *folder = NULL;
2724 IMAPFolder *imap_folder = NULL;
2725 IMAPSession *session = NULL;
2730 folder = item->folder;
2735 imap_folder = IMAP_FOLDER(folder);
2740 debug_print("getting session...");
2741 session = imap_session_get(FOLDER(folder));
2742 result = imap_get_path_separator(session, imap_folder, item->path);
2743 unlock_session(session);
2747 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
2751 gchar separator = '\0';
2753 g_return_val_if_fail(session != NULL, '/');
2754 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
2756 if (r != MAILIMAP_NO_ERROR) {
2757 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
2761 if (clist_count(lep_list) > 0) {
2762 clistiter * iter = clist_begin(lep_list);
2763 struct mailimap_mailbox_list * mb;
2764 mb = clist_content(iter);
2766 separator = mb->mb_delimiter;
2767 debug_print("got separator: %c\n", folder->last_seen_separator);
2769 mailimap_list_result_free(lep_list);
2773 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2775 gchar separator = '/';
2777 if (folder->last_seen_separator == 0) {
2778 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
2781 if (folder->last_seen_separator == 0) {
2782 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
2785 if (folder->last_seen_separator != 0) {
2786 debug_print("using separator: %c\n", folder->last_seen_separator);
2787 return folder->last_seen_separator;
2793 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2798 g_return_val_if_fail(folder != NULL, NULL);
2799 g_return_val_if_fail(path != NULL, NULL);
2801 real_path = imap_utf8_to_modified_utf7(path);
2802 separator = imap_get_path_separator(session, folder, path);
2803 imap_path_separator_subst(real_path, separator);
2808 static gint imap_set_message_flags(IMAPSession *session,
2809 MsgNumberList *numlist,
2817 seq_list = imap_get_lep_set_from_numlist(numlist);
2819 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
2820 struct mailimap_set * imapset;
2822 imapset = cur->data;
2824 ok = imap_cmd_store(session, imapset,
2828 imap_lep_set_free(seq_list);
2830 return IMAP_SUCCESS;
2833 typedef struct _select_data {
2834 IMAPSession *session;
2839 guint32 *uid_validity;
2843 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
2845 gint *exists, gint *recent, gint *unseen,
2846 guint32 *uid_validity, gboolean block)
2850 gint exists_, recent_, unseen_;
2851 guint32 uid_validity_;
2853 if (!exists && !recent && !unseen && !uid_validity) {
2854 if (session->mbox && strcmp(session->mbox, path) == 0)
2855 return IMAP_SUCCESS;
2864 uid_validity = &uid_validity_;
2866 g_free(session->mbox);
2867 session->mbox = NULL;
2868 session->exists = 0;
2869 session->recent = 0;
2870 session->expunge = 0;
2872 real_path = imap_get_real_path(session, folder, path);
2874 ok = imap_cmd_select(session, real_path,
2875 exists, recent, unseen, uid_validity, block);
2876 if (ok != IMAP_SUCCESS) {
2877 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
2878 if (ok == MAILIMAP_ERROR_STREAM) {
2879 imap_disc_session_destroy(FOLDER(folder));
2883 session->mbox = g_strdup(path);
2884 session->folder_content_changed = FALSE;
2885 session->exists = *exists;
2886 session->recent = *recent;
2887 session->expunge = 0;
2888 session->unseen = *unseen;
2889 session->uid_validity = *uid_validity;
2890 debug_print("select: exists %d recent %d expunge %d uid_validity %d\n",
2891 session->exists, session->recent, session->expunge,
2892 session->uid_validity);
2899 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
2900 const gchar *path, IMAPFolderItem *item,
2902 guint32 *uid_next, guint32 *uid_validity,
2903 gint *unseen, gboolean block)
2907 struct mailimap_mailbox_data_status * data_status;
2912 real_path = imap_get_real_path(session, folder, path);
2931 if (session->mbox != NULL &&
2932 !strcmp(session->mbox, item->item.path)) {
2933 r = imap_cmd_close(session);
2934 if (r != MAILIMAP_NO_ERROR) {
2935 debug_print("close err %d\n", r);
2940 r = imap_threaded_status(FOLDER(folder), real_path,
2941 &data_status, mask);
2944 if (r != MAILIMAP_NO_ERROR) {
2945 debug_print("status err %d\n", r);
2949 if (data_status->st_info_list == NULL) {
2950 mailimap_mailbox_data_status_free(data_status);
2951 debug_print("status->st_info_list == NULL\n");
2956 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
2957 iter = clist_next(iter)) {
2958 struct mailimap_status_info * info;
2960 info = clist_content(iter);
2961 switch (info->st_att) {
2962 case MAILIMAP_STATUS_ATT_MESSAGES:
2964 * messages = info->st_value;
2965 got_values |= 1 << 0;
2969 case MAILIMAP_STATUS_ATT_UIDNEXT:
2971 * uid_next = info->st_value;
2972 got_values |= 1 << 2;
2976 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
2978 * uid_validity = info->st_value;
2979 got_values |= 1 << 3;
2983 case MAILIMAP_STATUS_ATT_UNSEEN:
2985 * unseen = info->st_value;
2986 got_values |= 1 << 4;
2991 mailimap_mailbox_data_status_free(data_status);
2993 if (got_values != mask) {
2994 g_warning("status: incomplete values received (%d)\n", got_values);
2996 return IMAP_SUCCESS;
2999 static void imap_free_capabilities(IMAPSession *session)
3001 slist_free_strings(session->capability);
3002 g_slist_free(session->capability);
3003 session->capability = NULL;
3006 /* low-level IMAP4rev1 commands */
3008 static gint imap_cmd_login(IMAPSession *session,
3009 const gchar *user, const gchar *pass,
3015 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3016 gint ok = IMAP_ERROR;
3017 if (imap_has_capability(session, "STARTTLS")) {
3019 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3020 ok = imap_cmd_starttls(session);
3021 if (ok != IMAP_SUCCESS) {
3022 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3026 imap_free_capabilities(session);
3027 if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR) {
3028 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3033 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3034 "server requires TLS, but Claws Mail "
3035 "has been compiled without OpenSSL "
3037 SESSION(session)->server);
3041 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3046 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3048 SESSION(session)->server,
3050 r = imap_threaded_login(session->folder, user, pass, type);
3051 if (r != MAILIMAP_NO_ERROR) {
3052 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3053 SESSION(session)->server);
3056 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3057 SESSION(session)->server);
3063 static gint imap_cmd_noop(IMAPSession *session)
3066 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3068 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3069 if (r != MAILIMAP_NO_ERROR) {
3070 debug_print("noop err %d\n", r);
3074 session->folder_content_changed = FALSE;
3076 if ((exists && exists != session->exists)
3077 || (recent && recent != session->recent)
3078 || (expunge && expunge != session->expunge)
3079 || (unseen && unseen != session->unseen)) {
3080 session->folder_content_changed = TRUE;
3082 if (uidnext != 0 && uidnext != session->uid_next) {
3083 session->uid_next = uidnext;
3084 session->folder_content_changed = TRUE;
3086 if (uidval != 0 && uidval != session->uid_validity) {
3087 session->uid_validity = uidval;
3088 session->folder_content_changed = TRUE;
3091 session->exists = exists;
3092 session->recent = recent;
3093 session->expunge = expunge;
3094 session->unseen = unseen;
3096 session_set_access_time(SESSION(session));
3098 return IMAP_SUCCESS;
3102 static gint imap_cmd_starttls(IMAPSession *session)
3106 r = imap_threaded_starttls(session->folder,
3107 SESSION(session)->server, SESSION(session)->port);
3108 if (r != MAILIMAP_NO_ERROR) {
3109 debug_print("starttls err %d\n", r);
3112 return IMAP_SUCCESS;
3116 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3117 gint *exists, gint *recent, gint *unseen,
3118 guint32 *uid_validity, gboolean block)
3122 r = imap_threaded_select(session->folder, folder,
3123 exists, recent, unseen, uid_validity);
3124 if (r != MAILIMAP_NO_ERROR) {
3125 debug_print("select err %d\n", r);
3128 return IMAP_SUCCESS;
3131 static gint imap_cmd_close(IMAPSession *session)
3135 r = imap_threaded_close(session->folder);
3136 if (r != MAILIMAP_NO_ERROR) {
3137 debug_print("close err %d\n", r);
3140 g_free(session->mbox);
3141 session->mbox = NULL;
3142 session->exists = 0;
3143 session->recent = 0;
3144 session->expunge = 0;
3145 return IMAP_SUCCESS;
3148 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3149 gint *exists, gint *recent, gint *unseen,
3150 guint32 *uid_validity, gboolean block)
3154 r = imap_threaded_examine(session->folder, folder,
3155 exists, recent, unseen, uid_validity);
3156 if (r != MAILIMAP_NO_ERROR) {
3157 debug_print("examine err %d\n", r);
3161 return IMAP_SUCCESS;
3164 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3168 r = imap_threaded_create(session->folder, folder);
3169 if (r != MAILIMAP_NO_ERROR) {
3174 return IMAP_SUCCESS;
3177 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3178 const gchar *new_folder)
3182 r = imap_threaded_rename(session->folder, old_folder,
3184 if (r != MAILIMAP_NO_ERROR) {
3189 return IMAP_SUCCESS;
3192 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3197 r = imap_threaded_delete(session->folder, folder);
3198 if (r != MAILIMAP_NO_ERROR) {
3203 return IMAP_SUCCESS;
3206 typedef struct _fetch_data {
3207 IMAPSession *session;
3209 const gchar *filename;
3215 static void *imap_cmd_fetch_thread(void *data)
3217 fetch_data *stuff = (fetch_data *)data;
3218 IMAPSession *session = stuff->session;
3219 guint32 uid = stuff->uid;
3220 const gchar *filename = stuff->filename;
3224 r = imap_threaded_fetch_content(session->folder,
3228 r = imap_threaded_fetch_content(session->folder,
3231 if (r != MAILIMAP_NO_ERROR) {
3232 debug_print("fetch err %d\n", r);
3233 return GINT_TO_POINTER(IMAP_ERROR);
3235 return GINT_TO_POINTER(IMAP_SUCCESS);
3238 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3239 const gchar *filename, gboolean headers,
3242 fetch_data *data = g_new0(fetch_data, 1);
3245 data->session = session;
3247 data->filename = filename;
3248 data->headers = headers;
3251 if (prefs_common.work_offline &&
3252 !inc_offline_should_override(FALSE,
3253 _("Claws Mail needs network access in order "
3254 "to access the IMAP server."))) {
3258 statusbar_print_all(_("Fetching message..."));
3259 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3260 statusbar_pop_all();
3266 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
3267 const gchar *file, IMAPFlags flags,
3270 struct mailimap_flag_list * flag_list;
3273 g_return_val_if_fail(file != NULL, IMAP_ERROR);
3275 flag_list = imap_flag_to_lep(flags);
3276 r = imap_threaded_append(session->folder, destfolder,
3277 file, flag_list, (int *)new_uid);
3278 mailimap_flag_list_free(flag_list);
3280 if (r != MAILIMAP_NO_ERROR) {
3281 debug_print("append err %d\n", r);
3284 return IMAP_SUCCESS;
3287 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3288 const gchar *destfolder, GRelation *uid_mapping,
3289 struct mailimap_set **source, struct mailimap_set **dest)
3293 g_return_val_if_fail(session != NULL, IMAP_ERROR);
3294 g_return_val_if_fail(set != NULL, IMAP_ERROR);
3295 g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
3297 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3298 if (r != MAILIMAP_NO_ERROR) {
3303 return IMAP_SUCCESS;
3306 static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
3307 IMAPFlags flags, int do_add)
3310 struct mailimap_flag_list * flag_list;
3311 struct mailimap_store_att_flags * store_att_flags;
3313 flag_list = imap_flag_to_lep(flags);
3317 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3320 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3322 r = imap_threaded_store(session->folder, set, store_att_flags);
3323 mailimap_store_att_flags_free(store_att_flags);
3324 if (r != MAILIMAP_NO_ERROR) {
3329 return IMAP_SUCCESS;
3332 static gint imap_cmd_expunge(IMAPSession *session)
3336 if (prefs_common.work_offline &&
3337 !inc_offline_should_override(FALSE,
3338 _("Claws Mail needs network access in order "
3339 "to access the IMAP server."))) {
3343 r = imap_threaded_expunge(session->folder);
3344 if (r != MAILIMAP_NO_ERROR) {
3349 return IMAP_SUCCESS;
3352 static void imap_path_separator_subst(gchar *str, gchar separator)
3355 gboolean in_escape = FALSE;
3357 if (!separator || separator == '/') return;
3359 for (p = str; *p != '\0'; p++) {
3360 if (*p == '/' && !in_escape)
3362 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3364 else if (*p == '-' && in_escape)
3369 static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
3371 static iconv_t cd = (iconv_t)-1;
3372 static gboolean iconv_ok = TRUE;
3375 size_t norm_utf7_len;
3377 gchar *to_str, *to_p;
3379 gboolean in_escape = FALSE;
3381 if (!iconv_ok) return g_strdup(mutf7_str);
3383 if (cd == (iconv_t)-1) {
3384 cd = iconv_open(CS_INTERNAL, CS_UTF_7);
3385 if (cd == (iconv_t)-1) {
3386 g_warning("iconv cannot convert UTF-7 to %s\n",
3389 return g_strdup(mutf7_str);
3393 /* modified UTF-7 to normal UTF-7 conversion */
3394 norm_utf7 = g_string_new(NULL);
3396 for (p = mutf7_str; *p != '\0'; p++) {
3397 /* replace: '&' -> '+',
3399 escaped ',' -> '/' */
3400 if (!in_escape && *p == '&') {
3401 if (*(p + 1) != '-') {
3402 g_string_append_c(norm_utf7, '+');
3405 g_string_append_c(norm_utf7, '&');
3408 } else if (in_escape && *p == ',') {
3409 g_string_append_c(norm_utf7, '/');
3410 } else if (in_escape && *p == '-') {
3411 g_string_append_c(norm_utf7, '-');
3414 g_string_append_c(norm_utf7, *p);
3418 norm_utf7_p = norm_utf7->str;
3419 norm_utf7_len = norm_utf7->len;
3420 to_len = strlen(mutf7_str) * 5;
3421 to_p = to_str = g_malloc(to_len + 1);
3423 if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
3424 &to_p, &to_len) == -1) {
3425 g_warning(_("iconv cannot convert UTF-7 to %s\n"),
3426 conv_get_locale_charset_str());
3427 g_string_free(norm_utf7, TRUE);
3429 return g_strdup(mutf7_str);
3432 /* second iconv() call for flushing */
3433 iconv(cd, NULL, NULL, &to_p, &to_len);
3434 g_string_free(norm_utf7, TRUE);
3440 static gchar *imap_utf8_to_modified_utf7(const gchar *from)
3442 static iconv_t cd = (iconv_t)-1;
3443 static gboolean iconv_ok = TRUE;
3444 gchar *norm_utf7, *norm_utf7_p;
3445 size_t from_len, norm_utf7_len;
3447 gchar *from_tmp, *to, *p;
3448 gboolean in_escape = FALSE;
3450 if (!iconv_ok) return g_strdup(from);
3452 if (cd == (iconv_t)-1) {
3453 cd = iconv_open(CS_UTF_7, CS_INTERNAL);
3454 if (cd == (iconv_t)-1) {
3455 g_warning(_("iconv cannot convert %s to UTF-7\n"),
3458 return g_strdup(from);
3462 /* UTF-8 to normal UTF-7 conversion */
3463 Xstrdup_a(from_tmp, from, return g_strdup(from));
3464 from_len = strlen(from);
3465 norm_utf7_len = from_len * 5;
3466 Xalloca(norm_utf7, norm_utf7_len + 1, return g_strdup(from));
3467 norm_utf7_p = norm_utf7;
3469 #define IS_PRINT(ch) (isprint(ch) && IS_ASCII(ch))
3471 while (from_len > 0) {
3472 if (*from_tmp == '+') {
3473 *norm_utf7_p++ = '+';
3474 *norm_utf7_p++ = '-';
3478 } else if (IS_PRINT(*(guchar *)from_tmp)) {
3479 /* printable ascii char */
3480 *norm_utf7_p = *from_tmp;
3486 size_t conv_len = 0;
3488 /* unprintable char: convert to UTF-7 */
3490 while (!IS_PRINT(*(guchar *)p) && conv_len < from_len) {
3491 conv_len += g_utf8_skip[*(guchar *)p];
3492 p += g_utf8_skip[*(guchar *)p];
3495 from_len -= conv_len;
3496 if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
3498 &norm_utf7_p, &norm_utf7_len) == -1) {
3499 g_warning(_("iconv cannot convert UTF-8 to UTF-7\n"));
3500 return g_strdup(from);
3503 /* second iconv() call for flushing */
3504 iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
3510 *norm_utf7_p = '\0';
3511 to_str = g_string_new(NULL);
3512 for (p = norm_utf7; p < norm_utf7_p; p++) {
3513 /* replace: '&' -> "&-",
3516 BASE64 '/' -> ',' */
3517 if (!in_escape && *p == '&') {
3518 g_string_append(to_str, "&-");
3519 } else if (!in_escape && *p == '+') {
3520 if (*(p + 1) == '-') {
3521 g_string_append_c(to_str, '+');
3524 g_string_append_c(to_str, '&');
3527 } else if (in_escape && *p == '/') {
3528 g_string_append_c(to_str, ',');
3529 } else if (in_escape && *p == '-') {
3530 g_string_append_c(to_str, '-');
3533 g_string_append_c(to_str, *p);
3539 g_string_append_c(to_str, '-');
3543 g_string_free(to_str, FALSE);
3548 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3550 FolderItem *item = node->data;
3551 gchar **paths = data;
3552 const gchar *oldpath = paths[0];
3553 const gchar *newpath = paths[1];
3554 gchar *real_oldpath, *real_newpath;
3556 gchar *new_itempath;
3558 IMAPSession *session = imap_session_get(item->folder);
3560 oldpathlen = strlen(oldpath);
3561 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3562 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3566 base = item->path + oldpathlen;
3567 while (*base == G_DIR_SEPARATOR) base++;
3569 new_itempath = g_strdup(newpath);
3571 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3574 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3576 item->path = new_itempath;
3578 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3580 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3581 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3583 g_free(real_oldpath);
3584 g_free(real_newpath);
3588 typedef struct _get_list_uid_data {
3590 IMAPSession *session;
3591 IMAPFolderItem *item;
3592 GSList **msgnum_list;
3594 } get_list_uid_data;
3596 static void *get_list_of_uids_thread(void *data)
3598 get_list_uid_data *stuff = (get_list_uid_data *)data;
3599 Folder *folder = stuff->folder;
3600 IMAPFolderItem *item = stuff->item;
3601 GSList **msgnum_list = stuff->msgnum_list;
3602 gint ok, nummsgs = 0, lastuid_old;
3603 IMAPSession *session;
3604 GSList *uidlist, *elem;
3606 clist * lep_uidlist;
3608 session = stuff->session;
3609 if (session == NULL) {
3611 return GINT_TO_POINTER(-1);
3613 /* no session locking here, it's already locked by caller */
3614 ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
3615 NULL, NULL, NULL, NULL, TRUE);
3616 if (ok != IMAP_SUCCESS) {
3618 if (ok == MAILIMAP_ERROR_STREAM) {
3619 imap_disc_session_destroy(folder);
3622 return GINT_TO_POINTER(-1);
3625 g_slist_free(item->uid_list);
3626 item->uid_list = NULL;
3630 if (folder->account && folder->account->low_bandwidth) {
3631 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3635 if (r == MAILIMAP_NO_ERROR) {
3636 GSList * fetchuid_list =
3637 imap_uid_list_from_lep(lep_uidlist);
3638 mailimap_search_result_free(lep_uidlist);
3640 uidlist = g_slist_concat(fetchuid_list, uidlist);
3642 carray * lep_uidtab;
3643 r = imap_threaded_fetch_uid(folder, 1,
3645 if (r == MAILIMAP_NO_ERROR) {
3646 GSList * fetchuid_list =
3647 imap_uid_list_from_lep_tab(lep_uidtab);
3648 imap_fetch_uid_list_free(lep_uidtab);
3649 uidlist = g_slist_concat(fetchuid_list, uidlist);
3653 if (r != MAILIMAP_NO_ERROR) {
3655 return GINT_TO_POINTER(-1);
3658 lastuid_old = item->lastuid;
3660 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3663 msgnum = GPOINTER_TO_INT(elem->data);
3665 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3666 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3669 g_slist_free(uidlist);
3671 return GINT_TO_POINTER(nummsgs);
3674 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3677 get_list_uid_data *data = g_new0(get_list_uid_data, 1);
3679 data->folder = folder;
3681 data->msgnum_list = msgnum_list;
3682 data->session = session;
3683 if (prefs_common.work_offline &&
3684 !inc_offline_should_override(FALSE,
3685 _("Claws Mail needs network access in order "
3686 "to access the IMAP server."))) {
3691 result = GPOINTER_TO_INT(get_list_of_uids_thread(data));
3697 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3699 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3700 IMAPSession *session;
3702 GSList *uidlist = NULL;
3704 gboolean selected_folder;
3705 gint known_list_len = 0;
3706 debug_print("get_num_list\n");
3708 g_return_val_if_fail(folder != NULL, -1);
3709 g_return_val_if_fail(item != NULL, -1);
3710 g_return_val_if_fail(item->item.path != NULL, -1);
3711 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3712 g_return_val_if_fail(folder->account != NULL, -1);
3714 known_list_len = g_slist_length(item->uid_list);
3715 if (!item->should_update) {
3716 debug_print("get_num_list: nothing to update\n");
3717 *old_uids_valid = TRUE;
3718 if (known_list_len == item->item.total_msgs
3719 && known_list_len > 0) {
3720 *msgnum_list = g_slist_copy(item->uid_list);
3721 return known_list_len;
3723 debug_print("don't know the list length...\n");
3727 debug_print("getting session...\n");
3728 session = imap_session_get(folder);
3729 g_return_val_if_fail(session != NULL, -1);
3731 if (FOLDER_ITEM(item)->path)
3732 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3733 FOLDER_ITEM(item)->folder->name,
3735 FOLDER_ITEM(item)->path);
3737 statusbar_print_all(_("Scanning folder %s ..."),
3738 FOLDER_ITEM(item)->folder->name);
3740 selected_folder = (session->mbox != NULL) &&
3741 (!strcmp(session->mbox, item->item.path));
3743 if (item->should_trash_cache) {
3744 *old_uids_valid = FALSE;
3745 debug_print("get_num_list: trashing num list\n");
3746 debug_print("Freeing imap uid cache\n");
3748 g_slist_free(item->uid_list);
3749 item->uid_list = NULL;
3751 imap_delete_all_cached_messages((FolderItem *)item);
3753 debug_print("get_num_list: updating num list\n");
3754 *old_uids_valid = TRUE;
3757 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3758 debug_print("get_num_list: got %d msgs\n", nummsgs);
3761 statusbar_pop_all();
3762 unlock_session(session);
3766 *msgnum_list = uidlist;
3768 dir = folder_item_get_path((FolderItem *)item);
3769 debug_print("removing old messages from %s\n", dir);
3770 remove_numbered_files_not_in_list(dir, *msgnum_list);
3773 debug_print("get_num_list - ok - %i\n", nummsgs);
3774 statusbar_pop_all();
3775 unlock_session(session);
3776 item->should_trash_cache = FALSE;
3777 item->should_update = FALSE;
3781 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3786 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3787 flags.tmp_flags = 0;
3789 g_return_val_if_fail(item != NULL, NULL);
3790 g_return_val_if_fail(file != NULL, NULL);
3792 if (folder_has_parent_of_type(item, F_QUEUE)) {
3793 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3794 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3795 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3798 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3799 if (!msginfo) return NULL;
3801 msginfo->plaintext_file = g_strdup(file);
3802 msginfo->folder = item;
3807 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3808 GSList *msgnum_list)
3810 IMAPSession *session;
3811 MsgInfoList *ret = NULL;
3814 debug_print("get_msginfos\n");
3816 g_return_val_if_fail(folder != NULL, NULL);
3817 g_return_val_if_fail(item != NULL, NULL);
3818 g_return_val_if_fail(msgnum_list != NULL, NULL);
3820 debug_print("getting session...\n");
3821 session = imap_session_get(folder);
3822 g_return_val_if_fail(session != NULL, NULL);
3824 debug_print("IMAP getting msginfos\n");
3825 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
3826 NULL, NULL, NULL, NULL, FALSE);
3827 if (ok != IMAP_SUCCESS) {
3828 unlock_session(session);
3829 if (ok == MAILIMAP_ERROR_STREAM) {
3830 imap_disc_session_destroy(folder);
3835 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
3836 folder_has_parent_of_type(item, F_QUEUE))) {
3837 ret = g_slist_concat(ret,
3838 imap_get_uncached_messages(session, item,
3841 MsgNumberList *sorted_list, *elem, *llast = NULL;
3842 gint startnum, lastnum;
3844 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
3846 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
3848 llast = g_slist_last(ret);
3849 for (elem = sorted_list;; elem = g_slist_next(elem)) {
3853 num = GPOINTER_TO_INT(elem->data);
3855 if (num > lastnum + 1 || elem == NULL) {
3857 for (i = startnum; i <= lastnum; ++i) {
3859 unlock_session(session);
3860 file = imap_fetch_msg(folder, item, i);
3861 lock_session(session);
3863 MsgInfo *msginfo = imap_parse_msg(file, item);
3864 if (msginfo != NULL) {
3865 msginfo->msgnum = i;
3867 llast = ret = g_slist_append(ret, msginfo);
3869 llast = g_slist_append(llast, msginfo);
3870 llast = llast->next;
3875 session_set_access_time(SESSION(session));
3886 g_slist_free(sorted_list);
3888 unlock_session(session);
3892 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
3894 MsgInfo *msginfo = NULL;
3895 MsgInfoList *msginfolist;
3896 MsgNumberList numlist;
3898 numlist.next = NULL;
3899 numlist.data = GINT_TO_POINTER(uid);
3901 msginfolist = imap_get_msginfos(folder, item, &numlist);
3902 if (msginfolist != NULL) {
3903 msginfo = msginfolist->data;
3904 g_slist_free(msginfolist);
3910 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
3912 IMAPSession *session;
3913 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3914 gint ok, exists = 0, unseen = 0;
3915 guint32 uid_next = 0, uid_val = 0;
3916 gboolean selected_folder;
3918 g_return_val_if_fail(folder != NULL, FALSE);
3919 g_return_val_if_fail(item != NULL, FALSE);
3920 g_return_val_if_fail(item->item.folder != NULL, FALSE);
3921 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
3923 if (item->item.path == NULL)
3926 if (item->should_update) {
3927 debug_print("scan already required\n");
3930 debug_print("getting session...\n");
3931 session = imap_session_get(folder);
3932 g_return_val_if_fail(session != NULL, FALSE);
3934 selected_folder = (session->mbox != NULL) &&
3935 (!strcmp(session->mbox, item->item.path));
3936 if (selected_folder) {
3937 if (!session->folder_content_changed) {
3938 ok = imap_cmd_noop(session);
3939 if (ok != IMAP_SUCCESS) {
3940 debug_print("disconnected!\n");
3941 session = imap_reconnect_if_possible(folder, session);
3942 if (session == NULL)
3946 if (session->folder_content_changed) {
3947 debug_print("CHANGED (self-noop)! scan_required\n");
3948 unlock_session(session);
3949 item->should_update = TRUE;
3950 if (session->uid_validity && session->uid_validity != item->item.mtime) {
3951 item->item.mtime = session->uid_validity;
3952 item->should_trash_cache = TRUE;
3958 debug_print("CHANGED (previous noop)! scan_required\n");
3959 unlock_session(session);
3960 item->should_update = TRUE;
3961 if (session->uid_validity && session->uid_validity != item->item.mtime) {
3962 item->item.mtime = session->uid_validity;
3963 item->should_trash_cache = TRUE;
3968 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
3969 &exists, &uid_next, &uid_val, &unseen, FALSE);
3970 if (ok != IMAP_SUCCESS) {
3971 unlock_session(session);
3972 if (ok == MAILIMAP_ERROR_STREAM) {
3973 imap_disc_session_destroy(folder);
3979 debug_print("exists %d, item->item.total_msgs %d\n",
3980 exists, item->item.total_msgs);
3981 if (exists != item->item.total_msgs
3982 || unseen != item->item.unread_msgs
3983 || uid_next != item->uid_next
3984 || uid_val != item->item.mtime) {
3985 unlock_session(session);
3986 debug_print("CHANGED (status)! scan_required\n");
3987 item->last_change = time(NULL);
3988 item->should_update = TRUE;
3989 item->uid_next = uid_next;
3990 if (uid_val != item->item.mtime) {
3991 item->item.mtime = uid_val;
3992 item->should_trash_cache = TRUE;
3997 item->should_update = FALSE;
3998 unlock_session(session);
4002 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4004 IMAPSession *session;
4005 IMAPFlags flags_set = 0, flags_unset = 0;
4006 gint ok = IMAP_SUCCESS;
4007 MsgNumberList numlist;
4008 hashtable_data *ht_data = NULL;
4010 g_return_if_fail(folder != NULL);
4011 g_return_if_fail(folder->klass == &imap_class);
4012 g_return_if_fail(item != NULL);
4013 g_return_if_fail(item->folder == folder);
4014 g_return_if_fail(msginfo != NULL);
4015 g_return_if_fail(msginfo->folder == item);
4017 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4018 flags_set |= IMAP_FLAG_FLAGGED;
4019 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4020 flags_unset |= IMAP_FLAG_FLAGGED;
4022 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4023 flags_unset |= IMAP_FLAG_SEEN;
4024 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4025 flags_set |= IMAP_FLAG_SEEN;
4027 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4028 flags_set |= IMAP_FLAG_ANSWERED;
4029 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4030 flags_unset |= IMAP_FLAG_ANSWERED;
4032 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4033 flags_set |= IMAP_FLAG_DELETED;
4034 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4035 flags_unset |= IMAP_FLAG_DELETED;
4037 if (!flags_set && !flags_unset) {
4038 /* the changed flags were not translatable to IMAP-speak.
4039 * like MSG_POSTFILTERED, so just apply. */
4040 msginfo->flags.perm_flags = newflags;
4044 debug_print("getting session...\n");
4045 session = imap_session_get(folder);
4050 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
4051 NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
4052 unlock_session(session);
4053 if (ok == MAILIMAP_ERROR_STREAM) {
4054 imap_disc_session_destroy(folder);
4059 numlist.next = NULL;
4060 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4062 if (IMAP_FOLDER_ITEM(item)->batching) {
4063 /* instead of performing an UID STORE command for each message change,
4064 * as a lot of them can change "together", we just fill in hashtables
4065 * and defer the treatment so that we're able to send only one
4068 debug_print("IMAP batch mode on, deferring flags change\n");
4070 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4071 GINT_TO_POINTER(flags_set));
4072 if (ht_data == NULL) {
4073 ht_data = g_new0(hashtable_data, 1);
4074 ht_data->session = session;
4075 ht_data->item = IMAP_FOLDER_ITEM(item);
4076 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4077 GINT_TO_POINTER(flags_set), ht_data);
4079 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4082 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4083 GINT_TO_POINTER(flags_unset));
4084 if (ht_data == NULL) {
4085 ht_data = g_new0(hashtable_data, 1);
4086 ht_data->session = session;
4087 ht_data->item = IMAP_FOLDER_ITEM(item);
4088 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4089 GINT_TO_POINTER(flags_unset), ht_data);
4091 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4092 GINT_TO_POINTER(msginfo->msgnum));
4095 debug_print("IMAP changing flags\n");
4097 ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
4098 if (ok != IMAP_SUCCESS) {
4099 unlock_session(session);
4100 if (ok == MAILIMAP_ERROR_STREAM) {
4101 imap_disc_session_destroy(folder);
4109 ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
4110 if (ok != IMAP_SUCCESS) {
4111 unlock_session(session);
4112 if (ok == MAILIMAP_ERROR_STREAM) {
4113 imap_disc_session_destroy(folder);
4120 msginfo->flags.perm_flags = newflags;
4121 unlock_session(session);
4125 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4128 IMAPSession *session;
4130 MsgNumberList numlist;
4132 g_return_val_if_fail(folder != NULL, -1);
4133 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4134 g_return_val_if_fail(item != NULL, -1);
4136 debug_print("getting session...\n");
4137 session = imap_session_get(folder);
4138 if (!session) return -1;
4140 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
4141 NULL, NULL, NULL, NULL, FALSE);
4142 if (ok != IMAP_SUCCESS) {
4143 unlock_session(session);
4144 if (ok == MAILIMAP_ERROR_STREAM) {
4145 imap_disc_session_destroy(folder);
4150 numlist.next = NULL;
4151 numlist.data = GINT_TO_POINTER(uid);
4153 ok = imap_set_message_flags
4154 (session, &numlist, IMAP_FLAG_DELETED, TRUE);
4155 if (ok != IMAP_SUCCESS) {
4156 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4157 unlock_session(session);
4158 if (ok == MAILIMAP_ERROR_STREAM) {
4159 imap_disc_session_destroy(folder);
4165 if (!session->uidplus) {
4166 ok = imap_cmd_expunge(session);
4170 uidstr = g_strdup_printf("%u", uid);
4171 ok = imap_cmd_expunge(session);
4174 if (ok != IMAP_SUCCESS) {
4175 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4176 unlock_session(session);
4177 if (ok == MAILIMAP_ERROR_STREAM) {
4178 imap_disc_session_destroy(folder);
4184 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4185 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4186 dir = folder_item_get_path(item);
4187 if (is_dir_exist(dir))
4188 remove_numbered_files(dir, uid, uid);
4190 unlock_session(session);
4191 return IMAP_SUCCESS;
4194 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4196 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4199 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4203 g_return_val_if_fail(list != NULL, -1);
4205 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4206 if (GPOINTER_TO_INT(elem->data) >= num)
4209 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4213 * NEW and DELETED flags are not syncronized
4214 * - The NEW/RECENT flags in IMAP folders can not really be directly
4215 * modified by Sylpheed
4216 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4217 * meaning, in IMAP it always removes the messages from the FolderItem
4218 * in Sylpheed it can mean to move the message to trash
4221 typedef struct _get_flags_data {
4224 MsgInfoList *msginfo_list;
4225 GRelation *msgflags;
4226 gboolean full_search;
4230 static /*gint*/ void *imap_get_flags_thread(void *data)
4232 get_flags_data *stuff = (get_flags_data *)data;
4233 Folder *folder = stuff->folder;
4234 FolderItem *fitem = (FolderItem *) stuff->item;
4235 MsgInfoList *msginfo_list = stuff->msginfo_list;
4236 GRelation *msgflags = stuff->msgflags;
4238 carray * lep_uidtab;
4239 IMAPSession *session;
4242 GHashTable *flags_hash = NULL;
4243 gboolean full_search = stuff->full_search;
4244 GSList *sorted_list = NULL;
4245 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
4246 GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
4247 GSList *seq_list, *cur;
4248 gboolean reverse_seen = FALSE;
4249 gboolean selected_folder;
4250 gint exists_cnt, unseen_cnt;
4252 session = imap_session_get(folder);
4253 if (session == NULL) {
4255 return GINT_TO_POINTER(-1);
4257 selected_folder = (session->mbox != NULL) &&
4258 (!strcmp(session->mbox, fitem->path));
4260 if (!selected_folder) {
4261 ok = imap_select(session, IMAP_FOLDER(folder), fitem->path,
4262 &exists_cnt, NULL, &unseen_cnt, NULL, TRUE);
4263 if (ok != IMAP_SUCCESS) {
4265 unlock_session(session);
4266 if (ok == MAILIMAP_ERROR_STREAM) {
4267 imap_disc_session_destroy(folder);
4270 return GINT_TO_POINTER(-1);
4273 if (unseen_cnt > exists_cnt / 2)
4274 reverse_seen = TRUE;
4277 if (fitem->unread_msgs > fitem->total_msgs / 2)
4278 reverse_seen = TRUE;
4281 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4283 seq_list = imap_get_lep_set_from_msglist(msginfo_list);
4285 struct mailimap_set * set;
4286 set = mailimap_set_new_interval(1, 0);
4287 seq_list = g_slist_append(NULL, set);
4290 if (folder->account && folder->account->low_bandwidth) {
4291 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4292 struct mailimap_set * imapset;
4293 clist * lep_uidlist;
4296 imapset = cur->data;
4298 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4299 full_search ? NULL:imapset, &lep_uidlist);
4302 r = imap_threaded_search(folder,
4303 IMAP_SEARCH_TYPE_UNSEEN,
4304 full_search ? NULL:imapset, &lep_uidlist);
4306 if (r == MAILIMAP_NO_ERROR) {
4309 uidlist = imap_uid_list_from_lep(lep_uidlist);
4310 mailimap_search_result_free(lep_uidlist);
4312 unseen = g_slist_concat(unseen, uidlist);
4315 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4316 full_search ? NULL:imapset, &lep_uidlist);
4317 if (r == MAILIMAP_NO_ERROR) {
4320 uidlist = imap_uid_list_from_lep(lep_uidlist);
4321 mailimap_search_result_free(lep_uidlist);
4323 flagged = g_slist_concat(flagged, uidlist);
4326 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4327 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4328 full_search ? NULL:imapset, &lep_uidlist);
4329 if (r == MAILIMAP_NO_ERROR) {
4332 uidlist = imap_uid_list_from_lep(lep_uidlist);
4333 mailimap_search_result_free(lep_uidlist);
4335 answered = g_slist_concat(answered, uidlist);
4338 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4339 full_search ? NULL:imapset, &lep_uidlist);
4340 if (r == MAILIMAP_NO_ERROR) {
4343 uidlist = imap_uid_list_from_lep(lep_uidlist);
4344 mailimap_search_result_free(lep_uidlist);
4346 deleted = g_slist_concat(deleted, uidlist);
4351 p_answered = answered;
4352 p_flagged = flagged;
4353 p_deleted = deleted;
4356 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4357 if (r == MAILIMAP_NO_ERROR) {
4358 flags_hash = g_hash_table_new_full(g_int_hash, g_int_equal, free, NULL);
4359 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash);
4360 imap_fetch_uid_flags_list_free(lep_uidtab);
4363 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4365 MsgPermFlags flags, oldflags;
4368 msginfo = (MsgInfo *) elem->data;
4369 flags = msginfo->flags.perm_flags;
4370 wasnew = (flags & MSG_NEW);
4371 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_MARKED|MSG_DELETED);
4373 if (folder->account && folder->account->low_bandwidth) {
4374 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4375 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
4377 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4380 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4381 if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
4382 if (!reverse_seen) {
4383 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4385 flags &= ~(MSG_UNREAD | MSG_NEW);
4389 if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
4390 flags |= MSG_MARKED;
4392 flags &= ~MSG_MARKED;
4394 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4395 if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
4396 flags |= MSG_REPLIED;
4398 flags &= ~MSG_REPLIED;
4399 if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
4400 flags |= MSG_DELETED;
4402 flags &= ~MSG_DELETED;
4405 if (flags_hash != NULL) {
4408 puid = malloc(sizeof(* puid));
4409 * puid = msginfo->msgnum;
4411 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash, puid));
4415 if ((flags & MSG_UNREAD) == 0)
4422 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4426 g_hash_table_destroy(flags_hash);
4428 imap_lep_set_free(seq_list);
4429 g_slist_free(flagged);
4430 g_slist_free(deleted);
4431 g_slist_free(answered);
4432 g_slist_free(unseen);
4433 g_slist_free(sorted_list);
4435 unlock_session(session);
4437 return GINT_TO_POINTER(0);
4440 static gint imap_get_flags(Folder *folder, FolderItem *item,
4441 MsgInfoList *msginfo_list, GRelation *msgflags)
4444 get_flags_data *data = g_new0(get_flags_data, 1);
4446 data->folder = folder;
4448 data->msginfo_list = msginfo_list;
4449 data->msgflags = msgflags;
4450 data->full_search = FALSE;
4452 GSList *tmp = NULL, *cur;
4454 if (prefs_common.work_offline &&
4455 !inc_offline_should_override(FALSE,
4456 _("Claws Mail needs network access in order "
4457 "to access the IMAP server."))) {
4462 tmp = folder_item_get_msg_list(item);
4464 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4465 data->full_search = TRUE;
4467 for (cur = tmp; cur; cur = cur->next)
4468 procmsg_msginfo_free((MsgInfo *)cur->data);
4472 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4479 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4481 gboolean flags_set = GPOINTER_TO_INT(user_data);
4482 gint flags_value = GPOINTER_TO_INT(key);
4483 hashtable_data *data = (hashtable_data *)value;
4484 IMAPFolderItem *_item = data->item;
4485 FolderItem *item = (FolderItem *)_item;
4486 gint ok = IMAP_ERROR;
4487 IMAPSession *session = NULL;
4489 debug_print("getting session...\n");
4490 session = imap_session_get(item->folder);
4492 data->msglist = g_slist_reverse(data->msglist);
4494 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4497 g_slist_length(data->msglist));
4500 ok = imap_select(session, IMAP_FOLDER(item->folder), item->path,
4501 NULL, NULL, NULL, NULL, FALSE);
4503 if (ok == IMAP_SUCCESS) {
4504 imap_set_message_flags(data->session, data->msglist, flags_value, flags_set);
4506 g_warning("can't select mailbox %s\n", item->path);
4509 unlock_session(session);
4510 if (ok == MAILIMAP_ERROR_STREAM) {
4511 imap_disc_session_destroy(item->folder);
4514 g_slist_free(data->msglist);
4519 static void process_hashtable(IMAPFolderItem *item)
4521 if (item->flags_set_table) {
4522 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4523 g_hash_table_destroy(item->flags_set_table);
4524 item->flags_set_table = NULL;
4526 if (item->flags_unset_table) {
4527 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4528 g_hash_table_destroy(item->flags_unset_table);
4529 item->flags_unset_table = NULL;
4534 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4536 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4537 IMAPSession *session;
4539 g_return_if_fail(item != NULL);
4541 if (item->batching == batch)
4545 item->batching = TRUE;
4546 debug_print("IMAP switching to batch mode\n");
4547 if (!item->flags_set_table) {
4548 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4550 if (!item->flags_unset_table) {
4551 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4553 session = imap_session_get(folder);
4555 imap_refresh_sensitivity(session);
4556 session->sens_update_block = TRUE;
4557 unlock_session(session);
4560 debug_print("IMAP switching away from batch mode\n");
4562 process_hashtable(item);
4563 item->batching = FALSE;
4564 session = imap_session_get(folder);
4566 unlock_session(session);
4567 session->sens_update_block = FALSE;
4568 imap_refresh_sensitivity(session);
4575 /* data types conversion libetpan <-> claws */
4579 #define ETPAN_IMAP_MB_MARKED 1
4580 #define ETPAN_IMAP_MB_UNMARKED 2
4581 #define ETPAN_IMAP_MB_NOSELECT 4
4582 #define ETPAN_IMAP_MB_NOINFERIORS 8
4584 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4590 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4591 switch (imap_flags->mbf_sflag) {
4592 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4593 flags |= ETPAN_IMAP_MB_MARKED;
4595 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4596 flags |= ETPAN_IMAP_MB_NOSELECT;
4598 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4599 flags |= ETPAN_IMAP_MB_UNMARKED;
4604 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4605 cur = clist_next(cur)) {
4606 struct mailimap_mbx_list_oflag * oflag;
4608 oflag = clist_content(cur);
4610 switch (oflag->of_type) {
4611 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4612 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4620 static GSList * imap_list_from_lep(IMAPFolder * folder,
4621 clist * list, const gchar * real_path, gboolean all)
4624 GSList * item_list = NULL, *llast = NULL;
4626 for(iter = clist_begin(list) ; iter != NULL ;
4627 iter = clist_next(iter)) {
4628 struct mailimap_mailbox_list * mb;
4636 FolderItem *new_item;
4638 mb = clist_content(iter);
4644 if (mb->mb_flag != NULL)
4645 flags = imap_flags_to_flags(mb->mb_flag);
4647 delimiter = mb->mb_delimiter;
4650 dup_name = strdup(name);
4651 if (delimiter != '\0')
4652 subst_char(dup_name, delimiter, '/');
4654 base = g_path_get_basename(dup_name);
4655 if (base[0] == '.') {
4660 if (!all && path_cmp(name, real_path) == 0) {
4666 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4667 dup_name[strlen(dup_name)-1] = '\0';
4670 loc_name = imap_modified_utf7_to_utf8(base);
4671 loc_path = imap_modified_utf7_to_utf8(dup_name);
4673 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4674 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4675 new_item->no_sub = TRUE;
4676 if (strcmp(dup_name, "INBOX") != 0 &&
4677 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4678 new_item->no_select = TRUE;
4680 if (item_list == NULL)
4681 llast = item_list = g_slist_append(item_list, new_item);
4683 llast = g_slist_append(llast, new_item);
4684 llast = llast->next;
4686 debug_print("folder '%s' found.\n", loc_path);
4697 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
4699 GSList *sorted_list, *cur;
4700 guint first, last, next;
4701 GSList *ret_list = NULL, *llast = NULL;
4703 struct mailimap_set * current_set;
4704 unsigned int item_count;
4706 if (numlist == NULL)
4710 current_set = mailimap_set_new_empty();
4712 sorted_list = g_slist_copy(numlist);
4713 sorted_list = g_slist_sort(sorted_list, g_int_compare);
4715 first = GPOINTER_TO_INT(sorted_list->data);
4718 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
4719 if (GPOINTER_TO_INT(cur->data) == 0)
4724 last = GPOINTER_TO_INT(cur->data);
4726 next = GPOINTER_TO_INT(cur->next->data);
4730 if (last + 1 != next || next == 0) {
4732 struct mailimap_set_item * item;
4733 item = mailimap_set_item_new(first, last);
4734 mailimap_set_add(current_set, item);
4739 if (count >= IMAP_SET_MAX_COUNT) {
4740 if (ret_list == NULL)
4741 llast = ret_list = g_slist_append(ret_list,
4744 llast = g_slist_append(llast, current_set);
4745 llast = llast->next;
4747 current_set = mailimap_set_new_empty();
4754 if (clist_count(current_set->set_list) > 0) {
4755 ret_list = g_slist_append(ret_list,
4759 g_slist_free(sorted_list);
4764 static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist)
4766 MsgNumberList *numlist = NULL;
4770 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
4771 MsgInfo *msginfo = (MsgInfo *) cur->data;
4773 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
4775 numlist = g_slist_reverse(numlist);
4776 seq_list = imap_get_lep_set_from_numlist(numlist);
4777 g_slist_free(numlist);
4782 static GSList * imap_uid_list_from_lep(clist * list)
4789 for(iter = clist_begin(list) ; iter != NULL ;
4790 iter = clist_next(iter)) {
4793 puid = clist_content(iter);
4794 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4797 result = g_slist_reverse(result);
4801 static GSList * imap_uid_list_from_lep_tab(carray * list)
4808 for(i = 0 ; i < carray_count(list) ; i ++) {
4811 puid = carray_get(list, i);
4812 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4814 result = g_slist_reverse(result);
4818 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
4826 for(i = 0 ; i < carray_count(list) ; i += 2) {
4831 puid = carray_get(list, i);
4832 pflags = carray_get(list, i + 1);
4833 pguid = malloc(sizeof(* pguid));
4836 g_hash_table_insert(hash, pguid, GINT_TO_POINTER(* pflags));
4840 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
4843 MsgInfo *msginfo = NULL;
4846 MsgFlags flags = {0, 0};
4848 if (info->headers == NULL)
4851 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
4852 if (folder_has_parent_of_type(item, F_QUEUE)) {
4853 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
4854 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
4855 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
4857 flags.perm_flags = info->flags;
4861 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
4864 msginfo->msgnum = uid;
4865 msginfo->size = size;
4871 static void imap_lep_set_free(GSList *seq_list)
4875 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
4876 struct mailimap_set * imapset;
4878 imapset = cur->data;
4879 mailimap_set_free(imapset);
4881 g_slist_free(seq_list);
4884 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags)
4886 struct mailimap_flag_list * flag_list;
4888 flag_list = mailimap_flag_list_new_empty();
4890 if (IMAP_IS_SEEN(flags))
4891 mailimap_flag_list_add(flag_list,
4892 mailimap_flag_new_seen());
4893 if (IMAP_IS_ANSWERED(flags))
4894 mailimap_flag_list_add(flag_list,
4895 mailimap_flag_new_answered());
4896 if (IMAP_IS_FLAGGED(flags))
4897 mailimap_flag_list_add(flag_list,
4898 mailimap_flag_new_flagged());
4899 if (IMAP_IS_DELETED(flags))
4900 mailimap_flag_list_add(flag_list,
4901 mailimap_flag_new_deleted());
4902 if (IMAP_IS_DRAFT(flags))
4903 mailimap_flag_list_add(flag_list,
4904 mailimap_flag_new_draft());
4909 guint imap_folder_get_refcnt(Folder *folder)
4911 return ((IMAPFolder *)folder)->refcnt;
4914 void imap_folder_ref(Folder *folder)
4916 ((IMAPFolder *)folder)->refcnt++;
4919 void imap_disconnect_all(void)
4922 for (list = account_get_list(); list != NULL; list = list->next) {
4923 PrefsAccount *account = list->data;
4924 if (account->protocol == A_IMAP4) {
4925 RemoteFolder *folder = (RemoteFolder *)account->folder;
4926 if (folder && folder->session) {
4927 IMAPSession *session = (IMAPSession *)folder->session;
4928 imap_threaded_disconnect(FOLDER(folder));
4929 SESSION(session)->state = SESSION_DISCONNECTED;
4930 session_destroy(SESSION(session));
4931 folder->session = NULL;
4937 void imap_folder_unref(Folder *folder)
4939 if (((IMAPFolder *)folder)->refcnt > 0)
4940 ((IMAPFolder *)folder)->refcnt--;
4943 void imap_cancel_all(void)
4948 folderlist = folder_get_list();
4949 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
4950 Folder *folder = (Folder *) cur->data;
4952 if (folder->klass == &imap_class) {
4953 if (imap_is_busy(folder)) {
4954 IMAPSession *imap_session;
4955 RemoteFolder *rfolder;
4957 g_printerr("cancelled\n");
4958 imap_threaded_cancel(folder);
4959 rfolder = (RemoteFolder *) folder;
4960 imap_session = (IMAPSession *) rfolder->session;
4962 imap_session->cancelled = 1;
4968 gboolean imap_cancel_all_enabled(void)
4973 folderlist = folder_get_list();
4974 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
4975 Folder *folder = (Folder *) cur->data;
4977 if (folder->klass == &imap_class) {
4978 if (imap_is_busy(folder)) {
4987 static gboolean imap_is_busy(Folder *folder)
4989 IMAPSession *imap_session;
4990 RemoteFolder *rfolder;
4992 rfolder = (RemoteFolder *) folder;
4993 imap_session = (IMAPSession *) rfolder->session;
4994 if (imap_session == NULL)
4997 return imap_session->busy;
5000 #else /* HAVE_LIBETPAN */
5002 static FolderClass imap_class;
5004 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5005 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5007 static Folder *imap_folder_new (const gchar *name,
5010 static gboolean missing_imap_warning = TRUE;
5011 if (missing_imap_warning) {
5012 missing_imap_warning = FALSE;
5014 _("You have one or more IMAP accounts "
5015 "defined. However this version of "
5016 "Claws Mail has been built without "
5017 "IMAP support; your IMAP account(s) are "
5019 "You probably need to "
5020 "install libetpan and recompile "
5025 static gint imap_create_tree (Folder *folder)
5029 static FolderItem *imap_create_folder (Folder *folder,
5035 static gint imap_rename_folder (Folder *folder,
5042 gchar imap_get_path_separator_for_item(FolderItem *item)
5047 FolderClass *imap_get_class(void)
5049 if (imap_class.idstr == NULL) {
5050 imap_class.type = F_IMAP;
5051 imap_class.idstr = "imap";
5052 imap_class.uistr = "IMAP4";
5054 imap_class.new_folder = imap_folder_new;
5055 imap_class.create_tree = imap_create_tree;
5056 imap_class.create_folder = imap_create_folder;
5057 imap_class.rename_folder = imap_rename_folder;
5059 imap_class.set_xml = folder_set_xml;
5060 imap_class.get_xml = folder_get_xml;
5061 imap_class.item_set_xml = imap_item_set_xml;
5062 imap_class.item_get_xml = imap_item_get_xml;
5063 /* nothing implemented */
5069 void imap_disconnect_all(void)
5073 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5078 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5083 void imap_cache_msg(FolderItem *item, gint msgnum)
5087 void imap_cancel_all(void)
5091 gboolean imap_cancel_all_enabled(void)
5098 void imap_synchronise(FolderItem *item, gint days)
5100 #ifdef HAVE_LIBETPAN
5101 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5102 debug_print("%s already synced\n", item->path?item->path:item->name);
5105 debug_print("syncing %s\n", item->path?item->path:item->name);
5106 imap_gtk_synchronise(item, days);
5107 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5111 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5113 #ifdef HAVE_LIBETPAN
5116 folder_item_set_xml(folder, item, tag);
5118 #ifdef HAVE_LIBETPAN
5119 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5120 XMLAttr *attr = (XMLAttr *) cur->data;
5122 if (!attr || !attr->name || !attr->value) continue;
5123 if (!strcmp(attr->name, "uidnext"))
5124 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5125 if (!strcmp(attr->name, "last_sync"))
5126 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5127 if (!strcmp(attr->name, "last_change"))
5128 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5130 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5131 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5135 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5139 tag = folder_item_get_xml(folder, item);
5141 #ifdef HAVE_LIBETPAN
5142 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5143 IMAP_FOLDER_ITEM(item)->uid_next));
5144 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5145 IMAP_FOLDER_ITEM(item)->last_sync));
5146 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5147 IMAP_FOLDER_ITEM(item)->last_change));