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"
74 typedef struct _IMAPFolder IMAPFolder;
75 typedef struct _IMAPSession IMAPSession;
76 typedef struct _IMAPNameSpace IMAPNameSpace;
77 typedef struct _IMAPFolderItem IMAPFolderItem;
79 #include "prefs_account.h"
81 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
82 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
83 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
89 /* list of IMAPNameSpace */
93 gchar last_seen_separator;
102 gboolean authenticated;
111 gboolean folder_content_changed;
122 gboolean sens_update_block;
125 struct _IMAPNameSpace
131 #define IMAPBUFSIZE 8192
135 IMAP_FLAG_SEEN = 1 << 0,
136 IMAP_FLAG_ANSWERED = 1 << 1,
137 IMAP_FLAG_FLAGGED = 1 << 2,
138 IMAP_FLAG_DELETED = 1 << 3,
139 IMAP_FLAG_DRAFT = 1 << 4
142 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
143 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
144 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
145 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
146 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
149 #define IMAP4_PORT 143
150 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
151 #define IMAPS_PORT 993
154 #define IMAP_CMD_LIMIT 1000
157 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
158 ITEM_CAN_CREATE_FLAGS,
159 ITEM_CANNOT_CREATE_FLAGS
162 struct _IMAPFolderItem
171 GHashTable *flags_set_table;
172 GHashTable *flags_unset_table;
175 gboolean should_update;
176 gboolean should_trash_cache;
177 gint can_create_flags;
179 GHashTable *tags_set_table;
180 GHashTable *tags_unset_table;
184 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
185 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
187 static void imap_folder_init (Folder *folder,
191 static Folder *imap_folder_new (const gchar *name,
193 static void imap_folder_destroy (Folder *folder);
195 static IMAPSession *imap_session_new (Folder *folder,
196 const PrefsAccount *account);
197 static void imap_session_authenticate(IMAPSession *session,
198 const PrefsAccount *account);
199 static void imap_session_destroy (Session *session);
201 static gchar *imap_fetch_msg (Folder *folder,
204 static gchar *imap_fetch_msg_full (Folder *folder,
209 static void imap_remove_cached_msg (Folder *folder,
212 static gint imap_add_msg (Folder *folder,
216 static gint imap_add_msgs (Folder *folder,
219 GRelation *relation);
221 static gint imap_copy_msg (Folder *folder,
224 static gint imap_copy_msgs (Folder *folder,
226 MsgInfoList *msglist,
227 GRelation *relation);
229 static gint imap_remove_msg (Folder *folder,
232 static gint imap_remove_msgs (Folder *folder,
234 MsgInfoList *msglist,
235 GRelation *relation);
236 static gint imap_remove_all_msg (Folder *folder,
239 static gboolean imap_is_msg_changed (Folder *folder,
243 static gint imap_close (Folder *folder,
246 static gint imap_scan_tree (Folder *folder);
248 static gint imap_create_tree (Folder *folder);
250 static FolderItem *imap_create_folder (Folder *folder,
253 static gint imap_rename_folder (Folder *folder,
256 static gint imap_remove_folder (Folder *folder,
259 static FolderItem *imap_folder_item_new (Folder *folder);
260 static void imap_folder_item_destroy (Folder *folder,
263 static IMAPSession *imap_session_get (Folder *folder);
265 static gint imap_auth (IMAPSession *session,
270 static gint imap_scan_tree_recursive (IMAPSession *session,
274 static void imap_create_missing_folders (Folder *folder);
275 static FolderItem *imap_create_special_folder
277 SpecialFolderItemType stype,
280 static gint imap_do_copy_msgs (Folder *folder,
282 MsgInfoList *msglist,
283 GRelation *relation);
285 static void imap_delete_all_cached_messages (FolderItem *item);
286 static void imap_set_batch (Folder *folder,
289 static gint imap_set_message_flags (IMAPSession *session,
290 MsgNumberList *numlist,
294 static gint imap_select (IMAPSession *session,
300 guint32 *uid_validity,
301 gint *can_create_flags,
303 static gint imap_status (IMAPSession *session,
306 IMAPFolderItem *item,
309 guint32 *uid_validity,
312 static void imap_commit_tags (FolderItem *item,
317 static gchar imap_get_path_separator (IMAPSession *session,
320 static gchar *imap_get_real_path (IMAPSession *session,
324 static void imap_synchronise (FolderItem *item, gint days);
326 static gboolean imap_is_busy (Folder *folder);
328 static void imap_free_capabilities (IMAPSession *session);
330 /* low-level IMAP4rev1 commands */
331 static gint imap_cmd_login (IMAPSession *session,
335 static gint imap_cmd_noop (IMAPSession *session);
336 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
337 static gint imap_cmd_starttls (IMAPSession *session);
339 static gint imap_cmd_select (IMAPSession *session,
344 guint32 *uid_validity,
345 gint *can_create_flags,
347 static gint imap_cmd_close (IMAPSession *session);
348 static gint imap_cmd_examine (IMAPSession *session,
353 guint32 *uid_validity,
355 static gint imap_cmd_create (IMAPSession *sock,
356 const gchar *folder);
357 static gint imap_cmd_rename (IMAPSession *sock,
358 const gchar *oldfolder,
359 const gchar *newfolder);
360 static gint imap_cmd_delete (IMAPSession *session,
361 const gchar *folder);
362 static gint imap_cmd_fetch (IMAPSession *sock,
364 const gchar *filename,
367 static gint imap_cmd_append (IMAPSession *session,
368 const gchar *destfolder,
372 static gint imap_cmd_copy (IMAPSession *session,
373 struct mailimap_set * set,
374 const gchar *destfolder,
375 GRelation *uid_mapping,
376 struct mailimap_set ** source,
377 struct mailimap_set ** dest);
378 static gint imap_cmd_store (IMAPSession *session,
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(IMAPFlags flags, GSList *tags);
431 typedef struct _hashtable_data {
433 IMAPFolderItem *item;
436 static FolderClass imap_class;
438 typedef struct _thread_data {
443 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
448 FolderClass *imap_get_class(void)
450 if (imap_class.idstr == NULL) {
451 imap_class.type = F_IMAP;
452 imap_class.idstr = "imap";
453 imap_class.uistr = "IMAP4";
455 /* Folder functions */
456 imap_class.new_folder = imap_folder_new;
457 imap_class.destroy_folder = imap_folder_destroy;
458 imap_class.scan_tree = imap_scan_tree;
459 imap_class.create_tree = imap_create_tree;
461 /* FolderItem functions */
462 imap_class.item_new = imap_folder_item_new;
463 imap_class.item_destroy = imap_folder_item_destroy;
464 imap_class.item_get_path = imap_item_get_path;
465 imap_class.create_folder = imap_create_folder;
466 imap_class.rename_folder = imap_rename_folder;
467 imap_class.remove_folder = imap_remove_folder;
468 imap_class.close = imap_close;
469 imap_class.get_num_list = imap_get_num_list;
470 imap_class.scan_required = imap_scan_required;
471 imap_class.set_xml = folder_set_xml;
472 imap_class.get_xml = folder_get_xml;
473 imap_class.item_set_xml = imap_item_set_xml;
474 imap_class.item_get_xml = imap_item_get_xml;
476 /* Message functions */
477 imap_class.get_msginfo = imap_get_msginfo;
478 imap_class.get_msginfos = imap_get_msginfos;
479 imap_class.fetch_msg = imap_fetch_msg;
480 imap_class.fetch_msg_full = imap_fetch_msg_full;
481 imap_class.add_msg = imap_add_msg;
482 imap_class.add_msgs = imap_add_msgs;
483 imap_class.copy_msg = imap_copy_msg;
484 imap_class.copy_msgs = imap_copy_msgs;
485 imap_class.remove_msg = imap_remove_msg;
486 imap_class.remove_msgs = imap_remove_msgs;
487 imap_class.remove_all_msg = imap_remove_all_msg;
488 imap_class.is_msg_changed = imap_is_msg_changed;
489 imap_class.change_flags = imap_change_flags;
490 imap_class.get_flags = imap_get_flags;
491 imap_class.set_batch = imap_set_batch;
492 imap_class.synchronise = imap_synchronise;
493 imap_class.remove_cached_msg = imap_remove_cached_msg;
494 imap_class.commit_tags = imap_commit_tags;
496 pthread_mutex_init(&imap_mutex, NULL);
503 static void imap_refresh_sensitivity (IMAPSession *session)
507 if (session->sens_update_block)
509 mainwin = mainwindow_get_mainwindow();
511 toolbar_main_set_sensitive(mainwin);
512 main_window_set_menu_sensitive(mainwin);
516 static void lock_session(IMAPSession *session)
519 debug_print("locking session %p (%d)\n", session, session->busy);
521 debug_print(" SESSION WAS LOCKED !! \n");
522 session->busy = TRUE;
523 imap_refresh_sensitivity(session);
525 debug_print("can't lock null session\n");
529 static void unlock_session(IMAPSession *session)
532 debug_print("unlocking session %p\n", session);
533 session->busy = FALSE;
534 imap_refresh_sensitivity(session);
536 debug_print("can't unlock null session\n");
540 static void imap_disc_session_destroy(Folder *folder)
542 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
543 IMAPSession *session = NULL;
547 session = IMAP_SESSION(rfolder->session);
550 rfolder->session = NULL;
551 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
552 SESSION(session)->state = SESSION_DISCONNECTED;
553 session_destroy(SESSION(session));
556 static gboolean is_fatal(int libetpan_errcode)
558 switch(libetpan_errcode) {
559 case MAILIMAP_ERROR_STREAM:
560 case MAILIMAP_ERROR_PROTOCOL:
561 case MAILIMAP_ERROR_PARSE:
562 case MAILIMAP_ERROR_BAD_STATE:
569 static void imap_handle_error(Session *session, int libetpan_errcode)
571 switch(libetpan_errcode) {
572 case MAILIMAP_NO_ERROR:
574 case MAILIMAP_NO_ERROR_AUTHENTICATED:
575 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
577 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
578 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
580 case MAILIMAP_ERROR_BAD_STATE:
581 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
583 case MAILIMAP_ERROR_STREAM:
584 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
586 case MAILIMAP_ERROR_PARSE:
587 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
588 "(very probably non-RFC compliance from the server)\n"));
590 case MAILIMAP_ERROR_CONNECTION_REFUSED:
591 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
593 case MAILIMAP_ERROR_MEMORY:
594 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
596 case MAILIMAP_ERROR_FATAL:
597 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
599 case MAILIMAP_ERROR_PROTOCOL:
600 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
601 "(very probably non-RFC compliance from the server)\n"));
603 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
604 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
606 case MAILIMAP_ERROR_APPEND:
607 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
609 case MAILIMAP_ERROR_NOOP:
610 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
612 case MAILIMAP_ERROR_LOGOUT:
613 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
615 case MAILIMAP_ERROR_CAPABILITY:
616 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
618 case MAILIMAP_ERROR_CHECK:
619 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
621 case MAILIMAP_ERROR_CLOSE:
622 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
624 case MAILIMAP_ERROR_EXPUNGE:
625 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
627 case MAILIMAP_ERROR_COPY:
628 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
630 case MAILIMAP_ERROR_UID_COPY:
631 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
633 case MAILIMAP_ERROR_CREATE:
634 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
636 case MAILIMAP_ERROR_DELETE:
637 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
639 case MAILIMAP_ERROR_EXAMINE:
640 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
642 case MAILIMAP_ERROR_FETCH:
643 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
645 case MAILIMAP_ERROR_UID_FETCH:
646 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
648 case MAILIMAP_ERROR_LIST:
649 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
651 case MAILIMAP_ERROR_LOGIN:
652 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
654 case MAILIMAP_ERROR_LSUB:
655 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
657 case MAILIMAP_ERROR_RENAME:
658 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
660 case MAILIMAP_ERROR_SEARCH:
661 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
663 case MAILIMAP_ERROR_UID_SEARCH:
664 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
666 case MAILIMAP_ERROR_SELECT:
667 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
669 case MAILIMAP_ERROR_STATUS:
670 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
672 case MAILIMAP_ERROR_STORE:
673 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
675 case MAILIMAP_ERROR_UID_STORE:
676 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
678 case MAILIMAP_ERROR_SUBSCRIBE:
679 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
681 case MAILIMAP_ERROR_UNSUBSCRIBE:
682 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
684 case MAILIMAP_ERROR_STARTTLS:
685 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
687 case MAILIMAP_ERROR_INVAL:
688 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
690 case MAILIMAP_ERROR_EXTENSION:
691 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
693 case MAILIMAP_ERROR_SASL:
694 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
696 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
697 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
698 case MAILIMAP_ERROR_SSL:
699 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
704 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
709 if (session && is_fatal(libetpan_errcode)) {
710 imap_disc_session_destroy(IMAP_SESSION(session)->folder);
711 } else if (session && !is_fatal(libetpan_errcode)) {
712 if (IMAP_SESSION(session)->busy)
713 unlock_session(IMAP_SESSION(session));
717 static Folder *imap_folder_new(const gchar *name, const gchar *path)
721 folder = (Folder *)g_new0(IMAPFolder, 1);
722 folder->klass = &imap_class;
723 imap_folder_init(folder, name, path);
728 static void imap_folder_destroy(Folder *folder)
730 while (imap_folder_get_refcnt(folder) > 0)
731 gtk_main_iteration();
733 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
737 static void imap_folder_init(Folder *folder, const gchar *name,
740 folder_remote_folder_init((Folder *)folder, name, path);
741 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
744 static FolderItem *imap_folder_item_new(Folder *folder)
746 IMAPFolderItem *item;
748 item = g_new0(IMAPFolderItem, 1);
751 item->uid_list = NULL;
753 return (FolderItem *)item;
756 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
758 IMAPFolderItem *item = (IMAPFolderItem *)_item;
760 g_return_if_fail(item != NULL);
761 g_slist_free(item->uid_list);
766 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
768 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
771 g_slist_free(item->uid_list);
772 item->uid_list = NULL;
777 static void imap_reset_uid_lists(Folder *folder)
779 if(folder->node == NULL)
782 /* Destroy all uid lists and rest last uid */
783 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
786 static int imap_get_capabilities(IMAPSession *session)
788 struct mailimap_capability_data *capabilities = NULL;
792 if (session->capability != NULL)
793 return MAILIMAP_NO_ERROR;
795 capabilities = imap_threaded_capability(session->folder, &result);
797 if (result != MAILIMAP_NO_ERROR) {
798 imap_handle_error(SESSION(session), result);
799 return MAILIMAP_ERROR_CAPABILITY;
802 if (capabilities == NULL) {
803 return MAILIMAP_NO_ERROR;
806 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
807 cur = clist_next(cur)) {
808 struct mailimap_capability * cap =
810 if (!cap || cap->cap_data.cap_name == NULL)
812 session->capability = g_slist_append
813 (session->capability,
814 g_strdup(cap->cap_data.cap_name));
815 debug_print("got capa %s\n", cap->cap_data.cap_name);
817 mailimap_capability_data_free(capabilities);
818 return MAILIMAP_NO_ERROR;
821 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
824 for (cur = session->capability; cur; cur = cur->next) {
825 if (!g_ascii_strcasecmp(cur->data, cap))
831 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
834 gint ok = MAILIMAP_ERROR_BAD_STATE;
835 static time_t last_login_err = 0;
836 gchar *ext_info = "";
838 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
839 imap_handle_error(SESSION(session), r);
844 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
846 case IMAP_AUTH_CRAM_MD5:
847 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
849 case IMAP_AUTH_LOGIN:
850 ok = imap_cmd_login(session, user, pass, "LOGIN");
852 case IMAP_AUTH_GSSAPI:
853 ok = imap_cmd_login(session, user, pass, "GSSAPI");
856 debug_print("capabilities:\n"
861 imap_has_capability(session, "ANONYMOUS"),
862 imap_has_capability(session, "CRAM-MD5"),
863 imap_has_capability(session, "LOGIN"),
864 imap_has_capability(session, "GSSAPI"));
865 if (imap_has_capability(session, "CRAM-MD5"))
866 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
867 if ((ok == MAILIMAP_ERROR_BAD_STATE ||
868 ok == MAILIMAP_ERROR_LOGIN) && imap_has_capability(session, "GSSAPI"))
869 ok = imap_cmd_login(session, user, pass, "GSSAPI");
870 if (ok == MAILIMAP_ERROR_BAD_STATE||
871 ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
872 ok = imap_cmd_login(session, user, pass, "LOGIN");
875 if (ok == MAILIMAP_NO_ERROR)
876 session->authenticated = TRUE;
878 if (type == IMAP_AUTH_CRAM_MD5) {
879 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
880 "compiled with SASL support and the "
881 "CRAM-MD5 SASL plugin is installed.");
884 if (time(NULL) - last_login_err > 10) {
885 if (!prefs_common.no_recv_err_panel) {
886 alertpanel_error(_("Connection to %s failed: "
888 SESSION(session)->server, ext_info);
890 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
891 "login refused.%s\n"),
892 SESSION(session)->server, ext_info);
895 last_login_err = time(NULL);
900 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
902 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
903 /* Check if this is the first try to establish a
904 connection, if yes we don't try to reconnect */
905 debug_print("reconnecting\n");
906 if (rfolder->session == NULL) {
907 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
908 folder->account->recv_server);
909 session_destroy(SESSION(session));
912 rfolder->session = NULL;
913 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
914 " disconnected. Reconnecting...\n"),
915 folder->account->recv_server);
916 statusbar_print_all(_("IMAP4 connection to %s has been"
917 " disconnected. Reconnecting...\n"),
918 folder->account->recv_server);
919 SESSION(session)->state = SESSION_DISCONNECTED;
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) {
952 session = IMAP_SESSION(rfolder->session);
953 } else if (rfolder->connecting) {
954 debug_print("already connecting\n");
957 imap_reset_uid_lists(folder);
958 if (time(NULL) - rfolder->last_failure <= 2)
960 rfolder->connecting = TRUE;
961 session = imap_session_new(folder, folder->account);
963 if(session == NULL) {
964 rfolder->last_failure = time(NULL);
965 rfolder->connecting = FALSE;
969 /* Make sure session is authenticated */
970 if (!IMAP_SESSION(session)->authenticated)
971 imap_session_authenticate(IMAP_SESSION(session), folder->account);
973 if (!IMAP_SESSION(session)->authenticated) {
974 imap_threaded_disconnect(session->folder);
975 rfolder->session = NULL;
976 SESSION(session)->state = SESSION_DISCONNECTED;
977 session_destroy(SESSION(session));
978 rfolder->last_failure = time(NULL);
979 rfolder->connecting = FALSE;
983 /* I think the point of this code is to avoid sending a
984 * keepalive if we've used the session recently and therefore
985 * think it's still alive. Unfortunately, most of the code
986 * does not yet check for errors on the socket, and so if the
987 * connection drops we don't notice until the timeout expires.
988 * A better solution than sending a NOOP every time would be
989 * for every command to be prepared to retry until it is
990 * successfully sent. -- mbp */
991 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
992 /* verify that the session is still alive */
993 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
994 debug_print("disconnected!\n");
996 session = imap_reconnect_if_possible(folder, session);
998 session = imap_session_get(folder);
1001 session->cancelled = FALSE;
1004 rfolder->session = SESSION(session);
1005 rfolder->connecting = FALSE;
1007 return IMAP_SESSION(session);
1010 static IMAPSession *imap_session_new(Folder * folder,
1011 const PrefsAccount *account)
1013 IMAPSession *session;
1016 int authenticated = FALSE;
1018 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1019 /* FIXME: IMAP over SSL only... */
1022 port = account->set_imapport ? account->imapport
1023 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1024 ssl_type = account->ssl_imap;
1026 if (account->ssl_imap != SSL_NONE) {
1027 if (alertpanel_full(_("Insecure connection"),
1028 _("This connection is configured to be secured "
1029 "using SSL, but SSL is not available in this "
1030 "build of Claws Mail. \n\n"
1031 "Do you want to continue connecting to this "
1032 "server? The communication would not be "
1034 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1035 NULL, FALSE, NULL, ALERT_WARNING,
1036 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1039 port = account->set_imapport ? account->imapport
1044 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1045 if (account->set_tunnelcmd) {
1046 r = imap_threaded_connect_cmd(folder,
1048 account->recv_server,
1052 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1053 if (ssl_type == SSL_TUNNEL) {
1054 r = imap_threaded_connect_ssl(folder,
1055 account->recv_server,
1061 r = imap_threaded_connect(folder,
1062 account->recv_server,
1067 statuswindow_pop_all();
1068 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1069 authenticated = TRUE;
1071 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1072 authenticated = FALSE;
1075 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1076 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1077 if (r == MAILIMAP_ERROR_SSL)
1078 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1082 imap_handle_error(NULL, r);
1084 if(!prefs_common.no_recv_err_panel) {
1085 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1086 account->recv_server, port);
1088 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1089 account->recv_server, port);
1095 session = g_new0(IMAPSession, 1);
1096 session_init(SESSION(session));
1097 SESSION(session)->type = SESSION_IMAP;
1098 SESSION(session)->server = g_strdup(account->recv_server);
1099 SESSION(session)->sock = NULL;
1101 SESSION(session)->destroy = imap_session_destroy;
1103 session->capability = NULL;
1105 session->authenticated = authenticated;
1106 session->mbox = NULL;
1107 session->exists = 0;
1108 session->recent = 0;
1109 session->expunge = 0;
1110 session->cmd_count = 0;
1111 session->folder = folder;
1112 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1114 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1115 if (account->ssl_imap == SSL_STARTTLS) {
1118 ok = imap_cmd_starttls(session);
1119 if (ok != MAILIMAP_NO_ERROR) {
1120 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1121 session_destroy(SESSION(session));
1125 imap_free_capabilities(session);
1126 session->authenticated = FALSE;
1127 session->uidplus = FALSE;
1128 session->cmd_count = 1;
1131 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1132 (session->authenticated) ? "pre" : "un");
1137 static void imap_session_authenticate(IMAPSession *session,
1138 const PrefsAccount *account)
1140 gchar *pass, *acc_pass;
1141 gboolean failed = FALSE;
1143 g_return_if_fail(account->userid != NULL);
1144 acc_pass = account->passwd;
1147 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1149 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1152 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
1154 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1157 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1158 account->recv_server);
1159 if (imap_auth(session, account->userid, pass, account->imap_auth_type) != MAILIMAP_NO_ERROR) {
1160 statusbar_pop_all();
1167 if (prefs_common.no_recv_err_panel) {
1168 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
1169 mainwindow_show_error();
1171 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1177 statuswindow_pop_all();
1178 session->authenticated = TRUE;
1182 static void imap_session_destroy(Session *session)
1184 if (session->state != SESSION_DISCONNECTED)
1185 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1187 imap_free_capabilities(IMAP_SESSION(session));
1188 g_free(IMAP_SESSION(session)->mbox);
1189 sock_close(session->sock);
1190 session->sock = NULL;
1193 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1195 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1198 static guint get_file_size_with_crs(const gchar *filename)
1204 if (filename == NULL)
1207 fp = fopen(filename, "rb");
1211 while (fgets(buf, sizeof (buf), fp) != NULL) {
1213 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1221 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1223 gchar *path, *filename;
1225 path = folder_item_get_path(item);
1227 if (!is_dir_exist(path)) {
1232 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1235 if (is_file_exist(filename)) {
1241 typedef struct _TagsData {
1244 IMAPFolderItem *item;
1247 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1249 IMAPSession *session;
1250 gint ok, can_create_tags;
1251 Folder *folder = NULL;
1252 TagsData *ht_data = NULL;
1255 g_return_if_fail(item != NULL);
1256 g_return_if_fail(msginfo != NULL);
1258 folder = item->folder;
1259 debug_print("getting session...\n");
1260 session = imap_session_get(folder);
1263 debug_print("can't get session\n");
1267 ok = imap_select(session, IMAP_FOLDER(folder), item,
1268 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1270 if (ok != MAILIMAP_NO_ERROR) {
1271 imap_handle_error(SESSION(session), ok);
1276 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1279 if (IMAP_FOLDER_ITEM(item)->batching) {
1280 /* instead of performing an UID STORE command for each message change,
1281 * as a lot of them can change "together", we just fill in hashtables
1282 * and defer the treatment so that we're able to send only one
1285 debug_print("IMAP batch mode on, deferring tags change\n");
1286 for (cur = tags_set; cur; cur = cur->next) {
1287 gint cur_tag = GPOINTER_TO_INT(cur->data);
1289 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1290 GINT_TO_POINTER(cur_tag));
1291 if (ht_data == NULL) {
1292 ht_data = g_new0(TagsData, 1);
1293 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1294 ht_data->item = IMAP_FOLDER_ITEM(item);
1295 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1296 GINT_TO_POINTER(cur_tag), ht_data);
1298 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1301 for (cur = tags_unset; cur; cur = cur->next) {
1302 gint cur_tag = GPOINTER_TO_INT(cur->data);
1304 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1305 GINT_TO_POINTER(cur_tag));
1306 if (ht_data == NULL) {
1307 ht_data = g_new0(TagsData, 1);
1308 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1309 ht_data->item = IMAP_FOLDER_ITEM(item);
1310 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1311 GINT_TO_POINTER(cur_tag), ht_data);
1313 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1317 GSList *list_set = NULL;
1318 GSList *list_unset = NULL;
1321 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1322 numlist.next = NULL;
1324 debug_print("IMAP changing tags NOW\n");
1325 for (cur = tags_set; cur; cur = cur->next) {
1326 gint cur_tag = GPOINTER_TO_INT(cur->data);
1327 const gchar *str = tags_get_tag(cur_tag);
1328 list_set = g_slist_prepend(list_set, g_strdup(str));
1331 ok = imap_set_message_flags(session, &numlist, 0, list_set, TRUE);
1332 slist_free_strings(list_set);
1333 g_slist_free(list_set);
1334 if (ok != MAILIMAP_NO_ERROR) {
1339 for (cur = tags_unset; cur; cur = cur->next) {
1340 gint cur_tag = GPOINTER_TO_INT(cur->data);
1341 const gchar *str = tags_get_tag(cur_tag);
1342 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1345 ok = imap_set_message_flags(session, &numlist, 0, list_unset, FALSE);
1346 slist_free_strings(list_unset);
1347 g_slist_free(list_unset);
1348 if (ok != MAILIMAP_NO_ERROR) {
1355 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1356 gboolean headers, gboolean body)
1358 gchar *path, *filename;
1359 IMAPSession *session;
1362 g_return_val_if_fail(folder != NULL, NULL);
1363 g_return_val_if_fail(item != NULL, NULL);
1368 path = folder_item_get_path(item);
1369 if (!is_dir_exist(path))
1370 make_dir_hier(path);
1371 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1373 debug_print("trying to fetch cached %s\n", filename);
1374 if (is_file_exist(filename)) {
1375 /* see whether the local file represents the whole message
1376 * or not. As the IMAP server reports size with \r chars,
1377 * we have to update the local file (UNIX \n only) size */
1378 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1379 guint have_size = -1;
1382 debug_print("message %d has been already %scached.\n", uid,
1383 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1385 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1386 have_size = get_file_size_with_crs(filename);
1387 if (cached && (cached->size <= have_size || !body)) {
1388 procmsg_msginfo_free(cached);
1389 ok = file_strip_crs(filename);
1390 if (ok == 0 && cached && cached->size <= have_size) {
1391 /* we have it all and stripped */
1392 debug_print("...fully cached in fact; setting flag.\n");
1393 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1396 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1397 debug_print("message not cached and file recent, considering file complete\n");
1398 ok = file_strip_crs(filename);
1402 procmsg_msginfo_free(cached);
1405 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1406 procmsg_msginfo_free(cached);
1410 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1412 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1413 procmsg_msginfo_free(cached);
1417 debug_print("getting session...\n");
1418 session = imap_session_get(folder);
1424 session_set_access_time(SESSION(session));
1425 lock_session(session); /* unlocked later in the function */
1427 debug_print("IMAP fetching messages\n");
1428 ok = imap_select(session, IMAP_FOLDER(folder), item,
1429 NULL, NULL, NULL, NULL, NULL, FALSE);
1430 if (ok != MAILIMAP_NO_ERROR) {
1431 g_warning("can't select mailbox %s\n", item->path);
1436 session_set_access_time(SESSION(session));
1438 debug_print("getting message %d...\n", uid);
1439 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1441 if (ok != MAILIMAP_NO_ERROR) {
1442 g_warning("can't fetch message %d\n", uid);
1447 session_set_access_time(SESSION(session));
1448 unlock_session(session);
1450 ok = file_strip_crs(filename);
1452 if (ok == 0 && headers && body) {
1453 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1455 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1456 procmsg_msginfo_free(cached);
1458 } else if (ok == -1) {
1459 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1461 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1462 procmsg_msginfo_free(cached);
1468 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1470 gchar *path, *filename;
1472 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1477 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1478 procmsg_msginfo_free(cached);
1481 path = folder_item_get_path(item);
1482 if (!is_dir_exist(path))
1485 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1487 if (is_file_exist(filename)) {
1488 if (cached && cached->total_size == cached->size) {
1491 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1494 size = get_file_size_with_crs(filename);
1497 if (cached && size >= cached->size) {
1498 cached->total_size = cached->size;
1499 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1500 procmsg_msginfo_free(cached);
1504 procmsg_msginfo_free(cached);
1508 void imap_cache_msg(FolderItem *item, gint msgnum)
1510 Folder *folder = NULL;
1514 folder = item->folder;
1516 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1517 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1518 debug_print("fetched %s\n", tmp);
1523 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1524 const gchar *file, MsgFlags *flags)
1528 MsgFileInfo fileinfo;
1530 g_return_val_if_fail(file != NULL, -1);
1532 fileinfo.msginfo = NULL;
1533 fileinfo.file = (gchar *)file;
1534 fileinfo.flags = flags;
1535 file_list.data = &fileinfo;
1536 file_list.next = NULL;
1538 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1542 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1543 GRelation *relation)
1546 IMAPSession *session;
1547 guint32 last_uid = 0;
1549 MsgFileInfo *fileinfo;
1550 gint ok = MAILIMAP_NO_ERROR;
1551 gint curnum = 0, total = 0;
1552 gboolean missing_uids = FALSE;
1554 g_return_val_if_fail(folder != NULL, -1);
1555 g_return_val_if_fail(dest != NULL, -1);
1556 g_return_val_if_fail(file_list != NULL, -1);
1558 debug_print("getting session...\n");
1559 session = imap_session_get(folder);
1563 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1565 statusbar_print_all(_("Adding messages..."));
1566 total = g_slist_length(file_list);
1567 for (cur = file_list; cur != NULL; cur = cur->next) {
1568 IMAPFlags iflags = 0;
1569 guint32 new_uid = 0;
1570 gchar *real_file = NULL;
1571 fileinfo = (MsgFileInfo *)cur->data;
1573 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1576 if (fileinfo->flags) {
1577 if (MSG_IS_MARKED(*fileinfo->flags))
1578 iflags |= IMAP_FLAG_FLAGGED;
1579 if (MSG_IS_REPLIED(*fileinfo->flags))
1580 iflags |= IMAP_FLAG_ANSWERED;
1581 if (!MSG_IS_UNREAD(*fileinfo->flags))
1582 iflags |= IMAP_FLAG_SEEN;
1585 if (real_file == NULL)
1586 real_file = g_strdup(fileinfo->file);
1588 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1589 folder_has_parent_of_type(dest, F_OUTBOX) ||
1590 folder_has_parent_of_type(dest, F_DRAFT) ||
1591 folder_has_parent_of_type(dest, F_TRASH))
1592 iflags |= IMAP_FLAG_SEEN;
1594 ok = imap_cmd_append(session, destdir, real_file, iflags,
1597 if (ok != MAILIMAP_NO_ERROR) {
1598 g_warning("can't append message %s\n", real_file);
1601 statusbar_progress_all(0,0,0);
1602 statusbar_pop_all();
1605 debug_print("appended new message as %d\n", new_uid);
1606 /* put the local file in the imapcache, so that we don't
1607 * have to fetch it back later. */
1610 missing_uids = TRUE;
1611 debug_print("Missing UID (0)\n");
1614 gchar *cache_path = folder_item_get_path(dest);
1615 if (!is_dir_exist(cache_path))
1616 make_dir_hier(cache_path);
1617 if (is_dir_exist(cache_path)) {
1618 gchar *cache_file = g_strconcat(
1619 cache_path, G_DIR_SEPARATOR_S,
1620 itos(new_uid), NULL);
1621 copy_file(real_file, cache_file, TRUE);
1622 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1629 if (relation != NULL)
1630 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1631 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1632 GINT_TO_POINTER(new_uid));
1633 if (last_uid < new_uid) {
1640 statusbar_progress_all(0,0,0);
1641 statusbar_pop_all();
1646 imap_scan_required(folder, dest);
1648 session = imap_session_get(folder);
1654 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1655 &a, NULL, NULL, NULL, NULL, FALSE);
1660 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1662 GSList *result = NULL;
1667 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1668 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1669 start = item->set_first;
1670 end = item->set_last;
1671 for (t = start; t <= end; t++) {
1672 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1675 result = g_slist_reverse(result);
1676 if (debug_get_mode()) {
1677 debug_print("flat imap set: ");
1678 for (cur = result; cur; cur = cur->next) {
1679 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1686 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1687 MsgInfoList *msglist, GRelation *relation)
1691 GSList *seq_list, *cur;
1693 IMAPSession *session;
1694 gint ok = MAILIMAP_NO_ERROR;
1695 GRelation *uid_mapping;
1697 gboolean single = FALSE;
1699 g_return_val_if_fail(folder != NULL, -1);
1700 g_return_val_if_fail(dest != NULL, -1);
1701 g_return_val_if_fail(msglist != NULL, -1);
1703 debug_print("getting session...\n");
1704 session = imap_session_get(folder);
1710 msginfo = (MsgInfo *)msglist->data;
1711 if (msglist->next == NULL)
1713 src = msginfo->folder;
1715 g_warning("the src folder is identical to the dest.\n");
1719 if (src->folder != dest->folder) {
1720 GSList *infolist = NULL, *cur;
1722 for (cur = msglist; cur; cur = cur->next) {
1723 msginfo = (MsgInfo *)cur->data;
1724 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1725 fileinfo->file = procmsg_get_message_file(msginfo);
1726 fileinfo->flags = &(msginfo->flags);
1727 infolist = g_slist_prepend(infolist, fileinfo);
1729 infolist = g_slist_reverse(infolist);
1730 res = folder_item_add_msgs(dest, infolist, FALSE);
1731 for (cur = infolist; cur; cur = cur->next) {
1732 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1736 g_slist_free(infolist);
1740 lock_session(session); /* unlocked later in the function */
1742 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1743 NULL, NULL, NULL, NULL, NULL, FALSE);
1744 if (ok != MAILIMAP_NO_ERROR) {
1748 unlock_session(session);
1750 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1751 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1752 uid_mapping = g_relation_new(2);
1753 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1755 statusbar_print_all(_("Copying messages..."));
1756 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1757 struct mailimap_set * seq_set;
1758 struct mailimap_set * source = NULL;
1759 struct mailimap_set * dest = NULL;
1760 seq_set = cur->data;
1762 debug_print("Copying messages from %s to %s ...\n",
1763 src->path, destdir);
1765 lock_session(session); /* unlocked later in the function */
1766 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1773 if (ok == MAILIMAP_NO_ERROR) {
1774 unlock_session(session);
1775 if (relation && source && dest) {
1776 GSList *s_list = flatten_mailimap_set(source);
1777 GSList *d_list = flatten_mailimap_set(dest);
1778 GSList *s_cur, *d_cur;
1779 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1781 for (s_cur = s_list, d_cur = d_list;
1783 s_cur = s_cur->next, d_cur = d_cur->next) {
1784 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1788 debug_print("hhhmm, source list length != dest list length.\n");
1790 g_slist_free(s_list);
1791 g_slist_free(d_list);
1797 mailimap_set_free(source);
1799 mailimap_set_free(dest);
1801 if (ok != MAILIMAP_NO_ERROR) {
1802 g_relation_destroy(uid_mapping);
1803 imap_lep_set_free(seq_list);
1804 statusbar_pop_all();
1809 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1810 MsgInfo *msginfo = (MsgInfo *)cur->data;
1813 tuples = g_relation_select(uid_mapping,
1814 GINT_TO_POINTER(msginfo->msgnum),
1816 if (tuples->len > 0) {
1817 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1818 g_relation_insert(relation, msginfo,
1819 GINT_TO_POINTER(num));
1822 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1823 /* put the local file in the imapcache, so that we don't
1824 * have to fetch it back later. */
1826 gchar *cache_path = folder_item_get_path(msginfo->folder);
1827 gchar *real_file = g_strconcat(
1828 cache_path, G_DIR_SEPARATOR_S,
1829 itos(msginfo->msgnum), NULL);
1830 gchar *cache_file = NULL;
1832 cache_path = folder_item_get_path(dest);
1833 cache_file = g_strconcat(
1834 cache_path, G_DIR_SEPARATOR_S,
1836 if (!is_dir_exist(cache_path))
1837 make_dir_hier(cache_path);
1838 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1839 copy_file(real_file, cache_file, TRUE);
1840 debug_print("copied to cache: %s\n", cache_file);
1847 g_relation_insert(relation, msginfo,
1848 GINT_TO_POINTER(0));
1849 g_tuples_destroy(tuples);
1851 statusbar_pop_all();
1853 g_relation_destroy(uid_mapping);
1854 imap_lep_set_free(seq_list);
1858 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1859 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1860 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1861 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1863 imap_scan_required(folder, dest);
1864 if (ok == MAILIMAP_NO_ERROR)
1870 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1874 g_return_val_if_fail(msginfo != NULL, -1);
1876 msglist.data = msginfo;
1877 msglist.next = NULL;
1879 return imap_copy_msgs(folder, dest, &msglist, NULL);
1882 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1883 MsgInfoList *msglist, GRelation *relation)
1888 g_return_val_if_fail(folder != NULL, -1);
1889 g_return_val_if_fail(dest != NULL, -1);
1890 g_return_val_if_fail(msglist != NULL, -1);
1892 msginfo = (MsgInfo *)msglist->data;
1893 g_return_val_if_fail(msginfo->folder != NULL, -1);
1895 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1900 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1901 MsgInfoList *msglist, GRelation *relation)
1903 gchar *destdir, *dir;
1904 GSList *numlist = NULL, *cur;
1906 IMAPSession *session;
1907 gint ok = MAILIMAP_NO_ERROR;
1908 GRelation *uid_mapping;
1910 g_return_val_if_fail(folder != NULL, -1);
1911 g_return_val_if_fail(dest != NULL, -1);
1912 g_return_val_if_fail(msglist != NULL, -1);
1914 debug_print("getting session...\n");
1915 session = imap_session_get(folder);
1920 lock_session(session); /* unlocked later in the function */
1922 msginfo = (MsgInfo *)msglist->data;
1924 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1925 NULL, NULL, NULL, NULL, NULL, FALSE);
1926 if (ok != MAILIMAP_NO_ERROR) {
1930 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1931 for (cur = msglist; cur; cur = cur->next) {
1932 msginfo = (MsgInfo *)cur->data;
1933 if (!MSG_IS_DELETED(msginfo->flags))
1934 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1936 numlist = g_slist_reverse(numlist);
1938 uid_mapping = g_relation_new(2);
1939 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1941 if (numlist != NULL) {
1942 ok = imap_set_message_flags
1943 (session, numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1944 if (ok != MAILIMAP_NO_ERROR) {
1945 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1948 } /* else we just need to expunge */
1949 ok = imap_cmd_expunge(session);
1950 if (ok != MAILIMAP_NO_ERROR) {
1951 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1955 session->folder_content_changed = TRUE;
1956 unlock_session(session);
1958 dir = folder_item_get_path(msginfo->folder);
1959 if (is_dir_exist(dir)) {
1960 for (cur = msglist; cur; cur = cur->next) {
1961 msginfo = (MsgInfo *)cur->data;
1962 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1967 g_relation_destroy(uid_mapping);
1968 g_slist_free(numlist);
1970 imap_scan_required(folder, dest);
1973 if (ok == MAILIMAP_NO_ERROR)
1979 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
1980 MsgInfoList *msglist, GRelation *relation)
1984 g_return_val_if_fail(folder != NULL, -1);
1985 g_return_val_if_fail(dest != NULL, -1);
1986 if (msglist == NULL)
1989 msginfo = (MsgInfo *)msglist->data;
1990 g_return_val_if_fail(msginfo->folder != NULL, -1);
1992 return imap_do_remove_msgs(folder, dest, msglist, relation);
1995 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1997 GSList *list = folder_item_get_msg_list(item);
1998 gint res = imap_remove_msgs(folder, item, list, NULL);
1999 procmsg_msg_list_free(list);
2003 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2006 /* TODO: properly implement this method */
2010 static gint imap_close(Folder *folder, FolderItem *item)
2015 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2017 FolderItem *item = NULL;
2018 IMAPSession *session;
2019 gchar *root_folder = NULL;
2021 g_return_val_if_fail(folder != NULL, -1);
2022 g_return_val_if_fail(folder->account != NULL, -1);
2024 debug_print("getting session...\n");
2025 session = imap_session_get(folder);
2027 if (!folder->node) {
2028 folder_tree_destroy(folder);
2029 item = folder_item_new(folder, folder->name, NULL);
2030 item->folder = folder;
2031 folder->node = item->node = g_node_new(item);
2036 if (folder->account->imap_dir && *folder->account->imap_dir) {
2041 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2042 extract_quote(root_folder, '"');
2043 subst_char(root_folder,
2044 imap_get_path_separator(session, IMAP_FOLDER(folder),
2047 strtailchomp(root_folder, '/');
2048 real_path = imap_get_real_path
2049 (session, IMAP_FOLDER(folder), root_folder);
2050 debug_print("IMAP root directory: %s\n", real_path);
2052 /* check if root directory exist */
2054 r = imap_threaded_list(session->folder, "", real_path,
2057 if (r != MAILIMAP_NO_ERROR)
2058 imap_handle_error(SESSION(session), r);
2060 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2061 if (!folder->node) {
2062 item = folder_item_new(folder, folder->name, NULL);
2063 item->folder = folder;
2064 folder->node = item->node = g_node_new(item);
2068 mailimap_list_result_free(lep_list);
2074 item = FOLDER_ITEM(folder->node->data);
2076 if (item && !item->path && root_folder) {
2077 item->path = g_strdup(root_folder);
2080 if (!item || ((item->path || root_folder) &&
2081 strcmp2(item->path, root_folder) != 0)) {
2082 folder_tree_destroy(folder);
2083 item = folder_item_new(folder, folder->name, root_folder);
2084 item->folder = folder;
2085 folder->node = item->node = g_node_new(item);
2088 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2089 imap_create_missing_folders(folder);
2094 static gint imap_scan_tree(Folder *folder)
2096 gboolean subs_only = FALSE;
2097 if (folder->account) {
2098 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2099 subs_only = folder->account->imap_subsonly;
2101 return imap_scan_tree_real(folder, subs_only);
2104 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2107 IMAPFolder *imapfolder;
2108 FolderItem *new_item;
2109 GSList *item_list, *cur;
2112 gchar *wildcard_path;
2118 g_return_val_if_fail(item != NULL, -1);
2119 g_return_val_if_fail(item->folder != NULL, -1);
2120 g_return_val_if_fail(item->no_sub == FALSE, -1);
2122 folder = item->folder;
2123 imapfolder = IMAP_FOLDER(folder);
2125 separator = imap_get_path_separator(session, imapfolder, item->path);
2127 if (folder->ui_func)
2128 folder->ui_func(folder, item, folder->ui_func_data);
2131 wildcard[0] = separator;
2134 real_path = imap_get_real_path(session, imapfolder, item->path);
2138 real_path = g_strdup("");
2141 Xstrcat_a(wildcard_path, real_path, wildcard,
2142 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2146 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2148 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2150 if (r != MAILIMAP_NO_ERROR) {
2151 imap_handle_error(SESSION(session), r);
2155 item_list = imap_list_from_lep(imapfolder,
2156 lep_list, real_path, FALSE);
2157 mailimap_list_result_free(lep_list);
2162 node = item->node->children;
2163 while (node != NULL) {
2164 FolderItem *old_item = FOLDER_ITEM(node->data);
2165 GNode *next = node->next;
2168 for (cur = item_list; cur != NULL; cur = cur->next) {
2169 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2170 if (!strcmp2(old_item->path, cur_item->path)) {
2171 new_item = cur_item;
2176 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2177 debug_print("not removing INBOX\n");
2179 debug_print("folder '%s' not found. removing...\n",
2181 folder_item_remove(old_item);
2184 old_item->no_sub = new_item->no_sub;
2185 old_item->no_select = new_item->no_select;
2186 if (old_item->no_sub == TRUE && node->children) {
2187 debug_print("folder '%s' doesn't have "
2188 "subfolders. removing...\n",
2190 folder_item_remove_children(old_item);
2197 for (cur = item_list; cur != NULL; cur = cur->next) {
2198 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2201 for (node = item->node->children; node != NULL;
2202 node = node->next) {
2203 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2205 new_item = FOLDER_ITEM(node->data);
2206 folder_item_destroy(cur_item);
2212 new_item = cur_item;
2213 debug_print("new folder '%s' found.\n", new_item->path);
2214 folder_item_append(item, new_item);
2217 if (!strcmp(new_item->path, "INBOX")) {
2218 new_item->stype = F_INBOX;
2219 folder->inbox = new_item;
2220 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2223 base = g_path_get_basename(new_item->path);
2225 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2226 new_item->stype = F_OUTBOX;
2227 folder->outbox = new_item;
2228 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2229 new_item->stype = F_DRAFT;
2230 folder->draft = new_item;
2231 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2232 new_item->stype = F_QUEUE;
2233 folder->queue = new_item;
2234 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2235 new_item->stype = F_TRASH;
2236 folder->trash = new_item;
2241 if (new_item->no_sub == FALSE)
2242 imap_scan_tree_recursive(session, new_item, subs_only);
2245 g_slist_free(item_list);
2247 return MAILIMAP_NO_ERROR;
2250 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2252 IMAPSession *session = imap_session_get(folder);
2254 gchar *wildcard_path;
2258 GSList *item_list = NULL, *cur;
2259 GList *child_list = NULL, *tmplist = NULL;
2260 GSList *sub_list = NULL;
2266 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2269 wildcard[0] = separator;
2272 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2276 real_path = g_strdup("");
2279 Xstrcat_a(wildcard_path, real_path, wildcard,
2280 {g_free(real_path); return NULL;});
2284 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2285 item->path?item->path:item->name);
2287 statusbar_print_all(_("Looking for subfolders of %s..."),
2288 item->path?item->path:item->name);
2290 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2292 statusbar_pop_all();
2295 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2296 lep_list, real_path, FALSE);
2297 mailimap_list_result_free(lep_list);
2299 for (cur = item_list; cur != NULL; cur = cur->next) {
2300 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2302 tmplist = imap_scan_subtree(folder, cur_item,
2303 unsubs_only, recursive);
2305 child_list = g_list_concat(child_list, tmplist);
2307 child_list = g_list_prepend(child_list,
2308 imap_get_real_path(session,
2309 IMAP_FOLDER(folder), cur_item->path));
2311 folder_item_destroy(cur_item);
2313 child_list = g_list_reverse(child_list);
2314 g_slist_free(item_list);
2317 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2319 statusbar_pop_all();
2322 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2323 lep_list, real_path, FALSE);
2324 mailimap_list_result_free(lep_list);
2326 for (cur = sub_list; cur != NULL; cur = cur->next) {
2327 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2328 GList *oldlitem = NULL;
2329 gchar *tmp = imap_get_real_path(session,
2330 IMAP_FOLDER(folder), cur_item->path);
2331 folder_item_destroy(cur_item);
2332 oldlitem = g_list_find_custom(
2333 child_list, tmp, (GCompareFunc)strcmp2);
2335 child_list = g_list_remove_link(child_list, oldlitem);
2336 g_free(oldlitem->data);
2337 g_list_free(oldlitem);
2343 statusbar_pop_all();
2348 static gint imap_create_tree(Folder *folder)
2350 g_return_val_if_fail(folder != NULL, -1);
2351 g_return_val_if_fail(folder->node != NULL, -1);
2352 g_return_val_if_fail(folder->node->data != NULL, -1);
2353 g_return_val_if_fail(folder->account != NULL, -1);
2355 imap_scan_tree(folder);
2356 imap_create_missing_folders(folder);
2361 static void imap_create_missing_folders(Folder *folder)
2363 g_return_if_fail(folder != NULL);
2366 folder->inbox = imap_create_special_folder
2367 (folder, F_INBOX, "INBOX");
2369 folder->trash = imap_create_special_folder
2370 (folder, F_TRASH, "Trash");
2372 folder->queue = imap_create_special_folder
2373 (folder, F_QUEUE, "Queue");
2374 if (!folder->outbox)
2375 folder->outbox = imap_create_special_folder
2376 (folder, F_OUTBOX, "Sent");
2378 folder->draft = imap_create_special_folder
2379 (folder, F_DRAFT, "Drafts");
2382 static FolderItem *imap_create_special_folder(Folder *folder,
2383 SpecialFolderItemType stype,
2387 FolderItem *new_item;
2389 g_return_val_if_fail(folder != NULL, NULL);
2390 g_return_val_if_fail(folder->node != NULL, NULL);
2391 g_return_val_if_fail(folder->node->data != NULL, NULL);
2392 g_return_val_if_fail(folder->account != NULL, NULL);
2393 g_return_val_if_fail(name != NULL, NULL);
2395 item = FOLDER_ITEM(folder->node->data);
2396 new_item = imap_create_folder(folder, item, name);
2399 g_warning("Can't create '%s'\n", name);
2400 if (!folder->inbox) return NULL;
2402 new_item = imap_create_folder(folder, folder->inbox, name);
2404 g_warning("Can't create '%s' under INBOX\n", name);
2406 new_item->stype = stype;
2408 new_item->stype = stype;
2413 static gchar *imap_folder_get_path(Folder *folder)
2417 g_return_val_if_fail(folder != NULL, NULL);
2418 g_return_val_if_fail(folder->account != NULL, NULL);
2420 folder_path = g_strconcat(get_imap_cache_dir(),
2422 folder->account->recv_server,
2424 folder->account->userid,
2430 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2432 gchar *folder_path, *path;
2434 g_return_val_if_fail(folder != NULL, NULL);
2435 g_return_val_if_fail(item != NULL, NULL);
2436 folder_path = imap_folder_get_path(folder);
2438 g_return_val_if_fail(folder_path != NULL, NULL);
2439 if (folder_path[0] == G_DIR_SEPARATOR) {
2441 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2444 path = g_strdup(folder_path);
2447 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2448 folder_path, G_DIR_SEPARATOR_S,
2451 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2454 g_free(folder_path);
2459 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2462 gchar *dirpath, *imap_path;
2463 IMAPSession *session;
2464 FolderItem *new_item;
2469 gboolean no_select = FALSE, no_sub = FALSE;
2470 gboolean exist = FALSE;
2472 g_return_val_if_fail(folder != NULL, NULL);
2473 g_return_val_if_fail(folder->account != NULL, NULL);
2474 g_return_val_if_fail(parent != NULL, NULL);
2475 g_return_val_if_fail(name != NULL, NULL);
2477 debug_print("getting session...\n");
2478 session = imap_session_get(folder);
2483 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2484 dirpath = g_strdup(name);
2485 }else if (parent->path)
2486 dirpath = g_strconcat(parent->path, "/", name, NULL);
2487 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2488 dirpath = g_strdup(name);
2489 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2492 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2493 strtailchomp(imap_dir, '/');
2494 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2496 dirpath = g_strdup(name);
2500 /* keep trailing directory separator to create a folder that contains
2502 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2504 strtailchomp(dirpath, '/');
2505 Xstrdup_a(new_name, name, {
2509 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2510 imap_path_separator_subst(imap_path, separator);
2511 /* remove trailing / for display */
2512 strtailchomp(new_name, '/');
2514 if (strcmp(dirpath, "INBOX") != 0) {
2519 argbuf = g_ptr_array_new();
2520 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2521 if (r != MAILIMAP_NO_ERROR) {
2522 imap_handle_error(SESSION(session), r);
2523 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2526 ptr_array_free_strings(argbuf);
2527 g_ptr_array_free(argbuf, TRUE);
2531 if (clist_count(lep_list) > 0)
2533 mailimap_list_result_free(lep_list);
2536 ok = imap_cmd_create(session, imap_path);
2537 if (ok != MAILIMAP_NO_ERROR) {
2538 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2543 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2544 if (r == MAILIMAP_NO_ERROR) {
2545 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2546 lep_list, dirpath, TRUE);
2548 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2549 no_select = cur_item->no_select;
2550 no_sub = cur_item->no_sub;
2551 g_slist_free(item_list);
2553 mailimap_list_result_free(lep_list);
2555 imap_handle_error(SESSION(session), r);
2558 imap_threaded_subscribe(folder, imap_path, TRUE);
2562 /* just get flags */
2563 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2564 if (r == MAILIMAP_NO_ERROR) {
2565 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2566 lep_list, dirpath, TRUE);
2568 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2569 no_select = cur_item->no_select;
2570 no_sub = cur_item->no_sub;
2571 g_slist_free(item_list);
2573 mailimap_list_result_free(lep_list);
2575 imap_handle_error(SESSION(session), r);
2579 new_item = folder_item_new(folder, new_name, dirpath);
2580 new_item->no_select = no_select;
2581 new_item->no_sub = no_sub;
2582 folder_item_append(parent, new_item);
2586 dirpath = folder_item_get_path(new_item);
2587 if (!is_dir_exist(dirpath))
2588 make_dir_hier(dirpath);
2592 /* folder existed, scan it */
2593 imap_scan_required(folder, new_item);
2594 folder_item_scan_full(new_item, FALSE);
2600 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2605 gchar *real_oldpath;
2606 gchar *real_newpath;
2608 gchar *old_cache_dir;
2609 gchar *new_cache_dir;
2610 IMAPSession *session;
2613 gint exists, recent, unseen;
2614 guint32 uid_validity;
2616 g_return_val_if_fail(folder != NULL, -1);
2617 g_return_val_if_fail(item != NULL, -1);
2618 g_return_val_if_fail(item->path != NULL, -1);
2619 g_return_val_if_fail(name != NULL, -1);
2621 debug_print("getting session...\n");
2622 session = imap_session_get(folder);
2627 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2628 g_warning(_("New folder name must not contain the namespace "
2633 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2635 g_free(session->mbox);
2636 session->mbox = NULL;
2637 session->exists = 0;
2638 session->recent = 0;
2639 session->expunge = 0;
2640 ok = imap_cmd_examine(session, "INBOX",
2641 &exists, &recent, &unseen, &uid_validity, FALSE);
2642 if (ok != MAILIMAP_NO_ERROR) {
2643 g_free(real_oldpath);
2647 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2648 if (strchr(item->path, G_DIR_SEPARATOR)) {
2649 dirpath = g_path_get_dirname(item->path);
2650 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2653 newpath = g_strdup(name);
2655 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2656 imap_path_separator_subst(real_newpath, separator);
2658 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2659 if (ok != MAILIMAP_NO_ERROR) {
2660 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2661 real_oldpath, real_newpath);
2662 g_free(real_oldpath);
2664 g_free(real_newpath);
2668 item->name = g_strdup(name);
2670 old_cache_dir = folder_item_get_path(item);
2672 paths[0] = g_strdup(item->path);
2674 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2675 imap_rename_folder_func, paths);
2677 if (is_dir_exist(old_cache_dir)) {
2678 new_cache_dir = folder_item_get_path(item);
2679 if (rename(old_cache_dir, new_cache_dir) < 0) {
2680 FILE_OP_ERROR(old_cache_dir, "rename");
2682 g_free(new_cache_dir);
2685 g_free(old_cache_dir);
2688 g_free(real_oldpath);
2689 g_free(real_newpath);
2693 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2697 IMAPSession *session;
2698 debug_print("getting session...\n");
2700 session = imap_session_get(folder);
2704 if (item && item->path) {
2705 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2708 if (!strcmp(path, "INBOX") && sub == FALSE)
2710 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2711 r = imap_threaded_subscribe(folder, path, sub);
2714 r = imap_threaded_subscribe(folder, rpath, sub);
2720 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2723 IMAPSession *session;
2726 gboolean selected_folder;
2728 g_return_val_if_fail(folder != NULL, -1);
2729 g_return_val_if_fail(item != NULL, -1);
2730 g_return_val_if_fail(item->path != NULL, -1);
2732 debug_print("getting session...\n");
2733 session = imap_session_get(folder);
2737 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2739 imap_threaded_subscribe(folder, path, FALSE);
2741 selected_folder = (session->mbox != NULL) &&
2742 (!strcmp(session->mbox, item->path));
2743 if (selected_folder) {
2744 ok = imap_cmd_close(session);
2745 if (ok != MAILIMAP_NO_ERROR) {
2746 debug_print("close err %d\n", ok);
2747 imap_handle_error(SESSION(session), ok);
2751 ok = imap_cmd_delete(session, path);
2752 if (ok != MAILIMAP_NO_ERROR) {
2753 gchar *tmp = g_strdup_printf("%s%c", path,
2754 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2757 ok = imap_cmd_delete(session, path);
2760 if (ok != MAILIMAP_NO_ERROR) {
2761 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2767 cache_dir = folder_item_get_path(item);
2768 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2769 g_warning("can't remove directory '%s'\n", cache_dir);
2771 folder_item_remove(item);
2775 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2779 g_return_val_if_fail(item != NULL, -1);
2780 g_return_val_if_fail(item->folder != NULL, -1);
2781 g_return_val_if_fail(item->node != NULL, -1);
2783 node = item->node->children;
2784 while (node != NULL) {
2786 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2790 debug_print("IMAP removing %s\n", item->path);
2792 if (imap_remove_all_msg(folder, item) < 0)
2794 return imap_remove_folder_real(folder, item);
2797 typedef struct _uncached_data {
2798 IMAPSession *session;
2800 MsgNumberList *numlist;
2807 static void *imap_get_uncached_messages_thread(void *data)
2809 uncached_data *stuff = (uncached_data *)data;
2810 IMAPSession *session = stuff->session;
2811 FolderItem *item = stuff->item;
2812 MsgNumberList *numlist = stuff->numlist;
2813 GSList *newlist = NULL;
2814 GSList *llast = NULL;
2815 GSList *seq_list, *cur;
2816 gboolean got_alien_tags;
2818 debug_print("uncached_messages\n");
2820 if (session == NULL || item == NULL || item->folder == NULL
2821 || FOLDER_CLASS(item->folder) != &imap_class) {
2826 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2827 debug_print("get msgs info\n");
2828 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2829 struct mailimap_set * imapset;
2835 if (session->cancelled)
2838 imapset = cur->data;
2840 r = imap_threaded_fetch_env(session->folder,
2841 imapset, &env_list);
2842 if (r != MAILIMAP_NO_ERROR) {
2843 imap_handle_error(SESSION(session), r);
2851 session_set_access_time(SESSION(session));
2854 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2855 struct imap_fetch_env_info * info;
2857 GSList *tags = NULL, *cur = NULL;
2858 info = carray_get(env_list, i);
2859 tags = carray_get(env_list, i+1);
2860 msginfo = imap_envelope_from_lep(info, item);
2861 if (msginfo == NULL) {
2862 slist_free_strings(tags);
2867 g_slist_free(msginfo->tags);
2868 msginfo->tags = NULL;
2870 for (cur = tags; cur; cur = cur->next) {
2871 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2873 id = tags_get_id_for_str(real_tag);
2875 id = tags_add_tag(real_tag);
2876 got_alien_tags = TRUE;
2878 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2879 msginfo->tags = g_slist_append(
2881 GINT_TO_POINTER(id));
2885 slist_free_strings(tags);
2887 msginfo->folder = item;
2889 llast = newlist = g_slist_append(newlist, msginfo);
2891 llast = g_slist_append(llast, msginfo);
2892 llast = llast->next;
2897 imap_fetch_env_free(env_list);
2900 if (got_alien_tags) {
2902 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2905 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2906 struct mailimap_set * imapset;
2908 imapset = cur->data;
2909 mailimap_set_free(imapset);
2912 session_set_access_time(SESSION(session));
2917 #define MAX_MSG_NUM 50
2919 static GSList *imap_get_uncached_messages(IMAPSession *session,
2921 MsgNumberList *numlist,
2924 GSList *result = NULL;
2926 uncached_data *data = g_new0(uncached_data, 1);
2931 data->total = g_slist_length(numlist);
2932 data->ok = MAILIMAP_NO_ERROR;
2933 debug_print("messages list : %i\n", data->total);
2935 while (cur != NULL) {
2936 GSList * partial_result;
2944 while (count < MAX_MSG_NUM) {
2949 if (newlist == NULL)
2950 llast = newlist = g_slist_append(newlist, p);
2952 llast = g_slist_append(llast, p);
2953 llast = llast->next;
2963 data->session = session;
2965 data->numlist = newlist;
2968 if (prefs_common.work_offline &&
2969 !inc_offline_should_override(FALSE,
2970 _("Claws Mail needs network access in order "
2971 "to access the IMAP server."))) {
2977 (GSList *)imap_get_uncached_messages_thread(data);
2979 if (data->ok != MAILIMAP_NO_ERROR) {
2982 statusbar_progress_all(data->cur,data->total, 1);
2984 g_slist_free(newlist);
2986 result = g_slist_concat(result, partial_result);
2991 statusbar_progress_all(0,0,0);
2992 statusbar_pop_all();
2997 static void imap_delete_all_cached_messages(FolderItem *item)
3001 g_return_if_fail(item != NULL);
3002 g_return_if_fail(item->folder != NULL);
3003 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3005 debug_print("Deleting all cached messages...\n");
3007 dir = folder_item_get_path(item);
3008 if (is_dir_exist(dir))
3009 remove_all_numbered_files(dir);
3012 debug_print("done.\n");
3015 gchar imap_get_path_separator_for_item(FolderItem *item)
3017 Folder *folder = NULL;
3018 IMAPFolder *imap_folder = NULL;
3019 IMAPSession *session = NULL;
3024 folder = item->folder;
3029 imap_folder = IMAP_FOLDER(folder);
3034 debug_print("getting session...");
3035 session = imap_session_get(FOLDER(folder));
3036 result = imap_get_path_separator(session, imap_folder, item->path);
3040 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
3044 gchar separator = '\0';
3046 g_return_val_if_fail(session != NULL, '/');
3047 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3049 if (r != MAILIMAP_NO_ERROR) {
3050 imap_handle_error(SESSION(session), r);
3051 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3055 if (clist_count(lep_list) > 0) {
3056 clistiter * iter = clist_begin(lep_list);
3057 struct mailimap_mailbox_list * mb;
3058 mb = clist_content(iter);
3060 separator = mb->mb_delimiter;
3061 debug_print("got separator: %c\n", folder->last_seen_separator);
3063 mailimap_list_result_free(lep_list);
3067 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3069 gchar separator = '/';
3071 if (folder->last_seen_separator == 0) {
3072 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
3075 if (folder->last_seen_separator == 0) {
3076 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
3079 if (folder->last_seen_separator != 0) {
3080 debug_print("using separator: %c\n", folder->last_seen_separator);
3081 return folder->last_seen_separator;
3087 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3092 g_return_val_if_fail(folder != NULL, NULL);
3093 g_return_val_if_fail(path != NULL, NULL);
3095 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3096 separator = imap_get_path_separator(session, folder, path);
3097 imap_path_separator_subst(real_path, separator);
3102 static gint imap_set_message_flags(IMAPSession *session,
3103 MsgNumberList *numlist,
3112 IMAPFolder *folder = NULL;
3113 GSList *sorted_list = NULL;
3115 if (numlist == NULL || session == NULL)
3116 return MAILIMAP_ERROR_BAD_STATE;
3118 folder = IMAP_FOLDER(session->folder);
3120 sorted_list = g_slist_copy(numlist);
3121 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3123 cur = g_slist_last(sorted_list);
3126 total = GPOINTER_TO_INT(cur->data);
3128 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3130 statusbar_print_all(_("Flagging messages..."));
3132 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3133 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3134 struct mailimap_set_item *set_item = clist_content(
3135 clist_begin(imapset->set_list));
3137 statusbar_progress_all(set_item->set_first, total, 1);
3139 ok = imap_cmd_store(session, imapset,
3140 flags, tags, is_set);
3141 statusbar_progress_all(set_item->set_last, total, 1);
3142 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3143 /* reduce max set size */
3144 folder->max_set_size /= 2;
3146 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3151 g_slist_free(sorted_list);
3153 statusbar_progress_all(0,0,0);
3154 statusbar_pop_all();
3156 imap_lep_set_free(seq_list);
3161 typedef struct _select_data {
3162 IMAPSession *session;
3167 guint32 *uid_validity;
3171 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3173 gint *exists, gint *recent, gint *unseen,
3174 guint32 *uid_validity, gint *can_create_flags,
3179 gint exists_, recent_, unseen_;
3180 guint32 uid_validity_;
3181 gint can_create_flags_;
3182 const gchar *path = item ? item->path:NULL;
3185 return MAILIMAP_ERROR_BAD_STATE;
3188 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3189 if (session->mbox && strcmp(session->mbox, path) == 0)
3190 return MAILIMAP_NO_ERROR;
3192 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3193 if (session->mbox && strcmp(session->mbox, path) == 0) {
3194 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3195 return MAILIMAP_NO_ERROR;
3205 uid_validity = &uid_validity_;
3206 if (!can_create_flags)
3207 can_create_flags = &can_create_flags_;
3209 g_free(session->mbox);
3210 session->mbox = NULL;
3211 session->exists = 0;
3212 session->recent = 0;
3213 session->expunge = 0;
3215 real_path = imap_get_real_path(session, folder, path);
3217 ok = imap_cmd_select(session, real_path,
3218 exists, recent, unseen, uid_validity, can_create_flags, block);
3219 if (ok != MAILIMAP_NO_ERROR) {
3220 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3222 session->mbox = g_strdup(path);
3223 session->folder_content_changed = FALSE;
3224 session->exists = *exists;
3225 session->recent = *recent;
3226 session->expunge = 0;
3227 session->unseen = *unseen;
3228 session->uid_validity = *uid_validity;
3229 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3230 session->exists, session->recent, session->expunge,
3231 session->uid_validity, *can_create_flags);
3233 if (*can_create_flags) {
3234 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3236 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3243 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3244 const gchar *path, IMAPFolderItem *item,
3246 guint32 *uid_next, guint32 *uid_validity,
3247 gint *unseen, gboolean block)
3251 struct mailimap_mailbox_data_status * data_status;
3256 real_path = imap_get_real_path(session, folder, path);
3275 if (session->mbox != NULL &&
3276 !strcmp(session->mbox, item->item.path)) {
3277 r = imap_cmd_close(session);
3278 if (r != MAILIMAP_NO_ERROR) {
3279 imap_handle_error(SESSION(session), r);
3280 debug_print("close err %d\n", r);
3285 r = imap_threaded_status(FOLDER(folder), real_path,
3286 &data_status, mask);
3289 if (r != MAILIMAP_NO_ERROR) {
3290 imap_handle_error(SESSION(session), r);
3291 debug_print("status err %d\n", r);
3295 if (data_status->st_info_list == NULL) {
3296 mailimap_mailbox_data_status_free(data_status);
3297 debug_print("status->st_info_list == NULL\n");
3298 return MAILIMAP_ERROR_BAD_STATE;
3302 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3303 iter = clist_next(iter)) {
3304 struct mailimap_status_info * info;
3306 info = clist_content(iter);
3307 switch (info->st_att) {
3308 case MAILIMAP_STATUS_ATT_MESSAGES:
3310 * messages = info->st_value;
3311 got_values |= 1 << 0;
3315 case MAILIMAP_STATUS_ATT_UIDNEXT:
3317 * uid_next = info->st_value;
3318 got_values |= 1 << 2;
3322 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3324 * uid_validity = info->st_value;
3325 got_values |= 1 << 3;
3329 case MAILIMAP_STATUS_ATT_UNSEEN:
3331 * unseen = info->st_value;
3332 got_values |= 1 << 4;
3337 mailimap_mailbox_data_status_free(data_status);
3339 if (got_values != mask) {
3340 g_warning("status: incomplete values received (%d)\n", got_values);
3342 return MAILIMAP_NO_ERROR;
3345 static void imap_free_capabilities(IMAPSession *session)
3347 slist_free_strings(session->capability);
3348 g_slist_free(session->capability);
3349 session->capability = NULL;
3352 /* low-level IMAP4rev1 commands */
3354 static gint imap_cmd_login(IMAPSession *session,
3355 const gchar *user, const gchar *pass,