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)
140 #define IMAP_IS_SPAM(flags) ((flags & IMAP_FLAG_SPAM) != 0)
141 #define IMAP_IS_HAM(flags) ((flags & IMAP_FLAG_HAM) != 0)
144 #define IMAP4_PORT 143
145 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
146 #define IMAPS_PORT 993
149 #define IMAP_CMD_LIMIT 1000
152 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
153 ITEM_CAN_CREATE_FLAGS,
154 ITEM_CANNOT_CREATE_FLAGS
157 struct _IMAPFolderItem
166 GHashTable *flags_set_table;
167 GHashTable *flags_unset_table;
170 gboolean should_update;
171 gboolean should_trash_cache;
172 gint can_create_flags;
174 GHashTable *tags_set_table;
175 GHashTable *tags_unset_table;
180 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
181 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
183 static void imap_folder_init (Folder *folder,
187 static Folder *imap_folder_new (const gchar *name,
189 static void imap_folder_destroy (Folder *folder);
191 static IMAPSession *imap_session_new (Folder *folder,
192 const PrefsAccount *account);
193 static gint imap_session_authenticate(IMAPSession *session,
194 const PrefsAccount *account);
195 static void imap_session_destroy (Session *session);
197 static gchar *imap_fetch_msg (Folder *folder,
200 static gchar *imap_fetch_msg_full (Folder *folder,
205 static void imap_remove_cached_msg (Folder *folder,
208 static gint imap_add_msg (Folder *folder,
212 static gint imap_add_msgs (Folder *folder,
215 GRelation *relation);
217 static gint imap_copy_msg (Folder *folder,
220 static gint imap_copy_msgs (Folder *folder,
222 MsgInfoList *msglist,
223 GRelation *relation);
225 static gint imap_remove_msg (Folder *folder,
228 static gint imap_remove_msgs (Folder *folder,
230 MsgInfoList *msglist,
231 GRelation *relation);
232 static gint imap_remove_all_msg (Folder *folder,
235 static gboolean imap_is_msg_changed (Folder *folder,
239 static gint imap_close (Folder *folder,
242 static gint imap_scan_tree (Folder *folder);
244 static gint imap_create_tree (Folder *folder);
246 static FolderItem *imap_create_folder (Folder *folder,
249 static gint imap_rename_folder (Folder *folder,
252 static gint imap_remove_folder (Folder *folder,
255 static FolderItem *imap_folder_item_new (Folder *folder);
256 static void imap_folder_item_destroy (Folder *folder,
259 static IMAPSession *imap_session_get (Folder *folder);
261 static gint imap_auth (IMAPSession *session,
266 static gint imap_scan_tree_recursive (IMAPSession *session,
270 static void imap_create_missing_folders (Folder *folder);
271 static FolderItem *imap_create_special_folder
273 SpecialFolderItemType stype,
276 static gint imap_do_copy_msgs (Folder *folder,
278 MsgInfoList *msglist,
279 GRelation *relation);
281 static void imap_delete_all_cached_messages (FolderItem *item);
282 static void imap_set_batch (Folder *folder,
285 static gint imap_set_message_flags (IMAPSession *session,
286 IMAPFolderItem *item,
287 MsgNumberList *numlist,
291 static gint imap_select (IMAPSession *session,
297 guint32 *uid_validity,
298 gint *can_create_flags,
300 static gint imap_status (IMAPSession *session,
303 IMAPFolderItem *item,
306 guint32 *uid_validity,
309 static void imap_commit_tags (FolderItem *item,
314 static gchar imap_get_path_separator (IMAPSession *session,
318 static gchar *imap_get_real_path (IMAPSession *session,
323 static void imap_synchronise (FolderItem *item, gint days);
325 static gboolean imap_is_busy (Folder *folder);
327 static void imap_free_capabilities (IMAPSession *session);
329 /* low-level IMAP4rev1 commands */
330 static gint imap_cmd_login (IMAPSession *session,
334 static gint imap_cmd_noop (IMAPSession *session);
335 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
336 static gint imap_cmd_starttls (IMAPSession *session);
338 static gint imap_cmd_select (IMAPSession *session,
343 guint32 *uid_validity,
344 gint *can_create_flags,
347 static gint imap_cmd_close (IMAPSession *session);
348 static gint imap_cmd_examine (IMAPSession *session,
353 guint32 *uid_validity,
355 static gint imap_cmd_create (IMAPSession *sock,
356 const gchar *folder);
357 static gint imap_cmd_rename (IMAPSession *sock,
358 const gchar *oldfolder,
359 const gchar *newfolder);
360 static gint imap_cmd_delete (IMAPSession *session,
361 const gchar *folder);
362 static gint imap_cmd_fetch (IMAPSession *sock,
364 const gchar *filename,
367 static gint imap_cmd_append (IMAPSession *session,
368 IMAPFolderItem *item,
369 const gchar *destfolder,
373 static gint imap_cmd_copy (IMAPSession *session,
374 struct mailimap_set * set,
375 const gchar *destfolder,
376 GRelation *uid_mapping,
377 struct mailimap_set ** source,
378 struct mailimap_set ** dest);
379 static gint imap_cmd_store (IMAPSession *session,
380 IMAPFolderItem *item,
381 struct mailimap_set * set,
385 static gint imap_cmd_expunge (IMAPSession *session);
387 static void imap_path_separator_subst (gchar *str,
390 static gboolean imap_rename_folder_func (GNode *node,
392 static gint imap_get_num_list (Folder *folder,
395 gboolean *old_uids_valid);
396 static GSList *imap_get_msginfos (Folder *folder,
398 GSList *msgnum_list);
399 static MsgInfo *imap_get_msginfo (Folder *folder,
402 static gboolean imap_scan_required (Folder *folder,
404 static void imap_change_flags (Folder *folder,
407 MsgPermFlags newflags);
408 static gint imap_get_flags (Folder *folder,
410 MsgInfoList *msglist,
411 GRelation *msgflags);
412 static gchar *imap_folder_get_path (Folder *folder);
413 static gchar *imap_item_get_path (Folder *folder,
415 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
418 /* data types conversion libetpan <-> claws */
419 static GSList * imap_list_from_lep(IMAPFolder * folder,
420 clist * list, const gchar * real_path, gboolean all);
421 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
422 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
423 static GSList * imap_uid_list_from_lep(clist * list);
424 static GSList * imap_uid_list_from_lep_tab(carray * list);
425 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
427 GHashTable *tags_hash);
428 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
430 static void imap_lep_set_free(GSList *seq_list);
431 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
433 typedef struct _hashtable_data {
435 IMAPFolderItem *item;
438 static FolderClass imap_class;
440 FolderClass *imap_get_class(void)
442 if (imap_class.idstr == NULL) {
443 imap_class.type = F_IMAP;
444 imap_class.idstr = "imap";
445 imap_class.uistr = "IMAP4";
447 /* Folder functions */
448 imap_class.new_folder = imap_folder_new;
449 imap_class.destroy_folder = imap_folder_destroy;
450 imap_class.scan_tree = imap_scan_tree;
451 imap_class.create_tree = imap_create_tree;
453 /* FolderItem functions */
454 imap_class.item_new = imap_folder_item_new;
455 imap_class.item_destroy = imap_folder_item_destroy;
456 imap_class.item_get_path = imap_item_get_path;
457 imap_class.create_folder = imap_create_folder;
458 imap_class.rename_folder = imap_rename_folder;
459 imap_class.remove_folder = imap_remove_folder;
460 imap_class.close = imap_close;
461 imap_class.get_num_list = imap_get_num_list;
462 imap_class.scan_required = imap_scan_required;
463 imap_class.set_xml = folder_set_xml;
464 imap_class.get_xml = folder_get_xml;
465 imap_class.item_set_xml = imap_item_set_xml;
466 imap_class.item_get_xml = imap_item_get_xml;
468 /* Message functions */
469 imap_class.get_msginfo = imap_get_msginfo;
470 imap_class.get_msginfos = imap_get_msginfos;
471 imap_class.fetch_msg = imap_fetch_msg;
472 imap_class.fetch_msg_full = imap_fetch_msg_full;
473 imap_class.add_msg = imap_add_msg;
474 imap_class.add_msgs = imap_add_msgs;
475 imap_class.copy_msg = imap_copy_msg;
476 imap_class.copy_msgs = imap_copy_msgs;
477 imap_class.remove_msg = imap_remove_msg;
478 imap_class.remove_msgs = imap_remove_msgs;
479 imap_class.remove_all_msg = imap_remove_all_msg;
480 imap_class.is_msg_changed = imap_is_msg_changed;
481 imap_class.change_flags = imap_change_flags;
482 imap_class.get_flags = imap_get_flags;
483 imap_class.set_batch = imap_set_batch;
484 imap_class.synchronise = imap_synchronise;
485 imap_class.remove_cached_msg = imap_remove_cached_msg;
486 imap_class.commit_tags = imap_commit_tags;
488 pthread_mutex_init(&imap_mutex, NULL);
495 static void imap_refresh_sensitivity (IMAPSession *session)
499 if (session->sens_update_block)
501 mainwin = mainwindow_get_mainwindow();
503 toolbar_main_set_sensitive(mainwin);
504 main_window_set_menu_sensitive(mainwin);
508 static void lock_session(IMAPSession *session)
511 debug_print("locking session %p (%d)\n", session, session->busy);
513 debug_print(" SESSION WAS LOCKED !! \n");
514 session->busy = TRUE;
515 imap_refresh_sensitivity(session);
517 debug_print("can't lock null session\n");
521 static void unlock_session(IMAPSession *session)
524 debug_print("unlocking session %p\n", session);
525 session->busy = FALSE;
526 imap_refresh_sensitivity(session);
528 debug_print("can't unlock null session\n");
532 static void imap_disc_session_destroy(IMAPSession *session)
534 RemoteFolder *rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
540 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
541 SESSION(session)->state = SESSION_DISCONNECTED;
542 SESSION(session)->sock = NULL;
545 static gboolean is_fatal(int libetpan_errcode)
547 switch(libetpan_errcode) {
548 case MAILIMAP_ERROR_STREAM:
549 case MAILIMAP_ERROR_PROTOCOL:
550 case MAILIMAP_ERROR_PARSE:
551 case MAILIMAP_ERROR_BAD_STATE:
558 static void imap_handle_error(Session *session, int libetpan_errcode)
560 switch(libetpan_errcode) {
561 case MAILIMAP_NO_ERROR:
563 case MAILIMAP_NO_ERROR_AUTHENTICATED:
564 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
566 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
567 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
569 case MAILIMAP_ERROR_BAD_STATE:
570 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
572 case MAILIMAP_ERROR_STREAM:
573 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
575 case MAILIMAP_ERROR_PARSE:
576 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
577 "(very probably non-RFC compliance from the server)\n"));
579 case MAILIMAP_ERROR_CONNECTION_REFUSED:
580 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
582 case MAILIMAP_ERROR_MEMORY:
583 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
585 case MAILIMAP_ERROR_FATAL:
586 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
588 case MAILIMAP_ERROR_PROTOCOL:
589 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
590 "(very probably non-RFC compliance from the server)\n"));
592 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
593 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
595 case MAILIMAP_ERROR_APPEND:
596 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
598 case MAILIMAP_ERROR_NOOP:
599 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
601 case MAILIMAP_ERROR_LOGOUT:
602 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
604 case MAILIMAP_ERROR_CAPABILITY:
605 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
607 case MAILIMAP_ERROR_CHECK:
608 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
610 case MAILIMAP_ERROR_CLOSE:
611 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
613 case MAILIMAP_ERROR_EXPUNGE:
614 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
616 case MAILIMAP_ERROR_COPY:
617 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
619 case MAILIMAP_ERROR_UID_COPY:
620 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
622 case MAILIMAP_ERROR_CREATE:
623 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
625 case MAILIMAP_ERROR_DELETE:
626 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
628 case MAILIMAP_ERROR_EXAMINE:
629 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
631 case MAILIMAP_ERROR_FETCH:
632 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
634 case MAILIMAP_ERROR_UID_FETCH:
635 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
637 case MAILIMAP_ERROR_LIST:
638 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
640 case MAILIMAP_ERROR_LOGIN:
641 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
643 case MAILIMAP_ERROR_LSUB:
644 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
646 case MAILIMAP_ERROR_RENAME:
647 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
649 case MAILIMAP_ERROR_SEARCH:
650 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
652 case MAILIMAP_ERROR_UID_SEARCH:
653 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
655 case MAILIMAP_ERROR_SELECT:
656 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
658 case MAILIMAP_ERROR_STATUS:
659 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
661 case MAILIMAP_ERROR_STORE:
662 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
664 case MAILIMAP_ERROR_UID_STORE:
665 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
667 case MAILIMAP_ERROR_SUBSCRIBE:
668 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
670 case MAILIMAP_ERROR_UNSUBSCRIBE:
671 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
673 case MAILIMAP_ERROR_STARTTLS:
674 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
676 case MAILIMAP_ERROR_INVAL:
677 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
679 case MAILIMAP_ERROR_EXTENSION:
680 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
682 case MAILIMAP_ERROR_SASL:
683 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
685 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
686 case MAILIMAP_ERROR_SSL:
687 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
691 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
696 if (session && is_fatal(libetpan_errcode)) {
697 imap_disc_session_destroy(IMAP_SESSION(session));
698 } else if (session && !is_fatal(libetpan_errcode)) {
699 if (IMAP_SESSION(session)->busy)
700 unlock_session(IMAP_SESSION(session));
704 static Folder *imap_folder_new(const gchar *name, const gchar *path)
708 folder = (Folder *)g_new0(IMAPFolder, 1);
709 folder->klass = &imap_class;
710 imap_folder_init(folder, name, path);
715 static void imap_folder_destroy(Folder *folder)
717 while (imap_folder_get_refcnt(folder) > 0)
718 gtk_main_iteration();
720 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
724 static void imap_folder_init(Folder *folder, const gchar *name,
727 folder_remote_folder_init((Folder *)folder, name, path);
728 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
731 static FolderItem *imap_folder_item_new(Folder *folder)
733 IMAPFolderItem *item;
735 item = g_new0(IMAPFolderItem, 1);
738 item->uid_list = NULL;
740 return (FolderItem *)item;
743 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
745 IMAPFolderItem *item = (IMAPFolderItem *)_item;
747 g_return_if_fail(item != NULL);
748 g_slist_free(item->uid_list);
753 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
755 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
758 g_slist_free(item->uid_list);
759 item->uid_list = NULL;
764 static void imap_reset_uid_lists(Folder *folder)
766 if(folder->node == NULL)
769 /* Destroy all uid lists and rest last uid */
770 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
773 static int imap_get_capabilities(IMAPSession *session)
775 struct mailimap_capability_data *capabilities = NULL;
779 if (session->capability != NULL)
780 return MAILIMAP_NO_ERROR;
782 capabilities = imap_threaded_capability(session->folder, &result);
784 if (result != MAILIMAP_NO_ERROR) {
788 if (capabilities == NULL) {
789 return MAILIMAP_NO_ERROR;
792 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
793 cur = clist_next(cur)) {
794 struct mailimap_capability * cap =
796 if (!cap || cap->cap_data.cap_name == NULL)
798 session->capability = g_slist_append
799 (session->capability,
800 g_strdup(cap->cap_data.cap_name));
801 debug_print("got capa %s\n", cap->cap_data.cap_name);
803 mailimap_capability_data_free(capabilities);
804 return MAILIMAP_NO_ERROR;
807 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
810 for (cur = session->capability; cur; cur = cur->next) {
811 if (!g_ascii_strcasecmp(cur->data, cap))
817 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
820 gint ok = MAILIMAP_ERROR_LOGIN;
821 static time_t last_login_err = 0;
822 gchar *ext_info = "";
824 gchar *server = NULL;
825 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
826 imap_handle_error(SESSION(session), r);
829 server = g_strdup(SESSION(session)->server);
832 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
834 case IMAP_AUTH_CRAM_MD5:
835 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
837 case IMAP_AUTH_DIGEST_MD5:
838 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
840 case IMAP_AUTH_LOGIN:
841 ok = imap_cmd_login(session, user, pass, "LOGIN");
843 case IMAP_AUTH_GSSAPI:
844 ok = imap_cmd_login(session, user, pass, "GSSAPI");
847 debug_print("capabilities:\n"
853 imap_has_capability(session, "ANONYMOUS"),
854 imap_has_capability(session, "CRAM-MD5"),
855 imap_has_capability(session, "DIGEST-MD5"),
856 imap_has_capability(session, "LOGIN"),
857 imap_has_capability(session, "GSSAPI"));
858 if (imap_has_capability(session, "CRAM-MD5"))
859 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
860 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
861 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
862 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
863 ok = imap_cmd_login(session, user, pass, "GSSAPI");
864 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
865 ok = imap_cmd_login(session, user, pass, "LOGIN");
868 if (ok == MAILIMAP_NO_ERROR)
869 session->authenticated = TRUE;
871 if (type == IMAP_AUTH_CRAM_MD5) {
872 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
873 "compiled with SASL support and the "
874 "CRAM-MD5 SASL plugin is installed.");
877 if (type == IMAP_AUTH_DIGEST_MD5) {
878 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
879 "compiled with SASL support and the "
880 "DIGEST-MD5 SASL plugin is installed.");
883 if (time(NULL) - last_login_err > 10) {
884 if (!prefs_common.no_recv_err_panel) {
885 alertpanel_error_log(_("Connection to %s failed: "
889 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
890 "login refused.%s\n"),
894 last_login_err = time(NULL);
900 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
902 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
903 /* Check if this is the first try to establish a
904 connection, if yes we don't try to reconnect */
905 debug_print("reconnecting\n");
906 if (rfolder->session == NULL) {
907 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
908 folder->account->recv_server);
909 SESSION(session)->sock = NULL;
910 session_destroy(SESSION(session));
913 rfolder->session = NULL;
914 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
915 " disconnected. Reconnecting...\n"),
916 folder->account->recv_server);
917 statusbar_print_all(_("IMAP4 connection to %s has been"
918 " disconnected. Reconnecting...\n"),
919 folder->account->recv_server);
920 SESSION(session)->state = SESSION_DISCONNECTED;
921 SESSION(session)->sock = NULL;
922 session_destroy(SESSION(session));
923 /* Clear folders session to make imap_session_get create
924 a new session, because of rfolder->session == NULL
925 it will not try to reconnect again and so avoid an
927 debug_print("getting session...\n");
928 session = imap_session_get(folder);
929 rfolder->session = SESSION(session);
935 static IMAPSession *imap_session_get(Folder *folder)
937 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
938 IMAPSession *session = NULL;
939 gint r = MAILIMAP_NO_ERROR;
941 g_return_val_if_fail(folder != NULL, NULL);
942 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
943 g_return_val_if_fail(folder->account != NULL, NULL);
945 if (prefs_common.work_offline &&
946 !inc_offline_should_override(FALSE,
947 _("Claws Mail needs network access in order "
948 "to access the IMAP server."))) {
952 /* Make sure we have a session */
953 if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
954 session = IMAP_SESSION(rfolder->session);
955 } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
956 session_destroy(SESSION(rfolder->session));
957 rfolder->session = NULL;
959 } else if (rfolder->connecting) {
960 debug_print("already connecting\n");
964 imap_reset_uid_lists(folder);
965 if (time(NULL) - rfolder->last_failure <= 2)
967 rfolder->connecting = TRUE;
968 session = imap_session_new(folder, folder->account);
970 if(session == NULL) {
971 rfolder->last_failure = time(NULL);
972 rfolder->connecting = FALSE;
976 /* Make sure session is authenticated */
977 if (!IMAP_SESSION(session)->authenticated)
978 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
980 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
981 rfolder->session = NULL;
983 imap_threaded_disconnect(session->folder);
984 SESSION(session)->state = SESSION_DISCONNECTED;
985 SESSION(session)->sock = NULL;
986 session_destroy(SESSION(session));
988 rfolder->last_failure = time(NULL);
989 rfolder->connecting = FALSE;
993 /* I think the point of this code is to avoid sending a
994 * keepalive if we've used the session recently and therefore
995 * think it's still alive. Unfortunately, most of the code
996 * does not yet check for errors on the socket, and so if the
997 * connection drops we don't notice until the timeout expires.
998 * A better solution than sending a NOOP every time would be
999 * for every command to be prepared to retry until it is
1000 * successfully sent. -- mbp */
1001 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1002 /* verify that the session is still alive */
1003 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1004 debug_print("disconnected!\n");
1006 session = imap_reconnect_if_possible(folder, session);
1008 rfolder->session = NULL;
1009 rfolder->connecting = FALSE;
1010 session = imap_session_get(folder);
1014 session->cancelled = FALSE;
1017 rfolder->session = SESSION(session);
1018 rfolder->connecting = FALSE;
1020 return IMAP_SESSION(session);
1023 static IMAPSession *imap_session_new(Folder * folder,
1024 const PrefsAccount *account)
1026 IMAPSession *session;
1029 int authenticated = FALSE;
1031 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1032 /* FIXME: IMAP over SSL only... */
1035 port = account->set_imapport ? account->imapport
1036 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1037 ssl_type = account->ssl_imap;
1039 if (account->ssl_imap != SSL_NONE) {
1040 if (alertpanel_full(_("Insecure connection"),
1041 _("This connection is configured to be secured "
1042 "using SSL, but SSL is not available in this "
1043 "build of Claws Mail. \n\n"
1044 "Do you want to continue connecting to this "
1045 "server? The communication would not be "
1047 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1048 NULL, FALSE, NULL, ALERT_WARNING,
1049 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1052 port = account->set_imapport ? account->imapport
1057 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1059 if (account->set_tunnelcmd) {
1060 r = imap_threaded_connect_cmd(folder,
1062 account->recv_server,
1068 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1069 if (ssl_type == SSL_TUNNEL) {
1070 r = imap_threaded_connect_ssl(folder,
1071 account->recv_server,
1077 r = imap_threaded_connect(folder,
1078 account->recv_server,
1083 statuswindow_pop_all();
1084 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1085 authenticated = TRUE;
1087 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1088 authenticated = FALSE;
1091 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1092 if (r == MAILIMAP_ERROR_SSL)
1093 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1096 imap_handle_error(NULL, r);
1098 if(!prefs_common.no_recv_err_panel) {
1099 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1100 account->recv_server, port);
1102 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1103 account->recv_server, port);
1109 session = g_new0(IMAPSession, 1);
1110 session_init(SESSION(session), account, FALSE);
1111 SESSION(session)->type = SESSION_IMAP;
1112 SESSION(session)->server = g_strdup(account->recv_server);
1113 SESSION(session)->port = port;
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 if (!is_fatal(ok)) {
1137 SESSION(session)->sock = NULL;
1138 session_destroy(SESSION(session));
1143 imap_free_capabilities(session);
1144 session->authenticated = FALSE;
1145 session->uidplus = FALSE;
1146 session->cmd_count = 1;
1149 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1150 (session->authenticated) ? "pre" : "un");
1155 static gint imap_session_authenticate(IMAPSession *session,
1156 const PrefsAccount *account)
1158 gchar *pass, *acc_pass;
1159 gboolean failed = FALSE;
1160 gint ok = MAILIMAP_NO_ERROR;
1161 g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1162 acc_pass = account->passwd;
1165 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1167 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1169 return MAILIMAP_NO_ERROR;
1170 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1172 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1175 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1176 account->recv_server);
1177 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1178 statusbar_pop_all();
1180 if (!failed && !is_fatal(ok)) {
1185 if (prefs_common.no_recv_err_panel) {
1186 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1187 mainwindow_show_error();
1189 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1195 statuswindow_pop_all();
1196 session->authenticated = TRUE;
1197 return MAILIMAP_NO_ERROR;
1200 static void imap_session_destroy(Session *session)
1202 if (session->state != SESSION_DISCONNECTED)
1203 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1205 imap_free_capabilities(IMAP_SESSION(session));
1206 g_free(IMAP_SESSION(session)->mbox);
1209 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1211 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1214 static guint get_file_size_with_crs(const gchar *filename)
1220 if (filename == NULL)
1223 fp = fopen(filename, "rb");
1227 while (fgets(buf, sizeof (buf), fp) != NULL) {
1229 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1237 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1239 gchar *path, *filename;
1241 path = folder_item_get_path(item);
1243 if (!is_dir_exist(path)) {
1248 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1251 if (is_file_exist(filename)) {
1252 claws_unlink(filename);
1257 typedef struct _TagsData {
1260 IMAPFolderItem *item;
1263 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1265 IMAPSession *session;
1266 gint ok, can_create_tags;
1267 Folder *folder = NULL;
1268 TagsData *ht_data = NULL;
1271 g_return_if_fail(item != NULL);
1272 g_return_if_fail(msginfo != NULL);
1274 folder = item->folder;
1275 debug_print("getting session...\n");
1276 session = imap_session_get(folder);
1279 debug_print("can't get session\n");
1283 ok = imap_select(session, IMAP_FOLDER(folder), item,
1284 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1286 if (ok != MAILIMAP_NO_ERROR) {
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 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1344 list_set = g_slist_prepend(list_set, g_strdup(str));
1347 ok = imap_set_message_flags(session,
1348 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1349 slist_free_strings(list_set);
1350 g_slist_free(list_set);
1351 if (ok != MAILIMAP_NO_ERROR) {
1356 for (cur = tags_unset; cur; cur = cur->next) {
1357 gint cur_tag = GPOINTER_TO_INT(cur->data);
1358 const gchar *str = tags_get_tag(cur_tag);
1359 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1360 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1363 ok = imap_set_message_flags(session,
1364 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1365 slist_free_strings(list_unset);
1366 g_slist_free(list_unset);
1367 if (ok != MAILIMAP_NO_ERROR) {
1374 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1375 gboolean headers, gboolean body)
1377 gchar *path, *filename;
1378 IMAPSession *session;
1381 g_return_val_if_fail(folder != NULL, NULL);
1382 g_return_val_if_fail(item != NULL, NULL);
1387 path = folder_item_get_path(item);
1388 if (!is_dir_exist(path))
1389 make_dir_hier(path);
1390 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1392 debug_print("trying to fetch cached %s\n", filename);
1393 if (is_file_exist(filename)) {
1394 /* see whether the local file represents the whole message
1395 * or not. As the IMAP server reports size with \r chars,
1396 * we have to update the local file (UNIX \n only) size */
1397 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1398 guint have_size = -1;
1401 debug_print("message %d has been already %scached.\n", uid,
1402 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1404 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1405 have_size = get_file_size_with_crs(filename);
1406 if (cached && (cached->size <= have_size || !body)) {
1407 procmsg_msginfo_free(cached);
1408 ok = file_strip_crs(filename);
1409 if (ok == 0 && cached && cached->size <= have_size) {
1410 /* we have it all and stripped */
1411 debug_print("...fully cached in fact; setting flag.\n");
1412 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1415 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1416 debug_print("message not cached and file recent, considering file complete\n");
1417 ok = file_strip_crs(filename);
1421 procmsg_msginfo_free(cached);
1424 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1425 procmsg_msginfo_free(cached);
1429 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1431 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1432 procmsg_msginfo_free(cached);
1436 debug_print("getting session...\n");
1437 session = imap_session_get(folder);
1443 session_set_access_time(SESSION(session));
1444 lock_session(session); /* unlocked later in the function */
1446 debug_print("IMAP fetching messages\n");
1447 ok = imap_select(session, IMAP_FOLDER(folder), item,
1448 NULL, NULL, NULL, NULL, NULL, FALSE);
1449 if (ok != MAILIMAP_NO_ERROR) {
1450 g_warning("can't select mailbox %s\n", item->path);
1455 session_set_access_time(SESSION(session));
1457 debug_print("getting message %d...\n", uid);
1458 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1460 if (ok != MAILIMAP_NO_ERROR) {
1461 g_warning("can't fetch message %d\n", uid);
1466 session_set_access_time(SESSION(session));
1467 unlock_session(session);
1469 ok = file_strip_crs(filename);
1471 if (ok == 0 && headers && body) {
1472 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1474 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1475 procmsg_msginfo_free(cached);
1477 } else if (ok == -1) {
1478 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1480 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1481 procmsg_msginfo_free(cached);
1487 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1489 gchar *path, *filename;
1491 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1496 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1497 procmsg_msginfo_free(cached);
1500 path = folder_item_get_path(item);
1501 if (!is_dir_exist(path))
1504 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1506 if (is_file_exist(filename)) {
1507 if (cached && cached->total_size == cached->size) {
1510 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1513 size = get_file_size_with_crs(filename);
1516 if (cached && size >= cached->size) {
1517 cached->total_size = cached->size;
1518 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1519 procmsg_msginfo_free(cached);
1523 procmsg_msginfo_free(cached);
1527 void imap_cache_msg(FolderItem *item, gint msgnum)
1529 Folder *folder = NULL;
1533 folder = item->folder;
1535 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1536 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1537 debug_print("fetched %s\n", tmp);
1542 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1543 const gchar *file, MsgFlags *flags)
1547 MsgFileInfo fileinfo;
1549 g_return_val_if_fail(file != NULL, -1);
1551 fileinfo.msginfo = NULL;
1552 fileinfo.file = (gchar *)file;
1553 fileinfo.flags = flags;
1554 file_list.data = &fileinfo;
1555 file_list.next = NULL;
1557 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1561 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1562 GRelation *relation)
1565 IMAPSession *session;
1566 guint32 last_uid = 0;
1568 MsgFileInfo *fileinfo;
1569 gint ok = MAILIMAP_NO_ERROR;
1570 gint curnum = 0, total = 0;
1571 gboolean missing_uids = FALSE;
1573 g_return_val_if_fail(folder != NULL, -1);
1574 g_return_val_if_fail(dest != NULL, -1);
1575 g_return_val_if_fail(file_list != NULL, -1);
1577 debug_print("getting session...\n");
1578 session = imap_session_get(folder);
1582 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1585 statusbar_print_all(_("Adding messages..."));
1586 total = g_slist_length(file_list);
1587 for (cur = file_list; cur != NULL; cur = cur->next) {
1588 IMAPFlags iflags = 0;
1589 guint32 new_uid = 0;
1590 gchar *real_file = NULL;
1591 fileinfo = (MsgFileInfo *)cur->data;
1593 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1596 if (fileinfo->flags) {
1597 if (MSG_IS_MARKED(*fileinfo->flags))
1598 iflags |= IMAP_FLAG_FLAGGED;
1599 if (MSG_IS_REPLIED(*fileinfo->flags))
1600 iflags |= IMAP_FLAG_ANSWERED;
1601 if (MSG_IS_FORWARDED(*fileinfo->flags))
1602 iflags |= IMAP_FLAG_FORWARDED;
1603 if (MSG_IS_SPAM(*fileinfo->flags))
1604 iflags |= IMAP_FLAG_SPAM;
1606 iflags |= IMAP_FLAG_HAM;
1607 if (!MSG_IS_UNREAD(*fileinfo->flags))
1608 iflags |= IMAP_FLAG_SEEN;
1612 if (real_file == NULL)
1613 real_file = g_strdup(fileinfo->file);
1615 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1616 folder_has_parent_of_type(dest, F_OUTBOX) ||
1617 folder_has_parent_of_type(dest, F_DRAFT) ||
1618 folder_has_parent_of_type(dest, F_TRASH))
1619 iflags |= IMAP_FLAG_SEEN;
1621 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1624 if (ok != MAILIMAP_NO_ERROR) {
1625 g_warning("can't append message %s\n", real_file);
1628 statusbar_progress_all(0,0,0);
1629 statusbar_pop_all();
1632 debug_print("appended new message as %d\n", new_uid);
1633 /* put the local file in the imapcache, so that we don't
1634 * have to fetch it back later. */
1637 missing_uids = TRUE;
1638 debug_print("Missing UID (0)\n");
1641 gchar *cache_path = folder_item_get_path(dest);
1642 if (!is_dir_exist(cache_path))
1643 make_dir_hier(cache_path);
1644 if (is_dir_exist(cache_path)) {
1645 gchar *cache_file = g_strconcat(
1646 cache_path, G_DIR_SEPARATOR_S,
1647 itos(new_uid), NULL);
1648 copy_file(real_file, cache_file, TRUE);
1649 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1656 if (relation != NULL)
1657 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1658 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1659 GINT_TO_POINTER(new_uid));
1660 if (last_uid < new_uid) {
1667 statusbar_progress_all(0,0,0);
1668 statusbar_pop_all();
1673 imap_scan_required(folder, dest);
1675 session = imap_session_get(folder);
1681 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1682 &a, NULL, NULL, NULL, NULL, FALSE);
1687 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1689 GSList *result = NULL;
1694 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1695 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1696 start = item->set_first;
1697 end = item->set_last;
1698 for (t = start; t <= end; t++) {
1699 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1702 result = g_slist_reverse(result);
1703 if (debug_get_mode()) {
1704 debug_print("flat imap set: ");
1705 for (cur = result; cur; cur = cur->next) {
1706 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1713 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1714 MsgInfoList *msglist, GRelation *relation)
1718 GSList *seq_list, *cur;
1720 IMAPSession *session;
1721 gint ok = MAILIMAP_NO_ERROR;
1722 GRelation *uid_mapping;
1724 gboolean single = FALSE;
1726 g_return_val_if_fail(folder != NULL, -1);
1727 g_return_val_if_fail(dest != NULL, -1);
1728 g_return_val_if_fail(msglist != NULL, -1);
1730 debug_print("getting session...\n");
1731 session = imap_session_get(folder);
1737 msginfo = (MsgInfo *)msglist->data;
1738 if (msglist->next == NULL)
1740 src = msginfo->folder;
1742 g_warning("the src folder is identical to the dest.\n");
1746 if (src->folder != dest->folder) {
1747 GSList *infolist = NULL, *cur;
1749 for (cur = msglist; cur; cur = cur->next) {
1750 msginfo = (MsgInfo *)cur->data;
1751 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1752 fileinfo->file = procmsg_get_message_file(msginfo);
1753 fileinfo->flags = &(msginfo->flags);
1754 infolist = g_slist_prepend(infolist, fileinfo);
1756 infolist = g_slist_reverse(infolist);
1757 res = folder_item_add_msgs(dest, infolist, FALSE);
1758 for (cur = infolist; cur; cur = cur->next) {
1759 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1763 g_slist_free(infolist);
1767 lock_session(session); /* unlocked later in the function */
1769 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1770 NULL, NULL, NULL, NULL, NULL, FALSE);
1771 if (ok != MAILIMAP_NO_ERROR) {
1775 unlock_session(session);
1777 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1782 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1783 uid_mapping = g_relation_new(2);
1784 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1786 statusbar_print_all(_("Copying messages..."));
1787 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1788 struct mailimap_set * seq_set;
1789 struct mailimap_set * source = NULL;
1790 struct mailimap_set * dest = NULL;
1791 seq_set = cur->data;
1793 debug_print("Copying messages from %s to %s ...\n",
1794 src->path, destdir);
1796 lock_session(session); /* unlocked later in the function */
1797 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1804 if (ok == MAILIMAP_NO_ERROR) {
1805 unlock_session(session);
1806 if (relation && source && dest) {
1807 GSList *s_list = flatten_mailimap_set(source);
1808 GSList *d_list = flatten_mailimap_set(dest);
1809 GSList *s_cur, *d_cur;
1810 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1812 for (s_cur = s_list, d_cur = d_list;
1814 s_cur = s_cur->next, d_cur = d_cur->next) {
1815 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1819 debug_print("hhhmm, source list length != dest list length.\n");
1821 g_slist_free(s_list);
1822 g_slist_free(d_list);
1828 mailimap_set_free(source);
1830 mailimap_set_free(dest);
1832 if (ok != MAILIMAP_NO_ERROR) {
1833 g_relation_destroy(uid_mapping);
1834 imap_lep_set_free(seq_list);
1835 statusbar_pop_all();
1840 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1841 MsgInfo *msginfo = (MsgInfo *)cur->data;
1844 tuples = g_relation_select(uid_mapping,
1845 GINT_TO_POINTER(msginfo->msgnum),
1847 if (tuples->len > 0) {
1848 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1849 g_relation_insert(relation, msginfo,
1850 GINT_TO_POINTER(num));
1853 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1854 /* put the local file in the imapcache, so that we don't
1855 * have to fetch it back later. */
1857 gchar *cache_path = folder_item_get_path(msginfo->folder);
1858 gchar *real_file = g_strconcat(
1859 cache_path, G_DIR_SEPARATOR_S,
1860 itos(msginfo->msgnum), NULL);
1861 gchar *cache_file = NULL;
1863 cache_path = folder_item_get_path(dest);
1864 cache_file = g_strconcat(
1865 cache_path, G_DIR_SEPARATOR_S,
1867 if (!is_dir_exist(cache_path))
1868 make_dir_hier(cache_path);
1869 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1870 copy_file(real_file, cache_file, TRUE);
1871 debug_print("copied to cache: %s\n", cache_file);
1878 g_relation_insert(relation, msginfo,
1879 GINT_TO_POINTER(0));
1880 g_tuples_destroy(tuples);
1882 statusbar_pop_all();
1884 g_relation_destroy(uid_mapping);
1885 imap_lep_set_free(seq_list);
1889 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1890 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1891 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1892 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1894 imap_scan_required(folder, dest);
1895 if (ok == MAILIMAP_NO_ERROR)
1901 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1905 g_return_val_if_fail(msginfo != NULL, -1);
1907 msglist.data = msginfo;
1908 msglist.next = NULL;
1910 return imap_copy_msgs(folder, dest, &msglist, NULL);
1913 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1914 MsgInfoList *msglist, GRelation *relation)
1919 g_return_val_if_fail(folder != NULL, -1);
1920 g_return_val_if_fail(dest != NULL, -1);
1921 g_return_val_if_fail(msglist != NULL, -1);
1923 msginfo = (MsgInfo *)msglist->data;
1924 g_return_val_if_fail(msginfo->folder != NULL, -1);
1926 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1931 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1932 MsgInfoList *msglist, GRelation *relation)
1934 gchar *destdir, *dir;
1935 GSList *numlist = NULL, *cur;
1937 IMAPSession *session;
1938 gint ok = MAILIMAP_NO_ERROR;
1939 GRelation *uid_mapping;
1941 g_return_val_if_fail(folder != NULL, -1);
1942 g_return_val_if_fail(dest != NULL, -1);
1943 g_return_val_if_fail(msglist != NULL, -1);
1945 debug_print("getting session...\n");
1946 session = imap_session_get(folder);
1951 lock_session(session); /* unlocked later in the function */
1953 msginfo = (MsgInfo *)msglist->data;
1955 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1956 NULL, NULL, NULL, NULL, NULL, FALSE);
1957 if (ok != MAILIMAP_NO_ERROR) {
1961 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1964 for (cur = msglist; cur; cur = cur->next) {
1965 msginfo = (MsgInfo *)cur->data;
1966 if (!MSG_IS_DELETED(msginfo->flags))
1967 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1969 numlist = g_slist_reverse(numlist);
1971 uid_mapping = g_relation_new(2);
1972 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1974 if (numlist != NULL) {
1975 ok = imap_set_message_flags
1976 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1977 if (ok != MAILIMAP_NO_ERROR) {
1978 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1981 } /* else we just need to expunge */
1982 ok = imap_cmd_expunge(session);
1983 if (ok != MAILIMAP_NO_ERROR) {
1984 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1988 session->folder_content_changed = TRUE;
1989 unlock_session(session);
1991 dir = folder_item_get_path(msginfo->folder);
1992 if (is_dir_exist(dir)) {
1993 for (cur = msglist; cur; cur = cur->next) {
1994 msginfo = (MsgInfo *)cur->data;
1995 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
2000 g_relation_destroy(uid_mapping);
2001 g_slist_free(numlist);
2003 imap_scan_required(folder, dest);
2006 if (ok == MAILIMAP_NO_ERROR)
2012 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2013 MsgInfoList *msglist, GRelation *relation)
2017 g_return_val_if_fail(folder != NULL, -1);
2018 g_return_val_if_fail(dest != NULL, -1);
2019 if (msglist == NULL)
2022 msginfo = (MsgInfo *)msglist->data;
2023 g_return_val_if_fail(msginfo->folder != NULL, -1);
2025 return imap_do_remove_msgs(folder, dest, msglist, relation);
2028 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2030 GSList *list = folder_item_get_msg_list(item);
2031 gint res = imap_remove_msgs(folder, item, list, NULL);
2032 procmsg_msg_list_free(list);
2036 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2039 /* TODO: properly implement this method */
2043 static gint imap_close(Folder *folder, FolderItem *item)
2048 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2050 FolderItem *item = NULL;
2051 IMAPSession *session;
2052 gchar *root_folder = NULL;
2054 g_return_val_if_fail(folder != NULL, -1);
2055 g_return_val_if_fail(folder->account != NULL, -1);
2057 debug_print("getting session...\n");
2058 session = imap_session_get(folder);
2060 if (!folder->node) {
2061 folder_tree_destroy(folder);
2062 item = folder_item_new(folder, folder->name, NULL);
2063 item->folder = folder;
2064 folder->node = item->node = g_node_new(item);
2069 if (folder->account->imap_dir && *folder->account->imap_dir) {
2074 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2075 extract_quote(root_folder, '"');
2076 subst_char(root_folder,
2077 imap_get_path_separator(session, IMAP_FOLDER(folder),
2082 strtailchomp(root_folder, '/');
2083 real_path = imap_get_real_path
2084 (session, IMAP_FOLDER(folder), root_folder, &r);
2087 debug_print("IMAP root directory: %s\n", real_path);
2089 /* check if root directory exist */
2091 r = imap_threaded_list(session->folder, "", real_path,
2094 if (r != MAILIMAP_NO_ERROR)
2095 imap_handle_error(SESSION(session), r);
2097 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2098 if (!folder->node) {
2099 item = folder_item_new(folder, folder->name, NULL);
2100 item->folder = folder;
2101 folder->node = item->node = g_node_new(item);
2105 mailimap_list_result_free(lep_list);
2111 item = FOLDER_ITEM(folder->node->data);
2113 if (item && !item->path && root_folder) {
2114 item->path = g_strdup(root_folder);
2117 if (!item || ((item->path || root_folder) &&
2118 strcmp2(item->path, root_folder) != 0)) {
2119 folder_tree_destroy(folder);
2120 item = folder_item_new(folder, folder->name, root_folder);
2121 item->folder = folder;
2122 folder->node = item->node = g_node_new(item);
2125 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2126 imap_create_missing_folders(folder);
2131 static gint imap_scan_tree(Folder *folder)
2133 gboolean subs_only = FALSE;
2134 if (folder->account) {
2135 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2136 subs_only = folder->account->imap_subsonly;
2138 return imap_scan_tree_real(folder, subs_only);
2141 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2144 IMAPFolder *imapfolder;
2145 FolderItem *new_item;
2146 GSList *item_list, *cur;
2149 gchar *wildcard_path;
2155 g_return_val_if_fail(item != NULL, -1);
2156 g_return_val_if_fail(item->folder != NULL, -1);
2157 g_return_val_if_fail(item->no_sub == FALSE, -1);
2159 folder = item->folder;
2160 imapfolder = IMAP_FOLDER(folder);
2162 separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2166 if (folder->ui_func)
2167 folder->ui_func(folder, item, folder->ui_func_data);
2170 wildcard[0] = separator;
2173 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2179 real_path = g_strdup("");
2182 Xstrcat_a(wildcard_path, real_path, wildcard,
2183 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2187 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2189 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2191 if (r != MAILIMAP_NO_ERROR) {
2192 imap_handle_error(SESSION(session), r);
2197 item_list = imap_list_from_lep(imapfolder,
2198 lep_list, real_path, FALSE);
2199 mailimap_list_result_free(lep_list);
2204 node = item->node->children;
2205 while (node != NULL) {
2206 FolderItem *old_item = FOLDER_ITEM(node->data);
2207 GNode *next = node->next;
2210 for (cur = item_list; cur != NULL; cur = cur->next) {
2211 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2212 if (!strcmp2(old_item->path, cur_item->path)) {
2213 new_item = cur_item;
2218 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2219 debug_print("not removing INBOX\n");
2221 debug_print("folder '%s' not found. removing...\n",
2223 folder_item_remove(old_item);
2226 old_item->no_sub = new_item->no_sub;
2227 old_item->no_select = new_item->no_select;
2228 if (old_item->no_sub == TRUE && node->children) {
2229 debug_print("folder '%s' doesn't have "
2230 "subfolders. removing...\n",
2232 folder_item_remove_children(old_item);
2239 for (cur = item_list; cur != NULL; cur = cur->next) {
2240 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2243 for (node = item->node->children; node != NULL;
2244 node = node->next) {
2245 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2247 new_item = FOLDER_ITEM(node->data);
2248 folder_item_destroy(cur_item);
2254 new_item = cur_item;
2255 debug_print("new folder '%s' found.\n", new_item->path);
2256 folder_item_append(item, new_item);
2259 if (!strcmp(new_item->path, "INBOX")) {
2260 new_item->stype = F_INBOX;
2261 folder->inbox = new_item;
2262 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2265 base = g_path_get_basename(new_item->path);
2267 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2268 new_item->stype = F_OUTBOX;
2269 folder->outbox = new_item;
2270 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2271 new_item->stype = F_DRAFT;
2272 folder->draft = new_item;
2273 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2274 new_item->stype = F_QUEUE;
2275 folder->queue = new_item;
2276 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2277 new_item->stype = F_TRASH;
2278 folder->trash = new_item;
2283 if (new_item->no_sub == FALSE)
2284 imap_scan_tree_recursive(session, new_item, subs_only);
2287 g_slist_free(item_list);
2289 return MAILIMAP_NO_ERROR;
2292 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2294 IMAPSession *session = imap_session_get(folder);
2296 gchar *wildcard_path;
2300 GSList *item_list = NULL, *cur;
2301 GList *child_list = NULL, *tmplist = NULL;
2302 GSList *sub_list = NULL;
2308 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2313 wildcard[0] = separator;
2316 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2322 real_path = g_strdup("");
2325 Xstrcat_a(wildcard_path, real_path, wildcard,
2326 {g_free(real_path); return NULL;});
2330 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2331 item->path?item->path:item->name);
2333 statusbar_print_all(_("Looking for subfolders of %s..."),
2334 item->path?item->path:item->name);
2336 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2338 statusbar_pop_all();
2341 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2342 lep_list, real_path, FALSE);
2343 mailimap_list_result_free(lep_list);
2345 for (cur = item_list; cur != NULL; cur = cur->next) {
2346 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2348 tmplist = imap_scan_subtree(folder, cur_item,
2349 unsubs_only, recursive);
2351 child_list = g_list_concat(child_list, tmplist);
2353 child_list = g_list_prepend(child_list,
2354 imap_get_real_path(session,
2355 IMAP_FOLDER(folder), cur_item->path, &r));
2357 statusbar_pop_all();
2360 folder_item_destroy(cur_item);
2362 child_list = g_list_reverse(child_list);
2363 g_slist_free(item_list);
2366 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2368 statusbar_pop_all();
2371 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2372 lep_list, real_path, FALSE);
2373 mailimap_list_result_free(lep_list);
2375 for (cur = sub_list; cur != NULL; cur = cur->next) {
2376 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2377 GList *oldlitem = NULL;
2378 gchar *tmp = imap_get_real_path(session,
2379 IMAP_FOLDER(folder), cur_item->path, &r);
2381 statusbar_pop_all();
2384 folder_item_destroy(cur_item);
2385 oldlitem = g_list_find_custom(
2386 child_list, tmp, (GCompareFunc)strcmp2);
2388 child_list = g_list_remove_link(child_list, oldlitem);
2389 g_free(oldlitem->data);
2390 g_list_free(oldlitem);
2396 statusbar_pop_all();
2401 static gint imap_create_tree(Folder *folder)
2403 g_return_val_if_fail(folder != NULL, -1);
2404 g_return_val_if_fail(folder->node != NULL, -1);
2405 g_return_val_if_fail(folder->node->data != NULL, -1);
2406 g_return_val_if_fail(folder->account != NULL, -1);
2408 imap_scan_tree(folder);
2409 imap_create_missing_folders(folder);
2414 static void imap_create_missing_folders(Folder *folder)
2416 g_return_if_fail(folder != NULL);
2419 folder->inbox = imap_create_special_folder
2420 (folder, F_INBOX, "INBOX");
2422 folder->trash = imap_create_special_folder
2423 (folder, F_TRASH, "Trash");
2425 folder->queue = imap_create_special_folder
2426 (folder, F_QUEUE, "Queue");
2427 if (!folder->outbox)
2428 folder->outbox = imap_create_special_folder
2429 (folder, F_OUTBOX, "Sent");
2431 folder->draft = imap_create_special_folder
2432 (folder, F_DRAFT, "Drafts");
2435 static FolderItem *imap_create_special_folder(Folder *folder,
2436 SpecialFolderItemType stype,
2440 FolderItem *new_item;
2442 g_return_val_if_fail(folder != NULL, NULL);
2443 g_return_val_if_fail(folder->node != NULL, NULL);
2444 g_return_val_if_fail(folder->node->data != NULL, NULL);
2445 g_return_val_if_fail(folder->account != NULL, NULL);
2446 g_return_val_if_fail(name != NULL, NULL);
2448 item = FOLDER_ITEM(folder->node->data);
2449 new_item = imap_create_folder(folder, item, name);
2452 g_warning("Can't create '%s'\n", name);
2453 if (!folder->inbox) return NULL;
2455 new_item = imap_create_folder(folder, folder->inbox, name);
2457 g_warning("Can't create '%s' under INBOX\n", name);
2459 new_item->stype = stype;
2461 new_item->stype = stype;
2466 static gchar *imap_folder_get_path(Folder *folder)
2470 g_return_val_if_fail(folder != NULL, NULL);
2471 g_return_val_if_fail(folder->account != NULL, NULL);
2473 folder_path = g_strconcat(get_imap_cache_dir(),
2475 folder->account->recv_server,
2477 folder->account->userid,
2483 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2485 gchar *folder_path, *path;
2487 g_return_val_if_fail(folder != NULL, NULL);
2488 g_return_val_if_fail(item != NULL, NULL);
2489 folder_path = imap_folder_get_path(folder);
2491 g_return_val_if_fail(folder_path != NULL, NULL);
2492 if (folder_path[0] == G_DIR_SEPARATOR) {
2494 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2497 path = g_strdup(folder_path);
2500 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2501 folder_path, G_DIR_SEPARATOR_S,
2504 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2507 g_free(folder_path);
2512 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2515 gchar *dirpath, *imap_path;
2516 IMAPSession *session;
2517 FolderItem *new_item;
2522 gboolean no_select = FALSE, no_sub = FALSE;
2523 gboolean exist = FALSE;
2525 g_return_val_if_fail(folder != NULL, NULL);
2526 g_return_val_if_fail(folder->account != NULL, NULL);
2527 g_return_val_if_fail(parent != NULL, NULL);
2528 g_return_val_if_fail(name != NULL, NULL);
2530 debug_print("getting session...\n");
2531 session = imap_session_get(folder);
2536 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2537 dirpath = g_strdup(name);
2538 }else if (parent->path)
2539 dirpath = g_strconcat(parent->path, "/", name, NULL);
2540 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2541 dirpath = g_strdup(name);
2542 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2545 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2546 strtailchomp(imap_dir, '/');
2547 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2549 dirpath = g_strdup(name);
2553 /* keep trailing directory separator to create a folder that contains
2555 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2557 strtailchomp(dirpath, '/');
2558 Xstrdup_a(new_name, name, {
2562 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2565 imap_path_separator_subst(imap_path, separator);
2566 /* remove trailing / for display */
2567 strtailchomp(new_name, '/');
2569 if (strcmp(dirpath, "INBOX") != 0) {
2574 argbuf = g_ptr_array_new();
2575 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2576 if (r != MAILIMAP_NO_ERROR) {
2577 imap_handle_error(SESSION(session), r);
2578 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2581 ptr_array_free_strings(argbuf);
2582 g_ptr_array_free(argbuf, TRUE);
2586 if (clist_count(lep_list) > 0)
2588 mailimap_list_result_free(lep_list);
2591 ok = imap_cmd_create(session, imap_path);
2592 if (ok != MAILIMAP_NO_ERROR) {
2593 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2598 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2599 if (r == MAILIMAP_NO_ERROR) {
2600 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2601 lep_list, dirpath, TRUE);
2603 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2604 no_select = cur_item->no_select;
2605 no_sub = cur_item->no_sub;
2606 g_slist_free(item_list);
2608 mailimap_list_result_free(lep_list);
2610 imap_handle_error(SESSION(session), r);
2613 imap_threaded_subscribe(folder, imap_path, TRUE);
2617 /* just get flags */
2618 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2619 if (r == MAILIMAP_NO_ERROR) {
2620 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2621 lep_list, dirpath, TRUE);
2623 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2624 no_select = cur_item->no_select;
2625 no_sub = cur_item->no_sub;
2626 g_slist_free(item_list);
2628 mailimap_list_result_free(lep_list);
2630 imap_handle_error(SESSION(session), r);
2634 new_item = folder_item_new(folder, new_name, dirpath);
2635 new_item->no_select = no_select;
2636 new_item->no_sub = no_sub;
2637 folder_item_append(parent, new_item);
2641 dirpath = folder_item_get_path(new_item);
2642 if (!is_dir_exist(dirpath))
2643 make_dir_hier(dirpath);
2647 /* folder existed, scan it */
2648 imap_scan_required(folder, new_item);
2649 folder_item_scan_full(new_item, FALSE);
2655 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2660 gchar *real_oldpath;
2661 gchar *real_newpath;
2663 gchar *old_cache_dir;
2664 gchar *new_cache_dir;
2665 IMAPSession *session;
2668 gint exists, recent, unseen;
2669 guint32 uid_validity;
2671 g_return_val_if_fail(folder != NULL, -1);
2672 g_return_val_if_fail(item != NULL, -1);
2673 g_return_val_if_fail(item->path != NULL, -1);
2674 g_return_val_if_fail(name != NULL, -1);
2676 debug_print("getting session...\n");
2677 session = imap_session_get(folder);
2682 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
2684 g_warning(_("New folder name must not contain the namespace "
2689 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2694 g_free(session->mbox);
2695 session->mbox = NULL;
2696 session->exists = 0;
2697 session->recent = 0;
2698 session->expunge = 0;
2699 ok = imap_cmd_examine(session, "INBOX",
2700 &exists, &recent, &unseen, &uid_validity, FALSE);
2701 if (ok != MAILIMAP_NO_ERROR) {
2702 g_free(real_oldpath);
2706 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
2709 if (strchr(item->path, G_DIR_SEPARATOR)) {
2710 dirpath = g_path_get_dirname(item->path);
2711 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2714 newpath = g_strdup(name);
2716 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2717 imap_path_separator_subst(real_newpath, separator);
2719 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2720 if (ok != MAILIMAP_NO_ERROR) {
2721 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2722 real_oldpath, real_newpath);
2723 g_free(real_oldpath);
2725 g_free(real_newpath);
2729 item->name = g_strdup(name);
2731 old_cache_dir = folder_item_get_path(item);
2733 paths[0] = g_strdup(item->path);
2735 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2736 imap_rename_folder_func, paths);
2738 if (is_dir_exist(old_cache_dir)) {
2739 new_cache_dir = folder_item_get_path(item);
2740 if (rename(old_cache_dir, new_cache_dir) < 0) {
2741 FILE_OP_ERROR(old_cache_dir, "rename");
2743 g_free(new_cache_dir);
2746 g_free(old_cache_dir);
2749 g_free(real_oldpath);
2750 g_free(real_newpath);
2754 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2758 IMAPSession *session;
2759 debug_print("getting session...\n");
2761 session = imap_session_get(folder);
2765 if (item && item->path) {
2766 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2767 if (!path || is_fatal(r))
2769 if (!strcmp(path, "INBOX") && sub == FALSE)
2771 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2772 r = imap_threaded_subscribe(folder, path, sub);
2775 r = imap_threaded_subscribe(folder, rpath, sub);
2781 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2784 IMAPSession *session;
2787 gboolean selected_folder;
2789 g_return_val_if_fail(folder != NULL, -1);
2790 g_return_val_if_fail(item != NULL, -1);
2791 g_return_val_if_fail(item->path != NULL, -1);
2793 debug_print("getting session...\n");
2794 session = imap_session_get(folder);
2798 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2802 imap_threaded_subscribe(folder, path, FALSE);
2804 selected_folder = (session->mbox != NULL) &&
2805 (!strcmp(session->mbox, item->path));
2806 if (selected_folder) {
2807 ok = imap_cmd_close(session);
2808 if (ok != MAILIMAP_NO_ERROR) {
2809 debug_print("close err %d\n", ok);
2813 ok = imap_cmd_delete(session, path);
2814 if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
2815 gchar *tmp = g_strdup_printf("%s%c", path,
2816 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
2820 ok = imap_cmd_delete(session, path);
2823 if (ok != MAILIMAP_NO_ERROR) {
2824 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2830 cache_dir = folder_item_get_path(item);
2831 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2832 g_warning("can't remove directory '%s'\n", cache_dir);
2834 folder_item_remove(item);
2838 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2842 g_return_val_if_fail(item != NULL, -1);
2843 g_return_val_if_fail(item->folder != NULL, -1);
2844 g_return_val_if_fail(item->node != NULL, -1);
2846 node = item->node->children;
2847 while (node != NULL) {
2849 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2853 debug_print("IMAP removing %s\n", item->path);
2855 if (imap_remove_all_msg(folder, item) < 0)
2857 return imap_remove_folder_real(folder, item);
2860 typedef struct _uncached_data {
2861 IMAPSession *session;
2863 MsgNumberList *numlist;
2870 static void *imap_get_uncached_messages_thread(void *data)
2872 uncached_data *stuff = (uncached_data *)data;
2873 IMAPSession *session = stuff->session;
2874 FolderItem *item = stuff->item;
2875 MsgNumberList *numlist = stuff->numlist;
2876 GSList *newlist = NULL;
2877 GSList *llast = NULL;
2878 GSList *seq_list, *cur;
2879 gboolean got_alien_tags = FALSE;
2881 debug_print("uncached_messages\n");
2883 if (session == NULL || item == NULL || item->folder == NULL
2884 || FOLDER_CLASS(item->folder) != &imap_class) {
2889 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2890 debug_print("get msgs info\n");
2891 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2892 struct mailimap_set * imapset;
2898 if (session->cancelled)
2901 imapset = cur->data;
2903 r = imap_threaded_fetch_env(session->folder,
2904 imapset, &env_list);
2905 if (r != MAILIMAP_NO_ERROR) {
2906 imap_handle_error(SESSION(session), r);
2914 session_set_access_time(SESSION(session));
2917 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2918 struct imap_fetch_env_info * info;
2920 GSList *tags = NULL, *cur = NULL;
2921 info = carray_get(env_list, i);
2922 tags = carray_get(env_list, i+1);
2923 msginfo = imap_envelope_from_lep(info, item);
2924 if (msginfo == NULL) {
2925 slist_free_strings(tags);
2930 g_slist_free(msginfo->tags);
2931 msginfo->tags = NULL;
2933 for (cur = tags; cur; cur = cur->next) {
2934 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2936 id = tags_get_id_for_str(real_tag);
2938 id = tags_add_tag(real_tag);
2939 got_alien_tags = TRUE;
2941 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2942 msginfo->tags = g_slist_append(
2944 GINT_TO_POINTER(id));
2948 slist_free_strings(tags);
2950 msginfo->folder = item;
2952 llast = newlist = g_slist_append(newlist, msginfo);
2954 llast = g_slist_append(llast, msginfo);
2955 llast = llast->next;
2960 imap_fetch_env_free(env_list);
2963 if (got_alien_tags) {
2965 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2968 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2969 struct mailimap_set * imapset;
2971 imapset = cur->data;
2972 mailimap_set_free(imapset);
2975 session_set_access_time(SESSION(session));
2980 #define MAX_MSG_NUM 50
2982 static GSList *imap_get_uncached_messages(IMAPSession *session,
2984 MsgNumberList *numlist,
2987 GSList *result = NULL;
2989 uncached_data *data = g_new0(uncached_data, 1);
2994 data->total = g_slist_length(numlist);
2995 data->ok = MAILIMAP_NO_ERROR;
2996 debug_print("messages list : %i\n", data->total);
2998 while (cur != NULL) {
2999 GSList * partial_result;
3007 while (count < MAX_MSG_NUM) {
3012 if (newlist == NULL)
3013 llast = newlist = g_slist_append(newlist, p);
3015 llast = g_slist_append(llast, p);
3016 llast = llast->next;
3026 data->session = session;
3028 data->numlist = newlist;
3031 if (prefs_common.work_offline &&
3032 !inc_offline_should_override(FALSE,
3033 _("Claws Mail needs network access in order "
3034 "to access the IMAP server."))) {
3040 (GSList *)imap_get_uncached_messages_thread(data);
3042 if (data->ok != MAILIMAP_NO_ERROR) {
3045 statusbar_progress_all(data->cur,data->total, 1);
3047 g_slist_free(newlist);
3049 result = g_slist_concat(result, partial_result);
3054 statusbar_progress_all(0,0,0);
3055 statusbar_pop_all();
3060 static void imap_delete_all_cached_messages(FolderItem *item)
3064 g_return_if_fail(item != NULL);
3065 g_return_if_fail(item->folder != NULL);
3066 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3068 debug_print("Deleting all cached messages...\n");
3070 dir = folder_item_get_path(item);
3071 if (is_dir_exist(dir))
3072 remove_all_numbered_files(dir);
3075 debug_print("done.\n");
3078 gchar imap_get_path_separator_for_item(FolderItem *item)
3080 Folder *folder = NULL;
3081 IMAPFolder *imap_folder = NULL;
3082 IMAPSession *session = NULL;
3087 folder = item->folder;
3092 imap_folder = IMAP_FOLDER(folder);
3097 debug_print("getting session...");
3098 session = imap_session_get(FOLDER(folder));
3099 result = imap_get_path_separator(session, imap_folder, item->path, &ok);
3103 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder, gint *ok)
3107 gchar separator = '\0';
3109 g_return_val_if_fail(session != NULL, '/');
3110 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3112 if (r != MAILIMAP_NO_ERROR) {
3113 imap_handle_error(SESSION(session), r);
3114 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3119 if (clist_count(lep_list) > 0) {
3120 clistiter * iter = clist_begin(lep_list);
3121 struct mailimap_mailbox_list * mb;
3122 mb = clist_content(iter);
3124 separator = mb->mb_delimiter;
3125 debug_print("got separator: %c\n", folder->last_seen_separator);
3127 *ok = MAILIMAP_NO_ERROR;
3128 mailimap_list_result_free(lep_list);
3132 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3134 gchar separator = '/';
3136 if (folder->last_seen_separator == 0) {
3137 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "", ok);
3140 if (folder->last_seen_separator == 0) {
3141 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX", ok);
3144 if (folder->last_seen_separator != 0) {
3145 debug_print("using separator: %c\n", folder->last_seen_separator);
3146 return folder->last_seen_separator;
3152 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3157 g_return_val_if_fail(folder != NULL, NULL);
3158 g_return_val_if_fail(path != NULL, NULL);
3160 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3161 separator = imap_get_path_separator(session, folder, path, ok);
3162 imap_path_separator_subst(real_path, separator);
3167 static gint imap_set_message_flags(IMAPSession *session,
3168 IMAPFolderItem *item,
3169 MsgNumberList *numlist,
3178 IMAPFolder *folder = NULL;
3179 GSList *sorted_list = NULL;
3181 if (numlist == NULL || session == NULL)
3182 return MAILIMAP_ERROR_BAD_STATE;
3184 folder = IMAP_FOLDER(session->folder);
3186 sorted_list = g_slist_copy(numlist);
3187 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3189 cur = g_slist_last(sorted_list);
3192 total = GPOINTER_TO_INT(cur->data);
3194 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3196 statusbar_print_all(_("Flagging messages..."));
3198 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3199 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3200 struct mailimap_set_item *set_item = clist_content(
3201 clist_begin(imapset->set_list));
3203 statusbar_progress_all(set_item->set_first, total, 1);
3205 ok = imap_cmd_store(session, item, imapset,
3206 flags, tags, is_set);
3207 statusbar_progress_all(set_item->set_last, total, 1);
3208 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3209 /* reduce max set size */
3210 folder->max_set_size /= 2;
3212 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3217 g_slist_free(sorted_list);
3219 statusbar_progress_all(0,0,0);
3220 statusbar_pop_all();
3222 imap_lep_set_free(seq_list);
3227 typedef struct _select_data {
3228 IMAPSession *session;
3233 guint32 *uid_validity;
3237 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3239 gint *exists, gint *recent, gint *unseen,
3240 guint32 *uid_validity, gint *can_create_flags,
3245 gint exists_, recent_, unseen_;
3246 guint32 uid_validity_;
3247 gint can_create_flags_;
3248 const gchar *path = item ? item->path:NULL;
3251 return MAILIMAP_ERROR_BAD_STATE;
3254 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3255 if (session->mbox && strcmp(session->mbox, path) == 0)
3256 return MAILIMAP_NO_ERROR;
3258 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3259 if (session->mbox && strcmp(session->mbox, path) == 0) {
3260 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3261 return MAILIMAP_NO_ERROR;
3271 uid_validity = &uid_validity_;
3272 if (!can_create_flags)
3273 can_create_flags = &can_create_flags_;
3275 g_free(session->mbox);
3276 session->mbox = NULL;
3277 session->exists = 0;
3278 session->recent = 0;
3279 session->expunge = 0;
3281 real_path = imap_get_real_path(session, folder, path, &ok);
3284 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3285 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3286 ok = imap_cmd_select(session, real_path,
3287 exists, recent, unseen, uid_validity, can_create_flags,
3288 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3289 if (ok != MAILIMAP_NO_ERROR) {
3290 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3292 session->mbox = g_strdup(path);
3293 session->folder_content_changed = FALSE;
3294 session->exists = *exists;
3295 session->recent = *recent;
3296 session->expunge = 0;
3297 session->unseen = *unseen;
3298 session->uid_validity = *uid_validity;
3299 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3300 session->exists, session->recent, session->expunge,
3301 session->uid_validity, *can_create_flags);
3303 if (*can_create_flags) {
3304 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3306 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3313 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3314 const gchar *path, IMAPFolderItem *item,
3316 guint32 *uid_next, guint32 *uid_validity,
3317 gint *unseen, gboolean block)
3321 struct mailimap_mailbox_data_status * data_status;
3326 real_path = imap_get_real_path(session, folder, path, &r);
3347 if (session->mbox != NULL &&
3348 !strcmp(session->mbox, item->item.path)) {
3349 r = imap_cmd_close(session);
3350 if (r != MAILIMAP_NO_ERROR) {
3351 debug_print("close err %d\n", r);
3356 r = imap_threaded_status(FOLDER(folder), real_path,
3357 &data_status, mask);
3360 if (r != MAILIMAP_NO_ERROR) {
3361 imap_handle_error(SESSION(session), r);
3362 debug_print("status err %d\n", r);
3366 if (data_status->st_info_list == NULL) {
3367 mailimap_mailbox_data_status_free(data_status);
3368 debug_print("status->st_info_list == NULL\n");
3369 return MAILIMAP_ERROR_BAD_STATE;
3373 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3374 iter = clist_next(iter)) {
3375 struct mailimap_status_info * info;
3377 info = clist_content(iter);
3378 switch (info->st_att) {
3379 case MAILIMAP_STATUS_ATT_MESSAGES:
3381 * messages = info->st_value;
3382 got_values |= 1 << 0;
3386 case MAILIMAP_STATUS_ATT_UIDNEXT:
3388 * uid_next = info->st_value;
3389 got_values |= 1 << 2;
3393 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3395 * uid_validity = info->st_value;
3396 got_values |= 1 << 3;
3400 case MAILIMAP_STATUS_ATT_UNSEEN:
3402 * unseen = info->st_value;
3403 got_values |= 1 << 4;
3408 mailimap_mailbox_data_status_free(data_status);
3410 if (got_values != mask) {
3411 g_warning("status: incomplete values received (%d)\n", got_values);
3413 return MAILIMAP_NO_ERROR;
3416 static void imap_free_capabilities(IMAPSession *session)
3418 slist_free_strings(session->capability);
3419 g_slist_free(session->capability);
3420 session->capability = NULL;
3423 /* low-level IMAP4rev1 commands */
3425 static gint imap_cmd_login(IMAPSession *session,
3426 const gchar *user, const gchar *pass,
3432 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3433 gint ok = MAILIMAP_ERROR_BAD_STATE;
3434 if (imap_has_capability(session, "STARTTLS")) {
3435 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3436 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3437 ok = imap_cmd_starttls(session);
3438 if (ok != MAILIMAP_NO_ERROR) {
3439 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3443 imap_free_capabilities(session);
3444 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3445 imap_handle_error(SESSION(session), r);
3446 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3451 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3452 "server requires TLS, but Claws Mail "
3453 "has been compiled without OpenSSL "
3455 SESSION(session)->server);
3456 return MAILIMAP_ERROR_LOGIN;
3459 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3460 return MAILIMAP_ERROR_LOGIN;
3464 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3466 SESSION(session)->server,
3468 r = imap_threaded_login(session->folder, user, pass, type);
3469 if (r != MAILIMAP_NO_ERROR) {
3470 imap_handle_error(SESSION(session), r);
3471 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3472 SESSION(session)->server);
3475 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3476 SESSION(session)->server);
3477 ok = MAILIMAP_NO_ERROR;
3482 static gint imap_cmd_noop(IMAPSession *session)
3485 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3487 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3488 if (r != MAILIMAP_NO_ERROR) {
3489 imap_handle_error(SESSION(session), r);
3490 debug_print("noop err %d\n", r);
3494 session->folder_content_changed = FALSE;
3496 if ((exists && exists != session->exists)
3497 || (recent && recent != session->recent)
3498 || (expunge && expunge != session->expunge)
3499 || (unseen && unseen != session->unseen)) {
3500 session->folder_content_changed = TRUE;
3502 if (uidnext != 0 && uidnext != session->uid_next) {
3503 session->uid_next = uidnext;
3504 session->folder_content_changed = TRUE;
3506 if (uidval != 0 && uidval != session->uid_validity) {
3507 session->uid_validity = uidval;
3508 session->folder_content_changed = TRUE;
3511 session->exists = exists;
3512 session->recent = recent;
3513 session->expunge = expunge;
3514 session->unseen = unseen;
3516 session_set_access_time(SESSION(session));
3518 return MAILIMAP_NO_ERROR;
3521 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3522 static gint imap_cmd_starttls(IMAPSession *session)
3526 r = imap_threaded_starttls(session->folder,
3527 SESSION(session)->server, SESSION(session)->port);
3528 if (r != MAILIMAP_NO_ERROR) {
3529 imap_handle_error(SESSION(session), r);
3530 debug_print("starttls err %d\n", r);
3533 return MAILIMAP_NO_ERROR;
3537 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3538 gint *exists, gint *recent, gint *unseen,
3539 guint32 *uid_validity, gint *can_create_flags,
3540 GSList **ok_flags, gboolean block)
3544 r = imap_threaded_select(session->folder, folder,
3545 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3546 if (r != MAILIMAP_NO_ERROR) {
3547 imap_handle_error(SESSION(session), r);
3548 debug_print("select err %d\n", r);
3551 return MAILIMAP_NO_ERROR;
3554 static gint imap_cmd_close(IMAPSession *session)
3558 r = imap_threaded_close(session->folder);
3559 if (r != MAILIMAP_NO_ERROR) {
3560 imap_handle_error(SESSION(session), r);
3561 debug_print("close err %d\n", r);
3564 g_free(session->mbox);
3565 session->mbox = NULL;
3566 session->exists = 0;
3567 session->recent = 0;
3568 session->expunge = 0;
3569 return MAILIMAP_NO_ERROR;
3572 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3573 gint *exists, gint *recent, gint *unseen,
3574 guint32 *uid_validity, gboolean block)
3578 r = imap_threaded_examine(session->folder, folder,
3579 exists, recent, unseen, uid_validity);
3580 if (r != MAILIMAP_NO_ERROR) {
3581 imap_handle_error(SESSION(session), r);
3582 debug_print("examine err %d\n", r);
3586 return MAILIMAP_NO_ERROR;
3589 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3593 r = imap_threaded_create(session->folder, folder);
3594 if (r != MAILIMAP_NO_ERROR) {
3595 imap_handle_error(SESSION(session), r);
3599 return MAILIMAP_NO_ERROR;
3602 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3603 const gchar *new_folder)
3607 r = imap_threaded_rename(session->folder, old_folder,
3609 if (r != MAILIMAP_NO_ERROR) {
3610 imap_handle_error(SESSION(session), r);
3614 return MAILIMAP_NO_ERROR;
3617 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3622 r = imap_threaded_delete(session->folder, folder);
3623 if (r != MAILIMAP_NO_ERROR) {
3624 imap_handle_error(SESSION(session), r);
3628 return MAILIMAP_NO_ERROR;
3631 typedef struct _fetch_data {
3632 IMAPSession *session;
3634 const gchar *filename;
3640 static void *imap_cmd_fetch_thread(void *data)
3642 fetch_data *stuff = (fetch_data *)data;
3643 IMAPSession *session = stuff->session;
3644 guint32 uid = stuff->uid;
3645 const gchar *filename = stuff->filename;
3649 r = imap_threaded_fetch_content(session->folder,
3653 r = imap_threaded_fetch_content(session->folder,
3656 if (r != MAILIMAP_NO_ERROR) {
3657 imap_handle_error(SESSION(session), r);
3658 debug_print("fetch err %d\n", r);
3659 return GINT_TO_POINTER(r);
3661 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3664 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3665 const gchar *filename, gboolean headers,
3668 fetch_data *data = g_new0(fetch_data, 1);
3671 data->session = session;
3673 data->filename = filename;
3674 data->headers = headers;
3677 if (prefs_common.work_offline &&
3678 !inc_offline_should_override(FALSE,
3679 _("Claws Mail needs network access in order "
3680 "to access the IMAP server."))) {
3684 statusbar_print_all(_("Fetching message..."));
3685 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3686 statusbar_pop_all();
3692 static gint imap_cmd_append(IMAPSession *session,
3693 IMAPFolderItem *item,
3694 const gchar *destfolder,
3695 const gchar *file, IMAPFlags flags,
3698 struct mailimap_flag_list * flag_list;
3701 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3703 flag_list = imap_flag_to_lep(item, flags, NULL);
3704 lock_session(session);
3705 r = imap_threaded_append(session->folder, destfolder,
3706 file, flag_list, (int *)new_uid);
3707 mailimap_flag_list_free(flag_list);
3709 if (r != MAILIMAP_NO_ERROR) {
3710 imap_handle_error(SESSION(session), r);
3711 debug_print("append err %d\n", r);
3715 unlock_session(session);
3717 return MAILIMAP_NO_ERROR;
3720 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3721 const gchar *destfolder, GRelation *uid_mapping,
3722 struct mailimap_set **source, struct mailimap_set **dest)
3726 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3727 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3728 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3730 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3731 if (r != MAILIMAP_NO_ERROR) {
3732 imap_handle_error(SESSION(session), r);
3736 return MAILIMAP_NO_ERROR;
3739 static gint imap_cmd_store(IMAPSession *session,
3740 IMAPFolderItem *item,
3741 struct mailimap_set * set,
3742 IMAPFlags flags, GSList *tags, int do_add)
3745 struct mailimap_flag_list * flag_list = NULL;
3746 struct mailimap_store_att_flags * store_att_flags;
3748 flag_list = imap_flag_to_lep(item, flags, tags);
3752 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3755 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3757 r = imap_threaded_store(session->folder, set, store_att_flags);
3758 mailimap_store_att_flags_free(store_att_flags);
3759 if (r != MAILIMAP_NO_ERROR) {
3760 imap_handle_error(SESSION(session), r);
3764 return MAILIMAP_NO_ERROR;
3767 static gint imap_cmd_expunge(IMAPSession *session)
3771 if (prefs_common.work_offline &&
3772 !inc_offline_should_override(FALSE,
3773 _("Claws Mail needs network access in order "
3774 "to access the IMAP server."))) {
3778 r = imap_threaded_expunge(session->folder);
3779 if (r != MAILIMAP_NO_ERROR) {
3780 imap_handle_error(SESSION(session), r);
3784 return MAILIMAP_NO_ERROR;
3787 static void imap_path_separator_subst(gchar *str, gchar separator)
3790 gboolean in_escape = FALSE;
3792 if (!separator || separator == '/') return;
3794 for (p = str; *p != '\0'; p++) {
3795 if (*p == '/' && !in_escape)
3797 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3799 else if (*p == '-' && in_escape)
3804 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3806 FolderItem *item = node->data;
3807 gchar **paths = data;
3808 const gchar *oldpath = paths[0];
3809 const gchar *newpath = paths[1];
3810 gchar *real_oldpath, *real_newpath;
3812 gchar *new_itempath;
3814 IMAPSession *session = imap_session_get(item->folder);
3816 oldpathlen = strlen(oldpath);
3817 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3818 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3822 base = item->path + oldpathlen;
3823 while (*base == G_DIR_SEPARATOR) base++;
3825 new_itempath = g_strdup(newpath);
3827 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3830 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3832 item->path = new_itempath;
3834 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path, &ok);
3836 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3837 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3839 g_free(real_oldpath);
3840 g_free(real_newpath);
3844 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3846 GSList *uidlist, *elem;
3848 clist * lep_uidlist;
3849 gint ok, nummsgs = 0, lastuid_old;
3851 if (session == NULL) {
3855 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3856 NULL, NULL, NULL, NULL, NULL, TRUE);
3857 if (ok != MAILIMAP_NO_ERROR) {
3861 g_slist_free(item->uid_list);
3862 item->uid_list = NULL;
3866 if (folder->account && folder->account->low_bandwidth) {
3867 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3871 if (r == MAILIMAP_NO_ERROR) {
3872 GSList * fetchuid_list =
3873 imap_uid_list_from_lep(lep_uidlist);
3874 mailimap_search_result_free(lep_uidlist);
3876 uidlist = g_slist_concat(fetchuid_list, uidlist);
3878 carray * lep_uidtab;
3879 if (r != -1) { /* inited */
3880 imap_handle_error(SESSION(session), r);
3884 r = imap_threaded_fetch_uid(folder, 1,
3886 if (r == MAILIMAP_NO_ERROR) {
3887 GSList * fetchuid_list =
3888 imap_uid_list_from_lep_tab(lep_uidtab);
3889 imap_fetch_uid_list_free(lep_uidtab);
3890 uidlist = g_slist_concat(fetchuid_list, uidlist);
3894 if (r != MAILIMAP_NO_ERROR) {
3895 imap_handle_error(SESSION(session), r);
3899 lastuid_old = item->lastuid;
3901 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3904 msgnum = GPOINTER_TO_INT(elem->data);
3906 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3907 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3910 g_slist_free(uidlist);
3912 unlock_session(session); /* locked from imap_get_num_list */
3918 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3920 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3921 IMAPSession *session;
3923 GSList *uidlist = NULL;
3925 gboolean selected_folder;
3926 gint known_list_len = 0;
3927 debug_print("get_num_list\n");
3929 g_return_val_if_fail(folder != NULL, -1);
3930 g_return_val_if_fail(item != NULL, -1);
3931 g_return_val_if_fail(item->item.path != NULL, -1);
3932 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3933 g_return_val_if_fail(folder->account != NULL, -1);
3935 known_list_len = g_slist_length(item->uid_list);
3936 if (!item->should_update) {
3937 debug_print("get_num_list: nothing to update\n");
3938 *old_uids_valid = TRUE;
3939 if (known_list_len == item->item.total_msgs
3940 && known_list_len > 0) {
3941 *msgnum_list = g_slist_copy(item->uid_list);
3942 return known_list_len;
3944 debug_print("don't know the list length...\n");
3948 if (prefs_common.work_offline &&
3949 !inc_offline_should_override(FALSE,
3950 _("Claws Mail needs network access in order "
3951 "to access the IMAP server."))) {
3955 debug_print("getting session...\n");
3956 session = imap_session_get(folder);
3957 g_return_val_if_fail(session != NULL, -1);
3959 lock_session(session); /* unlocked by get_list_of_uids */
3960 if (FOLDER_ITEM(item)->path)
3961 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3962 FOLDER_ITEM(item)->folder->name,
3964 FOLDER_ITEM(item)->path);
3966 statusbar_print_all(_("Scanning folder %s ..."),
3967 FOLDER_ITEM(item)->folder->name);
3969 selected_folder = (session->mbox != NULL) &&
3970 (!strcmp(session->mbox, item->item.path));
3972 if (item->should_trash_cache) {
3973 *old_uids_valid = FALSE;
3974 debug_print("get_num_list: trashing num list\n");
3975 debug_print("Freeing imap uid cache\n");
3977 g_slist_free(item->uid_list);
3978 item->uid_list = NULL;
3980 imap_delete_all_cached_messages((FolderItem *)item);
3982 debug_print("get_num_list: updating num list\n");
3983 *old_uids_valid = TRUE;
3986 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3987 /* session could be broken now, in case of fatal error */
3989 debug_print("get_num_list: got %d msgs\n", nummsgs);
3992 statusbar_pop_all();
3996 *msgnum_list = uidlist;
3998 dir = folder_item_get_path((FolderItem *)item);
3999 debug_print("removing old messages from %s\n", dir);
4000 remove_numbered_files_not_in_list(dir, *msgnum_list);
4003 debug_print("get_num_list - ok - %i\n", nummsgs);
4004 statusbar_pop_all();
4005 item->should_trash_cache = FALSE;
4006 item->should_update = FALSE;
4010 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
4015 flags.perm_flags = MSG_NEW|MSG_UNREAD;
4016 flags.tmp_flags = 0;
4018 g_return_val_if_fail(item != NULL, NULL);
4019 g_return_val_if_fail(file != NULL, NULL);
4021 if (folder_has_parent_of_type(item, F_QUEUE)) {
4022 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
4023 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
4024 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
4027 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
4028 if (!msginfo) return NULL;
4030 msginfo->plaintext_file = g_strdup(file);
4031 msginfo->folder = item;
4036 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
4037 GSList *msgnum_list)
4039 IMAPSession *session;
4040 MsgInfoList *ret = NULL;
4043 debug_print("get_msginfos\n");
4045 g_return_val_if_fail(folder != NULL, NULL);
4046 g_return_val_if_fail(item != NULL, NULL);
4047 g_return_val_if_fail(msgnum_list != NULL, NULL);
4049 debug_print("getting session...\n");
4050 session = imap_session_get(folder);
4051 g_return_val_if_fail(session != NULL, NULL);
4053 lock_session(session); /* unlocked later in the function */
4055 debug_print("IMAP getting msginfos\n");
4056 ok = imap_select(session, IMAP_FOLDER(folder), item,
4057 NULL, NULL, NULL, NULL, NULL, FALSE);
4058 if (ok != MAILIMAP_NO_ERROR) {
4061 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4062 folder_has_parent_of_type(item, F_QUEUE))) {
4063 ret = g_slist_concat(ret,
4064 imap_get_uncached_messages(session, item,
4066 if (ok != MAILIMAP_NO_ERROR)
4068 unlock_session(session);
4070 MsgNumberList *sorted_list, *elem, *llast = NULL;
4071 gint startnum, lastnum;
4073 unlock_session(session);
4075 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4077 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4079 llast = g_slist_last(ret);
4080 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4084 num = GPOINTER_TO_INT(elem->data);
4086 if (num > lastnum + 1 || elem == NULL) {
4088 for (i = startnum; i <= lastnum; ++i) {
4090 file = imap_fetch_msg(folder, item, i);
4092 MsgInfo *msginfo = imap_parse_msg(file, item);
4093 if (msginfo != NULL) {
4094 msginfo->msgnum = i;
4096 llast = ret = g_slist_append(ret, msginfo);
4098 llast = g_slist_append(llast, msginfo);
4099 llast = llast->next;
4114 g_slist_free(sorted_list);
4119 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4121 MsgInfo *msginfo = NULL;
4122 MsgInfoList *msginfolist;
4123 MsgNumberList numlist;
4125 numlist.next = NULL;
4126 numlist.data = GINT_TO_POINTER(uid);
4128 msginfolist = imap_get_msginfos(folder, item, &numlist);
4129 if (msginfolist != NULL) {
4130 msginfo = msginfolist->data;
4131 g_slist_free(msginfolist);
4137 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4139 IMAPSession *session;
4140 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4141 gint ok, exists = 0, unseen = 0;
4142 guint32 uid_next = 0, uid_val = 0;
4143 gboolean selected_folder;
4145 g_return_val_if_fail(folder != NULL, FALSE);
4146 g_return_val_if_fail(item != NULL, FALSE);
4147 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4148 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4150 if (item->item.path == NULL)
4153 if (item->should_update) {
4154 debug_print("scan already required\n");
4157 debug_print("getting session...\n");
4158 session = imap_session_get(folder);
4160 g_return_val_if_fail(session != NULL, FALSE);
4161 lock_session(session); /* unlocked later in the function */
4163 selected_folder = (session->mbox != NULL) &&
4164 (!strcmp(session->mbox, item->item.path));
4165 if (selected_folder) {
4166 if (!session->folder_content_changed) {
4167 ok = imap_cmd_noop(session);
4168 if (ok != MAILIMAP_NO_ERROR) {
4169 debug_print("disconnected!\n");
4171 session = imap_reconnect_if_possible(folder, session);
4173 session = imap_session_get(folder);
4174 if (session == NULL)
4178 if (session->folder_content_changed) {
4179 debug_print("CHANGED (self-noop)! scan_required\n");
4180 item->should_update = TRUE;
4181 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4182 item->item.mtime = session->uid_validity;
4183 item->should_trash_cache = TRUE;
4185 unlock_session(session);
4189 debug_print("CHANGED (previous noop)! scan_required\n");
4190 item->should_update = TRUE;
4191 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4192 item->item.mtime = session->uid_validity;
4193 item->should_trash_cache = TRUE;
4195 unlock_session(session);
4199 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4200 &exists, &uid_next, &uid_val, &unseen, FALSE);
4201 if (ok != MAILIMAP_NO_ERROR) {
4205 debug_print("exists %d, item->item.total_msgs %d\n",
4206 exists, item->item.total_msgs);
4207 if (exists != item->item.total_msgs
4208 || unseen != item->item.unread_msgs
4209 || uid_next != item->uid_next
4210 || uid_val != item->item.mtime) {
4211 debug_print("CHANGED (status)! scan_required\n");
4212 item->last_change = time(NULL);
4213 item->should_update = TRUE;
4214 item->uid_next = uid_next;
4215 if (uid_val != item->item.mtime) {
4216 item->item.mtime = uid_val;
4217 item->should_trash_cache = TRUE;
4219 unlock_session(session);
4223 unlock_session(session);
4225 item->should_update = FALSE;
4229 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4231 IMAPSession *session;
4232 IMAPFlags flags_set = 0, flags_unset = 0;
4233 gint ok = MAILIMAP_NO_ERROR;
4234 MsgNumberList numlist;
4235 hashtable_data *ht_data = NULL;
4237 g_return_if_fail(folder != NULL);
4238 g_return_if_fail(folder->klass == &imap_class);
4239 g_return_if_fail(item != NULL);
4240 g_return_if_fail(item->folder == folder);
4241 g_return_if_fail(msginfo != NULL);
4242 g_return_if_fail(msginfo->folder == item);
4244 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4245 flags_set |= IMAP_FLAG_FLAGGED;
4246 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4247 flags_unset |= IMAP_FLAG_FLAGGED;
4249 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4250 flags_unset |= IMAP_FLAG_SEEN;
4251 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4252 flags_set |= IMAP_FLAG_SEEN;
4254 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4255 flags_set |= IMAP_FLAG_ANSWERED;
4256 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4257 flags_unset |= IMAP_FLAG_ANSWERED;
4259 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4260 flags_set |= IMAP_FLAG_FORWARDED;
4261 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4262 flags_unset |= IMAP_FLAG_FORWARDED;
4264 if (!MSG_IS_SPAM(msginfo->flags) && (newflags & MSG_SPAM)) {
4265 flags_set |= IMAP_FLAG_SPAM;
4266 flags_unset |= IMAP_FLAG_HAM;
4268 if ( MSG_IS_SPAM(msginfo->flags) && !(newflags & MSG_SPAM)) {
4269 flags_set |= IMAP_FLAG_HAM;
4270 flags_unset |= IMAP_FLAG_SPAM;
4272 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4273 flags_set |= IMAP_FLAG_DELETED;
4274 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4275 flags_unset |= IMAP_FLAG_DELETED;
4277 if (!flags_set && !flags_unset) {
4278 /* the changed flags were not translatable to IMAP-speak.
4279 * like MSG_POSTFILTERED, so just apply. */
4280 msginfo->flags.perm_flags = newflags;
4284 debug_print("getting session...\n");
4285 session = imap_session_get(folder);
4290 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4291 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4294 numlist.next = NULL;
4295 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4297 if (IMAP_FOLDER_ITEM(item)->batching) {
4298 /* instead of performing an UID STORE command for each message change,
4299 * as a lot of them can change "together", we just fill in hashtables
4300 * and defer the treatment so that we're able to send only one
4303 debug_print("IMAP batch mode on, deferring flags change\n");
4305 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4306 GINT_TO_POINTER(flags_set));
4307 if (ht_data == NULL) {
4308 ht_data = g_new0(hashtable_data, 1);
4309 ht_data->item = IMAP_FOLDER_ITEM(item);
4310 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4311 GINT_TO_POINTER(flags_set), ht_data);
4313 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4316 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4317 GINT_TO_POINTER(flags_unset));
4318 if (ht_data == NULL) {
4319 ht_data = g_new0(hashtable_data, 1);
4320 ht_data->item = IMAP_FOLDER_ITEM(item);
4321 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4322 GINT_TO_POINTER(flags_unset), ht_data);
4324 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4325 GINT_TO_POINTER(msginfo->msgnum));
4328 debug_print("IMAP changing flags\n");
4330 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4331 if (ok != MAILIMAP_NO_ERROR) {
4337 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4338 if (ok != MAILIMAP_NO_ERROR) {
4343 msginfo->flags.perm_flags = newflags;
4347 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4350 IMAPSession *session;
4352 MsgNumberList numlist;
4354 g_return_val_if_fail(folder != NULL, -1);
4355 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4356 g_return_val_if_fail(item != NULL, -1);
4358 debug_print("getting session...\n");
4359 session = imap_session_get(folder);
4360 if (!session) return -1;
4362 ok = imap_select(session, IMAP_FOLDER(folder), item,
4363 NULL, NULL, NULL, NULL, NULL, FALSE);
4364 if (ok != MAILIMAP_NO_ERROR) {
4367 numlist.next = NULL;
4368 numlist.data = GINT_TO_POINTER(uid);
4370 ok = imap_set_message_flags
4371 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4372 if (ok != MAILIMAP_NO_ERROR) {
4373 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4377 if (!session->uidplus) {
4378 ok = imap_cmd_expunge(session);
4382 uidstr = g_strdup_printf("%u", uid);
4383 ok = imap_cmd_expunge(session);
4386 if (ok != MAILIMAP_NO_ERROR) {
4387 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4391 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4392 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4393 dir = folder_item_get_path(item);
4394 if (is_dir_exist(dir))
4395 remove_numbered_files(dir, uid, uid);
4397 return MAILIMAP_NO_ERROR;
4400 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4402 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4405 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4409 g_return_val_if_fail(list != NULL, -1);
4411 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4412 if (GPOINTER_TO_INT(elem->data) >= num)
4415 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4418 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4420 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4421 debug_print("flag %d is OK\n", flag);
4424 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4425 debug_print("creating flags is OK\n");
4432 * NEW and DELETED flags are not syncronized
4433 * - The NEW/RECENT flags in IMAP folders can not really be directly
4434 * modified by Sylpheed
4435 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4436 * meaning, in IMAP it always removes the messages from the FolderItem
4437 * in Sylpheed it can mean to move the message to trash
4440 typedef struct _get_flags_data {
4443 MsgInfoList *msginfo_list;
4444 GRelation *msgflags;
4445 gboolean full_search;
4449 static /*gint*/ void *imap_get_flags_thread(void *data)
4451 get_flags_data *stuff = (get_flags_data *)data;
4452 Folder *folder = stuff->folder;
4453 FolderItem *fitem = (FolderItem *) stuff->item;
4454 MsgInfoList *msginfo_list = stuff->msginfo_list;
4455 GRelation *msgflags = stuff->msgflags;
4457 carray * lep_uidtab;
4458 IMAPSession *session;
4460 int r = MAILIMAP_NO_ERROR;
4461 GHashTable *flags_hash = NULL;
4462 GHashTable *tags_hash = NULL;
4463 gboolean full_search = stuff->full_search;
4464 GSList *sorted_list = NULL;
4465 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL, *spam = NULL;
4466 GSList *seq_list, *cur;
4467 gboolean reverse_seen = FALSE;
4468 gboolean selected_folder;
4469 gint exists_cnt, unseen_cnt;
4470 gboolean got_alien_tags = FALSE;
4472 session = imap_session_get(folder);
4474 if (session == NULL) {
4476 return GINT_TO_POINTER(-1);
4478 selected_folder = (session->mbox != NULL) &&
4479 (!strcmp(session->mbox, fitem->path));
4481 lock_session(session);
4482 if (!selected_folder) {
4483 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4484 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4485 if (ok != MAILIMAP_NO_ERROR) {
4487 return GINT_TO_POINTER(-1);
4490 if (unseen_cnt > exists_cnt / 2)
4491 reverse_seen = TRUE;
4494 if (fitem->unread_msgs > fitem->total_msgs / 2)
4495 reverse_seen = TRUE;
4498 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4500 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4502 struct mailimap_set * set;
4503 set = mailimap_set_new_interval(1, 0);
4504 seq_list = g_slist_append(NULL, set);
4507 if (folder->account && folder->account->low_bandwidth) {
4508 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4509 struct mailimap_set * imapset;
4510 clist * lep_uidlist;
4513 imapset = cur->data;
4515 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4516 full_search ? NULL:imapset, &lep_uidlist);
4519 r = imap_threaded_search(folder,
4520 IMAP_SEARCH_TYPE_UNSEEN,
4521 full_search ? NULL:imapset, &lep_uidlist);
4523 if (r == MAILIMAP_NO_ERROR) {
4526 uidlist = imap_uid_list_from_lep(lep_uidlist);
4527 mailimap_search_result_free(lep_uidlist);
4529 unseen = g_slist_concat(unseen, uidlist);
4531 imap_handle_error(SESSION(session), r);
4535 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4536 full_search ? NULL:imapset, &lep_uidlist);
4537 if (r == MAILIMAP_NO_ERROR) {
4540 uidlist = imap_uid_list_from_lep(lep_uidlist);
4541 mailimap_search_result_free(lep_uidlist);
4543 flagged = g_slist_concat(flagged, uidlist);
4545 imap_handle_error(SESSION(session), r);
4549 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4550 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4551 full_search ? NULL:imapset, &lep_uidlist);
4552 if (r == MAILIMAP_NO_ERROR) {
4555 uidlist = imap_uid_list_from_lep(lep_uidlist);
4556 mailimap_search_result_free(lep_uidlist);
4558 answered = g_slist_concat(answered, uidlist);
4560 imap_handle_error(SESSION(session), r);
4564 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4565 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4566 full_search ? NULL:imapset, &lep_uidlist);
4567 if (r == MAILIMAP_NO_ERROR) {
4570 uidlist = imap_uid_list_from_lep(lep_uidlist);
4571 mailimap_search_result_free(lep_uidlist);
4573 forwarded = g_slist_concat(forwarded, uidlist);
4575 imap_handle_error(SESSION(session), r);
4580 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_SPAM)) {
4581 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SPAM,
4582 full_search ? NULL:imapset, &lep_uidlist);
4583 if (r == MAILIMAP_NO_ERROR) {
4586 uidlist = imap_uid_list_from_lep(lep_uidlist);
4587 mailimap_search_result_free(lep_uidlist);
4589 spam = g_slist_concat(spam, uidlist);
4591 imap_handle_error(SESSION(session), r);
4596 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4597 full_search ? NULL:imapset, &lep_uidlist);
4598 if (r == MAILIMAP_NO_ERROR) {
4601 uidlist = imap_uid_list_from_lep(lep_uidlist);
4602 mailimap_search_result_free(lep_uidlist);
4604 deleted = g_slist_concat(deleted, uidlist);
4606 imap_handle_error(SESSION(session), r);
4613 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4614 if (r == MAILIMAP_NO_ERROR) {
4615 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4616 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4617 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4618 imap_fetch_uid_flags_list_free(lep_uidtab);
4620 imap_handle_error(SESSION(session), r);
4626 if (r == MAILIMAP_NO_ERROR)
4627 unlock_session(session);
4629 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4631 MsgPermFlags flags, oldflags;
4634 msginfo = (MsgInfo *) elem->data;
4635 flags = msginfo->flags.perm_flags;
4636 wasnew = (flags & MSG_NEW);
4637 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED|MSG_SPAM);
4639 if (folder->account && folder->account->low_bandwidth) {
4640 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4641 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED | MSG_SPAM);
4643 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4646 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4647 if (gslist_find_next_num(&unseen, msginfo->msgnum) == msginfo->msgnum) {
4648 if (!reverse_seen) {
4649 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4651 flags &= ~(MSG_UNREAD | MSG_NEW);
4655 if (gslist_find_next_num(&flagged, msginfo->msgnum) == msginfo->msgnum)
4656 flags |= MSG_MARKED;
4658 flags &= ~MSG_MARKED;
4660 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4661 if (gslist_find_next_num(&answered, msginfo->msgnum) == msginfo->msgnum)
4662 flags |= MSG_REPLIED;
4664 flags &= ~MSG_REPLIED;
4665 if (gslist_find_next_num(&forwarded, msginfo->msgnum) == msginfo->msgnum)
4666 flags |= MSG_FORWARDED;
4668 flags &= ~MSG_FORWARDED;
4669 if (gslist_find_next_num(&spam, msginfo->msgnum) == msginfo->msgnum)
4673 if (gslist_find_next_num(&deleted, msginfo->msgnum) == msginfo->msgnum)
4674 flags |= MSG_DELETED;
4676 flags &= ~MSG_DELETED;
4679 if (flags_hash != NULL) {
4681 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4682 GINT_TO_POINTER(msginfo->msgnum)));
4685 if ((flags & MSG_UNREAD) == 0)
4691 if (tags_hash != NULL) {
4692 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4696 g_slist_free(msginfo->tags);
4697 msginfo->tags = NULL;
4699 for (cur = tags; cur; cur = cur->next) {
4700 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4702 id = tags_get_id_for_str(real_tag);
4704 id = tags_add_tag(real_tag);
4705 got_alien_tags = TRUE;
4707 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4708 msginfo->tags = g_slist_append(
4710 GINT_TO_POINTER(id));
4714 slist_free_strings(tags);
4719 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4722 if (got_alien_tags) {
4724 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4728 g_hash_table_destroy(flags_hash);
4730 g_hash_table_destroy(tags_hash);
4732 imap_lep_set_free(seq_list);
4733 g_slist_free(flagged);
4734 g_slist_free(deleted);
4735 g_slist_free(answered);
4736 g_slist_free(forwarded);
4738 g_slist_free(unseen);
4739 g_slist_free(sorted_list);
4742 return GINT_TO_POINTER(0);
4745 static gint imap_get_flags(Folder *folder, FolderItem *item,
4746 MsgInfoList *msginfo_list, GRelation *msgflags)
4749 get_flags_data *data = g_new0(get_flags_data, 1);
4751 data->folder = folder;
4753 data->msginfo_list = msginfo_list;
4754 data->msgflags = msgflags;
4755 data->full_search = FALSE;
4757 GSList *tmp = NULL, *cur;
4759 if (prefs_common.work_offline &&
4760 !inc_offline_should_override(FALSE,
4761 _("Claws Mail needs network access in order "
4762 "to access the IMAP server."))) {
4767 tmp = folder_item_get_msg_list(item);
4769 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4770 data->full_search = TRUE;
4772 for (cur = tmp; cur; cur = cur->next)
4773 procmsg_msginfo_free((MsgInfo *)cur->data);
4777 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4784 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4786 gboolean flags_set = GPOINTER_TO_INT(user_data);
4787 gint flags_value = GPOINTER_TO_INT(key);
4788 hashtable_data *data = (hashtable_data *)value;
4789 IMAPFolderItem *_item = data->item;
4790 FolderItem *item = (FolderItem *)_item;
4791 gint ok = MAILIMAP_ERROR_BAD_STATE;
4792 IMAPSession *session = NULL;
4794 debug_print("getting session...\n");
4795 session = imap_session_get(item->folder);
4797 data->msglist = g_slist_reverse(data->msglist);
4799 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4802 g_slist_length(data->msglist));
4804 lock_session(session);
4806 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4807 NULL, NULL, NULL, NULL, NULL, FALSE);
4809 if (ok == MAILIMAP_NO_ERROR) {
4810 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4811 data->msglist, flags_value, NULL, flags_set);
4813 g_warning("can't select mailbox %s\n", item->path);
4817 unlock_session(session);
4819 g_slist_free(data->msglist);
4824 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4826 gboolean tags_set = GPOINTER_TO_INT(user_data);
4827 TagsData *data = (TagsData *)value;
4828 IMAPFolderItem *_item = data->item;
4829 FolderItem *item = (FolderItem *)_item;
4830 gchar *str = data->str;
4831 gint ok = MAILIMAP_ERROR_BAD_STATE;
4832 IMAPSession *session = NULL;
4834 debug_print("getting session...\n");
4835 session = imap_session_get(item->folder);
4837 data->msglist = g_slist_reverse(data->msglist);
4839 debug_print("IMAP %ssetting tags %s for %d messages\n",
4842 g_slist_length(data->msglist));
4844 lock_session(session);
4846 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4847 NULL, NULL, NULL, NULL, NULL, FALSE);
4849 if (ok == MAILIMAP_NO_ERROR) {
4853 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4854 data->msglist, 0, &list, tags_set);
4856 g_warning("can't select mailbox %s\n", item->path);
4860 unlock_session(session);
4862 g_slist_free(data->msglist);
4868 static void process_hashtable(IMAPFolderItem *item)
4870 if (item->flags_set_table) {
4871 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4872 g_hash_table_destroy(item->flags_set_table);
4873 item->flags_set_table = NULL;
4875 if (item->flags_unset_table) {
4876 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4877 g_hash_table_destroy(item->flags_unset_table);
4878 item->flags_unset_table = NULL;
4880 if (item->tags_set_table) {
4881 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4882 g_hash_table_destroy(item->tags_set_table);
4883 item->tags_set_table = NULL;
4885 if (item->tags_unset_table) {
4886 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4887 g_hash_table_destroy(item->tags_unset_table);
4888 item->tags_unset_table = NULL;
4893 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4895 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4896 IMAPSession *session;
4898 g_return_if_fail(item != NULL);
4900 if (item->batching == batch)
4904 item->batching = TRUE;
4905 debug_print("IMAP switching to batch mode\n");
4906 if (!item->flags_set_table) {
4907 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4909 if (!item->flags_unset_table) {
4910 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4912 if (!item->tags_set_table) {
4913 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4915 if (!item->tags_unset_table) {
4916 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4918 session = imap_session_get(folder);
4920 imap_refresh_sensitivity(session);
4921 session->sens_update_block = TRUE;
4924 debug_print("IMAP switching away from batch mode\n");
4926 process_hashtable(item);
4927 item->batching = FALSE;
4928 session = imap_session_get(folder);
4930 session->sens_update_block = FALSE;
4931 imap_refresh_sensitivity(session);
4938 /* data types conversion libetpan <-> claws */
4942 #define ETPAN_IMAP_MB_MARKED 1
4943 #define ETPAN_IMAP_MB_UNMARKED 2
4944 #define ETPAN_IMAP_MB_NOSELECT 4
4945 #define ETPAN_IMAP_MB_NOINFERIORS 8
4947 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4953 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4954 switch (imap_flags->mbf_sflag) {
4955 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4956 flags |= ETPAN_IMAP_MB_MARKED;
4958 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4959 flags |= ETPAN_IMAP_MB_NOSELECT;
4961 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4962 flags |= ETPAN_IMAP_MB_UNMARKED;
4967 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4968 cur = clist_next(cur)) {
4969 struct mailimap_mbx_list_oflag * oflag;
4971 oflag = clist_content(cur);
4973 switch (oflag->of_type) {
4974 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4975 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4983 static GSList * imap_list_from_lep(IMAPFolder * folder,
4984 clist * list, const gchar * real_path, gboolean all)
4987 GSList * item_list = NULL, *llast = NULL;
4989 for(iter = clist_begin(list) ; iter != NULL ;
4990 iter = clist_next(iter)) {
4991 struct mailimap_mailbox_list * mb;
4999 FolderItem *new_item;
5001 mb = clist_content(iter);
5007 if (mb->mb_flag != NULL)
5008 flags = imap_flags_to_flags(mb->mb_flag);
5010 delimiter = mb->mb_delimiter;
5013 dup_name = strdup(name);
5014 if (delimiter != '\0')
5015 subst_char(dup_name, delimiter, '/');
5017 base = g_path_get_basename(dup_name);
5018 if (base[0] == '.') {
5023 if (!all && path_cmp(name, real_path) == 0) {
5029 if (!all && dup_name[strlen(dup_name)-1] == '/') {
5030 dup_name[strlen(dup_name)-1] = '\0';
5033 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
5034 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
5036 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
5037 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
5038 new_item->no_sub = TRUE;
5039 if (strcmp(dup_name, "INBOX") != 0 &&
5040 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
5041 new_item->no_select = TRUE;
5043 if (item_list == NULL)
5044 llast = item_list = g_slist_append(item_list, new_item);
5046 llast = g_slist_append(llast, new_item);
5047 llast = llast->next;
5049 debug_print("folder '%s' found.\n", loc_path);
5060 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
5062 GSList *sorted_list, *cur;
5063 guint first, last, next;
5064 GSList *ret_list = NULL, *llast = NULL;
5065 struct mailimap_set * current_set;
5066 unsigned int item_count;
5068 if (numlist == NULL)
5071 current_set = mailimap_set_new_empty();
5073 sorted_list = g_slist_copy(numlist);
5074 sorted_list = g_slist_sort(sorted_list, g_int_compare);
5076 first = GPOINTER_TO_INT(sorted_list->data);
5079 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
5080 if (GPOINTER_TO_INT(cur->data) == 0)
5085 last = GPOINTER_TO_INT(cur->data);
5087 next = GPOINTER_TO_INT(cur->next->data);
5091 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5093 struct mailimap_set_item * item;
5094 item = mailimap_set_item_new(first, last);
5095 mailimap_set_add(current_set, item);
5099 if (item_count >= folder->max_set_size) {
5100 if (ret_list == NULL)
5101 llast = ret_list = g_slist_append(ret_list,
5104 llast = g_slist_append(llast, current_set);
5105 llast = llast->next;
5108 current_set = mailimap_set_new_empty();
5114 if (clist_count(current_set->set_list) > 0) {
5115 ret_list = g_slist_append(ret_list,
5119 g_slist_free(sorted_list);
5124 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5126 MsgNumberList *numlist = NULL;
5130 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5131 MsgInfo *msginfo = (MsgInfo *) cur->data;
5133 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5135 numlist = g_slist_reverse(numlist);
5136 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5137 g_slist_free(numlist);
5142 static GSList * imap_uid_list_from_lep(clist * list)
5149 for(iter = clist_begin(list) ; iter != NULL ;
5150 iter = clist_next(iter)) {
5153 puid = clist_content(iter);
5154 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5157 result = g_slist_reverse(result);
5161 static GSList * imap_uid_list_from_lep_tab(carray * list)
5168 for(i = 0 ; i < carray_count(list) ; i ++) {
5171 puid = carray_get(list, i);
5172 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5174 result = g_slist_reverse(result);
5178 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5180 GHashTable * tags_hash)
5187 for(i = 0 ; i < carray_count(list) ; i += 3) {
5192 puid = carray_get(list, i);
5193 pflags = carray_get(list, i + 1);
5194 tags = carray_get(list, i + 2);
5196 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5197 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5201 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5204 MsgInfo *msginfo = NULL;
5207 MsgFlags flags = {0, 0};
5209 if (info->headers == NULL)
5212 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5213 if (folder_has_parent_of_type(item, F_QUEUE)) {
5214 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5215 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5216 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5218 flags.perm_flags = info->flags;
5221 size = (goffset) info->size;
5222 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5225 msginfo->msgnum = uid;
5226 msginfo->size = size;
5232 static void imap_lep_set_free(GSList *seq_list)
5236 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5237 struct mailimap_set * imapset;
5239 imapset = cur->data;
5240 mailimap_set_free(imapset);
5242 g_slist_free(seq_list);
5245 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5247 struct mailimap_flag_list * flag_list;
5250 flag_list = mailimap_flag_list_new_empty();
5252 if (IMAP_IS_SEEN(flags))
5253 mailimap_flag_list_add(flag_list,
5254 mailimap_flag_new_seen());
5255 if (IMAP_IS_ANSWERED(flags))
5256 mailimap_flag_list_add(flag_list,
5257 mailimap_flag_new_answered());
5258 if (IMAP_IS_FLAGGED(flags))
5259 mailimap_flag_list_add(flag_list,
5260 mailimap_flag_new_flagged());
5261 if (IMAP_IS_DELETED(flags))
5262 mailimap_flag_list_add(flag_list,
5263 mailimap_flag_new_deleted());
5264 if (IMAP_IS_DRAFT(flags))
5265 mailimap_flag_list_add(flag_list,
5266 mailimap_flag_new_draft());
5267 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5268 mailimap_flag_list_add(flag_list,
5269 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5270 if (IMAP_IS_SPAM(flags) && flag_ok(item, IMAP_FLAG_SPAM))
5271 mailimap_flag_list_add(flag_list,
5272 mailimap_flag_new_flag_keyword(strdup("Junk")));
5273 else if (IMAP_IS_HAM(flags) && flag_ok(item, IMAP_FLAG_HAM))
5274 mailimap_flag_list_add(flag_list,
5275 mailimap_flag_new_flag_keyword(strdup("NonJunk")));
5277 for (; cur; cur = cur->next) {
5279 imap_utf8_to_modified_utf7(cur->data, TRUE);
5280 g_strstrip(enc_str);
5282 mailimap_flag_list_add(flag_list,
5283 mailimap_flag_new_flag_keyword(enc_str));
5289 guint imap_folder_get_refcnt(Folder *folder)
5291 return ((IMAPFolder *)folder)->refcnt;
5294 void imap_folder_ref(Folder *folder)
5296 ((IMAPFolder *)folder)->refcnt++;
5299 void imap_disconnect_all(void)
5302 gboolean short_timeout;
5303 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5307 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5309 short_timeout = !networkmanager_is_online(&error);
5311 short_timeout = TRUE;
5312 g_error_free(error);
5315 short_timeout = TRUE;
5319 imap_main_set_timeout(1);
5321 for (list = account_get_list(); list != NULL; list = list->next) {
5322 PrefsAccount *account = list->data;
5323 if (account->protocol == A_IMAP4) {
5324 RemoteFolder *folder = (RemoteFolder *)account->folder;
5325 if (folder && folder->session) {
5326 IMAPSession *session = (IMAPSession *)folder->session;
5327 imap_threaded_disconnect(FOLDER(folder));
5328 SESSION(session)->state = SESSION_DISCONNECTED;
5329 SESSION(session)->sock = NULL;
5330 session_destroy(SESSION(session));
5331 folder->session = NULL;
5337 imap_main_set_timeout(prefs_common.io_timeout_secs);
5340 void imap_folder_unref(Folder *folder)
5342 if (((IMAPFolder *)folder)->refcnt > 0)
5343 ((IMAPFolder *)folder)->refcnt--;
5346 void imap_cancel_all(void)
5351 folderlist = folder_get_list();
5352 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5353 Folder *folder = (Folder *) cur->data;
5355 if (folder->klass == &imap_class) {
5356 if (imap_is_busy(folder)) {
5357 IMAPSession *imap_session;
5358 RemoteFolder *rfolder;
5360 g_printerr("cancelled\n");
5361 imap_threaded_cancel(folder);
5362 rfolder = (RemoteFolder *) folder;
5363 imap_session = (IMAPSession *) rfolder->session;
5365 imap_session->cancelled = 1;
5371 gboolean imap_cancel_all_enabled(void)
5376 folderlist = folder_get_list();
5377 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5378 Folder *folder = (Folder *) cur->data;
5380 if (folder->klass == &imap_class) {
5381 if (imap_is_busy(folder)) {
5390 static gboolean imap_is_busy(Folder *folder)
5392 IMAPSession *imap_session;
5393 RemoteFolder *rfolder;
5395 rfolder = (RemoteFolder *) folder;
5396 imap_session = (IMAPSession *) rfolder->session;
5397 if (imap_session == NULL)
5400 return imap_session->busy;
5403 #else /* HAVE_LIBETPAN */
5405 static FolderClass imap_class;
5407 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5408 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5410 static Folder *imap_folder_new (const gchar *name,
5413 static gboolean missing_imap_warning = TRUE;
5414 if (missing_imap_warning) {
5415 missing_imap_warning = FALSE;
5417 _("You have one or more IMAP accounts "
5418 "defined. However this version of "
5419 "Claws Mail has been built without "
5420 "IMAP support; your IMAP account(s) are "
5422 "You probably need to "
5423 "install libetpan and recompile "
5428 static gint imap_create_tree (Folder *folder)
5432 static FolderItem *imap_create_folder (Folder *folder,
5438 static gint imap_rename_folder (Folder *folder,
5445 gchar imap_get_path_separator_for_item(FolderItem *item)
5450 FolderClass *imap_get_class(void)
5452 if (imap_class.idstr == NULL) {
5453 imap_class.type = F_IMAP;
5454 imap_class.idstr = "imap";
5455 imap_class.uistr = "IMAP4";
5457 imap_class.new_folder = imap_folder_new;
5458 imap_class.create_tree = imap_create_tree;
5459 imap_class.create_folder = imap_create_folder;
5460 imap_class.rename_folder = imap_rename_folder;
5462 imap_class.set_xml = folder_set_xml;
5463 imap_class.get_xml = folder_get_xml;
5464 imap_class.item_set_xml = imap_item_set_xml;
5465 imap_class.item_get_xml = imap_item_get_xml;
5466 /* nothing implemented */
5472 void imap_disconnect_all(void)
5476 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5481 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5486 void imap_cache_msg(FolderItem *item, gint msgnum)
5490 void imap_cancel_all(void)
5494 gboolean imap_cancel_all_enabled(void)
5501 #ifdef HAVE_LIBETPAN
5502 static void imap_synchronise(FolderItem *item, gint days)
5504 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5505 debug_print("%s already synced\n", item->path?item->path:item->name);
5508 debug_print("syncing %s\n", item->path?item->path:item->name);
5509 imap_gtk_synchronise(item, days);
5510 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5514 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5516 #ifdef HAVE_LIBETPAN
5519 folder_item_set_xml(folder, item, tag);
5521 #ifdef HAVE_LIBETPAN
5522 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5523 XMLAttr *attr = (XMLAttr *) cur->data;
5525 if (!attr || !attr->name || !attr->value) continue;
5526 if (!strcmp(attr->name, "uidnext"))
5527 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5528 if (!strcmp(attr->name, "last_sync"))
5529 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5530 if (!strcmp(attr->name, "last_change"))
5531 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5533 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5534 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5538 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5542 tag = folder_item_get_xml(folder, item);
5544 #ifdef HAVE_LIBETPAN
5545 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5546 IMAP_FOLDER_ITEM(item)->uid_next));
5547 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5548 IMAP_FOLDER_ITEM(item)->last_sync));
5549 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5550 IMAP_FOLDER_ITEM(item)->last_change));
5556 /* ===================================================================
5557 * UTF-7 conversion routines as in RFC 2192
5558 * ===================================================================
5559 * These two functions from:
5561 * Copyright (C) 2003-2004 Pawel Salek. */
5563 /* UTF7 modified base64 alphabet */
5564 static char base64chars[] =
5565 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5566 #define UNDEFINED 64
5568 /* UTF16 definitions */
5569 #define UTF16MASK 0x03FFUL
5570 #define UTF16SHIFT 10
5571 #define UTF16BASE 0x10000UL
5572 #define UTF16HIGHSTART 0xD800UL
5573 #define UTF16HIGHEND 0xDBFFUL
5574 #define UTF16LOSTART 0xDC00UL
5575 #define UTF16LOEND 0xDFFFUL
5578 /* Convert an IMAP mailbox to a UTF-8 string.
5579 * dst needs to have roughly 4 times the storage space of src
5580 * Hex encoding can triple the size of the input
5581 * UTF-7 can be slightly denser than UTF-8
5582 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5584 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5586 unsigned c, i, bitcount;
5587 unsigned long ucs4, utf16, bitbuf;
5588 unsigned char base64[256];
5590 char *dst, *res = malloc(2*strlen(mbox)+1);
5595 if(!dst) return NULL;
5596 /* initialize modified base64 decoding table */
5597 memset(base64, UNDEFINED, sizeof (base64));
5598 for (i = 0; i < sizeof (base64chars); ++i) {
5599 base64[(unsigned)base64chars[i]] = i;
5602 /* loop until end of string */
5603 while (*src != '\0') {
5605 /* deal with literal characters and &- */
5606 if (c != '&' || *src == '-') {
5607 /* encode literally */
5608 if (change_spaces && c == '_')
5612 /* skip over the '-' if this is an &- sequence */
5613 if (c == '&') ++src;
5615 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5619 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5621 bitbuf = (bitbuf << 6) | c;
5623 /* enough bits for a UTF-16 character? */
5624 if (bitcount >= 16) {
5626 utf16 = (bitcount ? bitbuf >> bitcount
5628 /* convert UTF16 to UCS4 */
5630 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5631 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5634 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5635 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5640 /* convert UTF-16 range of UCS4 to UTF-8 */
5641 if (ucs4 <= 0x7fUL) {
5644 } else if (ucs4 <= 0x7ffUL) {
5645 dst[0] = 0xc0 | (ucs4 >> 6);
5646 dst[1] = 0x80 | (ucs4 & 0x3f);
5648 } else if (ucs4 <= 0xffffUL) {
5649 dst[0] = 0xe0 | (ucs4 >> 12);
5650 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5651 dst[2] = 0x80 | (ucs4 & 0x3f);
5654 dst[0] = 0xf0 | (ucs4 >> 18);
5655 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5656 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5657 dst[3] = 0x80 | (ucs4 & 0x3f);
5662 /* skip over trailing '-' in modified UTF-7 encoding */
5663 if (*src == '-') ++src;
5666 /* terminate destination string */
5671 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5672 * dst should be about twice the length of src to deal with non-hex
5675 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5677 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5678 unsigned long ucs4 = 0, bitbuf = 0;
5680 /* initialize hex lookup table */
5681 char *dst, *res = malloc(2*strlen(src)+1);
5683 if(!dst) return NULL;
5689 while ((c = (unsigned char)*src) != '\0') {
5691 /* normal character? */
5692 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5693 /* switch out of UTF-7 mode */
5696 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5704 if (change_spaces && c == ' ')
5708 /* encode '&' as '&-' */
5714 /* switch to UTF-7 mode */
5719 /* Encode US-ASCII characters as themselves */
5723 } else if (utf8total) {
5724 /* save UTF8 bits into UCS4 */
5725 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5726 if (++utf8pos < utf8total) {
5734 } else if (c < 0xF0) {
5738 /* NOTE: can't convert UTF8 sequences longer than 4 */
5744 /* loop to split ucs4 into two utf16 chars if necessary */
5747 if (ucs4 >= UTF16BASE) {
5749 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5751 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5754 bitbuf = (bitbuf << 16) | ucs4;
5758 /* spew out base64 */
5759 while (bitstogo >= 6) {
5761 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5765 } while (utf16flag);
5767 /* if in UTF-7 mode, finish in ASCII */
5770 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5774 /* tie off string */