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 void 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,
315 static gchar *imap_get_real_path (IMAPSession *session,
319 static void imap_synchronise (FolderItem *item, gint days);
321 static gboolean imap_is_busy (Folder *folder);
323 static void imap_free_capabilities (IMAPSession *session);
325 /* low-level IMAP4rev1 commands */
326 static gint imap_cmd_login (IMAPSession *session,
330 static gint imap_cmd_noop (IMAPSession *session);
331 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
332 static gint imap_cmd_starttls (IMAPSession *session);
334 static gint imap_cmd_select (IMAPSession *session,
339 guint32 *uid_validity,
340 gint *can_create_flags,
343 static gint imap_cmd_close (IMAPSession *session);
344 static gint imap_cmd_examine (IMAPSession *session,
349 guint32 *uid_validity,
351 static gint imap_cmd_create (IMAPSession *sock,
352 const gchar *folder);
353 static gint imap_cmd_rename (IMAPSession *sock,
354 const gchar *oldfolder,
355 const gchar *newfolder);
356 static gint imap_cmd_delete (IMAPSession *session,
357 const gchar *folder);
358 static gint imap_cmd_fetch (IMAPSession *sock,
360 const gchar *filename,
363 static gint imap_cmd_append (IMAPSession *session,
364 IMAPFolderItem *item,
365 const gchar *destfolder,
369 static gint imap_cmd_copy (IMAPSession *session,
370 struct mailimap_set * set,
371 const gchar *destfolder,
372 GRelation *uid_mapping,
373 struct mailimap_set ** source,
374 struct mailimap_set ** dest);
375 static gint imap_cmd_store (IMAPSession *session,
376 IMAPFolderItem *item,
377 struct mailimap_set * set,
381 static gint imap_cmd_expunge (IMAPSession *session);
383 static void imap_path_separator_subst (gchar *str,
386 static gboolean imap_rename_folder_func (GNode *node,
388 static gint imap_get_num_list (Folder *folder,
391 gboolean *old_uids_valid);
392 static GSList *imap_get_msginfos (Folder *folder,
394 GSList *msgnum_list);
395 static MsgInfo *imap_get_msginfo (Folder *folder,
398 static gboolean imap_scan_required (Folder *folder,
400 static void imap_change_flags (Folder *folder,
403 MsgPermFlags newflags);
404 static gint imap_get_flags (Folder *folder,
406 MsgInfoList *msglist,
407 GRelation *msgflags);
408 static gchar *imap_folder_get_path (Folder *folder);
409 static gchar *imap_item_get_path (Folder *folder,
411 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
414 /* data types conversion libetpan <-> claws */
415 static GSList * imap_list_from_lep(IMAPFolder * folder,
416 clist * list, const gchar * real_path, gboolean all);
417 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
418 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
419 static GSList * imap_uid_list_from_lep(clist * list);
420 static GSList * imap_uid_list_from_lep_tab(carray * list);
421 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
423 GHashTable *tags_hash);
424 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
426 static void imap_lep_set_free(GSList *seq_list);
427 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
429 typedef struct _hashtable_data {
431 IMAPFolderItem *item;
434 static FolderClass imap_class;
436 typedef struct _thread_data {
441 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
446 FolderClass *imap_get_class(void)
448 if (imap_class.idstr == NULL) {
449 imap_class.type = F_IMAP;
450 imap_class.idstr = "imap";
451 imap_class.uistr = "IMAP4";
453 /* Folder functions */
454 imap_class.new_folder = imap_folder_new;
455 imap_class.destroy_folder = imap_folder_destroy;
456 imap_class.scan_tree = imap_scan_tree;
457 imap_class.create_tree = imap_create_tree;
459 /* FolderItem functions */
460 imap_class.item_new = imap_folder_item_new;
461 imap_class.item_destroy = imap_folder_item_destroy;
462 imap_class.item_get_path = imap_item_get_path;
463 imap_class.create_folder = imap_create_folder;
464 imap_class.rename_folder = imap_rename_folder;
465 imap_class.remove_folder = imap_remove_folder;
466 imap_class.close = imap_close;
467 imap_class.get_num_list = imap_get_num_list;
468 imap_class.scan_required = imap_scan_required;
469 imap_class.set_xml = folder_set_xml;
470 imap_class.get_xml = folder_get_xml;
471 imap_class.item_set_xml = imap_item_set_xml;
472 imap_class.item_get_xml = imap_item_get_xml;
474 /* Message functions */
475 imap_class.get_msginfo = imap_get_msginfo;
476 imap_class.get_msginfos = imap_get_msginfos;
477 imap_class.fetch_msg = imap_fetch_msg;
478 imap_class.fetch_msg_full = imap_fetch_msg_full;
479 imap_class.add_msg = imap_add_msg;
480 imap_class.add_msgs = imap_add_msgs;
481 imap_class.copy_msg = imap_copy_msg;
482 imap_class.copy_msgs = imap_copy_msgs;
483 imap_class.remove_msg = imap_remove_msg;
484 imap_class.remove_msgs = imap_remove_msgs;
485 imap_class.remove_all_msg = imap_remove_all_msg;
486 imap_class.is_msg_changed = imap_is_msg_changed;
487 imap_class.change_flags = imap_change_flags;
488 imap_class.get_flags = imap_get_flags;
489 imap_class.set_batch = imap_set_batch;
490 imap_class.synchronise = imap_synchronise;
491 imap_class.remove_cached_msg = imap_remove_cached_msg;
492 imap_class.commit_tags = imap_commit_tags;
494 pthread_mutex_init(&imap_mutex, NULL);
501 static void imap_refresh_sensitivity (IMAPSession *session)
505 if (session->sens_update_block)
507 mainwin = mainwindow_get_mainwindow();
509 toolbar_main_set_sensitive(mainwin);
510 main_window_set_menu_sensitive(mainwin);
514 static void lock_session(IMAPSession *session)
517 debug_print("locking session %p (%d)\n", session, session->busy);
519 debug_print(" SESSION WAS LOCKED !! \n");
520 session->busy = TRUE;
521 imap_refresh_sensitivity(session);
523 debug_print("can't lock null session\n");
527 static void unlock_session(IMAPSession *session)
530 debug_print("unlocking session %p\n", session);
531 session->busy = FALSE;
532 imap_refresh_sensitivity(session);
534 debug_print("can't unlock null session\n");
538 static void imap_disc_session_destroy(Folder *folder)
540 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
541 IMAPSession *session = NULL;
545 session = IMAP_SESSION(rfolder->session);
548 rfolder->session = NULL;
549 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
550 SESSION(session)->state = SESSION_DISCONNECTED;
551 session_destroy(SESSION(session));
554 static gboolean is_fatal(int libetpan_errcode)
556 switch(libetpan_errcode) {
557 case MAILIMAP_ERROR_STREAM:
558 case MAILIMAP_ERROR_PROTOCOL:
559 case MAILIMAP_ERROR_PARSE:
560 case MAILIMAP_ERROR_BAD_STATE:
567 static void imap_handle_error(Session *session, int libetpan_errcode)
569 switch(libetpan_errcode) {
570 case MAILIMAP_NO_ERROR:
572 case MAILIMAP_NO_ERROR_AUTHENTICATED:
573 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
575 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
576 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
578 case MAILIMAP_ERROR_BAD_STATE:
579 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
581 case MAILIMAP_ERROR_STREAM:
582 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
584 case MAILIMAP_ERROR_PARSE:
585 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
586 "(very probably non-RFC compliance from the server)\n"));
588 case MAILIMAP_ERROR_CONNECTION_REFUSED:
589 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
591 case MAILIMAP_ERROR_MEMORY:
592 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
594 case MAILIMAP_ERROR_FATAL:
595 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
597 case MAILIMAP_ERROR_PROTOCOL:
598 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
599 "(very probably non-RFC compliance from the server)\n"));
601 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
602 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
604 case MAILIMAP_ERROR_APPEND:
605 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
607 case MAILIMAP_ERROR_NOOP:
608 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
610 case MAILIMAP_ERROR_LOGOUT:
611 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
613 case MAILIMAP_ERROR_CAPABILITY:
614 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
616 case MAILIMAP_ERROR_CHECK:
617 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
619 case MAILIMAP_ERROR_CLOSE:
620 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
622 case MAILIMAP_ERROR_EXPUNGE:
623 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
625 case MAILIMAP_ERROR_COPY:
626 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
628 case MAILIMAP_ERROR_UID_COPY:
629 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
631 case MAILIMAP_ERROR_CREATE:
632 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
634 case MAILIMAP_ERROR_DELETE:
635 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
637 case MAILIMAP_ERROR_EXAMINE:
638 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
640 case MAILIMAP_ERROR_FETCH:
641 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
643 case MAILIMAP_ERROR_UID_FETCH:
644 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
646 case MAILIMAP_ERROR_LIST:
647 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
649 case MAILIMAP_ERROR_LOGIN:
650 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
652 case MAILIMAP_ERROR_LSUB:
653 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
655 case MAILIMAP_ERROR_RENAME:
656 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
658 case MAILIMAP_ERROR_SEARCH:
659 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
661 case MAILIMAP_ERROR_UID_SEARCH:
662 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
664 case MAILIMAP_ERROR_SELECT:
665 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
667 case MAILIMAP_ERROR_STATUS:
668 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
670 case MAILIMAP_ERROR_STORE:
671 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
673 case MAILIMAP_ERROR_UID_STORE:
674 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
676 case MAILIMAP_ERROR_SUBSCRIBE:
677 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
679 case MAILIMAP_ERROR_UNSUBSCRIBE:
680 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
682 case MAILIMAP_ERROR_STARTTLS:
683 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
685 case MAILIMAP_ERROR_INVAL:
686 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
688 case MAILIMAP_ERROR_EXTENSION:
689 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
691 case MAILIMAP_ERROR_SASL:
692 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
694 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
695 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
696 case MAILIMAP_ERROR_SSL:
697 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
702 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
707 if (session && is_fatal(libetpan_errcode)) {
708 imap_disc_session_destroy(IMAP_SESSION(session)->folder);
709 } else if (session && !is_fatal(libetpan_errcode)) {
710 if (IMAP_SESSION(session)->busy)
711 unlock_session(IMAP_SESSION(session));
715 static Folder *imap_folder_new(const gchar *name, const gchar *path)
719 folder = (Folder *)g_new0(IMAPFolder, 1);
720 folder->klass = &imap_class;
721 imap_folder_init(folder, name, path);
726 static void imap_folder_destroy(Folder *folder)
728 while (imap_folder_get_refcnt(folder) > 0)
729 gtk_main_iteration();
731 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
735 static void imap_folder_init(Folder *folder, const gchar *name,
738 folder_remote_folder_init((Folder *)folder, name, path);
739 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
742 static FolderItem *imap_folder_item_new(Folder *folder)
744 IMAPFolderItem *item;
746 item = g_new0(IMAPFolderItem, 1);
749 item->uid_list = NULL;
751 return (FolderItem *)item;
754 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
756 IMAPFolderItem *item = (IMAPFolderItem *)_item;
758 g_return_if_fail(item != NULL);
759 g_slist_free(item->uid_list);
764 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
766 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
769 g_slist_free(item->uid_list);
770 item->uid_list = NULL;
775 static void imap_reset_uid_lists(Folder *folder)
777 if(folder->node == NULL)
780 /* Destroy all uid lists and rest last uid */
781 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
784 static int imap_get_capabilities(IMAPSession *session)
786 struct mailimap_capability_data *capabilities = NULL;
790 if (session->capability != NULL)
791 return MAILIMAP_NO_ERROR;
793 capabilities = imap_threaded_capability(session->folder, &result);
795 if (result != MAILIMAP_NO_ERROR) {
796 imap_handle_error(SESSION(session), result);
797 return MAILIMAP_ERROR_CAPABILITY;
800 if (capabilities == NULL) {
801 return MAILIMAP_NO_ERROR;
804 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
805 cur = clist_next(cur)) {
806 struct mailimap_capability * cap =
808 if (!cap || cap->cap_data.cap_name == NULL)
810 session->capability = g_slist_append
811 (session->capability,
812 g_strdup(cap->cap_data.cap_name));
813 debug_print("got capa %s\n", cap->cap_data.cap_name);
815 mailimap_capability_data_free(capabilities);
816 return MAILIMAP_NO_ERROR;
819 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
822 for (cur = session->capability; cur; cur = cur->next) {
823 if (!g_ascii_strcasecmp(cur->data, cap))
829 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
832 gint ok = MAILIMAP_ERROR_BAD_STATE;
833 static time_t last_login_err = 0;
834 gchar *ext_info = "";
836 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
837 imap_handle_error(SESSION(session), r);
842 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
844 case IMAP_AUTH_CRAM_MD5:
845 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
847 case IMAP_AUTH_DIGEST_MD5:
848 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
850 case IMAP_AUTH_LOGIN:
851 ok = imap_cmd_login(session, user, pass, "LOGIN");
853 case IMAP_AUTH_GSSAPI:
854 ok = imap_cmd_login(session, user, pass, "GSSAPI");
857 debug_print("capabilities:\n"
863 imap_has_capability(session, "ANONYMOUS"),
864 imap_has_capability(session, "CRAM-MD5"),
865 imap_has_capability(session, "DIGEST-MD5"),
866 imap_has_capability(session, "LOGIN"),
867 imap_has_capability(session, "GSSAPI"));
868 if (imap_has_capability(session, "CRAM-MD5"))
869 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
870 if ((ok == MAILIMAP_ERROR_BAD_STATE ||
871 ok == MAILIMAP_ERROR_LOGIN) && imap_has_capability(session, "DIGEST-MD5"))
872 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
873 if ((ok == MAILIMAP_ERROR_BAD_STATE ||
874 ok == MAILIMAP_ERROR_LOGIN) && imap_has_capability(session, "GSSAPI"))
875 ok = imap_cmd_login(session, user, pass, "GSSAPI");
876 if (ok == MAILIMAP_ERROR_BAD_STATE||
877 ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
878 ok = imap_cmd_login(session, user, pass, "LOGIN");
881 if (ok == MAILIMAP_NO_ERROR)
882 session->authenticated = TRUE;
884 if (type == IMAP_AUTH_CRAM_MD5) {
885 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
886 "compiled with SASL support and the "
887 "CRAM-MD5 SASL plugin is installed.");
890 if (type == IMAP_AUTH_DIGEST_MD5) {
891 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
892 "compiled with SASL support and the "
893 "DIGEST-MD5 SASL plugin is installed.");
896 if (time(NULL) - last_login_err > 10) {
897 if (!prefs_common.no_recv_err_panel) {
898 alertpanel_error(_("Connection to %s failed: "
900 SESSION(session)->server, ext_info);
902 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
903 "login refused.%s\n"),
904 SESSION(session)->server, ext_info);
907 last_login_err = time(NULL);
912 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
914 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
915 /* Check if this is the first try to establish a
916 connection, if yes we don't try to reconnect */
917 debug_print("reconnecting\n");
918 if (rfolder->session == NULL) {
919 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
920 folder->account->recv_server);
921 session_destroy(SESSION(session));
924 rfolder->session = NULL;
925 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
926 " disconnected. Reconnecting...\n"),
927 folder->account->recv_server);
928 statusbar_print_all(_("IMAP4 connection to %s has been"
929 " disconnected. Reconnecting...\n"),
930 folder->account->recv_server);
931 SESSION(session)->state = SESSION_DISCONNECTED;
932 session_destroy(SESSION(session));
933 /* Clear folders session to make imap_session_get create
934 a new session, because of rfolder->session == NULL
935 it will not try to reconnect again and so avoid an
937 debug_print("getting session...\n");
938 session = imap_session_get(folder);
939 rfolder->session = SESSION(session);
945 static IMAPSession *imap_session_get(Folder *folder)
947 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
948 IMAPSession *session = NULL;
949 gint r = MAILIMAP_NO_ERROR;
951 g_return_val_if_fail(folder != NULL, NULL);
952 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
953 g_return_val_if_fail(folder->account != NULL, NULL);
955 if (prefs_common.work_offline &&
956 !inc_offline_should_override(FALSE,
957 _("Claws Mail needs network access in order "
958 "to access the IMAP server."))) {
962 /* Make sure we have a session */
963 if (rfolder->session != NULL) {
964 session = IMAP_SESSION(rfolder->session);
965 } else if (rfolder->connecting) {
966 debug_print("already connecting\n");
969 imap_reset_uid_lists(folder);
970 if (time(NULL) - rfolder->last_failure <= 2)
972 rfolder->connecting = TRUE;
973 session = imap_session_new(folder, folder->account);
975 if(session == NULL) {
976 rfolder->last_failure = time(NULL);
977 rfolder->connecting = FALSE;
981 /* Make sure session is authenticated */
982 if (!IMAP_SESSION(session)->authenticated)
983 imap_session_authenticate(IMAP_SESSION(session), folder->account);
985 if (!IMAP_SESSION(session)->authenticated) {
986 imap_threaded_disconnect(session->folder);
987 rfolder->session = NULL;
988 SESSION(session)->state = SESSION_DISCONNECTED;
989 session_destroy(SESSION(session));
990 rfolder->last_failure = time(NULL);
991 rfolder->connecting = FALSE;
995 /* I think the point of this code is to avoid sending a
996 * keepalive if we've used the session recently and therefore
997 * think it's still alive. Unfortunately, most of the code
998 * does not yet check for errors on the socket, and so if the
999 * connection drops we don't notice until the timeout expires.
1000 * A better solution than sending a NOOP every time would be
1001 * for every command to be prepared to retry until it is
1002 * successfully sent. -- mbp */
1003 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1004 /* verify that the session is still alive */
1005 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1006 debug_print("disconnected!\n");
1008 session = imap_reconnect_if_possible(folder, session);
1010 session = imap_session_get(folder);
1013 session->cancelled = FALSE;
1016 rfolder->session = SESSION(session);
1017 rfolder->connecting = FALSE;
1019 return IMAP_SESSION(session);
1022 static IMAPSession *imap_session_new(Folder * folder,
1023 const PrefsAccount *account)
1025 IMAPSession *session;
1028 int authenticated = FALSE;
1030 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1031 /* FIXME: IMAP over SSL only... */
1034 port = account->set_imapport ? account->imapport
1035 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1036 ssl_type = account->ssl_imap;
1038 if (account->ssl_imap != SSL_NONE) {
1039 if (alertpanel_full(_("Insecure connection"),
1040 _("This connection is configured to be secured "
1041 "using SSL, but SSL is not available in this "
1042 "build of Claws Mail. \n\n"
1043 "Do you want to continue connecting to this "
1044 "server? The communication would not be "
1046 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1047 NULL, FALSE, NULL, ALERT_WARNING,
1048 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1051 port = account->set_imapport ? account->imapport
1056 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1058 if (account->set_tunnelcmd) {
1059 r = imap_threaded_connect_cmd(folder,
1061 account->recv_server,
1067 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1068 if (ssl_type == SSL_TUNNEL) {
1069 r = imap_threaded_connect_ssl(folder,
1070 account->recv_server,
1076 r = imap_threaded_connect(folder,
1077 account->recv_server,
1082 statuswindow_pop_all();
1083 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1084 authenticated = TRUE;
1086 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1087 authenticated = FALSE;
1090 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1091 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1092 if (r == MAILIMAP_ERROR_SSL)
1093 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1097 imap_handle_error(NULL, r);
1099 if(!prefs_common.no_recv_err_panel) {
1100 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1101 account->recv_server, port);
1103 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1104 account->recv_server, port);
1110 session = g_new0(IMAPSession, 1);
1111 session_init(SESSION(session));
1112 SESSION(session)->type = SESSION_IMAP;
1113 SESSION(session)->server = g_strdup(account->recv_server);
1114 SESSION(session)->sock = NULL;
1116 SESSION(session)->destroy = imap_session_destroy;
1118 session->capability = NULL;
1120 session->authenticated = authenticated;
1121 session->mbox = NULL;
1122 session->exists = 0;
1123 session->recent = 0;
1124 session->expunge = 0;
1125 session->cmd_count = 0;
1126 session->folder = folder;
1127 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1129 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1130 if (account->ssl_imap == SSL_STARTTLS) {
1133 ok = imap_cmd_starttls(session);
1134 if (ok != MAILIMAP_NO_ERROR) {
1135 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1136 session_destroy(SESSION(session));
1140 imap_free_capabilities(session);
1141 session->authenticated = FALSE;
1142 session->uidplus = FALSE;
1143 session->cmd_count = 1;
1146 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1147 (session->authenticated) ? "pre" : "un");
1152 static void imap_session_authenticate(IMAPSession *session,
1153 const PrefsAccount *account)
1155 gchar *pass, *acc_pass;
1156 gboolean failed = FALSE;
1158 g_return_if_fail(account->userid != NULL);
1159 acc_pass = account->passwd;
1162 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1164 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1167 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
1169 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1172 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1173 account->recv_server);
1174 if (imap_auth(session, account->userid, pass, account->imap_auth_type) != MAILIMAP_NO_ERROR) {
1175 statusbar_pop_all();
1182 if (prefs_common.no_recv_err_panel) {
1183 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
1184 mainwindow_show_error();
1186 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1192 statuswindow_pop_all();
1193 session->authenticated = TRUE;
1197 static void imap_session_destroy(Session *session)
1199 if (session->state != SESSION_DISCONNECTED)
1200 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1202 imap_free_capabilities(IMAP_SESSION(session));
1203 g_free(IMAP_SESSION(session)->mbox);
1204 sock_close(session->sock);
1205 session->sock = NULL;
1208 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1210 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1213 static guint get_file_size_with_crs(const gchar *filename)
1219 if (filename == NULL)
1222 fp = fopen(filename, "rb");
1226 while (fgets(buf, sizeof (buf), fp) != NULL) {
1228 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1236 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1238 gchar *path, *filename;
1240 path = folder_item_get_path(item);
1242 if (!is_dir_exist(path)) {
1247 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1250 if (is_file_exist(filename)) {
1256 typedef struct _TagsData {
1259 IMAPFolderItem *item;
1262 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1264 IMAPSession *session;
1265 gint ok, can_create_tags;
1266 Folder *folder = NULL;
1267 TagsData *ht_data = NULL;
1270 g_return_if_fail(item != NULL);
1271 g_return_if_fail(msginfo != NULL);
1273 folder = item->folder;
1274 debug_print("getting session...\n");
1275 session = imap_session_get(folder);
1278 debug_print("can't get session\n");
1282 ok = imap_select(session, IMAP_FOLDER(folder), item,
1283 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1285 if (ok != MAILIMAP_NO_ERROR) {
1286 imap_handle_error(SESSION(session), ok);
1291 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1294 if (IMAP_FOLDER_ITEM(item)->batching) {
1295 /* instead of performing an UID STORE command for each message change,
1296 * as a lot of them can change "together", we just fill in hashtables
1297 * and defer the treatment so that we're able to send only one
1300 debug_print("IMAP batch mode on, deferring tags change\n");
1301 for (cur = tags_set; cur; cur = cur->next) {
1302 gint cur_tag = GPOINTER_TO_INT(cur->data);
1304 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1305 GINT_TO_POINTER(cur_tag));
1306 if (ht_data == NULL) {
1307 ht_data = g_new0(TagsData, 1);
1308 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1309 ht_data->item = IMAP_FOLDER_ITEM(item);
1310 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1311 GINT_TO_POINTER(cur_tag), ht_data);
1313 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1316 for (cur = tags_unset; cur; cur = cur->next) {
1317 gint cur_tag = GPOINTER_TO_INT(cur->data);
1319 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1320 GINT_TO_POINTER(cur_tag));
1321 if (ht_data == NULL) {
1322 ht_data = g_new0(TagsData, 1);
1323 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1324 ht_data->item = IMAP_FOLDER_ITEM(item);
1325 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1326 GINT_TO_POINTER(cur_tag), ht_data);
1328 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1332 GSList *list_set = NULL;
1333 GSList *list_unset = NULL;
1336 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1337 numlist.next = NULL;
1339 debug_print("IMAP changing tags NOW\n");
1340 for (cur = tags_set; cur; cur = cur->next) {
1341 gint cur_tag = GPOINTER_TO_INT(cur->data);
1342 const gchar *str = tags_get_tag(cur_tag);
1343 list_set = g_slist_prepend(list_set, g_strdup(str));
1346 ok = imap_set_message_flags(session,
1347 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1348 slist_free_strings(list_set);
1349 g_slist_free(list_set);
1350 if (ok != MAILIMAP_NO_ERROR) {
1355 for (cur = tags_unset; cur; cur = cur->next) {
1356 gint cur_tag = GPOINTER_TO_INT(cur->data);
1357 const gchar *str = tags_get_tag(cur_tag);
1358 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1361 ok = imap_set_message_flags(session,
1362 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1363 slist_free_strings(list_unset);
1364 g_slist_free(list_unset);
1365 if (ok != MAILIMAP_NO_ERROR) {
1372 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1373 gboolean headers, gboolean body)
1375 gchar *path, *filename;
1376 IMAPSession *session;
1379 g_return_val_if_fail(folder != NULL, NULL);
1380 g_return_val_if_fail(item != NULL, NULL);
1385 path = folder_item_get_path(item);
1386 if (!is_dir_exist(path))
1387 make_dir_hier(path);
1388 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1390 debug_print("trying to fetch cached %s\n", filename);
1391 if (is_file_exist(filename)) {
1392 /* see whether the local file represents the whole message
1393 * or not. As the IMAP server reports size with \r chars,
1394 * we have to update the local file (UNIX \n only) size */
1395 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1396 guint have_size = -1;
1399 debug_print("message %d has been already %scached.\n", uid,
1400 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1402 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1403 have_size = get_file_size_with_crs(filename);
1404 if (cached && (cached->size <= have_size || !body)) {
1405 procmsg_msginfo_free(cached);
1406 ok = file_strip_crs(filename);
1407 if (ok == 0 && cached && cached->size <= have_size) {
1408 /* we have it all and stripped */
1409 debug_print("...fully cached in fact; setting flag.\n");
1410 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1413 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1414 debug_print("message not cached and file recent, considering file complete\n");
1415 ok = file_strip_crs(filename);
1419 procmsg_msginfo_free(cached);
1422 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1423 procmsg_msginfo_free(cached);
1427 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1429 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1430 procmsg_msginfo_free(cached);
1434 debug_print("getting session...\n");
1435 session = imap_session_get(folder);
1441 session_set_access_time(SESSION(session));
1442 lock_session(session); /* unlocked later in the function */
1444 debug_print("IMAP fetching messages\n");
1445 ok = imap_select(session, IMAP_FOLDER(folder), item,
1446 NULL, NULL, NULL, NULL, NULL, FALSE);
1447 if (ok != MAILIMAP_NO_ERROR) {
1448 g_warning("can't select mailbox %s\n", item->path);
1453 session_set_access_time(SESSION(session));
1455 debug_print("getting message %d...\n", uid);
1456 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1458 if (ok != MAILIMAP_NO_ERROR) {
1459 g_warning("can't fetch message %d\n", uid);
1464 session_set_access_time(SESSION(session));
1465 unlock_session(session);
1467 ok = file_strip_crs(filename);
1469 if (ok == 0 && headers && body) {
1470 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1472 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1473 procmsg_msginfo_free(cached);
1475 } else if (ok == -1) {
1476 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1478 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1479 procmsg_msginfo_free(cached);
1485 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1487 gchar *path, *filename;
1489 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1494 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1495 procmsg_msginfo_free(cached);
1498 path = folder_item_get_path(item);
1499 if (!is_dir_exist(path))
1502 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1504 if (is_file_exist(filename)) {
1505 if (cached && cached->total_size == cached->size) {
1508 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1511 size = get_file_size_with_crs(filename);
1514 if (cached && size >= cached->size) {
1515 cached->total_size = cached->size;
1516 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1517 procmsg_msginfo_free(cached);
1521 procmsg_msginfo_free(cached);
1525 void imap_cache_msg(FolderItem *item, gint msgnum)
1527 Folder *folder = NULL;
1531 folder = item->folder;
1533 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1534 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1535 debug_print("fetched %s\n", tmp);
1540 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1541 const gchar *file, MsgFlags *flags)
1545 MsgFileInfo fileinfo;
1547 g_return_val_if_fail(file != NULL, -1);
1549 fileinfo.msginfo = NULL;
1550 fileinfo.file = (gchar *)file;
1551 fileinfo.flags = flags;
1552 file_list.data = &fileinfo;
1553 file_list.next = NULL;
1555 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1559 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1560 GRelation *relation)
1563 IMAPSession *session;
1564 guint32 last_uid = 0;
1566 MsgFileInfo *fileinfo;
1567 gint ok = MAILIMAP_NO_ERROR;
1568 gint curnum = 0, total = 0;
1569 gboolean missing_uids = FALSE;
1571 g_return_val_if_fail(folder != NULL, -1);
1572 g_return_val_if_fail(dest != NULL, -1);
1573 g_return_val_if_fail(file_list != NULL, -1);
1575 debug_print("getting session...\n");
1576 session = imap_session_get(folder);
1580 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1582 statusbar_print_all(_("Adding messages..."));
1583 total = g_slist_length(file_list);
1584 for (cur = file_list; cur != NULL; cur = cur->next) {
1585 IMAPFlags iflags = 0;
1586 guint32 new_uid = 0;
1587 gchar *real_file = NULL;
1588 fileinfo = (MsgFileInfo *)cur->data;
1590 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1593 if (fileinfo->flags) {
1594 if (MSG_IS_MARKED(*fileinfo->flags))
1595 iflags |= IMAP_FLAG_FLAGGED;
1596 if (MSG_IS_REPLIED(*fileinfo->flags))
1597 iflags |= IMAP_FLAG_ANSWERED;
1598 if (MSG_IS_FORWARDED(*fileinfo->flags))
1599 iflags |= IMAP_FLAG_FORWARDED;
1600 if (!MSG_IS_UNREAD(*fileinfo->flags))
1601 iflags |= IMAP_FLAG_SEEN;
1605 if (real_file == NULL)
1606 real_file = g_strdup(fileinfo->file);
1608 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1609 folder_has_parent_of_type(dest, F_OUTBOX) ||
1610 folder_has_parent_of_type(dest, F_DRAFT) ||
1611 folder_has_parent_of_type(dest, F_TRASH))
1612 iflags |= IMAP_FLAG_SEEN;
1614 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1617 if (ok != MAILIMAP_NO_ERROR) {
1618 g_warning("can't append message %s\n", real_file);
1621 statusbar_progress_all(0,0,0);
1622 statusbar_pop_all();
1625 debug_print("appended new message as %d\n", new_uid);
1626 /* put the local file in the imapcache, so that we don't
1627 * have to fetch it back later. */
1630 missing_uids = TRUE;
1631 debug_print("Missing UID (0)\n");
1634 gchar *cache_path = folder_item_get_path(dest);
1635 if (!is_dir_exist(cache_path))
1636 make_dir_hier(cache_path);
1637 if (is_dir_exist(cache_path)) {
1638 gchar *cache_file = g_strconcat(
1639 cache_path, G_DIR_SEPARATOR_S,
1640 itos(new_uid), NULL);
1641 copy_file(real_file, cache_file, TRUE);
1642 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1649 if (relation != NULL)
1650 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1651 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1652 GINT_TO_POINTER(new_uid));
1653 if (last_uid < new_uid) {
1660 statusbar_progress_all(0,0,0);
1661 statusbar_pop_all();
1666 imap_scan_required(folder, dest);
1668 session = imap_session_get(folder);
1674 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1675 &a, NULL, NULL, NULL, NULL, FALSE);
1680 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1682 GSList *result = NULL;
1687 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1688 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1689 start = item->set_first;
1690 end = item->set_last;
1691 for (t = start; t <= end; t++) {
1692 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1695 result = g_slist_reverse(result);
1696 if (debug_get_mode()) {
1697 debug_print("flat imap set: ");
1698 for (cur = result; cur; cur = cur->next) {
1699 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1706 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1707 MsgInfoList *msglist, GRelation *relation)
1711 GSList *seq_list, *cur;
1713 IMAPSession *session;
1714 gint ok = MAILIMAP_NO_ERROR;
1715 GRelation *uid_mapping;
1717 gboolean single = FALSE;
1719 g_return_val_if_fail(folder != NULL, -1);
1720 g_return_val_if_fail(dest != NULL, -1);
1721 g_return_val_if_fail(msglist != NULL, -1);
1723 debug_print("getting session...\n");
1724 session = imap_session_get(folder);
1730 msginfo = (MsgInfo *)msglist->data;
1731 if (msglist->next == NULL)
1733 src = msginfo->folder;
1735 g_warning("the src folder is identical to the dest.\n");
1739 if (src->folder != dest->folder) {
1740 GSList *infolist = NULL, *cur;
1742 for (cur = msglist; cur; cur = cur->next) {
1743 msginfo = (MsgInfo *)cur->data;
1744 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1745 fileinfo->file = procmsg_get_message_file(msginfo);
1746 fileinfo->flags = &(msginfo->flags);
1747 infolist = g_slist_prepend(infolist, fileinfo);
1749 infolist = g_slist_reverse(infolist);
1750 res = folder_item_add_msgs(dest, infolist, FALSE);
1751 for (cur = infolist; cur; cur = cur->next) {
1752 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1756 g_slist_free(infolist);
1760 lock_session(session); /* unlocked later in the function */
1762 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1763 NULL, NULL, NULL, NULL, NULL, FALSE);
1764 if (ok != MAILIMAP_NO_ERROR) {
1768 unlock_session(session);
1770 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1771 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1772 uid_mapping = g_relation_new(2);
1773 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1775 statusbar_print_all(_("Copying messages..."));
1776 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1777 struct mailimap_set * seq_set;
1778 struct mailimap_set * source = NULL;
1779 struct mailimap_set * dest = NULL;
1780 seq_set = cur->data;
1782 debug_print("Copying messages from %s to %s ...\n",
1783 src->path, destdir);
1785 lock_session(session); /* unlocked later in the function */
1786 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1793 if (ok == MAILIMAP_NO_ERROR) {
1794 unlock_session(session);
1795 if (relation && source && dest) {
1796 GSList *s_list = flatten_mailimap_set(source);
1797 GSList *d_list = flatten_mailimap_set(dest);
1798 GSList *s_cur, *d_cur;
1799 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1801 for (s_cur = s_list, d_cur = d_list;
1803 s_cur = s_cur->next, d_cur = d_cur->next) {
1804 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1808 debug_print("hhhmm, source list length != dest list length.\n");
1810 g_slist_free(s_list);
1811 g_slist_free(d_list);
1817 mailimap_set_free(source);
1819 mailimap_set_free(dest);
1821 if (ok != MAILIMAP_NO_ERROR) {
1822 g_relation_destroy(uid_mapping);
1823 imap_lep_set_free(seq_list);
1824 statusbar_pop_all();
1829 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1830 MsgInfo *msginfo = (MsgInfo *)cur->data;
1833 tuples = g_relation_select(uid_mapping,
1834 GINT_TO_POINTER(msginfo->msgnum),
1836 if (tuples->len > 0) {
1837 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1838 g_relation_insert(relation, msginfo,
1839 GINT_TO_POINTER(num));
1842 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1843 /* put the local file in the imapcache, so that we don't
1844 * have to fetch it back later. */
1846 gchar *cache_path = folder_item_get_path(msginfo->folder);
1847 gchar *real_file = g_strconcat(
1848 cache_path, G_DIR_SEPARATOR_S,
1849 itos(msginfo->msgnum), NULL);
1850 gchar *cache_file = NULL;
1852 cache_path = folder_item_get_path(dest);
1853 cache_file = g_strconcat(
1854 cache_path, G_DIR_SEPARATOR_S,
1856 if (!is_dir_exist(cache_path))
1857 make_dir_hier(cache_path);
1858 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1859 copy_file(real_file, cache_file, TRUE);
1860 debug_print("copied to cache: %s\n", cache_file);
1867 g_relation_insert(relation, msginfo,
1868 GINT_TO_POINTER(0));
1869 g_tuples_destroy(tuples);
1871 statusbar_pop_all();
1873 g_relation_destroy(uid_mapping);
1874 imap_lep_set_free(seq_list);
1878 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1879 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1880 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1881 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1883 imap_scan_required(folder, dest);
1884 if (ok == MAILIMAP_NO_ERROR)
1890 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1894 g_return_val_if_fail(msginfo != NULL, -1);
1896 msglist.data = msginfo;
1897 msglist.next = NULL;
1899 return imap_copy_msgs(folder, dest, &msglist, NULL);
1902 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1903 MsgInfoList *msglist, GRelation *relation)
1908 g_return_val_if_fail(folder != NULL, -1);
1909 g_return_val_if_fail(dest != NULL, -1);
1910 g_return_val_if_fail(msglist != NULL, -1);
1912 msginfo = (MsgInfo *)msglist->data;
1913 g_return_val_if_fail(msginfo->folder != NULL, -1);
1915 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1920 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1921 MsgInfoList *msglist, GRelation *relation)
1923 gchar *destdir, *dir;
1924 GSList *numlist = NULL, *cur;
1926 IMAPSession *session;
1927 gint ok = MAILIMAP_NO_ERROR;
1928 GRelation *uid_mapping;
1930 g_return_val_if_fail(folder != NULL, -1);
1931 g_return_val_if_fail(dest != NULL, -1);
1932 g_return_val_if_fail(msglist != NULL, -1);
1934 debug_print("getting session...\n");
1935 session = imap_session_get(folder);
1940 lock_session(session); /* unlocked later in the function */
1942 msginfo = (MsgInfo *)msglist->data;
1944 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1945 NULL, NULL, NULL, NULL, NULL, FALSE);
1946 if (ok != MAILIMAP_NO_ERROR) {
1950 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1951 for (cur = msglist; cur; cur = cur->next) {
1952 msginfo = (MsgInfo *)cur->data;
1953 if (!MSG_IS_DELETED(msginfo->flags))
1954 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1956 numlist = g_slist_reverse(numlist);
1958 uid_mapping = g_relation_new(2);
1959 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1961 if (numlist != NULL) {
1962 ok = imap_set_message_flags
1963 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1964 if (ok != MAILIMAP_NO_ERROR) {
1965 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1968 } /* else we just need to expunge */
1969 ok = imap_cmd_expunge(session);
1970 if (ok != MAILIMAP_NO_ERROR) {
1971 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1975 session->folder_content_changed = TRUE;
1976 unlock_session(session);
1978 dir = folder_item_get_path(msginfo->folder);
1979 if (is_dir_exist(dir)) {
1980 for (cur = msglist; cur; cur = cur->next) {
1981 msginfo = (MsgInfo *)cur->data;
1982 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1987 g_relation_destroy(uid_mapping);
1988 g_slist_free(numlist);
1990 imap_scan_required(folder, dest);
1993 if (ok == MAILIMAP_NO_ERROR)
1999 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2000 MsgInfoList *msglist, GRelation *relation)
2004 g_return_val_if_fail(folder != NULL, -1);
2005 g_return_val_if_fail(dest != NULL, -1);
2006 if (msglist == NULL)
2009 msginfo = (MsgInfo *)msglist->data;
2010 g_return_val_if_fail(msginfo->folder != NULL, -1);
2012 return imap_do_remove_msgs(folder, dest, msglist, relation);
2015 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2017 GSList *list = folder_item_get_msg_list(item);
2018 gint res = imap_remove_msgs(folder, item, list, NULL);
2019 procmsg_msg_list_free(list);
2023 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2026 /* TODO: properly implement this method */
2030 static gint imap_close(Folder *folder, FolderItem *item)
2035 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2037 FolderItem *item = NULL;
2038 IMAPSession *session;
2039 gchar *root_folder = NULL;
2041 g_return_val_if_fail(folder != NULL, -1);
2042 g_return_val_if_fail(folder->account != NULL, -1);
2044 debug_print("getting session...\n");
2045 session = imap_session_get(folder);
2047 if (!folder->node) {
2048 folder_tree_destroy(folder);
2049 item = folder_item_new(folder, folder->name, NULL);
2050 item->folder = folder;
2051 folder->node = item->node = g_node_new(item);
2056 if (folder->account->imap_dir && *folder->account->imap_dir) {
2061 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2062 extract_quote(root_folder, '"');
2063 subst_char(root_folder,
2064 imap_get_path_separator(session, IMAP_FOLDER(folder),
2067 strtailchomp(root_folder, '/');
2068 real_path = imap_get_real_path
2069 (session, IMAP_FOLDER(folder), root_folder);
2070 debug_print("IMAP root directory: %s\n", real_path);
2072 /* check if root directory exist */
2074 r = imap_threaded_list(session->folder, "", real_path,
2077 if (r != MAILIMAP_NO_ERROR)
2078 imap_handle_error(SESSION(session), r);
2080 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2081 if (!folder->node) {
2082 item = folder_item_new(folder, folder->name, NULL);
2083 item->folder = folder;
2084 folder->node = item->node = g_node_new(item);
2088 mailimap_list_result_free(lep_list);
2094 item = FOLDER_ITEM(folder->node->data);
2096 if (item && !item->path && root_folder) {
2097 item->path = g_strdup(root_folder);
2100 if (!item || ((item->path || root_folder) &&
2101 strcmp2(item->path, root_folder) != 0)) {
2102 folder_tree_destroy(folder);
2103 item = folder_item_new(folder, folder->name, root_folder);
2104 item->folder = folder;
2105 folder->node = item->node = g_node_new(item);
2108 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2109 imap_create_missing_folders(folder);
2114 static gint imap_scan_tree(Folder *folder)
2116 gboolean subs_only = FALSE;
2117 if (folder->account) {
2118 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2119 subs_only = folder->account->imap_subsonly;
2121 return imap_scan_tree_real(folder, subs_only);
2124 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2127 IMAPFolder *imapfolder;
2128 FolderItem *new_item;
2129 GSList *item_list, *cur;
2132 gchar *wildcard_path;
2138 g_return_val_if_fail(item != NULL, -1);
2139 g_return_val_if_fail(item->folder != NULL, -1);
2140 g_return_val_if_fail(item->no_sub == FALSE, -1);
2142 folder = item->folder;
2143 imapfolder = IMAP_FOLDER(folder);
2145 separator = imap_get_path_separator(session, imapfolder, item->path);
2147 if (folder->ui_func)
2148 folder->ui_func(folder, item, folder->ui_func_data);
2151 wildcard[0] = separator;
2154 real_path = imap_get_real_path(session, imapfolder, item->path);
2158 real_path = g_strdup("");
2161 Xstrcat_a(wildcard_path, real_path, wildcard,
2162 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2166 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2168 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2170 if (r != MAILIMAP_NO_ERROR) {
2171 imap_handle_error(SESSION(session), r);
2175 item_list = imap_list_from_lep(imapfolder,
2176 lep_list, real_path, FALSE);
2177 mailimap_list_result_free(lep_list);
2182 node = item->node->children;
2183 while (node != NULL) {
2184 FolderItem *old_item = FOLDER_ITEM(node->data);
2185 GNode *next = node->next;
2188 for (cur = item_list; cur != NULL; cur = cur->next) {
2189 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2190 if (!strcmp2(old_item->path, cur_item->path)) {
2191 new_item = cur_item;
2196 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2197 debug_print("not removing INBOX\n");
2199 debug_print("folder '%s' not found. removing...\n",
2201 folder_item_remove(old_item);
2204 old_item->no_sub = new_item->no_sub;
2205 old_item->no_select = new_item->no_select;
2206 if (old_item->no_sub == TRUE && node->children) {
2207 debug_print("folder '%s' doesn't have "
2208 "subfolders. removing...\n",
2210 folder_item_remove_children(old_item);
2217 for (cur = item_list; cur != NULL; cur = cur->next) {
2218 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2221 for (node = item->node->children; node != NULL;
2222 node = node->next) {
2223 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2225 new_item = FOLDER_ITEM(node->data);
2226 folder_item_destroy(cur_item);
2232 new_item = cur_item;
2233 debug_print("new folder '%s' found.\n", new_item->path);
2234 folder_item_append(item, new_item);
2237 if (!strcmp(new_item->path, "INBOX")) {
2238 new_item->stype = F_INBOX;
2239 folder->inbox = new_item;
2240 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2243 base = g_path_get_basename(new_item->path);
2245 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2246 new_item->stype = F_OUTBOX;
2247 folder->outbox = new_item;
2248 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2249 new_item->stype = F_DRAFT;
2250 folder->draft = new_item;
2251 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2252 new_item->stype = F_QUEUE;
2253 folder->queue = new_item;
2254 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2255 new_item->stype = F_TRASH;
2256 folder->trash = new_item;
2261 if (new_item->no_sub == FALSE)
2262 imap_scan_tree_recursive(session, new_item, subs_only);
2265 g_slist_free(item_list);
2267 return MAILIMAP_NO_ERROR;
2270 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2272 IMAPSession *session = imap_session_get(folder);
2274 gchar *wildcard_path;
2278 GSList *item_list = NULL, *cur;
2279 GList *child_list = NULL, *tmplist = NULL;
2280 GSList *sub_list = NULL;
2286 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2289 wildcard[0] = separator;
2292 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2296 real_path = g_strdup("");
2299 Xstrcat_a(wildcard_path, real_path, wildcard,
2300 {g_free(real_path); return NULL;});
2304 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2305 item->path?item->path:item->name);
2307 statusbar_print_all(_("Looking for subfolders of %s..."),
2308 item->path?item->path:item->name);
2310 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2312 statusbar_pop_all();
2315 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2316 lep_list, real_path, FALSE);
2317 mailimap_list_result_free(lep_list);
2319 for (cur = item_list; cur != NULL; cur = cur->next) {
2320 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2322 tmplist = imap_scan_subtree(folder, cur_item,
2323 unsubs_only, recursive);
2325 child_list = g_list_concat(child_list, tmplist);
2327 child_list = g_list_prepend(child_list,
2328 imap_get_real_path(session,
2329 IMAP_FOLDER(folder), cur_item->path));
2331 folder_item_destroy(cur_item);
2333 child_list = g_list_reverse(child_list);
2334 g_slist_free(item_list);
2337 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2339 statusbar_pop_all();
2342 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2343 lep_list, real_path, FALSE);
2344 mailimap_list_result_free(lep_list);
2346 for (cur = sub_list; cur != NULL; cur = cur->next) {
2347 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2348 GList *oldlitem = NULL;
2349 gchar *tmp = imap_get_real_path(session,
2350 IMAP_FOLDER(folder), cur_item->path);
2351 folder_item_destroy(cur_item);
2352 oldlitem = g_list_find_custom(
2353 child_list, tmp, (GCompareFunc)strcmp2);
2355 child_list = g_list_remove_link(child_list, oldlitem);
2356 g_free(oldlitem->data);
2357 g_list_free(oldlitem);
2363 statusbar_pop_all();
2368 static gint imap_create_tree(Folder *folder)
2370 g_return_val_if_fail(folder != NULL, -1);
2371 g_return_val_if_fail(folder->node != NULL, -1);
2372 g_return_val_if_fail(folder->node->data != NULL, -1);
2373 g_return_val_if_fail(folder->account != NULL, -1);
2375 imap_scan_tree(folder);
2376 imap_create_missing_folders(folder);
2381 static void imap_create_missing_folders(Folder *folder)
2383 g_return_if_fail(folder != NULL);
2386 folder->inbox = imap_create_special_folder
2387 (folder, F_INBOX, "INBOX");
2389 folder->trash = imap_create_special_folder
2390 (folder, F_TRASH, "Trash");
2392 folder->queue = imap_create_special_folder
2393 (folder, F_QUEUE, "Queue");
2394 if (!folder->outbox)
2395 folder->outbox = imap_create_special_folder
2396 (folder, F_OUTBOX, "Sent");
2398 folder->draft = imap_create_special_folder
2399 (folder, F_DRAFT, "Drafts");
2402 static FolderItem *imap_create_special_folder(Folder *folder,
2403 SpecialFolderItemType stype,
2407 FolderItem *new_item;
2409 g_return_val_if_fail(folder != NULL, NULL);
2410 g_return_val_if_fail(folder->node != NULL, NULL);
2411 g_return_val_if_fail(folder->node->data != NULL, NULL);
2412 g_return_val_if_fail(folder->account != NULL, NULL);
2413 g_return_val_if_fail(name != NULL, NULL);
2415 item = FOLDER_ITEM(folder->node->data);
2416 new_item = imap_create_folder(folder, item, name);
2419 g_warning("Can't create '%s'\n", name);
2420 if (!folder->inbox) return NULL;
2422 new_item = imap_create_folder(folder, folder->inbox, name);
2424 g_warning("Can't create '%s' under INBOX\n", name);
2426 new_item->stype = stype;
2428 new_item->stype = stype;
2433 static gchar *imap_folder_get_path(Folder *folder)
2437 g_return_val_if_fail(folder != NULL, NULL);
2438 g_return_val_if_fail(folder->account != NULL, NULL);
2440 folder_path = g_strconcat(get_imap_cache_dir(),
2442 folder->account->recv_server,
2444 folder->account->userid,
2450 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2452 gchar *folder_path, *path;
2454 g_return_val_if_fail(folder != NULL, NULL);
2455 g_return_val_if_fail(item != NULL, NULL);
2456 folder_path = imap_folder_get_path(folder);
2458 g_return_val_if_fail(folder_path != NULL, NULL);
2459 if (folder_path[0] == G_DIR_SEPARATOR) {
2461 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2464 path = g_strdup(folder_path);
2467 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2468 folder_path, G_DIR_SEPARATOR_S,
2471 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2474 g_free(folder_path);
2479 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2482 gchar *dirpath, *imap_path;
2483 IMAPSession *session;
2484 FolderItem *new_item;
2489 gboolean no_select = FALSE, no_sub = FALSE;
2490 gboolean exist = FALSE;
2492 g_return_val_if_fail(folder != NULL, NULL);
2493 g_return_val_if_fail(folder->account != NULL, NULL);
2494 g_return_val_if_fail(parent != NULL, NULL);
2495 g_return_val_if_fail(name != NULL, NULL);
2497 debug_print("getting session...\n");
2498 session = imap_session_get(folder);
2503 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2504 dirpath = g_strdup(name);
2505 }else if (parent->path)
2506 dirpath = g_strconcat(parent->path, "/", name, NULL);
2507 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2508 dirpath = g_strdup(name);
2509 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2512 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2513 strtailchomp(imap_dir, '/');
2514 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2516 dirpath = g_strdup(name);
2520 /* keep trailing directory separator to create a folder that contains
2522 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2524 strtailchomp(dirpath, '/');
2525 Xstrdup_a(new_name, name, {
2529 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2530 imap_path_separator_subst(imap_path, separator);
2531 /* remove trailing / for display */
2532 strtailchomp(new_name, '/');
2534 if (strcmp(dirpath, "INBOX") != 0) {
2539 argbuf = g_ptr_array_new();
2540 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2541 if (r != MAILIMAP_NO_ERROR) {
2542 imap_handle_error(SESSION(session), r);
2543 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2546 ptr_array_free_strings(argbuf);
2547 g_ptr_array_free(argbuf, TRUE);
2551 if (clist_count(lep_list) > 0)
2553 mailimap_list_result_free(lep_list);
2556 ok = imap_cmd_create(session, imap_path);
2557 if (ok != MAILIMAP_NO_ERROR) {
2558 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2563 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2564 if (r == MAILIMAP_NO_ERROR) {
2565 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2566 lep_list, dirpath, TRUE);
2568 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2569 no_select = cur_item->no_select;
2570 no_sub = cur_item->no_sub;
2571 g_slist_free(item_list);
2573 mailimap_list_result_free(lep_list);
2575 imap_handle_error(SESSION(session), r);
2578 imap_threaded_subscribe(folder, imap_path, TRUE);
2582 /* just get flags */
2583 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2584 if (r == MAILIMAP_NO_ERROR) {
2585 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2586 lep_list, dirpath, TRUE);
2588 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2589 no_select = cur_item->no_select;
2590 no_sub = cur_item->no_sub;
2591 g_slist_free(item_list);
2593 mailimap_list_result_free(lep_list);
2595 imap_handle_error(SESSION(session), r);
2599 new_item = folder_item_new(folder, new_name, dirpath);
2600 new_item->no_select = no_select;
2601 new_item->no_sub = no_sub;
2602 folder_item_append(parent, new_item);
2606 dirpath = folder_item_get_path(new_item);
2607 if (!is_dir_exist(dirpath))
2608 make_dir_hier(dirpath);
2612 /* folder existed, scan it */
2613 imap_scan_required(folder, new_item);
2614 folder_item_scan_full(new_item, FALSE);
2620 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2625 gchar *real_oldpath;
2626 gchar *real_newpath;
2628 gchar *old_cache_dir;
2629 gchar *new_cache_dir;
2630 IMAPSession *session;
2633 gint exists, recent, unseen;
2634 guint32 uid_validity;
2636 g_return_val_if_fail(folder != NULL, -1);
2637 g_return_val_if_fail(item != NULL, -1);
2638 g_return_val_if_fail(item->path != NULL, -1);
2639 g_return_val_if_fail(name != NULL, -1);
2641 debug_print("getting session...\n");
2642 session = imap_session_get(folder);
2647 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2648 g_warning(_("New folder name must not contain the namespace "
2653 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2655 g_free(session->mbox);
2656 session->mbox = NULL;
2657 session->exists = 0;
2658 session->recent = 0;
2659 session->expunge = 0;
2660 ok = imap_cmd_examine(session, "INBOX",
2661 &exists, &recent, &unseen, &uid_validity, FALSE);
2662 if (ok != MAILIMAP_NO_ERROR) {
2663 g_free(real_oldpath);
2667 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2668 if (strchr(item->path, G_DIR_SEPARATOR)) {
2669 dirpath = g_path_get_dirname(item->path);
2670 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2673 newpath = g_strdup(name);
2675 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2676 imap_path_separator_subst(real_newpath, separator);
2678 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2679 if (ok != MAILIMAP_NO_ERROR) {
2680 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2681 real_oldpath, real_newpath);
2682 g_free(real_oldpath);
2684 g_free(real_newpath);
2688 item->name = g_strdup(name);
2690 old_cache_dir = folder_item_get_path(item);
2692 paths[0] = g_strdup(item->path);
2694 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2695 imap_rename_folder_func, paths);
2697 if (is_dir_exist(old_cache_dir)) {
2698 new_cache_dir = folder_item_get_path(item);
2699 if (rename(old_cache_dir, new_cache_dir) < 0) {
2700 FILE_OP_ERROR(old_cache_dir, "rename");
2702 g_free(new_cache_dir);
2705 g_free(old_cache_dir);
2708 g_free(real_oldpath);
2709 g_free(real_newpath);
2713 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2717 IMAPSession *session;
2718 debug_print("getting session...\n");
2720 session = imap_session_get(folder);
2724 if (item && item->path) {
2725 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2728 if (!strcmp(path, "INBOX") && sub == FALSE)
2730 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2731 r = imap_threaded_subscribe(folder, path, sub);
2734 r = imap_threaded_subscribe(folder, rpath, sub);
2740 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2743 IMAPSession *session;
2746 gboolean selected_folder;
2748 g_return_val_if_fail(folder != NULL, -1);
2749 g_return_val_if_fail(item != NULL, -1);
2750 g_return_val_if_fail(item->path != NULL, -1);
2752 debug_print("getting session...\n");
2753 session = imap_session_get(folder);
2757 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2759 imap_threaded_subscribe(folder, path, FALSE);
2761 selected_folder = (session->mbox != NULL) &&
2762 (!strcmp(session->mbox, item->path));
2763 if (selected_folder) {
2764 ok = imap_cmd_close(session);
2765 if (ok != MAILIMAP_NO_ERROR) {
2766 debug_print("close err %d\n", ok);
2767 imap_handle_error(SESSION(session), ok);
2771 ok = imap_cmd_delete(session, path);
2772 if (ok != MAILIMAP_NO_ERROR) {
2773 gchar *tmp = g_strdup_printf("%s%c", path,
2774 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2777 ok = imap_cmd_delete(session, path);
2780 if (ok != MAILIMAP_NO_ERROR) {
2781 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2787 cache_dir = folder_item_get_path(item);
2788 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2789 g_warning("can't remove directory '%s'\n", cache_dir);
2791 folder_item_remove(item);
2795 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2799 g_return_val_if_fail(item != NULL, -1);
2800 g_return_val_if_fail(item->folder != NULL, -1);
2801 g_return_val_if_fail(item->node != NULL, -1);
2803 node = item->node->children;
2804 while (node != NULL) {
2806 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2810 debug_print("IMAP removing %s\n", item->path);
2812 if (imap_remove_all_msg(folder, item) < 0)
2814 return imap_remove_folder_real(folder, item);
2817 typedef struct _uncached_data {
2818 IMAPSession *session;
2820 MsgNumberList *numlist;
2827 static void *imap_get_uncached_messages_thread(void *data)
2829 uncached_data *stuff = (uncached_data *)data;
2830 IMAPSession *session = stuff->session;
2831 FolderItem *item = stuff->item;
2832 MsgNumberList *numlist = stuff->numlist;
2833 GSList *newlist = NULL;
2834 GSList *llast = NULL;
2835 GSList *seq_list, *cur;
2836 gboolean got_alien_tags = FALSE;
2838 debug_print("uncached_messages\n");
2840 if (session == NULL || item == NULL || item->folder == NULL
2841 || FOLDER_CLASS(item->folder) != &imap_class) {
2846 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2847 debug_print("get msgs info\n");
2848 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2849 struct mailimap_set * imapset;
2855 if (session->cancelled)
2858 imapset = cur->data;
2860 r = imap_threaded_fetch_env(session->folder,
2861 imapset, &env_list);
2862 if (r != MAILIMAP_NO_ERROR) {
2863 imap_handle_error(SESSION(session), r);
2871 session_set_access_time(SESSION(session));
2874 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2875 struct imap_fetch_env_info * info;
2877 GSList *tags = NULL, *cur = NULL;
2878 info = carray_get(env_list, i);
2879 tags = carray_get(env_list, i+1);
2880 msginfo = imap_envelope_from_lep(info, item);
2881 if (msginfo == NULL) {
2882 slist_free_strings(tags);
2887 g_slist_free(msginfo->tags);
2888 msginfo->tags = NULL;
2890 for (cur = tags; cur; cur = cur->next) {
2891 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2893 id = tags_get_id_for_str(real_tag);
2894 printf("tag %s %d\n", real_tag, id);
2896 id = tags_add_tag(real_tag);
2897 got_alien_tags = TRUE;
2899 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2900 msginfo->tags = g_slist_append(
2902 GINT_TO_POINTER(id));
2906 slist_free_strings(tags);
2908 msginfo->folder = item;
2910 llast = newlist = g_slist_append(newlist, msginfo);
2912 llast = g_slist_append(llast, msginfo);
2913 llast = llast->next;
2918 imap_fetch_env_free(env_list);
2921 if (got_alien_tags) {
2923 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2926 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2927 struct mailimap_set * imapset;
2929 imapset = cur->data;
2930 mailimap_set_free(imapset);
2933 session_set_access_time(SESSION(session));
2938 #define MAX_MSG_NUM 50
2940 static GSList *imap_get_uncached_messages(IMAPSession *session,
2942 MsgNumberList *numlist,
2945 GSList *result = NULL;
2947 uncached_data *data = g_new0(uncached_data, 1);
2952 data->total = g_slist_length(numlist);
2953 data->ok = MAILIMAP_NO_ERROR;
2954 debug_print("messages list : %i\n", data->total);
2956 while (cur != NULL) {
2957 GSList * partial_result;
2965 while (count < MAX_MSG_NUM) {
2970 if (newlist == NULL)
2971 llast = newlist = g_slist_append(newlist, p);
2973 llast = g_slist_append(llast, p);
2974 llast = llast->next;
2984 data->session = session;
2986 data->numlist = newlist;
2989 if (prefs_common.work_offline &&
2990 !inc_offline_should_override(FALSE,
2991 _("Claws Mail needs network access in order "
2992 "to access the IMAP server."))) {
2998 (GSList *)imap_get_uncached_messages_thread(data);
3000 if (data->ok != MAILIMAP_NO_ERROR) {
3003 statusbar_progress_all(data->cur,data->total, 1);
3005 g_slist_free(newlist);
3007 result = g_slist_concat(result, partial_result);
3012 statusbar_progress_all(0,0,0);
3013 statusbar_pop_all();
3018 static void imap_delete_all_cached_messages(FolderItem *item)
3022 g_return_if_fail(item != NULL);
3023 g_return_if_fail(item->folder != NULL);
3024 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3026 debug_print("Deleting all cached messages...\n");
3028 dir = folder_item_get_path(item);
3029 if (is_dir_exist(dir))
3030 remove_all_numbered_files(dir);
3033 debug_print("done.\n");
3036 gchar imap_get_path_separator_for_item(FolderItem *item)
3038 Folder *folder = NULL;
3039 IMAPFolder *imap_folder = NULL;
3040 IMAPSession *session = NULL;
3045 folder = item->folder;
3050 imap_folder = IMAP_FOLDER(folder);
3055 debug_print("getting session...");
3056 session = imap_session_get(FOLDER(folder));
3057 result = imap_get_path_separator(session, imap_folder, item->path);
3061 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
3065 gchar separator = '\0';
3067 g_return_val_if_fail(session != NULL, '/');
3068 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3070 if (r != MAILIMAP_NO_ERROR) {
3071 imap_handle_error(SESSION(session), r);
3072 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3076 if (clist_count(lep_list) > 0) {
3077 clistiter * iter = clist_begin(lep_list);
3078 struct mailimap_mailbox_list * mb;
3079 mb = clist_content(iter);
3081 separator = mb->mb_delimiter;
3082 debug_print("got separator: %c\n", folder->last_seen_separator);
3084 mailimap_list_result_free(lep_list);
3088 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3090 gchar separator = '/';
3092 if (folder->last_seen_separator == 0) {
3093 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
3096 if (folder->last_seen_separator == 0) {
3097 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
3100 if (folder->last_seen_separator != 0) {
3101 debug_print("using separator: %c\n", folder->last_seen_separator);
3102 return folder->last_seen_separator;
3108 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3113 g_return_val_if_fail(folder != NULL, NULL);
3114 g_return_val_if_fail(path != NULL, NULL);
3116 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3117 separator = imap_get_path_separator(session, folder, path);
3118 imap_path_separator_subst(real_path, separator);
3123 static gint imap_set_message_flags(IMAPSession *session,
3124 IMAPFolderItem *item,
3125 MsgNumberList *numlist,
3134 IMAPFolder *folder = NULL;
3135 GSList *sorted_list = NULL;
3137 if (numlist == NULL || session == NULL)
3138 return MAILIMAP_ERROR_BAD_STATE;
3140 folder = IMAP_FOLDER(session->folder);
3142 sorted_list = g_slist_copy(numlist);
3143 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3145 cur = g_slist_last(sorted_list);
3148 total = GPOINTER_TO_INT(cur->data);
3150 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3152 statusbar_print_all(_("Flagging messages..."));
3154 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3155 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3156 struct mailimap_set_item *set_item = clist_content(
3157 clist_begin(imapset->set_list));
3159 statusbar_progress_all(set_item->set_first, total, 1);
3161 ok = imap_cmd_store(session, item, imapset,
3162 flags, tags, is_set);
3163 statusbar_progress_all(set_item->set_last, total, 1);
3164 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3165 /* reduce max set size */
3166 folder->max_set_size /= 2;
3168 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3173 g_slist_free(sorted_list);
3175 statusbar_progress_all(0,0,0);
3176 statusbar_pop_all();
3178 imap_lep_set_free(seq_list);
3183 typedef struct _select_data {
3184 IMAPSession *session;
3189 guint32 *uid_validity;
3193 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3195 gint *exists, gint *recent, gint *unseen,
3196 guint32 *uid_validity, gint *can_create_flags,
3201 gint exists_, recent_, unseen_;
3202 guint32 uid_validity_;
3203 gint can_create_flags_;
3204 const gchar *path = item ? item->path:NULL;
3207 return MAILIMAP_ERROR_BAD_STATE;
3210 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3211 if (session->mbox && strcmp(session->mbox, path) == 0)
3212 return MAILIMAP_NO_ERROR;
3214 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3215 if (session->mbox && strcmp(session->mbox, path) == 0) {
3216 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3217 return MAILIMAP_NO_ERROR;
3227 uid_validity = &uid_validity_;
3228 if (!can_create_flags)
3229 can_create_flags = &can_create_flags_;
3231 g_free(session->mbox);
3232 session->mbox = NULL;
3233 session->exists = 0;
3234 session->recent = 0;
3235 session->expunge = 0;
3237 real_path = imap_get_real_path(session, folder, path);
3239 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3240 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3241 ok = imap_cmd_select(session, real_path,
3242 exists, recent, unseen, uid_validity, can_create_flags,
3243 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3244 if (ok != MAILIMAP_NO_ERROR) {
3245 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3247 session->mbox = g_strdup(path);
3248 session->folder_content_changed = FALSE;
3249 session->exists = *exists;
3250 session->recent = *recent;
3251 session->expunge = 0;
3252 session->unseen = *unseen;
3253 session->uid_validity = *uid_validity;
3254 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3255 session->exists, session->recent, session->expunge,
3256 session->uid_validity, *can_create_flags);
3258 if (*can_create_flags) {
3259 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3261 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3268 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3269 const gchar *path, IMAPFolderItem *item,
3271 guint32 *uid_next, guint32 *uid_validity,
3272 gint *unseen, gboolean block)
3276 struct mailimap_mailbox_data_status * data_status;
3281 real_path = imap_get_real_path(session, folder, path);
3300 if (session->mbox != NULL &&
3301 !strcmp(session->mbox, item->item.path)) {
3302 r = imap_cmd_close(session);
3303 if (r != MAILIMAP_NO_ERROR) {
3304 imap_handle_error(SESSION(session), r);
3305 debug_print("close err %d\n", r);
3310 r = imap_threaded_status(FOLDER(folder), real_path,
3311 &data_status, mask);
3314 if (r != MAILIMAP_NO_ERROR) {
3315 imap_handle_error(SESSION(session), r);
3316 debug_print("status err %d\n", r);
3320 if (data_status->st_info_list == NULL) {
3321 mailimap_mailbox_data_status_free(data_status);
3322 debug_print("status->st_info_list == NULL\n");
3323 return MAILIMAP_ERROR_BAD_STATE;
3327 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3328 iter = clist_next(iter)) {
3329 struct mailimap_status_info * info;
3331 info = clist_content(iter);
3332 switch (info->st_att) {
3333 case MAILIMAP_STATUS_ATT_MESSAGES:
3335 * messages = info->st_value;
3336 got_values |= 1 << 0;
3340 case MAILIMAP_STATUS_ATT_UIDNEXT:
3342 * uid_next = info->st_value;
3343 got_values |= 1 << 2;
3347 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3349 * uid_validity = info->st_value;
3350 got_values |= 1 << 3;
3354 case MAILIMAP_STATUS_ATT_UNSEEN:
3356 * unseen = info->st_value;
3357 got_values |= 1 << 4;
3362 mailimap_mailbox_data_status_free(data_status);
3364 if (got_values != mask) {
3365 g_warning("status: incomplete values received (%d)\n", got_values);
3367 return MAILIMAP_NO_ERROR;
3370 static void imap_free_capabilities(IMAPSession *session)
3372 slist_free_strings(session->capability);
3373 g_slist_free(session->capability);
3374 session->capability = NULL;
3377 /* low-level IMAP4rev1 commands */
3379 static gint imap_cmd_login(IMAPSession *session,
3380 const gchar *user, const gchar *pass,
3386 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3387 gint ok = MAILIMAP_ERROR_BAD_STATE;
3388 if (imap_has_capability(session, "STARTTLS")) {
3389 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3390 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3391 ok = imap_cmd_starttls(session);
3392 if (ok != MAILIMAP_NO_ERROR) {
3393 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3397 imap_free_capabilities(session);
3398 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3399 imap_handle_error(SESSION(session), r);
3400 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3405 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3406 "server requires TLS, but Claws Mail "
3407 "has been compiled without OpenSSL "
3409 SESSION(session)->server);
3410 return MAILIMAP_ERROR_BAD_STATE;
3413 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3414 return MAILIMAP_ERROR_BAD_STATE;
3418 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3420 SESSION(session)->server,
3422 r = imap_threaded_login(session->folder, user, pass, type);
3423 if (r != MAILIMAP_NO_ERROR) {
3424 imap_handle_error(SESSION(session), r);
3425 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3426 SESSION(session)->server);
3429 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3430 SESSION(session)->server);
3431 ok = MAILIMAP_NO_ERROR;
3436 static gint imap_cmd_noop(IMAPSession *session)
3439 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3441 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3442 if (r != MAILIMAP_NO_ERROR) {
3443 imap_handle_error(SESSION(session), r);
3444 debug_print("noop err %d\n", r);
3448 session->folder_content_changed = FALSE;
3450 if ((exists && exists != session->exists)
3451 || (recent && recent != session->recent)
3452 || (expunge && expunge != session->expunge)
3453 || (unseen && unseen != session->unseen)) {
3454 session->folder_content_changed = TRUE;
3456 if (uidnext != 0 && uidnext != session->uid_next) {
3457 session->uid_next = uidnext;
3458 session->folder_content_changed = TRUE;
3460 if (uidval != 0 && uidval != session->uid_validity) {
3461 session->uid_validity = uidval;
3462 session->folder_content_changed = TRUE;
3465 session->exists = exists;
3466 session->recent = recent;
3467 session->expunge = expunge;
3468 session->unseen = unseen;
3470 session_set_access_time(SESSION(session));
3472 return MAILIMAP_NO_ERROR;
3475 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3476 static gint imap_cmd_starttls(IMAPSession *session)
3480 r = imap_threaded_starttls(session->folder,
3481 SESSION(session)->server, SESSION(session)->port);
3482 if (r != MAILIMAP_NO_ERROR) {
3483 imap_handle_error(SESSION(session), r);
3484 debug_print("starttls err %d\n", r);
3487 return MAILIMAP_NO_ERROR;
3491 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3492 gint *exists, gint *recent, gint *unseen,
3493 guint32 *uid_validity, gint *can_create_flags,
3494 GSList **ok_flags, gboolean block)
3498 r = imap_threaded_select(session->folder, folder,
3499 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3500 if (r != MAILIMAP_NO_ERROR) {
3501 imap_handle_error(SESSION(session), r);
3502 debug_print("select err %d\n", r);
3505 return MAILIMAP_NO_ERROR;
3508 static gint imap_cmd_close(IMAPSession *session)
3512 r = imap_threaded_close(session->folder);
3513 if (r != MAILIMAP_NO_ERROR) {
3514 imap_handle_error(SESSION(session), r);
3515 debug_print("close err %d\n", r);
3518 g_free(session->mbox);
3519 session->mbox = NULL;
3520 session->exists = 0;
3521 session->recent = 0;
3522 session->expunge = 0;
3523 return MAILIMAP_NO_ERROR;
3526 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3527 gint *exists, gint *recent, gint *unseen,
3528 guint32 *uid_validity, gboolean block)
3532 r = imap_threaded_examine(session->folder, folder,
3533 exists, recent, unseen, uid_validity);
3534 if (r != MAILIMAP_NO_ERROR) {
3535 imap_handle_error(SESSION(session), r);
3536 debug_print("examine err %d\n", r);
3540 return MAILIMAP_NO_ERROR;
3543 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3547 r = imap_threaded_create(session->folder, folder);
3548 if (r != MAILIMAP_NO_ERROR) {
3549 imap_handle_error(SESSION(session), r);
3554 return MAILIMAP_NO_ERROR;
3557 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3558 const gchar *new_folder)
3562 r = imap_threaded_rename(session->folder, old_folder,
3564 if (r != MAILIMAP_NO_ERROR) {
3565 imap_handle_error(SESSION(session), r);
3569 return MAILIMAP_NO_ERROR;
3572 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3577 r = imap_threaded_delete(session->folder, folder);
3578 if (r != MAILIMAP_NO_ERROR) {
3579 imap_handle_error(SESSION(session), r);
3583 return MAILIMAP_NO_ERROR;
3586 typedef struct _fetch_data {
3587 IMAPSession *session;
3589 const gchar *filename;
3595 static void *imap_cmd_fetch_thread(void *data)
3597 fetch_data *stuff = (fetch_data *)data;
3598 IMAPSession *session = stuff->session;
3599 guint32 uid = stuff->uid;
3600 const gchar *filename = stuff->filename;
3604 r = imap_threaded_fetch_content(session->folder,
3608 r = imap_threaded_fetch_content(session->folder,
3611 if (r != MAILIMAP_NO_ERROR) {
3612 imap_handle_error(SESSION(session), r);
3613 debug_print("fetch err %d\n", r);
3614 return GINT_TO_POINTER(MAILIMAP_ERROR_BAD_STATE);
3616 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3619 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3620 const gchar *filename, gboolean headers,
3623 fetch_data *data = g_new0(fetch_data, 1);
3626 data->session = session;
3628 data->filename = filename;
3629 data->headers = headers;
3632 if (prefs_common.work_offline &&
3633 !inc_offline_should_override(FALSE,
3634 _("Claws Mail needs network access in order "
3635 "to access the IMAP server."))) {
3639 statusbar_print_all(_("Fetching message..."));
3640 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3641 statusbar_pop_all();
3647 static gint imap_cmd_append(IMAPSession *session,
3648 IMAPFolderItem *item,
3649 const gchar *destfolder,
3650 const gchar *file, IMAPFlags flags,
3653 struct mailimap_flag_list * flag_list;
3656 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3658 flag_list = imap_flag_to_lep(item, flags, NULL);
3659 lock_session(session);
3660 r = imap_threaded_append(session->folder, destfolder,
3661 file, flag_list, (int *)new_uid);
3662 mailimap_flag_list_free(flag_list);
3664 if (r != MAILIMAP_NO_ERROR) {
3665 imap_handle_error(SESSION(session), r);
3666 debug_print("append err %d\n", r);
3670 unlock_session(session);
3672 return MAILIMAP_NO_ERROR;
3675 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3676 const gchar *destfolder, GRelation *uid_mapping,
3677 struct mailimap_set **source, struct mailimap_set **dest)
3681 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3682 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3683 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3685 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3686 if (r != MAILIMAP_NO_ERROR) {
3687 imap_handle_error(SESSION(session), r);
3691 return MAILIMAP_NO_ERROR;
3694 static gint imap_cmd_store(IMAPSession *session,
3695 IMAPFolderItem *item,
3696 struct mailimap_set * set,
3697 IMAPFlags flags, GSList *tags, int do_add)
3700 struct mailimap_flag_list * flag_list = NULL;
3701 struct mailimap_store_att_flags * store_att_flags;
3703 flag_list = imap_flag_to_lep(item, flags, tags);
3707 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3710 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3712 r = imap_threaded_store(session->folder, set, store_att_flags);
3713 mailimap_store_att_flags_free(store_att_flags);
3714 if (r != MAILIMAP_NO_ERROR) {
3715 imap_handle_error(SESSION(session), r);
3719 return MAILIMAP_NO_ERROR;
3722 static gint imap_cmd_expunge(IMAPSession *session)
3726 if (prefs_common.work_offline &&
3727 !inc_offline_should_override(FALSE,
3728 _("Claws Mail needs network access in order "
3729 "to access the IMAP server."))) {
3733 r = imap_threaded_expunge(session->folder);
3734 if (r != MAILIMAP_NO_ERROR) {
3735 imap_handle_error(SESSION(session), r);
3739 return MAILIMAP_NO_ERROR;
3742 static void imap_path_separator_subst(gchar *str, gchar separator)
3745 gboolean in_escape = FALSE;
3747 if (!separator || separator == '/') return;
3749 for (p = str; *p != '\0'; p++) {
3750 if (*p == '/' && !in_escape)
3752 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3754 else if (*p == '-' && in_escape)
3759 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3761 FolderItem *item = node->data;
3762 gchar **paths = data;
3763 const gchar *oldpath = paths[0];
3764 const gchar *newpath = paths[1];
3765 gchar *real_oldpath, *real_newpath;
3767 gchar *new_itempath;
3769 IMAPSession *session = imap_session_get(item->folder);
3771 oldpathlen = strlen(oldpath);
3772 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3773 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3777 base = item->path + oldpathlen;
3778 while (*base == G_DIR_SEPARATOR) base++;
3780 new_itempath = g_strdup(newpath);
3782 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3785 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3787 item->path = new_itempath;
3789 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3791 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3792 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3794 g_free(real_oldpath);
3795 g_free(real_newpath);
3799 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3801 GSList *uidlist, *elem;
3803 clist * lep_uidlist;
3804 gint ok, nummsgs = 0, lastuid_old;
3806 if (session == NULL) {
3810 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3811 NULL, NULL, NULL, NULL, NULL, TRUE);
3812 if (ok != MAILIMAP_NO_ERROR) {
3816 g_slist_free(item->uid_list);
3817 item->uid_list = NULL;
3821 if (folder->account && folder->account->low_bandwidth) {
3822 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3826 if (r == MAILIMAP_NO_ERROR) {
3827 GSList * fetchuid_list =
3828 imap_uid_list_from_lep(lep_uidlist);
3829 mailimap_search_result_free(lep_uidlist);
3831 uidlist = g_slist_concat(fetchuid_list, uidlist);
3833 carray * lep_uidtab;
3834 if (r != -1) { /* inited */
3835 imap_handle_error(SESSION(session), r);
3839 r = imap_threaded_fetch_uid(folder, 1,
3841 if (r == MAILIMAP_NO_ERROR) {
3842 GSList * fetchuid_list =
3843 imap_uid_list_from_lep_tab(lep_uidtab);
3844 imap_fetch_uid_list_free(lep_uidtab);
3845 uidlist = g_slist_concat(fetchuid_list, uidlist);
3849 if (r != MAILIMAP_NO_ERROR) {
3850 imap_handle_error(SESSION(session), r);
3854 lastuid_old = item->lastuid;
3856 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3859 msgnum = GPOINTER_TO_INT(elem->data);
3861 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3862 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3865 g_slist_free(uidlist);
3867 unlock_session(session); /* locked from imap_get_num_list */
3873 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3875 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3876 IMAPSession *session;
3878 GSList *uidlist = NULL;
3880 gboolean selected_folder;
3881 gint known_list_len = 0;
3882 debug_print("get_num_list\n");
3884 g_return_val_if_fail(folder != NULL, -1);
3885 g_return_val_if_fail(item != NULL, -1);
3886 g_return_val_if_fail(item->item.path != NULL, -1);
3887 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3888 g_return_val_if_fail(folder->account != NULL, -1);
3890 known_list_len = g_slist_length(item->uid_list);
3891 if (!item->should_update) {
3892 debug_print("get_num_list: nothing to update\n");
3893 *old_uids_valid = TRUE;
3894 if (known_list_len == item->item.total_msgs
3895 && known_list_len > 0) {
3896 *msgnum_list = g_slist_copy(item->uid_list);
3897 return known_list_len;
3899 debug_print("don't know the list length...\n");
3903 if (prefs_common.work_offline &&
3904 !inc_offline_should_override(FALSE,
3905 _("Claws Mail needs network access in order "
3906 "to access the IMAP server."))) {
3910 debug_print("getting session...\n");
3911 session = imap_session_get(folder);
3912 g_return_val_if_fail(session != NULL, -1);
3914 lock_session(session); /* unlocked by get_list_of_uids */
3915 if (FOLDER_ITEM(item)->path)
3916 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3917 FOLDER_ITEM(item)->folder->name,
3919 FOLDER_ITEM(item)->path);
3921 statusbar_print_all(_("Scanning folder %s ..."),
3922 FOLDER_ITEM(item)->folder->name);
3924 selected_folder = (session->mbox != NULL) &&
3925 (!strcmp(session->mbox, item->item.path));
3927 if (item->should_trash_cache) {
3928 *old_uids_valid = FALSE;
3929 debug_print("get_num_list: trashing num list\n");
3930 debug_print("Freeing imap uid cache\n");
3932 g_slist_free(item->uid_list);
3933 item->uid_list = NULL;
3935 imap_delete_all_cached_messages((FolderItem *)item);
3937 debug_print("get_num_list: updating num list\n");
3938 *old_uids_valid = TRUE;
3941 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3942 /* session could be broken now, in case of fatal error */
3944 debug_print("get_num_list: got %d msgs\n", nummsgs);
3947 statusbar_pop_all();
3951 *msgnum_list = uidlist;
3953 dir = folder_item_get_path((FolderItem *)item);
3954 debug_print("removing old messages from %s\n", dir);
3955 remove_numbered_files_not_in_list(dir, *msgnum_list);
3958 debug_print("get_num_list - ok - %i\n", nummsgs);
3959 statusbar_pop_all();
3960 item->should_trash_cache = FALSE;
3961 item->should_update = FALSE;
3965 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3970 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3971 flags.tmp_flags = 0;
3973 g_return_val_if_fail(item != NULL, NULL);
3974 g_return_val_if_fail(file != NULL, NULL);
3976 if (folder_has_parent_of_type(item, F_QUEUE)) {
3977 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3978 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3979 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3982 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3983 if (!msginfo) return NULL;
3985 msginfo->plaintext_file = g_strdup(file);
3986 msginfo->folder = item;
3991 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3992 GSList *msgnum_list)
3994 IMAPSession *session;
3995 MsgInfoList *ret = NULL;
3998 debug_print("get_msginfos\n");
4000 g_return_val_if_fail(folder != NULL, NULL);
4001 g_return_val_if_fail(item != NULL, NULL);
4002 g_return_val_if_fail(msgnum_list != NULL, NULL);
4004 debug_print("getting session...\n");
4005 session = imap_session_get(folder);
4006 g_return_val_if_fail(session != NULL, NULL);
4008 lock_session(session); /* unlocked later in the function */
4010 debug_print("IMAP getting msginfos\n");
4011 ok = imap_select(session, IMAP_FOLDER(folder), item,
4012 NULL, NULL, NULL, NULL, NULL, FALSE);
4013 if (ok != MAILIMAP_NO_ERROR) {
4016 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4017 folder_has_parent_of_type(item, F_QUEUE))) {
4018 ret = g_slist_concat(ret,
4019 imap_get_uncached_messages(session, item,
4021 if (ok != MAILIMAP_NO_ERROR)
4023 unlock_session(session);
4025 MsgNumberList *sorted_list, *elem, *llast = NULL;
4026 gint startnum, lastnum;
4028 unlock_session(session);
4030 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4032 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4034 llast = g_slist_last(ret);
4035 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4039 num = GPOINTER_TO_INT(elem->data);
4041 if (num > lastnum + 1 || elem == NULL) {
4043 for (i = startnum; i <= lastnum; ++i) {
4045 file = imap_fetch_msg(folder, item, i);
4047 MsgInfo *msginfo = imap_parse_msg(file, item);
4048 if (msginfo != NULL) {
4049 msginfo->msgnum = i;
4051 llast = ret = g_slist_append(ret, msginfo);
4053 llast = g_slist_append(llast, msginfo);
4054 llast = llast->next;
4069 g_slist_free(sorted_list);
4074 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4076 MsgInfo *msginfo = NULL;
4077 MsgInfoList *msginfolist;
4078 MsgNumberList numlist;
4080 numlist.next = NULL;
4081 numlist.data = GINT_TO_POINTER(uid);
4083 msginfolist = imap_get_msginfos(folder, item, &numlist);
4084 if (msginfolist != NULL) {
4085 msginfo = msginfolist->data;
4086 g_slist_free(msginfolist);
4092 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4094 IMAPSession *session;
4095 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4096 gint ok, exists = 0, unseen = 0;
4097 guint32 uid_next = 0, uid_val = 0;
4098 gboolean selected_folder;
4100 g_return_val_if_fail(folder != NULL, FALSE);
4101 g_return_val_if_fail(item != NULL, FALSE);
4102 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4103 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4105 if (item->item.path == NULL)
4108 if (item->should_update) {
4109 debug_print("scan already required\n");
4112 debug_print("getting session...\n");
4113 session = imap_session_get(folder);
4115 g_return_val_if_fail(session != NULL, FALSE);
4116 lock_session(session); /* unlocked later in the function */
4118 selected_folder = (session->mbox != NULL) &&
4119 (!strcmp(session->mbox, item->item.path));
4120 if (selected_folder) {
4121 if (!session->folder_content_changed) {
4122 ok = imap_cmd_noop(session);
4123 if (ok != MAILIMAP_NO_ERROR) {
4124 debug_print("disconnected!\n");
4126 session = imap_reconnect_if_possible(folder, session);
4128 session = imap_session_get(folder);
4129 if (session == NULL)
4133 if (session->folder_content_changed) {
4134 debug_print("CHANGED (self-noop)! scan_required\n");
4135 item->should_update = TRUE;
4136 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4137 item->item.mtime = session->uid_validity;
4138 item->should_trash_cache = TRUE;
4140 unlock_session(session);
4144 debug_print("CHANGED (previous noop)! scan_required\n");
4145 item->should_update = TRUE;
4146 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4147 item->item.mtime = session->uid_validity;
4148 item->should_trash_cache = TRUE;
4150 unlock_session(session);
4154 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4155 &exists, &uid_next, &uid_val, &unseen, FALSE);
4156 if (ok != MAILIMAP_NO_ERROR) {
4160 debug_print("exists %d, item->item.total_msgs %d\n",
4161 exists, item->item.total_msgs);
4162 if (exists != item->item.total_msgs
4163 || unseen != item->item.unread_msgs
4164 || uid_next != item->uid_next
4165 || uid_val != item->item.mtime) {
4166 debug_print("CHANGED (status)! scan_required\n");
4167 item->last_change = time(NULL);
4168 item->should_update = TRUE;
4169 item->uid_next = uid_next;
4170 if (uid_val != item->item.mtime) {
4171 item->item.mtime = uid_val;
4172 item->should_trash_cache = TRUE;
4174 unlock_session(session);
4178 unlock_session(session);
4180 item->should_update = FALSE;
4184 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4186 IMAPSession *session;
4187 IMAPFlags flags_set = 0, flags_unset = 0;
4188 gint ok = MAILIMAP_NO_ERROR;
4189 MsgNumberList numlist;
4190 hashtable_data *ht_data = NULL;
4192 g_return_if_fail(folder != NULL);
4193 g_return_if_fail(folder->klass == &imap_class);
4194 g_return_if_fail(item != NULL);
4195 g_return_if_fail(item->folder == folder);
4196 g_return_if_fail(msginfo != NULL);
4197 g_return_if_fail(msginfo->folder == item);
4199 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4200 flags_set |= IMAP_FLAG_FLAGGED;
4201 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4202 flags_unset |= IMAP_FLAG_FLAGGED;
4204 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4205 flags_unset |= IMAP_FLAG_SEEN;
4206 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4207 flags_set |= IMAP_FLAG_SEEN;
4209 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4210 flags_set |= IMAP_FLAG_ANSWERED;
4211 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4212 flags_unset |= IMAP_FLAG_ANSWERED;
4214 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4215 flags_set |= IMAP_FLAG_FORWARDED;
4216 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4217 flags_unset |= IMAP_FLAG_FORWARDED;
4219 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4220 flags_set |= IMAP_FLAG_DELETED;
4221 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4222 flags_unset |= IMAP_FLAG_DELETED;
4224 if (!flags_set && !flags_unset) {
4225 /* the changed flags were not translatable to IMAP-speak.
4226 * like MSG_POSTFILTERED, so just apply. */
4227 msginfo->flags.perm_flags = newflags;
4231 debug_print("getting session...\n");
4232 session = imap_session_get(folder);
4237 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4238 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4241 numlist.next = NULL;
4242 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4244 if (IMAP_FOLDER_ITEM(item)->batching) {
4245 /* instead of performing an UID STORE command for each message change,
4246 * as a lot of them can change "together", we just fill in hashtables
4247 * and defer the treatment so that we're able to send only one
4250 debug_print("IMAP batch mode on, deferring flags change\n");
4252 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4253 GINT_TO_POINTER(flags_set));
4254 if (ht_data == NULL) {
4255 ht_data = g_new0(hashtable_data, 1);
4256 ht_data->item = IMAP_FOLDER_ITEM(item);
4257 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4258 GINT_TO_POINTER(flags_set), ht_data);
4260 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4263 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4264 GINT_TO_POINTER(flags_unset));
4265 if (ht_data == NULL) {
4266 ht_data = g_new0(hashtable_data, 1);
4267 ht_data->item = IMAP_FOLDER_ITEM(item);
4268 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4269 GINT_TO_POINTER(flags_unset), ht_data);
4271 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4272 GINT_TO_POINTER(msginfo->msgnum));
4275 debug_print("IMAP changing flags\n");
4277 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4278 if (ok != MAILIMAP_NO_ERROR) {
4284 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4285 if (ok != MAILIMAP_NO_ERROR) {
4290 msginfo->flags.perm_flags = newflags;
4294 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4297 IMAPSession *session;
4299 MsgNumberList numlist;
4301 g_return_val_if_fail(folder != NULL, -1);
4302 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4303 g_return_val_if_fail(item != NULL, -1);
4305 debug_print("getting session...\n");
4306 session = imap_session_get(folder);
4307 if (!session) return -1;
4309 ok = imap_select(session, IMAP_FOLDER(folder), item,
4310 NULL, NULL, NULL, NULL, NULL, FALSE);
4311 if (ok != MAILIMAP_NO_ERROR) {
4314 numlist.next = NULL;
4315 numlist.data = GINT_TO_POINTER(uid);
4317 ok = imap_set_message_flags
4318 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4319 if (ok != MAILIMAP_NO_ERROR) {
4320 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4324 if (!session->uidplus) {
4325 ok = imap_cmd_expunge(session);
4329 uidstr = g_strdup_printf("%u", uid);
4330 ok = imap_cmd_expunge(session);
4333 if (ok != MAILIMAP_NO_ERROR) {
4334 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4338 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4339 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4340 dir = folder_item_get_path(item);
4341 if (is_dir_exist(dir))
4342 remove_numbered_files(dir, uid, uid);
4344 return MAILIMAP_NO_ERROR;
4347 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4349 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4352 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4356 g_return_val_if_fail(list != NULL, -1);
4358 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4359 if (GPOINTER_TO_INT(elem->data) >= num)
4362 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4365 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4367 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4368 debug_print("flag %d is OK\n", flag);
4371 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4372 debug_print("creating flags is OK\n");
4379 * NEW and DELETED flags are not syncronized
4380 * - The NEW/RECENT flags in IMAP folders can not really be directly
4381 * modified by Sylpheed
4382 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4383 * meaning, in IMAP it always removes the messages from the FolderItem
4384 * in Sylpheed it can mean to move the message to trash
4387 typedef struct _get_flags_data {
4390 MsgInfoList *msginfo_list;
4391 GRelation *msgflags;
4392 gboolean full_search;
4396 static /*gint*/ void *imap_get_flags_thread(void *data)
4398 get_flags_data *stuff = (get_flags_data *)data;
4399 Folder *folder = stuff->folder;
4400 FolderItem *fitem = (FolderItem *) stuff->item;
4401 MsgInfoList *msginfo_list = stuff->msginfo_list;
4402 GRelation *msgflags = stuff->msgflags;
4404 carray * lep_uidtab;
4405 IMAPSession *session;
4407 int r = MAILIMAP_NO_ERROR;
4408 GHashTable *flags_hash = NULL;
4409 GHashTable *tags_hash = NULL;
4410 gboolean full_search = stuff->full_search;
4411 GSList *sorted_list = NULL;
4412 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL;
4413 GSList *p_unseen, *p_answered, *p_flagged, *p_deleted, *p_forwarded;
4414 GSList *seq_list, *cur;
4415 gboolean reverse_seen = FALSE;
4416 gboolean selected_folder;
4417 gint exists_cnt, unseen_cnt;
4418 gboolean got_alien_tags = FALSE;
4420 session = imap_session_get(folder);
4422 if (session == NULL) {
4424 return GINT_TO_POINTER(-1);
4426 selected_folder = (session->mbox != NULL) &&
4427 (!strcmp(session->mbox, fitem->path));
4429 lock_session(session);
4430 if (!selected_folder) {
4431 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4432 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4433 if (ok != MAILIMAP_NO_ERROR) {
4435 return GINT_TO_POINTER(-1);
4438 if (unseen_cnt > exists_cnt / 2)
4439 reverse_seen = TRUE;
4442 if (fitem->unread_msgs > fitem->total_msgs / 2)
4443 reverse_seen = TRUE;
4446 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4448 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4450 struct mailimap_set * set;
4451 set = mailimap_set_new_interval(1, 0);
4452 seq_list = g_slist_append(NULL, set);
4455 if (folder->account && folder->account->low_bandwidth) {
4456 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4457 struct mailimap_set * imapset;
4458 clist * lep_uidlist;
4461 imapset = cur->data;
4463 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4464 full_search ? NULL:imapset, &lep_uidlist);
4467 r = imap_threaded_search(folder,
4468 IMAP_SEARCH_TYPE_UNSEEN,
4469 full_search ? NULL:imapset, &lep_uidlist);
4471 if (r == MAILIMAP_NO_ERROR) {
4474 uidlist = imap_uid_list_from_lep(lep_uidlist);
4475 mailimap_search_result_free(lep_uidlist);
4477 unseen = g_slist_concat(unseen, uidlist);
4479 imap_handle_error(SESSION(session), r);
4483 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4484 full_search ? NULL:imapset, &lep_uidlist);
4485 if (r == MAILIMAP_NO_ERROR) {
4488 uidlist = imap_uid_list_from_lep(lep_uidlist);
4489 mailimap_search_result_free(lep_uidlist);
4491 flagged = g_slist_concat(flagged, uidlist);
4493 imap_handle_error(SESSION(session), r);
4497 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4498 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4499 full_search ? NULL:imapset, &lep_uidlist);
4500 if (r == MAILIMAP_NO_ERROR) {
4503 uidlist = imap_uid_list_from_lep(lep_uidlist);
4504 mailimap_search_result_free(lep_uidlist);
4506 answered = g_slist_concat(answered, uidlist);
4508 imap_handle_error(SESSION(session), r);
4512 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4513 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4514 full_search ? NULL:imapset, &lep_uidlist);
4515 if (r == MAILIMAP_NO_ERROR) {
4518 uidlist = imap_uid_list_from_lep(lep_uidlist);
4519 mailimap_search_result_free(lep_uidlist);
4521 forwarded = g_slist_concat(forwarded, uidlist);
4523 imap_handle_error(SESSION(session), r);
4528 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4529 full_search ? NULL:imapset, &lep_uidlist);
4530 if (r == MAILIMAP_NO_ERROR) {
4533 uidlist = imap_uid_list_from_lep(lep_uidlist);
4534 mailimap_search_result_free(lep_uidlist);
4536 deleted = g_slist_concat(deleted, uidlist);
4538 imap_handle_error(SESSION(session), r);
4544 p_answered = answered;
4545 p_forwarded = forwarded;
4546 p_flagged = flagged;
4547 p_deleted = deleted;
4550 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4551 if (r == MAILIMAP_NO_ERROR) {
4552 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4553 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4554 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4555 imap_fetch_uid_flags_list_free(lep_uidtab);
4557 imap_handle_error(SESSION(session), r);
4563 if (r == MAILIMAP_NO_ERROR)
4564 unlock_session(session);
4566 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4568 MsgPermFlags flags, oldflags;
4571 msginfo = (MsgInfo *) elem->data;
4572 flags = msginfo->flags.perm_flags;
4573 wasnew = (flags & MSG_NEW);
4574 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED);
4576 if (folder->account && folder->account->low_bandwidth) {
4577 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4578 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED);
4580 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4583 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4584 if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
4585 if (!reverse_seen) {
4586 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4588 flags &= ~(MSG_UNREAD | MSG_NEW);
4592 if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
4593 flags |= MSG_MARKED;
4595 flags &= ~MSG_MARKED;
4597 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4598 if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
4599 flags |= MSG_REPLIED;
4601 flags &= ~MSG_REPLIED;
4602 if (gslist_find_next_num(&p_forwarded, msginfo->msgnum) == msginfo->msgnum)
4603 flags |= MSG_FORWARDED;
4605 flags &= ~MSG_FORWARDED;
4606 if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
4607 flags |= MSG_DELETED;
4609 flags &= ~MSG_DELETED;
4612 if (flags_hash != NULL) {
4614 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4615 GINT_TO_POINTER(msginfo->msgnum)));
4618 if ((flags & MSG_UNREAD) == 0)
4624 if (tags_hash != NULL) {
4625 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4629 g_slist_free(msginfo->tags);
4630 msginfo->tags = NULL;
4632 for (cur = tags; cur; cur = cur->next) {
4633 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4635 id = tags_get_id_for_str(real_tag);
4637 id = tags_add_tag(real_tag);
4638 got_alien_tags = TRUE;
4640 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4641 msginfo->tags = g_slist_append(
4643 GINT_TO_POINTER(id));
4647 slist_free_strings(tags);
4652 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4655 if (got_alien_tags) {
4657 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4661 g_hash_table_destroy(flags_hash);
4663 g_hash_table_destroy(tags_hash);
4665 imap_lep_set_free(seq_list);
4666 g_slist_free(flagged);
4667 g_slist_free(deleted);
4668 g_slist_free(answered);
4669 g_slist_free(forwarded);
4670 g_slist_free(unseen);
4671 g_slist_free(sorted_list);
4674 return GINT_TO_POINTER(0);
4677 static gint imap_get_flags(Folder *folder, FolderItem *item,
4678 MsgInfoList *msginfo_list, GRelation *msgflags)
4681 get_flags_data *data = g_new0(get_flags_data, 1);
4683 data->folder = folder;
4685 data->msginfo_list = msginfo_list;
4686 data->msgflags = msgflags;
4687 data->full_search = FALSE;
4689 GSList *tmp = NULL, *cur;
4691 if (prefs_common.work_offline &&
4692 !inc_offline_should_override(FALSE,
4693 _("Claws Mail needs network access in order "
4694 "to access the IMAP server."))) {
4699 tmp = folder_item_get_msg_list(item);
4701 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4702 data->full_search = TRUE;
4704 for (cur = tmp; cur; cur = cur->next)
4705 procmsg_msginfo_free((MsgInfo *)cur->data);
4709 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4716 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4718 gboolean flags_set = GPOINTER_TO_INT(user_data);
4719 gint flags_value = GPOINTER_TO_INT(key);
4720 hashtable_data *data = (hashtable_data *)value;
4721 IMAPFolderItem *_item = data->item;
4722 FolderItem *item = (FolderItem *)_item;
4723 gint ok = MAILIMAP_ERROR_BAD_STATE;
4724 IMAPSession *session = NULL;
4726 debug_print("getting session...\n");
4727 session = imap_session_get(item->folder);
4729 data->msglist = g_slist_reverse(data->msglist);
4731 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4734 g_slist_length(data->msglist));
4736 lock_session(session);
4738 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4739 NULL, NULL, NULL, NULL, NULL, FALSE);
4741 if (ok == MAILIMAP_NO_ERROR) {
4742 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4743 data->msglist, flags_value, NULL, flags_set);
4745 g_warning("can't select mailbox %s\n", item->path);
4749 unlock_session(session);
4751 g_slist_free(data->msglist);
4756 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4758 gboolean tags_set = GPOINTER_TO_INT(user_data);
4759 TagsData *data = (TagsData *)value;
4760 IMAPFolderItem *_item = data->item;
4761 FolderItem *item = (FolderItem *)_item;
4762 gchar *str = data->str;
4763 gint ok = MAILIMAP_ERROR_BAD_STATE;
4764 IMAPSession *session = NULL;
4766 debug_print("getting session...\n");
4767 session = imap_session_get(item->folder);
4769 data->msglist = g_slist_reverse(data->msglist);
4771 debug_print("IMAP %ssetting tags %s for %d messages\n",
4774 g_slist_length(data->msglist));
4776 lock_session(session);
4778 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4779 NULL, NULL, NULL, NULL, NULL, FALSE);
4781 if (ok == MAILIMAP_NO_ERROR) {
4785 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4786 data->msglist, 0, &list, tags_set);
4788 g_warning("can't select mailbox %s\n", item->path);
4792 unlock_session(session);
4794 g_slist_free(data->msglist);
4800 static void process_hashtable(IMAPFolderItem *item)
4802 if (item->flags_set_table) {
4803 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4804 g_hash_table_destroy(item->flags_set_table);
4805 item->flags_set_table = NULL;
4807 if (item->flags_unset_table) {
4808 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4809 g_hash_table_destroy(item->flags_unset_table);
4810 item->flags_unset_table = NULL;
4812 if (item->tags_set_table) {
4813 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4814 g_hash_table_destroy(item->tags_set_table);
4815 item->tags_set_table = NULL;
4817 if (item->tags_unset_table) {
4818 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4819 g_hash_table_destroy(item->tags_unset_table);
4820 item->tags_unset_table = NULL;
4825 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4827 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4828 IMAPSession *session;
4830 g_return_if_fail(item != NULL);
4832 if (item->batching == batch)
4836 item->batching = TRUE;
4837 debug_print("IMAP switching to batch mode\n");
4838 if (!item->flags_set_table) {
4839 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4841 if (!item->flags_unset_table) {
4842 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4844 if (!item->tags_set_table) {
4845 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4847 if (!item->tags_unset_table) {
4848 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4850 session = imap_session_get(folder);
4852 imap_refresh_sensitivity(session);
4853 session->sens_update_block = TRUE;
4856 debug_print("IMAP switching away from batch mode\n");
4858 process_hashtable(item);
4859 item->batching = FALSE;
4860 session = imap_session_get(folder);
4862 session->sens_update_block = FALSE;
4863 imap_refresh_sensitivity(session);
4870 /* data types conversion libetpan <-> claws */
4874 #define ETPAN_IMAP_MB_MARKED 1
4875 #define ETPAN_IMAP_MB_UNMARKED 2
4876 #define ETPAN_IMAP_MB_NOSELECT 4
4877 #define ETPAN_IMAP_MB_NOINFERIORS 8
4879 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4885 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4886 switch (imap_flags->mbf_sflag) {
4887 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4888 flags |= ETPAN_IMAP_MB_MARKED;
4890 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4891 flags |= ETPAN_IMAP_MB_NOSELECT;
4893 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4894 flags |= ETPAN_IMAP_MB_UNMARKED;
4899 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4900 cur = clist_next(cur)) {
4901 struct mailimap_mbx_list_oflag * oflag;
4903 oflag = clist_content(cur);
4905 switch (oflag->of_type) {
4906 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4907 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4915 static GSList * imap_list_from_lep(IMAPFolder * folder,
4916 clist * list, const gchar * real_path, gboolean all)
4919 GSList * item_list = NULL, *llast = NULL;
4921 for(iter = clist_begin(list) ; iter != NULL ;
4922 iter = clist_next(iter)) {
4923 struct mailimap_mailbox_list * mb;
4931 FolderItem *new_item;
4933 mb = clist_content(iter);
4939 if (mb->mb_flag != NULL)
4940 flags = imap_flags_to_flags(mb->mb_flag);
4942 delimiter = mb->mb_delimiter;
4945 dup_name = strdup(name);
4946 if (delimiter != '\0')
4947 subst_char(dup_name, delimiter, '/');
4949 base = g_path_get_basename(dup_name);
4950 if (base[0] == '.') {
4955 if (!all && path_cmp(name, real_path) == 0) {
4961 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4962 dup_name[strlen(dup_name)-1] = '\0';
4965 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
4966 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
4968 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4969 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4970 new_item->no_sub = TRUE;
4971 if (strcmp(dup_name, "INBOX") != 0 &&
4972 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4973 new_item->no_select = TRUE;
4975 if (item_list == NULL)
4976 llast = item_list = g_slist_append(item_list, new_item);
4978 llast = g_slist_append(llast, new_item);
4979 llast = llast->next;
4981 debug_print("folder '%s' found.\n", loc_path);
4992 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
4994 GSList *sorted_list, *cur;
4995 guint first, last, next;
4996 GSList *ret_list = NULL, *llast = NULL;
4997 struct mailimap_set * current_set;
4998 unsigned int item_count;
5000 if (numlist == NULL)
5003 current_set = mailimap_set_new_empty();
5005 sorted_list = g_slist_copy(numlist);
5006 sorted_list = g_slist_sort(sorted_list, g_int_compare);
5008 first = GPOINTER_TO_INT(sorted_list->data);
5011 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
5012 if (GPOINTER_TO_INT(cur->data) == 0)
5017 last = GPOINTER_TO_INT(cur->data);
5019 next = GPOINTER_TO_INT(cur->next->data);
5023 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5025 struct mailimap_set_item * item;
5026 item = mailimap_set_item_new(first, last);
5027 mailimap_set_add(current_set, item);
5031 if (item_count >= folder->max_set_size) {
5032 if (ret_list == NULL)
5033 llast = ret_list = g_slist_append(ret_list,
5036 llast = g_slist_append(llast, current_set);
5037 llast = llast->next;
5040 current_set = mailimap_set_new_empty();
5046 if (clist_count(current_set->set_list) > 0) {
5047 ret_list = g_slist_append(ret_list,
5051 g_slist_free(sorted_list);
5056 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5058 MsgNumberList *numlist = NULL;
5062 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5063 MsgInfo *msginfo = (MsgInfo *) cur->data;
5065 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5067 numlist = g_slist_reverse(numlist);
5068 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5069 g_slist_free(numlist);
5074 static GSList * imap_uid_list_from_lep(clist * list)
5081 for(iter = clist_begin(list) ; iter != NULL ;
5082 iter = clist_next(iter)) {
5085 puid = clist_content(iter);
5086 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5089 result = g_slist_reverse(result);
5093 static GSList * imap_uid_list_from_lep_tab(carray * list)
5100 for(i = 0 ; i < carray_count(list) ; i ++) {
5103 puid = carray_get(list, i);
5104 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5106 result = g_slist_reverse(result);
5110 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5112 GHashTable * tags_hash)
5119 for(i = 0 ; i < carray_count(list) ; i += 3) {
5124 puid = carray_get(list, i);
5125 pflags = carray_get(list, i + 1);
5126 tags = carray_get(list, i + 2);
5128 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5129 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5133 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5136 MsgInfo *msginfo = NULL;
5139 MsgFlags flags = {0, 0};
5141 if (info->headers == NULL)
5144 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5145 if (folder_has_parent_of_type(item, F_QUEUE)) {
5146 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5147 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5148 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5150 flags.perm_flags = info->flags;
5154 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5157 msginfo->msgnum = uid;
5158 msginfo->size = size;
5164 static void imap_lep_set_free(GSList *seq_list)
5168 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5169 struct mailimap_set * imapset;
5171 imapset = cur->data;
5172 mailimap_set_free(imapset);
5174 g_slist_free(seq_list);
5177 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5179 struct mailimap_flag_list * flag_list;
5182 flag_list = mailimap_flag_list_new_empty();
5184 if (IMAP_IS_SEEN(flags) && flag_ok(item, IMAP_FLAG_SEEN))
5185 mailimap_flag_list_add(flag_list,
5186 mailimap_flag_new_seen());
5187 if (IMAP_IS_ANSWERED(flags) && flag_ok(item, IMAP_FLAG_ANSWERED))
5188 mailimap_flag_list_add(flag_list,
5189 mailimap_flag_new_answered());
5190 if (IMAP_IS_FLAGGED(flags) && flag_ok(item, IMAP_FLAG_FLAGGED))
5191 mailimap_flag_list_add(flag_list,
5192 mailimap_flag_new_flagged());
5193 if (IMAP_IS_DELETED(flags) && flag_ok(item, IMAP_FLAG_DELETED))
5194 mailimap_flag_list_add(flag_list,
5195 mailimap_flag_new_deleted());
5196 if (IMAP_IS_DRAFT(flags) && flag_ok(item, IMAP_FLAG_DRAFT))
5197 mailimap_flag_list_add(flag_list,
5198 mailimap_flag_new_draft());
5199 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5200 mailimap_flag_list_add(flag_list,
5201 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5203 for (; cur; cur = cur->next) {
5205 imap_utf8_to_modified_utf7(cur->data, TRUE);
5206 g_strstrip(enc_str);
5208 mailimap_flag_list_add(flag_list,
5209 mailimap_flag_new_flag_keyword(enc_str));
5215 guint imap_folder_get_refcnt(Folder *folder)
5217 return ((IMAPFolder *)folder)->refcnt;
5220 void imap_folder_ref(Folder *folder)
5222 ((IMAPFolder *)folder)->refcnt++;
5225 void imap_disconnect_all(void)
5228 gboolean short_timeout;
5229 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5233 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5235 short_timeout = !networkmanager_is_online(&error);
5237 short_timeout = TRUE;
5238 g_error_free(error);
5241 short_timeout = TRUE;
5245 imap_main_set_timeout(1);
5247 for (list = account_get_list(); list != NULL; list = list->next) {
5248 PrefsAccount *account = list->data;
5249 if (account->protocol == A_IMAP4) {
5250 RemoteFolder *folder = (RemoteFolder *)account->folder;
5251 if (folder && folder->session) {
5252 IMAPSession *session = (IMAPSession *)folder->session;
5253 imap_threaded_disconnect(FOLDER(folder));
5254 SESSION(session)->state = SESSION_DISCONNECTED;
5255 session_destroy(SESSION(session));
5256 folder->session = NULL;
5262 imap_main_set_timeout(prefs_common.io_timeout_secs);
5265 void imap_folder_unref(Folder *folder)
5267 if (((IMAPFolder *)folder)->refcnt > 0)
5268 ((IMAPFolder *)folder)->refcnt--;
5271 void imap_cancel_all(void)
5276 folderlist = folder_get_list();
5277 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5278 Folder *folder = (Folder *) cur->data;
5280 if (folder->klass == &imap_class) {
5281 if (imap_is_busy(folder)) {
5282 IMAPSession *imap_session;
5283 RemoteFolder *rfolder;
5285 g_printerr("cancelled\n");
5286 imap_threaded_cancel(folder);
5287 rfolder = (RemoteFolder *) folder;
5288 imap_session = (IMAPSession *) rfolder->session;
5290 imap_session->cancelled = 1;
5296 gboolean imap_cancel_all_enabled(void)
5301 folderlist = folder_get_list();
5302 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5303 Folder *folder = (Folder *) cur->data;
5305 if (folder->klass == &imap_class) {
5306 if (imap_is_busy(folder)) {
5315 static gboolean imap_is_busy(Folder *folder)
5317 IMAPSession *imap_session;
5318 RemoteFolder *rfolder;
5320 rfolder = (RemoteFolder *) folder;
5321 imap_session = (IMAPSession *) rfolder->session;
5322 if (imap_session == NULL)
5325 return imap_session->busy;
5328 #else /* HAVE_LIBETPAN */
5330 static FolderClass imap_class;
5332 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5333 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5335 static Folder *imap_folder_new (const gchar *name,
5338 static gboolean missing_imap_warning = TRUE;
5339 if (missing_imap_warning) {
5340 missing_imap_warning = FALSE;
5342 _("You have one or more IMAP accounts "
5343 "defined. However this version of "
5344 "Claws Mail has been built without "
5345 "IMAP support; your IMAP account(s) are "
5347 "You probably need to "
5348 "install libetpan and recompile "
5353 static gint imap_create_tree (Folder *folder)
5357 static FolderItem *imap_create_folder (Folder *folder,
5363 static gint imap_rename_folder (Folder *folder,
5370 gchar imap_get_path_separator_for_item(FolderItem *item)
5375 FolderClass *imap_get_class(void)
5377 if (imap_class.idstr == NULL) {
5378 imap_class.type = F_IMAP;
5379 imap_class.idstr = "imap";
5380 imap_class.uistr = "IMAP4";
5382 imap_class.new_folder = imap_folder_new;
5383 imap_class.create_tree = imap_create_tree;
5384 imap_class.create_folder = imap_create_folder;
5385 imap_class.rename_folder = imap_rename_folder;
5387 imap_class.set_xml = folder_set_xml;
5388 imap_class.get_xml = folder_get_xml;
5389 imap_class.item_set_xml = imap_item_set_xml;
5390 imap_class.item_get_xml = imap_item_get_xml;
5391 /* nothing implemented */
5397 void imap_disconnect_all(void)
5401 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5406 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5411 void imap_cache_msg(FolderItem *item, gint msgnum)
5415 void imap_cancel_all(void)
5419 gboolean imap_cancel_all_enabled(void)
5426 #ifdef HAVE_LIBETPAN
5427 static void imap_synchronise(FolderItem *item, gint days)
5429 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5430 debug_print("%s already synced\n", item->path?item->path:item->name);
5433 debug_print("syncing %s\n", item->path?item->path:item->name);
5434 imap_gtk_synchronise(item, days);
5435 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5439 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5441 #ifdef HAVE_LIBETPAN
5444 folder_item_set_xml(folder, item, tag);
5446 #ifdef HAVE_LIBETPAN
5447 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5448 XMLAttr *attr = (XMLAttr *) cur->data;
5450 if (!attr || !attr->name || !attr->value) continue;
5451 if (!strcmp(attr->name, "uidnext"))
5452 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5453 if (!strcmp(attr->name, "last_sync"))
5454 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5455 if (!strcmp(attr->name, "last_change"))
5456 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5458 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5459 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5463 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5467 tag = folder_item_get_xml(folder, item);
5469 #ifdef HAVE_LIBETPAN
5470 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5471 IMAP_FOLDER_ITEM(item)->uid_next));
5472 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5473 IMAP_FOLDER_ITEM(item)->last_sync));
5474 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5475 IMAP_FOLDER_ITEM(item)->last_change));
5481 /* ===================================================================
5482 * UTF-7 conversion routines as in RFC 2192
5483 * ===================================================================
5484 * These two functions from:
5486 * Copyright (C) 2003-2004 Pawel Salek. */
5488 /* UTF7 modified base64 alphabet */
5489 static char base64chars[] =
5490 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5491 #define UNDEFINED 64
5493 /* UTF16 definitions */
5494 #define UTF16MASK 0x03FFUL
5495 #define UTF16SHIFT 10
5496 #define UTF16BASE 0x10000UL
5497 #define UTF16HIGHSTART 0xD800UL
5498 #define UTF16HIGHEND 0xDBFFUL
5499 #define UTF16LOSTART 0xDC00UL
5500 #define UTF16LOEND 0xDFFFUL
5503 /* Convert an IMAP mailbox to a UTF-8 string.
5504 * dst needs to have roughly 4 times the storage space of src
5505 * Hex encoding can triple the size of the input
5506 * UTF-7 can be slightly denser than UTF-8
5507 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5509 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5511 unsigned c, i, bitcount;
5512 unsigned long ucs4, utf16, bitbuf;
5513 unsigned char base64[256];
5515 char *dst, *res = malloc(2*strlen(mbox)+1);
5520 if(!dst) return NULL;
5521 /* initialize modified base64 decoding table */
5522 memset(base64, UNDEFINED, sizeof (base64));
5523 for (i = 0; i < sizeof (base64chars); ++i) {
5524 base64[(unsigned)base64chars[i]] = i;
5527 /* loop until end of string */
5528 while (*src != '\0') {
5530 /* deal with literal characters and &- */
5531 if (c != '&' || *src == '-') {
5532 /* encode literally */
5533 if (change_spaces && c == '_')
5537 /* skip over the '-' if this is an &- sequence */
5538 if (c == '&') ++src;
5540 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5544 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5546 bitbuf = (bitbuf << 6) | c;
5548 /* enough bits for a UTF-16 character? */
5549 if (bitcount >= 16) {
5551 utf16 = (bitcount ? bitbuf >> bitcount
5553 /* convert UTF16 to UCS4 */
5555 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5556 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5559 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5560 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5565 /* convert UTF-16 range of UCS4 to UTF-8 */
5566 if (ucs4 <= 0x7fUL) {
5569 } else if (ucs4 <= 0x7ffUL) {
5570 dst[0] = 0xc0 | (ucs4 >> 6);
5571 dst[1] = 0x80 | (ucs4 & 0x3f);
5573 } else if (ucs4 <= 0xffffUL) {
5574 dst[0] = 0xe0 | (ucs4 >> 12);
5575 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5576 dst[2] = 0x80 | (ucs4 & 0x3f);
5579 dst[0] = 0xf0 | (ucs4 >> 18);
5580 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5581 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5582 dst[3] = 0x80 | (ucs4 & 0x3f);
5587 /* skip over trailing '-' in modified UTF-7 encoding */
5588 if (*src == '-') ++src;
5591 /* terminate destination string */
5596 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5597 * dst should be about twice the length of src to deal with non-hex
5600 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5602 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5603 unsigned long ucs4 = 0, bitbuf = 0;
5605 /* initialize hex lookup table */
5606 char *dst, *res = malloc(2*strlen(src)+1);
5608 if(!dst) return NULL;
5614 while ((c = (unsigned char)*src) != '\0') {
5616 /* normal character? */
5617 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5618 /* switch out of UTF-7 mode */
5621 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5629 if (change_spaces && c == ' ')
5633 /* encode '&' as '&-' */
5639 /* switch to UTF-7 mode */
5644 /* Encode US-ASCII characters as themselves */
5648 } else if (utf8total) {
5649 /* save UTF8 bits into UCS4 */
5650 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5651 if (++utf8pos < utf8total) {
5659 } else if (c < 0xF0) {
5663 /* NOTE: can't convert UTF8 sequences longer than 4 */
5669 /* loop to split ucs4 into two utf16 chars if necessary */
5672 if (ucs4 >= UTF16BASE) {
5674 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5676 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5679 bitbuf = (bitbuf << 16) | ucs4;
5683 /* spew out base64 */
5684 while (bitstogo >= 6) {
5686 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5690 } while (utf16flag);
5692 /* if in UTF-7 mode, finish in ASCII */
5695 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5699 /* tie off string */