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 (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
684 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
685 case MAILIMAP_ERROR_SSL:
686 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
691 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
696 if (session && is_fatal(libetpan_errcode)) {
697 imap_disc_session_destroy(IMAP_SESSION(session));
698 } else if (session && !is_fatal(libetpan_errcode)) {
699 if (IMAP_SESSION(session)->busy)
700 unlock_session(IMAP_SESSION(session));
704 static Folder *imap_folder_new(const gchar *name, const gchar *path)
708 folder = (Folder *)g_new0(IMAPFolder, 1);
709 folder->klass = &imap_class;
710 imap_folder_init(folder, name, path);
715 static void imap_folder_destroy(Folder *folder)
717 while (imap_folder_get_refcnt(folder) > 0)
718 gtk_main_iteration();
720 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
724 static void imap_folder_init(Folder *folder, const gchar *name,
727 folder_remote_folder_init((Folder *)folder, name, path);
728 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
731 static FolderItem *imap_folder_item_new(Folder *folder)
733 IMAPFolderItem *item;
735 item = g_new0(IMAPFolderItem, 1);
738 item->uid_list = NULL;
740 return (FolderItem *)item;
743 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
745 IMAPFolderItem *item = (IMAPFolderItem *)_item;
747 g_return_if_fail(item != NULL);
748 g_slist_free(item->uid_list);
753 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
755 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
758 g_slist_free(item->uid_list);
759 item->uid_list = NULL;
764 static void imap_reset_uid_lists(Folder *folder)
766 if(folder->node == NULL)
769 /* Destroy all uid lists and rest last uid */
770 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
773 static int imap_get_capabilities(IMAPSession *session)
775 struct mailimap_capability_data *capabilities = NULL;
779 if (session->capability != NULL)
780 return MAILIMAP_NO_ERROR;
782 capabilities = imap_threaded_capability(session->folder, &result);
784 if (result != MAILIMAP_NO_ERROR) {
788 if (capabilities == NULL) {
789 return MAILIMAP_NO_ERROR;
792 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
793 cur = clist_next(cur)) {
794 struct mailimap_capability * cap =
796 if (!cap || cap->cap_data.cap_name == NULL)
798 session->capability = g_slist_append
799 (session->capability,
800 g_strdup(cap->cap_data.cap_name));
801 debug_print("got capa %s\n", cap->cap_data.cap_name);
803 mailimap_capability_data_free(capabilities);
804 return MAILIMAP_NO_ERROR;
807 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
810 for (cur = session->capability; cur; cur = cur->next) {
811 if (!g_ascii_strcasecmp(cur->data, cap))
817 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
820 gint ok = MAILIMAP_ERROR_LOGIN;
821 static time_t last_login_err = 0;
822 gchar *ext_info = "";
824 gchar *server = NULL;
825 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
826 imap_handle_error(SESSION(session), r);
829 server = g_strdup(SESSION(session)->server);
832 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
834 case IMAP_AUTH_CRAM_MD5:
835 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
837 case IMAP_AUTH_DIGEST_MD5:
838 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
840 case IMAP_AUTH_LOGIN:
841 ok = imap_cmd_login(session, user, pass, "LOGIN");
843 case IMAP_AUTH_GSSAPI:
844 ok = imap_cmd_login(session, user, pass, "GSSAPI");
847 debug_print("capabilities:\n"
853 imap_has_capability(session, "ANONYMOUS"),
854 imap_has_capability(session, "CRAM-MD5"),
855 imap_has_capability(session, "DIGEST-MD5"),
856 imap_has_capability(session, "LOGIN"),
857 imap_has_capability(session, "GSSAPI"));
858 if (imap_has_capability(session, "CRAM-MD5"))
859 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
860 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
861 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
862 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
863 ok = imap_cmd_login(session, user, pass, "GSSAPI");
864 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
865 ok = imap_cmd_login(session, user, pass, "LOGIN");
868 if (ok == MAILIMAP_NO_ERROR)
869 session->authenticated = TRUE;
871 if (type == IMAP_AUTH_CRAM_MD5) {
872 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
873 "compiled with SASL support and the "
874 "CRAM-MD5 SASL plugin is installed.");
877 if (type == IMAP_AUTH_DIGEST_MD5) {
878 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
879 "compiled with SASL support and the "
880 "DIGEST-MD5 SASL plugin is installed.");
883 if (time(NULL) - last_login_err > 10) {
884 if (!prefs_common.no_recv_err_panel) {
885 alertpanel_error_log(_("Connection to %s failed: "
889 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
890 "login refused.%s\n"),
894 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(session)->sock = NULL;
910 session_destroy(SESSION(session));
913 rfolder->session = NULL;
914 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
915 " disconnected. Reconnecting...\n"),
916 folder->account->recv_server);
917 statusbar_print_all(_("IMAP4 connection to %s has been"
918 " disconnected. Reconnecting...\n"),
919 folder->account->recv_server);
920 SESSION(session)->state = SESSION_DISCONNECTED;
921 SESSION(session)->sock = NULL;
922 session_destroy(SESSION(session));
923 /* Clear folders session to make imap_session_get create
924 a new session, because of rfolder->session == NULL
925 it will not try to reconnect again and so avoid an
927 debug_print("getting session...\n");
928 session = imap_session_get(folder);
929 rfolder->session = SESSION(session);
935 static IMAPSession *imap_session_get(Folder *folder)
937 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
938 IMAPSession *session = NULL;
939 gint r = MAILIMAP_NO_ERROR;
941 g_return_val_if_fail(folder != NULL, NULL);
942 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
943 g_return_val_if_fail(folder->account != NULL, NULL);
945 if (prefs_common.work_offline &&
946 !inc_offline_should_override(FALSE,
947 _("Claws Mail needs network access in order "
948 "to access the IMAP server."))) {
952 /* Make sure we have a session */
953 if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
954 session = IMAP_SESSION(rfolder->session);
955 } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
956 session_destroy(SESSION(rfolder->session));
957 rfolder->session = NULL;
959 } else if (rfolder->connecting) {
960 debug_print("already connecting\n");
964 imap_reset_uid_lists(folder);
965 if (time(NULL) - rfolder->last_failure <= 2)
967 rfolder->connecting = TRUE;
968 session = imap_session_new(folder, folder->account);
970 if(session == NULL) {
971 rfolder->last_failure = time(NULL);
972 rfolder->connecting = FALSE;
976 /* Make sure session is authenticated */
977 if (!IMAP_SESSION(session)->authenticated)
978 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
980 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
981 rfolder->session = NULL;
983 imap_threaded_disconnect(session->folder);
984 SESSION(session)->state = SESSION_DISCONNECTED;
985 SESSION(session)->sock = NULL;
986 session_destroy(SESSION(session));
988 rfolder->last_failure = time(NULL);
989 rfolder->connecting = FALSE;
993 /* I think the point of this code is to avoid sending a
994 * keepalive if we've used the session recently and therefore
995 * think it's still alive. Unfortunately, most of the code
996 * does not yet check for errors on the socket, and so if the
997 * connection drops we don't notice until the timeout expires.
998 * A better solution than sending a NOOP every time would be
999 * for every command to be prepared to retry until it is
1000 * successfully sent. -- mbp */
1001 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1002 /* verify that the session is still alive */
1003 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1004 debug_print("disconnected!\n");
1006 session = imap_reconnect_if_possible(folder, session);
1008 rfolder->session = NULL;
1009 rfolder->connecting = FALSE;
1010 session = imap_session_get(folder);
1014 session->cancelled = FALSE;
1017 rfolder->session = SESSION(session);
1018 rfolder->connecting = FALSE;
1020 return IMAP_SESSION(session);
1023 static IMAPSession *imap_session_new(Folder * folder,
1024 const PrefsAccount *account)
1026 IMAPSession *session;
1029 int authenticated = FALSE;
1031 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1032 /* FIXME: IMAP over SSL only... */
1035 port = account->set_imapport ? account->imapport
1036 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1037 ssl_type = account->ssl_imap;
1039 if (account->ssl_imap != SSL_NONE) {
1040 if (alertpanel_full(_("Insecure connection"),
1041 _("This connection is configured to be secured "
1042 "using SSL, but SSL is not available in this "
1043 "build of Claws Mail. \n\n"
1044 "Do you want to continue connecting to this "
1045 "server? The communication would not be "
1047 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1048 NULL, FALSE, NULL, ALERT_WARNING,
1049 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1052 port = account->set_imapport ? account->imapport
1057 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1059 if (account->set_tunnelcmd) {
1060 r = imap_threaded_connect_cmd(folder,
1062 account->recv_server,
1068 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1069 if (ssl_type == SSL_TUNNEL) {
1070 r = imap_threaded_connect_ssl(folder,
1071 account->recv_server,
1077 r = imap_threaded_connect(folder,
1078 account->recv_server,
1083 statuswindow_pop_all();
1084 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1085 authenticated = TRUE;
1087 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1088 authenticated = FALSE;
1091 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1092 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1093 if (r == MAILIMAP_ERROR_SSL)
1094 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1098 imap_handle_error(NULL, r);
1100 if(!prefs_common.no_recv_err_panel) {
1101 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1102 account->recv_server, port);
1104 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1105 account->recv_server, port);
1111 session = g_new0(IMAPSession, 1);
1112 session_init(SESSION(session));
1113 SESSION(session)->type = SESSION_IMAP;
1114 SESSION(session)->server = g_strdup(account->recv_server);
1115 SESSION(session)->port = port;
1116 SESSION(session)->sock = NULL;
1118 SESSION(session)->destroy = imap_session_destroy;
1120 session->capability = NULL;
1122 session->authenticated = authenticated;
1123 session->mbox = NULL;
1124 session->exists = 0;
1125 session->recent = 0;
1126 session->expunge = 0;
1127 session->cmd_count = 0;
1128 session->folder = folder;
1129 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1131 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1132 if (account->ssl_imap == SSL_STARTTLS) {
1135 ok = imap_cmd_starttls(session);
1136 if (ok != MAILIMAP_NO_ERROR) {
1137 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1138 if (!is_fatal(ok)) {
1139 SESSION(session)->sock = NULL;
1140 session_destroy(SESSION(session));
1145 imap_free_capabilities(session);
1146 session->authenticated = FALSE;
1147 session->uidplus = FALSE;
1148 session->cmd_count = 1;
1151 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1152 (session->authenticated) ? "pre" : "un");
1157 static gint imap_session_authenticate(IMAPSession *session,
1158 const PrefsAccount *account)
1160 gchar *pass, *acc_pass;
1161 gboolean failed = FALSE;
1162 gint ok = MAILIMAP_NO_ERROR;
1163 g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1164 acc_pass = account->passwd;
1167 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1169 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1171 return MAILIMAP_NO_ERROR;
1172 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1174 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1177 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1178 account->recv_server);
1179 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1180 statusbar_pop_all();
1182 if (!failed && !is_fatal(ok)) {
1187 if (prefs_common.no_recv_err_panel) {
1188 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1189 mainwindow_show_error();
1191 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1197 statuswindow_pop_all();
1198 session->authenticated = TRUE;
1199 return MAILIMAP_NO_ERROR;
1202 static void imap_session_destroy(Session *session)
1204 if (session->state != SESSION_DISCONNECTED)
1205 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1207 imap_free_capabilities(IMAP_SESSION(session));
1208 g_free(IMAP_SESSION(session)->mbox);
1211 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1213 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1216 static guint get_file_size_with_crs(const gchar *filename)
1222 if (filename == NULL)
1225 fp = fopen(filename, "rb");
1229 while (fgets(buf, sizeof (buf), fp) != NULL) {
1231 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1239 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1241 gchar *path, *filename;
1243 path = folder_item_get_path(item);
1245 if (!is_dir_exist(path)) {
1250 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1253 if (is_file_exist(filename)) {
1254 claws_unlink(filename);
1259 typedef struct _TagsData {
1262 IMAPFolderItem *item;
1265 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1267 IMAPSession *session;
1268 gint ok, can_create_tags;
1269 Folder *folder = NULL;
1270 TagsData *ht_data = NULL;
1273 g_return_if_fail(item != NULL);
1274 g_return_if_fail(msginfo != NULL);
1276 folder = item->folder;
1277 debug_print("getting session...\n");
1278 session = imap_session_get(folder);
1281 debug_print("can't get session\n");
1285 ok = imap_select(session, IMAP_FOLDER(folder), item,
1286 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1288 if (ok != MAILIMAP_NO_ERROR) {
1293 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1296 if (IMAP_FOLDER_ITEM(item)->batching) {
1297 /* instead of performing an UID STORE command for each message change,
1298 * as a lot of them can change "together", we just fill in hashtables
1299 * and defer the treatment so that we're able to send only one
1302 debug_print("IMAP batch mode on, deferring tags change\n");
1303 for (cur = tags_set; cur; cur = cur->next) {
1304 gint cur_tag = GPOINTER_TO_INT(cur->data);
1306 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1307 GINT_TO_POINTER(cur_tag));
1308 if (ht_data == NULL) {
1309 ht_data = g_new0(TagsData, 1);
1310 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1311 ht_data->item = IMAP_FOLDER_ITEM(item);
1312 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1313 GINT_TO_POINTER(cur_tag), ht_data);
1315 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1318 for (cur = tags_unset; cur; cur = cur->next) {
1319 gint cur_tag = GPOINTER_TO_INT(cur->data);
1321 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1322 GINT_TO_POINTER(cur_tag));
1323 if (ht_data == NULL) {
1324 ht_data = g_new0(TagsData, 1);
1325 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1326 ht_data->item = IMAP_FOLDER_ITEM(item);
1327 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1328 GINT_TO_POINTER(cur_tag), ht_data);
1330 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1334 GSList *list_set = NULL;
1335 GSList *list_unset = NULL;
1338 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1339 numlist.next = NULL;
1341 debug_print("IMAP changing tags NOW\n");
1342 for (cur = tags_set; cur; cur = cur->next) {
1343 gint cur_tag = GPOINTER_TO_INT(cur->data);
1344 const gchar *str = tags_get_tag(cur_tag);
1345 list_set = g_slist_prepend(list_set, g_strdup(str));
1348 ok = imap_set_message_flags(session,
1349 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1350 slist_free_strings(list_set);
1351 g_slist_free(list_set);
1352 if (ok != MAILIMAP_NO_ERROR) {
1357 for (cur = tags_unset; cur; cur = cur->next) {
1358 gint cur_tag = GPOINTER_TO_INT(cur->data);
1359 const gchar *str = tags_get_tag(cur_tag);
1360 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1363 ok = imap_set_message_flags(session,
1364 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1365 slist_free_strings(list_unset);
1366 g_slist_free(list_unset);
1367 if (ok != MAILIMAP_NO_ERROR) {
1374 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1375 gboolean headers, gboolean body)
1377 gchar *path, *filename;
1378 IMAPSession *session;
1381 g_return_val_if_fail(folder != NULL, NULL);
1382 g_return_val_if_fail(item != NULL, NULL);
1387 path = folder_item_get_path(item);
1388 if (!is_dir_exist(path))
1389 make_dir_hier(path);
1390 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1392 debug_print("trying to fetch cached %s\n", filename);
1393 if (is_file_exist(filename)) {
1394 /* see whether the local file represents the whole message
1395 * or not. As the IMAP server reports size with \r chars,
1396 * we have to update the local file (UNIX \n only) size */
1397 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1398 guint have_size = -1;
1401 debug_print("message %d has been already %scached.\n", uid,
1402 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1404 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1405 have_size = get_file_size_with_crs(filename);
1406 if (cached && (cached->size <= have_size || !body)) {
1407 procmsg_msginfo_free(cached);
1408 ok = file_strip_crs(filename);
1409 if (ok == 0 && cached && cached->size <= have_size) {
1410 /* we have it all and stripped */
1411 debug_print("...fully cached in fact; setting flag.\n");
1412 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1415 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1416 debug_print("message not cached and file recent, considering file complete\n");
1417 ok = file_strip_crs(filename);
1421 procmsg_msginfo_free(cached);
1424 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1425 procmsg_msginfo_free(cached);
1429 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1431 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1432 procmsg_msginfo_free(cached);
1436 debug_print("getting session...\n");
1437 session = imap_session_get(folder);
1443 session_set_access_time(SESSION(session));
1444 lock_session(session); /* unlocked later in the function */
1446 debug_print("IMAP fetching messages\n");
1447 ok = imap_select(session, IMAP_FOLDER(folder), item,
1448 NULL, NULL, NULL, NULL, NULL, FALSE);
1449 if (ok != MAILIMAP_NO_ERROR) {
1450 g_warning("can't select mailbox %s\n", item->path);
1455 session_set_access_time(SESSION(session));
1457 debug_print("getting message %d...\n", uid);
1458 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1460 if (ok != MAILIMAP_NO_ERROR) {
1461 g_warning("can't fetch message %d\n", uid);
1466 session_set_access_time(SESSION(session));
1467 unlock_session(session);
1469 ok = file_strip_crs(filename);
1471 if (ok == 0 && headers && body) {
1472 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1474 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1475 procmsg_msginfo_free(cached);
1477 } else if (ok == -1) {
1478 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1480 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1481 procmsg_msginfo_free(cached);
1487 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1489 gchar *path, *filename;
1491 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1496 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1497 procmsg_msginfo_free(cached);
1500 path = folder_item_get_path(item);
1501 if (!is_dir_exist(path))
1504 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1506 if (is_file_exist(filename)) {
1507 if (cached && cached->total_size == cached->size) {
1510 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1513 size = get_file_size_with_crs(filename);
1516 if (cached && size >= cached->size) {
1517 cached->total_size = cached->size;
1518 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1519 procmsg_msginfo_free(cached);
1523 procmsg_msginfo_free(cached);
1527 void imap_cache_msg(FolderItem *item, gint msgnum)
1529 Folder *folder = NULL;
1533 folder = item->folder;
1535 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1536 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1537 debug_print("fetched %s\n", tmp);
1542 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1543 const gchar *file, MsgFlags *flags)
1547 MsgFileInfo fileinfo;
1549 g_return_val_if_fail(file != NULL, -1);
1551 fileinfo.msginfo = NULL;
1552 fileinfo.file = (gchar *)file;
1553 fileinfo.flags = flags;
1554 file_list.data = &fileinfo;
1555 file_list.next = NULL;
1557 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1561 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1562 GRelation *relation)
1565 IMAPSession *session;
1566 guint32 last_uid = 0;
1568 MsgFileInfo *fileinfo;
1569 gint ok = MAILIMAP_NO_ERROR;
1570 gint curnum = 0, total = 0;
1571 gboolean missing_uids = FALSE;
1573 g_return_val_if_fail(folder != NULL, -1);
1574 g_return_val_if_fail(dest != NULL, -1);
1575 g_return_val_if_fail(file_list != NULL, -1);
1577 debug_print("getting session...\n");
1578 session = imap_session_get(folder);
1582 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1585 statusbar_print_all(_("Adding messages..."));
1586 total = g_slist_length(file_list);
1587 for (cur = file_list; cur != NULL; cur = cur->next) {
1588 IMAPFlags iflags = 0;
1589 guint32 new_uid = 0;
1590 gchar *real_file = NULL;
1591 fileinfo = (MsgFileInfo *)cur->data;
1593 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1596 if (fileinfo->flags) {
1597 if (MSG_IS_MARKED(*fileinfo->flags))
1598 iflags |= IMAP_FLAG_FLAGGED;
1599 if (MSG_IS_REPLIED(*fileinfo->flags))
1600 iflags |= IMAP_FLAG_ANSWERED;
1601 if (MSG_IS_FORWARDED(*fileinfo->flags))
1602 iflags |= IMAP_FLAG_FORWARDED;
1603 if (!MSG_IS_UNREAD(*fileinfo->flags))
1604 iflags |= IMAP_FLAG_SEEN;
1608 if (real_file == NULL)
1609 real_file = g_strdup(fileinfo->file);
1611 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1612 folder_has_parent_of_type(dest, F_OUTBOX) ||
1613 folder_has_parent_of_type(dest, F_DRAFT) ||
1614 folder_has_parent_of_type(dest, F_TRASH))
1615 iflags |= IMAP_FLAG_SEEN;
1617 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1620 if (ok != MAILIMAP_NO_ERROR) {
1621 g_warning("can't append message %s\n", real_file);
1624 statusbar_progress_all(0,0,0);
1625 statusbar_pop_all();
1628 debug_print("appended new message as %d\n", new_uid);
1629 /* put the local file in the imapcache, so that we don't
1630 * have to fetch it back later. */
1633 missing_uids = TRUE;
1634 debug_print("Missing UID (0)\n");
1637 gchar *cache_path = folder_item_get_path(dest);
1638 if (!is_dir_exist(cache_path))
1639 make_dir_hier(cache_path);
1640 if (is_dir_exist(cache_path)) {
1641 gchar *cache_file = g_strconcat(
1642 cache_path, G_DIR_SEPARATOR_S,
1643 itos(new_uid), NULL);
1644 copy_file(real_file, cache_file, TRUE);
1645 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1652 if (relation != NULL)
1653 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1654 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1655 GINT_TO_POINTER(new_uid));
1656 if (last_uid < new_uid) {
1663 statusbar_progress_all(0,0,0);
1664 statusbar_pop_all();
1669 imap_scan_required(folder, dest);
1671 session = imap_session_get(folder);
1677 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1678 &a, NULL, NULL, NULL, NULL, FALSE);
1683 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1685 GSList *result = NULL;
1690 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1691 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1692 start = item->set_first;
1693 end = item->set_last;
1694 for (t = start; t <= end; t++) {
1695 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1698 result = g_slist_reverse(result);
1699 if (debug_get_mode()) {
1700 debug_print("flat imap set: ");
1701 for (cur = result; cur; cur = cur->next) {
1702 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1709 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1710 MsgInfoList *msglist, GRelation *relation)
1714 GSList *seq_list, *cur;
1716 IMAPSession *session;
1717 gint ok = MAILIMAP_NO_ERROR;
1718 GRelation *uid_mapping;
1720 gboolean single = FALSE;
1722 g_return_val_if_fail(folder != NULL, -1);
1723 g_return_val_if_fail(dest != NULL, -1);
1724 g_return_val_if_fail(msglist != NULL, -1);
1726 debug_print("getting session...\n");
1727 session = imap_session_get(folder);
1733 msginfo = (MsgInfo *)msglist->data;
1734 if (msglist->next == NULL)
1736 src = msginfo->folder;
1738 g_warning("the src folder is identical to the dest.\n");
1742 if (src->folder != dest->folder) {
1743 GSList *infolist = NULL, *cur;
1745 for (cur = msglist; cur; cur = cur->next) {
1746 msginfo = (MsgInfo *)cur->data;
1747 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1748 fileinfo->file = procmsg_get_message_file(msginfo);
1749 fileinfo->flags = &(msginfo->flags);
1750 infolist = g_slist_prepend(infolist, fileinfo);
1752 infolist = g_slist_reverse(infolist);
1753 res = folder_item_add_msgs(dest, infolist, FALSE);
1754 for (cur = infolist; cur; cur = cur->next) {
1755 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1759 g_slist_free(infolist);
1763 lock_session(session); /* unlocked later in the function */
1765 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1766 NULL, NULL, NULL, NULL, NULL, FALSE);
1767 if (ok != MAILIMAP_NO_ERROR) {
1771 unlock_session(session);
1773 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1778 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1779 uid_mapping = g_relation_new(2);
1780 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1782 statusbar_print_all(_("Copying messages..."));
1783 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1784 struct mailimap_set * seq_set;
1785 struct mailimap_set * source = NULL;
1786 struct mailimap_set * dest = NULL;
1787 seq_set = cur->data;
1789 debug_print("Copying messages from %s to %s ...\n",
1790 src->path, destdir);
1792 lock_session(session); /* unlocked later in the function */
1793 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1800 if (ok == MAILIMAP_NO_ERROR) {
1801 unlock_session(session);
1802 if (relation && source && dest) {
1803 GSList *s_list = flatten_mailimap_set(source);
1804 GSList *d_list = flatten_mailimap_set(dest);
1805 GSList *s_cur, *d_cur;
1806 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1808 for (s_cur = s_list, d_cur = d_list;
1810 s_cur = s_cur->next, d_cur = d_cur->next) {
1811 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1815 debug_print("hhhmm, source list length != dest list length.\n");
1817 g_slist_free(s_list);
1818 g_slist_free(d_list);
1824 mailimap_set_free(source);
1826 mailimap_set_free(dest);
1828 if (ok != MAILIMAP_NO_ERROR) {
1829 g_relation_destroy(uid_mapping);
1830 imap_lep_set_free(seq_list);
1831 statusbar_pop_all();
1836 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1837 MsgInfo *msginfo = (MsgInfo *)cur->data;
1840 tuples = g_relation_select(uid_mapping,
1841 GINT_TO_POINTER(msginfo->msgnum),
1843 if (tuples->len > 0) {
1844 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1845 g_relation_insert(relation, msginfo,
1846 GINT_TO_POINTER(num));
1849 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1850 /* put the local file in the imapcache, so that we don't
1851 * have to fetch it back later. */
1853 gchar *cache_path = folder_item_get_path(msginfo->folder);
1854 gchar *real_file = g_strconcat(
1855 cache_path, G_DIR_SEPARATOR_S,
1856 itos(msginfo->msgnum), NULL);
1857 gchar *cache_file = NULL;
1859 cache_path = folder_item_get_path(dest);
1860 cache_file = g_strconcat(
1861 cache_path, G_DIR_SEPARATOR_S,
1863 if (!is_dir_exist(cache_path))
1864 make_dir_hier(cache_path);
1865 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1866 copy_file(real_file, cache_file, TRUE);
1867 debug_print("copied to cache: %s\n", cache_file);
1874 g_relation_insert(relation, msginfo,
1875 GINT_TO_POINTER(0));
1876 g_tuples_destroy(tuples);
1878 statusbar_pop_all();
1880 g_relation_destroy(uid_mapping);
1881 imap_lep_set_free(seq_list);
1885 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1886 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1887 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1888 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1890 imap_scan_required(folder, dest);
1891 if (ok == MAILIMAP_NO_ERROR)
1897 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1901 g_return_val_if_fail(msginfo != NULL, -1);
1903 msglist.data = msginfo;
1904 msglist.next = NULL;
1906 return imap_copy_msgs(folder, dest, &msglist, NULL);
1909 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1910 MsgInfoList *msglist, GRelation *relation)
1915 g_return_val_if_fail(folder != NULL, -1);
1916 g_return_val_if_fail(dest != NULL, -1);
1917 g_return_val_if_fail(msglist != NULL, -1);
1919 msginfo = (MsgInfo *)msglist->data;
1920 g_return_val_if_fail(msginfo->folder != NULL, -1);
1922 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1927 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1928 MsgInfoList *msglist, GRelation *relation)
1930 gchar *destdir, *dir;
1931 GSList *numlist = NULL, *cur;
1933 IMAPSession *session;
1934 gint ok = MAILIMAP_NO_ERROR;
1935 GRelation *uid_mapping;
1937 g_return_val_if_fail(folder != NULL, -1);
1938 g_return_val_if_fail(dest != NULL, -1);
1939 g_return_val_if_fail(msglist != NULL, -1);
1941 debug_print("getting session...\n");
1942 session = imap_session_get(folder);
1947 lock_session(session); /* unlocked later in the function */
1949 msginfo = (MsgInfo *)msglist->data;
1951 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1952 NULL, NULL, NULL, NULL, NULL, FALSE);
1953 if (ok != MAILIMAP_NO_ERROR) {
1957 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1960 for (cur = msglist; cur; cur = cur->next) {
1961 msginfo = (MsgInfo *)cur->data;
1962 if (!MSG_IS_DELETED(msginfo->flags))
1963 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1965 numlist = g_slist_reverse(numlist);
1967 uid_mapping = g_relation_new(2);
1968 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1970 if (numlist != NULL) {
1971 ok = imap_set_message_flags
1972 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1973 if (ok != MAILIMAP_NO_ERROR) {
1974 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1977 } /* else we just need to expunge */
1978 ok = imap_cmd_expunge(session);
1979 if (ok != MAILIMAP_NO_ERROR) {
1980 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1984 session->folder_content_changed = TRUE;
1985 unlock_session(session);
1987 dir = folder_item_get_path(msginfo->folder);
1988 if (is_dir_exist(dir)) {
1989 for (cur = msglist; cur; cur = cur->next) {
1990 msginfo = (MsgInfo *)cur->data;
1991 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1996 g_relation_destroy(uid_mapping);
1997 g_slist_free(numlist);
1999 imap_scan_required(folder, dest);
2002 if (ok == MAILIMAP_NO_ERROR)
2008 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2009 MsgInfoList *msglist, GRelation *relation)
2013 g_return_val_if_fail(folder != NULL, -1);
2014 g_return_val_if_fail(dest != NULL, -1);
2015 if (msglist == NULL)
2018 msginfo = (MsgInfo *)msglist->data;
2019 g_return_val_if_fail(msginfo->folder != NULL, -1);
2021 return imap_do_remove_msgs(folder, dest, msglist, relation);
2024 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2026 GSList *list = folder_item_get_msg_list(item);
2027 gint res = imap_remove_msgs(folder, item, list, NULL);
2028 procmsg_msg_list_free(list);
2032 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2035 /* TODO: properly implement this method */
2039 static gint imap_close(Folder *folder, FolderItem *item)
2044 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2046 FolderItem *item = NULL;
2047 IMAPSession *session;
2048 gchar *root_folder = NULL;
2050 g_return_val_if_fail(folder != NULL, -1);
2051 g_return_val_if_fail(folder->account != NULL, -1);
2053 debug_print("getting session...\n");
2054 session = imap_session_get(folder);
2056 if (!folder->node) {
2057 folder_tree_destroy(folder);
2058 item = folder_item_new(folder, folder->name, NULL);
2059 item->folder = folder;
2060 folder->node = item->node = g_node_new(item);
2065 if (folder->account->imap_dir && *folder->account->imap_dir) {
2070 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2071 extract_quote(root_folder, '"');
2072 subst_char(root_folder,
2073 imap_get_path_separator(session, IMAP_FOLDER(folder),
2078 strtailchomp(root_folder, '/');
2079 real_path = imap_get_real_path
2080 (session, IMAP_FOLDER(folder), root_folder, &r);
2083 debug_print("IMAP root directory: %s\n", real_path);
2085 /* check if root directory exist */
2087 r = imap_threaded_list(session->folder, "", real_path,
2090 if (r != MAILIMAP_NO_ERROR)
2091 imap_handle_error(SESSION(session), r);
2093 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2094 if (!folder->node) {
2095 item = folder_item_new(folder, folder->name, NULL);
2096 item->folder = folder;
2097 folder->node = item->node = g_node_new(item);
2101 mailimap_list_result_free(lep_list);
2107 item = FOLDER_ITEM(folder->node->data);
2109 if (item && !item->path && root_folder) {
2110 item->path = g_strdup(root_folder);
2113 if (!item || ((item->path || root_folder) &&
2114 strcmp2(item->path, root_folder) != 0)) {
2115 folder_tree_destroy(folder);
2116 item = folder_item_new(folder, folder->name, root_folder);
2117 item->folder = folder;
2118 folder->node = item->node = g_node_new(item);
2121 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2122 imap_create_missing_folders(folder);
2127 static gint imap_scan_tree(Folder *folder)
2129 gboolean subs_only = FALSE;
2130 if (folder->account) {
2131 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2132 subs_only = folder->account->imap_subsonly;
2134 return imap_scan_tree_real(folder, subs_only);
2137 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2140 IMAPFolder *imapfolder;
2141 FolderItem *new_item;
2142 GSList *item_list, *cur;
2145 gchar *wildcard_path;
2151 g_return_val_if_fail(item != NULL, -1);
2152 g_return_val_if_fail(item->folder != NULL, -1);
2153 g_return_val_if_fail(item->no_sub == FALSE, -1);
2155 folder = item->folder;
2156 imapfolder = IMAP_FOLDER(folder);
2158 separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2162 if (folder->ui_func)
2163 folder->ui_func(folder, item, folder->ui_func_data);
2166 wildcard[0] = separator;
2169 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2175 real_path = g_strdup("");
2178 Xstrcat_a(wildcard_path, real_path, wildcard,
2179 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2183 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2185 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2187 if (r != MAILIMAP_NO_ERROR) {
2188 imap_handle_error(SESSION(session), r);
2193 item_list = imap_list_from_lep(imapfolder,
2194 lep_list, real_path, FALSE);
2195 mailimap_list_result_free(lep_list);
2200 node = item->node->children;
2201 while (node != NULL) {
2202 FolderItem *old_item = FOLDER_ITEM(node->data);
2203 GNode *next = node->next;
2206 for (cur = item_list; cur != NULL; cur = cur->next) {
2207 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2208 if (!strcmp2(old_item->path, cur_item->path)) {
2209 new_item = cur_item;
2214 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2215 debug_print("not removing INBOX\n");
2217 debug_print("folder '%s' not found. removing...\n",
2219 folder_item_remove(old_item);
2222 old_item->no_sub = new_item->no_sub;
2223 old_item->no_select = new_item->no_select;
2224 if (old_item->no_sub == TRUE && node->children) {
2225 debug_print("folder '%s' doesn't have "
2226 "subfolders. removing...\n",
2228 folder_item_remove_children(old_item);
2235 for (cur = item_list; cur != NULL; cur = cur->next) {
2236 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2239 for (node = item->node->children; node != NULL;
2240 node = node->next) {
2241 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2243 new_item = FOLDER_ITEM(node->data);
2244 folder_item_destroy(cur_item);
2250 new_item = cur_item;
2251 debug_print("new folder '%s' found.\n", new_item->path);
2252 folder_item_append(item, new_item);
2255 if (!strcmp(new_item->path, "INBOX")) {
2256 new_item->stype = F_INBOX;
2257 folder->inbox = new_item;
2258 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2261 base = g_path_get_basename(new_item->path);
2263 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2264 new_item->stype = F_OUTBOX;
2265 folder->outbox = new_item;
2266 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2267 new_item->stype = F_DRAFT;
2268 folder->draft = new_item;
2269 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2270 new_item->stype = F_QUEUE;
2271 folder->queue = new_item;
2272 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2273 new_item->stype = F_TRASH;
2274 folder->trash = new_item;
2279 if (new_item->no_sub == FALSE)
2280 imap_scan_tree_recursive(session, new_item, subs_only);
2283 g_slist_free(item_list);
2285 return MAILIMAP_NO_ERROR;
2288 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2290 IMAPSession *session = imap_session_get(folder);
2292 gchar *wildcard_path;
2296 GSList *item_list = NULL, *cur;
2297 GList *child_list = NULL, *tmplist = NULL;
2298 GSList *sub_list = NULL;
2304 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2309 wildcard[0] = separator;
2312 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2318 real_path = g_strdup("");
2321 Xstrcat_a(wildcard_path, real_path, wildcard,
2322 {g_free(real_path); return NULL;});
2326 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2327 item->path?item->path:item->name);
2329 statusbar_print_all(_("Looking for subfolders of %s..."),
2330 item->path?item->path:item->name);
2332 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2334 statusbar_pop_all();
2337 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2338 lep_list, real_path, FALSE);
2339 mailimap_list_result_free(lep_list);
2341 for (cur = item_list; cur != NULL; cur = cur->next) {
2342 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2344 tmplist = imap_scan_subtree(folder, cur_item,
2345 unsubs_only, recursive);
2347 child_list = g_list_concat(child_list, tmplist);
2349 child_list = g_list_prepend(child_list,
2350 imap_get_real_path(session,
2351 IMAP_FOLDER(folder), cur_item->path, &r));
2353 statusbar_pop_all();
2356 folder_item_destroy(cur_item);
2358 child_list = g_list_reverse(child_list);
2359 g_slist_free(item_list);
2362 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2364 statusbar_pop_all();
2367 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2368 lep_list, real_path, FALSE);
2369 mailimap_list_result_free(lep_list);
2371 for (cur = sub_list; cur != NULL; cur = cur->next) {
2372 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2373 GList *oldlitem = NULL;
2374 gchar *tmp = imap_get_real_path(session,
2375 IMAP_FOLDER(folder), cur_item->path, &r);
2377 statusbar_pop_all();
2380 folder_item_destroy(cur_item);
2381 oldlitem = g_list_find_custom(
2382 child_list, tmp, (GCompareFunc)strcmp2);
2384 child_list = g_list_remove_link(child_list, oldlitem);
2385 g_free(oldlitem->data);
2386 g_list_free(oldlitem);
2392 statusbar_pop_all();
2397 static gint imap_create_tree(Folder *folder)
2399 g_return_val_if_fail(folder != NULL, -1);
2400 g_return_val_if_fail(folder->node != NULL, -1);
2401 g_return_val_if_fail(folder->node->data != NULL, -1);
2402 g_return_val_if_fail(folder->account != NULL, -1);
2404 imap_scan_tree(folder);
2405 imap_create_missing_folders(folder);
2410 static void imap_create_missing_folders(Folder *folder)
2412 g_return_if_fail(folder != NULL);
2415 folder->inbox = imap_create_special_folder
2416 (folder, F_INBOX, "INBOX");
2418 folder->trash = imap_create_special_folder
2419 (folder, F_TRASH, "Trash");
2421 folder->queue = imap_create_special_folder
2422 (folder, F_QUEUE, "Queue");
2423 if (!folder->outbox)
2424 folder->outbox = imap_create_special_folder
2425 (folder, F_OUTBOX, "Sent");
2427 folder->draft = imap_create_special_folder
2428 (folder, F_DRAFT, "Drafts");
2431 static FolderItem *imap_create_special_folder(Folder *folder,
2432 SpecialFolderItemType stype,
2436 FolderItem *new_item;
2438 g_return_val_if_fail(folder != NULL, NULL);
2439 g_return_val_if_fail(folder->node != NULL, NULL);
2440 g_return_val_if_fail(folder->node->data != NULL, NULL);
2441 g_return_val_if_fail(folder->account != NULL, NULL);
2442 g_return_val_if_fail(name != NULL, NULL);
2444 item = FOLDER_ITEM(folder->node->data);
2445 new_item = imap_create_folder(folder, item, name);
2448 g_warning("Can't create '%s'\n", name);
2449 if (!folder->inbox) return NULL;
2451 new_item = imap_create_folder(folder, folder->inbox, name);
2453 g_warning("Can't create '%s' under INBOX\n", name);
2455 new_item->stype = stype;
2457 new_item->stype = stype;
2462 static gchar *imap_folder_get_path(Folder *folder)
2466 g_return_val_if_fail(folder != NULL, NULL);
2467 g_return_val_if_fail(folder->account != NULL, NULL);
2469 folder_path = g_strconcat(get_imap_cache_dir(),
2471 folder->account->recv_server,
2473 folder->account->userid,
2479 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2481 gchar *folder_path, *path;
2483 g_return_val_if_fail(folder != NULL, NULL);
2484 g_return_val_if_fail(item != NULL, NULL);
2485 folder_path = imap_folder_get_path(folder);
2487 g_return_val_if_fail(folder_path != NULL, NULL);
2488 if (folder_path[0] == G_DIR_SEPARATOR) {
2490 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2493 path = g_strdup(folder_path);
2496 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2497 folder_path, G_DIR_SEPARATOR_S,
2500 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2503 g_free(folder_path);
2508 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2511 gchar *dirpath, *imap_path;
2512 IMAPSession *session;
2513 FolderItem *new_item;
2518 gboolean no_select = FALSE, no_sub = FALSE;
2519 gboolean exist = FALSE;
2521 g_return_val_if_fail(folder != NULL, NULL);
2522 g_return_val_if_fail(folder->account != NULL, NULL);
2523 g_return_val_if_fail(parent != NULL, NULL);
2524 g_return_val_if_fail(name != NULL, NULL);
2526 debug_print("getting session...\n");
2527 session = imap_session_get(folder);
2532 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2533 dirpath = g_strdup(name);
2534 }else if (parent->path)
2535 dirpath = g_strconcat(parent->path, "/", name, NULL);
2536 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2537 dirpath = g_strdup(name);
2538 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2541 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2542 strtailchomp(imap_dir, '/');
2543 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2545 dirpath = g_strdup(name);
2549 /* keep trailing directory separator to create a folder that contains
2551 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2553 strtailchomp(dirpath, '/');
2554 Xstrdup_a(new_name, name, {
2558 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2561 imap_path_separator_subst(imap_path, separator);
2562 /* remove trailing / for display */
2563 strtailchomp(new_name, '/');
2565 if (strcmp(dirpath, "INBOX") != 0) {
2570 argbuf = g_ptr_array_new();
2571 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2572 if (r != MAILIMAP_NO_ERROR) {
2573 imap_handle_error(SESSION(session), r);
2574 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2577 ptr_array_free_strings(argbuf);
2578 g_ptr_array_free(argbuf, TRUE);
2582 if (clist_count(lep_list) > 0)
2584 mailimap_list_result_free(lep_list);
2587 ok = imap_cmd_create(session, imap_path);
2588 if (ok != MAILIMAP_NO_ERROR) {
2589 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2594 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2595 if (r == MAILIMAP_NO_ERROR) {
2596 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2597 lep_list, dirpath, TRUE);
2599 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2600 no_select = cur_item->no_select;
2601 no_sub = cur_item->no_sub;
2602 g_slist_free(item_list);
2604 mailimap_list_result_free(lep_list);
2606 imap_handle_error(SESSION(session), r);
2609 imap_threaded_subscribe(folder, imap_path, TRUE);
2613 /* just get flags */
2614 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2615 if (r == MAILIMAP_NO_ERROR) {
2616 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2617 lep_list, dirpath, TRUE);
2619 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2620 no_select = cur_item->no_select;
2621 no_sub = cur_item->no_sub;
2622 g_slist_free(item_list);
2624 mailimap_list_result_free(lep_list);
2626 imap_handle_error(SESSION(session), r);
2630 new_item = folder_item_new(folder, new_name, dirpath);
2631 new_item->no_select = no_select;
2632 new_item->no_sub = no_sub;
2633 folder_item_append(parent, new_item);
2637 dirpath = folder_item_get_path(new_item);
2638 if (!is_dir_exist(dirpath))
2639 make_dir_hier(dirpath);
2643 /* folder existed, scan it */
2644 imap_scan_required(folder, new_item);
2645 folder_item_scan_full(new_item, FALSE);
2651 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2656 gchar *real_oldpath;
2657 gchar *real_newpath;
2659 gchar *old_cache_dir;
2660 gchar *new_cache_dir;
2661 IMAPSession *session;
2664 gint exists, recent, unseen;
2665 guint32 uid_validity;
2667 g_return_val_if_fail(folder != NULL, -1);
2668 g_return_val_if_fail(item != NULL, -1);
2669 g_return_val_if_fail(item->path != NULL, -1);
2670 g_return_val_if_fail(name != NULL, -1);
2672 debug_print("getting session...\n");
2673 session = imap_session_get(folder);
2678 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
2680 g_warning(_("New folder name must not contain the namespace "
2685 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2690 g_free(session->mbox);
2691 session->mbox = NULL;
2692 session->exists = 0;
2693 session->recent = 0;
2694 session->expunge = 0;
2695 ok = imap_cmd_examine(session, "INBOX",
2696 &exists, &recent, &unseen, &uid_validity, FALSE);
2697 if (ok != MAILIMAP_NO_ERROR) {
2698 g_free(real_oldpath);
2702 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
2705 if (strchr(item->path, G_DIR_SEPARATOR)) {
2706 dirpath = g_path_get_dirname(item->path);
2707 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2710 newpath = g_strdup(name);
2712 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2713 imap_path_separator_subst(real_newpath, separator);
2715 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2716 if (ok != MAILIMAP_NO_ERROR) {
2717 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2718 real_oldpath, real_newpath);
2719 g_free(real_oldpath);
2721 g_free(real_newpath);
2725 item->name = g_strdup(name);
2727 old_cache_dir = folder_item_get_path(item);
2729 paths[0] = g_strdup(item->path);
2731 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2732 imap_rename_folder_func, paths);
2734 if (is_dir_exist(old_cache_dir)) {
2735 new_cache_dir = folder_item_get_path(item);
2736 if (rename(old_cache_dir, new_cache_dir) < 0) {
2737 FILE_OP_ERROR(old_cache_dir, "rename");
2739 g_free(new_cache_dir);
2742 g_free(old_cache_dir);
2745 g_free(real_oldpath);
2746 g_free(real_newpath);
2750 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2754 IMAPSession *session;
2755 debug_print("getting session...\n");
2757 session = imap_session_get(folder);
2761 if (item && item->path) {
2762 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2763 if (!path || is_fatal(r))
2765 if (!strcmp(path, "INBOX") && sub == FALSE)
2767 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2768 r = imap_threaded_subscribe(folder, path, sub);
2771 r = imap_threaded_subscribe(folder, rpath, sub);
2777 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2780 IMAPSession *session;
2783 gboolean selected_folder;
2785 g_return_val_if_fail(folder != NULL, -1);
2786 g_return_val_if_fail(item != NULL, -1);
2787 g_return_val_if_fail(item->path != NULL, -1);
2789 debug_print("getting session...\n");
2790 session = imap_session_get(folder);
2794 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2798 imap_threaded_subscribe(folder, path, FALSE);
2800 selected_folder = (session->mbox != NULL) &&
2801 (!strcmp(session->mbox, item->path));
2802 if (selected_folder) {
2803 ok = imap_cmd_close(session);
2804 if (ok != MAILIMAP_NO_ERROR) {
2805 debug_print("close err %d\n", ok);
2809 ok = imap_cmd_delete(session, path);
2810 if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
2811 gchar *tmp = g_strdup_printf("%s%c", path,
2812 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
2816 ok = imap_cmd_delete(session, path);
2819 if (ok != MAILIMAP_NO_ERROR) {
2820 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2826 cache_dir = folder_item_get_path(item);
2827 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2828 g_warning("can't remove directory '%s'\n", cache_dir);
2830 folder_item_remove(item);
2834 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2838 g_return_val_if_fail(item != NULL, -1);
2839 g_return_val_if_fail(item->folder != NULL, -1);
2840 g_return_val_if_fail(item->node != NULL, -1);
2842 node = item->node->children;
2843 while (node != NULL) {
2845 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2849 debug_print("IMAP removing %s\n", item->path);
2851 if (imap_remove_all_msg(folder, item) < 0)
2853 return imap_remove_folder_real(folder, item);
2856 typedef struct _uncached_data {
2857 IMAPSession *session;
2859 MsgNumberList *numlist;
2866 static void *imap_get_uncached_messages_thread(void *data)
2868 uncached_data *stuff = (uncached_data *)data;
2869 IMAPSession *session = stuff->session;
2870 FolderItem *item = stuff->item;
2871 MsgNumberList *numlist = stuff->numlist;
2872 GSList *newlist = NULL;
2873 GSList *llast = NULL;
2874 GSList *seq_list, *cur;
2875 gboolean got_alien_tags = FALSE;
2877 debug_print("uncached_messages\n");
2879 if (session == NULL || item == NULL || item->folder == NULL
2880 || FOLDER_CLASS(item->folder) != &imap_class) {
2885 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2886 debug_print("get msgs info\n");
2887 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2888 struct mailimap_set * imapset;
2894 if (session->cancelled)
2897 imapset = cur->data;
2899 r = imap_threaded_fetch_env(session->folder,
2900 imapset, &env_list);
2901 if (r != MAILIMAP_NO_ERROR) {
2902 imap_handle_error(SESSION(session), r);
2910 session_set_access_time(SESSION(session));
2913 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2914 struct imap_fetch_env_info * info;
2916 GSList *tags = NULL, *cur = NULL;
2917 info = carray_get(env_list, i);
2918 tags = carray_get(env_list, i+1);
2919 msginfo = imap_envelope_from_lep(info, item);
2920 if (msginfo == NULL) {
2921 slist_free_strings(tags);
2926 g_slist_free(msginfo->tags);
2927 msginfo->tags = NULL;
2929 for (cur = tags; cur; cur = cur->next) {
2930 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2932 id = tags_get_id_for_str(real_tag);
2934 id = tags_add_tag(real_tag);
2935 got_alien_tags = TRUE;
2937 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2938 msginfo->tags = g_slist_append(
2940 GINT_TO_POINTER(id));
2944 slist_free_strings(tags);
2946 msginfo->folder = item;
2948 llast = newlist = g_slist_append(newlist, msginfo);
2950 llast = g_slist_append(llast, msginfo);
2951 llast = llast->next;
2956 imap_fetch_env_free(env_list);
2959 if (got_alien_tags) {
2961 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2964 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2965 struct mailimap_set * imapset;
2967 imapset = cur->data;
2968 mailimap_set_free(imapset);
2971 session_set_access_time(SESSION(session));
2976 #define MAX_MSG_NUM 50
2978 static GSList *imap_get_uncached_messages(IMAPSession *session,
2980 MsgNumberList *numlist,
2983 GSList *result = NULL;
2985 uncached_data *data = g_new0(uncached_data, 1);
2990 data->total = g_slist_length(numlist);
2991 data->ok = MAILIMAP_NO_ERROR;
2992 debug_print("messages list : %i\n", data->total);
2994 while (cur != NULL) {
2995 GSList * partial_result;
3003 while (count < MAX_MSG_NUM) {
3008 if (newlist == NULL)
3009 llast = newlist = g_slist_append(newlist, p);
3011 llast = g_slist_append(llast, p);
3012 llast = llast->next;
3022 data->session = session;
3024 data->numlist = newlist;
3027 if (prefs_common.work_offline &&
3028 !inc_offline_should_override(FALSE,
3029 _("Claws Mail needs network access in order "
3030 "to access the IMAP server."))) {
3036 (GSList *)imap_get_uncached_messages_thread(data);
3038 if (data->ok != MAILIMAP_NO_ERROR) {
3041 statusbar_progress_all(data->cur,data->total, 1);
3043 g_slist_free(newlist);
3045 result = g_slist_concat(result, partial_result);
3050 statusbar_progress_all(0,0,0);
3051 statusbar_pop_all();
3056 static void imap_delete_all_cached_messages(FolderItem *item)
3060 g_return_if_fail(item != NULL);
3061 g_return_if_fail(item->folder != NULL);
3062 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3064 debug_print("Deleting all cached messages...\n");
3066 dir = folder_item_get_path(item);
3067 if (is_dir_exist(dir))
3068 remove_all_numbered_files(dir);
3071 debug_print("done.\n");
3074 gchar imap_get_path_separator_for_item(FolderItem *item)
3076 Folder *folder = NULL;
3077 IMAPFolder *imap_folder = NULL;
3078 IMAPSession *session = NULL;
3083 folder = item->folder;
3088 imap_folder = IMAP_FOLDER(folder);
3093 debug_print("getting session...");
3094 session = imap_session_get(FOLDER(folder));
3095 result = imap_get_path_separator(session, imap_folder, item->path, &ok);
3099 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder, gint *ok)
3103 gchar separator = '\0';
3105 g_return_val_if_fail(session != NULL, '/');
3106 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3108 if (r != MAILIMAP_NO_ERROR) {
3109 imap_handle_error(SESSION(session), r);
3110 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3115 if (clist_count(lep_list) > 0) {
3116 clistiter * iter = clist_begin(lep_list);
3117 struct mailimap_mailbox_list * mb;
3118 mb = clist_content(iter);
3120 separator = mb->mb_delimiter;
3121 debug_print("got separator: %c\n", folder->last_seen_separator);
3123 *ok = MAILIMAP_NO_ERROR;
3124 mailimap_list_result_free(lep_list);
3128 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3130 gchar separator = '/';
3132 if (folder->last_seen_separator == 0) {
3133 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "", ok);
3136 if (folder->last_seen_separator == 0) {
3137 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX", ok);
3140 if (folder->last_seen_separator != 0) {
3141 debug_print("using separator: %c\n", folder->last_seen_separator);
3142 return folder->last_seen_separator;
3148 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3153 g_return_val_if_fail(folder != NULL, NULL);
3154 g_return_val_if_fail(path != NULL, NULL);
3156 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3157 separator = imap_get_path_separator(session, folder, path, ok);
3158 imap_path_separator_subst(real_path, separator);
3163 static gint imap_set_message_flags(IMAPSession *session,
3164 IMAPFolderItem *item,
3165 MsgNumberList *numlist,
3174 IMAPFolder *folder = NULL;
3175 GSList *sorted_list = NULL;
3177 if (numlist == NULL || session == NULL)
3178 return MAILIMAP_ERROR_BAD_STATE;
3180 folder = IMAP_FOLDER(session->folder);
3182 sorted_list = g_slist_copy(numlist);
3183 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3185 cur = g_slist_last(sorted_list);
3188 total = GPOINTER_TO_INT(cur->data);
3190 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3192 statusbar_print_all(_("Flagging messages..."));
3194 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3195 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3196 struct mailimap_set_item *set_item = clist_content(
3197 clist_begin(imapset->set_list));
3199 statusbar_progress_all(set_item->set_first, total, 1);
3201 ok = imap_cmd_store(session, item, imapset,
3202 flags, tags, is_set);
3203 statusbar_progress_all(set_item->set_last, total, 1);
3204 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3205 /* reduce max set size */
3206 folder->max_set_size /= 2;
3208 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3213 g_slist_free(sorted_list);
3215 statusbar_progress_all(0,0,0);
3216 statusbar_pop_all();
3218 imap_lep_set_free(seq_list);
3223 typedef struct _select_data {
3224 IMAPSession *session;
3229 guint32 *uid_validity;
3233 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3235 gint *exists, gint *recent, gint *unseen,
3236 guint32 *uid_validity, gint *can_create_flags,
3241 gint exists_, recent_, unseen_;
3242 guint32 uid_validity_;
3243 gint can_create_flags_;
3244 const gchar *path = item ? item->path:NULL;
3247 return MAILIMAP_ERROR_BAD_STATE;
3250 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3251 if (session->mbox && strcmp(session->mbox, path) == 0)
3252 return MAILIMAP_NO_ERROR;
3254 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3255 if (session->mbox && strcmp(session->mbox, path) == 0) {
3256 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3257 return MAILIMAP_NO_ERROR;
3267 uid_validity = &uid_validity_;
3268 if (!can_create_flags)
3269 can_create_flags = &can_create_flags_;
3271 g_free(session->mbox);
3272 session->mbox = NULL;
3273 session->exists = 0;
3274 session->recent = 0;
3275 session->expunge = 0;
3277 real_path = imap_get_real_path(session, folder, path, &ok);
3280 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3281 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3282 ok = imap_cmd_select(session, real_path,
3283 exists, recent, unseen, uid_validity, can_create_flags,
3284 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3285 if (ok != MAILIMAP_NO_ERROR) {
3286 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3288 session->mbox = g_strdup(path);
3289 session->folder_content_changed = FALSE;
3290 session->exists = *exists;
3291 session->recent = *recent;
3292 session->expunge = 0;
3293 session->unseen = *unseen;
3294 session->uid_validity = *uid_validity;
3295 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3296 session->exists, session->recent, session->expunge,
3297 session->uid_validity, *can_create_flags);
3299 if (*can_create_flags) {
3300 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3302 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3309 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3310 const gchar *path, IMAPFolderItem *item,
3312 guint32 *uid_next, guint32 *uid_validity,
3313 gint *unseen, gboolean block)
3317 struct mailimap_mailbox_data_status * data_status;
3322 real_path = imap_get_real_path(session, folder, path, &r);
3343 if (session->mbox != NULL &&
3344 !strcmp(session->mbox, item->item.path)) {
3345 r = imap_cmd_close(session);
3346 if (r != MAILIMAP_NO_ERROR) {
3347 debug_print("close err %d\n", r);
3352 r = imap_threaded_status(FOLDER(folder), real_path,
3353 &data_status, mask);
3356 if (r != MAILIMAP_NO_ERROR) {
3357 imap_handle_error(SESSION(session), r);
3358 debug_print("status err %d\n", r);
3362 if (data_status->st_info_list == NULL) {
3363 mailimap_mailbox_data_status_free(data_status);
3364 debug_print("status->st_info_list == NULL\n");
3365 return MAILIMAP_ERROR_BAD_STATE;
3369 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3370 iter = clist_next(iter)) {
3371 struct mailimap_status_info * info;
3373 info = clist_content(iter);
3374 switch (info->st_att) {
3375 case MAILIMAP_STATUS_ATT_MESSAGES:
3377 * messages = info->st_value;
3378 got_values |= 1 << 0;
3382 case MAILIMAP_STATUS_ATT_UIDNEXT:
3384 * uid_next = info->st_value;
3385 got_values |= 1 << 2;
3389 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3391 * uid_validity = info->st_value;
3392 got_values |= 1 << 3;
3396 case MAILIMAP_STATUS_ATT_UNSEEN:
3398 * unseen = info->st_value;
3399 got_values |= 1 << 4;
3404 mailimap_mailbox_data_status_free(data_status);
3406 if (got_values != mask) {
3407 g_warning("status: incomplete values received (%d)\n", got_values);
3409 return MAILIMAP_NO_ERROR;
3412 static void imap_free_capabilities(IMAPSession *session)
3414 slist_free_strings(session->capability);
3415 g_slist_free(session->capability);
3416 session->capability = NULL;
3419 /* low-level IMAP4rev1 commands */
3421 static gint imap_cmd_login(IMAPSession *session,
3422 const gchar *user, const gchar *pass,
3428 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3429 gint ok = MAILIMAP_ERROR_BAD_STATE;
3430 if (imap_has_capability(session, "STARTTLS")) {
3431 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3432 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3433 ok = imap_cmd_starttls(session);
3434 if (ok != MAILIMAP_NO_ERROR) {
3435 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3439 imap_free_capabilities(session);
3440 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3441 imap_handle_error(SESSION(session), r);
3442 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3447 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3448 "server requires TLS, but Claws Mail "
3449 "has been compiled without OpenSSL "
3451 SESSION(session)->server);
3452 return MAILIMAP_ERROR_LOGIN;
3455 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3456 return MAILIMAP_ERROR_LOGIN;
3460 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3462 SESSION(session)->server,
3464 r = imap_threaded_login(session->folder, user, pass, type);
3465 if (r != MAILIMAP_NO_ERROR) {
3466 imap_handle_error(SESSION(session), r);
3467 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3468 SESSION(session)->server);
3471 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3472 SESSION(session)->server);
3473 ok = MAILIMAP_NO_ERROR;
3478 static gint imap_cmd_noop(IMAPSession *session)
3481 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3483 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3484 if (r != MAILIMAP_NO_ERROR) {
3485 imap_handle_error(SESSION(session), r);
3486 debug_print("noop err %d\n", r);
3490 session->folder_content_changed = FALSE;
3492 if ((exists && exists != session->exists)
3493 || (recent && recent != session->recent)
3494 || (expunge && expunge != session->expunge)
3495 || (unseen && unseen != session->unseen)) {
3496 session->folder_content_changed = TRUE;
3498 if (uidnext != 0 && uidnext != session->uid_next) {
3499 session->uid_next = uidnext;
3500 session->folder_content_changed = TRUE;
3502 if (uidval != 0 && uidval != session->uid_validity) {
3503 session->uid_validity = uidval;
3504 session->folder_content_changed = TRUE;
3507 session->exists = exists;
3508 session->recent = recent;
3509 session->expunge = expunge;
3510 session->unseen = unseen;
3512 session_set_access_time(SESSION(session));
3514 return MAILIMAP_NO_ERROR;
3517 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3518 static gint imap_cmd_starttls(IMAPSession *session)
3522 r = imap_threaded_starttls(session->folder,
3523 SESSION(session)->server, SESSION(session)->port);
3524 if (r != MAILIMAP_NO_ERROR) {
3525 imap_handle_error(SESSION(session), r);
3526 debug_print("starttls err %d\n", r);
3529 return MAILIMAP_NO_ERROR;
3533 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3534 gint *exists, gint *recent, gint *unseen,
3535 guint32 *uid_validity, gint *can_create_flags,
3536 GSList **ok_flags, gboolean block)
3540 r = imap_threaded_select(session->folder, folder,
3541 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3542 if (r != MAILIMAP_NO_ERROR) {
3543 imap_handle_error(SESSION(session), r);
3544 debug_print("select err %d\n", r);
3547 return MAILIMAP_NO_ERROR;
3550 static gint imap_cmd_close(IMAPSession *session)
3554 r = imap_threaded_close(session->folder);
3555 if (r != MAILIMAP_NO_ERROR) {
3556 imap_handle_error(SESSION(session), r);
3557 debug_print("close err %d\n", r);
3560 g_free(session->mbox);
3561 session->mbox = NULL;
3562 session->exists = 0;
3563 session->recent = 0;
3564 session->expunge = 0;
3565 return MAILIMAP_NO_ERROR;
3568 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3569 gint *exists, gint *recent, gint *unseen,
3570 guint32 *uid_validity, gboolean block)
3574 r = imap_threaded_examine(session->folder, folder,
3575 exists, recent, unseen, uid_validity);
3576 if (r != MAILIMAP_NO_ERROR) {
3577 imap_handle_error(SESSION(session), r);
3578 debug_print("examine err %d\n", r);
3582 return MAILIMAP_NO_ERROR;
3585 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3589 r = imap_threaded_create(session->folder, folder);
3590 if (r != MAILIMAP_NO_ERROR) {
3591 imap_handle_error(SESSION(session), r);
3595 return MAILIMAP_NO_ERROR;
3598 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3599 const gchar *new_folder)
3603 r = imap_threaded_rename(session->folder, old_folder,
3605 if (r != MAILIMAP_NO_ERROR) {
3606 imap_handle_error(SESSION(session), r);
3610 return MAILIMAP_NO_ERROR;
3613 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3618 r = imap_threaded_delete(session->folder, folder);
3619 if (r != MAILIMAP_NO_ERROR) {
3620 imap_handle_error(SESSION(session), r);
3624 return MAILIMAP_NO_ERROR;
3627 typedef struct _fetch_data {
3628 IMAPSession *session;
3630 const gchar *filename;
3636 static void *imap_cmd_fetch_thread(void *data)
3638 fetch_data *stuff = (fetch_data *)data;
3639 IMAPSession *session = stuff->session;
3640 guint32 uid = stuff->uid;
3641 const gchar *filename = stuff->filename;
3645 r = imap_threaded_fetch_content(session->folder,
3649 r = imap_threaded_fetch_content(session->folder,
3652 if (r != MAILIMAP_NO_ERROR) {
3653 imap_handle_error(SESSION(session), r);
3654 debug_print("fetch err %d\n", r);
3655 return GINT_TO_POINTER(r);
3657 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3660 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3661 const gchar *filename, gboolean headers,
3664 fetch_data *data = g_new0(fetch_data, 1);
3667 data->session = session;
3669 data->filename = filename;
3670 data->headers = headers;
3673 if (prefs_common.work_offline &&
3674 !inc_offline_should_override(FALSE,
3675 _("Claws Mail needs network access in order "
3676 "to access the IMAP server."))) {
3680 statusbar_print_all(_("Fetching message..."));
3681 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3682 statusbar_pop_all();
3688 static gint imap_cmd_append(IMAPSession *session,
3689 IMAPFolderItem *item,
3690 const gchar *destfolder,
3691 const gchar *file, IMAPFlags flags,
3694 struct mailimap_flag_list * flag_list;
3697 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3699 flag_list = imap_flag_to_lep(item, flags, NULL);
3700 lock_session(session);
3701 r = imap_threaded_append(session->folder, destfolder,
3702 file, flag_list, (int *)new_uid);
3703 mailimap_flag_list_free(flag_list);
3705 if (r != MAILIMAP_NO_ERROR) {
3706 imap_handle_error(SESSION(session), r);
3707 debug_print("append err %d\n", r);
3711 unlock_session(session);
3713 return MAILIMAP_NO_ERROR;
3716 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3717 const gchar *destfolder, GRelation *uid_mapping,
3718 struct mailimap_set **source, struct mailimap_set **dest)
3722 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3723 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3724 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3726 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3727 if (r != MAILIMAP_NO_ERROR) {
3728 imap_handle_error(SESSION(session), r);
3732 return MAILIMAP_NO_ERROR;
3735 static gint imap_cmd_store(IMAPSession *session,
3736 IMAPFolderItem *item,
3737 struct mailimap_set * set,
3738 IMAPFlags flags, GSList *tags, int do_add)
3741 struct mailimap_flag_list * flag_list = NULL;
3742 struct mailimap_store_att_flags * store_att_flags;
3744 flag_list = imap_flag_to_lep(item, flags, tags);
3748 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3751 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3753 r = imap_threaded_store(session->folder, set, store_att_flags);
3754 mailimap_store_att_flags_free(store_att_flags);
3755 if (r != MAILIMAP_NO_ERROR) {
3756 imap_handle_error(SESSION(session), r);
3760 return MAILIMAP_NO_ERROR;
3763 static gint imap_cmd_expunge(IMAPSession *session)
3767 if (prefs_common.work_offline &&
3768 !inc_offline_should_override(FALSE,
3769 _("Claws Mail needs network access in order "
3770 "to access the IMAP server."))) {
3774 r = imap_threaded_expunge(session->folder);
3775 if (r != MAILIMAP_NO_ERROR) {
3776 imap_handle_error(SESSION(session), r);
3780 return MAILIMAP_NO_ERROR;
3783 static void imap_path_separator_subst(gchar *str, gchar separator)
3786 gboolean in_escape = FALSE;
3788 if (!separator || separator == '/') return;
3790 for (p = str; *p != '\0'; p++) {
3791 if (*p == '/' && !in_escape)
3793 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3795 else if (*p == '-' && in_escape)
3800 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3802 FolderItem *item = node->data;
3803 gchar **paths = data;
3804 const gchar *oldpath = paths[0];
3805 const gchar *newpath = paths[1];
3806 gchar *real_oldpath, *real_newpath;
3808 gchar *new_itempath;
3810 IMAPSession *session = imap_session_get(item->folder);
3812 oldpathlen = strlen(oldpath);
3813 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3814 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3818 base = item->path + oldpathlen;
3819 while (*base == G_DIR_SEPARATOR) base++;
3821 new_itempath = g_strdup(newpath);
3823 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3826 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3828 item->path = new_itempath;
3830 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3832 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3833 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3835 g_free(real_oldpath);
3836 g_free(real_newpath);
3840 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3842 GSList *uidlist, *elem;
3844 clist * lep_uidlist;
3845 gint ok, nummsgs = 0, lastuid_old;
3847 if (session == NULL) {
3851 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3852 NULL, NULL, NULL, NULL, NULL, TRUE);
3853 if (ok != MAILIMAP_NO_ERROR) {
3857 g_slist_free(item->uid_list);
3858 item->uid_list = NULL;
3862 if (folder->account && folder->account->low_bandwidth) {
3863 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3867 if (r == MAILIMAP_NO_ERROR) {
3868 GSList * fetchuid_list =
3869 imap_uid_list_from_lep(lep_uidlist);
3870 mailimap_search_result_free(lep_uidlist);
3872 uidlist = g_slist_concat(fetchuid_list, uidlist);
3874 carray * lep_uidtab;
3875 if (r != -1) { /* inited */
3876 imap_handle_error(SESSION(session), r);
3880 r = imap_threaded_fetch_uid(folder, 1,
3882 if (r == MAILIMAP_NO_ERROR) {
3883 GSList * fetchuid_list =
3884 imap_uid_list_from_lep_tab(lep_uidtab);
3885 imap_fetch_uid_list_free(lep_uidtab);
3886 uidlist = g_slist_concat(fetchuid_list, uidlist);
3890 if (r != MAILIMAP_NO_ERROR) {
3891 imap_handle_error(SESSION(session), r);
3895 lastuid_old = item->lastuid;
3897 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3900 msgnum = GPOINTER_TO_INT(elem->data);
3902 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3903 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3906 g_slist_free(uidlist);
3908 unlock_session(session); /* locked from imap_get_num_list */
3914 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3916 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3917 IMAPSession *session;
3919 GSList *uidlist = NULL;
3921 gboolean selected_folder;
3922 gint known_list_len = 0;
3923 debug_print("get_num_list\n");
3925 g_return_val_if_fail(folder != NULL, -1);
3926 g_return_val_if_fail(item != NULL, -1);
3927 g_return_val_if_fail(item->item.path != NULL, -1);
3928 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3929 g_return_val_if_fail(folder->account != NULL, -1);
3931 known_list_len = g_slist_length(item->uid_list);
3932 if (!item->should_update) {
3933 debug_print("get_num_list: nothing to update\n");
3934 *old_uids_valid = TRUE;
3935 if (known_list_len == item->item.total_msgs
3936 && known_list_len > 0) {
3937 *msgnum_list = g_slist_copy(item->uid_list);
3938 return known_list_len;
3940 debug_print("don't know the list length...\n");
3944 if (prefs_common.work_offline &&
3945 !inc_offline_should_override(FALSE,
3946 _("Claws Mail needs network access in order "
3947 "to access the IMAP server."))) {
3951 debug_print("getting session...\n");
3952 session = imap_session_get(folder);
3953 g_return_val_if_fail(session != NULL, -1);
3955 lock_session(session); /* unlocked by get_list_of_uids */
3956 if (FOLDER_ITEM(item)->path)
3957 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3958 FOLDER_ITEM(item)->folder->name,
3960 FOLDER_ITEM(item)->path);
3962 statusbar_print_all(_("Scanning folder %s ..."),
3963 FOLDER_ITEM(item)->folder->name);
3965 selected_folder = (session->mbox != NULL) &&
3966 (!strcmp(session->mbox, item->item.path));
3968 if (item->should_trash_cache) {
3969 *old_uids_valid = FALSE;
3970 debug_print("get_num_list: trashing num list\n");
3971 debug_print("Freeing imap uid cache\n");
3973 g_slist_free(item->uid_list);
3974 item->uid_list = NULL;
3976 imap_delete_all_cached_messages((FolderItem *)item);
3978 debug_print("get_num_list: updating num list\n");
3979 *old_uids_valid = TRUE;
3982 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3983 /* session could be broken now, in case of fatal error */
3985 debug_print("get_num_list: got %d msgs\n", nummsgs);
3988 statusbar_pop_all();
3992 *msgnum_list = uidlist;
3994 dir = folder_item_get_path((FolderItem *)item);
3995 debug_print("removing old messages from %s\n", dir);
3996 remove_numbered_files_not_in_list(dir, *msgnum_list);
3999 debug_print("get_num_list - ok - %i\n", nummsgs);
4000 statusbar_pop_all();
4001 item->should_trash_cache = FALSE;
4002 item->should_update = FALSE;
4006 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
4011 flags.perm_flags = MSG_NEW|MSG_UNREAD;
4012 flags.tmp_flags = 0;
4014 g_return_val_if_fail(item != NULL, NULL);
4015 g_return_val_if_fail(file != NULL, NULL);
4017 if (folder_has_parent_of_type(item, F_QUEUE)) {
4018 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
4019 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
4020 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
4023 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
4024 if (!msginfo) return NULL;
4026 msginfo->plaintext_file = g_strdup(file);
4027 msginfo->folder = item;
4032 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
4033 GSList *msgnum_list)
4035 IMAPSession *session;
4036 MsgInfoList *ret = NULL;
4039 debug_print("get_msginfos\n");
4041 g_return_val_if_fail(folder != NULL, NULL);
4042 g_return_val_if_fail(item != NULL, NULL);
4043 g_return_val_if_fail(msgnum_list != NULL, NULL);
4045 debug_print("getting session...\n");
4046 session = imap_session_get(folder);
4047 g_return_val_if_fail(session != NULL, NULL);
4049 lock_session(session); /* unlocked later in the function */
4051 debug_print("IMAP getting msginfos\n");
4052 ok = imap_select(session, IMAP_FOLDER(folder), item,
4053 NULL, NULL, NULL, NULL, NULL, FALSE);
4054 if (ok != MAILIMAP_NO_ERROR) {
4057 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4058 folder_has_parent_of_type(item, F_QUEUE))) {
4059 ret = g_slist_concat(ret,
4060 imap_get_uncached_messages(session, item,
4062 if (ok != MAILIMAP_NO_ERROR)
4064 unlock_session(session);
4066 MsgNumberList *sorted_list, *elem, *llast = NULL;
4067 gint startnum, lastnum;
4069 unlock_session(session);
4071 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4073 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4075 llast = g_slist_last(ret);
4076 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4080 num = GPOINTER_TO_INT(elem->data);
4082 if (num > lastnum + 1 || elem == NULL) {
4084 for (i = startnum; i <= lastnum; ++i) {
4086 file = imap_fetch_msg(folder, item, i);
4088 MsgInfo *msginfo = imap_parse_msg(file, item);
4089 if (msginfo != NULL) {
4090 msginfo->msgnum = i;
4092 llast = ret = g_slist_append(ret, msginfo);
4094 llast = g_slist_append(llast, msginfo);
4095 llast = llast->next;
4110 g_slist_free(sorted_list);
4115 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4117 MsgInfo *msginfo = NULL;
4118 MsgInfoList *msginfolist;
4119 MsgNumberList numlist;
4121 numlist.next = NULL;
4122 numlist.data = GINT_TO_POINTER(uid);
4124 msginfolist = imap_get_msginfos(folder, item, &numlist);
4125 if (msginfolist != NULL) {
4126 msginfo = msginfolist->data;
4127 g_slist_free(msginfolist);
4133 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4135 IMAPSession *session;
4136 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4137 gint ok, exists = 0, unseen = 0;
4138 guint32 uid_next = 0, uid_val = 0;
4139 gboolean selected_folder;
4141 g_return_val_if_fail(folder != NULL, FALSE);
4142 g_return_val_if_fail(item != NULL, FALSE);
4143 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4144 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4146 if (item->item.path == NULL)
4149 if (item->should_update) {
4150 debug_print("scan already required\n");
4153 debug_print("getting session...\n");
4154 session = imap_session_get(folder);
4156 g_return_val_if_fail(session != NULL, FALSE);
4157 lock_session(session); /* unlocked later in the function */
4159 selected_folder = (session->mbox != NULL) &&
4160 (!strcmp(session->mbox, item->item.path));
4161 if (selected_folder) {
4162 if (!session->folder_content_changed) {
4163 ok = imap_cmd_noop(session);
4164 if (ok != MAILIMAP_NO_ERROR) {
4165 debug_print("disconnected!\n");
4167 session = imap_reconnect_if_possible(folder, session);
4169 session = imap_session_get(folder);
4170 if (session == NULL)
4174 if (session->folder_content_changed) {
4175 debug_print("CHANGED (self-noop)! scan_required\n");
4176 item->should_update = TRUE;
4177 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4178 item->item.mtime = session->uid_validity;
4179 item->should_trash_cache = TRUE;
4181 unlock_session(session);
4185 debug_print("CHANGED (previous noop)! scan_required\n");
4186 item->should_update = TRUE;
4187 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4188 item->item.mtime = session->uid_validity;
4189 item->should_trash_cache = TRUE;
4191 unlock_session(session);
4195 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4196 &exists, &uid_next, &uid_val, &unseen, FALSE);
4197 if (ok != MAILIMAP_NO_ERROR) {
4201 debug_print("exists %d, item->item.total_msgs %d\n",
4202 exists, item->item.total_msgs);
4203 if (exists != item->item.total_msgs
4204 || unseen != item->item.unread_msgs
4205 || uid_next != item->uid_next
4206 || uid_val != item->item.mtime) {
4207 debug_print("CHANGED (status)! scan_required\n");
4208 item->last_change = time(NULL);
4209 item->should_update = TRUE;
4210 item->uid_next = uid_next;
4211 if (uid_val != item->item.mtime) {
4212 item->item.mtime = uid_val;
4213 item->should_trash_cache = TRUE;
4215 unlock_session(session);
4219 unlock_session(session);
4221 item->should_update = FALSE;
4225 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4227 IMAPSession *session;
4228 IMAPFlags flags_set = 0, flags_unset = 0;
4229 gint ok = MAILIMAP_NO_ERROR;
4230 MsgNumberList numlist;
4231 hashtable_data *ht_data = NULL;
4233 g_return_if_fail(folder != NULL);
4234 g_return_if_fail(folder->klass == &imap_class);
4235 g_return_if_fail(item != NULL);
4236 g_return_if_fail(item->folder == folder);
4237 g_return_if_fail(msginfo != NULL);
4238 g_return_if_fail(msginfo->folder == item);
4240 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4241 flags_set |= IMAP_FLAG_FLAGGED;
4242 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4243 flags_unset |= IMAP_FLAG_FLAGGED;
4245 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4246 flags_unset |= IMAP_FLAG_SEEN;
4247 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4248 flags_set |= IMAP_FLAG_SEEN;
4250 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4251 flags_set |= IMAP_FLAG_ANSWERED;
4252 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4253 flags_unset |= IMAP_FLAG_ANSWERED;
4255 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4256 flags_set |= IMAP_FLAG_FORWARDED;
4257 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4258 flags_unset |= IMAP_FLAG_FORWARDED;
4260 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4261 flags_set |= IMAP_FLAG_DELETED;
4262 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4263 flags_unset |= IMAP_FLAG_DELETED;
4265 if (!flags_set && !flags_unset) {
4266 /* the changed flags were not translatable to IMAP-speak.
4267 * like MSG_POSTFILTERED, so just apply. */
4268 msginfo->flags.perm_flags = newflags;
4272 debug_print("getting session...\n");
4273 session = imap_session_get(folder);
4278 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4279 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4282 numlist.next = NULL;
4283 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4285 if (IMAP_FOLDER_ITEM(item)->batching) {
4286 /* instead of performing an UID STORE command for each message change,
4287 * as a lot of them can change "together", we just fill in hashtables
4288 * and defer the treatment so that we're able to send only one
4291 debug_print("IMAP batch mode on, deferring flags change\n");
4293 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4294 GINT_TO_POINTER(flags_set));
4295 if (ht_data == NULL) {
4296 ht_data = g_new0(hashtable_data, 1);
4297 ht_data->item = IMAP_FOLDER_ITEM(item);
4298 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4299 GINT_TO_POINTER(flags_set), ht_data);
4301 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4304 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4305 GINT_TO_POINTER(flags_unset));
4306 if (ht_data == NULL) {
4307 ht_data = g_new0(hashtable_data, 1);
4308 ht_data->item = IMAP_FOLDER_ITEM(item);
4309 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4310 GINT_TO_POINTER(flags_unset), ht_data);
4312 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4313 GINT_TO_POINTER(msginfo->msgnum));
4316 debug_print("IMAP changing flags\n");
4318 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4319 if (ok != MAILIMAP_NO_ERROR) {
4325 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4326 if (ok != MAILIMAP_NO_ERROR) {
4331 msginfo->flags.perm_flags = newflags;
4335 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4338 IMAPSession *session;
4340 MsgNumberList numlist;
4342 g_return_val_if_fail(folder != NULL, -1);
4343 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4344 g_return_val_if_fail(item != NULL, -1);
4346 debug_print("getting session...\n");
4347 session = imap_session_get(folder);
4348 if (!session) return -1;
4350 ok = imap_select(session, IMAP_FOLDER(folder), item,
4351 NULL, NULL, NULL, NULL, NULL, FALSE);
4352 if (ok != MAILIMAP_NO_ERROR) {
4355 numlist.next = NULL;
4356 numlist.data = GINT_TO_POINTER(uid);
4358 ok = imap_set_message_flags
4359 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4360 if (ok != MAILIMAP_NO_ERROR) {
4361 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4365 if (!session->uidplus) {
4366 ok = imap_cmd_expunge(session);
4370 uidstr = g_strdup_printf("%u", uid);
4371 ok = imap_cmd_expunge(session);
4374 if (ok != MAILIMAP_NO_ERROR) {
4375 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4379 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4380 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4381 dir = folder_item_get_path(item);
4382 if (is_dir_exist(dir))
4383 remove_numbered_files(dir, uid, uid);
4385 return MAILIMAP_NO_ERROR;
4388 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4390 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4393 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4397 g_return_val_if_fail(list != NULL, -1);
4399 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4400 if (GPOINTER_TO_INT(elem->data) >= num)
4403 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4406 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4408 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4409 debug_print("flag %d is OK\n", flag);
4412 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4413 debug_print("creating flags is OK\n");
4420 * NEW and DELETED flags are not syncronized
4421 * - The NEW/RECENT flags in IMAP folders can not really be directly
4422 * modified by Sylpheed
4423 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4424 * meaning, in IMAP it always removes the messages from the FolderItem
4425 * in Sylpheed it can mean to move the message to trash
4428 typedef struct _get_flags_data {
4431 MsgInfoList *msginfo_list;
4432 GRelation *msgflags;
4433 gboolean full_search;
4437 static /*gint*/ void *imap_get_flags_thread(void *data)
4439 get_flags_data *stuff = (get_flags_data *)data;
4440 Folder *folder = stuff->folder;
4441 FolderItem *fitem = (FolderItem *) stuff->item;
4442 MsgInfoList *msginfo_list = stuff->msginfo_list;
4443 GRelation *msgflags = stuff->msgflags;
4445 carray * lep_uidtab;
4446 IMAPSession *session;
4448 int r = MAILIMAP_NO_ERROR;
4449 GHashTable *flags_hash = NULL;
4450 GHashTable *tags_hash = NULL;
4451 gboolean full_search = stuff->full_search;
4452 GSList *sorted_list = NULL;
4453 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL;
4454 GSList *seq_list, *cur;
4455 gboolean reverse_seen = FALSE;
4456 gboolean selected_folder;
4457 gint exists_cnt, unseen_cnt;
4458 gboolean got_alien_tags = FALSE;
4460 session = imap_session_get(folder);
4462 if (session == NULL) {
4464 return GINT_TO_POINTER(-1);
4466 selected_folder = (session->mbox != NULL) &&
4467 (!strcmp(session->mbox, fitem->path));
4469 lock_session(session);
4470 if (!selected_folder) {
4471 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4472 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4473 if (ok != MAILIMAP_NO_ERROR) {
4475 return GINT_TO_POINTER(-1);
4478 if (unseen_cnt > exists_cnt / 2)
4479 reverse_seen = TRUE;
4482 if (fitem->unread_msgs > fitem->total_msgs / 2)
4483 reverse_seen = TRUE;
4486 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4488 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4490 struct mailimap_set * set;
4491 set = mailimap_set_new_interval(1, 0);
4492 seq_list = g_slist_append(NULL, set);
4495 if (folder->account && folder->account->low_bandwidth) {
4496 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4497 struct mailimap_set * imapset;
4498 clist * lep_uidlist;
4501 imapset = cur->data;
4503 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4504 full_search ? NULL:imapset, &lep_uidlist);
4507 r = imap_threaded_search(folder,
4508 IMAP_SEARCH_TYPE_UNSEEN,
4509 full_search ? NULL:imapset, &lep_uidlist);
4511 if (r == MAILIMAP_NO_ERROR) {
4514 uidlist = imap_uid_list_from_lep(lep_uidlist);
4515 mailimap_search_result_free(lep_uidlist);
4517 unseen = g_slist_concat(unseen, uidlist);
4519 imap_handle_error(SESSION(session), r);
4523 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4524 full_search ? NULL:imapset, &lep_uidlist);
4525 if (r == MAILIMAP_NO_ERROR) {
4528 uidlist = imap_uid_list_from_lep(lep_uidlist);
4529 mailimap_search_result_free(lep_uidlist);
4531 flagged = g_slist_concat(flagged, uidlist);
4533 imap_handle_error(SESSION(session), r);
4537 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4538 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4539 full_search ? NULL:imapset, &lep_uidlist);
4540 if (r == MAILIMAP_NO_ERROR) {
4543 uidlist = imap_uid_list_from_lep(lep_uidlist);
4544 mailimap_search_result_free(lep_uidlist);
4546 answered = g_slist_concat(answered, uidlist);
4548 imap_handle_error(SESSION(session), r);
4552 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4553 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4554 full_search ? NULL:imapset, &lep_uidlist);
4555 if (r == MAILIMAP_NO_ERROR) {
4558 uidlist = imap_uid_list_from_lep(lep_uidlist);
4559 mailimap_search_result_free(lep_uidlist);
4561 forwarded = g_slist_concat(forwarded, uidlist);
4563 imap_handle_error(SESSION(session), r);
4568 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4569 full_search ? NULL:imapset, &lep_uidlist);
4570 if (r == MAILIMAP_NO_ERROR) {
4573 uidlist = imap_uid_list_from_lep(lep_uidlist);
4574 mailimap_search_result_free(lep_uidlist);
4576 deleted = g_slist_concat(deleted, uidlist);
4578 imap_handle_error(SESSION(session), r);
4585 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4586 if (r == MAILIMAP_NO_ERROR) {
4587 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4588 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4589 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4590 imap_fetch_uid_flags_list_free(lep_uidtab);
4592 imap_handle_error(SESSION(session), r);
4598 if (r == MAILIMAP_NO_ERROR)
4599 unlock_session(session);
4601 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4603 MsgPermFlags flags, oldflags;
4606 msginfo = (MsgInfo *) elem->data;
4607 flags = msginfo->flags.perm_flags;
4608 wasnew = (flags & MSG_NEW);
4609 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED);
4611 if (folder->account && folder->account->low_bandwidth) {
4612 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4613 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED);
4615 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4618 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4619 if (gslist_find_next_num(&unseen, msginfo->msgnum) == msginfo->msgnum) {
4620 if (!reverse_seen) {
4621 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4623 flags &= ~(MSG_UNREAD | MSG_NEW);
4627 if (gslist_find_next_num(&flagged, msginfo->msgnum) == msginfo->msgnum)
4628 flags |= MSG_MARKED;
4630 flags &= ~MSG_MARKED;
4632 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4633 if (gslist_find_next_num(&answered, msginfo->msgnum) == msginfo->msgnum)
4634 flags |= MSG_REPLIED;
4636 flags &= ~MSG_REPLIED;
4637 if (gslist_find_next_num(&forwarded, msginfo->msgnum) == msginfo->msgnum)
4638 flags |= MSG_FORWARDED;
4640 flags &= ~MSG_FORWARDED;
4641 if (gslist_find_next_num(&deleted, msginfo->msgnum) == msginfo->msgnum)
4642 flags |= MSG_DELETED;
4644 flags &= ~MSG_DELETED;
4647 if (flags_hash != NULL) {
4649 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4650 GINT_TO_POINTER(msginfo->msgnum)));
4653 if ((flags & MSG_UNREAD) == 0)
4659 if (tags_hash != NULL) {
4660 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4664 g_slist_free(msginfo->tags);
4665 msginfo->tags = NULL;
4667 for (cur = tags; cur; cur = cur->next) {
4668 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4670 id = tags_get_id_for_str(real_tag);
4672 id = tags_add_tag(real_tag);
4673 got_alien_tags = TRUE;
4675 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4676 msginfo->tags = g_slist_append(
4678 GINT_TO_POINTER(id));
4682 slist_free_strings(tags);
4687 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4690 if (got_alien_tags) {
4692 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4696 g_hash_table_destroy(flags_hash);
4698 g_hash_table_destroy(tags_hash);
4700 imap_lep_set_free(seq_list);
4701 g_slist_free(flagged);
4702 g_slist_free(deleted);
4703 g_slist_free(answered);
4704 g_slist_free(forwarded);
4705 g_slist_free(unseen);
4706 g_slist_free(sorted_list);
4709 return GINT_TO_POINTER(0);
4712 static gint imap_get_flags(Folder *folder, FolderItem *item,
4713 MsgInfoList *msginfo_list, GRelation *msgflags)
4716 get_flags_data *data = g_new0(get_flags_data, 1);
4718 data->folder = folder;
4720 data->msginfo_list = msginfo_list;
4721 data->msgflags = msgflags;
4722 data->full_search = FALSE;
4724 GSList *tmp = NULL, *cur;
4726 if (prefs_common.work_offline &&
4727 !inc_offline_should_override(FALSE,
4728 _("Claws Mail needs network access in order "
4729 "to access the IMAP server."))) {
4734 tmp = folder_item_get_msg_list(item);
4736 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4737 data->full_search = TRUE;
4739 for (cur = tmp; cur; cur = cur->next)
4740 procmsg_msginfo_free((MsgInfo *)cur->data);
4744 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4751 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4753 gboolean flags_set = GPOINTER_TO_INT(user_data);
4754 gint flags_value = GPOINTER_TO_INT(key);
4755 hashtable_data *data = (hashtable_data *)value;
4756 IMAPFolderItem *_item = data->item;
4757 FolderItem *item = (FolderItem *)_item;
4758 gint ok = MAILIMAP_ERROR_BAD_STATE;
4759 IMAPSession *session = NULL;
4761 debug_print("getting session...\n");
4762 session = imap_session_get(item->folder);
4764 data->msglist = g_slist_reverse(data->msglist);
4766 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4769 g_slist_length(data->msglist));
4771 lock_session(session);
4773 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4774 NULL, NULL, NULL, NULL, NULL, FALSE);
4776 if (ok == MAILIMAP_NO_ERROR) {
4777 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4778 data->msglist, flags_value, NULL, flags_set);
4780 g_warning("can't select mailbox %s\n", item->path);
4784 unlock_session(session);
4786 g_slist_free(data->msglist);
4791 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4793 gboolean tags_set = GPOINTER_TO_INT(user_data);
4794 TagsData *data = (TagsData *)value;
4795 IMAPFolderItem *_item = data->item;
4796 FolderItem *item = (FolderItem *)_item;
4797 gchar *str = data->str;
4798 gint ok = MAILIMAP_ERROR_BAD_STATE;
4799 IMAPSession *session = NULL;
4801 debug_print("getting session...\n");
4802 session = imap_session_get(item->folder);
4804 data->msglist = g_slist_reverse(data->msglist);
4806 debug_print("IMAP %ssetting tags %s for %d messages\n",
4809 g_slist_length(data->msglist));
4811 lock_session(session);
4813 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4814 NULL, NULL, NULL, NULL, NULL, FALSE);
4816 if (ok == MAILIMAP_NO_ERROR) {
4820 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4821 data->msglist, 0, &list, tags_set);
4823 g_warning("can't select mailbox %s\n", item->path);
4827 unlock_session(session);
4829 g_slist_free(data->msglist);
4835 static void process_hashtable(IMAPFolderItem *item)
4837 if (item->flags_set_table) {
4838 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4839 g_hash_table_destroy(item->flags_set_table);
4840 item->flags_set_table = NULL;
4842 if (item->flags_unset_table) {
4843 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4844 g_hash_table_destroy(item->flags_unset_table);
4845 item->flags_unset_table = NULL;
4847 if (item->tags_set_table) {
4848 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4849 g_hash_table_destroy(item->tags_set_table);
4850 item->tags_set_table = NULL;
4852 if (item->tags_unset_table) {
4853 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4854 g_hash_table_destroy(item->tags_unset_table);
4855 item->tags_unset_table = NULL;
4860 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4862 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4863 IMAPSession *session;
4865 g_return_if_fail(item != NULL);
4867 if (item->batching == batch)
4871 item->batching = TRUE;
4872 debug_print("IMAP switching to batch mode\n");
4873 if (!item->flags_set_table) {
4874 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4876 if (!item->flags_unset_table) {
4877 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4879 if (!item->tags_set_table) {
4880 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4882 if (!item->tags_unset_table) {
4883 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4885 session = imap_session_get(folder);
4887 imap_refresh_sensitivity(session);
4888 session->sens_update_block = TRUE;
4891 debug_print("IMAP switching away from batch mode\n");
4893 process_hashtable(item);
4894 item->batching = FALSE;
4895 session = imap_session_get(folder);
4897 session->sens_update_block = FALSE;
4898 imap_refresh_sensitivity(session);
4905 /* data types conversion libetpan <-> claws */
4909 #define ETPAN_IMAP_MB_MARKED 1
4910 #define ETPAN_IMAP_MB_UNMARKED 2
4911 #define ETPAN_IMAP_MB_NOSELECT 4
4912 #define ETPAN_IMAP_MB_NOINFERIORS 8
4914 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4920 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4921 switch (imap_flags->mbf_sflag) {
4922 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4923 flags |= ETPAN_IMAP_MB_MARKED;
4925 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4926 flags |= ETPAN_IMAP_MB_NOSELECT;
4928 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4929 flags |= ETPAN_IMAP_MB_UNMARKED;
4934 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4935 cur = clist_next(cur)) {
4936 struct mailimap_mbx_list_oflag * oflag;
4938 oflag = clist_content(cur);
4940 switch (oflag->of_type) {
4941 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4942 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4950 static GSList * imap_list_from_lep(IMAPFolder * folder,
4951 clist * list, const gchar * real_path, gboolean all)
4954 GSList * item_list = NULL, *llast = NULL;
4956 for(iter = clist_begin(list) ; iter != NULL ;
4957 iter = clist_next(iter)) {
4958 struct mailimap_mailbox_list * mb;
4966 FolderItem *new_item;
4968 mb = clist_content(iter);
4974 if (mb->mb_flag != NULL)
4975 flags = imap_flags_to_flags(mb->mb_flag);
4977 delimiter = mb->mb_delimiter;
4980 dup_name = strdup(name);
4981 if (delimiter != '\0')
4982 subst_char(dup_name, delimiter, '/');
4984 base = g_path_get_basename(dup_name);
4985 if (base[0] == '.') {
4990 if (!all && path_cmp(name, real_path) == 0) {
4996 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4997 dup_name[strlen(dup_name)-1] = '\0';
5000 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
5001 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
5003 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
5004 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
5005 new_item->no_sub = TRUE;
5006 if (strcmp(dup_name, "INBOX") != 0 &&
5007 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
5008 new_item->no_select = TRUE;
5010 if (item_list == NULL)
5011 llast = item_list = g_slist_append(item_list, new_item);
5013 llast = g_slist_append(llast, new_item);
5014 llast = llast->next;
5016 debug_print("folder '%s' found.\n", loc_path);
5027 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
5029 GSList *sorted_list, *cur;
5030 guint first, last, next;
5031 GSList *ret_list = NULL, *llast = NULL;
5032 struct mailimap_set * current_set;
5033 unsigned int item_count;
5035 if (numlist == NULL)
5038 current_set = mailimap_set_new_empty();
5040 sorted_list = g_slist_copy(numlist);
5041 sorted_list = g_slist_sort(sorted_list, g_int_compare);
5043 first = GPOINTER_TO_INT(sorted_list->data);
5046 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
5047 if (GPOINTER_TO_INT(cur->data) == 0)
5052 last = GPOINTER_TO_INT(cur->data);
5054 next = GPOINTER_TO_INT(cur->next->data);
5058 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5060 struct mailimap_set_item * item;
5061 item = mailimap_set_item_new(first, last);
5062 mailimap_set_add(current_set, item);
5066 if (item_count >= folder->max_set_size) {
5067 if (ret_list == NULL)
5068 llast = ret_list = g_slist_append(ret_list,
5071 llast = g_slist_append(llast, current_set);
5072 llast = llast->next;
5075 current_set = mailimap_set_new_empty();
5081 if (clist_count(current_set->set_list) > 0) {
5082 ret_list = g_slist_append(ret_list,
5086 g_slist_free(sorted_list);
5091 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5093 MsgNumberList *numlist = NULL;
5097 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5098 MsgInfo *msginfo = (MsgInfo *) cur->data;
5100 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5102 numlist = g_slist_reverse(numlist);
5103 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5104 g_slist_free(numlist);
5109 static GSList * imap_uid_list_from_lep(clist * list)
5116 for(iter = clist_begin(list) ; iter != NULL ;
5117 iter = clist_next(iter)) {
5120 puid = clist_content(iter);
5121 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5124 result = g_slist_reverse(result);
5128 static GSList * imap_uid_list_from_lep_tab(carray * list)
5135 for(i = 0 ; i < carray_count(list) ; i ++) {
5138 puid = carray_get(list, i);
5139 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5141 result = g_slist_reverse(result);
5145 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5147 GHashTable * tags_hash)
5154 for(i = 0 ; i < carray_count(list) ; i += 3) {
5159 puid = carray_get(list, i);
5160 pflags = carray_get(list, i + 1);
5161 tags = carray_get(list, i + 2);
5163 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5164 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5168 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5171 MsgInfo *msginfo = NULL;
5174 MsgFlags flags = {0, 0};
5176 if (info->headers == NULL)
5179 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5180 if (folder_has_parent_of_type(item, F_QUEUE)) {
5181 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5182 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5183 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5185 flags.perm_flags = info->flags;
5188 size = (goffset) info->size;
5189 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5192 msginfo->msgnum = uid;
5193 msginfo->size = size;
5199 static void imap_lep_set_free(GSList *seq_list)
5203 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5204 struct mailimap_set * imapset;
5206 imapset = cur->data;
5207 mailimap_set_free(imapset);
5209 g_slist_free(seq_list);
5212 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5214 struct mailimap_flag_list * flag_list;
5217 flag_list = mailimap_flag_list_new_empty();
5219 if (IMAP_IS_SEEN(flags))
5220 mailimap_flag_list_add(flag_list,
5221 mailimap_flag_new_seen());
5222 if (IMAP_IS_ANSWERED(flags))
5223 mailimap_flag_list_add(flag_list,
5224 mailimap_flag_new_answered());
5225 if (IMAP_IS_FLAGGED(flags))
5226 mailimap_flag_list_add(flag_list,
5227 mailimap_flag_new_flagged());
5228 if (IMAP_IS_DELETED(flags))
5229 mailimap_flag_list_add(flag_list,
5230 mailimap_flag_new_deleted());
5231 if (IMAP_IS_DRAFT(flags))
5232 mailimap_flag_list_add(flag_list,
5233 mailimap_flag_new_draft());
5234 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5235 mailimap_flag_list_add(flag_list,
5236 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5238 for (; cur; cur = cur->next) {
5240 imap_utf8_to_modified_utf7(cur->data, TRUE);
5241 g_strstrip(enc_str);
5243 mailimap_flag_list_add(flag_list,
5244 mailimap_flag_new_flag_keyword(enc_str));
5250 guint imap_folder_get_refcnt(Folder *folder)
5252 return ((IMAPFolder *)folder)->refcnt;
5255 void imap_folder_ref(Folder *folder)
5257 ((IMAPFolder *)folder)->refcnt++;
5260 void imap_disconnect_all(void)
5263 gboolean short_timeout;
5264 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5268 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5270 short_timeout = !networkmanager_is_online(&error);
5272 short_timeout = TRUE;
5273 g_error_free(error);
5276 short_timeout = TRUE;
5280 imap_main_set_timeout(1);
5282 for (list = account_get_list(); list != NULL; list = list->next) {
5283 PrefsAccount *account = list->data;
5284 if (account->protocol == A_IMAP4) {
5285 RemoteFolder *folder = (RemoteFolder *)account->folder;
5286 if (folder && folder->session) {
5287 IMAPSession *session = (IMAPSession *)folder->session;
5288 imap_threaded_disconnect(FOLDER(folder));
5289 SESSION(session)->state = SESSION_DISCONNECTED;
5290 SESSION(session)->sock = NULL;
5291 session_destroy(SESSION(session));
5292 folder->session = NULL;
5298 imap_main_set_timeout(prefs_common.io_timeout_secs);
5301 void imap_folder_unref(Folder *folder)
5303 if (((IMAPFolder *)folder)->refcnt > 0)
5304 ((IMAPFolder *)folder)->refcnt--;
5307 void imap_cancel_all(void)
5312 folderlist = folder_get_list();
5313 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5314 Folder *folder = (Folder *) cur->data;
5316 if (folder->klass == &imap_class) {
5317 if (imap_is_busy(folder)) {
5318 IMAPSession *imap_session;
5319 RemoteFolder *rfolder;
5321 g_printerr("cancelled\n");
5322 imap_threaded_cancel(folder);
5323 rfolder = (RemoteFolder *) folder;
5324 imap_session = (IMAPSession *) rfolder->session;
5326 imap_session->cancelled = 1;
5332 gboolean imap_cancel_all_enabled(void)
5337 folderlist = folder_get_list();
5338 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5339 Folder *folder = (Folder *) cur->data;
5341 if (folder->klass == &imap_class) {
5342 if (imap_is_busy(folder)) {
5351 static gboolean imap_is_busy(Folder *folder)
5353 IMAPSession *imap_session;
5354 RemoteFolder *rfolder;
5356 rfolder = (RemoteFolder *) folder;
5357 imap_session = (IMAPSession *) rfolder->session;
5358 if (imap_session == NULL)
5361 return imap_session->busy;
5364 #else /* HAVE_LIBETPAN */
5366 static FolderClass imap_class;
5368 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5369 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5371 static Folder *imap_folder_new (const gchar *name,
5374 static gboolean missing_imap_warning = TRUE;
5375 if (missing_imap_warning) {
5376 missing_imap_warning = FALSE;
5378 _("You have one or more IMAP accounts "
5379 "defined. However this version of "
5380 "Claws Mail has been built without "
5381 "IMAP support; your IMAP account(s) are "
5383 "You probably need to "
5384 "install libetpan and recompile "
5389 static gint imap_create_tree (Folder *folder)
5393 static FolderItem *imap_create_folder (Folder *folder,
5399 static gint imap_rename_folder (Folder *folder,
5406 gchar imap_get_path_separator_for_item(FolderItem *item)
5411 FolderClass *imap_get_class(void)
5413 if (imap_class.idstr == NULL) {
5414 imap_class.type = F_IMAP;
5415 imap_class.idstr = "imap";
5416 imap_class.uistr = "IMAP4";
5418 imap_class.new_folder = imap_folder_new;
5419 imap_class.create_tree = imap_create_tree;
5420 imap_class.create_folder = imap_create_folder;
5421 imap_class.rename_folder = imap_rename_folder;
5423 imap_class.set_xml = folder_set_xml;
5424 imap_class.get_xml = folder_get_xml;
5425 imap_class.item_set_xml = imap_item_set_xml;
5426 imap_class.item_get_xml = imap_item_get_xml;
5427 /* nothing implemented */
5433 void imap_disconnect_all(void)
5437 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5442 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5447 void imap_cache_msg(FolderItem *item, gint msgnum)
5451 void imap_cancel_all(void)
5455 gboolean imap_cancel_all_enabled(void)
5462 #ifdef HAVE_LIBETPAN
5463 static void imap_synchronise(FolderItem *item, gint days)
5465 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5466 debug_print("%s already synced\n", item->path?item->path:item->name);
5469 debug_print("syncing %s\n", item->path?item->path:item->name);
5470 imap_gtk_synchronise(item, days);
5471 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5475 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5477 #ifdef HAVE_LIBETPAN
5480 folder_item_set_xml(folder, item, tag);
5482 #ifdef HAVE_LIBETPAN
5483 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5484 XMLAttr *attr = (XMLAttr *) cur->data;
5486 if (!attr || !attr->name || !attr->value) continue;
5487 if (!strcmp(attr->name, "uidnext"))
5488 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5489 if (!strcmp(attr->name, "last_sync"))
5490 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5491 if (!strcmp(attr->name, "last_change"))
5492 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5494 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5495 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5499 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5503 tag = folder_item_get_xml(folder, item);
5505 #ifdef HAVE_LIBETPAN
5506 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5507 IMAP_FOLDER_ITEM(item)->uid_next));
5508 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5509 IMAP_FOLDER_ITEM(item)->last_sync));
5510 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5511 IMAP_FOLDER_ITEM(item)->last_change));
5517 /* ===================================================================
5518 * UTF-7 conversion routines as in RFC 2192
5519 * ===================================================================
5520 * These two functions from:
5522 * Copyright (C) 2003-2004 Pawel Salek. */
5524 /* UTF7 modified base64 alphabet */
5525 static char base64chars[] =
5526 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5527 #define UNDEFINED 64
5529 /* UTF16 definitions */
5530 #define UTF16MASK 0x03FFUL
5531 #define UTF16SHIFT 10
5532 #define UTF16BASE 0x10000UL
5533 #define UTF16HIGHSTART 0xD800UL
5534 #define UTF16HIGHEND 0xDBFFUL
5535 #define UTF16LOSTART 0xDC00UL
5536 #define UTF16LOEND 0xDFFFUL
5539 /* Convert an IMAP mailbox to a UTF-8 string.
5540 * dst needs to have roughly 4 times the storage space of src
5541 * Hex encoding can triple the size of the input
5542 * UTF-7 can be slightly denser than UTF-8
5543 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5545 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5547 unsigned c, i, bitcount;
5548 unsigned long ucs4, utf16, bitbuf;
5549 unsigned char base64[256];
5551 char *dst, *res = malloc(2*strlen(mbox)+1);
5556 if(!dst) return NULL;
5557 /* initialize modified base64 decoding table */
5558 memset(base64, UNDEFINED, sizeof (base64));
5559 for (i = 0; i < sizeof (base64chars); ++i) {
5560 base64[(unsigned)base64chars[i]] = i;
5563 /* loop until end of string */
5564 while (*src != '\0') {
5566 /* deal with literal characters and &- */
5567 if (c != '&' || *src == '-') {
5568 /* encode literally */
5569 if (change_spaces && c == '_')
5573 /* skip over the '-' if this is an &- sequence */
5574 if (c == '&') ++src;
5576 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5580 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5582 bitbuf = (bitbuf << 6) | c;
5584 /* enough bits for a UTF-16 character? */
5585 if (bitcount >= 16) {
5587 utf16 = (bitcount ? bitbuf >> bitcount
5589 /* convert UTF16 to UCS4 */
5591 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5592 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5595 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5596 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5601 /* convert UTF-16 range of UCS4 to UTF-8 */
5602 if (ucs4 <= 0x7fUL) {
5605 } else if (ucs4 <= 0x7ffUL) {
5606 dst[0] = 0xc0 | (ucs4 >> 6);
5607 dst[1] = 0x80 | (ucs4 & 0x3f);
5609 } else if (ucs4 <= 0xffffUL) {
5610 dst[0] = 0xe0 | (ucs4 >> 12);
5611 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5612 dst[2] = 0x80 | (ucs4 & 0x3f);
5615 dst[0] = 0xf0 | (ucs4 >> 18);
5616 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5617 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5618 dst[3] = 0x80 | (ucs4 & 0x3f);
5623 /* skip over trailing '-' in modified UTF-7 encoding */
5624 if (*src == '-') ++src;
5627 /* terminate destination string */
5632 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5633 * dst should be about twice the length of src to deal with non-hex
5636 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5638 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5639 unsigned long ucs4 = 0, bitbuf = 0;
5641 /* initialize hex lookup table */
5642 char *dst, *res = malloc(2*strlen(src)+1);
5644 if(!dst) return NULL;
5650 while ((c = (unsigned char)*src) != '\0') {
5652 /* normal character? */
5653 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5654 /* switch out of UTF-7 mode */
5657 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5665 if (change_spaces && c == ' ')
5669 /* encode '&' as '&-' */
5675 /* switch to UTF-7 mode */
5680 /* Encode US-ASCII characters as themselves */
5684 } else if (utf8total) {
5685 /* save UTF8 bits into UCS4 */
5686 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5687 if (++utf8pos < utf8total) {
5695 } else if (c < 0xF0) {
5699 /* NOTE: can't convert UTF8 sequences longer than 4 */
5705 /* loop to split ucs4 into two utf16 chars if necessary */
5708 if (ucs4 >= UTF16BASE) {
5710 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5712 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5715 bitbuf = (bitbuf << 16) | ucs4;
5719 /* spew out base64 */
5720 while (bitstogo >= 6) {
5722 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5726 } while (utf16flag);
5728 /* if in UTF-7 mode, finish in ASCII */
5731 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5735 /* tie off string */