2007-09-27 [colin] 3.0.1cvs35
[claws.git] / src / imap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "imap.h"
31 #include "imap_gtk.h"
32 #include "inc.h"
33 #include "xml.h"
34 #include "alertpanel.h"
35
36 #ifdef HAVE_LIBETPAN
37
38 #include <stdlib.h>
39 #include <dirent.h>
40 #include <unistd.h>
41 #include <ctype.h>
42 #include <time.h>
43 #include <errno.h>
44 #if HAVE_ICONV
45 #  include <iconv.h>
46 #endif
47
48 #if USE_OPENSSL
49 #  include "ssl.h"
50 #endif
51
52 #include "folder.h"
53 #include "session.h"
54 #include "procmsg.h"
55 #include "socket.h"
56 #include "recv.h"
57 #include "procheader.h"
58 #include "prefs_account.h"
59 #include "codeconv.h"
60 #include "md5.h"
61 #include "base64.h"
62 #include "utils.h"
63 #include "prefs_common.h"
64 #include "inputdialog.h"
65 #include "log.h"
66 #include "remotefolder.h"
67 #include "claws.h"
68 #include "statusbar.h"
69 #include "msgcache.h"
70 #include "imap-thread.h"
71 #include "account.h"
72
73 typedef struct _IMAPFolder      IMAPFolder;
74 typedef struct _IMAPSession     IMAPSession;
75 typedef struct _IMAPNameSpace   IMAPNameSpace;
76 typedef struct _IMAPFolderItem  IMAPFolderItem;
77
78 #include "prefs_account.h"
79
80 #define IMAP_FOLDER(obj)        ((IMAPFolder *)obj)
81 #define IMAP_FOLDER_ITEM(obj)   ((IMAPFolderItem *)obj)
82 #define IMAP_SESSION(obj)       ((IMAPSession *)obj)
83
84 struct _IMAPFolder
85 {
86         RemoteFolder rfolder;
87
88         /* list of IMAPNameSpace */
89         GList *ns_personal;
90         GList *ns_others;
91         GList *ns_shared;
92         gchar last_seen_separator;
93         guint refcnt;
94 };
95
96 struct _IMAPSession
97 {
98         Session session;
99
100         gboolean authenticated;
101
102         GSList *capability;
103         gboolean uidplus;
104
105         gchar *mbox;
106         guint cmd_count;
107
108         /* CLAWS */
109         gboolean folder_content_changed;
110         guint exists;
111         guint recent;
112         guint expunge;
113         guint unseen;
114         guint uid_validity;
115         guint uid_next;
116
117         Folder * folder;
118         gboolean busy;
119         gboolean cancelled;
120         gboolean sens_update_block;
121 };
122
123 struct _IMAPNameSpace
124 {
125         gchar *name;
126         gchar separator;
127 };
128
129 #define IMAP_SUCCESS    0
130 #define IMAP_SOCKET     2
131 #define IMAP_AUTHFAIL   3
132 #define IMAP_PROTOCOL   4
133 #define IMAP_SYNTAX     5
134 #define IMAP_IOERR      6
135 #define IMAP_ERROR      7
136
137 #define IMAPBUFSIZE     8192
138
139 typedef enum
140 {
141         IMAP_FLAG_SEEN          = 1 << 0,
142         IMAP_FLAG_ANSWERED      = 1 << 1,
143         IMAP_FLAG_FLAGGED       = 1 << 2,
144         IMAP_FLAG_DELETED       = 1 << 3,
145         IMAP_FLAG_DRAFT         = 1 << 4
146 } IMAPFlags;
147
148 #define IMAP_IS_SEEN(flags)     ((flags & IMAP_FLAG_SEEN) != 0)
149 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
150 #define IMAP_IS_FLAGGED(flags)  ((flags & IMAP_FLAG_FLAGGED) != 0)
151 #define IMAP_IS_DELETED(flags)  ((flags & IMAP_FLAG_DELETED) != 0)
152 #define IMAP_IS_DRAFT(flags)    ((flags & IMAP_FLAG_DRAFT) != 0)
153
154
155 #define IMAP4_PORT      143
156 #if USE_OPENSSL
157 #define IMAPS_PORT      993
158 #endif
159
160 #define IMAP_CMD_LIMIT  1000
161
162 struct _IMAPFolderItem
163 {
164         FolderItem item;
165
166         guint lastuid;
167         guint uid_next;
168         GSList *uid_list;
169         gboolean batching;
170
171         GHashTable *flags_set_table;
172         GHashTable *flags_unset_table;
173         guint32 last_change;
174         guint32 last_sync;
175         gboolean should_update;
176         gboolean should_trash_cache;
177 };
178
179 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
180 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
181
182 static void imap_folder_init            (Folder         *folder,
183                                          const gchar    *name,
184                                          const gchar    *path);
185
186 static Folder   *imap_folder_new        (const gchar    *name,
187                                          const gchar    *path);
188 static void      imap_folder_destroy    (Folder         *folder);
189
190 static IMAPSession *imap_session_new    (Folder         *folder,
191                                          const PrefsAccount     *account);
192 static void     imap_session_authenticate(IMAPSession           *session,
193                                           const PrefsAccount    *account);
194 static void     imap_session_destroy    (Session        *session);
195
196 static gchar   *imap_fetch_msg          (Folder         *folder, 
197                                          FolderItem     *item, 
198                                          gint            uid);
199 static gchar   *imap_fetch_msg_full     (Folder         *folder, 
200                                          FolderItem     *item, 
201                                          gint            uid,
202                                          gboolean        headers,
203                                          gboolean        body);
204 static void     imap_remove_cached_msg  (Folder         *folder, 
205                                          FolderItem     *item, 
206                                          MsgInfo        *msginfo);
207 static gint     imap_add_msg            (Folder         *folder,
208                                          FolderItem     *dest,
209                                          const gchar    *file, 
210                                          MsgFlags       *flags);
211 static gint     imap_add_msgs           (Folder         *folder, 
212                                          FolderItem     *dest,
213                                          GSList         *file_list,
214                                          GRelation      *relation);
215
216 static gint     imap_copy_msg           (Folder         *folder,
217                                          FolderItem     *dest, 
218                                          MsgInfo        *msginfo);
219 static gint     imap_copy_msgs          (Folder         *folder, 
220                                          FolderItem     *dest, 
221                                          MsgInfoList    *msglist, 
222                                          GRelation      *relation);
223
224 static gint     imap_remove_msg         (Folder         *folder, 
225                                          FolderItem     *item, 
226                                          gint            uid);
227 static gint     imap_remove_msgs        (Folder         *folder, 
228                                          FolderItem     *dest, 
229                                          MsgInfoList    *msglist, 
230                                          GRelation      *relation);
231 static gint     imap_remove_all_msg     (Folder         *folder, 
232                                          FolderItem     *item);
233
234 static gboolean imap_is_msg_changed     (Folder         *folder,
235                                          FolderItem     *item, 
236                                          MsgInfo        *msginfo);
237
238 static gint     imap_close              (Folder         *folder, 
239                                          FolderItem     *item);
240
241 static gint     imap_scan_tree          (Folder         *folder);
242
243 static gint     imap_create_tree        (Folder         *folder);
244
245 static FolderItem *imap_create_folder   (Folder         *folder,
246                                          FolderItem     *parent,
247                                          const gchar    *name);
248 static gint     imap_rename_folder      (Folder         *folder,
249                                          FolderItem     *item, 
250                                          const gchar    *name);
251 static gint     imap_remove_folder      (Folder         *folder, 
252                                          FolderItem     *item);
253
254 static FolderItem *imap_folder_item_new (Folder         *folder);
255 static void imap_folder_item_destroy    (Folder         *folder,
256                                          FolderItem     *item);
257
258 static IMAPSession *imap_session_get    (Folder         *folder);
259
260 static gint imap_auth                   (IMAPSession    *session,
261                                          const gchar    *user,
262                                          const gchar    *pass,
263                                          IMAPAuthType    type);
264
265 static gint imap_scan_tree_recursive    (IMAPSession    *session,
266                                          FolderItem     *item,
267                                          gboolean        subs_only);
268
269 static void imap_create_missing_folders (Folder         *folder);
270 static FolderItem *imap_create_special_folder
271                                         (Folder                 *folder,
272                                          SpecialFolderItemType   stype,
273                                          const gchar            *name);
274
275 static gint imap_do_copy_msgs           (Folder         *folder,
276                                          FolderItem     *dest,
277                                          MsgInfoList    *msglist,
278                                          GRelation      *relation);
279
280 static void imap_delete_all_cached_messages     (FolderItem     *item);
281 static void imap_set_batch              (Folder         *folder,
282                                          FolderItem     *item,
283                                          gboolean        batch);
284 static gint imap_set_message_flags      (IMAPSession    *session,
285                                          MsgNumberList  *numlist,
286                                          IMAPFlags       flags,
287                                          gboolean        is_set);
288 static gint imap_select                 (IMAPSession    *session,
289                                          IMAPFolder     *folder,
290                                          const gchar    *path,
291                                          gint           *exists,
292                                          gint           *recent,
293                                          gint           *unseen,
294                                          guint32        *uid_validity,
295                                          gboolean        block);
296 static gint imap_status                 (IMAPSession    *session,
297                                          IMAPFolder     *folder,
298                                          const gchar    *path,
299                                          IMAPFolderItem *item,
300                                          gint           *messages,
301                                          guint32        *uid_next,
302                                          guint32        *uid_validity,
303                                          gint           *unseen,
304                                          gboolean        block);
305
306 static gchar imap_get_path_separator            (IMAPSession    *session,
307                                                  IMAPFolder     *folder,
308                                                  const gchar    *path);
309 static gchar *imap_get_real_path                (IMAPSession    *session,
310                                                  IMAPFolder     *folder,
311                                                  const gchar    *path);
312 static void imap_synchronise            (FolderItem     *item, gint days);
313 static gboolean imap_is_busy            (Folder *folder);
314
315 static void imap_free_capabilities      (IMAPSession    *session);
316
317 /* low-level IMAP4rev1 commands */
318 static gint imap_cmd_login      (IMAPSession    *session,
319                                  const gchar    *user,
320                                  const gchar    *pass,
321                                  const gchar    *type);
322 static gint imap_cmd_noop       (IMAPSession    *session);
323 #if USE_OPENSSL
324 static gint imap_cmd_starttls   (IMAPSession    *session);
325 #endif
326 static gint imap_cmd_select     (IMAPSession    *session,
327                                  const gchar    *folder,
328                                  gint           *exists,
329                                  gint           *recent,
330                                  gint           *unseen,
331                                  guint32        *uid_validity,
332                                  gboolean        block);
333 static gint imap_cmd_close      (IMAPSession    *session);
334 static gint imap_cmd_examine    (IMAPSession    *session,
335                                  const gchar    *folder,
336                                  gint           *exists,
337                                  gint           *recent,
338                                  gint           *unseen,
339                                  guint32        *uid_validity,
340                                  gboolean        block);
341 static gint imap_cmd_create     (IMAPSession    *sock,
342                                  const gchar    *folder);
343 static gint imap_cmd_rename     (IMAPSession    *sock,
344                                  const gchar    *oldfolder,
345                                  const gchar    *newfolder);
346 static gint imap_cmd_delete     (IMAPSession    *session,
347                                  const gchar    *folder);
348 static gint imap_cmd_fetch      (IMAPSession    *sock,
349                                  guint32         uid,
350                                  const gchar    *filename,
351                                  gboolean        headers,
352                                  gboolean        body);
353 static gint imap_cmd_append     (IMAPSession    *session,
354                                  const gchar    *destfolder,
355                                  const gchar    *file,
356                                  IMAPFlags       flags,
357                                  guint32        *new_uid);
358 static gint imap_cmd_copy       (IMAPSession *session,
359                                  struct mailimap_set * set,
360                                  const gchar *destfolder,
361                                  GRelation *uid_mapping,
362                                  struct mailimap_set ** source,
363                                  struct mailimap_set ** dest);
364 static gint imap_cmd_store      (IMAPSession    *session,
365                                  struct mailimap_set * set,
366                                  IMAPFlags flags,
367                                  int do_add);
368 static gint imap_cmd_expunge    (IMAPSession    *session);
369
370 static void imap_path_separator_subst           (gchar          *str,
371                                                  gchar           separator);
372
373 static gchar *imap_utf8_to_modified_utf7        (const gchar    *from);
374 static gchar *imap_modified_utf7_to_utf8        (const gchar    *mutf7_str);
375
376 static gboolean imap_rename_folder_func         (GNode          *node,
377                                                  gpointer        data);
378 static gint imap_get_num_list                   (Folder         *folder,
379                                                  FolderItem     *item,
380                                                  GSList        **list,
381                                                  gboolean       *old_uids_valid);
382 static GSList *imap_get_msginfos                (Folder         *folder,
383                                                  FolderItem     *item,
384                                                  GSList         *msgnum_list);
385 static MsgInfo *imap_get_msginfo                (Folder         *folder,
386                                                  FolderItem     *item,
387                                                  gint            num);
388 static gboolean imap_scan_required              (Folder         *folder,
389                                                  FolderItem     *item);
390 static void imap_change_flags                   (Folder         *folder,
391                                                  FolderItem     *item,
392                                                  MsgInfo        *msginfo,
393                                                  MsgPermFlags    newflags);
394 static gint imap_get_flags                      (Folder         *folder,
395                                                  FolderItem     *item,
396                                                  MsgInfoList    *msglist,
397                                                  GRelation      *msgflags);
398 static gchar *imap_folder_get_path              (Folder         *folder);
399 static gchar *imap_item_get_path                (Folder         *folder,
400                                                  FolderItem     *item);
401 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
402
403
404 /* data types conversion libetpan <-> claws */
405 static GSList * imap_list_from_lep(IMAPFolder * folder,
406                                    clist * list, const gchar * real_path, gboolean all);
407 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist);
408 static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist);
409 static GSList * imap_uid_list_from_lep(clist * list);
410 static GSList * imap_uid_list_from_lep_tab(carray * list);
411 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
412                                                    GHashTable * hash);
413 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
414                                        FolderItem *item);
415 static void imap_lep_set_free(GSList *seq_list);
416 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags);
417
418 typedef struct _hashtable_data {
419         IMAPSession *session;
420         GSList *msglist;
421         IMAPFolderItem *item;
422 } hashtable_data;
423
424 static FolderClass imap_class;
425
426 typedef struct _thread_data {
427         gchar *server;
428         gushort port;
429         gboolean done;
430         SockInfo *sock;
431 #ifdef USE_OPENSSL
432         SSLType ssl_type;
433 #endif
434 } thread_data;
435
436 FolderClass *imap_get_class(void)
437 {
438         if (imap_class.idstr == NULL) {
439                 imap_class.type = F_IMAP;
440                 imap_class.idstr = "imap";
441                 imap_class.uistr = "IMAP4";
442
443                 /* Folder functions */
444                 imap_class.new_folder = imap_folder_new;
445                 imap_class.destroy_folder = imap_folder_destroy;
446                 imap_class.scan_tree = imap_scan_tree;
447                 imap_class.create_tree = imap_create_tree;
448
449                 /* FolderItem functions */
450                 imap_class.item_new = imap_folder_item_new;
451                 imap_class.item_destroy = imap_folder_item_destroy;
452                 imap_class.item_get_path = imap_item_get_path;
453                 imap_class.create_folder = imap_create_folder;
454                 imap_class.rename_folder = imap_rename_folder;
455                 imap_class.remove_folder = imap_remove_folder;
456                 imap_class.close = imap_close;
457                 imap_class.get_num_list = imap_get_num_list;
458                 imap_class.scan_required = imap_scan_required;
459                 imap_class.set_xml = folder_set_xml;
460                 imap_class.get_xml = folder_get_xml;
461                 imap_class.item_set_xml = imap_item_set_xml;
462                 imap_class.item_get_xml = imap_item_get_xml;
463
464                 /* Message functions */
465                 imap_class.get_msginfo = imap_get_msginfo;
466                 imap_class.get_msginfos = imap_get_msginfos;
467                 imap_class.fetch_msg = imap_fetch_msg;
468                 imap_class.fetch_msg_full = imap_fetch_msg_full;
469                 imap_class.add_msg = imap_add_msg;
470                 imap_class.add_msgs = imap_add_msgs;
471                 imap_class.copy_msg = imap_copy_msg;
472                 imap_class.copy_msgs = imap_copy_msgs;
473                 imap_class.remove_msg = imap_remove_msg;
474                 imap_class.remove_msgs = imap_remove_msgs;
475                 imap_class.remove_all_msg = imap_remove_all_msg;
476                 imap_class.is_msg_changed = imap_is_msg_changed;
477                 imap_class.change_flags = imap_change_flags;
478                 imap_class.get_flags = imap_get_flags;
479                 imap_class.set_batch = imap_set_batch;
480                 imap_class.synchronise = imap_synchronise;
481                 imap_class.remove_cached_msg = imap_remove_cached_msg;
482 #ifdef USE_PTREAD
483                 pthread_mutex_init(&imap_mutex, NULL);
484 #endif
485         }
486         
487         return &imap_class;
488 }
489
490 static Folder *imap_folder_new(const gchar *name, const gchar *path)
491 {
492         Folder *folder;
493
494         folder = (Folder *)g_new0(IMAPFolder, 1);
495         folder->klass = &imap_class;
496         imap_folder_init(folder, name, path);
497
498         return folder;
499 }
500
501 static void imap_folder_destroy(Folder *folder)
502 {
503         while (imap_folder_get_refcnt(folder) > 0)
504                 gtk_main_iteration();
505         
506         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
507         imap_done(folder);
508 }
509
510 static void imap_folder_init(Folder *folder, const gchar *name,
511                              const gchar *path)
512 {
513         folder_remote_folder_init((Folder *)folder, name, path);
514 }
515
516 static FolderItem *imap_folder_item_new(Folder *folder)
517 {
518         IMAPFolderItem *item;
519         
520         item = g_new0(IMAPFolderItem, 1);
521         item->lastuid = 0;
522         item->uid_next = 0;
523         item->uid_list = NULL;
524
525         return (FolderItem *)item;
526 }
527
528 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
529 {
530         IMAPFolderItem *item = (IMAPFolderItem *)_item;
531
532         g_return_if_fail(item != NULL);
533         g_slist_free(item->uid_list);
534
535         g_free(_item);
536 }
537
538 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
539 {
540         IMAPFolderItem *item = (IMAPFolderItem *)node->data;
541         
542         item->lastuid = 0;
543         g_slist_free(item->uid_list);
544         item->uid_list = NULL;
545         
546         return FALSE;
547 }
548
549 static void imap_reset_uid_lists(Folder *folder)
550 {
551         if(folder->node == NULL)
552                 return;
553         
554         /* Destroy all uid lists and rest last uid */
555         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
556 }
557
558 static int imap_get_capabilities(IMAPSession *session)
559 {
560         struct mailimap_capability_data *capabilities = NULL;
561         clistiter *cur;
562         int result = -1;
563
564         if (session->capability != NULL)
565                 return MAILIMAP_NO_ERROR;
566
567         capabilities = imap_threaded_capability(session->folder, &result);
568
569         if (result != MAILIMAP_NO_ERROR) {
570                 return MAILIMAP_ERROR_CAPABILITY;
571         }
572
573         if (capabilities == NULL) {
574                 return MAILIMAP_NO_ERROR;
575         }
576
577         for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
578             cur = clist_next(cur)) {
579                 struct mailimap_capability * cap = 
580                         clist_content(cur);
581                 if (!cap || cap->cap_data.cap_name == NULL)
582                         continue;
583                 session->capability = g_slist_append
584                                 (session->capability,
585                                  g_strdup(cap->cap_data.cap_name));
586                 debug_print("got capa %s\n", cap->cap_data.cap_name);
587         }
588         mailimap_capability_data_free(capabilities);
589         return MAILIMAP_NO_ERROR;
590 }
591
592 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
593 {
594         GSList *cur;
595         for (cur = session->capability; cur; cur = cur->next) {
596                 if (!g_ascii_strcasecmp(cur->data, cap))
597                         return TRUE;
598         }
599         return FALSE;
600 }
601
602 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
603                       IMAPAuthType type)
604 {
605         gint ok = IMAP_ERROR;
606         static time_t last_login_err = 0;
607         gchar *ext_info = "";
608         
609         if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR)
610                 return IMAP_ERROR;
611
612         switch(type) {
613         case IMAP_AUTH_ANON:
614                 ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
615                 break;
616         case IMAP_AUTH_CRAM_MD5:
617                 ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
618                 break;
619         case IMAP_AUTH_LOGIN:
620                 ok = imap_cmd_login(session, user, pass, "LOGIN");
621                 break;
622         case IMAP_AUTH_GSSAPI:
623                 ok = imap_cmd_login(session, user, pass, "GSSAPI");
624                 break;
625         default:
626                 debug_print("capabilities:\n"
627                                 "\t ANONYMOUS %d\n"
628                                 "\t CRAM-MD5 %d\n"
629                                 "\t LOGIN %d\n"
630                                 "\t GSSAPI %d\n", 
631                         imap_has_capability(session, "ANONYMOUS"),
632                         imap_has_capability(session, "CRAM-MD5"),
633                         imap_has_capability(session, "LOGIN"),
634                         imap_has_capability(session, "GSSAPI"));
635                 if (imap_has_capability(session, "CRAM-MD5"))
636                         ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
637                 if (ok == IMAP_ERROR && imap_has_capability(session, "GSSAPI"))
638                         ok = imap_cmd_login(session, user, pass, "GSSAPI");
639                 if (ok == IMAP_ERROR) /* we always try LOGIN before giving up */
640                         ok = imap_cmd_login(session, user, pass, "LOGIN");
641         }
642
643         if (ok == IMAP_SUCCESS)
644                 session->authenticated = TRUE;
645         else {
646                 if (type == IMAP_AUTH_CRAM_MD5) {
647                         ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
648                                      "compiled with SASL support and the "
649                                      "CRAM-MD5 SASL plugin is installed.");
650                 } 
651
652                 if (time(NULL) - last_login_err > 10) {
653                         if (!prefs_common.no_recv_err_panel) {
654                                 alertpanel_error(_("Connection to %s failed: "
655                                         "login refused.%s"),
656                                         SESSION(session)->server, ext_info);
657                         } else {
658                                 log_error(LOG_PROTOCOL, _("Connection to %s failed: "
659                                         "login refused.%s\n"),
660                                         SESSION(session)->server, ext_info);
661                         }
662                 }
663                 last_login_err = time(NULL);
664         }
665         return ok;
666 }
667
668 static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
669 {
670         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
671         /* Check if this is the first try to establish a
672            connection, if yes we don't try to reconnect */
673         debug_print("reconnecting\n");
674         if (rfolder->session == NULL) {
675                 log_warning(LOG_PROTOCOL, _("Connecting to %s failed"),
676                             folder->account->recv_server);
677                 session_destroy(SESSION(session));
678                 session = NULL;
679         } else {
680                 log_warning(LOG_PROTOCOL, _("IMAP4 connection to %s has been"
681                             " disconnected. Reconnecting...\n"),
682                             folder->account->recv_server);
683                 statusbar_print_all(_("IMAP4 connection to %s has been"
684                             " disconnected. Reconnecting...\n"),
685                             folder->account->recv_server);
686                 SESSION(session)->state = SESSION_DISCONNECTED;
687                 session_destroy(SESSION(session));
688                 /* Clear folders session to make imap_session_get create
689                    a new session, because of rfolder->session == NULL
690                    it will not try to reconnect again and so avoid an
691                    endless loop */
692                 rfolder->session = NULL;
693                 debug_print("getting session...\n");
694                 session = imap_session_get(folder);
695                 rfolder->session = SESSION(session);
696                 statusbar_pop_all();
697         }
698         return session;
699 }
700
701 static void imap_refresh_sensitivity (IMAPSession *session)
702 {
703         MainWindow *mainwin;
704
705         if (session->sens_update_block)
706                 return;
707         mainwin = mainwindow_get_mainwindow();
708         if (mainwin) {
709                 toolbar_main_set_sensitive(mainwin);
710                 main_window_set_menu_sensitive(mainwin);
711         }
712 }
713
714 static void lock_session(IMAPSession *session)
715 {
716         if (session) {
717                 debug_print("locking session %p (%d)\n", session, session->busy);
718                 if (session->busy)
719                         debug_print("         SESSION WAS LOCKED !!      \n");
720                 session->busy = TRUE;
721                 imap_refresh_sensitivity(session);
722         } else {
723                 debug_print("can't lock null session\n");
724         }
725 }
726
727 static void unlock_session(IMAPSession *session)
728 {
729         if (session) {
730                 
731                 debug_print("unlocking session %p\n", session);
732                 session->busy = FALSE;
733                 imap_refresh_sensitivity(session);
734         } else {
735                 debug_print("can't unlock null session\n");
736         }
737 }
738
739 static IMAPSession *imap_session_get(Folder *folder)
740 {
741         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
742         IMAPSession *session = NULL;
743
744         g_return_val_if_fail(folder != NULL, NULL);
745         g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
746         g_return_val_if_fail(folder->account != NULL, NULL);
747         
748         if (prefs_common.work_offline && 
749             !inc_offline_should_override(FALSE,
750                 _("Claws Mail needs network access in order "
751                   "to access the IMAP server."))) {
752                 return NULL;
753         }
754
755         /* Make sure we have a session */
756         if (rfolder->session != NULL) {
757                 session = IMAP_SESSION(rfolder->session);
758         } else if (rfolder->connecting) {
759                 debug_print("already connecting\n");
760                 return NULL;
761         } else {
762                 imap_reset_uid_lists(folder);
763                 if (time(NULL) - rfolder->last_failure <= 2)
764                         return NULL;
765                 rfolder->connecting = TRUE;
766                 session = imap_session_new(folder, folder->account);
767         }
768         if(session == NULL) {
769                 rfolder->last_failure = time(NULL);
770                 rfolder->connecting = FALSE;
771                 return NULL;
772         }
773
774         /* Make sure session is authenticated */
775         if (!IMAP_SESSION(session)->authenticated)
776                 imap_session_authenticate(IMAP_SESSION(session), folder->account);
777         
778         if (!IMAP_SESSION(session)->authenticated) {
779                 imap_threaded_disconnect(session->folder);
780                 SESSION(session)->state = SESSION_DISCONNECTED;
781                 session_destroy(SESSION(session));
782                 rfolder->session = NULL;
783                 rfolder->last_failure = time(NULL);
784                 rfolder->connecting = FALSE;
785                 return NULL;
786         }
787
788         lock_session(session);
789
790         /* I think the point of this code is to avoid sending a
791          * keepalive if we've used the session recently and therefore
792          * think it's still alive.  Unfortunately, most of the code
793          * does not yet check for errors on the socket, and so if the
794          * connection drops we don't notice until the timeout expires.
795          * A better solution than sending a NOOP every time would be
796          * for every command to be prepared to retry until it is
797          * successfully sent. -- mbp */
798         if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
799                 /* verify that the session is still alive */
800                 if (imap_cmd_noop(session) != IMAP_SUCCESS) {
801                         debug_print("disconnected!\n");
802                         session = imap_reconnect_if_possible(folder, session);
803                 }
804                 if (session)
805                         session->cancelled = FALSE;
806         }
807
808         rfolder->session = SESSION(session);
809         rfolder->connecting = FALSE;
810
811         return IMAP_SESSION(session);
812 }
813
814 static IMAPSession *imap_session_new(Folder * folder,
815                                      const PrefsAccount *account)
816 {
817         IMAPSession *session;
818         gushort port;
819         int r;
820         int authenticated = FALSE;
821         
822 #ifdef USE_OPENSSL
823         /* FIXME: IMAP over SSL only... */ 
824         SSLType ssl_type;
825
826         port = account->set_imapport ? account->imapport
827                 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
828         ssl_type = account->ssl_imap;   
829 #else
830         if (account->ssl_imap != SSL_NONE) {
831                 if (alertpanel_full(_("Insecure connection"),
832                         _("This connection is configured to be secured "
833                           "using SSL, but SSL is not available in this "
834                           "build of Claws Mail. \n\n"
835                           "Do you want to continue connecting to this "
836                           "server? The communication would not be "
837                           "secure."),
838                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
839                           NULL, FALSE, NULL, ALERT_WARNING,
840                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
841                         return NULL;
842         }
843         port = account->set_imapport ? account->imapport
844                 : IMAP4_PORT;
845 #endif
846
847         imap_init(folder);
848         statuswindow_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
849         if (account->set_tunnelcmd) {
850                 r = imap_threaded_connect_cmd(folder,
851                                               account->tunnelcmd,
852                                               account->recv_server,
853                                               port);
854         }
855         else {
856 #ifdef USE_OPENSSL
857                 if (ssl_type == SSL_TUNNEL) {
858                         r = imap_threaded_connect_ssl(folder,
859                                                       account->recv_server,
860                                                       port);
861                 }
862                 else 
863 #endif
864                 {
865                         r = imap_threaded_connect(folder,
866                                                   account->recv_server,
867                                                   port);
868                 }
869         }
870         
871         statuswindow_pop_all();
872         if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
873                 authenticated = TRUE;
874         }
875         else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
876                 authenticated = FALSE;
877         }
878         else {
879 #if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
880 #ifdef USE_OPENSSL
881                 if (r == MAILIMAP_ERROR_SSL)
882                         log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
883 #endif
884 #endif
885                 if(!prefs_common.no_recv_err_panel) {
886                         alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
887                                          account->recv_server, port);
888                 } else {
889                         log_error(LOG_PROTOCOL, _("Can't connect to IMAP4 server: %s:%d\n"),
890                                          account->recv_server, port);
891                 } 
892                 
893                 return NULL;
894         }
895         
896         session = g_new0(IMAPSession, 1);
897         session_init(SESSION(session));
898         SESSION(session)->type             = SESSION_IMAP;
899         SESSION(session)->server           = g_strdup(account->recv_server);
900         SESSION(session)->sock             = NULL;
901         
902         SESSION(session)->destroy          = imap_session_destroy;
903
904         session->capability = NULL;
905         
906         session->authenticated = authenticated;
907         session->mbox = NULL;
908         session->exists = 0;
909         session->recent = 0;
910         session->expunge = 0;
911         session->cmd_count = 0;
912         session->folder = folder;
913         IMAP_FOLDER(session->folder)->last_seen_separator = 0;
914
915 #if USE_OPENSSL
916         if (account->ssl_imap == SSL_STARTTLS) {
917                 gint ok;
918
919                 ok = imap_cmd_starttls(session);
920                 if (ok != IMAP_SUCCESS) {
921                         log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
922                         session_destroy(SESSION(session));
923                         return NULL;
924                 }
925
926                 imap_free_capabilities(session);
927                 session->authenticated = FALSE;
928                 session->uidplus = FALSE;
929                 session->cmd_count = 1;
930         }
931 #endif
932         log_message(LOG_PROTOCOL, "IMAP connection is %s-authenticated\n",
933                     (session->authenticated) ? "pre" : "un");
934         
935         return session;
936 }
937
938 static void imap_session_authenticate(IMAPSession *session, 
939                                       const PrefsAccount *account)
940 {
941         gchar *pass, *acc_pass;
942         gboolean failed = FALSE;
943
944         g_return_if_fail(account->userid != NULL);
945         acc_pass = account->passwd;
946 try_again:
947         pass = acc_pass;
948         if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
949                 gchar *tmp_pass;
950                 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
951                 if (!tmp_pass)
952                         return;
953                 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
954                 g_free(tmp_pass);
955         } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
956                 pass = "";
957         }
958         statuswindow_print_all(_("Connecting to IMAP4 server %s...\n"),
959                                 account->recv_server);
960         if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
961                 statusbar_pop_all();
962                 
963                 if (!failed) {
964                         acc_pass = NULL;
965                         failed = TRUE;
966                         goto try_again;
967                 } else {
968                         if (prefs_common.no_recv_err_panel) {
969                                 log_error(LOG_PROTOCOL, _("Couldn't login to IMAP server %s."), account->recv_server);
970                                 mainwindow_show_error();
971                         } else
972                                 alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
973                 }               
974
975                 return;
976         } 
977
978         statuswindow_pop_all();
979         session->authenticated = TRUE;
980         return;
981 }
982
983 static void imap_session_destroy(Session *session)
984 {
985         if (session->state != SESSION_DISCONNECTED)
986                 imap_threaded_disconnect(IMAP_SESSION(session)->folder);
987         
988         imap_free_capabilities(IMAP_SESSION(session));
989         g_free(IMAP_SESSION(session)->mbox);
990         sock_close(session->sock);
991         session->sock = NULL;
992 }
993
994 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
995 {
996         return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
997 }
998
999 static guint get_file_size_with_crs(const gchar *filename) 
1000 {
1001         FILE *fp = NULL;
1002         guint cnt = 0;
1003         gchar buf[4096];
1004         
1005         if (filename == NULL)
1006                 return -1;
1007         
1008         fp = fopen(filename, "rb");
1009         if (!fp)
1010                 return -1;
1011         
1012         while (fgets(buf, sizeof (buf), fp) != NULL) {
1013                 cnt += strlen(buf);
1014                 if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
1015                         cnt++;
1016         }
1017         
1018         fclose(fp);
1019         return cnt;
1020 }
1021
1022 static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
1023 {
1024         gchar *path, *filename;
1025
1026         path = folder_item_get_path(item);
1027
1028         if (!is_dir_exist(path)) {
1029                 g_free(path);
1030                 return;
1031         }
1032
1033         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
1034         g_free(path);
1035
1036         if (is_file_exist(filename)) {
1037                 g_unlink(filename);
1038         }
1039         g_free(filename);
1040 }
1041
1042 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1043                                   gboolean headers, gboolean body)
1044 {
1045         gchar *path, *filename;
1046         IMAPSession *session;
1047         gint ok;
1048
1049         g_return_val_if_fail(folder != NULL, NULL);
1050         g_return_val_if_fail(item != NULL, NULL);
1051
1052         if (uid == 0)
1053                 return NULL;
1054
1055         path = folder_item_get_path(item);
1056         if (!is_dir_exist(path))
1057                 make_dir_hier(path);
1058         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1059         g_free(path);
1060         debug_print("trying to fetch cached %s\n", filename);
1061         if (is_file_exist(filename)) {
1062                 /* see whether the local file represents the whole message
1063                  * or not. As the IMAP server reports size with \r chars,
1064                  * we have to update the local file (UNIX \n only) size */
1065                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1066                 guint have_size = -1;
1067
1068                 if (cached)
1069                         debug_print("message %d has been already %scached.\n", uid,
1070                                 MSG_IS_FULLY_CACHED(cached->flags) ? "fully ":"");
1071                 
1072                 if (!cached || !MSG_IS_FULLY_CACHED(cached->flags)) {
1073                         have_size = get_file_size_with_crs(filename);
1074                         if (cached && (cached->size <= have_size || !body)) {
1075                                 procmsg_msginfo_free(cached);
1076                                 ok = file_strip_crs(filename);
1077                                 if (ok == 0 && cached && cached->size <= have_size) {
1078                                         /* we have it all and stripped */
1079                                         debug_print("...fully cached in fact; setting flag.\n");
1080                                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1081                                 }
1082                                 return filename;
1083                         } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
1084                                 debug_print("message not cached and file recent, considering file complete\n");
1085                                 ok = file_strip_crs(filename);
1086                                 if (ok == 0)
1087                                         return filename;
1088                         } else {
1089                                 procmsg_msginfo_free(cached);
1090                         }
1091                 }
1092                 if (cached && MSG_IS_FULLY_CACHED(cached->flags)) {
1093                         procmsg_msginfo_free(cached);
1094                         return filename;
1095                 }
1096         } else {
1097                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1098                 if (cached) {
1099                         procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1100                         procmsg_msginfo_free(cached);
1101                 }
1102         }
1103
1104         debug_print("getting session...\n");
1105         session = imap_session_get(folder);
1106         
1107         if (!session) {
1108                 g_free(filename);
1109                 return NULL;
1110         }
1111
1112         debug_print("IMAP fetching messages\n");
1113         ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1114                          NULL, NULL, NULL, NULL, FALSE);
1115         if (ok != IMAP_SUCCESS) {
1116                 g_warning("can't select mailbox %s\n", item->path);
1117                 g_free(filename);
1118                 unlock_session(session);
1119                 return NULL;
1120         }
1121
1122         debug_print("getting message %d...\n", uid);
1123         ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1124
1125         if (ok != IMAP_SUCCESS) {
1126                 g_warning("can't fetch message %d\n", uid);
1127                 g_free(filename);
1128                 unlock_session(session);
1129                 return NULL;
1130         }
1131
1132         unlock_session(session);
1133         ok = file_strip_crs(filename);
1134
1135         if (ok == 0 && headers && body) {
1136                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1137                 if (cached) {
1138                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1139                         procmsg_msginfo_free(cached);
1140                 }
1141         } else if (ok == -1) {
1142                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1143                 if (cached) {
1144                         procmsg_msginfo_unset_flags(cached, MSG_FULLY_CACHED, 0);
1145                         procmsg_msginfo_free(cached);
1146                 }
1147         }
1148         return filename;
1149 }
1150
1151 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
1152 {
1153         gchar *path, *filename;
1154         guint size = 0;
1155         MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1156         
1157         if (!cached)
1158                 return FALSE;
1159
1160         if (MSG_IS_FULLY_CACHED(cached->flags)) {
1161                 procmsg_msginfo_free(cached);
1162                 return TRUE;
1163         }
1164         path = folder_item_get_path(item);
1165         if (!is_dir_exist(path))
1166                 return FALSE;
1167
1168         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1169         g_free(path);
1170         if (is_file_exist(filename)) {
1171                 if (cached && cached->total_size == cached->size) {
1172                         /* fast path */
1173                         g_free(filename);
1174                         procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1175                         return TRUE;
1176                 }
1177                 size = get_file_size_with_crs(filename);
1178                 g_free(filename);
1179         }
1180         if (cached && size >= cached->size) {
1181                 cached->total_size = cached->size;
1182                 procmsg_msginfo_set_flags(cached, MSG_FULLY_CACHED, 0);
1183                 procmsg_msginfo_free(cached);
1184                 return TRUE;
1185         }
1186         if (cached)
1187                 procmsg_msginfo_free(cached);
1188         return FALSE;   
1189 }
1190
1191 void imap_cache_msg(FolderItem *item, gint msgnum)
1192 {
1193         Folder *folder = NULL;
1194         
1195         if (!item)
1196                 return;
1197         folder = item->folder;
1198         
1199         if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
1200                 gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
1201                 debug_print("fetched %s\n", tmp);
1202                 g_free(tmp);
1203         }
1204 }
1205
1206 static gint imap_add_msg(Folder *folder, FolderItem *dest, 
1207                          const gchar *file, MsgFlags *flags)
1208 {
1209         gint ret;
1210         GSList file_list;
1211         MsgFileInfo fileinfo;
1212
1213         g_return_val_if_fail(file != NULL, -1);
1214
1215         fileinfo.msginfo = NULL;
1216         fileinfo.file = (gchar *)file;
1217         fileinfo.flags = flags;
1218         file_list.data = &fileinfo;
1219         file_list.next = NULL;
1220
1221         ret = imap_add_msgs(folder, dest, &file_list, NULL);
1222         return ret;
1223 }
1224
1225 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1226                    GRelation *relation)
1227 {
1228         gchar *destdir;
1229         IMAPSession *session;
1230         guint32 last_uid = 0;
1231         GSList *cur;
1232         MsgFileInfo *fileinfo;
1233         gint ok;
1234         gint curnum = 0, total = 0;
1235         gboolean missing_uids = FALSE;
1236
1237         g_return_val_if_fail(folder != NULL, -1);
1238         g_return_val_if_fail(dest != NULL, -1);
1239         g_return_val_if_fail(file_list != NULL, -1);
1240         
1241         debug_print("getting session...\n");
1242         session = imap_session_get(folder);
1243         if (!session) {
1244                 return -1;
1245         }
1246         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1247
1248         statusbar_print_all(_("Adding messages..."));
1249         total = g_slist_length(file_list);
1250         for (cur = file_list; cur != NULL; cur = cur->next) {
1251                 IMAPFlags iflags = 0;
1252                 guint32 new_uid = 0;
1253                 gchar *real_file = NULL;
1254                 fileinfo = (MsgFileInfo *)cur->data;
1255
1256                 statusbar_progress_all(curnum, total, total < 10 ? 1:10);
1257                 curnum++;
1258
1259                 if (fileinfo->flags) {
1260                         if (MSG_IS_MARKED(*fileinfo->flags))
1261                                 iflags |= IMAP_FLAG_FLAGGED;
1262                         if (MSG_IS_REPLIED(*fileinfo->flags))
1263                                 iflags |= IMAP_FLAG_ANSWERED;
1264                         if (!MSG_IS_UNREAD(*fileinfo->flags))
1265                                 iflags |= IMAP_FLAG_SEEN;
1266                 }
1267                 
1268                 if (real_file == NULL)
1269                         real_file = g_strdup(fileinfo->file);
1270                 
1271                 if (folder_has_parent_of_type(dest, F_QUEUE) ||
1272                     folder_has_parent_of_type(dest, F_OUTBOX) ||
1273                     folder_has_parent_of_type(dest, F_DRAFT) ||
1274                     folder_has_parent_of_type(dest, F_TRASH))
1275                         iflags |= IMAP_FLAG_SEEN;
1276
1277                 ok = imap_cmd_append(session, destdir, real_file, iflags, 
1278                                      &new_uid);
1279
1280                 if (ok != IMAP_SUCCESS) {
1281                         g_warning("can't append message %s\n", real_file);
1282                         g_free(real_file);
1283                         g_free(destdir);
1284                         unlock_session(session);
1285                         statusbar_progress_all(0,0,0);
1286                         statusbar_pop_all();
1287                         return -1;
1288                 } else {
1289                         debug_print("appended new message as %d\n", new_uid);
1290                         /* put the local file in the imapcache, so that we don't
1291                          * have to fetch it back later. */
1292                         
1293                         if (new_uid == 0) {
1294                                 missing_uids = TRUE;
1295                                 debug_print("Missing UID (0)\n");
1296                         }
1297                         if (new_uid > 0) {
1298                                 gchar *cache_path = folder_item_get_path(dest);
1299                                 if (!is_dir_exist(cache_path))
1300                                         make_dir_hier(cache_path);
1301                                 if (is_dir_exist(cache_path)) {
1302                                         gchar *cache_file = g_strconcat(
1303                                                 cache_path, G_DIR_SEPARATOR_S, 
1304                                                 itos(new_uid), NULL);
1305                                         copy_file(real_file, cache_file, TRUE);
1306                                         debug_print("got UID %d, copied to cache: %s\n", new_uid, cache_file);
1307                                         g_free(cache_file);
1308                                 }
1309                                 g_free(cache_path);
1310                         }
1311                 }
1312
1313                 if (relation != NULL)
1314                         g_relation_insert(relation, fileinfo->msginfo != NULL ? 
1315                                           (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1316                                           GINT_TO_POINTER(new_uid));
1317                 if (last_uid < new_uid) {
1318                         last_uid = new_uid;
1319                 }
1320
1321                 g_free(real_file);
1322         }
1323         
1324         statusbar_progress_all(0,0,0);
1325         statusbar_pop_all();
1326         
1327         unlock_session(session);
1328         
1329         g_free(destdir);
1330
1331         imap_scan_required(folder, dest);
1332
1333         if (missing_uids) {
1334                 gint a;
1335                 ok = imap_select(session, IMAP_FOLDER(folder), dest->path,
1336                          &a, NULL, NULL, NULL, FALSE);
1337         }
1338         return last_uid;
1339 }
1340
1341 static GSList *flatten_mailimap_set(struct mailimap_set * set) 
1342 {
1343         GSList *result = NULL;
1344         clistiter *list;
1345         int start, end, t;
1346         GSList *cur;
1347
1348         for (list = clist_begin(set->set_list); list; list = clist_next(list)) {
1349                 struct mailimap_set_item *item = (struct mailimap_set_item *)clist_content(list);
1350                 start = item->set_first;
1351                 end = item->set_last;
1352                 for (t = start; t <= end; t++) {
1353                         result = g_slist_prepend(result, GINT_TO_POINTER(t));
1354                 }
1355         }
1356         result = g_slist_reverse(result);
1357         if (debug_get_mode()) {
1358                 debug_print("flat imap set: ");
1359                 for (cur = result; cur; cur = cur->next) {
1360                         debug_print("%d ", GPOINTER_TO_INT(cur->data));
1361                 }
1362                 debug_print("\n");
1363         }
1364         
1365         return result;
1366 }
1367 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, 
1368                               MsgInfoList *msglist, GRelation *relation)
1369 {
1370         FolderItem *src;
1371         gchar *destdir;
1372         GSList *seq_list, *cur;
1373         MsgInfo *msginfo;
1374         IMAPSession *session;
1375         gint ok = IMAP_SUCCESS;
1376         GRelation *uid_mapping;
1377         gint last_num = 0;
1378         gboolean single = FALSE;
1379
1380         g_return_val_if_fail(folder != NULL, -1);
1381         g_return_val_if_fail(dest != NULL, -1);
1382         g_return_val_if_fail(msglist != NULL, -1);
1383         
1384         debug_print("getting session...\n");
1385         session = imap_session_get(folder);
1386         
1387         if (!session) {
1388                 return -1;
1389         }
1390
1391         msginfo = (MsgInfo *)msglist->data;
1392         if (msglist->next == NULL)
1393                 single = TRUE;
1394         src = msginfo->folder;
1395         if (src == dest) {
1396                 g_warning("the src folder is identical to the dest.\n");
1397                 unlock_session(session);
1398                 return -1;
1399         }
1400
1401         if (src->folder != dest->folder) {
1402                 GSList *infolist = NULL, *cur;
1403                 int res = -1;
1404                 for (cur = msglist; cur; cur = cur->next) {
1405                         msginfo = (MsgInfo *)cur->data;
1406                         MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
1407                         fileinfo->file = procmsg_get_message_file(msginfo);
1408                         fileinfo->flags = &(msginfo->flags);
1409                         infolist = g_slist_prepend(infolist, fileinfo);
1410                 }
1411                 infolist = g_slist_reverse(infolist);
1412                 unlock_session(session);
1413                 res = folder_item_add_msgs(dest, infolist, FALSE);
1414                 for (cur = infolist; cur; cur = cur->next) {
1415                         MsgFileInfo *info = (MsgFileInfo *)cur->data;
1416                         g_free(info->file);
1417                         g_free(info);
1418                 }
1419                 g_slist_free(infolist);
1420                 return res;
1421         } 
1422
1423         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1424                          NULL, NULL, NULL, NULL, FALSE);
1425         if (ok != IMAP_SUCCESS) {
1426                 unlock_session(session);
1427                 return ok;
1428         }
1429
1430         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1431         seq_list = imap_get_lep_set_from_msglist(msglist);
1432         uid_mapping = g_relation_new(2);
1433         g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1434         
1435         statusbar_print_all(_("Copying messages..."));
1436         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1437                 struct mailimap_set * seq_set;
1438                 struct mailimap_set * source = NULL;
1439                 struct mailimap_set * dest = NULL;
1440                 seq_set = cur->data;
1441
1442                 debug_print("Copying messages from %s to %s ...\n",
1443                             src->path, destdir);
1444
1445                 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
1446                         &source, &dest);
1447                 
1448                 if (ok == IMAP_SUCCESS) {
1449                         if (relation && source && dest) {
1450                                 GSList *s_list = flatten_mailimap_set(source);
1451                                 GSList *d_list = flatten_mailimap_set(dest);
1452                                 GSList *s_cur, *d_cur;
1453                                 if (g_slist_length(s_list) == g_slist_length(d_list)) {
1454
1455                                         for (s_cur = s_list, d_cur = d_list; 
1456                                              s_cur && d_cur; 
1457                                              s_cur = s_cur->next, d_cur = d_cur->next) {
1458                                                 g_relation_insert(uid_mapping, s_cur->data, d_cur->data);
1459                                         }
1460
1461                                 } else {
1462                                         debug_print("hhhmm, source list length != dest list length.\n");
1463                                 }
1464                                 g_slist_free(s_list);
1465                                 g_slist_free(d_list);
1466                         }
1467                 }
1468
1469
1470                 if (source)
1471                         mailimap_set_free(source);
1472                 if (dest)
1473                         mailimap_set_free(dest);
1474
1475                 if (ok != IMAP_SUCCESS) {
1476                         g_relation_destroy(uid_mapping);
1477                         imap_lep_set_free(seq_list);
1478                         unlock_session(session);
1479                         statusbar_pop_all();
1480                         return -1;
1481                 }
1482         }
1483
1484         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1485                 MsgInfo *msginfo = (MsgInfo *)cur->data;
1486                 GTuples *tuples;
1487
1488                 tuples = g_relation_select(uid_mapping, 
1489                                            GINT_TO_POINTER(msginfo->msgnum),
1490                                            0);
1491                 if (tuples->len > 0) {
1492                         gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1493                         g_relation_insert(relation, msginfo,
1494                                           GINT_TO_POINTER(num));
1495                         if (num > last_num)
1496                                 last_num = num;
1497                         debug_print("copied message %d as %d\n", msginfo->msgnum, num);
1498                         /* put the local file in the imapcache, so that we don't
1499                          * have to fetch it back later. */
1500                         if (num > 0) {
1501                                 gchar *cache_path = folder_item_get_path(msginfo->folder);
1502                                 gchar *real_file = g_strconcat(
1503                                         cache_path, G_DIR_SEPARATOR_S, 
1504                                         itos(msginfo->msgnum), NULL);
1505                                 gchar *cache_file = NULL;
1506                                 g_free(cache_path);
1507                                 cache_path = folder_item_get_path(dest);
1508                                 cache_file = g_strconcat(
1509                                         cache_path, G_DIR_SEPARATOR_S, 
1510                                         itos(num), NULL);
1511                                 if (!is_dir_exist(cache_path))
1512                                         make_dir_hier(cache_path);
1513                                 if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
1514                                         copy_file(real_file, cache_file, TRUE);
1515                                         debug_print("copied to cache: %s\n", cache_file);
1516                                 }
1517                                 g_free(real_file);
1518                                 g_free(cache_file);
1519                                 g_free(cache_path);
1520                         }
1521                 } else
1522                         g_relation_insert(relation, msginfo,
1523                                           GINT_TO_POINTER(0));
1524                 g_tuples_destroy(tuples);
1525         }
1526         statusbar_pop_all();
1527
1528         g_relation_destroy(uid_mapping);
1529         imap_lep_set_free(seq_list);
1530
1531         g_free(destdir);
1532         
1533         IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1534         IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1535         g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1536         IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1537
1538         unlock_session(session);
1539         imap_scan_required(folder, dest);
1540         if (ok == IMAP_SUCCESS)
1541                 return last_num;
1542         else
1543                 return -1;
1544 }
1545
1546 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1547 {
1548         GSList msglist;
1549
1550         g_return_val_if_fail(msginfo != NULL, -1);
1551
1552         msglist.data = msginfo;
1553         msglist.next = NULL;
1554
1555         return imap_copy_msgs(folder, dest, &msglist, NULL);
1556 }
1557
1558 static gint imap_copy_msgs(Folder *folder, FolderItem *dest, 
1559                     MsgInfoList *msglist, GRelation *relation)
1560 {
1561         MsgInfo *msginfo;
1562         gint ret;
1563
1564         g_return_val_if_fail(folder != NULL, -1);
1565         g_return_val_if_fail(dest != NULL, -1);
1566         g_return_val_if_fail(msglist != NULL, -1);
1567
1568         msginfo = (MsgInfo *)msglist->data;
1569         g_return_val_if_fail(msginfo->folder != NULL, -1);
1570
1571         ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1572         return ret;
1573 }
1574
1575
1576 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
1577                                 MsgInfoList *msglist, GRelation *relation)
1578 {
1579         gchar *destdir, *dir;
1580         GSList *numlist = NULL, *cur;
1581         MsgInfo *msginfo;
1582         IMAPSession *session;
1583         gint ok = IMAP_SUCCESS;
1584         GRelation *uid_mapping;
1585         
1586         g_return_val_if_fail(folder != NULL, -1);
1587         g_return_val_if_fail(dest != NULL, -1);
1588         g_return_val_if_fail(msglist != NULL, -1);
1589
1590         debug_print("getting session...\n");
1591         session = imap_session_get(folder);
1592         if (!session) {
1593                 return -1;
1594         }
1595
1596         msginfo = (MsgInfo *)msglist->data;
1597
1598         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1599                          NULL, NULL, NULL, NULL, FALSE);
1600         if (ok != IMAP_SUCCESS) {
1601                 unlock_session(session);
1602                 return ok;
1603         }
1604
1605         destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
1606         for (cur = msglist; cur; cur = cur->next) {
1607                 msginfo = (MsgInfo *)cur->data;
1608                 if (!MSG_IS_DELETED(msginfo->flags))
1609                         numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
1610         }
1611         numlist = g_slist_reverse(numlist);
1612
1613         uid_mapping = g_relation_new(2);
1614         g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1615
1616         ok = imap_set_message_flags
1617                 (session, numlist, IMAP_FLAG_DELETED, TRUE);
1618         if (ok != IMAP_SUCCESS) {
1619                 log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
1620                 unlock_session(session);
1621                 return ok;
1622         }
1623         ok = imap_cmd_expunge(session);
1624         if (ok != IMAP_SUCCESS) {
1625                 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
1626                 unlock_session(session);
1627                 return ok;
1628         }
1629         
1630         dir = folder_item_get_path(msginfo->folder);
1631         if (is_dir_exist(dir)) {
1632                 for (cur = msglist; cur; cur = cur->next) {
1633                         msginfo = (MsgInfo *)cur->data;
1634                         remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
1635                 }
1636         }
1637         g_free(dir);
1638
1639         g_relation_destroy(uid_mapping);
1640         g_slist_free(numlist);
1641
1642         g_free(destdir);
1643         unlock_session(session);
1644         if (ok == IMAP_SUCCESS)
1645                 return 0;
1646         else
1647                 return -1;
1648 }
1649
1650 static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
1651                     MsgInfoList *msglist, GRelation *relation)
1652 {
1653         MsgInfo *msginfo;
1654
1655         g_return_val_if_fail(folder != NULL, -1);
1656         g_return_val_if_fail(dest != NULL, -1);
1657         if (msglist == NULL)
1658                 return 0;
1659
1660         msginfo = (MsgInfo *)msglist->data;
1661         g_return_val_if_fail(msginfo->folder != NULL, -1);
1662
1663         return imap_do_remove_msgs(folder, dest, msglist, relation);
1664 }
1665
1666 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1667 {
1668         GSList *list = folder_item_get_msg_list(item);
1669         gint res = imap_remove_msgs(folder, item, list, NULL);
1670         procmsg_msg_list_free(list);
1671         return res;
1672 }
1673
1674 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
1675                                     MsgInfo *msginfo)
1676 {
1677         /* TODO: properly implement this method */
1678         return FALSE;
1679 }
1680
1681 static gint imap_close(Folder *folder, FolderItem *item)
1682 {
1683         return 0;
1684 }
1685
1686 gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
1687 {
1688         FolderItem *item = NULL;
1689         IMAPSession *session;
1690         gchar *root_folder = NULL;
1691
1692         g_return_val_if_fail(folder != NULL, -1);
1693         g_return_val_if_fail(folder->account != NULL, -1);
1694
1695         debug_print("getting session...\n");
1696         session = imap_session_get(folder);
1697         if (!session) {
1698                 if (!folder->node) {
1699                         folder_tree_destroy(folder);
1700                         item = folder_item_new(folder, folder->name, NULL);
1701                         item->folder = folder;
1702                         folder->node = item->node = g_node_new(item);
1703                 }
1704                 return -1;
1705         }
1706
1707         if (folder->account->imap_dir && *folder->account->imap_dir) {
1708                 gchar *real_path;
1709                 int r;
1710                 clist * lep_list;
1711
1712                 Xstrdup_a(root_folder, folder->account->imap_dir, {unlock_session(session);return -1;});
1713                 extract_quote(root_folder, '"');
1714                 subst_char(root_folder,
1715                            imap_get_path_separator(session, IMAP_FOLDER(folder),
1716                                                    root_folder),
1717                            '/');
1718                 strtailchomp(root_folder, '/');
1719                 real_path = imap_get_real_path
1720                         (session, IMAP_FOLDER(folder), root_folder);
1721                 debug_print("IMAP root directory: %s\n", real_path);
1722
1723                 /* check if root directory exist */
1724
1725                 r = imap_threaded_list(session->folder, "", real_path,
1726                                        &lep_list);
1727                 if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
1728                         if (!folder->node) {
1729                                 item = folder_item_new(folder, folder->name, NULL);
1730                                 item->folder = folder;
1731                                 folder->node = item->node = g_node_new(item);
1732                         }
1733                         unlock_session(session);
1734                         return -1;
1735                 }
1736                 mailimap_list_result_free(lep_list);
1737                                 
1738                 g_free(real_path);
1739         }
1740
1741         if (folder->node)
1742                 item = FOLDER_ITEM(folder->node->data);
1743                 
1744         if (item && !item->path && root_folder) {
1745                 item->path = g_strdup(root_folder);
1746         }
1747
1748         if (!item || ((item->path || root_folder) &&
1749                       strcmp2(item->path, root_folder) != 0)) {
1750                 folder_tree_destroy(folder);
1751                 item = folder_item_new(folder, folder->name, root_folder);
1752                 item->folder = folder;
1753                 folder->node = item->node = g_node_new(item);
1754         }
1755
1756         imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
1757         imap_create_missing_folders(folder);
1758         unlock_session(session);
1759
1760         return 0;
1761 }
1762
1763 static gint imap_scan_tree(Folder *folder)
1764 {
1765         gboolean subs_only = FALSE;
1766         if (folder->account) {
1767                 debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
1768                 subs_only = folder->account->imap_subsonly;
1769         }
1770         return imap_scan_tree_real(folder, subs_only);
1771 }
1772
1773 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
1774 {
1775         Folder *folder;
1776         IMAPFolder *imapfolder;
1777         FolderItem *new_item;
1778         GSList *item_list, *cur;
1779         GNode *node;
1780         gchar *real_path;
1781         gchar *wildcard_path;
1782         gchar separator;
1783         gchar wildcard[3];
1784         clist * lep_list;
1785         int r;
1786         
1787         g_return_val_if_fail(item != NULL, -1);
1788         g_return_val_if_fail(item->folder != NULL, -1);
1789         g_return_val_if_fail(item->no_sub == FALSE, -1);
1790
1791         folder = item->folder;
1792         imapfolder = IMAP_FOLDER(folder);
1793
1794         separator = imap_get_path_separator(session, imapfolder, item->path);
1795
1796         if (folder->ui_func)
1797                 folder->ui_func(folder, item, folder->ui_func_data);
1798
1799         if (item->path) {
1800                 wildcard[0] = separator;
1801                 wildcard[1] = '%';
1802                 wildcard[2] = '\0';
1803                 real_path = imap_get_real_path(session, imapfolder, item->path);
1804         } else {
1805                 wildcard[0] = '%';
1806                 wildcard[1] = '\0';
1807                 real_path = g_strdup("");
1808         }
1809
1810         Xstrcat_a(wildcard_path, real_path, wildcard,
1811                   {g_free(real_path); return IMAP_ERROR;});
1812         lep_list = NULL;
1813         
1814         if (subs_only)
1815                 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
1816         else
1817                 r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
1818
1819         if (r != MAILIMAP_NO_ERROR) {
1820                 item_list = NULL;
1821         }
1822         else {
1823                 item_list = imap_list_from_lep(imapfolder,
1824                                                lep_list, real_path, FALSE);
1825                 mailimap_list_result_free(lep_list);
1826         }
1827         
1828         g_free(real_path);
1829
1830         node = item->node->children;
1831         while (node != NULL) {
1832                 FolderItem *old_item = FOLDER_ITEM(node->data);
1833                 GNode *next = node->next;
1834
1835                 new_item = NULL;
1836                 for (cur = item_list; cur != NULL; cur = cur->next) {
1837                         FolderItem *cur_item = FOLDER_ITEM(cur->data);
1838                         if (!strcmp2(old_item->path, cur_item->path)) {
1839                                 new_item = cur_item;
1840                                 break;
1841                         }
1842                 }
1843                 if (!new_item) {
1844                         if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
1845                                 debug_print("not removing INBOX\n");
1846                         } else {
1847                                 debug_print("folder '%s' not found. removing...\n",
1848                                             old_item->path);
1849                                 folder_item_remove(old_item);
1850                         }
1851                 } else {
1852                         old_item->no_sub = new_item->no_sub;
1853                         old_item->no_select = new_item->no_select;
1854                         if (old_item->no_sub == TRUE && node->children) {
1855                                 debug_print("folder '%s' doesn't have "
1856                                             "subfolders. removing...\n",
1857                                             old_item->path);
1858                                 folder_item_remove_children(old_item);
1859                         }
1860                 }
1861
1862                 node = next;
1863         }
1864
1865         for (cur = item_list; cur != NULL; cur = cur->next) {
1866                 FolderItem *cur_item = FOLDER_ITEM(cur->data);
1867                 new_item = NULL;
1868
1869                 for (node = item->node->children; node != NULL;
1870                      node = node->next) {
1871                         if (!strcmp2(FOLDER_ITEM(node->data)->path,
1872                                      cur_item->path)) {
1873                                 new_item = FOLDER_ITEM(node->data);
1874                                 folder_item_destroy(cur_item);
1875                                 cur_item = NULL;
1876                                 break;
1877                         }
1878                 }
1879                 if (!new_item) {
1880                         new_item = cur_item;
1881                         debug_print("new folder '%s' found.\n", new_item->path);
1882                         folder_item_append(item, new_item);
1883                 }
1884
1885                 if (!strcmp(new_item->path, "INBOX")) {
1886                         new_item->stype = F_INBOX;
1887                         folder->inbox = new_item;
1888                 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
1889                         gchar *base;
1890
1891                         base = g_path_get_basename(new_item->path);
1892
1893                         if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
1894                                 new_item->stype = F_OUTBOX;
1895                                 folder->outbox = new_item;
1896                         } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
1897                                 new_item->stype = F_DRAFT;
1898                                 folder->draft = new_item;
1899                         } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
1900                                 new_item->stype = F_QUEUE;
1901                                 folder->queue = new_item;
1902                         } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
1903                                 new_item->stype = F_TRASH;
1904                                 folder->trash = new_item;
1905                         }
1906                         g_free(base);
1907                 }
1908
1909                 if (new_item->no_sub == FALSE)
1910                         imap_scan_tree_recursive(session, new_item, subs_only);
1911         }
1912
1913         g_slist_free(item_list);
1914
1915         return IMAP_SUCCESS;
1916 }
1917
1918 GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
1919 {
1920         IMAPSession *session = imap_session_get(folder);
1921         gchar *real_path;
1922         gchar *wildcard_path;
1923         gchar separator;
1924         gchar wildcard[3];
1925         clist * lep_list;
1926         GSList *item_list = NULL, *cur;
1927         GList *child_list = NULL, *tmplist = NULL;
1928         GSList *sub_list = NULL;
1929         int r;
1930
1931         if (!session)
1932                 return NULL;
1933
1934         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
1935
1936         if (item->path) {
1937                 wildcard[0] = separator;
1938                 wildcard[1] = '%';
1939                 wildcard[2] = '\0';
1940                 real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
1941         } else {
1942                 wildcard[0] = '%';
1943                 wildcard[1] = '\0';
1944                 real_path = g_strdup("");
1945         }
1946
1947         Xstrcat_a(wildcard_path, real_path, wildcard,
1948                   {g_free(real_path); return NULL;});
1949         lep_list = NULL;
1950         
1951         if (unsubs_only)
1952                 statusbar_print_all(_("Looking for unsubscribed folders in %s..."), 
1953                                 item->path?item->path:item->name);
1954         else
1955                 statusbar_print_all(_("Looking for subfolders of %s..."), 
1956                                 item->path?item->path:item->name);
1957
1958         r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
1959         if (r) {
1960                 statusbar_pop_all();
1961                 return NULL;
1962         }
1963         item_list = imap_list_from_lep(IMAP_FOLDER(folder),
1964                                lep_list, real_path, FALSE);
1965         mailimap_list_result_free(lep_list);
1966
1967         for (cur = item_list; cur != NULL; cur = cur->next) {
1968                 FolderItem *cur_item = FOLDER_ITEM(cur->data);
1969                 if (recursive) {
1970                         tmplist = imap_scan_subtree(folder, cur_item, 
1971                                         unsubs_only, recursive);
1972                         if (tmplist)
1973                                 child_list = g_list_concat(child_list, tmplist);
1974                 }
1975                 child_list = g_list_prepend(child_list,
1976                                 imap_get_real_path(session, 
1977                                         IMAP_FOLDER(folder), cur_item->path));
1978                 
1979                 folder_item_destroy(cur_item);
1980         }
1981         child_list = g_list_reverse(child_list);
1982         g_slist_free(item_list);
1983
1984         if (unsubs_only) {
1985                 r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
1986                 if (r) {
1987                         statusbar_pop_all();
1988                         return NULL;
1989                 }
1990                 sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
1991                                        lep_list, real_path, FALSE);
1992                 mailimap_list_result_free(lep_list);
1993
1994                 for (cur = sub_list; cur != NULL; cur = cur->next) {
1995                         FolderItem *cur_item = FOLDER_ITEM(cur->data);
1996                         GList *oldlitem = NULL;
1997                         gchar *tmp = imap_get_real_path(session, 
1998                                         IMAP_FOLDER(folder), cur_item->path);
1999                         folder_item_destroy(cur_item);
2000                         oldlitem = g_list_find_custom(
2001                                         child_list, tmp, (GCompareFunc)strcmp2);
2002                         if (oldlitem) {
2003                                 child_list = g_list_remove_link(child_list, oldlitem);
2004                                 g_free(oldlitem->data);
2005                                 g_list_free(oldlitem);
2006                         }
2007                         g_free(tmp);
2008                 }
2009         }
2010
2011         statusbar_pop_all();
2012
2013         return child_list;
2014 }
2015
2016 static gint imap_create_tree(Folder *folder)
2017 {
2018         g_return_val_if_fail(folder != NULL, -1);
2019         g_return_val_if_fail(folder->node != NULL, -1);
2020         g_return_val_if_fail(folder->node->data != NULL, -1);
2021         g_return_val_if_fail(folder->account != NULL, -1);
2022
2023         imap_scan_tree(folder);
2024         imap_create_missing_folders(folder);
2025
2026         return 0;
2027 }
2028
2029 static void imap_create_missing_folders(Folder *folder)
2030 {
2031         g_return_if_fail(folder != NULL);
2032
2033         if (!folder->inbox)
2034                 folder->inbox = imap_create_special_folder
2035                         (folder, F_INBOX, "INBOX");
2036         if (!folder->trash)
2037                 folder->trash = imap_create_special_folder
2038                         (folder, F_TRASH, "Trash");
2039         if (!folder->queue)
2040                 folder->queue = imap_create_special_folder
2041                         (folder, F_QUEUE, "Queue");
2042         if (!folder->outbox)
2043                 folder->outbox = imap_create_special_folder
2044                         (folder, F_OUTBOX, "Sent");
2045         if (!folder->draft)
2046                 folder->draft = imap_create_special_folder
2047                         (folder, F_DRAFT, "Drafts");
2048 }
2049
2050 static FolderItem *imap_create_special_folder(Folder *folder,
2051                                               SpecialFolderItemType stype,
2052                                               const gchar *name)
2053 {
2054         FolderItem *item;
2055         FolderItem *new_item;
2056
2057         g_return_val_if_fail(folder != NULL, NULL);
2058         g_return_val_if_fail(folder->node != NULL, NULL);
2059         g_return_val_if_fail(folder->node->data != NULL, NULL);
2060         g_return_val_if_fail(folder->account != NULL, NULL);
2061         g_return_val_if_fail(name != NULL, NULL);
2062
2063         item = FOLDER_ITEM(folder->node->data);
2064         new_item = imap_create_folder(folder, item, name);
2065
2066         if (!new_item) {
2067                 g_warning("Can't create '%s'\n", name);
2068                 if (!folder->inbox) return NULL;
2069
2070                 new_item = imap_create_folder(folder, folder->inbox, name);
2071                 if (!new_item)
2072                         g_warning("Can't create '%s' under INBOX\n", name);
2073                 else
2074                         new_item->stype = stype;
2075         } else
2076                 new_item->stype = stype;
2077
2078         return new_item;
2079 }
2080
2081 static gchar *imap_folder_get_path(Folder *folder)
2082 {
2083         gchar *folder_path;
2084
2085         g_return_val_if_fail(folder != NULL, NULL);
2086         g_return_val_if_fail(folder->account != NULL, NULL);
2087
2088         folder_path = g_strconcat(get_imap_cache_dir(),
2089                                   G_DIR_SEPARATOR_S,
2090                                   folder->account->recv_server,
2091                                   G_DIR_SEPARATOR_S,
2092                                   folder->account->userid,
2093                                   NULL);
2094
2095         return folder_path;
2096 }
2097
2098 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
2099 {
2100         gchar *folder_path, *path;
2101
2102         g_return_val_if_fail(folder != NULL, NULL);
2103         g_return_val_if_fail(item != NULL, NULL);
2104         folder_path = imap_folder_get_path(folder);
2105
2106         g_return_val_if_fail(folder_path != NULL, NULL);
2107         if (folder_path[0] == G_DIR_SEPARATOR) {
2108                 if (item->path)
2109                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
2110                                            item->path, NULL);
2111                 else
2112                         path = g_strdup(folder_path);
2113         } else {
2114                 if (item->path)
2115                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2116                                            folder_path, G_DIR_SEPARATOR_S,
2117                                            item->path, NULL);
2118                 else
2119                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2120                                            folder_path, NULL);
2121         }
2122         g_free(folder_path);
2123
2124         return path;
2125 }
2126
2127 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
2128                                const gchar *name)
2129 {
2130         gchar *dirpath, *imap_path;
2131         IMAPSession *session;
2132         FolderItem *new_item;
2133         gchar separator;
2134         gchar *new_name;
2135         const gchar *p;
2136         gint ok;
2137         gboolean no_select = FALSE, no_sub = FALSE;
2138         gboolean exist = FALSE;
2139         
2140         g_return_val_if_fail(folder != NULL, NULL);
2141         g_return_val_if_fail(folder->account != NULL, NULL);
2142         g_return_val_if_fail(parent != NULL, NULL);
2143         g_return_val_if_fail(name != NULL, NULL);
2144
2145         debug_print("getting session...\n");
2146         session = imap_session_get(folder);
2147         if (!session) {
2148                 return NULL;
2149         }
2150
2151         if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
2152                 dirpath = g_strdup(name);
2153         }else if (parent->path)
2154                 dirpath = g_strconcat(parent->path, "/", name, NULL);
2155         else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
2156                 dirpath = g_strdup(name);
2157         else if (folder->account->imap_dir && *folder->account->imap_dir) {
2158                 gchar *imap_dir;
2159
2160                 Xstrdup_a(imap_dir, folder->account->imap_dir, {unlock_session(session);return NULL;});
2161                 strtailchomp(imap_dir, '/');
2162                 dirpath = g_strconcat(imap_dir, "/", name, NULL);
2163         } else
2164                 dirpath = g_strdup(name);
2165                 
2166         
2167
2168         /* keep trailing directory separator to create a folder that contains
2169            sub folder */
2170         imap_path = imap_utf8_to_modified_utf7(dirpath);
2171
2172         strtailchomp(dirpath, '/');
2173         Xstrdup_a(new_name, name, {
2174                 g_free(dirpath); 
2175                 unlock_session(session);                
2176                 return NULL;});
2177
2178         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
2179         imap_path_separator_subst(imap_path, separator);
2180         /* remove trailing / for display */
2181         strtailchomp(new_name, '/');
2182
2183         if (strcmp(dirpath, "INBOX") != 0) {
2184                 GPtrArray *argbuf;
2185                 int r;
2186                 clist * lep_list;
2187                 
2188                 argbuf = g_ptr_array_new();
2189                 r = imap_threaded_list(folder, "", imap_path, &lep_list);
2190                 if (r != MAILIMAP_NO_ERROR) {
2191                         log_warning(LOG_PROTOCOL, _("can't create mailbox: LIST failed\n"));
2192                         g_free(imap_path);
2193                         g_free(dirpath);
2194                         ptr_array_free_strings(argbuf);
2195                         g_ptr_array_free(argbuf, TRUE);
2196                         unlock_session(session);
2197                         return NULL;
2198                 }
2199                 
2200                 if (clist_count(lep_list) > 0)
2201                         exist = TRUE;
2202                 mailimap_list_result_free(lep_list);
2203                 lep_list = NULL;
2204                 if (!exist) {
2205                         ok = imap_cmd_create(session, imap_path);
2206                         if (ok != IMAP_SUCCESS) {
2207                                 log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
2208                                 g_free(imap_path);
2209                                 g_free(dirpath);
2210                                 unlock_session(session);
2211                                 return NULL;
2212                         }
2213                         r = imap_threaded_list(folder, "", imap_path, &lep_list);
2214                         if (r == MAILIMAP_NO_ERROR) {
2215                                 GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2216                                                lep_list, dirpath, TRUE);
2217                                 if (item_list) {
2218                                         FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2219                                         no_select = cur_item->no_select;
2220                                         no_sub = cur_item->no_sub;
2221                                         g_slist_free(item_list);
2222                                 } 
2223                                 mailimap_list_result_free(lep_list);
2224                         }
2225                 }
2226                 imap_threaded_subscribe(folder, imap_path, TRUE);
2227         } else {
2228                 clist *lep_list;
2229                 int r;
2230                 /* just get flags */
2231                 r = imap_threaded_list(folder, "", "INBOX", &lep_list);
2232                 if (r == MAILIMAP_NO_ERROR) {
2233                         GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
2234                                        lep_list, dirpath, TRUE);
2235                         if (item_list) {
2236                                 FolderItem *cur_item = FOLDER_ITEM(item_list->data);
2237                                 no_select = cur_item->no_select;
2238                                 no_sub = cur_item->no_sub;
2239                                 g_slist_free(item_list);
2240                         } 
2241                         mailimap_list_result_free(lep_list);
2242                 }
2243         }
2244
2245         new_item = folder_item_new(folder, new_name, dirpath);
2246         new_item->no_select = no_select;
2247         new_item->no_sub = no_sub;
2248         folder_item_append(parent, new_item);
2249         g_free(imap_path);
2250         g_free(dirpath);
2251
2252         dirpath = folder_item_get_path(new_item);
2253         if (!is_dir_exist(dirpath))
2254                 make_dir_hier(dirpath);
2255         g_free(dirpath);
2256         unlock_session(session);
2257
2258         if (exist) {
2259                 /* folder existed, scan it */
2260                 imap_scan_required(folder, new_item);
2261                 folder_item_scan_full(new_item, FALSE);
2262         }
2263
2264         return new_item;
2265 }
2266
2267 static gint imap_rename_folder(Folder *folder, FolderItem *item,
2268                                const gchar *name)
2269 {
2270         gchar *dirpath;
2271         gchar *newpath;
2272         gchar *real_oldpath;
2273         gchar *real_newpath;
2274         gchar *paths[2];
2275         gchar *old_cache_dir;
2276         gchar *new_cache_dir;
2277         IMAPSession *session;
2278         gchar separator;
2279         gint ok;
2280         gint exists, recent, unseen;
2281         guint32 uid_validity;
2282
2283         g_return_val_if_fail(folder != NULL, -1);
2284         g_return_val_if_fail(item != NULL, -1);
2285         g_return_val_if_fail(item->path != NULL, -1);
2286         g_return_val_if_fail(name != NULL, -1);
2287
2288         debug_print("getting session...\n");
2289         session = imap_session_get(folder);
2290         if (!session) {
2291                 return -1;
2292         }
2293
2294         if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
2295                 g_warning(_("New folder name must not contain the namespace "
2296                             "path separator"));
2297                 unlock_session(session);
2298                 return -1;
2299         }
2300
2301         real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2302
2303         g_free(session->mbox);
2304         session->mbox = NULL;
2305         session->exists = 0;
2306         session->recent = 0;
2307         session->expunge = 0;
2308         ok = imap_cmd_examine(session, "INBOX",
2309                               &exists, &recent, &unseen, &uid_validity, FALSE);
2310         if (ok != IMAP_SUCCESS) {
2311                 g_free(real_oldpath);
2312                 unlock_session(session);
2313                 return -1;
2314         }
2315
2316         separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
2317         if (strchr(item->path, G_DIR_SEPARATOR)) {
2318                 dirpath = g_path_get_dirname(item->path);
2319                 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2320                 g_free(dirpath);
2321         } else
2322                 newpath = g_strdup(name);
2323
2324         real_newpath = imap_utf8_to_modified_utf7(newpath);
2325         imap_path_separator_subst(real_newpath, separator);
2326
2327         ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2328         if (ok != IMAP_SUCCESS) {
2329                 log_warning(LOG_PROTOCOL, _("can't rename mailbox: %s to %s\n"),
2330                             real_oldpath, real_newpath);
2331                 g_free(real_oldpath);
2332                 g_free(newpath);
2333                 g_free(real_newpath);
2334                 unlock_session(session);
2335                 return -1;
2336         }
2337         g_free(item->name);
2338         item->name = g_strdup(name);
2339
2340         old_cache_dir = folder_item_get_path(item);
2341
2342         paths[0] = g_strdup(item->path);
2343         paths[1] = newpath;
2344         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2345                         imap_rename_folder_func, paths);
2346
2347         if (is_dir_exist(old_cache_dir)) {
2348                 new_cache_dir = folder_item_get_path(item);
2349                 if (rename(old_cache_dir, new_cache_dir) < 0) {
2350                         FILE_OP_ERROR(old_cache_dir, "rename");
2351                 }
2352                 g_free(new_cache_dir);
2353         }
2354
2355         g_free(old_cache_dir);
2356         g_free(paths[0]);
2357         g_free(newpath);
2358         g_free(real_oldpath);
2359         g_free(real_newpath);
2360         unlock_session(session);
2361         return 0;
2362 }
2363
2364 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
2365 {
2366         gchar *path;
2367         gint r = -1;
2368         IMAPSession *session;
2369         debug_print("getting session...\n");
2370
2371         session = imap_session_get(folder);
2372         if (!session) {
2373                 return -1;
2374         }
2375         if (item && item->path) {
2376                 path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2377                 if (!path)
2378                         return -1;
2379                 if (!strcmp(path, "INBOX") && sub == FALSE)
2380                         return -1;
2381                 debug_print("%ssubscribing %s\n", sub?"":"un", path);
2382                 r = imap_threaded_subscribe(folder, path, sub);
2383                 g_free(path);
2384         } else if (rpath) {
2385                 r = imap_threaded_subscribe(folder, rpath, sub);
2386         } else
2387                 return -1;
2388         return r;
2389 }
2390
2391 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2392 {
2393         gint ok;
2394         IMAPSession *session;
2395         gchar *path;
2396         gchar *cache_dir;
2397         gboolean selected_folder;
2398
2399         g_return_val_if_fail(folder != NULL, -1);
2400         g_return_val_if_fail(item != NULL, -1);
2401         g_return_val_if_fail(item->path != NULL, -1);
2402
2403         debug_print("getting session...\n");
2404         session = imap_session_get(folder);
2405         if (!session) {
2406                 return -1;
2407         }
2408         path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
2409
2410         imap_threaded_subscribe(folder, path, FALSE);
2411
2412         selected_folder = (session->mbox != NULL) &&
2413                           (!strcmp(session->mbox, item->path));
2414         if (selected_folder) {
2415                 ok = imap_cmd_close(session);
2416                 if (ok != MAILIMAP_NO_ERROR) {
2417                         debug_print("close err %d\n", ok);
2418                         return IMAP_ERROR;
2419                 }
2420         }
2421         ok = imap_cmd_delete(session, path);
2422         if (ok != IMAP_SUCCESS) {
2423                 gchar *tmp = g_strdup_printf("%s%c", path, 
2424                                 imap_get_path_separator(session, IMAP_FOLDER(folder), path));
2425                 g_free(path);
2426                 path = tmp;
2427                 ok = imap_cmd_delete(session, path);
2428         }
2429
2430         if (ok != IMAP_SUCCESS) {
2431                 log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
2432                 g_free(path);
2433                 unlock_session(session);
2434                 return -1;
2435         }
2436
2437         g_free(path);
2438         cache_dir = folder_item_get_path(item);
2439         if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2440                 g_warning("can't remove directory '%s'\n", cache_dir);
2441         g_free(cache_dir);
2442         folder_item_remove(item);
2443         unlock_session(session);
2444         return 0;
2445 }
2446
2447 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2448 {
2449         GNode *node, *next;
2450
2451         g_return_val_if_fail(item != NULL, -1);
2452         g_return_val_if_fail(item->folder != NULL, -1);
2453         g_return_val_if_fail(item->node != NULL, -1);
2454
2455         node = item->node->children;
2456         while (node != NULL) {
2457                 next = node->next;
2458                 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2459                         return -1;
2460                 node = next;
2461         }
2462         debug_print("IMAP removing %s\n", item->path);
2463
2464         if (imap_remove_all_msg(folder, item) < 0)
2465                 return -1;
2466         return imap_remove_folder_real(folder, item);
2467 }
2468
2469 typedef struct _uncached_data {
2470         IMAPSession *session;
2471         FolderItem *item;
2472         MsgNumberList *numlist;
2473         guint cur;
2474         guint total;
2475         gboolean done;
2476 } uncached_data;
2477
2478 static void *imap_get_uncached_messages_thread(void *data)
2479 {
2480         uncached_data *stuff = (uncached_data *)data;
2481         IMAPSession *session = stuff->session;
2482         FolderItem *item = stuff->item;
2483         MsgNumberList *numlist = stuff->numlist;
2484         
2485         GSList *newlist = NULL;
2486         GSList *llast = NULL;
2487         GSList *seq_list, *cur;
2488
2489         debug_print("uncached_messages\n");
2490         
2491         if (session == NULL || item == NULL || item->folder == NULL
2492             || FOLDER_CLASS(item->folder) != &imap_class) {
2493                 stuff->done = TRUE;
2494                 return NULL;
2495         }
2496         
2497         seq_list = imap_get_lep_set_from_numlist(numlist);
2498         debug_print("get msgs info\n");
2499         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2500                 struct mailimap_set * imapset;
2501                 unsigned int i;
2502                 int r;
2503                 carray * env_list;
2504                 int count;
2505                 
2506                 if (session->cancelled)
2507                         break;
2508                 
2509                 imapset = cur->data;
2510                 
2511                 r = imap_threaded_fetch_env(session->folder,
2512                                             imapset, &env_list);
2513                 if (r != MAILIMAP_NO_ERROR)
2514                         continue;
2515                 
2516                 session_set_access_time(SESSION(session));
2517
2518                 count = 0;
2519                 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2520                         struct imap_fetch_env_info * info;
2521                         MsgInfo * msginfo;
2522                         
2523                         info = carray_get(env_list, i);
2524                         msginfo = imap_envelope_from_lep(info, item);
2525                         if (msginfo == NULL)
2526                                 continue;
2527                         msginfo->folder = item;
2528                         if (!newlist)
2529                                 llast = newlist = g_slist_append(newlist, msginfo);
2530                         else {
2531                                 llast = g_slist_append(llast, msginfo);
2532                                 llast = llast->next;
2533                         }
2534                         count ++;
2535                 }
2536                 
2537                 imap_fetch_env_free(env_list);
2538         }
2539         
2540         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2541                 struct mailimap_set * imapset;
2542                 
2543                 imapset = cur->data;
2544                 mailimap_set_free(imapset);
2545         }
2546         
2547         session_set_access_time(SESSION(session));
2548         stuff->done = TRUE;
2549         return newlist;
2550 }
2551
2552 #define MAX_MSG_NUM 50
2553
2554 static GSList *imap_get_uncached_messages(IMAPSession *session,
2555                                         FolderItem *item,
2556                                         MsgNumberList *numlist)
2557 {
2558         GSList *result = NULL;
2559         GSList * cur;
2560         uncached_data *data = g_new0(uncached_data, 1);
2561         int finished;
2562         
2563         finished = 0;
2564         cur = numlist;
2565         data->total = g_slist_length(numlist);
2566         debug_print("messages list : %i\n", data->total);
2567
2568         while (cur != NULL) {
2569                 GSList * partial_result;
2570                 int count;
2571                 GSList * newlist;
2572                 GSList * llast;
2573                 
2574                 llast = NULL;
2575                 count = 0;
2576                 newlist = NULL;
2577                 while (count < MAX_MSG_NUM) {
2578                         void * p;
2579                         
2580                         p = cur->data;
2581                         
2582                         if (newlist == NULL)
2583                                 llast = newlist = g_slist_append(newlist, p);
2584                         else {
2585                                 llast = g_slist_append(llast, p);
2586                                 llast = llast->next;
2587                         }
2588                         count ++;
2589                         
2590                         cur = cur->next;
2591                         if (cur == NULL)
2592                                 break;
2593                 }
2594                 
2595                 data->done = FALSE;
2596                 data->session = session;
2597                 data->item = item;
2598                 data->numlist = newlist;
2599                 data->cur += count;
2600                 
2601                 if (prefs_common.work_offline && 
2602                     !inc_offline_should_override(FALSE,
2603                         _("Claws Mail needs network access in order "
2604                           "to access the IMAP server."))) {
2605                         g_free(data);
2606                         return NULL;
2607                 }
2608                 
2609                 partial_result =
2610                         (GSList *)imap_get_uncached_messages_thread(data);
2611                 
2612                 statusbar_progress_all(data->cur,data->total, 1);
2613                 
2614                 g_slist_free(newlist);
2615                 
2616                 result = g_slist_concat(result, partial_result);
2617         }
2618         g_free(data);
2619         
2620         statusbar_progress_all(0,0,0);
2621         statusbar_pop_all();
2622         
2623         return result;
2624 }
2625
2626 static void imap_delete_all_cached_messages(FolderItem *item)
2627 {
2628         gchar *dir;
2629
2630         g_return_if_fail(item != NULL);
2631         g_return_if_fail(item->folder != NULL);
2632         g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
2633
2634         debug_print("Deleting all cached messages...\n");
2635
2636         dir = folder_item_get_path(item);
2637         if (is_dir_exist(dir))
2638                 remove_all_numbered_files(dir);
2639         g_free(dir);
2640
2641         debug_print("done.\n");
2642 }
2643
2644 gchar imap_get_path_separator_for_item(FolderItem *item)
2645 {
2646         Folder *folder = NULL;
2647         IMAPFolder *imap_folder = NULL;
2648         IMAPSession *session = NULL;
2649         gchar result = '/';
2650         
2651         if (!item)
2652                 return '/';
2653         folder = item->folder;
2654         
2655         if (!folder)
2656                 return '/';
2657         
2658         imap_folder = IMAP_FOLDER(folder);
2659         
2660         if (!imap_folder)
2661                 return '/';
2662         
2663         debug_print("getting session...");
2664         session = imap_session_get(FOLDER(folder));
2665         result = imap_get_path_separator(session, imap_folder, item->path);
2666         unlock_session(session);
2667         return result;
2668 }
2669
2670 static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
2671 {
2672         clist * lep_list;
2673         int r;
2674         gchar separator = '\0';
2675         
2676         g_return_val_if_fail(session != NULL, '/');
2677         r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
2678         
2679         if (r != MAILIMAP_NO_ERROR) {
2680                 log_warning(LOG_PROTOCOL, _("LIST failed\n"));
2681                 return '\0';
2682         }
2683
2684         if (clist_count(lep_list) > 0) {
2685                 clistiter * iter = clist_begin(lep_list); 
2686                 struct mailimap_mailbox_list * mb;
2687                 mb = clist_content(iter);
2688
2689                 separator = mb->mb_delimiter;
2690                 debug_print("got separator: %c\n", folder->last_seen_separator);
2691         }
2692         mailimap_list_result_free(lep_list);
2693         return separator;
2694 }
2695
2696 static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2697 {
2698         gchar separator = '/';
2699
2700         if (folder->last_seen_separator == 0) {
2701                 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
2702         }
2703
2704         if (folder->last_seen_separator == 0) {
2705                 folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
2706         }
2707
2708         if (folder->last_seen_separator != 0) {
2709                 debug_print("using separator: %c\n", folder->last_seen_separator);
2710                 return folder->last_seen_separator;
2711         }
2712
2713         return separator;
2714 }
2715
2716 static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
2717 {
2718         gchar *real_path;
2719         gchar separator;
2720
2721         g_return_val_if_fail(folder != NULL, NULL);
2722         g_return_val_if_fail(path != NULL, NULL);
2723
2724         real_path = imap_utf8_to_modified_utf7(path);
2725         separator = imap_get_path_separator(session, folder, path);
2726         imap_path_separator_subst(real_path, separator);
2727
2728         return real_path;
2729 }
2730
2731 static gint imap_set_message_flags(IMAPSession *session,
2732                                    MsgNumberList *numlist,
2733                                    IMAPFlags flags,
2734                                    gboolean is_set)
2735 {
2736         gint ok = 0;
2737         GSList *seq_list;
2738         GSList * cur;
2739
2740         seq_list = imap_get_lep_set_from_numlist(numlist);
2741         
2742         for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
2743                 struct mailimap_set * imapset;
2744                 
2745                 imapset = cur->data;
2746                 
2747                 ok = imap_cmd_store(session, imapset,
2748                                     flags, is_set);
2749         }
2750         
2751         imap_lep_set_free(seq_list);
2752         
2753         return IMAP_SUCCESS;
2754 }
2755
2756 typedef struct _select_data {
2757         IMAPSession *session;
2758         gchar *real_path;
2759         gint *exists;
2760         gint *recent;
2761         gint *unseen;
2762         guint32 *uid_validity;
2763         gboolean done;
2764 } select_data;
2765
2766 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
2767                         const gchar *path,
2768                         gint *exists, gint *recent, gint *unseen,
2769                         guint32 *uid_validity, gboolean block)
2770 {
2771         gchar *real_path;
2772         gint ok;
2773         gint exists_, recent_, unseen_;
2774         guint32 uid_validity_;
2775         
2776         if (!exists && !recent && !unseen && !uid_validity) {
2777                 if (session->mbox && strcmp(session->mbox, path) == 0)
2778                         return IMAP_SUCCESS;
2779         }
2780         if (!exists)
2781                 exists = &exists_;
2782         if (!recent)
2783                 recent = &recent_;
2784         if (!unseen)
2785                 unseen = &unseen_;
2786         if (!uid_validity)
2787                 uid_validity = &uid_validity_;
2788
2789         g_free(session->mbox);
2790         session->mbox = NULL;
2791         session->exists = 0;
2792         session->recent = 0;
2793         session->expunge = 0;
2794
2795         real_path = imap_get_real_path(session, folder, path);
2796
2797         ok = imap_cmd_select(session, real_path,
2798                              exists, recent, unseen, uid_validity, block);
2799         if (ok != IMAP_SUCCESS)
2800                 log_warning(LOG_PROTOCOL, _("can't select folder: %s\n"), real_path);
2801         else {
2802                 session->mbox = g_strdup(path);
2803                 session->folder_content_changed = FALSE;
2804                 session->exists = *exists;
2805                 session->recent = *recent;
2806                 session->expunge = 0;
2807                 session->unseen = *unseen;
2808                 session->uid_validity = *uid_validity;
2809                 debug_print("select: exists %d recent %d expunge %d uid_validity %d\n", 
2810                         session->exists, session->recent, session->expunge,
2811                         session->uid_validity);
2812         }
2813         g_free(real_path);
2814
2815         return ok;
2816 }
2817
2818 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
2819                         const gchar *path, IMAPFolderItem *item,
2820                         gint *messages,
2821                         guint32 *uid_next, guint32 *uid_validity,
2822                         gint *unseen, gboolean block)
2823 {
2824         int r;
2825         clistiter * iter;
2826         struct mailimap_mailbox_data_status * data_status;
2827         int got_values;
2828         gchar *real_path;
2829         guint mask = 0;
2830         
2831         real_path = imap_get_real_path(session, folder, path);
2832
2833         if (messages) {
2834                 mask |= 1 << 0;
2835                 *messages = 0;
2836         }
2837         if (uid_next) {
2838                 mask |= 1 << 2;
2839                 *uid_next = 0;
2840         }
2841         if (uid_validity) {
2842                 mask |= 1 << 3;
2843                 *uid_validity = 0;
2844         }
2845         if (unseen) {
2846                 mask |= 1 << 4;
2847                 *unseen = 0;
2848         }
2849         
2850         if (session->mbox != NULL &&
2851             !strcmp(session->mbox, item->item.path)) {
2852                 r = imap_cmd_close(session);
2853                 if (r != MAILIMAP_NO_ERROR) {
2854                         debug_print("close err %d\n", r);
2855                         return IMAP_ERROR;
2856                 }
2857         }
2858         
2859         r = imap_threaded_status(FOLDER(folder), real_path, 
2860                 &data_status, mask);
2861
2862         g_free(real_path);
2863         if (r != MAILIMAP_NO_ERROR) {
2864                 debug_print("status err %d\n", r);
2865                 return IMAP_ERROR;
2866         }
2867         
2868         if (data_status->st_info_list == NULL) {
2869                 mailimap_mailbox_data_status_free(data_status);
2870                 debug_print("status->st_info_list == NULL\n");
2871                 return IMAP_ERROR;
2872         }
2873         
2874         got_values = 0;
2875         for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
2876             iter = clist_next(iter)) {
2877                 struct mailimap_status_info * info;             
2878                 
2879                 info = clist_content(iter);
2880                 switch (info->st_att) {
2881                 case MAILIMAP_STATUS_ATT_MESSAGES:
2882                         if (messages) {
2883                                 * messages = info->st_value;
2884                                 got_values |= 1 << 0;
2885                         }
2886                         break;
2887                         
2888                 case MAILIMAP_STATUS_ATT_UIDNEXT:
2889                         if (uid_next) {
2890                                 * uid_next = info->st_value;
2891                                 got_values |= 1 << 2;
2892                         }
2893                         break;
2894                         
2895                 case MAILIMAP_STATUS_ATT_UIDVALIDITY:
2896                         if (uid_validity) {
2897                                 * uid_validity = info->st_value;
2898                                 got_values |= 1 << 3;
2899                         }
2900                         break;
2901                         
2902                 case MAILIMAP_STATUS_ATT_UNSEEN:
2903                         if (unseen) {
2904                                 * unseen = info->st_value;
2905                                 got_values |= 1 << 4;
2906                         }
2907                         break;
2908                 }
2909         }
2910         mailimap_mailbox_data_status_free(data_status);
2911         
2912         if (got_values != mask) {
2913                 g_warning("status: incomplete values received (%d)\n", got_values);
2914         }
2915         return IMAP_SUCCESS;
2916 }
2917
2918 static void imap_free_capabilities(IMAPSession *session)
2919 {
2920         slist_free_strings(session->capability);
2921         g_slist_free(session->capability);
2922         session->capability = NULL;
2923 }
2924
2925 /* low-level IMAP4rev1 commands */
2926
2927 static gint imap_cmd_login(IMAPSession *session,
2928                            const gchar *user, const gchar *pass,
2929                            const gchar *type)
2930 {
2931         int r;
2932         gint ok;
2933
2934         if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
2935                 gint ok = IMAP_ERROR;
2936                 if (imap_has_capability(session, "STARTTLS")) {
2937 #if USE_OPENSSL
2938                         log_warning(LOG_PROTOCOL, _("Server requires TLS to log in.\n"));
2939                         ok = imap_cmd_starttls(session);
2940                         if (ok != IMAP_SUCCESS) {
2941                                 log_warning(LOG_PROTOCOL, _("Can't start TLS session.\n"));
2942                                 return IMAP_ERROR;
2943                         } else {
2944                                 /* refresh capas */
2945                                 imap_free_capabilities(session);
2946                                 if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR) {
2947                                         log_warning(LOG_PROTOCOL, _("Can't refresh capabilities.\n"));
2948                                         return IMAP_ERROR;
2949                                 }
2950                         }
2951 #else           
2952                         log_error(LOG_PROTOCOL, _("Connection to %s failed: "
2953                                         "server requires TLS, but Claws Mail "
2954                                         "has been compiled without OpenSSL "
2955                                         "support.\n"),
2956                                         SESSION(session)->server);
2957                         return IMAP_ERROR;
2958 #endif
2959                 } else {
2960                         log_error(LOG_PROTOCOL, _("Server logins are disabled.\n"));
2961                         return IMAP_ERROR;
2962                 }
2963         }
2964
2965         log_print(LOG_PROTOCOL, "IMAP4> Logging %s to %s using %s\n", 
2966                         user,
2967                         SESSION(session)->server,
2968                         type);
2969         r = imap_threaded_login(session->folder, user, pass, type);
2970         if (r != MAILIMAP_NO_ERROR) {
2971                 log_print(LOG_PROTOCOL, "IMAP4< Error logging in to %s\n",
2972                                 SESSION(session)->server);
2973                 ok = IMAP_ERROR;
2974         } else {
2975                 log_print(LOG_PROTOCOL, "IMAP4< Login to %s successful\n",
2976                                 SESSION(session)->server);
2977                 ok = IMAP_SUCCESS;
2978         }
2979         return ok;
2980 }
2981
2982 static gint imap_cmd_noop(IMAPSession *session)
2983 {
2984         int r;
2985         unsigned int exists, recent, expunge, unseen, uidnext, uidval;
2986         
2987         r = imap_threaded_noop(session->folder, &exists, &recent, &expunge, &unseen, &uidnext, &uidval);
2988         if (r != MAILIMAP_NO_ERROR) {
2989                 debug_print("noop err %d\n", r);
2990                 return IMAP_ERROR;
2991         }
2992
2993         session->folder_content_changed = FALSE;
2994
2995         if ((exists && exists != session->exists)
2996          || (recent && recent != session->recent)
2997          || (expunge && expunge != session->expunge)
2998          || (unseen && unseen != session->unseen)) {
2999                 session->folder_content_changed = TRUE;
3000         }
3001         if (uidnext != 0 && uidnext != session->uid_next) {
3002                 session->uid_next = uidnext;
3003                 session->folder_content_changed = TRUE;
3004         }
3005         if (uidval != 0 && uidval != session->uid_validity) {
3006                 session->uid_validity = uidval;
3007                 session->folder_content_changed = TRUE;
3008         }
3009
3010         session->exists = exists;
3011         session->recent = recent;
3012         session->expunge = expunge;
3013         session->unseen = unseen;
3014
3015         session_set_access_time(SESSION(session));
3016
3017         return IMAP_SUCCESS;
3018 }
3019
3020 #if USE_OPENSSL
3021 static gint imap_cmd_starttls(IMAPSession *session)
3022 {
3023         int r;
3024         
3025         r = imap_threaded_starttls(session->folder, 
3026                 SESSION(session)->server, SESSION(session)->port);
3027         if (r != MAILIMAP_NO_ERROR) {
3028                 debug_print("starttls err %d\n", r);
3029                 return IMAP_ERROR;
3030         }
3031         return IMAP_SUCCESS;
3032 }
3033 #endif
3034
3035 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3036                             gint *exists, gint *recent, gint *unseen,
3037                             guint32 *uid_validity, gboolean block)
3038 {
3039         int r;
3040
3041         r = imap_threaded_select(session->folder, folder,
3042                                  exists, recent, unseen, uid_validity);
3043         if (r != MAILIMAP_NO_ERROR) {
3044                 debug_print("select err %d\n", r);
3045                 return IMAP_ERROR;
3046         }
3047         return IMAP_SUCCESS;
3048 }
3049
3050 static gint imap_cmd_close(IMAPSession *session)
3051 {
3052         int r;
3053
3054         r = imap_threaded_close(session->folder);
3055         if (r != MAILIMAP_NO_ERROR) {
3056                 debug_print("close err %d\n", r);
3057                 return IMAP_ERROR;
3058         }
3059         g_free(session->mbox);
3060         session->mbox = NULL;
3061         session->exists = 0;
3062         session->recent = 0;
3063         session->expunge = 0;
3064         return IMAP_SUCCESS;
3065 }
3066
3067 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3068                              gint *exists, gint *recent, gint *unseen,
3069                              guint32 *uid_validity, gboolean block)
3070 {
3071         int r;
3072
3073         r = imap_threaded_examine(session->folder, folder,
3074                                   exists, recent, unseen, uid_validity);
3075         if (r != MAILIMAP_NO_ERROR) {
3076                 debug_print("examine err %d\n", r);
3077                 
3078                 return IMAP_ERROR;
3079         }
3080         return IMAP_SUCCESS;
3081 }
3082
3083 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3084 {
3085         int r;
3086
3087         r = imap_threaded_create(session->folder, folder);
3088         if (r != MAILIMAP_NO_ERROR) {
3089                 
3090                 return IMAP_ERROR;
3091         }
3092
3093         return IMAP_SUCCESS;
3094 }
3095
3096 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3097                             const gchar *new_folder)
3098 {
3099         int r;
3100
3101         r = imap_threaded_rename(session->folder, old_folder,
3102                                  new_folder);
3103         if (r != MAILIMAP_NO_ERROR) {
3104                 
3105                 return IMAP_ERROR;
3106         }
3107
3108         return IMAP_SUCCESS;
3109 }
3110
3111 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3112 {
3113         int r;
3114         
3115
3116         r = imap_threaded_delete(session->folder, folder);
3117         if (r != MAILIMAP_NO_ERROR) {
3118                 
3119                 return IMAP_ERROR;
3120         }
3121
3122         return IMAP_SUCCESS;
3123 }
3124
3125 typedef struct _fetch_data {
3126         IMAPSession *session;
3127         guint32 uid;
3128         const gchar *filename;
3129         gboolean headers;
3130         gboolean body;
3131         gboolean done;
3132 } fetch_data;
3133
3134 static void *imap_cmd_fetch_thread(void *data)
3135 {
3136         fetch_data *stuff = (fetch_data *)data;
3137         IMAPSession *session = stuff->session;
3138         guint32 uid = stuff->uid;
3139         const gchar *filename = stuff->filename;
3140         int r;
3141         
3142         if (stuff->body) {
3143                 r = imap_threaded_fetch_content(session->folder,
3144                                                uid, 1, filename);
3145         }
3146         else {
3147                 r = imap_threaded_fetch_content(session->folder,
3148                                                 uid, 0, filename);
3149         }
3150         if (r != MAILIMAP_NO_ERROR) {
3151                 debug_print("fetch err %d\n", r);
3152                 return GINT_TO_POINTER(IMAP_ERROR);
3153         }
3154         return GINT_TO_POINTER(IMAP_SUCCESS);
3155 }
3156
3157 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3158                                 const gchar *filename, gboolean headers,
3159                                 gboolean body)
3160 {
3161         fetch_data *data = g_new0(fetch_data, 1);
3162         int result = 0;
3163         data->done = FALSE;
3164         data->session = session;
3165         data->uid = uid;
3166         data->filename = filename;
3167         data->headers = headers;
3168         data->body = body;
3169
3170         if (prefs_common.work_offline && 
3171             !inc_offline_should_override(FALSE,
3172                 _("Claws Mail needs network access in order "
3173                   "to access the IMAP server."))) {
3174                 g_free(data);
3175                 return -1;
3176         }
3177         statusbar_print_all(_("Fetching message..."));
3178         result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3179         statusbar_pop_all();
3180         g_free(data);
3181         return result;
3182 }
3183
3184
3185 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
3186                             const gchar *file, IMAPFlags flags, 
3187                             guint32 *new_uid)
3188 {
3189         struct mailimap_flag_list * flag_list;
3190         int r;
3191         
3192         g_return_val_if_fail(file != NULL, IMAP_ERROR);
3193
3194         flag_list = imap_flag_to_lep(flags);
3195         r = imap_threaded_append(session->folder, destfolder,
3196                          file, flag_list, (int *)new_uid);
3197         mailimap_flag_list_free(flag_list);
3198
3199         if (r != MAILIMAP_NO_ERROR) {
3200                 debug_print("append err %d\n", r);
3201                 return IMAP_ERROR;
3202         }
3203         return IMAP_SUCCESS;
3204 }
3205
3206 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
3207                           const gchar *destfolder, GRelation *uid_mapping,
3208                           struct mailimap_set **source, struct mailimap_set **dest)
3209 {
3210         int r;
3211         
3212         g_return_val_if_fail(session != NULL, IMAP_ERROR);
3213         g_return_val_if_fail(set != NULL, IMAP_ERROR);
3214         g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
3215
3216         r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
3217         if (r != MAILIMAP_NO_ERROR) {
3218                 
3219                 return IMAP_ERROR;
3220         }
3221
3222         return IMAP_SUCCESS;
3223 }
3224
3225 static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
3226                            IMAPFlags flags, int do_add)
3227 {
3228         int r;
3229         struct mailimap_flag_list * flag_list;
3230         struct mailimap_store_att_flags * store_att_flags;
3231         
3232         flag_list = imap_flag_to_lep(flags);
3233         
3234         if (do_add)
3235                 store_att_flags =
3236                         mailimap_store_att_flags_new_add_flags_silent(flag_list);
3237         else
3238                 store_att_flags =
3239                         mailimap_store_att_flags_new_remove_flags_silent(flag_list);
3240         
3241         r = imap_threaded_store(session->folder, set, store_att_flags);
3242         mailimap_store_att_flags_free(store_att_flags);
3243         if (r != MAILIMAP_NO_ERROR) {
3244                 
3245                 return IMAP_ERROR;
3246         }
3247         
3248         return IMAP_SUCCESS;
3249 }
3250
3251 static gint imap_cmd_expunge(IMAPSession *session)
3252 {
3253         int r;
3254         
3255         if (prefs_common.work_offline && 
3256             !inc_offline_should_override(FALSE,
3257                 _("Claws Mail needs network access in order "
3258                   "to access the IMAP server."))) {
3259                 return -1;
3260         }
3261
3262         r = imap_threaded_expunge(session->folder);
3263         if (r != MAILIMAP_NO_ERROR) {
3264                 
3265                 return IMAP_ERROR;
3266         }
3267
3268         return IMAP_SUCCESS;
3269 }
3270
3271 static void imap_path_separator_subst(gchar *str, gchar separator)
3272 {
3273         gchar *p;
3274         gboolean in_escape = FALSE;
3275
3276         if (!separator || separator == '/') return;
3277
3278         for (p = str; *p != '\0'; p++) {
3279                 if (*p == '/' && !in_escape)
3280                         *p = separator;
3281                 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
3282                         in_escape = TRUE;
3283                 else if (*p == '-' && in_escape)
3284                         in_escape = FALSE;
3285         }
3286 }
3287
3288 static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
3289 {
3290         static iconv_t cd = (iconv_t)-1;
3291         static gboolean iconv_ok = TRUE;
3292         GString *norm_utf7;
3293         gchar *norm_utf7_p;
3294         size_t norm_utf7_len;
3295         const gchar *p;
3296         gchar *to_str, *to_p;
3297         size_t to_len;
3298         gboolean in_escape = FALSE;
3299
3300         if (!iconv_ok) return g_strdup(mutf7_str);
3301
3302         if (cd == (iconv_t)-1) {
3303                 cd = iconv_open(CS_INTERNAL, CS_UTF_7);
3304                 if (cd == (iconv_t)-1) {
3305                         g_warning("iconv cannot convert UTF-7 to %s\n",
3306                                   CS_INTERNAL);
3307                         iconv_ok = FALSE;
3308                         return g_strdup(mutf7_str);
3309                 }
3310         }
3311
3312         /* modified UTF-7 to normal UTF-7 conversion */
3313         norm_utf7 = g_string_new(NULL);
3314
3315         for (p = mutf7_str; *p != '\0'; p++) {
3316                 /* replace: '&'  -> '+',
3317                             "&-" -> '&',
3318                             escaped ','  -> '/' */
3319                 if (!in_escape && *p == '&') {
3320                         if (*(p + 1) != '-') {
3321                                 g_string_append_c(norm_utf7, '+');
3322                                 in_escape = TRUE;
3323                         } else {
3324                                 g_string_append_c(norm_utf7, '&');
3325                                 p++;
3326                         }
3327                 } else if (in_escape && *p == ',') {
3328                         g_string_append_c(norm_utf7, '/');
3329                 } else if (in_escape && *p == '-') {
3330                         g_string_append_c(norm_utf7, '-');
3331                         in_escape = FALSE;
3332                 } else {
3333                         g_string_append_c(norm_utf7, *p);
3334                 }
3335         }
3336
3337         norm_utf7_p = norm_utf7->str;
3338         norm_utf7_len = norm_utf7->len;
3339         to_len = strlen(mutf7_str) * 5;
3340         to_p = to_str = g_malloc(to_len + 1);
3341
3342         if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
3343                   &to_p, &to_len) == -1) {
3344                 g_warning(_("iconv cannot convert UTF-7 to %s\n"),
3345                           conv_get_locale_charset_str());
3346                 g_string_free(norm_utf7, TRUE);
3347                 g_free(to_str);
3348                 return g_strdup(mutf7_str);
3349         }
3350
3351         /* second iconv() call for flushing */
3352         iconv(cd, NULL, NULL, &to_p, &to_len);
3353         g_string_free(norm_utf7, TRUE);
3354         *to_p = '\0';
3355
3356         return to_str;
3357 }
3358
3359 static gchar *imap_utf8_to_modified_utf7(const gchar *from)
3360 {
3361         static iconv_t cd = (iconv_t)-1;
3362         static gboolean iconv_ok = TRUE;
3363         gchar *norm_utf7, *norm_utf7_p;
3364         size_t from_len, norm_utf7_len;
3365         GString *to_str;
3366         gchar *from_tmp, *to, *p;
3367         gboolean in_escape = FALSE;
3368
3369         if (!iconv_ok) return g_strdup(from);
3370
3371         if (cd == (iconv_t)-1) {
3372                 cd = iconv_open(CS_UTF_7, CS_INTERNAL);
3373                 if (cd == (iconv_t)-1) {
3374                         g_warning(_("iconv cannot convert %s to UTF-7\n"),
3375                                   CS_INTERNAL);
3376                         iconv_ok = FALSE;
3377                         return g_strdup(from);
3378                 }
3379         }
3380
3381         /* UTF-8 to normal UTF-7 conversion */
3382         Xstrdup_a(from_tmp, from, return g_strdup(from));
3383         from_len = strlen(from);
3384         norm_utf7_len = from_len * 5;
3385         Xalloca(norm_utf7, norm_utf7_len + 1, return g_strdup(from));
3386         norm_utf7_p = norm_utf7;
3387
3388 #define IS_PRINT(ch) (isprint(ch) && IS_ASCII(ch))
3389
3390         while (from_len > 0) {
3391                 if (*from_tmp == '+') {
3392                         *norm_utf7_p++ = '+';
3393                         *norm_utf7_p++ = '-';
3394                         norm_utf7_len -= 2;
3395                         from_tmp++;
3396                         from_len--;
3397                 } else if (IS_PRINT(*(guchar *)from_tmp)) {
3398                         /* printable ascii char */
3399                         *norm_utf7_p = *from_tmp;
3400                         norm_utf7_p++;
3401                         norm_utf7_len--;
3402                         from_tmp++;
3403                         from_len--;
3404                 } else {
3405                         size_t conv_len = 0;
3406
3407                         /* unprintable char: convert to UTF-7 */
3408                         p = from_tmp;
3409                         while (!IS_PRINT(*(guchar *)p) && conv_len < from_len) {
3410                                 conv_len += g_utf8_skip[*(guchar *)p];
3411                                 p += g_utf8_skip[*(guchar *)p];
3412                         }
3413
3414                         from_len -= conv_len;
3415                         if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
3416                                   &conv_len,
3417                                   &norm_utf7_p, &norm_utf7_len) == -1) {
3418                                 g_warning(_("iconv cannot convert UTF-8 to UTF-7\n"));
3419                                 return g_strdup(from);
3420                         }
3421
3422                         /* second iconv() call for flushing */
3423                         iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
3424                 }
3425         }
3426
3427 #undef IS_PRINT
3428
3429         *norm_utf7_p = '\0';
3430         to_str = g_string_new(NULL);
3431         for (p = norm_utf7; p < norm_utf7_p; p++) {
3432                 /* replace: '&' -> "&-",
3433                             '+' -> '&',
3434                             "+-" -> '+',
3435                             BASE64 '/' -> ',' */
3436                 if (!in_escape && *p == '&') {
3437                         g_string_append(to_str, "&-");
3438                 } else if (!in_escape && *p == '+') {
3439                         if (*(p + 1) == '-') {
3440                                 g_string_append_c(to_str, '+');
3441                                 p++;
3442                         } else {
3443                                 g_string_append_c(to_str, '&');
3444                                 in_escape = TRUE;
3445                         }
3446                 } else if (in_escape && *p == '/') {
3447                         g_string_append_c(to_str, ',');
3448                 } else if (in_escape && *p == '-') {
3449                         g_string_append_c(to_str, '-');
3450                         in_escape = FALSE;
3451                 } else {
3452                         g_string_append_c(to_str, *p);
3453                 }
3454         }
3455
3456         if (in_escape) {
3457                 in_escape = FALSE;
3458                 g_string_append_c(to_str, '-');
3459         }
3460
3461         to = to_str->str;
3462         g_string_free(to_str, FALSE);
3463
3464         return to;
3465 }
3466
3467 static gboolean imap_rename_folder_func(GNode *node, gpointer data)
3468 {
3469         FolderItem *item = node->data;
3470         gchar **paths = data;
3471         const gchar *oldpath = paths[0];
3472         const gchar *newpath = paths[1];
3473         gchar *real_oldpath, *real_newpath;
3474         gchar *base;
3475         gchar *new_itempath;
3476         gint oldpathlen;
3477         IMAPSession *session = imap_session_get(item->folder);
3478
3479         oldpathlen = strlen(oldpath);
3480         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
3481                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
3482                 return TRUE;
3483         }
3484
3485         base = item->path + oldpathlen;
3486         while (*base == G_DIR_SEPARATOR) base++;
3487         if (*base == '\0')
3488                 new_itempath = g_strdup(newpath);
3489         else
3490                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
3491                                            NULL);
3492
3493         real_oldpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3494         g_free(item->path);
3495         item->path = new_itempath;
3496         
3497         real_newpath = imap_get_real_path(session, IMAP_FOLDER(item->folder), item->path);
3498         
3499         imap_threaded_subscribe(item->folder, real_oldpath, FALSE);
3500         imap_threaded_subscribe(item->folder, real_newpath, TRUE);
3501
3502         g_free(real_oldpath);
3503         g_free(real_newpath);
3504         return FALSE;
3505 }
3506
3507 typedef struct _get_list_uid_data {
3508         Folder *folder;
3509         IMAPSession *session;
3510         IMAPFolderItem *item;
3511         GSList **msgnum_list;
3512         gboolean done;
3513 } get_list_uid_data;
3514
3515 static void *get_list_of_uids_thread(void *data)
3516 {
3517         get_list_uid_data *stuff = (get_list_uid_data *)data;
3518         Folder *folder = stuff->folder;
3519         IMAPFolderItem *item = stuff->item;
3520         GSList **msgnum_list = stuff->msgnum_list;
3521         gint ok, nummsgs = 0, lastuid_old;
3522         IMAPSession *session;
3523         GSList *uidlist, *elem;
3524         int r = -1;
3525         clist * lep_uidlist;
3526
3527         session = stuff->session;
3528         if (session == NULL) {
3529                 stuff->done = TRUE;
3530                 return GINT_TO_POINTER(-1);
3531         }
3532         /* no session locking here, it's already locked by caller */
3533         ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
3534                          NULL, NULL, NULL, NULL, TRUE);
3535         if (ok != IMAP_SUCCESS) {
3536                 stuff->done = TRUE;
3537                 return GINT_TO_POINTER(-1);
3538         }
3539
3540         g_slist_free(item->uid_list);
3541         item->uid_list = NULL;
3542
3543         uidlist = NULL;
3544         
3545         if (folder->account && folder->account->low_bandwidth) {
3546                 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
3547                                  &lep_uidlist);
3548         }
3549         
3550         if (r == MAILIMAP_NO_ERROR) {
3551                 GSList * fetchuid_list =
3552                         imap_uid_list_from_lep(lep_uidlist);
3553                 mailimap_search_result_free(lep_uidlist);
3554                 
3555                 uidlist = g_slist_concat(fetchuid_list, uidlist);
3556         } else {
3557                 carray * lep_uidtab;
3558                 r = imap_threaded_fetch_uid(folder, 1,
3559                                     &lep_uidtab);
3560                 if (r == MAILIMAP_NO_ERROR) {
3561                         GSList * fetchuid_list =
3562                                 imap_uid_list_from_lep_tab(lep_uidtab);
3563                         imap_fetch_uid_list_free(lep_uidtab);
3564                         uidlist = g_slist_concat(fetchuid_list, uidlist);
3565                 }
3566         }
3567         
3568         if (r != MAILIMAP_NO_ERROR) {
3569                 stuff->done = TRUE;
3570                 return GINT_TO_POINTER(-1);
3571         }
3572
3573         lastuid_old = item->lastuid;
3574
3575         for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
3576                 guint msgnum;
3577
3578                 msgnum = GPOINTER_TO_INT(elem->data);
3579
3580                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
3581                 item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
3582                 nummsgs++;
3583         }
3584         g_slist_free(uidlist);
3585         stuff->done = TRUE;
3586         return GINT_TO_POINTER(nummsgs);
3587 }
3588
3589 static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
3590 {
3591         gint result;
3592         get_list_uid_data *data = g_new0(get_list_uid_data, 1);
3593         data->done = FALSE;
3594         data->folder = folder;
3595         data->item = item;
3596         data->msgnum_list = msgnum_list;
3597         data->session = session;
3598         if (prefs_common.work_offline && 
3599             !inc_offline_should_override(FALSE,
3600                 _("Claws Mail needs network access in order "
3601                   "to access the IMAP server."))) {
3602                 g_free(data);
3603                 return -1;
3604         }
3605
3606         result = GPOINTER_TO_INT(get_list_of_uids_thread(data));
3607         g_free(data);
3608         return result;
3609
3610 }
3611
3612 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
3613 {
3614         IMAPFolderItem *item = (IMAPFolderItem *)_item;
3615         IMAPSession *session;
3616         gint nummsgs;
3617         GSList *uidlist = NULL;
3618         gchar *dir;
3619         gboolean selected_folder;
3620         gint known_list_len = 0;
3621         debug_print("get_num_list\n");
3622         
3623         g_return_val_if_fail(folder != NULL, -1);
3624         g_return_val_if_fail(item != NULL, -1);
3625         g_return_val_if_fail(item->item.path != NULL, -1);
3626         g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
3627         g_return_val_if_fail(folder->account != NULL, -1);
3628
3629         known_list_len = g_slist_length(item->uid_list);
3630         if (!item->should_update) {
3631                 debug_print("get_num_list: nothing to update\n");
3632                 *old_uids_valid = TRUE;
3633                 if (known_list_len == item->item.total_msgs
3634                  && known_list_len > 0) {
3635                         *msgnum_list = g_slist_copy(item->uid_list);
3636                         return known_list_len;
3637                 } else {
3638                         debug_print("don't know the list length...\n");
3639                 }
3640         }
3641         
3642         debug_print("getting session...\n");
3643         session = imap_session_get(folder);
3644         g_return_val_if_fail(session != NULL, -1);
3645
3646         if (FOLDER_ITEM(item)->path) 
3647                 statusbar_print_all(_("Scanning folder %s%c%s ..."),
3648                                       FOLDER_ITEM(item)->folder->name, 
3649                                       G_DIR_SEPARATOR,
3650                                       FOLDER_ITEM(item)->path);
3651         else
3652                 statusbar_print_all(_("Scanning folder %s ..."),
3653                                       FOLDER_ITEM(item)->folder->name);
3654
3655         selected_folder = (session->mbox != NULL) &&
3656                           (!strcmp(session->mbox, item->item.path));
3657         
3658         if (item->should_trash_cache) {
3659                 *old_uids_valid = FALSE;
3660                 debug_print("get_num_list: trashing num list\n");
3661                 debug_print("Freeing imap uid cache\n");
3662                 item->lastuid = 0;
3663                 g_slist_free(item->uid_list);
3664                 item->uid_list = NULL;
3665
3666                 imap_delete_all_cached_messages((FolderItem *)item);
3667         } else {
3668                 debug_print("get_num_list: updating num list\n");
3669                 *old_uids_valid = TRUE;
3670         }
3671
3672         nummsgs = get_list_of_uids(session, folder, item, &uidlist);
3673         debug_print("get_num_list: got %d msgs\n", nummsgs);
3674
3675         if (nummsgs < 0) {
3676                 statusbar_pop_all();
3677                 unlock_session(session);
3678                 return -1;
3679         }
3680
3681         *msgnum_list = uidlist;
3682
3683         dir = folder_item_get_path((FolderItem *)item);
3684         debug_print("removing old messages from %s\n", dir);
3685         remove_numbered_files_not_in_list(dir, *msgnum_list);
3686         g_free(dir);
3687         
3688         debug_print("get_num_list - ok - %i\n", nummsgs);
3689         statusbar_pop_all();
3690         unlock_session(session);
3691         item->should_trash_cache = FALSE;
3692         item->should_update = FALSE;
3693         return nummsgs;
3694 }
3695
3696 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
3697 {
3698         MsgInfo *msginfo;
3699         MsgFlags flags;
3700
3701         flags.perm_flags = MSG_NEW|MSG_UNREAD;
3702         flags.tmp_flags = 0;
3703
3704         g_return_val_if_fail(item != NULL, NULL);
3705         g_return_val_if_fail(file != NULL, NULL);
3706
3707         if (folder_has_parent_of_type(item, F_QUEUE)) {
3708                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
3709         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
3710                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
3711         }
3712
3713         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3714         if (!msginfo) return NULL;
3715         
3716         msginfo->plaintext_file = g_strdup(file);
3717         msginfo->folder = item;
3718
3719         return msginfo;
3720 }
3721
3722 GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
3723                           GSList *msgnum_list)
3724 {
3725         IMAPSession *session;
3726         MsgInfoList *ret = NULL;
3727         gint ok;
3728         
3729         debug_print("get_msginfos\n");
3730         
3731         g_return_val_if_fail(folder != NULL, NULL);
3732         g_return_val_if_fail(item != NULL, NULL);
3733         g_return_val_if_fail(msgnum_list != NULL, NULL);
3734
3735         debug_print("getting session...\n");
3736         session = imap_session_get(folder);
3737         g_return_val_if_fail(session != NULL, NULL);
3738
3739         debug_print("IMAP getting msginfos\n");
3740         ok = imap_select(session, IMAP_FOLDER(folder), item->path,
3741                          NULL, NULL, NULL, NULL, FALSE);
3742         if (ok != IMAP_SUCCESS) {
3743                 unlock_session(session);
3744                 return NULL;
3745         }
3746         if (!(folder_has_parent_of_type(item, F_DRAFT) || 
3747               folder_has_parent_of_type(item, F_QUEUE))) {
3748                 ret = g_slist_concat(ret,
3749                         imap_get_uncached_messages(session, item,
3750                                                    msgnum_list));
3751         } else {
3752                 MsgNumberList *sorted_list, *elem, *llast = NULL;
3753                 gint startnum, lastnum;
3754
3755                 sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
3756
3757                 startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
3758
3759                 llast = g_slist_last(ret);
3760                 for (elem = sorted_list;; elem = g_slist_next(elem)) {
3761                         guint num = 0;
3762
3763                         if (elem)
3764                                 num = GPOINTER_TO_INT(elem->data);
3765
3766                         if (num > lastnum + 1 || elem == NULL) {
3767                                 int i;
3768                                 for (i = startnum; i <= lastnum; ++i) {
3769                                         gchar *file;
3770                                         unlock_session(session);
3771                                         file = imap_fetch_msg(folder, item, i);
3772                                         lock_session(session);
3773                                         if (file != NULL) {
3774                                                 MsgInfo *msginfo = imap_parse_msg(file, item);
3775                                                 if (msginfo != NULL) {
3776                                                         msginfo->msgnum = i;
3777                                                         if (llast == NULL)
3778                                                                 llast = ret = g_slist_append(ret, msginfo);
3779                                                         else {
3780                                                                 llast = g_slist_append(llast, msginfo);
3781                                                                 llast = llast->next;
3782                                                         }
3783                                                 }
3784                                                 g_free(file);
3785                                         }
3786                                         session_set_access_time(SESSION(session));
3787                                 }
3788
3789                                 if (elem == NULL)
3790                                         break;
3791
3792                                 startnum = num;
3793                         }
3794                         lastnum = num;
3795                 }
3796
3797                 g_slist_free(sorted_list);
3798         }
3799         unlock_session(session);
3800         return ret;
3801 }
3802
3803 MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
3804 {
3805         MsgInfo *msginfo = NULL;
3806         MsgInfoList *msginfolist;
3807         MsgNumberList numlist;
3808
3809         numlist.next = NULL;
3810         numlist.data = GINT_TO_POINTER(uid);
3811
3812         msginfolist = imap_get_msginfos(folder, item, &numlist);
3813         if (msginfolist != NULL) {
3814                 msginfo = msginfolist->data;
3815                 g_slist_free(msginfolist);
3816         }
3817
3818         return msginfo;
3819 }
3820
3821 gboolean imap_scan_required(Folder *folder, FolderItem *_item)
3822 {
3823         IMAPSession *session;
3824         IMAPFolderItem *item = (IMAPFolderItem *)_item;
3825         gint ok, exists = 0, unseen = 0;
3826         guint32 uid_next = 0, uid_val = 0;
3827         gboolean selected_folder;
3828         
3829         g_return_val_if_fail(folder != NULL, FALSE);
3830         g_return_val_if_fail(item != NULL, FALSE);
3831         g_return_val_if_fail(item->item.folder != NULL, FALSE);
3832         g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
3833
3834         if (item->item.path == NULL)
3835                 return FALSE;
3836
3837         if (item->should_update) {
3838                 debug_print("scan already required\n");
3839                 return TRUE;
3840         }
3841         debug_print("getting session...\n");
3842         session = imap_session_get(folder);
3843         g_return_val_if_fail(session != NULL, FALSE);
3844
3845         selected_folder = (session->mbox != NULL) &&
3846                           (!strcmp(session->mbox, item->item.path));
3847         if (selected_folder) {
3848                 if (!session->folder_content_changed) {
3849                         ok = imap_cmd_noop(session);
3850                         if (ok != IMAP_SUCCESS) {
3851                                 debug_print("disconnected!\n");
3852                                 session = imap_reconnect_if_possible(folder, session);
3853                                 if (session == NULL)
3854                                         return FALSE;
3855                         }
3856
3857                         if (session->folder_content_changed) {
3858                                 debug_print("CHANGED (self-noop)! scan_required\n");
3859                                 unlock_session(session);
3860                                 item->should_update = TRUE;
3861                                 if (session->uid_validity && session->uid_validity != item->item.mtime) {
3862                                         item->item.mtime = session->uid_validity;
3863                                         item->should_trash_cache = TRUE;
3864                                 }
3865
3866                                 return TRUE;
3867                         }
3868                 } else {
3869                         debug_print("CHANGED (previous noop)! scan_required\n");
3870                         unlock_session(session);
3871                         item->should_update = TRUE;
3872                         if (session->uid_validity && session->uid_validity != item->item.mtime) {
3873                                 item->item.mtime = session->uid_validity;
3874                                 item->should_trash_cache = TRUE;
3875                         }
3876                         return TRUE;
3877                 }
3878         } else {
3879                 ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
3880                                  &exists, &uid_next, &uid_val, &unseen, FALSE);
3881                 if (ok != IMAP_SUCCESS) {
3882                         unlock_session(session);
3883                         return FALSE;
3884                 }
3885
3886                 debug_print("exists %d, item->item.total_msgs %d\n", 
3887                         exists, item->item.total_msgs);
3888                 if (exists != item->item.total_msgs
3889                     || unseen != item->item.unread_msgs 
3890                     || uid_next != item->uid_next
3891                     || uid_val != item->item.mtime) {
3892                         unlock_session(session);
3893                         debug_print("CHANGED (status)! scan_required\n");
3894                         item->last_change = time(NULL);
3895                         item->should_update = TRUE;
3896                         item->uid_next = uid_next;
3897                         if (uid_val != item->item.mtime) {
3898                                 item->item.mtime = uid_val;
3899                                 item->should_trash_cache = TRUE;
3900                         }
3901                         return TRUE;
3902                 }
3903         }
3904         item->should_update = FALSE;
3905         unlock_session(session);
3906         return FALSE;
3907 }
3908
3909 void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3910 {
3911         IMAPSession *session;
3912         IMAPFlags flags_set = 0, flags_unset = 0;
3913         gint ok = IMAP_SUCCESS;
3914         MsgNumberList numlist;
3915         hashtable_data *ht_data = NULL;
3916
3917         g_return_if_fail(folder != NULL);
3918         g_return_if_fail(folder->klass == &imap_class);
3919         g_return_if_fail(item != NULL);
3920         g_return_if_fail(item->folder == folder);
3921         g_return_if_fail(msginfo != NULL);
3922         g_return_if_fail(msginfo->folder == item);
3923
3924         if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
3925                 flags_set |= IMAP_FLAG_FLAGGED;
3926         if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
3927                 flags_unset |= IMAP_FLAG_FLAGGED;
3928
3929         if (!MSG_IS_UNREAD(msginfo->flags) &&  (newflags & MSG_UNREAD))
3930                 flags_unset |= IMAP_FLAG_SEEN;
3931         if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
3932                 flags_set |= IMAP_FLAG_SEEN;
3933
3934         if (!MSG_IS_REPLIED(msginfo->flags) &&  (newflags & MSG_REPLIED))
3935                 flags_set |= IMAP_FLAG_ANSWERED;
3936         if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
3937                 flags_unset |= IMAP_FLAG_ANSWERED;
3938
3939         if (!MSG_IS_DELETED(msginfo->flags) &&  (newflags & MSG_DELETED))
3940                 flags_set |= IMAP_FLAG_DELETED;
3941         if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
3942                 flags_unset |= IMAP_FLAG_DELETED;
3943
3944         if (!flags_set && !flags_unset) {
3945                 /* the changed flags were not translatable to IMAP-speak.
3946                  * like MSG_POSTFILTERED, so just apply. */
3947                 msginfo->flags.perm_flags = newflags;
3948                 return;
3949         }
3950
3951         debug_print("getting session...\n");
3952         session = imap_session_get(folder);
3953         if (!session) {
3954                 return;
3955         }
3956
3957         if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
3958             NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
3959                 unlock_session(session);
3960                 return;
3961         }
3962         numlist.next = NULL;
3963         numlist.data = GINT_TO_POINTER(msginfo->msgnum);
3964
3965         if (IMAP_FOLDER_ITEM(item)->batching) {
3966                 /* instead of performing an UID STORE command for each message change,
3967                  * as a lot of them can change "together", we just fill in hashtables
3968                  * and defer the treatment so that we're able to send only one
3969                  * command.
3970                  */
3971                 debug_print("IMAP batch mode on, deferring flags change\n");
3972                 if (flags_set) {
3973                         ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_set_table, 
3974                                 GINT_TO_POINTER(flags_set));
3975                         if (ht_data == NULL) {
3976                                 ht_data = g_new0(hashtable_data, 1);
3977                                 ht_data->session = session;
3978                                 ht_data->item = IMAP_FOLDER_ITEM(item);
3979                                 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_set_table, 
3980                                         GINT_TO_POINTER(flags_set), ht_data);
3981                         }
3982                         ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
3983                 } 
3984                 if (flags_unset) {
3985                         ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->flags_unset_table, 
3986                                 GINT_TO_POINTER(flags_unset));
3987                         if (ht_data == NULL) {
3988                                 ht_data = g_new0(hashtable_data, 1);
3989                                 ht_data->session = session;
3990                                 ht_data->item = IMAP_FOLDER_ITEM(item);
3991                                 g_hash_table_insert(IMAP_FOLDER_ITEM(item)->flags_unset_table, 
3992                                         GINT_TO_POINTER(flags_unset), ht_data);
3993                         }
3994                         ht_data->msglist = g_slist_prepend(ht_data->msglist, 
3995                                         GINT_TO_POINTER(msginfo->msgnum));              
3996                 }
3997         } else {
3998                 debug_print("IMAP changing flags\n");
3999                 if (flags_set) {
4000                         ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
4001                         if (ok != IMAP_SUCCESS) {
4002                                 unlock_session(session);
4003                                 return;
4004                         }
4005                 }
4006
4007                 if (flags_unset) {
4008                         ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
4009                         if (ok != IMAP_SUCCESS) {
4010                                 unlock_session(session);
4011                                 return;
4012                         }
4013                 }
4014         }
4015         msginfo->flags.perm_flags = newflags;
4016         unlock_session(session);
4017         return;
4018 }
4019
4020 static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
4021 {
4022         gint ok;
4023         IMAPSession *session;
4024         gchar *dir;
4025         MsgNumberList numlist;
4026         
4027         g_return_val_if_fail(folder != NULL, -1);
4028         g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
4029         g_return_val_if_fail(item != NULL, -1);
4030
4031         debug_print("getting session...\n");
4032         session = imap_session_get(folder);
4033         if (!session) return -1;
4034
4035         ok = imap_select(session, IMAP_FOLDER(folder), item->path,
4036                          NULL, NULL, NULL, NULL, FALSE);
4037         if (ok != IMAP_SUCCESS) {
4038                 unlock_session(session);
4039                 return ok;
4040         }
4041         numlist.next = NULL;
4042         numlist.data = GINT_TO_POINTER(uid);
4043         
4044         ok = imap_set_message_flags
4045                 (session, &numlist, IMAP_FLAG_DELETED, TRUE);
4046         if (ok != IMAP_SUCCESS) {
4047                 log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
4048                 unlock_session(session);
4049                 return ok;
4050         }
4051
4052         if (!session->uidplus) {
4053                 ok = imap_cmd_expunge(session);
4054         } else {
4055                 gchar *uidstr;
4056
4057                 uidstr = g_strdup_printf("%u", uid);
4058                 ok = imap_cmd_expunge(session);
4059                 g_free(uidstr);
4060         }
4061         if (ok != IMAP_SUCCESS) {
4062                 log_warning(LOG_PROTOCOL, _("can't expunge\n"));
4063                 unlock_session(session);
4064                 return ok;
4065         }
4066
4067         IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
4068             IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
4069         dir = folder_item_get_path(item);
4070         if (is_dir_exist(dir))
4071                 remove_numbered_files(dir, uid, uid);
4072         g_free(dir);
4073         unlock_session(session);
4074         return IMAP_SUCCESS;
4075 }
4076
4077 static gint compare_msginfo(gconstpointer a, gconstpointer b)
4078 {
4079         return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
4080 }
4081
4082 static guint gslist_find_next_num(MsgNumberList **list, guint num)
4083 {
4084         GSList *elem;
4085
4086         g_return_val_if_fail(list != NULL, -1);
4087
4088         for (elem = *list; elem != NULL; elem = g_slist_next(elem))
4089                 if (GPOINTER_TO_INT(elem->data) >= num)
4090                         break;
4091         *list = elem;
4092         return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
4093 }
4094
4095 /*
4096  * NEW and DELETED flags are not syncronized
4097  * - The NEW/RECENT flags in IMAP folders can not really be directly
4098  *   modified by Sylpheed
4099  * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
4100  *   meaning, in IMAP it always removes the messages from the FolderItem
4101  *   in Sylpheed it can mean to move the message to trash
4102  */
4103
4104 typedef struct _get_flags_data {
4105         Folder *folder;
4106         FolderItem *item;
4107         MsgInfoList *msginfo_list;
4108         GRelation *msgflags;
4109         gboolean full_search;
4110         gboolean done;
4111 } get_flags_data;
4112
4113 static /*gint*/ void *imap_get_flags_thread(void *data)
4114 {
4115         get_flags_data *stuff = (get_flags_data *)data;
4116         Folder *folder = stuff->folder;
4117         FolderItem *fitem = (FolderItem *) stuff->item;
4118         MsgInfoList *msginfo_list = stuff->msginfo_list;
4119         GRelation *msgflags = stuff->msgflags;
4120         GSList *elem;
4121         carray * lep_uidtab;
4122         IMAPSession *session;
4123         gint ok;
4124         int r;
4125         GHashTable *flags_hash = NULL;
4126         gboolean full_search = stuff->full_search;
4127         GSList *sorted_list = NULL;
4128         GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
4129         GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
4130         GSList *seq_list, *cur;
4131         gboolean reverse_seen = FALSE;
4132         gboolean selected_folder;
4133         gint exists_cnt, unseen_cnt;
4134         
4135         session = imap_session_get(folder);
4136         if (session == NULL) {
4137                 stuff->done = TRUE;
4138                 return GINT_TO_POINTER(-1);
4139         }
4140         selected_folder = (session->mbox != NULL) &&
4141                           (!strcmp(session->mbox, fitem->path));
4142
4143         if (!selected_folder) {
4144                 ok = imap_select(session, IMAP_FOLDER(folder), fitem->path,
4145                         &exists_cnt, NULL, &unseen_cnt, NULL, TRUE);
4146                 if (ok != IMAP_SUCCESS) {
4147                         stuff->done = TRUE;
4148                         unlock_session(session);
4149                         return GINT_TO_POINTER(-1);
4150                 }
4151
4152                 if (unseen_cnt > exists_cnt / 2)
4153                         reverse_seen = TRUE;
4154         } 
4155         else {
4156                 if (fitem->unread_msgs > fitem->total_msgs / 2)
4157                         reverse_seen = TRUE;
4158         }
4159
4160         sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
4161         if (!full_search) {
4162                 seq_list = imap_get_lep_set_from_msglist(msginfo_list);
4163         } else {
4164                 struct mailimap_set * set;
4165                 set = mailimap_set_new_interval(1, 0);
4166                 seq_list = g_slist_append(NULL, set);
4167         }
4168
4169         if (folder->account && folder->account->low_bandwidth) {
4170                 for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
4171                         struct mailimap_set * imapset;
4172                         clist * lep_uidlist;
4173                         int r;
4174
4175                         imapset = cur->data;
4176                         if (reverse_seen) {
4177                                 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
4178                                                          full_search ? NULL:imapset, &lep_uidlist);
4179                         }
4180                         else {
4181                                 r = imap_threaded_search(folder,
4182                                                          IMAP_SEARCH_TYPE_UNSEEN,
4183                                                          full_search ? NULL:imapset, &lep_uidlist);
4184                         }
4185                         if (r == MAILIMAP_NO_ERROR) {
4186                                 GSList * uidlist;
4187
4188                                 uidlist = imap_uid_list_from_lep(lep_uidlist);
4189                                 mailimap_search_result_free(lep_uidlist);
4190
4191                                 unseen = g_slist_concat(unseen, uidlist);
4192                         }
4193
4194                         r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
4195                                                  full_search ? NULL:imapset, &lep_uidlist);
4196                         if (r == MAILIMAP_NO_ERROR) {
4197                                 GSList * uidlist;
4198
4199                                 uidlist = imap_uid_list_from_lep(lep_uidlist);
4200                                 mailimap_search_result_free(lep_uidlist);
4201
4202                                 flagged = g_slist_concat(flagged, uidlist);
4203                         }
4204
4205                         if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4206                                 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
4207                                                          full_search ? NULL:imapset, &lep_uidlist);
4208                                 if (r == MAILIMAP_NO_ERROR) {
4209                                         GSList * uidlist;
4210
4211                                         uidlist = imap_uid_list_from_lep(lep_uidlist);
4212                                         mailimap_search_result_free(lep_uidlist);
4213
4214                                         answered = g_slist_concat(answered, uidlist);
4215                                 }
4216
4217                                 r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
4218                                                          full_search ? NULL:imapset, &lep_uidlist);
4219                                 if (r == MAILIMAP_NO_ERROR) {
4220                                         GSList * uidlist;
4221
4222                                         uidlist = imap_uid_list_from_lep(lep_uidlist);
4223                                         mailimap_search_result_free(lep_uidlist);
4224
4225                                         deleted = g_slist_concat(deleted, uidlist);
4226                                 }
4227                         }
4228                 }
4229                 p_unseen = unseen;
4230                 p_answered = answered;
4231                 p_flagged = flagged;
4232                 p_deleted = deleted;
4233
4234         } else {
4235                 r = imap_threaded_fetch_uid_flags(folder, 1, &lep_uidtab);
4236                 if (r == MAILIMAP_NO_ERROR) {
4237                         flags_hash = g_hash_table_new_full(g_int_hash, g_int_equal, free, NULL);
4238                         imap_flags_hash_from_lep_uid_flags_tab(lep_uidtab, flags_hash);
4239                         imap_fetch_uid_flags_list_free(lep_uidtab);
4240                 }
4241         }
4242         for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
4243                 MsgInfo *msginfo;
4244                 MsgPermFlags flags, oldflags;
4245                 gboolean wasnew;
4246
4247                 msginfo = (MsgInfo *) elem->data;
4248                 flags = msginfo->flags.perm_flags;
4249                 wasnew = (flags & MSG_NEW);
4250                 oldflags = flags & ~(MSG_NEW|MSG_UNREAD|MSG_REPLIED|MSG_MARKED|MSG_DELETED);
4251
4252                 if (folder->account && folder->account->low_bandwidth) {
4253                         if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4254                                 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
4255                         } else {
4256                                 flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
4257                         }
4258                         if (reverse_seen)
4259                                 flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4260                         if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
4261                                 if (!reverse_seen) {
4262                                         flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
4263                                 } else {
4264                                         flags &= ~(MSG_UNREAD | MSG_NEW);
4265                                 }
4266                         }
4267
4268                         if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
4269                                 flags |= MSG_MARKED;
4270                         else
4271                                 flags &= ~MSG_MARKED;
4272
4273                         if (fitem->opened || fitem->processing_pending || fitem == folder->inbox) {
4274                                 if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
4275                                         flags |= MSG_REPLIED;
4276                                 else
4277                                         flags &= ~MSG_REPLIED;
4278                                 if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
4279                                         flags |= MSG_DELETED;
4280                                 else
4281                                         flags &= ~MSG_DELETED;
4282                         }
4283                 } else {
4284                         if (flags_hash != NULL) {
4285                                 gint * puid;
4286
4287                                 puid = malloc(sizeof(* puid));
4288                                 * puid = msginfo->msgnum;
4289
4290                                 flags = GPOINTER_TO_INT(g_hash_table_lookup(flags_hash, puid));
4291                                 free(puid);
4292                         }
4293
4294                         if ((flags & MSG_UNREAD) == 0)
4295                                 flags &= ~MSG_NEW;
4296                         else if (wasnew)
4297                                 flags |= MSG_NEW;
4298                         flags |= oldflags;
4299                 }
4300
4301                 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
4302         }
4303         
4304         if (flags_hash)
4305                 g_hash_table_destroy(flags_hash);
4306
4307         imap_lep_set_free(seq_list);
4308         g_slist_free(flagged);
4309         g_slist_free(deleted);
4310         g_slist_free(answered);
4311         g_slist_free(unseen);
4312         g_slist_free(sorted_list);
4313
4314         unlock_session(session);
4315         stuff->done = TRUE;
4316         return GINT_TO_POINTER(0);
4317 }
4318
4319 static gint imap_get_flags(Folder *folder, FolderItem *item,
4320                            MsgInfoList *msginfo_list, GRelation *msgflags)
4321 {
4322         gint result;
4323         get_flags_data *data = g_new0(get_flags_data, 1);
4324         data->done = FALSE;
4325         data->folder = folder;
4326         data->item = item;
4327         data->msginfo_list = msginfo_list;
4328         data->msgflags = msgflags;
4329         data->full_search = FALSE;
4330
4331         GSList *tmp = NULL, *cur;
4332         
4333         if (prefs_common.work_offline && 
4334             !inc_offline_should_override(FALSE,
4335                 _("Claws Mail needs network access in order "
4336                   "to access the IMAP server."))) {
4337                 g_free(data);
4338                 return -1;
4339         }
4340
4341         tmp = folder_item_get_msg_list(item);
4342
4343         if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
4344                 data->full_search = TRUE;
4345         
4346         for (cur = tmp; cur; cur = cur->next)
4347                 procmsg_msginfo_free((MsgInfo *)cur->data);
4348         
4349         g_slist_free(tmp);
4350
4351         result = GPOINTER_TO_INT(imap_get_flags_thread(data));
4352         
4353         g_free(data);
4354         return result;
4355
4356 }
4357
4358 static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
4359 {
4360         gboolean flags_set = GPOINTER_TO_INT(user_data);
4361         gint flags_value = GPOINTER_TO_INT(key);
4362         hashtable_data *data = (hashtable_data *)value;
4363         IMAPFolderItem *_item = data->item;
4364         FolderItem *item = (FolderItem *)_item;
4365         gint ok = IMAP_ERROR;
4366         IMAPSession *session = NULL;
4367         
4368         debug_print("getting session...\n");
4369         session = imap_session_get(item->folder);
4370
4371         data->msglist = g_slist_reverse(data->msglist);
4372         
4373         debug_print("IMAP %ssetting flags to %d for %d messages\n",
4374                 flags_set?"":"un",
4375                 flags_value,
4376                 g_slist_length(data->msglist));
4377         
4378         if (session) {
4379                 ok = imap_select(session, IMAP_FOLDER(item->folder), item->path,
4380                          NULL, NULL, NULL, NULL, FALSE);
4381         }
4382         if (ok == IMAP_SUCCESS) {
4383                 imap_set_message_flags(data->session, data->msglist, flags_value, flags_set);
4384         } else {
4385                 g_warning("can't select mailbox %s\n", item->path);
4386         }
4387
4388         unlock_session(session);
4389         g_slist_free(data->msglist);    
4390         g_free(data);
4391         return TRUE;
4392 }
4393
4394 static void process_hashtable(IMAPFolderItem *item)
4395 {
4396         if (item->flags_set_table) {
4397                 g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
4398                 g_hash_table_destroy(item->flags_set_table);
4399                 item->flags_set_table = NULL;
4400         }
4401         if (item->flags_unset_table) {
4402                 g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
4403                 g_hash_table_destroy(item->flags_unset_table);
4404                 item->flags_unset_table = NULL;
4405         }
4406         
4407 }
4408
4409 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
4410 {
4411         IMAPFolderItem *item = (IMAPFolderItem *)_item;
4412         IMAPSession *session;
4413
4414         g_return_if_fail(item != NULL);
4415         
4416         if (item->batching == batch)
4417                 return;
4418         
4419         if (batch) {
4420                 item->batching = TRUE;
4421                 debug_print("IMAP switching to batch mode\n");
4422                 if (!item->flags_set_table) {
4423                         item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
4424                 }
4425                 if (!item->flags_unset_table) {
4426                         item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
4427                 }
4428                 session = imap_session_get(folder);
4429                 if (session) {
4430                         imap_refresh_sensitivity(session);
4431                         session->sens_update_block = TRUE;
4432                         unlock_session(session);
4433                 }
4434         } else {
4435                 debug_print("IMAP switching away from batch mode\n");
4436                 /* process stuff */
4437                 process_hashtable(item);
4438                 item->batching = FALSE;
4439                 session = imap_session_get(folder);
4440                 if (session) {
4441                         unlock_session(session);
4442                         session->sens_update_block = FALSE;
4443                         imap_refresh_sensitivity(session);
4444                 }
4445         }
4446 }
4447
4448
4449
4450 /* data types conversion libetpan <-> claws */
4451
4452
4453
4454 #define ETPAN_IMAP_MB_MARKED      1
4455 #define ETPAN_IMAP_MB_UNMARKED    2
4456 #define ETPAN_IMAP_MB_NOSELECT    4
4457 #define ETPAN_IMAP_MB_NOINFERIORS 8
4458
4459 static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
4460 {
4461   int flags;
4462   clistiter * cur;
4463   
4464   flags = 0;
4465   if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
4466     switch (imap_flags->mbf_sflag) {
4467     case MAILIMAP_MBX_LIST_SFLAG_MARKED:
4468       flags |= ETPAN_IMAP_MB_MARKED;
4469       break;
4470     case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
4471       flags |= ETPAN_IMAP_MB_NOSELECT;
4472       break;
4473     case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
4474       flags |= ETPAN_IMAP_MB_UNMARKED;
4475       break;
4476     }
4477   }
4478   
4479   for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
4480       cur = clist_next(cur)) {
4481     struct mailimap_mbx_list_oflag * oflag;
4482     
4483     oflag = clist_content(cur);
4484     
4485     switch (oflag->of_type) {
4486     case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
4487       flags |= ETPAN_IMAP_MB_NOINFERIORS;
4488       break;
4489     }
4490   }
4491   
4492   return flags;
4493 }
4494
4495 static GSList * imap_list_from_lep(IMAPFolder * folder,
4496                                    clist * list, const gchar * real_path, gboolean all)
4497 {
4498         clistiter * iter;
4499         GSList * item_list = NULL, *llast = NULL;
4500         
4501         for(iter = clist_begin(list) ; iter != NULL ;
4502             iter = clist_next(iter)) {
4503                 struct mailimap_mailbox_list * mb;
4504                 int flags;
4505                 char delimiter;
4506                 char * name;
4507                 char * dup_name;
4508                 gchar * base;
4509                 gchar * loc_name;
4510                 gchar * loc_path;
4511                 FolderItem *new_item;
4512                 
4513                 mb = clist_content(iter);
4514
4515                 if (mb == NULL)
4516                         continue;
4517
4518                 flags = 0;
4519                 if (mb->mb_flag != NULL)
4520                         flags = imap_flags_to_flags(mb->mb_flag);
4521                 
4522                 delimiter = mb->mb_delimiter;
4523                 name = mb->mb_name;
4524                 
4525                 dup_name = strdup(name);                
4526                 if (delimiter != '\0')
4527                         subst_char(dup_name, delimiter, '/');
4528                 
4529                 base = g_path_get_basename(dup_name);
4530                 if (base[0] == '.') {
4531                         g_free(base);
4532                         free(dup_name);
4533                         continue;
4534                 }
4535                 if (!all && path_cmp(name, real_path) == 0) {
4536                         g_free(base);
4537                         free(dup_name);
4538                         continue;
4539                 }
4540
4541                 if (!all && dup_name[strlen(dup_name)-1] == '/') {
4542                         dup_name[strlen(dup_name)-1] = '\0';
4543                 }
4544                 
4545                 loc_name = imap_modified_utf7_to_utf8(base);
4546                 loc_path = imap_modified_utf7_to_utf8(dup_name);
4547                 
4548                 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
4549                 if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
4550                         new_item->no_sub = TRUE;
4551                 if (strcmp(dup_name, "INBOX") != 0 &&
4552                     ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
4553                         new_item->no_select = TRUE;
4554                 
4555                 if (item_list == NULL)
4556                         llast = item_list = g_slist_append(item_list, new_item);
4557                 else {
4558                         llast = g_slist_append(llast, new_item);
4559                         llast = llast->next;
4560                 }
4561                 debug_print("folder '%s' found.\n", loc_path);
4562                 g_free(base);
4563                 g_free(loc_path);
4564                 g_free(loc_name);
4565                 
4566                 free(dup_name);
4567         }
4568         
4569         return item_list;
4570 }
4571
4572 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
4573 {
4574         GSList *sorted_list, *cur;
4575         guint first, last, next;
4576         GSList *ret_list = NULL, *llast = NULL;
4577         unsigned int count;
4578         struct mailimap_set * current_set;
4579         unsigned int item_count;
4580         
4581         if (numlist == NULL)
4582                 return NULL;
4583         
4584         count = 0;
4585         current_set = mailimap_set_new_empty();
4586         
4587         sorted_list = g_slist_copy(numlist);
4588         sorted_list = g_slist_sort(sorted_list, g_int_compare);
4589
4590         first = GPOINTER_TO_INT(sorted_list->data);
4591         
4592         item_count = 0;
4593         for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
4594                 if (GPOINTER_TO_INT(cur->data) == 0)
4595                         continue;
4596                 
4597                 item_count ++;
4598
4599                 last = GPOINTER_TO_INT(cur->data);
4600                 if (cur->next)
4601                         next = GPOINTER_TO_INT(cur->next->data);
4602                 else
4603                         next = 0;
4604
4605                 if (last + 1 != next || next == 0) {
4606
4607                         struct mailimap_set_item * item;
4608                         item = mailimap_set_item_new(first, last);
4609                         mailimap_set_add(current_set, item);
4610                         count ++;
4611                         
4612                         first = next;
4613                         
4614                         if (count >= IMAP_SET_MAX_COUNT) {
4615                                 if (ret_list == NULL)
4616                                         llast = ret_list = g_slist_append(ret_list,
4617                                                           current_set);
4618                                 else {
4619                                         llast = g_slist_append(llast, current_set);
4620                                         llast = llast->next;
4621                                 }
4622                                 current_set = mailimap_set_new_empty();
4623                                 count = 0;
4624                                 item_count = 0;
4625                         }
4626                 }
4627         }
4628         
4629         if (clist_count(current_set->set_list) > 0) {
4630                 ret_list = g_slist_append(ret_list,
4631                                           current_set);
4632         }
4633         
4634         g_slist_free(sorted_list);
4635
4636         return ret_list;
4637 }
4638
4639 static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist)
4640 {
4641         MsgNumberList *numlist = NULL;
4642         MsgInfoList *cur;
4643         GSList *seq_list;
4644
4645         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
4646                 MsgInfo *msginfo = (MsgInfo *) cur->data;
4647
4648                 numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
4649         }
4650         numlist = g_slist_reverse(numlist);
4651         seq_list = imap_get_lep_set_from_numlist(numlist);
4652         g_slist_free(numlist);
4653
4654         return seq_list;
4655 }
4656
4657 static GSList * imap_uid_list_from_lep(clist * list)
4658 {
4659         clistiter * iter;
4660         GSList * result;
4661         
4662         result = NULL;
4663         
4664         for(iter = clist_begin(list) ; iter != NULL ;
4665             iter = clist_next(iter)) {
4666                 uint32_t * puid;
4667                 
4668                 puid = clist_content(iter);
4669                 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4670         }
4671         
4672         result = g_slist_reverse(result);
4673         return result;
4674 }
4675
4676 static GSList * imap_uid_list_from_lep_tab(carray * list)
4677 {
4678         unsigned int i;
4679         GSList * result;
4680         
4681         result = NULL;
4682         
4683         for(i = 0 ; i < carray_count(list) ; i ++) {
4684                 uint32_t * puid;
4685                 
4686                 puid = carray_get(list, i);
4687                 result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
4688         }
4689         result = g_slist_reverse(result);
4690         return result;
4691 }
4692
4693 static void imap_flags_hash_from_lep_uid_flags_tab(carray * list,
4694                                                    GHashTable * hash)
4695 {
4696         unsigned int i;
4697         GSList * result;
4698         
4699         result = NULL;
4700         
4701         for(i = 0 ; i < carray_count(list) ; i += 2) {
4702                 uint32_t * puid;
4703                 int * pflags;
4704                 gint * pguid;
4705                 
4706                 puid = carray_get(list, i);
4707                 pflags = carray_get(list, i + 1);
4708                 pguid = malloc(sizeof(* pguid));
4709                 * pguid = * puid;
4710                 
4711                 g_hash_table_insert(hash, pguid, GINT_TO_POINTER(* pflags));
4712         }
4713 }
4714
4715 static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
4716                                        FolderItem *item)
4717 {
4718         MsgInfo *msginfo = NULL;
4719         guint32 uid = 0;
4720         size_t size = 0;
4721         MsgFlags flags = {0, 0};
4722         
4723         if (info->headers == NULL)
4724                 return NULL;
4725
4726         MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
4727         if (folder_has_parent_of_type(item, F_QUEUE)) {
4728                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
4729         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
4730                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
4731         }
4732         flags.perm_flags = info->flags;
4733         
4734         uid = info->uid;
4735         size = info->size;
4736         msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
4737         
4738         if (msginfo) {
4739                 msginfo->msgnum = uid;
4740                 msginfo->size = size;
4741         }
4742
4743         return msginfo;
4744 }
4745
4746 static void imap_lep_set_free(GSList *seq_list)
4747 {
4748         GSList * cur;
4749         
4750         for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
4751                 struct mailimap_set * imapset;
4752                 
4753                 imapset = cur->data;
4754                 mailimap_set_free(imapset);
4755         }
4756         g_slist_free(seq_list);
4757 }
4758
4759 static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags)
4760 {
4761         struct mailimap_flag_list * flag_list;
4762         
4763         flag_list = mailimap_flag_list_new_empty();
4764         
4765         if (IMAP_IS_SEEN(flags))
4766                 mailimap_flag_list_add(flag_list,
4767                                        mailimap_flag_new_seen());
4768         if (IMAP_IS_ANSWERED(flags))
4769                 mailimap_flag_list_add(flag_list,
4770                                        mailimap_flag_new_answered());
4771         if (IMAP_IS_FLAGGED(flags))
4772                 mailimap_flag_list_add(flag_list,
4773                                        mailimap_flag_new_flagged());
4774         if (IMAP_IS_DELETED(flags))
4775                 mailimap_flag_list_add(flag_list,
4776                                        mailimap_flag_new_deleted());
4777         if (IMAP_IS_DRAFT(flags))
4778                 mailimap_flag_list_add(flag_list,
4779                                        mailimap_flag_new_draft());
4780         
4781         return flag_list;
4782 }
4783
4784 guint imap_folder_get_refcnt(Folder *folder)
4785 {
4786         return ((IMAPFolder *)folder)->refcnt;
4787 }
4788
4789 void imap_folder_ref(Folder *folder)
4790 {
4791         ((IMAPFolder *)folder)->refcnt++;
4792 }
4793
4794 void imap_disconnect_all(void)
4795 {
4796         GList *list;
4797         for (list = account_get_list(); list != NULL; list = list->next) {
4798                 PrefsAccount *account = list->data;
4799                 if (account->protocol == A_IMAP4) {
4800                         RemoteFolder *folder = (RemoteFolder *)account->folder;
4801                         if (folder && folder->session) {
4802                                 IMAPSession *session = (IMAPSession *)folder->session;
4803                                 imap_threaded_disconnect(FOLDER(folder));
4804                                 SESSION(session)->state = SESSION_DISCONNECTED;
4805                                 session_destroy(SESSION(session));
4806                                 folder->session = NULL;
4807                         }
4808                 }
4809         }
4810 }
4811
4812 void imap_folder_unref(Folder *folder)
4813 {
4814         if (((IMAPFolder *)folder)->refcnt > 0)
4815                 ((IMAPFolder *)folder)->refcnt--;
4816 }
4817
4818 void imap_cancel_all(void)
4819 {
4820         GList *folderlist;
4821         GList *cur;
4822         
4823         folderlist = folder_get_list();
4824         for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
4825                 Folder *folder = (Folder *) cur->data;
4826
4827                 if (folder->klass == &imap_class) {
4828                         if (imap_is_busy(folder)) {
4829                                 IMAPSession *imap_session;
4830                                 RemoteFolder *rfolder;
4831                                 
4832                                 g_printerr("cancelled\n");
4833                                 imap_threaded_cancel(folder);
4834                                 rfolder = (RemoteFolder *) folder;
4835                                 imap_session = (IMAPSession *) rfolder->session;
4836                                 if (imap_session)
4837                                         imap_session->cancelled = 1;
4838                         }
4839                 }
4840         }
4841 }
4842
4843 gboolean imap_cancel_all_enabled(void)
4844 {
4845         GList *folderlist;
4846         GList *cur;
4847         
4848         folderlist = folder_get_list();
4849         for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
4850                 Folder *folder = (Folder *) cur->data;
4851
4852                 if (folder->klass == &imap_class) {
4853                         if (imap_is_busy(folder)) {
4854                                 return TRUE;
4855                         }
4856                 }
4857         }
4858         
4859         return FALSE;
4860 }
4861
4862 static gboolean imap_is_busy(Folder *folder)
4863 {
4864         IMAPSession *imap_session;
4865         RemoteFolder *rfolder;
4866         
4867         rfolder = (RemoteFolder *) folder;
4868         imap_session = (IMAPSession *) rfolder->session;
4869         if (imap_session == NULL)
4870                 return FALSE;
4871         
4872         return imap_session->busy;
4873 }
4874
4875 #else /* HAVE_LIBETPAN */
4876
4877 static FolderClass imap_class;
4878
4879 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
4880 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
4881
4882 static Folder   *imap_folder_new        (const gchar    *name,
4883                                          const gchar    *path)
4884 {
4885         static gboolean missing_imap_warning = TRUE;
4886         if (missing_imap_warning) {
4887                 missing_imap_warning = FALSE;
4888                 alertpanel_error(
4889                         _("You have one or more IMAP accounts "
4890                           "defined. However this version of "
4891                           "Claws Mail has been built without "
4892                           "IMAP support; your IMAP account(s) are "
4893                           "disabled.\n\n"
4894                           "You probably need to "
4895                           "install libetpan and recompile "
4896                           "Claws Mail."));
4897         }
4898         return NULL;
4899 }
4900 static gint     imap_create_tree        (Folder         *folder)
4901 {
4902         return -1;
4903 }
4904 static FolderItem *imap_create_folder   (Folder         *folder,
4905                                          FolderItem     *parent,
4906                                          const gchar    *name)
4907 {
4908         return NULL;
4909 }
4910 static gint     imap_rename_folder      (Folder         *folder,
4911                                          FolderItem     *item, 
4912                                          const gchar    *name)
4913 {
4914         return -1;
4915 }
4916
4917 gchar imap_get_path_separator_for_item(FolderItem *item)
4918 {
4919         return '/';
4920 }
4921
4922 FolderClass *imap_get_class(void)
4923 {
4924         if (imap_class.idstr == NULL) {
4925                 imap_class.type = F_IMAP;
4926                 imap_class.idstr = "imap";
4927                 imap_class.uistr = "IMAP4";
4928
4929                 imap_class.new_folder = imap_folder_new;
4930                 imap_class.create_tree = imap_create_tree;
4931                 imap_class.create_folder = imap_create_folder;
4932                 imap_class.rename_folder = imap_rename_folder;
4933
4934                 imap_class.set_xml = folder_set_xml;
4935                 imap_class.get_xml = folder_get_xml;
4936                 imap_class.item_set_xml = imap_item_set_xml;
4937                 imap_class.item_get_xml = imap_item_get_xml;
4938                 /* nothing implemented */
4939         }
4940
4941         return &imap_class;
4942 }
4943
4944 void imap_disconnect_all(void)
4945 {
4946 }
4947
4948 gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
4949 {
4950         return -1;
4951 }
4952
4953 gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
4954 {
4955         return -1;
4956 }
4957
4958 GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
4959 {
4960         return NULL;
4961 }
4962
4963 void imap_cache_msg(FolderItem *item, gint msgnum)
4964 {
4965 }
4966
4967 void imap_cancel_all(void)
4968 {
4969 }
4970
4971 gboolean imap_cancel_all_enabled(void)
4972 {
4973         return FALSE;
4974 }
4975
4976 #endif
4977
4978 void imap_synchronise(FolderItem *item, gint days) 
4979 {
4980 #ifdef HAVE_LIBETPAN
4981         if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
4982                 debug_print("%s already synced\n", item->path?item->path:item->name);
4983                 return;
4984         }
4985         debug_print("syncing %s\n", item->path?item->path:item->name);
4986         imap_gtk_synchronise(item, days);
4987         IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
4988 #endif
4989 }
4990
4991 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
4992 {
4993 #ifdef HAVE_LIBETPAN
4994         GList *cur;
4995 #endif
4996         folder_item_set_xml(folder, item, tag);
4997         
4998 #ifdef HAVE_LIBETPAN
4999         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
5000                 XMLAttr *attr = (XMLAttr *) cur->data;
5001
5002                 if (!attr || !attr->name || !attr->value) continue;
5003                 if (!strcmp(attr->name, "uidnext"))
5004                         IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
5005                 if (!strcmp(attr->name, "last_sync"))
5006                         IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
5007                 if (!strcmp(attr->name, "last_change"))
5008                         IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
5009         }
5010         if (IMAP_FOLDER_ITEM(item)->last_change == 0)
5011                 IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
5012 #endif
5013 }
5014
5015 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
5016 {
5017         XMLTag *tag;
5018
5019         tag = folder_item_get_xml(folder, item);
5020
5021 #ifdef HAVE_LIBETPAN
5022         xml_tag_add_attr(tag, xml_attr_new_int("uidnext", 
5023                         IMAP_FOLDER_ITEM(item)->uid_next));
5024         xml_tag_add_attr(tag, xml_attr_new_int("last_sync", 
5025                         IMAP_FOLDER_ITEM(item)->last_sync));
5026         xml_tag_add_attr(tag, xml_attr_new_int("last_change", 
5027                         IMAP_FOLDER_ITEM(item)->last_change));
5028
5029 #endif
5030         return tag;
5031 }