2012-10-12 [mones] 3.8.1cvs98
[claws.git] / src / imap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "imap.h"
32 #include "imap_gtk.h"
33 #include "inc.h"
34 #include "xml.h"
35 #include "alertpanel.h"
36
37 #ifdef HAVE_LIBETPAN
38
39 #include <stdlib.h>
40 #include <dirent.h>
41 #include <unistd.h>
42 #include <ctype.h>
43 #include <time.h>
44 #include <errno.h>
45 #if HAVE_ICONV
46 #  include <iconv.h>
47 #endif
48
49 #ifdef USE_GNUTLS
50 #  include "ssl.h"
51 #endif
52
53 #include "folder.h"
54 #include "session.h"
55 #include "procmsg.h"
56 #include "socket.h"
57 #include "recv.h"
58 #include "procheader.h"
59 #include "prefs_account.h"
60 #include "codeconv.h"
61 #include "md5.h"
62 #include "base64.h"
63 #include "utils.h"
64 #include "prefs_common.h"
65 #include "inputdialog.h"
66 #include "log.h"
67 #include "remotefolder.h"
68 #include "claws.h"
69 #include "statusbar.h"
70 #include "msgcache.h"
71 #include "imap-thread.h"
72 #include "account.h"
73 #include "tags.h"
74 #include "main.h"
75
76 typedef struct _IMAPFolder      IMAPFolder;
77 typedef struct _IMAPSession     IMAPSession;
78 typedef struct _IMAPNameSpace   IMAPNameSpace;
79 typedef struct _IMAPFolderItem  IMAPFolderItem;
80
81 #include "prefs_account.h"
82
83 #define IMAP_FOLDER(obj)        ((IMAPFolder *)obj)
84 #define IMAP_FOLDER_ITEM(obj)   ((IMAPFolderItem *)obj)
85 #define IMAP_SESSION(obj)       ((IMAPSession *)obj)
86
87 struct _IMAPFolder
88 {
89         RemoteFolder rfolder;
90
91         /* list of IMAPNameSpace */
92         GList *ns_personal;
93         GList *ns_others;
94         GList *ns_shared;
95         gchar last_seen_separator;
96         guint refcnt;
97         guint max_set_size;
98 };
99
100 struct _IMAPSession
101 {
102         Session session;
103
104         gboolean authenticated;
105
106         GSList *capability;
107         gboolean uidplus;
108
109         gchar *mbox;
110         guint cmd_count;
111
112         /* CLAWS */
113         gboolean folder_content_changed;
114         guint exists;
115         guint recent;
116         guint expunge;
117         guint unseen;
118         guint uid_validity;
119         guint uid_next;
120
121         Folder * folder;
122         gboolean busy;
123         gboolean cancelled;
124         gboolean sens_update_block;
125 };
126
127 struct _IMAPNameSpace
128 {
129         gchar *name;
130         gchar separator;
131 };
132
133 #define IMAPBUFSIZE     8192
134
135 #define IMAP_IS_SEEN(flags)     ((flags & IMAP_FLAG_SEEN) != 0)
136 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
137 #define IMAP_IS_FLAGGED(flags)  ((flags & IMAP_FLAG_FLAGGED) != 0)
138 #define IMAP_IS_DELETED(flags)  ((flags & IMAP_FLAG_DELETED) != 0)
139 #define IMAP_IS_DRAFT(flags)    ((flags & IMAP_FLAG_DRAFT) != 0)
140 #define IMAP_IS_FORWARDED(flags)        ((flags & IMAP_FLAG_FORWARDED) != 0)
141 #define IMAP_IS_SPAM(flags)     ((flags & IMAP_FLAG_SPAM) != 0)
142 #define IMAP_IS_HAM(flags)      ((flags & IMAP_FLAG_HAM) != 0)
143
144
145 #define IMAP4_PORT      143
146 #ifdef USE_GNUTLS
147 #define IMAPS_PORT      993
148 #endif
149
150 #define IMAP_CMD_LIMIT  1000
151
152 enum {
153         ITEM_CAN_CREATE_FLAGS_UNKNOWN = 0,
154         ITEM_CAN_CREATE_FLAGS,
155         ITEM_CANNOT_CREATE_FLAGS
156 };
157
158 struct _IMAPFolderItem
159 {
160         FolderItem item;
161
162         guint lastuid;
163         guint uid_next;
164         GSList *uid_list;
165         gboolean batching;
166
167         GHashTable *flags_set_table;
168         GHashTable *flags_unset_table;
169         guint32 last_change;
170         guint32 last_sync;
171         gboolean should_update;
172         gboolean should_trash_cache;
173         gint can_create_flags;
174
175         GHashTable *tags_set_table;
176         GHashTable *tags_unset_table;
177         GSList *ok_flags;
178
179 };
180
181 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
182 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
183
184 static void imap_folder_init            (Folder         *folder,
185                                          const gchar    *name,
186                                          const gchar    *path);
187
188 static Folder   *imap_folder_new        (const gchar    *name,
189                                          const gchar    *path);
190 static void      imap_folder_destroy    (Folder         *folder);
191
192 static IMAPSession *imap_session_new    (Folder         *folder,
193                                          const PrefsAccount     *account);
194 static gint     imap_session_authenticate(IMAPSession   *session,
195                                           PrefsAccount  *account);
196 static void     imap_session_destroy    (Session        *session);
197
198 static gchar   *imap_fetch_msg          (Folder         *folder, 
199                                          FolderItem     *item, 
200                                          gint            uid);
201 static gchar   *imap_fetch_msg_full     (Folder         *folder, 
202                                          FolderItem     *item, 
203                                          gint            uid,
204                                          gboolean        headers,
205                                          gboolean        body);
206 static void     imap_remove_cached_msg  (Folder         *folder, 
207                                          FolderItem     *item, 
208                                          MsgInfo        *msginfo);
209 static gint     imap_add_msg            (Folder         *folder,
210                                          FolderItem     *dest,
211                                          const gchar    *file, 
212                                          MsgFlags       *flags);
213 static gint     imap_add_msgs           (Folder         *folder, 
214                                          FolderItem     *dest,
215                                          GSList         *file_list,
216                                          GHashTable     *relation);
217
218 static gint     imap_copy_msg           (Folder         *folder,
219                                          FolderItem     *dest, 
220                                          MsgInfo        *msginfo);
221 static gint     imap_copy_msgs          (Folder         *folder, 
222                                          FolderItem     *dest, 
223                                          MsgInfoList    *msglist, 
224                                          GHashTable     *relation);
225
226 static gint     search_msgs             (Folder                 *folder,
227                                          FolderItem             *container,
228                                          MsgNumberList          **msgs,
229                                          gboolean               *on_server,
230                                          MatcherList            *predicate,
231                                          SearchProgressNotify   progress_cb,
232                                          gpointer               progress_data);
233
234 static gint     imap_remove_msg         (Folder         *folder, 
235                                          FolderItem     *item, 
236                                          gint            uid);
237 static gint     imap_remove_msgs        (Folder         *folder, 
238                                          FolderItem     *dest, 
239                                          MsgInfoList    *msglist, 
240                                          GHashTable     *relation);
241 static gint     imap_expunge            (Folder         *folder, 
242                                          FolderItem     *dest);
243 static gint     imap_remove_all_msg     (Folder         *folder, 
244                                          FolderItem     *item);
245
246 static gboolean imap_is_msg_changed     (Folder         *folder,
247                                          FolderItem     *item, 
248                                          MsgInfo        *msginfo);
249
250 static gint     imap_close              (Folder         *folder, 
251                                          FolderItem     *item);
252
253 static gint     imap_scan_tree          (Folder         *folder);
254
255 static gint     imap_create_tree        (Folder         *folder);
256
257 static FolderItem *imap_create_folder   (Folder         *folder,
258                                          FolderItem     *parent,
259                                          const gchar    *name);
260 static gint     imap_rename_folder      (Folder         *folder,
261                                          FolderItem     *item, 
262                                          const gchar    *name);
263 static gint     imap_remove_folder      (Folder         *folder, 
264                                          FolderItem     *item);
265
266 static FolderItem *imap_folder_item_new (Folder         *folder);
267 static void imap_folder_item_destroy    (Folder         *folder,
268                                          FolderItem     *item);
269
270 static IMAPSession *imap_session_get    (Folder         *folder);
271
272 static gint imap_auth                   (IMAPSession    *session,
273                                          const gchar    *user,
274                                          const gchar    *pass,
275                                          IMAPAuthType    type);
276
277 static gint imap_scan_tree_recursive    (IMAPSession    *session,
278                                          FolderItem     *item,
279                                          gboolean        subs_only);
280
281 static void imap_create_missing_folders (Folder         *folder);
282 static FolderItem *imap_create_special_folder
283                                         (Folder                 *folder,
284                                          SpecialFolderItemType   stype,
285                                          const gchar            *name);
286
287 static gint imap_do_copy_msgs           (Folder         *folder,
288                                          FolderItem     *dest,
289                                          MsgInfoList    *msglist,
290                                          GHashTable     *relation);
291
292 static void imap_delete_all_cached_messages     (FolderItem     *item);
293 static void imap_set_batch              (Folder         *folder,
294                                          FolderItem     *item,
295                                          gboolean        batch);
296 static gint imap_set_message_flags      (IMAPSession    *session,
297                                          IMAPFolderItem *item,
298                                          MsgNumberList  *numlist,
299                                          IMAPFlags       flags,
300                                          GSList         *tags,
301                                          gboolean        is_set);
302 static gint imap_select                 (IMAPSession    *session,
303                                          IMAPFolder     *folder,
304                                          FolderItem     *item,
305                                          gint           *exists,
306                                          gint           *recent,
307                                          gint           *unseen,
308                                          guint32        *uid_validity,
309                                          gint           *can_create_flags,
310                                          gboolean        block);
311 static gint imap_status                 (IMAPSession    *session,
312                                          IMAPFolder     *folder,
313                                          const gchar    *path,
314                                          IMAPFolderItem *item,
315                                          gint           *messages,
316                                          guint32        *uid_next,
317                                          guint32        *uid_validity,
318                                          gint           *unseen,
319                                          gboolean        block);
320 static void     imap_commit_tags        (FolderItem     *item, 
321                                          MsgInfo        *msginfo,
322                                          GSList         *set_tags,
323                                          GSList         *unset_tags);
324
325 static gchar imap_get_path_separator            (IMAPSession    *session,
326                                                  IMAPFolder     *folder,
327                                                  const gchar    *path,
328                                                  gint           *ok);
329 static gchar *imap_get_real_path                (IMAPSession    *session,
330                                                  IMAPFolder     *folder,
331                                                  const gchar    *path,
332                                                  gint           *ok);
333 #ifdef HAVE_LIBETPAN
334 static void imap_synchronise            (FolderItem     *item, gint days);
335 #endif
336 static gboolean imap_is_busy            (Folder *folder);
337
338 static void imap_free_capabilities      (IMAPSession    *session);
339
340 /* low-level IMAP4rev1 commands */
341 static gint imap_cmd_login      (IMAPSession    *session,
342                                  const gchar    *user,
343                                  const gchar    *pass,
344                                  const gchar    *type);
345 static gint imap_cmd_noop       (IMAPSession    *session);
346 #ifdef USE_GNUTLS
347 static gint imap_cmd_starttls   (IMAPSession    *session);
348 #endif
349 static gint imap_cmd_select     (IMAPSession    *session,
350                                  const gchar    *folder,
351                                  gint           *exists,
352                                  gint           *recent,
353                                  gint           *unseen,
354                                  guint32        *uid_validity,
355                                  gint           *can_create_flags,
356                                  GSList         **ok_flags,
357                                  gboolean        block);
358 static gint imap_cmd_close      (IMAPSession    *session);
359 static gint imap_cmd_examine    (IMAPSession    *session,
360                                  const gchar    *folder,
361                                  gint           *exists,
362                                  gint           *recent,
363                                  gint           *unseen,
364                                  guint32        *uid_validity,
365                                  gboolean        block);
366 static gint imap_cmd_create     (IMAPSession    *sock,
367                                  const gchar    *folder);
368 static gint imap_cmd_rename     (IMAPSession    *sock,
369                                  const gchar    *oldfolder,
370                                  const gchar    *newfolder);
371 static gint imap_cmd_delete     (IMAPSession    *session,
372                                  const gchar    *folder);
373 static gint imap_cmd_fetch      (IMAPSession    *sock,
374                                  guint32         uid,
375                                  const gchar    *filename,
376                                  gboolean        headers,
377                                  gboolean        body);
378 static gint imap_cmd_append     (IMAPSession    *session,
379                                  IMAPFolderItem *item,
380                                  const gchar    *destfolder,
381                                  const gchar    *file,
382                                  IMAPFlags       flags,
383                                  guint32        *new_uid);
384 static gint imap_cmd_copy       (IMAPSession *session,
385                                  struct mailimap_set * set,
386                                  const gchar *destfolder,
387                                  struct mailimap_set ** source,
388                                  struct mailimap_set ** dest);
389 static gint imap_cmd_store      (IMAPSession    *session,
390                                  IMAPFolderItem *item,
391                                  struct mailimap_set * set,
392                                  IMAPFlags flags,
393                                  GSList *tags,
394                                  int do_add);
395 static gint imap_cmd_expunge    (IMAPSession    *session, gboolean force);
396
397 static void imap_path_separator_subst           (gchar          *str,
398                                                  gchar           separator);
399
400 static gboolean imap_rename_folder_func         (GNode          *node,
401                                                  gpointer        data);
402 static gint imap_get_num_list                   (Folder         *folder,
403                                                  FolderItem     *item,
404                                                  GSList        **list,
405                                                  gboolean       *old_uids_valid);
406 static GSList *imap_get_msginfos                (Folder         *folder,
407                                                  FolderItem     *item,
408                                                  GSList         *msgnum_list);
409 static MsgInfo *imap_get_msginfo                (Folder         *folder,
410                                                  FolderItem     *item,
411                                                  gint            num);
412 static gboolean imap_scan_required              (Folder         *folder,
413                                                  FolderItem     *item);
414 static void imap_change_flags                   (Folder         *folder,
415                                                  FolderItem     *item,
416                                                  MsgInfo        *msginfo,
417                                                  MsgPermFlags    newflags);
418 static gint imap_get_flags                      (Folder         *folder,
419                                                  FolderItem     *item,
420                                                  MsgInfoList    *msglist,
421                                                  GHashTable     *msgflags);
422 static gchar *imap_folder_get_path              (Folder         *folder);
423 static gchar *imap_item_get_path                (Folder         *folder,
424                                                  FolderItem     *item);
425 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
426
427
428 /* data types conversion libetpan <-> claws */
429 static GSList * imap_list_from_lep(IMAPFolder * folder,
430                                    clist * list, const gchar * real_path, gboolean all);
431 static GSList * imap_get_lep_set_from_numlist(IMAPFolder *folder, MsgNumberList *numlist);
432 static GSList * imap_get_lep_set_from_msglist(IMAPFolder *folder, MsgInfoList *msglist);
433 static GSList * imap_uid_list_from_lep(clist * list, gint* length);
434 static GSList * imap_uid_list_from_lep_tab(carray * list);
435 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
436                                                    GHashTable * hash,
437                                                    GHashTable *tags_hash);
438 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
439                                        FolderItem *item);
440 static void imap_lep_set_free(GSList *seq_list);
441 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFolderItem *item, IMAPFlags flags, GSList *tags);
442
443 typedef struct _hashtable_data {
444         GSList *msglist;
445         IMAPFolderItem *item;
446 } hashtable_data;
447
448 static FolderClass imap_class;
449
450 FolderClass *imap_get_class(void)
451 {
452         if (imap_class.idstr == NULL) {
453                 imap_class.type = F_IMAP;
454                 imap_class.idstr = "imap";
455                 imap_class.uistr = "IMAP4";
456                 imap_class.supports_server_search = TRUE;
457
458                 /* Folder functions */
459                 imap_class.new_folder = imap_folder_new;
460                 imap_class.destroy_folder = imap_folder_destroy;
461                 imap_class.scan_tree = imap_scan_tree;
462                 imap_class.create_tree = imap_create_tree;
463
464                 /* FolderItem functions */
465                 imap_class.item_new = imap_folder_item_new;
466                 imap_class.item_destroy = imap_folder_item_destroy;
467                 imap_class.item_get_path = imap_item_get_path;
468                 imap_class.create_folder = imap_create_folder;
469                 imap_class.rename_folder = imap_rename_folder;
470                 imap_class.remove_folder = imap_remove_folder;
471                 imap_class.close = imap_close;
472                 imap_class.get_num_list = imap_get_num_list;
473                 imap_class.scan_required = imap_scan_required;
474                 imap_class.set_xml = folder_set_xml;
475                 imap_class.get_xml = folder_get_xml;
476                 imap_class.item_set_xml = imap_item_set_xml;
477                 imap_class.item_get_xml = imap_item_get_xml;
478
479                 /* Message functions */
480                 imap_class.get_msginfo = imap_get_msginfo;
481                 imap_class.get_msginfos = imap_get_msginfos;
482                 imap_class.fetch_msg = imap_fetch_msg;
483                 imap_class.fetch_msg_full = imap_fetch_msg_full;
484                 imap_class.add_msg = imap_add_msg;
485                 imap_class.add_msgs = imap_add_msgs;
486                 imap_class.copy_msg = imap_copy_msg;
487                 imap_class.copy_msgs = imap_copy_msgs;
488                 imap_class.search_msgs = search_msgs;
489                 imap_class.remove_msg = imap_remove_msg;
490                 imap_class.remove_msgs = imap_remove_msgs;
491                 imap_class.expunge = imap_expunge;
492                 imap_class.remove_all_msg = imap_remove_all_msg;
493                 imap_class.is_msg_changed = imap_is_msg_changed;
494                 imap_class.change_flags = imap_change_flags;
495                 imap_class.get_flags = imap_get_flags;
496                 imap_class.set_batch = imap_set_batch;
497                 imap_class.synchronise = imap_synchronise;
498                 imap_class.remove_cached_msg = imap_remove_cached_msg;
499                 imap_class.commit_tags = imap_commit_tags;
500 #ifdef USE_PTREAD
501                 pthread_mutex_init(&imap_mutex, NULL);
502 #endif
503         }
504         
505         return &imap_class;
506 }
507
508 static void imap_refresh_sensitivity (IMAPSession *session)
509 {
510         MainWindow *mainwin;
511
512         if (session->sens_update_block)
513                 return;
514         mainwin = mainwindow_get_mainwindow();
515         if (mainwin) {
516                 toolbar_main_set_sensitive(mainwin);
517                 main_window_set_menu_sensitive(mainwin);
518         }
519 }
520
521 static void lock_session(IMAPSession *session)
522 {
523         if (session) {
524                 debug_print("locking session %p (%d)\n", session, session->busy);
525                 if (session->busy)
526                         debug_print("         SESSION WAS LOCKED !!      \n");
527                 session->busy = TRUE;
528                 imap_refresh_sensitivity(session);
529         } else {
530                 debug_print("can't lock null session\n");
531         }
532 }
533
534 static void unlock_session(IMAPSession *session)
535 {
536         if (session) {
537                 debug_print("unlocking session %p\n", session);
538                 session->busy = FALSE;
539                 imap_refresh_sensitivity(session);
540         } else {
541                 debug_print("can't unlock null session\n");
542         }
543 }
544
545 static void imap_disc_session_destroy(IMAPSession *session)
546 {
547         RemoteFolder *rfolder = NULL;
548
549         if (session == NULL)
550                 return;
551
552         rfolder = REMOTE_FOLDER(IMAP_SESSION(session)->folder);
553         
554         if (rfolder == NULL)
555                 return;
556         log_warning(LOG_PROTOCOL, _("IMAP4 connection broken\n"));
557         SESSION(session)->state = SESSION_DISCONNECTED;
558         SESSION(session)->sock = NULL;
559 }
560
561 static gboolean is_fatal(int libetpan_errcode)
562 {
563         switch(libetpan_errcode) {
564         case MAILIMAP_ERROR_STREAM:
565         case MAILIMAP_ERROR_PROTOCOL:
566         case MAILIMAP_ERROR_PARSE:
567         case MAILIMAP_ERROR_BAD_STATE:
568                 return TRUE;
569         default:
570                 return FALSE;
571         }
572 }
573
574 static void imap_handle_error(Session *session, const gchar *server, int libetpan_errcode)
575 {
576         const gchar *session_server = (session ? session->server : NULL);
577
578         if (session_server == NULL)
579                 session_server = server;
580         if (session_server == NULL)
581                 session_server = "(null)";
582
583         switch(libetpan_errcode) {
584         case MAILIMAP_NO_ERROR:
585                 return;
586         case MAILIMAP_NO_ERROR_AUTHENTICATED:
587                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: authenticated\n"), session_server);
588                 break;
589         case MAILIMAP_NO_ERROR_NON_AUTHENTICATED:
590                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: not authenticated\n"), session_server);
591                 break;
592         case MAILIMAP_ERROR_BAD_STATE:
593                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: bad state\n"), session_server);
594                 break;
595         case MAILIMAP_ERROR_STREAM:
596                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: stream error\n"), session_server);
597                 break;
598         case MAILIMAP_ERROR_PARSE:
599                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: parse error "
600                                             "(very probably non-RFC compliance from the server)\n"), session_server);
601                 break;
602         case MAILIMAP_ERROR_CONNECTION_REFUSED:
603                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection refused\n"), session_server);
604                 break;
605         case MAILIMAP_ERROR_MEMORY:
606                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: memory error\n"), session_server);
607                 break;
608         case MAILIMAP_ERROR_FATAL:
609                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: fatal error\n"), session_server);
610                 break;
611         case MAILIMAP_ERROR_PROTOCOL:
612                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: protocol error"
613                                             "(very probably non-RFC compliance from the server)\n"), session_server);
614                 break;
615         case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
616                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: connection not accepted\n"), session_server);
617                 break;
618         case MAILIMAP_ERROR_APPEND:
619                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: APPEND error\n"), session_server);
620                 break;
621         case MAILIMAP_ERROR_NOOP:
622                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: NOOP error\n"), session_server);
623                 break;
624         case MAILIMAP_ERROR_LOGOUT:
625                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGOUT error\n"), session_server);
626                 break;
627         case MAILIMAP_ERROR_CAPABILITY:
628                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CAPABILITY error\n"), session_server);
629                 break;
630         case MAILIMAP_ERROR_CHECK:
631                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CHECK error\n"), session_server);
632                 break;
633         case MAILIMAP_ERROR_CLOSE:
634                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CLOSE error\n"), session_server);
635                 break;
636         case MAILIMAP_ERROR_EXPUNGE:
637                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXPUNGE error\n"), session_server);
638                 break;
639         case MAILIMAP_ERROR_COPY:
640                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: COPY error\n"), session_server);
641                 break;
642         case MAILIMAP_ERROR_UID_COPY:
643                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID COPY error\n"), session_server);
644                 break;
645         case MAILIMAP_ERROR_CREATE:
646                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: CREATE error\n"), session_server);
647                 break;
648         case MAILIMAP_ERROR_DELETE:
649                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: DELETE error\n"), session_server);
650                 break;
651         case MAILIMAP_ERROR_EXAMINE:
652                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXAMINE error\n"), session_server);
653                 break;
654         case MAILIMAP_ERROR_FETCH:
655                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: FETCH error\n"), session_server);
656                 break;
657         case MAILIMAP_ERROR_UID_FETCH:
658                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID FETCH error\n"), session_server);
659                 break;
660         case MAILIMAP_ERROR_LIST:
661                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LIST error\n"), session_server);
662                 break;
663         case MAILIMAP_ERROR_LOGIN:
664                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LOGIN error\n"), session_server);
665                 break;
666         case MAILIMAP_ERROR_LSUB:
667                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: LSUB error\n"), session_server);
668                 break;
669         case MAILIMAP_ERROR_RENAME:
670                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: RENAME error\n"), session_server);
671                 break;
672         case MAILIMAP_ERROR_SEARCH:
673                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SEARCH error\n"), session_server);
674                 break;
675         case MAILIMAP_ERROR_UID_SEARCH:
676                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID SEARCH error\n"), session_server);
677                 break;
678         case MAILIMAP_ERROR_SELECT:
679                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SELECT error\n"), session_server);
680                 break;
681         case MAILIMAP_ERROR_STATUS:
682                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STATUS error\n"), session_server);
683                 break;
684         case MAILIMAP_ERROR_STORE:
685                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STORE error\n"), session_server);
686                 break;
687         case MAILIMAP_ERROR_UID_STORE:
688                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UID STORE error\n"), session_server);
689                 break;
690         case MAILIMAP_ERROR_SUBSCRIBE:
691                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SUBSCRIBE error\n"), session_server);
692                 break;
693         case MAILIMAP_ERROR_UNSUBSCRIBE:
694                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: UNSUBSCRIBE error\n"), session_server);
695                 break;
696         case MAILIMAP_ERROR_STARTTLS:
697                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: STARTTLS error\n"), session_server);
698                 break;
699         case MAILIMAP_ERROR_INVAL:
700                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: INVAL error\n"), session_server);
701                 break;
702         case MAILIMAP_ERROR_EXTENSION:
703                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: EXTENSION error\n"), session_server);
704                 break;
705         case MAILIMAP_ERROR_SASL:
706                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SASL error\n"), session_server);
707                 break;
708 #ifdef USE_GNUTLS
709         case MAILIMAP_ERROR_SSL:
710                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: SSL error\n"), session_server);
711                 break;
712 #endif
713         default:
714                 log_warning(LOG_PROTOCOL, _("IMAP error on %s: Unknown error [%d]\n"),
715                         session_server, libetpan_errcode);
716                 break;
717         }
718
719         if (session && is_fatal(libetpan_errcode)) {
720                 imap_disc_session_destroy(IMAP_SESSION(session));
721         } else if (session && !is_fatal(libetpan_errcode)) {
722                 if (IMAP_SESSION(session)->busy)
723                         unlock_session(IMAP_SESSION(session));
724         }
725 }
726
727 static Folder *imap_folder_new(const gchar *name, const gchar *path)
728 {
729         Folder *folder;
730
731         folder = (Folder *)g_new0(IMAPFolder, 1);
732         folder->klass = &imap_class;
733         imap_folder_init(folder, name, path);
734
735         return folder;
736 }
737
738 static void imap_folder_destroy(Folder *folder)
739 {
740         while (imap_folder_get_refcnt(folder) > 0)
741                 gtk_main_iteration();
742         
743         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
744         imap_done(folder);
745 }
746
747 static void imap_folder_init(Folder *folder, const gchar *name,
748                              const gchar *path)
749 {
750         folder_remote_folder_init((Folder *)folder, name, path);
751         IMAP_FOLDER(folder)->max_set_size = IMAP_SET_MAX_COUNT;
752 }
753
754 static FolderItem *imap_folder_item_new(Folder *folder)
755 {
756         IMAPFolderItem *item;
757         
758         item = g_new0(IMAPFolderItem, 1);
759         item->lastuid = 0;
760         item->uid_next = 0;
761         item->uid_list = NULL;
762
763         return (FolderItem *)item;
764 }
765
766 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
767 {
768         IMAPFolderItem *item = (IMAPFolderItem *)_item;
769
770         g_return_if_fail(item != NULL);
771         g_slist_free(item->uid_list);
772
773         g_free(_item);
774 }
775
776 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
777 {
778         IMAPFolderItem *item = (IMAPFolderItem *)node->data;
779         
780         item->lastuid = 0;
781         g_slist_free(item->uid_list);
782         item->uid_list = NULL;
783         
784         return FALSE;
785 }
786
787 static void imap_reset_uid_lists(Folder *folder)
788 {
789         if(folder->node == NULL)
790                 return;
791         
792         /* Destroy all uid lists and rest last uid */
793         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
794 }
795
796 static int imap_get_capabilities(IMAPSession *session)
797 {
798         struct mailimap_capability_data *capabilities = NULL;
799         clistiter *cur;
800         int result = -1;
801
802         if (session->capability != NULL)
803                 return MAILIMAP_NO_ERROR;
804
805         capabilities = imap_threaded_capability(session->folder, &result);
806
807         if (result != MAILIMAP_NO_ERROR) {
808                 return result;
809         }
810
811         if (capabilities == NULL || capabilities->cap_list == NULL) {
812                 return MAILIMAP_NO_ERROR;
813         }
814
815         for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
816             cur = clist_next(cur)) {
817                 struct mailimap_capability * cap = 
818                         clist_content(cur);
819                 if (!cap || cap->cap_data.cap_name == NULL)
820                         continue;
821                 session->capability = g_slist_append
822                                 (session->capability,
823                                  g_strdup(cap->cap_data.cap_name));
824                 debug_print("got capa %s\n", cap->cap_data.cap_name);
825         }
826         mailimap_capability_data_free(capabilities);
827         return MAILIMAP_NO_ERROR;
828 }
829
830 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
831 {
832         GSList *cur;
833         for (cur = session->capability; cur; cur = cur->next) {
834                 if (!g_ascii_strcasecmp(cur->data, cap))
835                         return TRUE;
836         }
837         return FALSE;
838 }
839
840 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
841                       IMAPAuthType type)
842 {
843         gint ok = MAILIMAP_ERROR_LOGIN;
844         static time_t last_login_err = 0;
845         gchar *ext_info = "";
846         int r;
847         gchar *server = NULL;
848         if ((r = imap_get_capabilities(session)) != MAILIMAP_NO_ERROR) {
849                 imap_handle_error(SESSION(session), NULL, r);
850                 return r;
851         }
852         server = g_strdup(SESSION(session)->server);
853         switch(type) {
854         case IMAP_AUTH_ANON:
855                 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
856                 break;
857         case IMAP_AUTH_CRAM_MD5:
858                 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
859                 break;
860         case IMAP_AUTH_DIGEST_MD5:
861                 ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
862                 break;
863         case IMAP_AUTH_LOGIN:
864                 ok = imap_cmd_login(session, user, pass, "LOGIN");
865                 break;
866         case IMAP_AUTH_GSSAPI:
867                 ok = imap_cmd_login(session, user, pass, "GSSAPI");
868                 break;
869         default:
870                 debug_print("capabilities:\n"
871                                 "\t ANONYMOUS %d\n"
872                                 "\t CRAM-MD5 %d\n"
873                                 "\t DIGEST-MD5 %d\n"
874                                 "\t LOGIN %d\n"
875                                 "\t GSSAPI %d\n", 
876                         imap_has_capability(session, "ANONYMOUS"),
877                         imap_has_capability(session, "CRAM-MD5"),
878                         imap_has_capability(session, "DIGEST-MD5"),
879                         imap_has_capability(session, "LOGIN"),
880                         imap_has_capability(session, "GSSAPI"));
881                 if (imap_has_capability(session, "CRAM-MD5"))
882                         ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
883                 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
884                         ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
885                 if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
886                         ok = imap_cmd_login(session, user, pass, "GSSAPI");
887                 if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
888                         ok = imap_cmd_login(session, user, pass, "LOGIN");
889         }
890
891         if (ok == MAILIMAP_NO_ERROR)
892                 session->authenticated = TRUE;
893         else {
894                 if (type == IMAP_AUTH_CRAM_MD5) {
895                         ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
896                                      "compiled with SASL support and the "
897                                      "CRAM-MD5 SASL plugin is installed.");
898                 } 
899
900                 if (type == IMAP_AUTH_DIGEST_MD5) {
901                         ext_info = _("\n\nDIGEST-MD5 logins only work if libetpan has been "
902                                      "compiled with SASL support and the "
903                                      "DIGEST-MD5 SASL plugin is installed.");
904                 } 
905
906                 if (time(NULL) - last_login_err > 10) {
907                         if (!prefs_common.no_recv_err_panel) {
908                                 alertpanel_error_log(_("Connection to %s failed: "
909                                         "login refused.%s"),
910                                         server, ext_info);
911                         } else {
912                                 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
913                                         "login refused.%s\n"),
914                                         server, ext_info);
915                         }
916                 }
917                 last_login_err = time(NULL);
918         }
919         g_free(server);
920         return ok;
921 }
922
923 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
924 {
925         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
926         /* Check if this is the first try to establish a
927            connection, if yes we don't try to reconnect */
928         debug_print("reconnecting\n");
929         if (rfolder->session == NULL) {
930                 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
931                             folder->account->recv_server);
932                 SESSION(session)->sock = NULL;
933                 session_destroy(SESSION(session));
934                 session = NULL;
935         } else {
936                 rfolder->session = NULL;
937                 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
938                             " disconnected. Reconnecting...\n"),
939                             folder->account->recv_server);
940                 statusbar_print_all(_("IMAP4 connection to %s has been"
941                             " disconnected. Reconnecting...\n"),
942                             folder->account->recv_server);
943                 SESSION(session)->state = SESSION_DISCONNECTED;
944                 SESSION(session)->sock = NULL;
945                 session_destroy(SESSION(session));
946                 /* Clear folders session to make imap_session_get create
947                    a new session, because of rfolder->session == NULL
948                    it will not try to reconnect again and so avoid an
949                    endless loop */
950                 debug_print("getting session...\n");
951                 session = imap_session_get(folder);
952                 rfolder->session = SESSION(session);
953                 statusbar_pop_all();
954         }
955         return session;
956 }
957
958 static IMAPSession *imap_session_get(Folder *folder)
959 {
960         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
961         IMAPSession *session = NULL;
962         gint r = MAILIMAP_NO_ERROR;
963
964         g_return_val_if_fail(folder != NULL, NULL);
965         g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
966         g_return_val_if_fail(folder->account != NULL, NULL);
967         
968         if (prefs_common.work_offline && 
969             !inc_offline_should_override(FALSE,
970                 _("Claws Mail needs network access in order "
971                   "to access the IMAP server."))) {
972                 return NULL;
973         }
974
975         /* Make sure we have a session */
976         if (rfolder->session != NULL && rfolder->session->state != SESSION_DISCONNECTED) {
977                 session = IMAP_SESSION(rfolder->session);
978         } else if (rfolder->session != NULL && rfolder->session->state == SESSION_DISCONNECTED) {
979                 session_destroy(SESSION(rfolder->session));
980                 rfolder->session = NULL;
981                 goto new_conn;
982         } else if (rfolder->connecting) {
983                 debug_print("already connecting\n");
984                 return NULL;
985         } else {
986 new_conn:
987                 imap_reset_uid_lists(folder);
988                 if (time(NULL) - rfolder->last_failure <= 2)
989                         return NULL;
990                 rfolder->connecting = TRUE;
991                 session = imap_session_new(folder, folder->account);
992         }
993         if(session == NULL) {
994                 rfolder->last_failure = time(NULL);
995                 rfolder->connecting = FALSE;
996                 return NULL;
997         }
998
999         /* Make sure session is authenticated */
1000         if (!IMAP_SESSION(session)->authenticated)
1001                 r = imap_session_authenticate(IMAP_SESSION(session), folder->account);
1002         
1003         if (r != MAILIMAP_NO_ERROR || (!is_fatal(r) && !IMAP_SESSION(session)->authenticated)) {
1004                 rfolder->session = NULL;
1005                 if (!is_fatal(r)) {
1006                         imap_threaded_disconnect(session->folder);
1007                         SESSION(session)->state = SESSION_DISCONNECTED;
1008                         SESSION(session)->sock = NULL;
1009                         session_destroy(SESSION(session));
1010                 }
1011                 rfolder->last_failure = time(NULL);
1012                 rfolder->connecting = FALSE;
1013                 return NULL;
1014         }
1015
1016         /* I think the point of this code is to avoid sending a
1017          * keepalive if we've used the session recently and therefore
1018          * think it's still alive.  Unfortunately, most of the code
1019          * does not yet check for errors on the socket, and so if the
1020          * connection drops we don't notice until the timeout expires.
1021          * A better solution than sending a NOOP every time would be
1022          * for every command to be prepared to retry until it is
1023          * successfully sent. -- mbp */
1024         if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
1025                 /* verify that the session is still alive */
1026                 if ((r = imap_cmd_noop(session)) != MAILIMAP_NO_ERROR) {
1027                         debug_print("disconnected!\n");
1028                         if (!is_fatal(r))
1029                                 session = imap_reconnect_if_possible(folder, session);
1030                         else {
1031                                 rfolder->session = NULL;
1032                                 rfolder->connecting = FALSE;
1033                                 session = imap_session_get(folder);
1034                         }
1035                 }
1036                 if (session)
1037                         session->cancelled = FALSE;
1038         }
1039
1040         rfolder->session = SESSION(session);
1041         rfolder->connecting = FALSE;
1042
1043         return IMAP_SESSION(session);
1044 }
1045
1046 static IMAPSession *imap_session_new(Folder * folder,
1047                                      const PrefsAccount *account)
1048 {
1049         IMAPSession *session;
1050         gushort port;
1051         int r;
1052         int authenticated = FALSE;
1053         gchar *buf;
1054
1055 #ifdef USE_GNUTLS
1056         /* FIXME: IMAP over SSL only... */ 
1057         SSLType ssl_type;
1058
1059         port = account->set_imapport ? account->imapport
1060                 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
1061         ssl_type = account->ssl_imap;   
1062 #else
1063         if (account->ssl_imap != SSL_NONE) {
1064                 if (alertpanel_full(_("Insecure connection"),
1065                         _("This connection is configured to be secured "
1066                           "using SSL, but SSL is not available in this "
1067                           "build of Claws Mail. \n\n"
1068                           "Do you want to continue connecting to this "
1069                           "server? The communication would not be "
1070                           "secure."),
1071                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
1072                           NULL, FALSE, NULL, ALERT_WARNING,
1073                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
1074                         return NULL;
1075         }
1076         port = account->set_imapport ? account->imapport
1077                 : IMAP4_PORT;
1078 #endif
1079
1080         imap_init(folder);
1081         buf = g_strdup_printf(_("Account '%s': Connecting to IMAP4 server: %s:%d..."),
1082                                 folder->account->account_name, folder->account->recv_server,
1083                                 port);
1084         statuswindow_print_all("%s", buf);
1085         log_message(LOG_PROTOCOL, "%s\n", buf);
1086         g_free(buf);
1087
1088 #ifndef G_OS_WIN32
1089         if (account->set_tunnelcmd) {
1090                 r = imap_threaded_connect_cmd(folder,
1091                                               account->tunnelcmd,
1092                                               account->recv_server,
1093                                               port);
1094         }
1095         else 
1096 #endif
1097         {
1098 #ifdef USE_GNUTLS
1099                 if (ssl_type == SSL_TUNNEL) {
1100                         r = imap_threaded_connect_ssl(folder,
1101                                                       account->recv_server,
1102                                                       port);
1103                 }
1104                 else 
1105 #endif
1106                 {
1107                         r = imap_threaded_connect(folder,
1108                                                   account->recv_server,
1109                                                   port);
1110                 }
1111         }
1112         
1113         statuswindow_pop_all();
1114         if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
1115                 authenticated = TRUE;
1116         }
1117         else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
1118                 authenticated = FALSE;
1119         }
1120         else {
1121 #ifdef USE_GNUTLS
1122                 if (r == MAILIMAP_ERROR_SSL)
1123                         log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
1124                 else
1125 #endif
1126                         imap_handle_error(NULL, account->recv_server, r);
1127
1128                 if(!prefs_common.no_recv_err_panel) {
1129                         alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
1130                                          account->recv_server, port);
1131                 } else {
1132                         log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
1133                                          account->recv_server, port);
1134                 } 
1135                 
1136                 return NULL;
1137         }
1138         
1139         session = g_new0(IMAPSession, 1);
1140         session_init(SESSION(session), account, FALSE);
1141         SESSION(session)->type             = SESSION_IMAP;
1142         SESSION(session)->server           = g_strdup(account->recv_server);
1143         SESSION(session)->port             = port;
1144         SESSION(session)->sock             = NULL;
1145         
1146         SESSION(session)->destroy          = imap_session_destroy;
1147
1148         session->capability = NULL;
1149         
1150         session->authenticated = authenticated;
1151         session->mbox = NULL;
1152         session->exists = 0;
1153         session->recent = 0;
1154         session->expunge = 0;
1155         session->cmd_count = 0;
1156         session->folder = folder;
1157         IMAP_FOLDER(session->folder)->last_seen_separator = 0;
1158
1159 #ifdef USE_GNUTLS
1160         if (account->ssl_imap == SSL_STARTTLS) {
1161                 gint ok;
1162
1163                 ok = imap_cmd_starttls(session);
1164                 if (ok != MAILIMAP_NO_ERROR) {
1165                         log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
1166                         if (!is_fatal(ok)) {
1167                                 SESSION(session)->sock = NULL;
1168                                 session_destroy(SESSION(session));
1169                         }
1170                         return NULL;
1171                 }
1172
1173                 imap_free_capabilities(session);
1174                 session->authenticated = FALSE;
1175                 session->uidplus = FALSE;
1176                 session->cmd_count = 1;
1177         }
1178 #endif
1179         log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
1180                     (session->authenticated) ? "pre" : "un");
1181         
1182         return session;
1183 }
1184
1185 static gint imap_session_authenticate(IMAPSession *session, 
1186                                       PrefsAccount *account)
1187 {
1188         gchar *pass, *acc_pass;
1189         gboolean failed = FALSE;
1190         gint ok = MAILIMAP_NO_ERROR;
1191         g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
1192         acc_pass = account->passwd;
1193 try_again:
1194         pass = acc_pass;
1195         if (!pass && account->imap_auth_type != IMAP_AUTH_ANON && account->imap_auth_type != IMAP_AUTH_GSSAPI) {
1196                 gchar *tmp_pass;
1197                 tmp_pass = input_dialog_query_password_keep(account->recv_server, 
1198                                                             account->userid,
1199                                                             &(account->session_passwd));
1200                 if (!tmp_pass)
1201                         return MAILIMAP_NO_ERROR;
1202                 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return MAILIMAP_NO_ERROR;});
1203                 g_free(tmp_pass);
1204         } else if (account->imap_auth_type == IMAP_AUTH_ANON || account->imap_auth_type == IMAP_AUTH_GSSAPI) {
1205                 pass = "";
1206         }
1207         if ((ok = imap_auth(session, account->userid, pass, account->imap_auth_type)) != MAILIMAP_NO_ERROR) {
1208                 
1209                 if (!failed && !is_fatal(ok)) {
1210                         acc_pass = NULL;
1211                         failed = TRUE;
1212                         if (account->session_passwd != NULL) {
1213                                 g_free(account->session_passwd);
1214                                 account->session_passwd = NULL;
1215                         }
1216                         goto try_again;
1217                 } else {
1218                         if (prefs_common.no_recv_err_panel) {
1219                                 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s.\n"), account->recv_server);
1220                                 mainwindow_show_error();
1221                         } else
1222                                 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
1223                 }               
1224
1225                 return ok;
1226         } 
1227
1228         statuswindow_pop_all();
1229         session->authenticated = TRUE;
1230         return MAILIMAP_NO_ERROR;
1231 }
1232
1233 static void imap_session_destroy(Session *session)
1234 {
1235         if (session->state != SESSION_DISCONNECTED)
1236                 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
1237         
1238         imap_free_capabilities(IMAP_SESSION(session));
1239         g_free(IMAP_SESSION(session)->mbox);
1240 }
1241
1242 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1243 {
1244         return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1245 }
1246
1247 static guint get_file_size_with_crs(const gchar *filename) 
1248 {
1249         FILE *fp = NULL;
1250         guint cnt = 0;
1251         gchar buf[4096];
1252         
1253         if (filename == NULL)
1254                 return -1;
1255         
1256         fp = g_fopen(filename, "rb");
1257         if (!fp)
1258                 return -1;
1259         
1260         while (fgets(buf, sizeof (buf), fp) != NULL) {
1261                 cnt += strlen(buf);
1262                 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1263                         cnt++;
1264         }
1265         
1266         fclose(fp);
1267         return cnt;
1268 }
1269
1270 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1271 {
1272         gchar *path, *filename;
1273
1274         path = folder_item_get_path(item);
1275
1276         if (!is_dir_exist(path)) {
1277                 g_free(path);
1278                 return;
1279         }
1280
1281         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1282         g_free(path);
1283
1284         if (is_file_exist(filename)) {
1285                 claws_unlink(filename);
1286         }
1287         g_free(filename);
1288 }
1289
1290 typedef struct _TagsData {
1291         gchar *str;
1292         GSList *msglist;
1293         IMAPFolderItem *item;
1294 } TagsData;
1295
1296 static void imap_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
1297 {
1298         IMAPSession *session;
1299         gint ok, can_create_tags;
1300         Folder *folder = NULL;
1301         TagsData *ht_data = NULL;
1302         GSList *cur;
1303
1304         g_return_if_fail(item != NULL);
1305         g_return_if_fail(msginfo != NULL);
1306
1307         folder = item->folder;
1308         debug_print("getting session...\n");
1309         session = imap_session_get(folder);
1310         
1311         if (!session) {
1312                 debug_print("can't get session\n");
1313                 return;
1314         }
1315
1316         ok = imap_select(session, IMAP_FOLDER(folder), item,
1317                          NULL, NULL, NULL, NULL, &can_create_tags, FALSE);
1318
1319         if (ok != MAILIMAP_NO_ERROR) {
1320                 return;
1321         }
1322
1323         
1324         if (IMAP_FOLDER_ITEM(item)->can_create_flags != ITEM_CAN_CREATE_FLAGS)
1325                 return;
1326         
1327         if (IMAP_FOLDER_ITEM(item)->batching) {
1328                 /* instead of performing an UID STORE command for each message change,
1329                  * as a lot of them can change "together", we just fill in hashtables
1330                  * and defer the treatment so that we're able to send only one
1331                  * command.
1332                  */
1333                 debug_print("IMAP batch mode on, deferring tags change\n");
1334                 for (cur = tags_set; cur; cur = cur->next) {
1335                         gint cur_tag = GPOINTER_TO_INT(cur->data);
1336                         if (cur_tag) {
1337                                 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_set_table, 
1338                                         GINT_TO_POINTER(cur_tag));
1339                                 if (ht_data == NULL) {
1340                                         ht_data = g_new0(TagsData, 1);
1341                                         ht_data->str = g_strdup(tags_get_tag(cur_tag));
1342                                         ht_data->item = IMAP_FOLDER_ITEM(item);
1343                                         g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_set_table, 
1344                                                 GINT_TO_POINTER(cur_tag), ht_data);
1345                                 }
1346                                 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1347                         } 
1348                 }
1349                 for (cur = tags_unset; cur; cur = cur->next) {
1350                         gint cur_tag = GPOINTER_TO_INT(cur->data);
1351                         if (cur_tag) {
1352                                 ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->tags_unset_table, 
1353                                         GINT_TO_POINTER(cur_tag));
1354                                 if (ht_data == NULL) {
1355                                         ht_data = g_new0(TagsData, 1);
1356                                         ht_data->str = g_strdup(tags_get_tag(cur_tag));
1357                                         ht_data->item = IMAP_FOLDER_ITEM(item);
1358                                         g_hash_table_insert(IMAP_FOLDER_ITEM(item)->tags_unset_table, 
1359                                                 GINT_TO_POINTER(cur_tag), ht_data);
1360                                 }
1361                                 ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
1362                         }
1363                 }
1364         } else {
1365                 GSList *list_set = NULL;
1366                 GSList *list_unset = NULL;
1367                 GSList numlist;
1368                 
1369                 numlist.data = GINT_TO_POINTER(msginfo->msgnum);
1370                 numlist.next = NULL;
1371         
1372                 debug_print("IMAP changing tags NOW\n");
1373                 for (cur = tags_set; cur; cur = cur->next) {
1374                         gint cur_tag = GPOINTER_TO_INT(cur->data);
1375                         const gchar *str = tags_get_tag(cur_tag);
1376                         if (IS_NOT_RESERVED_TAG(str))
1377                                 list_set = g_slist_prepend(list_set, g_strdup(str));
1378                 }
1379                 if (list_set) {
1380                         ok = imap_set_message_flags(session, 
1381                                 IMAP_FOLDER_ITEM(item), &numlist, 0, list_set, TRUE);
1382                         slist_free_strings_full(list_set);
1383                         if (ok != MAILIMAP_NO_ERROR) {
1384                                 return;
1385                         }
1386                 }
1387
1388                 for (cur = tags_unset; cur; cur = cur->next) {
1389                         gint cur_tag = GPOINTER_TO_INT(cur->data);
1390                         const gchar *str = tags_get_tag(cur_tag);
1391                         if (IS_NOT_RESERVED_TAG(str))
1392                                 list_unset = g_slist_prepend(list_unset, g_strdup(str));
1393                 }
1394                 if (list_unset) {
1395                         ok = imap_set_message_flags(session, 
1396                                 IMAP_FOLDER_ITEM(item), &numlist, 0, list_unset, FALSE);
1397                         slist_free_strings_full(list_unset);
1398                         if (ok != MAILIMAP_NO_ERROR) {
1399                                 return;
1400                         }
1401                 }
1402         }
1403 }
1404
1405 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1406                                   gboolean headers, gboolean body)
1407 {
1408         gchar *path, *filename;
1409         IMAPSession *session;
1410         gint ok;
1411
1412         g_return_val_if_fail(folder != NULL, NULL);
1413         g_return_val_if_fail(item != NULL, NULL);
1414
1415         if (uid == 0)
1416                 return NULL;
1417
1418         path = folder_item_get_path(item);
1419         if (!is_dir_exist(path))
1420                 make_dir_hier(path);
1421         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1422         g_free(path);
1423         debug_print("trying to fetch cached %s\n", filename);
1424         if (is_file_exist(filename)) {
1425                 /* see whether the local file represents the whole message
1426                  * or not. As the IMAP server reports size with \r chars,
1427                  * we have to update the local file (UNIX \n only) size */
1428                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1429                 guint have_size = -1;
1430
1431                 if (cached)
1432                         debug_print("message %d has been already %scached.\n", uid,
1433                                 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1434                 
1435                 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1436                         have_size = get_file_size_with_crs(filename);
1437                         if (cached && (cached->size <= have_size || !body)) {
1438                                 procmsg_msginfo_free(cached);
1439                                 ok = file_strip_crs(filename);
1440                                 if (ok == 0 && cached && cached->size <= have_size) {
1441                                         /* we have it all and stripped */
1442                                         debug_print("...fully cached in fact; setting flag.\n");
1443                                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1444                                 }
1445                                 return filename;
1446                         } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1447                                 debug_print("message not cached and file recent, considering file complete\n");
1448                                 ok = file_strip_crs(filename);
1449                                 if (ok == 0)
1450                                         return filename;
1451                         } else {
1452                                 procmsg_msginfo_free(cached);
1453                         }
1454                 }
1455                 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1456                         procmsg_msginfo_free(cached);
1457                         return filename;
1458                 }
1459         } else {
1460                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1461                 if (cached) {
1462                         procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1463                         procmsg_msginfo_free(cached);
1464                 }
1465         }
1466
1467         debug_print("getting session...\n");
1468         session = imap_session_get(folder);
1469         
1470         if (!session) {
1471                 g_free(filename);
1472                 return NULL;
1473         }
1474         session_set_access_time(SESSION(session));
1475         lock_session(session); /* unlocked later in the function */
1476
1477         debug_print("IMAP fetching messages\n");
1478         ok = imap_select(session, IMAP_FOLDER(folder), item,
1479                          NULL, NULL, NULL, NULL, NULL, FALSE);
1480         if (ok != MAILIMAP_NO_ERROR) {
1481                 g_warning("can't select mailbox %s\n", item->path);
1482                 g_free(filename);
1483                 return NULL;
1484         }
1485
1486         session_set_access_time(SESSION(session));
1487
1488         debug_print("getting message %d...\n", uid);
1489         ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1490
1491         if (ok != MAILIMAP_NO_ERROR) {
1492                 g_warning("can't fetch message %d\n", uid);
1493                 g_free(filename);
1494                 return NULL;
1495         }
1496
1497         session_set_access_time(SESSION(session));
1498         unlock_session(session);
1499
1500         ok = file_strip_crs(filename);
1501
1502         if (ok == 0 && headers && body) {
1503                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1504                 if (cached) {
1505                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1506                         procmsg_msginfo_free(cached);
1507                 }
1508         } else if (ok == -1) {
1509                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1510                 if (cached) {
1511                         procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1512                         procmsg_msginfo_free(cached);
1513                 }
1514         }
1515         return filename;
1516 }
1517
1518 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1519 {
1520         gchar *path, *filename;
1521         guint size = 0;
1522         MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1523         
1524         if (!cached)
1525                 return FALSE;
1526
1527         if (MSG_IS_FULLY_CACHED(cached->flags)) {
1528                 procmsg_msginfo_free(cached);
1529                 return TRUE;
1530         }
1531         path = folder_item_get_path(item);
1532         if (!is_dir_exist(path))
1533                 return FALSE;
1534
1535         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1536         g_free(path);
1537         if (is_file_exist(filename)) {
1538                 if (cached && cached->total_size == cached->size) {
1539                         /* fast path */
1540                         g_free(filename);
1541                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1542                         return TRUE;
1543                 }
1544                 size = get_file_size_with_crs(filename);
1545                 g_free(filename);
1546         }
1547         if (cached && size >= cached->size) {
1548                 cached->total_size = cached->size;
1549                 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1550                 procmsg_msginfo_free(cached);
1551                 return TRUE;
1552         }
1553         if (cached)
1554                 procmsg_msginfo_free(cached);
1555         return FALSE;   
1556 }
1557
1558 void imap_cache_msg(FolderItem *item, gint msgnum)
1559 {
1560         Folder *folder = NULL;
1561         
1562         if (!item)
1563                 return;
1564         folder = item->folder;
1565         
1566         if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1567                 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1568                 debug_print("fetched %s\n", tmp);
1569                 g_free(tmp);
1570         }
1571 }
1572
1573 static gint imap_add_msg(Folder *folder, FolderItem *dest, 
1574                          const gchar *file, MsgFlags *flags)
1575 {
1576         gint ret;
1577         GSList file_list;
1578         MsgFileInfo fileinfo;
1579
1580         g_return_val_if_fail(file != NULL, -1);
1581
1582         fileinfo.msginfo = NULL;
1583         fileinfo.file = (gchar *)file;
1584         fileinfo.flags = flags;
1585         file_list.data = &fileinfo;
1586         file_list.next = NULL;
1587
1588         ret = imap_add_msgs(folder, dest, &file_list, NULL);
1589         return ret;
1590 }
1591
1592 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1593                    GHashTable *relation)
1594 {
1595         gchar *destdir;
1596         IMAPSession *session;
1597         guint32 last_uid = 0;
1598         GSList *cur;
1599         MsgFileInfo *fileinfo;
1600         gint ok = MAILIMAP_NO_ERROR;
1601         gint curnum = 0, total = 0;
1602         gboolean missing_uids = FALSE;
1603
1604         g_return_val_if_fail(folder != NULL, -1);
1605         g_return_val_if_fail(dest != NULL, -1);
1606         g_return_val_if_fail(file_list != NULL, -1);
1607         
1608         debug_print("getting session...\n");
1609         session = imap_session_get(folder);
1610         if (!session) {
1611                 return -1;
1612         }
1613         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1614         if (is_fatal(ok))
1615                 return -1;
1616         statusbar_print_all(_("Adding messages..."));
1617         total = g_slist_length(file_list);
1618         for (cur = file_list; cur != NULL; cur = cur->next) {
1619                 IMAPFlags iflags = 0;
1620                 guint32 new_uid = 0;
1621                 gchar *real_file = NULL;
1622                 fileinfo = (MsgFileInfo *)cur->data;
1623
1624                 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1625                 curnum++;
1626
1627                 if (fileinfo->flags) {
1628                         if (MSG_IS_MARKED(*fileinfo->flags))
1629                                 iflags |= IMAP_FLAG_FLAGGED;
1630                         if (MSG_IS_REPLIED(*fileinfo->flags))
1631                                 iflags |= IMAP_FLAG_ANSWERED;
1632                         if (MSG_IS_FORWARDED(*fileinfo->flags))
1633                                 iflags |= IMAP_FLAG_FORWARDED;
1634                         if (MSG_IS_SPAM(*fileinfo->flags))
1635                                 iflags |= IMAP_FLAG_SPAM;
1636                         else
1637                                 iflags |= IMAP_FLAG_HAM;
1638                         if (!MSG_IS_UNREAD(*fileinfo->flags))
1639                                 iflags |= IMAP_FLAG_SEEN;
1640                         
1641                 }
1642                 
1643                 if (real_file == NULL)
1644                         real_file = g_strdup(fileinfo->file);
1645                 
1646                 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1647                     folder_has_parent_of_type(dest, F_OUTBOX) ||
1648                     folder_has_parent_of_type(dest, F_DRAFT) ||
1649                     folder_has_parent_of_type(dest, F_TRASH))
1650                         iflags |= IMAP_FLAG_SEEN;
1651
1652                 ok = imap_cmd_append(session, IMAP_FOLDER_ITEM(dest), destdir, real_file, iflags, 
1653                                      &new_uid);
1654
1655                 if (ok != MAILIMAP_NO_ERROR) {
1656                         g_warning("can't append message %s\n", real_file);
1657                         g_free(real_file);
1658                         g_free(destdir);
1659                         statusbar_progress_all(0,0,0);
1660                         statusbar_pop_all();
1661                         return -1;
1662                 } else {
1663                         debug_print("appended new message as %d\n", new_uid);
1664                         /* put the local file in the imapcache, so that we don't
1665                          * have to fetch it back later. */
1666                         
1667                         if (new_uid == 0) {
1668                                 missing_uids = TRUE;
1669                                 debug_print("Missing UID (0)\n");
1670                         }
1671                         if (new_uid > 0) {
1672                                 gchar *cache_path = folder_item_get_path(dest);
1673                                 if (!is_dir_exist(cache_path))
1674                                         make_dir_hier(cache_path);
1675                                 if (is_dir_exist(cache_path)) {
1676                                         gchar *cache_file = g_strconcat(
1677                                                 cache_path, G_DIR_SEPARATOR_S, 
1678                                                 itos(new_uid), NULL);
1679                                         copy_file(real_file, cache_file, TRUE);
1680                                         debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1681                                         g_free(cache_file);
1682                                 }
1683                                 g_free(cache_path);
1684                         }
1685                 }
1686
1687                 if (relation != NULL)
1688                         g_hash_table_insert(relation, fileinfo->msginfo != NULL ? 
1689                                           (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1690                                           GINT_TO_POINTER(new_uid));
1691                 if (last_uid < new_uid) {
1692                         last_uid = new_uid;
1693                 }
1694
1695                 g_free(real_file);
1696         }
1697         
1698         statusbar_progress_all(0,0,0);
1699         statusbar_pop_all();
1700         
1701         
1702         g_free(destdir);
1703
1704         imap_scan_required(folder, dest);
1705
1706         session = imap_session_get(folder);
1707         if (!session) {
1708                 return -1;
1709         }
1710         if (missing_uids) {
1711                 gint a;
1712                 ok = imap_select(session, IMAP_FOLDER(folder), dest,
1713                          &a, NULL, NULL, NULL, NULL, FALSE);
1714         }
1715         return last_uid;
1716 }
1717
1718 static GSList *flatten_mailimap_set(struct mailimap_set * set) 
1719 {
1720         GSList *result = NULL;
1721         clistiter *list;
1722         guint32 start, end, t;
1723         GSList *cur;
1724
1725         if (!set || !set->set_list)
1726                 return NULL;
1727
1728         for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1729                 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1730                 start = item->set_first;
1731                 end = item->set_last;
1732                 if (start <= end) {
1733                         for (t = start; t <= end; t++) {
1734                                 result = g_slist_prepend(result, GINT_TO_POINTER(t));
1735                         }
1736                 }
1737         }
1738         result = g_slist_reverse(result);
1739         if (debug_get_mode()) {
1740                 debug_print("flat imap set: ");
1741                 for (cur = result; cur; cur = cur->next) {
1742                         debug_print("%d ", GPOINTER_TO_INT(cur->data));
1743                 }
1744                 debug_print("\n");
1745         }
1746         
1747         return result;
1748 }
1749 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, 
1750                               MsgInfoList *msglist, GHashTable *relation)
1751 {
1752         FolderItem *src;
1753         gchar *destdir;
1754         GSList *seq_list, *cur;
1755         MsgInfo *msginfo;
1756         IMAPSession *session;
1757         gint ok = MAILIMAP_NO_ERROR;
1758         GHashTable *uid_hash;
1759         gint last_num = 0;
1760
1761         g_return_val_if_fail(folder != NULL, -1);
1762         g_return_val_if_fail(dest != NULL, -1);
1763         g_return_val_if_fail(msglist != NULL, -1);
1764         
1765         debug_print("getting session...\n");
1766         session = imap_session_get(folder);
1767         
1768         if (!session) {
1769                 return -1;
1770         }
1771
1772         msginfo = (MsgInfo *)msglist->data;
1773         src = msginfo->folder;
1774         if (src == dest) {
1775                 g_warning("the src folder is identical to the dest.\n");
1776                 return -1;
1777         }
1778
1779         if (src->folder != dest->folder) {
1780                 GSList *infolist = NULL, *cur;
1781                 int res = -1;
1782                 for (cur = msglist; cur; cur = cur->next) {
1783                         msginfo = (MsgInfo *)cur->data;
1784                         MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1785                         fileinfo->file = procmsg_get_message_file(msginfo);
1786                         fileinfo->flags = &(msginfo->flags);
1787                         infolist = g_slist_prepend(infolist, fileinfo);
1788                 }
1789                 infolist = g_slist_reverse(infolist);
1790                 res = folder_item_add_msgs(dest, infolist, FALSE);
1791                 for (cur = infolist; cur; cur = cur->next) {
1792                         MsgFileInfo *info = (MsgFileInfo *)cur->data;
1793                         g_free(info->file);
1794                         g_free(info);
1795                 }
1796                 g_slist_free(infolist);
1797                 return res;
1798         } 
1799
1800         lock_session(session); /* unlocked later in the function */
1801
1802         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
1803                          NULL, NULL, NULL, NULL, NULL, FALSE);
1804         if (ok != MAILIMAP_NO_ERROR) {
1805                 return ok;
1806         }
1807
1808         unlock_session(session);
1809
1810         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
1811
1812         if (is_fatal(ok))
1813                 return ok;
1814
1815         seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
1816         uid_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
1817         
1818         statusbar_print_all(_("Copying messages..."));
1819         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1820                 struct mailimap_set * seq_set;
1821                 struct mailimap_set * source = NULL;
1822                 struct mailimap_set * dest = NULL;
1823                 seq_set = cur->data;
1824
1825                 debug_print("Copying messages from %s to %s ...\n",
1826                             src->path, destdir);
1827
1828                 lock_session(session); /* unlocked later in the function */
1829                 ok = imap_cmd_copy(session, seq_set, destdir,
1830                         &source, &dest);
1831                 
1832                 if (is_fatal(ok)) {
1833                         session = NULL;
1834                 }
1835
1836                 if (ok == MAILIMAP_NO_ERROR) {
1837                         unlock_session(session);
1838                         if (relation && source && dest) {
1839                                 GSList *s_list = flatten_mailimap_set(source);
1840                                 GSList *d_list = flatten_mailimap_set(dest);
1841                                 GSList *s_cur, *d_cur;
1842                                 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1843
1844                                         for (s_cur = s_list, d_cur = d_list; 
1845                                              s_cur && d_cur; 
1846                                              s_cur = s_cur->next, d_cur = d_cur->next) {
1847                                                 g_hash_table_insert(uid_hash, s_cur->data, d_cur->data);
1848                                         }
1849
1850                                 } else {
1851                                         debug_print("hhhmm, source list length != dest list length.\n");
1852                                 }
1853                                 g_slist_free(s_list);
1854                                 g_slist_free(d_list);
1855                         }
1856                 }
1857
1858
1859                 if (source)
1860                         mailimap_set_free(source);
1861                 if (dest)
1862                         mailimap_set_free(dest);
1863
1864                 if (ok != MAILIMAP_NO_ERROR) {
1865                         g_hash_table_destroy(uid_hash);
1866                         imap_lep_set_free(seq_list);
1867                         statusbar_pop_all();
1868                         return -1;
1869                 }
1870         }
1871
1872         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1873                 MsgInfo *msginfo = (MsgInfo *)cur->data;
1874                 gpointer hashval;
1875
1876                 hashval = g_hash_table_lookup(uid_hash, GINT_TO_POINTER(msginfo->msgnum));
1877                 
1878                 if (hashval != NULL) {
1879                         gint num = GPOINTER_TO_INT(hashval);
1880                         g_hash_table_insert(relation, msginfo,
1881                                           GINT_TO_POINTER(num));
1882                         if (num > last_num)
1883                                 last_num = num;
1884                         debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1885                         /* put the local file in the imapcache, so that we don't
1886                          * have to fetch it back later. */
1887                         if (num > 0) {
1888                                 gchar *cache_path = folder_item_get_path(msginfo->folder);
1889                                 gchar *real_file = g_strconcat(
1890                                         cache_path, G_DIR_SEPARATOR_S, 
1891                                         itos(msginfo->msgnum), NULL);
1892                                 gchar *cache_file = NULL;
1893                                 g_free(cache_path);
1894                                 cache_path = folder_item_get_path(dest);
1895                                 cache_file = g_strconcat(
1896                                         cache_path, G_DIR_SEPARATOR_S, 
1897                                         itos(num), NULL);
1898                                 if (!is_dir_exist(cache_path))
1899                                         make_dir_hier(cache_path);
1900                                 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1901                                         copy_file(real_file, cache_file, TRUE);
1902                                         debug_print("copied to cache: %s\n", cache_file);
1903                                 }
1904                                 g_free(real_file);
1905                                 g_free(cache_file);
1906                                 g_free(cache_path);
1907                         }
1908                 } else
1909                         g_hash_table_insert(relation, msginfo,
1910                                           GINT_TO_POINTER(0));
1911         }
1912         statusbar_pop_all();
1913
1914         g_hash_table_destroy(uid_hash);
1915         imap_lep_set_free(seq_list);
1916
1917         g_free(destdir);
1918         
1919         IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1920         IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1921         g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1922         IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1923
1924         imap_scan_required(folder, dest);
1925         if (ok == MAILIMAP_NO_ERROR)
1926                 return last_num;
1927         else
1928                 return -1;
1929 }
1930
1931 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1932 {
1933         GSList msglist;
1934
1935         g_return_val_if_fail(msginfo != NULL, -1);
1936
1937         msglist.data = msginfo;
1938         msglist.next = NULL;
1939
1940         return imap_copy_msgs(folder, dest, &msglist, NULL);
1941 }
1942
1943 static gint imap_copy_msgs(Folder *folder, FolderItem *dest, 
1944                     MsgInfoList *msglist, GHashTable *relation)
1945 {
1946         MsgInfo *msginfo;
1947         gint ret;
1948
1949         g_return_val_if_fail(folder != NULL, -1);
1950         g_return_val_if_fail(dest != NULL, -1);
1951         g_return_val_if_fail(msglist != NULL, -1);
1952
1953         msginfo = (MsgInfo *)msglist->data;
1954         g_return_val_if_fail(msginfo->folder != NULL, -1);
1955
1956         ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1957         return ret;
1958 }
1959
1960
1961 static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
1962 {
1963         if (match->matchtype == MATCHTYPE_MATCHCASE || match->matchtype == MATCHTYPE_MATCH) {
1964                 IMAPSearchKey* result = NULL;
1965                 gboolean invert = FALSE;
1966                 gint matchertype = match->criteria;
1967
1968                 if (is_all) {
1969                         *is_all = FALSE;
1970                 }
1971
1972                 switch (matchertype) {
1973                 case MATCHCRITERIA_NOT_NEW: invert = TRUE; matchertype = MATCHCRITERIA_NEW; break;
1974                 case MATCHCRITERIA_NOT_MARKED: invert = TRUE; matchertype = MATCHCRITERIA_MARKED; break;
1975                 case MATCHCRITERIA_NOT_FORWARDED: invert = TRUE; matchertype = MATCHCRITERIA_FORWARDED; break;
1976                 case MATCHCRITERIA_NOT_SPAM: invert = TRUE; matchertype = MATCHCRITERIA_SPAM; break;
1977                 case MATCHCRITERIA_NOT_SUBJECT: invert = TRUE; matchertype = MATCHCRITERIA_SUBJECT; break;
1978                 case MATCHCRITERIA_NOT_FROM: invert = TRUE; matchertype = MATCHCRITERIA_FROM; break;
1979                 case MATCHCRITERIA_NOT_TO: invert = TRUE; matchertype = MATCHCRITERIA_TO; break;
1980                 case MATCHCRITERIA_NOT_CC: invert = TRUE; matchertype = MATCHCRITERIA_CC; break;
1981                 case MATCHCRITERIA_NOT_REFERENCES: invert = TRUE; matchertype = MATCHCRITERIA_REFERENCES; break;
1982                 case MATCHCRITERIA_NOT_HEADER: invert = TRUE; matchertype = MATCHCRITERIA_HEADER; break;
1983                 case MATCHCRITERIA_NOT_TAG: invert = TRUE; matchertype = MATCHCRITERIA_TAG; break;
1984                 case MATCHCRITERIA_NOT_HEADERS_PART: invert = TRUE; matchertype = MATCHCRITERIA_HEADERS_PART; break;
1985                 case MATCHCRITERIA_NOT_MESSAGE: invert = TRUE; matchertype = MATCHCRITERIA_MESSAGE; break;
1986                 case MATCHCRITERIA_NOT_BODY_PART: invert = TRUE; matchertype = MATCHCRITERIA_BODY_PART; break;
1987                 case MATCHCRITERIA_NOT_TO_AND_NOT_CC: invert = TRUE; matchertype = MATCHCRITERIA_TO_OR_CC; break;
1988                 case MATCHCRITERIA_NOT_INREPLYTO: invert = TRUE; matchertype = MATCHCRITERIA_INREPLYTO; break;
1989                 }
1990
1991                 /* 
1992                  * this aborts conversion even for predicates understood by the following code.
1993                  * while that might seem wasteful, claws local search for information listed below
1994                  * has proven faster than IMAP search plus network roundtrips. once this changes,
1995                  * consider removing these exceptions.
1996                  */
1997                 switch (matchertype) {
1998                 case MATCHCRITERIA_FROM:
1999                 case MATCHCRITERIA_TO:
2000                 case MATCHCRITERIA_CC:
2001                 case MATCHCRITERIA_TO_OR_CC:
2002                 case MATCHCRITERIA_SUBJECT:
2003                 case MATCHCRITERIA_REFERENCES:
2004                 case MATCHCRITERIA_INREPLYTO:
2005                 case MATCHCRITERIA_AGE_GREATER:
2006                 case MATCHCRITERIA_AGE_LOWER:
2007                 case MATCHCRITERIA_FORWARDED:
2008                 case MATCHCRITERIA_SPAM:
2009                 case MATCHCRITERIA_UNREAD:
2010                 case MATCHCRITERIA_NEW:
2011                 case MATCHCRITERIA_MARKED:
2012                 case MATCHCRITERIA_REPLIED:
2013                 case MATCHCRITERIA_DELETED:
2014                 case MATCHCRITERIA_SIZE_GREATER:
2015                 case MATCHCRITERIA_SIZE_SMALLER:
2016                 case MATCHCRITERIA_SIZE_EQUAL:
2017                         return NULL;
2018                 }
2019
2020                 /* the Message-ID header is also cached */
2021                 if (matchertype == MATCHCRITERIA_HEADER && g_strcmp0("Message-ID", match->header) == 0) {
2022                         return NULL;
2023                 }
2024
2025                 switch (matchertype) {
2026                 case MATCHCRITERIA_FORWARDED:
2027                         result = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
2028                         break;
2029
2030                 case MATCHCRITERIA_SPAM:
2031                         result = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
2032                         break;
2033
2034                 case MATCHCRITERIA_INREPLYTO:
2035                         result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "In-Reply-To", match->expr, 0);
2036                         break;
2037
2038                 case MATCHCRITERIA_REFERENCES:
2039                         result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "References", match->expr, 0);
2040                         break;
2041
2042                 case MATCHCRITERIA_TO_OR_CC:
2043                         result = imap_search_or(
2044                                         imap_search_new(IMAP_SEARCH_CRITERIA_TO, NULL, match->expr, 0),
2045                                         imap_search_new(IMAP_SEARCH_CRITERIA_CC, NULL, match->expr, 0)
2046                                         );
2047                         break;
2048
2049                 case MATCHCRITERIA_HEADERS_PART:
2050                         result = imap_search_and(
2051                                         imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_BODY, NULL, match->expr, 0)),
2052                                         imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE, NULL, match->expr, 0)
2053                                         );
2054                         break;
2055
2056                 case MATCHCRITERIA_SIZE_EQUAL:
2057                         result = imap_search_and(
2058                                         imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER, NULL, NULL, match->value)),
2059                                         imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER, NULL, NULL, match->value))
2060                                         );
2061                         break;
2062
2063                 case MATCHCRITERIA_NOT_UNREAD:
2064                         result = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
2065                         break;
2066
2067                 case MATCHCRITERIA_UNREAD:
2068                         result = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
2069                         break;
2070
2071                 case MATCHCRITERIA_NEW:
2072                         result = imap_search_new(IMAP_SEARCH_CRITERIA_NEW, NULL, NULL, 0);
2073                         break;
2074
2075                 case MATCHCRITERIA_MARKED:
2076                         result = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
2077                         break;
2078
2079                 case MATCHCRITERIA_DELETED:
2080                         result = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
2081                         break;
2082
2083                 case MATCHCRITERIA_REPLIED:
2084                         result = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
2085                         break;
2086
2087                 case MATCHCRITERIA_TAG:
2088                         {
2089                                 gchar *tmp = imap_utf8_to_modified_utf7(match->expr, TRUE);
2090                                 result = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, tmp, 0);
2091                                 g_free(tmp);
2092                         }
2093                         break;
2094
2095                 case MATCHCRITERIA_SUBJECT:
2096                         result = imap_search_new(IMAP_SEARCH_CRITERIA_SUBJECT, NULL, match->expr, 0);
2097                         break;
2098
2099                 case MATCHCRITERIA_FROM:
2100                         result = imap_search_new(IMAP_SEARCH_CRITERIA_FROM, NULL, match->expr, 0);
2101                         break;
2102
2103                 case MATCHCRITERIA_TO:
2104                         result = imap_search_new(IMAP_SEARCH_CRITERIA_TO, NULL, match->expr, 0);
2105                         break;
2106
2107                 case MATCHCRITERIA_CC:
2108                         result = imap_search_new(IMAP_SEARCH_CRITERIA_CC, NULL, match->expr, 0);
2109                         break;
2110
2111                 case MATCHCRITERIA_AGE_GREATER:
2112                         result = imap_search_new(IMAP_SEARCH_CRITERIA_AGE_GREATER, NULL, NULL, match->value);
2113                         break;
2114
2115                 case MATCHCRITERIA_AGE_LOWER:
2116                         result = imap_search_new(IMAP_SEARCH_CRITERIA_AGE_LOWER, NULL, NULL, match->value);
2117                         break;
2118
2119                 case MATCHCRITERIA_BODY_PART:
2120                         result = imap_search_new(IMAP_SEARCH_CRITERIA_BODY, NULL, match->expr, 0);
2121                         break;
2122
2123                 case MATCHCRITERIA_MESSAGE:
2124                         result = imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE, NULL, match->expr, 0);
2125                         break;
2126
2127                 case MATCHCRITERIA_HEADER:
2128                         result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, match->header, match->expr, 0);
2129                         break;
2130
2131                 case MATCHCRITERIA_SIZE_GREATER:
2132                         result = imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_GREATER, NULL, NULL, match->value);
2133                         break;
2134
2135                 case MATCHCRITERIA_SIZE_SMALLER:
2136                         result = imap_search_new(IMAP_SEARCH_CRITERIA_SIZE_SMALLER, NULL, NULL, match->value);
2137                         break;
2138
2139                 default:
2140                         result = imap_search_new(IMAP_SEARCH_CRITERIA_ALL, NULL, NULL, 0);
2141                         if (is_all) {
2142                                 *is_all = TRUE;
2143                         }
2144                         break;
2145                 }
2146
2147                 if (invert) {
2148                         result = imap_search_not(result);
2149                         if (is_all && *is_all) {
2150                                 *is_all = FALSE;
2151                         }
2152                 }
2153
2154                 return result;
2155         }
2156
2157         return NULL;
2158 }
2159
2160 static gint     search_msgs             (Folder                 *folder,
2161                                          FolderItem             *container,
2162                                          MsgNumberList          **msgs,
2163                                          gboolean               *on_server,
2164                                          MatcherList            *predicate,
2165                                          SearchProgressNotify   progress_cb,
2166                                          gpointer               progress_data)
2167 {
2168         IMAPSearchKey* key = NULL;
2169         GSList* cur;
2170         int result = -1;
2171         clist* uidlist = NULL;
2172         gboolean server_filtering_useless = FALSE;
2173         IMAPSession *session;
2174
2175         if (on_server == NULL || !*on_server) {
2176                 return folder_item_search_msgs_local(folder, container, msgs, on_server,
2177                                 predicate, progress_cb, progress_data);
2178         }
2179
2180         for (cur = predicate->matchers; cur != NULL; cur = cur->next) {
2181                 IMAPSearchKey* matcherPart = NULL;
2182                 MatcherProp* prop = (MatcherProp*) cur->data;
2183                 gboolean is_all;
2184
2185                 matcherPart = search_make_key(prop, &is_all);
2186
2187                 if (on_server) {
2188                         *on_server &= matcherPart != NULL && prop->matchtype == MATCHTYPE_MATCHCASE;
2189                 }
2190
2191                 if (matcherPart) {
2192                         if (key == NULL) {
2193                                 key = matcherPart;
2194                                 server_filtering_useless = is_all;
2195                         } else if (predicate->bool_and) {
2196                                 key = imap_search_and(key, matcherPart);
2197                                 server_filtering_useless &= is_all;
2198                         } else {
2199                                 key = imap_search_or(key, matcherPart);
2200                                 server_filtering_useless |= is_all;
2201                         }
2202                 }
2203         }
2204
2205         if (server_filtering_useless) {
2206                 imap_search_free(key);
2207                 key = NULL;
2208         }
2209
2210         if (key == NULL && progress_cb != NULL) {
2211                 GSList* cur;
2212                 GSList* list;
2213                 int count = 0;
2214
2215                 progress_cb(progress_data, TRUE, 0, 0, container->total_msgs);
2216                 progress_cb(progress_data, TRUE, container->total_msgs, 0, container->total_msgs);
2217
2218                 list = folder_item_get_msg_list(container);
2219                 for (cur = list; cur != NULL; cur = cur->next) {
2220                         *msgs = g_slist_prepend(*msgs, GUINT_TO_POINTER(((MsgInfo*) cur->data)->msgnum));
2221                         count++;
2222                 }
2223                 *msgs = g_slist_reverse(*msgs);
2224                 return count;
2225         }
2226
2227         session = imap_session_get(folder);
2228         if (!session) {
2229                 return -1;
2230         }
2231         result = imap_select(session, IMAP_FOLDER(folder), FOLDER_ITEM(container),
2232                          NULL, NULL, NULL, NULL, NULL, TRUE);
2233         if (result != MAILIMAP_NO_ERROR)
2234                 return -1;
2235
2236         if (progress_cb)
2237                 progress_cb(progress_data, TRUE, 0, 0, container->total_msgs);
2238         result = imap_threaded_search(folder, IMAP_SEARCH_TYPE_KEYED, key, NULL, &uidlist);
2239         if (progress_cb)
2240                 progress_cb(progress_data, TRUE, container->total_msgs, 0, container->total_msgs);
2241
2242         if (result == MAILIMAP_NO_ERROR) {
2243                 gint result = 0;
2244
2245                 *msgs = imap_uid_list_from_lep(uidlist, &result);
2246                 mailimap_search_result_free(uidlist);
2247
2248                 return result;
2249         } else {
2250                 return -1;
2251         }
2252 }
2253
2254
2255 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
2256                                 MsgInfoList *msglist, GHashTable *relation)
2257 {
2258         gchar *destdir, *dir;
2259         GSList *numlist = NULL, *cur;
2260         MsgInfo *msginfo;
2261         IMAPSession *session;
2262         gint ok = MAILIMAP_NO_ERROR;
2263         
2264         g_return_val_if_fail(folder != NULL, -1);
2265         g_return_val_if_fail(dest != NULL, -1);
2266         g_return_val_if_fail(msglist != NULL, -1);
2267
2268         debug_print("getting session...\n");
2269         session = imap_session_get(folder);
2270         if (!session) {
2271                 return -1;
2272         }
2273
2274         lock_session(session); /* unlocked later in the function */
2275
2276         msginfo = (MsgInfo *)msglist->data;
2277
2278         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder,
2279                          NULL, NULL, NULL, NULL, NULL, FALSE);
2280         if (ok != MAILIMAP_NO_ERROR) {
2281                 return ok;
2282         }
2283
2284         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
2285         if (is_fatal(ok)) {
2286                 g_free(destdir);
2287                 return ok;
2288         }
2289         for (cur = msglist; cur; cur = cur->next) {
2290                 msginfo = (MsgInfo *)cur->data;
2291                 if (!MSG_IS_DELETED(msginfo->flags))
2292                         numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
2293         }
2294         numlist = g_slist_reverse(numlist);
2295
2296         if (numlist != NULL) {
2297                 ok = imap_set_message_flags
2298                         (session, IMAP_FOLDER_ITEM(msginfo->folder), numlist, IMAP_FLAG_DELETED, NULL, TRUE);
2299                 if (ok != MAILIMAP_NO_ERROR) {
2300                         log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
2301                         g_free(destdir);
2302                         return ok;
2303                 }
2304         } /* else we just need to expunge */
2305         ok = imap_cmd_expunge(session, folder->account->imap_use_trash);
2306         if (ok != MAILIMAP_NO_ERROR) {
2307                 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
2308                 g_free(destdir);
2309                 return ok;
2310         }
2311         
2312         session->folder_content_changed = TRUE;
2313         unlock_session(session);
2314
2315         dir = folder_item_get_path(msginfo->folder);
2316         if (is_dir_exist(dir)) {
2317                 for (cur = msglist; cur; cur = cur->next) {
2318                         msginfo = (MsgInfo *)cur->data;
2319                         remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
2320                 }
2321         }
2322         g_free(dir);
2323
2324         g_slist_free(numlist);
2325
2326         imap_scan_required(folder, dest);
2327
2328         g_free(destdir);
2329         if (ok == MAILIMAP_NO_ERROR)
2330                 return 0;
2331         else
2332                 return -1;
2333 }
2334
2335 static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
2336                     MsgInfoList *msglist, GHashTable *relation)
2337 {
2338         MsgInfo *msginfo;
2339
2340         g_return_val_if_fail(folder != NULL, -1);
2341         g_return_val_if_fail(dest != NULL, -1);
2342         if (msglist == NULL)
2343                 return 0;
2344
2345         msginfo = (MsgInfo *)msglist->data;
2346         g_return_val_if_fail(msginfo->folder != NULL, -1);
2347
2348         return imap_do_remove_msgs(folder, dest, msglist, relation);
2349 }
2350
2351 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
2352 {
2353         GSList *list = folder_item_get_msg_list(item);
2354         gint res = imap_remove_msgs(folder, item, list, NULL);
2355         procmsg_msg_list_free(list);
2356         return res;
2357 }
2358
2359 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
2360                                     MsgInfo *msginfo)
2361 {
2362         /* TODO: properly implement this method */
2363         return FALSE;
2364 }
2365
2366 static gint imap_close(Folder *folder, FolderItem *item)
2367 {
2368         return 0;
2369 }
2370
2371 static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
2372 {
2373         FolderItem *item = NULL;
2374         IMAPSession *session;
2375         gchar *root_folder = NULL;
2376
2377         g_return_val_if_fail(folder != NULL, -1);
2378         g_return_val_if_fail(folder->account != NULL, -1);
2379
2380         debug_print("getting session...\n");
2381         session = imap_session_get(folder);
2382         if (!session) {
2383                 if (!folder->node) {
2384                         folder_tree_destroy(folder);
2385                         item = folder_item_new(folder, folder->name, NULL);
2386                         item->folder = folder;
2387                         folder->node = item->node = g_node_new(item);
2388                 }
2389                 return -1;
2390         }
2391
2392         if (folder->account->imap_dir && *folder->account->imap_dir) {
2393                 gchar *real_path;
2394                 int r = MAILIMAP_NO_ERROR;
2395                 clist * lep_list;
2396
2397                 Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
2398                 extract_quote(root_folder, '"');
2399                 subst_char(root_folder,
2400                            imap_get_path_separator(session, IMAP_FOLDER(folder),
2401                                                    root_folder, &r),
2402                            '/');
2403                 if (is_fatal(r))
2404                         return -1;
2405                 strtailchomp(root_folder, '/');
2406                 real_path = imap_get_real_path
2407                         (session, IMAP_FOLDER(folder), root_folder, &r);
2408                 if (is_fatal(r))
2409                         return -1;
2410                 debug_print("IMAP root directory: %s\n", real_path);
2411
2412                 /* check if root directory exist */
2413
2414                 r = imap_threaded_list(session->folder, "", real_path,
2415                                        &lep_list);
2416
2417                 if (r != MAILIMAP_NO_ERROR)
2418                         imap_handle_error(SESSION(session), NULL, r);
2419
2420                 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
2421                         if (!folder->node) {
2422                                 item = folder_item_new(folder, folder->name, NULL);
2423                                 item->folder = folder;
2424                                 folder->node = item->node = g_node_new(item);
2425                         }
2426                         return -1;
2427                 }
2428                 mailimap_list_result_free(lep_list);
2429                                 
2430                 g_free(real_path);
2431         }
2432
2433         if (folder->node)
2434                 item = FOLDER_ITEM(folder->node->data);
2435                 
2436         if (item && !item->path && root_folder) {
2437                 item->path = g_strdup(root_folder);
2438         }
2439
2440         if (!item || ((item->path || root_folder) &&
2441                       strcmp2(item->path, root_folder) != 0)) {
2442                 folder_tree_destroy(folder);
2443                 item = folder_item_new(folder, folder->name, root_folder);
2444                 item->folder = folder;
2445                 folder->node = item->node = g_node_new(item);
2446         }
2447
2448         imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
2449         imap_create_missing_folders(folder);
2450
2451         return 0;
2452 }
2453
2454 static gint imap_scan_tree(Folder *folder)
2455 {
2456         gboolean subs_only = FALSE;
2457         if (folder->account) {
2458                 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
2459                 subs_only = folder->account->imap_subsonly;
2460         }
2461         return imap_scan_tree_real(folder, subs_only);
2462 }
2463
2464 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
2465 {
2466         Folder *folder;
2467         IMAPFolder *imapfolder;
2468         FolderItem *new_item;
2469         GSList *item_list, *cur;
2470         GNode *node;
2471         gchar *real_path;
2472         gchar *wildcard_path;
2473         gchar separator;
2474         gchar wildcard[3];
2475         clist * lep_list;
2476         int r = MAILIMAP_NO_ERROR;
2477         
2478         g_return_val_if_fail(item != NULL, -1);
2479         g_return_val_if_fail(item->folder != NULL, -1);
2480         g_return_val_if_fail(item->no_sub == FALSE, -1);
2481
2482         folder = item->folder;
2483         imapfolder = IMAP_FOLDER(folder);
2484
2485         separator = imap_get_path_separator(session, imapfolder, item->path, &r);
2486         if (is_fatal(r))
2487                 return r;
2488
2489         if (folder->ui_func)
2490                 folder->ui_func(folder, item, folder->ui_func_data);
2491
2492         if (item->path) {
2493                 wildcard[0] = separator;
2494                 wildcard[1] = '%';
2495                 wildcard[2] = '\0';
2496                 real_path = imap_get_real_path(session, imapfolder, item->path, &r);
2497                 if (is_fatal(r)) {
2498                         g_free(real_path);
2499                         return r;
2500                 }
2501         } else {
2502                 wildcard[0] = '%';
2503                 wildcard[1] = '\0';
2504                 real_path = g_strdup("");
2505         }
2506
2507         Xstrcat_a(wildcard_path, real_path, wildcard,
2508                   {g_free(real_path); return MAILIMAP_ERROR_BAD_STATE;});
2509         lep_list = NULL;
2510         
2511         if (subs_only)
2512                 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2513         else
2514                 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2515
2516         if (r != MAILIMAP_NO_ERROR) {
2517                 imap_handle_error(SESSION(session), NULL, r);
2518                 item_list = NULL;
2519                 g_free(real_path);
2520                 return r;
2521         }
2522         else {
2523                 item_list = imap_list_from_lep(imapfolder,
2524                                                lep_list, real_path, FALSE);
2525                 mailimap_list_result_free(lep_list);
2526         }
2527         
2528         g_free(real_path);
2529
2530         node = item->node->children;
2531         while (node != NULL) {
2532                 FolderItem *old_item = FOLDER_ITEM(node->data);
2533                 GNode *next = node->next;
2534
2535                 new_item = NULL;
2536                 for (cur = item_list; cur != NULL; cur = cur->next) {
2537                         FolderItem *cur_item = FOLDER_ITEM(cur->data);
2538                         if (!strcmp2(old_item->path, cur_item->path)) {
2539                                 new_item = cur_item;
2540                                 break;
2541                         }
2542                 }
2543                 if (!new_item) {
2544                         if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
2545                                 debug_print("not removing INBOX\n");
2546                         } else {
2547                                 debug_print("folder '%s' not found. removing...\n",
2548                                             old_item->path);
2549                                 folder_item_remove(old_item);
2550                         }
2551                 } else {
2552                         old_item->no_sub = new_item->no_sub;
2553                         old_item->no_select = new_item->no_select;
2554                         if (old_item->no_sub == TRUE && node->children) {
2555                                 debug_print("folder '%s' doesn't have "
2556                                             "subfolders. removing...\n",
2557                                             old_item->path);
2558                                 folder_item_remove_children(old_item);
2559                         }
2560                 }
2561
2562                 node = next;
2563         }
2564
2565         for (cur = item_list; cur != NULL; cur = cur->next) {
2566                 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2567                 new_item = NULL;
2568
2569                 for (node = item->node->children; node != NULL;
2570                      node = node->next) {
2571                         if (!strcmp2(FOLDER_ITEM(node->data)->path,
2572                                      cur_item->path)) {
2573                                 new_item = FOLDER_ITEM(node->data);
2574                                 folder_item_destroy(cur_item);
2575                                 cur_item = NULL;
2576                                 break;
2577                         }
2578                 }
2579                 if (!new_item) {
2580                         new_item = cur_item;
2581                         debug_print("new folder '%s' found.\n", new_item->path);
2582                         folder_item_append(item, new_item);
2583                 }
2584
2585                 if (!strcmp(new_item->path, "INBOX")) {
2586                         new_item->stype = F_INBOX;
2587                         folder->inbox = new_item;
2588                 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
2589                         gchar *base;
2590
2591                         base = g_path_get_basename(new_item->path);
2592
2593                         if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
2594                                 new_item->stype = F_OUTBOX;
2595                                 folder->outbox = new_item;
2596                         } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
2597                                 new_item->stype = F_DRAFT;
2598                                 folder->draft = new_item;
2599                         } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
2600                                 new_item->stype = F_QUEUE;
2601                                 folder->queue = new_item;
2602                         } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
2603                                 new_item->stype = F_TRASH;
2604                                 folder->trash = new_item;
2605                         }
2606                         g_free(base);
2607                 }
2608
2609                 if (new_item->no_sub == FALSE)
2610                         imap_scan_tree_recursive(session, new_item, subs_only);
2611         }
2612
2613         g_slist_free(item_list);
2614
2615         return MAILIMAP_NO_ERROR;
2616 }
2617
2618 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
2619 {
2620         IMAPSession *session = imap_session_get(folder);
2621         gchar *real_path;
2622         gchar *wildcard_path;
2623         gchar separator;
2624         gchar wildcard[3];
2625         clist * lep_list;
2626         GSList *item_list = NULL, *cur;
2627         GList *child_list = NULL, *tmplist = NULL;
2628         GSList *sub_list = NULL;
2629         int r = MAILIMAP_NO_ERROR;
2630
2631         if (!session)
2632                 return NULL;
2633
2634         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &r);
2635         if (is_fatal(r))
2636                 return NULL;
2637
2638         if (item->path) {
2639                 wildcard[0] = separator;
2640                 wildcard[1] = '%';
2641                 wildcard[2] = '\0';
2642                 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
2643                 if (is_fatal(r)) {
2644                         g_free(real_path);
2645                         return NULL;
2646                 }
2647         } else {
2648                 wildcard[0] = '%';
2649                 wildcard[1] = '\0';
2650                 real_path = g_strdup("");
2651         }
2652
2653         Xstrcat_a(wildcard_path, real_path, wildcard,
2654                   {g_free(real_path); return NULL;});
2655         lep_list = NULL;
2656         
2657         if (unsubs_only)
2658                 statusbar_print_all(_("Looking for unsubscribed folders in %s..."), 
2659                                 item->path?item->path:item->name);
2660         else
2661                 statusbar_print_all(_("Looking for subfolders of %s..."), 
2662                                 item->path?item->path:item->name);
2663
2664         r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
2665         if (r) {
2666                 g_free(real_path);
2667                 statusbar_pop_all();
2668                 return NULL;
2669         }
2670         item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2671                                lep_list, real_path, FALSE);
2672         mailimap_list_result_free(lep_list);
2673
2674         for (cur = item_list; cur != NULL; cur = cur->next) {
2675                 FolderItem *cur_item = FOLDER_ITEM(cur->data);
2676                 if (recursive) {
2677                         tmplist = imap_scan_subtree(folder, cur_item, 
2678                                         unsubs_only, recursive);
2679                         if (tmplist)
2680                                 child_list = g_list_concat(child_list, tmplist);
2681                 }
2682                 child_list = g_list_prepend(child_list,
2683                                 imap_get_real_path(session, 
2684                                         IMAP_FOLDER(folder), cur_item->path, &r));
2685                 if (is_fatal(r)) {
2686                         g_free(real_path);
2687                         statusbar_pop_all();
2688                         return NULL;
2689                 }
2690                 folder_item_destroy(cur_item);
2691         }
2692         child_list = g_list_reverse(child_list);
2693         g_slist_free(item_list);
2694
2695         if (unsubs_only) {
2696                 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
2697                 if (r) {
2698                         g_free(real_path);
2699                         statusbar_pop_all();
2700                         return NULL;
2701                 }
2702                 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
2703                                        lep_list, real_path, FALSE);
2704                 mailimap_list_result_free(lep_list);
2705
2706                 for (cur = sub_list; cur != NULL; cur = cur->next) {
2707                         FolderItem *cur_item = FOLDER_ITEM(cur->data);
2708                         GList *oldlitem = NULL;
2709                         gchar *tmp = imap_get_real_path(session, 
2710                                         IMAP_FOLDER(folder), cur_item->path, &r);
2711                         if (r) {
2712                                 g_free(real_path);
2713                                 statusbar_pop_all();
2714                                 return NULL;
2715                         }
2716                         folder_item_destroy(cur_item);
2717                         oldlitem = g_list_find_custom(
2718                                         child_list, tmp, (GCompareFunc)strcmp2);
2719                         if (oldlitem) {
2720                                 child_list = g_list_remove_link(child_list, oldlitem);
2721                                 g_free(oldlitem->data);
2722                                 g_list_free(oldlitem);
2723                         }
2724                         g_free(tmp);
2725                 }
2726         }
2727
2728         g_free(real_path);
2729         statusbar_pop_all();
2730
2731         return child_list;
2732 }
2733
2734 static gint imap_create_tree(Folder *folder)
2735 {
2736         g_return_val_if_fail(folder != NULL, -1);
2737         g_return_val_if_fail(folder->node != NULL, -1);
2738         g_return_val_if_fail(folder->node->data != NULL, -1);
2739         g_return_val_if_fail(folder->account != NULL, -1);
2740
2741         imap_scan_tree(folder);
2742         imap_create_missing_folders(folder);
2743
2744         return 0;
2745 }
2746
2747 static void imap_create_missing_folders(Folder *folder)
2748 {
2749         g_return_if_fail(folder != NULL);
2750
2751         if (!folder->inbox)
2752                 folder->inbox = imap_create_special_folder
2753                         (folder, F_INBOX, "INBOX");
2754         if (!folder->trash)
2755                 folder->trash = imap_create_special_folder
2756                         (folder, F_TRASH, "Trash");
2757         if (!folder->queue)
2758                 folder->queue = imap_create_special_folder
2759                         (folder, F_QUEUE, "Queue");
2760         if (!folder->outbox)
2761                 folder->outbox = imap_create_special_folder
2762                         (folder, F_OUTBOX, "Sent");
2763         if (!folder->draft)
2764                 folder->draft = imap_create_special_folder
2765                         (folder, F_DRAFT, "Drafts");
2766 }
2767
2768 static FolderItem *imap_create_special_folder(Folder *folder,
2769                                               SpecialFolderItemType stype,
2770                                               const gchar *name)
2771 {
2772         FolderItem *item;
2773         FolderItem *new_item;
2774
2775         g_return_val_if_fail(folder != NULL, NULL);
2776         g_return_val_if_fail(folder->node != NULL, NULL);
2777         g_return_val_if_fail(folder->node->data != NULL, NULL);
2778         g_return_val_if_fail(folder->account != NULL, NULL);
2779         g_return_val_if_fail(name != NULL, NULL);
2780
2781         item = FOLDER_ITEM(folder->node->data);
2782         new_item = imap_create_folder(folder, item, name);
2783
2784         if (!new_item) {
2785                 g_warning("Can't create '%s'\n", name);
2786                 if (!folder->inbox) return NULL;
2787
2788                 new_item = imap_create_folder(folder, folder->inbox, name);
2789                 if (!new_item)
2790                         g_warning("Can't create '%s' under INBOX\n", name);
2791                 else
2792                         new_item->stype = stype;
2793         } else
2794                 new_item->stype = stype;
2795
2796         return new_item;
2797 }
2798
2799 static gchar *imap_folder_get_path(Folder *folder)
2800 {
2801         gchar *folder_path;
2802
2803         g_return_val_if_fail(folder != NULL, NULL);
2804         g_return_val_if_fail(folder->account != NULL, NULL);
2805
2806         folder_path = g_strconcat(get_imap_cache_dir(),
2807                                   G_DIR_SEPARATOR_S,
2808                                   folder->account->recv_server,
2809                                   G_DIR_SEPARATOR_S,
2810                                   folder->account->userid,
2811                                   NULL);
2812
2813         return folder_path;
2814 }
2815
2816 #ifdef G_OS_WIN32
2817 static gchar *imap_encode_unsafe_chars(const gchar *str)
2818 {
2819         gchar *ret = NULL, *o_ret;
2820         gchar *i;
2821         if (!str) 
2822                 return NULL;
2823         ret = g_malloc(3*strlen(str)+1);
2824         o_ret = ret;
2825         for (i = str; *i; i++) {
2826                 switch(*i) {
2827                         case ':':
2828                         case '|':
2829                         case '<':
2830                         case '>':
2831                         case '*':
2832                         case '?':
2833                         case '#':
2834                                 *ret++ = '%';
2835                                 *ret++ = '0'+(*i/10);
2836                                 *ret++ = '0'+(*i%10);
2837                                 break;
2838                         default:
2839                                 *ret++ = *i;
2840                 }
2841         }
2842         *ret++ = '\0';
2843         return o_ret;
2844 }
2845 #endif
2846 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2847 {
2848         gchar *folder_path, *path;
2849         gchar *item_path = NULL;
2850         
2851         g_return_val_if_fail(folder != NULL, NULL);
2852         g_return_val_if_fail(item != NULL, NULL);
2853         folder_path = imap_folder_get_path(folder);
2854
2855         g_return_val_if_fail(folder_path != NULL, NULL);
2856
2857 #ifdef G_OS_UNIX
2858         item_path = g_strdup(item->path);
2859 #else
2860         item_path = imap_encode_unsafe_chars(item->path);
2861 #endif  
2862
2863         if (g_path_is_absolute(folder_path)) {
2864                 if (item_path)
2865                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2866                                            item_path, NULL);
2867                 else
2868                         path = g_strdup(folder_path);
2869         } else {
2870                 if (item_path)
2871                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2872                                            folder_path, G_DIR_SEPARATOR_S,
2873                                            item_path, NULL);
2874                 else
2875                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2876                                            folder_path, NULL);
2877         }
2878         g_free(folder_path);
2879         g_free(item_path);
2880 #ifdef G_OS_WIN32
2881         while (strchr(path, '/'))
2882                 *strchr(path, '/') = '\\';
2883 #endif
2884
2885         return path;
2886 }
2887
2888 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2889                                const gchar *name)
2890 {
2891         gchar *dirpath, *imap_path;
2892         IMAPSession *session;
2893         FolderItem *new_item;
2894         gchar separator;
2895         gchar *new_name;
2896         const gchar *p;
2897         gint ok = MAILIMAP_NO_ERROR;
2898         gboolean no_select = FALSE, no_sub = FALSE;
2899         gboolean exist = FALSE;
2900         
2901         g_return_val_if_fail(folder != NULL, NULL);
2902         g_return_val_if_fail(folder->account != NULL, NULL);
2903         g_return_val_if_fail(parent != NULL, NULL);
2904         g_return_val_if_fail(name != NULL, NULL);
2905
2906         debug_print("getting session...\n");
2907         session = imap_session_get(folder);
2908         if (!session) {
2909                 return NULL;
2910         }
2911
2912         if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2913                 dirpath = g_strdup(name);
2914         }else if (parent->path)
2915                 dirpath = g_strconcat(parent->path, "/", name, NULL);
2916         else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2917                 dirpath = g_strdup(name);
2918         else if (folder->account->imap_dir && *folder->account->imap_dir) {
2919                 gchar *imap_dir;
2920
2921                 Xstrdup_a(imap_dir, folder->account->imap_dir, {return NULL;});
2922                 strtailchomp(imap_dir, '/');
2923                 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2924         } else
2925                 dirpath = g_strdup(name);
2926                 
2927         
2928
2929         /* keep trailing directory separator to create a folder that contains
2930            sub folder */
2931         imap_path = imap_utf8_to_modified_utf7(dirpath, FALSE);
2932
2933         strtailchomp(dirpath, '/');
2934         Xstrdup_a(new_name, name, {
2935                 g_free(dirpath); 
2936                 g_free(imap_path);
2937                 return NULL;});
2938
2939         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path, &ok);
2940         if (is_fatal(ok)) {
2941                 g_free(imap_path);
2942                 return NULL;
2943         }
2944         imap_path_separator_subst(imap_path, separator);
2945         /* remove trailing / for display */
2946         strtailchomp(new_name, '/');
2947
2948         if (strcmp(dirpath, "INBOX") != 0) {
2949                 GPtrArray *argbuf;
2950                 int r;
2951                 clist * lep_list;
2952                 
2953                 argbuf = g_ptr_array_new();
2954                 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2955                 if (r != MAILIMAP_NO_ERROR) {
2956                         imap_handle_error(SESSION(session), NULL, r);
2957                         log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2958                         g_free(imap_path);
2959                         g_free(dirpath);
2960                         ptr_array_free_strings(argbuf);
2961                         g_ptr_array_free(argbuf, TRUE);
2962                         return NULL;
2963                 }
2964                 
2965                 if (clist_count(lep_list) > 0)
2966                         exist = TRUE;
2967                 mailimap_list_result_free(lep_list);
2968                 lep_list = NULL;
2969                 if (!exist) {
2970                         ok = imap_cmd_create(session, imap_path);
2971                         if (ok != MAILIMAP_NO_ERROR) {
2972                                 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2973                                 g_free(imap_path);
2974                                 g_free(dirpath);
2975                                 return NULL;
2976                         }
2977                         r = imap_threaded_list(folder, "", imap_path, &lep_list);
2978                         if (r == MAILIMAP_NO_ERROR) {
2979                                 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2980                                                lep_list, dirpath, TRUE);
2981                                 if (item_list) {
2982                                         FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2983                                         no_select = cur_item->no_select;
2984                                         no_sub = cur_item->no_sub;
2985                                         g_slist_free(item_list);
2986                                 } 
2987                                 mailimap_list_result_free(lep_list);
2988                         } else {
2989                                 imap_handle_error(SESSION(session), NULL, r);
2990                         }
2991                 }
2992                 imap_threaded_subscribe(folder, imap_path, TRUE);
2993         } else {
2994                 clist *lep_list;
2995                 int r;
2996                 /* just get flags */
2997                 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2998                 if (r == MAILIMAP_NO_ERROR) {
2999                         GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
3000                                        lep_list, dirpath, TRUE);
3001                         if (item_list) {
3002                                 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
3003                                 no_select = cur_item->no_select;
3004                                 no_sub = cur_item->no_sub;
3005                                 g_slist_free(item_list);
3006                         } 
3007                         mailimap_list_result_free(lep_list);
3008                 } else {
3009                         imap_handle_error(SESSION(session), NULL, r);
3010                 }
3011         }
3012
3013         new_item = folder_item_new(folder, new_name, dirpath);
3014         new_item->no_select = no_select;
3015         new_item->no_sub = no_sub;
3016         folder_item_append(parent, new_item);
3017         g_free(imap_path);
3018         g_free(dirpath);
3019
3020         dirpath = folder_item_get_path(new_item);
3021         if (!is_dir_exist(dirpath))
3022                 make_dir_hier(dirpath);
3023         g_free(dirpath);
3024
3025         if (exist) {
3026                 /* folder existed, scan it */
3027                 imap_scan_required(folder, new_item);
3028                 folder_item_scan_full(new_item, FALSE);
3029         }
3030
3031         return new_item;
3032 }
3033
3034 static gint imap_rename_folder(Folder *folder, FolderItem *item,
3035                                const gchar *name)
3036 {
3037         gchar *dirpath;
3038         gchar *newpath;
3039         gchar *real_oldpath;
3040         gchar *real_newpath;
3041         gchar *paths[2];
3042         gchar *old_cache_dir;
3043         gchar *new_cache_dir;
3044         IMAPSession *session;
3045         gchar separator;
3046         gint ok = MAILIMAP_NO_ERROR;
3047         gint exists, recent, unseen;
3048         guint32 uid_validity;
3049
3050         g_return_val_if_fail(folder != NULL, -1);
3051         g_return_val_if_fail(item != NULL, -1);
3052         g_return_val_if_fail(item->path != NULL, -1);
3053         g_return_val_if_fail(name != NULL, -1);
3054
3055         debug_print("getting session...\n");
3056         session = imap_session_get(folder);
3057         if (!session) {
3058                 return -1;
3059         }
3060
3061         if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
3062                 is_fatal(ok)) {
3063                 g_warning(_("New folder name must not contain the namespace "
3064                             "path separator"));
3065                 return -1;
3066         }
3067
3068         real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
3069         if (is_fatal(ok)) {
3070                 return -1;
3071         }
3072
3073         g_free(session->mbox);
3074         session->mbox = NULL;
3075         session->exists = 0;
3076         session->recent = 0;
3077         session->expunge = 0;
3078         ok = imap_cmd_examine(session, "INBOX",
3079                               &exists, &recent, &unseen, &uid_validity, FALSE);
3080         if (ok != MAILIMAP_NO_ERROR) {
3081                 g_free(real_oldpath);
3082                 return -1;
3083         }
3084
3085         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
3086         if (is_fatal(ok))
3087                 return -1;
3088         if (strchr(item->path, G_DIR_SEPARATOR)) {
3089                 dirpath = g_path_get_dirname(item->path);
3090                 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
3091                 g_free(dirpath);
3092         } else
3093                 newpath = g_strdup(name);
3094
3095         real_newpath = imap_utf8_to_modified_utf7(newpath, FALSE);
3096         imap_path_separator_subst(real_newpath, separator);
3097
3098         ok = imap_cmd_rename(session, real_oldpath, real_newpath);
3099         if (ok != MAILIMAP_NO_ERROR) {
3100                 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
3101                             real_oldpath, real_newpath);
3102                 g_free(real_oldpath);
3103                 g_free(newpath);
3104                 g_free(real_newpath);
3105                 return -1;
3106         }
3107         g_free(item->name);
3108         item->name = g_strdup(name);
3109
3110         old_cache_dir = folder_item_get_path(item);
3111
3112         paths[0] = g_strdup(item->path);
3113         paths[1] = newpath;
3114         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3115                         imap_rename_folder_func, paths);
3116
3117         if (is_dir_exist(old_cache_dir)) {
3118                 new_cache_dir = folder_item_get_path(item);
3119                 if (g_rename(old_cache_dir, new_cache_dir) < 0) {
3120                         FILE_OP_ERROR(old_cache_dir, "rename");
3121                 }
3122                 g_free(new_cache_dir);
3123         }
3124
3125         g_free(old_cache_dir);
3126         g_free(paths[0]);
3127         g_free(newpath);
3128         g_free(real_oldpath);
3129         g_free(real_newpath);
3130         return 0;
3131 }
3132
3133 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
3134 {
3135         gchar *path;
3136         gint r = MAILIMAP_NO_ERROR;
3137         IMAPSession *session;
3138         debug_print("getting session...\n");
3139
3140         session = imap_session_get(folder);
3141         if (!session) {
3142                 return -1;
3143         }
3144         if (item && item->path) {
3145                 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &r);
3146                 if (!path)
3147                         return -1;
3148                 if (is_fatal(r)) {
3149                         g_free(path);
3150                         return -1;
3151                 }
3152                 if (!strcmp(path, "INBOX") && sub == FALSE) {
3153                         g_free(path);
3154                         return -1;
3155                 }
3156                 debug_print("%ssubscribing %s\n", sub?"":"un", path);
3157                 r = imap_threaded_subscribe(folder, path, sub);
3158                 g_free(path);
3159         } else if (rpath) {
3160                 r = imap_threaded_subscribe(folder, rpath, sub);
3161         } else
3162                 return -1;
3163         return r;
3164 }
3165
3166 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
3167 {
3168         gint ok = MAILIMAP_NO_ERROR;
3169         IMAPSession *session;
3170         gchar *path;
3171         gchar *cache_dir;
3172         gboolean selected_folder;
3173
3174         g_return_val_if_fail(folder != NULL, -1);
3175         g_return_val_if_fail(item != NULL, -1);
3176         g_return_val_if_fail(item->path != NULL, -1);
3177
3178         debug_print("getting session...\n");
3179         session = imap_session_get(folder);
3180         if (!session) {
3181                 return -1;
3182         }
3183         path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
3184         if (is_fatal(ok))
3185                 return -1;
3186
3187         imap_threaded_subscribe(folder, path, FALSE);
3188
3189         selected_folder = (session->mbox != NULL) &&
3190                           (!strcmp(session->mbox, item->path));
3191         if (selected_folder) {
3192                 ok = imap_cmd_close(session);
3193                 if (ok != MAILIMAP_NO_ERROR) {
3194                         debug_print("close err %d\n", ok);
3195                         return ok;
3196                 }
3197         }
3198         ok = imap_cmd_delete(session, path);
3199         if (ok != MAILIMAP_NO_ERROR && !is_fatal(ok)) {
3200                 gchar *tmp = NULL;
3201                 
3202                 ok = MAILIMAP_NO_ERROR;
3203                 tmp = g_strdup_printf("%s%c", path, 
3204                                 imap_get_path_separator(session, IMAP_FOLDER(folder), path, &ok));
3205                 g_free(path);
3206                 path = tmp;
3207                 if (!is_fatal(ok))
3208                         ok = imap_cmd_delete(session, path);
3209         }
3210
3211         if (ok != MAILIMAP_NO_ERROR) {
3212                 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
3213                 g_free(path);
3214                 return -1;
3215         }
3216
3217         g_free(path);
3218         cache_dir = folder_item_get_path(item);
3219         if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
3220                 g_warning("can't remove directory '%s'\n", cache_dir);
3221         g_free(cache_dir);
3222         folder_item_remove(item);
3223         return 0;
3224 }
3225
3226 static gint imap_remove_folder(Folder *folder, FolderItem *item)
3227 {
3228         GNode *node, *next;
3229
3230         g_return_val_if_fail(item != NULL, -1);
3231         g_return_val_if_fail(item->folder != NULL, -1);
3232         g_return_val_if_fail(item->node != NULL, -1);
3233
3234         node = item->node->children;
3235         while (node != NULL) {
3236                 next = node->next;
3237                 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
3238                         return -1;
3239                 node = next;
3240         }
3241         debug_print("IMAP removing %s\n", item->path);
3242
3243         if (imap_remove_all_msg(folder, item) < 0)
3244                 return -1;
3245         return imap_remove_folder_real(folder, item);
3246 }
3247
3248 typedef struct _uncached_data {
3249         IMAPSession *session;
3250         FolderItem *item;
3251         MsgNumberList *numlist;
3252         guint cur;
3253         guint total;
3254         gboolean done;
3255         int ok;
3256 } uncached_data;
3257
3258 static void *imap_get_uncached_messages_thread(void *data)
3259 {
3260         uncached_data *stuff = (uncached_data *)data;
3261         IMAPSession *session = stuff->session;
3262         FolderItem *item = stuff->item;
3263         MsgNumberList *numlist = stuff->numlist;
3264         GSList *newlist = NULL;
3265         GSList *llast = NULL;
3266         GSList *seq_list, *cur;
3267         gboolean got_alien_tags = FALSE;
3268
3269         debug_print("uncached_messages\n");
3270         
3271         if (session == NULL || item == NULL || item->folder == NULL
3272             || FOLDER_CLASS(item->folder) != &imap_class) {
3273                 stuff->done = TRUE;
3274                 return NULL;
3275         }
3276         
3277         seq_list = imap_get_lep_set_from_numlist(IMAP_FOLDER(item->folder), numlist);
3278         debug_print("get msgs info\n");
3279         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
3280                 struct mailimap_set * imapset;
3281                 unsigned int i;
3282                 int r;
3283                 carray * env_list;
3284                 int count;
3285                 
3286                 if (session->cancelled)
3287                         break;
3288                 
3289                 imapset = cur->data;
3290                 
3291                 r = imap_threaded_fetch_env(session->folder,
3292                                             imapset, &env_list);
3293                 if (r != MAILIMAP_NO_ERROR) {
3294                         imap_handle_error(SESSION(session), NULL, r);
3295                         if (is_fatal(r)) {
3296                                 stuff->ok = r;
3297                                 return NULL;
3298                         }
3299                         continue;
3300                 }
3301
3302                 session_set_access_time(SESSION(session));
3303