2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
34 #include "alertpanel.h"
48 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
57 #include "procheader.h"
58 #include "prefs_account.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
66 #include "remotefolder.h"
68 #include "statusbar.h"
70 #include "imap-thread.h"
75 typedef struct _IMAPFolder IMAPFolder;
76 typedef struct _IMAPSession IMAPSession;
77 typedef struct _IMAPNameSpace IMAPNameSpace;
78 typedef struct _IMAPFolderItem IMAPFolderItem;
80 #include "prefs_account.h"
82 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
83 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
84 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
90 /* list of IMAPNameSpace */
94 gchar last_seen_separator;
103 gboolean authenticated;
112 gboolean folder_content_changed;
123 gboolean sens_update_block;
126 struct _IMAPNameSpace
132 #define IMAPBUFSIZE 8192
134 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
135 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
136 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
137 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
138 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
139 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
142 #define IMAP4_PORT 143
143 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
144 #define IMAPS_PORT 993
147 #define IMAP_CMD_LIMIT 1000
150 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
151 ITEM_CAN_CREATE_FLAGS,
152 ITEM_CANNOT_CREATE_FLAGS
155 struct _IMAPFolderItem
164 GHashTable *flags_set_table;
165 GHashTable *flags_unset_table;
168 gboolean should_update;
169 gboolean should_trash_cache;
170 gint can_create_flags;
172 GHashTable *tags_set_table;
173 GHashTable *tags_unset_table;
178 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
179 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
181 static void imap_folder_init (Folder *folder,
185 static Folder *imap_folder_new (const gchar *name,
187 static void imap_folder_destroy (Folder *folder);
189 static IMAPSession *imap_session_new (Folder *folder,
190 const PrefsAccount *account);
191 static void imap_session_authenticate(IMAPSession *session,
192 const PrefsAccount *account);
193 static void imap_session_destroy (Session *session);
195 static gchar *imap_fetch_msg (Folder *folder,
198 static gchar *imap_fetch_msg_full (Folder *folder,
203 static void imap_remove_cached_msg (Folder *folder,
206 static gint imap_add_msg (Folder *folder,
210 static gint imap_add_msgs (Folder *folder,
213 GRelation *relation);
215 static gint imap_copy_msg (Folder *folder,
218 static gint imap_copy_msgs (Folder *folder,
220 MsgInfoList *msglist,
221 GRelation *relation);
223 static gint imap_remove_msg (Folder *folder,
226 static gint imap_remove_msgs (Folder *folder,
228 MsgInfoList *msglist,
229 GRelation *relation);
230 static gint imap_remove_all_msg (Folder *folder,
233 static gboolean imap_is_msg_changed (Folder *folder,
237 static gint imap_close (Folder *folder,
240 static gint imap_scan_tree (Folder *folder);
242 static gint imap_create_tree (Folder *folder);
244 static FolderItem *imap_create_folder (Folder *folder,
247 static gint imap_rename_folder (Folder *folder,
250 static gint imap_remove_folder (Folder *folder,
253 static FolderItem *imap_folder_item_new (Folder *folder);
254 static void imap_folder_item_destroy (Folder *folder,
257 static IMAPSession *imap_session_get (Folder *folder);
259 static gint imap_auth (IMAPSession *session,
264 static gint imap_scan_tree_recursive (IMAPSession *session,
268 static void imap_create_missing_folders (Folder *folder);
269 static FolderItem *imap_create_special_folder
271 SpecialFolderItemType stype,
274 static gint imap_do_copy_msgs (Folder *folder,
276 MsgInfoList *msglist,
277 GRelation *relation);
279 static void imap_delete_all_cached_messages (FolderItem *item);
280 static void imap_set_batch (Folder *folder,
283 static gint imap_set_message_flags (IMAPSession *session,
284 IMAPFolderItem *item,
285 MsgNumberList *numlist,
289 static gint imap_select (IMAPSession *session,
295 guint32 *uid_validity,
296 gint *can_create_flags,
298 static gint imap_status (IMAPSession *session,
301 IMAPFolderItem *item,
304 guint32 *uid_validity,
307 static void imap_commit_tags (FolderItem *item,
312 static gchar imap_get_path_separator (IMAPSession *session,
315 static gchar *imap_get_real_path (IMAPSession *session,
319 static void imap_synchronise (FolderItem *item, gint days);
321 static gboolean imap_is_busy (Folder *folder);
323 static void imap_free_capabilities (IMAPSession *session);
325 /* low-level IMAP4rev1 commands */
326 static gint imap_cmd_login (IMAPSession *session,
330 static gint imap_cmd_noop (IMAPSession *session);
331 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
332 static gint imap_cmd_starttls (IMAPSession *session);
334 static gint imap_cmd_select (IMAPSession *session,
339 guint32 *uid_validity,
340 gint *can_create_flags,
343 static gint imap_cmd_close (IMAPSession *session);
344 static gint imap_cmd_examine (IMAPSession *session,
349 guint32 *uid_validity,
351 static gint imap_cmd_create (IMAPSession *sock,
352 const gchar *folder);
353 static gint imap_cmd_rename (IMAPSession *sock,
354 const gchar *oldfolder,
355 const gchar *newfolder);
356 static gint imap_cmd_delete (IMAPSession *session,
357 const gchar *folder);
358 static gint imap_cmd_fetch (IMAPSession *sock,
360 const gchar *filename,
363 static gint imap_cmd_append (IMAPSession *session,
364 IMAPFolderItem *item,
365 const gchar *destfolder,
369 static gint imap_cmd_copy (IMAPSession *session,
370 struct mailimap_set * set,
371 const gchar *destfolder,
372 GRelation *uid_mapping,
373 struct mailimap_set ** source,
374 struct mailimap_set ** dest);
375 static gint imap_cmd_store (IMAPSession *session,
376 IMAPFolderItem *item,
377 struct mailimap_set * set,
381 static gint imap_cmd_expunge (IMAPSession *session);
383 static void imap_path_separator_subst (gchar *str,
386 static gboolean imap_rename_folder_func (GNode *node,
388 static gint imap_get_num_list (Folder *folder,
391 gboolean *old_uids_valid);
392 static GSList *imap_get_msginfos (Folder *folder,
394 GSList *msgnum_list);
395 static MsgInfo *imap_get_msginfo (Folder *folder,
398 static gboolean imap_scan_required (Folder *folder,
400 static void imap_change_flags (Folder *folder,
403 MsgPermFlags newflags);
404 static gint imap_get_flags (Folder *folder,
406 MsgInfoList *msglist,
407 GRelation *msgflags);
408 static gchar *imap_folder_get_path (Folder *folder);
409 static gchar *imap_item_get_path (Folder *folder,
411 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
414 /* data types conversion libetpan <-> claws */
415 static GSList * imap_list_from_lep(IMAPFolder * folder,
416 clist * list, const gchar * real_path, gboolean all);
417 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
418 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
419 static GSList * imap_uid_list_from_lep(clist * list);
420 static GSList * imap_uid_list_from_lep_tab(carray * list);
421 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
423 GHashTable *tags_hash);
424 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
426 static void imap_lep_set_free(GSList *seq_list);
427 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
429 typedef struct _hashtable_data {
431 IMAPFolderItem *item;
434 static FolderClass imap_class;
436 FolderClass *imap_get_class(void)
438 if (imap_class.idstr == NULL) {
439 imap_class.type = F_IMAP;
440 imap_class.idstr = "imap";
441 imap_class.uistr = "IMAP4";
443 /* Folder functions */
444 imap_class.new_folder = imap_folder_new;
445 imap_class.destroy_folder = imap_folder_destroy;
446 imap_class.scan_tree = imap_scan_tree;
447 imap_class.create_tree = imap_create_tree;
449 /* FolderItem functions */
450 imap_class.item_new = imap_folder_item_new;
451 imap_class.item_destroy = imap_folder_item_destroy;
452 imap_class.item_get_path = imap_item_get_path;
453 imap_class.create_folder = imap_create_folder;
454 imap_class.rename_folder = imap_rename_folder;
455 imap_class.remove_folder = imap_remove_folder;
456 imap_class.close = imap_close;
457 imap_class.get_num_list = imap_get_num_list;
458 imap_class.scan_required = imap_scan_required;
459 imap_class.set_xml = folder_set_xml;
460 imap_class.get_xml = folder_get_xml;
461 imap_class.item_set_xml = imap_item_set_xml;
462 imap_class.item_get_xml = imap_item_get_xml;
464 /* Message functions */
465 imap_class.get_msginfo = imap_get_msginfo;
466 imap_class.get_msginfos = imap_get_msginfos;
467 imap_class.fetch_msg = imap_fetch_msg;
468 imap_class.fetch_msg_full = imap_fetch_msg_full;
469 imap_class.add_msg = imap_add_msg;
470 imap_class.add_msgs = imap_add_msgs;
471 imap_class.copy_msg = imap_copy_msg;
472 imap_class.copy_msgs = imap_copy_msgs;
473 imap_class.remove_msg = imap_remove_msg;
474 imap_class.remove_msgs = imap_remove_msgs;
475 imap_class.remove_all_msg = imap_remove_all_msg;
476 imap_class.is_msg_changed = imap_is_msg_changed;
477 imap_class.change_flags = imap_change_flags;
478 imap_class.get_flags = imap_get_flags;
479 imap_class.set_batch = imap_set_batch;
480 imap_class.synchronise = imap_synchronise;
481 imap_class.remove_cached_msg = imap_remove_cached_msg;
482 imap_class.commit_tags = imap_commit_tags;
484 pthread_mutex_init(&imap_mutex, NULL);
491 static void imap_refresh_sensitivity (IMAPSession *session)
495 if (session->sens_update_block)
497 mainwin = mainwindow_get_mainwindow();
499 toolbar_main_set_sensitive(mainwin);
500 main_window_set_menu_sensitive(mainwin);
504 static void lock_session(IMAPSession *session)
507 debug_print("locking session %p (%d)\n", session, session->busy);
509 debug_print(" SESSION WAS LOCKED !! \n");
510 session->busy = TRUE;
511 imap_refresh_sensitivity(session);
513 debug_print("can't lock null session\n");
517 static void unlock_session(IMAPSession *session)
520 debug_print("unlocking session %p\n", session);
521 session->busy = FALSE;
522 imap_refresh_sensitivity(session);
524 debug_print("can't unlock null session\n");
528 static void imap_disc_session_destroy(IMAPSession *session)
530 RemoteFolder *rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
536 rfolder->session = NULL;
537 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
538 SESSION(session)->state = SESSION_DISCONNECTED;
539 SESSION(session)->sock = NULL;
540 session_destroy(SESSION(session));
543 static gboolean is_fatal(int libetpan_errcode)
545 switch(libetpan_errcode) {
546 case MAILIMAP_ERROR_STREAM:
547 case MAILIMAP_ERROR_PROTOCOL:
548 case MAILIMAP_ERROR_PARSE:
549 case MAILIMAP_ERROR_BAD_STATE:
556 static void imap_handle_error(Session *session, int libetpan_errcode)
558 switch(libetpan_errcode) {
559 case MAILIMAP_NO_ERROR:
561 case MAILIMAP_NO_ERROR_AUTHENTICATED:
562 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
564 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
565 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
567 case MAILIMAP_ERROR_BAD_STATE:
568 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
570 case MAILIMAP_ERROR_STREAM:
571 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
573 case MAILIMAP_ERROR_PARSE:
574 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
575 "(very probably non-RFC compliance from the server)\n"));
577 case MAILIMAP_ERROR_CONNECTION_REFUSED:
578 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
580 case MAILIMAP_ERROR_MEMORY:
581 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
583 case MAILIMAP_ERROR_FATAL:
584 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
586 case MAILIMAP_ERROR_PROTOCOL:
587 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
588 "(very probably non-RFC compliance from the server)\n"));
590 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
591 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
593 case MAILIMAP_ERROR_APPEND:
594 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
596 case MAILIMAP_ERROR_NOOP:
597 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
599 case MAILIMAP_ERROR_LOGOUT:
600 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
602 case MAILIMAP_ERROR_CAPABILITY:
603 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
605 case MAILIMAP_ERROR_CHECK:
606 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
608 case MAILIMAP_ERROR_CLOSE:
609 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
611 case MAILIMAP_ERROR_EXPUNGE:
612 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
614 case MAILIMAP_ERROR_COPY:
615 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
617 case MAILIMAP_ERROR_UID_COPY:
618 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
620 case MAILIMAP_ERROR_CREATE:
621 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
623 case MAILIMAP_ERROR_DELETE:
624 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
626 case MAILIMAP_ERROR_EXAMINE:
627 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
629 case MAILIMAP_ERROR_FETCH:
630 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
632 case MAILIMAP_ERROR_UID_FETCH:
633 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
635 case MAILIMAP_ERROR_LIST:
636 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
638 case MAILIMAP_ERROR_LOGIN:
639 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
641 case MAILIMAP_ERROR_LSUB:
642 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
644 case MAILIMAP_ERROR_RENAME:
645 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
647 case MAILIMAP_ERROR_SEARCH:
648 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
650 case MAILIMAP_ERROR_UID_SEARCH:
651 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
653 case MAILIMAP_ERROR_SELECT:
654 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
656 case MAILIMAP_ERROR_STATUS:
657 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
659 case MAILIMAP_ERROR_STORE:
660 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
662 case MAILIMAP_ERROR_UID_STORE:
663 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
665 case MAILIMAP_ERROR_SUBSCRIBE:
666 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
668 case MAILIMAP_ERROR_UNSUBSCRIBE:
669 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
671 case MAILIMAP_ERROR_STARTTLS:
672 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
674 case MAILIMAP_ERROR_INVAL:
675 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
677 case MAILIMAP_ERROR_EXTENSION:
678 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
680 case MAILIMAP_ERROR_SASL:
681 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
683 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
684 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
685 case MAILIMAP_ERROR_SSL:
686 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
691 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
696 if (session && is_fatal(libetpan_errcode)) {
697 imap_disc_session_destroy(IMAP_SESSION(session));
698 } else if (session && !is_fatal(libetpan_errcode)) {
699 if (IMAP_SESSION(session)->busy)
700 unlock_session(IMAP_SESSION(session));
704 static Folder *imap_folder_new(const gchar *name, const gchar *path)
708 folder = (Folder *)g_new0(IMAPFolder, 1);
709 folder->klass = &imap_class;
710 imap_folder_init(folder, name, path);
715 static void imap_folder_destroy(Folder *folder)
717 while (imap_folder_get_refcnt(folder) > 0)
718 gtk_main_iteration();
720 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
724 static void imap_folder_init(Folder *folder, const gchar *name,
727 folder_remote_folder_init((Folder *)folder, name, path);
728 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
731 static FolderItem *imap_folder_item_new(Folder *folder)
733 IMAPFolderItem *item;
735 item = g_new0(IMAPFolderItem, 1);
738 item->uid_list = NULL;
740 return (FolderItem *)item;
743 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
745 IMAPFolderItem *item = (IMAPFolderItem *)_item;
747 g_return_if_fail(item != NULL);
748 g_slist_free(item->uid_list);
753 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
755 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
758 g_slist_free(item->uid_list);
759 item->uid_list = NULL;
764 static void imap_reset_uid_lists(Folder *folder)
766 if(folder->node == NULL)
769 /* Destroy all uid lists and rest last uid */
770 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
773 static int imap_get_capabilities(IMAPSession *session)
775 struct mailimap_capability_data *capabilities = NULL;
779 if (session->capability != NULL)
780 return MAILIMAP_NO_ERROR;
782 capabilities = imap_threaded_capability(session->folder, &result);
784 if (result != MAILIMAP_NO_ERROR) {
785 imap_handle_error(SESSION(session), result);
786 return MAILIMAP_ERROR_CAPABILITY;
789 if (capabilities == NULL) {
790 return MAILIMAP_NO_ERROR;
793 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
794 cur = clist_next(cur)) {
795 struct mailimap_capability * cap =
797 if (!cap || cap->cap_data.cap_name == NULL)
799 session->capability = g_slist_append
800 (session->capability,
801 g_strdup(cap->cap_data.cap_name));
802 debug_print("got capa %s\n", cap->cap_data.cap_name);
804 mailimap_capability_data_free(capabilities);
805 return MAILIMAP_NO_ERROR;
808 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
811 for (cur = session->capability; cur; cur = cur->next) {
812 if (!g_ascii_strcasecmp(cur->data, cap))
818 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
821 gint ok = MAILIMAP_ERROR_BAD_STATE;
822 static time_t last_login_err = 0;
823 gchar *ext_info = "";
825 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
826 imap_handle_error(SESSION(session), r);
831 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
833 case IMAP_AUTH_CRAM_MD5:
834 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
836 case IMAP_AUTH_DIGEST_MD5:
837 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
839 case IMAP_AUTH_LOGIN:
840 ok = imap_cmd_login(session, user, pass, "LOGIN");
842 case IMAP_AUTH_GSSAPI:
843 ok = imap_cmd_login(session, user, pass, "GSSAPI");
846 debug_print("capabilities:\n"
852 imap_has_capability(session, "ANONYMOUS"),
853 imap_has_capability(session, "CRAM-MD5"),
854 imap_has_capability(session, "DIGEST-MD5"),
855 imap_has_capability(session, "LOGIN"),
856 imap_has_capability(session, "GSSAPI"));
857 if (imap_has_capability(session, "CRAM-MD5"))
858 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
859 if ((ok == MAILIMAP_ERROR_BAD_STATE ||
860 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_BAD_STATE ||
863 ok == MAILIMAP_ERROR_LOGIN) && imap_has_capability(session, "GSSAPI"))
864 ok = imap_cmd_login(session, user, pass, "GSSAPI");
865 if (ok == MAILIMAP_ERROR_BAD_STATE||
866 ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
867 ok = imap_cmd_login(session, user, pass, "LOGIN");
870 if (ok == MAILIMAP_NO_ERROR)
871 session->authenticated = TRUE;
873 if (type == IMAP_AUTH_CRAM_MD5) {
874 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
875 "compiled with SASL support and the "
876 "CRAM-MD5 SASL plugin is installed.");
879 if (type == IMAP_AUTH_DIGEST_MD5) {
880 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
881 "compiled with SASL support and the "
882 "DIGEST-MD5 SASL plugin is installed.");
885 if (time(NULL) - last_login_err > 10) {
886 if (!prefs_common.no_recv_err_panel) {
887 alertpanel_error_log(_("Connection to %s failed: "
889 SESSION(session)->server, ext_info);
891 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
892 "login refused.%s\n"),
893 SESSION(session)->server, ext_info);
896 last_login_err = time(NULL);
901 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
903 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
904 /* Check if this is the first try to establish a
905 connection, if yes we don't try to reconnect */
906 debug_print("reconnecting\n");
907 if (rfolder->session == NULL) {
908 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
909 folder->account->recv_server);
910 SESSION(session)->sock = NULL;
911 session_destroy(SESSION(session));
914 rfolder->session = NULL;
915 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
916 " disconnected. Reconnecting...\n"),
917 folder->account->recv_server);
918 statusbar_print_all(_("IMAP4 connection to %s has been"
919 " disconnected. Reconnecting...\n"),
920 folder->account->recv_server);
921 SESSION(session)->state = SESSION_DISCONNECTED;
922 SESSION(session)->sock = NULL;
923 session_destroy(SESSION(session));
924 /* Clear folders session to make imap_session_get create
925 a new session, because of rfolder->session == NULL
926 it will not try to reconnect again and so avoid an
928 debug_print("getting session...\n");
929 session = imap_session_get(folder);
930 rfolder->session = SESSION(session);
936 static IMAPSession *imap_session_get(Folder *folder)
938 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
939 IMAPSession *session = NULL;
940 gint r = MAILIMAP_NO_ERROR;
942 g_return_val_if_fail(folder != NULL, NULL);
943 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
944 g_return_val_if_fail(folder->account != NULL, NULL);
946 if (prefs_common.work_offline &&
947 !inc_offline_should_override(FALSE,
948 _("Claws Mail needs network access in order "
949 "to access the IMAP server."))) {
953 /* Make sure we have a session */
954 if (rfolder->session != NULL) {
955 session = IMAP_SESSION(rfolder->session);
956 } else if (rfolder->connecting) {
957 debug_print("already connecting\n");
960 imap_reset_uid_lists(folder);
961 if (time(NULL) - rfolder->last_failure <= 2)
963 rfolder->connecting = TRUE;
964 session = imap_session_new(folder, folder->account);
966 if(session == NULL) {
967 rfolder->last_failure = time(NULL);
968 rfolder->connecting = FALSE;
972 /* Make sure session is authenticated */
973 if (!IMAP_SESSION(session)->authenticated)
974 imap_session_authenticate(IMAP_SESSION(session), folder->account);
976 if (!IMAP_SESSION(session)->authenticated) {
977 imap_threaded_disconnect(session->folder);
978 rfolder->session = NULL;
979 SESSION(session)->state = SESSION_DISCONNECTED;
980 SESSION(session)->sock = NULL;
981 session_destroy(SESSION(session));
982 rfolder->last_failure = time(NULL);
983 rfolder->connecting = FALSE;
987 /* I think the point of this code is to avoid sending a
988 * keepalive if we've used the session recently and therefore
989 * think it's still alive. Unfortunately, most of the code
990 * does not yet check for errors on the socket, and so if the
991 * connection drops we don't notice until the timeout expires.
992 * A better solution than sending a NOOP every time would be
993 * for every command to be prepared to retry until it is
994 * successfully sent. -- mbp */
995 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
996 /* verify that the session is still alive */
997 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
998 debug_print("disconnected!\n");
1000 session = imap_reconnect_if_possible(folder, session);
1002 rfolder->session = NULL;
1003 rfolder->connecting = FALSE;
1004 session = imap_session_get(folder);
1008 session->cancelled = FALSE;
1011 rfolder->session = SESSION(session);
1012 rfolder->connecting = FALSE;
1014 return IMAP_SESSION(session);
1017 static IMAPSession *imap_session_new(Folder * folder,
1018 const PrefsAccount *account)
1020 IMAPSession *session;
1023 int authenticated = FALSE;
1025 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1026 /* FIXME: IMAP over SSL only... */
1029 port = account->set_imapport ? account->imapport
1030 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1031 ssl_type = account->ssl_imap;
1033 if (account->ssl_imap != SSL_NONE) {
1034 if (alertpanel_full(_("Insecure connection"),
1035 _("This connection is configured to be secured "
1036 "using SSL, but SSL is not available in this "
1037 "build of Claws Mail. \n\n"
1038 "Do you want to continue connecting to this "
1039 "server? The communication would not be "
1041 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1042 NULL, FALSE, NULL, ALERT_WARNING,
1043 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1046 port = account->set_imapport ? account->imapport
1051 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1053 if (account->set_tunnelcmd) {
1054 r = imap_threaded_connect_cmd(folder,
1056 account->recv_server,
1062 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1063 if (ssl_type == SSL_TUNNEL) {
1064 r = imap_threaded_connect_ssl(folder,
1065 account->recv_server,
1071 r = imap_threaded_connect(folder,
1072 account->recv_server,
1077 statuswindow_pop_all();
1078 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1079 authenticated = TRUE;
1081 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1082 authenticated = FALSE;
1085 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1086 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1087 if (r == MAILIMAP_ERROR_SSL)
1088 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1092 imap_handle_error(NULL, r);
1094 if(!prefs_common.no_recv_err_panel) {
1095 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1096 account->recv_server, port);
1098 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1099 account->recv_server, port);
1105 session = g_new0(IMAPSession, 1);
1106 session_init(SESSION(session));
1107 SESSION(session)->type = SESSION_IMAP;
1108 SESSION(session)->server = g_strdup(account->recv_server);
1109 SESSION(session)->sock = NULL;
1111 SESSION(session)->destroy = imap_session_destroy;
1113 session->capability = NULL;
1115 session->authenticated = authenticated;
1116 session->mbox = NULL;
1117 session->exists = 0;
1118 session->recent = 0;
1119 session->expunge = 0;
1120 session->cmd_count = 0;
1121 session->folder = folder;
1122 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1124 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1125 if (account->ssl_imap == SSL_STARTTLS) {
1128 ok = imap_cmd_starttls(session);
1129 if (ok != MAILIMAP_NO_ERROR) {
1130 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1131 SESSION(session)->sock = NULL;
1132 session_destroy(SESSION(session));
1136 imap_free_capabilities(session);
1137 session->authenticated = FALSE;
1138 session->uidplus = FALSE;
1139 session->cmd_count = 1;
1142 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1143 (session->authenticated) ? "pre" : "un");
1148 static void imap_session_authenticate(IMAPSession *session,
1149 const PrefsAccount *account)
1151 gchar *pass, *acc_pass;
1152 gboolean failed = FALSE;
1154 g_return_if_fail(account->userid != NULL);
1155 acc_pass = account->passwd;
1158 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1160 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1163 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
1165 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1168 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1169 account->recv_server);
1170 if (imap_auth(session, account->userid, pass, account->imap_auth_type) != MAILIMAP_NO_ERROR) {
1171 statusbar_pop_all();
1178 if (prefs_common.no_recv_err_panel) {
1179 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
1180 mainwindow_show_error();
1182 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1188 statuswindow_pop_all();
1189 session->authenticated = TRUE;
1193 static void imap_session_destroy(Session *session)
1195 if (session->state != SESSION_DISCONNECTED)
1196 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1198 imap_free_capabilities(IMAP_SESSION(session));
1199 g_free(IMAP_SESSION(session)->mbox);
1202 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1204 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1207 static guint get_file_size_with_crs(const gchar *filename)
1213 if (filename == NULL)
1216 fp = fopen(filename, "rb");
1220 while (fgets(buf, sizeof (buf), fp) != NULL) {
1222 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1230 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1232 gchar *path, *filename;
1234 path = folder_item_get_path(item);
1236 if (!is_dir_exist(path)) {
1241 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1244 if (is_file_exist(filename)) {
1250 typedef struct _TagsData {
1253 IMAPFolderItem *item;
1256 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1258 IMAPSession *session;
1259 gint ok, can_create_tags;
1260 Folder *folder = NULL;
1261 TagsData *ht_data = NULL;
1264 g_return_if_fail(item != NULL);
1265 g_return_if_fail(msginfo != NULL);
1267 folder = item->folder;
1268 debug_print("getting session...\n");
1269 session = imap_session_get(folder);
1272 debug_print("can't get session\n");
1276 ok = imap_select(session, IMAP_FOLDER(folder), item,
1277 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1279 if (ok != MAILIMAP_NO_ERROR) {
1280 imap_handle_error(SESSION(session), ok);
1285 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1288 if (IMAP_FOLDER_ITEM(item)->batching) {
1289 /* instead of performing an UID STORE command for each message change,
1290 * as a lot of them can change "together", we just fill in hashtables
1291 * and defer the treatment so that we're able to send only one
1294 debug_print("IMAP batch mode on, deferring tags change\n");
1295 for (cur = tags_set; cur; cur = cur->next) {
1296 gint cur_tag = GPOINTER_TO_INT(cur->data);
1298 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1299 GINT_TO_POINTER(cur_tag));
1300 if (ht_data == NULL) {
1301 ht_data = g_new0(TagsData, 1);
1302 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1303 ht_data->item = IMAP_FOLDER_ITEM(item);
1304 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1305 GINT_TO_POINTER(cur_tag), ht_data);
1307 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1310 for (cur = tags_unset; cur; cur = cur->next) {
1311 gint cur_tag = GPOINTER_TO_INT(cur->data);
1313 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1314 GINT_TO_POINTER(cur_tag));
1315 if (ht_data == NULL) {
1316 ht_data = g_new0(TagsData, 1);
1317 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1318 ht_data->item = IMAP_FOLDER_ITEM(item);
1319 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1320 GINT_TO_POINTER(cur_tag), ht_data);
1322 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1326 GSList *list_set = NULL;
1327 GSList *list_unset = NULL;
1330 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1331 numlist.next = NULL;
1333 debug_print("IMAP changing tags NOW\n");
1334 for (cur = tags_set; cur; cur = cur->next) {
1335 gint cur_tag = GPOINTER_TO_INT(cur->data);
1336 const gchar *str = tags_get_tag(cur_tag);
1337 list_set = g_slist_prepend(list_set, g_strdup(str));
1340 ok = imap_set_message_flags(session,
1341 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1342 slist_free_strings(list_set);
1343 g_slist_free(list_set);
1344 if (ok != MAILIMAP_NO_ERROR) {
1349 for (cur = tags_unset; cur; cur = cur->next) {
1350 gint cur_tag = GPOINTER_TO_INT(cur->data);
1351 const gchar *str = tags_get_tag(cur_tag);
1352 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1355 ok = imap_set_message_flags(session,
1356 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1357 slist_free_strings(list_unset);
1358 g_slist_free(list_unset);
1359 if (ok != MAILIMAP_NO_ERROR) {
1366 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1367 gboolean headers, gboolean body)
1369 gchar *path, *filename;
1370 IMAPSession *session;
1373 g_return_val_if_fail(folder != NULL, NULL);
1374 g_return_val_if_fail(item != NULL, NULL);
1379 path = folder_item_get_path(item);
1380 if (!is_dir_exist(path))
1381 make_dir_hier(path);
1382 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1384 debug_print("trying to fetch cached %s\n", filename);
1385 if (is_file_exist(filename)) {
1386 /* see whether the local file represents the whole message
1387 * or not. As the IMAP server reports size with \r chars,
1388 * we have to update the local file (UNIX \n only) size */
1389 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1390 guint have_size = -1;
1393 debug_print("message %d has been already %scached.\n", uid,
1394 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1396 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1397 have_size = get_file_size_with_crs(filename);
1398 if (cached && (cached->size <= have_size || !body)) {
1399 procmsg_msginfo_free(cached);
1400 ok = file_strip_crs(filename);
1401 if (ok == 0 && cached && cached->size <= have_size) {
1402 /* we have it all and stripped */
1403 debug_print("...fully cached in fact; setting flag.\n");
1404 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1407 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1408 debug_print("message not cached and file recent, considering file complete\n");
1409 ok = file_strip_crs(filename);
1413 procmsg_msginfo_free(cached);
1416 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1417 procmsg_msginfo_free(cached);
1421 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1423 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1424 procmsg_msginfo_free(cached);
1428 debug_print("getting session...\n");
1429 session = imap_session_get(folder);
1435 session_set_access_time(SESSION(session));
1436 lock_session(session); /* unlocked later in the function */
1438 debug_print("IMAP fetching messages\n");
1439 ok = imap_select(session, IMAP_FOLDER(folder), item,
1440 NULL, NULL, NULL, NULL, NULL, FALSE);
1441 if (ok != MAILIMAP_NO_ERROR) {
1442 g_warning("can't select mailbox %s\n", item->path);
1447 session_set_access_time(SESSION(session));
1449 debug_print("getting message %d...\n", uid);
1450 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1452 if (ok != MAILIMAP_NO_ERROR) {
1453 g_warning("can't fetch message %d\n", uid);
1458 session_set_access_time(SESSION(session));
1459 unlock_session(session);
1461 ok = file_strip_crs(filename);
1463 if (ok == 0 && headers && body) {
1464 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1466 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1467 procmsg_msginfo_free(cached);
1469 } else if (ok == -1) {
1470 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1472 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1473 procmsg_msginfo_free(cached);
1479 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1481 gchar *path, *filename;
1483 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1488 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1489 procmsg_msginfo_free(cached);
1492 path = folder_item_get_path(item);
1493 if (!is_dir_exist(path))
1496 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1498 if (is_file_exist(filename)) {
1499 if (cached && cached->total_size == cached->size) {
1502 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1505 size = get_file_size_with_crs(filename);
1508 if (cached && size >= cached->size) {
1509 cached->total_size = cached->size;
1510 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1511 procmsg_msginfo_free(cached);
1515 procmsg_msginfo_free(cached);
1519 void imap_cache_msg(FolderItem *item, gint msgnum)
1521 Folder *folder = NULL;
1525 folder = item->folder;
1527 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1528 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1529 debug_print("fetched %s\n", tmp);
1534 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1535 const gchar *file, MsgFlags *flags)
1539 MsgFileInfo fileinfo;
1541 g_return_val_if_fail(file != NULL, -1);
1543 fileinfo.msginfo = NULL;
1544 fileinfo.file = (gchar *)file;
1545 fileinfo.flags = flags;
1546 file_list.data = &fileinfo;
1547 file_list.next = NULL;
1549 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1553 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1554 GRelation *relation)
1557 IMAPSession *session;
1558 guint32 last_uid = 0;
1560 MsgFileInfo *fileinfo;
1561 gint ok = MAILIMAP_NO_ERROR;
1562 gint curnum = 0, total = 0;
1563 gboolean missing_uids = FALSE;
1565 g_return_val_if_fail(folder != NULL, -1);
1566 g_return_val_if_fail(dest != NULL, -1);
1567 g_return_val_if_fail(file_list != NULL, -1);
1569 debug_print("getting session...\n");
1570 session = imap_session_get(folder);
1574 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1576 statusbar_print_all(_("Adding messages..."));
1577 total = g_slist_length(file_list);
1578 for (cur = file_list; cur != NULL; cur = cur->next) {
1579 IMAPFlags iflags = 0;
1580 guint32 new_uid = 0;
1581 gchar *real_file = NULL;
1582 fileinfo = (MsgFileInfo *)cur->data;
1584 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1587 if (fileinfo->flags) {
1588 if (MSG_IS_MARKED(*fileinfo->flags))
1589 iflags |= IMAP_FLAG_FLAGGED;
1590 if (MSG_IS_REPLIED(*fileinfo->flags))
1591 iflags |= IMAP_FLAG_ANSWERED;
1592 if (MSG_IS_FORWARDED(*fileinfo->flags))
1593 iflags |= IMAP_FLAG_FORWARDED;
1594 if (!MSG_IS_UNREAD(*fileinfo->flags))
1595 iflags |= IMAP_FLAG_SEEN;
1599 if (real_file == NULL)
1600 real_file = g_strdup(fileinfo->file);
1602 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1603 folder_has_parent_of_type(dest, F_OUTBOX) ||
1604 folder_has_parent_of_type(dest, F_DRAFT) ||
1605 folder_has_parent_of_type(dest, F_TRASH))
1606 iflags |= IMAP_FLAG_SEEN;
1608 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1611 if (ok != MAILIMAP_NO_ERROR) {
1612 g_warning("can't append message %s\n", real_file);
1615 statusbar_progress_all(0,0,0);
1616 statusbar_pop_all();
1619 debug_print("appended new message as %d\n", new_uid);
1620 /* put the local file in the imapcache, so that we don't
1621 * have to fetch it back later. */
1624 missing_uids = TRUE;
1625 debug_print("Missing UID (0)\n");
1628 gchar *cache_path = folder_item_get_path(dest);
1629 if (!is_dir_exist(cache_path))
1630 make_dir_hier(cache_path);
1631 if (is_dir_exist(cache_path)) {
1632 gchar *cache_file = g_strconcat(
1633 cache_path, G_DIR_SEPARATOR_S,
1634 itos(new_uid), NULL);
1635 copy_file(real_file, cache_file, TRUE);
1636 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1643 if (relation != NULL)
1644 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1645 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1646 GINT_TO_POINTER(new_uid));
1647 if (last_uid < new_uid) {
1654 statusbar_progress_all(0,0,0);
1655 statusbar_pop_all();
1660 imap_scan_required(folder, dest);
1662 session = imap_session_get(folder);
1668 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1669 &a, NULL, NULL, NULL, NULL, FALSE);
1674 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1676 GSList *result = NULL;
1681 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1682 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1683 start = item->set_first;
1684 end = item->set_last;
1685 for (t = start; t <= end; t++) {
1686 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1689 result = g_slist_reverse(result);
1690 if (debug_get_mode()) {
1691 debug_print("flat imap set: ");
1692 for (cur = result; cur; cur = cur->next) {
1693 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1700 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1701 MsgInfoList *msglist, GRelation *relation)
1705 GSList *seq_list, *cur;
1707 IMAPSession *session;
1708 gint ok = MAILIMAP_NO_ERROR;
1709 GRelation *uid_mapping;
1711 gboolean single = FALSE;
1713 g_return_val_if_fail(folder != NULL, -1);
1714 g_return_val_if_fail(dest != NULL, -1);
1715 g_return_val_if_fail(msglist != NULL, -1);
1717 debug_print("getting session...\n");
1718 session = imap_session_get(folder);
1724 msginfo = (MsgInfo *)msglist->data;
1725 if (msglist->next == NULL)
1727 src = msginfo->folder;
1729 g_warning("the src folder is identical to the dest.\n");
1733 if (src->folder != dest->folder) {
1734 GSList *infolist = NULL, *cur;
1736 for (cur = msglist; cur; cur = cur->next) {
1737 msginfo = (MsgInfo *)cur->data;
1738 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1739 fileinfo->file = procmsg_get_message_file(msginfo);
1740 fileinfo->flags = &(msginfo->flags);
1741 infolist = g_slist_prepend(infolist, fileinfo);
1743 infolist = g_slist_reverse(infolist);
1744 res = folder_item_add_msgs(dest, infolist, FALSE);
1745 for (cur = infolist; cur; cur = cur->next) {
1746 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1750 g_slist_free(infolist);
1754 lock_session(session); /* unlocked later in the function */
1756 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1757 NULL, NULL, NULL, NULL, NULL, FALSE);
1758 if (ok != MAILIMAP_NO_ERROR) {
1762 unlock_session(session);
1764 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1765 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1766 uid_mapping = g_relation_new(2);
1767 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1769 statusbar_print_all(_("Copying messages..."));
1770 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1771 struct mailimap_set * seq_set;
1772 struct mailimap_set * source = NULL;
1773 struct mailimap_set * dest = NULL;
1774 seq_set = cur->data;
1776 debug_print("Copying messages from %s to %s ...\n",
1777 src->path, destdir);
1779 lock_session(session); /* unlocked later in the function */
1780 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1787 if (ok == MAILIMAP_NO_ERROR) {
1788 unlock_session(session);
1789 if (relation && source && dest) {
1790 GSList *s_list = flatten_mailimap_set(source);
1791 GSList *d_list = flatten_mailimap_set(dest);
1792 GSList *s_cur, *d_cur;
1793 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1795 for (s_cur = s_list, d_cur = d_list;
1797 s_cur = s_cur->next, d_cur = d_cur->next) {
1798 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1802 debug_print("hhhmm, source list length != dest list length.\n");
1804 g_slist_free(s_list);
1805 g_slist_free(d_list);
1811 mailimap_set_free(source);
1813 mailimap_set_free(dest);
1815 if (ok != MAILIMAP_NO_ERROR) {
1816 g_relation_destroy(uid_mapping);
1817 imap_lep_set_free(seq_list);
1818 statusbar_pop_all();
1823 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1824 MsgInfo *msginfo = (MsgInfo *)cur->data;
1827 tuples = g_relation_select(uid_mapping,
1828 GINT_TO_POINTER(msginfo->msgnum),
1830 if (tuples->len > 0) {
1831 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1832 g_relation_insert(relation, msginfo,
1833 GINT_TO_POINTER(num));
1836 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1837 /* put the local file in the imapcache, so that we don't
1838 * have to fetch it back later. */
1840 gchar *cache_path = folder_item_get_path(msginfo->folder);
1841 gchar *real_file = g_strconcat(
1842 cache_path, G_DIR_SEPARATOR_S,
1843 itos(msginfo->msgnum), NULL);
1844 gchar *cache_file = NULL;
1846 cache_path = folder_item_get_path(dest);
1847 cache_file = g_strconcat(
1848 cache_path, G_DIR_SEPARATOR_S,
1850 if (!is_dir_exist(cache_path))
1851 make_dir_hier(cache_path);
1852 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1853 copy_file(real_file, cache_file, TRUE);
1854 debug_print("copied to cache: %s\n", cache_file);
1861 g_relation_insert(relation, msginfo,
1862 GINT_TO_POINTER(0));
1863 g_tuples_destroy(tuples);
1865 statusbar_pop_all();
1867 g_relation_destroy(uid_mapping);
1868 imap_lep_set_free(seq_list);
1872 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1873 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1874 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1875 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1877 imap_scan_required(folder, dest);
1878 if (ok == MAILIMAP_NO_ERROR)
1884 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1888 g_return_val_if_fail(msginfo != NULL, -1);
1890 msglist.data = msginfo;
1891 msglist.next = NULL;
1893 return imap_copy_msgs(folder, dest, &msglist, NULL);
1896 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1897 MsgInfoList *msglist, GRelation *relation)
1902 g_return_val_if_fail(folder != NULL, -1);
1903 g_return_val_if_fail(dest != NULL, -1);
1904 g_return_val_if_fail(msglist != NULL, -1);
1906 msginfo = (MsgInfo *)msglist->data;
1907 g_return_val_if_fail(msginfo->folder != NULL, -1);
1909 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1914 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1915 MsgInfoList *msglist, GRelation *relation)
1917 gchar *destdir, *dir;
1918 GSList *numlist = NULL, *cur;
1920 IMAPSession *session;
1921 gint ok = MAILIMAP_NO_ERROR;
1922 GRelation *uid_mapping;
1924 g_return_val_if_fail(folder != NULL, -1);
1925 g_return_val_if_fail(dest != NULL, -1);
1926 g_return_val_if_fail(msglist != NULL, -1);
1928 debug_print("getting session...\n");
1929 session = imap_session_get(folder);
1934 lock_session(session); /* unlocked later in the function */
1936 msginfo = (MsgInfo *)msglist->data;
1938 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1939 NULL, NULL, NULL, NULL, NULL, FALSE);
1940 if (ok != MAILIMAP_NO_ERROR) {
1944 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1945 for (cur = msglist; cur; cur = cur->next) {
1946 msginfo = (MsgInfo *)cur->data;
1947 if (!MSG_IS_DELETED(msginfo->flags))
1948 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1950 numlist = g_slist_reverse(numlist);
1952 uid_mapping = g_relation_new(2);
1953 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1955 if (numlist != NULL) {
1956 ok = imap_set_message_flags
1957 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1958 if (ok != MAILIMAP_NO_ERROR) {
1959 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1962 } /* else we just need to expunge */
1963 ok = imap_cmd_expunge(session);
1964 if (ok != MAILIMAP_NO_ERROR) {
1965 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1969 session->folder_content_changed = TRUE;
1970 unlock_session(session);
1972 dir = folder_item_get_path(msginfo->folder);
1973 if (is_dir_exist(dir)) {
1974 for (cur = msglist; cur; cur = cur->next) {
1975 msginfo = (MsgInfo *)cur->data;
1976 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1981 g_relation_destroy(uid_mapping);
1982 g_slist_free(numlist);
1984 imap_scan_required(folder, dest);
1987 if (ok == MAILIMAP_NO_ERROR)
1993 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
1994 MsgInfoList *msglist, GRelation *relation)
1998 g_return_val_if_fail(folder != NULL, -1);
1999 g_return_val_if_fail(dest != NULL, -1);
2000 if (msglist == NULL)
2003 msginfo = (MsgInfo *)msglist->data;
2004 g_return_val_if_fail(msginfo->folder != NULL, -1);
2006 return imap_do_remove_msgs(folder, dest, msglist, relation);
2009 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2011 GSList *list = folder_item_get_msg_list(item);
2012 gint res = imap_remove_msgs(folder, item, list, NULL);
2013 procmsg_msg_list_free(list);
2017 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2020 /* TODO: properly implement this method */
2024 static gint imap_close(Folder *folder, FolderItem *item)
2029 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2031 FolderItem *item = NULL;
2032 IMAPSession *session;
2033 gchar *root_folder = NULL;
2035 g_return_val_if_fail(folder != NULL, -1);
2036 g_return_val_if_fail(folder->account != NULL, -1);
2038 debug_print("getting session...\n");
2039 session = imap_session_get(folder);
2041 if (!folder->node) {
2042 folder_tree_destroy(folder);
2043 item = folder_item_new(folder, folder->name, NULL);
2044 item->folder = folder;
2045 folder->node = item->node = g_node_new(item);
2050 if (folder->account->imap_dir && *folder->account->imap_dir) {
2055 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2056 extract_quote(root_folder, '"');
2057 subst_char(root_folder,
2058 imap_get_path_separator(session, IMAP_FOLDER(folder),
2061 strtailchomp(root_folder, '/');
2062 real_path = imap_get_real_path
2063 (session, IMAP_FOLDER(folder), root_folder);
2064 debug_print("IMAP root directory: %s\n", real_path);
2066 /* check if root directory exist */
2068 r = imap_threaded_list(session->folder, "", real_path,
2071 if (r != MAILIMAP_NO_ERROR)
2072 imap_handle_error(SESSION(session), r);
2074 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2075 if (!folder->node) {
2076 item = folder_item_new(folder, folder->name, NULL);
2077 item->folder = folder;
2078 folder->node = item->node = g_node_new(item);
2082 mailimap_list_result_free(lep_list);
2088 item = FOLDER_ITEM(folder->node->data);
2090 if (item && !item->path && root_folder) {
2091 item->path = g_strdup(root_folder);
2094 if (!item || ((item->path || root_folder) &&
2095 strcmp2(item->path, root_folder) != 0)) {
2096 folder_tree_destroy(folder);
2097 item = folder_item_new(folder, folder->name, root_folder);
2098 item->folder = folder;
2099 folder->node = item->node = g_node_new(item);
2102 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2103 imap_create_missing_folders(folder);
2108 static gint imap_scan_tree(Folder *folder)
2110 gboolean subs_only = FALSE;
2111 if (folder->account) {
2112 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2113 subs_only = folder->account->imap_subsonly;
2115 return imap_scan_tree_real(folder, subs_only);
2118 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2121 IMAPFolder *imapfolder;
2122 FolderItem *new_item;
2123 GSList *item_list, *cur;
2126 gchar *wildcard_path;
2132 g_return_val_if_fail(item != NULL, -1);
2133 g_return_val_if_fail(item->folder != NULL, -1);
2134 g_return_val_if_fail(item->no_sub == FALSE, -1);
2136 folder = item->folder;
2137 imapfolder = IMAP_FOLDER(folder);
2139 separator = imap_get_path_separator(session, imapfolder, item->path);
2141 if (folder->ui_func)
2142 folder->ui_func(folder, item, folder->ui_func_data);
2145 wildcard[0] = separator;
2148 real_path = imap_get_real_path(session, imapfolder, item->path);
2152 real_path = g_strdup("");
2155 Xstrcat_a(wildcard_path, real_path, wildcard,
2156 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2160 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2162 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2164 if (r != MAILIMAP_NO_ERROR) {
2165 imap_handle_error(SESSION(session), r);
2169 item_list = imap_list_from_lep(imapfolder,
2170 lep_list, real_path, FALSE);
2171 mailimap_list_result_free(lep_list);
2176 node = item->node->children;
2177 while (node != NULL) {
2178 FolderItem *old_item = FOLDER_ITEM(node->data);
2179 GNode *next = node->next;
2182 for (cur = item_list; cur != NULL; cur = cur->next) {
2183 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2184 if (!strcmp2(old_item->path, cur_item->path)) {
2185 new_item = cur_item;
2190 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2191 debug_print("not removing INBOX\n");
2193 debug_print("folder '%s' not found. removing...\n",
2195 folder_item_remove(old_item);
2198 old_item->no_sub = new_item->no_sub;
2199 old_item->no_select = new_item->no_select;
2200 if (old_item->no_sub == TRUE && node->children) {
2201 debug_print("folder '%s' doesn't have "
2202 "subfolders. removing...\n",
2204 folder_item_remove_children(old_item);
2211 for (cur = item_list; cur != NULL; cur = cur->next) {
2212 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2215 for (node = item->node->children; node != NULL;
2216 node = node->next) {
2217 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2219 new_item = FOLDER_ITEM(node->data);
2220 folder_item_destroy(cur_item);
2226 new_item = cur_item;
2227 debug_print("new folder '%s' found.\n", new_item->path);
2228 folder_item_append(item, new_item);
2231 if (!strcmp(new_item->path, "INBOX")) {
2232 new_item->stype = F_INBOX;
2233 folder->inbox = new_item;
2234 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2237 base = g_path_get_basename(new_item->path);
2239 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2240 new_item->stype = F_OUTBOX;
2241 folder->outbox = new_item;
2242 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2243 new_item->stype = F_DRAFT;
2244 folder->draft = new_item;
2245 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2246 new_item->stype = F_QUEUE;
2247 folder->queue = new_item;
2248 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2249 new_item->stype = F_TRASH;
2250 folder->trash = new_item;
2255 if (new_item->no_sub == FALSE)
2256 imap_scan_tree_recursive(session, new_item, subs_only);
2259 g_slist_free(item_list);
2261 return MAILIMAP_NO_ERROR;
2264 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2266 IMAPSession *session = imap_session_get(folder);
2268 gchar *wildcard_path;
2272 GSList *item_list = NULL, *cur;
2273 GList *child_list = NULL, *tmplist = NULL;
2274 GSList *sub_list = NULL;
2280 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2283 wildcard[0] = separator;
2286 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2290 real_path = g_strdup("");
2293 Xstrcat_a(wildcard_path, real_path, wildcard,
2294 {g_free(real_path); return NULL;});
2298 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2299 item->path?item->path:item->name);
2301 statusbar_print_all(_("Looking for subfolders of %s..."),
2302 item->path?item->path:item->name);
2304 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2306 statusbar_pop_all();
2309 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2310 lep_list, real_path, FALSE);
2311 mailimap_list_result_free(lep_list);
2313 for (cur = item_list; cur != NULL; cur = cur->next) {
2314 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2316 tmplist = imap_scan_subtree(folder, cur_item,
2317 unsubs_only, recursive);
2319 child_list = g_list_concat(child_list, tmplist);
2321 child_list = g_list_prepend(child_list,
2322 imap_get_real_path(session,
2323 IMAP_FOLDER(folder), cur_item->path));
2325 folder_item_destroy(cur_item);
2327 child_list = g_list_reverse(child_list);
2328 g_slist_free(item_list);
2331 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2333 statusbar_pop_all();
2336 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2337 lep_list, real_path, FALSE);
2338 mailimap_list_result_free(lep_list);
2340 for (cur = sub_list; cur != NULL; cur = cur->next) {
2341 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2342 GList *oldlitem = NULL;
2343 gchar *tmp = imap_get_real_path(session,
2344 IMAP_FOLDER(folder), cur_item->path);
2345 folder_item_destroy(cur_item);
2346 oldlitem = g_list_find_custom(
2347 child_list, tmp, (GCompareFunc)strcmp2);
2349 child_list = g_list_remove_link(child_list, oldlitem);
2350 g_free(oldlitem->data);
2351 g_list_free(oldlitem);
2357 statusbar_pop_all();
2362 static gint imap_create_tree(Folder *folder)
2364 g_return_val_if_fail(folder != NULL, -1);
2365 g_return_val_if_fail(folder->node != NULL, -1);
2366 g_return_val_if_fail(folder->node->data != NULL, -1);
2367 g_return_val_if_fail(folder->account != NULL, -1);
2369 imap_scan_tree(folder);
2370 imap_create_missing_folders(folder);
2375 static void imap_create_missing_folders(Folder *folder)
2377 g_return_if_fail(folder != NULL);
2380 folder->inbox = imap_create_special_folder
2381 (folder, F_INBOX, "INBOX");
2383 folder->trash = imap_create_special_folder
2384 (folder, F_TRASH, "Trash");
2386 folder->queue = imap_create_special_folder
2387 (folder, F_QUEUE, "Queue");
2388 if (!folder->outbox)
2389 folder->outbox = imap_create_special_folder
2390 (folder, F_OUTBOX, "Sent");
2392 folder->draft = imap_create_special_folder
2393 (folder, F_DRAFT, "Drafts");
2396 static FolderItem *imap_create_special_folder(Folder *folder,
2397 SpecialFolderItemType stype,
2401 FolderItem *new_item;
2403 g_return_val_if_fail(folder != NULL, NULL);
2404 g_return_val_if_fail(folder->node != NULL, NULL);
2405 g_return_val_if_fail(folder->node->data != NULL, NULL);
2406 g_return_val_if_fail(folder->account != NULL, NULL);
2407 g_return_val_if_fail(name != NULL, NULL);
2409 item = FOLDER_ITEM(folder->node->data);
2410 new_item = imap_create_folder(folder, item, name);
2413 g_warning("Can't create '%s'\n", name);
2414 if (!folder->inbox) return NULL;
2416 new_item = imap_create_folder(folder, folder->inbox, name);
2418 g_warning("Can't create '%s' under INBOX\n", name);
2420 new_item->stype = stype;
2422 new_item->stype = stype;
2427 static gchar *imap_folder_get_path(Folder *folder)
2431 g_return_val_if_fail(folder != NULL, NULL);
2432 g_return_val_if_fail(folder->account != NULL, NULL);
2434 folder_path = g_strconcat(get_imap_cache_dir(),
2436 folder->account->recv_server,
2438 folder->account->userid,
2444 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2446 gchar *folder_path, *path;
2448 g_return_val_if_fail(folder != NULL, NULL);
2449 g_return_val_if_fail(item != NULL, NULL);
2450 folder_path = imap_folder_get_path(folder);
2452 g_return_val_if_fail(folder_path != NULL, NULL);
2453 if (folder_path[0] == G_DIR_SEPARATOR) {
2455 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2458 path = g_strdup(folder_path);
2461 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2462 folder_path, G_DIR_SEPARATOR_S,
2465 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2468 g_free(folder_path);
2473 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2476 gchar *dirpath, *imap_path;
2477 IMAPSession *session;
2478 FolderItem *new_item;
2483 gboolean no_select = FALSE, no_sub = FALSE;
2484 gboolean exist = FALSE;
2486 g_return_val_if_fail(folder != NULL, NULL);
2487 g_return_val_if_fail(folder->account != NULL, NULL);
2488 g_return_val_if_fail(parent != NULL, NULL);
2489 g_return_val_if_fail(name != NULL, NULL);
2491 debug_print("getting session...\n");
2492 session = imap_session_get(folder);
2497 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2498 dirpath = g_strdup(name);
2499 }else if (parent->path)
2500 dirpath = g_strconcat(parent->path, "/", name, NULL);
2501 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2502 dirpath = g_strdup(name);
2503 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2506 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2507 strtailchomp(imap_dir, '/');
2508 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2510 dirpath = g_strdup(name);
2514 /* keep trailing directory separator to create a folder that contains
2516 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2518 strtailchomp(dirpath, '/');
2519 Xstrdup_a(new_name, name, {
2523 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2524 imap_path_separator_subst(imap_path, separator);
2525 /* remove trailing / for display */
2526 strtailchomp(new_name, '/');
2528 if (strcmp(dirpath, "INBOX") != 0) {
2533 argbuf = g_ptr_array_new();
2534 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2535 if (r != MAILIMAP_NO_ERROR) {
2536 imap_handle_error(SESSION(session), r);
2537 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2540 ptr_array_free_strings(argbuf);
2541 g_ptr_array_free(argbuf, TRUE);
2545 if (clist_count(lep_list) > 0)
2547 mailimap_list_result_free(lep_list);
2550 ok = imap_cmd_create(session, imap_path);
2551 if (ok != MAILIMAP_NO_ERROR) {
2552 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2557 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2558 if (r == MAILIMAP_NO_ERROR) {
2559 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2560 lep_list, dirpath, TRUE);
2562 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2563 no_select = cur_item->no_select;
2564 no_sub = cur_item->no_sub;
2565 g_slist_free(item_list);
2567 mailimap_list_result_free(lep_list);
2569 imap_handle_error(SESSION(session), r);
2572 imap_threaded_subscribe(folder, imap_path, TRUE);
2576 /* just get flags */
2577 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2578 if (r == MAILIMAP_NO_ERROR) {
2579 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2580 lep_list, dirpath, TRUE);
2582 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2583 no_select = cur_item->no_select;
2584 no_sub = cur_item->no_sub;
2585 g_slist_free(item_list);
2587 mailimap_list_result_free(lep_list);
2589 imap_handle_error(SESSION(session), r);
2593 new_item = folder_item_new(folder, new_name, dirpath);
2594 new_item->no_select = no_select;
2595 new_item->no_sub = no_sub;
2596 folder_item_append(parent, new_item);
2600 dirpath = folder_item_get_path(new_item);
2601 if (!is_dir_exist(dirpath))
2602 make_dir_hier(dirpath);
2606 /* folder existed, scan it */
2607 imap_scan_required(folder, new_item);
2608 folder_item_scan_full(new_item, FALSE);
2614 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2619 gchar *real_oldpath;
2620 gchar *real_newpath;
2622 gchar *old_cache_dir;
2623 gchar *new_cache_dir;
2624 IMAPSession *session;
2627 gint exists, recent, unseen;
2628 guint32 uid_validity;
2630 g_return_val_if_fail(folder != NULL, -1);
2631 g_return_val_if_fail(item != NULL, -1);
2632 g_return_val_if_fail(item->path != NULL, -1);
2633 g_return_val_if_fail(name != NULL, -1);
2635 debug_print("getting session...\n");
2636 session = imap_session_get(folder);
2641 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2642 g_warning(_("New folder name must not contain the namespace "
2647 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2649 g_free(session->mbox);
2650 session->mbox = NULL;
2651 session->exists = 0;
2652 session->recent = 0;
2653 session->expunge = 0;
2654 ok = imap_cmd_examine(session, "INBOX",
2655 &exists, &recent, &unseen, &uid_validity, FALSE);
2656 if (ok != MAILIMAP_NO_ERROR) {
2657 g_free(real_oldpath);
2661 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2662 if (strchr(item->path, G_DIR_SEPARATOR)) {
2663 dirpath = g_path_get_dirname(item->path);
2664 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2667 newpath = g_strdup(name);
2669 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2670 imap_path_separator_subst(real_newpath, separator);
2672 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2673 if (ok != MAILIMAP_NO_ERROR) {
2674 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2675 real_oldpath, real_newpath);
2676 g_free(real_oldpath);
2678 g_free(real_newpath);
2682 item->name = g_strdup(name);
2684 old_cache_dir = folder_item_get_path(item);
2686 paths[0] = g_strdup(item->path);
2688 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2689 imap_rename_folder_func, paths);
2691 if (is_dir_exist(old_cache_dir)) {
2692 new_cache_dir = folder_item_get_path(item);
2693 if (rename(old_cache_dir, new_cache_dir) < 0) {
2694 FILE_OP_ERROR(old_cache_dir, "rename");
2696 g_free(new_cache_dir);
2699 g_free(old_cache_dir);
2702 g_free(real_oldpath);
2703 g_free(real_newpath);
2707 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2711 IMAPSession *session;
2712 debug_print("getting session...\n");
2714 session = imap_session_get(folder);
2718 if (item && item->path) {
2719 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2722 if (!strcmp(path, "INBOX") && sub == FALSE)
2724 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2725 r = imap_threaded_subscribe(folder, path, sub);
2728 r = imap_threaded_subscribe(folder, rpath, sub);
2734 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2737 IMAPSession *session;
2740 gboolean selected_folder;
2742 g_return_val_if_fail(folder != NULL, -1);
2743 g_return_val_if_fail(item != NULL, -1);
2744 g_return_val_if_fail(item->path != NULL, -1);
2746 debug_print("getting session...\n");
2747 session = imap_session_get(folder);
2751 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2753 imap_threaded_subscribe(folder, path, FALSE);
2755 selected_folder = (session->mbox != NULL) &&
2756 (!strcmp(session->mbox, item->path));
2757 if (selected_folder) {
2758 ok = imap_cmd_close(session);
2759 if (ok != MAILIMAP_NO_ERROR) {
2760 debug_print("close err %d\n", ok);
2761 imap_handle_error(SESSION(session), ok);
2765 ok = imap_cmd_delete(session, path);
2766 if (ok != MAILIMAP_NO_ERROR) {
2767 gchar *tmp = g_strdup_printf("%s%c", path,
2768 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2771 ok = imap_cmd_delete(session, path);
2774 if (ok != MAILIMAP_NO_ERROR) {
2775 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2781 cache_dir = folder_item_get_path(item);
2782 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2783 g_warning("can't remove directory '%s'\n", cache_dir);
2785 folder_item_remove(item);
2789 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2793 g_return_val_if_fail(item != NULL, -1);
2794 g_return_val_if_fail(item->folder != NULL, -1);
2795 g_return_val_if_fail(item->node != NULL, -1);
2797 node = item->node->children;
2798 while (node != NULL) {
2800 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2804 debug_print("IMAP removing %s\n", item->path);
2806 if (imap_remove_all_msg(folder, item) < 0)
2808 return imap_remove_folder_real(folder, item);
2811 typedef struct _uncached_data {
2812 IMAPSession *session;
2814 MsgNumberList *numlist;
2821 static void *imap_get_uncached_messages_thread(void *data)
2823 uncached_data *stuff = (uncached_data *)data;
2824 IMAPSession *session = stuff->session;
2825 FolderItem *item = stuff->item;
2826 MsgNumberList *numlist = stuff->numlist;
2827 GSList *newlist = NULL;
2828 GSList *llast = NULL;
2829 GSList *seq_list, *cur;
2830 gboolean got_alien_tags = FALSE;
2832 debug_print("uncached_messages\n");
2834 if (session == NULL || item == NULL || item->folder == NULL
2835 || FOLDER_CLASS(item->folder) != &imap_class) {
2840 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2841 debug_print("get msgs info\n");
2842 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2843 struct mailimap_set * imapset;
2849 if (session->cancelled)
2852 imapset = cur->data;
2854 r = imap_threaded_fetch_env(session->folder,
2855 imapset, &env_list);
2856 if (r != MAILIMAP_NO_ERROR) {
2857 imap_handle_error(SESSION(session), r);
2865 session_set_access_time(SESSION(session));
2868 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2869 struct imap_fetch_env_info * info;
2871 GSList *tags = NULL, *cur = NULL;
2872 info = carray_get(env_list, i);
2873 tags = carray_get(env_list, i+1);
2874 msginfo = imap_envelope_from_lep(info, item);
2875 if (msginfo == NULL) {
2876 slist_free_strings(tags);
2881 g_slist_free(msginfo->tags);
2882 msginfo->tags = NULL;
2884 for (cur = tags; cur; cur = cur->next) {
2885 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2887 id = tags_get_id_for_str(real_tag);
2889 id = tags_add_tag(real_tag);
2890 got_alien_tags = TRUE;
2892 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2893 msginfo->tags = g_slist_append(
2895 GINT_TO_POINTER(id));
2899 slist_free_strings(tags);
2901 msginfo->folder = item;
2903 llast = newlist = g_slist_append(newlist, msginfo);
2905 llast = g_slist_append(llast, msginfo);
2906 llast = llast->next;
2911 imap_fetch_env_free(env_list);
2914 if (got_alien_tags) {
2916 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2919 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2920 struct mailimap_set * imapset;
2922 imapset = cur->data;
2923 mailimap_set_free(imapset);
2926 session_set_access_time(SESSION(session));
2931 #define MAX_MSG_NUM 50
2933 static GSList *imap_get_uncached_messages(IMAPSession *session,
2935 MsgNumberList *numlist,
2938 GSList *result = NULL;
2940 uncached_data *data = g_new0(uncached_data, 1);
2945 data->total = g_slist_length(numlist);
2946 data->ok = MAILIMAP_NO_ERROR;
2947 debug_print("messages list : %i\n", data->total);
2949 while (cur != NULL) {
2950 GSList * partial_result;
2958 while (count < MAX_MSG_NUM) {
2963 if (newlist == NULL)
2964 llast = newlist = g_slist_append(newlist, p);
2966 llast = g_slist_append(llast, p);
2967 llast = llast->next;
2977 data->session = session;
2979 data->numlist = newlist;
2982 if (prefs_common.work_offline &&
2983 !inc_offline_should_override(FALSE,
2984 _("Claws Mail needs network access in order "
2985 "to access the IMAP server."))) {
2991 (GSList *)imap_get_uncached_messages_thread(data);
2993 if (data->ok != MAILIMAP_NO_ERROR) {
2996 statusbar_progress_all(data->cur,data->total, 1);
2998 g_slist_free(newlist);
3000 result = g_slist_concat(result, partial_result);
3005 statusbar_progress_all(0,0,0);
3006 statusbar_pop_all();
3011 static void imap_delete_all_cached_messages(FolderItem *item)
3015 g_return_if_fail(item != NULL);
3016 g_return_if_fail(item->folder != NULL);
3017 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3019 debug_print("Deleting all cached messages...\n");
3021 dir = folder_item_get_path(item);
3022 if (is_dir_exist(dir))
3023 remove_all_numbered_files(dir);
3026 debug_print("done.\n");
3029 gchar imap_get_path_separator_for_item(FolderItem *item)
3031 Folder *folder = NULL;
3032 IMAPFolder *imap_folder = NULL;
3033 IMAPSession *session = NULL;
3038 folder = item->folder;
3043 imap_folder = IMAP_FOLDER(folder);
3048 debug_print("getting session...");
3049 session = imap_session_get(FOLDER(folder));
3050 result = imap_get_path_separator(session, imap_folder, item->path);
3054 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
3058 gchar separator = '\0';
3060 g_return_val_if_fail(session != NULL, '/');
3061 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3063 if (r != MAILIMAP_NO_ERROR) {
3064 imap_handle_error(SESSION(session), r);
3065 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3069 if (clist_count(lep_list) > 0) {
3070 clistiter * iter = clist_begin(lep_list);
3071 struct mailimap_mailbox_list * mb;
3072 mb = clist_content(iter);
3074 separator = mb->mb_delimiter;
3075 debug_print("got separator: %c\n", folder->last_seen_separator);
3077 mailimap_list_result_free(lep_list);
3081 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3083 gchar separator = '/';
3085 if (folder->last_seen_separator == 0) {
3086 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
3089 if (folder->last_seen_separator == 0) {
3090 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
3093 if (folder->last_seen_separator != 0) {
3094 debug_print("using separator: %c\n", folder->last_seen_separator);
3095 return folder->last_seen_separator;
3101 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3106 g_return_val_if_fail(folder != NULL, NULL);
3107 g_return_val_if_fail(path != NULL, NULL);
3109 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3110 separator = imap_get_path_separator(session, folder, path);
3111 imap_path_separator_subst(real_path, separator);
3116 static gint imap_set_message_flags(IMAPSession *session,
3117 IMAPFolderItem *item,
3118 MsgNumberList *numlist,
3127 IMAPFolder *folder = NULL;
3128 GSList *sorted_list = NULL;
3130 if (numlist == NULL || session == NULL)
3131 return MAILIMAP_ERROR_BAD_STATE;
3133 folder = IMAP_FOLDER(session->folder);
3135 sorted_list = g_slist_copy(numlist);
3136 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3138 cur = g_slist_last(sorted_list);
3141 total = GPOINTER_TO_INT(cur->data);
3143 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3145 statusbar_print_all(_("Flagging messages..."));
3147 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3148 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3149 struct mailimap_set_item *set_item = clist_content(
3150 clist_begin(imapset->set_list));
3152 statusbar_progress_all(set_item->set_first, total, 1);
3154 ok = imap_cmd_store(session, item, imapset,
3155 flags, tags, is_set);
3156 statusbar_progress_all(set_item->set_last, total, 1);
3157 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3158 /* reduce max set size */
3159 folder->max_set_size /= 2;
3161 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3166 g_slist_free(sorted_list);
3168 statusbar_progress_all(0,0,0);
3169 statusbar_pop_all();
3171 imap_lep_set_free(seq_list);
3176 typedef struct _select_data {
3177 IMAPSession *session;
3182 guint32 *uid_validity;
3186 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3188 gint *exists, gint *recent, gint *unseen,
3189 guint32 *uid_validity, gint *can_create_flags,
3194 gint exists_, recent_, unseen_;
3195 guint32 uid_validity_;
3196 gint can_create_flags_;
3197 const gchar *path = item ? item->path:NULL;
3200 return MAILIMAP_ERROR_BAD_STATE;
3203 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3204 if (session->mbox && strcmp(session->mbox, path) == 0)
3205 return MAILIMAP_NO_ERROR;
3207 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3208 if (session->mbox && strcmp(session->mbox, path) == 0) {
3209 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3210 return MAILIMAP_NO_ERROR;
3220 uid_validity = &uid_validity_;
3221 if (!can_create_flags)
3222 can_create_flags = &can_create_flags_;
3224 g_free(session->mbox);
3225 session->mbox = NULL;
3226 session->exists = 0;
3227 session->recent = 0;
3228 session->expunge = 0;
3230 real_path = imap_get_real_path(session, folder, path);
3232 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3233 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3234 ok = imap_cmd_select(session, real_path,
3235 exists, recent, unseen, uid_validity, can_create_flags,
3236 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3237 if (ok != MAILIMAP_NO_ERROR) {
3238 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3240 session->mbox = g_strdup(path);
3241 session->folder_content_changed = FALSE;
3242 session->exists = *exists;
3243 session->recent = *recent;
3244 session->expunge = 0;
3245 session->unseen = *unseen;
3246 session->uid_validity = *uid_validity;
3247 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3248 session->exists, session->recent, session->expunge,
3249 session->uid_validity, *can_create_flags);
3251 if (*can_create_flags) {
3252 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3254 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3261 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3262 const gchar *path, IMAPFolderItem *item,
3264 guint32 *uid_next, guint32 *uid_validity,
3265 gint *unseen, gboolean block)
3269 struct mailimap_mailbox_data_status * data_status;
3274 real_path = imap_get_real_path(session, folder, path);
3293 if (session->mbox != NULL &&
3294 !strcmp(session->mbox, item->item.path)) {
3295 r = imap_cmd_close(session);
3296 if (r != MAILIMAP_NO_ERROR) {
3297 imap_handle_error(SESSION(session), r);
3298 debug_print("close err %d\n", r);
3303 r = imap_threaded_status(FOLDER(folder), real_path,
3304 &data_status, mask);
3307 if (r != MAILIMAP_NO_ERROR) {
3308 imap_handle_error(SESSION(session), r);
3309 debug_print("status err %d\n", r);
3313 if (data_status->st_info_list == NULL) {
3314 mailimap_mailbox_data_status_free(data_status);
3315 debug_print("status->st_info_list == NULL\n");
3316 return MAILIMAP_ERROR_BAD_STATE;
3320 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3321 iter = clist_next(iter)) {
3322 struct mailimap_status_info * info;
3324 info = clist_content(iter);
3325 switch (info->st_att) {
3326 case MAILIMAP_STATUS_ATT_MESSAGES:
3328 * messages = info->st_value;
3329 got_values |= 1 << 0;
3333 case MAILIMAP_STATUS_ATT_UIDNEXT:
3335 * uid_next = info->st_value;
3336 got_values |= 1 << 2;
3340 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3342 * uid_validity = info->st_value;
3343 got_values |= 1 << 3;
3347 case MAILIMAP_STATUS_ATT_UNSEEN:
3349 * unseen = info->st_value;
3350 got_values |= 1 << 4;
3355 mailimap_mailbox_data_status_free(data_status);
3357 if (got_values != mask) {
3358 g_warning("status: incomplete values received (%d)\n", got_values);
3360 return MAILIMAP_NO_ERROR;
3363 static void imap_free_capabilities(IMAPSession *session)
3365 slist_free_strings(session->capability);
3366 g_slist_free(session->capability);
3367 session->capability = NULL;
3370 /* low-level IMAP4rev1 commands */
3372 static gint imap_cmd_login(IMAPSession *session,
3373 const gchar *user, const gchar *pass,
3379 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3380 gint ok = MAILIMAP_ERROR_BAD_STATE;
3381 if (imap_has_capability(session, "STARTTLS")) {
3382 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3383 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3384 ok = imap_cmd_starttls(session);
3385 if (ok != MAILIMAP_NO_ERROR) {
3386 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3390 imap_free_capabilities(session);
3391 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3392 imap_handle_error(SESSION(session), r);
3393 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3398 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3399 "server requires TLS, but Claws Mail "
3400 "has been compiled without OpenSSL "
3402 SESSION(session)->server);
3403 return MAILIMAP_ERROR_BAD_STATE;
3406 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3407 return MAILIMAP_ERROR_BAD_STATE;
3411 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3413 SESSION(session)->server,
3415 r = imap_threaded_login(session->folder, user, pass, type);
3416 if (r != MAILIMAP_NO_ERROR) {
3417 imap_handle_error(SESSION(session), r);
3418 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3419 SESSION(session)->server);
3422 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3423 SESSION(session)->server);
3424 ok = MAILIMAP_NO_ERROR;
3429 static gint imap_cmd_noop(IMAPSession *session)
3432 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3434 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3435 if (r != MAILIMAP_NO_ERROR) {
3436 imap_handle_error(SESSION(session), r);
3437 debug_print("noop err %d\n", r);
3441 session->folder_content_changed = FALSE;
3443 if ((exists && exists != session->exists)
3444 || (recent && recent != session->recent)
3445 || (expunge && expunge != session->expunge)
3446 || (unseen && unseen != session->unseen)) {
3447 session->folder_content_changed = TRUE;
3449 if (uidnext != 0 && uidnext != session->uid_next) {
3450 session->uid_next = uidnext;
3451 session->folder_content_changed = TRUE;
3453 if (uidval != 0 && uidval != session->uid_validity) {
3454 session->uid_validity = uidval;
3455 session->folder_content_changed = TRUE;
3458 session->exists = exists;
3459 session->recent = recent;
3460 session->expunge = expunge;
3461 session->unseen = unseen;
3463 session_set_access_time(SESSION(session));
3465 return MAILIMAP_NO_ERROR;
3468 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3469 static gint imap_cmd_starttls(IMAPSession *session)
3473 r = imap_threaded_starttls(session->folder,
3474 SESSION(session)->server, SESSION(session)->port);
3475 if (r != MAILIMAP_NO_ERROR) {
3476 imap_handle_error(SESSION(session), r);
3477 debug_print("starttls err %d\n", r);
3480 return MAILIMAP_NO_ERROR;
3484 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3485 gint *exists, gint *recent, gint *unseen,
3486 guint32 *uid_validity, gint *can_create_flags,
3487 GSList **ok_flags, gboolean block)
3491 r = imap_threaded_select(session->folder, folder,
3492 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3493 if (r != MAILIMAP_NO_ERROR) {
3494 imap_handle_error(SESSION(session), r);
3495 debug_print("select err %d\n", r);
3498 return MAILIMAP_NO_ERROR;
3501 static gint imap_cmd_close(IMAPSession *session)
3505 r = imap_threaded_close(session->folder);
3506 if (r != MAILIMAP_NO_ERROR) {
3507 imap_handle_error(SESSION(session), r);
3508 debug_print("close err %d\n", r);
3511 g_free(session->mbox);
3512 session->mbox = NULL;
3513 session->exists = 0;
3514 session->recent = 0;
3515 session->expunge = 0;
3516 return MAILIMAP_NO_ERROR;
3519 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3520 gint *exists, gint *recent, gint *unseen,
3521 guint32 *uid_validity, gboolean block)
3525 r = imap_threaded_examine(session->folder, folder,
3526 exists, recent, unseen, uid_validity);
3527 if (r != MAILIMAP_NO_ERROR) {
3528 imap_handle_error(SESSION(session), r);
3529 debug_print("examine err %d\n", r);
3533 return MAILIMAP_NO_ERROR;
3536 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3540 r = imap_threaded_create(session->folder, folder);
3541 if (r != MAILIMAP_NO_ERROR) {
3542 imap_handle_error(SESSION(session), r);
3547 return MAILIMAP_NO_ERROR;
3550 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3551 const gchar *new_folder)
3555 r = imap_threaded_rename(session->folder, old_folder,
3557 if (r != MAILIMAP_NO_ERROR) {
3558 imap_handle_error(SESSION(session), r);
3562 return MAILIMAP_NO_ERROR;
3565 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3570 r = imap_threaded_delete(session->folder, folder);
3571 if (r != MAILIMAP_NO_ERROR) {
3572 imap_handle_error(SESSION(session), r);
3576 return MAILIMAP_NO_ERROR;
3579 typedef struct _fetch_data {
3580 IMAPSession *session;
3582 const gchar *filename;
3588 static void *imap_cmd_fetch_thread(void *data)
3590 fetch_data *stuff = (fetch_data *)data;
3591 IMAPSession *session = stuff->session;
3592 guint32 uid = stuff->uid;
3593 const gchar *filename = stuff->filename;
3597 r = imap_threaded_fetch_content(session->folder,
3601 r = imap_threaded_fetch_content(session->folder,
3604 if (r != MAILIMAP_NO_ERROR) {
3605 imap_handle_error(SESSION(session), r);
3606 debug_print("fetch err %d\n", r);
3607 return GINT_TO_POINTER(MAILIMAP_ERROR_BAD_STATE);
3609 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3612 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3613 const gchar *filename, gboolean headers,
3616 fetch_data *data = g_new0(fetch_data, 1);
3619 data->session = session;
3621 data->filename = filename;
3622 data->headers = headers;
3625 if (prefs_common.work_offline &&
3626 !inc_offline_should_override(FALSE,
3627 _("Claws Mail needs network access in order "
3628 "to access the IMAP server."))) {
3632 statusbar_print_all(_("Fetching message..."));
3633 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3634 statusbar_pop_all();
3640 static gint imap_cmd_append(IMAPSession *session,
3641 IMAPFolderItem *item,
3642 const gchar *destfolder,
3643 const gchar *file, IMAPFlags flags,
3646 struct mailimap_flag_list * flag_list;
3649 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3651 flag_list = imap_flag_to_lep(item, flags, NULL);
3652 lock_session(session);
3653 r = imap_threaded_append(session->folder, destfolder,
3654 file, flag_list, (int *)new_uid);
3655 mailimap_flag_list_free(flag_list);
3657 if (r != MAILIMAP_NO_ERROR) {
3658 imap_handle_error(SESSION(session), r);
3659 debug_print("append err %d\n", r);
3663 unlock_session(session);
3665 return MAILIMAP_NO_ERROR;
3668 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3669 const gchar *destfolder, GRelation *uid_mapping,
3670 struct mailimap_set **source, struct mailimap_set **dest)
3674 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3675 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3676 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3678 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3679 if (r != MAILIMAP_NO_ERROR) {
3680 imap_handle_error(SESSION(session), r);
3684 return MAILIMAP_NO_ERROR;
3687 static gint imap_cmd_store(IMAPSession *session,
3688 IMAPFolderItem *item,
3689 struct mailimap_set * set,
3690 IMAPFlags flags, GSList *tags, int do_add)
3693 struct mailimap_flag_list * flag_list = NULL;
3694 struct mailimap_store_att_flags * store_att_flags;
3696 flag_list = imap_flag_to_lep(item, flags, tags);
3700 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3703 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3705 r = imap_threaded_store(session->folder, set, store_att_flags);
3706 mailimap_store_att_flags_free(store_att_flags);
3707 if (r != MAILIMAP_NO_ERROR) {
3708 imap_handle_error(SESSION(session), r);
3712 return MAILIMAP_NO_ERROR;
3715 static gint imap_cmd_expunge(IMAPSession *session)
3719 if (prefs_common.work_offline &&
3720 !inc_offline_should_override(FALSE,
3721 _("Claws Mail needs network access in order "
3722 "to access the IMAP server."))) {
3726 r = imap_threaded_expunge(session->folder);
3727 if (r != MAILIMAP_NO_ERROR) {
3728 imap_handle_error(SESSION(session), r);
3732 return MAILIMAP_NO_ERROR;
3735 static void imap_path_separator_subst(gchar *str, gchar separator)
3738 gboolean in_escape = FALSE;
3740 if (!separator || separator == '/') return;
3742 for (p = str; *p != '\0'; p++) {
3743 if (*p == '/' && !in_escape)
3745 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3747 else if (*p == '-' && in_escape)
3752 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3754 FolderItem *item = node->data;
3755 gchar **paths = data;
3756 const gchar *oldpath = paths[0];
3757 const gchar *newpath = paths[1];
3758 gchar *real_oldpath, *real_newpath;
3760 gchar *new_itempath;
3762 IMAPSession *session = imap_session_get(item->folder);
3764 oldpathlen = strlen(oldpath);
3765 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3766 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3770 base = item->path + oldpathlen;
3771 while (*base == G_DIR_SEPARATOR) base++;
3773 new_itempath = g_strdup(newpath);
3775 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3778 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3780 item->path = new_itempath;
3782 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3784 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3785 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3787 g_free(real_oldpath);
3788 g_free(real_newpath);
3792 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3794 GSList *uidlist, *elem;
3796 clist * lep_uidlist;
3797 gint ok, nummsgs = 0, lastuid_old;
3799 if (session == NULL) {
3803 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3804 NULL, NULL, NULL, NULL, NULL, TRUE);
3805 if (ok != MAILIMAP_NO_ERROR) {
3809 g_slist_free(item->uid_list);
3810 item->uid_list = NULL;
3814 if (folder->account && folder->account->low_bandwidth) {
3815 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3819 if (r == MAILIMAP_NO_ERROR) {
3820 GSList * fetchuid_list =
3821 imap_uid_list_from_lep(lep_uidlist);
3822 mailimap_search_result_free(lep_uidlist);
3824 uidlist = g_slist_concat(fetchuid_list, uidlist);
3826 carray * lep_uidtab;
3827 if (r != -1) { /* inited */
3828 imap_handle_error(SESSION(session), r);
3832 r = imap_threaded_fetch_uid(folder, 1,
3834 if (r == MAILIMAP_NO_ERROR) {
3835 GSList * fetchuid_list =
3836 imap_uid_list_from_lep_tab(lep_uidtab);
3837 imap_fetch_uid_list_free(lep_uidtab);
3838 uidlist = g_slist_concat(fetchuid_list, uidlist);
3842 if (r != MAILIMAP_NO_ERROR) {
3843 imap_handle_error(SESSION(session), r);
3847 lastuid_old = item->lastuid;
3849 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3852 msgnum = GPOINTER_TO_INT(elem->data);
3854 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3855 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3858 g_slist_free(uidlist);
3860 unlock_session(session); /* locked from imap_get_num_list */
3866 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3868 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3869 IMAPSession *session;
3871 GSList *uidlist = NULL;
3873 gboolean selected_folder;
3874 gint known_list_len = 0;
3875 debug_print("get_num_list\n");
3877 g_return_val_if_fail(folder != NULL, -1);
3878 g_return_val_if_fail(item != NULL, -1);
3879 g_return_val_if_fail(item->item.path != NULL, -1);
3880 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3881 g_return_val_if_fail(folder->account != NULL, -1);
3883 known_list_len = g_slist_length(item->uid_list);
3884 if (!item->should_update) {
3885 debug_print("get_num_list: nothing to update\n");
3886 *old_uids_valid = TRUE;
3887 if (known_list_len == item->item.total_msgs
3888 && known_list_len > 0) {
3889 *msgnum_list = g_slist_copy(item->uid_list);
3890 return known_list_len;
3892 debug_print("don't know the list length...\n");
3896 if (prefs_common.work_offline &&
3897 !inc_offline_should_override(FALSE,
3898 _("Claws Mail needs network access in order "
3899 "to access the IMAP server."))) {
3903 debug_print("getting session...\n");
3904 session = imap_session_get(folder);
3905 g_return_val_if_fail(session != NULL, -1);
3907 lock_session(session); /* unlocked by get_list_of_uids */
3908 if (FOLDER_ITEM(item)->path)
3909 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3910 FOLDER_ITEM(item)->folder->name,
3912 FOLDER_ITEM(item)->path);
3914 statusbar_print_all(_("Scanning folder %s ..."),
3915 FOLDER_ITEM(item)->folder->name);
3917 selected_folder = (session->mbox != NULL) &&
3918 (!strcmp(session->mbox, item->item.path));
3920 if (item->should_trash_cache) {
3921 *old_uids_valid = FALSE;
3922 debug_print("get_num_list: trashing num list\n");
3923 debug_print("Freeing imap uid cache\n");
3925 g_slist_free(item->uid_list);
3926 item->uid_list = NULL;
3928 imap_delete_all_cached_messages((FolderItem *)item);
3930 debug_print("get_num_list: updating num list\n");
3931 *old_uids_valid = TRUE;
3934 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3935 /* session could be broken now, in case of fatal error */
3937 debug_print("get_num_list: got %d msgs\n", nummsgs);
3940 statusbar_pop_all();
3944 *msgnum_list = uidlist;
3946 dir = folder_item_get_path((FolderItem *)item);
3947 debug_print("removing old messages from %s\n", dir);
3948 remove_numbered_files_not_in_list(dir, *msgnum_list);
3951 debug_print("get_num_list - ok - %i\n", nummsgs);
3952 statusbar_pop_all();
3953 item->should_trash_cache = FALSE;
3954 item->should_update = FALSE;
3958 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3963 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3964 flags.tmp_flags = 0;
3966 g_return_val_if_fail(item != NULL, NULL);
3967 g_return_val_if_fail(file != NULL, NULL);
3969 if (folder_has_parent_of_type(item, F_QUEUE)) {
3970 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3971 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3972 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3975 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3976 if (!msginfo) return NULL;
3978 msginfo->plaintext_file = g_strdup(file);
3979 msginfo->folder = item;
3984 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3985 GSList *msgnum_list)
3987 IMAPSession *session;
3988 MsgInfoList *ret = NULL;
3991 debug_print("get_msginfos\n");
3993 g_return_val_if_fail(folder != NULL, NULL);
3994 g_return_val_if_fail(item != NULL, NULL);
3995 g_return_val_if_fail(msgnum_list != NULL, NULL);
3997 debug_print("getting session...\n");
3998 session = imap_session_get(folder);
3999 g_return_val_if_fail(session != NULL, NULL);
4001 lock_session(session); /* unlocked later in the function */
4003 debug_print("IMAP getting msginfos\n");
4004 ok = imap_select(session, IMAP_FOLDER(folder), item,
4005 NULL, NULL, NULL, NULL, NULL, FALSE);
4006 if (ok != MAILIMAP_NO_ERROR) {
4009 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4010 folder_has_parent_of_type(item, F_QUEUE))) {
4011 ret = g_slist_concat(ret,
4012 imap_get_uncached_messages(session, item,
4014 if (ok != MAILIMAP_NO_ERROR)
4016 unlock_session(session);
4018 MsgNumberList *sorted_list, *elem, *llast = NULL;
4019 gint startnum, lastnum;
4021 unlock_session(session);
4023 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4025 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4027 llast = g_slist_last(ret);
4028 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4032 num = GPOINTER_TO_INT(elem->data);
4034 if (num > lastnum + 1 || elem == NULL) {
4036 for (i = startnum; i <= lastnum; ++i) {
4038 file = imap_fetch_msg(folder, item, i);
4040 MsgInfo *msginfo = imap_parse_msg(file, item);
4041 if (msginfo != NULL) {
4042 msginfo->msgnum = i;
4044 llast = ret = g_slist_append(ret, msginfo);
4046 llast = g_slist_append(llast, msginfo);
4047 llast = llast->next;
4062 g_slist_free(sorted_list);
4067 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4069 MsgInfo *msginfo = NULL;
4070 MsgInfoList *msginfolist;
4071 MsgNumberList numlist;
4073 numlist.next = NULL;
4074 numlist.data = GINT_TO_POINTER(uid);
4076 msginfolist = imap_get_msginfos(folder, item, &numlist);
4077 if (msginfolist != NULL) {
4078 msginfo = msginfolist->data;
4079 g_slist_free(msginfolist);
4085 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4087 IMAPSession *session;
4088 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4089 gint ok, exists = 0, unseen = 0;
4090 guint32 uid_next = 0, uid_val = 0;
4091 gboolean selected_folder;
4093 g_return_val_if_fail(folder != NULL, FALSE);
4094 g_return_val_if_fail(item != NULL, FALSE);
4095 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4096 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4098 if (item->item.path == NULL)
4101 if (item->should_update) {
4102 debug_print("scan already required\n");
4105 debug_print("getting session...\n");
4106 session = imap_session_get(folder);
4108 g_return_val_if_fail(session != NULL, FALSE);
4109 lock_session(session); /* unlocked later in the function */
4111 selected_folder = (session->mbox != NULL) &&
4112 (!strcmp(session->mbox, item->item.path));
4113 if (selected_folder) {
4114 if (!session->folder_content_changed) {
4115 ok = imap_cmd_noop(session);
4116 if (ok != MAILIMAP_NO_ERROR) {
4117 debug_print("disconnected!\n");
4119 session = imap_reconnect_if_possible(folder, session);
4121 session = imap_session_get(folder);
4122 if (session == NULL)
4126 if (session->folder_content_changed) {
4127 debug_print("CHANGED (self-noop)! scan_required\n");
4128 item->should_update = TRUE;
4129 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4130 item->item.mtime = session->uid_validity;
4131 item->should_trash_cache = TRUE;
4133 unlock_session(session);
4137 debug_print("CHANGED (previous noop)! scan_required\n");
4138 item->should_update = TRUE;
4139 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4140 item->item.mtime = session->uid_validity;
4141 item->should_trash_cache = TRUE;
4143 unlock_session(session);
4147 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4148 &exists, &uid_next, &uid_val, &unseen, FALSE);
4149 if (ok != MAILIMAP_NO_ERROR) {
4153 debug_print("exists %d, item->item.total_msgs %d\n",
4154 exists, item->item.total_msgs);
4155 if (exists != item->item.total_msgs
4156 || unseen != item->item.unread_msgs
4157 || uid_next != item->uid_next
4158 || uid_val != item->item.mtime) {
4159 debug_print("CHANGED (status)! scan_required\n");
4160 item->last_change = time(NULL);
4161 item->should_update = TRUE;
4162 item->uid_next = uid_next;
4163 if (uid_val != item->item.mtime) {
4164 item->item.mtime = uid_val;
4165 item->should_trash_cache = TRUE;
4167 unlock_session(session);
4171 unlock_session(session);
4173 item->should_update = FALSE;
4177 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4179 IMAPSession *session;
4180 IMAPFlags flags_set = 0, flags_unset = 0;
4181 gint ok = MAILIMAP_NO_ERROR;
4182 MsgNumberList numlist;
4183 hashtable_data *ht_data = NULL;
4185 g_return_if_fail(folder != NULL);
4186 g_return_if_fail(folder->klass == &imap_class);
4187 g_return_if_fail(item != NULL);
4188 g_return_if_fail(item->folder == folder);
4189 g_return_if_fail(msginfo != NULL);
4190 g_return_if_fail(msginfo->folder == item);
4192 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4193 flags_set |= IMAP_FLAG_FLAGGED;
4194 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4195 flags_unset |= IMAP_FLAG_FLAGGED;
4197 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4198 flags_unset |= IMAP_FLAG_SEEN;
4199 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4200 flags_set |= IMAP_FLAG_SEEN;
4202 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4203 flags_set |= IMAP_FLAG_ANSWERED;
4204 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4205 flags_unset |= IMAP_FLAG_ANSWERED;
4207 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4208 flags_set |= IMAP_FLAG_FORWARDED;
4209 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4210 flags_unset |= IMAP_FLAG_FORWARDED;
4212 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4213 flags_set |= IMAP_FLAG_DELETED;
4214 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4215 flags_unset |= IMAP_FLAG_DELETED;
4217 if (!flags_set && !flags_unset) {
4218 /* the changed flags were not translatable to IMAP-speak.
4219 * like MSG_POSTFILTERED, so just apply. */
4220 msginfo->flags.perm_flags = newflags;
4224 debug_print("getting session...\n");
4225 session = imap_session_get(folder);
4230 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4231 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4234 numlist.next = NULL;
4235 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4237 if (IMAP_FOLDER_ITEM(item)->batching) {
4238 /* instead of performing an UID STORE command for each message change,
4239 * as a lot of them can change "together", we just fill in hashtables
4240 * and defer the treatment so that we're able to send only one
4243 debug_print("IMAP batch mode on, deferring flags change\n");
4245 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4246 GINT_TO_POINTER(flags_set));
4247 if (ht_data == NULL) {
4248 ht_data = g_new0(hashtable_data, 1);
4249 ht_data->item = IMAP_FOLDER_ITEM(item);
4250 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4251 GINT_TO_POINTER(flags_set), ht_data);
4253 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4256 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4257 GINT_TO_POINTER(flags_unset));
4258 if (ht_data == NULL) {
4259 ht_data = g_new0(hashtable_data, 1);
4260 ht_data->item = IMAP_FOLDER_ITEM(item);
4261 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4262 GINT_TO_POINTER(flags_unset), ht_data);
4264 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4265 GINT_TO_POINTER(msginfo->msgnum));
4268 debug_print("IMAP changing flags\n");
4270 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4271 if (ok != MAILIMAP_NO_ERROR) {
4277 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4278 if (ok != MAILIMAP_NO_ERROR) {
4283 msginfo->flags.perm_flags = newflags;
4287 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4290 IMAPSession *session;
4292 MsgNumberList numlist;
4294 g_return_val_if_fail(folder != NULL, -1);
4295 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4296 g_return_val_if_fail(item != NULL, -1);
4298 debug_print("getting session...\n");
4299 session = imap_session_get(folder);
4300 if (!session) return -1;
4302 ok = imap_select(session, IMAP_FOLDER(folder), item,
4303 NULL, NULL, NULL, NULL, NULL, FALSE);
4304 if (ok != MAILIMAP_NO_ERROR) {
4307 numlist.next = NULL;
4308 numlist.data = GINT_TO_POINTER(uid);
4310 ok = imap_set_message_flags
4311 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4312 if (ok != MAILIMAP_NO_ERROR) {
4313 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4317 if (!session->uidplus) {
4318 ok = imap_cmd_expunge(session);
4322 uidstr = g_strdup_printf("%u", uid);
4323 ok = imap_cmd_expunge(session);
4326 if (ok != MAILIMAP_NO_ERROR) {
4327 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4331 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4332 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4333 dir = folder_item_get_path(item);
4334 if (is_dir_exist(dir))
4335 remove_numbered_files(dir, uid, uid);
4337 return MAILIMAP_NO_ERROR;
4340 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4342 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4345 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4349 g_return_val_if_fail(list != NULL, -1);
4351 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4352 if (GPOINTER_TO_INT(elem->data) >= num)
4355 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4358 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4360 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4361 debug_print("flag %d is OK\n", flag);
4364 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4365 debug_print("creating flags is OK\n");
4372 * NEW and DELETED flags are not syncronized
4373 * - The NEW/RECENT flags in IMAP folders can not really be directly
4374 * modified by Sylpheed
4375 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4376 * meaning, in IMAP it always removes the messages from the FolderItem
4377 * in Sylpheed it can mean to move the message to trash
4380 typedef struct _get_flags_data {
4383 MsgInfoList *msginfo_list;
4384 GRelation *msgflags;
4385 gboolean full_search;
4389 static /*gint*/ void *imap_get_flags_thread(void *data)
4391 get_flags_data *stuff = (get_flags_data *)data;
4392 Folder *folder = stuff->folder;
4393 FolderItem *fitem = (FolderItem *) stuff->item;
4394 MsgInfoList *msginfo_list = stuff->msginfo_list;
4395 GRelation *msgflags = stuff->msgflags;
4397 carray * lep_uidtab;
4398 IMAPSession *session;
4400 int r = MAILIMAP_NO_ERROR;
4401 GHashTable *flags_hash = NULL;
4402 GHashTable *tags_hash = NULL;
4403 gboolean full_search = stuff->full_search;
4404 GSList *sorted_list = NULL;
4405 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL;
4406 GSList *seq_list, *cur;
4407 gboolean reverse_seen = FALSE;
4408 gboolean selected_folder;
4409 gint exists_cnt, unseen_cnt;
4410 gboolean got_alien_tags = FALSE;
4412 session = imap_session_get(folder);
4414 if (session == NULL) {
4416 return GINT_TO_POINTER(-1);
4418 selected_folder = (session->mbox != NULL) &&
4419 (!strcmp(session->mbox, fitem->path));
4421 lock_session(session);
4422 if (!selected_folder) {
4423 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4424 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4425 if (ok != MAILIMAP_NO_ERROR) {
4427 return GINT_TO_POINTER(-1);
4430 if (unseen_cnt > exists_cnt / 2)
4431 reverse_seen = TRUE;
4434 if (fitem->unread_msgs > fitem->total_msgs / 2)
4435 reverse_seen = TRUE;
4438 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4440 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4442 struct mailimap_set * set;
4443 set = mailimap_set_new_interval(1, 0);
4444 seq_list = g_slist_append(NULL, set);
4447 if (folder->account && folder->account->low_bandwidth) {
4448 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4449 struct mailimap_set * imapset;
4450 clist * lep_uidlist;
4453 imapset = cur->data;
4455 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4456 full_search ? NULL:imapset, &lep_uidlist);
4459 r = imap_threaded_search(folder,
4460 IMAP_SEARCH_TYPE_UNSEEN,
4461 full_search ? NULL:imapset, &lep_uidlist);
4463 if (r == MAILIMAP_NO_ERROR) {
4466 uidlist = imap_uid_list_from_lep(lep_uidlist);
4467 mailimap_search_result_free(lep_uidlist);
4469 unseen = g_slist_concat(unseen, uidlist);
4471 imap_handle_error(SESSION(session), r);
4475 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4476 full_search ? NULL:imapset, &lep_uidlist);
4477 if (r == MAILIMAP_NO_ERROR) {
4480 uidlist = imap_uid_list_from_lep(lep_uidlist);
4481 mailimap_search_result_free(lep_uidlist);
4483 flagged = g_slist_concat(flagged, uidlist);
4485 imap_handle_error(SESSION(session), r);
4489 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4490 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4491 full_search ? NULL:imapset, &lep_uidlist);
4492 if (r == MAILIMAP_NO_ERROR) {
4495 uidlist = imap_uid_list_from_lep(lep_uidlist);
4496 mailimap_search_result_free(lep_uidlist);
4498 answered = g_slist_concat(answered, uidlist);
4500 imap_handle_error(SESSION(session), r);
4504 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4505 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4506 full_search ? NULL:imapset, &lep_uidlist);
4507 if (r == MAILIMAP_NO_ERROR) {
4510 uidlist = imap_uid_list_from_lep(lep_uidlist);
4511 mailimap_search_result_free(lep_uidlist);
4513 forwarded = g_slist_concat(forwarded, uidlist);
4515 imap_handle_error(SESSION(session), r);
4520 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4521 full_search ? NULL:imapset, &lep_uidlist);
4522 if (r == MAILIMAP_NO_ERROR) {
4525 uidlist = imap_uid_list_from_lep(lep_uidlist);
4526 mailimap_search_result_free(lep_uidlist);
4528 deleted = g_slist_concat(deleted, uidlist);
4530 imap_handle_error(SESSION(session), r);
4537 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4538 if (r == MAILIMAP_NO_ERROR) {
4539 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4540 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4541 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4542 imap_fetch_uid_flags_list_free(lep_uidtab);
4544 imap_handle_error(SESSION(session), r);
4550 if (r == MAILIMAP_NO_ERROR)
4551 unlock_session(session);
4553 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4555 MsgPermFlags flags, oldflags;
4558 msginfo = (MsgInfo *) elem->data;
4559 flags = msginfo->flags.perm_flags;
4560 wasnew = (flags & MSG_NEW);
4561 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED);
4563 if (folder->account && folder->account->low_bandwidth) {
4564 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4565 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED);
4567 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4570 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4571 if (gslist_find_next_num(&unseen, msginfo->msgnum) == msginfo->msgnum) {
4572 if (!reverse_seen) {
4573 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4575 flags &= ~(MSG_UNREAD | MSG_NEW);
4579 if (gslist_find_next_num(&flagged, msginfo->msgnum) == msginfo->msgnum)
4580 flags |= MSG_MARKED;
4582 flags &= ~MSG_MARKED;
4584 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4585 if (gslist_find_next_num(&answered, msginfo->msgnum) == msginfo->msgnum)
4586 flags |= MSG_REPLIED;
4588 flags &= ~MSG_REPLIED;
4589 if (gslist_find_next_num(&forwarded, msginfo->msgnum) == msginfo->msgnum)
4590 flags |= MSG_FORWARDED;
4592 flags &= ~MSG_FORWARDED;
4593 if (gslist_find_next_num(&deleted, msginfo->msgnum) == msginfo->msgnum)
4594 flags |= MSG_DELETED;
4596 flags &= ~MSG_DELETED;
4599 if (flags_hash != NULL) {
4601 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4602 GINT_TO_POINTER(msginfo->msgnum)));
4605 if ((flags & MSG_UNREAD) == 0)
4611 if (tags_hash != NULL) {
4612 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4616 g_slist_free(msginfo->tags);
4617 msginfo->tags = NULL;
4619 for (cur = tags; cur; cur = cur->next) {
4620 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4622 id = tags_get_id_for_str(real_tag);
4624 id = tags_add_tag(real_tag);
4625 got_alien_tags = TRUE;
4627 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4628 msginfo->tags = g_slist_append(
4630 GINT_TO_POINTER(id));
4634 slist_free_strings(tags);
4639 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4642 if (got_alien_tags) {
4644 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4648 g_hash_table_destroy(flags_hash);
4650 g_hash_table_destroy(tags_hash);
4652 imap_lep_set_free(seq_list);
4653 g_slist_free(flagged);
4654 g_slist_free(deleted);
4655 g_slist_free(answered);
4656 g_slist_free(forwarded);
4657 g_slist_free(unseen);
4658 g_slist_free(sorted_list);
4661 return GINT_TO_POINTER(0);
4664 static gint imap_get_flags(Folder *folder, FolderItem *item,
4665 MsgInfoList *msginfo_list, GRelation *msgflags)
4668 get_flags_data *data = g_new0(get_flags_data, 1);
4670 data->folder = folder;
4672 data->msginfo_list = msginfo_list;
4673 data->msgflags = msgflags;
4674 data->full_search = FALSE;
4676 GSList *tmp = NULL, *cur;
4678 if (prefs_common.work_offline &&
4679 !inc_offline_should_override(FALSE,
4680 _("Claws Mail needs network access in order "
4681 "to access the IMAP server."))) {
4686 tmp = folder_item_get_msg_list(item);
4688 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4689 data->full_search = TRUE;
4691 for (cur = tmp; cur; cur = cur->next)
4692 procmsg_msginfo_free((MsgInfo *)cur->data);
4696 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4703 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4705 gboolean flags_set = GPOINTER_TO_INT(user_data);
4706 gint flags_value = GPOINTER_TO_INT(key);
4707 hashtable_data *data = (hashtable_data *)value;
4708 IMAPFolderItem *_item = data->item;
4709 FolderItem *item = (FolderItem *)_item;
4710 gint ok = MAILIMAP_ERROR_BAD_STATE;
4711 IMAPSession *session = NULL;
4713 debug_print("getting session...\n");
4714 session = imap_session_get(item->folder);
4716 data->msglist = g_slist_reverse(data->msglist);
4718 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4721 g_slist_length(data->msglist));
4723 lock_session(session);
4725 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4726 NULL, NULL, NULL, NULL, NULL, FALSE);
4728 if (ok == MAILIMAP_NO_ERROR) {
4729 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4730 data->msglist, flags_value, NULL, flags_set);
4732 g_warning("can't select mailbox %s\n", item->path);
4736 unlock_session(session);
4738 g_slist_free(data->msglist);
4743 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4745 gboolean tags_set = GPOINTER_TO_INT(user_data);
4746 TagsData *data = (TagsData *)value;
4747 IMAPFolderItem *_item = data->item;
4748 FolderItem *item = (FolderItem *)_item;
4749 gchar *str = data->str;
4750 gint ok = MAILIMAP_ERROR_BAD_STATE;
4751 IMAPSession *session = NULL;
4753 debug_print("getting session...\n");
4754 session = imap_session_get(item->folder);
4756 data->msglist = g_slist_reverse(data->msglist);
4758 debug_print("IMAP %ssetting tags %s for %d messages\n",
4761 g_slist_length(data->msglist));
4763 lock_session(session);
4765 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4766 NULL, NULL, NULL, NULL, NULL, FALSE);
4768 if (ok == MAILIMAP_NO_ERROR) {
4772 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4773 data->msglist, 0, &list, tags_set);
4775 g_warning("can't select mailbox %s\n", item->path);
4779 unlock_session(session);
4781 g_slist_free(data->msglist);
4787 static void process_hashtable(IMAPFolderItem *item)
4789 if (item->flags_set_table) {
4790 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4791 g_hash_table_destroy(item->flags_set_table);
4792 item->flags_set_table = NULL;
4794 if (item->flags_unset_table) {
4795 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4796 g_hash_table_destroy(item->flags_unset_table);
4797 item->flags_unset_table = NULL;
4799 if (item->tags_set_table) {
4800 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4801 g_hash_table_destroy(item->tags_set_table);
4802 item->tags_set_table = NULL;
4804 if (item->tags_unset_table) {
4805 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4806 g_hash_table_destroy(item->tags_unset_table);
4807 item->tags_unset_table = NULL;
4812 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4814 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4815 IMAPSession *session;
4817 g_return_if_fail(item != NULL);
4819 if (item->batching == batch)
4823 item->batching = TRUE;
4824 debug_print("IMAP switching to batch mode\n");
4825 if (!item->flags_set_table) {
4826 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4828 if (!item->flags_unset_table) {
4829 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4831 if (!item->tags_set_table) {
4832 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4834 if (!item->tags_unset_table) {
4835 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4837 session = imap_session_get(folder);
4839 imap_refresh_sensitivity(session);
4840 session->sens_update_block = TRUE;
4843 debug_print("IMAP switching away from batch mode\n");
4845 process_hashtable(item);
4846 item->batching = FALSE;
4847 session = imap_session_get(folder);
4849 session->sens_update_block = FALSE;
4850 imap_refresh_sensitivity(session);
4857 /* data types conversion libetpan <-> claws */
4861 #define ETPAN_IMAP_MB_MARKED 1
4862 #define ETPAN_IMAP_MB_UNMARKED 2
4863 #define ETPAN_IMAP_MB_NOSELECT 4
4864 #define ETPAN_IMAP_MB_NOINFERIORS 8
4866 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4872 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4873 switch (imap_flags->mbf_sflag) {
4874 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4875 flags |= ETPAN_IMAP_MB_MARKED;
4877 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4878 flags |= ETPAN_IMAP_MB_NOSELECT;
4880 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4881 flags |= ETPAN_IMAP_MB_UNMARKED;
4886 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4887 cur = clist_next(cur)) {
4888 struct mailimap_mbx_list_oflag * oflag;
4890 oflag = clist_content(cur);
4892 switch (oflag->of_type) {
4893 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4894 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4902 static GSList * imap_list_from_lep(IMAPFolder * folder,
4903 clist * list, const gchar * real_path, gboolean all)
4906 GSList * item_list = NULL, *llast = NULL;
4908 for(iter = clist_begin(list) ; iter != NULL ;
4909 iter = clist_next(iter)) {
4910 struct mailimap_mailbox_list * mb;
4918 FolderItem *new_item;
4920 mb = clist_content(iter);
4926 if (mb->mb_flag != NULL)
4927 flags = imap_flags_to_flags(mb->mb_flag);
4929 delimiter = mb->mb_delimiter;
4932 dup_name = strdup(name);
4933 if (delimiter != '\0')
4934 subst_char(dup_name, delimiter, '/');
4936 base = g_path_get_basename(dup_name);
4937 if (base[0] == '.') {
4942 if (!all && path_cmp(name, real_path) == 0) {
4948 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4949 dup_name[strlen(dup_name)-1] = '\0';
4952 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
4953 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
4955 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4956 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4957 new_item->no_sub = TRUE;
4958 if (strcmp(dup_name, "INBOX") != 0 &&
4959 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4960 new_item->no_select = TRUE;
4962 if (item_list == NULL)
4963 llast = item_list = g_slist_append(item_list, new_item);
4965 llast = g_slist_append(llast, new_item);
4966 llast = llast->next;
4968 debug_print("folder '%s' found.\n", loc_path);
4979 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
4981 GSList *sorted_list, *cur;
4982 guint first, last, next;
4983 GSList *ret_list = NULL, *llast = NULL;
4984 struct mailimap_set * current_set;
4985 unsigned int item_count;
4987 if (numlist == NULL)
4990 current_set = mailimap_set_new_empty();
4992 sorted_list = g_slist_copy(numlist);
4993 sorted_list = g_slist_sort(sorted_list, g_int_compare);
4995 first = GPOINTER_TO_INT(sorted_list->data);
4998 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
4999 if (GPOINTER_TO_INT(cur->data) == 0)
5004 last = GPOINTER_TO_INT(cur->data);
5006 next = GPOINTER_TO_INT(cur->next->data);
5010 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5012 struct mailimap_set_item * item;
5013 item = mailimap_set_item_new(first, last);
5014 mailimap_set_add(current_set, item);
5018 if (item_count >= folder->max_set_size) {
5019 if (ret_list == NULL)
5020 llast = ret_list = g_slist_append(ret_list,
5023 llast = g_slist_append(llast, current_set);
5024 llast = llast->next;
5027 current_set = mailimap_set_new_empty();
5033 if (clist_count(current_set->set_list) > 0) {
5034 ret_list = g_slist_append(ret_list,
5038 g_slist_free(sorted_list);
5043 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5045 MsgNumberList *numlist = NULL;
5049 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5050 MsgInfo *msginfo = (MsgInfo *) cur->data;
5052 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5054 numlist = g_slist_reverse(numlist);
5055 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5056 g_slist_free(numlist);
5061 static GSList * imap_uid_list_from_lep(clist * list)
5068 for(iter = clist_begin(list) ; iter != NULL ;
5069 iter = clist_next(iter)) {
5072 puid = clist_content(iter);
5073 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5076 result = g_slist_reverse(result);
5080 static GSList * imap_uid_list_from_lep_tab(carray * list)
5087 for(i = 0 ; i < carray_count(list) ; i ++) {
5090 puid = carray_get(list, i);
5091 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5093 result = g_slist_reverse(result);
5097 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5099 GHashTable * tags_hash)
5106 for(i = 0 ; i < carray_count(list) ; i += 3) {
5111 puid = carray_get(list, i);
5112 pflags = carray_get(list, i + 1);
5113 tags = carray_get(list, i + 2);
5115 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5116 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5120 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5123 MsgInfo *msginfo = NULL;
5126 MsgFlags flags = {0, 0};
5128 if (info->headers == NULL)
5131 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5132 if (folder_has_parent_of_type(item, F_QUEUE)) {
5133 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5134 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5135 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5137 flags.perm_flags = info->flags;
5141 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5144 msginfo->msgnum = uid;
5145 msginfo->size = size;
5151 static void imap_lep_set_free(GSList *seq_list)
5155 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5156 struct mailimap_set * imapset;
5158 imapset = cur->data;
5159 mailimap_set_free(imapset);
5161 g_slist_free(seq_list);
5164 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5166 struct mailimap_flag_list * flag_list;
5169 flag_list = mailimap_flag_list_new_empty();
5171 if (IMAP_IS_SEEN(flags))
5172 mailimap_flag_list_add(flag_list,
5173 mailimap_flag_new_seen());
5174 if (IMAP_IS_ANSWERED(flags))
5175 mailimap_flag_list_add(flag_list,
5176 mailimap_flag_new_answered());
5177 if (IMAP_IS_FLAGGED(flags))
5178 mailimap_flag_list_add(flag_list,
5179 mailimap_flag_new_flagged());
5180 if (IMAP_IS_DELETED(flags))
5181 mailimap_flag_list_add(flag_list,
5182 mailimap_flag_new_deleted());
5183 if (IMAP_IS_DRAFT(flags))
5184 mailimap_flag_list_add(flag_list,
5185 mailimap_flag_new_draft());
5186 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5187 mailimap_flag_list_add(flag_list,
5188 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5190 for (; cur; cur = cur->next) {
5192 imap_utf8_to_modified_utf7(cur->data, TRUE);
5193 g_strstrip(enc_str);
5195 mailimap_flag_list_add(flag_list,
5196 mailimap_flag_new_flag_keyword(enc_str));
5202 guint imap_folder_get_refcnt(Folder *folder)
5204 return ((IMAPFolder *)folder)->refcnt;
5207 void imap_folder_ref(Folder *folder)
5209 ((IMAPFolder *)folder)->refcnt++;
5212 void imap_disconnect_all(void)
5215 gboolean short_timeout;
5216 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5220 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5222 short_timeout = !networkmanager_is_online(&error);
5224 short_timeout = TRUE;
5225 g_error_free(error);
5228 short_timeout = TRUE;
5232 imap_main_set_timeout(1);
5234 for (list = account_get_list(); list != NULL; list = list->next) {
5235 PrefsAccount *account = list->data;
5236 if (account->protocol == A_IMAP4) {
5237 RemoteFolder *folder = (RemoteFolder *)account->folder;
5238 if (folder && folder->session) {
5239 IMAPSession *session = (IMAPSession *)folder->session;
5240 imap_threaded_disconnect(FOLDER(folder));
5241 SESSION(session)->state = SESSION_DISCONNECTED;
5242 SESSION(session)->sock = NULL;
5243 session_destroy(SESSION(session));
5244 folder->session = NULL;
5250 imap_main_set_timeout(prefs_common.io_timeout_secs);
5253 void imap_folder_unref(Folder *folder)
5255 if (((IMAPFolder *)folder)->refcnt > 0)
5256 ((IMAPFolder *)folder)->refcnt--;
5259 void imap_cancel_all(void)
5264 folderlist = folder_get_list();
5265 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5266 Folder *folder = (Folder *) cur->data;
5268 if (folder->klass == &imap_class) {
5269 if (imap_is_busy(folder)) {
5270 IMAPSession *imap_session;
5271 RemoteFolder *rfolder;
5273 g_printerr("cancelled\n");
5274 imap_threaded_cancel(folder);
5275 rfolder = (RemoteFolder *) folder;
5276 imap_session = (IMAPSession *) rfolder->session;
5278 imap_session->cancelled = 1;
5284 gboolean imap_cancel_all_enabled(void)
5289 folderlist = folder_get_list();
5290 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5291 Folder *folder = (Folder *) cur->data;
5293 if (folder->klass == &imap_class) {
5294 if (imap_is_busy(folder)) {
5303 static gboolean imap_is_busy(Folder *folder)
5305 IMAPSession *imap_session;
5306 RemoteFolder *rfolder;
5308 rfolder = (RemoteFolder *) folder;
5309 imap_session = (IMAPSession *) rfolder->session;
5310 if (imap_session == NULL)
5313 return imap_session->busy;
5316 #else /* HAVE_LIBETPAN */
5318 static FolderClass imap_class;
5320 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5321 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5323 static Folder *imap_folder_new (const gchar *name,
5326 static gboolean missing_imap_warning = TRUE;
5327 if (missing_imap_warning) {
5328 missing_imap_warning = FALSE;
5330 _("You have one or more IMAP accounts "
5331 "defined. However this version of "
5332 "Claws Mail has been built without "
5333 "IMAP support; your IMAP account(s) are "
5335 "You probably need to "
5336 "install libetpan and recompile "
5341 static gint imap_create_tree (Folder *folder)
5345 static FolderItem *imap_create_folder (Folder *folder,
5351 static gint imap_rename_folder (Folder *folder,
5358 gchar imap_get_path_separator_for_item(FolderItem *item)
5363 FolderClass *imap_get_class(void)
5365 if (imap_class.idstr == NULL) {
5366 imap_class.type = F_IMAP;
5367 imap_class.idstr = "imap";
5368 imap_class.uistr = "IMAP4";
5370 imap_class.new_folder = imap_folder_new;
5371 imap_class.create_tree = imap_create_tree;
5372 imap_class.create_folder = imap_create_folder;
5373 imap_class.rename_folder = imap_rename_folder;
5375 imap_class.set_xml = folder_set_xml;
5376 imap_class.get_xml = folder_get_xml;
5377 imap_class.item_set_xml = imap_item_set_xml;
5378 imap_class.item_get_xml = imap_item_get_xml;
5379 /* nothing implemented */
5385 void imap_disconnect_all(void)
5389 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5394 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5399 void imap_cache_msg(FolderItem *item, gint msgnum)
5403 void imap_cancel_all(void)
5407 gboolean imap_cancel_all_enabled(void)
5414 #ifdef HAVE_LIBETPAN
5415 static void imap_synchronise(FolderItem *item, gint days)
5417 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5418 debug_print("%s already synced\n", item->path?item->path:item->name);
5421 debug_print("syncing %s\n", item->path?item->path:item->name);
5422 imap_gtk_synchronise(item, days);
5423 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5427 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5429 #ifdef HAVE_LIBETPAN
5432 folder_item_set_xml(folder, item, tag);
5434 #ifdef HAVE_LIBETPAN
5435 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5436 XMLAttr *attr = (XMLAttr *) cur->data;
5438 if (!attr || !attr->name || !attr->value) continue;
5439 if (!strcmp(attr->name, "uidnext"))
5440 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5441 if (!strcmp(attr->name, "last_sync"))
5442 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5443 if (!strcmp(attr->name, "last_change"))
5444 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5446 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5447 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5451 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5455 tag = folder_item_get_xml(folder, item);
5457 #ifdef HAVE_LIBETPAN
5458 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5459 IMAP_FOLDER_ITEM(item)->uid_next));
5460 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5461 IMAP_FOLDER_ITEM(item)->last_sync));
5462 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5463 IMAP_FOLDER_ITEM(item)->last_change));
5469 /* ===================================================================
5470 * UTF-7 conversion routines as in RFC 2192
5471 * ===================================================================
5472 * These two functions from:
5474 * Copyright (C) 2003-2004 Pawel Salek. */
5476 /* UTF7 modified base64 alphabet */
5477 static char base64chars[] =
5478 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5479 #define UNDEFINED 64
5481 /* UTF16 definitions */
5482 #define UTF16MASK 0x03FFUL
5483 #define UTF16SHIFT 10
5484 #define UTF16BASE 0x10000UL
5485 #define UTF16HIGHSTART 0xD800UL
5486 #define UTF16HIGHEND 0xDBFFUL
5487 #define UTF16LOSTART 0xDC00UL
5488 #define UTF16LOEND 0xDFFFUL
5491 /* Convert an IMAP mailbox to a UTF-8 string.
5492 * dst needs to have roughly 4 times the storage space of src
5493 * Hex encoding can triple the size of the input
5494 * UTF-7 can be slightly denser than UTF-8
5495 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5497 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5499 unsigned c, i, bitcount;
5500 unsigned long ucs4, utf16, bitbuf;
5501 unsigned char base64[256];
5503 char *dst, *res = malloc(2*strlen(mbox)+1);
5508 if(!dst) return NULL;
5509 /* initialize modified base64 decoding table */
5510 memset(base64, UNDEFINED, sizeof (base64));
5511 for (i = 0; i < sizeof (base64chars); ++i) {
5512 base64[(unsigned)base64chars[i]] = i;
5515 /* loop until end of string */
5516 while (*src != '\0') {
5518 /* deal with literal characters and &- */
5519 if (c != '&' || *src == '-') {
5520 /* encode literally */
5521 if (change_spaces && c == '_')
5525 /* skip over the '-' if this is an &- sequence */
5526 if (c == '&') ++src;
5528 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5532 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5534 bitbuf = (bitbuf << 6) | c;
5536 /* enough bits for a UTF-16 character? */
5537 if (bitcount >= 16) {
5539 utf16 = (bitcount ? bitbuf >> bitcount
5541 /* convert UTF16 to UCS4 */
5543 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5544 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5547 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5548 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5553 /* convert UTF-16 range of UCS4 to UTF-8 */
5554 if (ucs4 <= 0x7fUL) {
5557 } else if (ucs4 <= 0x7ffUL) {
5558 dst[0] = 0xc0 | (ucs4 >> 6);
5559 dst[1] = 0x80 | (ucs4 & 0x3f);
5561 } else if (ucs4 <= 0xffffUL) {
5562 dst[0] = 0xe0 | (ucs4 >> 12);
5563 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5564 dst[2] = 0x80 | (ucs4 & 0x3f);
5567 dst[0] = 0xf0 | (ucs4 >> 18);
5568 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5569 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5570 dst[3] = 0x80 | (ucs4 & 0x3f);
5575 /* skip over trailing '-' in modified UTF-7 encoding */
5576 if (*src == '-') ++src;
5579 /* terminate destination string */
5584 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5585 * dst should be about twice the length of src to deal with non-hex
5588 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5590 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5591 unsigned long ucs4 = 0, bitbuf = 0;
5593 /* initialize hex lookup table */
5594 char *dst, *res = malloc(2*strlen(src)+1);
5596 if(!dst) return NULL;
5602 while ((c = (unsigned char)*src) != '\0') {
5604 /* normal character? */
5605 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5606 /* switch out of UTF-7 mode */
5609 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5617 if (change_spaces && c == ' ')
5621 /* encode '&' as '&-' */
5627 /* switch to UTF-7 mode */
5632 /* Encode US-ASCII characters as themselves */
5636 } else if (utf8total) {
5637 /* save UTF8 bits into UCS4 */
5638 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5639 if (++utf8pos < utf8total) {
5647 } else if (c < 0xF0) {
5651 /* NOTE: can't convert UTF8 sequences longer than 4 */
5657 /* loop to split ucs4 into two utf16 chars if necessary */
5660 if (ucs4 >= UTF16BASE) {
5662 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5664 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5667 bitbuf = (bitbuf << 16) | ucs4;
5671 /* spew out base64 */
5672 while (bitstogo >= 6) {
5674 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5678 } while (utf16flag);
5680 /* if in UTF-7 mode, finish in ASCII */
5683 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5687 /* tie off string */