2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
34 #include "alertpanel.h"
48 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
57 #include "procheader.h"
58 #include "prefs_account.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
66 #include "remotefolder.h"
68 #include "statusbar.h"
70 #include "imap-thread.h"
75 typedef struct _IMAPFolder IMAPFolder;
76 typedef struct _IMAPSession IMAPSession;
77 typedef struct _IMAPNameSpace IMAPNameSpace;
78 typedef struct _IMAPFolderItem IMAPFolderItem;
80 #include "prefs_account.h"
82 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
83 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
84 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
90 /* list of IMAPNameSpace */
94 gchar last_seen_separator;
103 gboolean authenticated;
112 gboolean folder_content_changed;
123 gboolean sens_update_block;
126 struct _IMAPNameSpace
132 #define IMAPBUFSIZE 8192
134 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
135 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
136 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
137 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
138 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
139 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
142 #define IMAP4_PORT 143
143 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
144 #define IMAPS_PORT 993
147 #define IMAP_CMD_LIMIT 1000
150 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
151 ITEM_CAN_CREATE_FLAGS,
152 ITEM_CANNOT_CREATE_FLAGS
155 struct _IMAPFolderItem
164 GHashTable *flags_set_table;
165 GHashTable *flags_unset_table;
168 gboolean should_update;
169 gboolean should_trash_cache;
170 gint can_create_flags;
172 GHashTable *tags_set_table;
173 GHashTable *tags_unset_table;
178 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
179 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
181 static void imap_folder_init (Folder *folder,
185 static Folder *imap_folder_new (const gchar *name,
187 static void imap_folder_destroy (Folder *folder);
189 static IMAPSession *imap_session_new (Folder *folder,
190 const PrefsAccount *account);
191 static gint imap_session_authenticate(IMAPSession *session,
192 const PrefsAccount *account);
193 static void imap_session_destroy (Session *session);
195 static gchar *imap_fetch_msg (Folder *folder,
198 static gchar *imap_fetch_msg_full (Folder *folder,
203 static void imap_remove_cached_msg (Folder *folder,
206 static gint imap_add_msg (Folder *folder,
210 static gint imap_add_msgs (Folder *folder,
213 GRelation *relation);
215 static gint imap_copy_msg (Folder *folder,
218 static gint imap_copy_msgs (Folder *folder,
220 MsgInfoList *msglist,
221 GRelation *relation);
223 static gint imap_remove_msg (Folder *folder,
226 static gint imap_remove_msgs (Folder *folder,
228 MsgInfoList *msglist,
229 GRelation *relation);
230 static gint imap_remove_all_msg (Folder *folder,
233 static gboolean imap_is_msg_changed (Folder *folder,
237 static gint imap_close (Folder *folder,
240 static gint imap_scan_tree (Folder *folder);
242 static gint imap_create_tree (Folder *folder);
244 static FolderItem *imap_create_folder (Folder *folder,
247 static gint imap_rename_folder (Folder *folder,
250 static gint imap_remove_folder (Folder *folder,
253 static FolderItem *imap_folder_item_new (Folder *folder);
254 static void imap_folder_item_destroy (Folder *folder,
257 static IMAPSession *imap_session_get (Folder *folder);
259 static gint imap_auth (IMAPSession *session,
264 static gint imap_scan_tree_recursive (IMAPSession *session,
268 static void imap_create_missing_folders (Folder *folder);
269 static FolderItem *imap_create_special_folder
271 SpecialFolderItemType stype,
274 static gint imap_do_copy_msgs (Folder *folder,
276 MsgInfoList *msglist,
277 GRelation *relation);
279 static void imap_delete_all_cached_messages (FolderItem *item);
280 static void imap_set_batch (Folder *folder,
283 static gint imap_set_message_flags (IMAPSession *session,
284 IMAPFolderItem *item,
285 MsgNumberList *numlist,
289 static gint imap_select (IMAPSession *session,
295 guint32 *uid_validity,
296 gint *can_create_flags,
298 static gint imap_status (IMAPSession *session,
301 IMAPFolderItem *item,
304 guint32 *uid_validity,
307 static void imap_commit_tags (FolderItem *item,
312 static gchar imap_get_path_separator (IMAPSession *session,
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 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
976 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
977 rfolder->session = NULL;
979 imap_threaded_disconnect(session->folder);
980 SESSION(session)->state = SESSION_DISCONNECTED;
981 SESSION(session)->sock = NULL;
982 session_destroy(SESSION(session));
984 rfolder->last_failure = time(NULL);
985 rfolder->connecting = FALSE;
989 /* I think the point of this code is to avoid sending a
990 * keepalive if we've used the session recently and therefore
991 * think it's still alive. Unfortunately, most of the code
992 * does not yet check for errors on the socket, and so if the
993 * connection drops we don't notice until the timeout expires.
994 * A better solution than sending a NOOP every time would be
995 * for every command to be prepared to retry until it is
996 * successfully sent. -- mbp */
997 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
998 /* verify that the session is still alive */
999 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1000 debug_print("disconnected!\n");
1002 session = imap_reconnect_if_possible(folder, session);
1004 rfolder->session = NULL;
1005 rfolder->connecting = FALSE;
1006 session = imap_session_get(folder);
1010 session->cancelled = FALSE;
1013 rfolder->session = SESSION(session);
1014 rfolder->connecting = FALSE;
1016 return IMAP_SESSION(session);
1019 static IMAPSession *imap_session_new(Folder * folder,
1020 const PrefsAccount *account)
1022 IMAPSession *session;
1025 int authenticated = FALSE;
1027 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1028 /* FIXME: IMAP over SSL only... */
1031 port = account->set_imapport ? account->imapport
1032 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1033 ssl_type = account->ssl_imap;
1035 if (account->ssl_imap != SSL_NONE) {
1036 if (alertpanel_full(_("Insecure connection"),
1037 _("This connection is configured to be secured "
1038 "using SSL, but SSL is not available in this "
1039 "build of Claws Mail. \n\n"
1040 "Do you want to continue connecting to this "
1041 "server? The communication would not be "
1043 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1044 NULL, FALSE, NULL, ALERT_WARNING,
1045 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1048 port = account->set_imapport ? account->imapport
1053 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1055 if (account->set_tunnelcmd) {
1056 r = imap_threaded_connect_cmd(folder,
1058 account->recv_server,
1064 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1065 if (ssl_type == SSL_TUNNEL) {
1066 r = imap_threaded_connect_ssl(folder,
1067 account->recv_server,
1073 r = imap_threaded_connect(folder,
1074 account->recv_server,
1079 statuswindow_pop_all();
1080 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1081 authenticated = TRUE;
1083 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1084 authenticated = FALSE;
1087 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1088 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1089 if (r == MAILIMAP_ERROR_SSL)
1090 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1094 imap_handle_error(NULL, r);
1096 if(!prefs_common.no_recv_err_panel) {
1097 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1098 account->recv_server, port);
1100 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1101 account->recv_server, port);
1107 session = g_new0(IMAPSession, 1);
1108 session_init(SESSION(session));
1109 SESSION(session)->type = SESSION_IMAP;
1110 SESSION(session)->server = g_strdup(account->recv_server);
1111 SESSION(session)->port = port;
1112 SESSION(session)->sock = NULL;
1114 SESSION(session)->destroy = imap_session_destroy;
1116 session->capability = NULL;
1118 session->authenticated = authenticated;
1119 session->mbox = NULL;
1120 session->exists = 0;
1121 session->recent = 0;
1122 session->expunge = 0;
1123 session->cmd_count = 0;
1124 session->folder = folder;
1125 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1127 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1128 if (account->ssl_imap == SSL_STARTTLS) {
1131 ok = imap_cmd_starttls(session);
1132 if (ok != MAILIMAP_NO_ERROR) {
1133 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1134 if (!is_fatal(ok)) {
1135 SESSION(session)->sock = NULL;
1136 session_destroy(SESSION(session));
1141 imap_free_capabilities(session);
1142 session->authenticated = FALSE;
1143 session->uidplus = FALSE;
1144 session->cmd_count = 1;
1147 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1148 (session->authenticated) ? "pre" : "un");
1153 static gint imap_session_authenticate(IMAPSession *session,
1154 const PrefsAccount *account)
1156 gchar *pass, *acc_pass;
1157 gboolean failed = FALSE;
1158 gint ok = MAILIMAP_NO_ERROR;
1159 g_return_val_if_fail(account->userid != NULL, -1, MAILIMAP_ERROR_BAD_STATE);
1160 acc_pass = account->passwd;
1163 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1165 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1167 return MAILIMAP_NO_ERROR;
1168 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1170 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1173 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1174 account->recv_server);
1175 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1176 statusbar_pop_all();
1178 if (!failed && !is_fatal(ok)) {
1183 if (prefs_common.no_recv_err_panel) {
1184 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
1185 mainwindow_show_error();
1187 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1193 statuswindow_pop_all();
1194 session->authenticated = TRUE;
1195 return MAILIMAP_NO_ERROR;
1198 static void imap_session_destroy(Session *session)
1200 if (session->state != SESSION_DISCONNECTED)
1201 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1203 imap_free_capabilities(IMAP_SESSION(session));
1204 g_free(IMAP_SESSION(session)->mbox);
1207 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1209 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1212 static guint get_file_size_with_crs(const gchar *filename)
1218 if (filename == NULL)
1221 fp = fopen(filename, "rb");
1225 while (fgets(buf, sizeof (buf), fp) != NULL) {
1227 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1235 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1237 gchar *path, *filename;
1239 path = folder_item_get_path(item);
1241 if (!is_dir_exist(path)) {
1246 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1249 if (is_file_exist(filename)) {
1250 claws_unlink(filename);
1255 typedef struct _TagsData {
1258 IMAPFolderItem *item;
1261 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1263 IMAPSession *session;
1264 gint ok, can_create_tags;
1265 Folder *folder = NULL;
1266 TagsData *ht_data = NULL;
1269 g_return_if_fail(item != NULL);
1270 g_return_if_fail(msginfo != NULL);
1272 folder = item->folder;
1273 debug_print("getting session...\n");
1274 session = imap_session_get(folder);
1277 debug_print("can't get session\n");
1281 ok = imap_select(session, IMAP_FOLDER(folder), item,
1282 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1284 if (ok != MAILIMAP_NO_ERROR) {
1285 imap_handle_error(SESSION(session), ok);
1290 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1293 if (IMAP_FOLDER_ITEM(item)->batching) {
1294 /* instead of performing an UID STORE command for each message change,
1295 * as a lot of them can change "together", we just fill in hashtables
1296 * and defer the treatment so that we're able to send only one
1299 debug_print("IMAP batch mode on, deferring tags change\n");
1300 for (cur = tags_set; cur; cur = cur->next) {
1301 gint cur_tag = GPOINTER_TO_INT(cur->data);
1303 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1304 GINT_TO_POINTER(cur_tag));
1305 if (ht_data == NULL) {
1306 ht_data = g_new0(TagsData, 1);
1307 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1308 ht_data->item = IMAP_FOLDER_ITEM(item);
1309 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1310 GINT_TO_POINTER(cur_tag), ht_data);
1312 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1315 for (cur = tags_unset; cur; cur = cur->next) {
1316 gint cur_tag = GPOINTER_TO_INT(cur->data);
1318 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1319 GINT_TO_POINTER(cur_tag));
1320 if (ht_data == NULL) {
1321 ht_data = g_new0(TagsData, 1);
1322 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1323 ht_data->item = IMAP_FOLDER_ITEM(item);
1324 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1325 GINT_TO_POINTER(cur_tag), ht_data);
1327 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1331 GSList *list_set = NULL;
1332 GSList *list_unset = NULL;
1335 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1336 numlist.next = NULL;
1338 debug_print("IMAP changing tags NOW\n");
1339 for (cur = tags_set; cur; cur = cur->next) {
1340 gint cur_tag = GPOINTER_TO_INT(cur->data);
1341 const gchar *str = tags_get_tag(cur_tag);
1342 list_set = g_slist_prepend(list_set, g_strdup(str));
1345 ok = imap_set_message_flags(session,
1346 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1347 slist_free_strings(list_set);
1348 g_slist_free(list_set);
1349 if (ok != MAILIMAP_NO_ERROR) {
1354 for (cur = tags_unset; cur; cur = cur->next) {
1355 gint cur_tag = GPOINTER_TO_INT(cur->data);
1356 const gchar *str = tags_get_tag(cur_tag);
1357 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1360 ok = imap_set_message_flags(session,
1361 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1362 slist_free_strings(list_unset);
1363 g_slist_free(list_unset);
1364 if (ok != MAILIMAP_NO_ERROR) {
1371 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1372 gboolean headers, gboolean body)
1374 gchar *path, *filename;
1375 IMAPSession *session;
1378 g_return_val_if_fail(folder != NULL, NULL);
1379 g_return_val_if_fail(item != NULL, NULL);
1384 path = folder_item_get_path(item);
1385 if (!is_dir_exist(path))
1386 make_dir_hier(path);
1387 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1389 debug_print("trying to fetch cached %s\n", filename);
1390 if (is_file_exist(filename)) {
1391 /* see whether the local file represents the whole message
1392 * or not. As the IMAP server reports size with \r chars,
1393 * we have to update the local file (UNIX \n only) size */
1394 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1395 guint have_size = -1;
1398 debug_print("message %d has been already %scached.\n", uid,
1399 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1401 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1402 have_size = get_file_size_with_crs(filename);
1403 if (cached && (cached->size <= have_size || !body)) {
1404 procmsg_msginfo_free(cached);
1405 ok = file_strip_crs(filename);
1406 if (ok == 0 && cached && cached->size <= have_size) {
1407 /* we have it all and stripped */
1408 debug_print("...fully cached in fact; setting flag.\n");
1409 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1412 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1413 debug_print("message not cached and file recent, considering file complete\n");
1414 ok = file_strip_crs(filename);
1418 procmsg_msginfo_free(cached);
1421 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1422 procmsg_msginfo_free(cached);
1426 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1428 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1429 procmsg_msginfo_free(cached);
1433 debug_print("getting session...\n");
1434 session = imap_session_get(folder);
1440 session_set_access_time(SESSION(session));
1441 lock_session(session); /* unlocked later in the function */
1443 debug_print("IMAP fetching messages\n");
1444 ok = imap_select(session, IMAP_FOLDER(folder), item,
1445 NULL, NULL, NULL, NULL, NULL, FALSE);
1446 if (ok != MAILIMAP_NO_ERROR) {
1447 g_warning("can't select mailbox %s\n", item->path);
1452 session_set_access_time(SESSION(session));
1454 debug_print("getting message %d...\n", uid);
1455 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1457 if (ok != MAILIMAP_NO_ERROR) {
1458 g_warning("can't fetch message %d\n", uid);
1463 session_set_access_time(SESSION(session));
1464 unlock_session(session);
1466 ok = file_strip_crs(filename);
1468 if (ok == 0 && headers && body) {
1469 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1471 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1472 procmsg_msginfo_free(cached);
1474 } else if (ok == -1) {
1475 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1477 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1478 procmsg_msginfo_free(cached);
1484 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1486 gchar *path, *filename;
1488 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1493 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1494 procmsg_msginfo_free(cached);
1497 path = folder_item_get_path(item);
1498 if (!is_dir_exist(path))
1501 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1503 if (is_file_exist(filename)) {
1504 if (cached && cached->total_size == cached->size) {
1507 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1510 size = get_file_size_with_crs(filename);
1513 if (cached && size >= cached->size) {
1514 cached->total_size = cached->size;
1515 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1516 procmsg_msginfo_free(cached);
1520 procmsg_msginfo_free(cached);
1524 void imap_cache_msg(FolderItem *item, gint msgnum)
1526 Folder *folder = NULL;
1530 folder = item->folder;
1532 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1533 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1534 debug_print("fetched %s\n", tmp);
1539 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1540 const gchar *file, MsgFlags *flags)
1544 MsgFileInfo fileinfo;
1546 g_return_val_if_fail(file != NULL, -1);
1548 fileinfo.msginfo = NULL;
1549 fileinfo.file = (gchar *)file;
1550 fileinfo.flags = flags;
1551 file_list.data = &fileinfo;
1552 file_list.next = NULL;
1554 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1558 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1559 GRelation *relation)
1562 IMAPSession *session;
1563 guint32 last_uid = 0;
1565 MsgFileInfo *fileinfo;
1566 gint ok = MAILIMAP_NO_ERROR;
1567 gint curnum = 0, total = 0;
1568 gboolean missing_uids = FALSE;
1570 g_return_val_if_fail(folder != NULL, -1);
1571 g_return_val_if_fail(dest != NULL, -1);
1572 g_return_val_if_fail(file_list != NULL, -1);
1574 debug_print("getting session...\n");
1575 session = imap_session_get(folder);
1579 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1581 statusbar_print_all(_("Adding messages..."));
1582 total = g_slist_length(file_list);
1583 for (cur = file_list; cur != NULL; cur = cur->next) {
1584 IMAPFlags iflags = 0;
1585 guint32 new_uid = 0;
1586 gchar *real_file = NULL;
1587 fileinfo = (MsgFileInfo *)cur->data;
1589 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1592 if (fileinfo->flags) {
1593 if (MSG_IS_MARKED(*fileinfo->flags))
1594 iflags |= IMAP_FLAG_FLAGGED;
1595 if (MSG_IS_REPLIED(*fileinfo->flags))
1596 iflags |= IMAP_FLAG_ANSWERED;
1597 if (MSG_IS_FORWARDED(*fileinfo->flags))
1598 iflags |= IMAP_FLAG_FORWARDED;
1599 if (!MSG_IS_UNREAD(*fileinfo->flags))
1600 iflags |= IMAP_FLAG_SEEN;
1604 if (real_file == NULL)
1605 real_file = g_strdup(fileinfo->file);
1607 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1608 folder_has_parent_of_type(dest, F_OUTBOX) ||
1609 folder_has_parent_of_type(dest, F_DRAFT) ||
1610 folder_has_parent_of_type(dest, F_TRASH))
1611 iflags |= IMAP_FLAG_SEEN;
1613 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1616 if (ok != MAILIMAP_NO_ERROR) {
1617 g_warning("can't append message %s\n", real_file);
1620 statusbar_progress_all(0,0,0);
1621 statusbar_pop_all();
1624 debug_print("appended new message as %d\n", new_uid);
1625 /* put the local file in the imapcache, so that we don't
1626 * have to fetch it back later. */
1629 missing_uids = TRUE;
1630 debug_print("Missing UID (0)\n");
1633 gchar *cache_path = folder_item_get_path(dest);
1634 if (!is_dir_exist(cache_path))
1635 make_dir_hier(cache_path);
1636 if (is_dir_exist(cache_path)) {
1637 gchar *cache_file = g_strconcat(
1638 cache_path, G_DIR_SEPARATOR_S,
1639 itos(new_uid), NULL);
1640 copy_file(real_file, cache_file, TRUE);
1641 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1648 if (relation != NULL)
1649 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1650 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1651 GINT_TO_POINTER(new_uid));
1652 if (last_uid < new_uid) {
1659 statusbar_progress_all(0,0,0);
1660 statusbar_pop_all();
1665 imap_scan_required(folder, dest);
1667 session = imap_session_get(folder);
1673 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1674 &a, NULL, NULL, NULL, NULL, FALSE);
1679 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1681 GSList *result = NULL;
1686 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1687 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1688 start = item->set_first;
1689 end = item->set_last;
1690 for (t = start; t <= end; t++) {
1691 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1694 result = g_slist_reverse(result);
1695 if (debug_get_mode()) {
1696 debug_print("flat imap set: ");
1697 for (cur = result; cur; cur = cur->next) {
1698 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1705 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1706 MsgInfoList *msglist, GRelation *relation)
1710 GSList *seq_list, *cur;
1712 IMAPSession *session;
1713 gint ok = MAILIMAP_NO_ERROR;
1714 GRelation *uid_mapping;
1716 gboolean single = FALSE;
1718 g_return_val_if_fail(folder != NULL, -1);
1719 g_return_val_if_fail(dest != NULL, -1);
1720 g_return_val_if_fail(msglist != NULL, -1);
1722 debug_print("getting session...\n");
1723 session = imap_session_get(folder);
1729 msginfo = (MsgInfo *)msglist->data;
1730 if (msglist->next == NULL)
1732 src = msginfo->folder;
1734 g_warning("the src folder is identical to the dest.\n");
1738 if (src->folder != dest->folder) {
1739 GSList *infolist = NULL, *cur;
1741 for (cur = msglist; cur; cur = cur->next) {
1742 msginfo = (MsgInfo *)cur->data;
1743 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1744 fileinfo->file = procmsg_get_message_file(msginfo);
1745 fileinfo->flags = &(msginfo->flags);
1746 infolist = g_slist_prepend(infolist, fileinfo);
1748 infolist = g_slist_reverse(infolist);
1749 res = folder_item_add_msgs(dest, infolist, FALSE);
1750 for (cur = infolist; cur; cur = cur->next) {
1751 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1755 g_slist_free(infolist);
1759 lock_session(session); /* unlocked later in the function */
1761 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1762 NULL, NULL, NULL, NULL, NULL, FALSE);
1763 if (ok != MAILIMAP_NO_ERROR) {
1767 unlock_session(session);
1769 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1770 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1771 uid_mapping = g_relation_new(2);
1772 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1774 statusbar_print_all(_("Copying messages..."));
1775 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1776 struct mailimap_set * seq_set;
1777 struct mailimap_set * source = NULL;
1778 struct mailimap_set * dest = NULL;
1779 seq_set = cur->data;
1781 debug_print("Copying messages from %s to %s ...\n",
1782 src->path, destdir);
1784 lock_session(session); /* unlocked later in the function */
1785 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1792 if (ok == MAILIMAP_NO_ERROR) {
1793 unlock_session(session);
1794 if (relation && source && dest) {
1795 GSList *s_list = flatten_mailimap_set(source);
1796 GSList *d_list = flatten_mailimap_set(dest);
1797 GSList *s_cur, *d_cur;
1798 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1800 for (s_cur = s_list, d_cur = d_list;
1802 s_cur = s_cur->next, d_cur = d_cur->next) {
1803 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1807 debug_print("hhhmm, source list length != dest list length.\n");
1809 g_slist_free(s_list);
1810 g_slist_free(d_list);
1816 mailimap_set_free(source);
1818 mailimap_set_free(dest);
1820 if (ok != MAILIMAP_NO_ERROR) {
1821 g_relation_destroy(uid_mapping);
1822 imap_lep_set_free(seq_list);
1823 statusbar_pop_all();
1828 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1829 MsgInfo *msginfo = (MsgInfo *)cur->data;
1832 tuples = g_relation_select(uid_mapping,
1833 GINT_TO_POINTER(msginfo->msgnum),
1835 if (tuples->len > 0) {
1836 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1837 g_relation_insert(relation, msginfo,
1838 GINT_TO_POINTER(num));
1841 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1842 /* put the local file in the imapcache, so that we don't
1843 * have to fetch it back later. */
1845 gchar *cache_path = folder_item_get_path(msginfo->folder);
1846 gchar *real_file = g_strconcat(
1847 cache_path, G_DIR_SEPARATOR_S,
1848 itos(msginfo->msgnum), NULL);
1849 gchar *cache_file = NULL;
1851 cache_path = folder_item_get_path(dest);
1852 cache_file = g_strconcat(
1853 cache_path, G_DIR_SEPARATOR_S,
1855 if (!is_dir_exist(cache_path))
1856 make_dir_hier(cache_path);
1857 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1858 copy_file(real_file, cache_file, TRUE);
1859 debug_print("copied to cache: %s\n", cache_file);
1866 g_relation_insert(relation, msginfo,
1867 GINT_TO_POINTER(0));
1868 g_tuples_destroy(tuples);
1870 statusbar_pop_all();
1872 g_relation_destroy(uid_mapping);
1873 imap_lep_set_free(seq_list);
1877 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1878 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1879 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1880 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1882 imap_scan_required(folder, dest);
1883 if (ok == MAILIMAP_NO_ERROR)
1889 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1893 g_return_val_if_fail(msginfo != NULL, -1);
1895 msglist.data = msginfo;
1896 msglist.next = NULL;
1898 return imap_copy_msgs(folder, dest, &msglist, NULL);
1901 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1902 MsgInfoList *msglist, GRelation *relation)
1907 g_return_val_if_fail(folder != NULL, -1);
1908 g_return_val_if_fail(dest != NULL, -1);
1909 g_return_val_if_fail(msglist != NULL, -1);
1911 msginfo = (MsgInfo *)msglist->data;
1912 g_return_val_if_fail(msginfo->folder != NULL, -1);
1914 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1919 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1920 MsgInfoList *msglist, GRelation *relation)
1922 gchar *destdir, *dir;
1923 GSList *numlist = NULL, *cur;
1925 IMAPSession *session;
1926 gint ok = MAILIMAP_NO_ERROR;
1927 GRelation *uid_mapping;
1929 g_return_val_if_fail(folder != NULL, -1);
1930 g_return_val_if_fail(dest != NULL, -1);
1931 g_return_val_if_fail(msglist != NULL, -1);
1933 debug_print("getting session...\n");
1934 session = imap_session_get(folder);
1939 lock_session(session); /* unlocked later in the function */
1941 msginfo = (MsgInfo *)msglist->data;
1943 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1944 NULL, NULL, NULL, NULL, NULL, FALSE);
1945 if (ok != MAILIMAP_NO_ERROR) {
1949 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1950 for (cur = msglist; cur; cur = cur->next) {
1951 msginfo = (MsgInfo *)cur->data;
1952 if (!MSG_IS_DELETED(msginfo->flags))
1953 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1955 numlist = g_slist_reverse(numlist);
1957 uid_mapping = g_relation_new(2);
1958 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1960 if (numlist != NULL) {
1961 ok = imap_set_message_flags
1962 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1963 if (ok != MAILIMAP_NO_ERROR) {
1964 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1967 } /* else we just need to expunge */
1968 ok = imap_cmd_expunge(session);
1969 if (ok != MAILIMAP_NO_ERROR) {
1970 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1974 session->folder_content_changed = TRUE;
1975 unlock_session(session);
1977 dir = folder_item_get_path(msginfo->folder);
1978 if (is_dir_exist(dir)) {
1979 for (cur = msglist; cur; cur = cur->next) {
1980 msginfo = (MsgInfo *)cur->data;
1981 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1986 g_relation_destroy(uid_mapping);
1987 g_slist_free(numlist);
1989 imap_scan_required(folder, dest);
1992 if (ok == MAILIMAP_NO_ERROR)
1998 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
1999 MsgInfoList *msglist, GRelation *relation)
2003 g_return_val_if_fail(folder != NULL, -1);
2004 g_return_val_if_fail(dest != NULL, -1);
2005 if (msglist == NULL)
2008 msginfo = (MsgInfo *)msglist->data;
2009 g_return_val_if_fail(msginfo->folder != NULL, -1);
2011 return imap_do_remove_msgs(folder, dest, msglist, relation);
2014 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2016 GSList *list = folder_item_get_msg_list(item);
2017 gint res = imap_remove_msgs(folder, item, list, NULL);
2018 procmsg_msg_list_free(list);
2022 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2025 /* TODO: properly implement this method */
2029 static gint imap_close(Folder *folder, FolderItem *item)
2034 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2036 FolderItem *item = NULL;
2037 IMAPSession *session;
2038 gchar *root_folder = NULL;
2040 g_return_val_if_fail(folder != NULL, -1);
2041 g_return_val_if_fail(folder->account != NULL, -1);
2043 debug_print("getting session...\n");
2044 session = imap_session_get(folder);
2046 if (!folder->node) {
2047 folder_tree_destroy(folder);
2048 item = folder_item_new(folder, folder->name, NULL);
2049 item->folder = folder;
2050 folder->node = item->node = g_node_new(item);
2055 if (folder->account->imap_dir && *folder->account->imap_dir) {
2060 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2061 extract_quote(root_folder, '"');
2062 subst_char(root_folder,
2063 imap_get_path_separator(session, IMAP_FOLDER(folder),
2066 strtailchomp(root_folder, '/');
2067 real_path = imap_get_real_path
2068 (session, IMAP_FOLDER(folder), root_folder);
2069 debug_print("IMAP root directory: %s\n", real_path);
2071 /* check if root directory exist */
2073 r = imap_threaded_list(session->folder, "", real_path,
2076 if (r != MAILIMAP_NO_ERROR)
2077 imap_handle_error(SESSION(session), r);
2079 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2080 if (!folder->node) {
2081 item = folder_item_new(folder, folder->name, NULL);
2082 item->folder = folder;
2083 folder->node = item->node = g_node_new(item);
2087 mailimap_list_result_free(lep_list);
2093 item = FOLDER_ITEM(folder->node->data);
2095 if (item && !item->path && root_folder) {
2096 item->path = g_strdup(root_folder);
2099 if (!item || ((item->path || root_folder) &&
2100 strcmp2(item->path, root_folder) != 0)) {
2101 folder_tree_destroy(folder);
2102 item = folder_item_new(folder, folder->name, root_folder);
2103 item->folder = folder;
2104 folder->node = item->node = g_node_new(item);
2107 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2108 imap_create_missing_folders(folder);
2113 static gint imap_scan_tree(Folder *folder)
2115 gboolean subs_only = FALSE;
2116 if (folder->account) {
2117 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2118 subs_only = folder->account->imap_subsonly;
2120 return imap_scan_tree_real(folder, subs_only);
2123 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2126 IMAPFolder *imapfolder;
2127 FolderItem *new_item;
2128 GSList *item_list, *cur;
2131 gchar *wildcard_path;
2137 g_return_val_if_fail(item != NULL, -1);
2138 g_return_val_if_fail(item->folder != NULL, -1);
2139 g_return_val_if_fail(item->no_sub == FALSE, -1);
2141 folder = item->folder;
2142 imapfolder = IMAP_FOLDER(folder);
2144 separator = imap_get_path_separator(session, imapfolder, item->path);
2146 if (folder->ui_func)
2147 folder->ui_func(folder, item, folder->ui_func_data);
2150 wildcard[0] = separator;
2153 real_path = imap_get_real_path(session, imapfolder, item->path);
2157 real_path = g_strdup("");
2160 Xstrcat_a(wildcard_path, real_path, wildcard,
2161 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2165 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2167 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2169 if (r != MAILIMAP_NO_ERROR) {
2170 imap_handle_error(SESSION(session), r);
2174 item_list = imap_list_from_lep(imapfolder,
2175 lep_list, real_path, FALSE);
2176 mailimap_list_result_free(lep_list);
2181 node = item->node->children;
2182 while (node != NULL) {
2183 FolderItem *old_item = FOLDER_ITEM(node->data);
2184 GNode *next = node->next;
2187 for (cur = item_list; cur != NULL; cur = cur->next) {
2188 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2189 if (!strcmp2(old_item->path, cur_item->path)) {
2190 new_item = cur_item;
2195 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2196 debug_print("not removing INBOX\n");
2198 debug_print("folder '%s' not found. removing...\n",
2200 folder_item_remove(old_item);
2203 old_item->no_sub = new_item->no_sub;
2204 old_item->no_select = new_item->no_select;
2205 if (old_item->no_sub == TRUE && node->children) {
2206 debug_print("folder '%s' doesn't have "
2207 "subfolders. removing...\n",
2209 folder_item_remove_children(old_item);
2216 for (cur = item_list; cur != NULL; cur = cur->next) {
2217 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2220 for (node = item->node->children; node != NULL;
2221 node = node->next) {
2222 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2224 new_item = FOLDER_ITEM(node->data);
2225 folder_item_destroy(cur_item);
2231 new_item = cur_item;
2232 debug_print("new folder '%s' found.\n", new_item->path);
2233 folder_item_append(item, new_item);
2236 if (!strcmp(new_item->path, "INBOX")) {
2237 new_item->stype = F_INBOX;
2238 folder->inbox = new_item;
2239 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2242 base = g_path_get_basename(new_item->path);
2244 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2245 new_item->stype = F_OUTBOX;
2246 folder->outbox = new_item;
2247 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2248 new_item->stype = F_DRAFT;
2249 folder->draft = new_item;
2250 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2251 new_item->stype = F_QUEUE;
2252 folder->queue = new_item;
2253 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2254 new_item->stype = F_TRASH;
2255 folder->trash = new_item;
2260 if (new_item->no_sub == FALSE)
2261 imap_scan_tree_recursive(session, new_item, subs_only);
2264 g_slist_free(item_list);
2266 return MAILIMAP_NO_ERROR;
2269 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2271 IMAPSession *session = imap_session_get(folder);
2273 gchar *wildcard_path;
2277 GSList *item_list = NULL, *cur;
2278 GList *child_list = NULL, *tmplist = NULL;
2279 GSList *sub_list = NULL;
2285 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2288 wildcard[0] = separator;
2291 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2295 real_path = g_strdup("");
2298 Xstrcat_a(wildcard_path, real_path, wildcard,
2299 {g_free(real_path); return NULL;});
2303 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2304 item->path?item->path:item->name);
2306 statusbar_print_all(_("Looking for subfolders of %s..."),
2307 item->path?item->path:item->name);
2309 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2311 statusbar_pop_all();
2314 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2315 lep_list, real_path, FALSE);
2316 mailimap_list_result_free(lep_list);
2318 for (cur = item_list; cur != NULL; cur = cur->next) {
2319 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2321 tmplist = imap_scan_subtree(folder, cur_item,
2322 unsubs_only, recursive);
2324 child_list = g_list_concat(child_list, tmplist);
2326 child_list = g_list_prepend(child_list,
2327 imap_get_real_path(session,
2328 IMAP_FOLDER(folder), cur_item->path));
2330 folder_item_destroy(cur_item);
2332 child_list = g_list_reverse(child_list);
2333 g_slist_free(item_list);
2336 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2338 statusbar_pop_all();
2341 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2342 lep_list, real_path, FALSE);
2343 mailimap_list_result_free(lep_list);
2345 for (cur = sub_list; cur != NULL; cur = cur->next) {
2346 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2347 GList *oldlitem = NULL;
2348 gchar *tmp = imap_get_real_path(session,
2349 IMAP_FOLDER(folder), cur_item->path);
2350 folder_item_destroy(cur_item);
2351 oldlitem = g_list_find_custom(
2352 child_list, tmp, (GCompareFunc)strcmp2);
2354 child_list = g_list_remove_link(child_list, oldlitem);
2355 g_free(oldlitem->data);
2356 g_list_free(oldlitem);
2362 statusbar_pop_all();
2367 static gint imap_create_tree(Folder *folder)
2369 g_return_val_if_fail(folder != NULL, -1);
2370 g_return_val_if_fail(folder->node != NULL, -1);
2371 g_return_val_if_fail(folder->node->data != NULL, -1);
2372 g_return_val_if_fail(folder->account != NULL, -1);
2374 imap_scan_tree(folder);
2375 imap_create_missing_folders(folder);
2380 static void imap_create_missing_folders(Folder *folder)
2382 g_return_if_fail(folder != NULL);
2385 folder->inbox = imap_create_special_folder
2386 (folder, F_INBOX, "INBOX");
2388 folder->trash = imap_create_special_folder
2389 (folder, F_TRASH, "Trash");
2391 folder->queue = imap_create_special_folder
2392 (folder, F_QUEUE, "Queue");
2393 if (!folder->outbox)
2394 folder->outbox = imap_create_special_folder
2395 (folder, F_OUTBOX, "Sent");
2397 folder->draft = imap_create_special_folder
2398 (folder, F_DRAFT, "Drafts");
2401 static FolderItem *imap_create_special_folder(Folder *folder,
2402 SpecialFolderItemType stype,
2406 FolderItem *new_item;
2408 g_return_val_if_fail(folder != NULL, NULL);
2409 g_return_val_if_fail(folder->node != NULL, NULL);
2410 g_return_val_if_fail(folder->node->data != NULL, NULL);
2411 g_return_val_if_fail(folder->account != NULL, NULL);
2412 g_return_val_if_fail(name != NULL, NULL);
2414 item = FOLDER_ITEM(folder->node->data);
2415 new_item = imap_create_folder(folder, item, name);
2418 g_warning("Can't create '%s'\n", name);
2419 if (!folder->inbox) return NULL;
2421 new_item = imap_create_folder(folder, folder->inbox, name);
2423 g_warning("Can't create '%s' under INBOX\n", name);
2425 new_item->stype = stype;
2427 new_item->stype = stype;
2432 static gchar *imap_folder_get_path(Folder *folder)
2436 g_return_val_if_fail(folder != NULL, NULL);
2437 g_return_val_if_fail(folder->account != NULL, NULL);
2439 folder_path = g_strconcat(get_imap_cache_dir(),
2441 folder->account->recv_server,
2443 folder->account->userid,
2449 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2451 gchar *folder_path, *path;
2453 g_return_val_if_fail(folder != NULL, NULL);
2454 g_return_val_if_fail(item != NULL, NULL);
2455 folder_path = imap_folder_get_path(folder);
2457 g_return_val_if_fail(folder_path != NULL, NULL);
2458 if (folder_path[0] == G_DIR_SEPARATOR) {
2460 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2463 path = g_strdup(folder_path);
2466 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2467 folder_path, G_DIR_SEPARATOR_S,
2470 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2473 g_free(folder_path);
2478 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2481 gchar *dirpath, *imap_path;
2482 IMAPSession *session;
2483 FolderItem *new_item;
2488 gboolean no_select = FALSE, no_sub = FALSE;
2489 gboolean exist = FALSE;
2491 g_return_val_if_fail(folder != NULL, NULL);
2492 g_return_val_if_fail(folder->account != NULL, NULL);
2493 g_return_val_if_fail(parent != NULL, NULL);
2494 g_return_val_if_fail(name != NULL, NULL);
2496 debug_print("getting session...\n");
2497 session = imap_session_get(folder);
2502 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2503 dirpath = g_strdup(name);
2504 }else if (parent->path)
2505 dirpath = g_strconcat(parent->path, "/", name, NULL);
2506 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2507 dirpath = g_strdup(name);
2508 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2511 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2512 strtailchomp(imap_dir, '/');
2513 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2515 dirpath = g_strdup(name);
2519 /* keep trailing directory separator to create a folder that contains
2521 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2523 strtailchomp(dirpath, '/');
2524 Xstrdup_a(new_name, name, {
2528 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2529 imap_path_separator_subst(imap_path, separator);
2530 /* remove trailing / for display */
2531 strtailchomp(new_name, '/');
2533 if (strcmp(dirpath, "INBOX") != 0) {
2538 argbuf = g_ptr_array_new();
2539 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2540 if (r != MAILIMAP_NO_ERROR) {
2541 imap_handle_error(SESSION(session), r);
2542 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2545 ptr_array_free_strings(argbuf);
2546 g_ptr_array_free(argbuf, TRUE);
2550 if (clist_count(lep_list) > 0)
2552 mailimap_list_result_free(lep_list);
2555 ok = imap_cmd_create(session, imap_path);
2556 if (ok != MAILIMAP_NO_ERROR) {
2557 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2562 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2563 if (r == MAILIMAP_NO_ERROR) {
2564 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2565 lep_list, dirpath, TRUE);
2567 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2568 no_select = cur_item->no_select;
2569 no_sub = cur_item->no_sub;
2570 g_slist_free(item_list);
2572 mailimap_list_result_free(lep_list);
2574 imap_handle_error(SESSION(session), r);
2577 imap_threaded_subscribe(folder, imap_path, TRUE);
2581 /* just get flags */
2582 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2583 if (r == MAILIMAP_NO_ERROR) {
2584 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2585 lep_list, dirpath, TRUE);
2587 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2588 no_select = cur_item->no_select;
2589 no_sub = cur_item->no_sub;
2590 g_slist_free(item_list);
2592 mailimap_list_result_free(lep_list);
2594 imap_handle_error(SESSION(session), r);
2598 new_item = folder_item_new(folder, new_name, dirpath);
2599 new_item->no_select = no_select;
2600 new_item->no_sub = no_sub;
2601 folder_item_append(parent, new_item);
2605 dirpath = folder_item_get_path(new_item);
2606 if (!is_dir_exist(dirpath))
2607 make_dir_hier(dirpath);
2611 /* folder existed, scan it */
2612 imap_scan_required(folder, new_item);
2613 folder_item_scan_full(new_item, FALSE);
2619 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2624 gchar *real_oldpath;
2625 gchar *real_newpath;
2627 gchar *old_cache_dir;
2628 gchar *new_cache_dir;
2629 IMAPSession *session;
2632 gint exists, recent, unseen;
2633 guint32 uid_validity;
2635 g_return_val_if_fail(folder != NULL, -1);
2636 g_return_val_if_fail(item != NULL, -1);
2637 g_return_val_if_fail(item->path != NULL, -1);
2638 g_return_val_if_fail(name != NULL, -1);
2640 debug_print("getting session...\n");
2641 session = imap_session_get(folder);
2646 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2647 g_warning(_("New folder name must not contain the namespace "
2652 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2654 g_free(session->mbox);
2655 session->mbox = NULL;
2656 session->exists = 0;
2657 session->recent = 0;
2658 session->expunge = 0;
2659 ok = imap_cmd_examine(session, "INBOX",
2660 &exists, &recent, &unseen, &uid_validity, FALSE);
2661 if (ok != MAILIMAP_NO_ERROR) {
2662 g_free(real_oldpath);
2666 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2667 if (strchr(item->path, G_DIR_SEPARATOR)) {
2668 dirpath = g_path_get_dirname(item->path);
2669 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2672 newpath = g_strdup(name);
2674 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2675 imap_path_separator_subst(real_newpath, separator);
2677 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2678 if (ok != MAILIMAP_NO_ERROR) {
2679 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2680 real_oldpath, real_newpath);
2681 g_free(real_oldpath);
2683 g_free(real_newpath);
2687 item->name = g_strdup(name);
2689 old_cache_dir = folder_item_get_path(item);
2691 paths[0] = g_strdup(item->path);
2693 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2694 imap_rename_folder_func, paths);
2696 if (is_dir_exist(old_cache_dir)) {
2697 new_cache_dir = folder_item_get_path(item);
2698 if (rename(old_cache_dir, new_cache_dir) < 0) {
2699 FILE_OP_ERROR(old_cache_dir, "rename");
2701 g_free(new_cache_dir);
2704 g_free(old_cache_dir);
2707 g_free(real_oldpath);
2708 g_free(real_newpath);
2712 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2716 IMAPSession *session;
2717 debug_print("getting session...\n");
2719 session = imap_session_get(folder);
2723 if (item && item->path) {
2724 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2727 if (!strcmp(path, "INBOX") && sub == FALSE)
2729 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2730 r = imap_threaded_subscribe(folder, path, sub);
2733 r = imap_threaded_subscribe(folder, rpath, sub);
2739 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2742 IMAPSession *session;
2745 gboolean selected_folder;
2747 g_return_val_if_fail(folder != NULL, -1);
2748 g_return_val_if_fail(item != NULL, -1);
2749 g_return_val_if_fail(item->path != NULL, -1);
2751 debug_print("getting session...\n");
2752 session = imap_session_get(folder);
2756 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2758 imap_threaded_subscribe(folder, path, FALSE);
2760 selected_folder = (session->mbox != NULL) &&
2761 (!strcmp(session->mbox, item->path));
2762 if (selected_folder) {
2763 ok = imap_cmd_close(session);
2764 if (ok != MAILIMAP_NO_ERROR) {
2765 debug_print("close err %d\n", ok);
2766 imap_handle_error(SESSION(session), ok);
2770 ok = imap_cmd_delete(session, path);
2771 if (ok != MAILIMAP_NO_ERROR) {
2772 gchar *tmp = g_strdup_printf("%s%c", path,
2773 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2776 ok = imap_cmd_delete(session, path);
2779 if (ok != MAILIMAP_NO_ERROR) {
2780 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2786 cache_dir = folder_item_get_path(item);
2787 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2788 g_warning("can't remove directory '%s'\n", cache_dir);
2790 folder_item_remove(item);
2794 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2798 g_return_val_if_fail(item != NULL, -1);
2799 g_return_val_if_fail(item->folder != NULL, -1);
2800 g_return_val_if_fail(item->node != NULL, -1);
2802 node = item->node->children;
2803 while (node != NULL) {
2805 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2809 debug_print("IMAP removing %s\n", item->path);
2811 if (imap_remove_all_msg(folder, item) < 0)
2813 return imap_remove_folder_real(folder, item);
2816 typedef struct _uncached_data {
2817 IMAPSession *session;
2819 MsgNumberList *numlist;
2826 static void *imap_get_uncached_messages_thread(void *data)
2828 uncached_data *stuff = (uncached_data *)data;
2829 IMAPSession *session = stuff->session;
2830 FolderItem *item = stuff->item;
2831 MsgNumberList *numlist = stuff->numlist;
2832 GSList *newlist = NULL;
2833 GSList *llast = NULL;
2834 GSList *seq_list, *cur;
2835 gboolean got_alien_tags = FALSE;
2837 debug_print("uncached_messages\n");
2839 if (session == NULL || item == NULL || item->folder == NULL
2840 || FOLDER_CLASS(item->folder) != &imap_class) {
2845 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2846 debug_print("get msgs info\n");
2847 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2848 struct mailimap_set * imapset;
2854 if (session->cancelled)
2857 imapset = cur->data;
2859 r = imap_threaded_fetch_env(session->folder,
2860 imapset, &env_list);
2861 if (r != MAILIMAP_NO_ERROR) {
2862 imap_handle_error(SESSION(session), r);
2870 session_set_access_time(SESSION(session));
2873 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2874 struct imap_fetch_env_info * info;
2876 GSList *tags = NULL, *cur = NULL;
2877 info = carray_get(env_list, i);
2878 tags = carray_get(env_list, i+1);
2879 msginfo = imap_envelope_from_lep(info, item);
2880 if (msginfo == NULL) {
2881 slist_free_strings(tags);
2886 g_slist_free(msginfo->tags);
2887 msginfo->tags = NULL;
2889 for (cur = tags; cur; cur = cur->next) {
2890 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
2892 id = tags_get_id_for_str(real_tag);
2894 id = tags_add_tag(real_tag);
2895 got_alien_tags = TRUE;
2897 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
2898 msginfo->tags = g_slist_append(
2900 GINT_TO_POINTER(id));
2904 slist_free_strings(tags);
2906 msginfo->folder = item;
2908 llast = newlist = g_slist_append(newlist, msginfo);
2910 llast = g_slist_append(llast, msginfo);
2911 llast = llast->next;
2916 imap_fetch_env_free(env_list);
2919 if (got_alien_tags) {
2921 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
2924 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2925 struct mailimap_set * imapset;
2927 imapset = cur->data;
2928 mailimap_set_free(imapset);
2931 session_set_access_time(SESSION(session));
2936 #define MAX_MSG_NUM 50
2938 static GSList *imap_get_uncached_messages(IMAPSession *session,
2940 MsgNumberList *numlist,
2943 GSList *result = NULL;
2945 uncached_data *data = g_new0(uncached_data, 1);
2950 data->total = g_slist_length(numlist);
2951 data->ok = MAILIMAP_NO_ERROR;
2952 debug_print("messages list : %i\n", data->total);
2954 while (cur != NULL) {
2955 GSList * partial_result;
2963 while (count < MAX_MSG_NUM) {
2968 if (newlist == NULL)
2969 llast = newlist = g_slist_append(newlist, p);
2971 llast = g_slist_append(llast, p);
2972 llast = llast->next;
2982 data->session = session;
2984 data->numlist = newlist;
2987 if (prefs_common.work_offline &&
2988 !inc_offline_should_override(FALSE,
2989 _("Claws Mail needs network access in order "
2990 "to access the IMAP server."))) {
2996 (GSList *)imap_get_uncached_messages_thread(data);
2998 if (data->ok != MAILIMAP_NO_ERROR) {
3001 statusbar_progress_all(data->cur,data->total, 1);
3003 g_slist_free(newlist);
3005 result = g_slist_concat(result, partial_result);
3010 statusbar_progress_all(0,0,0);
3011 statusbar_pop_all();
3016 static void imap_delete_all_cached_messages(FolderItem *item)
3020 g_return_if_fail(item != NULL);
3021 g_return_if_fail(item->folder != NULL);
3022 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3024 debug_print("Deleting all cached messages...\n");
3026 dir = folder_item_get_path(item);
3027 if (is_dir_exist(dir))
3028 remove_all_numbered_files(dir);
3031 debug_print("done.\n");
3034 gchar imap_get_path_separator_for_item(FolderItem *item)
3036 Folder *folder = NULL;
3037 IMAPFolder *imap_folder = NULL;
3038 IMAPSession *session = NULL;
3043 folder = item->folder;
3048 imap_folder = IMAP_FOLDER(folder);
3053 debug_print("getting session...");
3054 session = imap_session_get(FOLDER(folder));
3055 result = imap_get_path_separator(session, imap_folder, item->path);
3059 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
3063 gchar separator = '\0';
3065 g_return_val_if_fail(session != NULL, '/');
3066 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3068 if (r != MAILIMAP_NO_ERROR) {
3069 imap_handle_error(SESSION(session), r);
3070 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3074 if (clist_count(lep_list) > 0) {
3075 clistiter * iter = clist_begin(lep_list);
3076 struct mailimap_mailbox_list * mb;
3077 mb = clist_content(iter);
3079 separator = mb->mb_delimiter;
3080 debug_print("got separator: %c\n", folder->last_seen_separator);
3082 mailimap_list_result_free(lep_list);
3086 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3088 gchar separator = '/';
3090 if (folder->last_seen_separator == 0) {
3091 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
3094 if (folder->last_seen_separator == 0) {
3095 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
3098 if (folder->last_seen_separator != 0) {
3099 debug_print("using separator: %c\n", folder->last_seen_separator);
3100 return folder->last_seen_separator;
3106 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
3111 g_return_val_if_fail(folder != NULL, NULL);
3112 g_return_val_if_fail(path != NULL, NULL);
3114 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3115 separator = imap_get_path_separator(session, folder, path);
3116 imap_path_separator_subst(real_path, separator);
3121 static gint imap_set_message_flags(IMAPSession *session,
3122 IMAPFolderItem *item,
3123 MsgNumberList *numlist,
3132 IMAPFolder *folder = NULL;
3133 GSList *sorted_list = NULL;
3135 if (numlist == NULL || session == NULL)
3136 return MAILIMAP_ERROR_BAD_STATE;
3138 folder = IMAP_FOLDER(session->folder);
3140 sorted_list = g_slist_copy(numlist);
3141 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3143 cur = g_slist_last(sorted_list);
3146 total = GPOINTER_TO_INT(cur->data);
3148 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3150 statusbar_print_all(_("Flagging messages..."));
3152 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3153 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3154 struct mailimap_set_item *set_item = clist_content(
3155 clist_begin(imapset->set_list));
3157 statusbar_progress_all(set_item->set_first, total, 1);
3159 ok = imap_cmd_store(session, item, imapset,
3160 flags, tags, is_set);
3161 statusbar_progress_all(set_item->set_last, total, 1);
3162 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3163 /* reduce max set size */
3164 folder->max_set_size /= 2;
3166 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3171 g_slist_free(sorted_list);
3173 statusbar_progress_all(0,0,0);
3174 statusbar_pop_all();
3176 imap_lep_set_free(seq_list);
3181 typedef struct _select_data {
3182 IMAPSession *session;
3187 guint32 *uid_validity;
3191 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3193 gint *exists, gint *recent, gint *unseen,
3194 guint32 *uid_validity, gint *can_create_flags,
3199 gint exists_, recent_, unseen_;
3200 guint32 uid_validity_;
3201 gint can_create_flags_;
3202 const gchar *path = item ? item->path:NULL;
3205 return MAILIMAP_ERROR_BAD_STATE;
3208 if (!exists && !recent && !unseen && !uid_validity && !can_create_flags) {
3209 if (session->mbox && strcmp(session->mbox, path) == 0)
3210 return MAILIMAP_NO_ERROR;
3212 if (!exists && !recent && !unseen && !uid_validity && can_create_flags) {
3213 if (session->mbox && strcmp(session->mbox, path) == 0) {
3214 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS_UNKNOWN)
3215 return MAILIMAP_NO_ERROR;
3225 uid_validity = &uid_validity_;
3226 if (!can_create_flags)
3227 can_create_flags = &can_create_flags_;
3229 g_free(session->mbox);
3230 session->mbox = NULL;
3231 session->exists = 0;
3232 session->recent = 0;
3233 session->expunge = 0;
3235 real_path = imap_get_real_path(session, folder, path);
3237 g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
3238 IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
3239 ok = imap_cmd_select(session, real_path,
3240 exists, recent, unseen, uid_validity, can_create_flags,
3241 &(IMAP_FOLDER_ITEM(item)->ok_flags), block);
3242 if (ok != MAILIMAP_NO_ERROR) {
3243 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3245 session->mbox = g_strdup(path);
3246 session->folder_content_changed = FALSE;
3247 session->exists = *exists;
3248 session->recent = *recent;
3249 session->expunge = 0;
3250 session->unseen = *unseen;
3251 session->uid_validity = *uid_validity;
3252 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3253 session->exists, session->recent, session->expunge,
3254 session->uid_validity, *can_create_flags);
3256 if (*can_create_flags) {
3257 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CAN_CREATE_FLAGS;
3259 IMAP_FOLDER_ITEM(item)->can_create_flags = ITEM_CANNOT_CREATE_FLAGS;
3266 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3267 const gchar *path, IMAPFolderItem *item,
3269 guint32 *uid_next, guint32 *uid_validity,
3270 gint *unseen, gboolean block)
3274 struct mailimap_mailbox_data_status * data_status;
3279 real_path = imap_get_real_path(session, folder, path);
3298 if (session->mbox != NULL &&
3299 !strcmp(session->mbox, item->item.path)) {
3300 r = imap_cmd_close(session);
3301 if (r != MAILIMAP_NO_ERROR) {
3302 imap_handle_error(SESSION(session), r);
3303 debug_print("close err %d\n", r);
3308 r = imap_threaded_status(FOLDER(folder), real_path,
3309 &data_status, mask);
3312 if (r != MAILIMAP_NO_ERROR) {
3313 imap_handle_error(SESSION(session), r);
3314 debug_print("status err %d\n", r);
3318 if (data_status->st_info_list == NULL) {
3319 mailimap_mailbox_data_status_free(data_status);
3320 debug_print("status->st_info_list == NULL\n");
3321 return MAILIMAP_ERROR_BAD_STATE;
3325 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3326 iter = clist_next(iter)) {
3327 struct mailimap_status_info * info;
3329 info = clist_content(iter);
3330 switch (info->st_att) {
3331 case MAILIMAP_STATUS_ATT_MESSAGES:
3333 * messages = info->st_value;
3334 got_values |= 1 << 0;
3338 case MAILIMAP_STATUS_ATT_UIDNEXT:
3340 * uid_next = info->st_value;
3341 got_values |= 1 << 2;
3345 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3347 * uid_validity = info->st_value;
3348 got_values |= 1 << 3;
3352 case MAILIMAP_STATUS_ATT_UNSEEN:
3354 * unseen = info->st_value;
3355 got_values |= 1 << 4;
3360 mailimap_mailbox_data_status_free(data_status);
3362 if (got_values != mask) {
3363 g_warning("status: incomplete values received (%d)\n", got_values);
3365 return MAILIMAP_NO_ERROR;
3368 static void imap_free_capabilities(IMAPSession *session)
3370 slist_free_strings(session->capability);
3371 g_slist_free(session->capability);
3372 session->capability = NULL;
3375 /* low-level IMAP4rev1 commands */
3377 static gint imap_cmd_login(IMAPSession *session,
3378 const gchar *user, const gchar *pass,
3384 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3385 gint ok = MAILIMAP_ERROR_BAD_STATE;
3386 if (imap_has_capability(session, "STARTTLS")) {
3387 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3388 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3389 ok = imap_cmd_starttls(session);
3390 if (ok != MAILIMAP_NO_ERROR) {
3391 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3395 imap_free_capabilities(session);
3396 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3397 imap_handle_error(SESSION(session), r);
3398 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3403 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3404 "server requires TLS, but Claws Mail "
3405 "has been compiled without OpenSSL "
3407 SESSION(session)->server);
3408 return MAILIMAP_ERROR_BAD_STATE;
3411 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3412 return MAILIMAP_ERROR_BAD_STATE;
3416 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3418 SESSION(session)->server,
3420 r = imap_threaded_login(session->folder, user, pass, type);
3421 if (r != MAILIMAP_NO_ERROR) {
3422 imap_handle_error(SESSION(session), r);
3423 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3424 SESSION(session)->server);
3427 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3428 SESSION(session)->server);
3429 ok = MAILIMAP_NO_ERROR;
3434 static gint imap_cmd_noop(IMAPSession *session)
3437 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3439 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3440 if (r != MAILIMAP_NO_ERROR) {
3441 imap_handle_error(SESSION(session), r);
3442 debug_print("noop err %d\n", r);
3446 session->folder_content_changed = FALSE;
3448 if ((exists && exists != session->exists)
3449 || (recent && recent != session->recent)
3450 || (expunge && expunge != session->expunge)
3451 || (unseen && unseen != session->unseen)) {
3452 session->folder_content_changed = TRUE;
3454 if (uidnext != 0 && uidnext != session->uid_next) {
3455 session->uid_next = uidnext;
3456 session->folder_content_changed = TRUE;
3458 if (uidval != 0 && uidval != session->uid_validity) {
3459 session->uid_validity = uidval;
3460 session->folder_content_changed = TRUE;
3463 session->exists = exists;
3464 session->recent = recent;
3465 session->expunge = expunge;
3466 session->unseen = unseen;
3468 session_set_access_time(SESSION(session));
3470 return MAILIMAP_NO_ERROR;
3473 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3474 static gint imap_cmd_starttls(IMAPSession *session)
3478 r = imap_threaded_starttls(session->folder,
3479 SESSION(session)->server, SESSION(session)->port);
3480 if (r != MAILIMAP_NO_ERROR) {
3481 imap_handle_error(SESSION(session), r);
3482 debug_print("starttls err %d\n", r);
3485 return MAILIMAP_NO_ERROR;
3489 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3490 gint *exists, gint *recent, gint *unseen,
3491 guint32 *uid_validity, gint *can_create_flags,
3492 GSList **ok_flags, gboolean block)
3496 r = imap_threaded_select(session->folder, folder,
3497 exists, recent, unseen, uid_validity, can_create_flags, ok_flags);
3498 if (r != MAILIMAP_NO_ERROR) {
3499 imap_handle_error(SESSION(session), r);
3500 debug_print("select err %d\n", r);
3503 return MAILIMAP_NO_ERROR;
3506 static gint imap_cmd_close(IMAPSession *session)
3510 r = imap_threaded_close(session->folder);
3511 if (r != MAILIMAP_NO_ERROR) {
3512 imap_handle_error(SESSION(session), r);
3513 debug_print("close err %d\n", r);
3516 g_free(session->mbox);
3517 session->mbox = NULL;
3518 session->exists = 0;
3519 session->recent = 0;
3520 session->expunge = 0;
3521 return MAILIMAP_NO_ERROR;
3524 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3525 gint *exists, gint *recent, gint *unseen,
3526 guint32 *uid_validity, gboolean block)
3530 r = imap_threaded_examine(session->folder, folder,
3531 exists, recent, unseen, uid_validity);
3532 if (r != MAILIMAP_NO_ERROR) {
3533 imap_handle_error(SESSION(session), r);
3534 debug_print("examine err %d\n", r);
3538 return MAILIMAP_NO_ERROR;
3541 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3545 r = imap_threaded_create(session->folder, folder);
3546 if (r != MAILIMAP_NO_ERROR) {
3547 imap_handle_error(SESSION(session), r);
3552 return MAILIMAP_NO_ERROR;
3555 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3556 const gchar *new_folder)
3560 r = imap_threaded_rename(session->folder, old_folder,
3562 if (r != MAILIMAP_NO_ERROR) {
3563 imap_handle_error(SESSION(session), r);
3567 return MAILIMAP_NO_ERROR;
3570 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3575 r = imap_threaded_delete(session->folder, folder);
3576 if (r != MAILIMAP_NO_ERROR) {
3577 imap_handle_error(SESSION(session), r);
3581 return MAILIMAP_NO_ERROR;
3584 typedef struct _fetch_data {
3585 IMAPSession *session;
3587 const gchar *filename;
3593 static void *imap_cmd_fetch_thread(void *data)
3595 fetch_data *stuff = (fetch_data *)data;
3596 IMAPSession *session = stuff->session;
3597 guint32 uid = stuff->uid;
3598 const gchar *filename = stuff->filename;
3602 r = imap_threaded_fetch_content(session->folder,
3606 r = imap_threaded_fetch_content(session->folder,
3609 if (r != MAILIMAP_NO_ERROR) {
3610 imap_handle_error(SESSION(session), r);
3611 debug_print("fetch err %d\n", r);
3612 return GINT_TO_POINTER(MAILIMAP_ERROR_BAD_STATE);
3614 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3617 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3618 const gchar *filename, gboolean headers,
3621 fetch_data *data = g_new0(fetch_data, 1);
3624 data->session = session;
3626 data->filename = filename;
3627 data->headers = headers;
3630 if (prefs_common.work_offline &&
3631 !inc_offline_should_override(FALSE,
3632 _("Claws Mail needs network access in order "
3633 "to access the IMAP server."))) {
3637 statusbar_print_all(_("Fetching message..."));
3638 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3639 statusbar_pop_all();
3645 static gint imap_cmd_append(IMAPSession *session,
3646 IMAPFolderItem *item,
3647 const gchar *destfolder,
3648 const gchar *file, IMAPFlags flags,
3651 struct mailimap_flag_list * flag_list;
3654 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3656 flag_list = imap_flag_to_lep(item, flags, NULL);
3657 lock_session(session);
3658 r = imap_threaded_append(session->folder, destfolder,
3659 file, flag_list, (int *)new_uid);
3660 mailimap_flag_list_free(flag_list);
3662 if (r != MAILIMAP_NO_ERROR) {
3663 imap_handle_error(SESSION(session), r);
3664 debug_print("append err %d\n", r);
3668 unlock_session(session);
3670 return MAILIMAP_NO_ERROR;
3673 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3674 const gchar *destfolder, GRelation *uid_mapping,
3675 struct mailimap_set **source, struct mailimap_set **dest)
3679 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3680 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3681 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3683 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3684 if (r != MAILIMAP_NO_ERROR) {
3685 imap_handle_error(SESSION(session), r);
3689 return MAILIMAP_NO_ERROR;
3692 static gint imap_cmd_store(IMAPSession *session,
3693 IMAPFolderItem *item,
3694 struct mailimap_set * set,
3695 IMAPFlags flags, GSList *tags, int do_add)
3698 struct mailimap_flag_list * flag_list = NULL;
3699 struct mailimap_store_att_flags * store_att_flags;
3701 flag_list = imap_flag_to_lep(item, flags, tags);
3705 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3708 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3710 r = imap_threaded_store(session->folder, set, store_att_flags);
3711 mailimap_store_att_flags_free(store_att_flags);
3712 if (r != MAILIMAP_NO_ERROR) {
3713 imap_handle_error(SESSION(session), r);
3717 return MAILIMAP_NO_ERROR;
3720 static gint imap_cmd_expunge(IMAPSession *session)
3724 if (prefs_common.work_offline &&
3725 !inc_offline_should_override(FALSE,
3726 _("Claws Mail needs network access in order "
3727 "to access the IMAP server."))) {
3731 r = imap_threaded_expunge(session->folder);
3732 if (r != MAILIMAP_NO_ERROR) {
3733 imap_handle_error(SESSION(session), r);
3737 return MAILIMAP_NO_ERROR;
3740 static void imap_path_separator_subst(gchar *str, gchar separator)
3743 gboolean in_escape = FALSE;
3745 if (!separator || separator == '/') return;
3747 for (p = str; *p != '\0'; p++) {
3748 if (*p == '/' && !in_escape)
3750 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3752 else if (*p == '-' && in_escape)
3757 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3759 FolderItem *item = node->data;
3760 gchar **paths = data;
3761 const gchar *oldpath = paths[0];
3762 const gchar *newpath = paths[1];
3763 gchar *real_oldpath, *real_newpath;
3765 gchar *new_itempath;
3767 IMAPSession *session = imap_session_get(item->folder);
3769 oldpathlen = strlen(oldpath);
3770 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3771 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3775 base = item->path + oldpathlen;
3776 while (*base == G_DIR_SEPARATOR) base++;
3778 new_itempath = g_strdup(newpath);
3780 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3783 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3785 item->path = new_itempath;
3787 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3789 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3790 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3792 g_free(real_oldpath);
3793 g_free(real_newpath);
3797 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3799 GSList *uidlist, *elem;
3801 clist * lep_uidlist;
3802 gint ok, nummsgs = 0, lastuid_old;
3804 if (session == NULL) {
3808 ok = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(item),
3809 NULL, NULL, NULL, NULL, NULL, TRUE);
3810 if (ok != MAILIMAP_NO_ERROR) {
3814 g_slist_free(item->uid_list);
3815 item->uid_list = NULL;
3819 if (folder->account && folder->account->low_bandwidth) {
3820 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3824 if (r == MAILIMAP_NO_ERROR) {
3825 GSList * fetchuid_list =
3826 imap_uid_list_from_lep(lep_uidlist);
3827 mailimap_search_result_free(lep_uidlist);
3829 uidlist = g_slist_concat(fetchuid_list, uidlist);
3831 carray * lep_uidtab;
3832 if (r != -1) { /* inited */
3833 imap_handle_error(SESSION(session), r);
3837 r = imap_threaded_fetch_uid(folder, 1,
3839 if (r == MAILIMAP_NO_ERROR) {
3840 GSList * fetchuid_list =
3841 imap_uid_list_from_lep_tab(lep_uidtab);
3842 imap_fetch_uid_list_free(lep_uidtab);
3843 uidlist = g_slist_concat(fetchuid_list, uidlist);
3847 if (r != MAILIMAP_NO_ERROR) {
3848 imap_handle_error(SESSION(session), r);
3852 lastuid_old = item->lastuid;
3854 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3857 msgnum = GPOINTER_TO_INT(elem->data);
3859 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3860 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3863 g_slist_free(uidlist);
3865 unlock_session(session); /* locked from imap_get_num_list */
3871 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3873 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3874 IMAPSession *session;
3876 GSList *uidlist = NULL;
3878 gboolean selected_folder;
3879 gint known_list_len = 0;
3880 debug_print("get_num_list\n");
3882 g_return_val_if_fail(folder != NULL, -1);
3883 g_return_val_if_fail(item != NULL, -1);
3884 g_return_val_if_fail(item->item.path != NULL, -1);
3885 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3886 g_return_val_if_fail(folder->account != NULL, -1);
3888 known_list_len = g_slist_length(item->uid_list);
3889 if (!item->should_update) {
3890 debug_print("get_num_list: nothing to update\n");
3891 *old_uids_valid = TRUE;
3892 if (known_list_len == item->item.total_msgs
3893 && known_list_len > 0) {
3894 *msgnum_list = g_slist_copy(item->uid_list);
3895 return known_list_len;
3897 debug_print("don't know the list length...\n");
3901 if (prefs_common.work_offline &&
3902 !inc_offline_should_override(FALSE,
3903 _("Claws Mail needs network access in order "
3904 "to access the IMAP server."))) {
3908 debug_print("getting session...\n");
3909 session = imap_session_get(folder);
3910 g_return_val_if_fail(session != NULL, -1);
3912 lock_session(session); /* unlocked by get_list_of_uids */
3913 if (FOLDER_ITEM(item)->path)
3914 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3915 FOLDER_ITEM(item)->folder->name,
3917 FOLDER_ITEM(item)->path);
3919 statusbar_print_all(_("Scanning folder %s ..."),
3920 FOLDER_ITEM(item)->folder->name);
3922 selected_folder = (session->mbox != NULL) &&
3923 (!strcmp(session->mbox, item->item.path));
3925 if (item->should_trash_cache) {
3926 *old_uids_valid = FALSE;
3927 debug_print("get_num_list: trashing num list\n");
3928 debug_print("Freeing imap uid cache\n");
3930 g_slist_free(item->uid_list);
3931 item->uid_list = NULL;
3933 imap_delete_all_cached_messages((FolderItem *)item);
3935 debug_print("get_num_list: updating num list\n");
3936 *old_uids_valid = TRUE;
3939 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3940 /* session could be broken now, in case of fatal error */
3942 debug_print("get_num_list: got %d msgs\n", nummsgs);
3945 statusbar_pop_all();
3949 *msgnum_list = uidlist;
3951 dir = folder_item_get_path((FolderItem *)item);
3952 debug_print("removing old messages from %s\n", dir);
3953 remove_numbered_files_not_in_list(dir, *msgnum_list);
3956 debug_print("get_num_list - ok - %i\n", nummsgs);
3957 statusbar_pop_all();
3958 item->should_trash_cache = FALSE;
3959 item->should_update = FALSE;
3963 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3968 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3969 flags.tmp_flags = 0;
3971 g_return_val_if_fail(item != NULL, NULL);
3972 g_return_val_if_fail(file != NULL, NULL);
3974 if (folder_has_parent_of_type(item, F_QUEUE)) {
3975 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3976 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3977 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3980 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3981 if (!msginfo) return NULL;
3983 msginfo->plaintext_file = g_strdup(file);
3984 msginfo->folder = item;
3989 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3990 GSList *msgnum_list)
3992 IMAPSession *session;
3993 MsgInfoList *ret = NULL;
3996 debug_print("get_msginfos\n");
3998 g_return_val_if_fail(folder != NULL, NULL);
3999 g_return_val_if_fail(item != NULL, NULL);
4000 g_return_val_if_fail(msgnum_list != NULL, NULL);
4002 debug_print("getting session...\n");
4003 session = imap_session_get(folder);
4004 g_return_val_if_fail(session != NULL, NULL);
4006 lock_session(session); /* unlocked later in the function */
4008 debug_print("IMAP getting msginfos\n");
4009 ok = imap_select(session, IMAP_FOLDER(folder), item,
4010 NULL, NULL, NULL, NULL, NULL, FALSE);
4011 if (ok != MAILIMAP_NO_ERROR) {
4014 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4015 folder_has_parent_of_type(item, F_QUEUE))) {
4016 ret = g_slist_concat(ret,
4017 imap_get_uncached_messages(session, item,
4019 if (ok != MAILIMAP_NO_ERROR)
4021 unlock_session(session);
4023 MsgNumberList *sorted_list, *elem, *llast = NULL;
4024 gint startnum, lastnum;
4026 unlock_session(session);
4028 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4030 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4032 llast = g_slist_last(ret);
4033 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4037 num = GPOINTER_TO_INT(elem->data);
4039 if (num > lastnum + 1 || elem == NULL) {
4041 for (i = startnum; i <= lastnum; ++i) {
4043 file = imap_fetch_msg(folder, item, i);
4045 MsgInfo *msginfo = imap_parse_msg(file, item);
4046 if (msginfo != NULL) {
4047 msginfo->msgnum = i;
4049 llast = ret = g_slist_append(ret, msginfo);
4051 llast = g_slist_append(llast, msginfo);
4052 llast = llast->next;
4067 g_slist_free(sorted_list);
4072 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4074 MsgInfo *msginfo = NULL;
4075 MsgInfoList *msginfolist;
4076 MsgNumberList numlist;
4078 numlist.next = NULL;
4079 numlist.data = GINT_TO_POINTER(uid);
4081 msginfolist = imap_get_msginfos(folder, item, &numlist);
4082 if (msginfolist != NULL) {
4083 msginfo = msginfolist->data;
4084 g_slist_free(msginfolist);
4090 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4092 IMAPSession *session;
4093 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4094 gint ok, exists = 0, unseen = 0;
4095 guint32 uid_next = 0, uid_val = 0;
4096 gboolean selected_folder;
4098 g_return_val_if_fail(folder != NULL, FALSE);
4099 g_return_val_if_fail(item != NULL, FALSE);
4100 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4101 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4103 if (item->item.path == NULL)
4106 if (item->should_update) {
4107 debug_print("scan already required\n");
4110 debug_print("getting session...\n");
4111 session = imap_session_get(folder);
4113 g_return_val_if_fail(session != NULL, FALSE);
4114 lock_session(session); /* unlocked later in the function */
4116 selected_folder = (session->mbox != NULL) &&
4117 (!strcmp(session->mbox, item->item.path));
4118 if (selected_folder) {
4119 if (!session->folder_content_changed) {
4120 ok = imap_cmd_noop(session);
4121 if (ok != MAILIMAP_NO_ERROR) {
4122 debug_print("disconnected!\n");
4124 session = imap_reconnect_if_possible(folder, session);
4126 session = imap_session_get(folder);
4127 if (session == NULL)
4131 if (session->folder_content_changed) {
4132 debug_print("CHANGED (self-noop)! scan_required\n");
4133 item->should_update = TRUE;
4134 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4135 item->item.mtime = session->uid_validity;
4136 item->should_trash_cache = TRUE;
4138 unlock_session(session);
4142 debug_print("CHANGED (previous noop)! scan_required\n");
4143 item->should_update = TRUE;
4144 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4145 item->item.mtime = session->uid_validity;
4146 item->should_trash_cache = TRUE;
4148 unlock_session(session);
4152 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4153 &exists, &uid_next, &uid_val, &unseen, FALSE);
4154 if (ok != MAILIMAP_NO_ERROR) {
4158 debug_print("exists %d, item->item.total_msgs %d\n",
4159 exists, item->item.total_msgs);
4160 if (exists != item->item.total_msgs
4161 || unseen != item->item.unread_msgs
4162 || uid_next != item->uid_next
4163 || uid_val != item->item.mtime) {
4164 debug_print("CHANGED (status)! scan_required\n");
4165 item->last_change = time(NULL);
4166 item->should_update = TRUE;
4167 item->uid_next = uid_next;
4168 if (uid_val != item->item.mtime) {
4169 item->item.mtime = uid_val;
4170 item->should_trash_cache = TRUE;
4172 unlock_session(session);
4176 unlock_session(session);
4178 item->should_update = FALSE;
4182 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4184 IMAPSession *session;
4185 IMAPFlags flags_set = 0, flags_unset = 0;
4186 gint ok = MAILIMAP_NO_ERROR;
4187 MsgNumberList numlist;
4188 hashtable_data *ht_data = NULL;
4190 g_return_if_fail(folder != NULL);
4191 g_return_if_fail(folder->klass == &imap_class);
4192 g_return_if_fail(item != NULL);
4193 g_return_if_fail(item->folder == folder);
4194 g_return_if_fail(msginfo != NULL);
4195 g_return_if_fail(msginfo->folder == item);
4197 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4198 flags_set |= IMAP_FLAG_FLAGGED;
4199 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4200 flags_unset |= IMAP_FLAG_FLAGGED;
4202 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4203 flags_unset |= IMAP_FLAG_SEEN;
4204 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4205 flags_set |= IMAP_FLAG_SEEN;
4207 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4208 flags_set |= IMAP_FLAG_ANSWERED;
4209 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4210 flags_unset |= IMAP_FLAG_ANSWERED;
4212 if (!MSG_IS_FORWARDED(msginfo->flags) && (newflags & MSG_FORWARDED))
4213 flags_set |= IMAP_FLAG_FORWARDED;
4214 if ( MSG_IS_FORWARDED(msginfo->flags) && !(newflags & MSG_FORWARDED))
4215 flags_unset |= IMAP_FLAG_FORWARDED;
4217 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4218 flags_set |= IMAP_FLAG_DELETED;
4219 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4220 flags_unset |= IMAP_FLAG_DELETED;
4222 if (!flags_set && !flags_unset) {
4223 /* the changed flags were not translatable to IMAP-speak.
4224 * like MSG_POSTFILTERED, so just apply. */
4225 msginfo->flags.perm_flags = newflags;
4229 debug_print("getting session...\n");
4230 session = imap_session_get(folder);
4235 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
4236 NULL, NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4239 numlist.next = NULL;
4240 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4242 if (IMAP_FOLDER_ITEM(item)->batching) {
4243 /* instead of performing an UID STORE command for each message change,
4244 * as a lot of them can change "together", we just fill in hashtables
4245 * and defer the treatment so that we're able to send only one
4248 debug_print("IMAP batch mode on, deferring flags change\n");
4250 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4251 GINT_TO_POINTER(flags_set));
4252 if (ht_data == NULL) {
4253 ht_data = g_new0(hashtable_data, 1);
4254 ht_data->item = IMAP_FOLDER_ITEM(item);
4255 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4256 GINT_TO_POINTER(flags_set), ht_data);
4258 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4261 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4262 GINT_TO_POINTER(flags_unset));
4263 if (ht_data == NULL) {
4264 ht_data = g_new0(hashtable_data, 1);
4265 ht_data->item = IMAP_FOLDER_ITEM(item);
4266 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4267 GINT_TO_POINTER(flags_unset), ht_data);
4269 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4270 GINT_TO_POINTER(msginfo->msgnum));
4273 debug_print("IMAP changing flags\n");
4275 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_set, NULL, TRUE);
4276 if (ok != MAILIMAP_NO_ERROR) {
4282 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item), &numlist, flags_unset, NULL, FALSE);
4283 if (ok != MAILIMAP_NO_ERROR) {
4288 msginfo->flags.perm_flags = newflags;
4292 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4295 IMAPSession *session;
4297 MsgNumberList numlist;
4299 g_return_val_if_fail(folder != NULL, -1);
4300 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4301 g_return_val_if_fail(item != NULL, -1);
4303 debug_print("getting session...\n");
4304 session = imap_session_get(folder);
4305 if (!session) return -1;
4307 ok = imap_select(session, IMAP_FOLDER(folder), item,
4308 NULL, NULL, NULL, NULL, NULL, FALSE);
4309 if (ok != MAILIMAP_NO_ERROR) {
4312 numlist.next = NULL;
4313 numlist.data = GINT_TO_POINTER(uid);
4315 ok = imap_set_message_flags
4316 (session, IMAP_FOLDER_ITEM(item), &numlist, IMAP_FLAG_DELETED, NULL, TRUE);
4317 if (ok != MAILIMAP_NO_ERROR) {
4318 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4322 if (!session->uidplus) {
4323 ok = imap_cmd_expunge(session);
4327 uidstr = g_strdup_printf("%u", uid);
4328 ok = imap_cmd_expunge(session);
4331 if (ok != MAILIMAP_NO_ERROR) {
4332 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4336 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4337 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4338 dir = folder_item_get_path(item);
4339 if (is_dir_exist(dir))
4340 remove_numbered_files(dir, uid, uid);
4342 return MAILIMAP_NO_ERROR;
4345 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4347 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4350 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4354 g_return_val_if_fail(list != NULL, -1);
4356 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4357 if (GPOINTER_TO_INT(elem->data) >= num)
4360 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4363 static gboolean flag_ok(IMAPFolderItem *item, guint flag)
4365 if (item->ok_flags && g_slist_find(item->ok_flags, GUINT_TO_POINTER(flag))) {
4366 debug_print("flag %d is OK\n", flag);
4369 if (item->can_create_flags == ITEM_CAN_CREATE_FLAGS) {
4370 debug_print("creating flags is OK\n");
4377 * NEW and DELETED flags are not syncronized
4378 * - The NEW/RECENT flags in IMAP folders can not really be directly
4379 * modified by Sylpheed
4380 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4381 * meaning, in IMAP it always removes the messages from the FolderItem
4382 * in Sylpheed it can mean to move the message to trash
4385 typedef struct _get_flags_data {
4388 MsgInfoList *msginfo_list;
4389 GRelation *msgflags;
4390 gboolean full_search;
4394 static /*gint*/ void *imap_get_flags_thread(void *data)
4396 get_flags_data *stuff = (get_flags_data *)data;
4397 Folder *folder = stuff->folder;
4398 FolderItem *fitem = (FolderItem *) stuff->item;
4399 MsgInfoList *msginfo_list = stuff->msginfo_list;
4400 GRelation *msgflags = stuff->msgflags;
4402 carray * lep_uidtab;
4403 IMAPSession *session;
4405 int r = MAILIMAP_NO_ERROR;
4406 GHashTable *flags_hash = NULL;
4407 GHashTable *tags_hash = NULL;
4408 gboolean full_search = stuff->full_search;
4409 GSList *sorted_list = NULL;
4410 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL, *forwarded = NULL;
4411 GSList *seq_list, *cur;
4412 gboolean reverse_seen = FALSE;
4413 gboolean selected_folder;
4414 gint exists_cnt, unseen_cnt;
4415 gboolean got_alien_tags = FALSE;
4417 session = imap_session_get(folder);
4419 if (session == NULL) {
4421 return GINT_TO_POINTER(-1);
4423 selected_folder = (session->mbox != NULL) &&
4424 (!strcmp(session->mbox, fitem->path));
4426 lock_session(session);
4427 if (!selected_folder) {
4428 ok = imap_select(session, IMAP_FOLDER(folder), fitem,
4429 &exists_cnt, NULL, &unseen_cnt, NULL, NULL, TRUE);
4430 if (ok != MAILIMAP_NO_ERROR) {
4432 return GINT_TO_POINTER(-1);
4435 if (unseen_cnt > exists_cnt / 2)
4436 reverse_seen = TRUE;
4439 if (fitem->unread_msgs > fitem->total_msgs / 2)
4440 reverse_seen = TRUE;
4443 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4445 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4447 struct mailimap_set * set;
4448 set = mailimap_set_new_interval(1, 0);
4449 seq_list = g_slist_append(NULL, set);
4452 if (folder->account && folder->account->low_bandwidth) {
4453 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4454 struct mailimap_set * imapset;
4455 clist * lep_uidlist;
4458 imapset = cur->data;
4460 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4461 full_search ? NULL:imapset, &lep_uidlist);
4464 r = imap_threaded_search(folder,
4465 IMAP_SEARCH_TYPE_UNSEEN,
4466 full_search ? NULL:imapset, &lep_uidlist);
4468 if (r == MAILIMAP_NO_ERROR) {
4471 uidlist = imap_uid_list_from_lep(lep_uidlist);
4472 mailimap_search_result_free(lep_uidlist);
4474 unseen = g_slist_concat(unseen, uidlist);
4476 imap_handle_error(SESSION(session), r);
4480 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4481 full_search ? NULL:imapset, &lep_uidlist);
4482 if (r == MAILIMAP_NO_ERROR) {
4485 uidlist = imap_uid_list_from_lep(lep_uidlist);
4486 mailimap_search_result_free(lep_uidlist);
4488 flagged = g_slist_concat(flagged, uidlist);
4490 imap_handle_error(SESSION(session), r);
4494 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4495 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4496 full_search ? NULL:imapset, &lep_uidlist);
4497 if (r == MAILIMAP_NO_ERROR) {
4500 uidlist = imap_uid_list_from_lep(lep_uidlist);
4501 mailimap_search_result_free(lep_uidlist);
4503 answered = g_slist_concat(answered, uidlist);
4505 imap_handle_error(SESSION(session), r);
4509 if (flag_ok(IMAP_FOLDER_ITEM(fitem), IMAP_FLAG_FORWARDED)) {
4510 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FORWARDED,
4511 full_search ? NULL:imapset, &lep_uidlist);
4512 if (r == MAILIMAP_NO_ERROR) {
4515 uidlist = imap_uid_list_from_lep(lep_uidlist);
4516 mailimap_search_result_free(lep_uidlist);
4518 forwarded = g_slist_concat(forwarded, uidlist);
4520 imap_handle_error(SESSION(session), r);
4525 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4526 full_search ? NULL:imapset, &lep_uidlist);
4527 if (r == MAILIMAP_NO_ERROR) {
4530 uidlist = imap_uid_list_from_lep(lep_uidlist);
4531 mailimap_search_result_free(lep_uidlist);
4533 deleted = g_slist_concat(deleted, uidlist);
4535 imap_handle_error(SESSION(session), r);
4542 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4543 if (r == MAILIMAP_NO_ERROR) {
4544 flags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4545 tags_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
4546 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash, tags_hash);
4547 imap_fetch_uid_flags_list_free(lep_uidtab);
4549 imap_handle_error(SESSION(session), r);
4555 if (r == MAILIMAP_NO_ERROR)
4556 unlock_session(session);
4558 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4560 MsgPermFlags flags, oldflags;
4563 msginfo = (MsgInfo *) elem->data;
4564 flags = msginfo->flags.perm_flags;
4565 wasnew = (flags & MSG_NEW);
4566 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_FORWARDED|MSG_MARKED|MSG_DELETED);
4568 if (folder->account && folder->account->low_bandwidth) {
4569 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4570 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_FORWARDED | MSG_MARKED);
4572 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4575 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4576 if (gslist_find_next_num(&unseen, msginfo->msgnum) == msginfo->msgnum) {
4577 if (!reverse_seen) {
4578 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4580 flags &= ~(MSG_UNREAD | MSG_NEW);
4584 if (gslist_find_next_num(&flagged, msginfo->msgnum) == msginfo->msgnum)
4585 flags |= MSG_MARKED;
4587 flags &= ~MSG_MARKED;
4589 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4590 if (gslist_find_next_num(&answered, msginfo->msgnum) == msginfo->msgnum)
4591 flags |= MSG_REPLIED;
4593 flags &= ~MSG_REPLIED;
4594 if (gslist_find_next_num(&forwarded, msginfo->msgnum) == msginfo->msgnum)
4595 flags |= MSG_FORWARDED;
4597 flags &= ~MSG_FORWARDED;
4598 if (gslist_find_next_num(&deleted, msginfo->msgnum) == msginfo->msgnum)
4599 flags |= MSG_DELETED;
4601 flags &= ~MSG_DELETED;
4604 if (flags_hash != NULL) {
4606 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash,
4607 GINT_TO_POINTER(msginfo->msgnum)));
4610 if ((flags & MSG_UNREAD) == 0)
4616 if (tags_hash != NULL) {
4617 GSList *tags = g_hash_table_lookup(tags_hash, GINT_TO_POINTER(msginfo->msgnum));
4621 g_slist_free(msginfo->tags);
4622 msginfo->tags = NULL;
4624 for (cur = tags; cur; cur = cur->next) {
4625 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
4627 id = tags_get_id_for_str(real_tag);
4629 id = tags_add_tag(real_tag);
4630 got_alien_tags = TRUE;
4632 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
4633 msginfo->tags = g_slist_append(
4635 GINT_TO_POINTER(id));
4639 slist_free_strings(tags);
4644 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4647 if (got_alien_tags) {
4649 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
4653 g_hash_table_destroy(flags_hash);
4655 g_hash_table_destroy(tags_hash);
4657 imap_lep_set_free(seq_list);
4658 g_slist_free(flagged);
4659 g_slist_free(deleted);
4660 g_slist_free(answered);
4661 g_slist_free(forwarded);
4662 g_slist_free(unseen);
4663 g_slist_free(sorted_list);
4666 return GINT_TO_POINTER(0);
4669 static gint imap_get_flags(Folder *folder, FolderItem *item,
4670 MsgInfoList *msginfo_list, GRelation *msgflags)
4673 get_flags_data *data = g_new0(get_flags_data, 1);
4675 data->folder = folder;
4677 data->msginfo_list = msginfo_list;
4678 data->msgflags = msgflags;
4679 data->full_search = FALSE;
4681 GSList *tmp = NULL, *cur;
4683 if (prefs_common.work_offline &&
4684 !inc_offline_should_override(FALSE,
4685 _("Claws Mail needs network access in order "
4686 "to access the IMAP server."))) {
4691 tmp = folder_item_get_msg_list(item);
4693 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4694 data->full_search = TRUE;
4696 for (cur = tmp; cur; cur = cur->next)
4697 procmsg_msginfo_free((MsgInfo *)cur->data);
4701 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4708 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4710 gboolean flags_set = GPOINTER_TO_INT(user_data);
4711 gint flags_value = GPOINTER_TO_INT(key);
4712 hashtable_data *data = (hashtable_data *)value;
4713 IMAPFolderItem *_item = data->item;
4714 FolderItem *item = (FolderItem *)_item;
4715 gint ok = MAILIMAP_ERROR_BAD_STATE;
4716 IMAPSession *session = NULL;
4718 debug_print("getting session...\n");
4719 session = imap_session_get(item->folder);
4721 data->msglist = g_slist_reverse(data->msglist);
4723 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4726 g_slist_length(data->msglist));
4728 lock_session(session);
4730 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4731 NULL, NULL, NULL, NULL, NULL, FALSE);
4733 if (ok == MAILIMAP_NO_ERROR) {
4734 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4735 data->msglist, flags_value, NULL, flags_set);
4737 g_warning("can't select mailbox %s\n", item->path);
4741 unlock_session(session);
4743 g_slist_free(data->msglist);
4748 static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
4750 gboolean tags_set = GPOINTER_TO_INT(user_data);
4751 TagsData *data = (TagsData *)value;
4752 IMAPFolderItem *_item = data->item;
4753 FolderItem *item = (FolderItem *)_item;
4754 gchar *str = data->str;
4755 gint ok = MAILIMAP_ERROR_BAD_STATE;
4756 IMAPSession *session = NULL;
4758 debug_print("getting session...\n");
4759 session = imap_session_get(item->folder);
4761 data->msglist = g_slist_reverse(data->msglist);
4763 debug_print("IMAP %ssetting tags %s for %d messages\n",
4766 g_slist_length(data->msglist));
4768 lock_session(session);
4770 ok = imap_select(session, IMAP_FOLDER(item->folder), item,
4771 NULL, NULL, NULL, NULL, NULL, FALSE);
4773 if (ok == MAILIMAP_NO_ERROR) {
4777 ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
4778 data->msglist, 0, &list, tags_set);
4780 g_warning("can't select mailbox %s\n", item->path);
4784 unlock_session(session);
4786 g_slist_free(data->msglist);
4792 static void process_hashtable(IMAPFolderItem *item)
4794 if (item->flags_set_table) {
4795 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4796 g_hash_table_destroy(item->flags_set_table);
4797 item->flags_set_table = NULL;
4799 if (item->flags_unset_table) {
4800 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4801 g_hash_table_destroy(item->flags_unset_table);
4802 item->flags_unset_table = NULL;
4804 if (item->tags_set_table) {
4805 g_hash_table_foreach_remove(item->tags_set_table, process_tags, GINT_TO_POINTER(TRUE));
4806 g_hash_table_destroy(item->tags_set_table);
4807 item->tags_set_table = NULL;
4809 if (item->tags_unset_table) {
4810 g_hash_table_foreach_remove(item->tags_unset_table, process_tags, GINT_TO_POINTER(FALSE));
4811 g_hash_table_destroy(item->tags_unset_table);
4812 item->tags_unset_table = NULL;
4817 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4819 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4820 IMAPSession *session;
4822 g_return_if_fail(item != NULL);
4824 if (item->batching == batch)
4828 item->batching = TRUE;
4829 debug_print("IMAP switching to batch mode\n");
4830 if (!item->flags_set_table) {
4831 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4833 if (!item->flags_unset_table) {
4834 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4836 if (!item->tags_set_table) {
4837 item->tags_set_table = g_hash_table_new(NULL, g_direct_equal);
4839 if (!item->tags_unset_table) {
4840 item->tags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4842 session = imap_session_get(folder);
4844 imap_refresh_sensitivity(session);
4845 session->sens_update_block = TRUE;
4848 debug_print("IMAP switching away from batch mode\n");
4850 process_hashtable(item);
4851 item->batching = FALSE;
4852 session = imap_session_get(folder);
4854 session->sens_update_block = FALSE;
4855 imap_refresh_sensitivity(session);
4862 /* data types conversion libetpan <-> claws */
4866 #define ETPAN_IMAP_MB_MARKED 1
4867 #define ETPAN_IMAP_MB_UNMARKED 2
4868 #define ETPAN_IMAP_MB_NOSELECT 4
4869 #define ETPAN_IMAP_MB_NOINFERIORS 8
4871 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4877 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4878 switch (imap_flags->mbf_sflag) {
4879 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4880 flags |= ETPAN_IMAP_MB_MARKED;
4882 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4883 flags |= ETPAN_IMAP_MB_NOSELECT;
4885 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4886 flags |= ETPAN_IMAP_MB_UNMARKED;
4891 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4892 cur = clist_next(cur)) {
4893 struct mailimap_mbx_list_oflag * oflag;
4895 oflag = clist_content(cur);
4897 switch (oflag->of_type) {
4898 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4899 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4907 static GSList * imap_list_from_lep(IMAPFolder * folder,
4908 clist * list, const gchar * real_path, gboolean all)
4911 GSList * item_list = NULL, *llast = NULL;
4913 for(iter = clist_begin(list) ; iter != NULL ;
4914 iter = clist_next(iter)) {
4915 struct mailimap_mailbox_list * mb;
4923 FolderItem *new_item;
4925 mb = clist_content(iter);
4931 if (mb->mb_flag != NULL)
4932 flags = imap_flags_to_flags(mb->mb_flag);
4934 delimiter = mb->mb_delimiter;
4937 dup_name = strdup(name);
4938 if (delimiter != '\0')
4939 subst_char(dup_name, delimiter, '/');
4941 base = g_path_get_basename(dup_name);
4942 if (base[0] == '.') {
4947 if (!all && path_cmp(name, real_path) == 0) {
4953 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4954 dup_name[strlen(dup_name)-1] = '\0';
4957 loc_name = imap_modified_utf7_to_utf8(base, FALSE);
4958 loc_path = imap_modified_utf7_to_utf8(dup_name, FALSE);
4960 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4961 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4962 new_item->no_sub = TRUE;
4963 if (strcmp(dup_name, "INBOX") != 0 &&
4964 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4965 new_item->no_select = TRUE;
4967 if (item_list == NULL)
4968 llast = item_list = g_slist_append(item_list, new_item);
4970 llast = g_slist_append(llast, new_item);
4971 llast = llast->next;
4973 debug_print("folder '%s' found.\n", loc_path);
4984 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
4986 GSList *sorted_list, *cur;
4987 guint first, last, next;
4988 GSList *ret_list = NULL, *llast = NULL;
4989 struct mailimap_set * current_set;
4990 unsigned int item_count;
4992 if (numlist == NULL)
4995 current_set = mailimap_set_new_empty();
4997 sorted_list = g_slist_copy(numlist);
4998 sorted_list = g_slist_sort(sorted_list, g_int_compare);
5000 first = GPOINTER_TO_INT(sorted_list->data);
5003 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
5004 if (GPOINTER_TO_INT(cur->data) == 0)
5009 last = GPOINTER_TO_INT(cur->data);
5011 next = GPOINTER_TO_INT(cur->next->data);
5015 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
5017 struct mailimap_set_item * item;
5018 item = mailimap_set_item_new(first, last);
5019 mailimap_set_add(current_set, item);
5023 if (item_count >= folder->max_set_size) {
5024 if (ret_list == NULL)
5025 llast = ret_list = g_slist_append(ret_list,
5028 llast = g_slist_append(llast, current_set);
5029 llast = llast->next;
5032 current_set = mailimap_set_new_empty();
5038 if (clist_count(current_set->set_list) > 0) {
5039 ret_list = g_slist_append(ret_list,
5043 g_slist_free(sorted_list);
5048 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
5050 MsgNumberList *numlist = NULL;
5054 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
5055 MsgInfo *msginfo = (MsgInfo *) cur->data;
5057 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
5059 numlist = g_slist_reverse(numlist);
5060 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
5061 g_slist_free(numlist);
5066 static GSList * imap_uid_list_from_lep(clist * list)
5073 for(iter = clist_begin(list) ; iter != NULL ;
5074 iter = clist_next(iter)) {
5077 puid = clist_content(iter);
5078 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5081 result = g_slist_reverse(result);
5085 static GSList * imap_uid_list_from_lep_tab(carray * list)
5092 for(i = 0 ; i < carray_count(list) ; i ++) {
5095 puid = carray_get(list, i);
5096 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
5098 result = g_slist_reverse(result);
5102 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
5104 GHashTable * tags_hash)
5111 for(i = 0 ; i < carray_count(list) ; i += 3) {
5116 puid = carray_get(list, i);
5117 pflags = carray_get(list, i + 1);
5118 tags = carray_get(list, i + 2);
5120 g_hash_table_insert(hash, GINT_TO_POINTER(*puid), GINT_TO_POINTER(* pflags));
5121 g_hash_table_insert(tags_hash, GINT_TO_POINTER(*puid), tags);
5125 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5128 MsgInfo *msginfo = NULL;
5131 MsgFlags flags = {0, 0};
5133 if (info->headers == NULL)
5136 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5137 if (folder_has_parent_of_type(item, F_QUEUE)) {
5138 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5139 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5140 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5142 flags.perm_flags = info->flags;
5146 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5149 msginfo->msgnum = uid;
5150 msginfo->size = size;
5156 static void imap_lep_set_free(GSList *seq_list)
5160 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5161 struct mailimap_set * imapset;
5163 imapset = cur->data;
5164 mailimap_set_free(imapset);
5166 g_slist_free(seq_list);
5169 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags)
5171 struct mailimap_flag_list * flag_list;
5174 flag_list = mailimap_flag_list_new_empty();
5176 if (IMAP_IS_SEEN(flags))
5177 mailimap_flag_list_add(flag_list,
5178 mailimap_flag_new_seen());
5179 if (IMAP_IS_ANSWERED(flags))
5180 mailimap_flag_list_add(flag_list,
5181 mailimap_flag_new_answered());
5182 if (IMAP_IS_FLAGGED(flags))
5183 mailimap_flag_list_add(flag_list,
5184 mailimap_flag_new_flagged());
5185 if (IMAP_IS_DELETED(flags))
5186 mailimap_flag_list_add(flag_list,
5187 mailimap_flag_new_deleted());
5188 if (IMAP_IS_DRAFT(flags))
5189 mailimap_flag_list_add(flag_list,
5190 mailimap_flag_new_draft());
5191 if (IMAP_IS_FORWARDED(flags) && flag_ok(item, IMAP_FLAG_FORWARDED))
5192 mailimap_flag_list_add(flag_list,
5193 mailimap_flag_new_flag_keyword(strdup("$Forwarded")));
5195 for (; cur; cur = cur->next) {
5197 imap_utf8_to_modified_utf7(cur->data, TRUE);
5198 g_strstrip(enc_str);
5200 mailimap_flag_list_add(flag_list,
5201 mailimap_flag_new_flag_keyword(enc_str));
5207 guint imap_folder_get_refcnt(Folder *folder)
5209 return ((IMAPFolder *)folder)->refcnt;
5212 void imap_folder_ref(Folder *folder)
5214 ((IMAPFolder *)folder)->refcnt++;
5217 void imap_disconnect_all(void)
5220 gboolean short_timeout;
5221 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5225 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5227 short_timeout = !networkmanager_is_online(&error);
5229 short_timeout = TRUE;
5230 g_error_free(error);
5233 short_timeout = TRUE;
5237 imap_main_set_timeout(1);
5239 for (list = account_get_list(); list != NULL; list = list->next) {
5240 PrefsAccount *account = list->data;
5241 if (account->protocol == A_IMAP4) {
5242 RemoteFolder *folder = (RemoteFolder *)account->folder;
5243 if (folder && folder->session) {
5244 IMAPSession *session = (IMAPSession *)folder->session;
5245 imap_threaded_disconnect(FOLDER(folder));
5246 SESSION(session)->state = SESSION_DISCONNECTED;
5247 SESSION(session)->sock = NULL;
5248 session_destroy(SESSION(session));
5249 folder->session = NULL;
5255 imap_main_set_timeout(prefs_common.io_timeout_secs);
5258 void imap_folder_unref(Folder *folder)
5260 if (((IMAPFolder *)folder)->refcnt > 0)
5261 ((IMAPFolder *)folder)->refcnt--;
5264 void imap_cancel_all(void)
5269 folderlist = folder_get_list();
5270 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5271 Folder *folder = (Folder *) cur->data;
5273 if (folder->klass == &imap_class) {
5274 if (imap_is_busy(folder)) {
5275 IMAPSession *imap_session;
5276 RemoteFolder *rfolder;
5278 g_printerr("cancelled\n");
5279 imap_threaded_cancel(folder);
5280 rfolder = (RemoteFolder *) folder;
5281 imap_session = (IMAPSession *) rfolder->session;
5283 imap_session->cancelled = 1;
5289 gboolean imap_cancel_all_enabled(void)
5294 folderlist = folder_get_list();
5295 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5296 Folder *folder = (Folder *) cur->data;
5298 if (folder->klass == &imap_class) {
5299 if (imap_is_busy(folder)) {
5308 static gboolean imap_is_busy(Folder *folder)
5310 IMAPSession *imap_session;
5311 RemoteFolder *rfolder;
5313 rfolder = (RemoteFolder *) folder;
5314 imap_session = (IMAPSession *) rfolder->session;
5315 if (imap_session == NULL)
5318 return imap_session->busy;
5321 #else /* HAVE_LIBETPAN */
5323 static FolderClass imap_class;
5325 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5326 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5328 static Folder *imap_folder_new (const gchar *name,
5331 static gboolean missing_imap_warning = TRUE;
5332 if (missing_imap_warning) {
5333 missing_imap_warning = FALSE;
5335 _("You have one or more IMAP accounts "
5336 "defined. However this version of "
5337 "Claws Mail has been built without "
5338 "IMAP support; your IMAP account(s) are "
5340 "You probably need to "
5341 "install libetpan and recompile "
5346 static gint imap_create_tree (Folder *folder)
5350 static FolderItem *imap_create_folder (Folder *folder,
5356 static gint imap_rename_folder (Folder *folder,
5363 gchar imap_get_path_separator_for_item(FolderItem *item)
5368 FolderClass *imap_get_class(void)
5370 if (imap_class.idstr == NULL) {
5371 imap_class.type = F_IMAP;
5372 imap_class.idstr = "imap";
5373 imap_class.uistr = "IMAP4";
5375 imap_class.new_folder = imap_folder_new;
5376 imap_class.create_tree = imap_create_tree;
5377 imap_class.create_folder = imap_create_folder;
5378 imap_class.rename_folder = imap_rename_folder;
5380 imap_class.set_xml = folder_set_xml;
5381 imap_class.get_xml = folder_get_xml;
5382 imap_class.item_set_xml = imap_item_set_xml;
5383 imap_class.item_get_xml = imap_item_get_xml;
5384 /* nothing implemented */
5390 void imap_disconnect_all(void)
5394 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5399 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5404 void imap_cache_msg(FolderItem *item, gint msgnum)
5408 void imap_cancel_all(void)
5412 gboolean imap_cancel_all_enabled(void)
5419 #ifdef HAVE_LIBETPAN
5420 static void imap_synchronise(FolderItem *item, gint days)
5422 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5423 debug_print("%s already synced\n", item->path?item->path:item->name);
5426 debug_print("syncing %s\n", item->path?item->path:item->name);
5427 imap_gtk_synchronise(item, days);
5428 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5432 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5434 #ifdef HAVE_LIBETPAN
5437 folder_item_set_xml(folder, item, tag);
5439 #ifdef HAVE_LIBETPAN
5440 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5441 XMLAttr *attr = (XMLAttr *) cur->data;
5443 if (!attr || !attr->name || !attr->value) continue;
5444 if (!strcmp(attr->name, "uidnext"))
5445 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5446 if (!strcmp(attr->name, "last_sync"))
5447 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5448 if (!strcmp(attr->name, "last_change"))
5449 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5451 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5452 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5456 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5460 tag = folder_item_get_xml(folder, item);
5462 #ifdef HAVE_LIBETPAN
5463 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5464 IMAP_FOLDER_ITEM(item)->uid_next));
5465 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5466 IMAP_FOLDER_ITEM(item)->last_sync));
5467 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5468 IMAP_FOLDER_ITEM(item)->last_change));
5474 /* ===================================================================
5475 * UTF-7 conversion routines as in RFC 2192
5476 * ===================================================================
5477 * These two functions from:
5479 * Copyright (C) 2003-2004 Pawel Salek. */
5481 /* UTF7 modified base64 alphabet */
5482 static char base64chars[] =
5483 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
5484 #define UNDEFINED 64
5486 /* UTF16 definitions */
5487 #define UTF16MASK 0x03FFUL
5488 #define UTF16SHIFT 10
5489 #define UTF16BASE 0x10000UL
5490 #define UTF16HIGHSTART 0xD800UL
5491 #define UTF16HIGHEND 0xDBFFUL
5492 #define UTF16LOSTART 0xDC00UL
5493 #define UTF16LOEND 0xDFFFUL
5496 /* Convert an IMAP mailbox to a UTF-8 string.
5497 * dst needs to have roughly 4 times the storage space of src
5498 * Hex encoding can triple the size of the input
5499 * UTF-7 can be slightly denser than UTF-8
5500 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
5502 char* imap_modified_utf7_to_utf8(const char *mbox, gboolean change_spaces)
5504 unsigned c, i, bitcount;
5505 unsigned long ucs4, utf16, bitbuf;
5506 unsigned char base64[256];
5508 char *dst, *res = malloc(2*strlen(mbox)+1);
5513 if(!dst) return NULL;
5514 /* initialize modified base64 decoding table */
5515 memset(base64, UNDEFINED, sizeof (base64));
5516 for (i = 0; i < sizeof (base64chars); ++i) {
5517 base64[(unsigned)base64chars[i]] = i;
5520 /* loop until end of string */
5521 while (*src != '\0') {
5523 /* deal with literal characters and &- */
5524 if (c != '&' || *src == '-') {
5525 /* encode literally */
5526 if (change_spaces && c == '_')
5530 /* skip over the '-' if this is an &- sequence */
5531 if (c == '&') ++src;
5533 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
5537 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
5539 bitbuf = (bitbuf << 6) | c;
5541 /* enough bits for a UTF-16 character? */
5542 if (bitcount >= 16) {
5544 utf16 = (bitcount ? bitbuf >> bitcount
5546 /* convert UTF16 to UCS4 */
5548 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
5549 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
5552 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
5553 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
5558 /* convert UTF-16 range of UCS4 to UTF-8 */
5559 if (ucs4 <= 0x7fUL) {
5562 } else if (ucs4 <= 0x7ffUL) {
5563 dst[0] = 0xc0 | (ucs4 >> 6);
5564 dst[1] = 0x80 | (ucs4 & 0x3f);
5566 } else if (ucs4 <= 0xffffUL) {
5567 dst[0] = 0xe0 | (ucs4 >> 12);
5568 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
5569 dst[2] = 0x80 | (ucs4 & 0x3f);
5572 dst[0] = 0xf0 | (ucs4 >> 18);
5573 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
5574 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
5575 dst[3] = 0x80 | (ucs4 & 0x3f);
5580 /* skip over trailing '-' in modified UTF-7 encoding */
5581 if (*src == '-') ++src;
5584 /* terminate destination string */
5589 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
5590 * dst should be about twice the length of src to deal with non-hex
5593 char* imap_utf8_to_modified_utf7(const char *src, gboolean change_spaces)
5595 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
5596 unsigned long ucs4 = 0, bitbuf = 0;
5598 /* initialize hex lookup table */
5599 char *dst, *res = malloc(2*strlen(src)+1);
5601 if(!dst) return NULL;
5607 while ((c = (unsigned char)*src) != '\0') {
5609 /* normal character? */
5610 if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) {
5611 /* switch out of UTF-7 mode */
5614 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5622 if (change_spaces && c == ' ')
5626 /* encode '&' as '&-' */
5632 /* switch to UTF-7 mode */
5637 /* Encode US-ASCII characters as themselves */
5641 } else if (utf8total) {
5642 /* save UTF8 bits into UCS4 */
5643 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
5644 if (++utf8pos < utf8total) {
5652 } else if (c < 0xF0) {
5656 /* NOTE: can't convert UTF8 sequences longer than 4 */
5662 /* loop to split ucs4 into two utf16 chars if necessary */
5665 if (ucs4 >= UTF16BASE) {
5667 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
5669 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
5672 bitbuf = (bitbuf << 16) | ucs4;
5676 /* spew out base64 */
5677 while (bitstogo >= 6) {
5679 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
5683 } while (utf16flag);
5685 /* if in UTF-7 mode, finish in ASCII */
5688 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
5692 /* tie off string */