2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
34 #include "alertpanel.h"
57 #include "procheader.h"
58 #include "prefs_account.h"
62 #include "prefs_common.h"
63 #include "inputdialog.h"
65 #include "remotefolder.h"
67 #include "statusbar.h"
69 #include "imap-thread.h"
73 #include "passwordstore.h"
75 typedef struct _IMAPFolder IMAPFolder
;
76 typedef struct _IMAPSession IMAPSession
;
77 typedef struct _IMAPNameSpace IMAPNameSpace
;
78 typedef struct _IMAPFolderItem IMAPFolderItem
;
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
;
95 gchar
*search_charset
;
96 gboolean search_charset_supported
;
103 gboolean authenticated
;
112 gboolean folder_content_changed
;
123 gboolean sens_update_block
;
127 struct _IMAPNameSpace
133 #define IMAPBUFSIZE 8192
135 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
136 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
137 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
138 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
139 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
140 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
141 #define IMAP_IS_SPAM(flags) ((flags & IMAP_FLAG_SPAM) != 0)
142 #define IMAP_IS_HAM(flags) ((flags & IMAP_FLAG_HAM) != 0)
145 #define IMAP4_PORT 143
147 #define IMAPS_PORT 993
150 #define IMAP_CMD_LIMIT 1000
153 ITEM_CAN_CREATE_FLAGS_UNKNOWN
= 0,
154 ITEM_CAN_CREATE_FLAGS
,
155 ITEM_CANNOT_CREATE_FLAGS
158 struct _IMAPFolderItem
167 GHashTable
*flags_set_table
;
168 GHashTable
*flags_unset_table
;
171 gboolean should_update
;
172 gboolean should_trash_cache
;
173 gint can_create_flags
;
175 GHashTable
*tags_set_table
;
176 GHashTable
*tags_unset_table
;
181 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
);
182 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
);
184 static void imap_folder_init (Folder
*folder
,
188 static Folder
*imap_folder_new (const gchar
*name
,
190 static void imap_folder_destroy (Folder
*folder
);
192 static IMAPSession
*imap_session_new (Folder
*folder
,
193 const PrefsAccount
*account
);
194 static gint
imap_session_authenticate(IMAPSession
*session
,
195 PrefsAccount
*account
);
196 static void imap_session_destroy (Session
*session
);
198 static gchar
*imap_fetch_msg (Folder
*folder
,
201 static gchar
*imap_fetch_msg_full (Folder
*folder
,
206 static void imap_remove_cached_msg (Folder
*folder
,
209 static gint
imap_add_msg (Folder
*folder
,
213 static gint
imap_add_msgs (Folder
*folder
,
216 GHashTable
*relation
);
218 static gint
imap_copy_msg (Folder
*folder
,
221 static gint
imap_copy_msgs (Folder
*folder
,
223 MsgInfoList
*msglist
,
224 GHashTable
*relation
);
226 static gint
search_msgs (Folder
*folder
,
227 FolderItem
*container
,
228 MsgNumberList
**msgs
,
230 MatcherList
*predicate
,
231 SearchProgressNotify progress_cb
,
232 gpointer progress_data
);
234 static gint
imap_remove_msg (Folder
*folder
,
237 static gint
imap_remove_msgs (Folder
*folder
,
239 MsgInfoList
*msglist
,
240 GHashTable
*relation
);
241 static gint
imap_expunge (Folder
*folder
,
243 static gint
imap_remove_all_msg (Folder
*folder
,
246 static gboolean
imap_is_msg_changed (Folder
*folder
,
250 static gint
imap_close (Folder
*folder
,
253 static gint
imap_scan_tree (Folder
*folder
);
255 static gint
imap_create_tree (Folder
*folder
);
257 static FolderItem
*imap_create_folder (Folder
*folder
,
260 static gint
imap_rename_folder (Folder
*folder
,
263 static gint
imap_remove_folder (Folder
*folder
,
266 static FolderItem
*imap_folder_item_new (Folder
*folder
);
267 static void imap_folder_item_destroy (Folder
*folder
,
270 static IMAPSession
*imap_session_get (Folder
*folder
);
272 static gint
imap_auth (IMAPSession
*session
,
277 static gint
imap_scan_tree_recursive (IMAPSession
*session
,
281 static void imap_create_missing_folders (Folder
*folder
);
282 static FolderItem
*imap_create_special_folder
284 SpecialFolderItemType stype
,
287 static gint
imap_do_copy_msgs (Folder
*folder
,
289 MsgInfoList
*msglist
,
290 GHashTable
*relation
,
291 gboolean same_dest_ok
);
293 static gint
imap_do_remove_msgs (Folder
*folder
,
295 MsgInfoList
*msglist
,
296 GHashTable
*relation
);
298 static void imap_delete_all_cached_messages (FolderItem
*item
);
299 static void imap_set_batch (Folder
*folder
,
302 static gint
imap_set_message_flags (IMAPSession
*session
,
303 IMAPFolderItem
*item
,
304 MsgNumberList
*numlist
,
308 static gint
imap_select (IMAPSession
*session
,
314 guint32
*uid_validity
,
315 gint
*can_create_flags
,
317 static gint
imap_status (IMAPSession
*session
,
320 IMAPFolderItem
*item
,
323 guint32
*uid_validity
,
326 static void imap_commit_tags (FolderItem
*item
,
331 static gchar
imap_get_path_separator (IMAPSession
*session
,
335 static gchar
*imap_get_real_path (IMAPSession
*session
,
340 static void imap_synchronise (FolderItem
*item
, gint days
);
342 static gboolean
imap_is_busy (Folder
*folder
);
344 static void imap_free_capabilities (IMAPSession
*session
);
346 /* low-level IMAP4rev1 commands */
347 static gint
imap_cmd_login (IMAPSession
*session
,
351 static gint
imap_cmd_noop (IMAPSession
*session
);
353 static gint
imap_cmd_starttls (IMAPSession
*session
);
355 static gint
imap_cmd_select (IMAPSession
*session
,
360 guint32
*uid_validity
,
361 gint
*can_create_flags
,
364 static gint
imap_cmd_close (IMAPSession
*session
);
365 static gint
imap_cmd_examine (IMAPSession
*session
,
370 guint32
*uid_validity
,
372 static gint
imap_cmd_create (IMAPSession
*sock
,
373 const gchar
*folder
);
374 static gint
imap_cmd_rename (IMAPSession
*sock
,
375 const gchar
*oldfolder
,
376 const gchar
*newfolder
);
377 static gint
imap_cmd_delete (IMAPSession
*session
,
378 const gchar
*folder
);
379 static gint
imap_cmd_fetch (IMAPSession
*sock
,
381 const gchar
*filename
,
384 static gint
imap_cmd_append (IMAPSession
*session
,
385 IMAPFolderItem
*item
,
386 const gchar
*destfolder
,
390 static gint
imap_cmd_copy (IMAPSession
*session
,
391 struct mailimap_set
* set
,
392 const gchar
*destfolder
,
393 struct mailimap_set
** source
,
394 struct mailimap_set
** dest
);
395 static gint
imap_cmd_store (IMAPSession
*session
,
396 IMAPFolderItem
*item
,
397 struct mailimap_set
* set
,
401 static gint
imap_cmd_expunge (IMAPSession
*session
);
403 static void imap_path_separator_subst (gchar
*str
,
406 static gboolean
imap_rename_folder_func (GNode
*node
,
408 static gint
imap_get_num_list (Folder
*folder
,
411 gboolean
*old_uids_valid
);
412 static GSList
*imap_get_msginfos (Folder
*folder
,
414 GSList
*msgnum_list
);
415 static MsgInfo
*imap_get_msginfo (Folder
*folder
,
418 static gboolean
imap_scan_required (Folder
*folder
,
420 static void imap_change_flags (Folder
*folder
,
423 MsgPermFlags newflags
);
424 static gint
imap_get_flags (Folder
*folder
,
426 MsgInfoList
*msglist
,
427 GHashTable
*msgflags
);
428 static gchar
*imap_item_get_path (Folder
*folder
,
430 static MsgInfo
*imap_parse_msg(const gchar
*file
, FolderItem
*item
);
433 /* data types conversion libetpan <-> claws */
434 static GSList
* imap_list_from_lep(IMAPFolder
* folder
,
435 clist
* list
, const gchar
* real_path
, gboolean all
);
436 static GSList
* imap_get_lep_set_from_numlist(IMAPFolder
*folder
, MsgNumberList
*numlist
);
437 static GSList
* imap_get_lep_set_from_msglist(IMAPFolder
*folder
, MsgInfoList
*msglist
);
438 static GSList
* imap_uid_list_from_lep(clist
* list
, gint
* length
);
439 static GSList
* imap_uid_list_from_lep_tab(carray
* list
);
440 static void imap_flags_hash_from_lep_uid_flags_tab(carray
* list
,
442 GHashTable
*tags_hash
);
443 static MsgInfo
*imap_envelope_from_lep(struct imap_fetch_env_info
* info
,
445 static void imap_lep_set_free(GSList
*seq_list
);
446 static struct mailimap_flag_list
* imap_flag_to_lep(IMAPFolderItem
*item
, IMAPFlags flags
, GSList
*tags
);
448 typedef struct _hashtable_data
{
450 IMAPFolderItem
*item
;
453 static FolderClass imap_class
;
455 FolderClass
*imap_get_class(void)
457 if (imap_class
.idstr
== NULL
) {
458 imap_class
.type
= F_IMAP
;
459 imap_class
.idstr
= "imap";
460 imap_class
.uistr
= "IMAP";
461 imap_class
.supports_server_search
= TRUE
;
463 /* Folder functions */
464 imap_class
.new_folder
= imap_folder_new
;
465 imap_class
.destroy_folder
= imap_folder_destroy
;
466 imap_class
.scan_tree
= imap_scan_tree
;
467 imap_class
.create_tree
= imap_create_tree
;
469 /* FolderItem functions */
470 imap_class
.item_new
= imap_folder_item_new
;
471 imap_class
.item_destroy
= imap_folder_item_destroy
;
472 imap_class
.item_get_path
= imap_item_get_path
;
473 imap_class
.create_folder
= imap_create_folder
;
474 imap_class
.rename_folder
= imap_rename_folder
;
475 imap_class
.remove_folder
= imap_remove_folder
;
476 imap_class
.close
= imap_close
;
477 imap_class
.get_num_list
= imap_get_num_list
;
478 imap_class
.scan_required
= imap_scan_required
;
479 imap_class
.set_xml
= folder_set_xml
;
480 imap_class
.get_xml
= folder_get_xml
;
481 imap_class
.item_set_xml
= imap_item_set_xml
;
482 imap_class
.item_get_xml
= imap_item_get_xml
;
484 /* Message functions */
485 imap_class
.get_msginfo
= imap_get_msginfo
;
486 imap_class
.get_msginfos
= imap_get_msginfos
;
487 imap_class
.fetch_msg
= imap_fetch_msg
;
488 imap_class
.fetch_msg_full
= imap_fetch_msg_full
;
489 imap_class
.add_msg
= imap_add_msg
;
490 imap_class
.add_msgs
= imap_add_msgs
;
491 imap_class
.copy_msg
= imap_copy_msg
;
492 imap_class
.copy_msgs
= imap_copy_msgs
;
493 imap_class
.search_msgs
= search_msgs
;
494 imap_class
.remove_msg
= imap_remove_msg
;
495 imap_class
.remove_msgs
= imap_remove_msgs
;
496 imap_class
.expunge
= imap_expunge
;
497 imap_class
.remove_all_msg
= imap_remove_all_msg
;
498 imap_class
.is_msg_changed
= imap_is_msg_changed
;
499 imap_class
.change_flags
= imap_change_flags
;
500 imap_class
.get_flags
= imap_get_flags
;
501 imap_class
.set_batch
= imap_set_batch
;
502 imap_class
.synchronise
= imap_synchronise
;
503 imap_class
.remove_cached_msg
= imap_remove_cached_msg
;
504 imap_class
.commit_tags
= imap_commit_tags
;
510 static void imap_refresh_sensitivity (IMAPSession
*session
)
514 if (session
->sens_update_block
)
516 mainwin
= mainwindow_get_mainwindow();
518 toolbar_main_set_sensitive(mainwin
);
519 main_window_set_menu_sensitive(mainwin
);
523 static void lock_session(IMAPSession
*session
)
526 debug_print("locking session %p (%d)\n", session
, session
->busy
);
528 debug_print(" SESSION WAS LOCKED !! \n");
529 session
->busy
= TRUE
;
530 imap_refresh_sensitivity(session
);
532 debug_print("can't lock null session\n");
536 static void unlock_session(IMAPSession
*session
)
539 debug_print("unlocking session %p\n", session
);
540 session
->busy
= FALSE
;
541 imap_refresh_sensitivity(session
);
543 debug_print("can't unlock null session\n");
547 static gboolean
imap_ping(gpointer data
)
549 Session
*session
= (Session
*)data
;
550 IMAPSession
*imap_session
= IMAP_SESSION(session
);
553 if (session
->state
!= SESSION_READY
)
555 if (imap_session
->busy
|| !imap_session
->authenticated
)
558 lock_session(imap_session
);
559 r
= imap_cmd_noop(imap_session
);
560 unlock_session(imap_session
);
562 return r
== MAILIMAP_NO_ERROR
;
565 static void imap_disc_session_destroy(IMAPSession
*session
)
567 RemoteFolder
*rfolder
= NULL
;
572 rfolder
= REMOTE_FOLDER(IMAP_SESSION(session
)->folder
);
576 log_warning(LOG_PROTOCOL
, _("IMAP connection broken\n"));
577 SESSION(session
)->state
= SESSION_DISCONNECTED
;
578 SESSION(session
)->sock
= NULL
;
581 static void imap_safe_destroy(IMAPSession
*session
)
584 session_destroy(SESSION(session
));
586 session
->do_destroy
= TRUE
;
589 static gboolean
is_fatal(int libetpan_errcode
)
591 switch(libetpan_errcode
) {
592 case MAILIMAP_ERROR_STREAM
:
593 case MAILIMAP_ERROR_PROTOCOL
:
594 case MAILIMAP_ERROR_PARSE
:
595 case MAILIMAP_ERROR_BAD_STATE
:
602 static void imap_handle_error(Session
*session
, const gchar
*server
, int libetpan_errcode
)
604 const gchar
*session_server
= (session
? session
->server
: NULL
);
606 if (session_server
== NULL
)
607 session_server
= server
;
608 if (session_server
== NULL
)
609 session_server
= "(null)";
611 switch(libetpan_errcode
) {
612 case MAILIMAP_NO_ERROR
:
614 case MAILIMAP_NO_ERROR_AUTHENTICATED
:
615 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("authenticated"), "\n", NULL
), session_server
);
617 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED
:
618 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("not authenticated"), "\n", NULL
), session_server
);
620 case MAILIMAP_ERROR_BAD_STATE
:
621 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("bad state"), "\n", NULL
), session_server
);
623 case MAILIMAP_ERROR_STREAM
:
624 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("stream error"), "\n", NULL
), session_server
);
626 case MAILIMAP_ERROR_PARSE
:
627 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("parse error "
628 "(very probably non-RFC compliance from the server)"), "\n", NULL
), session_server
);
630 case MAILIMAP_ERROR_CONNECTION_REFUSED
:
631 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("connection refused"), "\n", NULL
), session_server
);
633 case MAILIMAP_ERROR_MEMORY
:
634 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("memory error"), "\n", NULL
), session_server
);
636 case MAILIMAP_ERROR_FATAL
:
637 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("fatal error"), "\n", NULL
), session_server
);
639 case MAILIMAP_ERROR_PROTOCOL
:
640 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("protocol error "
641 "(very probably non-RFC compliance from the server)"), "\n", NULL
), session_server
);
643 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION
:
644 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("connection not accepted"), "\n", NULL
), session_server
);
646 case MAILIMAP_ERROR_APPEND
:
647 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("APPEND error"), "\n", NULL
), session_server
);
649 case MAILIMAP_ERROR_NOOP
:
650 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("NOOP error"), "\n", NULL
), session_server
);
652 case MAILIMAP_ERROR_LOGOUT
:
653 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("LOGOUT error"), "\n", NULL
), session_server
);
655 case MAILIMAP_ERROR_CAPABILITY
:
656 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("CAPABILITY error"), "\n", NULL
), session_server
);
658 case MAILIMAP_ERROR_CHECK
:
659 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("CHECK error"), "\n", NULL
), session_server
);
661 case MAILIMAP_ERROR_CLOSE
:
662 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("CLOSE error"), "\n", NULL
), session_server
);
664 case MAILIMAP_ERROR_EXPUNGE
:
665 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("EXPUNGE error"), "\n", NULL
), session_server
);
667 case MAILIMAP_ERROR_COPY
:
668 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("COPY error"), "\n", NULL
), session_server
);
670 case MAILIMAP_ERROR_UID_COPY
:
671 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("UID COPY error"), "\n", NULL
), session_server
);
673 case MAILIMAP_ERROR_CREATE
:
674 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("CREATE error"), "\n", NULL
), session_server
);
676 case MAILIMAP_ERROR_DELETE
:
677 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("DELETE error"), "\n", NULL
), session_server
);
679 case MAILIMAP_ERROR_EXAMINE
:
680 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("EXAMINE error"), "\n", NULL
), session_server
);
682 case MAILIMAP_ERROR_FETCH
:
683 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("FETCH error"), "\n", NULL
), session_server
);
685 case MAILIMAP_ERROR_UID_FETCH
:
686 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("UID FETCH error"), "\n", NULL
), session_server
);
688 case MAILIMAP_ERROR_LIST
:
689 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("LIST error"), "\n", NULL
), session_server
);
691 case MAILIMAP_ERROR_LOGIN
:
692 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("LOGIN error"), "\n", NULL
), session_server
);
694 case MAILIMAP_ERROR_LSUB
:
695 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("LSUB error"), "\n", NULL
), session_server
);
697 case MAILIMAP_ERROR_RENAME
:
698 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("RENAME error"), "\n", NULL
), session_server
);
700 case MAILIMAP_ERROR_SEARCH
:
701 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("SEARCH error"), "\n", NULL
), session_server
);
703 case MAILIMAP_ERROR_UID_SEARCH
:
704 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("UID SEARCH error"), "\n", NULL
), session_server
);
706 case MAILIMAP_ERROR_SELECT
:
707 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("SELECT error"), "\n", NULL
), session_server
);
709 case MAILIMAP_ERROR_STATUS
:
710 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("STATUS error"), "\n", NULL
), session_server
);
712 case MAILIMAP_ERROR_STORE
:
713 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("STORE error"), "\n", NULL
), session_server
);
715 case MAILIMAP_ERROR_UID_STORE
:
716 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("UID STORE error"), "\n", NULL
), session_server
);
718 case MAILIMAP_ERROR_SUBSCRIBE
:
719 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("SUBSCRIBE error"), "\n", NULL
), session_server
);
721 case MAILIMAP_ERROR_UNSUBSCRIBE
:
722 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("UNSUBSCRIBE error"), "\n", NULL
), session_server
);
724 case MAILIMAP_ERROR_STARTTLS
:
725 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("STARTTLS error"), "\n", NULL
), session_server
);
727 case MAILIMAP_ERROR_INVAL
:
728 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("INVAL error"), "\n", NULL
), session_server
);
730 case MAILIMAP_ERROR_EXTENSION
:
731 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("EXTENSION error"), "\n", NULL
), session_server
);
733 case MAILIMAP_ERROR_SASL
:
734 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("SASL error"), "\n", NULL
), session_server
);
737 case MAILIMAP_ERROR_SSL
:
738 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("SSL/TLS error"), "\n", NULL
), session_server
);
742 log_warning(LOG_PROTOCOL
, g_strconcat(_("IMAP error on %s:"), " ", _("Unknown error [%d]"), "\n", NULL
),
743 session_server
, libetpan_errcode
);
747 if (session
&& is_fatal(libetpan_errcode
)) {
748 imap_disc_session_destroy(IMAP_SESSION(session
));
749 } else if (session
&& !is_fatal(libetpan_errcode
)) {
750 if (IMAP_SESSION(session
)->busy
)
751 unlock_session(IMAP_SESSION(session
));
755 static Folder
*imap_folder_new(const gchar
*name
, const gchar
*path
)
759 folder
= (Folder
*)g_new0(IMAPFolder
, 1);
760 folder
->klass
= &imap_class
;
761 imap_folder_init(folder
, name
, path
);
766 static void imap_folder_destroy(Folder
*folder
)
768 while (imap_folder_get_refcnt(folder
) > 0)
769 gtk_main_iteration();
771 g_free(IMAP_FOLDER(folder
)->search_charset
);
773 folder_remote_folder_destroy(REMOTE_FOLDER(folder
));
777 static void imap_folder_init(Folder
*folder
, const gchar
*name
,
780 folder_remote_folder_init((Folder
*)folder
, name
, path
);
781 IMAP_FOLDER(folder
)->max_set_size
= IMAP_SET_MAX_COUNT
;
782 IMAP_FOLDER(folder
)->search_charset_supported
= TRUE
;
783 IMAP_FOLDER(folder
)->search_charset
= g_strdup(conv_get_locale_charset_str_no_utf8());
786 static FolderItem
*imap_folder_item_new(Folder
*folder
)
788 IMAPFolderItem
*item
;
790 item
= g_new0(IMAPFolderItem
, 1);
793 item
->uid_list
= NULL
;
795 return (FolderItem
*)item
;
798 static void imap_folder_item_destroy(Folder
*folder
, FolderItem
*_item
)
800 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
802 g_return_if_fail(item
!= NULL
);
803 g_slist_free(item
->uid_list
);
808 static gboolean
imap_reset_uid_lists_func(GNode
*node
, gpointer data
)
810 IMAPFolderItem
*item
= (IMAPFolderItem
*)node
->data
;
813 g_slist_free(item
->uid_list
);
814 item
->uid_list
= NULL
;
819 static void imap_reset_uid_lists(Folder
*folder
)
821 if(folder
->node
== NULL
)
824 /* Destroy all uid lists and rest last uid */
825 g_node_traverse(folder
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, imap_reset_uid_lists_func
, NULL
);
828 static int imap_get_capabilities(IMAPSession
*session
)
830 struct mailimap_capability_data
*capabilities
= NULL
;
834 if (session
->capability
!= NULL
)
835 return MAILIMAP_NO_ERROR
;
837 result
= imap_threaded_capability(session
->folder
, &capabilities
);
839 if (result
!= MAILIMAP_NO_ERROR
) {
843 if (capabilities
== NULL
|| capabilities
->cap_list
== NULL
) {
844 return MAILIMAP_NO_ERROR
;
847 for(cur
= clist_begin(capabilities
->cap_list
) ; cur
!= NULL
;
848 cur
= clist_next(cur
)) {
849 struct mailimap_capability
* cap
=
851 if (!cap
|| cap
->cap_data
.cap_name
== NULL
)
853 session
->capability
= g_slist_append
854 (session
->capability
,
855 g_strdup(cap
->cap_data
.cap_name
));
856 debug_print("got capa %s\n", cap
->cap_data
.cap_name
);
858 mailimap_capability_data_free(capabilities
);
859 return MAILIMAP_NO_ERROR
;
862 static gboolean
imap_has_capability(IMAPSession
*session
, const gchar
*cap
)
865 for (cur
= session
->capability
; cur
; cur
= cur
->next
) {
866 if (!g_ascii_strcasecmp(cur
->data
, cap
))
872 static gint
imap_auth(IMAPSession
*session
, const gchar
*user
, const gchar
*pass
,
875 gint ok
= MAILIMAP_ERROR_LOGIN
;
876 static time_t last_login_err
= 0;
877 gchar
*ext_info
= "";
879 gchar
*server
= NULL
;
880 if ((r
= imap_get_capabilities(session
)) != MAILIMAP_NO_ERROR
) {
881 imap_handle_error(SESSION(session
), NULL
, r
);
884 server
= g_strdup(SESSION(session
)->server
);
887 ok
= imap_cmd_login(session
, user
, pass
, "ANONYMOUS");
889 case IMAP_AUTH_CRAM_MD5
:
890 ok
= imap_cmd_login(session
, user
, pass
, "CRAM-MD5");
892 case IMAP_AUTH_DIGEST_MD5
:
893 ok
= imap_cmd_login(session
, user
, pass
, "DIGEST-MD5");
895 case IMAP_AUTH_SCRAM_SHA1
:
896 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-1");
898 case IMAP_AUTH_PLAIN
:
899 ok
= imap_cmd_login(session
, user
, pass
, "PLAIN");
901 case IMAP_AUTH_LOGIN
:
902 ok
= imap_cmd_login(session
, user
, pass
, "LOGIN");
904 case IMAP_AUTH_PLAINTEXT
:
905 ok
= imap_cmd_login(session
, user
, pass
, "plaintext");
907 case IMAP_AUTH_GSSAPI
:
908 ok
= imap_cmd_login(session
, user
, pass
, "GSSAPI");
911 debug_print("capabilities:\n"
915 "\t SCRAM-SHA-1 %d\n"
919 imap_has_capability(session
, "ANONYMOUS"),
920 imap_has_capability(session
, "CRAM-MD5"),
921 imap_has_capability(session
, "DIGEST-MD5"),
922 imap_has_capability(session
, "SCRAM-SHA-1"),
923 imap_has_capability(session
, "PLAIN"),
924 imap_has_capability(session
, "LOGIN"),
925 imap_has_capability(session
, "GSSAPI"));
926 if (imap_has_capability(session
, "CRAM-MD5"))
927 ok
= imap_cmd_login(session
, user
, pass
, "CRAM-MD5");
928 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "DIGEST-MD5"))
929 ok
= imap_cmd_login(session
, user
, pass
, "DIGEST-MD5");
930 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-1"))
931 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-1");
932 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "PLAIN"))
933 ok
= imap_cmd_login(session
, user
, pass
, "PLAIN");
934 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "LOGIN"))
935 ok
= imap_cmd_login(session
, user
, pass
, "LOGIN");
936 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "GSSAPI"))
937 ok
= imap_cmd_login(session
, user
, pass
, "GSSAPI");
938 if (ok
== MAILIMAP_ERROR_LOGIN
) /* we always try plaintext login before giving up */
939 ok
= imap_cmd_login(session
, user
, pass
, "plaintext");
942 if (ok
== MAILIMAP_NO_ERROR
)
943 session
->authenticated
= TRUE
;
945 if (type
== IMAP_AUTH_CRAM_MD5
) {
946 ext_info
= _("\n\nCRAM-MD5 logins only work if libetpan has been "
947 "compiled with SASL support and the "
948 "CRAM-MD5 SASL plugin is installed.");
951 if (type
== IMAP_AUTH_DIGEST_MD5
) {
952 ext_info
= _("\n\nDIGEST-MD5 logins only work if libetpan has been "
953 "compiled with SASL support and the "
954 "DIGEST-MD5 SASL plugin is installed.");
957 if (type
== IMAP_AUTH_SCRAM_SHA1
) {
958 ext_info
= _("\n\nSCRAM-SHA-1 logins only work if libetpan has been "
959 "compiled with SASL support and the "
960 "SCRAM SASL plugin is installed.");
963 if (type
== IMAP_AUTH_PLAIN
) {
964 ext_info
= _("\n\nPLAIN logins only work if libetpan has been "
965 "compiled with SASL support and the "
966 "PLAIN SASL plugin is installed.");
969 if (type
== IMAP_AUTH_LOGIN
) {
970 ext_info
= _("\n\nLOGIN logins only work if libetpan has been "
971 "compiled with SASL support and the "
972 "LOGIN SASL plugin is installed.");
975 if (time(NULL
) - last_login_err
> 10) {
976 if (!prefs_common
.no_recv_err_panel
) {
977 alertpanel_error_log(_("Connection to %s failed: "
981 log_error(LOG_PROTOCOL
, _("Connection to %s failed: "
982 "login refused.%s\n"),
986 last_login_err
= time(NULL
);
992 static IMAPSession
*imap_reconnect_if_possible(Folder
*folder
, IMAPSession
*session
)
994 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
995 /* Check if this is the first try to establish a
996 connection, if yes we don't try to reconnect */
997 debug_print("reconnecting\n");
998 if (rfolder
->session
== NULL
) {
999 log_warning(LOG_PROTOCOL
, _("Connecting to %s failed"),
1000 folder
->account
->recv_server
);
1001 SESSION(session
)->sock
= NULL
;
1002 imap_safe_destroy(session
);
1005 rfolder
->session
= NULL
;
1006 log_warning(LOG_PROTOCOL
, _("IMAP connection to %s has been"
1007 " disconnected. Reconnecting...\n"),
1008 folder
->account
->recv_server
);
1009 statusbar_print_all(_("IMAP connection to %s has been"
1010 " disconnected. Reconnecting...\n"),
1011 folder
->account
->recv_server
);
1012 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1013 SESSION(session
)->sock
= NULL
;
1014 imap_safe_destroy(session
);
1015 /* Clear folders session to make imap_session_get create
1016 a new session, because of rfolder->session == NULL
1017 it will not try to reconnect again and so avoid an
1019 debug_print("getting session...\n");
1020 session
= imap_session_get(folder
);
1021 rfolder
->session
= SESSION(session
);
1022 statusbar_pop_all();
1027 static IMAPSession
*imap_session_get(Folder
*folder
)
1029 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
1030 IMAPSession
*session
= NULL
;
1031 gint r
= MAILIMAP_NO_ERROR
;
1033 g_return_val_if_fail(folder
!= NULL
, NULL
);
1034 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, NULL
);
1035 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
1037 if (prefs_common
.work_offline
&&
1038 !inc_offline_should_override(FALSE
,
1039 _("Claws Mail needs network access in order "
1040 "to access the IMAP server."))) {
1044 /* check for deferred destroy */
1045 if (rfolder
->session
!= NULL
) {
1046 session
= IMAP_SESSION(rfolder
->session
);
1047 if (!session
->busy
&& session
->do_destroy
) {
1048 rfolder
->session
= NULL
;
1049 imap_safe_destroy(session
);
1054 /* Make sure we have a session */
1055 if (rfolder
->session
!= NULL
&& rfolder
->session
->state
!= SESSION_DISCONNECTED
) {
1056 session
= IMAP_SESSION(rfolder
->session
);
1057 } else if (rfolder
->session
!= NULL
&& rfolder
->session
->state
== SESSION_DISCONNECTED
) {
1058 imap_safe_destroy(IMAP_SESSION(rfolder
->session
));
1059 rfolder
->session
= NULL
;
1061 } else if (rfolder
->connecting
) {
1062 debug_print("already connecting\n");
1066 imap_reset_uid_lists(folder
);
1067 if (time(NULL
) - rfolder
->last_failure
<= 2)
1069 rfolder
->connecting
= TRUE
;
1070 session
= imap_session_new(folder
, folder
->account
);
1072 if(session
== NULL
) {
1073 rfolder
->last_failure
= time(NULL
);
1074 rfolder
->connecting
= FALSE
;
1078 /* Make sure session is authenticated */
1079 if (!IMAP_SESSION(session
)->authenticated
)
1080 r
= imap_session_authenticate(IMAP_SESSION(session
), folder
->account
);
1082 if (r
!= MAILIMAP_NO_ERROR
|| (!is_fatal(r
) && !IMAP_SESSION(session
)->authenticated
)) {
1083 rfolder
->session
= NULL
;
1085 imap_threaded_disconnect(session
->folder
);
1087 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1088 SESSION(session
)->sock
= NULL
;
1089 imap_safe_destroy(session
);
1090 rfolder
->last_failure
= time(NULL
);
1091 rfolder
->connecting
= FALSE
;
1095 /* I think the point of this code is to avoid sending a
1096 * keepalive if we've used the session recently and therefore
1097 * think it's still alive. Unfortunately, most of the code
1098 * does not yet check for errors on the socket, and so if the
1099 * connection drops we don't notice until the timeout expires.
1100 * A better solution than sending a NOOP every time would be
1101 * for every command to be prepared to retry until it is
1102 * successfully sent. -- mbp */
1103 if ((time(NULL
) - SESSION(session
)->last_access_time
> SESSION_TIMEOUT_INTERVAL
) || session
->cancelled
) {
1104 /* verify that the session is still alive */
1105 r
= imap_cmd_noop(session
);
1107 if (r
!= MAILIMAP_NO_ERROR
) {
1108 debug_print("disconnected!\n");
1110 session
= imap_reconnect_if_possible(folder
, session
);
1112 rfolder
->session
= NULL
;
1113 rfolder
->connecting
= FALSE
;
1114 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1115 SESSION(session
)->sock
= NULL
;
1116 imap_safe_destroy(session
);
1117 session
= imap_session_get(folder
);
1121 session
->cancelled
= FALSE
;
1124 rfolder
->session
= SESSION(session
);
1125 rfolder
->connecting
= FALSE
;
1127 return IMAP_SESSION(session
);
1130 static IMAPSession
*imap_session_new(Folder
* folder
,
1131 const PrefsAccount
*account
)
1133 IMAPSession
*session
;
1136 int authenticated
= FALSE
;
1140 /* FIXME: IMAP over SSL only... */
1143 port
= account
->set_imapport
? account
->imapport
1144 : account
->ssl_imap
== SSL_TUNNEL
? IMAPS_PORT
: IMAP4_PORT
;
1145 ssl_type
= account
->ssl_imap
;
1147 if (account
->ssl_imap
!= SSL_NONE
) {
1148 if (alertpanel_full(_("Insecure connection"),
1149 _("This connection is configured to be secured "
1150 "using SSL/TLS, but SSL/TLS is not available "
1151 "in this build of Claws Mail. \n\n"
1152 "Do you want to continue connecting to this "
1153 "server? The communication would not be "
1155 GTK_STOCK_CANCEL
, _("Con_tinue connecting"),
1156 NULL
, FALSE
, NULL
, ALERT_WARNING
,
1157 G_ALERTDEFAULT
) != G_ALERTALTERNATE
)
1160 port
= account
->set_imapport
? account
->imapport
1165 buf
= g_strdup_printf(_("Account '%s': Connecting to IMAP server: %s:%d..."),
1166 folder
->account
->account_name
, folder
->account
->recv_server
,
1168 statuswindow_print_all("%s", buf
);
1169 log_message(LOG_PROTOCOL
, "%s\n", buf
);
1173 if (account
->set_tunnelcmd
) {
1174 r
= imap_threaded_connect_cmd(folder
,
1176 account
->recv_server
,
1183 if (ssl_type
== SSL_TUNNEL
) {
1184 r
= imap_threaded_connect_ssl(folder
,
1185 account
->recv_server
,
1191 r
= imap_threaded_connect(folder
,
1192 account
->recv_server
,
1197 statuswindow_pop_all();
1198 if (r
== MAILIMAP_NO_ERROR_AUTHENTICATED
) {
1199 authenticated
= TRUE
;
1201 else if (r
== MAILIMAP_NO_ERROR_NON_AUTHENTICATED
) {
1202 authenticated
= FALSE
;
1206 if (r
== MAILIMAP_ERROR_SSL
)
1207 log_error(LOG_PROTOCOL
, _("SSL/TLS handshake failed\n"));
1210 imap_handle_error(NULL
, account
->recv_server
, r
);
1212 if(!prefs_common
.no_recv_err_panel
) {
1213 alertpanel_error_log(_("Can't connect to IMAP server: %s:%d"),
1214 account
->recv_server
, port
);
1216 log_error(LOG_PROTOCOL
, _("Can't connect to IMAP server: %s:%d\n"),
1217 account
->recv_server
, port
);
1223 session
= g_new0(IMAPSession
, 1);
1224 session_init(SESSION(session
), account
, FALSE
);
1225 SESSION(session
)->type
= SESSION_IMAP
;
1226 SESSION(session
)->server
= g_strdup(account
->recv_server
);
1227 SESSION(session
)->port
= port
;
1228 SESSION(session
)->sock
= NULL
;
1230 SESSION(session
)->destroy
= imap_session_destroy
;
1232 session
->capability
= NULL
;
1234 session
->authenticated
= authenticated
;
1235 session
->mbox
= NULL
;
1236 session
->exists
= 0;
1237 session
->recent
= 0;
1238 session
->expunge
= 0;
1239 session
->cmd_count
= 0;
1240 session
->folder
= folder
;
1241 IMAP_FOLDER(session
->folder
)->last_seen_separator
= 0;
1244 if (account
->ssl_imap
== SSL_STARTTLS
) {
1247 ok
= imap_cmd_starttls(session
);
1248 if (ok
!= MAILIMAP_NO_ERROR
) {
1249 log_warning(LOG_PROTOCOL
, _("Can't start STARTTLS session.\n"));
1250 if (!is_fatal(ok
)) {
1251 SESSION(session
)->sock
= NULL
;
1252 imap_safe_destroy(session
);
1257 imap_free_capabilities(session
);
1258 session
->authenticated
= FALSE
;
1259 session
->uidplus
= FALSE
;
1260 session
->cmd_count
= 1;
1263 log_message(LOG_PROTOCOL
, "IMAP connection is %s-authenticated\n",
1264 (session
->authenticated
) ? "pre" : "un");
1266 session_register_ping(SESSION(session
), imap_ping
);
1271 static gint
imap_session_authenticate(IMAPSession
*session
,
1272 PrefsAccount
*account
)
1274 gchar
*pass
, *acc_pass
= NULL
;
1275 gboolean failed
= FALSE
;
1276 gint ok
= MAILIMAP_NO_ERROR
;
1277 g_return_val_if_fail(account
->userid
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
1279 if (!password_get(account
->userid
, account
->recv_server
, "imap",
1280 SESSION(session
)->port
, &acc_pass
)) {
1281 acc_pass
= passwd_store_get_account(account
->account_id
,
1287 if (!pass
&& account
->imap_auth_type
!= IMAP_AUTH_ANON
&& account
->imap_auth_type
!= IMAP_AUTH_GSSAPI
) {
1289 tmp_pass
= input_dialog_query_password_keep(account
->recv_server
,
1291 &(account
->session_passwd
));
1293 return MAILIMAP_NO_ERROR
;
1295 Xstrdup_a(pass
, tmp_pass
, {g_free(tmp_pass
); return MAILIMAP_NO_ERROR
;});
1297 } else if (account
->imap_auth_type
== IMAP_AUTH_ANON
|| account
->imap_auth_type
== IMAP_AUTH_GSSAPI
) {
1300 if ((ok
= imap_auth(session
, account
->userid
, pass
, account
->imap_auth_type
)) != MAILIMAP_NO_ERROR
) {
1302 if (!failed
&& !is_fatal(ok
)) {
1303 if (acc_pass
!= NULL
) {
1304 memset(acc_pass
, 0, strlen(acc_pass
));
1309 if (account
->session_passwd
!= NULL
) {
1310 g_free(account
->session_passwd
);
1311 account
->session_passwd
= NULL
;
1315 if (prefs_common
.no_recv_err_panel
) {
1316 log_error(LOG_PROTOCOL
, _("Couldn't login to IMAP server %s.\n"), account
->recv_server
);
1317 mainwindow_show_error();
1319 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account
->recv_server
);
1322 if (acc_pass
!= NULL
) {
1324 memset(acc_pass
, 0, strlen(acc_pass
));
1331 memset(acc_pass
, 0, strlen(acc_pass
));
1334 statuswindow_pop_all();
1335 session
->authenticated
= TRUE
;
1336 return MAILIMAP_NO_ERROR
;
1339 static void imap_session_destroy(Session
*session
)
1341 if (session
->state
!= SESSION_DISCONNECTED
)
1342 imap_threaded_disconnect(IMAP_SESSION(session
)->folder
);
1344 imap_free_capabilities(IMAP_SESSION(session
));
1345 g_free(IMAP_SESSION(session
)->mbox
);
1348 static gchar
*imap_fetch_msg(Folder
*folder
, FolderItem
*item
, gint uid
)
1350 return imap_fetch_msg_full(folder
, item
, uid
, TRUE
, TRUE
);
1353 static guint
get_file_size_with_crs(const gchar
*filename
)
1359 if (filename
== NULL
)
1362 fp
= g_fopen(filename
, "rb");
1366 while (fgets(buf
, sizeof (buf
), fp
) != NULL
) {
1368 if (!strstr(buf
, "\r\n") && strstr(buf
, "\n"))
1376 static gchar
*imap_get_cached_filename(FolderItem
*item
, guint msgnum
)
1378 gchar
*path
, *filename
;
1380 cm_return_val_if_fail(item
!= NULL
, NULL
);
1382 path
= folder_item_get_path(item
);
1384 if (!is_dir_exist(path
)) {
1389 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msgnum
), NULL
);
1391 if (is_dir_exist(filename
)) {
1393 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, ".", itos(msgnum
), NULL
);
1400 static void imap_remove_cached_msg(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
)
1404 filename
= imap_get_cached_filename(item
, msginfo
->msgnum
);
1406 cm_return_if_fail(filename
!= NULL
);
1408 if (is_file_exist(filename
)) {
1409 claws_unlink(filename
);
1414 typedef struct _TagsData
{
1417 IMAPFolderItem
*item
;
1420 static void imap_commit_tags(FolderItem
*item
, MsgInfo
*msginfo
, GSList
*tags_set
, GSList
*tags_unset
)
1422 IMAPSession
*session
;
1423 gint ok
, can_create_tags
;
1424 Folder
*folder
= NULL
;
1425 TagsData
*ht_data
= NULL
;
1428 g_return_if_fail(item
!= NULL
);
1429 g_return_if_fail(msginfo
!= NULL
);
1431 folder
= item
->folder
;
1432 debug_print("getting session...\n");
1433 session
= imap_session_get(folder
);
1436 debug_print("can't get session\n");
1440 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
1441 NULL
, NULL
, NULL
, NULL
, &can_create_tags
, FALSE
);
1443 if (ok
!= MAILIMAP_NO_ERROR
) {
1448 if (IMAP_FOLDER_ITEM(item
)->can_create_flags
!= ITEM_CAN_CREATE_FLAGS
)
1451 if (IMAP_FOLDER_ITEM(item
)->batching
) {
1452 /* instead of performing an UID STORE command for each message change,
1453 * as a lot of them can change "together", we just fill in hashtables
1454 * and defer the treatment so that we're able to send only one
1457 debug_print("IMAP batch mode on, deferring tags change\n");
1458 for (cur
= tags_set
; cur
; cur
= cur
->next
) {
1459 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1461 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->tags_set_table
,
1462 GINT_TO_POINTER(cur_tag
));
1463 if (ht_data
== NULL
) {
1464 ht_data
= g_new0(TagsData
, 1);
1465 ht_data
->str
= g_strdup(tags_get_tag(cur_tag
));
1466 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
1467 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->tags_set_table
,
1468 GINT_TO_POINTER(cur_tag
), ht_data
);
1470 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
1473 for (cur
= tags_unset
; cur
; cur
= cur
->next
) {
1474 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1476 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->tags_unset_table
,
1477 GINT_TO_POINTER(cur_tag
));
1478 if (ht_data
== NULL
) {
1479 ht_data
= g_new0(TagsData
, 1);
1480 ht_data
->str
= g_strdup(tags_get_tag(cur_tag
));
1481 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
1482 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->tags_unset_table
,
1483 GINT_TO_POINTER(cur_tag
), ht_data
);
1485 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
1489 GSList
*list_set
= NULL
;
1490 GSList
*list_unset
= NULL
;
1493 numlist
.data
= GINT_TO_POINTER(msginfo
->msgnum
);
1494 numlist
.next
= NULL
;
1496 debug_print("IMAP changing tags NOW\n");
1497 for (cur
= tags_set
; cur
; cur
= cur
->next
) {
1498 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1499 const gchar
*str
= tags_get_tag(cur_tag
);
1500 if (IS_NOT_RESERVED_TAG(str
))
1501 list_set
= g_slist_prepend(list_set
, g_strdup(str
));
1504 ok
= imap_set_message_flags(session
,
1505 IMAP_FOLDER_ITEM(item
), &numlist
, 0, list_set
, TRUE
);
1506 slist_free_strings_full(list_set
);
1507 if (ok
!= MAILIMAP_NO_ERROR
) {
1512 for (cur
= tags_unset
; cur
; cur
= cur
->next
) {
1513 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1514 const gchar
*str
= tags_get_tag(cur_tag
);
1515 if (IS_NOT_RESERVED_TAG(str
))
1516 list_unset
= g_slist_prepend(list_unset
, g_strdup(str
));
1519 ok
= imap_set_message_flags(session
,
1520 IMAP_FOLDER_ITEM(item
), &numlist
, 0, list_unset
, FALSE
);
1521 slist_free_strings_full(list_unset
);
1522 if (ok
!= MAILIMAP_NO_ERROR
) {
1529 static gchar
*imap_fetch_msg_full(Folder
*folder
, FolderItem
*item
, gint uid
,
1530 gboolean headers
, gboolean body
)
1532 gchar
*path
, *filename
;
1533 IMAPSession
*session
;
1536 g_return_val_if_fail(folder
!= NULL
, NULL
);
1537 g_return_val_if_fail(item
!= NULL
, NULL
);
1542 path
= folder_item_get_path(item
);
1543 if (!is_dir_exist(path
)) {
1544 if(is_file_exist(path
))
1546 make_dir_hier(path
);
1550 filename
= imap_get_cached_filename(item
, uid
);
1551 debug_print("trying to fetch cached %s\n", filename
);
1553 if (is_file_exist(filename
)) {
1554 /* see whether the local file represents the whole message
1555 * or not. As the IMAP server reports size with \r chars,
1556 * we have to update the local file (UNIX \n only) size */
1557 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1558 guint have_size
= -1;
1561 debug_print("message %d has been already %scached.\n", uid
,
1562 MSG_IS_FULLY_CACHED(cached
->flags
) ? "fully ":"");
1564 if (!cached
|| !MSG_IS_FULLY_CACHED(cached
->flags
)) {
1565 have_size
= get_file_size_with_crs(filename
);
1566 if (cached
&& (cached
->size
<= have_size
|| !body
)) {
1567 ok
= file_strip_crs(filename
);
1568 if (ok
== 0 && cached
&& cached
->size
<= have_size
) {
1569 /* we have it all and stripped */
1570 debug_print("...fully cached in fact (%u/%zd); setting flag.\n",
1571 have_size
, cached
->size
);
1572 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1574 procmsg_msginfo_free(&cached
);
1576 } else if (!cached
&& time(NULL
) - get_file_mtime(filename
) < 60) {
1577 debug_print("message not cached and file recent, considering file complete\n");
1578 ok
= file_strip_crs(filename
);
1582 procmsg_msginfo_free(&cached
);
1585 if (cached
&& MSG_IS_FULLY_CACHED(cached
->flags
)) {
1586 procmsg_msginfo_free(&cached
);
1590 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1592 procmsg_msginfo_unset_flags(cached
, MSG_FULLY_CACHED
, 0);
1593 procmsg_msginfo_free(&cached
);
1597 debug_print("getting session...\n");
1598 session
= imap_session_get(folder
);
1604 session_set_access_time(SESSION(session
));
1605 lock_session(session
); /* unlocked later in the function */
1607 debug_print("IMAP fetching messages\n");
1608 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
1609 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
1610 if (ok
!= MAILIMAP_NO_ERROR
) {
1611 g_warning("can't select mailbox %s", item
->path
);
1616 session_set_access_time(SESSION(session
));
1618 debug_print("getting message %d...\n", uid
);
1619 ok
= imap_cmd_fetch(session
, (guint32
)uid
, filename
, headers
, body
);
1621 if (ok
!= MAILIMAP_NO_ERROR
) {
1622 g_warning("can't fetch message %d", uid
);
1627 session_set_access_time(SESSION(session
));
1628 unlock_session(session
);
1630 ok
= file_strip_crs(filename
);
1632 if (ok
== 0 && headers
&& body
) {
1633 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1635 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1636 procmsg_msginfo_free(&cached
);
1638 } else if (ok
== -1) {
1639 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1641 procmsg_msginfo_unset_flags(cached
, MSG_FULLY_CACHED
, 0);
1642 procmsg_msginfo_free(&cached
);
1648 static gboolean
imap_is_msg_fully_cached(Folder
*folder
, FolderItem
*item
, gint uid
)
1652 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1657 if (MSG_IS_FULLY_CACHED(cached
->flags
)) {
1658 procmsg_msginfo_free(&cached
);
1662 filename
= imap_get_cached_filename(item
, uid
);
1664 if (is_file_exist(filename
)) {
1665 if (cached
&& cached
->total_size
== cached
->size
) {
1668 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1671 size
= get_file_size_with_crs(filename
);
1674 debug_print("msg %d cached, has size %d, full should be %zd.\n", uid
, size
, cached
->size
);
1675 if (cached
&& size
>= cached
->size
) {
1676 cached
->total_size
= cached
->size
;
1677 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1678 procmsg_msginfo_free(&cached
);
1682 procmsg_msginfo_free(&cached
);
1686 void imap_cache_msg(FolderItem
*item
, gint msgnum
)
1688 Folder
*folder
= NULL
;
1692 folder
= item
->folder
;
1694 if (!imap_is_msg_fully_cached(folder
, item
, msgnum
)) {
1695 gchar
*tmp
= imap_fetch_msg_full(folder
, item
, msgnum
, TRUE
, TRUE
);
1696 debug_print("fetched %s\n", tmp
);
1701 static gint
imap_add_msg(Folder
*folder
, FolderItem
*dest
,
1702 const gchar
*file
, MsgFlags
*flags
)
1706 MsgFileInfo fileinfo
;
1708 g_return_val_if_fail(file
!= NULL
, -1);
1710 fileinfo
.msginfo
= NULL
;
1711 fileinfo
.file
= (gchar
*)file
;
1712 fileinfo
.flags
= flags
;
1713 file_list
.data
= &fileinfo
;
1714 file_list
.next
= NULL
;
1716 ret
= imap_add_msgs(folder
, dest
, &file_list
, NULL
);
1720 static gint
imap_add_msgs(Folder
*folder
, FolderItem
*dest
, GSList
*file_list
,
1721 GHashTable
*relation
)
1724 IMAPSession
*session
;
1725 guint32 last_uid
= 0;
1727 MsgFileInfo
*fileinfo
;
1728 gint ok
= MAILIMAP_NO_ERROR
;
1729 gint curnum
= 0, total
= 0;
1730 gboolean missing_uids
= FALSE
;
1732 g_return_val_if_fail(folder
!= NULL
, -1);
1733 g_return_val_if_fail(dest
!= NULL
, -1);
1734 g_return_val_if_fail(file_list
!= NULL
, -1);
1736 debug_print("getting session...\n");
1737 session
= imap_session_get(folder
);
1741 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
1746 statusbar_print_all(_("Adding messages..."));
1747 total
= g_slist_length(file_list
);
1748 for (cur
= file_list
; cur
!= NULL
; cur
= cur
->next
) {
1749 IMAPFlags iflags
= 0;
1750 guint32 new_uid
= 0;
1751 gchar
*real_file
= NULL
;
1752 fileinfo
= (MsgFileInfo
*)cur
->data
;
1754 statusbar_progress_all(curnum
, total
, total
< 10 ? 1:10);
1757 if (fileinfo
->flags
) {
1758 if (MSG_IS_MARKED(*fileinfo
->flags
))
1759 iflags
|= IMAP_FLAG_FLAGGED
;
1760 if (MSG_IS_REPLIED(*fileinfo
->flags
))
1761 iflags
|= IMAP_FLAG_ANSWERED
;
1762 if (MSG_IS_FORWARDED(*fileinfo
->flags
))
1763 iflags
|= IMAP_FLAG_FORWARDED
;
1764 if (MSG_IS_SPAM(*fileinfo
->flags
))
1765 iflags
|= IMAP_FLAG_SPAM
;
1767 iflags
|= IMAP_FLAG_HAM
;
1768 if (!MSG_IS_UNREAD(*fileinfo
->flags
))
1769 iflags
|= IMAP_FLAG_SEEN
;
1773 if (real_file
== NULL
)
1774 real_file
= g_strdup(fileinfo
->file
);
1776 if (folder_has_parent_of_type(dest
, F_QUEUE
) ||
1777 folder_has_parent_of_type(dest
, F_OUTBOX
) ||
1778 folder_has_parent_of_type(dest
, F_DRAFT
) ||
1779 folder_has_parent_of_type(dest
, F_TRASH
))
1780 iflags
|= IMAP_FLAG_SEEN
;
1782 ok
= imap_cmd_append(session
, IMAP_FOLDER_ITEM(dest
), destdir
, real_file
, iflags
,
1785 if (ok
!= MAILIMAP_NO_ERROR
) {
1786 g_warning("can't append message %s", real_file
);
1789 statusbar_progress_all(0,0,0);
1790 statusbar_pop_all();
1793 debug_print("appended new message as %d\n", new_uid
);
1794 /* put the local file in the imapcache, so that we don't
1795 * have to fetch it back later. */
1798 missing_uids
= TRUE
;
1799 debug_print("Missing UID (0)\n");
1802 gchar
*cache_path
= folder_item_get_path(dest
);
1803 if (!is_dir_exist(cache_path
))
1804 make_dir_hier(cache_path
);
1805 if (is_dir_exist(cache_path
)) {
1806 gchar
*cache_file
= g_strconcat(
1807 cache_path
, G_DIR_SEPARATOR_S
,
1808 itos(new_uid
), NULL
);
1809 copy_file(real_file
, cache_file
, TRUE
);
1810 debug_print("got UID %d, copied to cache: %s\n", new_uid
, cache_file
);
1817 if (relation
!= NULL
)
1818 g_hash_table_insert(relation
, fileinfo
->msginfo
!= NULL
?
1819 (gpointer
) fileinfo
->msginfo
: (gpointer
) fileinfo
,
1820 GINT_TO_POINTER(new_uid
));
1821 if (last_uid
< new_uid
) {
1828 statusbar_progress_all(0,0,0);
1829 statusbar_pop_all();
1834 imap_scan_required(folder
, dest
);
1836 session
= imap_session_get(folder
);
1842 ok
= imap_select(session
, IMAP_FOLDER(folder
), dest
,
1843 &a
, NULL
, NULL
, NULL
, NULL
, FALSE
);
1848 static GSList
*flatten_mailimap_set(struct mailimap_set
* set
)
1850 GSList
*result
= NULL
;
1852 guint32 start
, end
, t
;
1855 if (!set
|| !set
->set_list
)
1858 for (list
= clist_begin(set
->set_list
); list
; list
= clist_next(list
)) {
1859 struct mailimap_set_item
*item
= (struct mailimap_set_item
*)clist_content(list
);
1860 start
= item
->set_first
;
1861 end
= item
->set_last
;
1863 for (t
= start
; t
<= end
; t
++) {
1864 result
= g_slist_prepend(result
, GINT_TO_POINTER(t
));
1868 result
= g_slist_reverse(result
);
1869 if (debug_get_mode()) {
1870 debug_print("flat imap set: ");
1871 for (cur
= result
; cur
; cur
= cur
->next
) {
1872 debug_print("%d ", GPOINTER_TO_INT(cur
->data
));
1879 static gint
imap_do_copy_msgs(Folder
*folder
, FolderItem
*dest
,
1880 MsgInfoList
*msglist
, GHashTable
*relation
,
1881 gboolean same_dest_ok
)
1885 GSList
*seq_list
, *cur
;
1887 IMAPSession
*session
;
1888 gint ok
= MAILIMAP_NO_ERROR
;
1889 GHashTable
*uid_hash
;
1892 g_return_val_if_fail(folder
!= NULL
, -1);
1893 g_return_val_if_fail(dest
!= NULL
, -1);
1894 g_return_val_if_fail(msglist
!= NULL
, -1);
1896 debug_print("getting session...\n");
1897 session
= imap_session_get(folder
);
1903 msginfo
= (MsgInfo
*)msglist
->data
;
1904 src
= msginfo
->folder
;
1905 if (!same_dest_ok
&& src
== dest
) {
1906 g_warning("the src folder is identical to the dest.");
1910 if (src
->folder
!= dest
->folder
) {
1911 GSList
*infolist
= NULL
, *cur
;
1913 for (cur
= msglist
; cur
; cur
= cur
->next
) {
1914 msginfo
= (MsgInfo
*)cur
->data
;
1915 MsgFileInfo
*fileinfo
= g_new0(MsgFileInfo
, 1);
1916 fileinfo
->file
= procmsg_get_message_file(msginfo
);
1917 fileinfo
->flags
= &(msginfo
->flags
);
1918 infolist
= g_slist_prepend(infolist
, fileinfo
);
1920 infolist
= g_slist_reverse(infolist
);
1921 res
= folder_item_add_msgs(dest
, infolist
, FALSE
);
1922 for (cur
= infolist
; cur
; cur
= cur
->next
) {
1923 MsgFileInfo
*info
= (MsgFileInfo
*)cur
->data
;
1927 g_slist_free(infolist
);
1931 lock_session(session
); /* unlocked later in the function */
1933 ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
1934 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
1935 if (ok
!= MAILIMAP_NO_ERROR
) {
1939 unlock_session(session
);
1941 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
1948 seq_list
= imap_get_lep_set_from_msglist(IMAP_FOLDER(folder
), msglist
);
1949 uid_hash
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1951 statusbar_print_all(_("Copying messages..."));
1952 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1953 struct mailimap_set
* seq_set
;
1954 struct mailimap_set
* source
= NULL
;
1955 struct mailimap_set
* dest
= NULL
;
1956 seq_set
= cur
->data
;
1958 debug_print("Copying messages from %s to %s ...\n",
1959 src
->path
, destdir
);
1961 lock_session(session
); /* unlocked later in the function */
1962 ok
= imap_cmd_copy(session
, seq_set
, destdir
,
1969 if (ok
== MAILIMAP_NO_ERROR
) {
1970 unlock_session(session
);
1971 if (relation
&& source
&& dest
) {
1972 GSList
*s_list
= flatten_mailimap_set(source
);
1973 GSList
*d_list
= flatten_mailimap_set(dest
);
1974 GSList
*s_cur
, *d_cur
;
1975 if (g_slist_length(s_list
) == g_slist_length(d_list
)) {
1977 for (s_cur
= s_list
, d_cur
= d_list
;
1979 s_cur
= s_cur
->next
, d_cur
= d_cur
->next
) {
1980 g_hash_table_insert(uid_hash
, s_cur
->data
, d_cur
->data
);
1984 debug_print("hhhmm, source list length != dest list length.\n");
1986 g_slist_free(s_list
);
1987 g_slist_free(d_list
);
1993 mailimap_set_free(source
);
1995 mailimap_set_free(dest
);
1997 if (ok
!= MAILIMAP_NO_ERROR
) {
1998 g_hash_table_destroy(uid_hash
);
1999 imap_lep_set_free(seq_list
);
2000 statusbar_pop_all();
2005 for (cur
= msglist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
2006 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
2009 hashval
= g_hash_table_lookup(uid_hash
, GINT_TO_POINTER(msginfo
->msgnum
));
2011 if (hashval
!= NULL
) {
2012 gint num
= GPOINTER_TO_INT(hashval
);
2013 g_hash_table_insert(relation
, msginfo
,
2014 GINT_TO_POINTER(num
));
2017 debug_print("copied message %d as %d\n", msginfo
->msgnum
, num
);
2018 /* put the local file in the imapcache, so that we don't
2019 * have to fetch it back later. */
2021 gchar
*cache_path
= folder_item_get_path(msginfo
->folder
);
2022 gchar
*real_file
= g_strconcat(
2023 cache_path
, G_DIR_SEPARATOR_S
,
2024 itos(msginfo
->msgnum
), NULL
);
2025 gchar
*cache_file
= NULL
;
2027 cache_path
= folder_item_get_path(dest
);
2028 cache_file
= g_strconcat(
2029 cache_path
, G_DIR_SEPARATOR_S
,
2031 if (!is_dir_exist(cache_path
))
2032 make_dir_hier(cache_path
);
2033 if (is_file_exist(real_file
) && is_dir_exist(cache_path
)) {
2034 if (copy_file(real_file
, cache_file
, TRUE
) < 0)
2035 debug_print("couldn't cache to %s: %s\n", cache_file
,
2038 debug_print("copied to cache: %s\n", cache_file
);
2045 g_hash_table_insert(relation
, msginfo
,
2046 GINT_TO_POINTER(0));
2048 statusbar_pop_all();
2050 g_hash_table_destroy(uid_hash
);
2051 imap_lep_set_free(seq_list
);
2055 IMAP_FOLDER_ITEM(dest
)->lastuid
= 0;
2056 IMAP_FOLDER_ITEM(dest
)->uid_next
= 0;
2057 g_slist_free(IMAP_FOLDER_ITEM(dest
)->uid_list
);
2058 IMAP_FOLDER_ITEM(dest
)->uid_list
= NULL
;
2060 imap_scan_required(folder
, dest
);
2061 if (ok
== MAILIMAP_NO_ERROR
)
2067 static gint
imap_copy_msg(Folder
*folder
, FolderItem
*dest
, MsgInfo
*msginfo
)
2071 g_return_val_if_fail(msginfo
!= NULL
, -1);
2073 msglist
.data
= msginfo
;
2074 msglist
.next
= NULL
;
2076 return imap_copy_msgs(folder
, dest
, &msglist
, NULL
);
2079 static gint
imap_copy_msgs(Folder
*folder
, FolderItem
*dest
,
2080 MsgInfoList
*msglist
, GHashTable
*relation
)
2085 g_return_val_if_fail(folder
!= NULL
, -1);
2086 g_return_val_if_fail(dest
!= NULL
, -1);
2087 g_return_val_if_fail(msglist
!= NULL
, -1);
2089 msginfo
= (MsgInfo
*)msglist
->data
;
2090 g_return_val_if_fail(msginfo
->folder
!= NULL
, -1);
2092 ret
= imap_do_copy_msgs(folder
, dest
, msglist
, relation
, FALSE
);
2096 static gboolean
imap_matcher_type_is_local(gint matchertype
)
2098 switch (matchertype
) {
2099 case MATCHCRITERIA_FROM
:
2100 case MATCHCRITERIA_TO
:
2101 case MATCHCRITERIA_CC
:
2102 case MATCHCRITERIA_TO_OR_CC
:
2103 case MATCHCRITERIA_SUBJECT
:
2104 case MATCHCRITERIA_REFERENCES
:
2105 case MATCHCRITERIA_MESSAGEID
:
2106 case MATCHCRITERIA_INREPLYTO
:
2107 case MATCHCRITERIA_AGE_GREATER
:
2108 case MATCHCRITERIA_AGE_LOWER
:
2109 case MATCHCRITERIA_FORWARDED
:
2110 case MATCHCRITERIA_SPAM
:
2111 case MATCHCRITERIA_UNREAD
:
2112 case MATCHCRITERIA_NEW
:
2113 case MATCHCRITERIA_MARKED
:
2114 case MATCHCRITERIA_REPLIED
:
2115 case MATCHCRITERIA_DELETED
:
2116 case MATCHCRITERIA_SIZE_GREATER
:
2117 case MATCHCRITERIA_SIZE_SMALLER
:
2118 case MATCHCRITERIA_SIZE_EQUAL
:
2124 static IMAPSearchKey
* search_make_key(MatcherProp
* match
, gboolean
* is_all
)
2126 if (match
->matchtype
== MATCHTYPE_MATCHCASE
|| match
->matchtype
== MATCHTYPE_MATCH
) {
2127 IMAPSearchKey
* result
= NULL
;
2128 gboolean invert
= FALSE
;
2129 gint matchertype
= match
->criteria
;
2135 switch (matchertype
) {
2136 case MATCHCRITERIA_NOT_NEW
: invert
= TRUE
; matchertype
= MATCHCRITERIA_NEW
; break;
2137 case MATCHCRITERIA_NOT_MARKED
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MARKED
; break;
2138 case MATCHCRITERIA_NOT_FORWARDED
: invert
= TRUE
; matchertype
= MATCHCRITERIA_FORWARDED
; break;
2139 case MATCHCRITERIA_NOT_SPAM
: invert
= TRUE
; matchertype
= MATCHCRITERIA_SPAM
; break;
2140 case MATCHCRITERIA_NOT_SUBJECT
: invert
= TRUE
; matchertype
= MATCHCRITERIA_SUBJECT
; break;
2141 case MATCHCRITERIA_NOT_FROM
: invert
= TRUE
; matchertype
= MATCHCRITERIA_FROM
; break;
2142 case MATCHCRITERIA_NOT_TO
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TO
; break;
2143 case MATCHCRITERIA_NOT_CC
: invert
= TRUE
; matchertype
= MATCHCRITERIA_CC
; break;
2144 case MATCHCRITERIA_NOT_REFERENCES
: invert
= TRUE
; matchertype
= MATCHCRITERIA_REFERENCES
; break;
2145 case MATCHCRITERIA_NOT_HEADER
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADER
; break;
2146 case MATCHCRITERIA_NOT_TAG
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TAG
; break;
2147 case MATCHCRITERIA_NOT_HEADERS_PART
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADERS_PART
; break;
2148 case MATCHCRITERIA_NOT_HEADERS_CONT
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADERS_CONT
; break;
2149 case MATCHCRITERIA_NOT_MESSAGE
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MESSAGE
; break;
2150 case MATCHCRITERIA_NOT_BODY_PART
: invert
= TRUE
; matchertype
= MATCHCRITERIA_BODY_PART
; break;
2151 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TO_OR_CC
; break;
2152 case MATCHCRITERIA_NOT_MESSAGEID
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MESSAGEID
; break;
2153 case MATCHCRITERIA_NOT_INREPLYTO
: invert
= TRUE
; matchertype
= MATCHCRITERIA_INREPLYTO
; break;
2157 * this aborts conversion even for predicates understood by the following code.
2158 * while that might seem wasteful, claws local search for information listed below
2159 * has proven faster than IMAP search plus network roundtrips. once this changes,
2160 * consider removing these exceptions.
2162 if (imap_matcher_type_is_local(matchertype
))
2165 /* the Message-ID header is also cached */
2166 if (matchertype
== MATCHCRITERIA_HEADER
&& g_strcmp0("Message-ID", match
->header
) == 0) {
2170 switch (matchertype
) {
2171 case MATCHCRITERIA_FORWARDED
:
2172 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, RTAG_FORWARDED
, 0);
2175 case MATCHCRITERIA_SPAM
:
2176 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, RTAG_JUNK
, 0);
2179 case MATCHCRITERIA_MESSAGEID
:
2180 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "Message-ID", match
->expr
, 0);
2183 case MATCHCRITERIA_INREPLYTO
:
2184 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "In-Reply-To", match
->expr
, 0);
2187 case MATCHCRITERIA_REFERENCES
:
2188 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "References", match
->expr
, 0);
2191 case MATCHCRITERIA_TO_OR_CC
:
2192 result
= imap_search_or(
2193 imap_search_new(IMAP_SEARCH_CRITERIA_TO
, NULL
, match
->expr
, 0),
2194 imap_search_new(IMAP_SEARCH_CRITERIA_CC
, NULL
, match
->expr
, 0)
2198 case MATCHCRITERIA_HEADERS_PART
:
2199 case MATCHCRITERIA_HEADERS_CONT
:
2200 result
= imap_search_and(
2201 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_BODY
, NULL
, match
->expr
, 0)),
2202 imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE
, NULL
, match
->expr
, 0)
2206 case MATCHCRITERIA_SIZE_EQUAL
:
2207 result
= imap_search_and(
2208 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER
, NULL
, NULL
, match
->value
)),
2209 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER
, NULL
, NULL
, match
->value
))
2213 case MATCHCRITERIA_NOT_UNREAD
:
2214 result
= imap_search_new(IMAP_SEARCH_CRITERIA_READ
, NULL
, NULL
, 0);
2217 case MATCHCRITERIA_UNREAD
:
2218 result
= imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD
, NULL
, NULL
, 0);
2221 case MATCHCRITERIA_NEW
:
2222 result
= imap_search_new(IMAP_SEARCH_CRITERIA_NEW
, NULL
, NULL
, 0);
2225 case MATCHCRITERIA_MARKED
:
2226 result
= imap_search_new(IMAP_SEARCH_CRITERIA_MARKED
, NULL
, NULL
, 0);
2229 case MATCHCRITERIA_DELETED
:
2230 result
= imap_search_new(IMAP_SEARCH_CRITERIA_DELETED
, NULL
, NULL
, 0);
2233 case MATCHCRITERIA_REPLIED
:
2234 result
= imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED
, NULL
, NULL
, 0);
2237 case MATCHCRITERIA_TAG
:
2239 gchar
*tmp
= imap_utf8_to_modified_utf7(match
->expr
, TRUE
);
2240 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, tmp
, 0);
2245 case MATCHCRITERIA_SUBJECT
:
2246 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SUBJECT
, NULL
, match
->expr
, 0);
2249 case MATCHCRITERIA_FROM
:
2250 result
= imap_search_new(IMAP_SEARCH_CRITERIA_FROM
, NULL
, match
->expr
, 0);
2253 case MATCHCRITERIA_TO
:
2254 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TO
, NULL
, match
->expr
, 0);
2257 case MATCHCRITERIA_CC
:
2258 result
= imap_search_new(IMAP_SEARCH_CRITERIA_CC
, NULL
, match
->expr
, 0);
2261 case MATCHCRITERIA_AGE_GREATER
:
2262 result
= imap_search_new(IMAP_SEARCH_CRITERIA_AGE_GREATER
, NULL
, NULL
, match
->value
);
2265 case MATCHCRITERIA_AGE_LOWER
:
2266 result
= imap_search_new(IMAP_SEARCH_CRITERIA_AGE_LOWER
, NULL
, NULL
, match
->value
);
2269 case MATCHCRITERIA_BODY_PART
:
2270 result
= imap_search_new(IMAP_SEARCH_CRITERIA_BODY
, NULL
, match
->expr
, 0);
2273 case MATCHCRITERIA_MESSAGE
:
2274 result
= imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE
, NULL
, match
->expr
, 0);
2277 case MATCHCRITERIA_HEADER
:
2278 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, match
->header
, match
->expr
, 0);
2281 case MATCHCRITERIA_SIZE_GREATER
:
2282 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER
, NULL
, NULL
, match
->value
);
2285 case MATCHCRITERIA_SIZE_SMALLER
:
2286 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER
, NULL
, NULL
, match
->value
);
2290 result
= imap_search_new(IMAP_SEARCH_CRITERIA_ALL
, NULL
, NULL
, 0);
2298 result
= imap_search_not(result
);
2299 if (is_all
&& *is_all
) {
2310 static void imap_change_search_charset(IMAPFolder
*folder
)
2312 /* If server supports charset in searches, but the last used one failed,
2313 * changed to the next preferred charset. If none are still available,
2314 * disable charset searches.
2315 * Charsets are tried in the following order:
2316 * UTF-8, locale's charset, UTF-7.
2319 if (folder
->search_charset_supported
) {
2320 if (folder
->search_charset
&& !strcmp(folder
->search_charset
, conv_get_locale_charset_str_no_utf8()))
2321 folder
->search_charset
= "UTF-8";
2322 else if (folder
->search_charset
&& !strcmp(folder
->search_charset
, "UTF-8"))
2323 folder
->search_charset
= "UTF-7";
2325 folder
->search_charset
= NULL
;
2326 folder
->search_charset_supported
= FALSE
;
2331 static MatcherProp
*imap_matcher_prop_set_charset(IMAPFolder
*folder
,
2332 MatcherProp
*utf8_prop
,
2335 /* If the match is going to be done locally, or the criteria is on
2336 * tag (special-cased to modified-UTF-7), or the expression searched
2337 * is ASCII, don't bother converting.
2339 if (imap_matcher_type_is_local(utf8_prop
->criteria
)
2340 || utf8_prop
->criteria
== MATCHCRITERIA_TAG
2341 || utf8_prop
->criteria
== MATCHCRITERIA_NOT_TAG
2342 || utf8_prop
->expr
== NULL
2343 || is_ascii_str(utf8_prop
->expr
))
2344 return matcherprop_new(utf8_prop
->criteria
,
2346 utf8_prop
->matchtype
,
2350 gchar
*conv_expr
= NULL
;
2352 /* If the search is server-side and the server doesn't support
2353 * searching with the charsets we handle, bail out.
2355 if (folder
->search_charset_supported
== FALSE
)
2358 /* Else, convert. */
2359 if (*charset
== NULL
)
2360 *charset
= g_strdup(folder
->search_charset
);
2362 conv_expr
= conv_codeset_strdup(utf8_prop
->expr
, CS_UTF_8
, *charset
);
2364 if (conv_expr
== NULL
)
2365 conv_expr
= g_strdup(utf8_prop
->expr
);
2367 return matcherprop_new(utf8_prop
->criteria
,
2369 utf8_prop
->matchtype
,
2375 static gint
search_msgs (Folder
*folder
,
2376 FolderItem
*container
,
2377 MsgNumberList
**msgs
,
2378 gboolean
*on_server
,
2379 MatcherList
*predicate
,
2380 SearchProgressNotify progress_cb
,
2381 gpointer progress_data
)
2383 IMAPSearchKey
* key
= NULL
;
2386 clist
* uidlist
= NULL
;
2387 gboolean server_filtering_useless
= FALSE
;
2388 IMAPSession
*session
;
2389 gchar
*charset_to_use
= NULL
;
2391 if (on_server
== NULL
|| !*on_server
) {
2392 return folder_item_search_msgs_local(folder
, container
, msgs
, on_server
,
2393 predicate
, progress_cb
, progress_data
);
2396 for (cur
= predicate
->matchers
; cur
!= NULL
; cur
= cur
->next
) {
2397 IMAPSearchKey
* matcherPart
= NULL
;
2398 MatcherProp
* prop
= (MatcherProp
*) cur
->data
;
2400 MatcherProp
*imap_prop
= imap_matcher_prop_set_charset(IMAP_FOLDER(folder
), prop
, &charset_to_use
);
2402 if (imap_prop
== NULL
) {
2403 /* Couldn't convert matcherprop to IMAP - probably not ascii
2404 * and server doesn't support the charsets we do. */
2408 matcherPart
= search_make_key(imap_prop
, &is_all
);
2410 matcherprop_free(imap_prop
);
2413 *on_server
&= matcherPart
!= NULL
&& prop
->matchtype
== MATCHTYPE_MATCHCASE
;
2419 server_filtering_useless
= is_all
;
2420 } else if (predicate
->bool_and
) {
2421 key
= imap_search_and(key
, matcherPart
);
2422 server_filtering_useless
&= is_all
;
2424 key
= imap_search_or(key
, matcherPart
);
2425 server_filtering_useless
|= is_all
;
2430 if (server_filtering_useless
) {
2431 imap_search_free(key
);
2435 if (key
== NULL
&& progress_cb
!= NULL
) {
2440 progress_cb(progress_data
, TRUE
, 0, 0, container
->total_msgs
);
2441 progress_cb(progress_data
, TRUE
, container
->total_msgs
, 0, container
->total_msgs
);
2443 list
= folder_item_get_msg_list(container
);
2444 for (cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
2445 *msgs
= g_slist_prepend(*msgs
, GUINT_TO_POINTER(((MsgInfo
*) cur
->data
)->msgnum
));
2448 procmsg_msg_list_free(list
);
2450 *msgs
= g_slist_reverse(*msgs
);
2455 session
= imap_session_get(folder
);
2459 result
= imap_select(session
, IMAP_FOLDER(folder
), FOLDER_ITEM(container
),
2460 NULL
, NULL
, NULL
, NULL
, NULL
, TRUE
);
2461 if (result
!= MAILIMAP_NO_ERROR
)
2465 progress_cb(progress_data
, TRUE
, 0, 0, container
->total_msgs
);
2466 result
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_KEYED
, key
, charset_to_use
, NULL
, &uidlist
);
2468 progress_cb(progress_data
, TRUE
, container
->total_msgs
, 0, container
->total_msgs
);
2470 if (result
== MAILIMAP_NO_ERROR
) {
2473 *msgs
= imap_uid_list_from_lep(uidlist
, &result
);
2475 mailimap_search_result_free(uidlist
);
2477 if (charset_to_use
!= NULL
)
2478 g_free(charset_to_use
);
2481 } else if (charset_to_use
!= NULL
) {
2482 /* If search failed and was on an 8-bit string, try the next
2483 * available charset to search if there still are some.
2485 g_free(charset_to_use
);
2487 imap_change_search_charset(IMAP_FOLDER(folder
));
2488 if (IMAP_FOLDER(folder
)->search_charset_supported
)
2489 return search_msgs(folder
, container
, msgs
, on_server
, predicate
,
2490 progress_cb
, progress_data
);
2499 static gint
imap_do_remove_msgs(Folder
*folder
, FolderItem
*dest
,
2500 MsgInfoList
*msglist
, GHashTable
*relation
)
2502 gchar
*destdir
, *dir
;
2503 GSList
*numlist
= NULL
, *cur
;
2505 IMAPSession
*session
;
2506 gint ok
= MAILIMAP_NO_ERROR
;
2508 g_return_val_if_fail(folder
!= NULL
, -1);
2509 g_return_val_if_fail(dest
!= NULL
, -1);
2510 g_return_val_if_fail(msglist
!= NULL
, -1);
2512 debug_print("getting session...\n");
2513 session
= imap_session_get(folder
);
2518 lock_session(session
); /* unlocked later in the function */
2520 msginfo
= (MsgInfo
*)msglist
->data
;
2522 ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
2523 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
2524 if (ok
!= MAILIMAP_NO_ERROR
) {
2528 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
2533 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2534 msginfo
= (MsgInfo
*)cur
->data
;
2535 if (!MSG_IS_DELETED(msginfo
->flags
))
2536 numlist
= g_slist_prepend(numlist
, GINT_TO_POINTER(msginfo
->msgnum
));
2538 numlist
= g_slist_reverse(numlist
);
2540 if (numlist
!= NULL
) {
2541 ok
= imap_set_message_flags
2542 (session
, IMAP_FOLDER_ITEM(msginfo
->folder
), numlist
, IMAP_FLAG_DELETED
, NULL
, TRUE
);
2543 if (ok
!= MAILIMAP_NO_ERROR
) {
2544 log_warning(LOG_PROTOCOL
, _("can't set deleted flags\n"));
2548 } /* else we just need to expunge */
2549 ok
= imap_cmd_expunge(session
);
2550 if (ok
!= MAILIMAP_NO_ERROR
) {
2551 log_warning(LOG_PROTOCOL
, _("can't expunge\n"));
2556 session
->folder_content_changed
= TRUE
;
2557 unlock_session(session
);
2559 dir
= folder_item_get_path(msginfo
->folder
);
2560 if (is_dir_exist(dir
)) {
2561 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2562 msginfo
= (MsgInfo
*)cur
->data
;
2563 remove_numbered_files(dir
, msginfo
->msgnum
, msginfo
->msgnum
);
2568 g_slist_free(numlist
);
2570 imap_scan_required(folder
, dest
);
2577 static gint
imap_remove_msgs(Folder
*folder
, FolderItem
*dest
,
2578 MsgInfoList
*msglist
, GHashTable
*relation
)
2582 g_return_val_if_fail(folder
!= NULL
, -1);
2583 g_return_val_if_fail(dest
!= NULL
, -1);
2584 if (msglist
== NULL
)
2587 msginfo
= (MsgInfo
*)msglist
->data
;
2588 g_return_val_if_fail(msginfo
->folder
!= NULL
, -1);
2590 return imap_do_remove_msgs(folder
, dest
, msglist
, relation
);
2593 static gint
imap_remove_all_msg(Folder
*folder
, FolderItem
*item
)
2595 GSList
*list
= folder_item_get_msg_list(item
);
2596 gint res
= imap_remove_msgs(folder
, item
, list
, NULL
);
2597 procmsg_msg_list_free(list
);
2601 static gboolean
imap_is_msg_changed(Folder
*folder
, FolderItem
*item
,
2604 /* TODO: properly implement this method */
2608 static gint
imap_close(Folder
*folder
, FolderItem
*item
)
2613 static gint
imap_scan_tree_real(Folder
*folder
, gboolean subs_only
)
2615 FolderItem
*item
= NULL
;
2616 IMAPSession
*session
;
2617 gchar
*root_folder
= NULL
;
2619 g_return_val_if_fail(folder
!= NULL
, -1);
2620 g_return_val_if_fail(folder
->account
!= NULL
, -1);
2622 debug_print("getting session...\n");
2623 session
= imap_session_get(folder
);
2625 if (!folder
->node
) {
2626 folder_tree_destroy(folder
);
2627 item
= folder_item_new(folder
, folder
->name
, NULL
);
2628 item
->folder
= folder
;
2629 folder
->node
= item
->node
= g_node_new(item
);
2634 if (folder
->account
->imap_dir
&& *folder
->account
->imap_dir
) {
2636 int r
= MAILIMAP_NO_ERROR
;
2639 Xstrdup_a(root_folder
, folder
->account
->imap_dir
, {return -1;});
2640 extract_quote(root_folder
, '"');
2641 subst_char(root_folder
,
2642 imap_get_path_separator(session
, IMAP_FOLDER(folder
),
2647 strtailchomp(root_folder
, '/');
2648 real_path
= imap_get_real_path
2649 (session
, IMAP_FOLDER(folder
), root_folder
, &r
);
2654 debug_print("IMAP root directory: %s\n", real_path
);
2656 /* check if root directory exist */
2658 r
= imap_threaded_list(session
->folder
, "", real_path
,
2661 if (r
!= MAILIMAP_NO_ERROR
)
2662 imap_handle_error(SESSION(session
), NULL
, r
);
2664 if ((r
!= MAILIMAP_NO_ERROR
) || (clist_count(lep_list
) == 0)) {
2665 if (!folder
->node
) {
2666 item
= folder_item_new(folder
, folder
->name
, NULL
);
2667 item
->folder
= folder
;
2668 folder
->node
= item
->node
= g_node_new(item
);
2672 mailimap_list_result_free(lep_list
);
2678 item
= FOLDER_ITEM(folder
->node
->data
);
2680 if (item
&& !item
->path
&& root_folder
) {
2681 item
->path
= g_strdup(root_folder
);
2684 if (!item
|| ((item
->path
|| root_folder
) &&
2685 strcmp2(item
->path
, root_folder
) != 0)) {
2686 folder_tree_destroy(folder
);
2687 item
= folder_item_new(folder
, folder
->name
, root_folder
);
2688 item
->folder
= folder
;
2689 folder
->node
= item
->node
= g_node_new(item
);
2692 imap_scan_tree_recursive(session
, FOLDER_ITEM(folder
->node
->data
), subs_only
);
2693 imap_create_missing_folders(folder
);
2698 static gint
imap_scan_tree(Folder
*folder
)
2700 gboolean subs_only
= FALSE
;
2701 if (folder
->account
) {
2702 debug_print(" scanning only subs %d\n", folder
->account
->imap_subsonly
);
2703 subs_only
= folder
->account
->imap_subsonly
;
2705 return imap_scan_tree_real(folder
, subs_only
);
2708 static gint
imap_scan_tree_recursive(IMAPSession
*session
, FolderItem
*item
, gboolean subs_only
)
2711 IMAPFolder
*imapfolder
;
2712 FolderItem
*new_item
;
2713 GSList
*item_list
, *cur
;
2716 gchar
*wildcard_path
;
2720 int r
= MAILIMAP_NO_ERROR
;
2722 g_return_val_if_fail(item
!= NULL
, -1);
2723 g_return_val_if_fail(item
->folder
!= NULL
, -1);
2724 g_return_val_if_fail(item
->no_sub
== FALSE
, -1);
2726 folder
= item
->folder
;
2727 imapfolder
= IMAP_FOLDER(folder
);
2729 separator
= imap_get_path_separator(session
, imapfolder
, item
->path
, &r
);
2733 if (folder
->ui_func
)
2734 folder
->ui_func(folder
, item
, folder
->ui_func_data
);
2737 wildcard
[0] = separator
;
2740 real_path
= imap_get_real_path(session
, imapfolder
, item
->path
, &r
);
2748 real_path
= g_strdup("");
2751 Xstrcat_a(wildcard_path
, real_path
, wildcard
,
2752 {g_free(real_path
); return MAILIMAP_ERROR_BAD_STATE
;});
2756 r
= imap_threaded_lsub(folder
, "", wildcard_path
, &lep_list
);
2758 r
= imap_threaded_list(folder
, "", wildcard_path
, &lep_list
);
2760 if (r
!= MAILIMAP_NO_ERROR
) {
2761 imap_handle_error(SESSION(session
), NULL
, r
);
2767 item_list
= imap_list_from_lep(imapfolder
,
2768 lep_list
, real_path
, FALSE
);
2769 mailimap_list_result_free(lep_list
);
2774 node
= item
->node
->children
;
2775 while (node
!= NULL
) {
2776 FolderItem
*old_item
= FOLDER_ITEM(node
->data
);
2777 GNode
*next
= node
->next
;
2780 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
2781 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2782 if (!strcmp2(old_item
->path
, cur_item
->path
)) {
2783 new_item
= cur_item
;
2788 if (old_item
&& old_item
->path
&& !strcasecmp(old_item
->path
, "INBOX")) {
2789 debug_print("not removing INBOX\n");
2791 debug_print("folder '%s' not found. removing...\n",
2793 folder_item_remove(old_item
);
2796 old_item
->no_sub
= new_item
->no_sub
;
2797 old_item
->no_select
= new_item
->no_select
;
2798 if (old_item
->no_sub
== TRUE
&& node
->children
) {
2799 debug_print("folder '%s' doesn't have "
2800 "subfolders. removing...\n",
2802 folder_item_remove_children(old_item
);
2809 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
2810 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2813 for (node
= item
->node
->children
; node
!= NULL
;
2814 node
= node
->next
) {
2815 if (!strcmp2(FOLDER_ITEM(node
->data
)->path
,
2817 new_item
= FOLDER_ITEM(node
->data
);
2818 folder_item_destroy(cur_item
);
2824 new_item
= cur_item
;
2825 debug_print("new folder '%s' found.\n", new_item
->path
);
2826 folder_item_append(item
, new_item
);
2829 if (!strcasecmp(new_item
->path
, "INBOX")) {
2830 new_item
->stype
= F_INBOX
;
2831 folder
->inbox
= new_item
;
2832 } else if (!folder_item_parent(item
) || item
->stype
== F_INBOX
) {
2835 base
= g_path_get_basename(new_item
->path
);
2837 if (!folder
->outbox
&& !g_ascii_strcasecmp(base
, "Sent")) {
2838 new_item
->stype
= F_OUTBOX
;
2839 folder
->outbox
= new_item
;
2840 } else if (!folder
->draft
&& (!g_ascii_strcasecmp(base
, "Drafts") || !g_ascii_strcasecmp(base
, "Draft"))) {
2841 new_item
->stype
= F_DRAFT
;
2842 folder
->draft
= new_item
;
2843 } else if (!folder
->queue
&& !g_ascii_strcasecmp(base
, "Queue")) {
2844 new_item
->stype
= F_QUEUE
;
2845 folder
->queue
= new_item
;
2846 } else if (!folder
->trash
&& !g_ascii_strcasecmp(base
, "Trash")) {
2847 new_item
->stype
= F_TRASH
;
2848 folder
->trash
= new_item
;
2853 if (new_item
->no_sub
== FALSE
)
2854 imap_scan_tree_recursive(session
, new_item
, subs_only
);
2857 g_slist_free(item_list
);
2859 return MAILIMAP_NO_ERROR
;
2862 GList
*imap_scan_subtree(Folder
*folder
, FolderItem
*item
, gboolean unsubs_only
, gboolean recursive
)
2864 IMAPSession
*session
= imap_session_get(folder
);
2866 gchar
*wildcard_path
;
2870 GSList
*item_list
= NULL
, *cur
;
2871 GList
*child_list
= NULL
, *tmplist
= NULL
;
2872 GSList
*sub_list
= NULL
;
2873 int r
= MAILIMAP_NO_ERROR
;
2878 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
2883 wildcard
[0] = separator
;
2886 real_path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
2894 real_path
= g_strdup("");
2897 Xstrcat_a(wildcard_path
, real_path
, wildcard
,
2898 {g_free(real_path
); return NULL
;});
2902 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
2903 item
->path
?item
->path
:item
->name
);
2905 statusbar_print_all(_("Looking for subfolders of %s..."),
2906 item
->path
?item
->path
:item
->name
);
2908 r
= imap_threaded_list(folder
, "", wildcard_path
, &lep_list
);
2911 statusbar_pop_all();
2914 item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
2915 lep_list
, real_path
, FALSE
);
2916 mailimap_list_result_free(lep_list
);
2918 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
2919 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2921 tmplist
= imap_scan_subtree(folder
, cur_item
,
2922 unsubs_only
, recursive
);
2924 child_list
= g_list_concat(child_list
, tmplist
);
2926 child_list
= g_list_prepend(child_list
,
2927 imap_get_real_path(session
,
2928 IMAP_FOLDER(folder
), cur_item
->path
, &r
));
2931 statusbar_pop_all();
2934 folder_item_destroy(cur_item
);
2936 child_list
= g_list_reverse(child_list
);
2937 g_slist_free(item_list
);
2940 r
= imap_threaded_lsub(folder
, "", wildcard_path
, &lep_list
);
2943 statusbar_pop_all();
2946 sub_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
2947 lep_list
, real_path
, FALSE
);
2948 mailimap_list_result_free(lep_list
);
2950 for (cur
= sub_list
; cur
!= NULL
; cur
= cur
->next
) {
2951 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2952 GList
*oldlitem
= NULL
;
2953 gchar
*tmp
= imap_get_real_path(session
,
2954 IMAP_FOLDER(folder
), cur_item
->path
, &r
);
2957 statusbar_pop_all();
2961 folder_item_destroy(cur_item
);
2962 oldlitem
= g_list_find_custom(
2963 child_list
, tmp
, (GCompareFunc
)strcmp2
);
2965 child_list
= g_list_remove_link(child_list
, oldlitem
);
2966 g_free(oldlitem
->data
);
2967 g_list_free(oldlitem
);
2974 statusbar_pop_all();
2979 static gint
imap_create_tree(Folder
*folder
)
2981 g_return_val_if_fail(folder
!= NULL
, -1);
2982 g_return_val_if_fail(folder
->node
!= NULL
, -1);
2983 g_return_val_if_fail(folder
->node
->data
!= NULL
, -1);
2984 g_return_val_if_fail(folder
->account
!= NULL
, -1);
2986 imap_scan_tree(folder
);
2987 imap_create_missing_folders(folder
);
2992 static void imap_create_missing_folders(Folder
*folder
)
2994 g_return_if_fail(folder
!= NULL
);
2997 folder
->inbox
= imap_create_special_folder
2998 (folder
, F_INBOX
, "INBOX");
3000 folder
->trash
= imap_create_special_folder
3001 (folder
, F_TRASH
, "Trash");
3003 folder
->queue
= imap_create_special_folder
3004 (folder
, F_QUEUE
, "Queue");
3005 if (!folder
->outbox
)
3006 folder
->outbox
= imap_create_special_folder
3007 (folder
, F_OUTBOX
, "Sent");
3009 folder
->draft
= imap_create_special_folder
3010 (folder
, F_DRAFT
, "Drafts");
3013 static FolderItem
*imap_create_special_folder(Folder
*folder
,
3014 SpecialFolderItemType stype
,
3018 FolderItem
*new_item
;
3020 g_return_val_if_fail(folder
!= NULL
, NULL
);
3021 g_return_val_if_fail(folder
->node
!= NULL
, NULL
);
3022 g_return_val_if_fail(folder
->node
->data
!= NULL
, NULL
);
3023 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3024 g_return_val_if_fail(name
!= NULL
, NULL
);
3026 item
= FOLDER_ITEM(folder
->node
->data
);
3027 new_item
= imap_create_folder(folder
, item
, name
);
3030 g_warning("Can't create '%s'", name
);
3031 if (!folder
->inbox
) return NULL
;
3033 new_item
= imap_create_folder(folder
, folder
->inbox
, name
);
3035 g_warning("Can't create '%s' under INBOX", name
);
3037 new_item
->stype
= stype
;
3039 new_item
->stype
= stype
;
3045 static gchar
*imap_encode_unsafe_chars(const gchar
*str
)
3047 gchar
*ret
= NULL
, *o_ret
;
3051 ret
= g_malloc(3*strlen(str
)+1);
3053 for (i
= (gchar
*)str
; *i
; i
++) {
3063 *ret
++ = '0'+(*i
/10);
3064 *ret
++ = '0'+(*i
%10);
3074 static gchar
*imap_item_get_path(Folder
*folder
, FolderItem
*item
)
3076 gchar
*folder_path
, *path
;
3077 gchar
*item_path
= NULL
;
3079 g_return_val_if_fail(folder
!= NULL
, NULL
);
3080 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3081 g_return_val_if_fail(item
!= NULL
, NULL
);
3082 folder_path
= prefs_account_cache_dir(folder
->account
, FALSE
);
3084 g_return_val_if_fail(folder_path
!= NULL
, NULL
);
3087 item_path
= g_strdup(item
->path
);
3089 item_path
= imap_encode_unsafe_chars(item
->path
);
3092 if (g_path_is_absolute(folder_path
)) {
3094 path
= g_strconcat(folder_path
, G_DIR_SEPARATOR_S
,
3097 path
= g_strdup(folder_path
);
3100 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
3101 folder_path
, G_DIR_SEPARATOR_S
,
3104 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
3107 g_free(folder_path
);
3110 while (strchr(path
, '/'))
3111 *strchr(path
, '/') = '\\';
3117 static FolderItem
*imap_create_folder(Folder
*folder
, FolderItem
*parent
,
3120 gchar
*dirpath
, *imap_path
;
3121 IMAPSession
*session
;
3122 FolderItem
*new_item
;
3126 gint ok
= MAILIMAP_NO_ERROR
;
3127 gboolean no_select
= FALSE
, no_sub
= FALSE
;
3128 gboolean exist
= FALSE
;
3130 g_return_val_if_fail(folder
!= NULL
, NULL
);
3131 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3132 g_return_val_if_fail(parent
!= NULL
, NULL
);
3133 g_return_val_if_fail(name
!= NULL
, NULL
);
3135 if (to_number(name
) > 0) {
3136 gchar
*cached_msg
= imap_get_cached_filename(parent
, to_number(name
));
3137 if (is_file_exist(cached_msg
)) {
3138 if (claws_unlink(cached_msg
) != 0) {
3144 debug_print("getting session...\n");
3145 session
= imap_session_get(folder
);
3150 if (!folder_item_parent(parent
) && strcasecmp(name
, "INBOX") == 0) {
3151 dirpath
= g_strdup(name
);
3152 }else if (parent
->path
)
3153 dirpath
= g_strconcat(parent
->path
, "/", name
, NULL
);
3154 else if ((p
= strchr(name
, '/')) != NULL
&& *(p
+ 1) != '\0')
3155 dirpath
= g_strdup(name
);
3156 else if (folder
->account
->imap_dir
&& *folder
->account
->imap_dir
) {
3159 Xstrdup_a(imap_dir
, folder
->account
->imap_dir
, {return NULL
;});
3160 strtailchomp(imap_dir
, '/');
3161 dirpath
= g_strconcat(imap_dir
, "/", name
, NULL
);
3163 dirpath
= g_strdup(name
);
3167 /* keep trailing directory separator to create a folder that contains
3169 imap_path
= imap_utf8_to_modified_utf7(dirpath
, FALSE
);
3171 strtailchomp(dirpath
, '/');
3172 Xstrdup_a(new_name
, name
, {
3177 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), imap_path
, &ok
);
3182 imap_path_separator_subst(imap_path
, separator
);
3183 /* remove trailing / for display */
3184 strtailchomp(new_name
, '/');
3186 if (strcasecmp(dirpath
, "INBOX") != 0) {
3191 argbuf
= g_ptr_array_new();
3192 r
= imap_threaded_list(folder
, "", imap_path
, &lep_list
);
3193 if (r
!= MAILIMAP_NO_ERROR
) {
3194 imap_handle_error(SESSION(session
), NULL
, r
);
3195 log_warning(LOG_PROTOCOL
, _("can't create mailbox: LIST failed\n"));
3198 ptr_array_free_strings(argbuf
);
3199 g_ptr_array_free(argbuf
, TRUE
);
3203 if (clist_count(lep_list
) > 0)
3205 mailimap_list_result_free(lep_list
);
3208 ok
= imap_cmd_create(session
, imap_path
);
3209 if (ok
!= MAILIMAP_NO_ERROR
) {
3210 log_warning(LOG_PROTOCOL
, _("can't create mailbox\n"));
3215 r
= imap_threaded_list(folder
, "", imap_path
, &lep_list
);
3216 if (r
== MAILIMAP_NO_ERROR
) {
3217 GSList
*item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3218 lep_list
, dirpath
, TRUE
);
3220 FolderItem
*cur_item
= FOLDER_ITEM(item_list
->data
);
3221 no_select
= cur_item
->no_select
;
3222 no_sub
= cur_item
->no_sub
;
3223 g_slist_free(item_list
);
3225 mailimap_list_result_free(lep_list
);
3227 imap_handle_error(SESSION(session
), NULL
, r
);
3230 imap_threaded_subscribe(folder
, imap_path
, TRUE
);
3234 /* just get flags */
3235 r
= imap_threaded_list(folder
, "", "INBOX", &lep_list
);
3236 if (r
== MAILIMAP_NO_ERROR
) {
3237 GSList
*item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3238 lep_list
, dirpath
, TRUE
);
3240 FolderItem
*cur_item
= FOLDER_ITEM(item_list
->data
);
3241 no_select
= cur_item
->no_select
;
3242 no_sub
= cur_item
->no_sub
;
3243 g_slist_free(item_list
);
3245 mailimap_list_result_free(lep_list
);
3247 imap_handle_error(SESSION(session
), NULL
, r
);
3251 new_item
= folder_item_new(folder
, new_name
, dirpath
);
3252 new_item
->no_select
= no_select
;
3253 new_item
->no_sub
= no_sub
;
3254 folder_item_append(parent
, new_item
);
3258 dirpath
= folder_item_get_path(new_item
);
3259 if (!is_dir_exist(dirpath
))
3260 make_dir_hier(dirpath
);
3264 /* folder existed, scan it */
3265 imap_scan_required(folder
, new_item
);
3266 folder_item_scan_full(new_item
, FALSE
);
3272 static gint
imap_rename_folder(Folder
*folder
, FolderItem
*item
,
3277 gchar
*real_oldpath
;
3278 gchar
*real_newpath
;
3280 gchar
*old_cache_dir
;
3281 gchar
*new_cache_dir
;
3282 IMAPSession
*session
;
3284 gint ok
= MAILIMAP_NO_ERROR
;
3285 gint exists
, recent
, unseen
;
3286 guint32 uid_validity
;
3288 g_return_val_if_fail(folder
!= NULL
, -1);
3289 g_return_val_if_fail(item
!= NULL
, -1);
3290 g_return_val_if_fail(item
->path
!= NULL
, -1);
3291 g_return_val_if_fail(name
!= NULL
, -1);
3293 debug_print("getting session...\n");
3294 session
= imap_session_get(folder
);
3299 if (strchr(name
, imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &ok
)) != NULL
||
3301 g_warning("New folder name must not contain the namespace "
3306 real_oldpath
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3308 g_free(real_oldpath
);
3312 g_free(session
->mbox
);
3313 session
->mbox
= NULL
;
3314 session
->exists
= 0;
3315 session
->recent
= 0;
3316 session
->expunge
= 0;
3317 ok
= imap_cmd_examine(session
, "INBOX",
3318 &exists
, &recent
, &unseen
, &uid_validity
, FALSE
);
3319 if (ok
!= MAILIMAP_NO_ERROR
) {
3320 g_free(real_oldpath
);
3324 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3326 g_free(real_oldpath
);
3329 if (strchr(item
->path
, '/')) {
3330 dirpath
= g_path_get_dirname(item
->path
);
3331 newpath
= g_strconcat(dirpath
, "/", name
, NULL
);
3334 newpath
= g_strdup(name
);
3336 real_newpath
= imap_utf8_to_modified_utf7(newpath
, FALSE
);
3337 imap_path_separator_subst(real_newpath
, separator
);
3339 ok
= imap_cmd_rename(session
, real_oldpath
, real_newpath
);
3340 if (ok
!= MAILIMAP_NO_ERROR
) {
3341 log_warning(LOG_PROTOCOL
, _("can't rename mailbox: %s to %s\n"),
3342 real_oldpath
, real_newpath
);
3343 g_free(real_oldpath
);
3345 g_free(real_newpath
);
3349 item
->name
= g_strdup(name
);
3351 old_cache_dir
= folder_item_get_path(item
);
3353 paths
[0] = g_strdup(item
->path
);
3355 g_node_traverse(item
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
3356 imap_rename_folder_func
, paths
);
3358 if (is_dir_exist(old_cache_dir
)) {
3359 new_cache_dir
= folder_item_get_path(item
);
3360 if (g_rename(old_cache_dir
, new_cache_dir
) < 0) {
3361 FILE_OP_ERROR(old_cache_dir
, "rename");
3363 g_free(new_cache_dir
);
3366 g_free(old_cache_dir
);
3369 g_free(real_oldpath
);
3370 g_free(real_newpath
);
3374 gint
imap_subscribe(Folder
*folder
, FolderItem
*item
, gchar
*rpath
, gboolean sub
)
3377 gint r
= MAILIMAP_NO_ERROR
;
3378 IMAPSession
*session
;
3379 debug_print("getting session...\n");
3381 session
= imap_session_get(folder
);
3385 if (item
&& item
->path
) {
3386 path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
3393 if (!strcasecmp(path
, "INBOX") && sub
== FALSE
) {
3397 debug_print("%ssubscribing %s\n", sub
?"":"un", path
);
3398 r
= imap_threaded_subscribe(folder
, path
, sub
);
3401 r
= imap_threaded_subscribe(folder
, rpath
, sub
);
3407 static gint
imap_remove_folder_real(Folder
*folder
, FolderItem
*item
)
3409 gint ok
= MAILIMAP_NO_ERROR
;
3410 IMAPSession
*session
;
3413 gboolean selected_folder
;
3415 g_return_val_if_fail(folder
!= NULL
, -1);
3416 g_return_val_if_fail(item
!= NULL
, -1);
3417 g_return_val_if_fail(item
->path
!= NULL
, -1);
3419 debug_print("getting session...\n");
3420 session
= imap_session_get(folder
);
3424 path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3429 imap_threaded_subscribe(folder
, path
, FALSE
);
3431 selected_folder
= (session
->mbox
!= NULL
) &&
3432 (!strcmp(session
->mbox
, item
->path
));
3433 if (selected_folder
) {
3434 ok
= imap_cmd_close(session
);
3435 if (ok
!= MAILIMAP_NO_ERROR
) {
3436 debug_print("close err %d\n", ok
);
3440 ok
= imap_cmd_delete(session
, path
);
3441 if (ok
!= MAILIMAP_NO_ERROR
&& !is_fatal(ok
)) {
3444 ok
= MAILIMAP_NO_ERROR
;
3445 tmp
= g_strdup_printf("%s%c", path
,
3446 imap_get_path_separator(session
, IMAP_FOLDER(folder
), path
, &ok
));
3450 ok
= imap_cmd_delete(session
, path
);
3453 if (ok
!= MAILIMAP_NO_ERROR
) {
3454 log_warning(LOG_PROTOCOL
, _("can't delete mailbox\n"));
3460 cache_dir
= folder_item_get_path(item
);
3461 if (is_dir_exist(cache_dir
) && remove_dir_recursive(cache_dir
) < 0)
3462 g_warning("can't remove directory '%s'", cache_dir
);
3464 folder_item_remove(item
);
3468 static gint
imap_remove_folder(Folder
*folder
, FolderItem
*item
)
3472 g_return_val_if_fail(item
!= NULL
, -1);
3473 g_return_val_if_fail(item
->folder
!= NULL
, -1);
3474 g_return_val_if_fail(item
->node
!= NULL
, -1);
3476 node
= item
->node
->children
;
3477 while (node
!= NULL
) {
3479 if (imap_remove_folder(folder
, FOLDER_ITEM(node
->data
)) < 0)
3483 debug_print("IMAP removing %s\n", item
->path
);
3485 if (imap_remove_all_msg(folder
, item
) < 0)
3487 return imap_remove_folder_real(folder
, item
);
3490 typedef struct _uncached_data
{
3491 IMAPSession
*session
;
3493 MsgNumberList
*numlist
;
3500 static void *imap_get_uncached_messages_thread(void *data
)
3502 uncached_data
*stuff
= (uncached_data
*)data
;
3503 IMAPSession
*session
= stuff
->session
;
3504 FolderItem
*item
= stuff
->item
;
3505 MsgNumberList
*numlist
= stuff
->numlist
;
3506 GSList
*newlist
= NULL
;
3507 GSList
*llast
= NULL
;
3508 GSList
*seq_list
, *cur
;
3509 gboolean got_alien_tags
= FALSE
;
3511 debug_print("uncached_messages\n");
3513 if (session
== NULL
|| item
== NULL
|| item
->folder
== NULL
3514 || FOLDER_CLASS(item
->folder
) != &imap_class
) {
3519 seq_list
= imap_get_lep_set_from_numlist(IMAP_FOLDER(item
->folder
), numlist
);
3520 debug_print("get msgs info\n");
3521 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
3522 struct mailimap_set
* imapset
;
3528 if (session
->cancelled
)
3531 imapset
= cur
->data
;
3533 r
= imap_threaded_fetch_env(session
->folder
,
3534 imapset
, &env_list
);
3535 if (r
!= MAILIMAP_NO_ERROR
) {
3536 imap_handle_error(SESSION(session
), NULL
, r
);
3544 session_set_access_time(SESSION(session
));
3547 for(i
= 0 ; i
< carray_count(env_list
) ; i
+= 2) {
3548 struct imap_fetch_env_info
* info
;
3550 GSList
*tags
= NULL
, *cur
= NULL
;
3551 info
= carray_get(env_list
, i
);
3552 tags
= carray_get(env_list
, i
+1);
3553 msginfo
= imap_envelope_from_lep(info
, item
);
3554 if (msginfo
== NULL
) {
3555 slist_free_strings_full(tags
);
3558 g_slist_free(msginfo
->tags
);
3559 msginfo
->tags
= NULL
;
3561 for (cur
= tags
; cur
; cur
= cur
->next
) {
3562 gchar
*real_tag
= imap_modified_utf7_to_utf8(cur
->data
, TRUE
);
3564 id
= tags_get_id_for_str(real_tag
);
3566 id
= tags_add_tag(real_tag
);
3567 got_alien_tags
= TRUE
;
3569 if (!g_slist_find(msginfo
->tags
, GINT_TO_POINTER(id
))) {
3570 msginfo
->tags
= g_slist_prepend(
3572 GINT_TO_POINTER(id
));
3577 msginfo
->tags
= g_slist_reverse(msginfo
->tags
);
3578 slist_free_strings_full(tags
);
3579 msginfo
->folder
= item
;
3581 llast
= newlist
= g_slist_append(newlist
, msginfo
);
3583 llast
= g_slist_append(llast
, msginfo
);
3584 llast
= llast
->next
;
3589 imap_fetch_env_free(env_list
);
3592 if (got_alien_tags
) {
3594 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
3597 imap_lep_set_free(seq_list
);
3599 session_set_access_time(SESSION(session
));
3604 #define MAX_MSG_NUM 50
3606 static GSList
*imap_get_uncached_messages(IMAPSession
*session
,
3608 MsgNumberList
*numlist
,
3611 GSList
*result
= NULL
;
3613 uncached_data
*data
= g_new0(uncached_data
, 1);
3616 data
->total
= g_slist_length(numlist
);
3617 data
->ok
= MAILIMAP_NO_ERROR
;
3618 debug_print("messages list : %i\n", data
->total
);
3620 while (cur
!= NULL
) {
3621 GSList
* partial_result
;
3629 while (count
< MAX_MSG_NUM
) {
3634 if (newlist
== NULL
)
3635 llast
= newlist
= g_slist_append(newlist
, p
);
3637 llast
= g_slist_append(llast
, p
);
3638 llast
= llast
->next
;
3648 data
->session
= session
;
3650 data
->numlist
= newlist
;
3653 if (prefs_common
.work_offline
&&
3654 !inc_offline_should_override(FALSE
,
3655 _("Claws Mail needs network access in order "
3656 "to access the IMAP server."))) {
3662 (GSList
*)imap_get_uncached_messages_thread(data
);
3664 if (data
->ok
!= MAILIMAP_NO_ERROR
) {
3667 statusbar_progress_all(data
->cur
,data
->total
, 1);
3669 g_slist_free(newlist
);
3671 result
= g_slist_concat(result
, partial_result
);
3676 statusbar_progress_all(0,0,0);
3677 statusbar_pop_all();
3682 static void imap_delete_all_cached_messages(FolderItem
*item
)
3686 g_return_if_fail(item
!= NULL
);
3687 g_return_if_fail(item
->folder
!= NULL
);
3688 g_return_if_fail(FOLDER_CLASS(item
->folder
) == &imap_class
);
3690 debug_print("Deleting all cached messages...\n");
3692 dir
= folder_item_get_path(item
);
3693 if (is_dir_exist(dir
))
3694 remove_all_numbered_files(dir
);
3697 debug_print("Deleting all cached messages done.\n");
3700 gchar
imap_get_path_separator_for_item(FolderItem
*item
)
3702 Folder
*folder
= NULL
;
3703 IMAPFolder
*imap_folder
= NULL
;
3704 IMAPSession
*session
= NULL
;
3706 gint ok
= MAILIMAP_NO_ERROR
;
3709 folder
= item
->folder
;
3714 imap_folder
= IMAP_FOLDER(folder
);
3716 debug_print("getting session...\n");
3717 session
= imap_session_get(FOLDER(folder
));
3718 result
= imap_get_path_separator(session
, imap_folder
, item
->path
, &ok
);
3722 static gchar
imap_refresh_path_separator(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*subfolder
, gint
*ok
)
3726 gchar separator
= '\0';
3728 g_return_val_if_fail(session
!= NULL
, '/');
3729 r
= imap_threaded_list((Folder
*)folder
, "", subfolder
, &lep_list
);
3731 if (r
!= MAILIMAP_NO_ERROR
) {
3732 imap_handle_error(SESSION(session
), NULL
, r
);
3733 log_warning(LOG_PROTOCOL
, _("LIST failed\n"));
3738 if (lep_list
!= NULL
&& clist_count(lep_list
) > 0) {
3739 clistiter
* iter
= clist_begin(lep_list
);
3740 struct mailimap_mailbox_list
* mb
;
3741 mb
= clist_content(iter
);
3743 separator
= mb
->mb_delimiter
;
3744 debug_print("got separator: %c\n", folder
->last_seen_separator
);
3746 *ok
= MAILIMAP_NO_ERROR
;
3747 mailimap_list_result_free(lep_list
);
3751 static gchar
imap_get_path_separator(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*path
, gint
*ok
)
3753 gchar separator
= '/';
3754 *ok
= MAILIMAP_NO_ERROR
;
3755 if (folder
->last_seen_separator
== 0) {
3756 folder
->last_seen_separator
= imap_refresh_path_separator(session
, folder
, "", ok
);
3759 if (folder
->last_seen_separator
== 0) {
3760 folder
->last_seen_separator
= imap_refresh_path_separator(session
, folder
, "INBOX", ok
);
3763 if (folder
->last_seen_separator
!= 0) {
3764 debug_print("using separator: %c\n", folder
->last_seen_separator
);
3765 return folder
->last_seen_separator
;
3771 static gchar
*imap_get_real_path(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*path
, gint
*ok
)
3773 gchar
*real_path
= NULL
;
3776 g_return_val_if_fail(folder
!= NULL
, NULL
);
3777 g_return_val_if_fail(path
!= NULL
, NULL
);
3779 *ok
= MAILIMAP_NO_ERROR
;
3781 real_path
= imap_utf8_to_modified_utf7(path
, FALSE
);
3782 separator
= imap_get_path_separator(session
, folder
, path
, ok
);
3783 if (*ok
== MAILIMAP_NO_ERROR
)
3784 imap_path_separator_subst(real_path
, separator
);
3789 static gint
imap_set_message_flags(IMAPSession
*session
,
3790 IMAPFolderItem
*item
,
3791 MsgNumberList
*numlist
,
3800 IMAPFolder
*folder
= NULL
;
3801 GSList
*sorted_list
= NULL
;
3803 if (numlist
== NULL
|| session
== NULL
)
3804 return MAILIMAP_ERROR_BAD_STATE
;
3806 folder
= IMAP_FOLDER(session
->folder
);
3808 sorted_list
= g_slist_copy(numlist
);
3809 sorted_list
= g_slist_sort(sorted_list
, g_int_compare
);
3811 cur
= g_slist_last(sorted_list
);
3814 total
= GPOINTER_TO_INT(cur
->data
);
3816 seq_list
= imap_get_lep_set_from_numlist(IMAP_FOLDER(session
->folder
), sorted_list
);
3818 statusbar_print_all(_("Flagging messages..."));
3820 for(cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
3821 struct mailimap_set
* imapset
= (struct mailimap_set
*)cur
->data
;
3822 struct mailimap_set_item
*set_item
= NULL
;
3824 if (imapset
->set_list
)
3825 set_item
= clist_content(clist_begin(imapset
->set_list
));
3829 if (set_item
== NULL
)
3832 statusbar_progress_all(set_item
->set_first
, total
, 1);
3834 ok
= imap_cmd_store(session
, item
, imapset
,
3835 flags
, tags
, is_set
);
3836 statusbar_progress_all(set_item
->set_last
, total
, 1);
3837 if (ok
!= MAILIMAP_NO_ERROR
&& folder
->max_set_size
> 20) {
3838 /* reduce max set size */
3839 folder
->max_set_size
/= 2;
3841 if (ok
!= MAILIMAP_NO_ERROR
&& is_fatal(ok
)) {
3846 g_slist_free(sorted_list
);
3848 statusbar_progress_all(0,0,0);
3849 statusbar_pop_all();
3851 imap_lep_set_free(seq_list
);
3856 typedef struct _select_data
{
3857 IMAPSession
*session
;
3862 guint32
*uid_validity
;
3866 static gint
imap_select(IMAPSession
*session
, IMAPFolder
*folder
,
3868 gint
*exists
, gint
*recent
, gint
*unseen
,
3869 guint32
*uid_validity
, gint
*can_create_flags
,
3873 gint ok
= MAILIMAP_NO_ERROR
;
3874 gint exists_
, recent_
, unseen_
;
3875 guint32 uid_validity_
;
3876 gint can_create_flags_
;
3877 const gchar
*path
= item
? item
->path
:NULL
;
3880 return MAILIMAP_ERROR_BAD_STATE
;
3883 if (!exists
&& !recent
&& !unseen
&& !uid_validity
&& !can_create_flags
) {
3884 if (session
->mbox
&& strcmp(session
->mbox
, path
) == 0)
3885 return MAILIMAP_NO_ERROR
;
3887 if (!exists
&& !recent
&& !unseen
&& !uid_validity
&& can_create_flags
) {
3888 if (session
->mbox
&& strcmp(session
->mbox
, path
) == 0) {
3889 if (IMAP_FOLDER_ITEM(item
)->can_create_flags
!= ITEM_CAN_CREATE_FLAGS_UNKNOWN
)
3890 return MAILIMAP_NO_ERROR
;
3900 uid_validity
= &uid_validity_
;
3901 if (!can_create_flags
)
3902 can_create_flags
= &can_create_flags_
;
3904 g_free(session
->mbox
);
3905 session
->mbox
= NULL
;
3906 session
->exists
= 0;
3907 session
->recent
= 0;
3908 session
->expunge
= 0;
3910 real_path
= imap_get_real_path(session
, folder
, path
, &ok
);
3915 g_slist_free(IMAP_FOLDER_ITEM(item
)->ok_flags
);
3916 IMAP_FOLDER_ITEM(item
)->ok_flags
= NULL
;
3917 ok
= imap_cmd_select(session
, real_path
,
3918 exists
, recent
, unseen
, uid_validity
, can_create_flags
,
3919 &(IMAP_FOLDER_ITEM(item
)->ok_flags
), block
);
3920 if (ok
!= MAILIMAP_NO_ERROR
) {
3921 log_warning(LOG_PROTOCOL
, _("can't select folder: %s\n"), real_path
);
3923 session
->mbox
= g_strdup(path
);
3924 session
->folder_content_changed
= FALSE
;
3925 session
->exists
= *exists
;
3926 session
->recent
= *recent
;
3927 session
->expunge
= 0;
3928 session
->unseen
= *unseen
;
3929 session
->uid_validity
= *uid_validity
;
3930 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
3931 session
->exists
, session
->recent
, session
->expunge
,
3932 session
->uid_validity
, *can_create_flags
);
3934 if (*can_create_flags
) {
3935 IMAP_FOLDER_ITEM(item
)->can_create_flags
= ITEM_CAN_CREATE_FLAGS
;
3937 IMAP_FOLDER_ITEM(item
)->can_create_flags
= ITEM_CANNOT_CREATE_FLAGS
;
3944 static gint
imap_status(IMAPSession
*session
, IMAPFolder
*folder
,
3945 const gchar
*path
, IMAPFolderItem
*item
,
3947 guint32
*uid_next
, guint32
*uid_validity
,
3948 gint
*unseen
, gboolean block
)
3950 int r
= MAILIMAP_NO_ERROR
;
3952 struct mailimap_mailbox_data_status
* data_status
;
3957 real_path
= imap_get_real_path(session
, folder
, path
, &r
);
3979 if (session
->mbox
!= NULL
&&
3980 !strcmp(session
->mbox
, item
->item
.path
)) {
3981 r
= imap_cmd_close(session
);
3982 if (r
!= MAILIMAP_NO_ERROR
) {
3983 debug_print("close err %d\n", r
);
3989 r
= imap_threaded_status(FOLDER(folder
), real_path
,
3990 &data_status
, mask
);
3993 if (r
!= MAILIMAP_NO_ERROR
) {
3994 imap_handle_error(SESSION(session
), NULL
, r
);
3995 debug_print("status err %d\n", r
);
3999 if (data_status
== NULL
|| data_status
->st_info_list
== NULL
) {
4000 debug_print("data_status %p\n", data_status
);
4002 debug_print("data_status->st_info_list %p\n", data_status
->st_info_list
);
4003 mailimap_mailbox_data_status_free(data_status
);
4005 return MAILIMAP_ERROR_BAD_STATE
;
4009 if (data_status
->st_info_list
) {
4010 for(iter
= clist_begin(data_status
->st_info_list
) ; iter
!= NULL
;
4011 iter
= clist_next(iter
)) {
4012 struct mailimap_status_info
* info
;
4014 info
= clist_content(iter
);
4015 switch (info
->st_att
) {
4016 case MAILIMAP_STATUS_ATT_MESSAGES
:
4018 * messages
= info
->st_value
;
4019 got_values
|= 1 << 0;
4023 case MAILIMAP_STATUS_ATT_UIDNEXT
:
4025 * uid_next
= info
->st_value
;
4026 got_values
|= 1 << 2;
4030 case MAILIMAP_STATUS_ATT_UIDVALIDITY
:
4032 * uid_validity
= info
->st_value
;
4033 got_values
|= 1 << 3;
4037 case MAILIMAP_STATUS_ATT_UNSEEN
:
4039 * unseen
= info
->st_value
;
4040 got_values
|= 1 << 4;
4046 mailimap_mailbox_data_status_free(data_status
);
4048 if (got_values
!= mask
) {
4049 g_warning("status: incomplete values received (%d)", got_values
);
4051 return MAILIMAP_NO_ERROR
;
4054 static void imap_free_capabilities(IMAPSession
*session
)
4056 slist_free_strings_full(session
->capability
);
4057 session
->capability
= NULL
;
4060 /* low-level IMAP4rev1 commands */
4062 static gint
imap_cmd_login(IMAPSession
*session
,
4063 const gchar
*user
, const gchar
*pass
,
4069 if (!strcmp(type
, "plaintext") && imap_has_capability(session
, "LOGINDISABLED")) {
4070 gint ok
= MAILIMAP_ERROR_BAD_STATE
;
4071 if (imap_has_capability(session
, "STARTTLS")) {
4073 log_warning(LOG_PROTOCOL
, _("Server requires STARTTLS to log in.\n"));
4074 ok
= imap_cmd_starttls(session
);
4075 if (ok
!= MAILIMAP_NO_ERROR
) {
4076 log_warning(LOG_PROTOCOL
, _("Can't start STARTTLS session.\n"));
4080 imap_free_capabilities(session
);
4081 if ((r
= imap_get_capabilities(session
)) != MAILIMAP_NO_ERROR
) {
4082 imap_handle_error(SESSION(session
), NULL
, r
);
4083 log_warning(LOG_PROTOCOL
, _("Can't refresh capabilities.\n"));
4088 log_error(LOG_PROTOCOL
, _("Connection to %s failed: "
4089 "server requires STARTTLS, but Claws Mail "
4090 "has been compiled without STARTTLS "
4092 SESSION(session
)->server
);
4093 return MAILIMAP_ERROR_LOGIN
;
4096 log_error(LOG_PROTOCOL
, _("Server logins are disabled.\n"));
4097 return MAILIMAP_ERROR_LOGIN
;
4101 log_print(LOG_PROTOCOL
, "IMAP> Logging %s to %s using %s\n",
4103 SESSION(session
)->server
,
4105 r
= imap_threaded_login(session
->folder
, user
, pass
, type
);
4106 if (r
!= MAILIMAP_NO_ERROR
) {
4107 imap_handle_error(SESSION(session
), NULL
, r
);
4108 log_print(LOG_PROTOCOL
, "IMAP< Error logging in to %s\n",
4109 SESSION(session
)->server
);
4112 log_print(LOG_PROTOCOL
, "IMAP< Login to %s successful\n",
4113 SESSION(session
)->server
);
4114 ok
= MAILIMAP_NO_ERROR
;
4119 static gint
imap_cmd_noop(IMAPSession
*session
)
4122 unsigned int exists
, recent
, expunge
, unseen
, uidnext
, uidval
;
4124 r
= imap_threaded_noop(session
->folder
, &exists
, &recent
, &expunge
, &unseen
, &uidnext
, &uidval
);
4125 if (r
!= MAILIMAP_NO_ERROR
) {
4126 imap_handle_error(SESSION(session
), NULL
, r
);
4127 debug_print("noop err %d\n", r
);
4131 if ((exists
&& exists
!= session
->exists
)
4132 || (recent
&& recent
!= session
->recent
)
4133 || (expunge
&& expunge
!= session
->expunge
)
4134 || (unseen
&& unseen
!= session
->unseen
)) {
4135 session
->folder_content_changed
= TRUE
;
4137 if (uidnext
!= 0 && uidnext
!= session
->uid_next
) {
4138 session
->uid_next
= uidnext
;
4139 session
->folder_content_changed
= TRUE
;
4141 if (uidval
!= 0 && uidval
!= session
->uid_validity
) {
4142 session
->uid_validity
= uidval
;
4143 session
->folder_content_changed
= TRUE
;
4146 session
->exists
= exists
;
4147 session
->recent
= recent
;
4148 session
->expunge
= expunge
;
4149 session
->unseen
= unseen
;
4151 session_set_access_time(SESSION(session
));
4153 return MAILIMAP_NO_ERROR
;
4157 static gint
imap_cmd_starttls(IMAPSession
*session
)
4161 r
= imap_threaded_starttls(session
->folder
,
4162 SESSION(session
)->server
, SESSION(session
)->port
);
4163 if (r
!= MAILIMAP_NO_ERROR
) {
4164 imap_handle_error(SESSION(session
), NULL
, r
);
4165 debug_print("STARTTLS err %d\n", r
);
4168 return MAILIMAP_NO_ERROR
;
4172 static gint
imap_cmd_select(IMAPSession
*session
, const gchar
*folder
,
4173 gint
*exists
, gint
*recent
, gint
*unseen
,
4174 guint32
*uid_validity
, gint
*can_create_flags
,
4175 GSList
**ok_flags
, gboolean block
)
4179 r
= imap_threaded_select(session
->folder
, folder
,
4180 exists
, recent
, unseen
, uid_validity
, can_create_flags
, ok_flags
);
4181 if (r
!= MAILIMAP_NO_ERROR
) {
4182 imap_handle_error(SESSION(session
), NULL
, r
);
4183 debug_print("select err %d\n", r
);
4186 return MAILIMAP_NO_ERROR
;
4189 static gint
imap_cmd_close(IMAPSession
*session
)
4193 r
= imap_threaded_close(session
->folder
);
4194 if (r
!= MAILIMAP_NO_ERROR
) {
4195 imap_handle_error(SESSION(session
), NULL
, r
);
4196 debug_print("close err %d\n", r
);
4199 g_free(session
->mbox
);
4200 session
->mbox
= NULL
;
4201 session
->exists
= 0;
4202 session
->recent
= 0;
4203 session
->expunge
= 0;
4204 return MAILIMAP_NO_ERROR
;
4207 static gint
imap_cmd_examine(IMAPSession
*session
, const gchar
*folder
,
4208 gint
*exists
, gint
*recent
, gint
*unseen
,
4209 guint32
*uid_validity
, gboolean block
)
4213 r
= imap_threaded_examine(session
->folder
, folder
,
4214 exists
, recent
, unseen
, uid_validity
);
4215 if (r
!= MAILIMAP_NO_ERROR
) {
4216 imap_handle_error(SESSION(session
), NULL
, r
);
4217 debug_print("examine err %d\n", r
);
4221 return MAILIMAP_NO_ERROR
;
4224 static gint
imap_cmd_create(IMAPSession
*session
, const gchar
*folder
)
4228 r
= imap_threaded_create(session
->folder
, folder
);
4229 if (r
!= MAILIMAP_NO_ERROR
) {
4230 imap_handle_error(SESSION(session
), NULL
, r
);
4234 return MAILIMAP_NO_ERROR
;
4237 static gint
imap_cmd_rename(IMAPSession
*session
, const gchar
*old_folder
,
4238 const gchar
*new_folder
)
4242 r
= imap_threaded_rename(session
->folder
, old_folder
,
4244 if (r
!= MAILIMAP_NO_ERROR
) {
4245 imap_handle_error(SESSION(session
), NULL
, r
);
4249 return MAILIMAP_NO_ERROR
;
4252 static gint
imap_cmd_delete(IMAPSession
*session
, const gchar
*folder
)
4257 r
= imap_threaded_delete(session
->folder
, folder
);
4258 if (r
!= MAILIMAP_NO_ERROR
) {
4259 imap_handle_error(SESSION(session
), NULL
, r
);
4263 return MAILIMAP_NO_ERROR
;
4266 typedef struct _fetch_data
{
4267 IMAPSession
*session
;
4269 const gchar
*filename
;
4275 static void *imap_cmd_fetch_thread(void *data
)
4277 fetch_data
*stuff
= (fetch_data
*)data
;
4278 IMAPSession
*session
= stuff
->session
;
4279 guint32 uid
= stuff
->uid
;
4280 const gchar
*filename
= stuff
->filename
;
4284 r
= imap_threaded_fetch_content(session
->folder
,
4288 r
= imap_threaded_fetch_content(session
->folder
,
4291 if (r
!= MAILIMAP_NO_ERROR
) {
4292 imap_handle_error(SESSION(session
), NULL
, r
);
4293 debug_print("fetch err %d\n", r
);
4294 return GINT_TO_POINTER(r
);
4296 return GINT_TO_POINTER(MAILIMAP_NO_ERROR
);
4299 static gint
imap_cmd_fetch(IMAPSession
*session
, guint32 uid
,
4300 const gchar
*filename
, gboolean headers
,
4303 fetch_data
*data
= g_new0(fetch_data
, 1);
4306 data
->session
= session
;
4308 data
->filename
= filename
;
4309 data
->headers
= headers
;
4312 if (prefs_common
.work_offline
&&
4313 !inc_offline_should_override(FALSE
,
4314 _("Claws Mail needs network access in order "
4315 "to access the IMAP server."))) {
4319 statusbar_print_all(_("Fetching message..."));
4320 result
= GPOINTER_TO_INT(imap_cmd_fetch_thread(data
));
4321 statusbar_pop_all();
4327 static gint
imap_cmd_append(IMAPSession
*session
,
4328 IMAPFolderItem
*item
,
4329 const gchar
*destfolder
,
4330 const gchar
*file
, IMAPFlags flags
,
4333 struct mailimap_flag_list
* flag_list
;
4336 cm_return_val_if_fail(file
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4338 flag_list
= imap_flag_to_lep(item
, flags
, NULL
);
4339 lock_session(session
);
4340 r
= imap_threaded_append(session
->folder
, destfolder
,
4341 file
, flag_list
, (int *)new_uid
);
4342 mailimap_flag_list_free(flag_list
);
4344 if (r
!= MAILIMAP_NO_ERROR
) {
4345 imap_handle_error(SESSION(session
), NULL
, r
);
4346 debug_print("append err %d\n", r
);
4350 unlock_session(session
);
4352 return MAILIMAP_NO_ERROR
;
4355 static gint
imap_cmd_copy(IMAPSession
*session
, struct mailimap_set
* set
,
4356 const gchar
*destfolder
,
4357 struct mailimap_set
**source
, struct mailimap_set
**dest
)
4361 g_return_val_if_fail(session
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4362 g_return_val_if_fail(set
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4363 g_return_val_if_fail(destfolder
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4365 r
= imap_threaded_copy(session
->folder
, set
, destfolder
, source
, dest
);
4366 if (r
!= MAILIMAP_NO_ERROR
) {
4367 imap_handle_error(SESSION(session
), NULL
, r
);
4371 return MAILIMAP_NO_ERROR
;
4374 static gint
imap_cmd_store(IMAPSession
*session
,
4375 IMAPFolderItem
*item
,
4376 struct mailimap_set
* set
,
4377 IMAPFlags flags
, GSList
*tags
, int do_add
)
4380 struct mailimap_flag_list
* flag_list
= NULL
;
4381 struct mailimap_store_att_flags
* store_att_flags
;
4383 flag_list
= imap_flag_to_lep(item
, flags
, tags
);
4387 mailimap_store_att_flags_new_add_flags_silent(flag_list
);
4390 mailimap_store_att_flags_new_remove_flags_silent(flag_list
);
4392 r
= imap_threaded_store(session
->folder
, set
, store_att_flags
);
4393 mailimap_store_att_flags_free(store_att_flags
);
4394 if (r
!= MAILIMAP_NO_ERROR
) {
4395 imap_handle_error(SESSION(session
), NULL
, r
);
4399 return MAILIMAP_NO_ERROR
;
4402 static gint
imap_cmd_expunge(IMAPSession
*session
)
4406 if (prefs_common
.work_offline
&&
4407 !inc_offline_should_override(FALSE
,
4408 _("Claws Mail needs network access in order "
4409 "to access the IMAP server."))) {
4413 r
= imap_threaded_expunge(session
->folder
);
4414 if (r
!= MAILIMAP_NO_ERROR
) {
4415 imap_handle_error(SESSION(session
), NULL
, r
);
4419 return MAILIMAP_NO_ERROR
;
4422 gint
imap_expunge(Folder
*folder
, FolderItem
*item
)
4424 IMAPSession
*session
= imap_session_get(folder
);
4425 if (session
== NULL
)
4428 return imap_cmd_expunge(session
);
4431 static void imap_path_separator_subst(gchar
*str
, gchar separator
)
4434 gboolean in_escape
= FALSE
;
4436 if (!separator
|| separator
== '/') return;
4438 for (p
= str
; *p
!= '\0'; p
++) {
4439 if (*p
== '/' && !in_escape
)
4441 else if (*p
== '&' && *(p
+ 1) != '-' && !in_escape
)
4443 else if (*p
== '-' && in_escape
)
4448 static gboolean
imap_rename_folder_func(GNode
*node
, gpointer data
)
4450 FolderItem
*item
= node
->data
;
4451 gchar
**paths
= data
;
4452 const gchar
*oldpath
= paths
[0];
4453 const gchar
*newpath
= paths
[1];
4454 gchar
*real_oldpath
, *real_newpath
;
4456 gchar
*new_itempath
;
4458 IMAPSession
*session
= imap_session_get(item
->folder
);
4459 gint ok
= MAILIMAP_NO_ERROR
;
4460 oldpathlen
= strlen(oldpath
);
4461 if (strncmp(oldpath
, item
->path
, oldpathlen
) != 0) {
4462 g_warning("path doesn't match: %s, %s", oldpath
, item
->path
);
4466 base
= item
->path
+ oldpathlen
;
4467 while (*base
== '/') base
++;
4469 new_itempath
= g_strdup(newpath
);
4471 new_itempath
= g_strconcat(newpath
, "/", base
,
4474 real_oldpath
= imap_get_real_path(session
, IMAP_FOLDER(item
->folder
), item
->path
, &ok
);
4476 item
->path
= new_itempath
;
4478 real_newpath
= imap_get_real_path(session
, IMAP_FOLDER(item
->folder
), item
->path
, &ok
);
4480 imap_threaded_subscribe(item
->folder
, real_oldpath
, FALSE
);
4481 imap_threaded_subscribe(item
->folder
, real_newpath
, TRUE
);
4483 g_free(real_oldpath
);
4484 g_free(real_newpath
);
4488 static gint
get_list_of_uids(IMAPSession
*session
, Folder
*folder
, IMAPFolderItem
*item
, GSList
**msgnum_list
)
4490 GSList
*uidlist
, *elem
;
4492 clist
* lep_uidlist
;
4493 gint ok
, nummsgs
= 0;
4495 if (session
== NULL
) {
4499 ok
= imap_select(session
, IMAP_FOLDER(folder
), FOLDER_ITEM(item
),
4500 NULL
, NULL
, NULL
, NULL
, NULL
, TRUE
);
4501 if (ok
!= MAILIMAP_NO_ERROR
) {
4505 g_slist_free(item
->uid_list
);
4506 item
->uid_list
= NULL
;
4510 if (folder
->account
&& folder
->account
->low_bandwidth
) {
4511 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SIMPLE
,
4512 NULL
, NULL
, NULL
, &lep_uidlist
);
4515 if (r
== MAILIMAP_NO_ERROR
) {
4516 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
4517 mailimap_search_result_free(lep_uidlist
);
4519 carray
* lep_uidtab
;
4520 if (r
!= -1) { /* inited */
4521 imap_handle_error(SESSION(session
), NULL
, r
);
4525 r
= imap_threaded_fetch_uid(folder
, 1,
4527 if (r
== MAILIMAP_NO_ERROR
) {
4528 uidlist
= imap_uid_list_from_lep_tab(lep_uidtab
);
4529 imap_fetch_uid_list_free(lep_uidtab
);
4533 if (r
!= MAILIMAP_NO_ERROR
) {
4534 imap_handle_error(SESSION(session
), NULL
, r
);
4538 for (elem
= uidlist
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
4541 msgnum
= GPOINTER_TO_INT(elem
->data
);
4543 *msgnum_list
= g_slist_prepend(*msgnum_list
, GINT_TO_POINTER(msgnum
));
4544 item
->uid_list
= g_slist_prepend(item
->uid_list
, GINT_TO_POINTER(msgnum
));
4547 g_slist_free(uidlist
);
4553 gint
imap_get_num_list(Folder
*folder
, FolderItem
*_item
, GSList
**msgnum_list
, gboolean
*old_uids_valid
)
4555 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
4556 IMAPSession
*session
;
4558 GSList
*uidlist
= NULL
;
4560 gint known_list_len
= 0;
4563 debug_print("get_num_list\n");
4565 g_return_val_if_fail(folder
!= NULL
, -1);
4566 g_return_val_if_fail(item
!= NULL
, -1);
4567 g_return_val_if_fail(item
->item
.path
!= NULL
, -1);
4568 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, -1);
4569 g_return_val_if_fail(folder
->account
!= NULL
, -1);
4571 known_list_len
= g_slist_length(item
->uid_list
);
4572 if (!item
->should_update
) {
4573 debug_print("get_num_list: nothing to update\n");
4574 *old_uids_valid
= TRUE
;
4575 if (known_list_len
== item
->item
.total_msgs
4576 && known_list_len
> 0) {
4577 *msgnum_list
= g_slist_copy(item
->uid_list
);
4578 return known_list_len
;
4580 debug_print("don't know the list length...\n");
4584 if (prefs_common
.work_offline
&&
4585 !inc_offline_should_override(FALSE
,
4586 _("Claws Mail needs network access in order "
4587 "to access the IMAP server."))) {
4591 path
= folder_item_get_path(_item
);
4592 if (!is_dir_exist(path
)) {
4593 if(is_file_exist(path
))
4595 make_dir_hier(path
);
4599 debug_print("getting session...\n");
4600 session
= imap_session_get(folder
);
4601 g_return_val_if_fail(session
!= NULL
, -1);
4603 lock_session(session
);
4604 if (FOLDER_ITEM(item
)->path
)
4605 statusbar_print_all(_("Scanning folder %s/%s..."),
4606 FOLDER_ITEM(item
)->folder
->name
,
4607 FOLDER_ITEM(item
)->path
);
4609 statusbar_print_all(_("Scanning folder %s..."),
4610 FOLDER_ITEM(item
)->folder
->name
);
4612 if (item
->should_trash_cache
) {
4613 *old_uids_valid
= FALSE
;
4614 debug_print("get_num_list: trashing num list\n");
4615 debug_print("Freeing imap uid cache\n");
4617 g_slist_free(item
->uid_list
);
4618 item
->uid_list
= NULL
;
4620 imap_delete_all_cached_messages((FolderItem
*)item
);
4622 debug_print("get_num_list: updating num list\n");
4623 *old_uids_valid
= TRUE
;
4626 nummsgs
= get_list_of_uids(session
, folder
, item
, &uidlist
);
4628 unlock_session(session
);
4630 /* session could be broken now, in case of fatal error */
4632 debug_print("get_num_list: got %d msgs\n", nummsgs
);
4635 statusbar_pop_all();
4639 *msgnum_list
= uidlist
;
4641 dir
= folder_item_get_path((FolderItem
*)item
);
4642 debug_print("removing old messages from %s\n", dir
);
4643 remove_numbered_files_not_in_list(dir
, *msgnum_list
);
4646 debug_print("get_num_list - ok - %i\n", nummsgs
);
4647 statusbar_pop_all();
4648 item
->should_trash_cache
= FALSE
;
4649 item
->should_update
= FALSE
;
4653 static MsgInfo
*imap_parse_msg(const gchar
*file
, FolderItem
*item
)
4658 flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
4659 flags
.tmp_flags
= 0;
4661 g_return_val_if_fail(item
!= NULL
, NULL
);
4662 g_return_val_if_fail(file
!= NULL
, NULL
);
4664 if (folder_has_parent_of_type(item
, F_QUEUE
)) {
4665 MSG_SET_TMP_FLAGS(flags
, MSG_QUEUED
);
4666 } else if (folder_has_parent_of_type(item
, F_DRAFT
)) {
4667 MSG_SET_TMP_FLAGS(flags
, MSG_DRAFT
);
4670 msginfo
= procheader_parse_file(file
, flags
, FALSE
, FALSE
);
4671 if (!msginfo
) return NULL
;
4673 msginfo
->plaintext_file
= g_strdup(file
);
4674 msginfo
->folder
= item
;
4679 GSList
*imap_get_msginfos(Folder
*folder
, FolderItem
*item
,
4680 GSList
*msgnum_list
)
4682 IMAPSession
*session
;
4683 MsgInfoList
*ret
= NULL
;
4686 debug_print("get_msginfos\n");
4688 g_return_val_if_fail(folder
!= NULL
, NULL
);
4689 g_return_val_if_fail(item
!= NULL
, NULL
);
4690 g_return_val_if_fail(msgnum_list
!= NULL
, NULL
);
4692 debug_print("getting session...\n");
4693 session
= imap_session_get(folder
);
4694 g_return_val_if_fail(session
!= NULL
, NULL
);
4696 lock_session(session
); /* unlocked later in the function */
4698 debug_print("IMAP getting msginfos\n");
4699 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
4700 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
4701 if (ok
!= MAILIMAP_NO_ERROR
) {
4704 if (!(folder_has_parent_of_type(item
, F_DRAFT
) ||
4705 folder_has_parent_of_type(item
, F_QUEUE
))) {
4706 ret
= g_slist_concat(ret
,
4707 imap_get_uncached_messages(session
, item
,
4709 if (ok
!= MAILIMAP_NO_ERROR
)
4711 unlock_session(session
);
4713 MsgNumberList
*sorted_list
, *elem
, *llast
= NULL
;
4714 gint startnum
, lastnum
;
4716 unlock_session(session
);
4718 sorted_list
= g_slist_sort(g_slist_copy(msgnum_list
), g_int_compare
);
4720 startnum
= lastnum
= GPOINTER_TO_INT(sorted_list
->data
);
4722 llast
= g_slist_last(ret
);
4723 for (elem
= sorted_list
;; elem
= g_slist_next(elem
)) {
4727 num
= GPOINTER_TO_INT(elem
->data
);
4729 if (num
> lastnum
+ 1 || elem
== NULL
) {
4731 for (i
= startnum
; i
<= lastnum
; ++i
) {
4733 file
= imap_fetch_msg(folder
, item
, i
);
4735 MsgInfo
*msginfo
= imap_parse_msg(file
, item
);
4736 if (msginfo
!= NULL
) {
4737 msginfo
->msgnum
= i
;
4739 llast
= ret
= g_slist_append(ret
, msginfo
);
4741 llast
= g_slist_append(llast
, msginfo
);
4742 llast
= llast
->next
;
4757 g_slist_free(sorted_list
);
4762 MsgInfo
*imap_get_msginfo(Folder
*folder
, FolderItem
*item
, gint uid
)
4764 MsgInfo
*msginfo
= NULL
;
4765 MsgInfoList
*msginfolist
;
4766 MsgNumberList numlist
;
4768 numlist
.next
= NULL
;
4769 numlist
.data
= GINT_TO_POINTER(uid
);
4771 msginfolist
= imap_get_msginfos(folder
, item
, &numlist
);
4772 if (msginfolist
!= NULL
) {
4773 msginfo
= msginfolist
->data
;
4774 g_slist_free(msginfolist
);
4780 gboolean
imap_scan_required(Folder
*folder
, FolderItem
*_item
)
4782 IMAPSession
*session
;
4783 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
4784 gint ok
, exists
= 0, unseen
= 0;
4785 guint32 uid_next
= 0, uid_val
= 0;
4786 gboolean selected_folder
;
4788 g_return_val_if_fail(folder
!= NULL
, FALSE
);
4789 g_return_val_if_fail(item
!= NULL
, FALSE
);
4790 g_return_val_if_fail(item
->item
.folder
!= NULL
, FALSE
);
4791 g_return_val_if_fail(FOLDER_CLASS(item
->item
.folder
) == &imap_class
, FALSE
);
4793 if (item
->item
.path
== NULL
)
4796 if (item
->should_update
) {
4797 debug_print("scan already required\n");
4800 debug_print("getting session...\n");
4801 session
= imap_session_get(folder
);
4803 g_return_val_if_fail(session
!= NULL
, FALSE
);
4804 lock_session(session
); /* unlocked later in the function */
4806 selected_folder
= (session
->mbox
!= NULL
) &&
4807 (!strcmp(session
->mbox
, item
->item
.path
));
4808 if (selected_folder
) {
4809 if (!session
->folder_content_changed
) {
4810 ok
= imap_cmd_noop(session
);
4811 if (ok
!= MAILIMAP_NO_ERROR
) {
4812 debug_print("disconnected!\n");
4814 session
= imap_reconnect_if_possible(folder
, session
);
4816 session
= imap_session_get(folder
);
4817 if (session
== NULL
)
4821 if (session
->folder_content_changed
) {
4822 debug_print("CHANGED (self-noop)! scan_required\n");
4823 item
->should_update
= TRUE
;
4824 if (session
->uid_validity
&& session
->uid_validity
!= item
->item
.mtime
) {
4825 item
->item
.mtime
= session
->uid_validity
;
4826 item
->should_trash_cache
= TRUE
;
4828 unlock_session(session
);
4832 debug_print("CHANGED (previous noop)! scan_required\n");
4833 item
->should_update
= TRUE
;
4834 if (session
->uid_validity
&& session
->uid_validity
!= item
->item
.mtime
) {
4835 item
->item
.mtime
= session
->uid_validity
;
4836 item
->should_trash_cache
= TRUE
;
4838 unlock_session(session
);
4842 ok
= imap_status(session
, IMAP_FOLDER(folder
), item
->item
.path
, IMAP_FOLDER_ITEM(item
),
4843 &exists
, &uid_next
, &uid_val
, &unseen
, FALSE
);
4844 if (ok
!= MAILIMAP_NO_ERROR
) {
4848 debug_print("exists %d, item->item.total_msgs %d\n"
4849 "\tunseen %d, item->item.unread_msgs %d\n"
4850 "\tuid_next %d, item->uid_next %d\n"
4851 "\tuid_val %d, item->item.mtime %ld\n",
4852 exists
, item
->item
.total_msgs
, unseen
, item
->item
.unread_msgs
,
4853 uid_next
, item
->uid_next
, uid_val
, (long)(item
->item
.mtime
));
4854 if (exists
!= item
->item
.total_msgs
4855 || unseen
!= item
->item
.unread_msgs
4856 || uid_next
!= item
->uid_next
4857 || uid_val
!= item
->item
.mtime
) {
4858 debug_print("CHANGED (status)! scan_required\n");
4859 item
->last_change
= time(NULL
);
4860 item
->should_update
= TRUE
;
4861 item
->uid_next
= uid_next
;
4862 if (uid_val
!= item
->item
.mtime
) {
4863 item
->item
.mtime
= uid_val
;
4864 item
->should_trash_cache
= TRUE
;
4866 unlock_session(session
);
4870 unlock_session(session
);
4872 item
->should_update
= FALSE
;
4876 void imap_change_flags(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
, MsgPermFlags newflags
)
4878 IMAPSession
*session
;
4879 IMAPFlags flags_set
= 0, flags_unset
= 0;
4880 gint ok
= MAILIMAP_NO_ERROR
;
4881 MsgNumberList numlist
;
4882 hashtable_data
*ht_data
= NULL
;
4884 g_return_if_fail(folder
!= NULL
);
4885 g_return_if_fail(folder
->klass
== &imap_class
);
4886 g_return_if_fail(item
!= NULL
);
4887 g_return_if_fail(item
->folder
== folder
);
4888 g_return_if_fail(msginfo
!= NULL
);
4889 g_return_if_fail(msginfo
->folder
== item
);
4891 if (!MSG_IS_MARKED(msginfo
->flags
) && (newflags
& MSG_MARKED
))
4892 flags_set
|= IMAP_FLAG_FLAGGED
;
4893 if ( MSG_IS_MARKED(msginfo
->flags
) && !(newflags
& MSG_MARKED
))
4894 flags_unset
|= IMAP_FLAG_FLAGGED
;
4896 if (!MSG_IS_UNREAD(msginfo
->flags
) && (newflags
& MSG_UNREAD
))
4897 flags_unset
|= IMAP_FLAG_SEEN
;
4898 if ( MSG_IS_UNREAD(msginfo
->flags
) && !(newflags
& MSG_UNREAD
))
4899 flags_set
|= IMAP_FLAG_SEEN
;
4901 if (!MSG_IS_REPLIED(msginfo
->flags
) && (newflags
& MSG_REPLIED
))
4902 flags_set
|= IMAP_FLAG_ANSWERED
;
4903 if ( MSG_IS_REPLIED(msginfo
->flags
) && !(newflags
& MSG_REPLIED
))
4904 flags_unset
|= IMAP_FLAG_ANSWERED
;
4906 if (!MSG_IS_FORWARDED(msginfo
->flags
) && (newflags
& MSG_FORWARDED
))
4907 flags_set
|= IMAP_FLAG_FORWARDED
;
4908 if ( MSG_IS_FORWARDED(msginfo
->flags
) && !(newflags
& MSG_FORWARDED
))
4909 flags_unset
|= IMAP_FLAG_FORWARDED
;
4911 if (!MSG_IS_SPAM(msginfo
->flags
) && (newflags
& MSG_SPAM
)) {
4912 flags_set
|= IMAP_FLAG_SPAM
;
4913 flags_unset
|= IMAP_FLAG_HAM
;
4915 if ( MSG_IS_SPAM(msginfo
->flags
) && !(newflags
& MSG_SPAM
)) {
4916 flags_set
|= IMAP_FLAG_HAM
;
4917 flags_unset
|= IMAP_FLAG_SPAM
;
4919 if (!MSG_IS_DELETED(msginfo
->flags
) && (newflags
& MSG_DELETED
))
4920 flags_set
|= IMAP_FLAG_DELETED
;
4921 if ( MSG_IS_DELETED(msginfo
->flags
) && !(newflags
& MSG_DELETED
))
4922 flags_unset
|= IMAP_FLAG_DELETED
;
4924 if (!flags_set
&& !flags_unset
) {
4925 /* the changed flags were not translatable to IMAP-speak.
4926 * like MSG_POSTFILTERED, so just apply. */
4927 msginfo
->flags
.perm_flags
= newflags
;
4931 debug_print("getting session...\n");
4932 session
= imap_session_get(folder
);
4937 if ((ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
4938 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
)) != MAILIMAP_NO_ERROR
) {
4941 numlist
.next
= NULL
;
4942 numlist
.data
= GINT_TO_POINTER(msginfo
->msgnum
);
4944 if (IMAP_FOLDER_ITEM(item
)->batching
) {
4945 /* instead of performing an UID STORE command for each message change,
4946 * as a lot of them can change "together", we just fill in hashtables
4947 * and defer the treatment so that we're able to send only one
4950 debug_print("IMAP batch mode on, deferring flags change\n");
4952 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->flags_set_table
,
4953 GINT_TO_POINTER(flags_set
));
4954 if (ht_data
== NULL
) {
4955 ht_data
= g_new0(hashtable_data
, 1);
4956 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
4957 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->flags_set_table
,
4958 GINT_TO_POINTER(flags_set
), ht_data
);
4960 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
4963 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->flags_unset_table
,
4964 GINT_TO_POINTER(flags_unset
));
4965 if (ht_data
== NULL
) {
4966 ht_data
= g_new0(hashtable_data
, 1);
4967 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
4968 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->flags_unset_table
,
4969 GINT_TO_POINTER(flags_unset
), ht_data
);
4971 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
,
4972 GINT_TO_POINTER(msginfo
->msgnum
));
4975 debug_print("IMAP changing flags\n");
4977 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
), &numlist
, flags_set
, NULL
, TRUE
);
4978 if (ok
!= MAILIMAP_NO_ERROR
) {
4984 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
), &numlist
, flags_unset
, NULL
, FALSE
);
4985 if (ok
!= MAILIMAP_NO_ERROR
) {
4990 msginfo
->flags
.perm_flags
= newflags
;
4994 static gint
imap_remove_msg(Folder
*folder
, FolderItem
*item
, gint uid
)
4997 IMAPSession
*session
;
4999 MsgNumberList numlist
;
5001 g_return_val_if_fail(folder
!= NULL
, -1);
5002 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, -1);
5003 g_return_val_if_fail(item
!= NULL
, -1);
5005 debug_print("getting session...\n");
5006 session
= imap_session_get(folder
);
5007 if (!session
) return -1;
5009 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
5010 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5011 if (ok
!= MAILIMAP_NO_ERROR
) {
5014 numlist
.next
= NULL
;
5015 numlist
.data
= GINT_TO_POINTER(uid
);
5017 ok
= imap_set_message_flags
5018 (session
, IMAP_FOLDER_ITEM(item
), &numlist
, IMAP_FLAG_DELETED
, NULL
, TRUE
);
5019 if (ok
!= MAILIMAP_NO_ERROR
) {
5020 log_warning(LOG_PROTOCOL
, _("can't set deleted flags: %d\n"), uid
);
5024 ok
= imap_cmd_expunge(session
);
5026 if (ok
!= MAILIMAP_NO_ERROR
) {
5027 log_warning(LOG_PROTOCOL
, _("can't expunge\n"));
5031 IMAP_FOLDER_ITEM(item
)->uid_list
= g_slist_remove(
5032 IMAP_FOLDER_ITEM(item
)->uid_list
, numlist
.data
);
5033 dir
= folder_item_get_path(item
);
5034 if (is_dir_exist(dir
))
5035 remove_numbered_files(dir
, uid
, uid
);
5037 return MAILIMAP_NO_ERROR
;
5040 static gint
compare_msginfo(gconstpointer a
, gconstpointer b
)
5042 return ((MsgInfo
*)a
)->msgnum
- ((MsgInfo
*)b
)->msgnum
;
5045 static guint
gslist_find_next_num(MsgNumberList
**list
, guint num
)
5049 g_return_val_if_fail(list
!= NULL
, -1);
5051 for (elem
= *list
; elem
!= NULL
; elem
= g_slist_next(elem
))
5052 if (GPOINTER_TO_INT(elem
->data
) >= num
)
5055 return elem
!= NULL
? GPOINTER_TO_INT(elem
->data
) : (gint
)-1;
5058 static gboolean
flag_ok(IMAPFolderItem
*item
, guint flag
)
5060 if (item
->ok_flags
&& g_slist_find(item
->ok_flags
, GUINT_TO_POINTER(flag
))) {
5061 debug_print("flag %d is OK\n", flag
);
5064 if (item
->can_create_flags
== ITEM_CAN_CREATE_FLAGS
) {
5065 debug_print("creating flags is OK\n");
5072 * NEW and DELETED flags are not syncronized
5073 * - The NEW/RECENT flags in IMAP folders can not really be directly
5074 * modified by Sylpheed
5075 * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
5076 * meaning, in IMAP it always removes the messages from the FolderItem
5077 * in Sylpheed it can mean to move the message to trash
5080 typedef struct _get_flags_data
{
5083 MsgInfoList
*msginfo_list
;
5084 GHashTable
*msgflags
;
5085 gboolean full_search
;
5089 static /*gint*/ void *imap_get_flags_thread(void *data
)
5091 get_flags_data
*stuff
= (get_flags_data
*)data
;
5092 Folder
*folder
= stuff
->folder
;
5093 FolderItem
*fitem
= (FolderItem
*) stuff
->item
;
5094 MsgInfoList
*msginfo_list
= stuff
->msginfo_list
;
5095 GHashTable
*msgflags
= stuff
->msgflags
;
5097 carray
* lep_uidtab
;
5098 IMAPSession
*session
;
5100 int r
= MAILIMAP_NO_ERROR
;
5101 GHashTable
*flags_hash
= NULL
;
5102 GHashTable
*tags_hash
= NULL
;
5103 gboolean full_search
= stuff
->full_search
;
5104 GSList
*sorted_list
= NULL
;
5105 GSList
*unseen
= NULL
, *answered
= NULL
, *flagged
= NULL
, *deleted
= NULL
, *forwarded
= NULL
, *spam
= NULL
;
5106 GSList
*seq_list
, *cur
;
5107 gboolean reverse_seen
= FALSE
;
5108 gboolean selected_folder
;
5109 gint exists_cnt
, unseen_cnt
;
5110 gboolean got_alien_tags
= FALSE
;
5112 session
= imap_session_get(folder
);
5114 if (session
== NULL
) {
5116 return GINT_TO_POINTER(-1);
5118 selected_folder
= (session
->mbox
!= NULL
) &&
5119 (!strcmp(session
->mbox
, fitem
->path
));
5121 lock_session(session
);
5122 if (!selected_folder
) {
5123 ok
= imap_select(session
, IMAP_FOLDER(folder
), fitem
,
5124 &exists_cnt
, NULL
, &unseen_cnt
, NULL
, NULL
, TRUE
);
5125 if (ok
!= MAILIMAP_NO_ERROR
) {
5127 return GINT_TO_POINTER(-1);
5130 if (unseen_cnt
> exists_cnt
/ 2)
5131 reverse_seen
= TRUE
;
5134 if (fitem
->unread_msgs
> fitem
->total_msgs
/ 2)
5135 reverse_seen
= TRUE
;
5138 sorted_list
= g_slist_sort(g_slist_copy(msginfo_list
), compare_msginfo
);
5140 seq_list
= imap_get_lep_set_from_msglist(IMAP_FOLDER(folder
), msginfo_list
);
5142 struct mailimap_set
* set
;
5143 set
= mailimap_set_new_interval(1, 0);
5144 seq_list
= g_slist_append(NULL
, set
);
5147 if (folder
->account
&& folder
->account
->low_bandwidth
) {
5148 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
5149 struct mailimap_set
* imapset
;
5150 clist
* lep_uidlist
;
5153 imapset
= cur
->data
;
5155 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SEEN
, NULL
,
5156 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5159 r
= imap_threaded_search(folder
,
5160 IMAP_SEARCH_TYPE_UNSEEN
, NULL
,
5161 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5163 if (r
== MAILIMAP_NO_ERROR
) {
5166 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5167 mailimap_search_result_free(lep_uidlist
);
5169 unseen
= g_slist_concat(unseen
, uidlist
);
5171 imap_handle_error(SESSION(session
), NULL
, r
);
5175 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_FLAGGED
, NULL
,
5176 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5177 if (r
== MAILIMAP_NO_ERROR
) {
5180 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5181 mailimap_search_result_free(lep_uidlist
);
5183 flagged
= g_slist_concat(flagged
, uidlist
);
5185 imap_handle_error(SESSION(session
), NULL
, r
);
5189 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5190 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_ANSWERED
, NULL
,
5191 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5192 if (r
== MAILIMAP_NO_ERROR
) {
5195 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5196 mailimap_search_result_free(lep_uidlist
);
5198 answered
= g_slist_concat(answered
, uidlist
);
5200 imap_handle_error(SESSION(session
), NULL
, r
);
5204 if (flag_ok(IMAP_FOLDER_ITEM(fitem
), IMAP_FLAG_FORWARDED
)) {
5205 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_FORWARDED
, NULL
,
5206 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5207 if (r
== MAILIMAP_NO_ERROR
) {
5210 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5211 mailimap_search_result_free(lep_uidlist
);
5213 forwarded
= g_slist_concat(forwarded
, uidlist
);
5215 imap_handle_error(SESSION(session
), NULL
, r
);
5220 if (flag_ok(IMAP_FOLDER_ITEM(fitem
), IMAP_FLAG_SPAM
)) {
5221 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SPAM
, NULL
,
5222 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5223 if (r
== MAILIMAP_NO_ERROR
) {
5226 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5227 mailimap_search_result_free(lep_uidlist
);
5229 spam
= g_slist_concat(spam
, uidlist
);
5231 imap_handle_error(SESSION(session
), NULL
, r
);
5236 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_DELETED
, NULL
,
5237 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5238 if (r
== MAILIMAP_NO_ERROR
) {
5241 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5242 mailimap_search_result_free(lep_uidlist
);
5244 deleted
= g_slist_concat(deleted
, uidlist
);
5246 imap_handle_error(SESSION(session
), NULL
, r
);
5253 r
= imap_threaded_fetch_uid_flags(folder
, 1, &lep_uidtab
);
5254 if (r
== MAILIMAP_NO_ERROR
) {
5255 flags_hash
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
, NULL
);
5256 tags_hash
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
, NULL
);
5257 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab
, flags_hash
, tags_hash
);
5258 imap_fetch_uid_flags_list_free(lep_uidtab
);
5260 imap_handle_error(SESSION(session
), NULL
, r
);
5266 if (r
== MAILIMAP_NO_ERROR
)
5267 unlock_session(session
);
5269 for (elem
= sorted_list
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
5271 MsgPermFlags flags
, oldflags
;
5274 msginfo
= (MsgInfo
*) elem
->data
;
5275 flags
= msginfo
->flags
.perm_flags
;
5276 wasnew
= (flags
& MSG_NEW
);
5277 oldflags
= flags
& ~(MSG_NEW
|MSG_UNREAD
|MSG_REPLIED
|MSG_FORWARDED
|MSG_MARKED
|MSG_DELETED
|MSG_SPAM
);
5279 if (folder
->account
&& folder
->account
->low_bandwidth
) {
5280 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5281 flags
&= ~((reverse_seen
? 0 : MSG_UNREAD
| MSG_NEW
) | MSG_REPLIED
| MSG_FORWARDED
| MSG_MARKED
| MSG_SPAM
);
5283 flags
&= ~((reverse_seen
? 0 : MSG_UNREAD
| MSG_NEW
| MSG_MARKED
));
5286 flags
|= MSG_UNREAD
| (wasnew
? MSG_NEW
: 0);
5287 if (gslist_find_next_num(&unseen
, msginfo
->msgnum
) == msginfo
->msgnum
) {
5288 if (!reverse_seen
) {
5289 flags
|= MSG_UNREAD
| (wasnew
? MSG_NEW
: 0);
5291 flags
&= ~(MSG_UNREAD
| MSG_NEW
);
5295 if (gslist_find_next_num(&flagged
, msginfo
->msgnum
) == msginfo
->msgnum
)
5296 flags
|= MSG_MARKED
;
5298 flags
&= ~MSG_MARKED
;
5300 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5301 if (gslist_find_next_num(&answered
, msginfo
->msgnum
) == msginfo
->msgnum
)
5302 flags
|= MSG_REPLIED
;
5304 flags
&= ~MSG_REPLIED
;
5305 if (gslist_find_next_num(&forwarded
, msginfo
->msgnum
) == msginfo
->msgnum
)
5306 flags
|= MSG_FORWARDED
;
5308 flags
&= ~MSG_FORWARDED
;
5309 if (gslist_find_next_num(&spam
, msginfo
->msgnum
) == msginfo
->msgnum
)
5313 if (gslist_find_next_num(&deleted
, msginfo
->msgnum
) == msginfo
->msgnum
)
5314 flags
|= MSG_DELETED
;
5316 flags
&= ~MSG_DELETED
;
5319 if (flags_hash
!= NULL
) {
5321 flags
= GPOINTER_TO_INT(g_hash_table_lookup(flags_hash
,
5322 GINT_TO_POINTER(msginfo
->msgnum
)));
5325 if ((flags
& MSG_UNREAD
) == 0)
5331 if (tags_hash
!= NULL
) {
5332 GSList
*tags
= g_hash_table_lookup(tags_hash
, GINT_TO_POINTER(msginfo
->msgnum
));
5335 g_slist_free(msginfo
->tags
);
5336 msginfo
->tags
= NULL
;
5338 for (cur
= tags
; cur
; cur
= cur
->next
) {
5339 gchar
*real_tag
= imap_modified_utf7_to_utf8(cur
->data
, TRUE
);
5341 id
= tags_get_id_for_str(real_tag
);
5343 id
= tags_add_tag(real_tag
);
5344 got_alien_tags
= TRUE
;
5346 msginfo
->tags
= g_slist_append(
5348 GINT_TO_POINTER(id
));
5351 slist_free_strings_full(tags
);
5355 g_hash_table_insert(msgflags
, msginfo
, GINT_TO_POINTER(flags
));
5358 if (got_alien_tags
) {
5360 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
5364 g_hash_table_destroy(flags_hash
);
5366 g_hash_table_destroy(tags_hash
);
5368 imap_lep_set_free(seq_list
);
5369 g_slist_free(flagged
);
5370 g_slist_free(deleted
);
5371 g_slist_free(answered
);
5372 g_slist_free(forwarded
);
5374 g_slist_free(unseen
);
5375 g_slist_free(sorted_list
);
5378 return GINT_TO_POINTER(0);
5381 static gint
imap_get_flags(Folder
*folder
, FolderItem
*item
,
5382 MsgInfoList
*msginfo_list
, GHashTable
*msgflags
)
5385 get_flags_data
*data
= g_new0(get_flags_data
, 1);
5387 data
->folder
= folder
;
5389 data
->msginfo_list
= msginfo_list
;
5390 data
->msgflags
= msgflags
;
5391 data
->full_search
= FALSE
;
5393 GSList
*tmp
= NULL
, *cur
;
5395 if (prefs_common
.work_offline
&&
5396 !inc_offline_should_override(FALSE
,
5397 _("Claws Mail needs network access in order "
5398 "to access the IMAP server."))) {
5403 tmp
= folder_item_get_msg_list(item
);
5405 if (g_slist_length(tmp
) <= g_slist_length(msginfo_list
))
5406 data
->full_search
= TRUE
;
5408 for (cur
= tmp
; cur
; cur
= cur
->next
)
5409 procmsg_msginfo_free((MsgInfo
**)&(cur
->data
));
5413 result
= GPOINTER_TO_INT(imap_get_flags_thread(data
));
5420 static gboolean
process_flags(gpointer key
, gpointer value
, gpointer user_data
)
5422 gboolean flags_set
= GPOINTER_TO_INT(user_data
);
5423 gint flags_value
= GPOINTER_TO_INT(key
);
5424 hashtable_data
*data
= (hashtable_data
*)value
;
5425 IMAPFolderItem
*_item
= data
->item
;
5426 FolderItem
*item
= (FolderItem
*)_item
;
5427 gint ok
= MAILIMAP_ERROR_BAD_STATE
;
5428 IMAPSession
*session
= NULL
;
5430 debug_print("getting session...\n");
5431 session
= imap_session_get(item
->folder
);
5433 data
->msglist
= g_slist_reverse(data
->msglist
);
5435 debug_print("IMAP %ssetting flags to %d for %d messages\n",
5438 g_slist_length(data
->msglist
));
5440 lock_session(session
);
5442 ok
= imap_select(session
, IMAP_FOLDER(item
->folder
), item
,
5443 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5445 if (ok
== MAILIMAP_NO_ERROR
) {
5446 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
),
5447 data
->msglist
, flags_value
, NULL
, flags_set
);
5449 g_warning("can't select mailbox %s", item
->path
);
5453 unlock_session(session
);
5455 g_slist_free(data
->msglist
);
5460 static gboolean
process_tags(gpointer key
, gpointer value
, gpointer user_data
)
5462 gboolean tags_set
= GPOINTER_TO_INT(user_data
);
5463 TagsData
*data
= (TagsData
*)value
;
5464 IMAPFolderItem
*_item
= data
->item
;
5465 FolderItem
*item
= (FolderItem
*)_item
;
5466 gchar
*str
= data
->str
;
5467 gint ok
= MAILIMAP_ERROR_BAD_STATE
;
5468 IMAPSession
*session
= NULL
;
5470 debug_print("getting session...\n");
5471 session
= imap_session_get(item
->folder
);
5473 data
->msglist
= g_slist_reverse(data
->msglist
);
5475 debug_print("IMAP %ssetting tags %s for %d messages\n",
5478 g_slist_length(data
->msglist
));
5480 lock_session(session
);
5482 ok
= imap_select(session
, IMAP_FOLDER(item
->folder
), item
,
5483 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5485 if (ok
== MAILIMAP_NO_ERROR
) {
5489 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
),
5490 data
->msglist
, 0, &list
, tags_set
);
5492 g_warning("can't select mailbox %s", item
->path
);
5496 unlock_session(session
);
5498 g_slist_free(data
->msglist
);
5504 static void process_hashtable(IMAPFolderItem
*item
)
5506 if (item
->flags_set_table
) {
5507 g_hash_table_foreach_remove(item
->flags_set_table
, process_flags
, GINT_TO_POINTER(TRUE
));
5508 g_hash_table_destroy(item
->flags_set_table
);
5509 item
->flags_set_table
= NULL
;
5511 if (item
->flags_unset_table
) {
5512 g_hash_table_foreach_remove(item
->flags_unset_table
, process_flags
, GINT_TO_POINTER(FALSE
));
5513 g_hash_table_destroy(item
->flags_unset_table
);
5514 item
->flags_unset_table
= NULL
;
5516 if (item
->tags_set_table
) {
5517 g_hash_table_foreach_remove(item
->tags_set_table
, process_tags
, GINT_TO_POINTER(TRUE
));
5518 g_hash_table_destroy(item
->tags_set_table
);
5519 item
->tags_set_table
= NULL
;
5521 if (item
->tags_unset_table
) {
5522 g_hash_table_foreach_remove(item
->tags_unset_table
, process_tags
, GINT_TO_POINTER(FALSE
));
5523 g_hash_table_destroy(item
->tags_unset_table
);
5524 item
->tags_unset_table
= NULL
;
5529 static void imap_set_batch (Folder
*folder
, FolderItem
*_item
, gboolean batch
)
5531 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
5532 IMAPSession
*session
;
5534 g_return_if_fail(item
!= NULL
);
5536 if (item
->batching
== batch
)
5540 item
->batching
= TRUE
;
5541 debug_print("IMAP switching to batch mode\n");
5542 if (!item
->flags_set_table
) {
5543 item
->flags_set_table
= g_hash_table_new(NULL
, g_direct_equal
);
5545 if (!item
->flags_unset_table
) {
5546 item
->flags_unset_table
= g_hash_table_new(NULL
, g_direct_equal
);
5548 if (!item
->tags_set_table
) {
5549 item
->tags_set_table
= g_hash_table_new(NULL
, g_direct_equal
);
5551 if (!item
->tags_unset_table
) {
5552 item
->tags_unset_table
= g_hash_table_new(NULL
, g_direct_equal
);
5554 session
= imap_session_get(folder
);
5556 imap_refresh_sensitivity(session
);
5557 session
->sens_update_block
= TRUE
;
5560 debug_print("IMAP switching away from batch mode\n");
5562 process_hashtable(item
);
5563 item
->batching
= FALSE
;
5564 session
= imap_session_get(folder
);
5566 session
->sens_update_block
= FALSE
;
5567 imap_refresh_sensitivity(session
);
5574 /* data types conversion libetpan <-> claws */
5578 #define ETPAN_IMAP_MB_MARKED 1
5579 #define ETPAN_IMAP_MB_UNMARKED 2
5580 #define ETPAN_IMAP_MB_NOSELECT 4
5581 #define ETPAN_IMAP_MB_NOINFERIORS 8
5583 static int imap_flags_to_flags(struct mailimap_mbx_list_flags
* imap_flags
)
5589 if (imap_flags
->mbf_type
== MAILIMAP_MBX_LIST_FLAGS_SFLAG
) {
5590 switch (imap_flags
->mbf_sflag
) {
5591 case MAILIMAP_MBX_LIST_SFLAG_MARKED
:
5592 flags
|= ETPAN_IMAP_MB_MARKED
;
5594 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT
:
5595 flags
|= ETPAN_IMAP_MB_NOSELECT
;
5597 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED
:
5598 flags
|= ETPAN_IMAP_MB_UNMARKED
;
5603 if (imap_flags
->mbf_oflags
) {
5604 for(cur
= clist_begin(imap_flags
->mbf_oflags
) ; cur
!= NULL
;
5605 cur
= clist_next(cur
)) {
5606 struct mailimap_mbx_list_oflag
* oflag
;
5608 oflag
= clist_content(cur
);
5610 switch (oflag
->of_type
) {
5611 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS
:
5612 flags
|= ETPAN_IMAP_MB_NOINFERIORS
;
5620 static GSList
* imap_list_from_lep(IMAPFolder
* folder
,
5621 clist
* list
, const gchar
* real_path
, gboolean all
)
5624 GSList
* item_list
= NULL
, *llast
= NULL
;
5627 for(iter
= clist_begin(list
) ; iter
!= NULL
;
5628 iter
= clist_next(iter
)) {
5629 struct mailimap_mailbox_list
* mb
;
5637 FolderItem
*new_item
;
5639 mb
= clist_content(iter
);
5645 if (mb
->mb_flag
!= NULL
)
5646 flags
= imap_flags_to_flags(mb
->mb_flag
);
5648 delimiter
= mb
->mb_delimiter
;
5651 dup_name
= strdup(name
);
5652 if (delimiter
!= '\0')
5653 subst_char(dup_name
, delimiter
, '/');
5655 base
= g_path_get_basename(dup_name
);
5656 if (base
[0] == '.') {
5661 if (!all
&& path_cmp(name
, real_path
) == 0) {
5667 if (!all
&& dup_name
[strlen(dup_name
)-1] == '/') {
5673 loc_name
= imap_modified_utf7_to_utf8(base
, FALSE
);
5674 loc_path
= imap_modified_utf7_to_utf8(dup_name
, FALSE
);
5676 new_item
= folder_item_new(FOLDER(folder
), loc_name
, loc_path
);
5677 if ((flags
& ETPAN_IMAP_MB_NOINFERIORS
) != 0)
5678 new_item
->no_sub
= TRUE
;
5679 if (strcasecmp(dup_name
, "INBOX") != 0 &&
5680 ((flags
& ETPAN_IMAP_MB_NOSELECT
) != 0))
5681 new_item
->no_select
= TRUE
;
5683 if (item_list
== NULL
)
5684 llast
= item_list
= g_slist_append(item_list
, new_item
);
5686 llast
= g_slist_append(llast
, new_item
);
5687 llast
= llast
->next
;
5689 debug_print("folder '%s' found.\n", loc_path
);
5700 static GSList
* imap_get_lep_set_from_numlist(IMAPFolder
*folder
, MsgNumberList
*numlist
)
5702 GSList
*sorted_list
, *cur
;
5703 guint first
, last
, next
;
5704 GSList
*ret_list
= NULL
, *llast
= NULL
;
5705 struct mailimap_set
* current_set
;
5706 unsigned int item_count
;
5708 if (numlist
== NULL
)
5711 current_set
= mailimap_set_new_empty();
5713 sorted_list
= g_slist_copy(numlist
);
5714 sorted_list
= g_slist_sort(sorted_list
, g_int_compare
);
5716 first
= GPOINTER_TO_INT(sorted_list
->data
);
5719 for (cur
= sorted_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
5720 if (GPOINTER_TO_INT(cur
->data
) == 0)
5725 last
= GPOINTER_TO_INT(cur
->data
);
5727 next
= GPOINTER_TO_INT(cur
->next
->data
);
5731 if (last
+ 1 != next
|| next
== 0 || item_count
>= folder
->max_set_size
) {
5733 struct mailimap_set_item
* item
;
5734 item
= mailimap_set_item_new(first
, last
);
5735 mailimap_set_add(current_set
, item
);
5739 if (item_count
>= folder
->max_set_size
) {
5740 if (ret_list
== NULL
)
5741 llast
= ret_list
= g_slist_append(ret_list
,
5744 llast
= g_slist_append(llast
, current_set
);
5745 llast
= llast
->next
;
5748 current_set
= mailimap_set_new_empty();
5754 if (clist_count(current_set
->set_list
) > 0) {
5755 ret_list
= g_slist_append(ret_list
,
5759 g_slist_free(sorted_list
);
5764 static GSList
* imap_get_lep_set_from_msglist(IMAPFolder
*folder
, MsgInfoList
*msglist
)
5766 MsgNumberList
*numlist
= NULL
;
5769 numlist
= procmsg_get_number_list_for_msgs(msglist
);
5771 seq_list
= imap_get_lep_set_from_numlist(folder
, numlist
);
5772 g_slist_free(numlist
);
5777 static GSList
* imap_uid_list_from_lep(clist
* list
, gint
* length
)
5786 for(iter
= clist_begin(list
) ; iter
!= NULL
;
5787 iter
= clist_next(iter
)) {
5790 puid
= clist_content(iter
);
5791 result
= g_slist_prepend(result
, GINT_TO_POINTER(* puid
));
5794 result
= g_slist_reverse(result
);
5801 static GSList
* imap_uid_list_from_lep_tab(carray
* list
)
5808 for(i
= 0 ; i
< carray_count(list
) ; i
++) {
5811 puid
= carray_get(list
, i
);
5812 result
= g_slist_prepend(result
, GINT_TO_POINTER(* puid
));
5814 result
= g_slist_reverse(result
);
5818 static void imap_flags_hash_from_lep_uid_flags_tab(carray
* list
,
5820 GHashTable
* tags_hash
)
5824 for(i
= 0 ; i
< carray_count(list
) ; i
+= 3) {
5829 puid
= carray_get(list
, i
);
5830 pflags
= carray_get(list
, i
+ 1);
5831 tags
= carray_get(list
, i
+ 2);
5833 g_hash_table_insert(hash
, GINT_TO_POINTER(*puid
), GINT_TO_POINTER(* pflags
));
5834 g_hash_table_insert(tags_hash
, GINT_TO_POINTER(*puid
), tags
);
5838 static MsgInfo
*imap_envelope_from_lep(struct imap_fetch_env_info
* info
,
5841 MsgInfo
*msginfo
= NULL
;
5844 MsgFlags flags
= {0, 0};
5846 if (info
->headers
== NULL
)
5849 MSG_SET_TMP_FLAGS(flags
, MSG_IMAP
);
5850 if (folder_has_parent_of_type(item
, F_QUEUE
)) {
5851 MSG_SET_TMP_FLAGS(flags
, MSG_QUEUED
);
5852 } else if (folder_has_parent_of_type(item
, F_DRAFT
)) {
5853 MSG_SET_TMP_FLAGS(flags
, MSG_DRAFT
);
5855 flags
.perm_flags
= info
->flags
;
5858 size
= (goffset
) info
->size
;
5859 msginfo
= procheader_parse_str(info
->headers
, flags
, FALSE
, FALSE
);
5862 msginfo
->msgnum
= uid
;
5863 msginfo
->size
= size
;
5869 static void imap_lep_set_free(GSList
*seq_list
)
5873 for(cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
5874 struct mailimap_set
* imapset
;
5876 imapset
= cur
->data
;
5877 mailimap_set_free(imapset
);
5879 g_slist_free(seq_list
);
5882 static struct mailimap_flag_list
* imap_flag_to_lep(IMAPFolderItem
*item
, IMAPFlags flags
, GSList
*tags
)
5884 struct mailimap_flag_list
* flag_list
;
5887 flag_list
= mailimap_flag_list_new_empty();
5889 if (IMAP_IS_SEEN(flags
))
5890 mailimap_flag_list_add(flag_list
,
5891 mailimap_flag_new_seen());
5892 if (IMAP_IS_ANSWERED(flags
))
5893 mailimap_flag_list_add(flag_list
,
5894 mailimap_flag_new_answered());
5895 if (IMAP_IS_FLAGGED(flags
))
5896 mailimap_flag_list_add(flag_list
,
5897 mailimap_flag_new_flagged());
5898 if (IMAP_IS_DELETED(flags
))
5899 mailimap_flag_list_add(flag_list
,
5900 mailimap_flag_new_deleted());
5901 if (IMAP_IS_DRAFT(flags
))
5902 mailimap_flag_list_add(flag_list
,
5903 mailimap_flag_new_draft());
5904 if (IMAP_IS_FORWARDED(flags
) && flag_ok(item
, IMAP_FLAG_FORWARDED
))
5905 mailimap_flag_list_add(flag_list
,
5906 mailimap_flag_new_flag_keyword(strdup(RTAG_FORWARDED
)));
5907 if (IMAP_IS_SPAM(flags
) && flag_ok(item
, IMAP_FLAG_SPAM
))
5908 mailimap_flag_list_add(flag_list
,
5909 mailimap_flag_new_flag_keyword(strdup(RTAG_JUNK
)));
5910 else if (IMAP_IS_HAM(flags
) && flag_ok(item
, IMAP_FLAG_HAM
))
5911 mailimap_flag_list_add(flag_list
,
5912 mailimap_flag_new_flag_keyword(strdup(RTAG_NON_JUNK
)));
5914 for (; cur
; cur
= cur
->next
) {
5916 imap_utf8_to_modified_utf7(cur
->data
, TRUE
);
5917 g_strstrip(enc_str
);
5919 mailimap_flag_list_add(flag_list
,
5920 mailimap_flag_new_flag_keyword(enc_str
));
5926 guint
imap_folder_get_refcnt(Folder
*folder
)
5928 return ((IMAPFolder
*)folder
)->refcnt
;
5931 void imap_folder_ref(Folder
*folder
)
5933 ((IMAPFolder
*)folder
)->refcnt
++;
5936 void imap_disconnect_all(gboolean have_connectivity
)
5939 gboolean short_timeout
;
5940 #ifdef HAVE_NETWORKMANAGER_SUPPORT
5941 GError
*error
= NULL
;
5943 short_timeout
= !networkmanager_is_online(&error
);
5945 short_timeout
= TRUE
;
5946 g_error_free(error
);
5949 short_timeout
= TRUE
;
5953 imap_main_set_timeout(1);
5955 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
5956 PrefsAccount
*account
= list
->data
;
5957 if (account
->protocol
== A_IMAP4
) {
5958 RemoteFolder
*folder
= (RemoteFolder
*)account
->folder
;
5959 if (folder
&& folder
->session
) {
5960 if (imap_is_busy(FOLDER(folder
)))
5961 imap_threaded_cancel(FOLDER(folder
));
5963 IMAPSession
*session
= (IMAPSession
*)folder
->session
;
5964 if (have_connectivity
)
5965 imap_threaded_disconnect(FOLDER(folder
));
5966 SESSION(session
)->state
= SESSION_DISCONNECTED
;
5967 SESSION(session
)->sock
= NULL
;
5968 imap_safe_destroy(session
);
5969 folder
->session
= NULL
;
5975 imap_main_set_timeout(prefs_common
.io_timeout_secs
);
5978 void imap_folder_unref(Folder
*folder
)
5980 if (((IMAPFolder
*)folder
)->refcnt
> 0)
5981 ((IMAPFolder
*)folder
)->refcnt
--;
5984 void imap_cancel_all(void)
5989 folderlist
= folder_get_list();
5990 for (cur
= folderlist
; cur
!= NULL
; cur
= g_list_next(cur
)) {
5991 Folder
*folder
= (Folder
*) cur
->data
;
5993 if (folder
->klass
== &imap_class
) {
5994 if (imap_is_busy(folder
)) {
5995 IMAPSession
*imap_session
;
5996 RemoteFolder
*rfolder
;
5998 g_printerr("cancelled\n");
5999 imap_threaded_cancel(folder
);
6000 rfolder
= (RemoteFolder
*) folder
;
6001 imap_session
= (IMAPSession
*) rfolder
->session
;
6003 imap_session
->cancelled
= 1;
6009 gboolean
imap_cancel_all_enabled(void)
6014 folderlist
= folder_get_list();
6015 for (cur
= folderlist
; cur
!= NULL
; cur
= g_list_next(cur
)) {
6016 Folder
*folder
= (Folder
*) cur
->data
;
6018 if (folder
->klass
== &imap_class
) {
6019 if (imap_is_busy(folder
)) {
6028 static gboolean
imap_is_busy(Folder
*folder
)
6030 IMAPSession
*imap_session
;
6031 RemoteFolder
*rfolder
;
6033 rfolder
= (RemoteFolder
*) folder
;
6034 imap_session
= (IMAPSession
*) rfolder
->session
;
6035 if (imap_session
== NULL
)
6038 return imap_session
->busy
;
6041 #else /* HAVE_LIBETPAN */
6043 static FolderClass imap_class
;
6045 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
);
6046 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
);
6048 static Folder
*imap_folder_new (const gchar
*name
,
6051 static gboolean missing_imap_warning
= TRUE
;
6052 if (missing_imap_warning
) {
6053 missing_imap_warning
= FALSE
;
6055 _("You have one or more IMAP accounts "
6056 "defined. However this version of "
6057 "Claws Mail has been built without "
6058 "IMAP support; your IMAP accounts are "
6060 "You probably need to "
6061 "install libetpan and recompile "
6066 static gint
imap_create_tree (Folder
*folder
)
6070 static FolderItem
*imap_create_folder (Folder
*folder
,
6076 static gint
imap_rename_folder (Folder
*folder
,
6083 gchar
imap_get_path_separator_for_item(FolderItem
*item
)
6088 FolderClass
*imap_get_class(void)
6090 if (imap_class
.idstr
== NULL
) {
6091 imap_class
.type
= F_IMAP
;
6092 imap_class
.idstr
= "imap";
6093 imap_class
.uistr
= "IMAP";
6095 imap_class
.new_folder
= imap_folder_new
;
6096 imap_class
.create_tree
= imap_create_tree
;
6097 imap_class
.create_folder
= imap_create_folder
;
6098 imap_class
.rename_folder
= imap_rename_folder
;
6100 imap_class
.set_xml
= folder_set_xml
;
6101 imap_class
.get_xml
= folder_get_xml
;
6102 imap_class
.item_set_xml
= imap_item_set_xml
;
6103 imap_class
.item_get_xml
= imap_item_get_xml
;
6104 /* nothing implemented */
6110 void imap_disconnect_all(gboolean have_connectivity
)
6114 gint
imap_subscribe(Folder
*folder
, FolderItem
*item
, gchar
*rpath
, gboolean sub
)
6119 GList
* imap_scan_subtree(Folder
*folder
, FolderItem
*item
, gboolean unsubs_only
, gboolean recursive
)
6124 void imap_cache_msg(FolderItem
*item
, gint msgnum
)
6128 void imap_cancel_all(void)
6132 gboolean
imap_cancel_all_enabled(void)
6139 #ifdef HAVE_LIBETPAN
6140 static void imap_synchronise(FolderItem
*item
, gint days
)
6142 if (IMAP_FOLDER_ITEM(item
)->last_sync
== IMAP_FOLDER_ITEM(item
)->last_change
) {
6143 debug_print("%s already synced\n", item
->path
?item
->path
:item
->name
);
6146 debug_print("syncing %s\n", item
->path
?item
->path
:item
->name
);
6147 imap_gtk_synchronise(item
, days
);
6148 IMAP_FOLDER_ITEM(item
)->last_sync
= IMAP_FOLDER_ITEM(item
)->last_change
;
6152 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
)
6154 #ifdef HAVE_LIBETPAN
6157 folder_item_set_xml(folder
, item
, tag
);
6159 #ifdef HAVE_LIBETPAN
6160 for (cur
= tag
->attr
; cur
!= NULL
; cur
= g_list_next(cur
)) {
6161 XMLAttr
*attr
= (XMLAttr
*) cur
->data
;
6163 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
6164 if (!strcmp(attr
->name
, "uidnext"))
6165 IMAP_FOLDER_ITEM(item
)->uid_next
= atoi(attr
->value
);
6166 if (!strcmp(attr
->name
, "last_sync"))
6167 IMAP_FOLDER_ITEM(item
)->last_sync
= atoi(attr
->value
);
6168 if (!strcmp(attr
->name
, "last_change"))
6169 IMAP_FOLDER_ITEM(item
)->last_change
= atoi(attr
->value
);
6171 if (IMAP_FOLDER_ITEM(item
)->last_change
== 0)
6172 IMAP_FOLDER_ITEM(item
)->last_change
= time(NULL
);
6176 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
)
6180 tag
= folder_item_get_xml(folder
, item
);
6182 #ifdef HAVE_LIBETPAN
6183 xml_tag_add_attr(tag
, xml_attr_new_int("uidnext",
6184 IMAP_FOLDER_ITEM(item
)->uid_next
));
6185 xml_tag_add_attr(tag
, xml_attr_new_int("last_sync",
6186 IMAP_FOLDER_ITEM(item
)->last_sync
));
6187 xml_tag_add_attr(tag
, xml_attr_new_int("last_change",
6188 IMAP_FOLDER_ITEM(item
)->last_change
));
6194 /* ===================================================================
6195 * UTF-7 conversion routines as in RFC 2192
6196 * ===================================================================
6197 * These two functions from:
6199 * Copyright (C) 2003-2004 Pawel Salek. */
6201 /* UTF7 modified base64 alphabet */
6202 static char base64chars
[] =
6203 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
6204 #define UNDEFINED 64
6206 /* UTF16 definitions */
6207 #define UTF16MASK 0x03FFUL
6208 #define UTF16SHIFT 10
6209 #define UTF16BASE 0x10000UL
6210 #define UTF16HIGHSTART 0xD800UL
6211 #define UTF16HIGHEND 0xDBFFUL
6212 #define UTF16LOSTART 0xDC00UL
6213 #define UTF16LOEND 0xDFFFUL
6216 /* Convert an IMAP mailbox to a UTF-8 string.
6217 * dst needs to have roughly 4 times the storage space of src
6218 * Hex encoding can triple the size of the input
6219 * UTF-7 can be slightly denser than UTF-8
6220 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
6222 char* imap_modified_utf7_to_utf8(const char *mbox
, gboolean change_spaces
)
6224 unsigned c
, i
, bitcount
;
6225 unsigned long ucs4
, utf16
, bitbuf
;
6226 unsigned char base64
[256];
6228 char *dst
, *res
= g_malloc(2*strlen(mbox
)+1);
6232 if(!dst
) return NULL
;
6233 /* initialize modified base64 decoding table */
6234 memset(base64
, UNDEFINED
, sizeof (base64
));
6235 for (i
= 0; i
< sizeof (base64chars
); ++i
) {
6236 base64
[(unsigned)base64chars
[i
]] = i
;
6239 /* loop until end of string */
6240 while (*src
!= '\0') {
6242 /* deal with literal characters and &- */
6243 if (c
!= '&' || *src
== '-') {
6244 /* encode literally */
6245 if (change_spaces
&& c
== '_')
6249 /* skip over the '-' if this is an &- sequence */
6250 if (c
== '&') ++src
;
6252 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
6256 while ((c
= base64
[(unsigned char) *src
]) != UNDEFINED
) {
6258 bitbuf
= (bitbuf
<< 6) | c
;
6260 /* enough bits for a UTF-16 character? */
6261 if (bitcount
>= 16) {
6263 utf16
= (bitcount
? bitbuf
>> bitcount
6265 /* convert UTF16 to UCS4 */
6267 (utf16
>= UTF16HIGHSTART
&& utf16
<= UTF16HIGHEND
) {
6268 ucs4
= (utf16
- UTF16HIGHSTART
) << UTF16SHIFT
;
6271 (utf16
>= UTF16LOSTART
&& utf16
<= UTF16LOEND
) {
6272 ucs4
+= utf16
- UTF16LOSTART
+ UTF16BASE
;
6277 /* convert UTF-16 range of UCS4 to UTF-8 */
6278 if (ucs4
<= 0x7fUL
) {
6281 } else if (ucs4
<= 0x7ffUL
) {
6282 dst
[0] = 0xc0 | (ucs4
>> 6);
6283 dst
[1] = 0x80 | (ucs4
& 0x3f);
6285 } else if (ucs4
<= 0xffffUL
) {
6286 dst
[0] = 0xe0 | (ucs4
>> 12);
6287 dst
[1] = 0x80 | ((ucs4
>> 6) & 0x3f);
6288 dst
[2] = 0x80 | (ucs4
& 0x3f);
6291 dst
[0] = 0xf0 | (ucs4
>> 18);
6292 dst
[1] = 0x80 | ((ucs4
>> 12) & 0x3f);
6293 dst
[2] = 0x80 | ((ucs4
>> 6) & 0x3f);
6294 dst
[3] = 0x80 | (ucs4
& 0x3f);
6299 /* skip over trailing '-' in modified UTF-7 encoding */
6300 if (*src
== '-') ++src
;
6303 /* terminate destination string */
6308 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
6309 * dst should be about twice the length of src to deal with non-hex
6312 char* imap_utf8_to_modified_utf7(const char *src
, gboolean change_spaces
)
6314 unsigned int utf8pos
, utf8total
, c
, utf7mode
, bitstogo
, utf16flag
;
6315 unsigned long ucs4
= 0, bitbuf
= 0;
6317 /* initialize hex lookup table */
6320 if (!src
) return NULL
;
6322 res
= malloc(2*strlen(src
)+1);
6324 if(!dst
) return NULL
;
6330 while ((c
= (unsigned char)*src
) != '\0') {
6332 /* normal character? */
6333 if (c
>= ' ' && c
<= '~' && (c
!= '_' || !change_spaces
)) {
6334 /* switch out of UTF-7 mode */
6337 *dst
++ = base64chars
[(bitbuf
<< (6 - bitstogo
)) & 0x3F];
6345 if (change_spaces
&& c
== ' ')
6349 /* encode '&' as '&-' */
6355 /* switch to UTF-7 mode */
6360 /* Encode US-ASCII characters as themselves */
6363 } else if (utf8total
) {
6364 /* save UTF8 bits into UCS4 */
6365 ucs4
= (ucs4
<< 6) | (c
& 0x3FUL
);
6366 if (++utf8pos
< utf8total
) {
6374 } else if (c
< 0xF0) {
6378 /* NOTE: can't convert UTF8 sequences longer than 4 */
6384 /* loop to split ucs4 into two utf16 chars if necessary */
6387 if (ucs4
>= UTF16BASE
) {
6389 bitbuf
= (bitbuf
<< 16) | ((ucs4
>> UTF16SHIFT
)
6391 ucs4
= (ucs4
& UTF16MASK
) + UTF16LOSTART
;
6394 bitbuf
= (bitbuf
<< 16) | ucs4
;
6398 /* spew out base64 */
6399 while (bitstogo
>= 6) {
6401 *dst
++ = base64chars
[(bitstogo
? (bitbuf
>> bitstogo
)
6405 } while (utf16flag
);
6407 /* if in UTF-7 mode, finish in ASCII */
6410 *dst
++ = base64chars
[(bitbuf
<< (6 - bitstogo
)) & 0x3F];
6414 /* tie off string */