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 struct mailimap_set ** source,
379 struct mailimap_set ** dest);
380 static gint imap_cmd_store (IMAPSession *session,
381 IMAPFolderItem *item,
382 struct mailimap_set * set,
386 static gint imap_cmd_expunge (IMAPSession *session, gboolean force);
388 static void imap_path_separator_subst (gchar *str,
391 static gboolean imap_rename_folder_func (GNode *node,
393 static gint imap_get_num_list (Folder *folder,
396 gboolean *old_uids_valid);
397 static GSList *imap_get_msginfos (Folder *folder,
399 GSList *msgnum_list);
400 static MsgInfo *imap_get_msginfo (Folder *folder,
403 static gboolean imap_scan_required (Folder *folder,
405 static void imap_change_flags (Folder *folder,
408 MsgPermFlags newflags);
409 static gint imap_get_flags (Folder *folder,
411 MsgInfoList *msglist,
412 GRelation *msgflags);
413 static gchar *imap_folder_get_path (Folder *folder);
414 static gchar *imap_item_get_path (Folder *folder,
416 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
419 /* data types conversion libetpan <-> claws */
420 static GSList * imap_list_from_lep(IMAPFolder * folder,
421 clist * list, const gchar * real_path, gboolean all);
422 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
423 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
424 static GSList * imap_uid_list_from_lep(clist * list);
425 static GSList * imap_uid_list_from_lep_tab(carray * list);
426 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
428 GHashTable *tags_hash);
429 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
431 static void imap_lep_set_free(GSList *seq_list);
432 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
434 typedef struct _hashtable_data {
436 IMAPFolderItem *item;
439 static FolderClass imap_class;
441 FolderClass *imap_get_class(void)
443 if (imap_class.idstr == NULL) {
444 imap_class.type = F_IMAP;
445 imap_class.idstr = "imap";
446 imap_class.uistr = "IMAP4";
448 /* Folder functions */
449 imap_class.new_folder = imap_folder_new;
450 imap_class.destroy_folder = imap_folder_destroy;
451 imap_class.scan_tree = imap_scan_tree;
452 imap_class.create_tree = imap_create_tree;
454 /* FolderItem functions */
455 imap_class.item_new = imap_folder_item_new;
456 imap_class.item_destroy = imap_folder_item_destroy;
457 imap_class.item_get_path = imap_item_get_path;
458 imap_class.create_folder = imap_create_folder;
459 imap_class.rename_folder = imap_rename_folder;
460 imap_class.remove_folder = imap_remove_folder;
461 imap_class.close = imap_close;
462 imap_class.get_num_list = imap_get_num_list;
463 imap_class.scan_required = imap_scan_required;
464 imap_class.set_xml = folder_set_xml;
465 imap_class.get_xml = folder_get_xml;
466 imap_class.item_set_xml = imap_item_set_xml;
467 imap_class.item_get_xml = imap_item_get_xml;
469 /* Message functions */
470 imap_class.get_msginfo = imap_get_msginfo;
471 imap_class.get_msginfos = imap_get_msginfos;
472 imap_class.fetch_msg = imap_fetch_msg;
473 imap_class.fetch_msg_full = imap_fetch_msg_full;
474 imap_class.add_msg = imap_add_msg;
475 imap_class.add_msgs = imap_add_msgs;
476 imap_class.copy_msg = imap_copy_msg;
477 imap_class.copy_msgs = imap_copy_msgs;
478 imap_class.remove_msg = imap_remove_msg;
479 imap_class.remove_msgs = imap_remove_msgs;
480 imap_class.expunge = imap_expunge;
481 imap_class.remove_all_msg = imap_remove_all_msg;
482 imap_class.is_msg_changed = imap_is_msg_changed;
483 imap_class.change_flags = imap_change_flags;
484 imap_class.get_flags = imap_get_flags;
485 imap_class.set_batch = imap_set_batch;
486 imap_class.synchronise = imap_synchronise;
487 imap_class.remove_cached_msg = imap_remove_cached_msg;
488 imap_class.commit_tags = imap_commit_tags;
490 pthread_mutex_init(&imap_mutex, NULL);
497 static void imap_refresh_sensitivity (IMAPSession *session)
501 if (session->sens_update_block)
503 mainwin = mainwindow_get_mainwindow();
505 toolbar_main_set_sensitive(mainwin);
506 main_window_set_menu_sensitive(mainwin);
510 static void lock_session(IMAPSession *session)
513 debug_print("locking session %p (%d)\n", session, session->busy);
515 debug_print(" SESSION WAS LOCKED !! \n");
516 session->busy = TRUE;
517 imap_refresh_sensitivity(session);
519 debug_print("can't lock null session\n");
523 static void unlock_session(IMAPSession *session)
526 debug_print("unlocking session %p\n", session);
527 session->busy = FALSE;
528 imap_refresh_sensitivity(session);
530 debug_print("can't unlock null session\n");
534 static void imap_disc_session_destroy(IMAPSession *session)
536 RemoteFolder *rfolder = NULL;
541 rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
545 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
546 SESSION(session)->state = SESSION_DISCONNECTED;
547 SESSION(session)->sock = NULL;
550 static gboolean is_fatal(int libetpan_errcode)
552 switch(libetpan_errcode) {
553 case MAILIMAP_ERROR_STREAM:
554 case MAILIMAP_ERROR_PROTOCOL:
555 case MAILIMAP_ERROR_PARSE:
556 case MAILIMAP_ERROR_BAD_STATE:
563 static void imap_handle_error(Session *session, const gchar *server, int libetpan_errcode)
565 const gchar *session_server = (session ? session->server : NULL);
567 if (session_server == NULL)
568 session_server = server;
569 if (session_server == NULL)
570 session_server = "(null)";
572 switch(libetpan_errcode) {
573 case MAILIMAP_NO_ERROR:
575 case MAILIMAP_NO_ERROR_AUTHENTICATED:
576 log_warning(LOG_PROTOCOL, _("IMAP error on %s: authenticated\n"), session_server);
578 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
579 log_warning(LOG_PROTOCOL, _("IMAP error on %s: not authenticated\n"), session_server);
581 case MAILIMAP_ERROR_BAD_STATE:
582 log_warning(LOG_PROTOCOL, _("IMAP error on %s: bad state\n"), session_server);
584 case MAILIMAP_ERROR_STREAM:
585 log_warning(LOG_PROTOCOL, _("IMAP error on %s: stream error\n"), session_server);
587 case MAILIMAP_ERROR_PARSE:
588 log_warning(LOG_PROTOCOL, _("IMAP error on %s: parse error "
589 "(very probably non-RFC compliance from the server)\n"), session_server);
591 case MAILIMAP_ERROR_CONNECTION_REFUSED:
592 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection refused\n"), session_server);
594 case MAILIMAP_ERROR_MEMORY:
595 log_warning(LOG_PROTOCOL, _("IMAP error on %s: memory error\n"), session_server);
597 case MAILIMAP_ERROR_FATAL:
598 log_warning(LOG_PROTOCOL, _("IMAP error on %s: fatal error\n"), session_server);
600 case MAILIMAP_ERROR_PROTOCOL:
601 log_warning(LOG_PROTOCOL, _("IMAP error on %s: protocol error"
602 "(very probably non-RFC compliance from the server)\n"), session_server);
604 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
605 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection not accepted\n"), session_server);
607 case MAILIMAP_ERROR_APPEND:
608 log_warning(LOG_PROTOCOL, _("IMAP error on %s: APPEND error\n"), session_server);
610 case MAILIMAP_ERROR_NOOP:
611 log_warning(LOG_PROTOCOL, _("IMAP error on %s: NOOP error\n"), session_server);
613 case MAILIMAP_ERROR_LOGOUT:
614 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGOUT error\n"), session_server);
616 case MAILIMAP_ERROR_CAPABILITY:
617 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CAPABILITY error\n"), session_server);
619 case MAILIMAP_ERROR_CHECK:
620 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CHECK error\n"), session_server);
622 case MAILIMAP_ERROR_CLOSE:
623 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CLOSE error\n"), session_server);
625 case MAILIMAP_ERROR_EXPUNGE:
626 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXPUNGE error\n"), session_server);
628 case MAILIMAP_ERROR_COPY:
629 log_warning(LOG_PROTOCOL, _("IMAP error on %s: COPY error\n"), session_server);
631 case MAILIMAP_ERROR_UID_COPY:
632 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID COPY error\n"), session_server);
634 case MAILIMAP_ERROR_CREATE:
635 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CREATE error\n"), session_server);
637 case MAILIMAP_ERROR_DELETE:
638 log_warning(LOG_PROTOCOL, _("IMAP error on %s: DELETE error\n"), session_server);
640 case MAILIMAP_ERROR_EXAMINE:
641 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXAMINE error\n"), session_server);
643 case MAILIMAP_ERROR_FETCH:
644 log_warning(LOG_PROTOCOL, _("IMAP error on %s: FETCH error\n"), session_server);
646 case MAILIMAP_ERROR_UID_FETCH:
647 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID FETCH error\n"), session_server);
649 case MAILIMAP_ERROR_LIST:
650 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LIST error\n"), session_server);
652 case MAILIMAP_ERROR_LOGIN:
653 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGIN error\n"), session_server);
655 case MAILIMAP_ERROR_LSUB:
656 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LSUB error\n"), session_server);
658 case MAILIMAP_ERROR_RENAME:
659 log_warning(LOG_PROTOCOL, _("IMAP error on %s: RENAME error\n"), session_server);
661 case MAILIMAP_ERROR_SEARCH:
662 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SEARCH error\n"), session_server);
664 case MAILIMAP_ERROR_UID_SEARCH:
665 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID SEARCH error\n"), session_server);
667 case MAILIMAP_ERROR_SELECT:
668 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SELECT error\n"), session_server);
670 case MAILIMAP_ERROR_STATUS:
671 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STATUS error\n"), session_server);
673 case MAILIMAP_ERROR_STORE:
674 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STORE error\n"), session_server);
676 case MAILIMAP_ERROR_UID_STORE:
677 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID STORE error\n"), session_server);
679 case MAILIMAP_ERROR_SUBSCRIBE:
680 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SUBSCRIBE error\n"), session_server);
682 case MAILIMAP_ERROR_UNSUBSCRIBE:
683 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UNSUBSCRIBE error\n"), session_server);
685 case MAILIMAP_ERROR_STARTTLS:
686 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STARTTLS error\n"), session_server);
688 case MAILIMAP_ERROR_INVAL:
689 log_warning(LOG_PROTOCOL, _("IMAP error on %s: INVAL error\n"), session_server);
691 case MAILIMAP_ERROR_EXTENSION:
692 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXTENSION error\n"), session_server);
694 case MAILIMAP_ERROR_SASL:
695 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SASL error\n"), session_server);
698 case MAILIMAP_ERROR_SSL:
699 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SSL error\n"), session_server);
703 log_warning(LOG_PROTOCOL, _("IMAP error on %s: Unknown error [%d]\n"),
704 session_server, libetpan_errcode);
708 if (session && is_fatal(libetpan_errcode)) {
709 imap_disc_session_destroy(IMAP_SESSION(session));
710 } else if (session && !is_fatal(libetpan_errcode)) {
711 if (IMAP_SESSION(session)->busy)
712 unlock_session(IMAP_SESSION(session));
716 static Folder *imap_folder_new(const gchar *name, const gchar *path)
720 folder = (Folder *)g_new0(IMAPFolder, 1);
721 folder->klass = &imap_class;
722 imap_folder_init(folder, name, path);
727 static void imap_folder_destroy(Folder *folder)
729 while (imap_folder_get_refcnt(folder) > 0)
730 gtk_main_iteration();
732 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
736 static void imap_folder_init(Folder *folder, const gchar *name,
739 folder_remote_folder_init((Folder *)folder, name, path);
740 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
743 static FolderItem *imap_folder_item_new(Folder *folder)
745 IMAPFolderItem *item;
747 item = g_new0(IMAPFolderItem, 1);
750 item->uid_list = NULL;
752 return (FolderItem *)item;
755 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
757 IMAPFolderItem *item = (IMAPFolderItem *)_item;
759 g_return_if_fail(item != NULL);
760 g_slist_free(item->uid_list);
765 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
767 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
770 g_slist_free(item->uid_list);
771 item->uid_list = NULL;
776 static void imap_reset_uid_lists(Folder *folder)
778 if(folder->node == NULL)
781 /* Destroy all uid lists and rest last uid */
782 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
785 static int imap_get_capabilities(IMAPSession *session)
787 struct mailimap_capability_data *capabilities = NULL;
791 if (session->capability != NULL)
792 return MAILIMAP_NO_ERROR;
794 capabilities = imap_threaded_capability(session->folder, &result);
796 if (result != MAILIMAP_NO_ERROR) {
800 if (capabilities == NULL || capabilities->cap_list == NULL) {
801 return MAILIMAP_NO_ERROR;
804 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
805 cur = clist_next(cur)) {
806 struct mailimap_capability * cap =
808 if (!cap || cap->cap_data.cap_name == NULL)
810 session->capability = g_slist_append
811 (session->capability,
812 g_strdup(cap->cap_data.cap_name));
813 debug_print("got capa %s\n", cap->cap_data.cap_name);
815 mailimap_capability_data_free(capabilities);
816 return MAILIMAP_NO_ERROR;
819 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
822 for (cur = session->capability; cur; cur = cur->next) {
823 if (!g_ascii_strcasecmp(cur->data, cap))
829 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
832 gint ok = MAILIMAP_ERROR_LOGIN;
833 static time_t last_login_err = 0;
834 gchar *ext_info = "";
836 gchar *server = NULL;
837 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
838 imap_handle_error(SESSION(session), NULL, r);
841 server = g_strdup(SESSION(session)->server);
844 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
846 case IMAP_AUTH_CRAM_MD5:
847 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
849 case IMAP_AUTH_DIGEST_MD5:
850 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
852 case IMAP_AUTH_LOGIN:
853 ok = imap_cmd_login(session, user, pass, "LOGIN");
855 case IMAP_AUTH_GSSAPI:
856 ok = imap_cmd_login(session, user, pass, "GSSAPI");
859 debug_print("capabilities:\n"
865 imap_has_capability(session, "ANONYMOUS"),
866 imap_has_capability(session, "CRAM-MD5"),
867 imap_has_capability(session, "DIGEST-MD5"),
868 imap_has_capability(session, "LOGIN"),
869 imap_has_capability(session, "GSSAPI"));
870 if (imap_has_capability(session, "CRAM-MD5"))
871 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
872 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
873 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
874 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
875 ok = imap_cmd_login(session, user, pass, "GSSAPI");
876 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
877 ok = imap_cmd_login(session, user, pass, "LOGIN");
880 if (ok == MAILIMAP_NO_ERROR)
881 session->authenticated = TRUE;
883 if (type == IMAP_AUTH_CRAM_MD5) {
884 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
885 "compiled with SASL support and the "
886 "CRAM-MD5 SASL plugin is installed.");
889 if (type == IMAP_AUTH_DIGEST_MD5) {
890 ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
891 "compiled with SASL support and the "
892 "DIGEST-MD5 SASL plugin is installed.");
895 if (time(NULL) - last_login_err > 10) {
896 if (!prefs_common.no_recv_err_panel) {
897 alertpanel_error_log(_("Connection to %s failed: "
901 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
902 "login refused.%s\n"),
906 last_login_err = time(NULL);
912 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
914 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
915 /* Check if this is the first try to establish a
916 connection, if yes we don't try to reconnect */
917 debug_print("reconnecting\n");
918 if (rfolder->session == NULL) {
919 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
920 folder->account->recv_server);
921 SESSION(session)->sock = NULL;
922 session_destroy(SESSION(session));
925 rfolder->session = NULL;
926 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
927 " disconnected. Reconnecting...\n"),
928 folder->account->recv_server);
929 statusbar_print_all(_("IMAP4 connection to %s has been"
930 " disconnected. Reconnecting...\n"),
931 folder->account->recv_server);
932 SESSION(session)->state = SESSION_DISCONNECTED;
933 SESSION(session)->sock = NULL;
934 session_destroy(SESSION(session));
935 /* Clear folders session to make imap_session_get create
936 a new session, because of rfolder->session == NULL
937 it will not try to reconnect again and so avoid an
939 debug_print("getting session...\n");
940 session = imap_session_get(folder);
941 rfolder->session = SESSION(session);
947 static IMAPSession *imap_session_get(Folder *folder)
949 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
950 IMAPSession *session = NULL;
951 gint r = MAILIMAP_NO_ERROR;
953 g_return_val_if_fail(folder != NULL, NULL);
954 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
955 g_return_val_if_fail(folder->account != NULL, NULL);
957 if (prefs_common.work_offline &&
958 !inc_offline_should_override(FALSE,
959 _("Claws Mail needs network access in order "
960 "to access the IMAP server."))) {
964 /* Make sure we have a session */
965 if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
966 session = IMAP_SESSION(rfolder->session);
967 } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
968 session_destroy(SESSION(rfolder->session));
969 rfolder->session = NULL;
971 } else if (rfolder->connecting) {
972 debug_print("already connecting\n");
976 imap_reset_uid_lists(folder);
977 if (time(NULL) - rfolder->last_failure <= 2)
979 rfolder->connecting = TRUE;
980 session = imap_session_new(folder, folder->account);
982 if(session == NULL) {
983 rfolder->last_failure = time(NULL);
984 rfolder->connecting = FALSE;
988 /* Make sure session is authenticated */
989 if (!IMAP_SESSION(session)->authenticated)
990 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
992 if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
993 rfolder->session = NULL;
995 imap_threaded_disconnect(session->folder);
996 SESSION(session)->state = SESSION_DISCONNECTED;
997 SESSION(session)->sock = NULL;
998 session_destroy(SESSION(session));
1000 rfolder->last_failure = time(NULL);
1001 rfolder->connecting = FALSE;
1005 /* I think the point of this code is to avoid sending a
1006 * keepalive if we've used the session recently and therefore
1007 * think it's still alive. Unfortunately, most of the code
1008 * does not yet check for errors on the socket, and so if the
1009 * connection drops we don't notice until the timeout expires.
1010 * A better solution than sending a NOOP every time would be
1011 * for every command to be prepared to retry until it is
1012 * successfully sent. -- mbp */
1013 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1014 /* verify that the session is still alive */
1015 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1016 debug_print("disconnected!\n");
1018 session = imap_reconnect_if_possible(folder, session);
1020 rfolder->session = NULL;
1021 rfolder->connecting = FALSE;
1022 session = imap_session_get(folder);
1026 session->cancelled = FALSE;
1029 rfolder->session = SESSION(session);
1030 rfolder->connecting = FALSE;
1032 return IMAP_SESSION(session);
1035 static IMAPSession *imap_session_new(Folder * folder,
1036 const PrefsAccount *account)
1038 IMAPSession *session;
1041 int authenticated = FALSE;
1044 /* FIXME: IMAP over SSL only... */
1047 port = account->set_imapport ? account->imapport
1048 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1049 ssl_type = account->ssl_imap;
1051 if (account->ssl_imap != SSL_NONE) {
1052 if (alertpanel_full(_("Insecure connection"),
1053 _("This connection is configured to be secured "
1054 "using SSL, but SSL is not available in this "
1055 "build of Claws Mail. \n\n"
1056 "Do you want to continue connecting to this "
1057 "server? The communication would not be "
1059 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1060 NULL, FALSE, NULL, ALERT_WARNING,
1061 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1064 port = account->set_imapport ? account->imapport
1069 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1071 if (account->set_tunnelcmd) {
1072 r = imap_threaded_connect_cmd(folder,
1074 account->recv_server,
1081 if (ssl_type == SSL_TUNNEL) {
1082 r = imap_threaded_connect_ssl(folder,
1083 account->recv_server,
1089 r = imap_threaded_connect(folder,
1090 account->recv_server,
1095 statuswindow_pop_all();
1096 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1097 authenticated = TRUE;
1099 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1100 authenticated = FALSE;
1104 if (r == MAILIMAP_ERROR_SSL)
1105 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1108 imap_handle_error(NULL, account->recv_server, r);
1110 if(!prefs_common.no_recv_err_panel) {
1111 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1112 account->recv_server, port);
1114 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1115 account->recv_server, port);
1121 session = g_new0(IMAPSession, 1);
1122 session_init(SESSION(session), account, FALSE);
1123 SESSION(session)->type = SESSION_IMAP;
1124 SESSION(session)->server = g_strdup(account->recv_server);
1125 SESSION(session)->port = port;
1126 SESSION(session)->sock = NULL;
1128 SESSION(session)->destroy = imap_session_destroy;
1130 session->capability = NULL;
1132 session->authenticated = authenticated;
1133 session->mbox = NULL;
1134 session->exists = 0;
1135 session->recent = 0;
1136 session->expunge = 0;
1137 session->cmd_count = 0;
1138 session->folder = folder;
1139 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1142 if (account->ssl_imap == SSL_STARTTLS) {
1145 ok = imap_cmd_starttls(session);
1146 if (ok != MAILIMAP_NO_ERROR) {
1147 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1148 if (!is_fatal(ok)) {
1149 SESSION(session)->sock = NULL;
1150 session_destroy(SESSION(session));
1155 imap_free_capabilities(session);
1156 session->authenticated = FALSE;
1157 session->uidplus = FALSE;
1158 session->cmd_count = 1;
1161 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1162 (session->authenticated) ? "pre" : "un");
1167 static gint imap_session_authenticate(IMAPSession *session,
1168 PrefsAccount *account)
1170 gchar *pass, *acc_pass;
1171 gboolean failed = FALSE;
1172 gint ok = MAILIMAP_NO_ERROR;
1173 g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1174 acc_pass = account->passwd;
1177 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON && account->imap_auth_type != IMAP_AUTH_GSSAPI) {
1179 tmp_pass = input_dialog_query_password_keep(account->recv_server,
1181 &(account->session_passwd));
1183 return MAILIMAP_NO_ERROR;
1184 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1186 } else if (account->imap_auth_type == IMAP_AUTH_ANON || account->imap_auth_type == IMAP_AUTH_GSSAPI) {
1189 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1190 account->recv_server);
1191 if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1192 statusbar_pop_all();
1194 if (!failed && !is_fatal(ok)) {
1197 if (account->session_passwd != NULL) {
1198 g_free(account->session_passwd);
1199 account->session_passwd = NULL;
1203 if (prefs_common.no_recv_err_panel) {
1204 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1205 mainwindow_show_error();
1207 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1213 statuswindow_pop_all();
1214 session->authenticated = TRUE;
1215 return MAILIMAP_NO_ERROR;
1218 static void imap_session_destroy(Session *session)
1220 if (session->state != SESSION_DISCONNECTED)
1221 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1223 imap_free_capabilities(IMAP_SESSION(session));
1224 g_free(IMAP_SESSION(session)->mbox);
1227 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1229 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1232 static guint get_file_size_with_crs(const gchar *filename)
1238 if (filename == NULL)
1241 fp = g_fopen(filename, "rb");
1245 while (fgets(buf, sizeof (buf), fp) != NULL) {
1247 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1255 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1257 gchar *path, *filename;
1259 path = folder_item_get_path(item);
1261 if (!is_dir_exist(path)) {
1266 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1269 if (is_file_exist(filename)) {
1270 claws_unlink(filename);
1275 typedef struct _TagsData {
1278 IMAPFolderItem *item;
1281 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1283 IMAPSession *session;
1284 gint ok, can_create_tags;
1285 Folder *folder = NULL;
1286 TagsData *ht_data = NULL;
1289 g_return_if_fail(item != NULL);
1290 g_return_if_fail(msginfo != NULL);
1292 folder = item->folder;
1293 debug_print("getting session...\n");
1294 session = imap_session_get(folder);
1297 debug_print("can't get session\n");
1301 ok = imap_select(session, IMAP_FOLDER(folder), item,
1302 NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1304 if (ok != MAILIMAP_NO_ERROR) {
1309 if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1312 if (IMAP_FOLDER_ITEM(item)->batching) {
1313 /* instead of performing an UID STORE command for each message change,
1314 * as a lot of them can change "together", we just fill in hashtables
1315 * and defer the treatment so that we're able to send only one
1318 debug_print("IMAP batch mode on, deferring tags change\n");
1319 for (cur = tags_set; cur; cur = cur->next) {
1320 gint cur_tag = GPOINTER_TO_INT(cur->data);
1322 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table,
1323 GINT_TO_POINTER(cur_tag));
1324 if (ht_data == NULL) {
1325 ht_data = g_new0(TagsData, 1);
1326 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1327 ht_data->item = IMAP_FOLDER_ITEM(item);
1328 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table,
1329 GINT_TO_POINTER(cur_tag), ht_data);
1331 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1334 for (cur = tags_unset; cur; cur = cur->next) {
1335 gint cur_tag = GPOINTER_TO_INT(cur->data);
1337 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1338 GINT_TO_POINTER(cur_tag));
1339 if (ht_data == NULL) {
1340 ht_data = g_new0(TagsData, 1);
1341 ht_data->str = g_strdup(tags_get_tag(cur_tag));
1342 ht_data->item = IMAP_FOLDER_ITEM(item);
1343 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table,
1344 GINT_TO_POINTER(cur_tag), ht_data);
1346 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1350 GSList *list_set = NULL;
1351 GSList *list_unset = NULL;
1354 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1355 numlist.next = NULL;
1357 debug_print("IMAP changing tags NOW\n");
1358 for (cur = tags_set; cur; cur = cur->next) {
1359 gint cur_tag = GPOINTER_TO_INT(cur->data);
1360 const gchar *str = tags_get_tag(cur_tag);
1361 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1362 list_set = g_slist_prepend(list_set, g_strdup(str));
1365 ok = imap_set_message_flags(session,
1366 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1367 slist_free_strings(list_set);
1368 g_slist_free(list_set);
1369 if (ok != MAILIMAP_NO_ERROR) {
1374 for (cur = tags_unset; cur; cur = cur->next) {
1375 gint cur_tag = GPOINTER_TO_INT(cur->data);
1376 const gchar *str = tags_get_tag(cur_tag);
1377 if (strcmp(str, "$Forwarded") && strcmp(str, "Junk") && strcmp(str, "NonJunk") && strcmp(str, "NotJunk") && strcmp(str, "NoJunk") && strcmp(str, "Junk") )
1378 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1381 ok = imap_set_message_flags(session,
1382 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1383 slist_free_strings(list_unset);
1384 g_slist_free(list_unset);
1385 if (ok != MAILIMAP_NO_ERROR) {
1392 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1393 gboolean headers, gboolean body)
1395 gchar *path, *filename;
1396 IMAPSession *session;
1399 g_return_val_if_fail(folder != NULL, NULL);
1400 g_return_val_if_fail(item != NULL, NULL);
1405 path = folder_item_get_path(item);
1406 if (!is_dir_exist(path))
1407 make_dir_hier(path);
1408 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1410 debug_print("trying to fetch cached %s\n", filename);
1411 if (is_file_exist(filename)) {
1412 /* see whether the local file represents the whole message
1413 * or not. As the IMAP server reports size with \r chars,
1414 * we have to update the local file (UNIX \n only) size */
1415 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1416 guint have_size = -1;
1419 debug_print("message %d has been already %scached.\n", uid,
1420 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1422 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1423 have_size = get_file_size_with_crs(filename);
1424 if (cached && (cached->size <= have_size || !body)) {
1425 procmsg_msginfo_free(cached);
1426 ok = file_strip_crs(filename);
1427 if (ok == 0 && cached && cached->size <= have_size) {
1428 /* we have it all and stripped */
1429 debug_print("...fully cached in fact; setting flag.\n");
1430 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1433 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1434 debug_print("message not cached and file recent, considering file complete\n");
1435 ok = file_strip_crs(filename);
1439 procmsg_msginfo_free(cached);
1442 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1443 procmsg_msginfo_free(cached);
1447 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1449 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1450 procmsg_msginfo_free(cached);
1454 debug_print("getting session...\n");
1455 session = imap_session_get(folder);
1461 session_set_access_time(SESSION(session));
1462 lock_session(session); /* unlocked later in the function */
1464 debug_print("IMAP fetching messages\n");
1465 ok = imap_select(session, IMAP_FOLDER(folder), item,
1466 NULL, NULL, NULL, NULL, NULL, FALSE);
1467 if (ok != MAILIMAP_NO_ERROR) {
1468 g_warning("can't select mailbox %s\n", item->path);
1473 session_set_access_time(SESSION(session));
1475 debug_print("getting message %d...\n", uid);
1476 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1478 if (ok != MAILIMAP_NO_ERROR) {
1479 g_warning("can't fetch message %d\n", uid);
1484 session_set_access_time(SESSION(session));
1485 unlock_session(session);
1487 ok = file_strip_crs(filename);
1489 if (ok == 0 && headers && body) {
1490 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1492 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1493 procmsg_msginfo_free(cached);
1495 } else if (ok == -1) {
1496 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1498 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1499 procmsg_msginfo_free(cached);
1505 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1507 gchar *path, *filename;
1509 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1514 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1515 procmsg_msginfo_free(cached);
1518 path = folder_item_get_path(item);
1519 if (!is_dir_exist(path))
1522 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1524 if (is_file_exist(filename)) {
1525 if (cached && cached->total_size == cached->size) {
1528 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1531 size = get_file_size_with_crs(filename);
1534 if (cached && size >= cached->size) {
1535 cached->total_size = cached->size;
1536 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1537 procmsg_msginfo_free(cached);
1541 procmsg_msginfo_free(cached);
1545 void imap_cache_msg(FolderItem *item, gint msgnum)
1547 Folder *folder = NULL;
1551 folder = item->folder;
1553 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1554 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1555 debug_print("fetched %s\n", tmp);
1560 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1561 const gchar *file, MsgFlags *flags)
1565 MsgFileInfo fileinfo;
1567 g_return_val_if_fail(file != NULL, -1);
1569 fileinfo.msginfo = NULL;
1570 fileinfo.file = (gchar *)file;
1571 fileinfo.flags = flags;
1572 file_list.data = &fileinfo;
1573 file_list.next = NULL;
1575 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1579 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1580 GRelation *relation)
1583 IMAPSession *session;
1584 guint32 last_uid = 0;
1586 MsgFileInfo *fileinfo;
1587 gint ok = MAILIMAP_NO_ERROR;
1588 gint curnum = 0, total = 0;
1589 gboolean missing_uids = FALSE;
1591 g_return_val_if_fail(folder != NULL, -1);
1592 g_return_val_if_fail(dest != NULL, -1);
1593 g_return_val_if_fail(file_list != NULL, -1);
1595 debug_print("getting session...\n");
1596 session = imap_session_get(folder);
1600 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1603 statusbar_print_all(_("Adding messages..."));
1604 total = g_slist_length(file_list);
1605 for (cur = file_list; cur != NULL; cur = cur->next) {
1606 IMAPFlags iflags = 0;
1607 guint32 new_uid = 0;
1608 gchar *real_file = NULL;
1609 fileinfo = (MsgFileInfo *)cur->data;
1611 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1614 if (fileinfo->flags) {
1615 if (MSG_IS_MARKED(*fileinfo->flags))
1616 iflags |= IMAP_FLAG_FLAGGED;
1617 if (MSG_IS_REPLIED(*fileinfo->flags))
1618 iflags |= IMAP_FLAG_ANSWERED;
1619 if (MSG_IS_FORWARDED(*fileinfo->flags))
1620 iflags |= IMAP_FLAG_FORWARDED;
1621 if (MSG_IS_SPAM(*fileinfo->flags))
1622 iflags |= IMAP_FLAG_SPAM;
1624 iflags |= IMAP_FLAG_HAM;
1625 if (!MSG_IS_UNREAD(*fileinfo->flags))
1626 iflags |= IMAP_FLAG_SEEN;
1630 if (real_file == NULL)
1631 real_file = g_strdup(fileinfo->file);
1633 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1634 folder_has_parent_of_type(dest, F_OUTBOX) ||
1635 folder_has_parent_of_type(dest, F_DRAFT) ||
1636 folder_has_parent_of_type(dest, F_TRASH))
1637 iflags |= IMAP_FLAG_SEEN;
1639 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags,
1642 if (ok != MAILIMAP_NO_ERROR) {
1643 g_warning("can't append message %s\n", real_file);
1646 statusbar_progress_all(0,0,0);
1647 statusbar_pop_all();
1650 debug_print("appended new message as %d\n", new_uid);
1651 /* put the local file in the imapcache, so that we don't
1652 * have to fetch it back later. */
1655 missing_uids = TRUE;
1656 debug_print("Missing UID (0)\n");
1659 gchar *cache_path = folder_item_get_path(dest);
1660 if (!is_dir_exist(cache_path))
1661 make_dir_hier(cache_path);
1662 if (is_dir_exist(cache_path)) {
1663 gchar *cache_file = g_strconcat(
1664 cache_path, G_DIR_SEPARATOR_S,
1665 itos(new_uid), NULL);
1666 copy_file(real_file, cache_file, TRUE);
1667 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1674 if (relation != NULL)
1675 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1676 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1677 GINT_TO_POINTER(new_uid));
1678 if (last_uid < new_uid) {
1685 statusbar_progress_all(0,0,0);
1686 statusbar_pop_all();
1691 imap_scan_required(folder, dest);
1693 session = imap_session_get(folder);
1699 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1700 &a, NULL, NULL, NULL, NULL, FALSE);
1705 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1707 GSList *result = NULL;
1712 if (!set || !set->set_list)
1715 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1716 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1717 start = item->set_first;
1718 end = item->set_last;
1719 for (t = start; t <= end; t++) {
1720 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1723 result = g_slist_reverse(result);
1724 if (debug_get_mode()) {
1725 debug_print("flat imap set: ");
1726 for (cur = result; cur; cur = cur->next) {
1727 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1734 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1735 MsgInfoList *msglist, GRelation *relation)
1739 GSList *seq_list, *cur;
1741 IMAPSession *session;
1742 gint ok = MAILIMAP_NO_ERROR;
1743 GHashTable *uid_hash;
1745 gboolean single = FALSE;
1747 g_return_val_if_fail(folder != NULL, -1);
1748 g_return_val_if_fail(dest != NULL, -1);
1749 g_return_val_if_fail(msglist != NULL, -1);
1751 debug_print("getting session...\n");
1752 session = imap_session_get(folder);
1758 msginfo = (MsgInfo *)msglist->data;
1759 if (msglist->next == NULL)
1761 src = msginfo->folder;
1763 g_warning("the src folder is identical to the dest.\n");
1767 if (src->folder != dest->folder) {
1768 GSList *infolist = NULL, *cur;
1770 for (cur = msglist; cur; cur = cur->next) {
1771 msginfo = (MsgInfo *)cur->data;
1772 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1773 fileinfo->file = procmsg_get_message_file(msginfo);
1774 fileinfo->flags = &(msginfo->flags);
1775 infolist = g_slist_prepend(infolist, fileinfo);
1777 infolist = g_slist_reverse(infolist);
1778 res = folder_item_add_msgs(dest, infolist, FALSE);
1779 for (cur = infolist; cur; cur = cur->next) {
1780 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1784 g_slist_free(infolist);
1788 lock_session(session); /* unlocked later in the function */
1790 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1791 NULL, NULL, NULL, NULL, NULL, FALSE);
1792 if (ok != MAILIMAP_NO_ERROR) {
1796 unlock_session(session);
1798 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1803 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1804 uid_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
1806 statusbar_print_all(_("Copying messages..."));
1807 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1808 struct mailimap_set * seq_set;
1809 struct mailimap_set * source = NULL;
1810 struct mailimap_set * dest = NULL;
1811 seq_set = cur->data;
1813 debug_print("Copying messages from %s to %s ...\n",
1814 src->path, destdir);
1816 lock_session(session); /* unlocked later in the function */
1817 ok = imap_cmd_copy(session, seq_set, destdir,
1824 if (ok == MAILIMAP_NO_ERROR) {
1825 unlock_session(session);
1826 if (relation && source && dest) {
1827 GSList *s_list = flatten_mailimap_set(source);
1828 GSList *d_list = flatten_mailimap_set(dest);
1829 GSList *s_cur, *d_cur;
1830 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1832 for (s_cur = s_list, d_cur = d_list;
1834 s_cur = s_cur->next, d_cur = d_cur->next) {
1835 g_hash_table_insert(uid_hash, s_cur->data, d_cur->data);
1839 debug_print("hhhmm, source list length != dest list length.\n");
1841 g_slist_free(s_list);
1842 g_slist_free(d_list);
1848 mailimap_set_free(source);
1850 mailimap_set_free(dest);
1852 if (ok != MAILIMAP_NO_ERROR) {
1853 g_hash_table_destroy(uid_hash);
1854 imap_lep_set_free(seq_list);
1855 statusbar_pop_all();
1860 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1861 MsgInfo *msginfo = (MsgInfo *)cur->data;
1864 hashval = g_hash_table_lookup(uid_hash, GINT_TO_POINTER(msginfo->msgnum));
1866 if (hashval != NULL) {
1867 gint num = GPOINTER_TO_INT(hashval);
1868 g_relation_insert(relation, msginfo,
1869 GINT_TO_POINTER(num));
1872 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1873 /* put the local file in the imapcache, so that we don't
1874 * have to fetch it back later. */
1876 gchar *cache_path = folder_item_get_path(msginfo->folder);
1877 gchar *real_file = g_strconcat(
1878 cache_path, G_DIR_SEPARATOR_S,
1879 itos(msginfo->msgnum), NULL);
1880 gchar *cache_file = NULL;
1882 cache_path = folder_item_get_path(dest);
1883 cache_file = g_strconcat(
1884 cache_path, G_DIR_SEPARATOR_S,
1886 if (!is_dir_exist(cache_path))
1887 make_dir_hier(cache_path);
1888 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1889 copy_file(real_file, cache_file, TRUE);
1890 debug_print("copied to cache: %s\n", cache_file);
1897 g_relation_insert(relation, msginfo,
1898 GINT_TO_POINTER(0));
1900 statusbar_pop_all();
1902 g_hash_table_destroy(uid_hash);
1903 imap_lep_set_free(seq_list);
1907 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1908 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1909 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1910 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1912 imap_scan_required(folder, dest);
1913 if (ok == MAILIMAP_NO_ERROR)
1919 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1923 g_return_val_if_fail(msginfo != NULL, -1);
1925 msglist.data = msginfo;
1926 msglist.next = NULL;
1928 return imap_copy_msgs(folder, dest, &msglist, NULL);
1931 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1932 MsgInfoList *msglist, GRelation *relation)
1937 g_return_val_if_fail(folder != NULL, -1);
1938 g_return_val_if_fail(dest != NULL, -1);
1939 g_return_val_if_fail(msglist != NULL, -1);
1941 msginfo = (MsgInfo *)msglist->data;
1942 g_return_val_if_fail(msginfo->folder != NULL, -1);
1944 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1949 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1950 MsgInfoList *msglist, GRelation *relation)
1952 gchar *destdir, *dir;
1953 GSList *numlist = NULL, *cur;
1955 IMAPSession *session;
1956 gint ok = MAILIMAP_NO_ERROR;
1958 g_return_val_if_fail(folder != NULL, -1);
1959 g_return_val_if_fail(dest != NULL, -1);
1960 g_return_val_if_fail(msglist != NULL, -1);
1962 debug_print("getting session...\n");
1963 session = imap_session_get(folder);
1968 lock_session(session); /* unlocked later in the function */
1970 msginfo = (MsgInfo *)msglist->data;
1972 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1973 NULL, NULL, NULL, NULL, NULL, FALSE);
1974 if (ok != MAILIMAP_NO_ERROR) {
1978 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1983 for (cur = msglist; cur; cur = cur->next) {
1984 msginfo = (MsgInfo *)cur->data;
1985 if (!MSG_IS_DELETED(msginfo->flags))
1986 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1988 numlist = g_slist_reverse(numlist);
1990 if (numlist != NULL) {
1991 ok = imap_set_message_flags
1992 (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
1993 if (ok != MAILIMAP_NO_ERROR) {
1994 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1998 } /* else we just need to expunge */
1999 ok = imap_cmd_expunge(session, folder->account->imap_use_trash);
2000 if (ok != MAILIMAP_NO_ERROR) {
2001 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
2006 session->folder_content_changed = TRUE;
2007 unlock_session(session);
2009 dir = folder_item_get_path(msginfo->folder);
2010 if (is_dir_exist(dir)) {
2011 for (cur = msglist; cur; cur = cur->next) {
2012 msginfo = (MsgInfo *)cur->data;
2013 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
2018 g_slist_free(numlist);
2020 imap_scan_required(folder, dest);
2023 if (ok == MAILIMAP_NO_ERROR)
2029 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
2030 MsgInfoList *msglist, GRelation *relation)
2034 g_return_val_if_fail(folder != NULL, -1);
2035 g_return_val_if_fail(dest != NULL, -1);
2036 if (msglist == NULL)
2039 msginfo = (MsgInfo *)msglist->data;
2040 g_return_val_if_fail(msginfo->folder != NULL, -1);
2042 return imap_do_remove_msgs(folder, dest, msglist, relation);
2045 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2047 GSList *list = folder_item_get_msg_list(item);
2048 gint res = imap_remove_msgs(folder, item, list, NULL);
2049 procmsg_msg_list_free(list);
2053 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2056 /* TODO: properly implement this method */
2060 static gint imap_close(Folder *folder, FolderItem *item)
2065 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2067 FolderItem *item = NULL;
2068 IMAPSession *session;
2069 gchar *root_folder = NULL;
2071 g_return_val_if_fail(folder != NULL, -1);
2072 g_return_val_if_fail(folder->account != NULL, -1);
2074 debug_print("getting session...\n");
2075 session = imap_session_get(folder);
2077 if (!folder->node) {
2078 folder_tree_destroy(folder);
2079 item = folder_item_new(folder, folder->name, NULL);
2080 item->folder = folder;
2081 folder->node = item->node = g_node_new(item);
2086 if (folder->account->imap_dir && *folder->account->imap_dir) {
2088 int r = MAILIMAP_NO_ERROR;
2091 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2092 extract_quote(root_folder, '"');
2093 subst_char(root_folder,
2094 imap_get_path_separator(session, IMAP_FOLDER(folder),
2099 strtailchomp(root_folder, '/');
2100 real_path = imap_get_real_path
2101 (session, IMAP_FOLDER(folder), root_folder, &r);
2104 debug_print("IMAP root directory: %s\n", real_path);
2106 /* check if root directory exist */
2108 r = imap_threaded_list(session->folder, "", real_path,
2111 if (r != MAILIMAP_NO_ERROR)
2112 imap_handle_error(SESSION(session), NULL, r);
2114 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2115 if (!folder->node) {
2116 item = folder_item_new(folder, folder->name, NULL);
2117 item->folder = folder;
2118 folder->node = item->node = g_node_new(item);
2122 mailimap_list_result_free(lep_list);
2128 item = FOLDER_ITEM(folder->node->data);
2130 if (item && !item->path && root_folder) {
2131 item->path = g_strdup(root_folder);
2134 if (!item || ((item->path || root_folder) &&
2135 strcmp2(item->path, root_folder) != 0)) {
2136 folder_tree_destroy(folder);
2137 item = folder_item_new(folder, folder->name, root_folder);
2138 item->folder = folder;
2139 folder->node = item->node = g_node_new(item);
2142 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2143 imap_create_missing_folders(folder);
2148 static gint imap_scan_tree(Folder *folder)
2150 gboolean subs_only = FALSE;
2151 if (folder->account) {
2152 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2153 subs_only = folder->account->imap_subsonly;
2155 return imap_scan_tree_real(folder, subs_only);
2158 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2161 IMAPFolder *imapfolder;
2162 FolderItem *new_item;
2163 GSList *item_list, *cur;
2166 gchar *wildcard_path;
2170 int r = MAILIMAP_NO_ERROR;
2172 g_return_val_if_fail(item != NULL, -1);
2173 g_return_val_if_fail(item->folder != NULL, -1);
2174 g_return_val_if_fail(item->no_sub == FALSE, -1);
2176 folder = item->folder;
2177 imapfolder = IMAP_FOLDER(folder);
2179 separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2183 if (folder->ui_func)
2184 folder->ui_func(folder, item, folder->ui_func_data);
2187 wildcard[0] = separator;
2190 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2198 real_path = g_strdup("");
2201 Xstrcat_a(wildcard_path, real_path, wildcard,
2202 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2206 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2208 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2210 if (r != MAILIMAP_NO_ERROR) {
2211 imap_handle_error(SESSION(session), NULL, r);
2217 item_list = imap_list_from_lep(imapfolder,
2218 lep_list, real_path, FALSE);
2219 mailimap_list_result_free(lep_list);
2224 node = item->node->children;
2225 while (node != NULL) {
2226 FolderItem *old_item = FOLDER_ITEM(node->data);
2227 GNode *next = node->next;
2230 for (cur = item_list; cur != NULL; cur = cur->next) {
2231 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2232 if (!strcmp2(old_item->path, cur_item->path)) {
2233 new_item = cur_item;
2238 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2239 debug_print("not removing INBOX\n");
2241 debug_print("folder '%s' not found. removing...\n",
2243 folder_item_remove(old_item);
2246 old_item->no_sub = new_item->no_sub;
2247 old_item->no_select = new_item->no_select;
2248 if (old_item->no_sub == TRUE && node->children) {
2249 debug_print("folder '%s' doesn't have "
2250 "subfolders. removing...\n",
2252 folder_item_remove_children(old_item);
2259 for (cur = item_list; cur != NULL; cur = cur->next) {
2260 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2263 for (node = item->node->children; node != NULL;
2264 node = node->next) {
2265 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2267 new_item = FOLDER_ITEM(node->data);
2268 folder_item_destroy(cur_item);
2274 new_item = cur_item;
2275 debug_print("new folder '%s' found.\n", new_item->path);
2276 folder_item_append(item, new_item);
2279 if (!strcmp(new_item->path, "INBOX")) {
2280 new_item->stype = F_INBOX;
2281 folder->inbox = new_item;
2282 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2285 base = g_path_get_basename(new_item->path);
2287 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2288 new_item->stype = F_OUTBOX;
2289 folder->outbox = new_item;
2290 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2291 new_item->stype = F_DRAFT;
2292 folder->draft = new_item;
2293 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2294 new_item->stype = F_QUEUE;
2295 folder->queue = new_item;
2296 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2297 new_item->stype = F_TRASH;
2298 folder->trash = new_item;
2303 if (new_item->no_sub == FALSE)
2304 imap_scan_tree_recursive(session, new_item, subs_only);
2307 g_slist_free(item_list);
2309 return MAILIMAP_NO_ERROR;
2312 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2314 IMAPSession *session = imap_session_get(folder);
2316 gchar *wildcard_path;
2320 GSList *item_list = NULL, *cur;
2321 GList *child_list = NULL, *tmplist = NULL;
2322 GSList *sub_list = NULL;
2323 int r = MAILIMAP_NO_ERROR;
2328 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2333 wildcard[0] = separator;
2336 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2344 real_path = g_strdup("");
2347 Xstrcat_a(wildcard_path, real_path, wildcard,
2348 {g_free(real_path); return NULL;});
2352 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2353 item->path?item->path:item->name);
2355 statusbar_print_all(_("Looking for subfolders of %s..."),
2356 item->path?item->path:item->name);
2358 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2361 statusbar_pop_all();
2364 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2365 lep_list, real_path, FALSE);
2366 mailimap_list_result_free(lep_list);
2368 for (cur = item_list; cur != NULL; cur = cur->next) {
2369 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2371 tmplist = imap_scan_subtree(folder, cur_item,
2372 unsubs_only, recursive);
2374 child_list = g_list_concat(child_list, tmplist);
2376 child_list = g_list_prepend(child_list,
2377 imap_get_real_path(session,
2378 IMAP_FOLDER(folder), cur_item->path, &r));
2381 statusbar_pop_all();
2384 folder_item_destroy(cur_item);
2386 child_list = g_list_reverse(child_list);
2387 g_slist_free(item_list);
2390 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2393 statusbar_pop_all();
2396 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2397 lep_list, real_path, FALSE);
2398 mailimap_list_result_free(lep_list);
2400 for (cur = sub_list; cur != NULL; cur = cur->next) {
2401 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2402 GList *oldlitem = NULL;
2403 gchar *tmp = imap_get_real_path(session,
2404 IMAP_FOLDER(folder), cur_item->path, &r);
2407 statusbar_pop_all();
2410 folder_item_destroy(cur_item);
2411 oldlitem = g_list_find_custom(
2412 child_list, tmp, (GCompareFunc)strcmp2);
2414 child_list = g_list_remove_link(child_list, oldlitem);
2415 g_free(oldlitem->data);
2416 g_list_free(oldlitem);
2423 statusbar_pop_all();
2428 static gint imap_create_tree(Folder *folder)
2430 g_return_val_if_fail(folder != NULL, -1);
2431 g_return_val_if_fail(folder->node != NULL, -1);
2432 g_return_val_if_fail(folder->node->data != NULL, -1);
2433 g_return_val_if_fail(folder->account != NULL, -1);
2435 imap_scan_tree(folder);
2436 imap_create_missing_folders(folder);
2441 static void imap_create_missing_folders(Folder *folder)
2443 g_return_if_fail(folder != NULL);
2446 folder->inbox = imap_create_special_folder
2447 (folder, F_INBOX, "INBOX");
2449 folder->trash = imap_create_special_folder
2450 (folder, F_TRASH, "Trash");
2452 folder->queue = imap_create_special_folder
2453 (folder, F_QUEUE, "Queue");
2454 if (!folder->outbox)
2455 folder->outbox = imap_create_special_folder
2456 (folder, F_OUTBOX, "Sent");
2458 folder->draft = imap_create_special_folder
2459 (folder, F_DRAFT, "Drafts");
2462 static FolderItem *imap_create_special_folder(Folder *folder,
2463 SpecialFolderItemType stype,
2467 FolderItem *new_item;
2469 g_return_val_if_fail(folder != NULL, NULL);
2470 g_return_val_if_fail(folder->node != NULL, NULL);
2471 g_return_val_if_fail(folder->node->data != NULL, NULL);
2472 g_return_val_if_fail(folder->account != NULL, NULL);
2473 g_return_val_if_fail(name != NULL, NULL);
2475 item = FOLDER_ITEM(folder->node->data);
2476 new_item = imap_create_folder(folder, item, name);
2479 g_warning("Can't create '%s'\n", name);
2480 if (!folder->inbox) return NULL;
2482 new_item = imap_create_folder(folder, folder->inbox, name);
2484 g_warning("Can't create '%s' under INBOX\n", name);
2486 new_item->stype = stype;
2488 new_item->stype = stype;
2493 static gchar *imap_folder_get_path(Folder *folder)
2497 g_return_val_if_fail(folder != NULL, NULL);
2498 g_return_val_if_fail(folder->account != NULL, NULL);
2500 folder_path = g_strconcat(get_imap_cache_dir(),
2502 folder->account->recv_server,
2504 folder->account->userid,
2511 static gchar *imap_encode_unsafe_chars(const gchar *str)
2513 gchar *ret = NULL, *o_ret;
2517 ret = g_malloc(3*strlen(str)+1);
2519 for (i = str; *i; i++) {
2529 *ret++ = '0'+(*i/10);
2530 *ret++ = '0'+(*i%10);
2540 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2542 gchar *folder_path, *path;
2543 gchar *item_path = NULL;
2545 g_return_val_if_fail(folder != NULL, NULL);
2546 g_return_val_if_fail(item != NULL, NULL);
2547 folder_path = imap_folder_get_path(folder);
2549 g_return_val_if_fail(folder_path != NULL, NULL);
2552 item_path = g_strdup(item->path);
2554 item_path = imap_encode_unsafe_chars(item->path);
2557 if (g_path_is_absolute(folder_path)) {
2559 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2562 path = g_strdup(folder_path);
2565 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2566 folder_path, G_DIR_SEPARATOR_S,
2569 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2572 g_free(folder_path);
2575 while (strchr(path, '/'))
2576 *strchr(path, '/') = '\\';
2582 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2585 gchar *dirpath, *imap_path;
2586 IMAPSession *session;
2587 FolderItem *new_item;
2591 gint ok = MAILIMAP_NO_ERROR;
2592 gboolean no_select = FALSE, no_sub = FALSE;
2593 gboolean exist = FALSE;
2595 g_return_val_if_fail(folder != NULL, NULL);
2596 g_return_val_if_fail(folder->account != NULL, NULL);
2597 g_return_val_if_fail(parent != NULL, NULL);
2598 g_return_val_if_fail(name != NULL, NULL);
2600 debug_print("getting session...\n");
2601 session = imap_session_get(folder);
2606 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2607 dirpath = g_strdup(name);
2608 }else if (parent->path)
2609 dirpath = g_strconcat(parent->path, "/", name, NULL);
2610 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2611 dirpath = g_strdup(name);
2612 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2615 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2616 strtailchomp(imap_dir, '/');
2617 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2619 dirpath = g_strdup(name);
2623 /* keep trailing directory separator to create a folder that contains
2625 imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2627 strtailchomp(dirpath, '/');
2628 Xstrdup_a(new_name, name, {
2633 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2638 imap_path_separator_subst(imap_path, separator);
2639 /* remove trailing / for display */
2640 strtailchomp(new_name, '/');
2642 if (strcmp(dirpath, "INBOX") != 0) {
2647 argbuf = g_ptr_array_new();
2648 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2649 if (r != MAILIMAP_NO_ERROR) {
2650 imap_handle_error(SESSION(session), NULL, r);
2651 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2654 ptr_array_free_strings(argbuf);
2655 g_ptr_array_free(argbuf, TRUE);
2659 if (clist_count(lep_list) > 0)
2661 mailimap_list_result_free(lep_list);
2664 ok = imap_cmd_create(session, imap_path);
2665 if (ok != MAILIMAP_NO_ERROR) {
2666 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2671 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2672 if (r == MAILIMAP_NO_ERROR) {
2673 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2674 lep_list, dirpath, TRUE);
2676 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2677 no_select = cur_item->no_select;
2678 no_sub = cur_item->no_sub;
2679 g_slist_free(item_list);
2681 mailimap_list_result_free(lep_list);
2683 imap_handle_error(SESSION(session), NULL, r);
2686 imap_threaded_subscribe(folder, imap_path, TRUE);
2690 /* just get flags */
2691 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2692 if (r == MAILIMAP_NO_ERROR) {
2693 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2694 lep_list, dirpath, TRUE);
2696 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2697 no_select = cur_item->no_select;
2698 no_sub = cur_item->no_sub;
2699 g_slist_free(item_list);
2701 mailimap_list_result_free(lep_list);
2703 imap_handle_error(SESSION(session), NULL, r);
2707 new_item = folder_item_new(folder, new_name, dirpath);
2708 new_item->no_select = no_select;
2709 new_item->no_sub = no_sub;
2710 folder_item_append(parent, new_item);
2714 dirpath = folder_item_get_path(new_item);
2715 if (!is_dir_exist(dirpath))
2716 make_dir_hier(dirpath);
2720 /* folder existed, scan it */
2721 imap_scan_required(folder, new_item);
2722 folder_item_scan_full(new_item, FALSE);
2728 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2733 gchar *real_oldpath;
2734 gchar *real_newpath;
2736 gchar *old_cache_dir;
2737 gchar *new_cache_dir;
2738 IMAPSession *session;
2740 gint ok = MAILIMAP_NO_ERROR;
2741 gint exists, recent, unseen;
2742 guint32 uid_validity;
2744 g_return_val_if_fail(folder != NULL, -1);
2745 g_return_val_if_fail(item != NULL, -1);
2746 g_return_val_if_fail(item->path != NULL, -1);
2747 g_return_val_if_fail(name != NULL, -1);
2749 debug_print("getting session...\n");
2750 session = imap_session_get(folder);
2755 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
2757 g_warning(_("New folder name must not contain the namespace "
2762 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2767 g_free(session->mbox);
2768 session->mbox = NULL;
2769 session->exists = 0;
2770 session->recent = 0;
2771 session->expunge = 0;
2772 ok = imap_cmd_examine(session, "INBOX",
2773 &exists, &recent, &unseen, &uid_validity, FALSE);
2774 if (ok != MAILIMAP_NO_ERROR) {
2775 g_free(real_oldpath);
2779 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
2782 if (strchr(item->path, G_DIR_SEPARATOR)) {
2783 dirpath = g_path_get_dirname(item->path);
2784 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2787 newpath = g_strdup(name);
2789 real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
2790 imap_path_separator_subst(real_newpath, separator);
2792 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2793 if (ok != MAILIMAP_NO_ERROR) {
2794 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2795 real_oldpath, real_newpath);
2796 g_free(real_oldpath);
2798 g_free(real_newpath);
2802 item->name = g_strdup(name);
2804 old_cache_dir = folder_item_get_path(item);
2806 paths[0] = g_strdup(item->path);
2808 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2809 imap_rename_folder_func, paths);
2811 if (is_dir_exist(old_cache_dir)) {
2812 new_cache_dir = folder_item_get_path(item);
2813 if (g_rename(old_cache_dir, new_cache_dir) < 0) {
2814 FILE_OP_ERROR(old_cache_dir, "rename");
2816 g_free(new_cache_dir);
2819 g_free(old_cache_dir);
2822 g_free(real_oldpath);
2823 g_free(real_newpath);
2827 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2830 gint r = MAILIMAP_NO_ERROR;
2831 IMAPSession *session;
2832 debug_print("getting session...\n");
2834 session = imap_session_get(folder);
2838 if (item && item->path) {
2839 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2846 if (!strcmp(path, "INBOX") && sub == FALSE) {
2850 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2851 r = imap_threaded_subscribe(folder, path, sub);
2854 r = imap_threaded_subscribe(folder, rpath, sub);
2860 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2862 gint ok = MAILIMAP_NO_ERROR;
2863 IMAPSession *session;
2866 gboolean selected_folder;
2868 g_return_val_if_fail(folder != NULL, -1);
2869 g_return_val_if_fail(item != NULL, -1);
2870 g_return_val_if_fail(item->path != NULL, -1);
2872 debug_print("getting session...\n");
2873 session = imap_session_get(folder);
2877 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
2881 imap_threaded_subscribe(folder, path, FALSE);
2883 selected_folder = (session->mbox != NULL) &&
2884 (!strcmp(session->mbox, item->path));
2885 if (selected_folder) {
2886 ok = imap_cmd_close(session);
2887 if (ok != MAILIMAP_NO_ERROR) {
2888 debug_print("close err %d\n", ok);
2892 ok = imap_cmd_delete(session, path);
2893 if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
2896 ok = MAILIMAP_NO_ERROR;
2897 tmp = g_strdup_printf("%s%c", path,
2898 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
2902 ok = imap_cmd_delete(session, path);
2905 if (ok != MAILIMAP_NO_ERROR) {
2906 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2912 cache_dir = folder_item_get_path(item);
2913 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2914 g_warning("can't remove directory '%s'\n", cache_dir);
2916 folder_item_remove(item);
2920 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2924 g_return_val_if_fail(item != NULL, -1);
2925 g_return_val_if_fail(item->folder != NULL, -1);
2926 g_return_val_if_fail(item->node != NULL, -1);
2928 node = item->node->children;
2929 while (node != NULL) {
2931 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2935 debug_print("IMAP removing %s\n", item->path);
2937 if (imap_remove_all_msg(folder, item) < 0)
2939 return imap_remove_folder_real(folder, item);
2942 typedef struct _uncached_data {
2943 IMAPSession *session;
2945 MsgNumberList *numlist;
2952 static void *imap_get_uncached_messages_thread(void *data)
2954 uncached_data *stuff = (uncached_data *)data;
2955 IMAPSession *session = stuff->session;
2956 FolderItem *item = stuff->item;
2957 MsgNumberList *numlist = stuff->numlist;
2958 GSList *newlist = NULL;
2959 GSList *llast = NULL;
2960 GSList *seq_list, *cur;
2961 gboolean got_alien_tags = FALSE;
2963 debug_print("uncached_messages\n");
2965 if (session == NULL || item == NULL || item->folder == NULL
2966 || FOLDER_CLASS(item->folder) != &imap_class) {
2971 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2972 debug_print("get msgs info\n");
2973 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2974 struct mailimap_set * imapset;
2980 if (session->cancelled)
2983 imapset = cur->data;
2985 r = imap_threaded_fetch_env(session->folder,
2986 imapset, &env_list);
2987 if (r != MAILIMAP_NO_ERROR) {
2988 imap_handle_error(SESSION(session), NULL, r);
2996 session_set_access_time(SESSION(session));
2999 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
3000 struct imap_fetch_env_info * info;
3002 GSList *tags = NULL, *cur = NULL;
3003 info = carray_get(env_list, i);
3004 tags = carray_get(env_list, i+1);
3005 msginfo = imap_envelope_from_lep(info, item);
3006 if (msginfo == NULL) {
3007 slist_free_strings(tags);
3011 g_slist_free(msginfo->tags);
3012 msginfo->tags = NULL;
3014 for (cur = tags; cur; cur = cur->next) {
3015 gchar *real_tag = imap_modified_utf7_to_utf8(cur->data, TRUE);
3017 id = tags_get_id_for_str(real_tag);
3019 id = tags_add_tag(real_tag);
3020 got_alien_tags = TRUE;
3022 if (!g_slist_find(msginfo->tags, GINT_TO_POINTER(id))) {
3023 msginfo->tags = g_slist_prepend(
3025 GINT_TO_POINTER(id));
3030 msginfo->tags = g_slist_reverse(msginfo->tags);
3031 slist_free_strings(tags);
3033 msginfo->folder = item;
3035 llast = newlist = g_slist_append(newlist, msginfo);
3037 llast = g_slist_append(llast, msginfo);
3038 llast = llast->next;
3043 imap_fetch_env_free(env_list);
3046 if (got_alien_tags) {
3048 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
3051 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
3052 struct mailimap_set * imapset;
3054 imapset = cur->data;
3055 mailimap_set_free(imapset);
3058 session_set_access_time(SESSION(session));
3063 #define MAX_MSG_NUM 50
3065 static GSList *imap_get_uncached_messages(IMAPSession *session,
3067 MsgNumberList *numlist,
3070 GSList *result = NULL;
3072 uncached_data *data = g_new0(uncached_data, 1);
3077 data->total = g_slist_length(numlist);
3078 data->ok = MAILIMAP_NO_ERROR;
3079 debug_print("messages list : %i\n", data->total);
3081 while (cur != NULL) {
3082 GSList * partial_result;
3090 while (count < MAX_MSG_NUM) {
3095 if (newlist == NULL)
3096 llast = newlist = g_slist_append(newlist, p);
3098 llast = g_slist_append(llast, p);
3099 llast = llast->next;
3109 data->session = session;
3111 data->numlist = newlist;
3114 if (prefs_common.work_offline &&
3115 !inc_offline_should_override(FALSE,
3116 _("Claws Mail needs network access in order "
3117 "to access the IMAP server."))) {
3123 (GSList *)imap_get_uncached_messages_thread(data);
3125 if (data->ok != MAILIMAP_NO_ERROR) {
3128 statusbar_progress_all(data->cur,data->total, 1);
3130 g_slist_free(newlist);
3132 result = g_slist_concat(result, partial_result);
3137 statusbar_progress_all(0,0,0);
3138 statusbar_pop_all();
3143 static void imap_delete_all_cached_messages(FolderItem *item)
3147 g_return_if_fail(item != NULL);
3148 g_return_if_fail(item->folder != NULL);
3149 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
3151 debug_print("Deleting all cached messages...\n");
3153 dir = folder_item_get_path(item);
3154 if (is_dir_exist(dir))
3155 remove_all_numbered_files(dir);
3158 debug_print("done.\n");
3161 gchar imap_get_path_separator_for_item(FolderItem *item)
3163 Folder *folder = NULL;
3164 IMAPFolder *imap_folder = NULL;
3165 IMAPSession *session = NULL;
3167 gint ok = MAILIMAP_NO_ERROR;
3170 folder = item->folder;
3175 imap_folder = IMAP_FOLDER(folder);
3180 debug_print("getting session...");
3181 session = imap_session_get(FOLDER(folder));
3182 result = imap_get_path_separator(session, imap_folder, item->path, &ok);
3186 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder, gint *ok)
3190 gchar separator = '\0';
3192 g_return_val_if_fail(session != NULL, '/');
3193 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
3195 if (r != MAILIMAP_NO_ERROR) {
3196 imap_handle_error(SESSION(session), NULL, r);
3197 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
3202 if (lep_list != NULL && clist_count(lep_list) > 0) {
3203 clistiter * iter = clist_begin(lep_list);
3204 struct mailimap_mailbox_list * mb;
3205 mb = clist_content(iter);
3207 separator = mb->mb_delimiter;
3208 debug_print("got separator: %c\n", folder->last_seen_separator);
3210 *ok = MAILIMAP_NO_ERROR;
3211 mailimap_list_result_free(lep_list);
3215 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3217 gchar separator = '/';
3218 *ok = MAILIMAP_NO_ERROR;
3219 if (folder->last_seen_separator == 0) {
3220 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "", ok);
3223 if (folder->last_seen_separator == 0) {
3224 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX", ok);
3227 if (folder->last_seen_separator != 0) {
3228 debug_print("using separator: %c\n", folder->last_seen_separator);
3229 return folder->last_seen_separator;
3235 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path, gint *ok)
3237 gchar *real_path = NULL;
3240 g_return_val_if_fail(folder != NULL, NULL);
3241 g_return_val_if_fail(path != NULL, NULL);
3243 *ok = MAILIMAP_NO_ERROR;
3245 real_path = imap_utf8_to_modified_utf7(path, FALSE);
3246 separator = imap_get_path_separator(session, folder, path, ok);
3247 if (*ok == MAILIMAP_NO_ERROR)
3248 imap_path_separator_subst(real_path, separator);
3253 static gint imap_set_message_flags(IMAPSession *session,
3254 IMAPFolderItem *item,
3255 MsgNumberList *numlist,
3264 IMAPFolder *folder = NULL;
3265 GSList *sorted_list = NULL;
3267 if (numlist == NULL || session == NULL)
3268 return MAILIMAP_ERROR_BAD_STATE;
3270 folder = IMAP_FOLDER(session->folder);
3272 sorted_list = g_slist_copy(numlist);
3273 sorted_list = g_slist_sort(sorted_list, g_int_compare);
3275 cur = g_slist_last(sorted_list);
3278 total = GPOINTER_TO_INT(cur->data);
3280 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
3282 statusbar_print_all(_("Flagging messages..."));
3284 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
3285 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
3286 struct mailimap_set_item *set_item = NULL;
3288 if (imapset->set_list)
3289 set_item = clist_content(clist_begin(imapset->set_list));
3293 if (set_item == NULL)
3296 statusbar_progress_all(set_item->set_first, total, 1);
3298 ok = imap_cmd_store(session, item, imapset,
3299 flags, tags, is_set);
3300 statusbar_progress_all(set_item->set_last, total, 1);
3301 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
3302 /* reduce max set size */
3303 folder->max_set_size /= 2;
3305 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
3310 g_slist_free(sorted_list);
3312 statusbar_progress_all(0,0,0);
3313 statusbar_pop_all();
3315 imap_lep_set_free(seq_list);
3320 typedef struct _select_data {
3321 IMAPSession *session;
3326 guint32 *uid_validity;
3330 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3332 gint *exists, gint *recent, gint *unseen,