2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
34 #include "alertpanel.h"
48 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
57 #include "procheader.h"
58 #include "prefs_account.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
66 #include "remotefolder.h"
68 #include "statusbar.h"
70 #include "imap-thread.h"
73 typedef struct _IMAPFolder IMAPFolder;
74 typedef struct _IMAPSession IMAPSession;
75 typedef struct _IMAPNameSpace IMAPNameSpace;
76 typedef struct _IMAPFolderItem IMAPFolderItem;
78 #include "prefs_account.h"
80 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
81 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
82 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
88 /* list of IMAPNameSpace */
92 gchar last_seen_separator;
101 gboolean authenticated;
110 gboolean folder_content_changed;
121 gboolean sens_update_block;
124 struct _IMAPNameSpace
130 #define IMAPBUFSIZE 8192
134 IMAP_FLAG_SEEN = 1 << 0,
135 IMAP_FLAG_ANSWERED = 1 << 1,
136 IMAP_FLAG_FLAGGED = 1 << 2,
137 IMAP_FLAG_DELETED = 1 << 3,
138 IMAP_FLAG_DRAFT = 1 << 4
141 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
142 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
143 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
144 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
145 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
148 #define IMAP4_PORT 143
149 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
150 #define IMAPS_PORT 993
153 #define IMAP_CMD_LIMIT 1000
155 struct _IMAPFolderItem
164 GHashTable *flags_set_table;
165 GHashTable *flags_unset_table;
168 gboolean should_update;
169 gboolean should_trash_cache;
172 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
173 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
175 static void imap_folder_init (Folder *folder,
179 static Folder *imap_folder_new (const gchar *name,
181 static void imap_folder_destroy (Folder *folder);
183 static IMAPSession *imap_session_new (Folder *folder,
184 const PrefsAccount *account);
185 static void imap_session_authenticate(IMAPSession *session,
186 const PrefsAccount *account);
187 static void imap_session_destroy (Session *session);
189 static gchar *imap_fetch_msg (Folder *folder,
192 static gchar *imap_fetch_msg_full (Folder *folder,
197 static void imap_remove_cached_msg (Folder *folder,
200 static gint imap_add_msg (Folder *folder,
204 static gint imap_add_msgs (Folder *folder,
207 GRelation *relation);
209 static gint imap_copy_msg (Folder *folder,
212 static gint imap_copy_msgs (Folder *folder,
214 MsgInfoList *msglist,
215 GRelation *relation);
217 static gint imap_remove_msg (Folder *folder,
220 static gint imap_remove_msgs (Folder *folder,
222 MsgInfoList *msglist,
223 GRelation *relation);
224 static gint imap_remove_all_msg (Folder *folder,
227 static gboolean imap_is_msg_changed (Folder *folder,
231 static gint imap_close (Folder *folder,
234 static gint imap_scan_tree (Folder *folder);
236 static gint imap_create_tree (Folder *folder);
238 static FolderItem *imap_create_folder (Folder *folder,
241 static gint imap_rename_folder (Folder *folder,
244 static gint imap_remove_folder (Folder *folder,
247 static FolderItem *imap_folder_item_new (Folder *folder);
248 static void imap_folder_item_destroy (Folder *folder,
251 static IMAPSession *imap_session_get (Folder *folder);
253 static gint imap_auth (IMAPSession *session,
258 static gint imap_scan_tree_recursive (IMAPSession *session,
262 static void imap_create_missing_folders (Folder *folder);
263 static FolderItem *imap_create_special_folder
265 SpecialFolderItemType stype,
268 static gint imap_do_copy_msgs (Folder *folder,
270 MsgInfoList *msglist,
271 GRelation *relation);
273 static void imap_delete_all_cached_messages (FolderItem *item);
274 static void imap_set_batch (Folder *folder,
277 static gint imap_set_message_flags (IMAPSession *session,
278 MsgNumberList *numlist,
281 static gint imap_select (IMAPSession *session,
287 guint32 *uid_validity,
289 static gint imap_status (IMAPSession *session,
292 IMAPFolderItem *item,
295 guint32 *uid_validity,
299 static gchar imap_get_path_separator (IMAPSession *session,
302 static gchar *imap_get_real_path (IMAPSession *session,
306 static void imap_synchronise (FolderItem *item, gint days);
308 static gboolean imap_is_busy (Folder *folder);
310 static void imap_free_capabilities (IMAPSession *session);
312 /* low-level IMAP4rev1 commands */
313 static gint imap_cmd_login (IMAPSession *session,
317 static gint imap_cmd_noop (IMAPSession *session);
318 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
319 static gint imap_cmd_starttls (IMAPSession *session);
321 static gint imap_cmd_select (IMAPSession *session,
326 guint32 *uid_validity,
328 static gint imap_cmd_close (IMAPSession *session);
329 static gint imap_cmd_examine (IMAPSession *session,
334 guint32 *uid_validity,
336 static gint imap_cmd_create (IMAPSession *sock,
337 const gchar *folder);
338 static gint imap_cmd_rename (IMAPSession *sock,
339 const gchar *oldfolder,
340 const gchar *newfolder);
341 static gint imap_cmd_delete (IMAPSession *session,
342 const gchar *folder);
343 static gint imap_cmd_fetch (IMAPSession *sock,
345 const gchar *filename,
348 static gint imap_cmd_append (IMAPSession *session,
349 const gchar *destfolder,
353 static gint imap_cmd_copy (IMAPSession *session,
354 struct mailimap_set * set,
355 const gchar *destfolder,
356 GRelation *uid_mapping,
357 struct mailimap_set ** source,
358 struct mailimap_set ** dest);
359 static gint imap_cmd_store (IMAPSession *session,
360 struct mailimap_set * set,
363 static gint imap_cmd_expunge (IMAPSession *session);
365 static void imap_path_separator_subst (gchar *str,
368 static gchar *imap_utf8_to_modified_utf7 (const gchar *from);
369 static gchar *imap_modified_utf7_to_utf8 (const gchar *mutf7_str);
371 static gboolean imap_rename_folder_func (GNode *node,
373 static gint imap_get_num_list (Folder *folder,
376 gboolean *old_uids_valid);
377 static GSList *imap_get_msginfos (Folder *folder,
379 GSList *msgnum_list);
380 static MsgInfo *imap_get_msginfo (Folder *folder,
383 static gboolean imap_scan_required (Folder *folder,
385 static void imap_change_flags (Folder *folder,
388 MsgPermFlags newflags);
389 static gint imap_get_flags (Folder *folder,
391 MsgInfoList *msglist,
392 GRelation *msgflags);
393 static gchar *imap_folder_get_path (Folder *folder);
394 static gchar *imap_item_get_path (Folder *folder,
396 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
399 /* data types conversion libetpan <-> claws */
400 static GSList * imap_list_from_lep(IMAPFolder * folder,
401 clist * list, const gchar * real_path, gboolean all);
402 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
403 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
404 static GSList * imap_uid_list_from_lep(clist * list);
405 static GSList * imap_uid_list_from_lep_tab(carray * list);
406 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
408 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
410 static void imap_lep_set_free(GSList *seq_list);
411 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags);
413 typedef struct _hashtable_data {
415 IMAPFolderItem *item;
418 static FolderClass imap_class;
420 typedef struct _thread_data {
425 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
430 FolderClass *imap_get_class(void)
432 if (imap_class.idstr == NULL) {
433 imap_class.type = F_IMAP;
434 imap_class.idstr = "imap";
435 imap_class.uistr = "IMAP4";
437 /* Folder functions */
438 imap_class.new_folder = imap_folder_new;
439 imap_class.destroy_folder = imap_folder_destroy;
440 imap_class.scan_tree = imap_scan_tree;
441 imap_class.create_tree = imap_create_tree;
443 /* FolderItem functions */
444 imap_class.item_new = imap_folder_item_new;
445 imap_class.item_destroy = imap_folder_item_destroy;
446 imap_class.item_get_path = imap_item_get_path;
447 imap_class.create_folder = imap_create_folder;
448 imap_class.rename_folder = imap_rename_folder;
449 imap_class.remove_folder = imap_remove_folder;
450 imap_class.close = imap_close;
451 imap_class.get_num_list = imap_get_num_list;
452 imap_class.scan_required = imap_scan_required;
453 imap_class.set_xml = folder_set_xml;
454 imap_class.get_xml = folder_get_xml;
455 imap_class.item_set_xml = imap_item_set_xml;
456 imap_class.item_get_xml = imap_item_get_xml;
458 /* Message functions */
459 imap_class.get_msginfo = imap_get_msginfo;
460 imap_class.get_msginfos = imap_get_msginfos;
461 imap_class.fetch_msg = imap_fetch_msg;
462 imap_class.fetch_msg_full = imap_fetch_msg_full;
463 imap_class.add_msg = imap_add_msg;
464 imap_class.add_msgs = imap_add_msgs;
465 imap_class.copy_msg = imap_copy_msg;
466 imap_class.copy_msgs = imap_copy_msgs;
467 imap_class.remove_msg = imap_remove_msg;
468 imap_class.remove_msgs = imap_remove_msgs;
469 imap_class.remove_all_msg = imap_remove_all_msg;
470 imap_class.is_msg_changed = imap_is_msg_changed;
471 imap_class.change_flags = imap_change_flags;
472 imap_class.get_flags = imap_get_flags;
473 imap_class.set_batch = imap_set_batch;
474 imap_class.synchronise = imap_synchronise;
475 imap_class.remove_cached_msg = imap_remove_cached_msg;
477 pthread_mutex_init(&imap_mutex, NULL);
484 static void imap_refresh_sensitivity (IMAPSession *session)
488 if (session->sens_update_block)
490 mainwin = mainwindow_get_mainwindow();
492 toolbar_main_set_sensitive(mainwin);
493 main_window_set_menu_sensitive(mainwin);
497 static void lock_session(IMAPSession *session)
500 debug_print("locking session %p (%d)\n", session, session->busy);
502 debug_print(" SESSION WAS LOCKED !! \n");
503 session->busy = TRUE;
504 imap_refresh_sensitivity(session);
506 debug_print("can't lock null session\n");
510 static void unlock_session(IMAPSession *session)
513 debug_print("unlocking session %p\n", session);
514 session->busy = FALSE;
515 imap_refresh_sensitivity(session);
517 debug_print("can't unlock null session\n");
521 static void imap_disc_session_destroy(Folder *folder)
523 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
524 IMAPSession *session = NULL;
528 session = IMAP_SESSION(rfolder->session);
531 rfolder->session = NULL;
532 log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
533 SESSION(session)->state = SESSION_DISCONNECTED;
534 session_destroy(SESSION(session));
537 static gboolean is_fatal(int libetpan_errcode)
539 switch(libetpan_errcode) {
540 case MAILIMAP_ERROR_STREAM:
541 case MAILIMAP_ERROR_PROTOCOL:
542 case MAILIMAP_ERROR_PARSE:
543 case MAILIMAP_ERROR_BAD_STATE:
550 static void imap_handle_error(Session *session, int libetpan_errcode)
552 switch(libetpan_errcode) {
553 case MAILIMAP_NO_ERROR:
555 case MAILIMAP_NO_ERROR_AUTHENTICATED:
556 log_warning(LOG_PROTOCOL, _("IMAP error: authenticated\n"));
558 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
559 log_warning(LOG_PROTOCOL, _("IMAP error: not authenticated\n"));
561 case MAILIMAP_ERROR_BAD_STATE:
562 log_warning(LOG_PROTOCOL, _("IMAP error: bad state\n"));
564 case MAILIMAP_ERROR_STREAM:
565 log_warning(LOG_PROTOCOL, _("IMAP error: stream error\n"));
567 case MAILIMAP_ERROR_PARSE:
568 log_warning(LOG_PROTOCOL, _("IMAP error: parse error "
569 "(very probably non-RFC compliance from the server)\n"));
571 case MAILIMAP_ERROR_CONNECTION_REFUSED:
572 log_warning(LOG_PROTOCOL, _("IMAP error: connection refused\n"));
574 case MAILIMAP_ERROR_MEMORY:
575 log_warning(LOG_PROTOCOL, _("IMAP error: memory error\n"));
577 case MAILIMAP_ERROR_FATAL:
578 log_warning(LOG_PROTOCOL, _("IMAP error: fatal error\n"));
580 case MAILIMAP_ERROR_PROTOCOL:
581 log_warning(LOG_PROTOCOL, _("IMAP error: protocol error"
582 "(very probably non-RFC compliance from the server)\n"));
584 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
585 log_warning(LOG_PROTOCOL, _("IMAP error: connection not accepted\n"));
587 case MAILIMAP_ERROR_APPEND:
588 log_warning(LOG_PROTOCOL, _("IMAP error: APPEND error\n"));
590 case MAILIMAP_ERROR_NOOP:
591 log_warning(LOG_PROTOCOL, _("IMAP error: NOOP error\n"));
593 case MAILIMAP_ERROR_LOGOUT:
594 log_warning(LOG_PROTOCOL, _("IMAP error: LOGOUT error\n"));
596 case MAILIMAP_ERROR_CAPABILITY:
597 log_warning(LOG_PROTOCOL, _("IMAP error: CAPABILITY error\n"));
599 case MAILIMAP_ERROR_CHECK:
600 log_warning(LOG_PROTOCOL, _("IMAP error: CHECK error\n"));
602 case MAILIMAP_ERROR_CLOSE:
603 log_warning(LOG_PROTOCOL, _("IMAP error: CLOSE error\n"));
605 case MAILIMAP_ERROR_EXPUNGE:
606 log_warning(LOG_PROTOCOL, _("IMAP error: EXPUNGE error\n"));
608 case MAILIMAP_ERROR_COPY:
609 log_warning(LOG_PROTOCOL, _("IMAP error: COPY error\n"));
611 case MAILIMAP_ERROR_UID_COPY:
612 log_warning(LOG_PROTOCOL, _("IMAP error: UID COPY error\n"));
614 case MAILIMAP_ERROR_CREATE:
615 log_warning(LOG_PROTOCOL, _("IMAP error: CREATE error\n"));
617 case MAILIMAP_ERROR_DELETE:
618 log_warning(LOG_PROTOCOL, _("IMAP error: DELETE error\n"));
620 case MAILIMAP_ERROR_EXAMINE:
621 log_warning(LOG_PROTOCOL, _("IMAP error: EXAMINE error\n"));
623 case MAILIMAP_ERROR_FETCH:
624 log_warning(LOG_PROTOCOL, _("IMAP error: FETCH error\n"));
626 case MAILIMAP_ERROR_UID_FETCH:
627 log_warning(LOG_PROTOCOL, _("IMAP error: UID FETCH error\n"));
629 case MAILIMAP_ERROR_LIST:
630 log_warning(LOG_PROTOCOL, _("IMAP error: LIST error\n"));
632 case MAILIMAP_ERROR_LOGIN:
633 log_warning(LOG_PROTOCOL, _("IMAP error: LOGIN error\n"));
635 case MAILIMAP_ERROR_LSUB:
636 log_warning(LOG_PROTOCOL, _("IMAP error: LSUB error\n"));
638 case MAILIMAP_ERROR_RENAME:
639 log_warning(LOG_PROTOCOL, _("IMAP error: RENAME error\n"));
641 case MAILIMAP_ERROR_SEARCH:
642 log_warning(LOG_PROTOCOL, _("IMAP error: SEARCH error\n"));
644 case MAILIMAP_ERROR_UID_SEARCH:
645 log_warning(LOG_PROTOCOL, _("IMAP error: UID SEARCH error\n"));
647 case MAILIMAP_ERROR_SELECT:
648 log_warning(LOG_PROTOCOL, _("IMAP error: SELECT error\n"));
650 case MAILIMAP_ERROR_STATUS:
651 log_warning(LOG_PROTOCOL, _("IMAP error: STATUS error\n"));
653 case MAILIMAP_ERROR_STORE:
654 log_warning(LOG_PROTOCOL, _("IMAP error: STORE error\n"));
656 case MAILIMAP_ERROR_UID_STORE:
657 log_warning(LOG_PROTOCOL, _("IMAP error: UID STORE error\n"));
659 case MAILIMAP_ERROR_SUBSCRIBE:
660 log_warning(LOG_PROTOCOL, _("IMAP error: SUBSCRIBE error\n"));
662 case MAILIMAP_ERROR_UNSUBSCRIBE:
663 log_warning(LOG_PROTOCOL, _("IMAP error: UNSUBSCRIBE error\n"));
665 case MAILIMAP_ERROR_STARTTLS:
666 log_warning(LOG_PROTOCOL, _("IMAP error: STARTTLS error\n"));
668 case MAILIMAP_ERROR_INVAL:
669 log_warning(LOG_PROTOCOL, _("IMAP error: INVAL error\n"));
671 case MAILIMAP_ERROR_EXTENSION:
672 log_warning(LOG_PROTOCOL, _("IMAP error: EXTENSION error\n"));
674 case MAILIMAP_ERROR_SASL:
675 log_warning(LOG_PROTOCOL, _("IMAP error: SASL error\n"));
677 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
678 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
679 case MAILIMAP_ERROR_SSL:
680 log_warning(LOG_PROTOCOL, _("IMAP error: SSL error\n"));
685 log_warning(LOG_PROTOCOL, _("IMAP error: Unknown error [%d]\n"),
690 if (session && is_fatal(libetpan_errcode)) {
691 imap_disc_session_destroy(IMAP_SESSION(session)->folder);
692 } else if (session && !is_fatal(libetpan_errcode)) {
693 if (IMAP_SESSION(session)->busy)
694 unlock_session(IMAP_SESSION(session));
698 static Folder *imap_folder_new(const gchar *name, const gchar *path)
702 folder = (Folder *)g_new0(IMAPFolder, 1);
703 folder->klass = &imap_class;
704 imap_folder_init(folder, name, path);
709 static void imap_folder_destroy(Folder *folder)
711 while (imap_folder_get_refcnt(folder) > 0)
712 gtk_main_iteration();
714 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
718 static void imap_folder_init(Folder *folder, const gchar *name,
721 folder_remote_folder_init((Folder *)folder, name, path);
722 IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
725 static FolderItem *imap_folder_item_new(Folder *folder)
727 IMAPFolderItem *item;
729 item = g_new0(IMAPFolderItem, 1);
732 item->uid_list = NULL;
734 return (FolderItem *)item;
737 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
739 IMAPFolderItem *item = (IMAPFolderItem *)_item;
741 g_return_if_fail(item != NULL);
742 g_slist_free(item->uid_list);
747 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
749 IMAPFolderItem *item = (IMAPFolderItem *)node->data;
752 g_slist_free(item->uid_list);
753 item->uid_list = NULL;
758 static void imap_reset_uid_lists(Folder *folder)
760 if(folder->node == NULL)
763 /* Destroy all uid lists and rest last uid */
764 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL);
767 static int imap_get_capabilities(IMAPSession *session)
769 struct mailimap_capability_data *capabilities = NULL;
773 if (session->capability != NULL)
774 return MAILIMAP_NO_ERROR;
776 capabilities = imap_threaded_capability(session->folder, &result);
778 if (result != MAILIMAP_NO_ERROR) {
779 imap_handle_error(SESSION(session), result);
780 return MAILIMAP_ERROR_CAPABILITY;
783 if (capabilities == NULL) {
784 return MAILIMAP_NO_ERROR;
787 for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
788 cur = clist_next(cur)) {
789 struct mailimap_capability * cap =
791 if (!cap || cap->cap_data.cap_name == NULL)
793 session->capability = g_slist_append
794 (session->capability,
795 g_strdup(cap->cap_data.cap_name));
796 debug_print("got capa %s\n", cap->cap_data.cap_name);
798 mailimap_capability_data_free(capabilities);
799 return MAILIMAP_NO_ERROR;
802 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
805 for (cur = session->capability; cur; cur = cur->next) {
806 if (!g_ascii_strcasecmp(cur->data, cap))
812 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
815 gint ok = MAILIMAP_ERROR_BAD_STATE;
816 static time_t last_login_err = 0;
817 gchar *ext_info = "";
819 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
820 imap_handle_error(SESSION(session), r);
825 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
827 case IMAP_AUTH_CRAM_MD5:
828 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
830 case IMAP_AUTH_LOGIN:
831 ok = imap_cmd_login(session, user, pass, "LOGIN");
833 case IMAP_AUTH_GSSAPI:
834 ok = imap_cmd_login(session, user, pass, "GSSAPI");
837 debug_print("capabilities:\n"
842 imap_has_capability(session, "ANONYMOUS"),
843 imap_has_capability(session, "CRAM-MD5"),
844 imap_has_capability(session, "LOGIN"),
845 imap_has_capability(session, "GSSAPI"));
846 if (imap_has_capability(session, "CRAM-MD5"))
847 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
848 if ((ok == MAILIMAP_ERROR_BAD_STATE ||
849 ok == MAILIMAP_ERROR_LOGIN) && imap_has_capability(session, "GSSAPI"))
850 ok = imap_cmd_login(session, user, pass, "GSSAPI");
851 if (ok == MAILIMAP_ERROR_BAD_STATE||
852 ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
853 ok = imap_cmd_login(session, user, pass, "LOGIN");
856 if (ok == MAILIMAP_NO_ERROR)
857 session->authenticated = TRUE;
859 if (type == IMAP_AUTH_CRAM_MD5) {
860 ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
861 "compiled with SASL support and the "
862 "CRAM-MD5 SASL plugin is installed.");
865 if (time(NULL) - last_login_err > 10) {
866 if (!prefs_common.no_recv_err_panel) {
867 alertpanel_error(_("Connection to %s failed: "
869 SESSION(session)->server, ext_info);
871 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
872 "login refused.%s\n"),
873 SESSION(session)->server, ext_info);
876 last_login_err = time(NULL);
881 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
883 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
884 /* Check if this is the first try to establish a
885 connection, if yes we don't try to reconnect */
886 debug_print("reconnecting\n");
887 if (rfolder->session == NULL) {
888 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
889 folder->account->recv_server);
890 session_destroy(SESSION(session));
893 rfolder->session = NULL;
894 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
895 " disconnected. Reconnecting...\n"),
896 folder->account->recv_server);
897 statusbar_print_all(_("IMAP4 connection to %s has been"
898 " disconnected. Reconnecting...\n"),
899 folder->account->recv_server);
900 SESSION(session)->state = SESSION_DISCONNECTED;
901 session_destroy(SESSION(session));
902 /* Clear folders session to make imap_session_get create
903 a new session, because of rfolder->session == NULL
904 it will not try to reconnect again and so avoid an
906 debug_print("getting session...\n");
907 session = imap_session_get(folder);
908 rfolder->session = SESSION(session);
914 static IMAPSession *imap_session_get(Folder *folder)
916 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
917 IMAPSession *session = NULL;
918 gint r = MAILIMAP_NO_ERROR;
920 g_return_val_if_fail(folder != NULL, NULL);
921 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
922 g_return_val_if_fail(folder->account != NULL, NULL);
924 if (prefs_common.work_offline &&
925 !inc_offline_should_override(FALSE,
926 _("Claws Mail needs network access in order "
927 "to access the IMAP server."))) {
931 /* Make sure we have a session */
932 if (rfolder->session != NULL) {
933 session = IMAP_SESSION(rfolder->session);
934 } else if (rfolder->connecting) {
935 debug_print("already connecting\n");
938 imap_reset_uid_lists(folder);
939 if (time(NULL) - rfolder->last_failure <= 2)
941 rfolder->connecting = TRUE;
942 session = imap_session_new(folder, folder->account);
944 if(session == NULL) {
945 rfolder->last_failure = time(NULL);
946 rfolder->connecting = FALSE;
950 /* Make sure session is authenticated */
951 if (!IMAP_SESSION(session)->authenticated)
952 imap_session_authenticate(IMAP_SESSION(session), folder->account);
954 if (!IMAP_SESSION(session)->authenticated) {
955 imap_threaded_disconnect(session->folder);
956 rfolder->session = NULL;
957 SESSION(session)->state = SESSION_DISCONNECTED;
958 session_destroy(SESSION(session));
959 rfolder->last_failure = time(NULL);
960 rfolder->connecting = FALSE;
964 /* I think the point of this code is to avoid sending a
965 * keepalive if we've used the session recently and therefore
966 * think it's still alive. Unfortunately, most of the code
967 * does not yet check for errors on the socket, and so if the
968 * connection drops we don't notice until the timeout expires.
969 * A better solution than sending a NOOP every time would be
970 * for every command to be prepared to retry until it is
971 * successfully sent. -- mbp */
972 if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
973 /* verify that the session is still alive */
974 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
975 debug_print("disconnected!\n");
977 session = imap_reconnect_if_possible(folder, session);
979 session = imap_session_get(folder);
982 session->cancelled = FALSE;
985 rfolder->session = SESSION(session);
986 rfolder->connecting = FALSE;
988 return IMAP_SESSION(session);
991 static IMAPSession *imap_session_new(Folder * folder,
992 const PrefsAccount *account)
994 IMAPSession *session;
997 int authenticated = FALSE;
999 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1000 /* FIXME: IMAP over SSL only... */
1003 port = account->set_imapport ? account->imapport
1004 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1005 ssl_type = account->ssl_imap;
1007 if (account->ssl_imap != SSL_NONE) {
1008 if (alertpanel_full(_("Insecure connection"),
1009 _("This connection is configured to be secured "
1010 "using SSL, but SSL is not available in this "
1011 "build of Claws Mail. \n\n"
1012 "Do you want to continue connecting to this "
1013 "server? The communication would not be "
1015 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
1016 NULL, FALSE, NULL, ALERT_WARNING,
1017 G_ALERTDEFAULT) != G_ALERTALTERNATE)
1020 port = account->set_imapport ? account->imapport
1025 statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
1026 if (account->set_tunnelcmd) {
1027 r = imap_threaded_connect_cmd(folder,
1029 account->recv_server,
1033 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1034 if (ssl_type == SSL_TUNNEL) {
1035 r = imap_threaded_connect_ssl(folder,
1036 account->recv_server,
1042 r = imap_threaded_connect(folder,
1043 account->recv_server,
1048 statuswindow_pop_all();
1049 if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1050 authenticated = TRUE;
1052 else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1053 authenticated = FALSE;
1056 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
1057 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1058 if (r == MAILIMAP_ERROR_SSL)
1059 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1063 imap_handle_error(NULL, r);
1065 if(!prefs_common.no_recv_err_panel) {
1066 alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1067 account->recv_server, port);
1069 log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1070 account->recv_server, port);
1076 session = g_new0(IMAPSession, 1);
1077 session_init(SESSION(session));
1078 SESSION(session)->type = SESSION_IMAP;
1079 SESSION(session)->server = g_strdup(account->recv_server);
1080 SESSION(session)->sock = NULL;
1082 SESSION(session)->destroy = imap_session_destroy;
1084 session->capability = NULL;
1086 session->authenticated = authenticated;
1087 session->mbox = NULL;
1088 session->exists = 0;
1089 session->recent = 0;
1090 session->expunge = 0;
1091 session->cmd_count = 0;
1092 session->folder = folder;
1093 IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1095 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1096 if (account->ssl_imap == SSL_STARTTLS) {
1099 ok = imap_cmd_starttls(session);
1100 if (ok != MAILIMAP_NO_ERROR) {
1101 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1102 session_destroy(SESSION(session));
1106 imap_free_capabilities(session);
1107 session->authenticated = FALSE;
1108 session->uidplus = FALSE;
1109 session->cmd_count = 1;
1112 log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1113 (session->authenticated) ? "pre" : "un");
1118 static void imap_session_authenticate(IMAPSession *session,
1119 const PrefsAccount *account)
1121 gchar *pass, *acc_pass;
1122 gboolean failed = FALSE;
1124 g_return_if_fail(account->userid != NULL);
1125 acc_pass = account->passwd;
1128 if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
1130 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
1133 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
1135 } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
1138 statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
1139 account->recv_server);
1140 if (imap_auth(session, account->userid, pass, account->imap_auth_type) != MAILIMAP_NO_ERROR) {
1141 statusbar_pop_all();
1148 if (prefs_common.no_recv_err_panel) {
1149 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
1150 mainwindow_show_error();
1152 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1158 statuswindow_pop_all();
1159 session->authenticated = TRUE;
1163 static void imap_session_destroy(Session *session)
1165 if (session->state != SESSION_DISCONNECTED)
1166 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1168 imap_free_capabilities(IMAP_SESSION(session));
1169 g_free(IMAP_SESSION(session)->mbox);
1170 sock_close(session->sock);
1171 session->sock = NULL;
1174 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1176 return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1179 static guint get_file_size_with_crs(const gchar *filename)
1185 if (filename == NULL)
1188 fp = fopen(filename, "rb");
1192 while (fgets(buf, sizeof (buf), fp) != NULL) {
1194 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1202 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1204 gchar *path, *filename;
1206 path = folder_item_get_path(item);
1208 if (!is_dir_exist(path)) {
1213 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1216 if (is_file_exist(filename)) {
1222 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1223 gboolean headers, gboolean body)
1225 gchar *path, *filename;
1226 IMAPSession *session;
1229 g_return_val_if_fail(folder != NULL, NULL);
1230 g_return_val_if_fail(item != NULL, NULL);
1235 path = folder_item_get_path(item);
1236 if (!is_dir_exist(path))
1237 make_dir_hier(path);
1238 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1240 debug_print("trying to fetch cached %s\n", filename);
1241 if (is_file_exist(filename)) {
1242 /* see whether the local file represents the whole message
1243 * or not. As the IMAP server reports size with \r chars,
1244 * we have to update the local file (UNIX \n only) size */
1245 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1246 guint have_size = -1;
1249 debug_print("message %d has been already %scached.\n", uid,
1250 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1252 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1253 have_size = get_file_size_with_crs(filename);
1254 if (cached && (cached->size <= have_size || !body)) {
1255 procmsg_msginfo_free(cached);
1256 ok = file_strip_crs(filename);
1257 if (ok == 0 && cached && cached->size <= have_size) {
1258 /* we have it all and stripped */
1259 debug_print("...fully cached in fact; setting flag.\n");
1260 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1263 } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1264 debug_print("message not cached and file recent, considering file complete\n");
1265 ok = file_strip_crs(filename);
1269 procmsg_msginfo_free(cached);
1272 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1273 procmsg_msginfo_free(cached);
1277 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1279 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1280 procmsg_msginfo_free(cached);
1284 debug_print("getting session...\n");
1285 session = imap_session_get(folder);
1291 session_set_access_time(SESSION(session));
1292 lock_session(session); /* unlocked later in the function */
1294 debug_print("IMAP fetching messages\n");
1295 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1296 NULL, NULL, NULL, NULL, FALSE);
1297 if (ok != MAILIMAP_NO_ERROR) {
1298 g_warning("can't select mailbox %s\n", item->path);
1303 session_set_access_time(SESSION(session));
1305 debug_print("getting message %d...\n", uid);
1306 ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1308 if (ok != MAILIMAP_NO_ERROR) {
1309 g_warning("can't fetch message %d\n", uid);
1314 session_set_access_time(SESSION(session));
1315 unlock_session(session);
1317 ok = file_strip_crs(filename);
1319 if (ok == 0 && headers && body) {
1320 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1322 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1323 procmsg_msginfo_free(cached);
1325 } else if (ok == -1) {
1326 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1328 procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1329 procmsg_msginfo_free(cached);
1335 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1337 gchar *path, *filename;
1339 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1344 if (MSG_IS_FULLY_CACHED(cached->flags)) {
1345 procmsg_msginfo_free(cached);
1348 path = folder_item_get_path(item);
1349 if (!is_dir_exist(path))
1352 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1354 if (is_file_exist(filename)) {
1355 if (cached && cached->total_size == cached->size) {
1358 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1361 size = get_file_size_with_crs(filename);
1364 if (cached && size >= cached->size) {
1365 cached->total_size = cached->size;
1366 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1367 procmsg_msginfo_free(cached);
1371 procmsg_msginfo_free(cached);
1375 void imap_cache_msg(FolderItem *item, gint msgnum)
1377 Folder *folder = NULL;
1381 folder = item->folder;
1383 if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1384 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1385 debug_print("fetched %s\n", tmp);
1390 static gint imap_add_msg(Folder *folder, FolderItem *dest,
1391 const gchar *file, MsgFlags *flags)
1395 MsgFileInfo fileinfo;
1397 g_return_val_if_fail(file != NULL, -1);
1399 fileinfo.msginfo = NULL;
1400 fileinfo.file = (gchar *)file;
1401 fileinfo.flags = flags;
1402 file_list.data = &fileinfo;
1403 file_list.next = NULL;
1405 ret = imap_add_msgs(folder, dest, &file_list, NULL);
1409 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1410 GRelation *relation)
1413 IMAPSession *session;
1414 guint32 last_uid = 0;
1416 MsgFileInfo *fileinfo;
1417 gint ok = MAILIMAP_NO_ERROR;
1418 gint curnum = 0, total = 0;
1419 gboolean missing_uids = FALSE;
1421 g_return_val_if_fail(folder != NULL, -1);
1422 g_return_val_if_fail(dest != NULL, -1);
1423 g_return_val_if_fail(file_list != NULL, -1);
1425 debug_print("getting session...\n");
1426 session = imap_session_get(folder);
1430 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1432 statusbar_print_all(_("Adding messages..."));
1433 total = g_slist_length(file_list);
1434 for (cur = file_list; cur != NULL; cur = cur->next) {
1435 IMAPFlags iflags = 0;
1436 guint32 new_uid = 0;
1437 gchar *real_file = NULL;
1438 fileinfo = (MsgFileInfo *)cur->data;
1440 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1443 if (fileinfo->flags) {
1444 if (MSG_IS_MARKED(*fileinfo->flags))
1445 iflags |= IMAP_FLAG_FLAGGED;
1446 if (MSG_IS_REPLIED(*fileinfo->flags))
1447 iflags |= IMAP_FLAG_ANSWERED;
1448 if (!MSG_IS_UNREAD(*fileinfo->flags))
1449 iflags |= IMAP_FLAG_SEEN;
1452 if (real_file == NULL)
1453 real_file = g_strdup(fileinfo->file);
1455 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1456 folder_has_parent_of_type(dest, F_OUTBOX) ||
1457 folder_has_parent_of_type(dest, F_DRAFT) ||
1458 folder_has_parent_of_type(dest, F_TRASH))
1459 iflags |= IMAP_FLAG_SEEN;
1461 ok = imap_cmd_append(session, destdir, real_file, iflags,
1464 if (ok != MAILIMAP_NO_ERROR) {
1465 g_warning("can't append message %s\n", real_file);
1468 statusbar_progress_all(0,0,0);
1469 statusbar_pop_all();
1472 debug_print("appended new message as %d\n", new_uid);
1473 /* put the local file in the imapcache, so that we don't
1474 * have to fetch it back later. */
1477 missing_uids = TRUE;
1478 debug_print("Missing UID (0)\n");
1481 gchar *cache_path = folder_item_get_path(dest);
1482 if (!is_dir_exist(cache_path))
1483 make_dir_hier(cache_path);
1484 if (is_dir_exist(cache_path)) {
1485 gchar *cache_file = g_strconcat(
1486 cache_path, G_DIR_SEPARATOR_S,
1487 itos(new_uid), NULL);
1488 copy_file(real_file, cache_file, TRUE);
1489 debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1496 if (relation != NULL)
1497 g_relation_insert(relation, fileinfo->msginfo != NULL ?
1498 (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1499 GINT_TO_POINTER(new_uid));
1500 if (last_uid < new_uid) {
1507 statusbar_progress_all(0,0,0);
1508 statusbar_pop_all();
1513 imap_scan_required(folder, dest);
1515 session = imap_session_get(folder);
1521 ok = imap_select(session, IMAP_FOLDER(folder), dest->path,
1522 &a, NULL, NULL, NULL, FALSE);
1527 static GSList *flatten_mailimap_set(struct mailimap_set * set)
1529 GSList *result = NULL;
1534 for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1535 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1536 start = item->set_first;
1537 end = item->set_last;
1538 for (t = start; t <= end; t++) {
1539 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1542 result = g_slist_reverse(result);
1543 if (debug_get_mode()) {
1544 debug_print("flat imap set: ");
1545 for (cur = result; cur; cur = cur->next) {
1546 debug_print("%d ", GPOINTER_TO_INT(cur->data));
1553 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
1554 MsgInfoList *msglist, GRelation *relation)
1558 GSList *seq_list, *cur;
1560 IMAPSession *session;
1561 gint ok = MAILIMAP_NO_ERROR;
1562 GRelation *uid_mapping;
1564 gboolean single = FALSE;
1566 g_return_val_if_fail(folder != NULL, -1);
1567 g_return_val_if_fail(dest != NULL, -1);
1568 g_return_val_if_fail(msglist != NULL, -1);
1570 debug_print("getting session...\n");
1571 session = imap_session_get(folder);
1577 msginfo = (MsgInfo *)msglist->data;
1578 if (msglist->next == NULL)
1580 src = msginfo->folder;
1582 g_warning("the src folder is identical to the dest.\n");
1586 if (src->folder != dest->folder) {
1587 GSList *infolist = NULL, *cur;
1589 for (cur = msglist; cur; cur = cur->next) {
1590 msginfo = (MsgInfo *)cur->data;
1591 MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1592 fileinfo->file = procmsg_get_message_file(msginfo);
1593 fileinfo->flags = &(msginfo->flags);
1594 infolist = g_slist_prepend(infolist, fileinfo);
1596 infolist = g_slist_reverse(infolist);
1597 res = folder_item_add_msgs(dest, infolist, FALSE);
1598 for (cur = infolist; cur; cur = cur->next) {
1599 MsgFileInfo *info = (MsgFileInfo *)cur->data;
1603 g_slist_free(infolist);
1607 lock_session(session); /* unlocked later in the function */
1609 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1610 NULL, NULL, NULL, NULL, FALSE);
1611 if (ok != MAILIMAP_NO_ERROR) {
1615 unlock_session(session);
1617 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1618 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1619 uid_mapping = g_relation_new(2);
1620 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1622 statusbar_print_all(_("Copying messages..."));
1623 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1624 struct mailimap_set * seq_set;
1625 struct mailimap_set * source = NULL;
1626 struct mailimap_set * dest = NULL;
1627 seq_set = cur->data;
1629 debug_print("Copying messages from %s to %s ...\n",
1630 src->path, destdir);
1632 lock_session(session); /* unlocked later in the function */
1633 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1640 if (ok == MAILIMAP_NO_ERROR) {
1641 unlock_session(session);
1642 if (relation && source && dest) {
1643 GSList *s_list = flatten_mailimap_set(source);
1644 GSList *d_list = flatten_mailimap_set(dest);
1645 GSList *s_cur, *d_cur;
1646 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1648 for (s_cur = s_list, d_cur = d_list;
1650 s_cur = s_cur->next, d_cur = d_cur->next) {
1651 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1655 debug_print("hhhmm, source list length != dest list length.\n");
1657 g_slist_free(s_list);
1658 g_slist_free(d_list);
1664 mailimap_set_free(source);
1666 mailimap_set_free(dest);
1668 if (ok != MAILIMAP_NO_ERROR) {
1669 g_relation_destroy(uid_mapping);
1670 imap_lep_set_free(seq_list);
1671 statusbar_pop_all();
1676 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1677 MsgInfo *msginfo = (MsgInfo *)cur->data;
1680 tuples = g_relation_select(uid_mapping,
1681 GINT_TO_POINTER(msginfo->msgnum),
1683 if (tuples->len > 0) {
1684 gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1685 g_relation_insert(relation, msginfo,
1686 GINT_TO_POINTER(num));
1689 debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1690 /* put the local file in the imapcache, so that we don't
1691 * have to fetch it back later. */
1693 gchar *cache_path = folder_item_get_path(msginfo->folder);
1694 gchar *real_file = g_strconcat(
1695 cache_path, G_DIR_SEPARATOR_S,
1696 itos(msginfo->msgnum), NULL);
1697 gchar *cache_file = NULL;
1699 cache_path = folder_item_get_path(dest);
1700 cache_file = g_strconcat(
1701 cache_path, G_DIR_SEPARATOR_S,
1703 if (!is_dir_exist(cache_path))
1704 make_dir_hier(cache_path);
1705 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1706 copy_file(real_file, cache_file, TRUE);
1707 debug_print("copied to cache: %s\n", cache_file);
1714 g_relation_insert(relation, msginfo,
1715 GINT_TO_POINTER(0));
1716 g_tuples_destroy(tuples);
1718 statusbar_pop_all();
1720 g_relation_destroy(uid_mapping);
1721 imap_lep_set_free(seq_list);
1725 IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1726 IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1727 g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1728 IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1730 imap_scan_required(folder, dest);
1731 if (ok == MAILIMAP_NO_ERROR)
1737 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1741 g_return_val_if_fail(msginfo != NULL, -1);
1743 msglist.data = msginfo;
1744 msglist.next = NULL;
1746 return imap_copy_msgs(folder, dest, &msglist, NULL);
1749 static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
1750 MsgInfoList *msglist, GRelation *relation)
1755 g_return_val_if_fail(folder != NULL, -1);
1756 g_return_val_if_fail(dest != NULL, -1);
1757 g_return_val_if_fail(msglist != NULL, -1);
1759 msginfo = (MsgInfo *)msglist->data;
1760 g_return_val_if_fail(msginfo->folder != NULL, -1);
1762 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1767 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
1768 MsgInfoList *msglist, GRelation *relation)
1770 gchar *destdir, *dir;
1771 GSList *numlist = NULL, *cur;
1773 IMAPSession *session;
1774 gint ok = MAILIMAP_NO_ERROR;
1775 GRelation *uid_mapping;
1777 g_return_val_if_fail(folder != NULL, -1);
1778 g_return_val_if_fail(dest != NULL, -1);
1779 g_return_val_if_fail(msglist != NULL, -1);
1781 debug_print("getting session...\n");
1782 session = imap_session_get(folder);
1787 lock_session(session); /* unlocked later in the function */
1789 msginfo = (MsgInfo *)msglist->data;
1791 ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1792 NULL, NULL, NULL, NULL, FALSE);
1793 if (ok != MAILIMAP_NO_ERROR) {
1797 destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1798 for (cur = msglist; cur; cur = cur->next) {
1799 msginfo = (MsgInfo *)cur->data;
1800 if (!MSG_IS_DELETED(msginfo->flags))
1801 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1803 numlist = g_slist_reverse(numlist);
1805 uid_mapping = g_relation_new(2);
1806 g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1808 if (numlist != NULL) {
1809 ok = imap_set_message_flags
1810 (session, numlist, IMAP_FLAG_DELETED, TRUE);
1811 if (ok != MAILIMAP_NO_ERROR) {
1812 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1815 } /* else we just need to expunge */
1816 ok = imap_cmd_expunge(session);
1817 if (ok != MAILIMAP_NO_ERROR) {
1818 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1822 session->folder_content_changed = TRUE;
1823 unlock_session(session);
1825 dir = folder_item_get_path(msginfo->folder);
1826 if (is_dir_exist(dir)) {
1827 for (cur = msglist; cur; cur = cur->next) {
1828 msginfo = (MsgInfo *)cur->data;
1829 remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1834 g_relation_destroy(uid_mapping);
1835 g_slist_free(numlist);
1837 imap_scan_required(folder, dest);
1840 if (ok == MAILIMAP_NO_ERROR)
1846 static gint imap_remove_msgs(Folder *folder, FolderItem *dest,
1847 MsgInfoList *msglist, GRelation *relation)
1851 g_return_val_if_fail(folder != NULL, -1);
1852 g_return_val_if_fail(dest != NULL, -1);
1853 if (msglist == NULL)
1856 msginfo = (MsgInfo *)msglist->data;
1857 g_return_val_if_fail(msginfo->folder != NULL, -1);
1859 return imap_do_remove_msgs(folder, dest, msglist, relation);
1862 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1864 GSList *list = folder_item_get_msg_list(item);
1865 gint res = imap_remove_msgs(folder, item, list, NULL);
1866 procmsg_msg_list_free(list);
1870 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
1873 /* TODO: properly implement this method */
1877 static gint imap_close(Folder *folder, FolderItem *item)
1882 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
1884 FolderItem *item = NULL;
1885 IMAPSession *session;
1886 gchar *root_folder = NULL;
1888 g_return_val_if_fail(folder != NULL, -1);
1889 g_return_val_if_fail(folder->account != NULL, -1);
1891 debug_print("getting session...\n");
1892 session = imap_session_get(folder);
1894 if (!folder->node) {
1895 folder_tree_destroy(folder);
1896 item = folder_item_new(folder, folder->name, NULL);
1897 item->folder = folder;
1898 folder->node = item->node = g_node_new(item);
1903 if (folder->account->imap_dir && *folder->account->imap_dir) {
1908 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
1909 extract_quote(root_folder, '"');
1910 subst_char(root_folder,
1911 imap_get_path_separator(session, IMAP_FOLDER(folder),
1914 strtailchomp(root_folder, '/');
1915 real_path = imap_get_real_path
1916 (session, IMAP_FOLDER(folder), root_folder);
1917 debug_print("IMAP root directory: %s\n", real_path);
1919 /* check if root directory exist */
1921 r = imap_threaded_list(session->folder, "", real_path,
1924 if (r != MAILIMAP_NO_ERROR)
1925 imap_handle_error(SESSION(session), r);
1927 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
1928 if (!folder->node) {
1929 item = folder_item_new(folder, folder->name, NULL);
1930 item->folder = folder;
1931 folder->node = item->node = g_node_new(item);
1935 mailimap_list_result_free(lep_list);
1941 item = FOLDER_ITEM(folder->node->data);
1943 if (item && !item->path && root_folder) {
1944 item->path = g_strdup(root_folder);
1947 if (!item || ((item->path || root_folder) &&
1948 strcmp2(item->path, root_folder) != 0)) {
1949 folder_tree_destroy(folder);
1950 item = folder_item_new(folder, folder->name, root_folder);
1951 item->folder = folder;
1952 folder->node = item->node = g_node_new(item);
1955 imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
1956 imap_create_missing_folders(folder);
1961 static gint imap_scan_tree(Folder *folder)
1963 gboolean subs_only = FALSE;
1964 if (folder->account) {
1965 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
1966 subs_only = folder->account->imap_subsonly;
1968 return imap_scan_tree_real(folder, subs_only);
1971 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
1974 IMAPFolder *imapfolder;
1975 FolderItem *new_item;
1976 GSList *item_list, *cur;
1979 gchar *wildcard_path;
1985 g_return_val_if_fail(item != NULL, -1);
1986 g_return_val_if_fail(item->folder != NULL, -1);
1987 g_return_val_if_fail(item->no_sub == FALSE, -1);
1989 folder = item->folder;
1990 imapfolder = IMAP_FOLDER(folder);
1992 separator = imap_get_path_separator(session, imapfolder, item->path);
1994 if (folder->ui_func)
1995 folder->ui_func(folder, item, folder->ui_func_data);
1998 wildcard[0] = separator;
2001 real_path = imap_get_real_path(session, imapfolder, item->path);
2005 real_path = g_strdup("");
2008 Xstrcat_a(wildcard_path, real_path, wildcard,
2009 {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2013 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2015 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2017 if (r != MAILIMAP_NO_ERROR) {
2018 imap_handle_error(SESSION(session), r);
2022 item_list = imap_list_from_lep(imapfolder,
2023 lep_list, real_path, FALSE);
2024 mailimap_list_result_free(lep_list);
2029 node = item->node->children;
2030 while (node != NULL) {
2031 FolderItem *old_item = FOLDER_ITEM(node->data);
2032 GNode *next = node->next;
2035 for (cur = item_list; cur != NULL; cur = cur->next) {
2036 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2037 if (!strcmp2(old_item->path, cur_item->path)) {
2038 new_item = cur_item;
2043 if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2044 debug_print("not removing INBOX\n");
2046 debug_print("folder '%s' not found. removing...\n",
2048 folder_item_remove(old_item);
2051 old_item->no_sub = new_item->no_sub;
2052 old_item->no_select = new_item->no_select;
2053 if (old_item->no_sub == TRUE && node->children) {
2054 debug_print("folder '%s' doesn't have "
2055 "subfolders. removing...\n",
2057 folder_item_remove_children(old_item);
2064 for (cur = item_list; cur != NULL; cur = cur->next) {
2065 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2068 for (node = item->node->children; node != NULL;
2069 node = node->next) {
2070 if (!strcmp2(FOLDER_ITEM(node->data)->path,
2072 new_item = FOLDER_ITEM(node->data);
2073 folder_item_destroy(cur_item);
2079 new_item = cur_item;
2080 debug_print("new folder '%s' found.\n", new_item->path);
2081 folder_item_append(item, new_item);
2084 if (!strcmp(new_item->path, "INBOX")) {
2085 new_item->stype = F_INBOX;
2086 folder->inbox = new_item;
2087 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2090 base = g_path_get_basename(new_item->path);
2092 if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2093 new_item->stype = F_OUTBOX;
2094 folder->outbox = new_item;
2095 } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2096 new_item->stype = F_DRAFT;
2097 folder->draft = new_item;
2098 } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2099 new_item->stype = F_QUEUE;
2100 folder->queue = new_item;
2101 } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2102 new_item->stype = F_TRASH;
2103 folder->trash = new_item;
2108 if (new_item->no_sub == FALSE)
2109 imap_scan_tree_recursive(session, new_item, subs_only);
2112 g_slist_free(item_list);
2114 return MAILIMAP_NO_ERROR;
2117 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2119 IMAPSession *session = imap_session_get(folder);
2121 gchar *wildcard_path;
2125 GSList *item_list = NULL, *cur;
2126 GList *child_list = NULL, *tmplist = NULL;
2127 GSList *sub_list = NULL;
2133 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2136 wildcard[0] = separator;
2139 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2143 real_path = g_strdup("");
2146 Xstrcat_a(wildcard_path, real_path, wildcard,
2147 {g_free(real_path); return NULL;});
2151 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2152 item->path?item->path:item->name);
2154 statusbar_print_all(_("Looking for subfolders of %s..."),
2155 item->path?item->path:item->name);
2157 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2159 statusbar_pop_all();
2162 item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2163 lep_list, real_path, FALSE);
2164 mailimap_list_result_free(lep_list);
2166 for (cur = item_list; cur != NULL; cur = cur->next) {
2167 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2169 tmplist = imap_scan_subtree(folder, cur_item,
2170 unsubs_only, recursive);
2172 child_list = g_list_concat(child_list, tmplist);
2174 child_list = g_list_prepend(child_list,
2175 imap_get_real_path(session,
2176 IMAP_FOLDER(folder), cur_item->path));
2178 folder_item_destroy(cur_item);
2180 child_list = g_list_reverse(child_list);
2181 g_slist_free(item_list);
2184 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2186 statusbar_pop_all();
2189 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2190 lep_list, real_path, FALSE);
2191 mailimap_list_result_free(lep_list);
2193 for (cur = sub_list; cur != NULL; cur = cur->next) {
2194 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2195 GList *oldlitem = NULL;
2196 gchar *tmp = imap_get_real_path(session,
2197 IMAP_FOLDER(folder), cur_item->path);
2198 folder_item_destroy(cur_item);
2199 oldlitem = g_list_find_custom(
2200 child_list, tmp, (GCompareFunc)strcmp2);
2202 child_list = g_list_remove_link(child_list, oldlitem);
2203 g_free(oldlitem->data);
2204 g_list_free(oldlitem);
2210 statusbar_pop_all();
2215 static gint imap_create_tree(Folder *folder)
2217 g_return_val_if_fail(folder != NULL, -1);
2218 g_return_val_if_fail(folder->node != NULL, -1);
2219 g_return_val_if_fail(folder->node->data != NULL, -1);
2220 g_return_val_if_fail(folder->account != NULL, -1);
2222 imap_scan_tree(folder);
2223 imap_create_missing_folders(folder);
2228 static void imap_create_missing_folders(Folder *folder)
2230 g_return_if_fail(folder != NULL);
2233 folder->inbox = imap_create_special_folder
2234 (folder, F_INBOX, "INBOX");
2236 folder->trash = imap_create_special_folder
2237 (folder, F_TRASH, "Trash");
2239 folder->queue = imap_create_special_folder
2240 (folder, F_QUEUE, "Queue");
2241 if (!folder->outbox)
2242 folder->outbox = imap_create_special_folder
2243 (folder, F_OUTBOX, "Sent");
2245 folder->draft = imap_create_special_folder
2246 (folder, F_DRAFT, "Drafts");
2249 static FolderItem *imap_create_special_folder(Folder *folder,
2250 SpecialFolderItemType stype,
2254 FolderItem *new_item;
2256 g_return_val_if_fail(folder != NULL, NULL);
2257 g_return_val_if_fail(folder->node != NULL, NULL);
2258 g_return_val_if_fail(folder->node->data != NULL, NULL);
2259 g_return_val_if_fail(folder->account != NULL, NULL);
2260 g_return_val_if_fail(name != NULL, NULL);
2262 item = FOLDER_ITEM(folder->node->data);
2263 new_item = imap_create_folder(folder, item, name);
2266 g_warning("Can't create '%s'\n", name);
2267 if (!folder->inbox) return NULL;
2269 new_item = imap_create_folder(folder, folder->inbox, name);
2271 g_warning("Can't create '%s' under INBOX\n", name);
2273 new_item->stype = stype;
2275 new_item->stype = stype;
2280 static gchar *imap_folder_get_path(Folder *folder)
2284 g_return_val_if_fail(folder != NULL, NULL);
2285 g_return_val_if_fail(folder->account != NULL, NULL);
2287 folder_path = g_strconcat(get_imap_cache_dir(),
2289 folder->account->recv_server,
2291 folder->account->userid,
2297 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2299 gchar *folder_path, *path;
2301 g_return_val_if_fail(folder != NULL, NULL);
2302 g_return_val_if_fail(item != NULL, NULL);
2303 folder_path = imap_folder_get_path(folder);
2305 g_return_val_if_fail(folder_path != NULL, NULL);
2306 if (folder_path[0] == G_DIR_SEPARATOR) {
2308 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2311 path = g_strdup(folder_path);
2314 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2315 folder_path, G_DIR_SEPARATOR_S,
2318 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2321 g_free(folder_path);
2326 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2329 gchar *dirpath, *imap_path;
2330 IMAPSession *session;
2331 FolderItem *new_item;
2336 gboolean no_select = FALSE, no_sub = FALSE;
2337 gboolean exist = FALSE;
2339 g_return_val_if_fail(folder != NULL, NULL);
2340 g_return_val_if_fail(folder->account != NULL, NULL);
2341 g_return_val_if_fail(parent != NULL, NULL);
2342 g_return_val_if_fail(name != NULL, NULL);
2344 debug_print("getting session...\n");
2345 session = imap_session_get(folder);
2350 if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2351 dirpath = g_strdup(name);
2352 }else if (parent->path)
2353 dirpath = g_strconcat(parent->path, "/", name, NULL);
2354 else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2355 dirpath = g_strdup(name);
2356 else if (folder->account->imap_dir && *folder->account->imap_dir) {
2359 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2360 strtailchomp(imap_dir, '/');
2361 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2363 dirpath = g_strdup(name);
2367 /* keep trailing directory separator to create a folder that contains
2369 imap_path = imap_utf8_to_modified_utf7(dirpath);
2371 strtailchomp(dirpath, '/');
2372 Xstrdup_a(new_name, name, {
2376 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2377 imap_path_separator_subst(imap_path, separator);
2378 /* remove trailing / for display */
2379 strtailchomp(new_name, '/');
2381 if (strcmp(dirpath, "INBOX") != 0) {
2386 argbuf = g_ptr_array_new();
2387 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2388 if (r != MAILIMAP_NO_ERROR) {
2389 imap_handle_error(SESSION(session), r);
2390 log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2393 ptr_array_free_strings(argbuf);
2394 g_ptr_array_free(argbuf, TRUE);
2398 if (clist_count(lep_list) > 0)
2400 mailimap_list_result_free(lep_list);
2403 ok = imap_cmd_create(session, imap_path);
2404 if (ok != MAILIMAP_NO_ERROR) {
2405 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2410 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2411 if (r == MAILIMAP_NO_ERROR) {
2412 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2413 lep_list, dirpath, TRUE);
2415 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2416 no_select = cur_item->no_select;
2417 no_sub = cur_item->no_sub;
2418 g_slist_free(item_list);
2420 mailimap_list_result_free(lep_list);
2422 imap_handle_error(SESSION(session), r);
2425 imap_threaded_subscribe(folder, imap_path, TRUE);
2429 /* just get flags */
2430 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2431 if (r == MAILIMAP_NO_ERROR) {
2432 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2433 lep_list, dirpath, TRUE);
2435 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2436 no_select = cur_item->no_select;
2437 no_sub = cur_item->no_sub;
2438 g_slist_free(item_list);
2440 mailimap_list_result_free(lep_list);
2442 imap_handle_error(SESSION(session), r);
2446 new_item = folder_item_new(folder, new_name, dirpath);
2447 new_item->no_select = no_select;
2448 new_item->no_sub = no_sub;
2449 folder_item_append(parent, new_item);
2453 dirpath = folder_item_get_path(new_item);
2454 if (!is_dir_exist(dirpath))
2455 make_dir_hier(dirpath);
2459 /* folder existed, scan it */
2460 imap_scan_required(folder, new_item);
2461 folder_item_scan_full(new_item, FALSE);
2467 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2472 gchar *real_oldpath;
2473 gchar *real_newpath;
2475 gchar *old_cache_dir;
2476 gchar *new_cache_dir;
2477 IMAPSession *session;
2480 gint exists, recent, unseen;
2481 guint32 uid_validity;
2483 g_return_val_if_fail(folder != NULL, -1);
2484 g_return_val_if_fail(item != NULL, -1);
2485 g_return_val_if_fail(item->path != NULL, -1);
2486 g_return_val_if_fail(name != NULL, -1);
2488 debug_print("getting session...\n");
2489 session = imap_session_get(folder);
2494 if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2495 g_warning(_("New folder name must not contain the namespace "
2500 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2502 g_free(session->mbox);
2503 session->mbox = NULL;
2504 session->exists = 0;
2505 session->recent = 0;
2506 session->expunge = 0;
2507 ok = imap_cmd_examine(session, "INBOX",
2508 &exists, &recent, &unseen, &uid_validity, FALSE);
2509 if (ok != MAILIMAP_NO_ERROR) {
2510 g_free(real_oldpath);
2514 separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2515 if (strchr(item->path, G_DIR_SEPARATOR)) {
2516 dirpath = g_path_get_dirname(item->path);
2517 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2520 newpath = g_strdup(name);
2522 real_newpath = imap_utf8_to_modified_utf7(newpath);
2523 imap_path_separator_subst(real_newpath, separator);
2525 ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2526 if (ok != MAILIMAP_NO_ERROR) {
2527 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2528 real_oldpath, real_newpath);
2529 g_free(real_oldpath);
2531 g_free(real_newpath);
2535 item->name = g_strdup(name);
2537 old_cache_dir = folder_item_get_path(item);
2539 paths[0] = g_strdup(item->path);
2541 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2542 imap_rename_folder_func, paths);
2544 if (is_dir_exist(old_cache_dir)) {
2545 new_cache_dir = folder_item_get_path(item);
2546 if (rename(old_cache_dir, new_cache_dir) < 0) {
2547 FILE_OP_ERROR(old_cache_dir, "rename");
2549 g_free(new_cache_dir);
2552 g_free(old_cache_dir);
2555 g_free(real_oldpath);
2556 g_free(real_newpath);
2560 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2564 IMAPSession *session;
2565 debug_print("getting session...\n");
2567 session = imap_session_get(folder);
2571 if (item && item->path) {
2572 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2575 if (!strcmp(path, "INBOX") && sub == FALSE)
2577 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2578 r = imap_threaded_subscribe(folder, path, sub);
2581 r = imap_threaded_subscribe(folder, rpath, sub);
2587 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2590 IMAPSession *session;
2593 gboolean selected_folder;
2595 g_return_val_if_fail(folder != NULL, -1);
2596 g_return_val_if_fail(item != NULL, -1);
2597 g_return_val_if_fail(item->path != NULL, -1);
2599 debug_print("getting session...\n");
2600 session = imap_session_get(folder);
2604 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2606 imap_threaded_subscribe(folder, path, FALSE);
2608 selected_folder = (session->mbox != NULL) &&
2609 (!strcmp(session->mbox, item->path));
2610 if (selected_folder) {
2611 ok = imap_cmd_close(session);
2612 if (ok != MAILIMAP_NO_ERROR) {
2613 debug_print("close err %d\n", ok);
2614 imap_handle_error(SESSION(session), ok);
2618 ok = imap_cmd_delete(session, path);
2619 if (ok != MAILIMAP_NO_ERROR) {
2620 gchar *tmp = g_strdup_printf("%s%c", path,
2621 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2624 ok = imap_cmd_delete(session, path);
2627 if (ok != MAILIMAP_NO_ERROR) {
2628 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2634 cache_dir = folder_item_get_path(item);
2635 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2636 g_warning("can't remove directory '%s'\n", cache_dir);
2638 folder_item_remove(item);
2642 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2646 g_return_val_if_fail(item != NULL, -1);
2647 g_return_val_if_fail(item->folder != NULL, -1);
2648 g_return_val_if_fail(item->node != NULL, -1);
2650 node = item->node->children;
2651 while (node != NULL) {
2653 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2657 debug_print("IMAP removing %s\n", item->path);
2659 if (imap_remove_all_msg(folder, item) < 0)
2661 return imap_remove_folder_real(folder, item);
2664 typedef struct _uncached_data {
2665 IMAPSession *session;
2667 MsgNumberList *numlist;
2674 static void *imap_get_uncached_messages_thread(void *data)
2676 uncached_data *stuff = (uncached_data *)data;
2677 IMAPSession *session = stuff->session;
2678 FolderItem *item = stuff->item;
2679 MsgNumberList *numlist = stuff->numlist;
2681 GSList *newlist = NULL;
2682 GSList *llast = NULL;
2683 GSList *seq_list, *cur;
2685 debug_print("uncached_messages\n");
2687 if (session == NULL || item == NULL || item->folder == NULL
2688 || FOLDER_CLASS(item->folder) != &imap_class) {
2693 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
2694 debug_print("get msgs info\n");
2695 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2696 struct mailimap_set * imapset;
2702 if (session->cancelled)
2705 imapset = cur->data;
2707 r = imap_threaded_fetch_env(session->folder,
2708 imapset, &env_list);
2709 if (r != MAILIMAP_NO_ERROR) {
2710 imap_handle_error(SESSION(session), r);
2718 session_set_access_time(SESSION(session));
2721 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2722 struct imap_fetch_env_info * info;
2725 info = carray_get(env_list, i);
2726 msginfo = imap_envelope_from_lep(info, item);
2727 if (msginfo == NULL)
2729 msginfo->folder = item;
2731 llast = newlist = g_slist_append(newlist, msginfo);
2733 llast = g_slist_append(llast, msginfo);
2734 llast = llast->next;
2739 imap_fetch_env_free(env_list);
2742 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2743 struct mailimap_set * imapset;
2745 imapset = cur->data;
2746 mailimap_set_free(imapset);
2749 session_set_access_time(SESSION(session));
2754 #define MAX_MSG_NUM 50
2756 static GSList *imap_get_uncached_messages(IMAPSession *session,
2758 MsgNumberList *numlist,
2761 GSList *result = NULL;
2763 uncached_data *data = g_new0(uncached_data, 1);
2768 data->total = g_slist_length(numlist);
2769 data->ok = MAILIMAP_NO_ERROR;
2770 debug_print("messages list : %i\n", data->total);
2772 while (cur != NULL) {
2773 GSList * partial_result;
2781 while (count < MAX_MSG_NUM) {
2786 if (newlist == NULL)
2787 llast = newlist = g_slist_append(newlist, p);
2789 llast = g_slist_append(llast, p);
2790 llast = llast->next;
2800 data->session = session;
2802 data->numlist = newlist;
2805 if (prefs_common.work_offline &&
2806 !inc_offline_should_override(FALSE,
2807 _("Claws Mail needs network access in order "
2808 "to access the IMAP server."))) {
2814 (GSList *)imap_get_uncached_messages_thread(data);
2816 if (data->ok != MAILIMAP_NO_ERROR) {
2819 statusbar_progress_all(data->cur,data->total, 1);
2821 g_slist_free(newlist);
2823 result = g_slist_concat(result, partial_result);
2828 statusbar_progress_all(0,0,0);
2829 statusbar_pop_all();
2834 static void imap_delete_all_cached_messages(FolderItem *item)
2838 g_return_if_fail(item != NULL);
2839 g_return_if_fail(item->folder != NULL);
2840 g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
2842 debug_print("Deleting all cached messages...\n");
2844 dir = folder_item_get_path(item);
2845 if (is_dir_exist(dir))
2846 remove_all_numbered_files(dir);
2849 debug_print("done.\n");
2852 gchar imap_get_path_separator_for_item(FolderItem *item)
2854 Folder *folder = NULL;
2855 IMAPFolder *imap_folder = NULL;
2856 IMAPSession *session = NULL;
2861 folder = item->folder;
2866 imap_folder = IMAP_FOLDER(folder);
2871 debug_print("getting session...");
2872 session = imap_session_get(FOLDER(folder));
2873 result = imap_get_path_separator(session, imap_folder, item->path);
2877 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
2881 gchar separator = '\0';
2883 g_return_val_if_fail(session != NULL, '/');
2884 r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
2886 if (r != MAILIMAP_NO_ERROR) {
2887 imap_handle_error(SESSION(session), r);
2888 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
2892 if (clist_count(lep_list) > 0) {
2893 clistiter * iter = clist_begin(lep_list);
2894 struct mailimap_mailbox_list * mb;
2895 mb = clist_content(iter);
2897 separator = mb->mb_delimiter;
2898 debug_print("got separator: %c\n", folder->last_seen_separator);
2900 mailimap_list_result_free(lep_list);
2904 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2906 gchar separator = '/';
2908 if (folder->last_seen_separator == 0) {
2909 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
2912 if (folder->last_seen_separator == 0) {
2913 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
2916 if (folder->last_seen_separator != 0) {
2917 debug_print("using separator: %c\n", folder->last_seen_separator);
2918 return folder->last_seen_separator;
2924 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2929 g_return_val_if_fail(folder != NULL, NULL);
2930 g_return_val_if_fail(path != NULL, NULL);
2932 real_path = imap_utf8_to_modified_utf7(path);
2933 separator = imap_get_path_separator(session, folder, path);
2934 imap_path_separator_subst(real_path, separator);
2939 static gint imap_set_message_flags(IMAPSession *session,
2940 MsgNumberList *numlist,
2948 IMAPFolder *folder = NULL;
2949 GSList *sorted_list = NULL;
2951 if (numlist == NULL || session == NULL)
2952 return MAILIMAP_ERROR_BAD_STATE;
2954 folder = IMAP_FOLDER(session->folder);
2956 sorted_list = g_slist_copy(numlist);
2957 sorted_list = g_slist_sort(sorted_list, g_int_compare);
2959 cur = g_slist_last(sorted_list);
2962 total = GPOINTER_TO_INT(cur->data);
2964 seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(session->folder), sorted_list);
2966 statusbar_print_all(_("Flagging messages..."));
2968 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
2969 struct mailimap_set * imapset = (struct mailimap_set *)cur->data;
2970 struct mailimap_set_item *set_item = clist_content(
2971 clist_begin(imapset->set_list));
2973 statusbar_progress_all(set_item->set_first, total, 1);
2975 ok = imap_cmd_store(session, imapset,
2977 statusbar_progress_all(set_item->set_last, total, 1);
2978 if (ok != MAILIMAP_NO_ERROR && folder->max_set_size > 20) {
2979 /* reduce max set size */
2980 folder->max_set_size /= 2;
2982 if (ok != MAILIMAP_NO_ERROR && is_fatal(ok)) {
2987 g_slist_free(sorted_list);
2989 statusbar_progress_all(0,0,0);
2990 statusbar_pop_all();
2992 imap_lep_set_free(seq_list);
2997 typedef struct _select_data {
2998 IMAPSession *session;
3003 guint32 *uid_validity;
3007 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
3009 gint *exists, gint *recent, gint *unseen,
3010 guint32 *uid_validity, gboolean block)
3014 gint exists_, recent_, unseen_;
3015 guint32 uid_validity_;
3017 if (!exists && !recent && !unseen && !uid_validity) {
3018 if (session->mbox && strcmp(session->mbox, path) == 0)
3019 return MAILIMAP_NO_ERROR;
3028 uid_validity = &uid_validity_;
3030 g_free(session->mbox);
3031 session->mbox = NULL;
3032 session->exists = 0;
3033 session->recent = 0;
3034 session->expunge = 0;
3036 real_path = imap_get_real_path(session, folder, path);
3038 ok = imap_cmd_select(session, real_path,
3039 exists, recent, unseen, uid_validity, block);
3040 if (ok != MAILIMAP_NO_ERROR) {
3041 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
3043 session->mbox = g_strdup(path);
3044 session->folder_content_changed = FALSE;
3045 session->exists = *exists;
3046 session->recent = *recent;
3047 session->expunge = 0;
3048 session->unseen = *unseen;
3049 session->uid_validity = *uid_validity;
3050 debug_print("select: exists %d recent %d expunge %d uid_validity %d\n",
3051 session->exists, session->recent, session->expunge,
3052 session->uid_validity);
3059 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3060 const gchar *path, IMAPFolderItem *item,
3062 guint32 *uid_next, guint32 *uid_validity,
3063 gint *unseen, gboolean block)
3067 struct mailimap_mailbox_data_status * data_status;
3072 real_path = imap_get_real_path(session, folder, path);
3091 if (session->mbox != NULL &&
3092 !strcmp(session->mbox, item->item.path)) {
3093 r = imap_cmd_close(session);
3094 if (r != MAILIMAP_NO_ERROR) {
3095 imap_handle_error(SESSION(session), r);
3096 debug_print("close err %d\n", r);
3101 r = imap_threaded_status(FOLDER(folder), real_path,
3102 &data_status, mask);
3105 if (r != MAILIMAP_NO_ERROR) {
3106 imap_handle_error(SESSION(session), r);
3107 debug_print("status err %d\n", r);
3111 if (data_status->st_info_list == NULL) {
3112 mailimap_mailbox_data_status_free(data_status);
3113 debug_print("status->st_info_list == NULL\n");
3114 return MAILIMAP_ERROR_BAD_STATE;
3118 for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
3119 iter = clist_next(iter)) {
3120 struct mailimap_status_info * info;
3122 info = clist_content(iter);
3123 switch (info->st_att) {
3124 case MAILIMAP_STATUS_ATT_MESSAGES:
3126 * messages = info->st_value;
3127 got_values |= 1 << 0;
3131 case MAILIMAP_STATUS_ATT_UIDNEXT:
3133 * uid_next = info->st_value;
3134 got_values |= 1 << 2;
3138 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
3140 * uid_validity = info->st_value;
3141 got_values |= 1 << 3;
3145 case MAILIMAP_STATUS_ATT_UNSEEN:
3147 * unseen = info->st_value;
3148 got_values |= 1 << 4;
3153 mailimap_mailbox_data_status_free(data_status);
3155 if (got_values != mask) {
3156 g_warning("status: incomplete values received (%d)\n", got_values);
3158 return MAILIMAP_NO_ERROR;
3161 static void imap_free_capabilities(IMAPSession *session)
3163 slist_free_strings(session->capability);
3164 g_slist_free(session->capability);
3165 session->capability = NULL;
3168 /* low-level IMAP4rev1 commands */
3170 static gint imap_cmd_login(IMAPSession *session,
3171 const gchar *user, const gchar *pass,
3177 if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
3178 gint ok = MAILIMAP_ERROR_BAD_STATE;
3179 if (imap_has_capability(session, "STARTTLS")) {
3180 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3181 log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
3182 ok = imap_cmd_starttls(session);
3183 if (ok != MAILIMAP_NO_ERROR) {
3184 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
3188 imap_free_capabilities(session);
3189 if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
3190 imap_handle_error(SESSION(session), r);
3191 log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
3196 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
3197 "server requires TLS, but Claws Mail "
3198 "has been compiled without OpenSSL "
3200 SESSION(session)->server);
3201 return MAILIMAP_ERROR_BAD_STATE;
3204 log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
3205 return MAILIMAP_ERROR_BAD_STATE;
3209 log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n",
3211 SESSION(session)->server,
3213 r = imap_threaded_login(session->folder, user, pass, type);
3214 if (r != MAILIMAP_NO_ERROR) {
3215 imap_handle_error(SESSION(session), r);
3216 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
3217 SESSION(session)->server);
3220 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
3221 SESSION(session)->server);
3222 ok = MAILIMAP_NO_ERROR;
3227 static gint imap_cmd_noop(IMAPSession *session)
3230 unsigned int exists, recent, expunge, unseen, uidnext, uidval;
3232 r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
3233 if (r != MAILIMAP_NO_ERROR) {
3234 imap_handle_error(SESSION(session), r);
3235 debug_print("noop err %d\n", r);
3239 session->folder_content_changed = FALSE;
3241 if ((exists && exists != session->exists)
3242 || (recent && recent != session->recent)
3243 || (expunge && expunge != session->expunge)
3244 || (unseen && unseen != session->unseen)) {
3245 session->folder_content_changed = TRUE;
3247 if (uidnext != 0 && uidnext != session->uid_next) {
3248 session->uid_next = uidnext;
3249 session->folder_content_changed = TRUE;
3251 if (uidval != 0 && uidval != session->uid_validity) {
3252 session->uid_validity = uidval;
3253 session->folder_content_changed = TRUE;
3256 session->exists = exists;
3257 session->recent = recent;
3258 session->expunge = expunge;
3259 session->unseen = unseen;
3261 session_set_access_time(SESSION(session));
3263 return MAILIMAP_NO_ERROR;
3266 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
3267 static gint imap_cmd_starttls(IMAPSession *session)
3271 r = imap_threaded_starttls(session->folder,
3272 SESSION(session)->server, SESSION(session)->port);
3273 if (r != MAILIMAP_NO_ERROR) {
3274 imap_handle_error(SESSION(session), r);
3275 debug_print("starttls err %d\n", r);
3278 return MAILIMAP_NO_ERROR;
3282 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3283 gint *exists, gint *recent, gint *unseen,
3284 guint32 *uid_validity, gboolean block)
3288 r = imap_threaded_select(session->folder, folder,
3289 exists, recent, unseen, uid_validity);
3290 if (r != MAILIMAP_NO_ERROR) {
3291 imap_handle_error(SESSION(session), r);
3292 debug_print("select err %d\n", r);
3295 return MAILIMAP_NO_ERROR;
3298 static gint imap_cmd_close(IMAPSession *session)
3302 r = imap_threaded_close(session->folder);
3303 if (r != MAILIMAP_NO_ERROR) {
3304 imap_handle_error(SESSION(session), r);
3305 debug_print("close err %d\n", r);
3308 g_free(session->mbox);
3309 session->mbox = NULL;
3310 session->exists = 0;
3311 session->recent = 0;
3312 session->expunge = 0;
3313 return MAILIMAP_NO_ERROR;
3316 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3317 gint *exists, gint *recent, gint *unseen,
3318 guint32 *uid_validity, gboolean block)
3322 r = imap_threaded_examine(session->folder, folder,
3323 exists, recent, unseen, uid_validity);
3324 if (r != MAILIMAP_NO_ERROR) {
3325 imap_handle_error(SESSION(session), r);
3326 debug_print("examine err %d\n", r);
3330 return MAILIMAP_NO_ERROR;
3333 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3337 r = imap_threaded_create(session->folder, folder);
3338 if (r != MAILIMAP_NO_ERROR) {
3339 imap_handle_error(SESSION(session), r);
3344 return MAILIMAP_NO_ERROR;
3347 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3348 const gchar *new_folder)
3352 r = imap_threaded_rename(session->folder, old_folder,
3354 if (r != MAILIMAP_NO_ERROR) {
3355 imap_handle_error(SESSION(session), r);
3359 return MAILIMAP_NO_ERROR;
3362 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3367 r = imap_threaded_delete(session->folder, folder);
3368 if (r != MAILIMAP_NO_ERROR) {
3369 imap_handle_error(SESSION(session), r);
3373 return MAILIMAP_NO_ERROR;
3376 typedef struct _fetch_data {
3377 IMAPSession *session;
3379 const gchar *filename;
3385 static void *imap_cmd_fetch_thread(void *data)
3387 fetch_data *stuff = (fetch_data *)data;
3388 IMAPSession *session = stuff->session;
3389 guint32 uid = stuff->uid;
3390 const gchar *filename = stuff->filename;
3394 r = imap_threaded_fetch_content(session->folder,
3398 r = imap_threaded_fetch_content(session->folder,
3401 if (r != MAILIMAP_NO_ERROR) {
3402 imap_handle_error(SESSION(session), r);
3403 debug_print("fetch err %d\n", r);
3404 return GINT_TO_POINTER(MAILIMAP_ERROR_BAD_STATE);
3406 return GINT_TO_POINTER(MAILIMAP_NO_ERROR);
3409 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3410 const gchar *filename, gboolean headers,
3413 fetch_data *data = g_new0(fetch_data, 1);
3416 data->session = session;
3418 data->filename = filename;
3419 data->headers = headers;
3422 if (prefs_common.work_offline &&
3423 !inc_offline_should_override(FALSE,
3424 _("Claws Mail needs network access in order "
3425 "to access the IMAP server."))) {
3429 statusbar_print_all(_("Fetching message..."));
3430 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3431 statusbar_pop_all();
3437 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
3438 const gchar *file, IMAPFlags flags,
3441 struct mailimap_flag_list * flag_list;
3444 g_return_val_if_fail(file != NULL, MAILIMAP_ERROR_BAD_STATE);
3446 flag_list = imap_flag_to_lep(flags);
3447 lock_session(session);
3448 r = imap_threaded_append(session->folder, destfolder,
3449 file, flag_list, (int *)new_uid);
3450 mailimap_flag_list_free(flag_list);
3452 if (r != MAILIMAP_NO_ERROR) {
3453 imap_handle_error(SESSION(session), r);
3454 debug_print("append err %d\n", r);
3458 unlock_session(session);
3460 return MAILIMAP_NO_ERROR;
3463 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3464 const gchar *destfolder, GRelation *uid_mapping,
3465 struct mailimap_set **source, struct mailimap_set **dest)
3469 g_return_val_if_fail(session != NULL, MAILIMAP_ERROR_BAD_STATE);
3470 g_return_val_if_fail(set != NULL, MAILIMAP_ERROR_BAD_STATE);
3471 g_return_val_if_fail(destfolder != NULL, MAILIMAP_ERROR_BAD_STATE);
3473 r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3474 if (r != MAILIMAP_NO_ERROR) {
3475 imap_handle_error(SESSION(session), r);
3479 return MAILIMAP_NO_ERROR;
3482 static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
3483 IMAPFlags flags, int do_add)
3486 struct mailimap_flag_list * flag_list;
3487 struct mailimap_store_att_flags * store_att_flags;
3489 flag_list = imap_flag_to_lep(flags);
3493 mailimap_store_att_flags_new_add_flags_silent(flag_list);
3496 mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3498 r = imap_threaded_store(session->folder, set, store_att_flags);
3499 mailimap_store_att_flags_free(store_att_flags);
3500 if (r != MAILIMAP_NO_ERROR) {
3501 imap_handle_error(SESSION(session), r);
3505 return MAILIMAP_NO_ERROR;
3508 static gint imap_cmd_expunge(IMAPSession *session)
3512 if (prefs_common.work_offline &&
3513 !inc_offline_should_override(FALSE,
3514 _("Claws Mail needs network access in order "
3515 "to access the IMAP server."))) {
3519 r = imap_threaded_expunge(session->folder);
3520 if (r != MAILIMAP_NO_ERROR) {
3521 imap_handle_error(SESSION(session), r);
3525 return MAILIMAP_NO_ERROR;
3528 static void imap_path_separator_subst(gchar *str, gchar separator)
3531 gboolean in_escape = FALSE;
3533 if (!separator || separator == '/') return;
3535 for (p = str; *p != '\0'; p++) {
3536 if (*p == '/' && !in_escape)
3538 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3540 else if (*p == '-' && in_escape)
3545 /* ===================================================================
3546 * UTF-7 conversion routines as in RFC 2192
3547 * ===================================================================
3548 * These two functions from:
3550 * Copyright (C) 2003-2004 Pawel Salek. */
3552 /* UTF7 modified base64 alphabet */
3553 static char base64chars[] =
3554 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
3555 #define UNDEFINED 64
3557 /* UTF16 definitions */
3558 #define UTF16MASK 0x03FFUL
3559 #define UTF16SHIFT 10
3560 #define UTF16BASE 0x10000UL
3561 #define UTF16HIGHSTART 0xD800UL
3562 #define UTF16HIGHEND 0xDBFFUL
3563 #define UTF16LOSTART 0xDC00UL
3564 #define UTF16LOEND 0xDFFFUL
3567 /* Convert an IMAP mailbox to a UTF-8 string.
3568 * dst needs to have roughly 4 times the storage space of src
3569 * Hex encoding can triple the size of the input
3570 * UTF-7 can be slightly denser than UTF-8
3571 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
3574 imap_modified_utf7_to_utf8(const char *mbox)
3576 unsigned c, i, bitcount;
3577 unsigned long ucs4, utf16, bitbuf;
3578 unsigned char base64[256];
3580 char *dst, *res = malloc(2*strlen(mbox)+1);
3585 if(!dst) return NULL;
3586 /* initialize modified base64 decoding table */
3587 memset(base64, UNDEFINED, sizeof (base64));
3588 for (i = 0; i < sizeof (base64chars); ++i) {
3589 base64[(unsigned)base64chars[i]] = i;
3592 /* loop until end of string */
3593 while (*src != '\0') {
3595 /* deal with literal characters and &- */
3596 if (c != '&' || *src == '-') {
3597 /* encode literally */
3599 /* skip over the '-' if this is an &- sequence */
3600 if (c == '&') ++src;
3602 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
3606 while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
3608 bitbuf = (bitbuf << 6) | c;
3610 /* enough bits for a UTF-16 character? */
3611 if (bitcount >= 16) {
3613 utf16 = (bitcount ? bitbuf >> bitcount
3615 /* convert UTF16 to UCS4 */
3617 (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
3618 ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
3621 (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
3622 ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
3627 /* convert UTF-16 range of UCS4 to UTF-8 */
3628 if (ucs4 <= 0x7fUL) {
3631 } else if (ucs4 <= 0x7ffUL) {
3632 dst[0] = 0xc0 | (ucs4 >> 6);
3633 dst[1] = 0x80 | (ucs4 & 0x3f);
3635 } else if (ucs4 <= 0xffffUL) {
3636 dst[0] = 0xe0 | (ucs4 >> 12);
3637 dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
3638 dst[2] = 0x80 | (ucs4 & 0x3f);
3641 dst[0] = 0xf0 | (ucs4 >> 18);
3642 dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
3643 dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
3644 dst[3] = 0x80 | (ucs4 & 0x3f);
3649 /* skip over trailing '-' in modified UTF-7 encoding */
3650 if (*src == '-') ++src;
3653 /* terminate destination string */
3658 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
3659 * dst should be about twice the length of src to deal with non-hex
3663 imap_utf8_to_modified_utf7(const char *src)
3665 unsigned int utf8pos, utf8total, c, utf7mode, bitstogo, utf16flag;
3666 unsigned long ucs4 = 0, bitbuf = 0;
3668 /* initialize hex lookup table */
3669 char *dst, *res = malloc(2*strlen(src)+1);
3671 if(!dst) return NULL;
3677 while ((c = (unsigned char)*src) != '\0') {
3679 /* normal character? */
3680 if (c >= ' ' && c <= '~') {
3681 /* switch out of UTF-7 mode */
3684 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
3693 /* encode '&' as '&-' */
3699 /* switch to UTF-7 mode */
3704 /* Encode US-ASCII characters as themselves */
3708 } else if (utf8total) {
3709 /* save UTF8 bits into UCS4 */
3710 ucs4 = (ucs4 << 6) | (c & 0x3FUL);
3711 if (++utf8pos < utf8total) {
3719 } else if (c < 0xF0) {
3723 /* NOTE: can't convert UTF8 sequences longer than 4 */
3729 /* loop to split ucs4 into two utf16 chars if necessary */
3732 if (ucs4 >= UTF16BASE) {
3734 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
3736 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
3739 bitbuf = (bitbuf << 16) | ucs4;
3743 /* spew out base64 */
3744 while (bitstogo >= 6) {
3746 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
3750 } while (utf16flag);
3752 /* if in UTF-7 mode, finish in ASCII */
3755 *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
3759 /* tie off string */
3764 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3766 FolderItem *item = node->data;
3767 gchar **paths = data;
3768 const gchar *oldpath = paths[0];
3769 const gchar *newpath = paths[1];
3770 gchar *real_oldpath, *real_newpath;
3772 gchar *new_itempath;
3774 IMAPSession *session = imap_session_get(item->folder);
3776 oldpathlen = strlen(oldpath);
3777 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3778 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3782 base = item->path + oldpathlen;
3783 while (*base == G_DIR_SEPARATOR) base++;
3785 new_itempath = g_strdup(newpath);
3787 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3790 real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3792 item->path = new_itempath;
3794 real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3796 imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3797 imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3799 g_free(real_oldpath);
3800 g_free(real_newpath);
3804 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3806 GSList *uidlist, *elem;
3808 clist * lep_uidlist;
3809 gint ok, nummsgs = 0, lastuid_old;
3811 if (session == NULL) {
3815 ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
3816 NULL, NULL, NULL, NULL, TRUE);
3817 if (ok != MAILIMAP_NO_ERROR) {
3821 g_slist_free(item->uid_list);
3822 item->uid_list = NULL;
3826 if (folder->account && folder->account->low_bandwidth) {
3827 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3831 if (r == MAILIMAP_NO_ERROR) {
3832 GSList * fetchuid_list =
3833 imap_uid_list_from_lep(lep_uidlist);
3834 mailimap_search_result_free(lep_uidlist);
3836 uidlist = g_slist_concat(fetchuid_list, uidlist);
3838 carray * lep_uidtab;
3839 if (r != -1) /* inited */
3840 imap_handle_error(SESSION(session), r);
3841 r = imap_threaded_fetch_uid(folder, 1,
3843 if (r == MAILIMAP_NO_ERROR) {
3844 GSList * fetchuid_list =
3845 imap_uid_list_from_lep_tab(lep_uidtab);
3846 imap_fetch_uid_list_free(lep_uidtab);
3847 uidlist = g_slist_concat(fetchuid_list, uidlist);
3851 if (r != MAILIMAP_NO_ERROR) {
3852 imap_handle_error(SESSION(session), r);
3856 lastuid_old = item->lastuid;
3858 for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3861 msgnum = GPOINTER_TO_INT(elem->data);
3863 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3864 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3867 g_slist_free(uidlist);
3869 unlock_session(session); /* locked from imap_get_num_list */
3875 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3877 IMAPFolderItem *item = (IMAPFolderItem *)_item;
3878 IMAPSession *session;
3880 GSList *uidlist = NULL;
3882 gboolean selected_folder;
3883 gint known_list_len = 0;
3884 debug_print("get_num_list\n");
3886 g_return_val_if_fail(folder != NULL, -1);
3887 g_return_val_if_fail(item != NULL, -1);
3888 g_return_val_if_fail(item->item.path != NULL, -1);
3889 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3890 g_return_val_if_fail(folder->account != NULL, -1);
3892 known_list_len = g_slist_length(item->uid_list);
3893 if (!item->should_update) {
3894 debug_print("get_num_list: nothing to update\n");
3895 *old_uids_valid = TRUE;
3896 if (known_list_len == item->item.total_msgs
3897 && known_list_len > 0) {
3898 *msgnum_list = g_slist_copy(item->uid_list);
3899 return known_list_len;
3901 debug_print("don't know the list length...\n");
3905 if (prefs_common.work_offline &&
3906 !inc_offline_should_override(FALSE,
3907 _("Claws Mail needs network access in order "
3908 "to access the IMAP server."))) {
3912 debug_print("getting session...\n");
3913 session = imap_session_get(folder);
3914 g_return_val_if_fail(session != NULL, -1);
3916 lock_session(session); /* unlocked by get_list_of_uids */
3917 if (FOLDER_ITEM(item)->path)
3918 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3919 FOLDER_ITEM(item)->folder->name,
3921 FOLDER_ITEM(item)->path);
3923 statusbar_print_all(_("Scanning folder %s ..."),
3924 FOLDER_ITEM(item)->folder->name);
3926 selected_folder = (session->mbox != NULL) &&
3927 (!strcmp(session->mbox, item->item.path));
3929 if (item->should_trash_cache) {
3930 *old_uids_valid = FALSE;
3931 debug_print("get_num_list: trashing num list\n");
3932 debug_print("Freeing imap uid cache\n");
3934 g_slist_free(item->uid_list);
3935 item->uid_list = NULL;
3937 imap_delete_all_cached_messages((FolderItem *)item);
3939 debug_print("get_num_list: updating num list\n");
3940 *old_uids_valid = TRUE;
3943 nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3944 /* session could be broken now, in case of fatal error */
3946 debug_print("get_num_list: got %d msgs\n", nummsgs);
3949 statusbar_pop_all();
3953 *msgnum_list = uidlist;
3955 dir = folder_item_get_path((FolderItem *)item);
3956 debug_print("removing old messages from %s\n", dir);
3957 remove_numbered_files_not_in_list(dir, *msgnum_list);
3960 debug_print("get_num_list - ok - %i\n", nummsgs);
3961 statusbar_pop_all();
3962 item->should_trash_cache = FALSE;
3963 item->should_update = FALSE;
3967 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3972 flags.perm_flags = MSG_NEW|MSG_UNREAD;
3973 flags.tmp_flags = 0;
3975 g_return_val_if_fail(item != NULL, NULL);
3976 g_return_val_if_fail(file != NULL, NULL);
3978 if (folder_has_parent_of_type(item, F_QUEUE)) {
3979 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3980 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3981 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3984 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3985 if (!msginfo) return NULL;
3987 msginfo->plaintext_file = g_strdup(file);
3988 msginfo->folder = item;
3993 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3994 GSList *msgnum_list)
3996 IMAPSession *session;
3997 MsgInfoList *ret = NULL;
4000 debug_print("get_msginfos\n");
4002 g_return_val_if_fail(folder != NULL, NULL);
4003 g_return_val_if_fail(item != NULL, NULL);
4004 g_return_val_if_fail(msgnum_list != NULL, NULL);
4006 debug_print("getting session...\n");
4007 session = imap_session_get(folder);
4008 g_return_val_if_fail(session != NULL, NULL);
4010 lock_session(session); /* unlocked later in the function */
4012 debug_print("IMAP getting msginfos\n");
4013 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
4014 NULL, NULL, NULL, NULL, FALSE);
4015 if (ok != MAILIMAP_NO_ERROR) {
4018 if (!(folder_has_parent_of_type(item, F_DRAFT) ||
4019 folder_has_parent_of_type(item, F_QUEUE))) {
4020 ret = g_slist_concat(ret,
4021 imap_get_uncached_messages(session, item,
4023 if (ok != MAILIMAP_NO_ERROR)
4025 unlock_session(session);
4027 MsgNumberList *sorted_list, *elem, *llast = NULL;
4028 gint startnum, lastnum;
4030 unlock_session(session);
4032 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
4034 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
4036 llast = g_slist_last(ret);
4037 for (elem = sorted_list;; elem = g_slist_next(elem)) {
4041 num = GPOINTER_TO_INT(elem->data);
4043 if (num > lastnum + 1 || elem == NULL) {
4045 for (i = startnum; i <= lastnum; ++i) {
4047 file = imap_fetch_msg(folder, item, i);
4049 MsgInfo *msginfo = imap_parse_msg(file, item);
4050 if (msginfo != NULL) {
4051 msginfo->msgnum = i;
4053 llast = ret = g_slist_append(ret, msginfo);
4055 llast = g_slist_append(llast, msginfo);
4056 llast = llast->next;
4071 g_slist_free(sorted_list);
4076 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
4078 MsgInfo *msginfo = NULL;
4079 MsgInfoList *msginfolist;
4080 MsgNumberList numlist;
4082 numlist.next = NULL;
4083 numlist.data = GINT_TO_POINTER(uid);
4085 msginfolist = imap_get_msginfos(folder, item, &numlist);
4086 if (msginfolist != NULL) {
4087 msginfo = msginfolist->data;
4088 g_slist_free(msginfolist);
4094 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
4096 IMAPSession *session;
4097 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4098 gint ok, exists = 0, unseen = 0;
4099 guint32 uid_next = 0, uid_val = 0;
4100 gboolean selected_folder;
4102 g_return_val_if_fail(folder != NULL, FALSE);
4103 g_return_val_if_fail(item != NULL, FALSE);
4104 g_return_val_if_fail(item->item.folder != NULL, FALSE);
4105 g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
4107 if (item->item.path == NULL)
4110 if (item->should_update) {
4111 debug_print("scan already required\n");
4114 debug_print("getting session...\n");
4115 session = imap_session_get(folder);
4117 g_return_val_if_fail(session != NULL, FALSE);
4118 lock_session(session); /* unlocked later in the function */
4120 selected_folder = (session->mbox != NULL) &&
4121 (!strcmp(session->mbox, item->item.path));
4122 if (selected_folder) {
4123 if (!session->folder_content_changed) {
4124 ok = imap_cmd_noop(session);
4125 if (ok != MAILIMAP_NO_ERROR) {
4126 debug_print("disconnected!\n");
4128 session = imap_reconnect_if_possible(folder, session);
4130 session = imap_session_get(folder);
4131 if (session == NULL)
4135 if (session->folder_content_changed) {
4136 debug_print("CHANGED (self-noop)! scan_required\n");
4137 item->should_update = TRUE;
4138 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4139 item->item.mtime = session->uid_validity;
4140 item->should_trash_cache = TRUE;
4142 unlock_session(session);
4146 debug_print("CHANGED (previous noop)! scan_required\n");
4147 item->should_update = TRUE;
4148 if (session->uid_validity && session->uid_validity != item->item.mtime) {
4149 item->item.mtime = session->uid_validity;
4150 item->should_trash_cache = TRUE;
4152 unlock_session(session);
4156 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
4157 &exists, &uid_next, &uid_val, &unseen, FALSE);
4158 if (ok != MAILIMAP_NO_ERROR) {
4162 debug_print("exists %d, item->item.total_msgs %d\n",
4163 exists, item->item.total_msgs);
4164 if (exists != item->item.total_msgs
4165 || unseen != item->item.unread_msgs
4166 || uid_next != item->uid_next
4167 || uid_val != item->item.mtime) {
4168 debug_print("CHANGED (status)! scan_required\n");
4169 item->last_change = time(NULL);
4170 item->should_update = TRUE;
4171 item->uid_next = uid_next;
4172 if (uid_val != item->item.mtime) {
4173 item->item.mtime = uid_val;
4174 item->should_trash_cache = TRUE;
4176 unlock_session(session);
4180 unlock_session(session);
4182 item->should_update = FALSE;
4186 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
4188 IMAPSession *session;
4189 IMAPFlags flags_set = 0, flags_unset = 0;
4190 gint ok = MAILIMAP_NO_ERROR;
4191 MsgNumberList numlist;
4192 hashtable_data *ht_data = NULL;
4194 g_return_if_fail(folder != NULL);
4195 g_return_if_fail(folder->klass == &imap_class);
4196 g_return_if_fail(item != NULL);
4197 g_return_if_fail(item->folder == folder);
4198 g_return_if_fail(msginfo != NULL);
4199 g_return_if_fail(msginfo->folder == item);
4201 if (!MSG_IS_MARKED(msginfo->flags) && (newflags & MSG_MARKED))
4202 flags_set |= IMAP_FLAG_FLAGGED;
4203 if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
4204 flags_unset |= IMAP_FLAG_FLAGGED;
4206 if (!MSG_IS_UNREAD(msginfo->flags) && (newflags & MSG_UNREAD))
4207 flags_unset |= IMAP_FLAG_SEEN;
4208 if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
4209 flags_set |= IMAP_FLAG_SEEN;
4211 if (!MSG_IS_REPLIED(msginfo->flags) && (newflags & MSG_REPLIED))
4212 flags_set |= IMAP_FLAG_ANSWERED;
4213 if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
4214 flags_unset |= IMAP_FLAG_ANSWERED;
4216 if (!MSG_IS_DELETED(msginfo->flags) && (newflags & MSG_DELETED))
4217 flags_set |= IMAP_FLAG_DELETED;
4218 if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
4219 flags_unset |= IMAP_FLAG_DELETED;
4221 if (!flags_set && !flags_unset) {
4222 /* the changed flags were not translatable to IMAP-speak.
4223 * like MSG_POSTFILTERED, so just apply. */
4224 msginfo->flags.perm_flags = newflags;
4228 debug_print("getting session...\n");
4229 session = imap_session_get(folder);
4234 if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
4235 NULL, NULL, NULL, NULL, FALSE)) != MAILIMAP_NO_ERROR) {
4238 numlist.next = NULL;
4239 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
4241 if (IMAP_FOLDER_ITEM(item)->batching) {
4242 /* instead of performing an UID STORE command for each message change,
4243 * as a lot of them can change "together", we just fill in hashtables
4244 * and defer the treatment so that we're able to send only one
4247 debug_print("IMAP batch mode on, deferring flags change\n");
4249 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table,
4250 GINT_TO_POINTER(flags_set));
4251 if (ht_data == NULL) {
4252 ht_data = g_new0(hashtable_data, 1);
4253 ht_data->item = IMAP_FOLDER_ITEM(item);
4254 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table,
4255 GINT_TO_POINTER(flags_set), ht_data);
4257 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
4260 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4261 GINT_TO_POINTER(flags_unset));
4262 if (ht_data == NULL) {
4263 ht_data = g_new0(hashtable_data, 1);
4264 ht_data->item = IMAP_FOLDER_ITEM(item);
4265 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table,
4266 GINT_TO_POINTER(flags_unset), ht_data);
4268 ht_data->msglist = g_slist_prepend(ht_data->msglist,
4269 GINT_TO_POINTER(msginfo->msgnum));
4272 debug_print("IMAP changing flags\n");
4274 ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
4275 if (ok != MAILIMAP_NO_ERROR) {
4281 ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
4282 if (ok != MAILIMAP_NO_ERROR) {
4287 msginfo->flags.perm_flags = newflags;
4291 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4294 IMAPSession *session;
4296 MsgNumberList numlist;
4298 g_return_val_if_fail(folder != NULL, -1);
4299 g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4300 g_return_val_if_fail(item != NULL, -1);
4302 debug_print("getting session...\n");
4303 session = imap_session_get(folder);
4304 if (!session) return -1;
4306 ok = imap_select(session, IMAP_FOLDER(folder), item->path,
4307 NULL, NULL, NULL, NULL, FALSE);
4308 if (ok != MAILIMAP_NO_ERROR) {
4311 numlist.next = NULL;
4312 numlist.data = GINT_TO_POINTER(uid);
4314 ok = imap_set_message_flags
4315 (session, &numlist, IMAP_FLAG_DELETED, TRUE);
4316 if (ok != MAILIMAP_NO_ERROR) {
4317 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4321 if (!session->uidplus) {
4322 ok = imap_cmd_expunge(session);
4326 uidstr = g_strdup_printf("%u", uid);
4327 ok = imap_cmd_expunge(session);
4330 if (ok != MAILIMAP_NO_ERROR) {
4331 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4335 IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4336 IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4337 dir = folder_item_get_path(item);
4338 if (is_dir_exist(dir))
4339 remove_numbered_files(dir, uid, uid);
4341 return MAILIMAP_NO_ERROR;
4344 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4346 return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4349 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4353 g_return_val_if_fail(list != NULL, -1);
4355 for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4356 if (GPOINTER_TO_INT(elem->data) >= num)
4359 return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4363 * NEW and DELETED flags are not syncronized
4364 * - The NEW/RECENT flags in IMAP folders can not really be directly
4365 * modified by Sylpheed
4366 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4367 * meaning, in IMAP it always removes the messages from the FolderItem
4368 * in Sylpheed it can mean to move the message to trash
4371 typedef struct _get_flags_data {
4374 MsgInfoList *msginfo_list;
4375 GRelation *msgflags;
4376 gboolean full_search;
4380 static /*gint*/ void *imap_get_flags_thread(void *data)
4382 get_flags_data *stuff = (get_flags_data *)data;
4383 Folder *folder = stuff->folder;
4384 FolderItem *fitem = (FolderItem *) stuff->item;
4385 MsgInfoList *msginfo_list = stuff->msginfo_list;
4386 GRelation *msgflags = stuff->msgflags;
4388 carray * lep_uidtab;
4389 IMAPSession *session;
4391 int r = MAILIMAP_NO_ERROR;
4392 GHashTable *flags_hash = NULL;
4393 gboolean full_search = stuff->full_search;
4394 GSList *sorted_list = NULL;
4395 GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
4396 GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
4397 GSList *seq_list, *cur;
4398 gboolean reverse_seen = FALSE;
4399 gboolean selected_folder;
4400 gint exists_cnt, unseen_cnt;
4402 session = imap_session_get(folder);
4404 if (session == NULL) {
4406 return GINT_TO_POINTER(-1);
4408 selected_folder = (session->mbox != NULL) &&
4409 (!strcmp(session->mbox, fitem->path));
4411 lock_session(session);
4412 if (!selected_folder) {
4413 ok = imap_select(session, IMAP_FOLDER(folder), fitem->path,
4414 &exists_cnt, NULL, &unseen_cnt, NULL, TRUE);
4415 if (ok != MAILIMAP_NO_ERROR) {
4417 return GINT_TO_POINTER(-1);
4420 if (unseen_cnt > exists_cnt / 2)
4421 reverse_seen = TRUE;
4424 if (fitem->unread_msgs > fitem->total_msgs / 2)
4425 reverse_seen = TRUE;
4428 sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4430 seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msginfo_list);
4432 struct mailimap_set * set;
4433 set = mailimap_set_new_interval(1, 0);
4434 seq_list = g_slist_append(NULL, set);
4437 if (folder->account && folder->account->low_bandwidth) {
4438 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4439 struct mailimap_set * imapset;
4440 clist * lep_uidlist;
4443 imapset = cur->data;
4445 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4446 full_search ? NULL:imapset, &lep_uidlist);
4449 r = imap_threaded_search(folder,
4450 IMAP_SEARCH_TYPE_UNSEEN,
4451 full_search ? NULL:imapset, &lep_uidlist);
4453 if (r == MAILIMAP_NO_ERROR) {
4456 uidlist = imap_uid_list_from_lep(lep_uidlist);
4457 mailimap_search_result_free(lep_uidlist);
4459 unseen = g_slist_concat(unseen, uidlist);
4461 imap_handle_error(SESSION(session), r);
4465 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4466 full_search ? NULL:imapset, &lep_uidlist);
4467 if (r == MAILIMAP_NO_ERROR) {
4470 uidlist = imap_uid_list_from_lep(lep_uidlist);
4471 mailimap_search_result_free(lep_uidlist);
4473 flagged = g_slist_concat(flagged, uidlist);
4475 imap_handle_error(SESSION(session), r);
4479 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4480 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4481 full_search ? NULL:imapset, &lep_uidlist);
4482 if (r == MAILIMAP_NO_ERROR) {
4485 uidlist = imap_uid_list_from_lep(lep_uidlist);
4486 mailimap_search_result_free(lep_uidlist);
4488 answered = g_slist_concat(answered, uidlist);
4490 imap_handle_error(SESSION(session), r);
4494 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4495 full_search ? NULL:imapset, &lep_uidlist);
4496 if (r == MAILIMAP_NO_ERROR) {
4499 uidlist = imap_uid_list_from_lep(lep_uidlist);
4500 mailimap_search_result_free(lep_uidlist);
4502 deleted = g_slist_concat(deleted, uidlist);
4504 imap_handle_error(SESSION(session), r);
4510 p_answered = answered;
4511 p_flagged = flagged;
4512 p_deleted = deleted;
4515 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4516 if (r == MAILIMAP_NO_ERROR) {
4517 flags_hash = g_hash_table_new_full(g_int_hash, g_int_equal, free, NULL);
4518 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash);
4519 imap_fetch_uid_flags_list_free(lep_uidtab);
4521 imap_handle_error(SESSION(session), r);
4527 if (r == MAILIMAP_NO_ERROR)
4528 unlock_session(session);
4530 for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4532 MsgPermFlags flags, oldflags;
4535 msginfo = (MsgInfo *) elem->data;
4536 flags = msginfo->flags.perm_flags;
4537 wasnew = (flags & MSG_NEW);
4538 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_MARKED|MSG_DELETED);
4540 if (folder->account && folder->account->low_bandwidth) {
4541 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4542 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
4544 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4547 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4548 if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
4549 if (!reverse_seen) {
4550 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4552 flags &= ~(MSG_UNREAD | MSG_NEW);
4556 if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
4557 flags |= MSG_MARKED;
4559 flags &= ~MSG_MARKED;
4561 if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4562 if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
4563 flags |= MSG_REPLIED;
4565 flags &= ~MSG_REPLIED;
4566 if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
4567 flags |= MSG_DELETED;
4569 flags &= ~MSG_DELETED;
4572 if (flags_hash != NULL) {
4575 puid = malloc(sizeof(* puid));
4576 * puid = msginfo->msgnum;
4578 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash, puid));
4582 if ((flags & MSG_UNREAD) == 0)
4589 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4593 g_hash_table_destroy(flags_hash);
4595 imap_lep_set_free(seq_list);
4596 g_slist_free(flagged);
4597 g_slist_free(deleted);
4598 g_slist_free(answered);
4599 g_slist_free(unseen);
4600 g_slist_free(sorted_list);
4603 return GINT_TO_POINTER(0);
4606 static gint imap_get_flags(Folder *folder, FolderItem *item,
4607 MsgInfoList *msginfo_list, GRelation *msgflags)
4610 get_flags_data *data = g_new0(get_flags_data, 1);
4612 data->folder = folder;
4614 data->msginfo_list = msginfo_list;
4615 data->msgflags = msgflags;
4616 data->full_search = FALSE;
4618 GSList *tmp = NULL, *cur;
4620 if (prefs_common.work_offline &&
4621 !inc_offline_should_override(FALSE,
4622 _("Claws Mail needs network access in order "
4623 "to access the IMAP server."))) {
4628 tmp = folder_item_get_msg_list(item);
4630 if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4631 data->full_search = TRUE;
4633 for (cur = tmp; cur; cur = cur->next)
4634 procmsg_msginfo_free((MsgInfo *)cur->data);
4638 result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4645 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4647 gboolean flags_set = GPOINTER_TO_INT(user_data);
4648 gint flags_value = GPOINTER_TO_INT(key);
4649 hashtable_data *data = (hashtable_data *)value;
4650 IMAPFolderItem *_item = data->item;
4651 FolderItem *item = (FolderItem *)_item;
4652 gint ok = MAILIMAP_ERROR_BAD_STATE;
4653 IMAPSession *session = NULL;
4655 debug_print("getting session...\n");
4656 session = imap_session_get(item->folder);
4658 data->msglist = g_slist_reverse(data->msglist);
4660 debug_print("IMAP %ssetting flags to %d for %d messages\n",
4663 g_slist_length(data->msglist));
4665 lock_session(session);
4667 ok = imap_select(session, IMAP_FOLDER(item->folder), item->path,
4668 NULL, NULL, NULL, NULL, FALSE);
4670 if (ok == MAILIMAP_NO_ERROR) {
4671 ok = imap_set_message_flags(session, data->msglist, flags_value, flags_set);
4673 g_warning("can't select mailbox %s\n", item->path);
4677 unlock_session(session);
4679 g_slist_free(data->msglist);
4684 static void process_hashtable(IMAPFolderItem *item)
4686 if (item->flags_set_table) {
4687 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4688 g_hash_table_destroy(item->flags_set_table);
4689 item->flags_set_table = NULL;
4691 if (item->flags_unset_table) {
4692 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4693 g_hash_table_destroy(item->flags_unset_table);
4694 item->flags_unset_table = NULL;
4699 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4701 IMAPFolderItem *item = (IMAPFolderItem *)_item;
4702 IMAPSession *session;
4704 g_return_if_fail(item != NULL);
4706 if (item->batching == batch)
4710 item->batching = TRUE;
4711 debug_print("IMAP switching to batch mode\n");
4712 if (!item->flags_set_table) {
4713 item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4715 if (!item->flags_unset_table) {
4716 item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4718 session = imap_session_get(folder);
4720 imap_refresh_sensitivity(session);
4721 session->sens_update_block = TRUE;
4724 debug_print("IMAP switching away from batch mode\n");
4726 process_hashtable(item);
4727 item->batching = FALSE;
4728 session = imap_session_get(folder);
4730 session->sens_update_block = FALSE;
4731 imap_refresh_sensitivity(session);
4738 /* data types conversion libetpan <-> claws */
4742 #define ETPAN_IMAP_MB_MARKED 1
4743 #define ETPAN_IMAP_MB_UNMARKED 2
4744 #define ETPAN_IMAP_MB_NOSELECT 4
4745 #define ETPAN_IMAP_MB_NOINFERIORS 8
4747 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4753 if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4754 switch (imap_flags->mbf_sflag) {
4755 case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4756 flags |= ETPAN_IMAP_MB_MARKED;
4758 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4759 flags |= ETPAN_IMAP_MB_NOSELECT;
4761 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4762 flags |= ETPAN_IMAP_MB_UNMARKED;
4767 for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4768 cur = clist_next(cur)) {
4769 struct mailimap_mbx_list_oflag * oflag;
4771 oflag = clist_content(cur);
4773 switch (oflag->of_type) {
4774 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4775 flags |= ETPAN_IMAP_MB_NOINFERIORS;
4783 static GSList * imap_list_from_lep(IMAPFolder * folder,
4784 clist * list, const gchar * real_path, gboolean all)
4787 GSList * item_list = NULL, *llast = NULL;
4789 for(iter = clist_begin(list) ; iter != NULL ;
4790 iter = clist_next(iter)) {
4791 struct mailimap_mailbox_list * mb;
4799 FolderItem *new_item;
4801 mb = clist_content(iter);
4807 if (mb->mb_flag != NULL)
4808 flags = imap_flags_to_flags(mb->mb_flag);
4810 delimiter = mb->mb_delimiter;
4813 dup_name = strdup(name);
4814 if (delimiter != '\0')
4815 subst_char(dup_name, delimiter, '/');
4817 base = g_path_get_basename(dup_name);
4818 if (base[0] == '.') {
4823 if (!all && path_cmp(name, real_path) == 0) {
4829 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4830 dup_name[strlen(dup_name)-1] = '\0';
4833 loc_name = imap_modified_utf7_to_utf8(base);
4834 loc_path = imap_modified_utf7_to_utf8(dup_name);
4836 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4837 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4838 new_item->no_sub = TRUE;
4839 if (strcmp(dup_name, "INBOX") != 0 &&
4840 ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4841 new_item->no_select = TRUE;
4843 if (item_list == NULL)
4844 llast = item_list = g_slist_append(item_list, new_item);
4846 llast = g_slist_append(llast, new_item);
4847 llast = llast->next;
4849 debug_print("folder '%s' found.\n", loc_path);
4860 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist)
4862 GSList *sorted_list, *cur;
4863 guint first, last, next;
4864 GSList *ret_list = NULL, *llast = NULL;
4865 struct mailimap_set * current_set;
4866 unsigned int item_count;
4868 if (numlist == NULL)
4871 current_set = mailimap_set_new_empty();
4873 sorted_list = g_slist_copy(numlist);
4874 sorted_list = g_slist_sort(sorted_list, g_int_compare);
4876 first = GPOINTER_TO_INT(sorted_list->data);
4879 for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
4880 if (GPOINTER_TO_INT(cur->data) == 0)
4885 last = GPOINTER_TO_INT(cur->data);
4887 next = GPOINTER_TO_INT(cur->next->data);
4891 if (last + 1 != next || next == 0 || item_count >= folder->max_set_size) {
4893 struct mailimap_set_item * item;
4894 item = mailimap_set_item_new(first, last);
4895 mailimap_set_add(current_set, item);
4899 if (item_count >= folder->max_set_size) {
4900 if (ret_list == NULL)
4901 llast = ret_list = g_slist_append(ret_list,
4904 llast = g_slist_append(llast, current_set);
4905 llast = llast->next;
4908 current_set = mailimap_set_new_empty();
4914 if (clist_count(current_set->set_list) > 0) {
4915 ret_list = g_slist_append(ret_list,
4919 g_slist_free(sorted_list);
4924 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist)
4926 MsgNumberList *numlist = NULL;
4930 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
4931 MsgInfo *msginfo = (MsgInfo *) cur->data;
4933 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
4935 numlist = g_slist_reverse(numlist);
4936 seq_list = imap_get_lep_set_from_numlist(folder, numlist);
4937 g_slist_free(numlist);
4942 static GSList * imap_uid_list_from_lep(clist * list)
4949 for(iter = clist_begin(list) ; iter != NULL ;
4950 iter = clist_next(iter)) {
4953 puid = clist_content(iter);
4954 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4957 result = g_slist_reverse(result);
4961 static GSList * imap_uid_list_from_lep_tab(carray * list)
4968 for(i = 0 ; i < carray_count(list) ; i ++) {
4971 puid = carray_get(list, i);
4972 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4974 result = g_slist_reverse(result);
4978 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
4986 for(i = 0 ; i < carray_count(list) ; i += 2) {
4991 puid = carray_get(list, i);
4992 pflags = carray_get(list, i + 1);
4993 pguid = malloc(sizeof(* pguid));
4996 g_hash_table_insert(hash, pguid, GINT_TO_POINTER(* pflags));
5000 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
5003 MsgInfo *msginfo = NULL;
5006 MsgFlags flags = {0, 0};
5008 if (info->headers == NULL)
5011 MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
5012 if (folder_has_parent_of_type(item, F_QUEUE)) {
5013 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
5014 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
5015 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
5017 flags.perm_flags = info->flags;
5021 msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
5024 msginfo->msgnum = uid;
5025 msginfo->size = size;
5031 static void imap_lep_set_free(GSList *seq_list)
5035 for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
5036 struct mailimap_set * imapset;
5038 imapset = cur->data;
5039 mailimap_set_free(imapset);
5041 g_slist_free(seq_list);
5044 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags)
5046 struct mailimap_flag_list * flag_list;
5048 flag_list = mailimap_flag_list_new_empty();
5050 if (IMAP_IS_SEEN(flags))
5051 mailimap_flag_list_add(flag_list,
5052 mailimap_flag_new_seen());
5053 if (IMAP_IS_ANSWERED(flags))
5054 mailimap_flag_list_add(flag_list,
5055 mailimap_flag_new_answered());
5056 if (IMAP_IS_FLAGGED(flags))
5057 mailimap_flag_list_add(flag_list,
5058 mailimap_flag_new_flagged());
5059 if (IMAP_IS_DELETED(flags))
5060 mailimap_flag_list_add(flag_list,
5061 mailimap_flag_new_deleted());
5062 if (IMAP_IS_DRAFT(flags))
5063 mailimap_flag_list_add(flag_list,
5064 mailimap_flag_new_draft());
5069 guint imap_folder_get_refcnt(Folder *folder)
5071 return ((IMAPFolder *)folder)->refcnt;
5074 void imap_folder_ref(Folder *folder)
5076 ((IMAPFolder *)folder)->refcnt++;
5079 void imap_disconnect_all(void)
5082 for (list = account_get_list(); list != NULL; list = list->next) {
5083 PrefsAccount *account = list->data;
5084 if (account->protocol == A_IMAP4) {
5085 RemoteFolder *folder = (RemoteFolder *)account->folder;
5086 if (folder && folder->session) {
5087 IMAPSession *session = (IMAPSession *)folder->session;
5088 imap_threaded_disconnect(FOLDER(folder));
5089 SESSION(session)->state = SESSION_DISCONNECTED;
5090 session_destroy(SESSION(session));
5091 folder->session = NULL;
5097 void imap_folder_unref(Folder *folder)
5099 if (((IMAPFolder *)folder)->refcnt > 0)
5100 ((IMAPFolder *)folder)->refcnt--;
5103 void imap_cancel_all(void)
5108 folderlist = folder_get_list();
5109 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5110 Folder *folder = (Folder *) cur->data;
5112 if (folder->klass == &imap_class) {
5113 if (imap_is_busy(folder)) {
5114 IMAPSession *imap_session;
5115 RemoteFolder *rfolder;
5117 g_printerr("cancelled\n");
5118 imap_threaded_cancel(folder);
5119 rfolder = (RemoteFolder *) folder;
5120 imap_session = (IMAPSession *) rfolder->session;
5122 imap_session->cancelled = 1;
5128 gboolean imap_cancel_all_enabled(void)
5133 folderlist = folder_get_list();
5134 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
5135 Folder *folder = (Folder *) cur->data;
5137 if (folder->klass == &imap_class) {
5138 if (imap_is_busy(folder)) {
5147 static gboolean imap_is_busy(Folder *folder)
5149 IMAPSession *imap_session;
5150 RemoteFolder *rfolder;
5152 rfolder = (RemoteFolder *) folder;
5153 imap_session = (IMAPSession *) rfolder->session;
5154 if (imap_session == NULL)
5157 return imap_session->busy;
5160 #else /* HAVE_LIBETPAN */
5162 static FolderClass imap_class;
5164 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
5165 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
5167 static Folder *imap_folder_new (const gchar *name,
5170 static gboolean missing_imap_warning = TRUE;
5171 if (missing_imap_warning) {
5172 missing_imap_warning = FALSE;
5174 _("You have one or more IMAP accounts "
5175 "defined. However this version of "
5176 "Claws Mail has been built without "
5177 "IMAP support; your IMAP account(s) are "
5179 "You probably need to "
5180 "install libetpan and recompile "
5185 static gint imap_create_tree (Folder *folder)
5189 static FolderItem *imap_create_folder (Folder *folder,
5195 static gint imap_rename_folder (Folder *folder,
5202 gchar imap_get_path_separator_for_item(FolderItem *item)
5207 FolderClass *imap_get_class(void)
5209 if (imap_class.idstr == NULL) {
5210 imap_class.type = F_IMAP;
5211 imap_class.idstr = "imap";
5212 imap_class.uistr = "IMAP4";
5214 imap_class.new_folder = imap_folder_new;
5215 imap_class.create_tree = imap_create_tree;
5216 imap_class.create_folder = imap_create_folder;
5217 imap_class.rename_folder = imap_rename_folder;
5219 imap_class.set_xml = folder_set_xml;
5220 imap_class.get_xml = folder_get_xml;
5221 imap_class.item_set_xml = imap_item_set_xml;
5222 imap_class.item_get_xml = imap_item_get_xml;
5223 /* nothing implemented */
5229 void imap_disconnect_all(void)
5233 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
5238 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
5243 void imap_cache_msg(FolderItem *item, gint msgnum)
5247 void imap_cancel_all(void)
5251 gboolean imap_cancel_all_enabled(void)
5258 #ifdef HAVE_LIBETPAN
5259 static void imap_synchronise(FolderItem *item, gint days)
5261 if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
5262 debug_print("%s already synced\n", item->path?item->path:item->name);
5265 debug_print("syncing %s\n", item->path?item->path:item->name);
5266 imap_gtk_synchronise(item, days);
5267 IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
5271 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
5273 #ifdef HAVE_LIBETPAN
5276 folder_item_set_xml(folder, item, tag);
5278 #ifdef HAVE_LIBETPAN
5279 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5280 XMLAttr *attr = (XMLAttr *) cur->data;
5282 if (!attr || !attr->name || !attr->value) continue;
5283 if (!strcmp(attr->name, "uidnext"))
5284 IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5285 if (!strcmp(attr->name, "last_sync"))
5286 IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5287 if (!strcmp(attr->name, "last_change"))
5288 IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5290 if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5291 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5295 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5299 tag = folder_item_get_xml(folder, item);
5301 #ifdef HAVE_LIBETPAN
5302 xml_tag_add_attr(tag, xml_attr_new_int("uidnext",
5303 IMAP_FOLDER_ITEM(item)->uid_next));
5304 xml_tag_add_attr(tag, xml_attr_new_int("last_sync",
5305 IMAP_FOLDER_ITEM(item)->last_sync));
5306 xml_tag_add_attr(tag, xml_attr_new_int("last_change",
5307 IMAP_FOLDER_ITEM(item)->last_change));