2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 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"
57 #include "procheader.h"
58 #include "prefs_account.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
66 #include "remotefolder.h"
68 #include "statusbar.h"
70 #include "imap-thread.h"
75 typedef struct _IMAPFolder IMAPFolder;
76 typedef struct _IMAPSession IMAPSession;
77 typedef struct _IMAPNameSpace IMAPNameSpace;
78 typedef struct _IMAPFolderItem IMAPFolderItem;
80 #include "prefs_account.h"
82 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
83 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
84 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
90 /* list of IMAPNameSpace */
94 gchar last_seen_separator;
103 gboolean authenticated;
112 gboolean folder_content_changed;
123 gboolean sens_update_block;
126 struct _IMAPNameSpace
132 #define IMAPBUFSIZE 8192
134 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
135 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
136 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
137 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
138 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
139 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
140 #define IMAP_IS_SPAM(flags) ((flags & IMAP_FLAG_SPAM) != 0)
141 #define IMAP_IS_HAM(flags) ((flags & IMAP_FLAG_HAM) != 0)
144 #define IMAP4_PORT 143
146 #define IMAPS_PORT 993
149 #define IMAP_CMD_LIMIT 1000
152 ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
153 ITEM_CAN_CREATE_FLAGS,
154 ITEM_CANNOT_CREATE_FLAGS
157 struct _IMAPFolderItem
166 GHashTable *flags_set_table;
167 GHashTable *flags_unset_table;
170 gboolean should_update;
171 gboolean should_trash_cache;
172 gint can_create_flags;
174 GHashTable *tags_set_table;
175 GHashTable *tags_unset_table;
180 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
181 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
183 static void imap_folder_init (Folder *folder,
187 static Folder *imap_folder_new (const gchar *name,
189 static void imap_folder_destroy (Folder *folder);
191 static IMAPSession *imap_session_new (Folder *folder,
192 const PrefsAccount *account);
193 static gint imap_session_authenticate(IMAPSession *session,
194 PrefsAccount *account);
195 static void imap_session_destroy (Session *session);
197 static gchar *imap_fetch_msg (Folder *folder,
200 static gchar *imap_fetch_msg_full (Folder *folder,
205 static void imap_remove_cached_msg (Folder *folder,
208 static gint imap_add_msg (Folder *folder,
212 static gint imap_add_msgs (Folder *folder,
215 GRelation *relation);
217 static gint imap_copy_msg (Folder *folder,
220 static gint imap_copy_msgs (Folder *folder,
222 MsgInfoList *msglist,
223 GRelation *relation);
225 static gint imap_remove_msg (Folder *folder,
228 static gint imap_remove_msgs (Folder *folder,
230 MsgInfoList *msglist,
231 GRelation *relation);
232 static gint imap_expunge (Folder *folder,
234 static gint imap_remove_all_msg (Folder *folder,
237 static gboolean imap_is_msg_changed (Folder *folder,
241 static gint imap_close (Folder *folder,
244 static gint imap_scan_tree (Folder *folder);
246 static gint imap_create_tree (Folder *folder);
248 static FolderItem *imap_create_folder (Folder *folder,
251 static gint imap_rename_folder (Folder *folder,
254 static gint imap_remove_folder (Folder *folder,
257 static FolderItem *imap_folder_item_new (Folder *folder);
258 static void imap_folder_item_destroy (Folder *folder,
261 static IMAPSession *imap_session_get (Folder *folder);
263 static gint imap_auth (IMAPSession *session,
268 static gint imap_scan_tree_recursive (IMAPSession *session,
272 static void imap_create_missing_folders (Folder *folder);
273 static FolderItem *imap_create_special_folder
275 SpecialFolderItemType stype,
278 static gint imap_do_copy_msgs (Folder *folder,
280 MsgInfoList *msglist,
281 GRelation *relation);
283 static void imap_delete_all_cached_messages (FolderItem *item);
284 static void imap_set_batch (Folder *folder,
287 static gint imap_set_message_flags (IMAPSession *session,
288 IMAPFolderItem *item,
289 MsgNumberList *numlist,
293 static gint imap_select (IMAPSession *session,
299 guint32 *uid_validity,
300 gint *can_create_flags,
302 static gint imap_status (IMAPSession *session,
305 IMAPFolderItem *item,
308 guint32 *uid_validity,
311 static void imap_commit_tags (FolderItem *item,
316 static gchar imap_get_path_separator (IMAPSession *session,
320 static gchar *imap_get_real_path (IMAPSession *session,
325 static void imap_synchronise (FolderItem *item, gint days);
327 static gboolean imap_is_busy (Folder *folder);
329 static void imap_free_capabilities (IMAPSession *session);
331 /* low-level IMAP4rev1 commands */
332 static gint imap_cmd_login (IMAPSession *session,
336 static gint imap_cmd_noop (IMAPSession *session);
338 static gint imap_cmd_starttls (IMAPSession *session);
340 static gint imap_cmd_select (IMAPSession *session,
345 guint32 *uid_validity,
346 gint *can_create_flags,
349 static gint imap_cmd_close (IMAPSession *session);
350 static gint imap_cmd_examine (IMAPSession *session,
355 guint32 *uid_validity,
357 static gint imap_cmd_create (IMAPSession *sock,
358 const gchar *folder);
359 static gint imap_cmd_rename (IMAPSession *sock,
360 const gchar *oldfolder,
361 const gchar *newfolder);
362 static gint imap_cmd_delete (IMAPSession *session,
363 const gchar *folder);
364 static gint imap_cmd_fetch (IMAPSession *sock,
366 const gchar *filename,
369 static gint imap_cmd_append (IMAPSession *session,
370 IMAPFolderItem *item,
371 const gchar *destfolder,
375 static gint imap_cmd_copy (IMAPSession *session,
376 struct mailimap_set * set,
377 const gchar *destfolder,
378 GRelation *uid_mapping,
379 struct mailimap_set ** source,
380 struct mailimap_set ** dest);
381 static gint imap_cmd_store (IMAPSession *session,
382 IMAPFolderItem *item,
383 struct mailimap_set * set,
387 static gint imap_cmd_expunge (IMAPSession *session, gboolean force);
389 static void imap_path_separator_subst (gchar *str,
392 static gboolean imap_rename_folder_func (GNode *node,
394 static gint imap_get_num_list (Folder *folder,
397 gboolean *old_uids_valid);
398 static GSList *imap_get_msginfos (Folder *folder,
400 GSList *msgnum_list);
401 static MsgInfo *imap_get_msginfo (Folder *folder,
404 static gboolean imap_scan_required (Folder *folder,
406 static void imap_change_flags (Folder *folder,
409 MsgPermFlags newflags);
410 static gint imap_get_flags (Folder *folder,
412 MsgInfoList *msglist,
413 GRelation *msgflags);
414 static gchar *imap_folder_get_path (Folder *folder);
415 static gchar *imap_item_get_path (Folder *folder,
417 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
420 /* data types conversion libetpan <-> claws */
421 static GSList * imap_list_from_lep(IMAPFolder * folder,
422 clist * list, const gchar * real_path, gboolean all);
423 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
424 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
425 static GSList * imap_uid_list_from_lep(clist * list);
426 static GSList * imap_uid_list_from_lep_tab(carray * list);
427 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
429 GHashTable *tags_hash);
430 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
432 static void imap_lep_set_free(GSList *seq_list);
433 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
435 typedef struct _hashtable_data {
437 IMAPFolderItem *item;
440 static FolderClass imap_class;
442 FolderClass *imap_get_class(void)
444 if (imap_class.idstr == NULL) {
445 imap_class.type = F_IMAP;
446 imap_class.idstr = "imap";
447 imap_class.uistr = "IMAP4";
449 /* Folder functions */
450 imap_class.new_folder = imap_folder_new;
451 imap_class.destroy_folder = imap_folder_destroy;
452 imap_class.scan_tree = imap_scan_tree;
453 imap_class.create_tree = imap_create_tree;
455 /* FolderItem functions */
456 imap_class.item_new = imap_folder_item_new;
457 imap_class.item_destroy = imap_folder_item_destroy;
458 imap_class.item_get_path = imap_item_get_path;
459 imap_class.create_folder = imap_create_folder;
460 imap_class.rename_folder = imap_rename_folder;
461 imap_class.remove_folder = imap_remove_folder;
462 imap_class.close = imap_close;
463 imap_class.get_num_list = imap_get_num_list;
464 imap_class.scan_required = imap_scan_required;
465 imap_class.set_xml = folder_set_xml;
466 imap_class.get_xml = folder_get_xml;
467 imap_class.item_set_xml = imap_item_set_xml;
468 imap_class.item_get_xml = imap_item_get_xml;
470 /* Message functions */
471 imap_class.get_msginfo = imap_get_msginfo;
472 imap_class.get_msginfos = imap_get_msginfos;
473 imap_class.fetch_msg = imap_fetch_msg;
474 imap_class.fetch_msg_full = imap_fetch_msg_full;
475 imap_class.add_msg = imap_add_msg;
476 imap_class.add_msgs = imap_add_msgs;
477 imap_class.copy_msg = imap_copy_msg;
478 imap_class.copy_msgs = imap_copy_msgs;
479 imap_class.remove_msg = imap_remove_msg;
480 imap_class.remove_msgs = imap_remove_msgs;
481 imap_class.expunge = imap_expunge;
482 imap_class.remove_all_msg = imap_remove_all_msg;
483 imap_class.is_msg_changed = imap_is_msg_changed;
484 imap_class.change_flags = imap_change_flags;
485 imap_class.get_flags = imap_get_flags;
486 imap_class.set_batch = imap_set_batch;
487 imap_class.synchronise = imap_synchronise;
488 imap_class.remove_cached_msg = imap_remove_cached_msg;
489 imap_class.commit_tags = imap_commit_tags;
491 pthread_mutex_init(&imap_mutex, NULL);
498 static void imap_refresh_sensitivity (IMAPSession *session)
502 if (session->sens_update_block)
504 mainwin = mainwindow_get_mainwindow();
506 toolbar_main_set_sensitive(mainwin);
507 main_window_set_menu_sensitive(mainwin);
511 static void lock_session(IMAPSession *session)
514 debug_print("locking session %p (%d)\n", session, session->busy);
516 debug_print(" SESSION WAS LOCKED !! \n");
517 session->busy = TRUE;
518 imap_refresh_sensitivity(session);
520 debug_print("can't lock null session\n");
524 static void unlock_session(IMAPSession *session)
527 debug_print("unlocking session %p\n", session);
528 session->busy = FALSE;
529 imap_refresh_sensitivity(session);
531 debug_print("can't unlock null session\n");
535 static void imap_disc_session_destroy(IMAPSession *session)
537 RemoteFolder *rfolder = NULL;
542 rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
546 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
547 SESSION(session)->state = SESSION_DISCONNECTED;
548 SESSION(session)->sock = NULL;
551 static gboolean is_fatal(int libetpan_errcode)
553 switch(libetpan_errcode) {
554 case MAILIMAP_ERROR_STREAM:
555 case MAILIMAP_ERROR_PROTOCOL:
556 case MAILIMAP_ERROR_PARSE:
557 case MAILIMAP_ERROR_BAD_STATE:
564 static void imap_handle_error(Session *session, int libetpan_errcode)
566 const gchar *session_server = (session ? session->server:"(null)");
567 switch(libetpan_errcode) {
568 case MAILIMAP_NO_ERROR:
570 case MAILIMAP_NO_ERROR_AUTHENTICATED:
571 log_warning(LOG_PROTOCOL, _("IMAP error on %s: authenticated\n"), session_server);
573 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
574 log_warning(LOG_PROTOCOL, _("IMAP error on %s: not authenticated\n"), session_server);
576 case MAILIMAP_ERROR_BAD_STATE:
577 log_warning(LOG_PROTOCOL, _("IMAP error on %s: bad state\n"), session_server);
579 case MAILIMAP_ERROR_STREAM:
580 log_warning(LOG_PROTOCOL, _("IMAP error on %s: stream error\n"), session_server);
582 case MAILIMAP_ERROR_PARSE:
583 log_warning(LOG_PROTOCOL, _("IMAP error on %s: parse error "
584 "(very probably non-RFC compliance from the server)\n"), session_server);
586 case MAILIMAP_ERROR_CONNECTION_REFUSED:
587 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection refused\n"), session_server);
589 case MAILIMAP_ERROR_MEMORY:
590 log_warning(LOG_PROTOCOL, _("IMAP error on %s: memory error\n"), session_server);
592 case MAILIMAP_ERROR_FATAL:
593 log_warning(LOG_PROTOCOL, _("IMAP error on %s: fatal error\n"), session_server);
595 case MAILIMAP_ERROR_PROTOCOL:
596 log_warning(LOG_PROTOCOL, _("IMAP error on %s: protocol error"
597 "(very probably non-RFC compliance from the server)\n"), session_server);
599 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
600 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection not accepted\n"), session_server);
602 case MAILIMAP_ERROR_APPEND:
603 log_warning(LOG_PROTOCOL, _("IMAP error on %s: APPEND error\n"), session_server);
605 case MAILIMAP_ERROR_NOOP:
606 log_warning(LOG_PROTOCOL, _("IMAP error on %s: NOOP error\n"), session_server);
608 case MAILIMAP_ERROR_LOGOUT:
609 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGOUT error\n"), session_server);
611 case MAILIMAP_ERROR_CAPABILITY:
612 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CAPABILITY error\n"), session_server);
614 case MAILIMAP_ERROR_CHECK:
615 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CHECK error\n"), session_server);
617 case MAILIMAP_ERROR_CLOSE:
618 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CLOSE error\n"), session_server);
620 case MAILIMAP_ERROR_EXPUNGE:
621 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXPUNGE error\n"), session_server);
623 case MAILIMAP_ERROR_COPY:
624 log_warning(LOG_PROTOCOL, _("IMAP error on %s: COPY error\n"), session_server);
626 case MAILIMAP_ERROR_UID_COPY:
627 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID COPY error\n"), session_server);
629 case MAILIMAP_ERROR_CREATE:
630 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CREATE error\n"), session_server);
632 case MAILIMAP_ERROR_DELETE:
633 log_warning(LOG_PROTOCOL, _("IMAP error on %s: DELETE error\n"), session_server);
635 case MAILIMAP_ERROR_EXAMINE:
636 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXAMINE error\n"), session_server);
638 case MAILIMAP_ERROR_FETCH:
639 log_warning(LOG_PROTOCOL, _("IMAP error on %s: FETCH error\n"), session_server);
641 case MAILIMAP_ERROR_UID_FETCH:
642 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID FETCH error\n"), session_server);
644 case MAILIMAP_ERROR_LIST:
645 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LIST error\n"), session_server);
647 case MAILIMAP_ERROR_LOGIN:
648 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGIN error\n"), session_server);
650 case MAILIMAP_ERROR_LSUB:
651 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LSUB error\n"), session_server);
653 case MAILIMAP_ERROR_RENAME:
654 log_warning(LOG_PROTOCOL, _("IMAP error on %s: RENAME error\n"), session_server);
656 case MAILIMAP_ERROR_SEARCH:
657 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SEARCH error\n"), session_server);
659 case MAILIMAP_ERROR_UID_SEARCH:
660 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID SEARCH error\n"), session_server);
662 case MAILIMAP_ERROR_SELECT:
663 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SELECT error\n"), session_server);
665 case MAILIMAP_ERROR_STATUS:
666 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STATUS error\n"), session_server);
668 case MAILIMAP_ERROR_STORE:
669 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STORE error\n"), session_server);
671 case MAILIMAP_ERROR_UID_STORE:
672 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID STORE error\n"), session_server);
674 case MAILIMAP_ERROR_SUBSCRIBE:
675 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SUBSCRIBE error\n"), session_server);
677 case MAILIMAP_ERROR_UNSUBSCRIBE:
678 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UNSUBSCRIBE error\n"), session_server);
680 case MAILIMAP_ERROR_STARTTLS:
681 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STARTTLS error\n"), session_server);
683 case MAILIMAP_ERROR_INVAL:
684 log_warning(LOG_PROTOCOL, _("IMAP error on %s: INVAL error\n"), session_server);
686 case MAILIMAP_ERROR_EXTENSION:
687 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXTENSION error\n"), session_server);
689 case MAILIMAP_ERROR_SASL:
690 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SASL error\n"), session_server);
693 case MAILIMAP_ERROR_SSL:
694 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SSL error\n"), session_server);
698 log_warning(LOG_PROTOCOL, _("IMAP error on %s: Unknown error [%d]\n"),
699 session_server, libetpan_errcode);
703 if (session && is_fatal(libetpan_errcode)) {
704 imap_disc_session_destroy(IMAP_SESSION(session));
705 } else if (session && !is_fatal(libetpan_errcode)) {
706 if (IMAP_SESSION(session)->busy)
707 unlock_session(IMAP_SESSION(session));
711 static Folder *imap_folder_new(const gchar *name, const gchar *path)
715 folder = (Folder *)g_new0(IMAPFolder, 1);
716 folder->klass = &imap_class;
717 imap_folder_init(folder, name, path);
722 static void imap_folder_destroy(Folder *folder)
724 while (imap_folder_get_refcnt(folder) > 0)
725 gtk_main_iteration();
727 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
731 static void imap_folder_init(Folder *folder, const gchar *name,
734 folder_remote_folder_init((Folder *)folder, name, path);
735 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
738 static FolderItem *imap_folder_item_new(Folder *folder)
740 IMAPFolderItem *item;
742 item = g_new0(IMAPFolderItem, 1);
745 item->uid_list = NULL;
747 return (FolderItem *)item;
750 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
752 IMAPFolderItem *item = (IMAPFolderItem *)_item;
754 g_return_if_fail(item != NULL);
755 g_slist_free(item->uid_list);
760 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
762 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
765 g_slist_free(item->uid_list);
766 item->uid_list = NULL;
771 static void imap_reset_uid_lists(Folder *folder)
773 if(folder->node == NULL)
776 /* Destroy all uid lists and rest last uid */
777 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
780 static int imap_get_capabilities(IMAPSession *session)
782 struct mailimap_capability_data *capabilities = NULL;
786 if (session->capability != NULL)
787 return MAILIMAP_NO_ERROR;
789 capabilities = imap_threaded_capability(session->folder, &result);
791 if (result != MAILIMAP_NO_ERROR) {
795 if (capabilities == NULL || capabilities->cap_list == NULL) {
796 return MAILIMAP_NO_ERROR;
799 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
800 cur = clist_next(cur)) {
801 struct mailimap_capability * cap =
803 if (!cap || cap->cap_data.cap_name == NULL)
805 session->capability = g_slist_append
806 (session->capability,
807 g_strdup(cap->cap_data.cap_name));
808 debug_print("got capa %s\n", cap->cap_data.cap_name);
810 mailimap_capability_data_free(capabilities);
811 return MAILIMAP_NO_ERROR;
814 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
817 for (cur = session->capability; cur; cur = cur->next) {
818 if (!g_ascii_strcasecmp(cur->data, cap))
824 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
827 gint ok = MAILIMAP_ERROR_LOGIN;
828 static time_t last_login_err = 0;
829 gchar *ext_info = "";
831 gchar *server = NULL;
832 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
833 imap_handle_error(SESSION(session), r);
836 server = g_strdup(SESSION(session)->server);
839 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
841 case IMAP_AUTH_CRAM_MD5:
842 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
844 case IMAP_AUTH_DIGEST_MD5:
845 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
847 case IMAP_AUTH_LOGIN:
848 ok = imap_cmd_login(session, user, pass, "LOGIN");
850 case IMAP_AUTH_GSSAPI:
851 ok = imap_cmd_login(session, user, pass, "GSSAPI");
854 debug_print("capabilities:\n"
860 imap_has_capability(session, "ANONYMOUS"),
861 imap_has_capability(session, "CRAM-MD5"),
862 imap_has_capability(session, "DIGEST-MD5"),
863 imap_has_capability(session, "LOGIN"),
864 imap_has_capability(session, "GSSAPI"));
865 if (imap_has_capability(session, "CRAM-MD5"))
866 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
867 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
868 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
869 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
870 ok = imap_cmd_login(session, user, pass, "GSSAPI");
871 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
872 ok = imap_cmd_login(session, user, pass, "LOGIN");
875 if (ok == MAILIMAP_NO_ERROR)
876 session->authenticated = TRUE;
878 if (type == IMAP_AUTH_CRAM_MD5) {
879 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
880 "compiled with SASL support and the "
881 "CRAM-MD5 SASL plugin is installed.");
884 if (type == IMAP_AUTH_DIGEST_MD5) {
885 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
886 "compiled with SASL support and the "
887 "DIGEST-MD5 SASL plugin is installed.");
890 if (time(NULL) - last_login_err > 10) {
891 if (!prefs_common.no_recv_err_panel) {
892 alertpanel_error_log(_("Connection to %s failed: "
896 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
897 "login refused.%s\n"),
901 last_login_err = time(NULL);
907 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
909 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
910 /* Check if this is the first try to establish a
911 connection, if yes we don't try to reconnect */
912 debug_print("reconnecting\n");
913 if (rfolder->session == NULL) {
914 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
915 folder->account->recv_server);
916 SESSION(session)->sock = NULL;
917 session_destroy(SESSION(session));
920 rfolder->session = NULL;
921 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
922 " disconnected. Reconnecting...\n"),
923 folder->account->recv_server);
924 statusbar_print_all(_("IMAP4 connection to %s has been"
925 " disconnected. Reconnecting...\n"),
926 folder->account->recv_server);
927 SESSION(session)->state = SESSION_DISCONNECTED;
928 SESSION(session)->sock = NULL;
929 session_destroy(SESSION(session));
930 /* Clear folders session to make imap_session_get create
931 a new session, because of rfolder->session == NULL
932 it will not try to reconnect again and so avoid an
934 debug_print("getting session...\n");
935 session = imap_session_get(folder);
936 rfolder->session = SESSION(session);
942 static IMAPSession *imap_session_get(Folder *folder)
944 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
945 IMAPSession *session = NULL;
946 gint r = MAILIMAP_NO_ERROR;
948 g_return_val_if_fail(folder != NULL, NULL);
949 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
950 g_return_val_if_fail(folder->account != NULL, NULL);
952 if (prefs_common.work_offline &&
953 !inc_offline_should_override(FALSE,
954 _("Claws Mail needs network access in order "
955 "to access the IMAP server."))) {
959 /* Make sure we have a session */
960 if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
961 session = IMAP_SESSION(rfolder->session);
962 } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
963 session_destroy(SESSION(rfolder->session));
964 rfolder->session = NULL;
966 } else if (rfolder->connecting) {
967 debug_print("already connecting\n");
971 imap_reset_uid_lists(folder);
972 if (time(NULL) - rfolder->last_failure <= 2)
974 rfolder->connecting = TRUE;
975 session = imap_session_new(folder, folder->account);
977 if(session == NULL) {
978 rfolder->last_failure = time(NULL);
979 rfolder->connecting = FALSE;
983 /* Make sure session is authenticated */
984 if (!IMAP_SESSION(session)->authenticated)
985 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
987 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
988 rfolder->session = NULL;
990 imap_threaded_disconnect(session->folder);
991 SESSION(session)->state = SESSION_DISCONNECTED;
992 SESSION(session)->sock = NULL;
993 session_destroy(SESSION(session));
995 rfolder->last_failure = time(NULL);
996 rfolder->connecting = FALSE;
1000 /* I think the point of this code is to avoid sending a
1001 * keepalive if we've used the session recently and therefore
1002 * think it's still alive. Unfortunately, most of the code
1003 * does not yet check for errors on the socket, and so if the
1004 * connection drops we don't notice until the timeout expires.
1005 * A better solution than sending a NOOP every time would be
1006 * for every command to be prepared to retry until it is
1007 * successfully sent. -- mbp */
1008 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1009 /* verify that the session is still alive */
1010 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1011 debug_print("disconnected!\n");
1013 session = imap_reconnect_if_possible(folder, session);
1015 rfolder->session = NULL;
1016 rfolder->connecting = FALSE;
1017 session = imap_session_get(folder);
1021 session->cancelled = FALSE;
1024 rfolder->session = SESSION(session);
1025 rfolder->connecting = FALSE;
1027 return IMAP_SESSION(session);
1030 static IMAPSession *imap_session_new(Folder * folder,
1031 const PrefsAccount *account)
1033 IMAPSession *session;
1036 int authenticated = FALSE;
1039 /* FIXME: IMAP over SSL only... */
1042 port = account->set_imapport ? account->imapport
1043 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1044 ssl_type = account->ssl_imap;
1046 if (account->ssl_imap != SSL_NONE) {
1047 if (alertpanel_full(_("Insecure connection"),
1048 _("This connection is configured to be secured "
1049 "using SSL, but SSL is not available in this "
1050 "build of Claws Mail. \n\n"
1051 "Do you want to continue connecting to this "
1052 "server? The communication would not be "
1054 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1055 NULL, FALSE, NULL, ALERT_WARNING,
1056 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1059 port = account->set_imapport ? account->imapport
1064 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1066 if (account->set_tunnelcmd) {
1067 r = imap_threaded_connect_cmd(folder,
1069 account->recv_server,
1076 if (ssl_type == SSL_TUNNEL) {
1077 r = imap_threaded_connect_ssl(folder,
1078 account->recv_server,
1084 r = imap_threaded_connect(folder,
1085 account->recv_server,
1090 statuswindow_pop_all();
1091 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1092 authenticated = TRUE;
1094 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1095 authenticated = FALSE;
1099 if (r == MAILIMAP_ERROR_SSL)
1100 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1103 imap_handle_error(NULL, r);
1105 if(!prefs_common.no_recv_err_panel) {
1106 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1107 account->recv_server, port);
1109 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1110 account->recv_server, port);
1116 session = g_new0(IMAPSession, 1);
1117 session_init(SESSION(session), account, FALSE);
1118 SESSION(session)->type = SESSION_IMAP;
1119 SESSION(session)->server = g_strdup(account->recv_server);
1120 SESSION(session)->port = port;
1121 SESSION(session)->sock = NULL;
1123 SESSION(session)->destroy = imap_session_destroy;
1125 session->capability = NULL;
1127 session->authenticated = authenticated;
1128 session->mbox = NULL;
1129 session->exists = 0;
1130 session->recent = 0;
1131 session->expunge = 0;
1132 session->cmd_count = 0;
1133 session->folder = folder;
1134 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1137 if (account->ssl_imap == SSL_STARTTLS) {
1140 ok = imap_cmd_starttls(session);
1141 if (ok != MAILIMAP_NO_ERROR) {
1142 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1143 if (!is_fatal(ok)) {
1144 SESSION(session)->sock = NULL;
1145 session_destroy(SESSION(session));
1150 imap_free_capabilities(session);
1151 session->authenticated = FALSE;
1152 session->uidplus = FALSE;
1153 session->cmd_count = 1;
1156 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1157 (session->authenticated) ? "pre" : "un");
1162 static gint imap_session_authenticate(IMAPSession *session,
1163 PrefsAccount *account)
1165 gchar *pass, *acc_pass;
1166 gboolean failed = FALSE;
1167 gint ok = MAILIMAP_NO_ERROR;
1168 g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1169 acc_pass = account->passwd;
1172 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON && account->imap_auth_type != IMAP_AUTH_GSSAPI) {
1174 tmp_pass = input_dialog_query_password_keep(account->recv_server,
1176 &(account->session_passwd));
1178 return MAILIMAP_NO_ERROR;
1179 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1181 } else if (account->imap_auth_type == IMAP_AUTH_ANON || account->imap_auth_type == IMAP_AUTH_GSSAPI) {
1184 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1185 account->recv_server);
1186 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1187 statusbar_pop_all();
1189 if (!failed && !is_fatal(ok)) {
1192 if (account->session_passwd != NULL) {
1193 g_free(account->session_passwd);
1194 account->session_passwd = NULL;
1198 if (prefs_common.no_recv_err_panel) {
1199 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1200 mainwindow_show_error();
1202 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1208 statuswindow_pop_all();
1209 session->authenticated = TRUE;
1210 return MAILIMAP_NO_ERROR;
1213 static void imap_session_destroy(Session *session)
1215 if (session->state != SESSION_DISCONNECTED)
1216 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1218 imap_free_capabilities(IMAP_SESSION(session));
1219 g_free(IMAP_SESSION(session)->mbox);
1222 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1224 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1227 static guint get_file_size_with_crs(const gchar *filename)
1233 if (filename == NULL)
1236 fp = g_fopen(filename, "rb");
1240 while (fgets(buf, sizeof (buf), fp) != NULL) {
1242 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1250 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1252 gchar *path, *filename;
1254 path = folder_item_get_path(item);
1256 if (!is_dir_exist(path)) {
1261 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1264 if (is_file_exist(filename)) {
1265 claws_unlink(filename);
1270 typedef struct _TagsData {
1273 IMAPFolderItem *item;
1276 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1278 IMAPSession *session;
1279 gint ok, can_create_tags;
1280 Folder *folder = NULL;
1281 TagsData *ht_data = NULL;
1284 g_return_if_fail(item != NULL);
1285 g_return_if_fail(msginfo != NULL);
1287 folder = item->folder;
1288 debug_print("getting session...\n");
1289 session = imap_session_get(folder);
1292 debug_print("can't get session\n");
1296 ok = imap_select(session, IMAP_FOLDER(folder), item,
1297 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1299 if (ok != MAILIMAP_NO_ERROR) {
1304 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1307 if (IMAP_FOLDER_ITEM(item)->batching) {
1308 /* instead of performing an UID STORE command for each message change,
1309 * as a lot of them can change "together", we just fill in hashtables
1310 * and defer the treatment so that we're able to send only one
1313 debug_print("IMAP batch mode on, deferring tags change\n");
1314 for (cur = tags_set; cur; cur = cur->next) {
1315 gint cur_tag = GPOINTER_TO_INT(cur->data);
1317 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1318 GINT_TO_POINTER(cur_tag));
1319 if (ht_data == NULL) {
1320 ht_data = g_new0(TagsData, 1);
1321 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1322 ht_data->item = IMAP_FOLDER_ITEM(item);
1323 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1324 GINT_TO_POINTER(cur_tag), ht_data);
1326 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1329 for (cur = tags_unset; cur; cur = cur->next) {
1330 gint cur_tag = GPOINTER_TO_INT(cur->data);
1332 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1333 GINT_TO_POINTER(cur_tag));
1334 if (ht_data == NULL) {
1335 ht_data = g_new0(TagsData, 1);
1336 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1337 ht_data->item = IMAP_FOLDER_ITEM(item);
1338 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1339 GINT_TO_POINTER(cur_tag), ht_data);
1341 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1345 GSList *list_set = NULL;
1346 GSList *list_unset = NULL;
1349 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1350 numlist.next = NULL;
1352 debug_print("IMAP changing tags NOW\n");
1353 for (cur = tags_set; cur; cur = cur->next) {
1354 gint cur_tag = GPOINTER_TO_INT(cur->data);
1355 const gchar *str = tags_get_tag(cur_tag);
1356 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1357 list_set = g_slist_prepend(list_set, g_strdup(str));
1360 ok = imap_set_message_flags(session,
1361 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1362 slist_free_strings(list_set);
1363 g_slist_free(list_set);
1364 if (ok != MAILIMAP_NO_ERROR) {
1369 for (cur = tags_unset; cur; cur = cur->next) {
1370 gint cur_tag = GPOINTER_TO_INT(cur->data);
1371 const gchar *str = tags_get_tag(cur_tag);
1372 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1373 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1376 ok = imap_set_message_flags(session,
1377 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1378 slist_free_strings(list_unset);
1379 g_slist_free(list_unset);
1380 if (ok != MAILIMAP_NO_ERROR) {
1387 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1388 gboolean headers, gboolean body)
1390 gchar *path, *filename;
1391 IMAPSession *session;
1394 g_return_val_if_fail(folder != NULL, NULL);
1395 g_return_val_if_fail(item != NULL, NULL);
1400 path = folder_item_get_path(item);
1401 if (!is_dir_exist(path))
1402 make_dir_hier(path);
1403 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1405 debug_print("trying to fetch cached %s\n", filename);
1406 if (is_file_exist(filename)) {
1407 /* see whether the local file represents the whole message
1408 * or not. As the IMAP server reports size with \r chars,
1409 * we have to update the local file (UNIX \n only) size */
1410 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1411 guint have_size = -1;
1414 debug_print("message %d has been already %scached.\n", uid,
1415 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1417 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1418 have_size = get_file_size_with_crs(filename);
1419 if (cached && (cached->size <= have_size || !body)) {
1420 procmsg_msginfo_free(cached);
1421 ok = file_strip_crs(filename);
1422 if (ok == 0 && cached && cached->size <= have_size) {
1423 /* we have it all and stripped */
1424 debug_print("...fully cached in fact; setting flag.\n");
1425 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1428 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1429 debug_print("message not cached and file recent, considering file complete\n");
1430 ok = file_strip_crs(filename);
1434 procmsg_msginfo_free(cached);
1437 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1438 procmsg_msginfo_free(cached);
1442 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1444 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1445 procmsg_msginfo_free(cached);
1449 debug_print("getting session...\n");
1450 session = imap_session_get(folder);
1456 session_set_access_time(SESSION(session));
1457 lock_session(session); /* unlocked later in the function */
1459 debug_print("IMAP fetching messages\n");
1460 ok = imap_select(session, IMAP_FOLDER(folder), item,
1461 NULL, NULL, NULL, NULL, NULL, FALSE);
1462 if (ok != MAILIMAP_NO_ERROR) {
1463 g_warning("can't select mailbox %s\n", item->path);
1468 session_set_access_time(SESSION(session));
1470 debug_print("getting message %d...\n", uid);
1471 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1473 if (ok != MAILIMAP_NO_ERROR) {
1474 g_warning("can't fetch message %d\n", uid);
1479 session_set_access_time(SESSION(session));
1480 unlock_session(session);
1482 ok = file_strip_crs(filename);
1484 if (ok == 0 && headers && body) {
1485 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1487 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1488 procmsg_msginfo_free(cached);
1490 } else if (ok == -1) {
1491 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1493 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1494 procmsg_msginfo_free(cached);
1500 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1502 gchar *path, *filename;
1504 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1509 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1510 procmsg_msginfo_free(cached);
1513 path = folder_item_get_path(item);
1514 if (!is_dir_exist(path))
1517 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1519 if (is_file_exist(filename)) {
1520 if (cached && cached->total_size == cached->size) {
1523 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1526 size = get_file_size_with_crs(filename);
1529 if (cached && size >= cached->size) {
1530 cached->total_size = cached->size;
1531 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1532 procmsg_msginfo_free(cached);
1536 procmsg_msginfo_free(cached);
1540 void imap_cache_msg(FolderItem *item, gint msgnum)
1542 Folder *folder = NULL;
1546 folder = item->folder;
1548 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1549 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1550 debug_print("fetched %s\n", tmp);
1555 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1556 const gchar *file, MsgFlags *flags)
1560 MsgFileInfo fileinfo;
1562 g_return_val_if_fail(file != NULL, -1);
1564 fileinfo.msginfo = NULL;
1565 fileinfo.file = (gchar *)file;
1566 fileinfo.flags = flags;
1567 file_list.data = &fileinfo;
1568 file_list.next = NULL;
1570 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1574 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1575 GRelation *relation)
1578 IMAPSession *session;
1579 guint32 last_uid = 0;
1581 MsgFileInfo *fileinfo;
1582 gint ok = MAILIMAP_NO_ERROR;
1583 gint curnum = 0, total = 0;
1584 gboolean missing_uids = FALSE;
1586 g_return_val_if_fail(folder != NULL, -1);
1587 g_return_val_if_fail(dest != NULL, -1);
1588 g_return_val_if_fail(file_list != NULL, -1);
1590 debug_print("getting session...\n");
1591 session = imap_session_get(folder);
1595 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1598 statusbar_print_all(_("Adding messages..."));
1599 total = g_slist_length(file_list);
1600 for (cur = file_list; cur != NULL; cur = cur->next) {
1601 IMAPFlags iflags = 0;
1602 guint32 new_uid = 0;
1603 gchar *real_file = NULL;
1604 fileinfo = (MsgFileInfo *)cur->data;
1606 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1609 if (fileinfo->flags) {
1610 if (MSG_IS_MARKED(*fileinfo->flags))
1611 iflags |= IMAP_FLAG_FLAGGED;
1612 if (MSG_IS_REPLIED(*fileinfo->flags))
1613 iflags |= IMAP_FLAG_ANSWERED;
1614 if (MSG_IS_FORWARDED(*fileinfo->flags))
1615 iflags |= IMAP_FLAG_FORWARDED;
1616 if (MSG_IS_SPAM(*fileinfo->flags))
1617 iflags |= IMAP_FLAG_SPAM;
1619 iflags |= IMAP_FLAG_HAM;
1620 if (!MSG_IS_UNREAD(*fileinfo->flags))
1621 iflags |= IMAP_FLAG_SEEN;
1625 if (real_file == NULL)
1626 real_file = g_strdup(fileinfo->file);
1628 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1629 folder_has_parent_of_type(dest, F_OUTBOX) ||
1630 folder_has_parent_of_type(dest, F_DRAFT) ||
1631 folder_has_parent_of_type(dest, F_TRASH))
1632 iflags |= IMAP_FLAG_SEEN;
1634 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1637 if (ok != MAILIMAP_NO_ERROR) {
1638 g_warning("can't append message %s\n", real_file);
1641 statusbar_progress_all(0,0,0);
1642 statusbar_pop_all();
1645 debug_print("appended new message as %d\n", new_uid);
1646 /* put the local file in the imapcache, so that we don't
1647 * have to fetch it back later. */
1650 missing_uids = TRUE;
1651 debug_print("Missing UID (0)\n");
1654 gchar *cache_path = folder_item_get_path(dest);
1655 if (!is_dir_exist(cache_path))
1656 make_dir_hier(cache_path);
1657 if (is_dir_exist(cache_path)) {
1658 gchar *cache_file = g_strconcat(
1659 cache_path, G_DIR_SEPARATOR_S,
1660 itos(new_uid), NULL);
1661 copy_file(real_file, cache_file, TRUE);
1662 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1669 if (relation != NULL)
1670 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1671 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1672 GINT_TO_POINTER(new_uid));
1673 if (last_uid < new_uid) {
1680 statusbar_progress_all(0,0,0);
1681 statusbar_pop_all();
1686 imap_scan_required(folder, dest);
1688 session = imap_session_get(folder);
1694 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1695 &a, NULL, NULL, NULL, NULL, FALSE);
1700 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1702 GSList *result = NULL;
1707 if (!set || !set->set_list)
1710 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1711 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1712 start = item->set_first;
1713 end = item->set_last;
1714 for (t = start; t <= end; t++) {
1715 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1718 result = g_slist_reverse(result);
1719 if (debug_get_mode()) {
1720 debug_print("flat imap set: ");
1721 for (cur = result; cur; cur = cur->next) {
1722 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1729 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1730 MsgInfoList *msglist, GRelation *relation)
1734 GSList *seq_list, *cur;
1736 IMAPSession *session;
1737 gint ok = MAILIMAP_NO_ERROR;
1738 GRelation *uid_mapping;
1740 gboolean single = FALSE;
1742 g_return_val_if_fail(folder != NULL, -1);
1743 g_return_val_if_fail(dest != NULL, -1);
1744 g_return_val_if_fail(msglist != NULL, -1);
1746 debug_print("getting session...\n");
1747 session = imap_session_get(folder);
1753 msginfo = (MsgInfo *)msglist->data;
1754 if (msglist->next == NULL)
1756 src = msginfo->folder;
1758 g_warning("the src folder is identical to the dest.\n");
1762 if (src->folder != dest->folder) {
1763 GSList *infolist = NULL, *cur;
1765 for (cur = msglist; cur; cur = cur->next) {
1766 msginfo = (MsgInfo *)cur->data;
1767 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1768 fileinfo->file = procmsg_get_message_file(msginfo);
1769 fileinfo->flags = &(msginfo->flags);
1770 infolist = g_slist_prepend(infolist, fileinfo);
1772 infolist = g_slist_reverse(infolist);
1773 res = folder_item_add_msgs(dest, infolist, FALSE);
1774 for (cur = infolist; cur; cur = cur->next) {
1775 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1779 g_slist_free(infolist);
1783 lock_session(session); /* unlocked later in the function */
1785 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1786 NULL, NULL, NULL, NULL, NULL, FALSE);
1787 if (ok != MAILIMAP_NO_ERROR) {
1791 unlock_session(session);
1793 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1798 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1799 uid_mapping = g_relation_new(2);
1800 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1802 statusbar_print_all(_("Copying messages..."));
1803 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1804 struct mailimap_set * seq_set;
1805 struct mailimap_set * source = NULL;
1806 struct mailimap_set * dest = NULL;
1807 seq_set = cur->data;
1809 debug_print("Copying messages from %s to %s ...\n",
1810 src->path, destdir);
1812 lock_session(session); /* unlocked later in the function */
1813 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1820 if (ok == MAILIMAP_NO_ERROR) {
1821 unlock_session(session);
1822 if (relation && source && dest) {
1823 GSList *s_list = flatten_mailimap_set(source);
1824 GSList *d_list = flatten_mailimap_set(dest);
1825 GSList *s_cur, *d_cur;
1826 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1828 for (s_cur = s_list, d_cur = d_list;
1830 s_cur = s_cur->next, d_cur = d_cur->next) {
1831 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1835 debug_print("hhhmm, source list length != dest list length.\n");
1837 g_slist_free(s_list);
1838 g_slist_free(d_list);
1844 mailimap_set_free(source);
1846 mailimap_set_free(dest);
1848 if (ok != MAILIMAP_NO_ERROR) {
1849 g_relation_destroy(uid_mapping);
1850 imap_lep_set_free(seq_list);
1851 statusbar_pop_all();
1856 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1857 MsgInfo *msginfo = (MsgInfo *)cur->data;
1860 tuples = g_relation_select(uid_mapping,
1861 GINT_TO_POINTER(msginfo->msgnum),
1863 if (tuples->len > 0) {
1864 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1865 g_relation_insert(relation, msginfo,
1866 GINT_TO_POINTER(num));
1869 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1870 /* put the local file in the imapcache, so that we don't
1871 * have to fetch it back later. */
1873 gchar *cache_path = folder_item_get_path(msginfo->folder);
1874 gchar *real_file = g_strconcat(
1875 cache_path, G_DIR_SEPARATOR_S,
1876 itos(msginfo->msgnum), NULL);
1877 gchar *cache_file = NULL;
1879 cache_path = folder_item_get_path(dest);
1880 cache_file = g_strconcat(
1881 cache_path, G_DIR_SEPARATOR_S,
1883 if (!is_dir_exist(cache_path))
1884 make_dir_hier(cache_path);
1885 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1886 copy_file(real_file, cache_file, TRUE);
1887 debug_print("copied to cache: %s\n", cache_file);
1894 g_relation_insert(relation, msginfo,
1895 GINT_TO_POINTER(0));
1896 g_tuples_destroy(tuples);
1898 statusbar_pop_all();
1900 g_relation_destroy(uid_mapping);
1901 imap_lep_set_free(seq_list);
1905 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1906 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1907 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1908 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1910 imap_scan_required(folder, dest);
1911 if (ok == MAILIMAP_NO_ERROR)
1917 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1921 g_return_val_if_fail(msginfo != NULL, -1);
1923 msglist.data = msginfo;
1924 msglist.next = NULL;
1926 return imap_copy_msgs(folder, dest, &msglist, NULL);
1929 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1930 MsgInfoList *msglist, GRelation *relation)
1935 g_return_val_if_fail(folder != NULL, -1);
1936 g_return_val_if_fail(dest != NULL, -1);
1937 g_return_val_if_fail(msglist != NULL, -1);
1939 msginfo = (MsgInfo *)msglist->data;
1940 g_return_val_if_fail(msginfo->folder != NULL, -1);
1942 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1947 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1948 MsgInfoList *msglist, GRelation *relation)
1950 gchar *destdir, *dir;
1951 GSList *numlist = NULL, *cur;
1953 IMAPSession *session;
1954 gint ok = MAILIMAP_NO_ERROR;
1955 GRelation *uid_mapping;
1957 g_return_val_if_fail(folder != NULL, -1);
1958 g_return_val_if_fail(dest != NULL, -1);
1959 g_return_val_if_fail(msglist != NULL, -1);
1961 debug_print("getting session...\n");
1962 session = imap_session_get(folder);
1967 lock_session(session); /* unlocked later in the function */
1969 msginfo = (MsgInfo *)msglist->data;
1971 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1972 NULL, NULL, NULL, NULL, NULL, FALSE);
1973 if (ok != MAILIMAP_NO_ERROR) {
1977 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1982 for (cur = msglist; cur; cur = cur->next) {
1983 msginfo = (MsgInfo *)cur->data;
1984 if (!MSG_IS_DELETED(msginfo->flags))
1985 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1987 numlist = g_slist_reverse(numlist);
1989 uid_mapping = g_relation_new(2);
1990 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1992 if (numlist != NULL) {
1993 ok = imap_set_message_flags
1994 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1995 if (ok != MAILIMAP_NO_ERROR) {
1996 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
2000 } /* else we just need to expunge */
2001 ok = imap_cmd_expunge(session, folder->account->imap_use_trash);
2002 if (ok != MAILIMAP_NO_ERROR) {
2003 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
2008 session->folder_content_changed = TRUE;
2009 unlock_session(session);
2011 dir = folder_item_get_path(msginfo->folder);
2012 if (is_dir_exist(dir)) {
2013 for (cur = msglist; cur; cur = cur->next) {
2014 msginfo = (MsgInfo *)cur->data;
2015 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
2020 g_relation_destroy(uid_mapping);
2021 g_slist_free(numlist);
2023 imap_scan_required(folder, dest);
2026 if (ok == MAILIMAP_NO_ERROR)
2032 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2033 MsgInfoList *msglist, GRelation *relation)
2037 g_return_val_if_fail(folder != NULL, -1);
2038 g_return_val_if_fail(dest != NULL, -1);
2039 if (msglist == NULL)
2042 msginfo = (MsgInfo *)msglist->data;
2043 g_return_val_if_fail(msginfo->folder != NULL, -1);
2045 return imap_do_remove_msgs(folder, dest, msglist, relation);
2048 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2050 GSList *list = folder_item_get_msg_list(item);
2051 gint res = imap_remove_msgs(folder, item, list, NULL);
2052 procmsg_msg_list_free(list);
2056 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2059 /* TODO: properly implement this method */
2063 static gint imap_close(Folder *folder, FolderItem *item)
2068 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2070 FolderItem *item = NULL;
2071 IMAPSession *session;
2072 gchar *root_folder = NULL;
2074 g_return_val_if_fail(folder != NULL, -1);
2075 g_return_val_if_fail(folder->account != NULL, -1);
2077 debug_print("getting session...\n");
2078 session = imap_session_get(folder);
2080 if (!folder->node) {
2081 folder_tree_destroy(folder);
2082 item = folder_item_new(folder, folder->name, NULL);
2083 item->folder = folder;
2084 folder->node = item->node = g_node_new(item);
2089 if (folder->account->imap_dir && *folder->account->imap_dir) {
2091 int r = MAILIMAP_NO_ERROR;
2094 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2095 extract_quote(root_folder, '"');
2096 subst_char(root_folder,
2097 imap_get_path_separator(session, IMAP_FOLDER(folder),
2102 strtailchomp(root_folder, '/');
2103 real_path = imap_get_real_path
2104 (session, IMAP_FOLDER(folder), root_folder, &r);
2107 debug_print("IMAP root directory: %s\n", real_path);
2109 /* check if root directory exist */
2111 r = imap_threaded_list(session->folder, "", real_path,
2114 if (r != MAILIMAP_NO_ERROR)
2115 imap_handle_error(SESSION(session), r);
2117 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2118 if (!folder->node) {
2119 item = folder_item_new(folder, folder->name, NULL);
2120 item->folder = folder;
2121 folder->node = item->node = g_node_new(item);
2125 mailimap_list_result_free(lep_list);
2131 item = FOLDER_ITEM(folder->node->data);
2133 if (item && !item->path && root_folder) {
2134 item->path = g_strdup(root_folder);
2137 if (!item || ((item->path || root_folder) &&
2138 strcmp2(item->path, root_folder) != 0)) {
2139 folder_tree_destroy(folder);
2140 item = folder_item_new(folder, folder->name, root_folder);
2141 item->folder = folder;
2142 folder->node = item->node = g_node_new(item);
2145 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2146 imap_create_missing_folders(folder);
2151 static gint imap_scan_tree(Folder *folder)
2153 gboolean subs_only = FALSE;
2154 if (folder->account) {
2155 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2156 subs_only = folder->account->imap_subsonly;
2158 return imap_scan_tree_real(folder, subs_only);
2161 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2164 IMAPFolder *imapfolder;
2165 FolderItem *new_item;
2166 GSList *item_list, *cur;
2169 gchar *wildcard_path;
2173 int r = MAILIMAP_NO_ERROR;
2175 g_return_val_if_fail(item != NULL, -1);
2176 g_return_val_if_fail(item->folder != NULL, -1);
2177 g_return_val_if_fail(item->no_sub == FALSE, -1);
2179 folder = item->folder;
2180 imapfolder = IMAP_FOLDER(folder);
2182 separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2186 if (folder->ui_func)
2187 folder->ui_func(folder, item, folder->ui_func_data);
2190 wildcard[0] = separator;
2193 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2201 real_path = g_strdup("");
2204 Xstrcat_a(wildcard_path, real_path, wildcard,
2205 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2209 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2211 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2213 if (r != MAILIMAP_NO_ERROR) {
2214 imap_handle_error(SESSION(session), r);
2220 item_list = imap_list_from_lep(imapfolder,
2221 lep_list, real_path, FALSE);
2222 mailimap_list_result_free(lep_list);
2227 node = item->node->children;
2228 while (node != NULL) {
2229 FolderItem *old_item = FOLDER_ITEM(node->data);
2230 GNode *next = node->next;
2233 for (cur = item_list; cur != NULL; cur = cur->next) {
2234 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2235 if (!strcmp2(old_item->path, cur_item->path)) {
2236 new_item = cur_item;
2241 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2242 debug_print("not removing INBOX\n");
2244 debug_print("folder '%s' not found. removing...\n",
2246 folder_item_remove(old_item);
2249 old_item->no_sub = new_item->no_sub;
2250 old_item->no_select = new_item->no_select;
2251 if (old_item->no_sub == TRUE && node->children) {
2252 debug_print("folder '%s' doesn't have "
2253 "subfolders. removing...\n",
2255 folder_item_remove_children(old_item);
2262 for (cur = item_list; cur != NULL; cur = cur->next) {
2263 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2266 for (node = item->node->children; node != NULL;
2267 node = node->next) {
2268 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2270 new_item = FOLDER_ITEM(node->data);
2271 folder_item_destroy(cur_item);
2277 new_item = cur_item;
2278 debug_print("new folder '%s' found.\n", new_item->path);
2279 folder_item_append(item, new_item);
2282 if (!strcmp(new_item->path, "INBOX")) {
2283 new_item->stype = F_INBOX;
2284 folder->inbox = new_item;
2285 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2288 base = g_path_get_basename(new_item->path);
2290 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2291 new_item->stype = F_OUTBOX;
2292 folder->outbox = new_item;
2293 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2294 new_item->stype = F_DRAFT;
2295 folder->draft = new_item;
2296 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2297 new_item->stype = F_QUEUE;
2298 folder->queue = new_item;
2299 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2300 new_item->stype = F_TRASH;
2301 folder->trash = new_item;
2306 if (new_item->no_sub == FALSE)
2307 imap_scan_tree_recursive(session, new_item, subs_only);
2310 g_slist_free(item_list);
2312 return MAILIMAP_NO_ERROR;
2315 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2317 IMAPSession *session = imap_session_get(folder);
2319 gchar *wildcard_path;
2323 GSList *item_list = NULL, *cur;
2324 GList *child_list = NULL, *tmplist = NULL;
2325 GSList *sub_list = NULL;
2326 int r = MAILIMAP_NO_ERROR;
2331 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2336 wildcard[0] = separator;
2339 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2347 real_path = g_strdup("");
2350 Xstrcat_a(wildcard_path, real_path, wildcard,
2351 {g_free(real_path); return NULL;});
2355 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2356 item->path?item->path:item->name);
2358 statusbar_print_all(_("Looking for subfolders of %s..."),
2359 item->path?item->path:item->name);
2361 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2364 statusbar_pop_all();
2367 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2368 lep_list, real_path, FALSE);
2369 mailimap_list_result_free(lep_list);
2371 for (cur = item_list; cur != NULL; cur = cur->next) {
2372 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2374 tmplist = imap_scan_subtree(folder, cur_item,
2375 unsubs_only, recursive);
2377 child_list = g_list_concat(child_list, tmplist);
2379 child_list = g_list_prepend(child_list,
2380 imap_get_real_path(session,
2381 IMAP_FOLDER(folder), cur_item->path, &r));
2384 statusbar_pop_all();
2387 folder_item_destroy(cur_item);
2389 child_list = g_list_reverse(child_list);
2390 g_slist_free(item_list);
2393 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2396 statusbar_pop_all();
2399 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2400 lep_list, real_path, FALSE);
2401 mailimap_list_result_free(lep_list);
2403 for (cur = sub_list; cur != NULL; cur = cur->next) {
2404 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2405 GList *oldlitem = NULL;
2406 gchar *tmp = imap_get_real_path(session,
2407 IMAP_FOLDER(folder), cur_item->path, &r);
2410 statusbar_pop_all();
2413 folder_item_destroy(cur_item);
2414 oldlitem = g_list_find_custom(
2415 child_list, tmp, (GCompareFunc)strcmp2);
2417 child_list = g_list_remove_link(child_list, oldlitem);
2418 g_free(oldlitem->data);
2419 g_list_free(oldlitem);
2426 statusbar_pop_all();
2431 static gint imap_create_tree(Folder *folder)
2433 g_return_val_if_fail(folder != NULL, -1);
2434 g_return_val_if_fail(folder->node != NULL, -1);
2435 g_return_val_if_fail(folder->node->data != NULL, -1);
2436 g_return_val_if_fail(folder->account != NULL, -1);
2438 imap_scan_tree(folder);
2439 imap_create_missing_folders(folder);
2444 static void imap_create_missing_folders(Folder *folder)
2446 g_return_if_fail(folder != NULL);
2449 folder->inbox = imap_create_special_folder
2450 (folder, F_INBOX, "INBOX");
2452 folder->trash = imap_create_special_folder
2453 (folder, F_TRASH, "Trash");
2455 folder->queue = imap_create_special_folder
2456 (folder, F_QUEUE, "Queue");
2457 if (!folder->outbox)
2458 folder->outbox = imap_create_special_folder
2459 (folder, F_OUTBOX, "Sent");
2461 folder->draft = imap_create_special_folder
2462 (folder, F_DRAFT, "Drafts");
2465 static FolderItem *imap_create_special_folder(Folder *folder,
2466 SpecialFolderItemType stype,
2470 FolderItem *new_item;
2472 g_return_val_if_fail(folder != NULL, NULL);
2473 g_return_val_if_fail(folder->node != NULL, NULL);
2474 g_return_val_if_fail(folder->node->data != NULL, NULL);
2475 g_return_val_if_fail(folder->account != NULL, NULL);
2476 g_return_val_if_fail(name != NULL, NULL);
2478 item = FOLDER_ITEM(folder->node->data);
2479 new_item = imap_create_folder(folder, item, name);
2482 g_warning("Can't create '%s'\n", name);
2483 if (!folder->inbox) return NULL;
2485 new_item = imap_create_folder(folder, folder->inbox, name);
2487 g_warning("Can't create '%s' under INBOX\n", name);
2489 new_item->stype = stype;
2491 new_item->stype = stype;
2496 static gchar *imap_folder_get_path(Folder *folder)
2500 g_return_val_if_fail(folder != NULL, NULL);
2501 g_return_val_if_fail(folder->account != NULL, NULL);
2503 folder_path = g_strconcat(get_imap_cache_dir(),
2505 folder->account->recv_server,
2507 folder->account->userid,
2514 static gchar *imap_encode_unsafe_chars(const gchar *str)
2516 gchar *ret = NULL, *o_ret;
2520 ret = g_malloc(3*strlen(str)+1);
2522 for (i = str; *i; i++) {
2532 *ret++ = '0'+(*i/10);
2533 *ret++ = '0'+(*i%10);
2543 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2545 gchar *folder_path, *path;
2546 gchar *item_path = NULL;
2548 g_return_val_if_fail(folder != NULL, NULL);
2549 g_return_val_if_fail(item != NULL, NULL);
2550 folder_path = imap_folder_get_path(folder);
2552 g_return_val_if_fail(folder_path != NULL, NULL);
2555 item_path = g_strdup(item->path);
2557 item_path = imap_encode_unsafe_chars(item->path);
2560 if (g_path_is_absolute(folder_path)) {
2562 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2565 path = g_strdup(folder_path);
2568 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2569 folder_path, G_DIR_SEPARATOR_S,
2572 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2575 g_free(folder_path);
2578 while (strchr(path, '/'))
2579 *strchr(path, '/') = '\\';
2585 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2588 gchar *dirpath, *imap_path;
2589 IMAPSession *session;
2590 FolderItem *new_item;
2594 gint ok = MAILIMAP_NO_ERROR;
2595 gboolean no_select = FALSE, no_sub = FALSE;
2596 gboolean exist = FALSE;
2598 g_return_val_if_fail(folder != NULL, NULL);
2599 g_return_val_if_fail(folder->account != NULL, NULL);
2600 g_return_val_if_fail(parent != NULL, NULL);
2601 g_return_val_if_fail(name != NULL, NULL);
2603 debug_print("getting session...\n");
2604 session = imap_session_get(folder);
2609 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2610 dirpath = g_strdup(name);
2611 }else if (parent->path)
2612 dirpath = g_strconcat(parent->path, "/", name, NULL);
2613 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2614 dirpath = g_strdup(name);
2615 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2618 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2619 strtailchomp(imap_dir, '/');
2620 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2622 dirpath = g_strdup(name);
2626 /* keep trailing directory separator to create a folder that contains
2628 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2630 strtailchomp(dirpath, '/');
2631 Xstrdup_a(new_name, name, {
2636 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2641 imap_path_separator_subst(imap_path, separator);
2642 /* remove trailing / for display */
2643 strtailchomp(new_name, '/');
2645 if (strcmp(dirpath, "INBOX") != 0) {
2650 argbuf = g_ptr_array_new();
2651 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2652 if (r != MAILIMAP_NO_ERROR) {
2653 imap_handle_error(SESSION(session), r);
2654 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2657 ptr_array_free_strings(argbuf);
2658 g_ptr_array_free(argbuf, TRUE);
2662 if (clist_count(lep_list) > 0)
2664 mailimap_list_result_free(lep_list);
2667 ok = imap_cmd_create(session, imap_path);
2668 if (ok != MAILIMAP_NO_ERROR) {
2669 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2674 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2675 if (r == MAILIMAP_NO_ERROR) {
2676 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2677 lep_list, dirpath, TRUE);
2679 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2680 no_select = cur_item->no_select;
2681 no_sub = cur_item->no_sub;
2682 g_slist_free(item_list);
2684 mailimap_list_result_free(lep_list);
2686 imap_handle_error(SESSION(session), r);
2689 imap_threaded_subscribe(folder, imap_path, TRUE);
2693 /* just get flags */
2694 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2695 if (r == MAILIMAP_NO_ERROR) {
2696 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2697 lep_list, dirpath, TRUE);
2699 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2700 no_select = cur_item->no_select;
2701 no_sub = cur_item->no_sub;
2702 g_slist_free(item_list);
2704 mailimap_list_result_free(lep_list);
2706 imap_handle_error(SESSION(session), r);
2710 new_item = folder_item_new(folder, new_name, dirpath);
2711 new_item->no_select = no_select;
2712 new_item->no_sub = no_sub;
2713 folder_item_append(parent, new_item);
2717 dirpath = folder_item_get_path(new_item);
2718 if (!is_dir_exist(dirpath))
2719 make_dir_hier(dirpath);
2723 /* folder existed, scan it */
2724 imap_scan_required(folder, new_item);
2725 folder_item_scan_full(new_item, FALSE);
2731 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2736 gchar *real_oldpath;
2737 gchar *real_newpath;
2739 gchar *old_cache_dir;
2740 gchar *new_cache_dir;
2741 IMAPSession *session;
2743 gint ok = MAILIMAP_NO_ERROR;
2744 gint exists, recent, unseen;
2745 guint32 uid_validity;
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);
2750 g_return_val_if_fail(name != NULL, -1);
2752 debug_print("getting session...\n");
2753 session = imap_session_get(folder);
2758 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
2760 g_warning(_("New folder name must not contain the namespace "
2765 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2770 g_free(session->mbox);
2771 session->mbox = NULL;
2772 session->exists = 0;
2773 session->recent = 0;
2774 session->expunge = 0;
2775 ok = imap_cmd_examine(session, "INBOX",
2776 &exists, &recent, &unseen, &uid_validity, FALSE);
2777 if (ok != MAILIMAP_NO_ERROR) {
2778 g_free(real_oldpath);
2782 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
2785 if (strchr(item->path, G_DIR_SEPARATOR)) {
2786 dirpath = g_path_get_dirname(item->path);
2787 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2790 newpath = g_strdup(name);
2792 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2793 imap_path_separator_subst(real_newpath, separator);
2795 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2796 if (ok != MAILIMAP_NO_ERROR) {
2797 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2798 real_oldpath, real_newpath);
2799 g_free(real_oldpath);
2801 g_free(real_newpath);
2805 item->name = g_strdup(name);
2807 old_cache_dir = folder_item_get_path(item);
2809 paths[0] = g_strdup(item->path);
2811 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2812 imap_rename_folder_func, paths);
2814 if (is_dir_exist(old_cache_dir)) {
2815 new_cache_dir = folder_item_get_path(item);
2816 if (g_rename(old_cache_dir, new_cache_dir) < 0) {
2817 FILE_OP_ERROR(old_cache_dir, "rename");
2819 g_free(new_cache_dir);
2822 g_free(old_cache_dir);
2825 g_free(real_oldpath);
2826 g_free(real_newpath);
2830 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2833 gint r = MAILIMAP_NO_ERROR;
2834 IMAPSession *session;
2835 debug_print("getting session...\n");
2837 session = imap_session_get(folder);
2841 if (item && item->path) {
2842 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2849 if (!strcmp(path, "INBOX") && sub == FALSE) {
2853 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2854 r = imap_threaded_subscribe(folder, path, sub);
2857 r = imap_threaded_subscribe(folder, rpath, sub);
2863 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2865 gint ok = MAILIMAP_NO_ERROR;
2866 IMAPSession *session;
2869 gboolean selected_folder;
2871 g_return_val_if_fail(folder != NULL, -1);
2872 g_return_val_if_fail(item != NULL, -1);
2873 g_return_val_if_fail(item->path != NULL, -1);
2875 debug_print("getting session...\n");
2876 session = imap_session_get(folder);
2880 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2884 imap_threaded_subscribe(folder, path, FALSE);
2886 selected_folder = (session->mbox != NULL) &&
2887 (!strcmp(session->mbox, item->path));
2888 if (selected_folder) {
2889 ok = imap_cmd_close(session);
2890 if (ok != MAILIMAP_NO_ERROR) {
2891 debug_print("close err %d\n", ok);
2895 ok = imap_cmd_delete(session, path);
2896 if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
2899 ok = MAILIMAP_NO_ERROR;
2900 tmp = g_strdup_printf("%s%c", path,
2901 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
2905 ok = imap_cmd_delete(session, path);
2908 if (ok != MAILIMAP_NO_ERROR) {
2909 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2915 cache_dir = folder_item_get_path(item);
2916 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2917 g_warning("can't remove directory '%s'\n", cache_dir);
2919 folder_item_remove(item);
2923 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2927 g_return_val_if_fail(item != NULL, -1);
2928 g_return_val_if_fail(item->folder != NULL, -1);
2929 g_return_val_if_fail(item->node != NULL, -1);
2931 node = item->node->children;
2932 while (node != NULL) {
2934 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2938 debug_print("IMAP removing %s\n", item->path);
2940 if (imap_remove_all_msg(folder, item) < 0)
2942 return imap_remove_folder_real(folder, item);
2945 typedef struct _uncached_data {
2946 IMAPSession *session;
2948 MsgNumberList *numlist;
2955 static void *imap_get_uncached_messages_thread(void *data)
2957 uncached_data *stuff = (uncached_data *)data;
2958 IMAPSession *session = stuff->session;
2959 FolderItem *item = stuff->item;
2960 MsgNumberList *numlist = stuff->numlist;
2961 GSList *newlist = NULL;
2962 GSList *llast = NULL;
2963 GSList *seq_list, *cur;
2964 gboolean got_alien_tags = FALSE;
2966 debug_print("uncached_messages\n");
2968 if (session == NULL || item == NULL || item->folder == NULL
2969 || FOLDER_CLASS(item->folder) != &imap_class) {
2974 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2975 debug_print("get msgs info\n");
2976 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2977 struct mailimap_set * imapset;
2983 if (session->cancelled)
2986 imapset = cur->data;
2988 r = imap_threaded_fetch_env(session->folder,
2989 imapset, &env_list);
2990 if (r != MAILIMAP_NO_ERROR) {
2991 imap_handle_error(SESSION(session), r);
2999 session_set_access_time(SESSION(session));
3002 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
3003 struct imap_fetch_env_info * info;
3005 GSList *tags = NULL, *cur = NULL;
3006 info = carray_get(env_list, i);
3007 tags = carray_get(env_list, i+1);
3008 msginfo = imap_envelope_from_lep(info, item);
3009 if (msginfo == NULL) {
3010 slist_free_strings(tags);
3014 g_slist_free(msginfo->tags);
3015 msginfo->tags = NULL;
3017 for (cur = tags; cur; cur = cur->next) {
3018 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
3020 id = tags_get_id_for_str(real_tag);
3022 id = tags_add_tag(real_tag);
3023 got_alien_tags = TRUE;
3025 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
3026 msginfo->tags = g_slist_prepend(
3028 GINT_TO_POINTER(id));
3033 msginfo->tags = g_slist_reverse(msginfo->tags);
3034 slist_free_strings(tags);
3036 msginfo->folder = item;
3038 llast = newlist = g_slist_append(newlist, msginfo);
3040 llast = g_slist_append(llast, msginfo);
3041 llast = llast->next;
3046 imap_fetch_env_free(env_list);
3049 if (got_alien_tags) {
3051 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
3054 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
3055 struct mailimap_set * imapset;
3057 imapset = cur->data;
3058 mailimap_set_free(imapset);
3061 session_set_access_time(SESSION(session));
3066 #define MAX_MSG_NUM 50
3068 static GSList *imap_get_uncached_messages(IMAPSession *session,
3070 MsgNumberList *numlist,
3073 GSList *result = NULL;
3075 uncached_data *data = g_new0(uncached_data, 1);
3080 data->total = g_slist_length(numlist);
3081 data->ok = MAILIMAP_NO_ERROR;
3082 debug_print("messages list : %i\n", data->total);
3084 while (cur != NULL) {
3085 GSList * partial_result;
3093 while (count < MAX_MSG_NUM) {
3098 if (newlist == NULL)
3099 llast = newlist = g_slist_append(newlist, p);
3101 llast = g_slist_append(llast, p);
3102 llast = llast->next;
3112 data->session = session;
3114 data->numlist = newlist;
3117 if (prefs_common.work_offline &&
3118 !inc_offline_should_override(FALSE,
3119 _("Claws Mail needs network access in order "
3120 "to access the IMAP server."))) {
3126 (GSList *)imap_get_uncached_messages_thread(data);
3128 if (data->ok != MAILIMAP_NO_ERROR) {
3131 statusbar_progress_all(data->cur,data->total, 1);
3133 g_slist_free(newlist);
3135 result = g_slist_concat(result, partial_result);
3140 statusbar_progress_all(0,0,0);
3141 statusbar_pop_all();
3146 static void imap_delete_all_cached_messages(FolderItem *item)
3150 g_return_if_fail(item != NULL);
3151 g_return_if_fail(item->folder != NULL);
3152 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3154 debug_print("Deleting all cached messages...\n");
3156 dir = folder_item_get_path(item);
3157 if (is_dir_exist(dir))
3158 remove_all_numbered_files(dir);
3161 debug_print("done.\n");
3164 gchar imap_get_path_separator_for_item(FolderItem *item)
3166 Folder *folder = NULL;
3167 IMAPFolder *imap_folder = NULL;
3168 IMAPSession *session = NULL;
3170 gint ok = MAILIMAP_NO_ERROR;
3173 folder = item->folder;
3178 imap_folder = IMAP_FOLDER(folder);
3183 debug_print("getting session...");
3184 session = imap_session_get(FOLDER(folder));
3185 result = imap_get_path_separator(session, imap_folder, item->path, &ok);
3189 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder, gint *ok)
3193 gchar separator = '\0';
3195 g_return_val_if_fail(session != NULL, '/');
3196 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3198 if (r != MAILIMAP_NO_ERROR) {
3199 imap_handle_error(SESSION(session), r);
3200 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3205 if (lep_list != NULL && clist_count(lep_list) > 0) {
3206 clistiter * iter = clist_begin(lep_list);
3207 struct mailimap_mailbox_list * mb;
3208 mb = clist_content(iter);
3210 separator = mb->mb_delimiter;
3211 debug_print("got separator: %c\n", folder->last_seen_separator);
3213 *ok = MAILIMAP_NO_ERROR;
3214 mailimap_list_result_free(lep_list);
3218 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3220 gchar separator = '/';
3221 *ok = MAILIMAP_NO_ERROR;
3222 if (folder->last_seen_separator == 0) {
3223 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "", ok);
3226 if (folder->last_seen_separator == 0) {
3227 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX", ok);
3230 if (folder->last_seen_separator != 0) {
3231 debug_print("using separator: %c\n", folder->last_seen_separator);
3232 return folder->last_seen_separator;
3238 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3240 gchar *real_path = NULL;
3243 g_return_val_if_fail(folder != NULL, NULL);
3244 g_return_val_if_fail(path != NULL, NULL);
3246 *ok = MAILIMAP_NO_ERROR;
3248 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3249 separator = imap_get_path_separator(session, folder, path, ok);
3250 if (*ok == MAILIMAP_NO_ERROR)
3251 imap_path_separator_subst(real_path, separator);
3256 static gint imap_set_message_flags(IMAPSession *session,
3257 IMAPFolderItem *item,
3258 MsgNumberList *numlist,
3267 IMAPFolder *folder = NULL;
3268 GSList *sorted_list = NULL;
3270 if (numlist == NULL || session == NULL)
3271 return MAILIMAP_ERROR_BAD_STATE;
3273 folder = IMAP_FOLDER(session->folder);
3275 sorted_list = g_slist_copy(numlist);
3276 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3278 cur = g_slist_last(sorted_list);
3281 total = GPOINTER_TO_INT(cur->data);
3283 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3285 statusbar_print_all(_("Flagging messages..."));
3287 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3288 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3289 struct mailimap_set_item *set_item = NULL;
3291 if (imapset->set_list)
3292 set_item = clist_content(clist_begin(imapset->set_list));
3296 if (set_item == NULL)
3299 statusbar_progress_all(set_item->set_first, total, 1);
3301 ok = imap_cmd_store(session, item, imapset,
3302 flags, tags, is_set);
3303 statusbar_progress_all(set_item->set_last, total, 1);
3304 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3305 /* reduce max set size */
3306 folder->max_set_size /= 2;
3308 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3313 g_slist_free(sorted_list);
3315 statusbar_progress_all(0,0,0);
3316 statusbar_pop_all();
3318 imap_lep_set_free(seq_list);
3323 typedef struct _select_data {
3324 IMAPSession *session;
3329 guint32 *uid_validity;