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"
48 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
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"
75 typedef struct _IMAPFolder IMAPFolder;
76 typedef struct _IMAPSession IMAPSession;
77 typedef struct _IMAPNameSpace IMAPNameSpace;
78 typedef struct _IMAPFolderItem IMAPFolderItem;
80 #include "prefs_account.h"
82 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
83 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
84 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
90 /* list of IMAPNameSpace */
94 gchar last_seen_separator;
103 gboolean authenticated;
112 gboolean folder_content_changed;
123 gboolean sens_update_block;
126 struct _IMAPNameSpace
132 #define IMAPBUFSIZE 8192
134 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
135 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
136 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
137 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
138 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
139 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
142 #define IMAP4_PORT 143
143 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
144 #define IMAPS_PORT 993
147 #define IMAP_CMD_LIMIT 1000
150 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
151 ITEM_CAN_CREATE_FLAGS,
152 ITEM_CANNOT_CREATE_FLAGS
155 struct _IMAPFolderItem
164 GHashTable *flags_set_table;
165 GHashTable *flags_unset_table;
168 gboolean should_update;
169 gboolean should_trash_cache;
170 gint can_create_flags;
172 GHashTable *tags_set_table;
173 GHashTable *tags_unset_table;
178 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
179 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
181 static void imap_folder_init (Folder *folder,
185 static Folder *imap_folder_new (const gchar *name,
187 static void imap_folder_destroy (Folder *folder);
189 static IMAPSession *imap_session_new (Folder *folder,
190 const PrefsAccount *account);
191 static gint imap_session_authenticate(IMAPSession *session,
192 const PrefsAccount *account);
193 static void imap_session_destroy (Session *session);
195 static gchar *imap_fetch_msg (Folder *folder,
198 static gchar *imap_fetch_msg_full (Folder *folder,
203 static void imap_remove_cached_msg (Folder *folder,
206 static gint imap_add_msg (Folder *folder,
210 static gint imap_add_msgs (Folder *folder,
213 GRelation *relation);
215 static gint imap_copy_msg (Folder *folder,
218 static gint imap_copy_msgs (Folder *folder,
220 MsgInfoList *msglist,
221 GRelation *relation);
223 static gint imap_remove_msg (Folder *folder,
226 static gint imap_remove_msgs (Folder *folder,
228 MsgInfoList *msglist,
229 GRelation *relation);
230 static gint imap_remove_all_msg (Folder *folder,
233 static gboolean imap_is_msg_changed (Folder *folder,
237 static gint imap_close (Folder *folder,
240 static gint imap_scan_tree (Folder *folder);
242 static gint imap_create_tree (Folder *folder);
244 static FolderItem *imap_create_folder (Folder *folder,
247 static gint imap_rename_folder (Folder *folder,
250 static gint imap_remove_folder (Folder *folder,
253 static FolderItem *imap_folder_item_new (Folder *folder);
254 static void imap_folder_item_destroy (Folder *folder,
257 static IMAPSession *imap_session_get (Folder *folder);
259 static gint imap_auth (IMAPSession *session,
264 static gint imap_scan_tree_recursive (IMAPSession *session,
268 static void imap_create_missing_folders (Folder *folder);
269 static FolderItem *imap_create_special_folder
271 SpecialFolderItemType stype,
274 static gint imap_do_copy_msgs (Folder *folder,
276 MsgInfoList *msglist,
277 GRelation *relation);
279 static void imap_delete_all_cached_messages (FolderItem *item);
280 static void imap_set_batch (Folder *folder,
283 static gint imap_set_message_flags (IMAPSession *session,
284 IMAPFolderItem *item,
285 MsgNumberList *numlist,
289 static gint imap_select (IMAPSession *session,
295 guint32 *uid_validity,
296 gint *can_create_flags,
298 static gint imap_status (IMAPSession *session,
301 IMAPFolderItem *item,
304 guint32 *uid_validity,
307 static void imap_commit_tags (FolderItem *item,
312 static gchar imap_get_path_separator (IMAPSession *session,
316 static gchar *imap_get_real_path (IMAPSession *session,
321 static void imap_synchronise (FolderItem *item, gint days);
323 static gboolean imap_is_busy (Folder *folder);
325 static void imap_free_capabilities (IMAPSession *session);
327 /* low-level IMAP4rev1 commands */
328 static gint imap_cmd_login (IMAPSession *session,
332 static gint imap_cmd_noop (IMAPSession *session);
333 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
334 static gint imap_cmd_starttls (IMAPSession *session);
336 static gint imap_cmd_select (IMAPSession *session,
341 guint32 *uid_validity,
342 gint *can_create_flags,
345 static gint imap_cmd_close (IMAPSession *session);
346 static gint imap_cmd_examine (IMAPSession *session,
351 guint32 *uid_validity,
353 static gint imap_cmd_create (IMAPSession *sock,
354 const gchar *folder);
355 static gint imap_cmd_rename (IMAPSession *sock,
356 const gchar *oldfolder,
357 const gchar *newfolder);
358 static gint imap_cmd_delete (IMAPSession *session,
359 const gchar *folder);
360 static gint imap_cmd_fetch (IMAPSession *sock,
362 const gchar *filename,
365 static gint imap_cmd_append (IMAPSession *session,
366 IMAPFolderItem *item,
367 const gchar *destfolder,
371 static gint imap_cmd_copy (IMAPSession *session,
372 struct mailimap_set * set,
373 const gchar *destfolder,
374 GRelation *uid_mapping,
375 struct mailimap_set ** source,
376 struct mailimap_set ** dest);
377 static gint imap_cmd_store (IMAPSession *session,
378 IMAPFolderItem *item,
379 struct mailimap_set * set,
383 static gint imap_cmd_expunge (IMAPSession *session);
385 static void imap_path_separator_subst (gchar *str,
388 static gboolean imap_rename_folder_func (GNode *node,
390 static gint imap_get_num_list (Folder *folder,
393 gboolean *old_uids_valid);
394 static GSList *imap_get_msginfos (Folder *folder,
396 GSList *msgnum_list);
397 static MsgInfo *imap_get_msginfo (Folder *folder,
400 static gboolean imap_scan_required (Folder *folder,
402 static void imap_change_flags (Folder *folder,
405 MsgPermFlags newflags);
406 static gint imap_get_flags (Folder *folder,
408 MsgInfoList *msglist,
409 GRelation *msgflags);
410 static gchar *imap_folder_get_path (Folder *folder);
411 static gchar *imap_item_get_path (Folder *folder,
413 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
416 /* data types conversion libetpan <-> claws */
417 static GSList * imap_list_from_lep(IMAPFolder * folder,
418 clist * list, const gchar * real_path, gboolean all);
419 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
420 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
421 static GSList * imap_uid_list_from_lep(clist * list);
422 static GSList * imap_uid_list_from_lep_tab(carray * list);
423 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
425 GHashTable *tags_hash);
426 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
428 static void imap_lep_set_free(GSList *seq_list);
429 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
431 typedef struct _hashtable_data {
433 IMAPFolderItem *item;
436 static FolderClass imap_class;
438 FolderClass *imap_get_class(void)
440 if (imap_class.idstr == NULL) {
441 imap_class.type = F_IMAP;
442 imap_class.idstr = "imap";
443 imap_class.uistr = "IMAP4";
445 /* Folder functions */
446 imap_class.new_folder = imap_folder_new;
447 imap_class.destroy_folder = imap_folder_destroy;
448 imap_class.scan_tree = imap_scan_tree;
449 imap_class.create_tree = imap_create_tree;
451 /* FolderItem functions */
452 imap_class.item_new = imap_folder_item_new;
453 imap_class.item_destroy = imap_folder_item_destroy;
454 imap_class.item_get_path = imap_item_get_path;
455 imap_class.create_folder = imap_create_folder;
456 imap_class.rename_folder = imap_rename_folder;
457 imap_class.remove_folder = imap_remove_folder;
458 imap_class.close = imap_close;
459 imap_class.get_num_list = imap_get_num_list;
460 imap_class.scan_required = imap_scan_required;
461 imap_class.set_xml = folder_set_xml;
462 imap_class.get_xml = folder_get_xml;
463 imap_class.item_set_xml = imap_item_set_xml;
464 imap_class.item_get_xml = imap_item_get_xml;
466 /* Message functions */
467 imap_class.get_msginfo = imap_get_msginfo;
468 imap_class.get_msginfos = imap_get_msginfos;
469 imap_class.fetch_msg = imap_fetch_msg;
470 imap_class.fetch_msg_full = imap_fetch_msg_full;
471 imap_class.add_msg = imap_add_msg;
472 imap_class.add_msgs = imap_add_msgs;
473 imap_class.copy_msg = imap_copy_msg;
474 imap_class.copy_msgs = imap_copy_msgs;
475 imap_class.remove_msg = imap_remove_msg;
476 imap_class.remove_msgs = imap_remove_msgs;
477 imap_class.remove_all_msg = imap_remove_all_msg;
478 imap_class.is_msg_changed = imap_is_msg_changed;
479 imap_class.change_flags = imap_change_flags;
480 imap_class.get_flags = imap_get_flags;
481 imap_class.set_batch = imap_set_batch;
482 imap_class.synchronise = imap_synchronise;
483 imap_class.remove_cached_msg = imap_remove_cached_msg;
484 imap_class.commit_tags = imap_commit_tags;
486 pthread_mutex_init(&imap_mutex, NULL);
493 static void imap_refresh_sensitivity (IMAPSession *session)
497 if (session->sens_update_block)
499 mainwin = mainwindow_get_mainwindow();
501 toolbar_main_set_sensitive(mainwin);
502 main_window_set_menu_sensitive(mainwin);
506 static void lock_session(IMAPSession *session)
509 debug_print("locking session %p (%d)\n", session, session->busy);
511 debug_print(" SESSION WAS LOCKED !! \n");
512 session->busy = TRUE;
513 imap_refresh_sensitivity(session);
515 debug_print("can't lock null session\n");
519 static void unlock_session(IMAPSession *session)
522 debug_print("unlocking session %p\n", session);
523 session->busy = FALSE;
524 imap_refresh_sensitivity(session);
526 debug_print("can't unlock null session\n");
530 static void imap_disc_session_destroy(IMAPSession *session)
532 RemoteFolder *rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
538 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
539 SESSION(session)->state = SESSION_DISCONNECTED;
540 SESSION(session)->sock = NULL;
543 static gboolean is_fatal(int libetpan_errcode)
545 switch(libetpan_errcode) {
546 case MAILIMAP_ERROR_STREAM:
547 case MAILIMAP_ERROR_PROTOCOL:
548 case MAILIMAP_ERROR_PARSE:
549 case MAILIMAP_ERROR_BAD_STATE:
556 static void imap_handle_error(Session *session, int libetpan_errcode)
558 switch(libetpan_errcode) {
559 case MAILIMAP_NO_ERROR:
561 case MAILIMAP_NO_ERROR_AUTHENTICATED:
562 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
564 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
565 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
567 case MAILIMAP_ERROR_BAD_STATE:
568 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
570 case MAILIMAP_ERROR_STREAM:
571 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
573 case MAILIMAP_ERROR_PARSE:
574 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
575 "(very probably non-RFC compliance from the server)\n"));
577 case MAILIMAP_ERROR_CONNECTION_REFUSED:
578 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
580 case MAILIMAP_ERROR_MEMORY:
581 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
583 case MAILIMAP_ERROR_FATAL:
584 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
586 case MAILIMAP_ERROR_PROTOCOL:
587 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
588 "(very probably non-RFC compliance from the server)\n"));
590 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
591 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
593 case MAILIMAP_ERROR_APPEND:
594 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
596 case MAILIMAP_ERROR_NOOP:
597 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
599 case MAILIMAP_ERROR_LOGOUT:
600 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
602 case MAILIMAP_ERROR_CAPABILITY:
603 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
605 case MAILIMAP_ERROR_CHECK:
606 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
608 case MAILIMAP_ERROR_CLOSE:
609 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
611 case MAILIMAP_ERROR_EXPUNGE:
612 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
614 case MAILIMAP_ERROR_COPY:
615 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
617 case MAILIMAP_ERROR_UID_COPY:
618 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
620 case MAILIMAP_ERROR_CREATE:
621 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
623 case MAILIMAP_ERROR_DELETE:
624 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
626 case MAILIMAP_ERROR_EXAMINE:
627 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
629 case MAILIMAP_ERROR_FETCH:
630 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
632 case MAILIMAP_ERROR_UID_FETCH:
633 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
635 case MAILIMAP_ERROR_LIST:
636 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
638 case MAILIMAP_ERROR_LOGIN:
639 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
641 case MAILIMAP_ERROR_LSUB:
642 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
644 case MAILIMAP_ERROR_RENAME:
645 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
647 case MAILIMAP_ERROR_SEARCH:
648 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
650 case MAILIMAP_ERROR_UID_SEARCH:
651 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
653 case MAILIMAP_ERROR_SELECT:
654 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
656 case MAILIMAP_ERROR_STATUS:
657 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
659 case MAILIMAP_ERROR_STORE:
660 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
662 case MAILIMAP_ERROR_UID_STORE:
663 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
665 case MAILIMAP_ERROR_SUBSCRIBE:
666 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
668 case MAILIMAP_ERROR_UNSUBSCRIBE:
669 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
671 case MAILIMAP_ERROR_STARTTLS:
672 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
674 case MAILIMAP_ERROR_INVAL:
675 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
677 case MAILIMAP_ERROR_EXTENSION:
678 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
680 case MAILIMAP_ERROR_SASL:
681 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
683 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
684 case MAILIMAP_ERROR_SSL:
685 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
689 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
694 if (session && is_fatal(libetpan_errcode)) {
695 imap_disc_session_destroy(IMAP_SESSION(session));
696 } else if (session && !is_fatal(libetpan_errcode)) {
697 if (IMAP_SESSION(session)->busy)
698 unlock_session(IMAP_SESSION(session));
702 static Folder *imap_folder_new(const gchar *name, const gchar *path)
706 folder = (Folder *)g_new0(IMAPFolder, 1);
707 folder->klass = &imap_class;
708 imap_folder_init(folder, name, path);
713 static void imap_folder_destroy(Folder *folder)
715 while (imap_folder_get_refcnt(folder) > 0)
716 gtk_main_iteration();
718 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
722 static void imap_folder_init(Folder *folder, const gchar *name,
725 folder_remote_folder_init((Folder *)folder, name, path);
726 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
729 static FolderItem *imap_folder_item_new(Folder *folder)
731 IMAPFolderItem *item;
733 item = g_new0(IMAPFolderItem, 1);
736 item->uid_list = NULL;
738 return (FolderItem *)item;
741 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
743 IMAPFolderItem *item = (IMAPFolderItem *)_item;
745 g_return_if_fail(item != NULL);
746 g_slist_free(item->uid_list);
751 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
753 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
756 g_slist_free(item->uid_list);
757 item->uid_list = NULL;
762 static void imap_reset_uid_lists(Folder *folder)
764 if(folder->node == NULL)
767 /* Destroy all uid lists and rest last uid */
768 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
771 static int imap_get_capabilities(IMAPSession *session)
773 struct mailimap_capability_data *capabilities = NULL;
777 if (session->capability != NULL)
778 return MAILIMAP_NO_ERROR;
780 capabilities = imap_threaded_capability(session->folder, &result);
782 if (result != MAILIMAP_NO_ERROR) {
786 if (capabilities == NULL) {
787 return MAILIMAP_NO_ERROR;
790 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
791 cur = clist_next(cur)) {
792 struct mailimap_capability * cap =
794 if (!cap || cap->cap_data.cap_name == NULL)
796 session->capability = g_slist_append
797 (session->capability,
798 g_strdup(cap->cap_data.cap_name));
799 debug_print("got capa %s\n", cap->cap_data.cap_name);
801 mailimap_capability_data_free(capabilities);
802 return MAILIMAP_NO_ERROR;
805 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
808 for (cur = session->capability; cur; cur = cur->next) {
809 if (!g_ascii_strcasecmp(cur->data, cap))
815 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
818 gint ok = MAILIMAP_ERROR_LOGIN;
819 static time_t last_login_err = 0;
820 gchar *ext_info = "";
822 gchar *server = NULL;
823 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
824 imap_handle_error(SESSION(session), r);
827 server = g_strdup(SESSION(session)->server);
830 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
832 case IMAP_AUTH_CRAM_MD5:
833 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
835 case IMAP_AUTH_DIGEST_MD5:
836 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
838 case IMAP_AUTH_LOGIN:
839 ok = imap_cmd_login(session, user, pass, "LOGIN");
841 case IMAP_AUTH_GSSAPI:
842 ok = imap_cmd_login(session, user, pass, "GSSAPI");
845 debug_print("capabilities:\n"
851 imap_has_capability(session, "ANONYMOUS"),
852 imap_has_capability(session, "CRAM-MD5"),
853 imap_has_capability(session, "DIGEST-MD5"),
854 imap_has_capability(session, "LOGIN"),
855 imap_has_capability(session, "GSSAPI"));
856 if (imap_has_capability(session, "CRAM-MD5"))
857 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
858 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
859 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
860 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
861 ok = imap_cmd_login(session, user, pass, "GSSAPI");
862 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
863 ok = imap_cmd_login(session, user, pass, "LOGIN");
866 if (ok == MAILIMAP_NO_ERROR)
867 session->authenticated = TRUE;
869 if (type == IMAP_AUTH_CRAM_MD5) {
870 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
871 "compiled with SASL support and the "
872 "CRAM-MD5 SASL plugin is installed.");
875 if (type == IMAP_AUTH_DIGEST_MD5) {
876 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
877 "compiled with SASL support and the "
878 "DIGEST-MD5 SASL plugin is installed.");
881 if (time(NULL) - last_login_err > 10) {
882 if (!prefs_common.no_recv_err_panel) {
883 alertpanel_error_log(_("Connection to %s failed: "
887 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
888 "login refused.%s\n"),
892 last_login_err = time(NULL);
898 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
900 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
901 /* Check if this is the first try to establish a
902 connection, if yes we don't try to reconnect */
903 debug_print("reconnecting\n");
904 if (rfolder->session == NULL) {
905 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
906 folder->account->recv_server);
907 SESSION(session)->sock = NULL;
908 session_destroy(SESSION(session));
911 rfolder->session = NULL;
912 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
913 " disconnected. Reconnecting...\n"),
914 folder->account->recv_server);
915 statusbar_print_all(_("IMAP4 connection to %s has been"
916 " disconnected. Reconnecting...\n"),
917 folder->account->recv_server);
918 SESSION(session)->state = SESSION_DISCONNECTED;
919 SESSION(session)->sock = NULL;
920 session_destroy(SESSION(session));
921 /* Clear folders session to make imap_session_get create
922 a new session, because of rfolder->session == NULL
923 it will not try to reconnect again and so avoid an
925 debug_print("getting session...\n");
926 session = imap_session_get(folder);
927 rfolder->session = SESSION(session);
933 static IMAPSession *imap_session_get(Folder *folder)
935 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
936 IMAPSession *session = NULL;
937 gint r = MAILIMAP_NO_ERROR;
939 g_return_val_if_fail(folder != NULL, NULL);
940 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
941 g_return_val_if_fail(folder->account != NULL, NULL);
943 if (prefs_common.work_offline &&
944 !inc_offline_should_override(FALSE,
945 _("Claws Mail needs network access in order "
946 "to access the IMAP server."))) {
950 /* Make sure we have a session */
951 if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
952 session = IMAP_SESSION(rfolder->session);
953 } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
954 session_destroy(SESSION(rfolder->session));
955 rfolder->session = NULL;
957 } else if (rfolder->connecting) {
958 debug_print("already connecting\n");
962 imap_reset_uid_lists(folder);
963 if (time(NULL) - rfolder->last_failure <= 2)
965 rfolder->connecting = TRUE;
966 session = imap_session_new(folder, folder->account);
968 if(session == NULL) {
969 rfolder->last_failure = time(NULL);
970 rfolder->connecting = FALSE;
974 /* Make sure session is authenticated */
975 if (!IMAP_SESSION(session)->authenticated)
976 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
978 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
979 rfolder->session = NULL;
981 imap_threaded_disconnect(session->folder);
982 SESSION(session)->state = SESSION_DISCONNECTED;
983 SESSION(session)->sock = NULL;
984 session_destroy(SESSION(session));
986 rfolder->last_failure = time(NULL);
987 rfolder->connecting = FALSE;
991 /* I think the point of this code is to avoid sending a
992 * keepalive if we've used the session recently and therefore
993 * think it's still alive. Unfortunately, most of the code
994 * does not yet check for errors on the socket, and so if the
995 * connection drops we don't notice until the timeout expires.
996 * A better solution than sending a NOOP every time would be
997 * for every command to be prepared to retry until it is
998 * successfully sent. -- mbp */
999 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1000 /* verify that the session is still alive */
1001 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1002 debug_print("disconnected!\n");
1004 session = imap_reconnect_if_possible(folder, session);
1006 rfolder->session = NULL;
1007 rfolder->connecting = FALSE;
1008 session = imap_session_get(folder);
1012 session->cancelled = FALSE;
1015 rfolder->session = SESSION(session);
1016 rfolder->connecting = FALSE;
1018 return IMAP_SESSION(session);
1021 static IMAPSession *imap_session_new(Folder * folder,
1022 const PrefsAccount *account)
1024 IMAPSession *session;
1027 int authenticated = FALSE;
1029 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1030 /* FIXME: IMAP over SSL only... */
1033 port = account->set_imapport ? account->imapport
1034 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1035 ssl_type = account->ssl_imap;
1037 if (account->ssl_imap != SSL_NONE) {
1038 if (alertpanel_full(_("Insecure connection"),
1039 _("This connection is configured to be secured "
1040 "using SSL, but SSL is not available in this "
1041 "build of Claws Mail. \n\n"
1042 "Do you want to continue connecting to this "
1043 "server? The communication would not be "
1045 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1046 NULL, FALSE, NULL, ALERT_WARNING,
1047 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1050 port = account->set_imapport ? account->imapport
1055 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1057 if (account->set_tunnelcmd) {
1058 r = imap_threaded_connect_cmd(folder,
1060 account->recv_server,
1066 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1067 if (ssl_type == SSL_TUNNEL) {
1068 r = imap_threaded_connect_ssl(folder,
1069 account->recv_server,
1075 r = imap_threaded_connect(folder,
1076 account->recv_server,
1081 statuswindow_pop_all();
1082 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1083 authenticated = TRUE;
1085 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1086 authenticated = FALSE;
1089 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1090 if (r == MAILIMAP_ERROR_SSL)
1091 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1094 imap_handle_error(NULL, r);
1096 if(!prefs_common.no_recv_err_panel) {
1097 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1098 account->recv_server, port);
1100 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1101 account->recv_server, port);
1107 session = g_new0(IMAPSession, 1);
1108 session_init(SESSION(session), account, FALSE);
1109 SESSION(session)->type = SESSION_IMAP;
1110 SESSION(session)->server = g_strdup(account->recv_server);
1111 SESSION(session)->port = port;
1112 SESSION(session)->sock = NULL;
1114 SESSION(session)->destroy = imap_session_destroy;
1116 session->capability = NULL;
1118 session->authenticated = authenticated;
1119 session->mbox = NULL;
1120 session->exists = 0;
1121 session->recent = 0;
1122 session->expunge = 0;
1123 session->cmd_count = 0;
1124 session->folder = folder;
1125 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1127 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1128 if (account->ssl_imap == SSL_STARTTLS) {
1131 ok = imap_cmd_starttls(session);
1132 if (ok != MAILIMAP_NO_ERROR) {
1133 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1134 if (!is_fatal(ok)) {
1135 SESSION(session)->sock = NULL;
1136 session_destroy(SESSION(session));
1141 imap_free_capabilities(session);
1142 session->authenticated = FALSE;
1143 session->uidplus = FALSE;
1144 session->cmd_count = 1;
1147 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1148 (session->authenticated) ? "pre" : "un");
1153 static gint imap_session_authenticate(IMAPSession *session,
1154 const PrefsAccount *account)
1156 gchar *pass, *acc_pass;
1157 gboolean failed = FALSE;
1158 gint ok = MAILIMAP_NO_ERROR;
1159 g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1160 acc_pass = account->passwd;
1163 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1165 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1167 return MAILIMAP_NO_ERROR;
1168 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1170 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1173 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1174 account->recv_server);
1175 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1176 statusbar_pop_all();
1178 if (!failed && !is_fatal(ok)) {
1183 if (prefs_common.no_recv_err_panel) {
1184 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1185 mainwindow_show_error();
1187 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1193 statuswindow_pop_all();
1194 session->authenticated = TRUE;
1195 return MAILIMAP_NO_ERROR;
1198 static void imap_session_destroy(Session *session)
1200 if (session->state != SESSION_DISCONNECTED)
1201 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1203 imap_free_capabilities(IMAP_SESSION(session));
1204 g_free(IMAP_SESSION(session)->mbox);
1207 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1209 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1212 static guint get_file_size_with_crs(const gchar *filename)
1218 if (filename == NULL)
1221 fp = fopen(filename, "rb");
1225 while (fgets(buf, sizeof (buf), fp) != NULL) {
1227 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1235 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1237 gchar *path, *filename;
1239 path = folder_item_get_path(item);
1241 if (!is_dir_exist(path)) {
1246 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1249 if (is_file_exist(filename)) {
1250 claws_unlink(filename);
1255 typedef struct _TagsData {
1258 IMAPFolderItem *item;
1261 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1263 IMAPSession *session;
1264 gint ok, can_create_tags;
1265 Folder *folder = NULL;
1266 TagsData *ht_data = NULL;
1269 g_return_if_fail(item != NULL);
1270 g_return_if_fail(msginfo != NULL);
1272 folder = item->folder;
1273 debug_print("getting session...\n");
1274 session = imap_session_get(folder);
1277 debug_print("can't get session\n");
1281 ok = imap_select(session, IMAP_FOLDER(folder), item,
1282 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1284 if (ok != MAILIMAP_NO_ERROR) {
1289 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1292 if (IMAP_FOLDER_ITEM(item)->batching) {
1293 /* instead of performing an UID STORE command for each message change,
1294 * as a lot of them can change "together", we just fill in hashtables
1295 * and defer the treatment so that we're able to send only one
1298 debug_print("IMAP batch mode on, deferring tags change\n");
1299 for (cur = tags_set; cur; cur = cur->next) {
1300 gint cur_tag = GPOINTER_TO_INT(cur->data);
1302 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1303 GINT_TO_POINTER(cur_tag));
1304 if (ht_data == NULL) {
1305 ht_data = g_new0(TagsData, 1);
1306 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1307 ht_data->item = IMAP_FOLDER_ITEM(item);
1308 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1309 GINT_TO_POINTER(cur_tag), ht_data);
1311 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1314 for (cur = tags_unset; cur; cur = cur->next) {
1315 gint cur_tag = GPOINTER_TO_INT(cur->data);
1317 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1318 GINT_TO_POINTER(cur_tag));
1319 if (ht_data == NULL) {
1320 ht_data = g_new0(TagsData, 1);
1321 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1322 ht_data->item = IMAP_FOLDER_ITEM(item);
1323 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1324 GINT_TO_POINTER(cur_tag), ht_data);
1326 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1330 GSList *list_set = NULL;
1331 GSList *list_unset = NULL;
1334 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1335 numlist.next = NULL;
1337 debug_print("IMAP changing tags NOW\n");
1338 for (cur = tags_set; cur; cur = cur->next) {
1339 gint cur_tag = GPOINTER_TO_INT(cur->data);
1340 const gchar *str = tags_get_tag(cur_tag);
1341 list_set = g_slist_prepend(list_set, g_strdup(str));
1344 ok = imap_set_message_flags(session,
1345 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1346 slist_free_strings(list_set);
1347 g_slist_free(list_set);
1348 if (ok != MAILIMAP_NO_ERROR) {
1353 for (cur = tags_unset; cur; cur = cur->next) {
1354 gint cur_tag = GPOINTER_TO_INT(cur->data);
1355 const gchar *str = tags_get_tag(cur_tag);
1356 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1359 ok = imap_set_message_flags(session,
1360 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1361 slist_free_strings(list_unset);
1362 g_slist_free(list_unset);
1363 if (ok != MAILIMAP_NO_ERROR) {
1370 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1371 gboolean headers, gboolean body)
1373 gchar *path, *filename;
1374 IMAPSession *session;
1377 g_return_val_if_fail(folder != NULL, NULL);
1378 g_return_val_if_fail(item != NULL, NULL);
1383 path = folder_item_get_path(item);
1384 if (!is_dir_exist(path))
1385 make_dir_hier(path);
1386 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1388 debug_print("trying to fetch cached %s\n", filename);
1389 if (is_file_exist(filename)) {
1390 /* see whether the local file represents the whole message
1391 * or not. As the IMAP server reports size with \r chars,
1392 * we have to update the local file (UNIX \n only) size */
1393 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1394 guint have_size = -1;
1397 debug_print("message %d has been already %scached.\n", uid,
1398 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1400 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1401 have_size = get_file_size_with_crs(filename);
1402 if (cached && (cached->size <= have_size || !body)) {
1403 procmsg_msginfo_free(cached);
1404 ok = file_strip_crs(filename);
1405 if (ok == 0 && cached && cached->size <= have_size) {
1406 /* we have it all and stripped */
1407 debug_print("...fully cached in fact; setting flag.\n");
1408 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1411 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1412 debug_print("message not cached and file recent, considering file complete\n");
1413 ok = file_strip_crs(filename);
1417 procmsg_msginfo_free(cached);
1420 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1421 procmsg_msginfo_free(cached);
1425 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1427 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1428 procmsg_msginfo_free(cached);
1432 debug_print("getting session...\n");
1433 session = imap_session_get(folder);
1439 session_set_access_time(SESSION(session));
1440 lock_session(session); /* unlocked later in the function */
1442 debug_print("IMAP fetching messages\n");
1443 ok = imap_select(session, IMAP_FOLDER(folder), item,
1444 NULL, NULL, NULL, NULL, NULL, FALSE);
1445 if (ok != MAILIMAP_NO_ERROR) {
1446 g_warning("can't select mailbox %s\n", item->path);
1451 session_set_access_time(SESSION(session));
1453 debug_print("getting message %d...\n", uid);
1454 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1456 if (ok != MAILIMAP_NO_ERROR) {
1457 g_warning("can't fetch message %d\n", uid);
1462 session_set_access_time(SESSION(session));
1463 unlock_session(session);
1465 ok = file_strip_crs(filename);
1467 if (ok == 0 && headers && body) {
1468 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1470 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1471 procmsg_msginfo_free(cached);
1473 } else if (ok == -1) {
1474 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1476 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1477 procmsg_msginfo_free(cached);
1483 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1485 gchar *path, *filename;
1487 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1492 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1493 procmsg_msginfo_free(cached);
1496 path = folder_item_get_path(item);
1497 if (!is_dir_exist(path))
1500 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1502 if (is_file_exist(filename)) {
1503 if (cached && cached->total_size == cached->size) {
1506 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1509 size = get_file_size_with_crs(filename);
1512 if (cached && size >= cached->size) {
1513 cached->total_size = cached->size;
1514 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1515 procmsg_msginfo_free(cached);
1519 procmsg_msginfo_free(cached);
1523 void imap_cache_msg(FolderItem *item, gint msgnum)
1525 Folder *folder = NULL;
1529 folder = item->folder;
1531 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1532 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1533 debug_print("fetched %s\n", tmp);
1538 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1539 const gchar *file, MsgFlags *flags)
1543 MsgFileInfo fileinfo;
1545 g_return_val_if_fail(file != NULL, -1);
1547 fileinfo.msginfo = NULL;
1548 fileinfo.file = (gchar *)file;
1549 fileinfo.flags = flags;
1550 file_list.data = &fileinfo;
1551 file_list.next = NULL;
1553 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1557 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1558 GRelation *relation)
1561 IMAPSession *session;
1562 guint32 last_uid = 0;
1564 MsgFileInfo *fileinfo;
1565 gint ok = MAILIMAP_NO_ERROR;
1566 gint curnum = 0, total = 0;
1567 gboolean missing_uids = FALSE;
1569 g_return_val_if_fail(folder != NULL, -1);
1570 g_return_val_if_fail(dest != NULL, -1);
1571 g_return_val_if_fail(file_list != NULL, -1);
1573 debug_print("getting session...\n");
1574 session = imap_session_get(folder);
1578 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1581 statusbar_print_all(_("Adding messages..."));
1582 total = g_slist_length(file_list);
1583 for (cur = file_list; cur != NULL; cur = cur->next) {
1584 IMAPFlags iflags = 0;
1585 guint32 new_uid = 0;
1586 gchar *real_file = NULL;
1587 fileinfo = (MsgFileInfo *)cur->data;
1589 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1592 if (fileinfo->flags) {
1593 if (MSG_IS_MARKED(*fileinfo->flags))
1594 iflags |= IMAP_FLAG_FLAGGED;
1595 if (MSG_IS_REPLIED(*fileinfo->flags))
1596 iflags |= IMAP_FLAG_ANSWERED;
1597 if (MSG_IS_FORWARDED(*fileinfo->flags))
1598 iflags |= IMAP_FLAG_FORWARDED;
1599 if (!MSG_IS_UNREAD(*fileinfo->flags))
1600 iflags |= IMAP_FLAG_SEEN;
1604 if (real_file == NULL)
1605 real_file = g_strdup(fileinfo->file);
1607 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1608 folder_has_parent_of_type(dest, F_OUTBOX) ||
1609 folder_has_parent_of_type(dest, F_DRAFT) ||
1610 folder_has_parent_of_type(dest, F_TRASH))
1611 iflags |= IMAP_FLAG_SEEN;
1613 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1616 if (ok != MAILIMAP_NO_ERROR) {
1617 g_warning("can't append message %s\n", real_file);
1620 statusbar_progress_all(0,0,0);
1621 statusbar_pop_all();
1624 debug_print("appended new message as %d\n", new_uid);
1625 /* put the local file in the imapcache, so that we don't
1626 * have to fetch it back later. */
1629 missing_uids = TRUE;
1630 debug_print("Missing UID (0)\n");
1633 gchar *cache_path = folder_item_get_path(dest);
1634 if (!is_dir_exist(cache_path))
1635 make_dir_hier(cache_path);
1636 if (is_dir_exist(cache_path)) {
1637 gchar *cache_file = g_strconcat(
1638 cache_path, G_DIR_SEPARATOR_S,
1639 itos(new_uid), NULL);
1640 copy_file(real_file, cache_file, TRUE);
1641 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1648 if (relation != NULL)
1649 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1650 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1651 GINT_TO_POINTER(new_uid));
1652 if (last_uid < new_uid) {
1659 statusbar_progress_all(0,0,0);
1660 statusbar_pop_all();
1665 imap_scan_required(folder, dest);
1667 session = imap_session_get(folder);
1673 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1674 &a, NULL, NULL, NULL, NULL, FALSE);
1679 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1681 GSList *result = NULL;
1686 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1687 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1688 start = item->set_first;
1689 end = item->set_last;
1690 for (t = start; t <= end; t++) {
1691 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1694 result = g_slist_reverse(result);
1695 if (debug_get_mode()) {
1696 debug_print("flat imap set: ");
1697 for (cur = result; cur; cur = cur->next) {
1698 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1705 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1706 MsgInfoList *msglist, GRelation *relation)
1710 GSList *seq_list, *cur;
1712 IMAPSession *session;
1713 gint ok = MAILIMAP_NO_ERROR;
1714 GRelation *uid_mapping;
1716 gboolean single = FALSE;
1718 g_return_val_if_fail(folder != NULL, -1);
1719 g_return_val_if_fail(dest != NULL, -1);
1720 g_return_val_if_fail(msglist != NULL, -1);
1722 debug_print("getting session...\n");
1723 session = imap_session_get(folder);
1729 msginfo = (MsgInfo *)msglist->data;
1730 if (msglist->next == NULL)
1732 src = msginfo->folder;
1734 g_warning("the src folder is identical to the dest.\n");
1738 if (src->folder != dest->folder) {
1739 GSList *infolist = NULL, *cur;
1741 for (cur = msglist; cur; cur = cur->next) {
1742 msginfo = (MsgInfo *)cur->data;
1743 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1744 fileinfo->file = procmsg_get_message_file(msginfo);
1745 fileinfo->flags = &(msginfo->flags);
1746 infolist = g_slist_prepend(infolist, fileinfo);
1748 infolist = g_slist_reverse(infolist);
1749 res = folder_item_add_msgs(dest, infolist, FALSE);
1750 for (cur = infolist; cur; cur = cur->next) {
1751 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1755 g_slist_free(infolist);
1759 lock_session(session); /* unlocked later in the function */
1761 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1762 NULL, NULL, NULL, NULL, NULL, FALSE);
1763 if (ok != MAILIMAP_NO_ERROR) {
1767 unlock_session(session);
1769 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1774 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1775 uid_mapping = g_relation_new(2);
1776 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1778 statusbar_print_all(_("Copying messages..."));
1779 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1780 struct mailimap_set * seq_set;
1781 struct mailimap_set * source = NULL;
1782 struct mailimap_set * dest = NULL;
1783 seq_set = cur->data;
1785 debug_print("Copying messages from %s to %s ...\n",
1786 src->path, destdir);
1788 lock_session(session); /* unlocked later in the function */
1789 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1796 if (ok == MAILIMAP_NO_ERROR) {
1797 unlock_session(session);
1798 if (relation && source && dest) {
1799 GSList *s_list = flatten_mailimap_set(source);
1800 GSList *d_list = flatten_mailimap_set(dest);
1801 GSList *s_cur, *d_cur;
1802 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1804 for (s_cur = s_list, d_cur = d_list;
1806 s_cur = s_cur->next, d_cur = d_cur->next) {
1807 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1811 debug_print("hhhmm, source list length != dest list length.\n");
1813 g_slist_free(s_list);
1814 g_slist_free(d_list);
1820 mailimap_set_free(source);
1822 mailimap_set_free(dest);
1824 if (ok != MAILIMAP_NO_ERROR) {
1825 g_relation_destroy(uid_mapping);
1826 imap_lep_set_free(seq_list);
1827 statusbar_pop_all();
1832 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1833 MsgInfo *msginfo = (MsgInfo *)cur->data;
1836 tuples = g_relation_select(uid_mapping,
1837 GINT_TO_POINTER(msginfo->msgnum),
1839 if (tuples->len > 0) {
1840 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1841 g_relation_insert(relation, msginfo,
1842 GINT_TO_POINTER(num));
1845 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1846 /* put the local file in the imapcache, so that we don't
1847 * have to fetch it back later. */
1849 gchar *cache_path = folder_item_get_path(msginfo->folder);
1850 gchar *real_file = g_strconcat(
1851 cache_path, G_DIR_SEPARATOR_S,
1852 itos(msginfo->msgnum), NULL);
1853 gchar *cache_file = NULL;
1855 cache_path = folder_item_get_path(dest);
1856 cache_file = g_strconcat(
1857 cache_path, G_DIR_SEPARATOR_S,
1859 if (!is_dir_exist(cache_path))
1860 make_dir_hier(cache_path);
1861 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1862 copy_file(real_file, cache_file, TRUE);
1863 debug_print("copied to cache: %s\n", cache_file);
1870 g_relation_insert(relation, msginfo,
1871 GINT_TO_POINTER(0));
1872 g_tuples_destroy(tuples);
1874 statusbar_pop_all();
1876 g_relation_destroy(uid_mapping);
1877 imap_lep_set_free(seq_list);
1881 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1882 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1883 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1884 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1886 imap_scan_required(folder, dest);
1887 if (ok == MAILIMAP_NO_ERROR)
1893 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1897 g_return_val_if_fail(msginfo != NULL, -1);
1899 msglist.data = msginfo;
1900 msglist.next = NULL;
1902 return imap_copy_msgs(folder, dest, &msglist, NULL);
1905 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1906 MsgInfoList *msglist, GRelation *relation)
1911 g_return_val_if_fail(folder != NULL, -1);
1912 g_return_val_if_fail(dest != NULL, -1);
1913 g_return_val_if_fail(msglist != NULL, -1);
1915 msginfo = (MsgInfo *)msglist->data;
1916 g_return_val_if_fail(msginfo->folder != NULL, -1);
1918 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1923 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1924 MsgInfoList *msglist, GRelation *relation)
1926 gchar *destdir, *dir;
1927 GSList *numlist = NULL, *cur;
1929 IMAPSession *session;
1930 gint ok = MAILIMAP_NO_ERROR;
1931 GRelation *uid_mapping;
1933 g_return_val_if_fail(folder != NULL, -1);
1934 g_return_val_if_fail(dest != NULL, -1);
1935 g_return_val_if_fail(msglist != NULL, -1);
1937 debug_print("getting session...\n");
1938 session = imap_session_get(folder);
1943 lock_session(session); /* unlocked later in the function */
1945 msginfo = (MsgInfo *)msglist->data;
1947 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1948 NULL, NULL, NULL, NULL, NULL, FALSE);
1949 if (ok != MAILIMAP_NO_ERROR) {
1953 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1956 for (cur = msglist; cur; cur = cur->next) {
1957 msginfo = (MsgInfo *)cur->data;
1958 if (!MSG_IS_DELETED(msginfo->flags))
1959 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1961 numlist = g_slist_reverse(numlist);
1963 uid_mapping = g_relation_new(2);
1964 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1966 if (numlist != NULL) {
1967 ok = imap_set_message_flags
1968 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1969 if (ok != MAILIMAP_NO_ERROR) {
1970 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1973 } /* else we just need to expunge */
1974 ok = imap_cmd_expunge(session);
1975 if (ok != MAILIMAP_NO_ERROR) {
1976 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1980 session->folder_content_changed = TRUE;
1981 unlock_session(session);
1983 dir = folder_item_get_path(msginfo->folder);
1984 if (is_dir_exist(dir)) {
1985 for (cur = msglist; cur; cur = cur->next) {
1986 msginfo = (MsgInfo *)cur->data;
1987 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1992 g_relation_destroy(uid_mapping);
1993 g_slist_free(numlist);
1995 imap_scan_required(folder, dest);
1998 if (ok == MAILIMAP_NO_ERROR)
2004 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2005 MsgInfoList *msglist, GRelation *relation)
2009 g_return_val_if_fail(folder != NULL, -1);
2010 g_return_val_if_fail(dest != NULL, -1);
2011 if (msglist == NULL)
2014 msginfo = (MsgInfo *)msglist->data;
2015 g_return_val_if_fail(msginfo->folder != NULL, -1);
2017 return imap_do_remove_msgs(folder, dest, msglist, relation);
2020 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2022 GSList *list = folder_item_get_msg_list(item);
2023 gint res = imap_remove_msgs(folder, item, list, NULL);
2024 procmsg_msg_list_free(list);
2028 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2031 /* TODO: properly implement this method */
2035 static gint imap_close(Folder *folder, FolderItem *item)
2040 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2042 FolderItem *item = NULL;
2043 IMAPSession *session;
2044 gchar *root_folder = NULL;
2046 g_return_val_if_fail(folder != NULL, -1);
2047 g_return_val_if_fail(folder->account != NULL, -1);
2049 debug_print("getting session...\n");
2050 session = imap_session_get(folder);
2052 if (!folder->node) {
2053 folder_tree_destroy(folder);
2054 item = folder_item_new(folder, folder->name, NULL);
2055 item->folder = folder;
2056 folder->node = item->node = g_node_new(item);
2061 if (folder->account->imap_dir && *folder->account->imap_dir) {
2066 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2067 extract_quote(root_folder, '"');
2068 subst_char(root_folder,
2069 imap_get_path_separator(session, IMAP_FOLDER(folder),
2074 strtailchomp(root_folder, '/');
2075 real_path = imap_get_real_path
2076 (session, IMAP_FOLDER(folder), root_folder, &r);
2079 debug_print("IMAP root directory: %s\n", real_path);
2081 /* check if root directory exist */
2083 r = imap_threaded_list(session->folder, "", real_path,
2086 if (r != MAILIMAP_NO_ERROR)
2087 imap_handle_error(SESSION(session), r);
2089 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2090 if (!folder->node) {
2091 item = folder_item_new(folder, folder->name, NULL);
2092 item->folder = folder;
2093 folder->node = item->node = g_node_new(item);
2097 mailimap_list_result_free(lep_list);
2103 item = FOLDER_ITEM(folder->node->data);
2105 if (item && !item->path && root_folder) {
2106 item->path = g_strdup(root_folder);
2109 if (!item || ((item->path || root_folder) &&
2110 strcmp2(item->path, root_folder) != 0)) {
2111 folder_tree_destroy(folder);
2112 item = folder_item_new(folder, folder->name, root_folder);
2113 item->folder = folder;
2114 folder->node = item->node = g_node_new(item);
2117 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2118 imap_create_missing_folders(folder);
2123 static gint imap_scan_tree(Folder *folder)
2125 gboolean subs_only = FALSE;
2126 if (folder->account) {
2127 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2128 subs_only = folder->account->imap_subsonly;
2130 return imap_scan_tree_real(folder, subs_only);
2133 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2136 IMAPFolder *imapfolder;
2137 FolderItem *new_item;
2138 GSList *item_list, *cur;
2141 gchar *wildcard_path;
2147 g_return_val_if_fail(item != NULL, -1);
2148 g_return_val_if_fail(item->folder != NULL, -1);
2149 g_return_val_if_fail(item->no_sub == FALSE, -1);
2151 folder = item->folder;
2152 imapfolder = IMAP_FOLDER(folder);
2154 separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2158 if (folder->ui_func)
2159 folder->ui_func(folder, item, folder->ui_func_data);
2162 wildcard[0] = separator;
2165 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2171 real_path = g_strdup("");
2174 Xstrcat_a(wildcard_path, real_path, wildcard,
2175 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2179 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2181 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2183 if (r != MAILIMAP_NO_ERROR) {
2184 imap_handle_error(SESSION(session), r);
2189 item_list = imap_list_from_lep(imapfolder,
2190 lep_list, real_path, FALSE);
2191 mailimap_list_result_free(lep_list);
2196 node = item->node->children;
2197 while (node != NULL) {
2198 FolderItem *old_item = FOLDER_ITEM(node->data);
2199 GNode *next = node->next;
2202 for (cur = item_list; cur != NULL; cur = cur->next) {
2203 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2204 if (!strcmp2(old_item->path, cur_item->path)) {
2205 new_item = cur_item;
2210 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2211 debug_print("not removing INBOX\n");
2213 debug_print("folder '%s' not found. removing...\n",
2215 folder_item_remove(old_item);
2218 old_item->no_sub = new_item->no_sub;
2219 old_item->no_select = new_item->no_select;
2220 if (old_item->no_sub == TRUE && node->children) {
2221 debug_print("folder '%s' doesn't have "
2222 "subfolders. removing...\n",
2224 folder_item_remove_children(old_item);
2231 for (cur = item_list; cur != NULL; cur = cur->next) {
2232 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2235 for (node = item->node->children; node != NULL;
2236 node = node->next) {
2237 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2239 new_item = FOLDER_ITEM(node->data);
2240 folder_item_destroy(cur_item);
2246 new_item = cur_item;
2247 debug_print("new folder '%s' found.\n", new_item->path);
2248 folder_item_append(item, new_item);
2251 if (!strcmp(new_item->path, "INBOX")) {
2252 new_item->stype = F_INBOX;
2253 folder->inbox = new_item;
2254 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2257 base = g_path_get_basename(new_item->path);
2259 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2260 new_item->stype = F_OUTBOX;
2261 folder->outbox = new_item;
2262 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2263 new_item->stype = F_DRAFT;
2264 folder->draft = new_item;
2265 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2266 new_item->stype = F_QUEUE;
2267 folder->queue = new_item;
2268 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2269 new_item->stype = F_TRASH;
2270 folder->trash = new_item;
2275 if (new_item->no_sub == FALSE)
2276 imap_scan_tree_recursive(session, new_item, subs_only);
2279 g_slist_free(item_list);
2281 return MAILIMAP_NO_ERROR;
2284 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2286 IMAPSession *session = imap_session_get(folder);
2288 gchar *wildcard_path;
2292 GSList *item_list = NULL, *cur;
2293 GList *child_list = NULL, *tmplist = NULL;
2294 GSList *sub_list = NULL;
2300 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2305 wildcard[0] = separator;
2308 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2314 real_path = g_strdup("");
2317 Xstrcat_a(wildcard_path, real_path, wildcard,
2318 {g_free(real_path); return NULL;});
2322 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2323 item->path?item->path:item->name);
2325 statusbar_print_all(_("Looking for subfolders of %s..."),
2326 item->path?item->path:item->name);
2328 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2330 statusbar_pop_all();
2333 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2334 lep_list, real_path, FALSE);
2335 mailimap_list_result_free(lep_list);
2337 for (cur = item_list; cur != NULL; cur = cur->next) {
2338 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2340 tmplist = imap_scan_subtree(folder, cur_item,
2341 unsubs_only, recursive);
2343 child_list = g_list_concat(child_list, tmplist);
2345 child_list = g_list_prepend(child_list,
2346 imap_get_real_path(session,
2347 IMAP_FOLDER(folder), cur_item->path, &r));
2349 statusbar_pop_all();
2352 folder_item_destroy(cur_item);
2354 child_list = g_list_reverse(child_list);
2355 g_slist_free(item_list);
2358 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2360 statusbar_pop_all();
2363 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2364 lep_list, real_path, FALSE);
2365 mailimap_list_result_free(lep_list);
2367 for (cur = sub_list; cur != NULL; cur = cur->next) {
2368 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2369 GList *oldlitem = NULL;
2370 gchar *tmp = imap_get_real_path(session,
2371 IMAP_FOLDER(folder), cur_item->path, &r);
2373 statusbar_pop_all();
2376 folder_item_destroy(cur_item);
2377 oldlitem = g_list_find_custom(
2378 child_list, tmp, (GCompareFunc)strcmp2);
2380 child_list = g_list_remove_link(child_list, oldlitem);
2381 g_free(oldlitem->data);
2382 g_list_free(oldlitem);
2388 statusbar_pop_all();
2393 static gint imap_create_tree(Folder *folder)
2395 g_return_val_if_fail(folder != NULL, -1);
2396 g_return_val_if_fail(folder->node != NULL, -1);
2397 g_return_val_if_fail(folder->node->data != NULL, -1);
2398 g_return_val_if_fail(folder->account != NULL, -1);
2400 imap_scan_tree(folder);
2401 imap_create_missing_folders(folder);
2406 static void imap_create_missing_folders(Folder *folder)
2408 g_return_if_fail(folder != NULL);
2411 folder->inbox = imap_create_special_folder
2412 (folder, F_INBOX, "INBOX");
2414 folder->trash = imap_create_special_folder
2415 (folder, F_TRASH, "Trash");
2417 folder->queue = imap_create_special_folder
2418 (folder, F_QUEUE, "Queue");
2419 if (!folder->outbox)
2420 folder->outbox = imap_create_special_folder
2421 (folder, F_OUTBOX, "Sent");
2423 folder->draft = imap_create_special_folder
2424 (folder, F_DRAFT, "Drafts");
2427 static FolderItem *imap_create_special_folder(Folder *folder,
2428 SpecialFolderItemType stype,
2432 FolderItem *new_item;
2434 g_return_val_if_fail(folder != NULL, NULL);
2435 g_return_val_if_fail(folder->node != NULL, NULL);
2436 g_return_val_if_fail(folder->node->data != NULL, NULL);
2437 g_return_val_if_fail(folder->account != NULL, NULL);
2438 g_return_val_if_fail(name != NULL, NULL);
2440 item = FOLDER_ITEM(folder->node->data);
2441 new_item = imap_create_folder(folder, item, name);
2444 g_warning("Can't create '%s'\n", name);
2445 if (!folder->inbox) return NULL;
2447 new_item = imap_create_folder(folder, folder->inbox, name);
2449 g_warning("Can't create '%s' under INBOX\n", name);
2451 new_item->stype = stype;
2453 new_item->stype = stype;
2458 static gchar *imap_folder_get_path(Folder *folder)
2462 g_return_val_if_fail(folder != NULL, NULL);
2463 g_return_val_if_fail(folder->account != NULL, NULL);
2465 folder_path = g_strconcat(get_imap_cache_dir(),
2467 folder->account->recv_server,
2469 folder->account->userid,
2475 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2477 gchar *folder_path, *path;
2479 g_return_val_if_fail(folder != NULL, NULL);
2480 g_return_val_if_fail(item != NULL, NULL);
2481 folder_path = imap_folder_get_path(folder);
2483 g_return_val_if_fail(folder_path != NULL, NULL);
2484 if (folder_path[0] == G_DIR_SEPARATOR) {
2486 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2489 path = g_strdup(folder_path);
2492 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2493 folder_path, G_DIR_SEPARATOR_S,
2496 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2499 g_free(folder_path);
2504 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2507 gchar *dirpath, *imap_path;
2508 IMAPSession *session;
2509 FolderItem *new_item;
2514 gboolean no_select = FALSE, no_sub = FALSE;
2515 gboolean exist = FALSE;
2517 g_return_val_if_fail(folder != NULL, NULL);
2518 g_return_val_if_fail(folder->account != NULL, NULL);
2519 g_return_val_if_fail(parent != NULL, NULL);
2520 g_return_val_if_fail(name != NULL, NULL);
2522 debug_print("getting session...\n");
2523 session = imap_session_get(folder);
2528 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2529 dirpath = g_strdup(name);
2530 }else if (parent->path)
2531 dirpath = g_strconcat(parent->path, "/", name, NULL);
2532 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2533 dirpath = g_strdup(name);
2534 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2537 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2538 strtailchomp(imap_dir, '/');
2539 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2541 dirpath = g_strdup(name);
2545 /* keep trailing directory separator to create a folder that contains
2547 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2549 strtailchomp(dirpath, '/');
2550 Xstrdup_a(new_name, name, {
2554 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2557 imap_path_separator_subst(imap_path, separator);
2558 /* remove trailing / for display */
2559 strtailchomp(new_name, '/');
2561 if (strcmp(dirpath, "INBOX") != 0) {
2566 argbuf = g_ptr_array_new();
2567 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2568 if (r != MAILIMAP_NO_ERROR) {
2569 imap_handle_error(SESSION(session), r);
2570 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2573 ptr_array_free_strings(argbuf);
2574 g_ptr_array_free(argbuf, TRUE);
2578 if (clist_count(lep_list) > 0)
2580 mailimap_list_result_free(lep_list);
2583 ok = imap_cmd_create(session, imap_path);
2584 if (ok != MAILIMAP_NO_ERROR) {
2585 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2590 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2591 if (r == MAILIMAP_NO_ERROR) {
2592 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2593 lep_list, dirpath, TRUE);
2595 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2596 no_select = cur_item->no_select;
2597 no_sub = cur_item->no_sub;
2598 g_slist_free(item_list);
2600 mailimap_list_result_free(lep_list);
2602 imap_handle_error(SESSION(session), r);
2605 imap_threaded_subscribe(folder, imap_path, TRUE);
2609 /* just get flags */
2610 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2611 if (r == MAILIMAP_NO_ERROR) {
2612 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2613 lep_list, dirpath, TRUE);
2615 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2616 no_select = cur_item->no_select;
2617 no_sub = cur_item->no_sub;
2618 g_slist_free(item_list);
2620 mailimap_list_result_free(lep_list);
2622 imap_handle_error(SESSION(session), r);
2626 new_item = folder_item_new(folder, new_name, dirpath);
2627 new_item->no_select = no_select;
2628 new_item->no_sub = no_sub;
2629 folder_item_append(parent, new_item);
2633 dirpath = folder_item_get_path(new_item);
2634 if (!is_dir_exist(dirpath))
2635 make_dir_hier(dirpath);
2639 /* folder existed, scan it */
2640 imap_scan_required(folder, new_item);
2641 folder_item_scan_full(new_item, FALSE);
2647 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2652 gchar *real_oldpath;
2653 gchar *real_newpath;
2655 gchar *old_cache_dir;
2656 gchar *new_cache_dir;
2657 IMAPSession *session;
2660 gint exists, recent, unseen;
2661 guint32 uid_validity;
2663 g_return_val_if_fail(folder != NULL, -1);
2664 g_return_val_if_fail(item != NULL, -1);
2665 g_return_val_if_fail(item->path != NULL, -1);
2666 g_return_val_if_fail(name != NULL, -1);
2668 debug_print("getting session...\n");
2669 session = imap_session_get(folder);
2674 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
2676 g_warning(_("New folder name must not contain the namespace "
2681 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2686 g_free(session->mbox);
2687 session->mbox = NULL;
2688 session->exists = 0;
2689 session->recent = 0;
2690 session->expunge = 0;
2691 ok = imap_cmd_examine(session, "INBOX",
2692 &exists, &recent, &unseen, &uid_validity, FALSE);
2693 if (ok != MAILIMAP_NO_ERROR) {
2694 g_free(real_oldpath);
2698 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
2701 if (strchr(item->path, G_DIR_SEPARATOR)) {
2702 dirpath = g_path_get_dirname(item->path);
2703 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2706 newpath = g_strdup(name);
2708 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2709 imap_path_separator_subst(real_newpath, separator);
2711 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2712 if (ok != MAILIMAP_NO_ERROR) {
2713 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2714 real_oldpath, real_newpath);
2715 g_free(real_oldpath);
2717 g_free(real_newpath);
2721 item->name = g_strdup(name);
2723 old_cache_dir = folder_item_get_path(item);
2725 paths[0] = g_strdup(item->path);
2727 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2728 imap_rename_folder_func, paths);
2730 if (is_dir_exist(old_cache_dir)) {
2731 new_cache_dir = folder_item_get_path(item);
2732 if (rename(old_cache_dir, new_cache_dir) < 0) {
2733 FILE_OP_ERROR(old_cache_dir, "rename");
2735 g_free(new_cache_dir);
2738 g_free(old_cache_dir);
2741 g_free(real_oldpath);
2742 g_free(real_newpath);
2746 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2750 IMAPSession *session;
2751 debug_print("getting session...\n");
2753 session = imap_session_get(folder);
2757 if (item && item->path) {
2758 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2759 if (!path || is_fatal(r))
2761 if (!strcmp(path, "INBOX") && sub == FALSE)
2763 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2764 r = imap_threaded_subscribe(folder, path, sub);
2767 r = imap_threaded_subscribe(folder, rpath, sub);
2773 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2776 IMAPSession *session;
2779 gboolean selected_folder;
2781 g_return_val_if_fail(folder != NULL, -1);
2782 g_return_val_if_fail(item != NULL, -1);
2783 g_return_val_if_fail(item->path != NULL, -1);
2785 debug_print("getting session...\n");
2786 session = imap_session_get(folder);
2790 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2794 imap_threaded_subscribe(folder, path, FALSE);
2796 selected_folder = (session->mbox != NULL) &&
2797 (!strcmp(session->mbox, item->path));
2798 if (selected_folder) {
2799 ok = imap_cmd_close(session);
2800 if (ok != MAILIMAP_NO_ERROR) {
2801 debug_print("close err %d\n", ok);
2805 ok = imap_cmd_delete(session, path);
2806 if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
2807 gchar *tmp = g_strdup_printf("%s%c", path,
2808 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
2812 ok = imap_cmd_delete(session, path);
2815 if (ok != MAILIMAP_NO_ERROR) {
2816 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2822 cache_dir = folder_item_get_path(item);
2823 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2824 g_warning("can't remove directory '%s'\n", cache_dir);
2826 folder_item_remove(item);
2830 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2834 g_return_val_if_fail(item != NULL, -1);
2835 g_return_val_if_fail(item->folder != NULL, -1);
2836 g_return_val_if_fail(item->node != NULL, -1);
2838 node = item->node->children;
2839 while (node != NULL) {
2841 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2845 debug_print("IMAP removing %s\n", item->path);
2847 if (imap_remove_all_msg(folder, item) < 0)
2849 return imap_remove_folder_real(folder, item);
2852 typedef struct _uncached_data {
2853 IMAPSession *session;
2855 MsgNumberList *numlist;
2862 static void *imap_get_uncached_messages_thread(void *data)
2864 uncached_data *stuff = (uncached_data *)data;
2865 IMAPSession *session = stuff->session;
2866 FolderItem *item = stuff->item;
2867 MsgNumberList *numlist = stuff->numlist;
2868 GSList *newlist = NULL;
2869 GSList *llast = NULL;
2870 GSList *seq_list, *cur;
2871 gboolean got_alien_tags = FALSE;
2873 debug_print("uncached_messages\n");
2875 if (session == NULL || item == NULL || item->folder == NULL
2876 || FOLDER_CLASS(item->folder) != &imap_class) {
2881 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2882 debug_print("get msgs info\n");
2883 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2884 struct mailimap_set * imapset;
2890 if (session->cancelled)
2893 imapset = cur->data;
2895 r = imap_threaded_fetch_env(session->folder,
2896 imapset, &env_list);
2897 if (r != MAILIMAP_NO_ERROR) {
2898 imap_handle_error(SESSION(session), r);
2906 session_set_access_time(SESSION(session));
2909 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2910 struct imap_fetch_env_info * info;
2912 GSList *tags = NULL, *cur = NULL;
2913 info = carray_get(env_list, i);
2914 tags = carray_get(env_list, i+1);
2915 msginfo = imap_envelope_from_lep(info, item);
2916 if (msginfo == NULL) {
2917 slist_free_strings(tags);
2922 g_slist_free(msginfo->tags);
2923 msginfo->tags = NULL;
2925 for (cur = tags; cur; cur = cur->next) {
2926 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2928 id = tags_get_id_for_str(real_tag);
2930 id = tags_add_tag(real_tag);
2931 got_alien_tags = TRUE;
2933 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2934 msginfo->tags = g_slist_append(
2936 GINT_TO_POINTER(id));
2940 slist_free_strings(tags);
2942 msginfo->folder = item;
2944 llast = newlist = g_slist_append(newlist, msginfo);
2946 llast = g_slist_append(llast, msginfo);
2947 llast = llast->next;
2952 imap_fetch_env_free(env_list);
2955 if (got_alien_tags) {
2957 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2960 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2961 struct mailimap_set * imapset;
2963 imapset = cur->data;
2964 mailimap_set_free(imapset);
2967 session_set_access_time(SESSION(session));
2972 #define MAX_MSG_NUM 50
2974 static GSList *imap_get_uncached_messages(IMAPSession *session,
2976 MsgNumberList *numlist,
2979 GSList *result = NULL;
2981 uncached_data *data = g_new0(uncached_data, 1);
2986 data->total = g_slist_length(numlist);
2987 data->ok = MAILIMAP_NO_ERROR;
2988 debug_print("messages list : %i\n", data->total);
2990 while (cur != NULL) {
2991 GSList * partial_result;
2999 while (count < MAX_MSG_NUM) {
3004 if (newlist == NULL)
3005 llast = newlist = g_slist_append(newlist, p);
3007 llast = g_slist_append(llast, p);
3008 llast = llast->next;
3018 data->session = session;
3020 data->numlist = newlist;
3023 if (prefs_common.work_offline &&
3024 !inc_offline_should_override(FALSE,
3025 _("Claws Mail needs network access in order "
3026 "to access the IMAP server."))) {
3032 (GSList *)imap_get_uncached_messages_thread(data);
3034 if (data->ok != MAILIMAP_NO_ERROR) {
3037 statusbar_progress_all(data->cur,data->total, 1);
3039 g_slist_free(newlist);
3041 result = g_slist_concat(result, partial_result);
3046 statusbar_progress_all(0,0,0);
3047 statusbar_pop_all();
3052 static void imap_delete_all_cached_messages(FolderItem *item)
3056 g_return_if_fail(item != NULL);
3057 g_return_if_fail(item->folder != NULL);
3058 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3060 debug_print("Deleting all cached messages...\n");
3062 dir = folder_item_get_path(item);
3063 if (is_dir_exist(dir))
3064 remove_all_numbered_files(dir);
3067 debug_print("done.\n");
3070 gchar imap_get_path_separator_for_item(FolderItem *item)
3072 Folder *folder = NULL;
3073 IMAPFolder *imap_folder = NULL;
3074 IMAPSession *session = NULL;
3079 folder = item->folder;
3084 imap_folder = IMAP_FOLDER(folder);
3089 debug_print("getting session...");
3090 session = imap_session_get(FOLDER(folder));
3091 result = imap_get_path_separator(session, imap_folder, item->path, &ok);
3095 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder, gint *ok)
3099 gchar separator = '\0';
3101 g_return_val_if_fail(session != NULL, '/');
3102 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3104 if (r != MAILIMAP_NO_ERROR) {
3105 imap_handle_error(SESSION(session), r);
3106 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3111 if (clist_count(lep_list) > 0) {
3112 clistiter * iter = clist_begin(lep_list);
3113 struct mailimap_mailbox_list * mb;
3114 mb = clist_content(iter);
3116 separator = mb->mb_delimiter;
3117 debug_print("got separator: %c\n", folder->last_seen_separator);
3119 *ok = MAILIMAP_NO_ERROR;
3120 mailimap_list_result_free(lep_list);
3124 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3126 gchar separator = '/';
3128 if (folder->last_seen_separator == 0) {
3129 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "", ok);
3132 if (folder->last_seen_separator == 0) {
3133 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX", ok);
3136 if (folder->last_seen_separator != 0) {
3137 debug_print("using separator: %c\n", folder->last_seen_separator);
3138 return folder->last_seen_separator;
3144 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3149 g_return_val_if_fail(folder != NULL, NULL);
3150 g_return_val_if_fail(path != NULL, NULL);
3152 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3153 separator = imap_get_path_separator(session, folder, path, ok);
3154 imap_path_separator_subst(real_path, separator);
3159 static gint imap_set_message_flags(IMAPSession *session,
3160 IMAPFolderItem *item,
3161 MsgNumberList *numlist,
3170 IMAPFolder *folder = NULL;
3171 GSList *sorted_list = NULL;
3173 if (numlist == NULL || session == NULL)
3174 return MAILIMAP_ERROR_BAD_STATE;
3176 folder = IMAP_FOLDER(session->folder);
3178 sorted_list = g_slist_copy(numlist);
3179 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3181 cur = g_slist_last(sorted_list);
3184 total = GPOINTER_TO_INT(cur->data);
3186 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3188 statusbar_print_all(_("Flagging messages..."));
3190 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3191 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3192 struct mailimap_set_item *set_item = clist_content(
3193 clist_begin(imapset->set_list));
3195 statusbar_progress_all(set_item->set_first, total, 1);
3197 ok = imap_cmd_store(session, item, imapset,
3198 flags, tags, is_set);
3199 statusbar_progress_all(set_item->set_last, total, 1);
3200 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3201 /* reduce max set size */
3202 folder->max_set_size /= 2;
3204 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3209 g_slist_free(sorted_list);
3211 statusbar_progress_all(0,0,0);
3212 statusbar_pop_all();
3214 imap_lep_set_free(seq_list);
3219 typedef struct _select_data {
3220 IMAPSession *session;
3225 guint32 *uid_validity;
3229 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3231 gint *exists, gint *recent, gint *unseen,
3232 guint32 *uid_validity, gint *can_create_flags,
3237 gint exists_, recent_, unseen_;
3238 guint32 uid_validity_;
3239 gint can_create_flags_;
3240 const gchar *path = item ? item->path:NULL;
3243 return MAILIMAP_ERROR_BAD_STATE;
3246 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3247 if (session->mbox && strcmp(session->mbox, path) == 0)
3248 return MAILIMAP_NO_ERROR;
3250 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3251 if (session->mbox && strcmp(session->mbox, path) == 0) {
3252 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3253 return MAILIMAP_NO_ERROR;
3263 uid_validity = &uid_validity_;
3264 if (!can_create_flags)
3265 can_create_flags = &can_create_flags_;
3267 g_free(session->mbox);
3268 session->mbox = NULL;
3269 session->exists = 0;
3270 session->recent = 0;
3271 session->expunge = 0;
3273 real_path = imap_get_real_path(session, folder, path, &ok);
3276 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3277 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3278 ok = imap_cmd_select(session, real_path,
3279 exists, recent, unseen, uid_validity, can_create_flags,
3280 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3281 if (ok != MAILIMAP_NO_ERROR) {
3282 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3284 session->mbox = g_strdup(path);
3285 session->folder_content_changed = FALSE;
3286 session->exists = *exists;
3287 session->recent = *recent;
3288 session->expunge = 0;
3289 session->unseen = *unseen;
3290 session->uid_validity = *uid_validity;
3291 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3292 session->exists, session->recent, session->expunge,
3293 session->uid_validity, *can_create_flags);
3295 if (*can_create_flags) {
3296 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3298 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3305 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3306 const gchar *path, IMAPFolderItem *item,
3308 guint32 *uid_next, guint32 *uid_validity,
3309 gint *unseen, gboolean block)
3313 struct mailimap_mailbox_data_status * data_status;
3318 real_path = imap_get_real_path(session, folder, path, &r);
3339 if (session->mbox != NULL &&
3340 !strcmp(session->mbox, item->item.path)) {
3341 r = imap_cmd_close(session);
3342 if (r != MAILIMAP_NO_ERROR) {
3343 debug_print("close err %d\n", r);
3348 r = imap_threaded_status(FOLDER(folder), real_path,
3349 &data_status, mask);
3352 if (r != MAILIMAP_NO_ERROR) {
3353 imap_handle_error(SESSION(session), r);
3354 debug_print("status err %d\n", r);
3358 if (data_status->st_info_list == NULL) {
3359 mailimap_mailbox_data_status_free(data_status);
3360 debug_print("status->st_info_list == NULL\n");
3361 return MAILIMAP_ERROR_BAD_STATE;
3365 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3366 iter = clist_next(iter)) {
3367 struct mailimap_status_info * info;
3369 info = clist_content(iter);
3370 switch (info->st_att) {
3371 case MAILIMAP_STATUS_ATT_MESSAGES:
3373 * messages = info->st_value;
3374 got_values |= 1 << 0;
3378 case MAILIMAP_STATUS_ATT_UIDNEXT:
3380 * uid_next = info->st_value;
3381 got_values |= 1 << 2;
3385 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3387 * uid_validity = info->st_value;
3388 got_values |= 1 << 3;
3392 case MAILIMAP_STATUS_ATT_UNSEEN:
3394 * unseen = info->st_value;
3395 got_values |= 1 << 4;
3400 mailimap_mailbox_data_status_free(data_status);
3402 if (got_values != mask) {
3403 g_warning("status: incomplete values received (%d)\n", got_values);
3405 return MAILIMAP_NO_ERROR;
3408 static void imap_free_capabilities(IMAPSession *session)
3410 slist_free_strings(session->capability);
3411 g_slist_free(session->capability);
3412 session->capability = NULL;
3415 /* low-level IMAP4rev1 commands */
3417 static gint imap_cmd_login(IMAPSession *session,
3418 const gchar *user, const gchar *pass,
3424 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3425 gint ok = MAILIMAP_ERROR_BAD_STATE;
3426 if (imap_has_capability(session, "STARTTLS")) {
3427 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3428 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3429 ok = imap_cmd_starttls(session);
3430 if (ok != MAILIMAP_NO_ERROR) {
3431 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3435 imap_free_capabilities(session);
3436 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3437 imap_handle_error(SESSION(session), r);
3438 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3443 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3444 "server requires TLS, but Claws Mail "
3445 "has been compiled without OpenSSL "
3447 SESSION(session)->server);
3448 return MAILIMAP_ERROR_LOGIN;
3451 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3452 return MAILIMAP_ERROR_LOGIN;
3456 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3458 SESSION(session)->server,
3460 r = imap_threaded_login(session->folder, user, pass, type);
3461 if (r != MAILIMAP_NO_ERROR) {
3462 imap_handle_error(SESSION(session), r);
3463 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3464 SESSION(session)->server);
3467 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3468 SESSION(session)->server);
3469 ok = MAILIMAP_NO_ERROR;
3474 static gint imap_cmd_noop(IMAPSession *session)
3477 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3479 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3480 if (r != MAILIMAP_NO_ERROR) {
3481 imap_handle_error(SESSION(session), r);
3482 debug_print("noop err %d\n", r);
3486 session->folder_content_changed = FALSE;
3488 if ((exists && exists != session->exists)
3489 || (recent && recent != session->recent)
3490 || (expunge && expunge != session->expunge)
3491 || (unseen && unseen != session->unseen)) {
3492 session->folder_content_changed = TRUE;
3494 if (uidnext != 0 && uidnext != session->uid_next) {
3495 session->uid_next = uidnext;
3496 session->folder_content_changed = TRUE;
3498 if (uidval != 0 && uidval != session->uid_validity) {
3499 session->uid_validity = uidval;
3500 session->folder_content_changed = TRUE;
3503 session->exists = exists;
3504 session->recent = recent;
3505 session->expunge = expunge;
3506 session->unseen = unseen;
3508 session_set_access_time(SESSION(session));
3510 return MAILIMAP_NO_ERROR;
3513 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3514 static gint imap_cmd_starttls(IMAPSession *session)
3518 r = imap_threaded_starttls(session->folder,
3519 SESSION(session)->server, SESSION(session)->port);
3520 if (r != MAILIMAP_NO_ERROR) {
3521 imap_handle_error(SESSION(session), r);
3522 debug_print("starttls err %d\n", r);
3525 return MAILIMAP_NO_ERROR;
3529 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3530 gint *exists, gint *recent, gint *unseen,
3531 guint32 *uid_validity, gint *can_create_flags,
3532 GSList **ok_flags, gboolean block)
3536 r = imap_threaded_select(session->folder, folder,
3537 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3538 if (r != MAILIMAP_NO_ERROR) {
3539 imap_handle_error(SESSION(session), r);
3540 debug_print("select err %d\n", r);
3543 return MAILIMAP_NO_ERROR;
3546 static gint imap_cmd_close(IMAPSession *session)
3550 r = imap_threaded_close(session->folder);
3551 if (r != MAILIMAP_NO_ERROR) {
3552 imap_handle_error(SESSION(session), r);
3553 debug_print("close err %d\n", r);
3556 g_free(session->mbox);
3557 session->mbox = NULL;
3558 session->exists = 0;
3559 session->recent = 0;
3560 session->expunge = 0;
3561 return MAILIMAP_NO_ERROR;
3564 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3565 gint *exists, gint *recent, gint *unseen,
3566 guint32 *uid_validity, gboolean block)
3570 r = imap_threaded_examine(session->folder, folder,
3571 exists, recent, unseen, uid_validity);
3572 if (r != MAILIMAP_NO_ERROR) {
3573 imap_handle_error(SESSION(session), r);
3574 debug_print("examine err %d\n", r);
3578 return MAILIMAP_NO_ERROR;
3581 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3585 r = imap_threaded_create(session->folder, folder);
3586 if (r != MAILIMAP_NO_ERROR) {
3587 imap_handle_error(SESSION(session), r);
3591 return MAILIMAP_NO_ERROR;
3594 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3595 const gchar *new_folder)
3599 r = imap_threaded_rename(session->folder, old_folder,
3601 if (r != MAILIMAP_NO_ERROR) {
3602 imap_handle_error(SESSION(session), r);
3606 return MAILIMAP_NO_ERROR;
3609 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3614 r = imap_threaded_delete(session->folder, folder);
3615 if (r != MAILIMAP_NO_ERROR) {
3616 imap_handle_error(SESSION(session), r);
3620 return MAILIMAP_NO_ERROR;
3623 typedef struct _fetch_data {
3624 IMAPSession *session;
3626 const gchar *filename;
3632 static void *imap_cmd_fetch_thread(void *data)
3634 fetch_data *stuff = (fetch_data *)data;
3635 IMAPSession *session = stuff->session;
3636 guint32 uid = stuff->uid;
3637 const gchar *filename = stuff->filename;
3641 r = imap_threaded_fetch_content(session->folder,
3645 r = imap_threaded_fetch_content(session->folder,
3648 if (r != MAILIMAP_NO_ERROR) {
3649 imap_handle_error(SESSION(session), r);
3650 debug_print("fetch err %d\n", r);
3651 return GINT_TO_POINTER(r);
3653 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3656 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3657 const gchar *filename, gboolean headers,
3660 fetch_data *data = g_new0(fetch_data, 1);
3663 data->session = session;
3665 data->filename = filename;
3666 data->headers = headers;
3669 if (prefs_common.work_offline &&
3670 !inc_offline_should_override(FALSE,
3671 _("Claws Mail needs network access in order "
3672 "to access the IMAP server."))) {
3676 statusbar_print_all(_("Fetching message..."));
3677 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3678 statusbar_pop_all();
3684 static gint imap_cmd_append(IMAPSession *session,
3685 IMAPFolderItem *item,
3686 const gchar *destfolder,
3687 const gchar *file, IMAPFlags flags,
3690 struct mailimap_flag_list * flag_list;
3693 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3695 flag_list = imap_flag_to_lep(item, flags, NULL);
3696 lock_session(session);
3697 r = imap_threaded_append(session->folder, destfolder,
3698 file, flag_list, (int *)new_uid);
3699 mailimap_flag_list_free(flag_list);
3701 if (r != MAILIMAP_NO_ERROR) {
3702 imap_handle_error(SESSION(session), r);
3703 debug_print("append err %d\n", r);
3707 unlock_session(session);
3709 return MAILIMAP_NO_ERROR;
3712 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3713 const gchar *destfolder, GRelation *uid_mapping,
3714 struct mailimap_set **source, struct mailimap_set **dest)
3718 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3719 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3720 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3722 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3723 if (r != MAILIMAP_NO_ERROR) {
3724 imap_handle_error(SESSION(session), r);
3728 return MAILIMAP_NO_ERROR;
3731 static gint imap_cmd_store(IMAPSession *session,
3732 IMAPFolderItem *item,
3733 struct mailimap_set * set,
3734 IMAPFlags flags, GSList *tags, int do_add)
3737 struct mailimap_flag_list * flag_list = NULL;
3738 struct mailimap_store_att_flags * store_att_flags;
3740 flag_list = imap_flag_to_lep(item, flags, tags);
3744 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3747 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3749 r = imap_threaded_store(session->folder, set, store_att_flags);
3750 mailimap_store_att_flags_free(store_att_flags);
3751 if (r != MAILIMAP_NO_ERROR) {
3752 imap_handle_error(SESSION(session), r);
3756 return MAILIMAP_NO_ERROR;
3759 static gint imap_cmd_expunge(IMAPSession *session)
3763 if (prefs_common.work_offline &&
3764 !inc_offline_should_override(FALSE,
3765 _("Claws Mail needs network access in order "
3766 "to access the IMAP server."))) {
3770 r = imap_threaded_expunge(session->folder);
3771 if (r != MAILIMAP_NO_ERROR) {
3772 imap_handle_error(SESSION(session), r);
3776 return MAILIMAP_NO_ERROR;
3779 static void imap_path_separator_subst(gchar *str, gchar separator)
3782 gboolean in_escape = FALSE;
3784 if (!separator || separator == '/') return;
3786 for (p = str; *p != '\0'; p++) {
3787 if (*p == '/' && !in_escape)
3789 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3791 else if (*p == '-' && in_escape)
3796 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3798 FolderItem *item = node->data;
3799 gchar **paths = data;
3800 const gchar *oldpath = paths[0];
3801 const gchar *newpath = paths[1];
3802 gchar *real_oldpath, *real_newpath;
3804 gchar *new_itempath;
3806 IMAPSession *session = imap_session_get(item->folder);
3808 oldpathlen = strlen(oldpath);
3809 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3810 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3814 base = item->path + oldpathlen;
3815 while (*base == G_DIR_SEPARATOR) base++;
3817 new_itempath = g_strdup(newpath);
3819 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3822 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3824 item->path = new_itempath;
3826 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3828 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3829 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3831 g_free(real_oldpath);
3832 g_free(real_newpath);
3836 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3838 GSList *uidlist, *elem;
3840 clist * lep_uidlist;
3841 gint ok, nummsgs = 0, lastuid_old;
3843 if (session == NULL) {
3847 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3848 NULL, NULL, NULL, NULL, NULL, TRUE);
3849 if (ok != MAILIMAP_NO_ERROR) {
3853 g_slist_free(item->uid_list);
3854 item->uid_list = NULL;
3858 if (folder->account && folder->account->low_bandwidth) {
3859 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3863 if (r == MAILIMAP_NO_ERROR) {
3864 GSList * fetchuid_list =
3865 imap_uid_list_from_lep(lep_uidlist);
3866 mailimap_search_result_free(lep_uidlist);
3868 uidlist = g_slist_concat(fetchuid_list, uidlist);
3870 carray * lep_uidtab;
3871 if (r != -1) { /* inited */
3872 imap_handle_error(SESSION(session), r);
3876 r = imap_threaded_fetch_uid(folder, 1,
3878 if (r == MAILIMAP_NO_ERROR) {
3879 GSList * fetchuid_list =
3880 imap_uid_list_from_lep_tab(lep_uidtab);
3881 imap_fetch_uid_list_free(lep_uidtab);
3882 uidlist = g_slist_concat(fetchuid_list, uidlist);
3886 if (r != MAILIMAP_NO_ERROR) {
3887 imap_handle_error(SESSION(session), r);
3891 lastuid_old = item->lastuid;
3893 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3896 msgnum = GPOINTER_TO_INT(elem->data);
3898 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3899 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3902 g_slist_free(uidlist);
3904 unlock_session(session); /* locked from imap_get_num_list */
3910 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3912 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3913 IMAPSession *session;
3915 GSList *uidlist = NULL;
3917 gboolean selected_folder;
3918 gint known_list_len = 0;
3919 debug_print("get_num_list\n");
3921 g_return_val_if_fail(folder != NULL, -1);
3922 g_return_val_if_fail(item != NULL, -1);
3923 g_return_val_if_fail(item->item.path != NULL, -1);
3924 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3925 g_return_val_if_fail(folder->account != NULL, -1);
3927 known_list_len = g_slist_length(item->uid_list);
3928 if (!item->should_update) {
3929 debug_print("get_num_list: nothing to update\n");
3930 *old_uids_valid = TRUE;
3931 if (known_list_len == item->item.total_msgs
3932 && known_list_len > 0) {
3933 *msgnum_list = g_slist_copy(item->uid_list);
3934 return known_list_len;
3936 debug_print("don't know the list length...\n");
3940 if (prefs_common.work_offline &&
3941 !inc_offline_should_override(FALSE,
3942 _("Claws Mail needs network access in order "
3943 "to access the IMAP server."))) {
3947 debug_print("getting session...\n");
3948 session = imap_session_get(folder);
3949 g_return_val_if_fail(session != NULL, -1);
3951 lock_session(session); /* unlocked by get_list_of_uids */
3952 if (FOLDER_ITEM(item)->path)
3953 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3954 FOLDER_ITEM(item)->folder->name,
3956 FOLDER_ITEM(item)->path);
3958 statusbar_print_all(_("Scanning folder %s ..."),
3959 FOLDER_ITEM(item)->folder->name);
3961 selected_folder = (session->mbox != NULL) &&
3962 (!strcmp(session->mbox, item->item.path));
3964 if (item->should_trash_cache) {
3965 *old_uids_valid = FALSE;
3966 debug_print("get_num_list: trashing num list\n");
3967 debug_print("Freeing imap uid cache\n");
3969 g_slist_free(item->uid_list);
3970 item->uid_list = NULL;
3972 imap_delete_all_cached_messages((FolderItem *)item);
3974 debug_print("get_num_list: updating num list\n");
3975 *old_uids_valid = TRUE;
3978 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3979 /* session could be broken now, in case of fatal error */
3981 debug_print("get_num_list: got %d msgs\n", nummsgs);
3984 statusbar_pop_all();
3988 *msgnum_list = uidlist;
3990 dir = folder_item_get_path((FolderItem *)item);
3991 debug_print("removing old messages from %s\n", dir);
3992 remove_numbered_files_not_in_list(dir, *msgnum_list);
3995 debug_print("get_num_list - ok - %i\n", nummsgs);
3996 statusbar_pop_all();
3997 item->should_trash_cache = FALSE;
3998 item->should_update = FALSE;
4002 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
4007 flags.perm_flags = MSG_NEW|MSG_UNREAD;
4008 flags.tmp_flags = 0;
4010 g_return_val_if_fail(item != NULL, NULL);
4011 g_return_val_if_fail(file != NULL, NULL);
4013 if (folder_has_parent_of_type(item, F_QUEUE)) {
4014 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
4015 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
4016 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
4019 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
4020 if (!msginfo) return NULL;
4022 msginfo->plaintext_file = g_strdup(file);
4023 msginfo->folder = item;
4028 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
4029 GSList *msgnum_list)
4031 IMAPSession *session;
4032 MsgInfoList *ret = NULL;
4035 debug_print("get_msginfos\n");
4037 g_return_val_if_fail(folder != NULL, NULL);
4038 g_return_val_if_fail(item != NULL, NULL);
4039 g_return_val_if_fail(msgnum_list != NULL, NULL);
4041 debug_print("getting session...\n");
4042 session = imap_session_get(folder);
4043 g_return_val_if_fail(session != NULL, NULL);
4045 lock_session(session); /* unlocked later in the function */
4047 debug_print("IMAP getting msginfos\n");
4048 ok = imap_select(session, IMAP_FOLDER(folder), item,
4049 NULL, NULL, NULL, NULL, NULL, FALSE);
4050 if (ok != MAILIMAP_NO_ERROR) {
4053 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4054 folder_has_parent_of_type(item, F_QUEUE))) {
4055 ret = g_slist_concat(ret,
4056 imap_get_uncached_messages(session, item,
4058 if (ok != MAILIMAP_NO_ERROR)
4060 unlock_session(session);
4062 MsgNumberList *sorted_list, *elem, *llast = NULL;
4063 gint startnum, lastnum;
4065 unlock_session(session);
4067 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4069 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4071 llast = g_slist_last(ret);
4072 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4076 num = GPOINTER_TO_INT(elem->data);
4078 if (num > lastnum + 1 || elem == NULL) {
4080 for (i = startnum; i <= lastnum; ++i) {
4082 file = imap_fetch_msg(folder, item, i);
4084 MsgInfo *msginfo = imap_parse_msg(file, item);
4085 if (msginfo != NULL) {
4086 msginfo->msgnum = i;
4088 llast = ret = g_slist_append(ret, msginfo);
4090 llast = g_slist_append(llast, msginfo);
4091 llast = llast->next;
4106 g_slist_free(sorted_list);
4111 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4113 MsgInfo *msginfo = NULL;
4114 MsgInfoList *msginfolist;
4115 MsgNumberList numlist;
4117 numlist.next = NULL;
4118 numlist.data = GINT_TO_POINTER(uid);
4120 msginfolist = imap_get_msginfos(folder, item, &numlist);
4121 if (msginfolist != NULL) {
4122 msginfo = msginfolist->data;
4123 g_slist_free(msginfolist);
4129 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4131 IMAPSession *session;
4132 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4133 gint ok, exists = 0, unseen = 0;
4134 guint32 uid_next = 0, uid_val = 0;
4135 gboolean selected_folder;
4137 g_return_val_if_fail(folder != NULL, FALSE);
4138 g_return_val_if_fail(item != NULL, FALSE);
4139 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4140 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4142 if (item->item.path == NULL)
4145 if (item->should_update) {
4146 debug_print("scan already required\n");
4149 debug_print("getting session...\n");
4150 session = imap_session_get(folder);
4152 g_return_val_if_fail(session != NULL, FALSE);
4153 lock_session(session); /* unlocked later in the function */
4155 selected_folder = (session->mbox != NULL) &&
4156 (!strcmp(session->mbox, item->item.path));
4157 if (selected_folder) {
4158 if (!session->folder_content_changed) {
4159 ok = imap_cmd_noop(session);
4160 if (ok != MAILIMAP_NO_ERROR) {
4161 debug_print("disconnected!\n");
4163 session = imap_reconnect_if_possible(folder, session);
4165 session = imap_session_get(folder);
4166 if (session == NULL)
4170 if (session->folder_content_changed) {
4171 debug_print("CHANGED (self-noop)! scan_required\n");
4172 item->should_update = TRUE;
4173 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4174 item->item.mtime = session->uid_validity;
4175 item->should_trash_cache = TRUE;
4177 unlock_session(session);
4181 debug_print("CHANGED (previous noop)! scan_required\n");
4182 item->should_update = TRUE;
4183 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4184 item->item.mtime = session->uid_validity;
4185 item->should_trash_cache = TRUE;
4187 unlock_session(session);
4191 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4192 &exists, &uid_next, &uid_val, &unseen, FALSE);
4193 if (ok != MAILIMAP_NO_ERROR) {
4197 debug_print("exists %d, item->item.total_msgs %d\n",
4198 exists, item->item.total_msgs);
4199 if (exists != item->item.total_msgs
4200 || unseen != item->item.unread_msgs
4201 || uid_next != item->uid_next
4202 || uid_val != item->item.mtime) {
4203 debug_print("CHANGED (status)! scan_required\n");
4204 item->last_change = time(NULL);
4205 item->should_update = TRUE;
4206 item->uid_next = uid_next;
4207 if (uid_val != item->item.mtime) {
4208 item->item.mtime = uid_val;
4209 item->should_trash_cache = TRUE;
4211 unlock_session(session);
4215 unlock_session(session);
4217 item->should_update = FALSE;
4221 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4223 IMAPSession *session;
4224 IMAPFlags flags_set = 0, flags_unset = 0;
4225 gint ok = MAILIMAP_NO_ERROR;
4226 MsgNumberList numlist;
4227 hashtable_data *ht_data = NULL;
4229 g_return_if_fail(folder != NULL);
4230 g_return_if_fail(folder->klass == &imap_class);
4231 g_return_if_fail(item != NULL);
4232 g_return_if_fail(item->folder == folder);
4233 g_return_if_fail(msginfo != NULL);
4234 g_return_if_fail(msginfo->folder == item);
4236 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4237 flags_set |= IMAP_FLAG_FLAGGED;
4238 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4239 flags_unset |= IMAP_FLAG_FLAGGED;
4241 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4242 flags_unset |= IMAP_FLAG_SEEN;
4243 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4244 flags_set |= IMAP_FLAG_SEEN;
4246 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4247 flags_set |= IMAP_FLAG_ANSWERED;
4248 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4249 flags_unset |= IMAP_FLAG_ANSWERED;
4251 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4252 flags_set |= IMAP_FLAG_FORWARDED;
4253 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4254 flags_unset |= IMAP_FLAG_FORWARDED;
4256 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4257 flags_set |= IMAP_FLAG_DELETED;
4258 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4259 flags_unset |= IMAP_FLAG_DELETED;
4261 if (!flags_set && !flags_unset) {
4262 /* the changed flags were not translatable to IMAP-speak.
4263 * like MSG_POSTFILTERED, so just apply. */
4264 msginfo->flags.perm_flags = newflags;
4268 debug_print("getting session...\n");
4269 session = imap_session_get(folder);
4274 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4275 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4278 numlist.next = NULL;
4279 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4281 if (IMAP_FOLDER_ITEM(item)->batching) {
4282 /* instead of performing an UID STORE command for each message change,
4283 * as a lot of them can change "together", we just fill in hashtables
4284 * and defer the treatment so that we're able to send only one
4287 debug_print("IMAP batch mode on, deferring flags change\n");
4289 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4290 GINT_TO_POINTER(flags_set));
4291 if (ht_data == NULL) {
4292 ht_data = g_new0(hashtable_data, 1);
4293 ht_data->item = IMAP_FOLDER_ITEM(item);
4294 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4295 GINT_TO_POINTER(flags_set), ht_data);
4297 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4300 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4301 GINT_TO_POINTER(flags_unset));
4302 if (ht_data == NULL) {
4303 ht_data = g_new0(hashtable_data, 1);
4304 ht_data->item = IMAP_FOLDER_ITEM(item);
4305 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4306 GINT_TO_POINTER(flags_unset), ht_data);
4308 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4309 GINT_TO_POINTER(msginfo->msgnum));
4312 debug_print("IMAP changing flags\n");
4314 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4315 if (ok != MAILIMAP_NO_ERROR) {
4321 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4322 if (ok != MAILIMAP_NO_ERROR) {
4327 msginfo->flags.perm_flags = newflags;
4331 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4334 IMAPSession *session;
4336 MsgNumberList numlist;
4338 g_return_val_if_fail(folder != NULL, -1);
4339 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4340 g_return_val_if_fail(item != NULL, -1);
4342 debug_print("getting session...\n");
4343 session = imap_session_get(folder);
4344 if (!session) return -1;
4346 ok = imap_select(session, IMAP_FOLDER(folder), item,
4347 NULL, NULL, NULL, NULL, NULL, FALSE);
4348 if (ok != MAILIMAP_NO_ERROR) {
4351 numlist.next = NULL;
4352 numlist.data = GINT_TO_POINTER(uid);
4354 ok = imap_set_message_flags
4355 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4356 if (ok != MAILIMAP_NO_ERROR) {
4357 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4361 if (!session->uidplus) {
4362 ok = imap_cmd_expunge(session);
4366 uidstr = g_strdup_printf("%u", uid);
4367 ok = imap_cmd_expunge(session);
4370 if (ok != MAILIMAP_NO_ERROR) {
4371 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4375 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4376 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4377 dir = folder_item_get_path(item);
4378 if (is_dir_exist(dir))
4379 remove_numbered_files(dir, uid, uid);
4381 return MAILIMAP_NO_ERROR;
4384 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4386 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4389 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4393 g_return_val_if_fail(list != NULL, -1);
4395 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4396 if (GPOINTER_TO_INT(elem->data) >= num)
4399 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4402 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4404 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4405 debug_print("flag %d is OK\n", flag);
4408 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4409 debug_print("creating flags is OK\n");
4416 * NEW and DELETED flags are not syncronized
4417 * - The NEW/RECENT flags in IMAP folders can not really be directly
4418 * modified by Sylpheed
4419 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4420 * meaning, in IMAP it always removes the messages from the FolderItem
4421 * in Sylpheed it can mean to move the message to trash
4424 typedef struct _get_flags_data {
4427 MsgInfoList *msginfo_list;
4428 GRelation *msgflags;
4429 gboolean full_search;
4433 static /*gint*/ void *imap_get_flags_thread(void *data)
4435 get_flags_data *stuff = (get_flags_data *)data;
4436 Folder *folder = stuff->folder;
4437 FolderItem *fitem = (FolderItem *) stuff->item;
4438 MsgInfoList *msginfo_list = stuff->msginfo_list;
4439 GRelation *msgflags = stuff->msgflags;
4441 carray * lep_uidtab;
4442 IMAPSession *session;
4444 int r = MAILIMAP_NO_ERROR;
4445 GHashTable *flags_hash = NULL;
4446 GHashTable *tags_hash = NULL;
4447 gboolean full_search = stuff->full_search;
4448 GSList *sorted_list = NULL;
4449 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL;
4450 GSList *seq_list, *cur;
4451 gboolean reverse_seen = FALSE;
4452 gboolean selected_folder;
4453 gint exists_cnt, unseen_cnt;
4454 gboolean got_alien_tags = FALSE;
4456 session = imap_session_get(folder);
4458 if (session == NULL) {
4460 return GINT_TO_POINTER(-1);
4462 selected_folder = (session->mbox != NULL) &&
4463 (!strcmp(session->mbox, fitem->path));
4465 lock_session(session);
4466 if (!selected_folder) {
4467 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4468 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4469 if (ok != MAILIMAP_NO_ERROR) {
4471 return GINT_TO_POINTER(-1);
4474 if (unseen_cnt > exists_cnt / 2)
4475 reverse_seen = TRUE;
4478 if (fitem->unread_msgs > fitem->total_msgs / 2)
4479 reverse_seen = TRUE;
4482 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4484 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4486 struct mailimap_set * set;
4487 set = mailimap_set_new_interval(1, 0);
4488 seq_list = g_slist_append(NULL, set);
4491 if (folder->account && folder->account->low_bandwidth) {
4492 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4493 struct mailimap_set * imapset;
4494 clist * lep_uidlist;
4497 imapset = cur->data;
4499 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4500 full_search ? NULL:imapset, &lep_uidlist);
4503 r = imap_threaded_search(folder,
4504 IMAP_SEARCH_TYPE_UNSEEN,
4505 full_search ? NULL:imapset, &lep_uidlist);
4507 if (r == MAILIMAP_NO_ERROR) {
4510 uidlist = imap_uid_list_from_lep(lep_uidlist);
4511 mailimap_search_result_free(lep_uidlist);
4513 unseen = g_slist_concat(unseen, uidlist);
4515 imap_handle_error(SESSION(session), r);
4519 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4520 full_search ? NULL:imapset, &lep_uidlist);
4521 if (r == MAILIMAP_NO_ERROR) {
4524 uidlist = imap_uid_list_from_lep(lep_uidlist);
4525 mailimap_search_result_free(lep_uidlist);
4527 flagged = g_slist_concat(flagged, uidlist);
4529 imap_handle_error(SESSION(session), r);
4533 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4534 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4535 full_search ? NULL:imapset, &lep_uidlist);
4536 if (r == MAILIMAP_NO_ERROR) {
4539 uidlist = imap_uid_list_from_lep(lep_uidlist);
4540 mailimap_search_result_free(lep_uidlist);
4542 answered = g_slist_concat(answered, uidlist);
4544 imap_handle_error(SESSION(session), r);
4548 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4549 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4550 full_search ? NULL:imapset, &lep_uidlist);
4551 if (r == MAILIMAP_NO_ERROR) {
4554 uidlist = imap_uid_list_from_lep(lep_uidlist);
4555 mailimap_search_result_free(lep_uidlist);
4557 forwarded = g_slist_concat(forwarded, uidlist);
4559 imap_handle_error(SESSION(session), r);
4564 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4565 full_search ? NULL:imapset, &lep_uidlist);
4566 if (r == MAILIMAP_NO_ERROR) {
4569 uidlist = imap_uid_list_from_lep(lep_uidlist);
4570 mailimap_search_result_free(lep_uidlist);
4572 deleted = g_slist_concat(deleted, uidlist);
4574 imap_handle_error(SESSION(session), r);
4581 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4582 if (r == MAILIMAP_NO_ERROR) {
4583 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4584 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4585 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4586 imap_fetch_uid_flags_list_free(lep_uidtab);
4588 imap_handle_error(SESSION(session), r);
4594 if (r == MAILIMAP_NO_ERROR)
4595 unlock_session(session);
4597 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4599 MsgPermFlags flags, oldflags;
4602 msginfo = (MsgInfo *) elem->data;
4603 flags = msginfo->flags.perm_flags;
4604 wasnew = (flags & MSG_NEW);
4605 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED);
4607 if (folder->account && folder->account->low_bandwidth) {
4608 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4609 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED);
4611 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4614 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4615 if (gslist_find_next_num(&unseen, msginfo->msgnum) == msginfo->msgnum) {
4616 if (!reverse_seen) {
4617 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4619 flags &= ~(MSG_UNREAD | MSG_NEW);
4623 if (gslist_find_next_num(&flagged, msginfo->msgnum) == msginfo->msgnum)
4624 flags |= MSG_MARKED;
4626 flags &= ~MSG_MARKED;
4628 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4629 if (gslist_find_next_num(&answered, msginfo->msgnum) == msginfo->msgnum)
4630 flags |= MSG_REPLIED;
4632 flags &= ~MSG_REPLIED;
4633 if (gslist_find_next_num(&forwarded, msginfo->msgnum) == msginfo->msgnum)
4634 flags |= MSG_FORWARDED;
4636 flags &= ~MSG_FORWARDED;
4637 if (gslist_find_next_num(&deleted, msginfo->msgnum) == msginfo->msgnum)
4638 flags |= MSG_DELETED;
4640 flags &= ~MSG_DELETED;
4643 if (flags_hash != NULL) {
4645 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4646 GINT_TO_POINTER(msginfo->msgnum)));
4649 if ((flags & MSG_UNREAD) == 0)
4655 if (tags_hash != NULL) {
4656 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4660 g_slist_free(msginfo->tags);
4661 msginfo->tags = NULL;
4663 for (cur = tags; cur; cur = cur->next) {
4664 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4666 id = tags_get_id_for_str(real_tag);
4668 id = tags_add_tag(real_tag);
4669 got_alien_tags = TRUE;
4671 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4672 msginfo->tags = g_slist_append(
4674 GINT_TO_POINTER(id));
4678 slist_free_strings(tags);
4683 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4686 if (got_alien_tags) {
4688 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4692 g_hash_table_destroy(flags_hash);
4694 g_hash_table_destroy(tags_hash);
4696 imap_lep_set_free(seq_list);
4697 g_slist_free(flagged);
4698 g_slist_free(deleted);
4699 g_slist_free(answered);
4700 g_slist_free(forwarded);
4701 g_slist_free(unseen);
4702 g_slist_free(sorted_list);
4705 return GINT_TO_POINTER(0);
4708 static gint imap_get_flags(Folder *folder, FolderItem *item,
4709 MsgInfoList *msginfo_list, GRelation *msgflags)
4712 get_flags_data *data = g_new0(get_flags_data, 1);
4714 data->folder = folder;
4716 data->msginfo_list = msginfo_list;
4717 data->msgflags = msgflags;
4718 data->full_search = FALSE;
4720 GSList *tmp = NULL, *cur;
4722 if (prefs_common.work_offline &&
4723 !inc_offline_should_override(FALSE,
4724 _("Claws Mail needs network access in order "
4725 "to access the IMAP server."))) {
4730 tmp = folder_item_get_msg_list(item);
4732 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4733 data->full_search = TRUE;
4735 for (cur = tmp; cur; cur = cur->next)
4736 procmsg_msginfo_free((MsgInfo *)cur->data);
4740 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4747 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4749 gboolean flags_set = GPOINTER_TO_INT(user_data);
4750 gint flags_value = GPOINTER_TO_INT(key);
4751 hashtable_data *data = (hashtable_data *)value;
4752 IMAPFolderItem *_item = data->item;
4753 FolderItem *item = (FolderItem *)_item;
4754 gint ok = MAILIMAP_ERROR_BAD_STATE;
4755 IMAPSession *session = NULL;
4757 debug_print("getting session...\n");
4758 session = imap_session_get(item->folder);
4760 data->msglist = g_slist_reverse(data->msglist);
4762 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4765 g_slist_length(data->msglist));
4767 lock_session(session);
4769 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4770 NULL, NULL, NULL, NULL, NULL, FALSE);
4772 if (ok == MAILIMAP_NO_ERROR) {
4773 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4774 data->msglist, flags_value, NULL, flags_set);
4776 g_warning("can't select mailbox %s\n", item->path);
4780 unlock_session(session);
4782 g_slist_free(data->msglist);
4787 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4789 gboolean tags_set = GPOINTER_TO_INT(user_data);
4790 TagsData *data = (TagsData *)value;
4791 IMAPFolderItem *_item = data->item;
4792 FolderItem *item = (FolderItem *)_item;
4793 gchar *str = data->str;
4794 gint ok = MAILIMAP_ERROR_BAD_STATE;
4795 IMAPSession *session = NULL;
4797 debug_print("getting session...\n");
4798 session = imap_session_get(item->folder);
4800 data->msglist = g_slist_reverse(data->msglist);
4802 debug_print("IMAP %ssetting tags %s for %d messages\n",
4805 g_slist_length(data->msglist));
4807 lock_session(session);
4809 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4810 NULL, NULL, NULL, NULL, NULL, FALSE);
4812 if (ok == MAILIMAP_NO_ERROR) {
4816 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4817 data->msglist, 0, &list, tags_set);
4819 g_warning("can't select mailbox %s\n", item->path);
4823 unlock_session(session);
4825 g_slist_free(data->msglist);
4831 static void process_hashtable(IMAPFolderItem *item)
4833 if (item->flags_set_table) {
4834 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4835 g_hash_table_destroy(item->flags_set_table);
4836 item->flags_set_table = NULL;
4838 if (item->flags_unset_table) {
4839 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4840 g_hash_table_destroy(item->flags_unset_table);
4841 item->flags_unset_table = NULL;
4843 if (item->tags_set_table) {
4844 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4845 g_hash_table_destroy(item->tags_set_table);
4846 item->tags_set_table = NULL;
4848 if (item->tags_unset_table) {
4849 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4850 g_hash_table_destroy(item->tags_unset_table);
4851 item->tags_unset_table = NULL;
4856 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4858 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4859 IMAPSession *session;
4861 g_return_if_fail(item != NULL);
4863 if (item->batching == batch)
4867 item->batching = TRUE;
4868 debug_print("IMAP switching to batch mode\n");
4869 if (!item->flags_set_table) {
4870 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4872 if (!item->flags_unset_table) {
4873 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4875 if (!item->tags_set_table) {
4876 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4878 if (!item->tags_unset_table) {
4879 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4881 session = imap_session_get(folder);
4883 imap_refresh_sensitivity(session);
4884 session->sens_update_block = TRUE;
4887 debug_print("IMAP switching away from batch mode\n");
4889 process_hashtable(item);
4890 item->batching = FALSE;
4891 session = imap_session_get(folder);
4893 session->sens_update_block = FALSE;
4894 imap_refresh_sensitivity(session);
4901 /* data types conversion libetpan <-> claws */
4905 #define ETPAN_IMAP_MB_MARKED 1
4906 #define ETPAN_IMAP_MB_UNMARKED 2
4907 #define ETPAN_IMAP_MB_NOSELECT 4
4908 #define ETPAN_IMAP_MB_NOINFERIORS 8
4910 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4916 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4917 switch (imap_flags->mbf_sflag) {
4918 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4919 flags |= ETPAN_IMAP_MB_MARKED;
4921 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4922 flags |= ETPAN_IMAP_MB_NOSELECT;
4924 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4925 flags |= ETPAN_IMAP_MB_UNMARKED;
4930 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4931 cur = clist_next(cur)) {
4932 struct mailimap_mbx_list_oflag * oflag;
4934 oflag = clist_content(cur);
4936 switch (oflag->of_type) {
4937 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4938 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4946 static GSList * imap_list_from_lep(IMAPFolder * folder,
4947 clist * list, const gchar * real_path, gboolean all)
4950 GSList * item_list = NULL, *llast = NULL;
4952 for(iter = clist_begin(list) ; iter != NULL ;
4953 iter = clist_next(iter)) {
4954 struct mailimap_mailbox_list * mb;
4962 FolderItem *new_item;
4964 mb = clist_content(iter);
4970 if (mb->mb_flag != NULL)
4971 flags = imap_flags_to_flags(mb->mb_flag);
4973 delimiter = mb->mb_delimiter;
4976 dup_name = strdup(name);
4977 if (delimiter != '\0')
4978 subst_char(dup_name, delimiter, '/');
4980 base = g_path_get_basename(dup_name);
4981 if (base[0] == '.') {
4986 if (!all && path_cmp(name, real_path) == 0) {
4992 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4993 dup_name[strlen(dup_name)-1] = '\0';
4996 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
4997 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
4999 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
5000 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
5001 new_item->no_sub = TRUE;
5002 if (strcmp(dup_name, "INBOX") != 0 &&
5003 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
5004 new_item->no_select = TRUE;
5006 if (item_list == NULL)
5007 llast = item_list = g_slist_append(item_list, new_item);
5009 llast = g_slist_append(llast, new_item);
5010 llast = llast->next;
5012 debug_print("folder '%s' found.\n", loc_path);
5023 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
5025 GSList *sorted_list, *cur;
5026 guint first, last, next;
5027 GSList *ret_list = NULL, *llast = NULL;
5028 struct mailimap_set * current_set;
5029 unsigned int item_count;
5031 if (numlist == NULL)
5034 current_set = mailimap_set_new_empty();
5036 sorted_list = g_slist_copy(numlist);
5037 sorted_list = g_slist_sort(sorted_list, g_int_compare);
5039 first = GPOINTER_TO_INT(sorted_list->data);
5042 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
5043 if (GPOINTER_TO_INT(cur->data) == 0)
5048 last = GPOINTER_TO_INT(cur->data);
5050 next = GPOINTER_TO_INT(cur->next->data);
5054 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5056 struct mailimap_set_item * item;
5057 item = mailimap_set_item_new(first, last);
5058 mailimap_set_add(current_set, item);
5062 if (item_count >= folder->max_set_size) {
5063 if (ret_list == NULL)
5064 llast = ret_list = g_slist_append(ret_list,
5067 llast = g_slist_append(llast, current_set);
5068 llast = llast->next;
5071 current_set = mailimap_set_new_empty();
5077 if (clist_count(current_set->set_list) > 0) {
5078 ret_list = g_slist_append(ret_list,
5082 g_slist_free(sorted_list);
5087 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5089 MsgNumberList *numlist = NULL;
5093 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5094 MsgInfo *msginfo = (MsgInfo *) cur->data;
5096 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5098 numlist = g_slist_reverse(numlist);
5099 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5100 g_slist_free(numlist);
5105 static GSList * imap_uid_list_from_lep(clist * list)
5112 for(iter = clist_begin(list) ; iter != NULL ;
5113 iter = clist_next(iter)) {
5116 puid = clist_content(iter);
5117 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5120 result = g_slist_reverse(result);
5124 static GSList * imap_uid_list_from_lep_tab(carray * list)
5131 for(i = 0 ; i < carray_count(list) ; i ++) {
5134 puid = carray_get(list, i);
5135 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5137 result = g_slist_reverse(result);
5141 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5143 GHashTable * tags_hash)
5150 for(i = 0 ; i < carray_count(list) ; i += 3) {
5155 puid = carray_get(list, i);
5156 pflags = carray_get(list, i + 1);
5157 tags = carray_get(list, i + 2);
5159 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5160 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5164 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5167 MsgInfo *msginfo = NULL;
5170 MsgFlags flags = {0, 0};
5172 if (info->headers == NULL)
5175 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5176 if (folder_has_parent_of_type(item, F_QUEUE)) {
5177 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5178 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5179 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5181 flags.perm_flags = info->flags;
5184 size = (goffset) info->size;
5185 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5188 msginfo->msgnum = uid;
5189 msginfo->size = size;
5195 static void imap_lep_set_free(GSList *seq_list)
5199 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5200 struct mailimap_set * imapset;
5202 imapset = cur->data;
5203 mailimap_set_free(imapset);
5205 g_slist_free(seq_list);
5208 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5210 struct mailimap_flag_list * flag_list;
5213 flag_list = mailimap_flag_list_new_empty();
5215 if (IMAP_IS_SEEN(flags))
5216 mailimap_flag_list_add(flag_list,
5217 mailimap_flag_new_seen());
5218 if (IMAP_IS_ANSWERED(flags))
5219 mailimap_flag_list_add(flag_list,
5220 mailimap_flag_new_answered());
5221 if (IMAP_IS_FLAGGED(flags))
5222 mailimap_flag_list_add(flag_list,
5223 mailimap_flag_new_flagged());
5224 if (IMAP_IS_DELETED(flags))
5225 mailimap_flag_list_add(flag_list,
5226 mailimap_flag_new_deleted());
5227 if (IMAP_IS_DRAFT(flags))
5228 mailimap_flag_list_add(flag_list,
5229 mailimap_flag_new_draft());
5230 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5231 mailimap_flag_list_add(flag_list,
5232 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5234 for (; cur; cur = cur->next) {
5236 imap_utf8_to_modified_utf7(cur->data, TRUE);
5237 g_strstrip(enc_str);
5239 mailimap_flag_list_add(flag_list,
5240 mailimap_flag_new_flag_keyword(enc_str));
5246 guint imap_folder_get_refcnt(Folder *folder)
5248 return ((IMAPFolder *)folder)->refcnt;
5251 void imap_folder_ref(Folder *folder)
5253 ((IMAPFolder *)folder)->refcnt++;
5256 void imap_disconnect_all(void)
5259 gboolean short_timeout;
5260 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5264 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5266 short_timeout = !networkmanager_is_online(&error);
5268 short_timeout = TRUE;
5269 g_error_free(error);
5272 short_timeout = TRUE;
5276 imap_main_set_timeout(1);
5278 for (list = account_get_list(); list != NULL; list = list->next) {
5279 PrefsAccount *account = list->data;
5280 if (account->protocol == A_IMAP4) {
5281 RemoteFolder *folder = (RemoteFolder *)account->folder;
5282 if (folder && folder->session) {
5283 IMAPSession *session = (IMAPSession *)folder->session;
5284 imap_threaded_disconnect(FOLDER(folder));
5285 SESSION(session)->state = SESSION_DISCONNECTED;
5286 SESSION(session)->sock = NULL;
5287 session_destroy(SESSION(session));
5288 folder->session = NULL;
5294 imap_main_set_timeout(prefs_common.io_timeout_secs);
5297 void imap_folder_unref(Folder *folder)
5299 if (((IMAPFolder *)folder)->refcnt > 0)
5300 ((IMAPFolder *)folder)->refcnt--;
5303 void imap_cancel_all(void)
5308 folderlist = folder_get_list();
5309 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5310 Folder *folder = (Folder *) cur->data;
5312 if (folder->klass == &imap_class) {
5313 if (imap_is_busy(folder)) {
5314 IMAPSession *imap_session;
5315 RemoteFolder *rfolder;
5317 g_printerr("cancelled\n");
5318 imap_threaded_cancel(folder);
5319 rfolder = (RemoteFolder *) folder;
5320 imap_session = (IMAPSession *) rfolder->session;
5322 imap_session->cancelled = 1;
5328 gboolean imap_cancel_all_enabled(void)
5333 folderlist = folder_get_list();
5334 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5335 Folder *folder = (Folder *) cur->data;
5337 if (folder->klass == &imap_class) {
5338 if (imap_is_busy(folder)) {
5347 static gboolean imap_is_busy(Folder *folder)
5349 IMAPSession *imap_session;
5350 RemoteFolder *rfolder;
5352 rfolder = (RemoteFolder *) folder;
5353 imap_session = (IMAPSession *) rfolder->session;
5354 if (imap_session == NULL)
5357 return imap_session->busy;
5360 #else /* HAVE_LIBETPAN */
5362 static FolderClass imap_class;
5364 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5365 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5367 static Folder *imap_folder_new (const gchar *name,
5370 static gboolean missing_imap_warning = TRUE;
5371 if (missing_imap_warning) {
5372 missing_imap_warning = FALSE;
5374 _("You have one or more IMAP accounts "
5375 "defined. However this version of "
5376 "Claws Mail has been built without "
5377 "IMAP support; your IMAP account(s) are "
5379 "You probably need to "
5380 "install libetpan and recompile "
5385 static gint imap_create_tree (Folder *folder)
5389 static FolderItem *imap_create_folder (Folder *folder,
5395 static gint imap_rename_folder (Folder *folder,
5402 gchar imap_get_path_separator_for_item(FolderItem *item)
5407 FolderClass *imap_get_class(void)
5409 if (imap_class.idstr == NULL) {
5410 imap_class.type = F_IMAP;
5411 imap_class.idstr = "imap";
5412 imap_class.uistr = "IMAP4";
5414 imap_class.new_folder = imap_folder_new;
5415 imap_class.create_tree = imap_create_tree;
5416 imap_class.create_folder = imap_create_folder;
5417 imap_class.rename_folder = imap_rename_folder;
5419 imap_class.set_xml = folder_set_xml;
5420 imap_class.get_xml = folder_get_xml;
5421 imap_class.item_set_xml = imap_item_set_xml;
5422 imap_class.item_get_xml = imap_item_get_xml;
5423 /* nothing implemented */
5429 void imap_disconnect_all(void)
5433 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5438 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5443 void imap_cache_msg(FolderItem *item, gint msgnum)
5447 void imap_cancel_all(void)
5451 gboolean imap_cancel_all_enabled(void)
5458 #ifdef HAVE_LIBETPAN
5459 static void imap_synchronise(FolderItem *item, gint days)
5461 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5462 debug_print("%s already synced\n", item->path?item->path:item->name);
5465 debug_print("syncing %s\n", item->path?item->path:item->name);
5466 imap_gtk_synchronise(item, days);
5467 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5471 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5473 #ifdef HAVE_LIBETPAN
5476 folder_item_set_xml(folder, item, tag);
5478 #ifdef HAVE_LIBETPAN
5479 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5480 XMLAttr *attr = (XMLAttr *) cur->data;
5482 if (!attr || !attr->name || !attr->value) continue;
5483 if (!strcmp(attr->name, "uidnext"))
5484 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5485 if (!strcmp(attr->name, "last_sync"))
5486 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5487 if (!strcmp(attr->name, "last_change"))
5488 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5490 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5491 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5495 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5499 tag = folder_item_get_xml(folder, item);
5501 #ifdef HAVE_LIBETPAN
5502 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5503 IMAP_FOLDER_ITEM(item)->uid_next));
5504 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5505 IMAP_FOLDER_ITEM(item)->last_sync));
5506 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5507 IMAP_FOLDER_ITEM(item)->last_change));
5513 /* ===================================================================
5514 * UTF-7 conversion routines as in RFC 2192
5515 * ===================================================================
5516 * These two functions from:
5518 * Copyright (C) 2003-2004 Pawel Salek. */
5520 /* UTF7 modified base64 alphabet */
5521 static char base64chars[] =
5522 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5523 #define UNDEFINED 64
5525 /* UTF16 definitions */
5526 #define UTF16MASK 0x03FFUL
5527 #define UTF16SHIFT 10
5528 #define UTF16BASE 0x10000UL
5529 #define UTF16HIGHSTART 0xD800UL
5530 #define UTF16HIGHEND 0xDBFFUL
5531 #define UTF16LOSTART 0xDC00UL
5532 #define UTF16LOEND 0xDFFFUL
5535 /* Convert an IMAP mailbox to a UTF-8 string.
5536 * dst needs to have roughly 4 times the storage space of src
5537 * Hex encoding can triple the size of the input
5538 * UTF-7 can be slightly denser than UTF-8
5539 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5541 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5543 unsigned c, i, bitcount;
5544 unsigned long ucs4, utf16, bitbuf;
5545 unsigned char base64[256];
5547 char *dst, *res = malloc(2*strlen(mbox)+1);
5552 if(!dst) return NULL;
5553 /* initialize modified base64 decoding table */
5554 memset(base64, UNDEFINED, sizeof (base64));
5555 for (i = 0; i < sizeof (base64chars); ++i) {
5556 base64[(unsigned)base64chars[i]] = i;
5559 /* loop until end of string */
5560 while (*src != '\0') {
5562 /* deal with literal characters and &- */
5563 if (c != '&' || *src == '-') {
5564 /* encode literally */
5565 if (change_spaces && c == '_')
5569 /* skip over the '-' if this is an &- sequence */
5570 if (c == '&') ++src;
5572 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5576 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5578 bitbuf = (bitbuf << 6) | c;
5580 /* enough bits for a UTF-16 character? */
5581 if (bitcount >= 16) {
5583 utf16 = (bitcount ? bitbuf >> bitcount
5585 /* convert UTF16 to UCS4 */
5587 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5588 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5591 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5592 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5597 /* convert UTF-16 range of UCS4 to UTF-8 */
5598 if (ucs4 <= 0x7fUL) {
5601 } else if (ucs4 <= 0x7ffUL) {
5602 dst[0] = 0xc0 | (ucs4 >> 6);
5603 dst[1] = 0x80 | (ucs4 & 0x3f);
5605 } else if (ucs4 <= 0xffffUL) {
5606 dst[0] = 0xe0 | (ucs4 >> 12);
5607 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5608 dst[2] = 0x80 | (ucs4 & 0x3f);
5611 dst[0] = 0xf0 | (ucs4 >> 18);
5612 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5613 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5614 dst[3] = 0x80 | (ucs4 & 0x3f);
5619 /* skip over trailing '-' in modified UTF-7 encoding */
5620 if (*src == '-') ++src;
5623 /* terminate destination string */
5628 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5629 * dst should be about twice the length of src to deal with non-hex
5632 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5634 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5635 unsigned long ucs4 = 0, bitbuf = 0;
5637 /* initialize hex lookup table */
5638 char *dst, *res = malloc(2*strlen(src)+1);
5640 if(!dst) return NULL;
5646 while ((c = (unsigned char)*src) != '\0') {
5648 /* normal character? */
5649 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5650 /* switch out of UTF-7 mode */
5653 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5661 if (change_spaces && c == ' ')
5665 /* encode '&' as '&-' */
5671 /* switch to UTF-7 mode */
5676 /* Encode US-ASCII characters as themselves */
5680 } else if (utf8total) {
5681 /* save UTF8 bits into UCS4 */
5682 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5683 if (++utf8pos < utf8total) {
5691 } else if (c < 0xF0) {
5695 /* NOTE: can't convert UTF8 sequences longer than 4 */
5701 /* loop to split ucs4 into two utf16 chars if necessary */
5704 if (ucs4 >= UTF16BASE) {
5706 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5708 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5711 bitbuf = (bitbuf << 16) | ucs4;
5715 /* spew out base64 */
5716 while (bitstogo >= 6) {
5718 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5722 } while (utf16flag);
5724 /* if in UTF-7 mode, finish in ASCII */
5727 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5731 /* tie off string */