2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 the Claws Mail team and Hiroyuki Yamamoto
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"
56 #include "procheader.h"
57 #include "prefs_account.h"
61 #include "prefs_common.h"
62 #include "inputdialog.h"
64 #include "remotefolder.h"
66 #include "statusbar.h"
68 #include "imap-thread.h"
72 #include "passwordstore.h"
73 #include "file-utils.h"
78 typedef struct _IMAPFolder IMAPFolder
;
79 typedef struct _IMAPSession IMAPSession
;
80 typedef struct _IMAPNameSpace IMAPNameSpace
;
81 typedef struct _IMAPFolderItem IMAPFolderItem
;
83 #define IMAP_FOLDER(obj) ((IMAPFolder *)obj)
84 #define IMAP_FOLDER_ITEM(obj) ((IMAPFolderItem *)obj)
85 #define IMAP_SESSION(obj) ((IMAPSession *)obj)
91 /* list of IMAPNameSpace */
95 gchar last_seen_separator
;
98 gchar
*search_charset
;
99 gboolean search_charset_supported
;
106 gboolean authenticated
;
115 gboolean folder_content_changed
;
126 gboolean sens_update_block
;
129 gint scan_tree_recurs_depth
;
132 struct _IMAPNameSpace
138 #define IMAPBUFSIZE 8192
140 #define IMAP_IS_SEEN(flags) ((flags & IMAP_FLAG_SEEN) != 0)
141 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
142 #define IMAP_IS_FLAGGED(flags) ((flags & IMAP_FLAG_FLAGGED) != 0)
143 #define IMAP_IS_DELETED(flags) ((flags & IMAP_FLAG_DELETED) != 0)
144 #define IMAP_IS_DRAFT(flags) ((flags & IMAP_FLAG_DRAFT) != 0)
145 #define IMAP_IS_FORWARDED(flags) ((flags & IMAP_FLAG_FORWARDED) != 0)
146 #define IMAP_IS_SPAM(flags) ((flags & IMAP_FLAG_SPAM) != 0)
147 #define IMAP_IS_HAM(flags) ((flags & IMAP_FLAG_HAM) != 0)
150 #define IMAP4_PORT 143
152 #define IMAPS_PORT 993
155 #define IMAP_CMD_LIMIT 1000
158 ITEM_CAN_CREATE_FLAGS_UNKNOWN
= 0,
159 ITEM_CAN_CREATE_FLAGS
,
160 ITEM_CANNOT_CREATE_FLAGS
163 struct _IMAPFolderItem
172 GHashTable
*flags_set_table
;
173 GHashTable
*flags_unset_table
;
176 gboolean should_update
;
177 gboolean should_trash_cache
;
178 gint can_create_flags
;
180 GHashTable
*tags_set_table
;
181 GHashTable
*tags_unset_table
;
186 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
);
187 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
);
189 static void imap_folder_init (Folder
*folder
,
193 static Folder
*imap_folder_new (const gchar
*name
,
195 static void imap_folder_destroy (Folder
*folder
);
197 static IMAPSession
*imap_session_new (Folder
*folder
,
198 const PrefsAccount
*account
);
199 static gint
imap_session_authenticate(IMAPSession
*session
,
200 PrefsAccount
*account
);
201 static void imap_session_destroy (Session
*session
);
203 static gchar
*imap_fetch_msg (Folder
*folder
,
206 static gchar
*imap_fetch_msg_full (Folder
*folder
,
211 static void imap_remove_cached_msg (Folder
*folder
,
214 static gint
imap_add_msg (Folder
*folder
,
218 static gint
imap_add_msgs (Folder
*folder
,
221 GHashTable
*relation
);
223 static gint
imap_copy_msg (Folder
*folder
,
226 static gint
imap_copy_msgs (Folder
*folder
,
228 MsgInfoList
*msglist
,
229 GHashTable
*relation
);
231 static gint
search_msgs (Folder
*folder
,
232 FolderItem
*container
,
233 MsgNumberList
**msgs
,
235 MatcherList
*predicate
,
236 SearchProgressNotify progress_cb
,
237 gpointer progress_data
);
239 static gint
imap_remove_msg (Folder
*folder
,
242 static gint
imap_remove_msgs (Folder
*folder
,
244 MsgInfoList
*msglist
,
245 GHashTable
*relation
);
246 static gint
imap_expunge (Folder
*folder
,
248 static gint
imap_remove_all_msg (Folder
*folder
,
251 static gboolean
imap_is_msg_changed (Folder
*folder
,
255 static gint
imap_close (Folder
*folder
,
258 static gint
imap_scan_tree (Folder
*folder
);
260 static gint
imap_create_tree (Folder
*folder
);
262 static FolderItem
*imap_create_folder (Folder
*folder
,
265 static gint
imap_rename_folder (Folder
*folder
,
268 static gint
imap_remove_folder (Folder
*folder
,
271 static FolderItem
*imap_folder_item_new (Folder
*folder
);
272 static void imap_folder_item_destroy (Folder
*folder
,
275 static IMAPSession
*imap_session_get (Folder
*folder
);
277 static gint
imap_auth (IMAPSession
*session
,
282 static gint
imap_scan_tree_recursive (IMAPSession
*session
,
285 static gint
imap_scan_tree_recursive_dive (IMAPSession
*session
,
289 static void imap_create_missing_folders (Folder
*folder
);
290 static FolderItem
*imap_create_special_folder
292 SpecialFolderItemType stype
,
295 static gint
imap_do_copy_msgs (Folder
*folder
,
297 MsgInfoList
*msglist
,
298 GHashTable
*relation
,
299 gboolean same_dest_ok
);
301 static gint
imap_do_remove_msgs (Folder
*folder
,
303 MsgInfoList
*msglist
,
304 GHashTable
*relation
);
306 static void imap_delete_all_cached_messages (FolderItem
*item
);
307 static void imap_set_batch (Folder
*folder
,
310 static gint
imap_set_message_flags (IMAPSession
*session
,
311 IMAPFolderItem
*item
,
312 MsgNumberList
*numlist
,
316 static gint
imap_select (IMAPSession
*session
,
322 guint32
*uid_validity
,
323 gint
*can_create_flags
,
325 static gint
imap_status (IMAPSession
*session
,
328 IMAPFolderItem
*item
,
331 guint32
*uid_validity
,
334 static void imap_commit_tags (FolderItem
*item
,
339 static gchar
imap_get_path_separator (IMAPSession
*session
,
343 static gchar
*imap_get_real_path (IMAPSession
*session
,
348 static void imap_synchronise (FolderItem
*item
, gint days
);
350 static gboolean
imap_is_busy (Folder
*folder
);
352 static void imap_free_capabilities (IMAPSession
*session
);
354 /* low-level IMAP4rev1 commands */
355 static gint
imap_cmd_login (IMAPSession
*session
,
359 static gint
imap_cmd_noop (IMAPSession
*session
);
361 static gint
imap_cmd_starttls (IMAPSession
*session
);
363 static gint
imap_cmd_select (IMAPSession
*session
,
368 guint32
*uid_validity
,
369 gint
*can_create_flags
,
372 static gint
imap_cmd_close (IMAPSession
*session
);
373 static gint
imap_cmd_examine (IMAPSession
*session
,
378 guint32
*uid_validity
,
380 static gint
imap_cmd_create (IMAPSession
*sock
,
381 const gchar
*folder
);
382 static gint
imap_cmd_rename (IMAPSession
*sock
,
383 const gchar
*oldfolder
,
384 const gchar
*newfolder
);
385 static gint
imap_cmd_delete (IMAPSession
*session
,
386 const gchar
*folder
);
387 static gint
imap_cmd_fetch (IMAPSession
*sock
,
389 const gchar
*filename
,
392 static gint
imap_cmd_append (IMAPSession
*session
,
393 IMAPFolderItem
*item
,
394 const gchar
*destfolder
,
398 static gint
imap_cmd_copy (IMAPSession
*session
,
399 struct mailimap_set
* set
,
400 const gchar
*destfolder
,
401 struct mailimap_set
** source
,
402 struct mailimap_set
** dest
);
403 static gint
imap_cmd_store (IMAPSession
*session
,
404 IMAPFolderItem
*item
,
405 struct mailimap_set
* set
,
409 static gint
imap_cmd_expunge (IMAPSession
*session
);
411 static void imap_path_separator_subst (gchar
*str
,
414 static gboolean
imap_rename_folder_func (GNode
*node
,
416 static gint
imap_get_num_list (Folder
*folder
,
419 gboolean
*old_uids_valid
);
420 static GSList
*imap_get_msginfos (Folder
*folder
,
422 GSList
*msgnum_list
);
423 static MsgInfo
*imap_get_msginfo (Folder
*folder
,
426 static gboolean
imap_scan_required (Folder
*folder
,
428 static void imap_change_flags (Folder
*folder
,
431 MsgPermFlags newflags
);
432 static gint
imap_get_flags (Folder
*folder
,
434 MsgInfoList
*msglist
,
435 GHashTable
*msgflags
);
436 static gchar
*imap_item_get_path (Folder
*folder
,
438 static MsgInfo
*imap_parse_msg(const gchar
*file
, FolderItem
*item
);
441 /* data types conversion libetpan <-> claws */
442 static GSList
* imap_list_from_lep(IMAPFolder
* folder
,
443 clist
* list
, const gchar
* real_path
, gboolean all
);
444 static GSList
* imap_get_lep_set_from_numlist(IMAPFolder
*folder
, MsgNumberList
*numlist
);
445 static GSList
* imap_get_lep_set_from_msglist(IMAPFolder
*folder
, MsgInfoList
*msglist
);
446 static GSList
* imap_uid_list_from_lep(clist
* list
, gint
* length
);
447 static GSList
* imap_uid_list_from_lep_tab(carray
* list
);
448 static void imap_flags_hash_from_lep_uid_flags_tab(carray
* list
,
450 GHashTable
*tags_hash
);
451 static MsgInfo
*imap_envelope_from_lep(struct imap_fetch_env_info
* info
,
453 static void imap_lep_set_free(GSList
*seq_list
);
454 static struct mailimap_flag_list
* imap_flag_to_lep(IMAPFolderItem
*item
, IMAPFlags flags
, GSList
*tags
);
456 typedef struct _hashtable_data
{
458 IMAPFolderItem
*item
;
461 static FolderClass imap_class
;
463 FolderClass
*imap_get_class(void)
465 if (imap_class
.idstr
== NULL
) {
466 imap_class
.type
= F_IMAP
;
467 imap_class
.idstr
= "imap";
468 imap_class
.uistr
= "IMAP";
469 imap_class
.supports_server_search
= TRUE
;
471 /* Folder functions */
472 imap_class
.new_folder
= imap_folder_new
;
473 imap_class
.destroy_folder
= imap_folder_destroy
;
474 imap_class
.scan_tree
= imap_scan_tree
;
475 imap_class
.create_tree
= imap_create_tree
;
477 /* FolderItem functions */
478 imap_class
.item_new
= imap_folder_item_new
;
479 imap_class
.item_destroy
= imap_folder_item_destroy
;
480 imap_class
.item_get_path
= imap_item_get_path
;
481 imap_class
.create_folder
= imap_create_folder
;
482 imap_class
.rename_folder
= imap_rename_folder
;
483 imap_class
.remove_folder
= imap_remove_folder
;
484 imap_class
.close
= imap_close
;
485 imap_class
.get_num_list
= imap_get_num_list
;
486 imap_class
.scan_required
= imap_scan_required
;
487 imap_class
.set_xml
= folder_set_xml
;
488 imap_class
.get_xml
= folder_get_xml
;
489 imap_class
.item_set_xml
= imap_item_set_xml
;
490 imap_class
.item_get_xml
= imap_item_get_xml
;
492 /* Message functions */
493 imap_class
.get_msginfo
= imap_get_msginfo
;
494 imap_class
.get_msginfos
= imap_get_msginfos
;
495 imap_class
.fetch_msg
= imap_fetch_msg
;
496 imap_class
.fetch_msg_full
= imap_fetch_msg_full
;
497 imap_class
.add_msg
= imap_add_msg
;
498 imap_class
.add_msgs
= imap_add_msgs
;
499 imap_class
.copy_msg
= imap_copy_msg
;
500 imap_class
.copy_msgs
= imap_copy_msgs
;
501 imap_class
.search_msgs
= search_msgs
;
502 imap_class
.remove_msg
= imap_remove_msg
;
503 imap_class
.remove_msgs
= imap_remove_msgs
;
504 imap_class
.expunge
= imap_expunge
;
505 imap_class
.remove_all_msg
= imap_remove_all_msg
;
506 imap_class
.is_msg_changed
= imap_is_msg_changed
;
507 imap_class
.change_flags
= imap_change_flags
;
508 imap_class
.get_flags
= imap_get_flags
;
509 imap_class
.set_batch
= imap_set_batch
;
510 imap_class
.synchronise
= imap_synchronise
;
511 imap_class
.remove_cached_msg
= imap_remove_cached_msg
;
512 imap_class
.commit_tags
= imap_commit_tags
;
518 static void imap_refresh_sensitivity (IMAPSession
*session
)
522 if (session
->sens_update_block
)
524 mainwin
= mainwindow_get_mainwindow();
526 toolbar_main_set_sensitive(mainwin
);
527 main_window_set_menu_sensitive(mainwin
);
531 static void lock_session(IMAPSession
*session
)
534 debug_print("locking session %p (%d)\n", session
, session
->busy
);
536 debug_print(" SESSION WAS LOCKED !! \n");
537 session
->busy
= TRUE
;
538 imap_refresh_sensitivity(session
);
540 debug_print("can't lock null session\n");
544 static void unlock_session(IMAPSession
*session
)
547 debug_print("unlocking session %p\n", session
);
548 session
->busy
= FALSE
;
549 imap_refresh_sensitivity(session
);
551 debug_print("can't unlock null session\n");
555 static gboolean
imap_ping(gpointer data
)
557 Session
*session
= (Session
*)data
;
558 IMAPSession
*imap_session
= IMAP_SESSION(session
);
561 if (session
->state
!= SESSION_READY
)
563 if (imap_session
->busy
|| !imap_session
->authenticated
)
566 lock_session(imap_session
);
567 r
= imap_cmd_noop(imap_session
);
568 unlock_session(imap_session
);
570 return r
== MAILIMAP_NO_ERROR
;
573 static void imap_disc_session_destroy(IMAPSession
*session
)
575 RemoteFolder
*rfolder
= NULL
;
580 rfolder
= REMOTE_FOLDER(IMAP_SESSION(session
)->folder
);
584 log_warning(LOG_PROTOCOL
, _("IMAP connection broken\n"));
585 SESSION(session
)->state
= SESSION_DISCONNECTED
;
586 SESSION(session
)->sock
= NULL
;
589 static void imap_safe_destroy(IMAPSession
*session
)
592 session_destroy(SESSION(session
));
594 session
->do_destroy
= TRUE
;
597 static gboolean
is_fatal(int libetpan_errcode
)
599 switch(libetpan_errcode
) {
600 case MAILIMAP_ERROR_STREAM
:
601 case MAILIMAP_ERROR_PROTOCOL
:
602 case MAILIMAP_ERROR_PARSE
:
603 case MAILIMAP_ERROR_BAD_STATE
:
610 #define MY_LOG_WARNING(concat_cmd, ...) \
612 log_warning(LOG_PROTOCOL, msg, __VA_ARGS__); \
615 static void imap_handle_error(Session
*session
, const gchar
*server
, int libetpan_errcode
)
617 const gchar
*session_server
= (session
? session
->server
: NULL
);
620 if (session_server
== NULL
)
621 session_server
= server
;
622 if (session_server
== NULL
)
623 session_server
= "(null)";
625 switch(libetpan_errcode
) {
626 case MAILIMAP_NO_ERROR
:
628 case MAILIMAP_NO_ERROR_AUTHENTICATED
:
629 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("authenticated"), "\n", NULL
), session_server
)
631 case MAILIMAP_NO_ERROR_NON_AUTHENTICATED
:
632 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("not authenticated"), "\n", NULL
), session_server
)
634 case MAILIMAP_ERROR_BAD_STATE
:
635 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("bad state"), "\n", NULL
), session_server
)
637 case MAILIMAP_ERROR_STREAM
:
638 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("stream error"), "\n", NULL
), session_server
)
640 case MAILIMAP_ERROR_PARSE
:
641 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("parse error "
642 "(very probably non-RFC compliance from the server)"), "\n", NULL
), session_server
)
644 case MAILIMAP_ERROR_CONNECTION_REFUSED
:
645 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("connection refused"), "\n", NULL
), session_server
)
647 case MAILIMAP_ERROR_MEMORY
:
648 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("memory error"), "\n", NULL
), session_server
)
650 case MAILIMAP_ERROR_FATAL
:
651 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("fatal error"), "\n", NULL
), session_server
)
653 case MAILIMAP_ERROR_PROTOCOL
:
654 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("protocol error "
655 "(very probably non-RFC compliance from the server)"), "\n", NULL
), session_server
)
657 case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION
:
658 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("connection not accepted"), "\n", NULL
), session_server
)
660 case MAILIMAP_ERROR_APPEND
:
661 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("APPEND error"), "\n", NULL
), session_server
)
663 case MAILIMAP_ERROR_NOOP
:
664 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("NOOP error"), "\n", NULL
), session_server
)
666 case MAILIMAP_ERROR_LOGOUT
:
667 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("LOGOUT error"), "\n", NULL
), session_server
)
669 case MAILIMAP_ERROR_CAPABILITY
:
670 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("CAPABILITY error"), "\n", NULL
), session_server
)
672 case MAILIMAP_ERROR_CHECK
:
673 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("CHECK error"), "\n", NULL
), session_server
)
675 case MAILIMAP_ERROR_CLOSE
:
676 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("CLOSE error"), "\n", NULL
), session_server
)
678 case MAILIMAP_ERROR_EXPUNGE
:
679 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("EXPUNGE error"), "\n", NULL
), session_server
)
681 case MAILIMAP_ERROR_COPY
:
682 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("COPY error"), "\n", NULL
), session_server
)
684 case MAILIMAP_ERROR_UID_COPY
:
685 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("UID COPY error"), "\n", NULL
), session_server
)
687 case MAILIMAP_ERROR_CREATE
:
688 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("CREATE error"), "\n", NULL
), session_server
)
690 case MAILIMAP_ERROR_DELETE
:
691 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("DELETE error"), "\n", NULL
), session_server
)
693 case MAILIMAP_ERROR_EXAMINE
:
694 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("EXAMINE error"), "\n", NULL
), session_server
)
696 case MAILIMAP_ERROR_FETCH
:
697 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("FETCH error"), "\n", NULL
), session_server
)
699 case MAILIMAP_ERROR_UID_FETCH
:
700 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("UID FETCH error"), "\n", NULL
), session_server
)
702 case MAILIMAP_ERROR_LIST
:
703 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("LIST error"), "\n", NULL
), session_server
)
705 case MAILIMAP_ERROR_LOGIN
:
706 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("LOGIN error"), "\n", NULL
), session_server
)
708 case MAILIMAP_ERROR_LSUB
:
709 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("LSUB error"), "\n", NULL
), session_server
)
711 case MAILIMAP_ERROR_RENAME
:
712 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("RENAME error"), "\n", NULL
), session_server
)
714 case MAILIMAP_ERROR_SEARCH
:
715 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("SEARCH error"), "\n", NULL
), session_server
)
717 case MAILIMAP_ERROR_UID_SEARCH
:
718 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("UID SEARCH error"), "\n", NULL
), session_server
)
720 case MAILIMAP_ERROR_SELECT
:
721 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("SELECT error"), "\n", NULL
), session_server
)
723 case MAILIMAP_ERROR_STATUS
:
724 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("STATUS error"), "\n", NULL
), session_server
)
726 case MAILIMAP_ERROR_STORE
:
727 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("STORE error"), "\n", NULL
), session_server
)
729 case MAILIMAP_ERROR_UID_STORE
:
730 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("UID STORE error"), "\n", NULL
), session_server
)
732 case MAILIMAP_ERROR_SUBSCRIBE
:
733 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("SUBSCRIBE error"), "\n", NULL
), session_server
)
735 case MAILIMAP_ERROR_UNSUBSCRIBE
:
736 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("UNSUBSCRIBE error"), "\n", NULL
), session_server
)
738 case MAILIMAP_ERROR_STARTTLS
:
739 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("STARTTLS error"), "\n", NULL
), session_server
)
741 case MAILIMAP_ERROR_INVAL
:
742 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("INVAL error"), "\n", NULL
), session_server
)
744 case MAILIMAP_ERROR_EXTENSION
:
745 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("EXTENSION error"), "\n", NULL
), session_server
)
747 case MAILIMAP_ERROR_SASL
:
748 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("SASL error"), "\n", NULL
), session_server
)
751 case MAILIMAP_ERROR_SSL
:
752 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("TLS error"), "\n", NULL
), session_server
)
756 MY_LOG_WARNING(g_strconcat(_("IMAP error on %s:"), " ", _("Unknown error [%d]"), "\n", NULL
),
757 session_server
, libetpan_errcode
)
761 if (session
&& is_fatal(libetpan_errcode
)) {
762 imap_disc_session_destroy(IMAP_SESSION(session
));
763 } else if (session
&& !is_fatal(libetpan_errcode
)) {
764 if (IMAP_SESSION(session
)->busy
)
765 unlock_session(IMAP_SESSION(session
));
769 #undef MY_LOG_WARNING
771 static Folder
*imap_folder_new(const gchar
*name
, const gchar
*path
)
775 folder
= (Folder
*)g_new0(IMAPFolder
, 1);
776 folder
->klass
= &imap_class
;
777 imap_folder_init(folder
, name
, path
);
782 static void imap_folder_destroy(Folder
*folder
)
784 while (imap_folder_get_refcnt(folder
) > 0)
785 gtk_main_iteration();
787 g_free(IMAP_FOLDER(folder
)->search_charset
);
789 folder_remote_folder_destroy(REMOTE_FOLDER(folder
));
793 static void imap_folder_init(Folder
*folder
, const gchar
*name
,
796 folder_remote_folder_init((Folder
*)folder
, name
, path
);
797 IMAP_FOLDER(folder
)->search_charset_supported
= TRUE
;
798 IMAP_FOLDER(folder
)->search_charset
= g_strdup(conv_get_locale_charset_str_no_utf8());
801 static FolderItem
*imap_folder_item_new(Folder
*folder
)
803 IMAPFolderItem
*item
;
805 item
= g_new0(IMAPFolderItem
, 1);
808 item
->uid_list
= NULL
;
810 return (FolderItem
*)item
;
813 static void imap_folder_item_destroy(Folder
*folder
, FolderItem
*_item
)
815 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
817 g_return_if_fail(item
!= NULL
);
818 g_slist_free(item
->uid_list
);
823 static gboolean
imap_reset_uid_lists_func(GNode
*node
, gpointer data
)
825 IMAPFolderItem
*item
= (IMAPFolderItem
*)node
->data
;
828 g_slist_free(item
->uid_list
);
829 item
->uid_list
= NULL
;
834 static void imap_reset_uid_lists(Folder
*folder
)
836 if(folder
->node
== NULL
)
839 /* Destroy all uid lists and rest last uid */
840 g_node_traverse(folder
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, imap_reset_uid_lists_func
, NULL
);
843 static int imap_get_capabilities(IMAPSession
*session
)
845 struct mailimap_capability_data
*capabilities
= NULL
;
849 if (session
->capability
!= NULL
)
850 return MAILIMAP_NO_ERROR
;
852 result
= imap_threaded_capability(session
->folder
, &capabilities
);
854 if (result
!= MAILIMAP_NO_ERROR
) {
858 if (capabilities
== NULL
|| capabilities
->cap_list
== NULL
) {
859 return MAILIMAP_NO_ERROR
;
862 for(cur
= clist_begin(capabilities
->cap_list
) ; cur
!= NULL
;
863 cur
= clist_next(cur
)) {
864 struct mailimap_capability
* cap
=
866 if (!cap
|| cap
->cap_data
.cap_name
== NULL
)
868 session
->capability
= g_slist_append
869 (session
->capability
,
870 g_strdup(cap
->cap_data
.cap_name
));
871 debug_print("got capa %s\n", cap
->cap_data
.cap_name
);
873 mailimap_capability_data_free(capabilities
);
874 return MAILIMAP_NO_ERROR
;
877 static gboolean
imap_has_capability(IMAPSession
*session
, const gchar
*cap
)
880 for (cur
= session
->capability
; cur
; cur
= cur
->next
) {
881 if (!g_ascii_strcasecmp(cur
->data
, cap
))
887 static gint
imap_auth(IMAPSession
*session
, const gchar
*user
, const gchar
*pass
,
890 gint ok
= MAILIMAP_ERROR_LOGIN
;
891 static time_t last_login_err
= 0;
892 gchar
*ext_info
= "";
894 gchar
*server
= NULL
;
895 if ((r
= imap_get_capabilities(session
)) != MAILIMAP_NO_ERROR
) {
896 imap_handle_error(SESSION(session
), NULL
, r
);
899 server
= g_strdup(SESSION(session
)->server
);
902 ok
= imap_cmd_login(session
, user
, pass
, "ANONYMOUS");
904 case IMAP_AUTH_CRAM_MD5
:
905 ok
= imap_cmd_login(session
, user
, pass
, "CRAM-MD5");
907 case IMAP_AUTH_DIGEST_MD5
:
908 ok
= imap_cmd_login(session
, user
, pass
, "DIGEST-MD5");
910 case IMAP_AUTH_SCRAM_SHA1
:
911 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-1");
913 case IMAP_AUTH_SCRAM_SHA224
:
914 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-224");
916 case IMAP_AUTH_SCRAM_SHA256
:
917 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-256");
919 case IMAP_AUTH_SCRAM_SHA384
:
920 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-384");
922 case IMAP_AUTH_SCRAM_SHA512
:
923 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-512");
925 case IMAP_AUTH_PLAIN
:
926 ok
= imap_cmd_login(session
, user
, pass
, "PLAIN");
929 case IMAP_AUTH_OAUTH2
:
930 ok
= imap_cmd_login(session
, user
, pass
, "XOAUTH2");
933 case IMAP_AUTH_LOGIN
:
934 ok
= imap_cmd_login(session
, user
, pass
, "LOGIN");
936 case IMAP_AUTH_PLAINTEXT
:
937 ok
= imap_cmd_login(session
, user
, pass
, "plaintext");
939 case IMAP_AUTH_GSSAPI
:
940 ok
= imap_cmd_login(session
, user
, pass
, "GSSAPI");
943 debug_print("capabilities:\n"
947 "\t SCRAM-SHA-1 %d\n"
948 "\t SCRAM-SHA-224 %d\n"
949 "\t SCRAM-SHA-256 %d\n"
950 "\t SCRAM-SHA-384 %d\n"
951 "\t SCRAM-SHA-512 %d\n"
958 imap_has_capability(session
, "ANONYMOUS"),
959 imap_has_capability(session
, "CRAM-MD5"),
960 imap_has_capability(session
, "DIGEST-MD5"),
961 imap_has_capability(session
, "SCRAM-SHA-1"),
962 imap_has_capability(session
, "SCRAM-SHA-224"),
963 imap_has_capability(session
, "SCRAM-SHA-256"),
964 imap_has_capability(session
, "SCRAM-SHA-384"),
965 imap_has_capability(session
, "SCRAM-SHA-512"),
966 imap_has_capability(session
, "PLAIN"),
968 imap_has_capability(session
, "XOAUTH2"),
970 imap_has_capability(session
, "LOGIN"),
971 imap_has_capability(session
, "GSSAPI"));
972 if (imap_has_capability(session
, "CRAM-MD5"))
973 ok
= imap_cmd_login(session
, user
, pass
, "CRAM-MD5");
974 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "DIGEST-MD5"))
975 ok
= imap_cmd_login(session
, user
, pass
, "DIGEST-MD5");
976 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-512"))
977 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-512");
978 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-384"))
979 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-384");
980 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-256"))
981 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-256");
982 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-224"))
983 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-224");
984 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "SCRAM-SHA-1"))
985 ok
= imap_cmd_login(session
, user
, pass
, "SCRAM-SHA-1");
986 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "PLAIN"))
987 ok
= imap_cmd_login(session
, user
, pass
, "PLAIN");
988 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "LOGIN"))
989 ok
= imap_cmd_login(session
, user
, pass
, "LOGIN");
990 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "GSSAPI"))
991 ok
= imap_cmd_login(session
, user
, pass
, "GSSAPI");
992 if (ok
== MAILIMAP_ERROR_LOGIN
) /* we always try plaintext login before giving up */
993 ok
= imap_cmd_login(session
, user
, pass
, "plaintext");
995 if (ok
== MAILIMAP_ERROR_LOGIN
&& imap_has_capability(session
, "XOAUTH2"))
996 ok
= imap_cmd_login(session
, user
, pass
, "XOAUTH2");
1000 if (ok
== MAILIMAP_NO_ERROR
)
1001 session
->authenticated
= TRUE
;
1003 if (type
== IMAP_AUTH_CRAM_MD5
) {
1004 ext_info
= _("\n\nCRAM-MD5 logins only work if libetpan has been "
1005 "compiled with SASL support and the "
1006 "CRAM-MD5 SASL plugin is installed.");
1009 if (type
== IMAP_AUTH_DIGEST_MD5
) {
1010 ext_info
= _("\n\nDIGEST-MD5 logins only work if libetpan has been "
1011 "compiled with SASL support and the "
1012 "DIGEST-MD5 SASL plugin is installed.");
1015 if (type
== IMAP_AUTH_SCRAM_SHA1
) {
1016 ext_info
= _("\n\nSCRAM-SHA-1 logins only work if libetpan has been "
1017 "compiled with SASL support and the "
1018 "SCRAM SASL plugin is installed.");
1021 if (type
== IMAP_AUTH_SCRAM_SHA224
) {
1022 ext_info
= _("\n\nSCRAM-SHA-224 logins only work if libetpan has been "
1023 "compiled with SASL support and the "
1024 "SCRAM SASL plugin is installed.");
1027 if (type
== IMAP_AUTH_SCRAM_SHA256
) {
1028 ext_info
= _("\n\nSCRAM-SHA-256 logins only work if libetpan has been "
1029 "compiled with SASL support and the "
1030 "SCRAM SASL plugin is installed.");
1033 if (type
== IMAP_AUTH_SCRAM_SHA384
) {
1034 ext_info
= _("\n\nSCRAM-SHA-384 logins only work if libetpan has been "
1035 "compiled with SASL support and the "
1036 "SCRAM SASL plugin is installed.");
1039 if (type
== IMAP_AUTH_SCRAM_SHA512
) {
1040 ext_info
= _("\n\nSCRAM-SHA-512 logins only work if libetpan has been "
1041 "compiled with SASL support and the "
1042 "SCRAM SASL plugin is installed.");
1045 if (type
== IMAP_AUTH_PLAIN
) {
1046 ext_info
= _("\n\nPLAIN logins only work if libetpan has been "
1047 "compiled with SASL support and the "
1048 "PLAIN SASL plugin is installed.");
1051 if (type
== IMAP_AUTH_LOGIN
) {
1052 ext_info
= _("\n\nLOGIN logins only work if libetpan has been "
1053 "compiled with SASL support and the "
1054 "LOGIN SASL plugin is installed.");
1057 if (type
== IMAP_AUTH_OAUTH2
) {
1058 ext_info
= _("\n\nOAuth2 error. Check and correct your OAuth2 "
1059 "account preferences.");
1062 if (time(NULL
) - last_login_err
> 10) {
1063 if (!prefs_common
.no_recv_err_panel
) {
1064 alertpanel_error_log(_("Connection to %s failed: "
1065 "login refused.%s"),
1068 log_error(LOG_PROTOCOL
, _("Connection to %s failed: "
1069 "login refused.%s\n"),
1073 last_login_err
= time(NULL
);
1079 static IMAPSession
*imap_reconnect_if_possible(Folder
*folder
, IMAPSession
*session
)
1081 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
1082 /* Check if this is the first try to establish a
1083 connection, if yes we don't try to reconnect */
1084 debug_print("reconnecting\n");
1085 if (rfolder
->session
== NULL
) {
1086 log_warning(LOG_PROTOCOL
, _("Connecting to %s failed"),
1087 folder
->account
->recv_server
);
1088 SESSION(session
)->sock
= NULL
;
1089 imap_safe_destroy(session
);
1092 rfolder
->session
= NULL
;
1093 log_warning(LOG_PROTOCOL
, _("IMAP connection to %s has been"
1094 " disconnected. Reconnecting...\n"),
1095 folder
->account
->recv_server
);
1096 statusbar_print_all(_("IMAP connection to %s has been"
1097 " disconnected. Reconnecting...\n"),
1098 folder
->account
->recv_server
);
1099 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1100 SESSION(session
)->sock
= NULL
;
1101 imap_safe_destroy(session
);
1102 /* Clear folders session to make imap_session_get create
1103 a new session, because of rfolder->session == NULL
1104 it will not try to reconnect again and so avoid an
1106 debug_print("getting session...\n");
1107 session
= imap_session_get(folder
);
1108 rfolder
->session
= SESSION(session
);
1109 statusbar_pop_all();
1114 static IMAPSession
*imap_session_get(Folder
*folder
)
1116 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
1117 IMAPSession
*session
= NULL
;
1118 gint r
= MAILIMAP_NO_ERROR
;
1120 g_return_val_if_fail(folder
!= NULL
, NULL
);
1121 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, NULL
);
1122 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
1124 if (prefs_common
.work_offline
&&
1125 !inc_offline_should_override(FALSE
,
1126 _("Claws Mail needs network access in order "
1127 "to access the IMAP server."))) {
1131 /* check for deferred destroy */
1132 if (rfolder
->session
!= NULL
) {
1133 session
= IMAP_SESSION(rfolder
->session
);
1134 if (!session
->busy
&& session
->do_destroy
) {
1135 rfolder
->session
= NULL
;
1136 imap_safe_destroy(session
);
1141 /* Make sure we have a session */
1142 if (rfolder
->session
!= NULL
&& rfolder
->session
->state
!= SESSION_DISCONNECTED
) {
1143 session
= IMAP_SESSION(rfolder
->session
);
1144 } else if (rfolder
->session
!= NULL
&& rfolder
->session
->state
== SESSION_DISCONNECTED
) {
1145 imap_safe_destroy(IMAP_SESSION(rfolder
->session
));
1146 rfolder
->session
= NULL
;
1148 } else if (rfolder
->connecting
) {
1149 debug_print("already connecting\n");
1153 imap_reset_uid_lists(folder
);
1154 if (time(NULL
) - rfolder
->last_failure
<= 2)
1156 rfolder
->connecting
= TRUE
;
1157 session
= imap_session_new(folder
, folder
->account
);
1159 if(session
== NULL
) {
1160 rfolder
->last_failure
= time(NULL
);
1161 rfolder
->connecting
= FALSE
;
1165 /* Make sure session is authenticated */
1166 if (!IMAP_SESSION(session
)->authenticated
)
1167 r
= imap_session_authenticate(IMAP_SESSION(session
), folder
->account
);
1169 if (r
!= MAILIMAP_NO_ERROR
|| (!is_fatal(r
) && !IMAP_SESSION(session
)->authenticated
)) {
1170 rfolder
->session
= NULL
;
1172 imap_threaded_disconnect(session
->folder
);
1174 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1175 SESSION(session
)->sock
= NULL
;
1176 imap_safe_destroy(session
);
1177 rfolder
->last_failure
= time(NULL
);
1178 rfolder
->connecting
= FALSE
;
1182 /* I think the point of this code is to avoid sending a
1183 * keepalive if we've used the session recently and therefore
1184 * think it's still alive. Unfortunately, most of the code
1185 * does not yet check for errors on the socket, and so if the
1186 * connection drops we don't notice until the timeout expires.
1187 * A better solution than sending a NOOP every time would be
1188 * for every command to be prepared to retry until it is
1189 * successfully sent. -- mbp */
1190 if ((time(NULL
) - SESSION(session
)->last_access_time
> SESSION_TIMEOUT_INTERVAL
) || session
->cancelled
) {
1191 /* verify that the session is still alive */
1192 r
= imap_cmd_noop(session
);
1194 if (r
!= MAILIMAP_NO_ERROR
) {
1195 debug_print("disconnected!\n");
1197 session
= imap_reconnect_if_possible(folder
, session
);
1199 rfolder
->session
= NULL
;
1200 rfolder
->connecting
= FALSE
;
1201 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1202 SESSION(session
)->sock
= NULL
;
1203 imap_safe_destroy(session
);
1204 session
= imap_session_get(folder
);
1208 session
->cancelled
= FALSE
;
1211 rfolder
->session
= SESSION(session
);
1212 rfolder
->connecting
= FALSE
;
1214 return IMAP_SESSION(session
);
1217 static IMAPSession
*imap_session_new(Folder
* folder
,
1218 const PrefsAccount
*account
)
1220 IMAPSession
*session
;
1221 ProxyInfo
*proxy_info
= NULL
;
1224 int authenticated
= FALSE
;
1228 /* FIXME: IMAP over SSL only... */
1231 port
= account
->set_imapport
? account
->imapport
1232 : account
->ssl_imap
== SSL_TUNNEL
? IMAPS_PORT
: IMAP4_PORT
;
1233 ssl_type
= account
->ssl_imap
;
1235 if (account
->ssl_imap
!= SSL_NONE
) {
1236 if (alertpanel_full(_("Insecure connection"),
1237 _("This connection is configured to be secured "
1238 "using TLS, but TLS is not available "
1239 "in this build of Claws Mail. \n\n"
1240 "Do you want to continue connecting to this "
1241 "server? The communication would not be "
1243 NULL
, _("_Cancel"), NULL
, _("Con_tinue connecting"), NULL
, NULL
,
1244 ALERTFOCUS_FIRST
, FALSE
, NULL
, ALERT_WARNING
) != G_ALERTALTERNATE
)
1247 port
= account
->set_imapport
? account
->imapport
1252 IMAP_FOLDER(folder
)->max_set_size
= account
->imap_batch_size
;
1253 buf
= g_strdup_printf(_("Account '%s': Connecting to IMAP server: %s:%d..."),
1254 folder
->account
->account_name
, folder
->account
->recv_server
,
1256 statusbar_print_all("%s", buf
);
1257 log_message(LOG_PROTOCOL
, "%s\n", buf
);
1260 if (account
->use_proxy
) {
1261 if (account
->use_default_proxy
) {
1262 proxy_info
= (ProxyInfo
*)&(prefs_common
.proxy_info
);
1263 if (proxy_info
->use_proxy_auth
)
1264 proxy_info
->proxy_pass
= passwd_store_get(PWS_CORE
, PWS_CORE_PROXY
,
1265 PWS_CORE_PROXY_PASS
);
1267 proxy_info
= (ProxyInfo
*)&(account
->proxy_info
);
1268 if (proxy_info
->use_proxy_auth
)
1269 proxy_info
->proxy_pass
= passwd_store_get_account(account
->account_id
,
1270 PWS_ACCOUNT_PROXY_PASS
);
1275 if (account
->set_tunnelcmd
) {
1276 r
= imap_threaded_connect_cmd(folder
,
1278 account
->recv_server
,
1286 if (ssl_type
== SSL_TUNNEL
) {
1287 r
= imap_threaded_connect_ssl(folder
,
1288 account
->recv_server
,
1295 r
= imap_threaded_connect(folder
,
1296 account
->recv_server
,
1302 statusbar_pop_all();
1303 if (r
== MAILIMAP_NO_ERROR_AUTHENTICATED
) {
1304 authenticated
= TRUE
;
1306 else if (r
== MAILIMAP_NO_ERROR_NON_AUTHENTICATED
) {
1307 authenticated
= FALSE
;
1311 if (r
== MAILIMAP_ERROR_SSL
)
1312 log_error(LOG_PROTOCOL
, _("TLS handshake failed\n"));
1315 imap_handle_error(NULL
, account
->recv_server
, r
);
1317 if(!prefs_common
.no_recv_err_panel
) {
1318 alertpanel_error_log(_("Can't connect to IMAP server: %s:%d"),
1319 account
->recv_server
, port
);
1321 log_error(LOG_PROTOCOL
, _("Can't connect to IMAP server: %s:%d\n"),
1322 account
->recv_server
, port
);
1328 session
= g_new0(IMAPSession
, 1);
1329 session_init(SESSION(session
), account
, FALSE
);
1330 SESSION(session
)->type
= SESSION_IMAP
;
1331 SESSION(session
)->server
= g_strdup(account
->recv_server
);
1332 SESSION(session
)->port
= port
;
1333 SESSION(session
)->sock
= NULL
;
1334 SESSION(session
)->proxy_info
= proxy_info
;
1335 SESSION(session
)->destroy
= imap_session_destroy
;
1337 session
->capability
= NULL
;
1338 session
->authenticated
= authenticated
;
1339 session
->mbox
= NULL
;
1340 session
->exists
= 0;
1341 session
->recent
= 0;
1342 session
->expunge
= 0;
1343 session
->cmd_count
= 0;
1344 session
->folder
= folder
;
1345 IMAP_FOLDER(session
->folder
)->last_seen_separator
= 0;
1348 if (account
->ssl_imap
== SSL_STARTTLS
) {
1351 ok
= imap_cmd_starttls(session
);
1352 if (ok
!= MAILIMAP_NO_ERROR
) {
1353 log_warning(LOG_PROTOCOL
, _("Can't start STARTTLS session.\n"));
1354 if (!is_fatal(ok
)) {
1355 SESSION(session
)->sock
= NULL
;
1356 imap_safe_destroy(session
);
1361 imap_free_capabilities(session
);
1362 session
->authenticated
= FALSE
;
1363 session
->uidplus
= FALSE
;
1364 session
->cmd_count
= 1;
1366 SESSION(session
)->use_tls_sni
= account
->use_tls_sni
;
1369 log_message(LOG_PROTOCOL
, "IMAP connection is %s-authenticated\n",
1370 (session
->authenticated
) ? "pre" : "un");
1372 session_register_ping(SESSION(session
), imap_ping
);
1377 static gint
imap_session_authenticate(IMAPSession
*session
,
1378 PrefsAccount
*account
)
1380 gchar
*pass
, *acc_pass
= NULL
;
1381 gboolean failed
= FALSE
;
1382 gint ok
= MAILIMAP_NO_ERROR
;
1383 g_return_val_if_fail(account
->userid
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
1385 if(account
->imap_auth_type
== IMAP_AUTH_OAUTH2
)
1386 oauth2_check_passwds (account
);
1388 if (!password_get(account
->userid
, account
->recv_server
, "imap",
1389 SESSION(session
)->port
, &acc_pass
)) {
1390 acc_pass
= passwd_store_get_account(account
->account_id
,
1396 if (!pass
&& account
->imap_auth_type
!= IMAP_AUTH_ANON
&& account
->imap_auth_type
!= IMAP_AUTH_GSSAPI
) {
1398 tmp_pass
= input_dialog_query_password_keep(account
->recv_server
,
1400 &(account
->session_passwd
));
1402 return MAILIMAP_NO_ERROR
;
1404 Xstrdup_a(pass
, tmp_pass
, {g_free(tmp_pass
); return MAILIMAP_NO_ERROR
;});
1406 } else if (account
->imap_auth_type
== IMAP_AUTH_ANON
|| account
->imap_auth_type
== IMAP_AUTH_GSSAPI
) {
1409 if ((ok
= imap_auth(session
, account
->userid
, pass
, account
->imap_auth_type
)) != MAILIMAP_NO_ERROR
) {
1411 if (!failed
&& !is_fatal(ok
)) {
1412 if (acc_pass
!= NULL
) {
1413 memset(acc_pass
, 0, strlen(acc_pass
));
1418 if (account
->session_passwd
!= NULL
) {
1419 g_free(account
->session_passwd
);
1420 account
->session_passwd
= NULL
;
1424 if (prefs_common
.no_recv_err_panel
) {
1425 log_error(LOG_PROTOCOL
, _("Couldn't login to IMAP server %s.\n"), account
->recv_server
);
1426 mainwindow_show_error();
1428 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account
->recv_server
);
1431 if (acc_pass
!= NULL
) {
1432 memset(acc_pass
, 0, strlen(acc_pass
));
1441 memset(acc_pass
, 0, strlen(acc_pass
));
1444 statusbar_pop_all();
1445 session
->authenticated
= TRUE
;
1446 return MAILIMAP_NO_ERROR
;
1449 static void imap_session_destroy(Session
*session
)
1451 if (session
->state
!= SESSION_DISCONNECTED
)
1452 imap_threaded_disconnect(IMAP_SESSION(session
)->folder
);
1454 imap_free_capabilities(IMAP_SESSION(session
));
1455 g_free(IMAP_SESSION(session
)->mbox
);
1458 static gchar
*imap_fetch_msg(Folder
*folder
, FolderItem
*item
, gint uid
)
1460 return imap_fetch_msg_full(folder
, item
, uid
, TRUE
, TRUE
);
1463 static guint
get_file_size_with_crs(const gchar
*filename
)
1469 if (filename
== NULL
)
1472 fp
= claws_fopen(filename
, "rb");
1476 while (claws_fgets(buf
, sizeof (buf
), fp
) != NULL
) {
1478 if (!strstr(buf
, "\r\n") && strstr(buf
, "\n"))
1486 static gchar
*imap_get_cached_filename(FolderItem
*item
, guint msgnum
)
1488 gchar
*path
, *filename
;
1490 cm_return_val_if_fail(item
!= NULL
, NULL
);
1492 path
= folder_item_get_path(item
);
1494 if (!is_dir_exist(path
)) {
1499 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msgnum
), NULL
);
1501 if (is_dir_exist(filename
)) {
1503 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, ".", itos(msgnum
), NULL
);
1510 static void imap_remove_cached_msg(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
)
1514 filename
= imap_get_cached_filename(item
, msginfo
->msgnum
);
1516 cm_return_if_fail(filename
!= NULL
);
1518 if (is_file_exist(filename
)) {
1519 claws_unlink(filename
);
1524 typedef struct _TagsData
{
1527 IMAPFolderItem
*item
;
1530 static void imap_commit_tags(FolderItem
*item
, MsgInfo
*msginfo
, GSList
*tags_set
, GSList
*tags_unset
)
1532 IMAPSession
*session
;
1533 gint ok
, can_create_tags
;
1534 Folder
*folder
= NULL
;
1535 TagsData
*ht_data
= NULL
;
1538 g_return_if_fail(item
!= NULL
);
1539 g_return_if_fail(msginfo
!= NULL
);
1541 folder
= item
->folder
;
1542 debug_print("getting session...\n");
1543 session
= imap_session_get(folder
);
1546 debug_print("can't get session\n");
1550 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
1551 NULL
, NULL
, NULL
, NULL
, &can_create_tags
, FALSE
);
1553 if (ok
!= MAILIMAP_NO_ERROR
) {
1558 if (IMAP_FOLDER_ITEM(item
)->can_create_flags
!= ITEM_CAN_CREATE_FLAGS
)
1561 if (IMAP_FOLDER_ITEM(item
)->batching
) {
1562 /* instead of performing an UID STORE command for each message change,
1563 * as a lot of them can change "together", we just fill in hashtables
1564 * and defer the treatment so that we're able to send only one
1567 debug_print("IMAP batch mode on, deferring tags change\n");
1568 for (cur
= tags_set
; cur
; cur
= cur
->next
) {
1569 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1571 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->tags_set_table
,
1572 GINT_TO_POINTER(cur_tag
));
1573 if (ht_data
== NULL
) {
1574 ht_data
= g_new0(TagsData
, 1);
1575 ht_data
->str
= g_strdup(tags_get_tag(cur_tag
));
1576 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
1577 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->tags_set_table
,
1578 GINT_TO_POINTER(cur_tag
), ht_data
);
1580 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
1583 for (cur
= tags_unset
; cur
; cur
= cur
->next
) {
1584 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1586 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->tags_unset_table
,
1587 GINT_TO_POINTER(cur_tag
));
1588 if (ht_data
== NULL
) {
1589 ht_data
= g_new0(TagsData
, 1);
1590 ht_data
->str
= g_strdup(tags_get_tag(cur_tag
));
1591 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
1592 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->tags_unset_table
,
1593 GINT_TO_POINTER(cur_tag
), ht_data
);
1595 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
1599 GSList
*list_set
= NULL
;
1600 GSList
*list_unset
= NULL
;
1603 numlist
.data
= GINT_TO_POINTER(msginfo
->msgnum
);
1604 numlist
.next
= NULL
;
1606 debug_print("IMAP changing tags NOW\n");
1607 for (cur
= tags_set
; cur
; cur
= cur
->next
) {
1608 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1609 const gchar
*str
= tags_get_tag(cur_tag
);
1610 if (IS_NOT_RESERVED_TAG(str
))
1611 list_set
= g_slist_prepend(list_set
, g_strdup(str
));
1614 ok
= imap_set_message_flags(session
,
1615 IMAP_FOLDER_ITEM(item
), &numlist
, 0, list_set
, TRUE
);
1616 slist_free_strings_full(list_set
);
1617 if (ok
!= MAILIMAP_NO_ERROR
) {
1622 for (cur
= tags_unset
; cur
; cur
= cur
->next
) {
1623 gint cur_tag
= GPOINTER_TO_INT(cur
->data
);
1624 const gchar
*str
= tags_get_tag(cur_tag
);
1625 if (IS_NOT_RESERVED_TAG(str
))
1626 list_unset
= g_slist_prepend(list_unset
, g_strdup(str
));
1629 ok
= imap_set_message_flags(session
,
1630 IMAP_FOLDER_ITEM(item
), &numlist
, 0, list_unset
, FALSE
);
1631 slist_free_strings_full(list_unset
);
1632 if (ok
!= MAILIMAP_NO_ERROR
) {
1639 static gchar
*imap_fetch_msg_full(Folder
*folder
, FolderItem
*item
, gint uid
,
1640 gboolean headers
, gboolean body
)
1642 gchar
*path
, *filename
;
1643 IMAPSession
*session
;
1646 g_return_val_if_fail(folder
!= NULL
, NULL
);
1647 g_return_val_if_fail(item
!= NULL
, NULL
);
1652 path
= folder_item_get_path(item
);
1653 if (!is_dir_exist(path
)) {
1654 if(is_file_exist(path
))
1656 make_dir_hier(path
);
1660 filename
= imap_get_cached_filename(item
, uid
);
1661 debug_print("trying to fetch cached %s\n", filename
);
1663 if (is_file_exist(filename
)) {
1664 /* see whether the local file represents the whole message
1665 * or not. As the IMAP server reports size with \r chars,
1666 * we have to update the local file (UNIX \n only) size */
1667 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1668 guint have_size
= -1;
1671 debug_print("message %d has been already %scached.\n", uid
,
1672 MSG_IS_FULLY_CACHED(cached
->flags
) ? "fully ":"");
1674 if (!cached
|| !MSG_IS_FULLY_CACHED(cached
->flags
)) {
1675 have_size
= get_file_size_with_crs(filename
);
1676 if (cached
&& (cached
->size
<= have_size
|| !body
)) {
1677 ok
= file_strip_crs(filename
);
1678 if (ok
== 0 && cached
&& cached
->size
<= have_size
) {
1679 /* we have it all and stripped */
1680 debug_print("...fully cached in fact (%u/%"G_GOFFSET_FORMAT
"); setting flag.\n",
1681 have_size
, cached
->size
);
1682 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1684 procmsg_msginfo_free(&cached
);
1686 } else if (!cached
&& time(NULL
) - get_file_mtime(filename
) < 60) {
1687 debug_print("message not cached and file recent, considering file complete\n");
1688 ok
= file_strip_crs(filename
);
1692 procmsg_msginfo_free(&cached
);
1695 if (cached
&& MSG_IS_FULLY_CACHED(cached
->flags
)) {
1696 procmsg_msginfo_free(&cached
);
1700 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1702 procmsg_msginfo_unset_flags(cached
, MSG_FULLY_CACHED
, 0);
1703 procmsg_msginfo_free(&cached
);
1707 debug_print("getting session...\n");
1708 session
= imap_session_get(folder
);
1714 session_set_access_time(SESSION(session
));
1715 lock_session(session
); /* unlocked later in the function */
1717 debug_print("IMAP fetching messages\n");
1718 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
1719 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
1720 if (ok
!= MAILIMAP_NO_ERROR
) {
1721 g_warning("can't select mailbox %s", item
->path
);
1726 session_set_access_time(SESSION(session
));
1728 debug_print("getting message %d...\n", uid
);
1729 ok
= imap_cmd_fetch(session
, (guint32
)uid
, filename
, headers
, body
);
1731 if (ok
!= MAILIMAP_NO_ERROR
) {
1732 g_warning("can't fetch message %d", uid
);
1737 session_set_access_time(SESSION(session
));
1738 unlock_session(session
);
1740 ok
= file_strip_crs(filename
);
1742 if (ok
== 0 && headers
&& body
) {
1743 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1745 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1746 procmsg_msginfo_free(&cached
);
1748 } else if (ok
== -1) {
1749 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1751 procmsg_msginfo_unset_flags(cached
, MSG_FULLY_CACHED
, 0);
1752 procmsg_msginfo_free(&cached
);
1758 static gboolean
imap_is_msg_fully_cached(Folder
*folder
, FolderItem
*item
, gint uid
)
1762 MsgInfo
*cached
= msgcache_get_msg(item
->cache
,uid
);
1767 if (MSG_IS_FULLY_CACHED(cached
->flags
)) {
1768 procmsg_msginfo_free(&cached
);
1772 filename
= imap_get_cached_filename(item
, uid
);
1774 if (is_file_exist(filename
)) {
1775 if (cached
&& cached
->total_size
== cached
->size
) {
1778 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1781 size
= get_file_size_with_crs(filename
);
1784 debug_print("msg %d cached, has size %d, full should be %"G_GOFFSET_FORMAT
".\n", uid
, size
, cached
->size
);
1785 if (cached
&& size
>= cached
->size
) {
1786 cached
->total_size
= cached
->size
;
1787 procmsg_msginfo_set_flags(cached
, MSG_FULLY_CACHED
, 0);
1788 procmsg_msginfo_free(&cached
);
1792 procmsg_msginfo_free(&cached
);
1796 void imap_cache_msg(FolderItem
*item
, gint msgnum
)
1798 Folder
*folder
= NULL
;
1802 folder
= item
->folder
;
1804 if (!imap_is_msg_fully_cached(folder
, item
, msgnum
)) {
1805 gchar
*tmp
= imap_fetch_msg_full(folder
, item
, msgnum
, TRUE
, TRUE
);
1806 debug_print("fetched %s\n", tmp
);
1811 static gint
imap_add_msg(Folder
*folder
, FolderItem
*dest
,
1812 const gchar
*file
, MsgFlags
*flags
)
1816 MsgFileInfo fileinfo
;
1818 g_return_val_if_fail(file
!= NULL
, -1);
1820 fileinfo
.msginfo
= NULL
;
1821 fileinfo
.file
= (gchar
*)file
;
1822 fileinfo
.flags
= flags
;
1823 file_list
.data
= &fileinfo
;
1824 file_list
.next
= NULL
;
1826 ret
= imap_add_msgs(folder
, dest
, &file_list
, NULL
);
1830 static gint
imap_add_msgs(Folder
*folder
, FolderItem
*dest
, GSList
*file_list
,
1831 GHashTable
*relation
)
1834 IMAPSession
*session
;
1835 guint32 last_uid
= 0;
1837 MsgFileInfo
*fileinfo
;
1838 gint ok
= MAILIMAP_NO_ERROR
;
1839 gint curnum
= 0, total
= 0;
1840 gboolean missing_uids
= FALSE
;
1842 g_return_val_if_fail(folder
!= NULL
, -1);
1843 g_return_val_if_fail(dest
!= NULL
, -1);
1844 g_return_val_if_fail(file_list
!= NULL
, -1);
1846 debug_print("getting session...\n");
1847 session
= imap_session_get(folder
);
1851 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
1856 statusbar_print_all(_("Adding messages..."));
1857 total
= g_slist_length(file_list
);
1858 for (cur
= file_list
; cur
!= NULL
; cur
= cur
->next
) {
1859 IMAPFlags iflags
= 0;
1860 guint32 new_uid
= 0;
1861 gchar
*real_file
= NULL
;
1862 fileinfo
= (MsgFileInfo
*)cur
->data
;
1864 statusbar_progress_all(curnum
, total
, total
< 10 ? 1:10);
1867 if (fileinfo
->flags
) {
1868 if (MSG_IS_MARKED(*fileinfo
->flags
))
1869 iflags
|= IMAP_FLAG_FLAGGED
;
1870 if (MSG_IS_REPLIED(*fileinfo
->flags
))
1871 iflags
|= IMAP_FLAG_ANSWERED
;
1872 if (MSG_IS_FORWARDED(*fileinfo
->flags
))
1873 iflags
|= IMAP_FLAG_FORWARDED
;
1874 if (MSG_IS_SPAM(*fileinfo
->flags
))
1875 iflags
|= IMAP_FLAG_SPAM
;
1877 iflags
|= IMAP_FLAG_HAM
;
1878 if (!MSG_IS_UNREAD(*fileinfo
->flags
))
1879 iflags
|= IMAP_FLAG_SEEN
;
1883 if (real_file
== NULL
)
1884 real_file
= g_strdup(fileinfo
->file
);
1886 if (folder_has_parent_of_type(dest
, F_QUEUE
) ||
1887 folder_has_parent_of_type(dest
, F_OUTBOX
) ||
1888 folder_has_parent_of_type(dest
, F_DRAFT
) ||
1889 folder_has_parent_of_type(dest
, F_TRASH
))
1890 iflags
|= IMAP_FLAG_SEEN
;
1892 ok
= imap_cmd_append(session
, IMAP_FOLDER_ITEM(dest
), destdir
, real_file
, iflags
,
1895 if (ok
!= MAILIMAP_NO_ERROR
) {
1896 g_warning("can't append message %s", real_file
);
1899 statusbar_progress_all(0,0,0);
1900 statusbar_pop_all();
1903 debug_print("appended new message as %d\n", new_uid
);
1904 /* put the local file in the imapcache, so that we don't
1905 * have to fetch it back later. */
1908 missing_uids
= TRUE
;
1909 debug_print("Missing UID (0)\n");
1912 gchar
*cache_path
= folder_item_get_path(dest
);
1913 if (!is_dir_exist(cache_path
))
1914 make_dir_hier(cache_path
);
1915 if (is_dir_exist(cache_path
)) {
1916 gchar
*cache_file
= g_strconcat(
1917 cache_path
, G_DIR_SEPARATOR_S
,
1918 itos(new_uid
), NULL
);
1919 copy_file(real_file
, cache_file
, TRUE
);
1920 debug_print("got UID %d, copied to cache: %s\n", new_uid
, cache_file
);
1927 if (relation
!= NULL
)
1928 g_hash_table_insert(relation
, fileinfo
->msginfo
!= NULL
?
1929 (gpointer
) fileinfo
->msginfo
: (gpointer
) fileinfo
,
1930 GINT_TO_POINTER(new_uid
));
1931 if (last_uid
< new_uid
) {
1938 statusbar_progress_all(0,0,0);
1939 statusbar_pop_all();
1944 imap_scan_required(folder
, dest
);
1946 session
= imap_session_get(folder
);
1952 ok
= imap_select(session
, IMAP_FOLDER(folder
), dest
,
1953 &a
, NULL
, NULL
, NULL
, NULL
, FALSE
);
1958 static GSList
*flatten_mailimap_set(struct mailimap_set
* set
)
1960 GSList
*result
= NULL
;
1962 guint32 start
, end
, t
;
1965 if (!set
|| !set
->set_list
)
1968 for (list
= clist_begin(set
->set_list
); list
; list
= clist_next(list
)) {
1969 struct mailimap_set_item
*item
= (struct mailimap_set_item
*)clist_content(list
);
1970 start
= item
->set_first
;
1971 end
= item
->set_last
;
1973 for (t
= start
; t
<= end
; t
++) {
1974 result
= g_slist_prepend(result
, GINT_TO_POINTER(t
));
1978 result
= g_slist_reverse(result
);
1979 if (debug_get_mode()) {
1980 debug_print("flat imap set: ");
1981 for (cur
= result
; cur
; cur
= cur
->next
) {
1982 debug_print("%d ", GPOINTER_TO_INT(cur
->data
));
1989 static gint
imap_do_copy_msgs(Folder
*folder
, FolderItem
*dest
,
1990 MsgInfoList
*msglist
, GHashTable
*relation
,
1991 gboolean same_dest_ok
)
1995 GSList
*seq_list
, *cur
;
1997 IMAPSession
*session
;
1998 gint ok
= MAILIMAP_NO_ERROR
;
1999 GHashTable
*uid_hash
;
2002 g_return_val_if_fail(folder
!= NULL
, -1);
2003 g_return_val_if_fail(dest
!= NULL
, -1);
2004 g_return_val_if_fail(msglist
!= NULL
, -1);
2006 debug_print("getting session...\n");
2007 session
= imap_session_get(folder
);
2013 msginfo
= (MsgInfo
*)msglist
->data
;
2014 src
= msginfo
->folder
;
2015 if (!same_dest_ok
&& src
== dest
) {
2016 g_warning("the src folder is identical to the dest");
2020 if (src
->folder
!= dest
->folder
) {
2021 GSList
*infolist
= NULL
, *cur
;
2023 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2024 msginfo
= (MsgInfo
*)cur
->data
;
2025 MsgFileInfo
*fileinfo
= g_new0(MsgFileInfo
, 1);
2026 fileinfo
->file
= procmsg_get_message_file(msginfo
);
2027 fileinfo
->flags
= &(msginfo
->flags
);
2028 infolist
= g_slist_prepend(infolist
, fileinfo
);
2030 infolist
= g_slist_reverse(infolist
);
2031 res
= folder_item_add_msgs(dest
, infolist
, FALSE
);
2032 for (cur
= infolist
; cur
; cur
= cur
->next
) {
2033 MsgFileInfo
*info
= (MsgFileInfo
*)cur
->data
;
2037 g_slist_free(infolist
);
2041 lock_session(session
); /* unlocked later in the function */
2043 ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
2044 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
2045 if (ok
!= MAILIMAP_NO_ERROR
) {
2049 unlock_session(session
);
2051 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
2058 seq_list
= imap_get_lep_set_from_msglist(IMAP_FOLDER(folder
), msglist
);
2059 uid_hash
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2061 statusbar_print_all(_("Copying messages..."));
2062 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
2063 struct mailimap_set
* seq_set
;
2064 struct mailimap_set
* source
= NULL
;
2065 struct mailimap_set
* dest
= NULL
;
2066 seq_set
= cur
->data
;
2068 debug_print("Copying messages from %s to %s ...\n",
2069 src
->path
, destdir
);
2071 lock_session(session
); /* unlocked later in the function */
2072 ok
= imap_cmd_copy(session
, seq_set
, destdir
,
2079 if (ok
== MAILIMAP_NO_ERROR
) {
2080 unlock_session(session
);
2081 if (relation
&& source
&& dest
) {
2082 GSList
*s_list
= flatten_mailimap_set(source
);
2083 GSList
*d_list
= flatten_mailimap_set(dest
);
2084 GSList
*s_cur
, *d_cur
;
2085 if (g_slist_length(s_list
) == g_slist_length(d_list
)) {
2087 for (s_cur
= s_list
, d_cur
= d_list
;
2089 s_cur
= s_cur
->next
, d_cur
= d_cur
->next
) {
2090 g_hash_table_insert(uid_hash
, s_cur
->data
, d_cur
->data
);
2094 debug_print("hhhmm, source list length != dest list length.\n");
2096 g_slist_free(s_list
);
2097 g_slist_free(d_list
);
2103 mailimap_set_free(source
);
2105 mailimap_set_free(dest
);
2107 if (ok
!= MAILIMAP_NO_ERROR
) {
2108 g_hash_table_destroy(uid_hash
);
2109 imap_lep_set_free(seq_list
);
2110 statusbar_pop_all();
2115 for (cur
= msglist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
2116 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
2119 hashval
= g_hash_table_lookup(uid_hash
, GINT_TO_POINTER(msginfo
->msgnum
));
2121 if (hashval
!= NULL
) {
2122 gint num
= GPOINTER_TO_INT(hashval
);
2123 g_hash_table_insert(relation
, msginfo
,
2124 GINT_TO_POINTER(num
));
2127 debug_print("copied message %d as %d\n", msginfo
->msgnum
, num
);
2128 /* put the local file in the imapcache, so that we don't
2129 * have to fetch it back later. */
2131 gchar
*cache_path
= folder_item_get_path(msginfo
->folder
);
2132 gchar
*real_file
= g_strconcat(
2133 cache_path
, G_DIR_SEPARATOR_S
,
2134 itos(msginfo
->msgnum
), NULL
);
2135 gchar
*cache_file
= NULL
;
2137 cache_path
= folder_item_get_path(dest
);
2138 cache_file
= g_strconcat(
2139 cache_path
, G_DIR_SEPARATOR_S
,
2141 if (!is_dir_exist(cache_path
))
2142 make_dir_hier(cache_path
);
2143 if (is_file_exist(real_file
) && is_dir_exist(cache_path
)) {
2144 if (copy_file(real_file
, cache_file
, TRUE
) < 0)
2145 debug_print("couldn't cache to %s: %s\n", cache_file
,
2148 debug_print("copied to cache: %s\n", cache_file
);
2155 g_hash_table_insert(relation
, msginfo
,
2156 GINT_TO_POINTER(0));
2158 statusbar_pop_all();
2160 g_hash_table_destroy(uid_hash
);
2161 imap_lep_set_free(seq_list
);
2165 IMAP_FOLDER_ITEM(dest
)->lastuid
= 0;
2166 IMAP_FOLDER_ITEM(dest
)->uid_next
= 0;
2167 g_slist_free(IMAP_FOLDER_ITEM(dest
)->uid_list
);
2168 IMAP_FOLDER_ITEM(dest
)->uid_list
= NULL
;
2170 imap_scan_required(folder
, dest
);
2171 if (ok
== MAILIMAP_NO_ERROR
)
2177 static gint
imap_copy_msg(Folder
*folder
, FolderItem
*dest
, MsgInfo
*msginfo
)
2181 g_return_val_if_fail(msginfo
!= NULL
, -1);
2183 msglist
.data
= msginfo
;
2184 msglist
.next
= NULL
;
2186 return imap_copy_msgs(folder
, dest
, &msglist
, NULL
);
2189 static gint
imap_copy_msgs(Folder
*folder
, FolderItem
*dest
,
2190 MsgInfoList
*msglist
, GHashTable
*relation
)
2195 g_return_val_if_fail(folder
!= NULL
, -1);
2196 g_return_val_if_fail(dest
!= NULL
, -1);
2197 g_return_val_if_fail(msglist
!= NULL
, -1);
2199 msginfo
= (MsgInfo
*)msglist
->data
;
2200 g_return_val_if_fail(msginfo
->folder
!= NULL
, -1);
2202 ret
= imap_do_copy_msgs(folder
, dest
, msglist
, relation
, FALSE
);
2206 static gboolean
imap_matcher_type_is_local(gint matchertype
)
2208 switch (matchertype
) {
2209 case MATCHCRITERIA_FROM
:
2210 case MATCHCRITERIA_TO
:
2211 case MATCHCRITERIA_CC
:
2212 case MATCHCRITERIA_TO_OR_CC
:
2213 case MATCHCRITERIA_SUBJECT
:
2214 case MATCHCRITERIA_REFERENCES
:
2215 case MATCHCRITERIA_MESSAGEID
:
2216 case MATCHCRITERIA_INREPLYTO
:
2217 case MATCHCRITERIA_AGE_GREATER
:
2218 case MATCHCRITERIA_AGE_LOWER
:
2219 case MATCHCRITERIA_FORWARDED
:
2220 case MATCHCRITERIA_SPAM
:
2221 case MATCHCRITERIA_UNREAD
:
2222 case MATCHCRITERIA_NEW
:
2223 case MATCHCRITERIA_MARKED
:
2224 case MATCHCRITERIA_REPLIED
:
2225 case MATCHCRITERIA_DELETED
:
2226 case MATCHCRITERIA_SIZE_GREATER
:
2227 case MATCHCRITERIA_SIZE_SMALLER
:
2228 case MATCHCRITERIA_SIZE_EQUAL
:
2234 static IMAPSearchKey
* search_make_key(MatcherProp
* match
, gboolean
* is_all
)
2236 if (match
->matchtype
== MATCHTYPE_MATCHCASE
|| match
->matchtype
== MATCHTYPE_MATCH
) {
2237 IMAPSearchKey
* result
= NULL
;
2238 gboolean invert
= FALSE
;
2239 gint matchertype
= match
->criteria
;
2245 switch (matchertype
) {
2246 case MATCHCRITERIA_NOT_NEW
: invert
= TRUE
; matchertype
= MATCHCRITERIA_NEW
; break;
2247 case MATCHCRITERIA_NOT_MARKED
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MARKED
; break;
2248 case MATCHCRITERIA_NOT_FORWARDED
: invert
= TRUE
; matchertype
= MATCHCRITERIA_FORWARDED
; break;
2249 case MATCHCRITERIA_NOT_SPAM
: invert
= TRUE
; matchertype
= MATCHCRITERIA_SPAM
; break;
2250 case MATCHCRITERIA_NOT_SUBJECT
: invert
= TRUE
; matchertype
= MATCHCRITERIA_SUBJECT
; break;
2251 case MATCHCRITERIA_NOT_FROM
: invert
= TRUE
; matchertype
= MATCHCRITERIA_FROM
; break;
2252 case MATCHCRITERIA_NOT_TO
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TO
; break;
2253 case MATCHCRITERIA_NOT_CC
: invert
= TRUE
; matchertype
= MATCHCRITERIA_CC
; break;
2254 case MATCHCRITERIA_NOT_REFERENCES
: invert
= TRUE
; matchertype
= MATCHCRITERIA_REFERENCES
; break;
2255 case MATCHCRITERIA_NOT_HEADER
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADER
; break;
2256 case MATCHCRITERIA_NOT_TAG
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TAG
; break;
2257 case MATCHCRITERIA_NOT_HEADERS_PART
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADERS_PART
; break;
2258 case MATCHCRITERIA_NOT_HEADERS_CONT
: invert
= TRUE
; matchertype
= MATCHCRITERIA_HEADERS_CONT
; break;
2259 case MATCHCRITERIA_NOT_MESSAGE
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MESSAGE
; break;
2260 case MATCHCRITERIA_NOT_BODY_PART
: invert
= TRUE
; matchertype
= MATCHCRITERIA_BODY_PART
; break;
2261 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
: invert
= TRUE
; matchertype
= MATCHCRITERIA_TO_OR_CC
; break;
2262 case MATCHCRITERIA_NOT_MESSAGEID
: invert
= TRUE
; matchertype
= MATCHCRITERIA_MESSAGEID
; break;
2263 case MATCHCRITERIA_NOT_INREPLYTO
: invert
= TRUE
; matchertype
= MATCHCRITERIA_INREPLYTO
; break;
2267 * this aborts conversion even for predicates understood by the following code.
2268 * while that might seem wasteful, claws local search for information listed below
2269 * has proven faster than IMAP search plus network roundtrips. once this changes,
2270 * consider removing these exceptions.
2272 if (imap_matcher_type_is_local(matchertype
))
2275 /* the Message-ID header is also cached */
2276 if (matchertype
== MATCHCRITERIA_HEADER
&& g_strcmp0("Message-ID", match
->header
) == 0) {
2280 switch (matchertype
) {
2281 case MATCHCRITERIA_FORWARDED
:
2282 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, RTAG_FORWARDED
, 0);
2285 case MATCHCRITERIA_SPAM
:
2286 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, RTAG_JUNK
, 0);
2289 case MATCHCRITERIA_MESSAGEID
:
2290 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "Message-ID", match
->expr
, 0);
2293 case MATCHCRITERIA_INREPLYTO
:
2294 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "In-Reply-To", match
->expr
, 0);
2297 case MATCHCRITERIA_REFERENCES
:
2298 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, "References", match
->expr
, 0);
2301 case MATCHCRITERIA_TO_OR_CC
:
2302 result
= imap_search_or(
2303 imap_search_new(IMAP_SEARCH_CRITERIA_TO
, NULL
, match
->expr
, 0),
2304 imap_search_new(IMAP_SEARCH_CRITERIA_CC
, NULL
, match
->expr
, 0)
2308 case MATCHCRITERIA_HEADERS_PART
:
2309 case MATCHCRITERIA_HEADERS_CONT
:
2310 result
= imap_search_and(
2311 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_BODY
, NULL
, match
->expr
, 0)),
2312 imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE
, NULL
, match
->expr
, 0)
2316 case MATCHCRITERIA_SIZE_EQUAL
:
2317 result
= imap_search_and(
2318 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER
, NULL
, NULL
, match
->value
)),
2319 imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER
, NULL
, NULL
, match
->value
))
2323 case MATCHCRITERIA_NOT_UNREAD
:
2324 result
= imap_search_new(IMAP_SEARCH_CRITERIA_READ
, NULL
, NULL
, 0);
2327 case MATCHCRITERIA_UNREAD
:
2328 result
= imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD
, NULL
, NULL
, 0);
2331 case MATCHCRITERIA_NEW
:
2332 result
= imap_search_new(IMAP_SEARCH_CRITERIA_NEW
, NULL
, NULL
, 0);
2335 case MATCHCRITERIA_MARKED
:
2336 result
= imap_search_new(IMAP_SEARCH_CRITERIA_MARKED
, NULL
, NULL
, 0);
2339 case MATCHCRITERIA_DELETED
:
2340 result
= imap_search_new(IMAP_SEARCH_CRITERIA_DELETED
, NULL
, NULL
, 0);
2343 case MATCHCRITERIA_REPLIED
:
2344 result
= imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED
, NULL
, NULL
, 0);
2347 case MATCHCRITERIA_TAG
:
2349 gchar
*tmp
= imap_utf8_to_modified_utf7(match
->expr
, TRUE
);
2350 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TAG
, NULL
, tmp
, 0);
2355 case MATCHCRITERIA_SUBJECT
:
2356 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SUBJECT
, NULL
, match
->expr
, 0);
2359 case MATCHCRITERIA_FROM
:
2360 result
= imap_search_new(IMAP_SEARCH_CRITERIA_FROM
, NULL
, match
->expr
, 0);
2363 case MATCHCRITERIA_TO
:
2364 result
= imap_search_new(IMAP_SEARCH_CRITERIA_TO
, NULL
, match
->expr
, 0);
2367 case MATCHCRITERIA_CC
:
2368 result
= imap_search_new(IMAP_SEARCH_CRITERIA_CC
, NULL
, match
->expr
, 0);
2371 case MATCHCRITERIA_AGE_GREATER
:
2372 result
= imap_search_new(IMAP_SEARCH_CRITERIA_AGE_GREATER
, NULL
, NULL
, match
->value
);
2375 case MATCHCRITERIA_AGE_LOWER
:
2376 result
= imap_search_new(IMAP_SEARCH_CRITERIA_AGE_LOWER
, NULL
, NULL
, match
->value
);
2379 case MATCHCRITERIA_BODY_PART
:
2380 result
= imap_search_new(IMAP_SEARCH_CRITERIA_BODY
, NULL
, match
->expr
, 0);
2383 case MATCHCRITERIA_MESSAGE
:
2384 result
= imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE
, NULL
, match
->expr
, 0);
2387 case MATCHCRITERIA_HEADER
:
2388 result
= imap_search_new(IMAP_SEARCH_CRITERIA_HEADER
, match
->header
, match
->expr
, 0);
2391 case MATCHCRITERIA_SIZE_GREATER
:
2392 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER
, NULL
, NULL
, match
->value
);
2395 case MATCHCRITERIA_SIZE_SMALLER
:
2396 result
= imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER
, NULL
, NULL
, match
->value
);
2400 result
= imap_search_new(IMAP_SEARCH_CRITERIA_ALL
, NULL
, NULL
, 0);
2408 result
= imap_search_not(result
);
2409 if (is_all
&& *is_all
) {
2420 static void imap_change_search_charset(IMAPFolder
*folder
)
2422 /* If server supports charset in searches, but the last used one failed,
2423 * changed to the next preferred charset. If none are still available,
2424 * disable charset searches.
2425 * Charsets are tried in the following order:
2426 * UTF-8, locale's charset, UTF-7.
2429 if (folder
->search_charset_supported
) {
2430 if (folder
->search_charset
&& !strcmp(folder
->search_charset
, conv_get_locale_charset_str_no_utf8()))
2431 folder
->search_charset
= "UTF-8";
2432 else if (folder
->search_charset
&& !strcmp(folder
->search_charset
, "UTF-8"))
2433 folder
->search_charset
= "UTF-7";
2435 folder
->search_charset
= NULL
;
2436 folder
->search_charset_supported
= FALSE
;
2441 static MatcherProp
*imap_matcher_prop_set_charset(IMAPFolder
*folder
,
2442 MatcherProp
*utf8_prop
,
2445 /* If the match is going to be done locally, or the criteria is on
2446 * tag (special-cased to modified-UTF-7), or the expression searched
2447 * is ASCII, don't bother converting.
2449 if (imap_matcher_type_is_local(utf8_prop
->criteria
)
2450 || utf8_prop
->criteria
== MATCHCRITERIA_TAG
2451 || utf8_prop
->criteria
== MATCHCRITERIA_NOT_TAG
2452 || utf8_prop
->expr
== NULL
2453 || is_ascii_str(utf8_prop
->expr
))
2454 return matcherprop_new(utf8_prop
->criteria
,
2456 utf8_prop
->matchtype
,
2460 gchar
*conv_expr
= NULL
;
2462 /* If the search is server-side and the server doesn't support
2463 * searching with the charsets we handle, bail out.
2465 if (folder
->search_charset_supported
== FALSE
)
2468 /* Else, convert. */
2469 if (*charset
== NULL
)
2470 *charset
= g_strdup(folder
->search_charset
);
2472 conv_expr
= conv_codeset_strdup(utf8_prop
->expr
, CS_UTF_8
, *charset
);
2474 if (conv_expr
== NULL
)
2475 conv_expr
= g_strdup(utf8_prop
->expr
);
2477 return matcherprop_new(utf8_prop
->criteria
,
2479 utf8_prop
->matchtype
,
2485 static gint
search_msgs (Folder
*folder
,
2486 FolderItem
*container
,
2487 MsgNumberList
**msgs
,
2488 gboolean
*on_server
,
2489 MatcherList
*predicate
,
2490 SearchProgressNotify progress_cb
,
2491 gpointer progress_data
)
2493 IMAPSearchKey
* key
= NULL
;
2496 clist
* uidlist
= NULL
;
2497 gboolean server_filtering_useless
= FALSE
;
2498 IMAPSession
*session
;
2499 gchar
*charset_to_use
= NULL
;
2501 if (on_server
== NULL
|| !*on_server
) {
2502 return folder_item_search_msgs_local(folder
, container
, msgs
, on_server
,
2503 predicate
, progress_cb
, progress_data
);
2506 for (cur
= predicate
->matchers
; cur
!= NULL
; cur
= cur
->next
) {
2507 IMAPSearchKey
* matcherPart
= NULL
;
2508 MatcherProp
* prop
= (MatcherProp
*) cur
->data
;
2510 MatcherProp
*imap_prop
= imap_matcher_prop_set_charset(IMAP_FOLDER(folder
), prop
, &charset_to_use
);
2512 if (imap_prop
== NULL
) {
2513 /* Couldn't convert matcherprop to IMAP - probably not ascii
2514 * and server doesn't support the charsets we do. */
2518 matcherPart
= search_make_key(imap_prop
, &is_all
);
2520 matcherprop_free(imap_prop
);
2523 *on_server
&= matcherPart
!= NULL
&& prop
->matchtype
== MATCHTYPE_MATCHCASE
;
2529 server_filtering_useless
= is_all
;
2530 } else if (predicate
->bool_and
) {
2531 key
= imap_search_and(key
, matcherPart
);
2532 server_filtering_useless
&= is_all
;
2534 key
= imap_search_or(key
, matcherPart
);
2535 server_filtering_useless
|= is_all
;
2540 if (server_filtering_useless
) {
2541 imap_search_free(key
);
2545 if (key
== NULL
&& progress_cb
!= NULL
) {
2550 progress_cb(progress_data
, TRUE
, 0, 0, container
->total_msgs
);
2551 progress_cb(progress_data
, TRUE
, container
->total_msgs
, 0, container
->total_msgs
);
2553 list
= folder_item_get_msg_list(container
);
2554 for (cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
2555 *msgs
= g_slist_prepend(*msgs
, GUINT_TO_POINTER(((MsgInfo
*) cur
->data
)->msgnum
));
2558 procmsg_msg_list_free(list
);
2560 *msgs
= g_slist_reverse(*msgs
);
2565 session
= imap_session_get(folder
);
2569 result
= imap_select(session
, IMAP_FOLDER(folder
), FOLDER_ITEM(container
),
2570 NULL
, NULL
, NULL
, NULL
, NULL
, TRUE
);
2571 if (result
!= MAILIMAP_NO_ERROR
)
2575 progress_cb(progress_data
, TRUE
, 0, 0, container
->total_msgs
);
2576 result
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_KEYED
, key
, charset_to_use
, NULL
, &uidlist
);
2578 progress_cb(progress_data
, TRUE
, container
->total_msgs
, 0, container
->total_msgs
);
2580 if (result
== MAILIMAP_ERROR_PROTOCOL
) {
2581 debug_print("Server side search unavailable, using local search\n");
2582 imap_handle_error(SESSION(session
), NULL
, result
);
2583 result
= folder_item_search_msgs_local(folder
, container
, msgs
, NULL
,
2584 predicate
, progress_cb
, progress_data
);
2586 debug_print("search_msgs - got protocol error, aborting\n");
2587 alertpanel_error_log(_("Search failed due to server error."));
2594 if (result
== MAILIMAP_NO_ERROR
) {
2597 *msgs
= imap_uid_list_from_lep(uidlist
, &result
);
2599 mailimap_search_result_free(uidlist
);
2601 if (charset_to_use
!= NULL
)
2602 g_free(charset_to_use
);
2605 } else if (charset_to_use
!= NULL
) {
2606 /* If search failed and was on an 8-bit string, try the next
2607 * available charset to search if there still are some.
2609 g_free(charset_to_use
);
2611 imap_change_search_charset(IMAP_FOLDER(folder
));
2612 if (IMAP_FOLDER(folder
)->search_charset_supported
)
2613 return search_msgs(folder
, container
, msgs
, on_server
, predicate
,
2614 progress_cb
, progress_data
);
2616 imap_handle_error(SESSION(session
), NULL
, result
);
2620 imap_handle_error(SESSION(session
), NULL
, result
);
2626 static gint
imap_do_remove_msgs(Folder
*folder
, FolderItem
*dest
,
2627 MsgInfoList
*msglist
, GHashTable
*relation
)
2629 gchar
*destdir
, *dir
;
2630 GSList
*numlist
= NULL
, *cur
;
2632 IMAPSession
*session
;
2633 gint ok
= MAILIMAP_NO_ERROR
;
2635 g_return_val_if_fail(folder
!= NULL
, -1);
2636 g_return_val_if_fail(dest
!= NULL
, -1);
2637 g_return_val_if_fail(msglist
!= NULL
, -1);
2639 debug_print("getting session...\n");
2640 session
= imap_session_get(folder
);
2645 lock_session(session
); /* unlocked later in the function */
2647 msginfo
= (MsgInfo
*)msglist
->data
;
2649 ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
2650 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
2651 if (ok
!= MAILIMAP_NO_ERROR
) {
2655 destdir
= imap_get_real_path(session
, IMAP_FOLDER(folder
), dest
->path
, &ok
);
2660 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2661 msginfo
= (MsgInfo
*)cur
->data
;
2662 if (!MSG_IS_DELETED(msginfo
->flags
))
2663 numlist
= g_slist_prepend(numlist
, GINT_TO_POINTER(msginfo
->msgnum
));
2665 numlist
= g_slist_reverse(numlist
);
2667 if (numlist
!= NULL
) {
2668 ok
= imap_set_message_flags
2669 (session
, IMAP_FOLDER_ITEM(msginfo
->folder
), numlist
, IMAP_FLAG_DELETED
, NULL
, TRUE
);
2670 if (ok
!= MAILIMAP_NO_ERROR
) {
2671 log_warning(LOG_PROTOCOL
, _("can't set deleted flags\n"));
2675 } /* else we just need to expunge */
2676 ok
= imap_cmd_expunge(session
);
2677 if (ok
!= MAILIMAP_NO_ERROR
) {
2678 log_warning(LOG_PROTOCOL
, _("can't expunge\n"));
2683 session
->folder_content_changed
= TRUE
;
2684 unlock_session(session
);
2686 dir
= folder_item_get_path(msginfo
->folder
);
2687 if (is_dir_exist(dir
)) {
2688 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2689 msginfo
= (MsgInfo
*)cur
->data
;
2690 remove_numbered_files(dir
, msginfo
->msgnum
, msginfo
->msgnum
);
2695 g_slist_free(numlist
);
2697 imap_scan_required(folder
, dest
);
2704 static gint
imap_remove_msgs(Folder
*folder
, FolderItem
*dest
,
2705 MsgInfoList
*msglist
, GHashTable
*relation
)
2709 g_return_val_if_fail(folder
!= NULL
, -1);
2710 g_return_val_if_fail(dest
!= NULL
, -1);
2711 if (msglist
== NULL
)
2714 msginfo
= (MsgInfo
*)msglist
->data
;
2715 g_return_val_if_fail(msginfo
->folder
!= NULL
, -1);
2717 return imap_do_remove_msgs(folder
, dest
, msglist
, relation
);
2720 static gint
imap_remove_all_msg(Folder
*folder
, FolderItem
*item
)
2722 GSList
*list
= folder_item_get_msg_list(item
);
2723 gint res
= imap_remove_msgs(folder
, item
, list
, NULL
);
2724 procmsg_msg_list_free(list
);
2728 static gboolean
imap_is_msg_changed(Folder
*folder
, FolderItem
*item
,
2731 /* TODO: properly implement this method */
2735 static gint
imap_close(Folder
*folder
, FolderItem
*item
)
2740 static gint
imap_scan_tree_real(Folder
*folder
, gboolean subs_only
)
2742 FolderItem
*item
= NULL
;
2743 IMAPSession
*session
;
2744 gchar
*root_folder
= NULL
;
2746 g_return_val_if_fail(folder
!= NULL
, -1);
2747 g_return_val_if_fail(folder
->account
!= NULL
, -1);
2749 debug_print("getting session...\n");
2750 session
= imap_session_get(folder
);
2752 if (!folder
->node
) {
2753 folder_tree_destroy(folder
);
2754 item
= folder_item_new(folder
, folder
->name
, NULL
);
2755 item
->folder
= folder
;
2756 folder
->node
= item
->node
= g_node_new(item
);
2761 if (folder
->account
->imap_dir
&& *folder
->account
->imap_dir
) {
2763 int r
= MAILIMAP_NO_ERROR
;
2766 Xstrdup_a(root_folder
, folder
->account
->imap_dir
, {return -1;});
2767 extract_quote(root_folder
, '"');
2768 subst_char(root_folder
,
2769 imap_get_path_separator(session
, IMAP_FOLDER(folder
),
2774 strtailchomp(root_folder
, '/');
2775 real_path
= imap_get_real_path
2776 (session
, IMAP_FOLDER(folder
), root_folder
, &r
);
2781 debug_print("IMAP root directory: %s\n", real_path
);
2783 /* check if root directory exist */
2785 r
= imap_threaded_list(session
->folder
, "", real_path
,
2788 if (r
!= MAILIMAP_NO_ERROR
)
2789 imap_handle_error(SESSION(session
), NULL
, r
);
2791 if ((r
!= MAILIMAP_NO_ERROR
) || (clist_count(lep_list
) == 0)) {
2792 if (!folder
->node
) {
2793 item
= folder_item_new(folder
, folder
->name
, NULL
);
2794 item
->folder
= folder
;
2795 folder
->node
= item
->node
= g_node_new(item
);
2799 mailimap_list_result_free(lep_list
);
2805 item
= FOLDER_ITEM(folder
->node
->data
);
2807 if (item
&& !item
->path
&& root_folder
) {
2808 item
->path
= g_strdup(root_folder
);
2811 if (!item
|| ((item
->path
|| root_folder
) &&
2812 g_strcmp0(item
->path
, root_folder
) != 0)) {
2813 folder_tree_destroy(folder
);
2814 item
= folder_item_new(folder
, folder
->name
, root_folder
);
2815 item
->folder
= folder
;
2816 folder
->node
= item
->node
= g_node_new(item
);
2819 imap_scan_tree_recursive(session
, FOLDER_ITEM(folder
->node
->data
), subs_only
);
2820 imap_create_missing_folders(folder
);
2825 static gint
imap_scan_tree(Folder
*folder
)
2827 gboolean subs_only
= FALSE
;
2828 if (folder
->account
) {
2829 debug_print(" scanning only subs %d\n", folder
->account
->imap_subsonly
);
2830 subs_only
= folder
->account
->imap_subsonly
;
2832 return imap_scan_tree_real(folder
, subs_only
);
2835 static gint
imap_scan_tree_recursive(IMAPSession
*session
, FolderItem
*item
, gboolean subs_only
)
2837 /* reset recursion depth counter */
2838 session
->scan_tree_recurs_depth
= 0;
2840 return imap_scan_tree_recursive_dive(session
, item
, subs_only
);
2843 static gint
imap_scan_tree_recursive_dive(IMAPSession
*session
, FolderItem
*item
, gboolean subs_only
)
2846 IMAPFolder
*imapfolder
;
2847 FolderItem
*new_item
;
2848 GSList
*item_list
, *cur
;
2851 gchar
*wildcard_path
;
2855 int r
= MAILIMAP_NO_ERROR
;
2857 g_return_val_if_fail(item
!= NULL
, -1);
2858 g_return_val_if_fail(item
->folder
!= NULL
, -1);
2859 g_return_val_if_fail(item
->no_sub
== FALSE
, -1);
2861 /* recursion depth limiter */
2862 if(session
->scan_tree_recurs_depth
>= prefs_common
.imap_scan_tree_recurs_limit
) {
2863 g_warning("IMAP scan tree recursion limit reached (%d, folder '%s')",
2864 prefs_common
.imap_scan_tree_recurs_limit
, item
->folder
->name
);
2867 /* entering recursion func: increase depth counter */
2868 session
->scan_tree_recurs_depth
++;
2870 folder
= item
->folder
;
2871 imapfolder
= IMAP_FOLDER(folder
);
2873 separator
= imap_get_path_separator(session
, imapfolder
, item
->path
, &r
);
2877 if (folder
->ui_func
)
2878 folder
->ui_func(folder
, item
, folder
->ui_func_data
);
2881 wildcard
[0] = separator
;
2884 real_path
= imap_get_real_path(session
, imapfolder
, item
->path
, &r
);
2892 real_path
= g_strdup("");
2895 Xstrcat_a(wildcard_path
, real_path
, wildcard
,
2896 {g_free(real_path
); return MAILIMAP_ERROR_BAD_STATE
;});
2900 r
= imap_threaded_lsub(folder
, "", wildcard_path
, &lep_list
);
2902 r
= imap_threaded_list(folder
, "", wildcard_path
, &lep_list
);
2904 if (r
!= MAILIMAP_NO_ERROR
) {
2905 imap_handle_error(SESSION(session
), NULL
, r
);
2911 item_list
= imap_list_from_lep(imapfolder
,
2912 lep_list
, real_path
, FALSE
);
2913 mailimap_list_result_free(lep_list
);
2918 node
= item
->node
->children
;
2919 while (node
!= NULL
) {
2920 FolderItem
*old_item
= FOLDER_ITEM(node
->data
);
2921 GNode
*next
= node
->next
;
2924 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
2925 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2926 if (!g_strcmp0(old_item
->path
, cur_item
->path
)) {
2927 new_item
= cur_item
;
2932 if (old_item
&& old_item
->path
&& !strcasecmp(old_item
->path
, "INBOX")) {
2933 debug_print("not removing INBOX\n");
2935 debug_print("folder '%s' not found. removing...\n",
2937 folder_item_remove(old_item
);
2940 old_item
->no_sub
= new_item
->no_sub
;
2941 old_item
->no_select
= new_item
->no_select
;
2942 if (old_item
->no_sub
== TRUE
&& node
->children
) {
2943 debug_print("folder '%s' doesn't have "
2944 "subfolders. removing...\n",
2946 folder_item_remove_children(old_item
);
2953 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
2954 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
2957 for (node
= item
->node
->children
; node
!= NULL
;
2958 node
= node
->next
) {
2959 if (!g_strcmp0(FOLDER_ITEM(node
->data
)->path
,
2961 new_item
= FOLDER_ITEM(node
->data
);
2962 folder_item_destroy(cur_item
);
2968 new_item
= cur_item
;
2969 debug_print("new folder '%s' found.\n", new_item
->path
);
2970 folder_item_append(item
, new_item
);
2973 if (!strcasecmp(new_item
->path
, "INBOX")) {
2974 new_item
->stype
= F_INBOX
;
2975 folder
->inbox
= new_item
;
2976 } else if (!folder_item_parent(item
) || item
->stype
== F_INBOX
) {
2979 base
= g_path_get_basename(new_item
->path
);
2981 if (!folder
->outbox
&& !g_ascii_strcasecmp(base
, "Sent")) {
2982 new_item
->stype
= F_OUTBOX
;
2983 folder
->outbox
= new_item
;
2984 } else if (!folder
->draft
&& (!g_ascii_strcasecmp(base
, "Drafts") || !g_ascii_strcasecmp(base
, "Draft"))) {
2985 new_item
->stype
= F_DRAFT
;
2986 folder
->draft
= new_item
;
2987 } else if (!folder
->queue
&& !g_ascii_strcasecmp(base
, "Queue")) {
2988 new_item
->stype
= F_QUEUE
;
2989 folder
->queue
= new_item
;
2990 } else if (!folder
->trash
&& !g_ascii_strcasecmp(base
, "Trash")) {
2991 new_item
->stype
= F_TRASH
;
2992 folder
->trash
= new_item
;
2997 if (new_item
->no_sub
== FALSE
) {
2998 imap_scan_tree_recursive_dive(session
, new_item
, subs_only
);
3000 /* entering recursion func: increase depth counter */
3001 session
->scan_tree_recurs_depth
--;
3002 if (session
->scan_tree_recurs_depth
< 0)
3003 g_error("IMAP scan tree recursion underflow (%d)",
3004 session
->scan_tree_recurs_depth
);
3008 g_slist_free(item_list
);
3010 return MAILIMAP_NO_ERROR
;
3013 GList
*imap_scan_subtree(Folder
*folder
, FolderItem
*item
, gboolean unsubs_only
, gboolean recursive
)
3015 IMAPSession
*session
= imap_session_get(folder
);
3017 gchar
*wildcard_path
;
3021 GSList
*item_list
= NULL
, *cur
;
3022 GList
*child_list
= NULL
, *tmplist
= NULL
;
3023 GSList
*sub_list
= NULL
;
3024 int r
= MAILIMAP_NO_ERROR
;
3029 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
3034 wildcard
[0] = separator
;
3037 real_path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
3045 real_path
= g_strdup("");
3048 Xstrcat_a(wildcard_path
, real_path
, wildcard
,
3049 {g_free(real_path
); return NULL
;});
3053 statusbar_print_all(_("Looking for unsubscribed folders in %s..."),
3054 item
->path
?item
->path
:item
->name
);
3056 statusbar_print_all(_("Looking for subfolders of %s..."),
3057 item
->path
?item
->path
:item
->name
);
3059 r
= imap_threaded_list(folder
, "", wildcard_path
, &lep_list
);
3062 statusbar_pop_all();
3065 item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3066 lep_list
, real_path
, FALSE
);
3067 mailimap_list_result_free(lep_list
);
3069 for (cur
= item_list
; cur
!= NULL
; cur
= cur
->next
) {
3070 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
3072 tmplist
= imap_scan_subtree(folder
, cur_item
,
3073 unsubs_only
, recursive
);
3075 child_list
= g_list_concat(child_list
, tmplist
);
3077 child_list
= g_list_prepend(child_list
,
3078 imap_get_real_path(session
,
3079 IMAP_FOLDER(folder
), cur_item
->path
, &r
));
3082 statusbar_pop_all();
3085 folder_item_destroy(cur_item
);
3087 child_list
= g_list_reverse(child_list
);
3088 g_slist_free(item_list
);
3091 r
= imap_threaded_lsub(folder
, "", wildcard_path
, &lep_list
);
3094 statusbar_pop_all();
3097 sub_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3098 lep_list
, real_path
, FALSE
);
3099 mailimap_list_result_free(lep_list
);
3101 for (cur
= sub_list
; cur
!= NULL
; cur
= cur
->next
) {
3102 FolderItem
*cur_item
= FOLDER_ITEM(cur
->data
);
3103 GList
*oldlitem
= NULL
;
3104 gchar
*tmp
= imap_get_real_path(session
,
3105 IMAP_FOLDER(folder
), cur_item
->path
, &r
);
3108 statusbar_pop_all();
3112 folder_item_destroy(cur_item
);
3113 oldlitem
= g_list_find_custom(
3114 child_list
, tmp
, (GCompareFunc
)g_strcmp0
);
3116 child_list
= g_list_remove_link(child_list
, oldlitem
);
3117 g_free(oldlitem
->data
);
3118 g_list_free(oldlitem
);
3125 statusbar_pop_all();
3130 static gint
imap_create_tree(Folder
*folder
)
3132 g_return_val_if_fail(folder
!= NULL
, -1);
3133 g_return_val_if_fail(folder
->node
!= NULL
, -1);
3134 g_return_val_if_fail(folder
->node
->data
!= NULL
, -1);
3135 g_return_val_if_fail(folder
->account
!= NULL
, -1);
3137 imap_scan_tree(folder
);
3138 imap_create_missing_folders(folder
);
3143 static void imap_create_missing_folders(Folder
*folder
)
3145 g_return_if_fail(folder
!= NULL
);
3148 folder
->inbox
= imap_create_special_folder
3149 (folder
, F_INBOX
, "INBOX");
3151 folder
->trash
= imap_create_special_folder
3152 (folder
, F_TRASH
, "Trash");
3154 folder
->queue
= imap_create_special_folder
3155 (folder
, F_QUEUE
, "Queue");
3156 if (!folder
->outbox
)
3157 folder
->outbox
= imap_create_special_folder
3158 (folder
, F_OUTBOX
, "Sent");
3160 folder
->draft
= imap_create_special_folder
3161 (folder
, F_DRAFT
, "Drafts");
3164 static FolderItem
*imap_create_special_folder(Folder
*folder
,
3165 SpecialFolderItemType stype
,
3169 FolderItem
*new_item
;
3171 g_return_val_if_fail(folder
!= NULL
, NULL
);
3172 g_return_val_if_fail(folder
->node
!= NULL
, NULL
);
3173 g_return_val_if_fail(folder
->node
->data
!= NULL
, NULL
);
3174 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3175 g_return_val_if_fail(name
!= NULL
, NULL
);
3177 item
= FOLDER_ITEM(folder
->node
->data
);
3178 new_item
= imap_create_folder(folder
, item
, name
);
3181 g_warning("can't create '%s'", name
);
3182 if (!folder
->inbox
) return NULL
;
3184 new_item
= imap_create_folder(folder
, folder
->inbox
, name
);
3186 g_warning("can't create '%s' under INBOX", name
);
3188 new_item
->stype
= stype
;
3190 new_item
->stype
= stype
;
3196 static gchar
*imap_encode_unsafe_chars(const gchar
*str
)
3198 gchar
*ret
= NULL
, *o_ret
;
3202 ret
= g_malloc(3*strlen(str
)+1);
3204 for (i
= (gchar
*)str
; *i
; i
++) {
3214 *ret
++ = '0'+(*i
/10);
3215 *ret
++ = '0'+(*i
%10);
3225 static gchar
*imap_item_get_path(Folder
*folder
, FolderItem
*item
)
3227 gchar
*folder_path
, *path
;
3228 gchar
*item_path
= NULL
;
3230 g_return_val_if_fail(folder
!= NULL
, NULL
);
3231 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3232 g_return_val_if_fail(item
!= NULL
, NULL
);
3233 folder_path
= prefs_account_cache_dir(folder
->account
, FALSE
);
3235 g_return_val_if_fail(folder_path
!= NULL
, NULL
);
3238 item_path
= g_strdup(item
->path
);
3240 item_path
= imap_encode_unsafe_chars(item
->path
);
3243 if (g_path_is_absolute(folder_path
)) {
3245 path
= g_strconcat(folder_path
, G_DIR_SEPARATOR_S
,
3248 path
= g_strdup(folder_path
);
3251 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
3252 folder_path
, G_DIR_SEPARATOR_S
,
3255 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
3258 g_free(folder_path
);
3261 while (strchr(path
, '/'))
3262 *strchr(path
, '/') = '\\';
3268 static FolderItem
*imap_create_folder(Folder
*folder
, FolderItem
*parent
,
3271 gchar
*dirpath
, *imap_path
;
3272 IMAPSession
*session
;
3273 FolderItem
*new_item
;
3277 gint ok
= MAILIMAP_NO_ERROR
;
3278 gboolean no_select
= FALSE
, no_sub
= FALSE
;
3279 gboolean exist
= FALSE
;
3281 g_return_val_if_fail(folder
!= NULL
, NULL
);
3282 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
3283 g_return_val_if_fail(parent
!= NULL
, NULL
);
3284 g_return_val_if_fail(name
!= NULL
, NULL
);
3286 if (to_number(name
) > 0) {
3287 gchar
*cached_msg
= imap_get_cached_filename(parent
, to_number(name
));
3288 if (is_file_exist(cached_msg
)) {
3289 if (claws_unlink(cached_msg
) != 0) {
3297 debug_print("getting session...\n");
3298 session
= imap_session_get(folder
);
3303 if (!folder_item_parent(parent
) && strcasecmp(name
, "INBOX") == 0) {
3304 dirpath
= g_strdup(name
);
3305 }else if (parent
->path
)
3306 dirpath
= g_strconcat(parent
->path
, "/", name
, NULL
);
3307 else if ((p
= strchr(name
, '/')) != NULL
&& *(p
+ 1) != '\0')
3308 dirpath
= g_strdup(name
);
3309 else if (folder
->account
->imap_dir
&& *folder
->account
->imap_dir
) {
3312 Xstrdup_a(imap_dir
, folder
->account
->imap_dir
, {return NULL
;});
3313 strtailchomp(imap_dir
, '/');
3314 dirpath
= g_strconcat(imap_dir
, "/", name
, NULL
);
3316 dirpath
= g_strdup(name
);
3320 /* keep trailing directory separator to create a folder that contains
3322 imap_path
= imap_utf8_to_modified_utf7(dirpath
, FALSE
);
3324 strtailchomp(dirpath
, '/');
3325 Xstrdup_a(new_name
, name
, {
3330 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), imap_path
, &ok
);
3336 imap_path_separator_subst(imap_path
, separator
);
3337 /* remove trailing / for display */
3338 strtailchomp(new_name
, '/');
3340 if (strcasecmp(dirpath
, "INBOX") != 0) {
3344 r
= imap_threaded_list(folder
, "", imap_path
, &lep_list
);
3345 if (r
!= MAILIMAP_NO_ERROR
) {
3346 imap_handle_error(SESSION(session
), NULL
, r
);
3347 log_warning(LOG_PROTOCOL
, _("can't create mailbox: LIST failed\n"));
3353 if (clist_count(lep_list
) > 0)
3355 mailimap_list_result_free(lep_list
);
3358 ok
= imap_cmd_create(session
, imap_path
);
3359 if (ok
!= MAILIMAP_NO_ERROR
) {
3360 log_warning(LOG_PROTOCOL
, _("can't create mailbox\n"));
3365 r
= imap_threaded_list(folder
, "", imap_path
, &lep_list
);
3366 if (r
== MAILIMAP_NO_ERROR
) {
3367 GSList
*item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3368 lep_list
, dirpath
, TRUE
);
3370 FolderItem
*cur_item
= FOLDER_ITEM(item_list
->data
);
3371 no_select
= cur_item
->no_select
;
3372 no_sub
= cur_item
->no_sub
;
3373 g_slist_free(item_list
);
3375 mailimap_list_result_free(lep_list
);
3377 imap_handle_error(SESSION(session
), NULL
, r
);
3380 imap_threaded_subscribe(folder
, imap_path
, TRUE
);
3384 /* just get flags */
3385 r
= imap_threaded_list(folder
, "", "INBOX", &lep_list
);
3386 if (r
== MAILIMAP_NO_ERROR
) {
3387 GSList
*item_list
= imap_list_from_lep(IMAP_FOLDER(folder
),
3388 lep_list
, dirpath
, TRUE
);
3390 FolderItem
*cur_item
= FOLDER_ITEM(item_list
->data
);
3391 no_select
= cur_item
->no_select
;
3392 no_sub
= cur_item
->no_sub
;
3393 g_slist_free(item_list
);
3395 mailimap_list_result_free(lep_list
);
3397 imap_handle_error(SESSION(session
), NULL
, r
);
3401 new_item
= folder_item_new(folder
, new_name
, dirpath
);
3402 new_item
->no_select
= no_select
;
3403 new_item
->no_sub
= no_sub
;
3404 folder_item_append(parent
, new_item
);
3408 dirpath
= folder_item_get_path(new_item
);
3409 if (!is_dir_exist(dirpath
))
3410 make_dir_hier(dirpath
);
3414 /* folder existed, scan it */
3415 imap_scan_required(folder
, new_item
);
3416 folder_item_scan_full(new_item
, FALSE
);
3422 static gint
imap_rename_folder(Folder
*folder
, FolderItem
*item
,
3427 gchar
*real_oldpath
;
3428 gchar
*real_newpath
;
3430 gchar
*old_cache_dir
;
3431 gchar
*new_cache_dir
;
3432 IMAPSession
*session
;
3434 gint ok
= MAILIMAP_NO_ERROR
;
3435 gint exists
, recent
, unseen
;
3436 guint32 uid_validity
;
3438 g_return_val_if_fail(folder
!= NULL
, -1);
3439 g_return_val_if_fail(item
!= NULL
, -1);
3440 g_return_val_if_fail(item
->path
!= NULL
, -1);
3441 g_return_val_if_fail(name
!= NULL
, -1);
3443 debug_print("getting session...\n");
3444 session
= imap_session_get(folder
);
3449 if (strchr(name
, imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &ok
)) != NULL
||
3451 g_warning("new folder name must not contain the namespace path separator");
3455 real_oldpath
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3457 g_free(real_oldpath
);
3461 g_free(session
->mbox
);
3462 session
->mbox
= NULL
;
3463 session
->exists
= 0;
3464 session
->recent
= 0;
3465 session
->expunge
= 0;
3466 ok
= imap_cmd_examine(session
, "INBOX",
3467 &exists
, &recent
, &unseen
, &uid_validity
, FALSE
);
3468 if (ok
!= MAILIMAP_NO_ERROR
) {
3469 g_free(real_oldpath
);
3473 separator
= imap_get_path_separator(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3475 g_free(real_oldpath
);
3478 if (strchr(item
->path
, '/')) {
3479 dirpath
= g_path_get_dirname(item
->path
);
3480 newpath
= g_strconcat(dirpath
, "/", name
, NULL
);
3483 newpath
= g_strdup(name
);
3485 real_newpath
= imap_utf8_to_modified_utf7(newpath
, FALSE
);
3486 imap_path_separator_subst(real_newpath
, separator
);
3488 ok
= imap_cmd_rename(session
, real_oldpath
, real_newpath
);
3489 if (ok
!= MAILIMAP_NO_ERROR
) {
3490 log_warning(LOG_PROTOCOL
, _("can't rename mailbox: %s to %s\n"),
3491 real_oldpath
, real_newpath
);
3492 g_free(real_oldpath
);
3494 g_free(real_newpath
);
3498 item
->name
= g_strdup(name
);
3500 old_cache_dir
= folder_item_get_path(item
);
3502 paths
[0] = g_strdup(item
->path
);
3504 g_node_traverse(item
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
3505 imap_rename_folder_func
, paths
);
3507 if (is_dir_exist(old_cache_dir
)) {
3508 new_cache_dir
= folder_item_get_path(item
);
3509 if (g_rename(old_cache_dir
, new_cache_dir
) < 0) {
3510 FILE_OP_ERROR(old_cache_dir
, "rename");
3512 g_free(new_cache_dir
);
3515 g_free(old_cache_dir
);
3518 g_free(real_oldpath
);
3519 g_free(real_newpath
);
3523 gint
imap_subscribe(Folder
*folder
, FolderItem
*item
, gchar
*rpath
, gboolean sub
)
3526 gint r
= MAILIMAP_NO_ERROR
;
3527 IMAPSession
*session
;
3528 debug_print("getting session...\n");
3530 session
= imap_session_get(folder
);
3534 if (item
&& item
->path
) {
3535 path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &r
);
3542 if (!strcasecmp(path
, "INBOX") && sub
== FALSE
) {
3546 debug_print("%ssubscribing %s\n", sub
?"":"un", path
);
3547 r
= imap_threaded_subscribe(folder
, path
, sub
);
3550 r
= imap_threaded_subscribe(folder
, rpath
, sub
);
3556 static gint
imap_remove_folder_real(Folder
*folder
, FolderItem
*item
)
3558 gint ok
= MAILIMAP_NO_ERROR
;
3559 IMAPSession
*session
;
3562 gboolean selected_folder
;
3564 g_return_val_if_fail(folder
!= NULL
, -1);
3565 g_return_val_if_fail(item
!= NULL
, -1);
3566 g_return_val_if_fail(item
->path
!= NULL
, -1);
3568 debug_print("getting session...\n");
3569 session
= imap_session_get(folder
);
3573 path
= imap_get_real_path(session
, IMAP_FOLDER(folder
), item
->path
, &ok
);
3578 imap_threaded_subscribe(folder
, path
, FALSE
);
3580 selected_folder
= (session
->mbox
!= NULL
) &&
3581 (!strcmp(session
->mbox
, item
->path
));
3582 if (selected_folder
) {
3583 ok
= imap_cmd_close(session
);
3584 if (ok
!= MAILIMAP_NO_ERROR
) {
3585 debug_print("close err %d\n", ok
);
3589 ok
= imap_cmd_delete(session
, path
);
3590 if (ok
!= MAILIMAP_NO_ERROR
&& !is_fatal(ok
)) {
3593 ok
= MAILIMAP_NO_ERROR
;
3594 tmp
= g_strdup_printf("%s%c", path
,
3595 imap_get_path_separator(session
, IMAP_FOLDER(folder
), path
, &ok
));
3599 ok
= imap_cmd_delete(session
, path
);
3602 if (ok
!= MAILIMAP_NO_ERROR
) {
3603 log_warning(LOG_PROTOCOL
, _("can't delete mailbox\n"));
3609 cache_dir
= folder_item_get_path(item
);
3610 if (is_dir_exist(cache_dir
) && remove_dir_recursive(cache_dir
) < 0)
3611 g_warning("can't remove directory '%s'", cache_dir
);
3613 folder_item_remove(item
);
3617 static gint
imap_remove_folder(Folder
*folder
, FolderItem
*item
)
3621 g_return_val_if_fail(item
!= NULL
, -1);
3622 g_return_val_if_fail(item
->folder
!= NULL
, -1);
3623 g_return_val_if_fail(item
->node
!= NULL
, -1);
3625 node
= item
->node
->children
;
3626 while (node
!= NULL
) {
3628 if (imap_remove_folder(folder
, FOLDER_ITEM(node
->data
)) < 0)
3632 debug_print("IMAP removing %s\n", item
->path
);
3634 if (imap_remove_all_msg(folder
, item
) < 0)
3636 return imap_remove_folder_real(folder
, item
);
3639 typedef struct _uncached_data
{
3640 IMAPSession
*session
;
3642 MsgNumberList
*numlist
;
3649 static void *imap_get_uncached_messages_thread(void *data
)
3651 uncached_data
*stuff
= (uncached_data
*)data
;
3652 IMAPSession
*session
= stuff
->session
;
3653 FolderItem
*item
= stuff
->item
;
3654 MsgNumberList
*numlist
= stuff
->numlist
;
3655 GSList
*newlist
= NULL
;
3656 GSList
*llast
= NULL
;
3657 GSList
*seq_list
, *cur
;
3658 gboolean got_alien_tags
= FALSE
;
3660 debug_print("uncached_messages\n");
3662 if (session
== NULL
|| item
== NULL
|| item
->folder
== NULL
3663 || FOLDER_CLASS(item
->folder
) != &imap_class
) {
3668 seq_list
= imap_get_lep_set_from_numlist(IMAP_FOLDER(item
->folder
), numlist
);
3669 debug_print("get msgs info\n");
3670 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
3671 struct mailimap_set
* imapset
;
3677 if (session
->cancelled
)
3680 imapset
= cur
->data
;
3682 r
= imap_threaded_fetch_env(session
->folder
,
3683 imapset
, &env_list
);
3684 if (r
!= MAILIMAP_NO_ERROR
) {
3685 imap_handle_error(SESSION(session
), NULL
, r
);
3693 session_set_access_time(SESSION(session
));
3696 for(i
= 0 ; i
< carray_count(env_list
) ; i
+= 2) {
3697 struct imap_fetch_env_info
* info
;
3699 GSList
*tags
= NULL
, *cur
= NULL
;
3700 info
= carray_get(env_list
, i
);
3701 tags
= carray_get(env_list
, i
+1);
3702 msginfo
= imap_envelope_from_lep(info
, item
);
3703 if (msginfo
== NULL
) {
3704 slist_free_strings_full(tags
);
3707 g_slist_free(msginfo
->tags
);
3708 msginfo
->tags
= NULL
;
3710 for (cur
= tags
; cur
; cur
= cur
->next
) {
3711 gchar
*real_tag
= imap_modified_utf7_to_utf8(cur
->data
, TRUE
);
3713 id
= tags_get_id_for_str(real_tag
);
3715 id
= tags_add_tag(real_tag
);
3716 got_alien_tags
= TRUE
;
3718 if (!g_slist_find(msginfo
->tags
, GINT_TO_POINTER(id
))) {
3719 msginfo
->tags
= g_slist_prepend(
3721 GINT_TO_POINTER(id
));
3726 msginfo
->tags
= g_slist_reverse(msginfo
->tags
);
3727 slist_free_strings_full(tags
);
3728 msginfo
->folder
= item
;
3730 llast
= newlist
= g_slist_append(newlist
, msginfo
);
3732 llast
= g_slist_append(llast
, msginfo
);
3733 llast
= llast
->next
;
3738 imap_fetch_env_free(env_list
);
3741 if (got_alien_tags
) {
3743 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
3746 imap_lep_set_free(seq_list
);
3748 session_set_access_time(SESSION(session
));
3753 #define MAX_MSG_NUM 50
3755 static GSList
*imap_get_uncached_messages(IMAPSession
*session
,
3757 MsgNumberList
*numlist
,
3760 GSList
*result
= NULL
;
3762 uncached_data
*data
= g_new0(uncached_data
, 1);
3765 data
->total
= g_slist_length(numlist
);
3766 data
->ok
= MAILIMAP_NO_ERROR
;
3767 debug_print("messages list : %i\n", data
->total
);
3769 while (cur
!= NULL
) {
3770 GSList
* partial_result
;
3778 while (count
< MAX_MSG_NUM
) {
3783 if (newlist
== NULL
)
3784 llast
= newlist
= g_slist_append(newlist
, p
);
3786 llast
= g_slist_append(llast
, p
);
3787 llast
= llast
->next
;
3797 data
->session
= session
;
3799 data
->numlist
= newlist
;
3802 if (prefs_common
.work_offline
&&
3803 !inc_offline_should_override(FALSE
,
3804 _("Claws Mail needs network access in order "
3805 "to access the IMAP server."))) {
3811 (GSList
*)imap_get_uncached_messages_thread(data
);
3813 if (data
->ok
!= MAILIMAP_NO_ERROR
) {
3816 statusbar_progress_all(data
->cur
,data
->total
, 1);
3818 g_slist_free(newlist
);
3820 result
= g_slist_concat(result
, partial_result
);
3825 statusbar_progress_all(0,0,0);
3826 statusbar_pop_all();
3831 static void imap_delete_all_cached_messages(FolderItem
*item
)
3835 g_return_if_fail(item
!= NULL
);
3836 g_return_if_fail(item
->folder
!= NULL
);
3837 g_return_if_fail(FOLDER_CLASS(item
->folder
) == &imap_class
);
3839 debug_print("Deleting all cached messages...\n");
3841 dir
= folder_item_get_path(item
);
3842 if (is_dir_exist(dir
))
3843 remove_all_numbered_files(dir
);
3846 debug_print("Deleting all cached messages done.\n");
3849 gchar
imap_get_path_separator_for_item(FolderItem
*item
)
3851 Folder
*folder
= NULL
;
3852 IMAPFolder
*imap_folder
= NULL
;
3853 IMAPSession
*session
= NULL
;
3855 gint ok
= MAILIMAP_NO_ERROR
;
3858 folder
= item
->folder
;
3863 imap_folder
= IMAP_FOLDER(folder
);
3865 debug_print("getting session...\n");
3866 session
= imap_session_get(FOLDER(folder
));
3867 result
= imap_get_path_separator(session
, imap_folder
, item
->path
, &ok
);
3871 static gchar
imap_refresh_path_separator(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*subfolder
, gint
*ok
)
3875 gchar separator
= '\0';
3877 g_return_val_if_fail(session
!= NULL
, '/');
3878 r
= imap_threaded_list((Folder
*)folder
, "", subfolder
, &lep_list
);
3880 if (r
!= MAILIMAP_NO_ERROR
) {
3881 imap_handle_error(SESSION(session
), NULL
, r
);
3882 log_warning(LOG_PROTOCOL
, _("LIST failed\n"));
3887 if (lep_list
!= NULL
&& clist_count(lep_list
) > 0) {
3888 clistiter
* iter
= clist_begin(lep_list
);
3889 struct mailimap_mailbox_list
* mb
;
3890 mb
= clist_content(iter
);
3892 separator
= mb
->mb_delimiter
;
3893 debug_print("got separator: %c\n", folder
->last_seen_separator
);
3895 *ok
= MAILIMAP_NO_ERROR
;
3896 mailimap_list_result_free(lep_list
);
3900 static gchar
imap_get_path_separator(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*path
, gint
*ok
)
3902 gchar separator
= '/';
3903 *ok
= MAILIMAP_NO_ERROR
;
3904 if (folder
->last_seen_separator
== 0) {
3905 folder
->last_seen_separator
= imap_refresh_path_separator(session
, folder
, "", ok
);
3908 if (folder
->last_seen_separator
== 0) {
3909 folder
->last_seen_separator
= imap_refresh_path_separator(session
, folder
, "INBOX", ok
);
3912 if (folder
->last_seen_separator
!= 0) {
3913 debug_print("using separator: %c\n", folder
->last_seen_separator
);
3914 return folder
->last_seen_separator
;
3920 static gchar
*imap_get_real_path(IMAPSession
*session
, IMAPFolder
*folder
, const gchar
*path
, gint
*ok
)
3922 gchar
*real_path
= NULL
;
3925 g_return_val_if_fail(folder
!= NULL
, NULL
);
3926 g_return_val_if_fail(path
!= NULL
, NULL
);
3928 *ok
= MAILIMAP_NO_ERROR
;
3930 real_path
= imap_utf8_to_modified_utf7(path
, FALSE
);
3931 separator
= imap_get_path_separator(session
, folder
, path
, ok
);
3932 if (*ok
== MAILIMAP_NO_ERROR
)
3933 imap_path_separator_subst(real_path
, separator
);
3938 static gint
imap_set_message_flags(IMAPSession
*session
,
3939 IMAPFolderItem
*item
,
3940 MsgNumberList
*numlist
,
3949 IMAPFolder
*folder
= NULL
;
3950 GSList
*sorted_list
= NULL
;
3952 if (numlist
== NULL
|| session
== NULL
)
3953 return MAILIMAP_ERROR_BAD_STATE
;
3955 folder
= IMAP_FOLDER(session
->folder
);
3957 sorted_list
= g_slist_copy(numlist
);
3958 sorted_list
= g_slist_sort(sorted_list
, g_int_compare
);
3960 cur
= g_slist_last(sorted_list
);
3963 total
= GPOINTER_TO_INT(cur
->data
);
3965 seq_list
= imap_get_lep_set_from_numlist(IMAP_FOLDER(session
->folder
), sorted_list
);
3967 statusbar_print_all(_("Flagging messages..."));
3969 for(cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
3970 struct mailimap_set
* imapset
= (struct mailimap_set
*)cur
->data
;
3971 struct mailimap_set_item
*set_item
= NULL
;
3973 if (imapset
->set_list
)
3974 set_item
= clist_content(clist_begin(imapset
->set_list
));
3978 if (set_item
== NULL
)
3981 statusbar_progress_all(set_item
->set_first
, total
, 1);
3983 ok
= imap_cmd_store(session
, item
, imapset
,
3984 flags
, tags
, is_set
);
3985 statusbar_progress_all(set_item
->set_last
, total
, 1);
3986 if (ok
!= MAILIMAP_NO_ERROR
&& folder
->max_set_size
> 20) {
3987 /* reduce max set size */
3988 folder
->max_set_size
/= 2;
3990 if (ok
!= MAILIMAP_NO_ERROR
&& is_fatal(ok
)) {
3995 g_slist_free(sorted_list
);
3997 statusbar_progress_all(0,0,0);
3998 statusbar_pop_all();
4000 imap_lep_set_free(seq_list
);
4005 typedef struct _select_data
{
4006 IMAPSession
*session
;
4011 guint32
*uid_validity
;
4015 static gint
imap_select(IMAPSession
*session
, IMAPFolder
*folder
,
4017 gint
*exists
, gint
*recent
, gint
*unseen
,
4018 guint32
*uid_validity
, gint
*can_create_flags
,
4022 gint ok
= MAILIMAP_NO_ERROR
;
4023 gint exists_
, recent_
, unseen_
;
4024 guint32 uid_validity_
;
4025 gint can_create_flags_
;
4026 const gchar
*path
= item
? item
->path
:NULL
;
4029 return MAILIMAP_ERROR_BAD_STATE
;
4032 if (!exists
&& !recent
&& !unseen
&& !uid_validity
&& !can_create_flags
) {
4033 if (session
->mbox
&& strcmp(session
->mbox
, path
) == 0)
4034 return MAILIMAP_NO_ERROR
;
4036 if (!exists
&& !recent
&& !unseen
&& !uid_validity
&& can_create_flags
) {
4037 if (session
->mbox
&& strcmp(session
->mbox
, path
) == 0) {
4038 if (IMAP_FOLDER_ITEM(item
)->can_create_flags
!= ITEM_CAN_CREATE_FLAGS_UNKNOWN
)
4039 return MAILIMAP_NO_ERROR
;
4049 uid_validity
= &uid_validity_
;
4050 if (!can_create_flags
)
4051 can_create_flags
= &can_create_flags_
;
4053 g_free(session
->mbox
);
4054 session
->mbox
= NULL
;
4055 session
->exists
= 0;
4056 session
->recent
= 0;
4057 session
->expunge
= 0;
4059 real_path
= imap_get_real_path(session
, folder
, path
, &ok
);
4064 g_slist_free(IMAP_FOLDER_ITEM(item
)->ok_flags
);
4065 IMAP_FOLDER_ITEM(item
)->ok_flags
= NULL
;
4066 ok
= imap_cmd_select(session
, real_path
,
4067 exists
, recent
, unseen
, uid_validity
, can_create_flags
,
4068 &(IMAP_FOLDER_ITEM(item
)->ok_flags
), block
);
4069 if (ok
!= MAILIMAP_NO_ERROR
) {
4070 log_warning(LOG_PROTOCOL
, _("can't select folder: %s\n"), real_path
);
4072 session
->mbox
= g_strdup(path
);
4073 session
->folder_content_changed
= FALSE
;
4074 session
->exists
= *exists
;
4075 session
->recent
= *recent
;
4076 session
->expunge
= 0;
4077 session
->unseen
= *unseen
;
4078 session
->uid_validity
= *uid_validity
;
4079 debug_print("select: exists %d recent %d expunge %d uid_validity %d can_create_flags %d\n",
4080 session
->exists
, session
->recent
, session
->expunge
,
4081 session
->uid_validity
, *can_create_flags
);
4083 if (*can_create_flags
) {
4084 IMAP_FOLDER_ITEM(item
)->can_create_flags
= ITEM_CAN_CREATE_FLAGS
;
4086 IMAP_FOLDER_ITEM(item
)->can_create_flags
= ITEM_CANNOT_CREATE_FLAGS
;
4093 static gint
imap_status(IMAPSession
*session
, IMAPFolder
*folder
,
4094 const gchar
*path
, IMAPFolderItem
*item
,
4096 guint32
*uid_next
, guint32
*uid_validity
,
4097 gint
*unseen
, gboolean block
)
4099 int r
= MAILIMAP_NO_ERROR
;
4101 struct mailimap_mailbox_data_status
* data_status
;
4106 real_path
= imap_get_real_path(session
, folder
, path
, &r
);
4128 if (session
->mbox
!= NULL
&&
4129 !strcmp(session
->mbox
, item
->item
.path
)) {
4130 r
= imap_cmd_close(session
);
4131 if (r
!= MAILIMAP_NO_ERROR
) {
4132 debug_print("close err %d\n", r
);
4138 r
= imap_threaded_status(FOLDER(folder
), real_path
,
4139 &data_status
, mask
);
4142 if (r
!= MAILIMAP_NO_ERROR
) {
4143 imap_handle_error(SESSION(session
), NULL
, r
);
4144 debug_print("status err %d\n", r
);
4148 if (data_status
== NULL
|| data_status
->st_info_list
== NULL
) {
4149 debug_print("data_status %p\n", data_status
);
4151 debug_print("data_status->st_info_list %p\n", data_status
->st_info_list
);
4152 mailimap_mailbox_data_status_free(data_status
);
4154 return MAILIMAP_ERROR_BAD_STATE
;
4158 if (data_status
->st_info_list
) {
4159 for(iter
= clist_begin(data_status
->st_info_list
) ; iter
!= NULL
;
4160 iter
= clist_next(iter
)) {
4161 struct mailimap_status_info
* info
;
4163 info
= clist_content(iter
);
4164 switch (info
->st_att
) {
4165 case MAILIMAP_STATUS_ATT_MESSAGES
:
4167 * messages
= info
->st_value
;
4168 got_values
|= 1 << 0;
4172 case MAILIMAP_STATUS_ATT_UIDNEXT
:
4174 * uid_next
= info
->st_value
;
4175 got_values
|= 1 << 2;
4179 case MAILIMAP_STATUS_ATT_UIDVALIDITY
:
4181 * uid_validity
= info
->st_value
;
4182 got_values
|= 1 << 3;
4186 case MAILIMAP_STATUS_ATT_UNSEEN
:
4188 * unseen
= info
->st_value
;
4189 got_values
|= 1 << 4;
4195 mailimap_mailbox_data_status_free(data_status
);
4197 if (got_values
!= mask
) {
4198 g_warning("status: incomplete values received (%d)", got_values
);
4200 return MAILIMAP_NO_ERROR
;
4203 static void imap_free_capabilities(IMAPSession
*session
)
4205 slist_free_strings_full(session
->capability
);
4206 session
->capability
= NULL
;
4209 /* low-level IMAP4rev1 commands */
4211 static gint
imap_cmd_login(IMAPSession
*session
,
4212 const gchar
*user
, const gchar
*pass
,
4218 if (!strcmp(type
, "plaintext") && imap_has_capability(session
, "LOGINDISABLED")) {
4219 ok
= MAILIMAP_ERROR_BAD_STATE
;
4220 if (imap_has_capability(session
, "STARTTLS")) {
4222 log_warning(LOG_PROTOCOL
, _("Server requires STARTTLS to log in.\n"));
4223 ok
= imap_cmd_starttls(session
);
4224 if (ok
!= MAILIMAP_NO_ERROR
) {
4225 log_warning(LOG_PROTOCOL
, _("Can't start STARTTLS session.\n"));
4229 imap_free_capabilities(session
);
4230 if ((r
= imap_get_capabilities(session
)) != MAILIMAP_NO_ERROR
) {
4231 imap_handle_error(SESSION(session
), NULL
, r
);
4232 log_warning(LOG_PROTOCOL
, _("Can't refresh capabilities.\n"));
4237 log_error(LOG_PROTOCOL
, _("Connection to %s failed: "
4238 "server requires STARTTLS, but Claws Mail "
4239 "has been compiled without STARTTLS "
4241 SESSION(session
)->server
);
4242 return MAILIMAP_ERROR_LOGIN
;
4245 log_error(LOG_PROTOCOL
, _("Server logins are disabled.\n"));
4246 return MAILIMAP_ERROR_LOGIN
;
4250 log_print(LOG_PROTOCOL
, "IMAP> Logging %s to %s using %s\n",
4252 SESSION(session
)->server
,
4254 r
= imap_threaded_login(session
->folder
, user
, pass
, type
);
4255 if (r
!= MAILIMAP_NO_ERROR
) {
4256 imap_handle_error(SESSION(session
), NULL
, r
);
4257 log_print(LOG_PROTOCOL
, "IMAP< Error logging in to %s\n",
4258 SESSION(session
)->server
);
4261 log_print(LOG_PROTOCOL
, "IMAP< Login to %s successful\n",
4262 SESSION(session
)->server
);
4263 ok
= MAILIMAP_NO_ERROR
;
4268 static gint
imap_cmd_noop(IMAPSession
*session
)
4271 unsigned int exists
, recent
, expunge
, unseen
, uidnext
, uidval
;
4273 r
= imap_threaded_noop(session
->folder
, &exists
, &recent
, &expunge
, &unseen
, &uidnext
, &uidval
);
4274 if (r
!= MAILIMAP_NO_ERROR
) {
4275 imap_handle_error(SESSION(session
), NULL
, r
);
4276 debug_print("noop err %d\n", r
);
4280 if ((exists
&& exists
!= session
->exists
)
4281 || (recent
&& recent
!= session
->recent
)
4282 || (expunge
&& expunge
!= session
->expunge
)
4283 || (unseen
&& unseen
!= session
->unseen
)) {
4284 session
->folder_content_changed
= TRUE
;
4286 if (uidnext
!= 0 && uidnext
!= session
->uid_next
) {
4287 session
->uid_next
= uidnext
;
4288 session
->folder_content_changed
= TRUE
;
4290 if (uidval
!= 0 && uidval
!= session
->uid_validity
) {
4291 session
->uid_validity
= uidval
;
4292 session
->folder_content_changed
= TRUE
;
4295 session
->exists
= exists
;
4296 session
->recent
= recent
;
4297 session
->expunge
= expunge
;
4298 session
->unseen
= unseen
;
4300 session_set_access_time(SESSION(session
));
4302 return MAILIMAP_NO_ERROR
;
4306 static gint
imap_cmd_starttls(IMAPSession
*session
)
4310 r
= imap_threaded_starttls(session
->folder
,
4311 SESSION(session
)->server
, SESSION(session
)->port
);
4312 if (r
!= MAILIMAP_NO_ERROR
) {
4313 imap_handle_error(SESSION(session
), NULL
, r
);
4314 debug_print("STARTTLS err %d\n", r
);
4317 return MAILIMAP_NO_ERROR
;
4321 static gint
imap_cmd_select(IMAPSession
*session
, const gchar
*folder
,
4322 gint
*exists
, gint
*recent
, gint
*unseen
,
4323 guint32
*uid_validity
, gint
*can_create_flags
,
4324 GSList
**ok_flags
, gboolean block
)
4328 r
= imap_threaded_select(session
->folder
, folder
,
4329 exists
, recent
, unseen
, uid_validity
, can_create_flags
, ok_flags
);
4330 if (r
!= MAILIMAP_NO_ERROR
) {
4331 imap_handle_error(SESSION(session
), NULL
, r
);
4332 debug_print("select err %d\n", r
);
4335 return MAILIMAP_NO_ERROR
;
4338 static gint
imap_cmd_close(IMAPSession
*session
)
4342 r
= imap_threaded_close(session
->folder
);
4343 if (r
!= MAILIMAP_NO_ERROR
) {
4344 imap_handle_error(SESSION(session
), NULL
, r
);
4345 debug_print("close err %d\n", r
);
4348 g_free(session
->mbox
);
4349 session
->mbox
= NULL
;
4350 session
->exists
= 0;
4351 session
->recent
= 0;
4352 session
->expunge
= 0;
4353 return MAILIMAP_NO_ERROR
;
4356 static gint
imap_cmd_examine(IMAPSession
*session
, const gchar
*folder
,
4357 gint
*exists
, gint
*recent
, gint
*unseen
,
4358 guint32
*uid_validity
, gboolean block
)
4362 r
= imap_threaded_examine(session
->folder
, folder
,
4363 exists
, recent
, unseen
, uid_validity
);
4364 if (r
!= MAILIMAP_NO_ERROR
) {
4365 imap_handle_error(SESSION(session
), NULL
, r
);
4366 debug_print("examine err %d\n", r
);
4370 return MAILIMAP_NO_ERROR
;
4373 static gint
imap_cmd_create(IMAPSession
*session
, const gchar
*folder
)
4377 r
= imap_threaded_create(session
->folder
, folder
);
4378 if (r
!= MAILIMAP_NO_ERROR
) {
4379 imap_handle_error(SESSION(session
), NULL
, r
);
4383 return MAILIMAP_NO_ERROR
;
4386 static gint
imap_cmd_rename(IMAPSession
*session
, const gchar
*old_folder
,
4387 const gchar
*new_folder
)
4391 r
= imap_threaded_rename(session
->folder
, old_folder
,
4393 if (r
!= MAILIMAP_NO_ERROR
) {
4394 imap_handle_error(SESSION(session
), NULL
, r
);
4398 return MAILIMAP_NO_ERROR
;
4401 static gint
imap_cmd_delete(IMAPSession
*session
, const gchar
*folder
)
4406 r
= imap_threaded_delete(session
->folder
, folder
);
4407 if (r
!= MAILIMAP_NO_ERROR
) {
4408 imap_handle_error(SESSION(session
), NULL
, r
);
4412 return MAILIMAP_NO_ERROR
;
4415 typedef struct _fetch_data
{
4416 IMAPSession
*session
;
4418 const gchar
*filename
;
4424 static void *imap_cmd_fetch_thread(void *data
)
4426 fetch_data
*stuff
= (fetch_data
*)data
;
4427 IMAPSession
*session
= stuff
->session
;
4428 guint32 uid
= stuff
->uid
;
4429 const gchar
*filename
= stuff
->filename
;
4433 r
= imap_threaded_fetch_content(session
->folder
,
4437 r
= imap_threaded_fetch_content(session
->folder
,
4440 if (r
!= MAILIMAP_NO_ERROR
) {
4441 imap_handle_error(SESSION(session
), NULL
, r
);
4442 debug_print("fetch err %d\n", r
);
4443 return GINT_TO_POINTER(r
);
4445 return GINT_TO_POINTER(MAILIMAP_NO_ERROR
);
4448 static gint
imap_cmd_fetch(IMAPSession
*session
, guint32 uid
,
4449 const gchar
*filename
, gboolean headers
,
4452 fetch_data
*data
= g_new0(fetch_data
, 1);
4455 data
->session
= session
;
4457 data
->filename
= filename
;
4458 data
->headers
= headers
;
4461 if (prefs_common
.work_offline
&&
4462 !inc_offline_should_override(FALSE
,
4463 _("Claws Mail needs network access in order "
4464 "to access the IMAP server."))) {
4468 statusbar_print_all(_("Fetching message..."));
4469 result
= GPOINTER_TO_INT(imap_cmd_fetch_thread(data
));
4470 statusbar_pop_all();
4476 static gint
imap_cmd_append(IMAPSession
*session
,
4477 IMAPFolderItem
*item
,
4478 const gchar
*destfolder
,
4479 const gchar
*file
, IMAPFlags flags
,
4482 struct mailimap_flag_list
* flag_list
;
4485 cm_return_val_if_fail(file
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4487 flag_list
= imap_flag_to_lep(item
, flags
, NULL
);
4488 lock_session(session
);
4489 r
= imap_threaded_append(session
->folder
, destfolder
,
4490 file
, flag_list
, (int *)new_uid
);
4491 mailimap_flag_list_free(flag_list
);
4493 if (r
!= MAILIMAP_NO_ERROR
) {
4494 imap_handle_error(SESSION(session
), NULL
, r
);
4495 debug_print("append err %d\n", r
);
4499 unlock_session(session
);
4501 return MAILIMAP_NO_ERROR
;
4504 static gint
imap_cmd_copy(IMAPSession
*session
, struct mailimap_set
* set
,
4505 const gchar
*destfolder
,
4506 struct mailimap_set
**source
, struct mailimap_set
**dest
)
4510 g_return_val_if_fail(session
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4511 g_return_val_if_fail(set
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4512 g_return_val_if_fail(destfolder
!= NULL
, MAILIMAP_ERROR_BAD_STATE
);
4514 r
= imap_threaded_copy(session
->folder
, set
, destfolder
, source
, dest
);
4515 if (r
!= MAILIMAP_NO_ERROR
) {
4516 imap_handle_error(SESSION(session
), NULL
, r
);
4520 return MAILIMAP_NO_ERROR
;
4523 static gint
imap_cmd_store(IMAPSession
*session
,
4524 IMAPFolderItem
*item
,
4525 struct mailimap_set
* set
,
4526 IMAPFlags flags
, GSList
*tags
, int do_add
)
4529 struct mailimap_flag_list
* flag_list
= NULL
;
4530 struct mailimap_store_att_flags
* store_att_flags
;
4532 flag_list
= imap_flag_to_lep(item
, flags
, tags
);
4536 mailimap_store_att_flags_new_add_flags_silent(flag_list
);
4539 mailimap_store_att_flags_new_remove_flags_silent(flag_list
);
4541 r
= imap_threaded_store(session
->folder
, set
, store_att_flags
);
4542 mailimap_store_att_flags_free(store_att_flags
);
4543 if (r
!= MAILIMAP_NO_ERROR
) {
4544 imap_handle_error(SESSION(session
), NULL
, r
);
4548 return MAILIMAP_NO_ERROR
;
4551 static gint
imap_cmd_expunge(IMAPSession
*session
)
4555 if (prefs_common
.work_offline
&&
4556 !inc_offline_should_override(FALSE
,
4557 _("Claws Mail needs network access in order "
4558 "to access the IMAP server."))) {
4562 statusbar_print_all(_("Expunging deleted messages..."));
4563 r
= imap_threaded_expunge(session
->folder
);
4564 statusbar_pop_all();
4565 if (r
!= MAILIMAP_NO_ERROR
) {
4566 imap_handle_error(SESSION(session
), NULL
, r
);
4570 return MAILIMAP_NO_ERROR
;
4573 gint
imap_expunge(Folder
*folder
, FolderItem
*item
)
4575 IMAPSession
*session
= imap_session_get(folder
);
4576 if (session
== NULL
)
4579 return imap_cmd_expunge(session
);
4582 static void imap_path_separator_subst(gchar
*str
, gchar separator
)
4585 gboolean in_escape
= FALSE
;
4587 if (!separator
|| separator
== '/') return;
4589 for (p
= str
; *p
!= '\0'; p
++) {
4590 if (*p
== '/' && !in_escape
)
4592 else if (*p
== '&' && *(p
+ 1) != '-' && !in_escape
)
4594 else if (*p
== '-' && in_escape
)
4599 static gboolean
imap_rename_folder_func(GNode
*node
, gpointer data
)
4601 FolderItem
*item
= node
->data
;
4602 gchar
**paths
= data
;
4603 const gchar
*oldpath
= paths
[0];
4604 const gchar
*newpath
= paths
[1];
4605 gchar
*real_oldpath
, *real_newpath
;
4607 gchar
*new_itempath
;
4609 IMAPSession
*session
= imap_session_get(item
->folder
);
4610 gint ok
= MAILIMAP_NO_ERROR
;
4611 oldpathlen
= strlen(oldpath
);
4612 if (strncmp(oldpath
, item
->path
, oldpathlen
) != 0) {
4613 g_warning("path doesn't match: %s, %s", oldpath
, item
->path
);
4617 base
= item
->path
+ oldpathlen
;
4618 while (*base
== '/') base
++;
4620 new_itempath
= g_strdup(newpath
);
4622 new_itempath
= g_strconcat(newpath
, "/", base
,
4625 real_oldpath
= imap_get_real_path(session
, IMAP_FOLDER(item
->folder
), item
->path
, &ok
);
4627 item
->path
= new_itempath
;
4629 real_newpath
= imap_get_real_path(session
, IMAP_FOLDER(item
->folder
), item
->path
, &ok
);
4631 imap_threaded_subscribe(item
->folder
, real_oldpath
, FALSE
);
4632 imap_threaded_subscribe(item
->folder
, real_newpath
, TRUE
);
4634 g_free(real_oldpath
);
4635 g_free(real_newpath
);
4639 static gint
get_list_of_uids(IMAPSession
*session
, Folder
*folder
, IMAPFolderItem
*item
, GSList
**msgnum_list
)
4641 GSList
*uidlist
, *elem
;
4643 clist
* lep_uidlist
;
4644 gint ok
, nummsgs
= 0;
4646 if (session
== NULL
) {
4650 ok
= imap_select(session
, IMAP_FOLDER(folder
), FOLDER_ITEM(item
),
4651 NULL
, NULL
, NULL
, NULL
, NULL
, TRUE
);
4652 if (ok
!= MAILIMAP_NO_ERROR
) {
4656 g_slist_free(item
->uid_list
);
4657 item
->uid_list
= NULL
;
4661 if (folder
->account
&& folder
->account
->low_bandwidth
) {
4662 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SIMPLE
,
4663 NULL
, NULL
, NULL
, &lep_uidlist
);
4666 if (r
== MAILIMAP_NO_ERROR
) {
4667 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
4668 mailimap_search_result_free(lep_uidlist
);
4670 carray
* lep_uidtab
;
4671 if (r
!= -1) { /* inited */
4672 imap_handle_error(SESSION(session
), NULL
, r
);
4676 r
= imap_threaded_fetch_uid(folder
, 1,
4678 if (r
== MAILIMAP_NO_ERROR
) {
4679 uidlist
= imap_uid_list_from_lep_tab(lep_uidtab
);
4680 imap_fetch_uid_list_free(lep_uidtab
);
4684 if (r
!= MAILIMAP_NO_ERROR
) {
4685 imap_handle_error(SESSION(session
), NULL
, r
);
4689 for (elem
= uidlist
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
4692 msgnum
= GPOINTER_TO_INT(elem
->data
);
4694 *msgnum_list
= g_slist_prepend(*msgnum_list
, GINT_TO_POINTER(msgnum
));
4695 item
->uid_list
= g_slist_prepend(item
->uid_list
, GINT_TO_POINTER(msgnum
));
4698 g_slist_free(uidlist
);
4704 gint
imap_get_num_list(Folder
*folder
, FolderItem
*_item
, GSList
**msgnum_list
, gboolean
*old_uids_valid
)
4706 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
4707 IMAPSession
*session
;
4709 GSList
*uidlist
= NULL
;
4711 gint known_list_len
= 0;
4714 debug_print("get_num_list\n");
4716 g_return_val_if_fail(folder
!= NULL
, -1);
4717 g_return_val_if_fail(item
!= NULL
, -1);
4718 g_return_val_if_fail(item
->item
.path
!= NULL
, -1);
4719 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, -1);
4720 g_return_val_if_fail(folder
->account
!= NULL
, -1);
4722 known_list_len
= g_slist_length(item
->uid_list
);
4723 if (!item
->should_update
) {
4724 debug_print("get_num_list: nothing to update\n");
4725 *old_uids_valid
= TRUE
;
4726 if (known_list_len
== item
->item
.total_msgs
4727 && known_list_len
> 0) {
4728 *msgnum_list
= g_slist_copy(item
->uid_list
);
4729 return known_list_len
;
4731 debug_print("don't know the list length...\n");
4735 if (prefs_common
.work_offline
&&
4736 !inc_offline_should_override(FALSE
,
4737 _("Claws Mail needs network access in order "
4738 "to access the IMAP server."))) {
4742 path
= folder_item_get_path(_item
);
4743 if (!is_dir_exist(path
)) {
4744 if(is_file_exist(path
))
4746 make_dir_hier(path
);
4750 debug_print("getting session...\n");
4751 session
= imap_session_get(folder
);
4752 g_return_val_if_fail(session
!= NULL
, -1);
4754 lock_session(session
);
4755 if (FOLDER_ITEM(item
)->path
)
4756 statusbar_print_all(_("Scanning folder %s/%s..."),
4757 FOLDER_ITEM(item
)->folder
->name
,
4758 FOLDER_ITEM(item
)->path
);
4760 statusbar_print_all(_("Scanning folder %s..."),
4761 FOLDER_ITEM(item
)->folder
->name
);
4763 if (item
->should_trash_cache
) {
4764 *old_uids_valid
= FALSE
;
4765 debug_print("get_num_list: trashing num list\n");
4766 debug_print("Freeing imap uid cache\n");
4768 g_slist_free(item
->uid_list
);
4769 item
->uid_list
= NULL
;
4771 imap_delete_all_cached_messages((FolderItem
*)item
);
4773 debug_print("get_num_list: updating num list\n");
4774 *old_uids_valid
= TRUE
;
4777 nummsgs
= get_list_of_uids(session
, folder
, item
, &uidlist
);
4779 unlock_session(session
);
4781 /* session could be broken now, in case of fatal error */
4783 debug_print("get_num_list: got %d msgs\n", nummsgs
);
4786 statusbar_pop_all();
4790 *msgnum_list
= uidlist
;
4792 dir
= folder_item_get_path((FolderItem
*)item
);
4793 debug_print("removing old messages from %s\n", dir
);
4794 remove_numbered_files_not_in_list(dir
, *msgnum_list
);
4797 debug_print("get_num_list - ok - %i\n", nummsgs
);
4798 statusbar_pop_all();
4799 item
->should_trash_cache
= FALSE
;
4800 item
->should_update
= FALSE
;
4804 static MsgInfo
*imap_parse_msg(const gchar
*file
, FolderItem
*item
)
4809 flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
4810 flags
.tmp_flags
= 0;
4812 g_return_val_if_fail(item
!= NULL
, NULL
);
4813 g_return_val_if_fail(file
!= NULL
, NULL
);
4815 if (folder_has_parent_of_type(item
, F_QUEUE
)) {
4816 MSG_SET_TMP_FLAGS(flags
, MSG_QUEUED
);
4817 } else if (folder_has_parent_of_type(item
, F_DRAFT
)) {
4818 MSG_SET_TMP_FLAGS(flags
, MSG_DRAFT
);
4821 msginfo
= procheader_parse_file(file
, flags
, FALSE
, FALSE
);
4822 if (!msginfo
) return NULL
;
4824 msginfo
->plaintext_file
= g_strdup(file
);
4825 msginfo
->folder
= item
;
4830 GSList
*imap_get_msginfos(Folder
*folder
, FolderItem
*item
,
4831 GSList
*msgnum_list
)
4833 IMAPSession
*session
;
4834 MsgInfoList
*ret
= NULL
;
4837 debug_print("get_msginfos\n");
4839 g_return_val_if_fail(folder
!= NULL
, NULL
);
4840 g_return_val_if_fail(item
!= NULL
, NULL
);
4841 g_return_val_if_fail(msgnum_list
!= NULL
, NULL
);
4843 debug_print("getting session...\n");
4844 session
= imap_session_get(folder
);
4845 g_return_val_if_fail(session
!= NULL
, NULL
);
4847 lock_session(session
); /* unlocked later in the function */
4849 debug_print("IMAP getting msginfos\n");
4850 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
4851 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
4852 if (ok
!= MAILIMAP_NO_ERROR
) {
4855 if (!(folder_has_parent_of_type(item
, F_DRAFT
) ||
4856 folder_has_parent_of_type(item
, F_QUEUE
))) {
4857 ret
= g_slist_concat(ret
,
4858 imap_get_uncached_messages(session
, item
,
4860 if (ok
!= MAILIMAP_NO_ERROR
)
4862 unlock_session(session
);
4864 MsgNumberList
*sorted_list
, *elem
, *llast
= NULL
;
4865 gint startnum
, lastnum
;
4867 unlock_session(session
);
4869 sorted_list
= g_slist_sort(g_slist_copy(msgnum_list
), g_int_compare
);
4871 startnum
= lastnum
= GPOINTER_TO_INT(sorted_list
->data
);
4873 llast
= g_slist_last(ret
);
4874 for (elem
= sorted_list
;; elem
= g_slist_next(elem
)) {
4878 num
= GPOINTER_TO_INT(elem
->data
);
4880 if (num
> lastnum
+ 1 || elem
== NULL
) {
4882 for (i
= startnum
; i
<= lastnum
; ++i
) {
4884 file
= imap_fetch_msg(folder
, item
, i
);
4886 MsgInfo
*msginfo
= imap_parse_msg(file
, item
);
4887 if (msginfo
!= NULL
) {
4888 msginfo
->msgnum
= i
;
4890 llast
= ret
= g_slist_append(ret
, msginfo
);
4892 llast
= g_slist_append(llast
, msginfo
);
4893 llast
= llast
->next
;
4908 g_slist_free(sorted_list
);
4913 MsgInfo
*imap_get_msginfo(Folder
*folder
, FolderItem
*item
, gint uid
)
4915 MsgInfo
*msginfo
= NULL
;
4916 MsgInfoList
*msginfolist
;
4917 MsgNumberList numlist
;
4919 numlist
.next
= NULL
;
4920 numlist
.data
= GINT_TO_POINTER(uid
);
4922 msginfolist
= imap_get_msginfos(folder
, item
, &numlist
);
4923 if (msginfolist
!= NULL
) {
4924 msginfo
= msginfolist
->data
;
4925 g_slist_free(msginfolist
);
4931 gboolean
imap_scan_required(Folder
*folder
, FolderItem
*_item
)
4933 IMAPSession
*session
;
4934 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
4935 gint ok
, exists
= 0, unseen
= 0;
4936 guint32 uid_next
= 0, uid_val
= 0;
4937 gboolean selected_folder
;
4939 g_return_val_if_fail(folder
!= NULL
, FALSE
);
4940 g_return_val_if_fail(item
!= NULL
, FALSE
);
4941 g_return_val_if_fail(item
->item
.folder
!= NULL
, FALSE
);
4942 g_return_val_if_fail(FOLDER_CLASS(item
->item
.folder
) == &imap_class
, FALSE
);
4944 if (item
->item
.path
== NULL
)
4947 if (item
->should_update
) {
4948 debug_print("scan already required\n");
4951 debug_print("getting session...\n");
4952 session
= imap_session_get(folder
);
4954 g_return_val_if_fail(session
!= NULL
, FALSE
);
4955 lock_session(session
); /* unlocked later in the function */
4957 selected_folder
= (session
->mbox
!= NULL
) &&
4958 (!strcmp(session
->mbox
, item
->item
.path
));
4959 if (selected_folder
) {
4960 if (!session
->folder_content_changed
) {
4961 ok
= imap_cmd_noop(session
);
4962 if (ok
!= MAILIMAP_NO_ERROR
) {
4963 debug_print("disconnected!\n");
4965 session
= imap_reconnect_if_possible(folder
, session
);
4967 session
= imap_session_get(folder
);
4968 if (session
== NULL
)
4972 if (session
->folder_content_changed
) {
4973 debug_print("CHANGED (self-noop)! scan_required\n");
4974 item
->should_update
= TRUE
;
4975 if (session
->uid_validity
&& session
->uid_validity
!= item
->item
.mtime
) {
4976 item
->item
.mtime
= session
->uid_validity
;
4977 item
->should_trash_cache
= TRUE
;
4979 unlock_session(session
);
4983 debug_print("CHANGED (previous noop)! scan_required\n");
4984 item
->should_update
= TRUE
;
4985 if (session
->uid_validity
&& session
->uid_validity
!= item
->item
.mtime
) {
4986 item
->item
.mtime
= session
->uid_validity
;
4987 item
->should_trash_cache
= TRUE
;
4989 unlock_session(session
);
4993 ok
= imap_status(session
, IMAP_FOLDER(folder
), item
->item
.path
, IMAP_FOLDER_ITEM(item
),
4994 &exists
, &uid_next
, &uid_val
, &unseen
, FALSE
);
4995 if (ok
!= MAILIMAP_NO_ERROR
) {
4999 debug_print("exists %d, item->item.total_msgs %d\n"
5000 "\tunseen %d, item->item.unread_msgs %d\n"
5001 "\tuid_next %d, item->uid_next %d\n"
5002 "\tuid_val %d, item->item.mtime %ld\n",
5003 exists
, item
->item
.total_msgs
, unseen
, item
->item
.unread_msgs
,
5004 uid_next
, item
->uid_next
, uid_val
, (long)(item
->item
.mtime
));
5005 if (exists
!= item
->item
.total_msgs
5006 || unseen
!= item
->item
.unread_msgs
5007 || uid_next
!= item
->uid_next
5008 || uid_val
!= item
->item
.mtime
) {
5009 debug_print("CHANGED (status)! scan_required\n");
5010 item
->last_change
= time(NULL
);
5011 item
->should_update
= TRUE
;
5012 item
->uid_next
= uid_next
;
5013 if (uid_val
!= item
->item
.mtime
) {
5014 item
->item
.mtime
= uid_val
;
5015 item
->should_trash_cache
= TRUE
;
5017 unlock_session(session
);
5021 unlock_session(session
);
5023 item
->should_update
= FALSE
;
5027 void imap_change_flags(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
, MsgPermFlags newflags
)
5029 IMAPSession
*session
;
5030 IMAPFlags flags_set
= 0, flags_unset
= 0;
5031 gint ok
= MAILIMAP_NO_ERROR
;
5032 MsgNumberList numlist
;
5033 hashtable_data
*ht_data
= NULL
;
5035 g_return_if_fail(folder
!= NULL
);
5036 g_return_if_fail(folder
->klass
== &imap_class
);
5037 g_return_if_fail(item
!= NULL
);
5038 g_return_if_fail(item
->folder
== folder
);
5039 g_return_if_fail(msginfo
!= NULL
);
5040 g_return_if_fail(msginfo
->folder
== item
);
5042 if (!MSG_IS_MARKED(msginfo
->flags
) && (newflags
& MSG_MARKED
))
5043 flags_set
|= IMAP_FLAG_FLAGGED
;
5044 if ( MSG_IS_MARKED(msginfo
->flags
) && !(newflags
& MSG_MARKED
))
5045 flags_unset
|= IMAP_FLAG_FLAGGED
;
5047 if (!MSG_IS_UNREAD(msginfo
->flags
) && (newflags
& MSG_UNREAD
))
5048 flags_unset
|= IMAP_FLAG_SEEN
;
5049 if ( MSG_IS_UNREAD(msginfo
->flags
) && !(newflags
& MSG_UNREAD
))
5050 flags_set
|= IMAP_FLAG_SEEN
;
5052 if (!MSG_IS_REPLIED(msginfo
->flags
) && (newflags
& MSG_REPLIED
))
5053 flags_set
|= IMAP_FLAG_ANSWERED
;
5054 if ( MSG_IS_REPLIED(msginfo
->flags
) && !(newflags
& MSG_REPLIED
))
5055 flags_unset
|= IMAP_FLAG_ANSWERED
;
5057 if (!MSG_IS_FORWARDED(msginfo
->flags
) && (newflags
& MSG_FORWARDED
))
5058 flags_set
|= IMAP_FLAG_FORWARDED
;
5059 if ( MSG_IS_FORWARDED(msginfo
->flags
) && !(newflags
& MSG_FORWARDED
))
5060 flags_unset
|= IMAP_FLAG_FORWARDED
;
5062 if (!MSG_IS_SPAM(msginfo
->flags
) && (newflags
& MSG_SPAM
)) {
5063 flags_set
|= IMAP_FLAG_SPAM
;
5064 flags_unset
|= IMAP_FLAG_HAM
;
5066 if ( MSG_IS_SPAM(msginfo
->flags
) && !(newflags
& MSG_SPAM
)) {
5067 flags_set
|= IMAP_FLAG_HAM
;
5068 flags_unset
|= IMAP_FLAG_SPAM
;
5070 if (!MSG_IS_DELETED(msginfo
->flags
) && (newflags
& MSG_DELETED
))
5071 flags_set
|= IMAP_FLAG_DELETED
;
5072 if ( MSG_IS_DELETED(msginfo
->flags
) && !(newflags
& MSG_DELETED
))
5073 flags_unset
|= IMAP_FLAG_DELETED
;
5075 if (!flags_set
&& !flags_unset
) {
5076 /* the changed flags were not translatable to IMAP-speak.
5077 * like MSG_POSTFILTERED, so just apply. */
5078 msginfo
->flags
.perm_flags
= newflags
;
5082 debug_print("getting session...\n");
5083 session
= imap_session_get(folder
);
5088 if ((ok
= imap_select(session
, IMAP_FOLDER(folder
), msginfo
->folder
,
5089 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
)) != MAILIMAP_NO_ERROR
) {
5092 numlist
.next
= NULL
;
5093 numlist
.data
= GINT_TO_POINTER(msginfo
->msgnum
);
5095 if (IMAP_FOLDER_ITEM(item
)->batching
) {
5096 /* instead of performing an UID STORE command for each message change,
5097 * as a lot of them can change "together", we just fill in hashtables
5098 * and defer the treatment so that we're able to send only one
5101 debug_print("IMAP batch mode on, deferring flags change\n");
5103 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->flags_set_table
,
5104 GINT_TO_POINTER(flags_set
));
5105 if (ht_data
== NULL
) {
5106 ht_data
= g_new0(hashtable_data
, 1);
5107 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
5108 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->flags_set_table
,
5109 GINT_TO_POINTER(flags_set
), ht_data
);
5111 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
, GINT_TO_POINTER(msginfo
->msgnum
));
5114 ht_data
= g_hash_table_lookup(IMAP_FOLDER_ITEM(item
)->flags_unset_table
,
5115 GINT_TO_POINTER(flags_unset
));
5116 if (ht_data
== NULL
) {
5117 ht_data
= g_new0(hashtable_data
, 1);
5118 ht_data
->item
= IMAP_FOLDER_ITEM(item
);
5119 g_hash_table_insert(IMAP_FOLDER_ITEM(item
)->flags_unset_table
,
5120 GINT_TO_POINTER(flags_unset
), ht_data
);
5122 ht_data
->msglist
= g_slist_prepend(ht_data
->msglist
,
5123 GINT_TO_POINTER(msginfo
->msgnum
));
5126 debug_print("IMAP changing flags\n");
5128 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
), &numlist
, flags_set
, NULL
, TRUE
);
5129 if (ok
!= MAILIMAP_NO_ERROR
) {
5135 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
), &numlist
, flags_unset
, NULL
, FALSE
);
5136 if (ok
!= MAILIMAP_NO_ERROR
) {
5141 msginfo
->flags
.perm_flags
= newflags
;
5145 static gint
imap_remove_msg(Folder
*folder
, FolderItem
*item
, gint uid
)
5148 IMAPSession
*session
;
5150 MsgNumberList numlist
;
5152 g_return_val_if_fail(folder
!= NULL
, -1);
5153 g_return_val_if_fail(FOLDER_CLASS(folder
) == &imap_class
, -1);
5154 g_return_val_if_fail(item
!= NULL
, -1);
5156 debug_print("getting session...\n");
5157 session
= imap_session_get(folder
);
5158 if (!session
) return -1;
5160 ok
= imap_select(session
, IMAP_FOLDER(folder
), item
,
5161 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5162 if (ok
!= MAILIMAP_NO_ERROR
) {
5165 numlist
.next
= NULL
;
5166 numlist
.data
= GINT_TO_POINTER(uid
);
5168 ok
= imap_set_message_flags
5169 (session
, IMAP_FOLDER_ITEM(item
), &numlist
, IMAP_FLAG_DELETED
, NULL
, TRUE
);
5170 if (ok
!= MAILIMAP_NO_ERROR
) {
5171 log_warning(LOG_PROTOCOL
, _("can't set deleted flags: %d\n"), uid
);
5175 ok
= imap_cmd_expunge(session
);
5177 if (ok
!= MAILIMAP_NO_ERROR
) {
5178 log_warning(LOG_PROTOCOL
, _("can't expunge\n"));
5182 IMAP_FOLDER_ITEM(item
)->uid_list
= g_slist_remove(
5183 IMAP_FOLDER_ITEM(item
)->uid_list
, numlist
.data
);
5184 dir
= folder_item_get_path(item
);
5185 if (is_dir_exist(dir
))
5186 remove_numbered_files(dir
, uid
, uid
);
5188 return MAILIMAP_NO_ERROR
;
5191 static gint
compare_msginfo(gconstpointer a
, gconstpointer b
)
5193 return ((MsgInfo
*)a
)->msgnum
- ((MsgInfo
*)b
)->msgnum
;
5196 static guint
gslist_find_next_num(MsgNumberList
**list
, guint num
)
5200 g_return_val_if_fail(list
!= NULL
, -1);
5202 for (elem
= *list
; elem
!= NULL
; elem
= g_slist_next(elem
))
5203 if (GPOINTER_TO_INT(elem
->data
) >= num
)
5206 return elem
!= NULL
? GPOINTER_TO_INT(elem
->data
) : (gint
)-1;
5209 static gboolean
flag_ok(IMAPFolderItem
*item
, guint flag
)
5211 if (item
->ok_flags
&& g_slist_find(item
->ok_flags
, GUINT_TO_POINTER(flag
))) {
5212 debug_print("flag %d is OK\n", flag
);
5215 if (item
->can_create_flags
== ITEM_CAN_CREATE_FLAGS
) {
5216 debug_print("creating flags is OK\n");
5223 * NEW and DELETED flags are not syncronized
5224 * - The NEW/RECENT flags in IMAP folders can not really be directly
5225 * modified by Claws Mail
5226 * - The DELETE/DELETED flag in IMAP and Claws Mail do not have the same
5227 * meaning, in IMAP it always removes the messages from the FolderItem
5228 * in Claws Mail it can mean to move the message to Trash
5231 typedef struct _get_flags_data
{
5234 MsgInfoList
*msginfo_list
;
5235 GHashTable
*msgflags
;
5236 gboolean full_search
;
5240 static /*gint*/ void *imap_get_flags_thread(void *data
)
5242 get_flags_data
*stuff
= (get_flags_data
*)data
;
5243 Folder
*folder
= stuff
->folder
;
5244 FolderItem
*fitem
= (FolderItem
*) stuff
->item
;
5245 MsgInfoList
*msginfo_list
= stuff
->msginfo_list
;
5246 GHashTable
*msgflags
= stuff
->msgflags
;
5248 carray
* lep_uidtab
;
5249 IMAPSession
*session
;
5251 int r
= MAILIMAP_NO_ERROR
;
5252 GHashTable
*flags_hash
= NULL
;
5253 GHashTable
*tags_hash
= NULL
;
5254 gboolean full_search
= stuff
->full_search
;
5255 GSList
*sorted_list
= NULL
;
5256 GSList
*unseen
= NULL
, *answered
= NULL
, *flagged
= NULL
, *deleted
= NULL
, *forwarded
= NULL
, *spam
= NULL
;
5257 GSList
*seq_list
, *cur
;
5258 gboolean reverse_seen
= FALSE
;
5259 gboolean selected_folder
;
5260 gint exists_cnt
, unseen_cnt
;
5261 gboolean got_alien_tags
= FALSE
;
5263 session
= imap_session_get(folder
);
5265 if (session
== NULL
) {
5267 return GINT_TO_POINTER(-1);
5269 selected_folder
= (session
->mbox
!= NULL
) &&
5270 (!strcmp(session
->mbox
, fitem
->path
));
5272 lock_session(session
);
5273 if (!selected_folder
) {
5274 ok
= imap_select(session
, IMAP_FOLDER(folder
), fitem
,
5275 &exists_cnt
, NULL
, &unseen_cnt
, NULL
, NULL
, TRUE
);
5276 if (ok
!= MAILIMAP_NO_ERROR
) {
5278 return GINT_TO_POINTER(-1);
5281 if (unseen_cnt
> exists_cnt
/ 2)
5282 reverse_seen
= TRUE
;
5285 if (fitem
->unread_msgs
> fitem
->total_msgs
/ 2)
5286 reverse_seen
= TRUE
;
5289 sorted_list
= g_slist_sort(g_slist_copy(msginfo_list
), compare_msginfo
);
5291 seq_list
= imap_get_lep_set_from_msglist(IMAP_FOLDER(folder
), msginfo_list
);
5293 struct mailimap_set
* set
;
5294 set
= mailimap_set_new_interval(1, 0);
5295 seq_list
= g_slist_append(NULL
, set
);
5298 if (folder
->account
&& folder
->account
->low_bandwidth
) {
5299 for (cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
5300 struct mailimap_set
* imapset
;
5301 clist
* lep_uidlist
;
5304 imapset
= cur
->data
;
5306 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SEEN
, NULL
,
5307 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5310 r
= imap_threaded_search(folder
,
5311 IMAP_SEARCH_TYPE_UNSEEN
, NULL
,
5312 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5314 if (r
== MAILIMAP_NO_ERROR
) {
5317 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5318 mailimap_search_result_free(lep_uidlist
);
5320 unseen
= g_slist_concat(unseen
, uidlist
);
5322 imap_handle_error(SESSION(session
), NULL
, r
);
5326 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_FLAGGED
, NULL
,
5327 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5328 if (r
== MAILIMAP_NO_ERROR
) {
5331 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5332 mailimap_search_result_free(lep_uidlist
);
5334 flagged
= g_slist_concat(flagged
, uidlist
);
5336 imap_handle_error(SESSION(session
), NULL
, r
);
5340 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5341 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_ANSWERED
, NULL
,
5342 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5343 if (r
== MAILIMAP_NO_ERROR
) {
5346 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5347 mailimap_search_result_free(lep_uidlist
);
5349 answered
= g_slist_concat(answered
, uidlist
);
5351 imap_handle_error(SESSION(session
), NULL
, r
);
5355 if (flag_ok(IMAP_FOLDER_ITEM(fitem
), IMAP_FLAG_FORWARDED
)) {
5356 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_FORWARDED
, NULL
,
5357 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5358 if (r
== MAILIMAP_NO_ERROR
) {
5361 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5362 mailimap_search_result_free(lep_uidlist
);
5364 forwarded
= g_slist_concat(forwarded
, uidlist
);
5366 imap_handle_error(SESSION(session
), NULL
, r
);
5371 if (flag_ok(IMAP_FOLDER_ITEM(fitem
), IMAP_FLAG_SPAM
)) {
5372 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_SPAM
, NULL
,
5373 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5374 if (r
== MAILIMAP_NO_ERROR
) {
5377 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5378 mailimap_search_result_free(lep_uidlist
);
5380 spam
= g_slist_concat(spam
, uidlist
);
5382 imap_handle_error(SESSION(session
), NULL
, r
);
5387 r
= imap_threaded_search(folder
, IMAP_SEARCH_TYPE_DELETED
, NULL
,
5388 NULL
, full_search
? NULL
:imapset
, &lep_uidlist
);
5389 if (r
== MAILIMAP_NO_ERROR
) {
5392 uidlist
= imap_uid_list_from_lep(lep_uidlist
, NULL
);
5393 mailimap_search_result_free(lep_uidlist
);
5395 deleted
= g_slist_concat(deleted
, uidlist
);
5397 imap_handle_error(SESSION(session
), NULL
, r
);
5404 r
= imap_threaded_fetch_uid_flags(folder
, 1, &lep_uidtab
);
5405 if (r
== MAILIMAP_NO_ERROR
) {
5406 flags_hash
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
, NULL
);
5407 tags_hash
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
, NULL
);
5408 imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab
, flags_hash
, tags_hash
);
5409 imap_fetch_uid_flags_list_free(lep_uidtab
);
5411 imap_handle_error(SESSION(session
), NULL
, r
);
5417 if (r
== MAILIMAP_NO_ERROR
)
5418 unlock_session(session
);
5420 for (elem
= sorted_list
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
5422 MsgPermFlags flags
, oldflags
;
5425 msginfo
= (MsgInfo
*) elem
->data
;
5426 flags
= msginfo
->flags
.perm_flags
;
5427 wasnew
= (flags
& MSG_NEW
);
5428 oldflags
= flags
& ~(MSG_NEW
|MSG_UNREAD
|MSG_REPLIED
|MSG_FORWARDED
|MSG_MARKED
|MSG_DELETED
|MSG_SPAM
);
5430 if (folder
->account
&& folder
->account
->low_bandwidth
) {
5431 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5432 flags
&= ~((reverse_seen
? 0 : MSG_UNREAD
| MSG_NEW
) | MSG_REPLIED
| MSG_FORWARDED
| MSG_MARKED
| MSG_SPAM
);
5434 flags
&= ~((reverse_seen
? 0 : MSG_UNREAD
| MSG_NEW
| MSG_MARKED
));
5437 flags
|= MSG_UNREAD
| (wasnew
? MSG_NEW
: 0);
5438 if (gslist_find_next_num(&unseen
, msginfo
->msgnum
) == msginfo
->msgnum
) {
5439 if (!reverse_seen
) {
5440 flags
|= MSG_UNREAD
| (wasnew
? MSG_NEW
: 0);
5442 flags
&= ~(MSG_UNREAD
| MSG_NEW
);
5446 if (gslist_find_next_num(&flagged
, msginfo
->msgnum
) == msginfo
->msgnum
)
5447 flags
|= MSG_MARKED
;
5449 flags
&= ~MSG_MARKED
;
5451 if (fitem
->opened
|| fitem
->processing_pending
|| fitem
== folder
->inbox
) {
5452 if (gslist_find_next_num(&answered
, msginfo
->msgnum
) == msginfo
->msgnum
)
5453 flags
|= MSG_REPLIED
;
5455 flags
&= ~MSG_REPLIED
;
5456 if (gslist_find_next_num(&forwarded
, msginfo
->msgnum
) == msginfo
->msgnum
)
5457 flags
|= MSG_FORWARDED
;
5459 flags
&= ~MSG_FORWARDED
;
5460 if (gslist_find_next_num(&spam
, msginfo
->msgnum
) == msginfo
->msgnum
)
5464 if (gslist_find_next_num(&deleted
, msginfo
->msgnum
) == msginfo
->msgnum
)
5465 flags
|= MSG_DELETED
;
5467 flags
&= ~MSG_DELETED
;
5470 if (flags_hash
!= NULL
) {
5472 flags
= GPOINTER_TO_INT(g_hash_table_lookup(flags_hash
,
5473 GINT_TO_POINTER(msginfo
->msgnum
)));
5476 if ((flags
& MSG_UNREAD
) == 0)
5482 if (tags_hash
!= NULL
) {
5483 GSList
*tags
= g_hash_table_lookup(tags_hash
, GINT_TO_POINTER(msginfo
->msgnum
));
5486 g_slist_free(msginfo
->tags
);
5487 msginfo
->tags
= NULL
;
5489 for (cur
= tags
; cur
; cur
= cur
->next
) {
5490 gchar
*real_tag
= imap_modified_utf7_to_utf8(cur
->data
, TRUE
);
5492 id
= tags_get_id_for_str(real_tag
);
5494 id
= tags_add_tag(real_tag
);
5495 got_alien_tags
= TRUE
;
5497 msginfo
->tags
= g_slist_append(
5499 GINT_TO_POINTER(id
));
5502 slist_free_strings_full(tags
);
5506 g_hash_table_insert(msgflags
, msginfo
, GINT_TO_POINTER(flags
));
5509 if (got_alien_tags
) {
5511 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
5515 g_hash_table_destroy(flags_hash
);
5517 g_hash_table_destroy(tags_hash
);
5519 imap_lep_set_free(seq_list
);
5520 g_slist_free(flagged
);
5521 g_slist_free(deleted
);
5522 g_slist_free(answered
);
5523 g_slist_free(forwarded
);
5525 g_slist_free(unseen
);
5526 g_slist_free(sorted_list
);
5529 return GINT_TO_POINTER(0);
5532 static gint
imap_get_flags(Folder
*folder
, FolderItem
*item
,
5533 MsgInfoList
*msginfo_list
, GHashTable
*msgflags
)
5536 get_flags_data
*data
= g_new0(get_flags_data
, 1);
5538 data
->folder
= folder
;
5540 data
->msginfo_list
= msginfo_list
;
5541 data
->msgflags
= msgflags
;
5542 data
->full_search
= FALSE
;
5544 GSList
*tmp
= NULL
, *cur
;
5546 if (prefs_common
.work_offline
&&
5547 !inc_offline_should_override(FALSE
,
5548 _("Claws Mail needs network access in order "
5549 "to access the IMAP server."))) {
5554 tmp
= folder_item_get_msg_list(item
);
5556 if (g_slist_length(tmp
) <= g_slist_length(msginfo_list
))
5557 data
->full_search
= TRUE
;
5559 for (cur
= tmp
; cur
; cur
= cur
->next
)
5560 procmsg_msginfo_free((MsgInfo
**)&(cur
->data
));
5564 result
= GPOINTER_TO_INT(imap_get_flags_thread(data
));
5571 static gboolean
process_flags(gpointer key
, gpointer value
, gpointer user_data
)
5573 gboolean flags_set
= GPOINTER_TO_INT(user_data
);
5574 gint flags_value
= GPOINTER_TO_INT(key
);
5575 hashtable_data
*data
= (hashtable_data
*)value
;
5576 IMAPFolderItem
*_item
= data
->item
;
5577 FolderItem
*item
= (FolderItem
*)_item
;
5578 gint ok
= MAILIMAP_ERROR_BAD_STATE
;
5579 IMAPSession
*session
= NULL
;
5581 debug_print("getting session...\n");
5582 session
= imap_session_get(item
->folder
);
5584 data
->msglist
= g_slist_reverse(data
->msglist
);
5586 debug_print("IMAP %ssetting flags to %d for %d messages\n",
5589 g_slist_length(data
->msglist
));
5591 lock_session(session
);
5593 ok
= imap_select(session
, IMAP_FOLDER(item
->folder
), item
,
5594 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5596 if (ok
== MAILIMAP_NO_ERROR
) {
5597 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
),
5598 data
->msglist
, flags_value
, NULL
, flags_set
);
5600 g_warning("can't select mailbox %s", item
->path
);
5604 unlock_session(session
);
5606 g_slist_free(data
->msglist
);
5611 static gboolean
process_tags(gpointer key
, gpointer value
, gpointer user_data
)
5613 gboolean tags_set
= GPOINTER_TO_INT(user_data
);
5614 TagsData
*data
= (TagsData
*)value
;
5615 IMAPFolderItem
*_item
= data
->item
;
5616 FolderItem
*item
= (FolderItem
*)_item
;
5617 gchar
*str
= data
->str
;
5618 gint ok
= MAILIMAP_ERROR_BAD_STATE
;
5619 IMAPSession
*session
= NULL
;
5621 debug_print("getting session...\n");
5622 session
= imap_session_get(item
->folder
);
5624 data
->msglist
= g_slist_reverse(data
->msglist
);
5626 debug_print("IMAP %ssetting tags %s for %d messages\n",
5629 g_slist_length(data
->msglist
));
5631 lock_session(session
);
5633 ok
= imap_select(session
, IMAP_FOLDER(item
->folder
), item
,
5634 NULL
, NULL
, NULL
, NULL
, NULL
, FALSE
);
5636 if (ok
== MAILIMAP_NO_ERROR
) {
5640 ok
= imap_set_message_flags(session
, IMAP_FOLDER_ITEM(item
),
5641 data
->msglist
, 0, &list
, tags_set
);
5643 g_warning("can't select mailbox %s", item
->path
);
5647 unlock_session(session
);
5649 g_slist_free(data
->msglist
);
5655 static void process_hashtable(IMAPFolderItem
*item
)
5657 if (item
->flags_set_table
) {
5658 g_hash_table_foreach_remove(item
->flags_set_table
, process_flags
, GINT_TO_POINTER(TRUE
));
5659 g_hash_table_destroy(item
->flags_set_table
);
5660 item
->flags_set_table
= NULL
;
5662 if (item
->flags_unset_table
) {
5663 g_hash_table_foreach_remove(item
->flags_unset_table
, process_flags
, GINT_TO_POINTER(FALSE
));
5664 g_hash_table_destroy(item
->flags_unset_table
);
5665 item
->flags_unset_table
= NULL
;
5667 if (item
->tags_set_table
) {
5668 g_hash_table_foreach_remove(item
->tags_set_table
, process_tags
, GINT_TO_POINTER(TRUE
));
5669 g_hash_table_destroy(item
->tags_set_table
);
5670 item
->tags_set_table
= NULL
;
5672 if (item
->tags_unset_table
) {
5673 g_hash_table_foreach_remove(item
->tags_unset_table
, process_tags
, GINT_TO_POINTER(FALSE
));
5674 g_hash_table_destroy(item
->tags_unset_table
);
5675 item
->tags_unset_table
= NULL
;
5680 static void imap_set_batch (Folder
*folder
, FolderItem
*_item
, gboolean batch
)
5682 IMAPFolderItem
*item
= (IMAPFolderItem
*)_item
;
5683 IMAPSession
*session
;
5685 g_return_if_fail(item
!= NULL
);
5687 if (item
->batching
== batch
)
5691 item
->batching
= TRUE
;
5692 debug_print("IMAP switching to batch mode\n");
5693 if (!item
->flags_set_table
) {
5694 item
->flags_set_table
= g_hash_table_new(NULL
, g_direct_equal
);
5696 if (!item
->flags_unset_table
) {
5697 item
->flags_unset_table
= g_hash_table_new(NULL
, g_direct_equal
);
5699 if (!item
->tags_set_table
) {
5700 item
->tags_set_table
= g_hash_table_new(NULL
, g_direct_equal
);
5702 if (!item
->tags_unset_table
) {
5703 item
->tags_unset_table
= g_hash_table_new(NULL
, g_direct_equal
);
5705 session
= imap_session_get(folder
);
5707 imap_refresh_sensitivity(session
);
5708 session
->sens_update_block
= TRUE
;
5711 debug_print("IMAP switching away from batch mode\n");
5713 process_hashtable(item
);
5714 item
->batching
= FALSE
;
5715 session
= imap_session_get(folder
);
5717 session
->sens_update_block
= FALSE
;
5718 imap_refresh_sensitivity(session
);
5725 /* data types conversion libetpan <-> claws */
5729 #define ETPAN_IMAP_MB_MARKED 1
5730 #define ETPAN_IMAP_MB_UNMARKED 2
5731 #define ETPAN_IMAP_MB_NOSELECT 4
5732 #define ETPAN_IMAP_MB_NOINFERIORS 8
5734 static int imap_flags_to_flags(struct mailimap_mbx_list_flags
* imap_flags
)
5740 if (imap_flags
->mbf_type
== MAILIMAP_MBX_LIST_FLAGS_SFLAG
) {
5741 switch (imap_flags
->mbf_sflag
) {
5742 case MAILIMAP_MBX_LIST_SFLAG_MARKED
:
5743 flags
|= ETPAN_IMAP_MB_MARKED
;
5745 case MAILIMAP_MBX_LIST_SFLAG_NOSELECT
:
5746 flags
|= ETPAN_IMAP_MB_NOSELECT
;
5748 case MAILIMAP_MBX_LIST_SFLAG_UNMARKED
:
5749 flags
|= ETPAN_IMAP_MB_UNMARKED
;
5754 if (imap_flags
->mbf_oflags
) {
5755 for(cur
= clist_begin(imap_flags
->mbf_oflags
) ; cur
!= NULL
;
5756 cur
= clist_next(cur
)) {
5757 struct mailimap_mbx_list_oflag
* oflag
;
5759 oflag
= clist_content(cur
);
5761 switch (oflag
->of_type
) {
5762 case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS
:
5763 flags
|= ETPAN_IMAP_MB_NOINFERIORS
;
5771 static GSList
* imap_list_from_lep(IMAPFolder
* folder
,
5772 clist
* list
, const gchar
* real_path
, gboolean all
)
5775 GSList
* item_list
= NULL
, *llast
= NULL
;
5778 for(iter
= clist_begin(list
) ; iter
!= NULL
;
5779 iter
= clist_next(iter
)) {
5780 struct mailimap_mailbox_list
* mb
;
5788 FolderItem
*new_item
;
5790 mb
= clist_content(iter
);
5796 if (mb
->mb_flag
!= NULL
)
5797 flags
= imap_flags_to_flags(mb
->mb_flag
);
5799 delimiter
= mb
->mb_delimiter
;
5802 dup_name
= strdup(name
);
5803 if (delimiter
!= '\0')
5804 subst_char(dup_name
, delimiter
, '/');
5806 base
= g_path_get_basename(dup_name
);
5807 if (base
[0] == '.') {
5812 if (!all
&& path_cmp(name
, real_path
) == 0) {
5818 if (!all
&& dup_name
[strlen(dup_name
)-1] == '/') {
5824 loc_name
= imap_modified_utf7_to_utf8(base
, FALSE
);
5825 loc_path
= imap_modified_utf7_to_utf8(dup_name
, FALSE
);
5827 new_item
= folder_item_new(FOLDER(folder
), loc_name
, loc_path
);
5828 if ((flags
& ETPAN_IMAP_MB_NOINFERIORS
) != 0)
5829 new_item
->no_sub
= TRUE
;
5830 if (strcasecmp(dup_name
, "INBOX") != 0 &&
5831 ((flags
& ETPAN_IMAP_MB_NOSELECT
) != 0))
5832 new_item
->no_select
= TRUE
;
5834 if (item_list
== NULL
)
5835 llast
= item_list
= g_slist_append(item_list
, new_item
);
5837 llast
= g_slist_append(llast
, new_item
);
5838 llast
= llast
->next
;
5840 debug_print("folder '%s' found.\n", loc_path
);
5851 static GSList
* imap_get_lep_set_from_numlist(IMAPFolder
*folder
, MsgNumberList
*numlist
)
5853 GSList
*sorted_list
, *cur
;
5854 guint first
, last
, next
;
5855 GSList
*ret_list
= NULL
, *llast
= NULL
;
5856 struct mailimap_set
* current_set
;
5857 unsigned int item_count
;
5859 if (numlist
== NULL
)
5862 current_set
= mailimap_set_new_empty();
5864 sorted_list
= g_slist_copy(numlist
);
5865 sorted_list
= g_slist_sort(sorted_list
, g_int_compare
);
5867 first
= GPOINTER_TO_INT(sorted_list
->data
);
5870 for (cur
= sorted_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
5871 if (GPOINTER_TO_INT(cur
->data
) == 0)
5876 last
= GPOINTER_TO_INT(cur
->data
);
5878 next
= GPOINTER_TO_INT(cur
->next
->data
);
5882 if (last
+ 1 != next
|| next
== 0 || item_count
>= folder
->max_set_size
) {
5884 struct mailimap_set_item
* item
;
5885 item
= mailimap_set_item_new(first
, last
);
5886 mailimap_set_add(current_set
, item
);
5890 if (item_count
>= folder
->max_set_size
) {
5891 if (ret_list
== NULL
)
5892 llast
= ret_list
= g_slist_append(ret_list
,
5895 llast
= g_slist_append(llast
, current_set
);
5896 llast
= llast
->next
;
5899 current_set
= mailimap_set_new_empty();
5905 if (clist_count(current_set
->set_list
) > 0) {
5906 ret_list
= g_slist_append(ret_list
,
5910 g_slist_free(sorted_list
);
5915 static GSList
* imap_get_lep_set_from_msglist(IMAPFolder
*folder
, MsgInfoList
*msglist
)
5917 MsgNumberList
*numlist
= NULL
;
5920 numlist
= procmsg_get_number_list_for_msgs(msglist
);
5922 seq_list
= imap_get_lep_set_from_numlist(folder
, numlist
);
5923 g_slist_free(numlist
);
5928 static GSList
* imap_uid_list_from_lep(clist
* list
, gint
* length
)
5937 for(iter
= clist_begin(list
) ; iter
!= NULL
;
5938 iter
= clist_next(iter
)) {
5941 puid
= clist_content(iter
);
5942 result
= g_slist_prepend(result
, GINT_TO_POINTER(* puid
));
5945 result
= g_slist_reverse(result
);
5952 static GSList
* imap_uid_list_from_lep_tab(carray
* list
)
5959 for(i
= 0 ; i
< carray_count(list
) ; i
++) {
5962 puid
= carray_get(list
, i
);
5963 result
= g_slist_prepend(result
, GINT_TO_POINTER(* puid
));
5965 result
= g_slist_reverse(result
);
5969 static void imap_flags_hash_from_lep_uid_flags_tab(carray
* list
,
5971 GHashTable
* tags_hash
)
5975 for(i
= 0 ; i
< carray_count(list
) ; i
+= 3) {
5980 puid
= carray_get(list
, i
);
5981 pflags
= carray_get(list
, i
+ 1);
5982 tags
= carray_get(list
, i
+ 2);
5984 g_hash_table_insert(hash
, GINT_TO_POINTER(*puid
), GINT_TO_POINTER(* pflags
));
5985 g_hash_table_insert(tags_hash
, GINT_TO_POINTER(*puid
), tags
);
5989 static MsgInfo
*imap_envelope_from_lep(struct imap_fetch_env_info
* info
,
5992 MsgInfo
*msginfo
= NULL
;
5995 MsgFlags flags
= {0, 0};
5997 if (info
->headers
== NULL
)
6000 MSG_SET_TMP_FLAGS(flags
, MSG_IMAP
);
6001 if (folder_has_parent_of_type(item
, F_QUEUE
)) {
6002 MSG_SET_TMP_FLAGS(flags
, MSG_QUEUED
);
6003 } else if (folder_has_parent_of_type(item
, F_DRAFT
)) {
6004 MSG_SET_TMP_FLAGS(flags
, MSG_DRAFT
);
6006 flags
.perm_flags
= info
->flags
;
6009 size
= (goffset
) info
->size
;
6010 msginfo
= procheader_parse_str(info
->headers
, flags
, FALSE
, FALSE
);
6013 msginfo
->msgnum
= uid
;
6014 msginfo
->size
= size
;
6020 static void imap_lep_set_free(GSList
*seq_list
)
6024 for(cur
= seq_list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
6025 struct mailimap_set
* imapset
;
6027 imapset
= cur
->data
;
6028 mailimap_set_free(imapset
);
6030 g_slist_free(seq_list
);
6033 static struct mailimap_flag_list
* imap_flag_to_lep(IMAPFolderItem
*item
, IMAPFlags flags
, GSList
*tags
)
6035 struct mailimap_flag_list
* flag_list
;
6038 flag_list
= mailimap_flag_list_new_empty();
6040 if (IMAP_IS_SEEN(flags
))
6041 mailimap_flag_list_add(flag_list
,
6042 mailimap_flag_new_seen());
6043 if (IMAP_IS_ANSWERED(flags
))
6044 mailimap_flag_list_add(flag_list
,
6045 mailimap_flag_new_answered());
6046 if (IMAP_IS_FLAGGED(flags
))
6047 mailimap_flag_list_add(flag_list
,
6048 mailimap_flag_new_flagged());
6049 if (IMAP_IS_DELETED(flags
))
6050 mailimap_flag_list_add(flag_list
,
6051 mailimap_flag_new_deleted());
6052 if (IMAP_IS_DRAFT(flags
))
6053 mailimap_flag_list_add(flag_list
,
6054 mailimap_flag_new_draft());
6055 if (IMAP_IS_FORWARDED(flags
) && flag_ok(item
, IMAP_FLAG_FORWARDED
))
6056 mailimap_flag_list_add(flag_list
,
6057 mailimap_flag_new_flag_keyword(strdup(RTAG_FORWARDED
)));
6058 if (IMAP_IS_SPAM(flags
) && flag_ok(item
, IMAP_FLAG_SPAM
))
6059 mailimap_flag_list_add(flag_list
,
6060 mailimap_flag_new_flag_keyword(strdup(RTAG_JUNK
)));
6061 else if (IMAP_IS_HAM(flags
) && flag_ok(item
, IMAP_FLAG_HAM
))
6062 mailimap_flag_list_add(flag_list
,
6063 mailimap_flag_new_flag_keyword(strdup(RTAG_NON_JUNK
)));
6065 for (; cur
; cur
= cur
->next
) {
6067 imap_utf8_to_modified_utf7(cur
->data
, TRUE
);
6068 g_strstrip(enc_str
);
6070 mailimap_flag_list_add(flag_list
,
6071 mailimap_flag_new_flag_keyword(enc_str
));
6077 guint
imap_folder_get_refcnt(Folder
*folder
)
6079 return ((IMAPFolder
*)folder
)->refcnt
;
6082 void imap_folder_ref(Folder
*folder
)
6084 ((IMAPFolder
*)folder
)->refcnt
++;
6087 void imap_disconnect_all(gboolean have_connectivity
)
6090 gboolean short_timeout
;
6091 #ifdef HAVE_NETWORKMANAGER_SUPPORT
6092 GError
*error
= NULL
;
6094 short_timeout
= !networkmanager_is_online(&error
);
6096 short_timeout
= TRUE
;
6097 g_error_free(error
);
6100 short_timeout
= TRUE
;
6104 imap_main_set_timeout(1);
6106 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
6107 PrefsAccount
*account
= list
->data
;
6108 if (account
->protocol
== A_IMAP4
) {
6109 RemoteFolder
*folder
= (RemoteFolder
*)account
->folder
;
6110 if (folder
&& folder
->session
) {
6111 if (imap_is_busy(FOLDER(folder
)))
6112 imap_threaded_cancel(FOLDER(folder
));
6114 IMAPSession
*session
= (IMAPSession
*)folder
->session
;
6115 if (have_connectivity
)
6116 imap_threaded_disconnect(FOLDER(folder
));
6117 SESSION(session
)->state
= SESSION_DISCONNECTED
;
6118 SESSION(session
)->sock
= NULL
;
6119 imap_safe_destroy(session
);
6120 folder
->session
= NULL
;
6126 imap_main_set_timeout(prefs_common
.io_timeout_secs
);
6129 void imap_folder_unref(Folder
*folder
)
6131 if (((IMAPFolder
*)folder
)->refcnt
> 0)
6132 ((IMAPFolder
*)folder
)->refcnt
--;
6135 void imap_cancel_all(void)
6140 folderlist
= folder_get_list();
6141 for (cur
= folderlist
; cur
!= NULL
; cur
= g_list_next(cur
)) {
6142 Folder
*folder
= (Folder
*) cur
->data
;
6144 if (folder
->klass
== &imap_class
) {
6145 if (imap_is_busy(folder
)) {
6146 IMAPSession
*imap_session
;
6147 RemoteFolder
*rfolder
;
6149 g_printerr("cancelled\n");
6150 imap_threaded_cancel(folder
);
6151 rfolder
= (RemoteFolder
*) folder
;
6152 imap_session
= (IMAPSession
*) rfolder
->session
;
6154 imap_session
->cancelled
= 1;
6160 gboolean
imap_cancel_all_enabled(void)
6165 folderlist
= folder_get_list();
6166 for (cur
= folderlist
; cur
!= NULL
; cur
= g_list_next(cur
)) {
6167 Folder
*folder
= (Folder
*) cur
->data
;
6169 if (folder
->klass
== &imap_class
) {
6170 if (imap_is_busy(folder
)) {
6179 static gboolean
imap_is_busy(Folder
*folder
)
6181 IMAPSession
*imap_session
;
6182 RemoteFolder
*rfolder
;
6184 rfolder
= (RemoteFolder
*) folder
;
6185 imap_session
= (IMAPSession
*) rfolder
->session
;
6186 if (imap_session
== NULL
)
6189 return imap_session
->busy
;
6192 #else /* HAVE_LIBETPAN */
6194 static FolderClass imap_class
;
6196 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
);
6197 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
);
6199 static Folder
*imap_folder_new (const gchar
*name
,
6202 static gboolean missing_imap_warning
= TRUE
;
6203 if (missing_imap_warning
) {
6204 missing_imap_warning
= FALSE
;
6206 _("You have one or more IMAP accounts "
6207 "defined. However this version of "
6208 "Claws Mail has been built without "
6209 "IMAP support; your IMAP accounts are "
6211 "You probably need to "
6212 "install libetpan and recompile "
6217 static gint
imap_create_tree (Folder
*folder
)
6221 static FolderItem
*imap_create_folder (Folder
*folder
,
6227 static gint
imap_rename_folder (Folder
*folder
,
6234 gchar
imap_get_path_separator_for_item(FolderItem
*item
)
6239 FolderClass
*imap_get_class(void)
6241 if (imap_class
.idstr
== NULL
) {
6242 imap_class
.type
= F_IMAP
;
6243 imap_class
.idstr
= "imap";
6244 imap_class
.uistr
= "IMAP";
6246 imap_class
.new_folder
= imap_folder_new
;
6247 imap_class
.create_tree
= imap_create_tree
;
6248 imap_class
.create_folder
= imap_create_folder
;
6249 imap_class
.rename_folder
= imap_rename_folder
;
6251 imap_class
.set_xml
= folder_set_xml
;
6252 imap_class
.get_xml
= folder_get_xml
;
6253 imap_class
.item_set_xml
= imap_item_set_xml
;
6254 imap_class
.item_get_xml
= imap_item_get_xml
;
6255 /* nothing implemented */
6261 void imap_disconnect_all(gboolean have_connectivity
)
6265 gint
imap_subscribe(Folder
*folder
, FolderItem
*item
, gchar
*rpath
, gboolean sub
)
6270 GList
* imap_scan_subtree(Folder
*folder
, FolderItem
*item
, gboolean unsubs_only
, gboolean recursive
)
6275 void imap_cache_msg(FolderItem
*item
, gint msgnum
)
6279 void imap_cancel_all(void)
6283 gboolean
imap_cancel_all_enabled(void)
6290 #ifdef HAVE_LIBETPAN
6291 static void imap_synchronise(FolderItem
*item
, gint days
)
6293 if (IMAP_FOLDER_ITEM(item
)->last_sync
== IMAP_FOLDER_ITEM(item
)->last_change
) {
6294 debug_print("%s already synced\n", item
->path
?item
->path
:item
->name
);
6297 debug_print("syncing %s\n", item
->path
?item
->path
:item
->name
);
6298 imap_gtk_synchronise(item
, days
);
6299 IMAP_FOLDER_ITEM(item
)->last_sync
= IMAP_FOLDER_ITEM(item
)->last_change
;
6303 static void imap_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
)
6305 #ifdef HAVE_LIBETPAN
6308 folder_item_set_xml(folder
, item
, tag
);
6310 #ifdef HAVE_LIBETPAN
6311 for (cur
= tag
->attr
; cur
!= NULL
; cur
= g_list_next(cur
)) {
6312 XMLAttr
*attr
= (XMLAttr
*) cur
->data
;
6314 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
6315 if (!strcmp(attr
->name
, "uidnext"))
6316 IMAP_FOLDER_ITEM(item
)->uid_next
= atoi(attr
->value
);
6317 if (!strcmp(attr
->name
, "last_sync"))
6318 IMAP_FOLDER_ITEM(item
)->last_sync
= atoi(attr
->value
);
6319 if (!strcmp(attr
->name
, "last_change"))
6320 IMAP_FOLDER_ITEM(item
)->last_change
= atoi(attr
->value
);
6322 if (IMAP_FOLDER_ITEM(item
)->last_change
== 0)
6323 IMAP_FOLDER_ITEM(item
)->last_change
= time(NULL
);
6327 static XMLTag
*imap_item_get_xml(Folder
*folder
, FolderItem
*item
)
6331 tag
= folder_item_get_xml(folder
, item
);
6333 #ifdef HAVE_LIBETPAN
6334 xml_tag_add_attr(tag
, xml_attr_new_int("uidnext",
6335 IMAP_FOLDER_ITEM(item
)->uid_next
));
6336 xml_tag_add_attr(tag
, xml_attr_new_int("last_sync",
6337 IMAP_FOLDER_ITEM(item
)->last_sync
));
6338 xml_tag_add_attr(tag
, xml_attr_new_int("last_change",
6339 IMAP_FOLDER_ITEM(item
)->last_change
));
6345 /* ===================================================================
6346 * UTF-7 conversion routines as in RFC 2192
6347 * ===================================================================
6348 * These two functions from:
6350 * Copyright (C) 2003-2004 Pawel Salek. */
6352 /* UTF7 modified base64 alphabet */
6353 static char base64chars
[] =
6354 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
6355 #define UNDEFINED 64
6357 /* UTF16 definitions */
6358 #define UTF16MASK 0x03FFUL
6359 #define UTF16SHIFT 10
6360 #define UTF16BASE 0x10000UL
6361 #define UTF16HIGHSTART 0xD800UL
6362 #define UTF16HIGHEND 0xDBFFUL
6363 #define UTF16LOSTART 0xDC00UL
6364 #define UTF16LOEND 0xDFFFUL
6367 /* Convert an IMAP mailbox to a UTF-8 string.
6368 * dst needs to have roughly 4 times the storage space of src
6369 * Hex encoding can triple the size of the input
6370 * UTF-7 can be slightly denser than UTF-8
6371 * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
6373 char* imap_modified_utf7_to_utf8(const char *mbox
, gboolean change_spaces
)
6375 unsigned c
, i
, bitcount
;
6376 unsigned long ucs4
, utf16
, bitbuf
;
6377 unsigned char base64
[256];
6379 char *dst
, *res
= g_malloc(2*strlen(mbox
)+1);
6383 if(!dst
) return NULL
;
6384 /* initialize modified base64 decoding table */
6385 memset(base64
, UNDEFINED
, sizeof (base64
));
6386 for (i
= 0; i
< sizeof (base64chars
); ++i
) {
6387 base64
[(unsigned)base64chars
[i
]] = i
;
6390 /* loop until end of string */
6391 while (*src
!= '\0') {
6393 /* deal with literal characters and &- */
6394 if (c
!= '&' || *src
== '-') {
6395 /* encode literally */
6396 if (change_spaces
&& c
== '_')
6400 /* skip over the '-' if this is an &- sequence */
6401 if (c
== '&') ++src
;
6403 /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
6407 while ((c
= base64
[(unsigned char) *src
]) != UNDEFINED
) {
6409 bitbuf
= (bitbuf
<< 6) | c
;
6411 /* enough bits for a UTF-16 character? */
6412 if (bitcount
>= 16) {
6414 utf16
= (bitcount
? bitbuf
>> bitcount
6416 /* convert UTF16 to UCS4 */
6418 (utf16
>= UTF16HIGHSTART
&& utf16
<= UTF16HIGHEND
) {
6419 ucs4
= (utf16
- UTF16HIGHSTART
) << UTF16SHIFT
;
6422 (utf16
>= UTF16LOSTART
&& utf16
<= UTF16LOEND
) {
6423 ucs4
+= utf16
- UTF16LOSTART
+ UTF16BASE
;
6428 /* convert UTF-16 range of UCS4 to UTF-8 */
6429 if (ucs4
<= 0x7fUL
) {
6432 } else if (ucs4
<= 0x7ffUL
) {
6433 dst
[0] = 0xc0 | (ucs4
>> 6);
6434 dst
[1] = 0x80 | (ucs4
& 0x3f);
6436 } else if (ucs4
<= 0xffffUL
) {
6437 dst
[0] = 0xe0 | (ucs4
>> 12);
6438 dst
[1] = 0x80 | ((ucs4
>> 6) & 0x3f);
6439 dst
[2] = 0x80 | (ucs4
& 0x3f);
6442 dst
[0] = 0xf0 | (ucs4
>> 18);
6443 dst
[1] = 0x80 | ((ucs4
>> 12) & 0x3f);
6444 dst
[2] = 0x80 | ((ucs4
>> 6) & 0x3f);
6445 dst
[3] = 0x80 | (ucs4
& 0x3f);
6450 /* skip over trailing '-' in modified UTF-7 encoding */
6451 if (*src
== '-') ++src
;
6454 /* terminate destination string */
6459 /* Convert hex coded UTF-8 string to modified UTF-7 IMAP mailbox
6460 * dst should be about twice the length of src to deal with non-hex
6463 char* imap_utf8_to_modified_utf7(const char *src
, gboolean change_spaces
)
6465 unsigned int utf8pos
, utf8total
, c
, utf7mode
, bitstogo
, utf16flag
;
6466 unsigned long ucs4
= 0, bitbuf
= 0;
6468 /* initialize hex lookup table */
6471 if (!src
) return NULL
;
6473 res
= malloc(2*strlen(src
)+1);
6475 if(!dst
) return NULL
;
6481 while ((c
= (unsigned char)*src
) != '\0') {
6483 /* normal character? */
6484 if (c
>= ' ' && c
<= '~' && (c
!= '_' || !change_spaces
)) {
6485 /* switch out of UTF-7 mode */
6488 *dst
++ = base64chars
[(bitbuf
<< (6 - bitstogo
)) & 0x3F];
6496 if (change_spaces
&& c
== ' ')
6500 /* encode '&' as '&-' */
6506 /* switch to UTF-7 mode */
6511 /* Encode US-ASCII characters as themselves */
6514 } else if (utf8total
) {
6515 /* save UTF8 bits into UCS4 */
6516 ucs4
= (ucs4
<< 6) | (c
& 0x3FUL
);
6517 if (++utf8pos
< utf8total
) {
6525 } else if (c
< 0xF0) {
6529 /* NOTE: can't convert UTF8 sequences longer than 4 */
6535 /* loop to split ucs4 into two utf16 chars if necessary */
6538 if (ucs4
>= UTF16BASE
) {
6540 bitbuf
= (bitbuf
<< 16) | ((ucs4
>> UTF16SHIFT
)
6542 ucs4
= (ucs4
& UTF16MASK
) + UTF16LOSTART
;
6545 bitbuf
= (bitbuf
<< 16) | ucs4
;
6549 /* spew out base64 */
6550 while (bitstogo
>= 6) {
6552 *dst
++ = base64chars
[(bitstogo
? (bitbuf
>> bitstogo
)
6556 } while (utf16flag
);
6558 /* if in UTF-7 mode, finish in ASCII */
6561 *dst
++ = base64chars
[(bitbuf
<< (6 - bitstogo
)) & 0x3F];
6565 /* tie off string */