da901dc4a2a6b9d2c30bb3d3d40cdd96752b78fe
[claws.git] / src / imap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <stdlib.h>
31 #include <dirent.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <time.h>
35 #include <errno.h>
36 #if HAVE_ICONV
37 #  include <iconv.h>
38 #endif
39
40 #if USE_OPENSSL
41 #  include "ssl.h"
42 #endif
43 #include "folder.h"
44 #include "session.h"
45 #include "procmsg.h"
46 #include "imap.h"
47 #include "imap_gtk.h"
48 #include "socket.h"
49 #include "ssl.h"
50 #include "recv.h"
51 #include "procheader.h"
52 #include "prefs_account.h"
53 #include "codeconv.h"
54 #include "md5.h"
55 #include "base64.h"
56 #include "utils.h"
57 #include "prefs_common.h"
58 #include "inputdialog.h"
59 #include "log.h"
60 #include "remotefolder.h"
61 #include "alertpanel.h"
62 #include "sylpheed.h"
63 #include "statusbar.h"
64 #include "msgcache.h"
65
66 #ifdef USE_PTHREAD
67 #include <pthread.h>
68 static pthread_mutex_t imap_mutex;
69 static const char *mutex_hold = NULL;
70
71 #ifndef __GNUC__
72 #define __FUNCTION__ __FILE__
73 #endif 
74
75 #define MUTEX_TRYLOCK_OR_RETURN() {                                     \
76         debug_print("%s: locking mutex\n", __FUNCTION__);               \
77         if (pthread_mutex_trylock(&imap_mutex) == EBUSY) {              \
78                 g_warning("can't lock mutex (held by %s)\n",            \
79                                 mutex_hold ? mutex_hold:"(nil)");       \
80                 return;                                                 \
81         }                                                               \
82         mutex_hold = __FUNCTION__;                                      \
83 }
84
85 #define MUTEX_TRYLOCK_OR_RETURN_VAL(retval) {                           \
86         debug_print("%s: locking mutex\n", __FUNCTION__);               \
87         if (pthread_mutex_trylock(&imap_mutex) == EBUSY) {              \
88                 g_warning("can't lock mutex (held by %s)\n",            \
89                                 mutex_hold ? mutex_hold:"(nil)");       \
90                 return retval;                                          \
91         }                                                               \
92         mutex_hold = __FUNCTION__;                                      \
93 }
94
95 #define MUTEX_UNLOCK() {                                                \
96         debug_print("%s: unlocking mutex\n", __FUNCTION__);\
97         pthread_mutex_unlock(&imap_mutex);                              \
98         mutex_hold = NULL;                                              \
99 }
100
101 #else
102 #define MUTEX_TRYLOCK_OR_RETURN() do {} while(0)
103 #define MUTEX_TRYLOCK_OR_RETURN_VAL(retval) do {} while(0)
104 #define MUTEX_UNLOCK() do {} while(0)
105 #endif
106
107 typedef struct _IMAPFolder      IMAPFolder;
108 typedef struct _IMAPSession     IMAPSession;
109 typedef struct _IMAPNameSpace   IMAPNameSpace;
110 typedef struct _IMAPFolderItem  IMAPFolderItem;
111
112 #include "prefs_account.h"
113
114 #define IMAP_FOLDER(obj)        ((IMAPFolder *)obj)
115 #define IMAP_FOLDER_ITEM(obj)   ((IMAPFolderItem *)obj)
116 #define IMAP_SESSION(obj)       ((IMAPSession *)obj)
117
118 struct _IMAPFolder
119 {
120         RemoteFolder rfolder;
121
122         /* list of IMAPNameSpace */
123         GList *ns_personal;
124         GList *ns_others;
125         GList *ns_shared;
126 };
127
128 struct _IMAPSession
129 {
130         Session session;
131
132         gboolean authenticated;
133
134         gchar **capability;
135         gboolean uidplus;
136
137         gchar *mbox;
138         guint cmd_count;
139
140         /* CLAWS */
141         gboolean folder_content_changed;
142         guint exists;
143 };
144
145 struct _IMAPNameSpace
146 {
147         gchar *name;
148         gchar separator;
149 };
150
151 #define IMAP_SUCCESS    0
152 #define IMAP_SOCKET     2
153 #define IMAP_AUTHFAIL   3
154 #define IMAP_PROTOCOL   4
155 #define IMAP_SYNTAX     5
156 #define IMAP_IOERR      6
157 #define IMAP_ERROR      7
158
159 #define IMAPBUFSIZE     8192
160
161 typedef enum
162 {
163         IMAP_FLAG_SEEN          = 1 << 0,
164         IMAP_FLAG_ANSWERED      = 1 << 1,
165         IMAP_FLAG_FLAGGED       = 1 << 2,
166         IMAP_FLAG_DELETED       = 1 << 3,
167         IMAP_FLAG_DRAFT         = 1 << 4
168 } IMAPFlags;
169
170 #define IMAP_IS_SEEN(flags)     ((flags & IMAP_FLAG_SEEN) != 0)
171 #define IMAP_IS_ANSWERED(flags) ((flags & IMAP_FLAG_ANSWERED) != 0)
172 #define IMAP_IS_FLAGGED(flags)  ((flags & IMAP_FLAG_FLAGGED) != 0)
173 #define IMAP_IS_DELETED(flags)  ((flags & IMAP_FLAG_DELETED) != 0)
174 #define IMAP_IS_DRAFT(flags)    ((flags & IMAP_FLAG_DRAFT) != 0)
175
176
177 #define IMAP4_PORT      143
178 #if USE_OPENSSL
179 #define IMAPS_PORT      993
180 #endif
181
182 #define IMAP_CMD_LIMIT  1000
183
184 #define QUOTE_IF_REQUIRED(out, str)                             \
185 {                                                               \
186         if (*str != '"' && strpbrk(str, " \t(){}[]%*\\") != NULL) {     \
187                 gchar *__tmp;                                   \
188                 gint len;                                       \
189                                                                 \
190                 len = strlen(str) + 3;                          \
191                 Xalloca(__tmp, len, return IMAP_ERROR);         \
192                 g_snprintf(__tmp, len, "\"%s\"", str);          \
193                 out = __tmp;                                    \
194         } else {                                                \
195                 Xstrdup_a(out, str, return IMAP_ERROR);         \
196         }                                                       \
197 }
198
199 typedef gchar * IMAPSet;
200
201 struct _IMAPFolderItem
202 {
203         FolderItem item;
204
205         guint lastuid;
206         guint uid_next;
207         GSList *uid_list;
208         gboolean batching;
209 };
210
211 static void imap_folder_init            (Folder         *folder,
212                                          const gchar    *name,
213                                          const gchar    *path);
214
215 static Folder   *imap_folder_new        (const gchar    *name,
216                                          const gchar    *path);
217 static void      imap_folder_destroy    (Folder         *folder);
218
219 static IMAPSession *imap_session_new    (const PrefsAccount     *account);
220 static void     imap_session_authenticate(IMAPSession           *session,
221                                           const PrefsAccount    *account);
222 static void     imap_session_destroy    (Session        *session);
223
224 static gchar   *imap_fetch_msg          (Folder         *folder, 
225                                          FolderItem     *item, 
226                                          gint            uid);
227 static gchar   *imap_fetch_msg_full     (Folder         *folder, 
228                                          FolderItem     *item, 
229                                          gint            uid,
230                                          gboolean        headers,
231                                          gboolean        body);
232 static gint     imap_add_msg            (Folder         *folder,
233                                          FolderItem     *dest,
234                                          const gchar    *file, 
235                                          MsgFlags       *flags);
236 static gint     imap_add_msgs           (Folder         *folder, 
237                                          FolderItem     *dest,
238                                          GSList         *file_list,
239                                          GRelation      *relation);
240
241 static gint     imap_copy_msg           (Folder         *folder,
242                                          FolderItem     *dest, 
243                                          MsgInfo        *msginfo);
244 static gint     imap_copy_msgs          (Folder         *folder, 
245                                          FolderItem     *dest, 
246                                          MsgInfoList    *msglist, 
247                                          GRelation      *relation);
248
249 static gint     imap_remove_msg         (Folder         *folder, 
250                                          FolderItem     *item, 
251                                          gint            uid);
252 static gint     imap_remove_msgs        (Folder         *folder, 
253                                          FolderItem     *dest, 
254                                          MsgInfoList    *msglist, 
255                                          GRelation      *relation);
256 static gint     imap_remove_all_msg     (Folder         *folder, 
257                                          FolderItem     *item);
258
259 static gboolean imap_is_msg_changed     (Folder         *folder,
260                                          FolderItem     *item, 
261                                          MsgInfo        *msginfo);
262
263 static gint     imap_close              (Folder         *folder, 
264                                          FolderItem     *item);
265
266 static gint     imap_scan_tree          (Folder         *folder);
267
268 static gint     imap_create_tree        (Folder         *folder);
269
270 static FolderItem *imap_create_folder   (Folder         *folder,
271                                          FolderItem     *parent,
272                                          const gchar    *name);
273 static gint     imap_rename_folder      (Folder         *folder,
274                                          FolderItem     *item, 
275                                          const gchar    *name);
276 static gint     imap_remove_folder      (Folder         *folder, 
277                                          FolderItem     *item);
278
279 static FolderItem *imap_folder_item_new (Folder         *folder);
280 static void imap_folder_item_destroy    (Folder         *folder,
281                                          FolderItem     *item);
282
283 static IMAPSession *imap_session_get    (Folder         *folder);
284
285 static gint imap_greeting               (IMAPSession    *session);
286 static gint imap_auth                   (IMAPSession    *session,
287                                          const gchar    *user,
288                                          const gchar    *pass,
289                                          IMAPAuthType    type);
290
291 static gint imap_scan_tree_recursive    (IMAPSession    *session,
292                                          FolderItem     *item);
293 static GSList *imap_parse_list          (IMAPFolder     *folder,
294                                          IMAPSession    *session,
295                                          const gchar    *real_path,
296                                          gchar          *separator);
297
298 static void imap_create_missing_folders (Folder         *folder);
299 static FolderItem *imap_create_special_folder
300                                         (Folder                 *folder,
301                                          SpecialFolderItemType   stype,
302                                          const gchar            *name);
303
304 static gint imap_do_copy_msgs           (Folder         *folder,
305                                          FolderItem     *dest,
306                                          MsgInfoList    *msglist,
307                                          GRelation      *relation);
308
309 static void imap_delete_all_cached_messages     (FolderItem     *item);
310 static void imap_set_batch              (Folder         *folder,
311                                          FolderItem     *item,
312                                          gboolean        batch);
313 #if USE_OPENSSL
314 static SockInfo *imap_open              (const gchar    *server,
315                                          gushort         port,
316                                          SSLType         ssl_type);
317 #else
318 static SockInfo *imap_open              (const gchar    *server,
319                                          gushort         port);
320 #endif
321
322 #if USE_OPENSSL
323 static SockInfo *imap_open_tunnel(const gchar *server,
324                                   const gchar *tunnelcmd,
325                                   SSLType ssl_type);
326 #else
327 static SockInfo *imap_open_tunnel(const gchar *server,
328                                   const gchar *tunnelcmd);
329 #endif
330
331 #if USE_OPENSSL
332 static SockInfo *imap_init_sock(SockInfo *sock, SSLType ssl_type);
333 #else
334 static SockInfo *imap_init_sock(SockInfo *sock);
335 #endif
336
337 static gchar *imap_get_flag_str         (IMAPFlags       flags);
338 static gint imap_set_message_flags      (IMAPSession    *session,
339                                          MsgNumberList  *numlist,
340                                          IMAPFlags       flags,
341                                          gboolean        is_set);
342 static gint imap_select                 (IMAPSession    *session,
343                                          IMAPFolder     *folder,
344                                          const gchar    *path,
345                                          gint           *exists,
346                                          gint           *recent,
347                                          gint           *unseen,
348                                          guint32        *uid_validity,
349                                          gboolean        block);
350 static gint imap_status                 (IMAPSession    *session,
351                                          IMAPFolder     *folder,
352                                          const gchar    *path,
353                                          gint           *messages,
354                                          gint           *recent,
355                                          guint32        *uid_next,
356                                          guint32        *uid_validity,
357                                          gint           *unseen,
358                                          gboolean        block);
359
360 static void imap_parse_namespace                (IMAPSession    *session,
361                                                  IMAPFolder     *folder);
362 static void imap_get_namespace_by_list          (IMAPSession    *session,
363                                                  IMAPFolder     *folder);
364 static IMAPNameSpace *imap_find_namespace       (IMAPFolder     *folder,
365                                                  const gchar    *path);
366 static gchar imap_get_path_separator            (IMAPFolder     *folder,
367                                                  const gchar    *path);
368 static gchar *imap_get_real_path                (IMAPFolder     *folder,
369                                                  const gchar    *path);
370
371 static gchar *imap_parse_atom           (SockInfo       *sock,
372                                          gchar          *src,
373                                          gchar          *dest,
374                                          gint            dest_len,
375                                          GString        *str);
376 static MsgFlags imap_parse_flags        (const gchar    *flag_str);
377 static MsgInfo *imap_parse_envelope     (SockInfo       *sock,
378                                          FolderItem     *item,
379                                          GString        *line_str);
380
381 static gboolean imap_has_capability     (IMAPSession    *session,
382                                          const gchar    *cap);
383 static void imap_free_capabilities      (IMAPSession    *session);
384
385 /* low-level IMAP4rev1 commands */
386 static gint imap_cmd_authenticate
387                                 (IMAPSession    *session,
388                                  const gchar    *user,
389                                  const gchar    *pass,
390                                  IMAPAuthType    type);
391 static gint imap_cmd_login      (IMAPSession    *session,
392                                  const gchar    *user,
393                                  const gchar    *pass);
394 static gint imap_cmd_logout     (IMAPSession    *session);
395 static gint imap_cmd_noop       (IMAPSession    *session);
396 #if USE_OPENSSL
397 static gint imap_cmd_starttls   (IMAPSession    *session);
398 #endif
399 static gint imap_cmd_namespace  (IMAPSession    *session,
400                                  gchar         **ns_str);
401 static gint imap_cmd_list       (IMAPSession    *session,
402                                  const gchar    *ref,
403                                  const gchar    *mailbox,
404                                  GPtrArray      *argbuf);
405 static gint imap_cmd_do_select  (IMAPSession    *session,
406                                  const gchar    *folder,
407                                  gboolean        examine,
408                                  gint           *exists,
409                                  gint           *recent,
410                                  gint           *unseen,
411                                  guint32        *uid_validity,
412                                  gboolean        block);
413 static gint imap_cmd_select     (IMAPSession    *session,
414                                  const gchar    *folder,
415                                  gint           *exists,
416                                  gint           *recent,
417                                  gint           *unseen,
418                                  guint32        *uid_validity,
419                                  gboolean        block);
420 static gint imap_cmd_examine    (IMAPSession    *session,
421                                  const gchar    *folder,
422                                  gint           *exists,
423                                  gint           *recent,
424                                  gint           *unseen,
425                                  guint32        *uid_validity,
426                                  gboolean        block);
427 static gint imap_cmd_create     (IMAPSession    *sock,
428                                  const gchar    *folder);
429 static gint imap_cmd_rename     (IMAPSession    *sock,
430                                  const gchar    *oldfolder,
431                                  const gchar    *newfolder);
432 static gint imap_cmd_delete     (IMAPSession    *session,
433                                  const gchar    *folder);
434 static gint imap_cmd_envelope   (IMAPSession    *session,
435                                  IMAPSet         set);
436 static gint imap_cmd_fetch      (IMAPSession    *sock,
437                                  guint32         uid,
438                                  const gchar    *filename,
439                                  gboolean        headers,
440                                  gboolean        body);
441 static gint imap_cmd_append     (IMAPSession    *session,
442                                  const gchar    *destfolder,
443                                  const gchar    *file,
444                                  IMAPFlags       flags,
445                                  guint32        *new_uid);
446 static gint imap_cmd_copy       (IMAPSession    *session, 
447                                  const gchar    *seq_set, 
448                                  const gchar    *destfolder,
449                                  GRelation      *uid_mapping);
450 static gint imap_cmd_store      (IMAPSession    *session,
451                                  IMAPSet         set,
452                                  gchar          *sub_cmd);
453 static gint imap_cmd_expunge    (IMAPSession    *session,
454                                  IMAPSet         seq_set);
455 static gint imap_cmd_close      (IMAPSession    *session);
456
457 static gint imap_cmd_ok         (IMAPSession    *session,
458                                  GPtrArray      *argbuf);
459 static gint imap_cmd_ok_block   (IMAPSession    *session,
460                                  GPtrArray      *argbuf);
461 static gint imap_cmd_ok_with_block
462                                 (IMAPSession    *session, 
463                                  GPtrArray      *argbuf, 
464                                  gboolean       block);
465 static void imap_gen_send       (IMAPSession    *session,
466                                  const gchar    *format, ...);
467 static gint imap_gen_recv       (IMAPSession    *session,
468                                  gchar         **ret);
469 static gint imap_gen_recv_block (IMAPSession    *session,
470                                  gchar         **ret);
471 static gint imap_gen_recv_with_block    
472                                 (IMAPSession    *session,
473                                  gchar         **ret,
474                                  gboolean        block);
475
476 /* misc utility functions */
477 static gchar *strchr_cpy                        (const gchar    *src,
478                                                  gchar           ch,
479                                                  gchar          *dest,
480                                                  gint            len);
481 static gchar *get_quoted                        (const gchar    *src,
482                                                  gchar           ch,
483                                                  gchar          *dest,
484                                                  gint            len);
485 static gchar *search_array_contain_str          (GPtrArray      *array,
486                                                  const gchar    *str);
487 static gchar *search_array_str                  (GPtrArray      *array,
488                                                  const gchar    *str);
489 static void imap_path_separator_subst           (gchar          *str,
490                                                  gchar           separator);
491
492 static gchar *imap_utf8_to_modified_utf7        (const gchar    *from);
493 static gchar *imap_modified_utf7_to_utf8        (const gchar    *mutf7_str);
494
495 static GSList *imap_get_seq_set_from_numlist    (MsgNumberList  *msglist);
496 static GSList *imap_get_seq_set_from_msglist    (MsgInfoList    *msglist);
497 static void imap_seq_set_free                   (GSList         *seq_list);
498
499 static gboolean imap_rename_folder_func         (GNode          *node,
500                                                  gpointer        data);
501 static gint imap_get_num_list                   (Folder         *folder,
502                                                  FolderItem     *item,
503                                                  GSList        **list,
504                                                  gboolean       *old_uids_valid);
505 static GSList *imap_get_msginfos                (Folder         *folder,
506                                                  FolderItem     *item,
507                                                  GSList         *msgnum_list);
508 static MsgInfo *imap_get_msginfo                (Folder         *folder,
509                                                  FolderItem     *item,
510                                                  gint            num);
511 static gboolean imap_scan_required              (Folder         *folder,
512                                                  FolderItem     *item);
513 static void imap_change_flags                   (Folder         *folder,
514                                                  FolderItem     *item,
515                                                  MsgInfo        *msginfo,
516                                                  MsgPermFlags    newflags);
517 static gint imap_get_flags                      (Folder         *folder,
518                                                  FolderItem     *item,
519                                                  MsgInfoList    *msglist,
520                                                  GRelation      *msgflags);
521 static gchar *imap_folder_get_path              (Folder         *folder);
522 static gchar *imap_item_get_path                (Folder         *folder,
523                                                  FolderItem     *item);
524 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
525 static GHashTable *flags_set_table = NULL;
526 static GHashTable *flags_unset_table = NULL;
527 typedef struct _hashtable_data {
528         IMAPSession *session;
529         GSList *msglist;
530 } hashtable_data;
531
532 static FolderClass imap_class;
533
534 typedef struct _thread_data {
535         gchar *server;
536         gushort port;
537         gboolean done;
538         SockInfo *sock;
539 #ifdef USE_OPENSSL
540         SSLType ssl_type;
541 #endif
542 } thread_data;
543
544 #ifdef USE_PTHREAD
545 void *imap_getline_thread(void *data)
546 {
547         thread_data *td = (thread_data *)data;
548         gchar *line = NULL;
549         
550         line = sock_getline(td->sock);
551         
552         td->done = TRUE;
553         
554         return (void *)line;
555 }
556 #endif
557
558 /* imap_getline just wraps sock_getline inside a thread, 
559  * performing gtk updates so that the interface isn't frozen.
560  */
561 static gchar *imap_getline(SockInfo *sock)
562 {
563 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
564         thread_data *td = g_new0(thread_data, 1);
565         pthread_t pt;
566         gchar *line;
567         td->sock = sock;
568         td->done = FALSE;
569         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
570                         imap_getline_thread, td) != 0) {
571                 g_free(td);
572                 return sock_getline(sock);
573         }
574         
575         debug_print("+++waiting for imap_getline_thread...\n");
576         while(!td->done) {
577                 /* don't let the interface freeze while waiting */
578                 sylpheed_do_idle();
579         }
580         debug_print("---imap_getline_thread done\n");
581
582         /* get the thread's return value and clean its resources */
583         pthread_join(pt, (void *)&line);
584         g_free(td);
585
586         return line;
587 #else
588         return sock_getline(sock);
589 #endif
590 }
591
592 FolderClass *imap_get_class(void)
593 {
594         if (imap_class.idstr == NULL) {
595                 imap_class.type = F_IMAP;
596                 imap_class.idstr = "imap";
597                 imap_class.uistr = "IMAP4";
598
599                 /* Folder functions */
600                 imap_class.new_folder = imap_folder_new;
601                 imap_class.destroy_folder = imap_folder_destroy;
602                 imap_class.scan_tree = imap_scan_tree;
603                 imap_class.create_tree = imap_create_tree;
604
605                 /* FolderItem functions */
606                 imap_class.item_new = imap_folder_item_new;
607                 imap_class.item_destroy = imap_folder_item_destroy;
608                 imap_class.item_get_path = imap_item_get_path;
609                 imap_class.create_folder = imap_create_folder;
610                 imap_class.rename_folder = imap_rename_folder;
611                 imap_class.remove_folder = imap_remove_folder;
612                 imap_class.close = imap_close;
613                 imap_class.get_num_list = imap_get_num_list;
614                 imap_class.scan_required = imap_scan_required;
615
616                 /* Message functions */
617                 imap_class.get_msginfo = imap_get_msginfo;
618                 imap_class.get_msginfos = imap_get_msginfos;
619                 imap_class.fetch_msg = imap_fetch_msg;
620                 imap_class.fetch_msg_full = imap_fetch_msg_full;
621                 imap_class.add_msg = imap_add_msg;
622                 imap_class.add_msgs = imap_add_msgs;
623                 imap_class.copy_msg = imap_copy_msg;
624                 imap_class.copy_msgs = imap_copy_msgs;
625                 imap_class.remove_msg = imap_remove_msg;
626                 imap_class.remove_msgs = imap_remove_msgs;
627                 imap_class.remove_all_msg = imap_remove_all_msg;
628                 imap_class.is_msg_changed = imap_is_msg_changed;
629                 imap_class.change_flags = imap_change_flags;
630                 imap_class.get_flags = imap_get_flags;
631                 imap_class.set_batch = imap_set_batch;
632 #ifdef USE_PTREAD
633                 pthread_mutex_init(&imap_mutex, NULL);
634 #endif
635         }
636         
637         return &imap_class;
638 }
639
640 static gchar *get_seq_set_from_seq_list(GSList *seq_list)
641 {
642         gchar *val = NULL;
643         gchar *tmp = NULL;
644         GSList *cur = NULL;
645         
646         if (seq_list != NULL) {
647                 cur = seq_list;
648                 val = g_strdup((gchar *)(cur->data));
649                 cur = cur->next;
650                 while (cur != NULL) {
651                         tmp = val;
652                         val = g_strconcat(tmp, ",", (gchar *)(cur->data),
653                                           NULL);
654                         g_free(tmp);
655                         cur = cur->next;
656                 }
657         }
658         return val;
659 }
660
661
662 static Folder *imap_folder_new(const gchar *name, const gchar *path)
663 {
664         Folder *folder;
665
666         folder = (Folder *)g_new0(IMAPFolder, 1);
667         folder->klass = &imap_class;
668         imap_folder_init(folder, name, path);
669
670         return folder;
671 }
672
673 static void imap_folder_destroy(Folder *folder)
674 {
675         gchar *dir;
676
677         dir = imap_folder_get_path(folder);
678         if (is_dir_exist(dir))
679                 remove_dir_recursive(dir);
680         g_free(dir);
681
682         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
683 }
684
685 static void imap_folder_init(Folder *folder, const gchar *name,
686                              const gchar *path)
687 {
688         folder_remote_folder_init((Folder *)folder, name, path);
689 }
690
691 static FolderItem *imap_folder_item_new(Folder *folder)
692 {
693         IMAPFolderItem *item;
694         
695         item = g_new0(IMAPFolderItem, 1);
696         item->lastuid = 0;
697         item->uid_next = 0;
698         item->uid_list = NULL;
699
700         return (FolderItem *)item;
701 }
702
703 static void imap_folder_item_destroy(Folder *folder, FolderItem *_item)
704 {
705         IMAPFolderItem *item = (IMAPFolderItem *)_item;
706
707         g_return_if_fail(item != NULL);
708         g_slist_free(item->uid_list);
709
710         g_free(_item);
711 }
712
713 static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
714 {
715         IMAPFolderItem *item = (IMAPFolderItem *)node->data;
716         
717         item->lastuid = 0;
718         item->uid_next = 0;
719         g_slist_free(item->uid_list);
720         item->uid_list = NULL;
721         
722         return FALSE;
723 }
724
725 static void imap_reset_uid_lists(Folder *folder)
726 {
727         if(folder->node == NULL)
728                 return;
729         
730         /* Destroy all uid lists and rest last uid */
731         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
732 }
733
734 /* Send CAPABILITY, and examine the server's response to see whether this
735  * connection is pre-authenticated or not and build a list of CAPABILITIES. */
736 static gint imap_greeting(IMAPSession *session)
737 {
738         gchar *capstr;
739         GPtrArray *argbuf;
740
741         imap_gen_send(session, "CAPABILITY");
742         
743         argbuf = g_ptr_array_new();
744
745         if (imap_cmd_ok(session, argbuf) != IMAP_SUCCESS ||
746             ((capstr = search_array_str(argbuf, "CAPABILITY ")) == NULL)) {
747                 ptr_array_free_strings(argbuf);
748                 g_ptr_array_free(argbuf, TRUE);
749                 return -1;
750         }
751
752         session->authenticated = search_array_str(argbuf, "PREAUTH") != NULL;
753         
754         capstr += strlen("CAPABILITY ");
755
756         IMAP_SESSION(session)->capability = g_strsplit(capstr, " ", 0);
757         
758         ptr_array_free_strings(argbuf);
759         g_ptr_array_free(argbuf, TRUE);
760
761         if (imap_has_capability(session, "UIDPLUS")) 
762                 session->uidplus = TRUE; 
763
764         return 0;
765 }
766
767 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
768                       IMAPAuthType type)
769 {
770         gint ok;
771
772         if (type == 0 || type == IMAP_AUTH_LOGIN)
773                 ok = imap_cmd_login(session, user, pass);
774         else
775                 ok = imap_cmd_authenticate(session, user, pass, type);
776
777         if (ok == IMAP_SUCCESS)
778                 session->authenticated = TRUE;
779
780         return ok;
781 }
782
783 static IMAPSession *imap_session_get(Folder *folder)
784 {
785         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
786         IMAPSession *session = NULL;
787
788         g_return_val_if_fail(folder != NULL, NULL);
789         g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
790         g_return_val_if_fail(folder->account != NULL, NULL);
791         
792         if (prefs_common.work_offline && !imap_gtk_should_override()) {
793                 return NULL;
794         }
795
796         /* Make sure we have a session */
797         if (rfolder->session != NULL) {
798                 session = IMAP_SESSION(rfolder->session);
799         } else {
800                 imap_reset_uid_lists(folder);
801                 session = imap_session_new(folder->account);
802         }
803         if(session == NULL)
804                 return NULL;
805
806         if (SESSION(session)->sock->state == CONN_DISCONNECTED) {
807                 debug_print("IMAP server disconnected\n");
808                 session_destroy(SESSION(session));
809                 imap_reset_uid_lists(folder);
810                 session = imap_session_new(folder->account);
811         }
812
813         /* Make sure session is authenticated */
814         if (!IMAP_SESSION(session)->authenticated)
815                 imap_session_authenticate(IMAP_SESSION(session), folder->account);
816         if (!IMAP_SESSION(session)->authenticated) {
817                 session_destroy(SESSION(session));
818                 rfolder->session = NULL;
819                 return NULL;
820         }
821
822         /* Make sure we have parsed the IMAP namespace */
823         imap_parse_namespace(IMAP_SESSION(session),
824                              IMAP_FOLDER(folder));
825
826         /* I think the point of this code is to avoid sending a
827          * keepalive if we've used the session recently and therefore
828          * think it's still alive.  Unfortunately, most of the code
829          * does not yet check for errors on the socket, and so if the
830          * connection drops we don't notice until the timeout expires.
831          * A better solution than sending a NOOP every time would be
832          * for every command to be prepared to retry until it is
833          * successfully sent. -- mbp */
834         if (time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) {
835                 /* verify that the session is still alive */
836                 if (imap_cmd_noop(session) != IMAP_SUCCESS) {
837                         /* Check if this is the first try to establish a
838                            connection, if yes we don't try to reconnect */
839                         if (rfolder->session == NULL) {
840                                 log_warning(_("Connecting to %s failed"),
841                                             folder->account->recv_server);
842                                 session_destroy(SESSION(session));
843                                 session = NULL;
844                         } else {
845                                 log_warning(_("IMAP4 connection to %s has been"
846                                             " disconnected. Reconnecting...\n"),
847                                             folder->account->recv_server);
848                                 statusbar_print_all(_("IMAP4 connection to %s has been"
849                                             " disconnected. Reconnecting...\n"),
850                                             folder->account->recv_server);
851                                 session_destroy(SESSION(session));
852                                 /* Clear folders session to make imap_session_get create
853                                    a new session, because of rfolder->session == NULL
854                                    it will not try to reconnect again and so avoid an
855                                    endless loop */
856                                 rfolder->session = NULL;
857                                 session = imap_session_get(folder);
858                                 statusbar_pop_all();
859                         }
860                 }
861         }
862
863         rfolder->session = SESSION(session);
864         
865         return IMAP_SESSION(session);
866 }
867
868 static IMAPSession *imap_session_new(const PrefsAccount *account)
869 {
870         IMAPSession *session;
871         SockInfo *imap_sock;
872         gushort port;
873
874 #ifdef USE_OPENSSL
875         /* FIXME: IMAP over SSL only... */ 
876         SSLType ssl_type;
877
878         port = account->set_imapport ? account->imapport
879                 : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
880         ssl_type = account->ssl_imap;   
881 #else
882         port = account->set_imapport ? account->imapport
883                 : IMAP4_PORT;
884 #endif
885
886         if (account->set_tunnelcmd) {
887                 log_message(_("creating tunneled IMAP4 connection\n"));
888 #if USE_OPENSSL
889                 if ((imap_sock = imap_open_tunnel(account->recv_server, 
890                                                   account->tunnelcmd,
891                                                   ssl_type)) == NULL)
892 #else
893                 if ((imap_sock = imap_open_tunnel(account->recv_server, 
894                                                   account->tunnelcmd)) == NULL)
895 #endif
896                         return NULL;
897         } else {
898                 g_return_val_if_fail(account->recv_server != NULL, NULL);
899
900                 log_message(_("creating IMAP4 connection to %s:%d ...\n"),
901                             account->recv_server, port);
902                 
903 #if USE_OPENSSL
904                 if ((imap_sock = imap_open(account->recv_server, port,
905                                            ssl_type)) == NULL)
906 #else
907                 if ((imap_sock = imap_open(account->recv_server, port)) == NULL)
908 #endif
909                         return NULL;
910         }
911
912         session = g_new0(IMAPSession, 1);
913         session_init(SESSION(session));
914         SESSION(session)->type             = SESSION_IMAP;
915         SESSION(session)->server           = g_strdup(account->recv_server);
916         SESSION(session)->sock             = imap_sock;
917
918         SESSION(session)->destroy          = imap_session_destroy;
919
920         session->capability = NULL;
921
922         session->authenticated = FALSE;
923         session->mbox = NULL;
924         session->cmd_count = 0;
925
926         /* Only need to log in if the connection was not PREAUTH */
927         if (imap_greeting(session) != IMAP_SUCCESS) {
928                 session_destroy(SESSION(session));
929                 return NULL;
930         }
931
932 #if USE_OPENSSL
933         if (account->ssl_imap == SSL_STARTTLS && 
934             imap_has_capability(session, "STARTTLS")) {
935                 gint ok;
936
937                 ok = imap_cmd_starttls(session);
938                 if (ok != IMAP_SUCCESS) {
939                         log_warning(_("Can't start TLS session.\n"));
940                         session_destroy(SESSION(session));
941                         return NULL;
942                 }
943                 if (!ssl_init_socket_with_method(SESSION(session)->sock, 
944                     SSL_METHOD_TLSv1)) {
945                         session_destroy(SESSION(session));
946                         return NULL;
947                 }
948
949                 imap_free_capabilities(session);
950                 session->authenticated = FALSE;
951                 session->uidplus = FALSE;
952                 session->cmd_count = 1;
953
954                 if (imap_greeting(session) != IMAP_SUCCESS) {
955                         session_destroy(SESSION(session));
956                         return NULL;
957                 }               
958         }
959 #endif
960         log_message("IMAP connection is %s-authenticated\n",
961                     (session->authenticated) ? "pre" : "un");
962
963         return session;
964 }
965
966 static void imap_session_authenticate(IMAPSession *session, 
967                                       const PrefsAccount *account)
968 {
969         gchar *pass;
970
971         g_return_if_fail(account->userid != NULL);
972
973         pass = account->passwd;
974         if (!pass) {
975                 gchar *tmp_pass;
976                 tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
977                 if (!tmp_pass)
978                         return;
979                 Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
980                 g_free(tmp_pass);
981         }
982         statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
983                                 account->recv_server);
984         if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
985                 imap_cmd_logout(session);
986                 statusbar_pop_all();
987                 
988                 return;
989         }
990         statusbar_pop_all();
991         session->authenticated = TRUE;
992 }
993
994 static void imap_session_destroy(Session *session)
995 {
996         imap_free_capabilities(IMAP_SESSION(session));
997         g_free(IMAP_SESSION(session)->mbox);
998         sock_close(session->sock);
999         session->sock = NULL;
1000 }
1001
1002 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
1003 {
1004         return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
1005 }
1006
1007 static guint get_size_with_lfs(MsgInfo *info) 
1008 {
1009         FILE *fp = NULL;
1010         guint cnt = 0;
1011         gchar buf[4096];
1012         
1013         if (info == NULL)
1014                 return -1;
1015         
1016         fp = procmsg_open_message(info);
1017         if (!fp)
1018                 return -1;
1019         
1020         while (fgets(buf, sizeof (buf), fp) != NULL) {
1021                 cnt += strlen(buf);
1022                 if (!strstr(buf, "\r") && strstr(buf, "\n"))
1023                         cnt++;
1024         }
1025         
1026         fclose(fp);
1027         return cnt;
1028 }
1029
1030 static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
1031                                   gboolean headers, gboolean body)
1032 {
1033         gchar *path, *filename;
1034         IMAPSession *session;
1035         gint ok;
1036
1037         g_return_val_if_fail(folder != NULL, NULL);
1038         g_return_val_if_fail(item != NULL, NULL);
1039
1040         if (uid == 0)
1041                 return NULL;
1042
1043         path = folder_item_get_path(item);
1044         if (!is_dir_exist(path))
1045                 make_dir_hier(path);
1046         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
1047         g_free(path);
1048
1049         if (is_file_exist(filename)) {
1050                 /* see whether the local file represents the whole message
1051                  * or not. As the IMAP server reports size with \r chars,
1052                  * we have to update the local file (UNIX \n only) size */
1053                 MsgInfo *msginfo = imap_parse_msg(filename, item);
1054                 MsgInfo *cached = msgcache_get_msg(item->cache,uid);
1055                 guint have_size = get_size_with_lfs(msginfo);
1056                 debug_print("message %d has been already %scached (%d/%d).\n", uid,
1057                                 have_size == cached->size ? "fully ":"",
1058                                 have_size, cached? (int)cached->size : -1);
1059                                 
1060                 if (cached && (cached->size == have_size || !body)) {
1061                         procmsg_msginfo_free(cached);
1062                         procmsg_msginfo_free(msginfo);
1063                         return filename;
1064                 } else {
1065                         procmsg_msginfo_free(cached);
1066                         procmsg_msginfo_free(msginfo);
1067                 }
1068         }
1069
1070         session = imap_session_get(folder);
1071         if (!session) {
1072                 g_free(filename);
1073                 return NULL;
1074         }
1075
1076         debug_print("IMAP fetching messages\n");
1077         ok = imap_select(session, IMAP_FOLDER(folder), item->path,
1078                          NULL, NULL, NULL, NULL, FALSE);
1079         if (ok != IMAP_SUCCESS) {
1080                 g_warning("can't select mailbox %s\n", item->path);
1081                 g_free(filename);
1082                 return NULL;
1083         }
1084
1085         debug_print("getting message %d...\n", uid);
1086         ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
1087
1088         if (ok != IMAP_SUCCESS) {
1089                 g_warning("can't fetch message %d\n", uid);
1090                 g_free(filename);
1091                 return NULL;
1092         }
1093
1094         return filename;
1095 }
1096
1097 static gint imap_add_msg(Folder *folder, FolderItem *dest, 
1098                          const gchar *file, MsgFlags *flags)
1099 {
1100         gint ret;
1101         GSList file_list;
1102         MsgFileInfo fileinfo;
1103
1104         g_return_val_if_fail(file != NULL, -1);
1105
1106         fileinfo.msginfo = NULL;
1107         fileinfo.file = (gchar *)file;
1108         fileinfo.flags = flags;
1109         file_list.data = &fileinfo;
1110         file_list.next = NULL;
1111
1112         ret = imap_add_msgs(folder, dest, &file_list, NULL);
1113         return ret;
1114 }
1115
1116 static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
1117                    GRelation *relation)
1118 {
1119         gchar *destdir;
1120         IMAPSession *session;
1121         guint32 last_uid = 0;
1122         GSList *cur;
1123         MsgFileInfo *fileinfo;
1124         gint ok;
1125
1126
1127         g_return_val_if_fail(folder != NULL, -1);
1128         g_return_val_if_fail(dest != NULL, -1);
1129         g_return_val_if_fail(file_list != NULL, -1);
1130         
1131         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
1132
1133         session = imap_session_get(folder);
1134         if (!session) {
1135                 MUTEX_UNLOCK();
1136                 return -1;
1137         }
1138         destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
1139
1140         for (cur = file_list; cur != NULL; cur = cur->next) {
1141                 IMAPFlags iflags = 0;
1142                 guint32 new_uid = 0;
1143
1144                 fileinfo = (MsgFileInfo *)cur->data;
1145
1146                 if (fileinfo->flags) {
1147                         if (MSG_IS_MARKED(*fileinfo->flags))
1148                                 iflags |= IMAP_FLAG_FLAGGED;
1149                         if (MSG_IS_REPLIED(*fileinfo->flags))
1150                                 iflags |= IMAP_FLAG_ANSWERED;
1151                         if (!MSG_IS_UNREAD(*fileinfo->flags))
1152                                 iflags |= IMAP_FLAG_SEEN;
1153                 }
1154
1155                 if (dest->stype == F_OUTBOX ||
1156                     dest->stype == F_QUEUE  ||
1157                     dest->stype == F_DRAFT  ||
1158                     dest->stype == F_TRASH)
1159                         iflags |= IMAP_FLAG_SEEN;
1160
1161                 ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, 
1162                                      &new_uid);
1163
1164                 if (ok != IMAP_SUCCESS) {
1165                         g_warning("can't append message %s\n", fileinfo->file);
1166                         g_free(destdir);
1167                         MUTEX_UNLOCK();
1168                         return -1;
1169                 }
1170
1171                 if (relation != NULL)
1172                         g_relation_insert(relation, fileinfo->msginfo != NULL ? 
1173                                           (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
1174                                           GINT_TO_POINTER(dest->last_num + 1));
1175                 if (last_uid < new_uid)
1176                         last_uid = new_uid;
1177         }
1178
1179         g_free(destdir);
1180
1181         MUTEX_UNLOCK();
1182         return last_uid;
1183 }
1184
1185 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, 
1186                               MsgInfoList *msglist, GRelation *relation)
1187 {
1188         FolderItem *src;
1189         gchar *destdir;
1190         GSList *seq_list, *cur;
1191         MsgInfo *msginfo;
1192         IMAPSession *session;
1193         gint ok = IMAP_SUCCESS;
1194         GRelation *uid_mapping;
1195         gint last_num = 0;
1196         
1197         g_return_val_if_fail(folder != NULL, -1);
1198         g_return_val_if_fail(dest != NULL, -1);
1199         g_return_val_if_fail(msglist != NULL, -1);
1200
1201         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
1202         
1203         session = imap_session_get(folder);
1204         
1205         if (!session) {
1206                 MUTEX_UNLOCK();
1207                 return -1;
1208         }
1209         msginfo = (MsgInfo *)msglist->data;
1210
1211         src = msginfo->folder;
1212         if (src == dest) {
1213                 g_warning("the src folder is identical to the dest.\n");
1214                 MUTEX_UNLOCK();
1215                 return -1;
1216         }
1217
1218         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1219                          NULL, NULL, NULL, NULL, FALSE);
1220         if (ok != IMAP_SUCCESS) {
1221                 MUTEX_UNLOCK();
1222                 return ok;
1223         }
1224
1225         destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
1226         seq_list = imap_get_seq_set_from_msglist(msglist);
1227         uid_mapping = g_relation_new(2);
1228         g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1229         
1230         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
1231                 gchar *seq_set = (gchar *)cur->data;
1232
1233                 debug_print("Copying message %s%c[%s] to %s ...\n",
1234                             src->path, G_DIR_SEPARATOR,
1235                             seq_set, destdir);
1236
1237                 ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
1238                 if (ok != IMAP_SUCCESS) {
1239                         g_relation_destroy(uid_mapping);
1240                         imap_seq_set_free(seq_list);
1241                         return -1;
1242                 }
1243         }
1244
1245         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1246                 MsgInfo *msginfo = (MsgInfo *)cur->data;
1247                 GTuples *tuples;
1248
1249                 tuples = g_relation_select(uid_mapping, 
1250                                            GINT_TO_POINTER(msginfo->msgnum),
1251                                            0);
1252                 if (tuples->len > 0) {
1253                         gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1254                         g_relation_insert(relation, msginfo,
1255                                           GPOINTER_TO_INT(num));
1256                         if (num > last_num)
1257                                 last_num = num;
1258                 } else
1259                         g_relation_insert(relation, msginfo,
1260                                           GPOINTER_TO_INT(0));
1261                 g_tuples_destroy(tuples);
1262         }
1263
1264         g_relation_destroy(uid_mapping);
1265         imap_seq_set_free(seq_list);
1266
1267         g_free(destdir);
1268         
1269         IMAP_FOLDER_ITEM(dest)->lastuid = 0;
1270         IMAP_FOLDER_ITEM(dest)->uid_next = 0;
1271         g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
1272         IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
1273
1274         MUTEX_UNLOCK();
1275
1276         if (ok == IMAP_SUCCESS)
1277                 return last_num;
1278         else
1279                 return -1;
1280 }
1281
1282 static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1283 {
1284         GSList msglist;
1285
1286         g_return_val_if_fail(msginfo != NULL, -1);
1287
1288         msglist.data = msginfo;
1289         msglist.next = NULL;
1290
1291         return imap_copy_msgs(folder, dest, &msglist, NULL);
1292 }
1293
1294 static gint imap_copy_msgs(Folder *folder, FolderItem *dest, 
1295                     MsgInfoList *msglist, GRelation *relation)
1296 {
1297         MsgInfo *msginfo;
1298         GSList *file_list;
1299         gint ret;
1300
1301         g_return_val_if_fail(folder != NULL, -1);
1302         g_return_val_if_fail(dest != NULL, -1);
1303         g_return_val_if_fail(msglist != NULL, -1);
1304
1305         msginfo = (MsgInfo *)msglist->data;
1306         g_return_val_if_fail(msginfo->folder != NULL, -1);
1307
1308         if (folder == msginfo->folder->folder) {
1309                 ret = imap_do_copy_msgs(folder, dest, msglist, relation);
1310                 return ret;
1311         }
1312
1313         file_list = procmsg_get_message_file_list(msglist);
1314         g_return_val_if_fail(file_list != NULL, -1);
1315
1316         ret = imap_add_msgs(folder, dest, file_list, relation);
1317
1318         procmsg_message_file_list_free(file_list);
1319
1320         return ret;
1321 }
1322
1323
1324 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
1325                                 MsgInfoList *msglist, GRelation *relation)
1326 {
1327         gchar *destdir;
1328         GSList *seq_list = NULL, *cur;
1329         MsgInfo *msginfo;
1330         IMAPSession *session;
1331         gint ok = IMAP_SUCCESS;
1332         GRelation *uid_mapping;
1333         
1334         g_return_val_if_fail(folder != NULL, -1);
1335         g_return_val_if_fail(dest != NULL, -1);
1336         g_return_val_if_fail(msglist != NULL, -1);
1337
1338         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
1339
1340         session = imap_session_get(folder);
1341         if (!session) {
1342                 MUTEX_UNLOCK();
1343                 return -1;
1344         }
1345         msginfo = (MsgInfo *)msglist->data;
1346
1347         ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
1348                          NULL, NULL, NULL, NULL, FALSE);
1349         if (ok != IMAP_SUCCESS) {
1350                 MUTEX_UNLOCK();
1351                 return ok;
1352         }
1353
1354         destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
1355         for (cur = msglist; cur; cur = cur->next) {
1356                 msginfo = (MsgInfo *)cur->data;
1357                 seq_list = g_slist_append(seq_list, GINT_TO_POINTER(msginfo->msgnum));
1358         }
1359
1360         uid_mapping = g_relation_new(2);
1361         g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
1362
1363         ok = imap_set_message_flags
1364                 (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
1365                 seq_list, IMAP_FLAG_DELETED, TRUE);
1366         if (ok != IMAP_SUCCESS) {
1367                 log_warning(_("can't set deleted flags\n"));
1368                 MUTEX_UNLOCK();
1369                 return ok;
1370         }
1371         ok = imap_cmd_expunge(session, NULL);
1372         if (ok != IMAP_SUCCESS) {
1373                 log_warning(_("can't expunge\n"));
1374                 MUTEX_UNLOCK();
1375                 return ok;
1376         }
1377         
1378         g_relation_destroy(uid_mapping);
1379         g_slist_free(seq_list);
1380
1381         g_free(destdir);
1382
1383         MUTEX_UNLOCK();
1384         if (ok == IMAP_SUCCESS)
1385                 return 0;
1386         else
1387                 return -1;
1388 }
1389
1390 static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
1391                     MsgInfoList *msglist, GRelation *relation)
1392 {
1393         MsgInfo *msginfo;
1394
1395         g_return_val_if_fail(folder != NULL, -1);
1396         g_return_val_if_fail(dest != NULL, -1);
1397         if (msglist == NULL)
1398                 return 0;
1399
1400         msginfo = (MsgInfo *)msglist->data;
1401         g_return_val_if_fail(msginfo->folder != NULL, -1);
1402
1403         return imap_do_remove_msgs(folder, dest, msglist, relation);
1404 }
1405
1406 static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
1407 {
1408         GSList *list = folder_item_get_msg_list(item);
1409         gint res = imap_remove_msgs(folder, item, list, NULL);
1410         g_slist_free(list);
1411         return res;
1412 }
1413
1414 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
1415                                     MsgInfo *msginfo)
1416 {
1417         /* TODO: properly implement this method */
1418         return FALSE;
1419 }
1420
1421 static gint imap_close(Folder *folder, FolderItem *item)
1422 {
1423         gint ok;
1424         IMAPSession *session;
1425
1426         g_return_val_if_fail(folder != NULL, -1);
1427         g_return_val_if_fail(item != NULL, -1);
1428         g_return_val_if_fail(item->path != NULL, -1);
1429
1430         session = imap_session_get(folder);
1431         if (!session) return -1;
1432
1433         if (session->mbox) {
1434                 if (strcmp2(session->mbox, item->path) != 0) return -1;
1435
1436                 ok = imap_cmd_close(session);
1437                 if (ok != IMAP_SUCCESS)
1438                         log_warning(_("can't close folder\n"));
1439
1440                 g_free(session->mbox);
1441
1442                 session->mbox = NULL;
1443
1444                 return ok;
1445         }
1446
1447         return 0;
1448 }
1449
1450 static gint imap_scan_tree(Folder *folder)
1451 {
1452         FolderItem *item = NULL;
1453         IMAPSession *session;
1454         gchar *root_folder = NULL;
1455
1456         g_return_val_if_fail(folder != NULL, -1);
1457         g_return_val_if_fail(folder->account != NULL, -1);
1458
1459         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
1460
1461         session = imap_session_get(folder);
1462         if (!session) {
1463                 if (!folder->node) {
1464                         folder_tree_destroy(folder);
1465                         item = folder_item_new(folder, folder->name, NULL);
1466                         item->folder = folder;
1467                         folder->node = item->node = g_node_new(item);
1468                 }
1469                 MUTEX_UNLOCK();
1470                 return -1;
1471         }
1472
1473         if (folder->account->imap_dir && *folder->account->imap_dir) {
1474                 gchar *real_path;
1475                 GPtrArray *argbuf;
1476                 gint ok;
1477
1478                 Xstrdup_a(root_folder, folder->account->imap_dir, {MUTEX_UNLOCK();return -1;});
1479                 extract_quote(root_folder, '"');
1480                 subst_char(root_folder,
1481                            imap_get_path_separator(IMAP_FOLDER(folder),
1482                                                    root_folder),
1483                            '/');
1484                 strtailchomp(root_folder, '/');
1485                 real_path = imap_get_real_path
1486                         (IMAP_FOLDER(folder), root_folder);
1487                 debug_print("IMAP root directory: %s\n", real_path);
1488
1489                 /* check if root directory exist */
1490                 argbuf = g_ptr_array_new();
1491                 ok = imap_cmd_list(session, NULL, real_path, argbuf);
1492                 if (ok != IMAP_SUCCESS ||
1493                     search_array_str(argbuf, "LIST ") == NULL) {
1494                         log_warning(_("root folder %s does not exist\n"), real_path);
1495                         g_ptr_array_free(argbuf, TRUE);
1496                         g_free(real_path);
1497
1498                         if (!folder->node) {
1499                                 item = folder_item_new(folder, folder->name, NULL);
1500                                 item->folder = folder;
1501                                 folder->node = item->node = g_node_new(item);
1502                         }
1503                         MUTEX_UNLOCK();
1504                         return -1;
1505                 }
1506                 g_ptr_array_free(argbuf, TRUE);
1507                 g_free(real_path);
1508         }
1509
1510         if (folder->node)
1511                 item = FOLDER_ITEM(folder->node->data);
1512         if (!item || ((item->path || root_folder) &&
1513                       strcmp2(item->path, root_folder) != 0)) {
1514                 folder_tree_destroy(folder);
1515                 item = folder_item_new(folder, folder->name, root_folder);
1516                 item->folder = folder;
1517                 folder->node = item->node = g_node_new(item);
1518         }
1519
1520         imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
1521         imap_create_missing_folders(folder);
1522
1523         MUTEX_UNLOCK();
1524         return 0;
1525 }
1526
1527 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
1528 {
1529         Folder *folder;
1530         IMAPFolder *imapfolder;
1531         FolderItem *new_item;
1532         GSList *item_list, *cur;
1533         GNode *node;
1534         gchar *real_path;
1535         gchar *wildcard_path;
1536         gchar separator;
1537         gchar wildcard[3];
1538
1539         g_return_val_if_fail(item != NULL, -1);
1540         g_return_val_if_fail(item->folder != NULL, -1);
1541         g_return_val_if_fail(item->no_sub == FALSE, -1);
1542
1543         folder = item->folder;
1544         imapfolder = IMAP_FOLDER(folder);
1545
1546         separator = imap_get_path_separator(imapfolder, item->path);
1547
1548         if (folder->ui_func)
1549                 folder->ui_func(folder, item, folder->ui_func_data);
1550
1551         if (item->path) {
1552                 wildcard[0] = separator;
1553                 wildcard[1] = '%';
1554                 wildcard[2] = '\0';
1555                 real_path = imap_get_real_path(imapfolder, item->path);
1556         } else {
1557                 wildcard[0] = '%';
1558                 wildcard[1] = '\0';
1559                 real_path = g_strdup("");
1560         }
1561
1562         Xstrcat_a(wildcard_path, real_path, wildcard,
1563                   {g_free(real_path); return IMAP_ERROR;});
1564         QUOTE_IF_REQUIRED(wildcard_path, wildcard_path);
1565
1566         imap_gen_send(session, "LIST \"\" %s",
1567                       wildcard_path);
1568
1569         strtailchomp(real_path, separator);
1570         item_list = imap_parse_list(imapfolder, session, real_path, NULL);
1571         g_free(real_path);
1572
1573         node = item->node->children;
1574         while (node != NULL) {
1575                 FolderItem *old_item = FOLDER_ITEM(node->data);
1576                 GNode *next = node->next;
1577
1578                 new_item = NULL;
1579                 for (cur = item_list; cur != NULL; cur = cur->next) {
1580                         FolderItem *cur_item = FOLDER_ITEM(cur->data);
1581                         if (!strcmp2(old_item->path, cur_item->path)) {
1582                                 new_item = cur_item;
1583                                 break;
1584                         }
1585                 }
1586                 if (!new_item) {
1587                         debug_print("folder '%s' not found. removing...\n",
1588                                     old_item->path);
1589                         folder_item_remove(old_item);
1590                 } else {
1591                         old_item->no_sub = new_item->no_sub;
1592                         old_item->no_select = new_item->no_select;
1593                         if (old_item->no_sub == TRUE && node->children) {
1594                                 debug_print("folder '%s' doesn't have "
1595                                             "subfolders. removing...\n",
1596                                             old_item->path);
1597                                 folder_item_remove_children(old_item);
1598                         }
1599                 }
1600
1601                 node = next;
1602         }
1603
1604         for (cur = item_list; cur != NULL; cur = cur->next) {
1605                 FolderItem *cur_item = FOLDER_ITEM(cur->data);
1606                 new_item = NULL;
1607                 for (node = item->node->children; node != NULL;
1608                      node = node->next) {
1609                         if (!strcmp2(FOLDER_ITEM(node->data)->path,
1610                                      cur_item->path)) {
1611                                 new_item = FOLDER_ITEM(node->data);
1612                                 folder_item_destroy(cur_item);
1613                                 cur_item = NULL;
1614                                 break;
1615                         }
1616                 }
1617                 if (!new_item) {
1618                         new_item = cur_item;
1619                         debug_print("new folder '%s' found.\n", new_item->path);
1620                         folder_item_append(item, new_item);
1621                 }
1622
1623                 if (!strcmp(new_item->path, "INBOX")) {
1624                         new_item->stype = F_INBOX;
1625                         folder->inbox = new_item;
1626                 } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
1627                         gchar *base;
1628
1629                         base = g_path_get_basename(new_item->path);
1630
1631                         if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
1632                                 new_item->stype = F_OUTBOX;
1633                                 folder->outbox = new_item;
1634                         } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
1635                                 new_item->stype = F_DRAFT;
1636                                 folder->draft = new_item;
1637                         } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
1638                                 new_item->stype = F_QUEUE;
1639                                 folder->queue = new_item;
1640                         } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
1641                                 new_item->stype = F_TRASH;
1642                                 folder->trash = new_item;
1643                         }
1644                         g_free(base);
1645                 }
1646
1647                 if (new_item->no_sub == FALSE)
1648                         imap_scan_tree_recursive(session, new_item);
1649         }
1650
1651         g_slist_free(item_list);
1652
1653         return IMAP_SUCCESS;
1654 }
1655
1656 static GSList *imap_parse_list(IMAPFolder *folder, IMAPSession *session,
1657                                const gchar *real_path, gchar *separator)
1658 {
1659         gchar buf[IMAPBUFSIZE];
1660         gchar flags[256];
1661         gchar separator_str[16];
1662         gchar *p;
1663         gchar *base;
1664         gchar *loc_name, *loc_path;
1665         GSList *item_list = NULL;
1666         GString *str;
1667         FolderItem *new_item;
1668
1669         debug_print("getting list of %s ...\n",
1670                     *real_path ? real_path : "\"\"");
1671
1672         str = g_string_new(NULL);
1673
1674         for (;;) {
1675                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) <= 0) {
1676                         log_warning(_("error occurred while getting LIST.\n"));
1677                         break;
1678                 }
1679                 strretchomp(buf);
1680                 if (buf[0] != '*' || buf[1] != ' ') {
1681                         log_print("IMAP4< %s\n", buf);
1682                         if (sscanf(buf, "%*d %16s", buf) < 1 ||
1683                             strcmp(buf, "OK") != 0)
1684                                 log_warning(_("error occurred while getting LIST.\n"));
1685                                 
1686                         break;
1687                 }
1688                 debug_print("IMAP4< %s\n", buf);
1689
1690                 g_string_assign(str, buf);
1691                 p = str->str + 2;
1692                 if (strncmp(p, "LIST ", 5) != 0) continue;
1693                 p += 5;
1694
1695                 if (*p != '(') continue;
1696                 p++;
1697                 p = strchr_cpy(p, ')', flags, sizeof(flags));
1698                 if (!p) continue;
1699                 while (*p == ' ') p++;
1700
1701                 p = strchr_cpy(p, ' ', separator_str, sizeof(separator_str));
1702                 if (!p) continue;
1703                 extract_quote(separator_str, '"');
1704                 if (!strcmp(separator_str, "NIL"))
1705                         separator_str[0] = '\0';
1706                 if (separator)
1707                         *separator = separator_str[0];
1708
1709                 buf[0] = '\0';
1710                 while (*p == ' ') p++;
1711                 if (*p == '{' || *p == '"')
1712                         p = imap_parse_atom(SESSION(session)->sock, p,
1713                                             buf, sizeof(buf), str);
1714                 else
1715                         strncpy2(buf, p, sizeof(buf));
1716                 strtailchomp(buf, separator_str[0]);
1717                 if (buf[0] == '\0') continue;
1718                 if (!strcmp(buf, real_path)) continue;
1719
1720                 if (separator_str[0] != '\0')
1721                         subst_char(buf, separator_str[0], '/');
1722                 base = g_path_get_basename(buf);
1723                 if (base[0] == '.') continue;
1724
1725                 loc_name = imap_modified_utf7_to_utf8(base);
1726                 loc_path = imap_modified_utf7_to_utf8(buf);
1727                 new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
1728                 if (strcasestr(flags, "\\Noinferiors") != NULL)
1729                         new_item->no_sub = TRUE;
1730                 if (strcmp(buf, "INBOX") != 0 &&
1731                     strcasestr(flags, "\\Noselect") != NULL)
1732                         new_item->no_select = TRUE;
1733
1734                 item_list = g_slist_append(item_list, new_item);
1735
1736                 debug_print("folder '%s' found.\n", loc_path);
1737                 g_free(base);
1738                 g_free(loc_path);
1739                 g_free(loc_name);
1740         }
1741
1742         g_string_free(str, TRUE);
1743
1744         return item_list;
1745 }
1746
1747 static gint imap_create_tree(Folder *folder)
1748 {
1749         g_return_val_if_fail(folder != NULL, -1);
1750         g_return_val_if_fail(folder->node != NULL, -1);
1751         g_return_val_if_fail(folder->node->data != NULL, -1);
1752         g_return_val_if_fail(folder->account != NULL, -1);
1753
1754         imap_scan_tree(folder);
1755         imap_create_missing_folders(folder);
1756
1757         return 0;
1758 }
1759
1760 static void imap_create_missing_folders(Folder *folder)
1761 {
1762         g_return_if_fail(folder != NULL);
1763
1764         if (!folder->inbox)
1765                 folder->inbox = imap_create_special_folder
1766                         (folder, F_INBOX, "INBOX");
1767 #if 0
1768         if (!folder->outbox)
1769                 folder->outbox = imap_create_special_folder
1770                         (folder, F_OUTBOX, "Sent");
1771         if (!folder->draft)
1772                 folder->draft = imap_create_special_folder
1773                         (folder, F_DRAFT, "Drafts");
1774         if (!folder->queue)
1775                 folder->queue = imap_create_special_folder
1776                         (folder, F_QUEUE, "Queue");
1777 #endif
1778         if (!folder->trash)
1779                 folder->trash = imap_create_special_folder
1780                         (folder, F_TRASH, "Trash");
1781 }
1782
1783 static FolderItem *imap_create_special_folder(Folder *folder,
1784                                               SpecialFolderItemType stype,
1785                                               const gchar *name)
1786 {
1787         FolderItem *item;
1788         FolderItem *new_item;
1789
1790         g_return_val_if_fail(folder != NULL, NULL);
1791         g_return_val_if_fail(folder->node != NULL, NULL);
1792         g_return_val_if_fail(folder->node->data != NULL, NULL);
1793         g_return_val_if_fail(folder->account != NULL, NULL);
1794         g_return_val_if_fail(name != NULL, NULL);
1795
1796         item = FOLDER_ITEM(folder->node->data);
1797         new_item = imap_create_folder(folder, item, name);
1798
1799         if (!new_item) {
1800                 g_warning("Can't create '%s'\n", name);
1801                 if (!folder->inbox) return NULL;
1802
1803                 new_item = imap_create_folder(folder, folder->inbox, name);
1804                 if (!new_item)
1805                         g_warning("Can't create '%s' under INBOX\n", name);
1806                 else
1807                         new_item->stype = stype;
1808         } else
1809                 new_item->stype = stype;
1810
1811         return new_item;
1812 }
1813
1814 static gchar *imap_folder_get_path(Folder *folder)
1815 {
1816         gchar *folder_path;
1817
1818         g_return_val_if_fail(folder != NULL, NULL);
1819         g_return_val_if_fail(folder->account != NULL, NULL);
1820
1821         folder_path = g_strconcat(get_imap_cache_dir(),
1822                                   G_DIR_SEPARATOR_S,
1823                                   folder->account->recv_server,
1824                                   G_DIR_SEPARATOR_S,
1825                                   folder->account->userid,
1826                                   NULL);
1827
1828         return folder_path;
1829 }
1830
1831 static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
1832 {
1833         gchar *folder_path, *path;
1834
1835         g_return_val_if_fail(folder != NULL, NULL);
1836         g_return_val_if_fail(item != NULL, NULL);
1837         folder_path = imap_folder_get_path(folder);
1838
1839         g_return_val_if_fail(folder_path != NULL, NULL);
1840         if (folder_path[0] == G_DIR_SEPARATOR) {
1841                 if (item->path)
1842                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
1843                                            item->path, NULL);
1844                 else
1845                         path = g_strdup(folder_path);
1846         } else {
1847                 if (item->path)
1848                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1849                                            folder_path, G_DIR_SEPARATOR_S,
1850                                            item->path, NULL);
1851                 else
1852                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1853                                            folder_path, NULL);
1854         }
1855         g_free(folder_path);
1856
1857         return path;
1858 }
1859
1860 static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
1861                                const gchar *name)
1862 {
1863         gchar *dirpath, *imap_path;
1864         IMAPSession *session;
1865         FolderItem *new_item;
1866         gchar separator;
1867         gchar *new_name;
1868         const gchar *p;
1869         gint ok;
1870
1871         g_return_val_if_fail(folder != NULL, NULL);
1872         g_return_val_if_fail(folder->account != NULL, NULL);
1873         g_return_val_if_fail(parent != NULL, NULL);
1874         g_return_val_if_fail(name != NULL, NULL);
1875
1876         MUTEX_TRYLOCK_OR_RETURN_VAL(NULL);
1877
1878         session = imap_session_get(folder);
1879         if (!session) {
1880                 MUTEX_UNLOCK();
1881                 return NULL;
1882         }
1883
1884         if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0)
1885                 dirpath = g_strdup(name);
1886         else if (parent->path)
1887                 dirpath = g_strconcat(parent->path, "/", name, NULL);
1888         else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
1889                 dirpath = g_strdup(name);
1890         else if (folder->account->imap_dir && *folder->account->imap_dir) {
1891                 gchar *imap_dir;
1892
1893                 Xstrdup_a(imap_dir, folder->account->imap_dir, {MUTEX_UNLOCK();return NULL;});
1894                 strtailchomp(imap_dir, '/');
1895                 dirpath = g_strconcat(imap_dir, "/", name, NULL);
1896         } else
1897                 dirpath = g_strdup(name);
1898
1899         /* keep trailing directory separator to create a folder that contains
1900            sub folder */
1901         imap_path = imap_utf8_to_modified_utf7(dirpath);
1902         strtailchomp(dirpath, '/');
1903         Xstrdup_a(new_name, name, {
1904                 g_free(dirpath);                
1905                 MUTEX_UNLOCK();
1906                 return NULL;});
1907
1908         strtailchomp(new_name, '/');
1909         separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
1910         imap_path_separator_subst(imap_path, separator);
1911         subst_char(new_name, '/', separator);
1912
1913         if (strcmp(name, "INBOX") != 0) {
1914                 GPtrArray *argbuf;
1915                 gint i;
1916                 gboolean exist = FALSE;
1917
1918                 argbuf = g_ptr_array_new();
1919                 ok = imap_cmd_list(session, NULL, imap_path,
1920                                    argbuf);
1921                 if (ok != IMAP_SUCCESS) {
1922                         log_warning(_("can't create mailbox: LIST failed\n"));
1923                         g_free(imap_path);
1924                         g_free(dirpath);
1925                         ptr_array_free_strings(argbuf);
1926                         g_ptr_array_free(argbuf, TRUE);
1927                         MUTEX_UNLOCK();
1928                         return NULL;
1929                 }
1930
1931                 for (i = 0; i < argbuf->len; i++) {
1932                         gchar *str;
1933                         str = g_ptr_array_index(argbuf, i);
1934                         if (!strncmp(str, "LIST ", 5)) {
1935                                 exist = TRUE;
1936                                 break;
1937                         }
1938                 }
1939                 ptr_array_free_strings(argbuf);
1940                 g_ptr_array_free(argbuf, TRUE);
1941
1942                 if (!exist) {
1943                         ok = imap_cmd_create(session, imap_path);
1944                         if (ok != IMAP_SUCCESS) {
1945                                 log_warning(_("can't create mailbox\n"));
1946                                 g_free(imap_path);
1947                                 g_free(dirpath);
1948                                 MUTEX_UNLOCK();
1949                                 return NULL;
1950                         }
1951                 }
1952         }
1953
1954         new_item = folder_item_new(folder, new_name, dirpath);
1955         folder_item_append(parent, new_item);
1956         g_free(imap_path);
1957         g_free(dirpath);
1958
1959         dirpath = folder_item_get_path(new_item);
1960         if (!is_dir_exist(dirpath))
1961                 make_dir_hier(dirpath);
1962         g_free(dirpath);
1963
1964         MUTEX_UNLOCK();
1965         return new_item;
1966 }
1967
1968 static gint imap_rename_folder(Folder *folder, FolderItem *item,
1969                                const gchar *name)
1970 {
1971         gchar *dirpath;
1972         gchar *newpath;
1973         gchar *real_oldpath;
1974         gchar *real_newpath;
1975         gchar *paths[2];
1976         gchar *old_cache_dir;
1977         gchar *new_cache_dir;
1978         IMAPSession *session;
1979         gchar separator;
1980         gint ok;
1981         gint exists, recent, unseen;
1982         guint32 uid_validity;
1983
1984         g_return_val_if_fail(folder != NULL, -1);
1985         g_return_val_if_fail(item != NULL, -1);
1986         g_return_val_if_fail(item->path != NULL, -1);
1987         g_return_val_if_fail(name != NULL, -1);
1988
1989         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
1990
1991         if (strchr(name, imap_get_path_separator(IMAP_FOLDER(folder), item->path)) != NULL) {
1992                 g_warning(_("New folder name must not contain the namespace "
1993                             "path separator"));
1994                 MUTEX_UNLOCK();
1995                 return -1;
1996         }
1997
1998         session = imap_session_get(folder);
1999         if (!session) {
2000                 MUTEX_UNLOCK();
2001                 return -1;
2002         }
2003         real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
2004
2005         g_free(session->mbox);
2006         session->mbox = NULL;
2007         ok = imap_cmd_examine(session, "INBOX",
2008                               &exists, &recent, &unseen, &uid_validity, FALSE);
2009         if (ok != IMAP_SUCCESS) {
2010                 g_free(real_oldpath);
2011                 MUTEX_UNLOCK();
2012                 return -1;
2013         }
2014
2015         separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path);
2016         if (strchr(item->path, G_DIR_SEPARATOR)) {
2017                 dirpath = g_path_get_dirname(item->path);
2018                 newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
2019                 g_free(dirpath);
2020         } else
2021                 newpath = g_strdup(name);
2022
2023         real_newpath = imap_utf8_to_modified_utf7(newpath);
2024         imap_path_separator_subst(real_newpath, separator);
2025
2026         ok = imap_cmd_rename(session, real_oldpath, real_newpath);
2027         if (ok != IMAP_SUCCESS) {
2028                 log_warning(_("can't rename mailbox: %s to %s\n"),
2029                             real_oldpath, real_newpath);
2030                 g_free(real_oldpath);
2031                 g_free(newpath);
2032                 g_free(real_newpath);
2033                 MUTEX_UNLOCK();
2034                 return -1;
2035         }
2036
2037         g_free(item->name);
2038         item->name = g_strdup(name);
2039
2040         old_cache_dir = folder_item_get_path(item);
2041
2042         paths[0] = g_strdup(item->path);
2043         paths[1] = newpath;
2044         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2045                         imap_rename_folder_func, paths);
2046
2047         if (is_dir_exist(old_cache_dir)) {
2048                 new_cache_dir = folder_item_get_path(item);
2049                 if (rename(old_cache_dir, new_cache_dir) < 0) {
2050                         FILE_OP_ERROR(old_cache_dir, "rename");
2051                 }
2052                 g_free(new_cache_dir);
2053         }
2054
2055         g_free(old_cache_dir);
2056         g_free(paths[0]);
2057         g_free(newpath);
2058         g_free(real_oldpath);
2059         g_free(real_newpath);
2060
2061         MUTEX_UNLOCK();
2062         return 0;
2063 }
2064
2065 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
2066 {
2067         gint ok;
2068         IMAPSession *session;
2069         gchar *path;
2070         gchar *cache_dir;
2071         gint exists, recent, unseen;
2072         guint32 uid_validity;
2073
2074         g_return_val_if_fail(folder != NULL, -1);
2075         g_return_val_if_fail(item != NULL, -1);
2076         g_return_val_if_fail(item->path != NULL, -1);
2077
2078         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
2079
2080         session = imap_session_get(folder);
2081         if (!session) {
2082                 MUTEX_UNLOCK();
2083                 return -1;
2084         }
2085         path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
2086
2087         ok = imap_cmd_examine(session, "INBOX",
2088                               &exists, &recent, &unseen, &uid_validity, FALSE);
2089         if (ok != IMAP_SUCCESS) {
2090                 g_free(path);
2091                 MUTEX_UNLOCK();
2092                 return -1;
2093         }
2094
2095         ok = imap_cmd_delete(session, path);
2096         if (ok != IMAP_SUCCESS) {
2097                 log_warning(_("can't delete mailbox\n"));
2098                 g_free(path);
2099                 MUTEX_UNLOCK();
2100                 return -1;
2101         }
2102
2103         g_free(path);
2104         cache_dir = folder_item_get_path(item);
2105         if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
2106                 g_warning("can't remove directory '%s'\n", cache_dir);
2107         g_free(cache_dir);
2108         folder_item_remove(item);
2109
2110         MUTEX_UNLOCK();
2111         return 0;
2112 }
2113
2114 static gint imap_remove_folder(Folder *folder, FolderItem *item)
2115 {
2116         GNode *node, *next;
2117
2118         g_return_val_if_fail(item != NULL, -1);
2119         g_return_val_if_fail(item->folder != NULL, -1);
2120         g_return_val_if_fail(item->node != NULL, -1);
2121
2122         node = item->node->children;
2123         while (node != NULL) {
2124                 next = node->next;
2125                 if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
2126                         return -1;
2127                 node = next;
2128         }
2129         debug_print("IMAP removing %s\n", item->path);
2130
2131         if (imap_remove_all_msg(folder, item) < 0)
2132                 return -1;
2133         return imap_remove_folder_real(folder, item);
2134 }
2135
2136 typedef struct _uncached_data {
2137         IMAPSession *session;
2138         FolderItem *item;
2139         MsgNumberList *numlist;
2140         guint cur;
2141         guint total;
2142         gboolean done;
2143 } uncached_data;
2144
2145 static void *imap_get_uncached_messages_thread(void *data)
2146 {
2147         uncached_data *stuff = (uncached_data *)data;
2148         IMAPSession *session = stuff->session;
2149         FolderItem *item = stuff->item;
2150         MsgNumberList *numlist = stuff->numlist;
2151
2152         gchar *tmp;
2153         GSList *newlist = NULL;
2154         GSList *llast = NULL;
2155         GString *str = NULL;
2156         MsgInfo *msginfo;
2157         GSList *seq_list, *cur;
2158         IMAPSet imapset;
2159         
2160         stuff->total = g_slist_length(numlist);
2161         stuff->cur = 0;
2162
2163         if (session == NULL || item == NULL || item->folder == NULL
2164             || FOLDER_CLASS(item->folder) != &imap_class) {
2165                 stuff->done = TRUE;
2166                 return NULL;
2167         }
2168
2169         seq_list = imap_get_seq_set_from_numlist(numlist);
2170         for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
2171                 imapset = cur->data;
2172                 
2173                 if (!imapset || strlen(imapset) == 0)
2174                         continue;
2175
2176                 if (imap_cmd_envelope(session, imapset)
2177                     != IMAP_SUCCESS) {
2178                         log_warning(_("can't get envelope\n"));
2179                         continue;
2180                 }
2181
2182                 str = g_string_new(NULL);
2183
2184                 for (;;) {
2185                         if ((tmp =sock_getline(SESSION(session)->sock)) == NULL) {
2186                                 log_warning(_("error occurred while getting envelope.\n"));
2187                                 g_string_free(str, TRUE);
2188                                 str = NULL;
2189                                 break;
2190                         }
2191                         strretchomp(tmp);
2192                         if (tmp[0] != '*' || tmp[1] != ' ') {
2193                                 log_print("IMAP4< %s\n", tmp);
2194                                 g_free(tmp);
2195                                 break;
2196                         }
2197                         if (strstr(tmp, "FETCH") == NULL) {
2198                                 log_print("IMAP4< %s\n", tmp);
2199                                 g_free(tmp);
2200                                 continue;
2201                         }
2202                         log_print("IMAP4< %s\n", tmp);
2203                         g_string_assign(str, tmp);
2204                         g_free(tmp);
2205                         
2206                         stuff->cur++;
2207
2208                         msginfo = imap_parse_envelope
2209                                 (SESSION(session)->sock, item, str);
2210                         if (!msginfo) {
2211                                 log_warning(_("can't parse envelope: %s\n"), str->str);
2212                                 continue;
2213                         }
2214                         if (item->stype == F_QUEUE) {
2215                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED);
2216                         } else if (item->stype == F_DRAFT) {
2217                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT);
2218                         }
2219
2220                         msginfo->folder = item;
2221
2222                         if (!newlist)
2223                                 llast = newlist = g_slist_append(newlist, msginfo);
2224                         else {
2225                                 llast = g_slist_append(llast, msginfo);
2226                                 llast = llast->next;
2227                         }
2228                 }
2229                 if (str)
2230                         g_string_free(str, TRUE);
2231         }
2232         imap_seq_set_free(seq_list);
2233         
2234         session_set_access_time(SESSION(session));
2235         stuff->done = TRUE;
2236
2237         return newlist;
2238 }
2239
2240 static GSList *imap_get_uncached_messages(IMAPSession *session,
2241                                         FolderItem *item,
2242                                         MsgNumberList *numlist)
2243 {
2244         uncached_data *data = g_new0(uncached_data, 1);
2245         GSList *result = NULL;
2246         gint last_cur = 0;
2247 #ifdef USE_PTHREAD
2248         pthread_t pt;
2249 #endif
2250         data->done = FALSE;
2251         data->session = session;
2252         data->item = item;
2253         data->numlist = numlist;
2254         data->cur = 0;
2255         data->total = 0;
2256         
2257         if (prefs_common.work_offline && !imap_gtk_should_override()) {
2258                 g_free(data);
2259                 return NULL;
2260         }
2261
2262 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
2263         MUTEX_TRYLOCK_OR_RETURN_VAL(NULL);
2264
2265         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
2266                         imap_get_uncached_messages_thread, data) != 0) {
2267                 result = (GSList *)imap_get_uncached_messages_thread(data);
2268                 g_free(data);
2269                 MUTEX_UNLOCK();
2270                 return result;
2271         }
2272         debug_print("+++waiting for imap_get_uncached_messages_thread...\n");
2273         statusbar_print_all(_("IMAP4 Fetching uncached short headers..."));
2274         while(!data->done) {
2275                 /* don't let the interface freeze while waiting */
2276                 sylpheed_do_idle();
2277                 if (data->total != 0 && last_cur != data->cur && data->cur % 10 == 0) {
2278                         gchar buf[32];
2279                         g_snprintf(buf, sizeof(buf), "%d / %d",
2280                                    data->cur, data->total);
2281                         gtk_progress_bar_set_text
2282                                 (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
2283                         gtk_progress_bar_set_fraction
2284                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
2285                          (gfloat)data->cur / (gfloat)data->total);
2286                         last_cur = data->cur;
2287                 }
2288         }
2289         gtk_progress_bar_set_fraction
2290                 (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0);
2291         gtk_progress_bar_set_text
2292                 (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
2293         statusbar_pop_all();
2294
2295         debug_print("---imap_get_uncached_messages_thread done\n");
2296
2297         /* get the thread's return value and clean its resources */
2298         pthread_join(pt, (void *)&result);
2299         MUTEX_UNLOCK();
2300 #else
2301         result = (GSList *)imap_get_uncached_messages_thread(data);
2302 #endif
2303         g_free(data);
2304         return result;
2305 }
2306
2307 static void imap_delete_all_cached_messages(FolderItem *item)
2308 {
2309         gchar *dir;
2310
2311         g_return_if_fail(item != NULL);
2312         g_return_if_fail(item->folder != NULL);
2313         g_return_if_fail(FOLDER_CLASS(item->folder) == &imap_class);
2314
2315         debug_print("Deleting all cached messages...\n");
2316
2317         dir = folder_item_get_path(item);
2318         if (is_dir_exist(dir))
2319                 remove_all_numbered_files(dir);
2320         g_free(dir);
2321
2322         debug_print("done.\n");
2323 }
2324
2325 #if USE_OPENSSL
2326 static SockInfo *imap_open_tunnel(const gchar *server,
2327                            const gchar *tunnelcmd,
2328                            SSLType ssl_type)
2329 #else
2330 static SockInfo *imap_open_tunnel(const gchar *server,
2331                            const gchar *tunnelcmd)
2332 #endif
2333 {
2334         SockInfo *sock;
2335
2336         if ((sock = sock_connect_cmd(server, tunnelcmd)) == NULL) {
2337                 log_warning(_("Can't establish IMAP4 session with: %s\n"),
2338                             server);
2339                 return NULL;
2340         }
2341 #if USE_OPENSSL
2342         return imap_init_sock(sock, ssl_type);
2343 #else
2344         return imap_init_sock(sock);
2345 #endif
2346 }
2347
2348 void *imap_open_thread(void *data)
2349 {
2350         SockInfo *sock = NULL;
2351         thread_data *td = (thread_data *)data;
2352         if ((sock = sock_connect(td->server, td->port)) == NULL) {
2353                 log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
2354                             td->server, td->port);
2355                 td->done = TRUE;
2356                 return NULL;
2357         }
2358
2359         td->done = TRUE;
2360         return sock;
2361 }
2362
2363 #if USE_OPENSSL
2364 static SockInfo *imap_open(const gchar *server, gushort port,
2365                            SSLType ssl_type)
2366 #else
2367 static SockInfo *imap_open(const gchar *server, gushort port)
2368 #endif
2369 {
2370         thread_data *td = g_new0(thread_data, 1);
2371 #if USE_PTHREAD
2372         pthread_t pt;
2373 #endif
2374         SockInfo *sock = NULL;
2375
2376 #if USE_OPENSSL
2377         td->ssl_type = ssl_type;
2378 #endif
2379         td->server = g_strdup(server);
2380         td->port = port;
2381         td->done = FALSE;
2382
2383         statusbar_print_all(_("Connecting to IMAP4 server: %s..."), server);
2384
2385 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
2386         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
2387                         imap_open_thread, td) != 0) {
2388                 statusbar_pop_all();
2389                 sock = imap_open_thread(td);
2390         } else {        
2391                 debug_print("+++waiting for imap_open_thread...\n");
2392                 while(!td->done) {
2393                         /* don't let the interface freeze while waiting */
2394                         sylpheed_do_idle();
2395                 }
2396
2397                 /* get the thread's return value and clean its resources */
2398                 pthread_join(pt, (void *)&sock);
2399         }
2400 #else
2401         sock = imap_open_thread(td);
2402 #endif
2403 #if USE_OPENSSL
2404         if (sock && td->ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
2405                 log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
2406                             td->server, td->port);
2407                 sock_close(sock);
2408                 sock = NULL;
2409                 td->done = TRUE;
2410         }
2411 #endif
2412         g_free(td->server);
2413         g_free(td);
2414
2415         debug_print("---imap_open_thread returned %p\n", sock);
2416         statusbar_pop_all();
2417
2418         if(!sock && !prefs_common.no_recv_err_panel) {
2419                 alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
2420                                  server, port);
2421         }
2422
2423         return sock;
2424 }
2425
2426 #if USE_OPENSSL
2427 static SockInfo *imap_init_sock(SockInfo *sock, SSLType ssl_type)
2428 #else
2429 static SockInfo *imap_init_sock(SockInfo *sock)
2430 #endif
2431 {
2432
2433         return sock;
2434 }
2435
2436 static GList *imap_parse_namespace_str(gchar *str)
2437 {
2438         guchar *p = str;
2439         gchar *name;
2440         gchar *separator;
2441         IMAPNameSpace *namespace;
2442         GList *ns_list = NULL;
2443
2444         while (*p != '\0') {
2445                 /* parse ("#foo" "/") */
2446
2447                 while (*p && *p != '(') p++;
2448                 if (*p == '\0') break;
2449                 p++;
2450
2451                 while (*p && *p != '"') p++;
2452                 if (*p == '\0') break;
2453                 p++;
2454                 name = p;
2455
2456                 while (*p && *p != '"') p++;
2457                 if (*p == '\0') break;
2458                 *p = '\0';
2459                 p++;
2460
2461                 while (*p && isspace(*p)) p++;
2462                 if (*p == '\0') break;
2463                 if (strncmp(p, "NIL", 3) == 0)
2464                         separator = NULL;
2465                 else if (*p == '"') {
2466                         p++;
2467                         separator = p;
2468                         while (*p && *p != '"') p++;
2469                         if (*p == '\0') break;
2470                         *p = '\0';
2471                         p++;
2472                 } else break;
2473
2474                 while (*p && *p != ')') p++;
2475                 if (*p == '\0') break;
2476                 p++;
2477
2478                 namespace = g_new(IMAPNameSpace, 1);
2479                 namespace->name = g_strdup(name);
2480                 namespace->separator = separator ? separator[0] : '\0';
2481                 ns_list = g_list_append(ns_list, namespace);
2482         }
2483
2484         return ns_list;
2485 }
2486
2487 static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
2488 {
2489         gchar *ns_str;
2490         gchar **str_array;
2491
2492         g_return_if_fail(session != NULL);
2493         g_return_if_fail(folder != NULL);
2494
2495         if (folder->ns_personal != NULL ||
2496             folder->ns_others   != NULL ||
2497             folder->ns_shared   != NULL)
2498                 return;
2499
2500         if (!imap_has_capability(session, "NAMESPACE")) {
2501                 imap_get_namespace_by_list(session, folder);
2502                 return;
2503         }
2504         
2505         if (imap_cmd_namespace(session, &ns_str)
2506             != IMAP_SUCCESS) {
2507                 log_warning(_("can't get namespace\n"));
2508                 return;
2509         }
2510
2511         str_array = strsplit_parenthesis(ns_str, '(', ')', 3);
2512         if (str_array == NULL) {
2513                 g_free(ns_str);
2514                 imap_get_namespace_by_list(session, folder);
2515                 return;
2516         }
2517         if (str_array[0])
2518                 folder->ns_personal = imap_parse_namespace_str(str_array[0]);
2519         if (str_array[0] && str_array[1])
2520                 folder->ns_others = imap_parse_namespace_str(str_array[1]);
2521         if (str_array[0] && str_array[1] && str_array[2])
2522                 folder->ns_shared = imap_parse_namespace_str(str_array[2]);
2523         g_strfreev(str_array);
2524         g_free(ns_str);
2525 }
2526
2527 static void imap_get_namespace_by_list(IMAPSession *session, IMAPFolder *folder)
2528 {
2529         GSList *item_list, *cur;
2530         gchar separator = '\0';
2531         IMAPNameSpace *namespace;
2532
2533         g_return_if_fail(session != NULL);
2534         g_return_if_fail(folder != NULL);
2535
2536         if (folder->ns_personal != NULL ||
2537             folder->ns_others   != NULL ||
2538             folder->ns_shared   != NULL)
2539                 return;
2540
2541         imap_gen_send(session, "LIST \"\" \"\"");
2542         item_list = imap_parse_list(folder, session, "", &separator);
2543         for (cur = item_list; cur != NULL; cur = cur->next)
2544                 folder_item_destroy(FOLDER_ITEM(cur->data));
2545         g_slist_free(item_list);
2546
2547         namespace = g_new(IMAPNameSpace, 1);
2548         namespace->name = g_strdup("");
2549         namespace->separator = separator;
2550         folder->ns_personal = g_list_append(NULL, namespace);
2551 }
2552
2553 static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
2554                                                     const gchar *path)
2555 {
2556         IMAPNameSpace *namespace = NULL;
2557         gchar *tmp_path, *name;
2558
2559         if (!path) path = "";
2560
2561         for (; ns_list != NULL; ns_list = ns_list->next) {
2562                 IMAPNameSpace *tmp_ns = ns_list->data;
2563
2564                 Xstrcat_a(tmp_path, path, "/", return namespace);
2565                 Xstrdup_a(name, tmp_ns->name, return namespace);
2566                 if (tmp_ns->separator && tmp_ns->separator != '/') {
2567                         subst_char(tmp_path, tmp_ns->separator, '/');
2568                         subst_char(name, tmp_ns->separator, '/');
2569                 }
2570                 if (strncmp(tmp_path, name, strlen(name)) == 0)
2571                         namespace = tmp_ns;
2572         }
2573
2574         return namespace;
2575 }
2576
2577 static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
2578                                           const gchar *path)
2579 {
2580         IMAPNameSpace *namespace;
2581
2582         g_return_val_if_fail(folder != NULL, NULL);
2583
2584         namespace = imap_find_namespace_from_list(folder->ns_personal, path);
2585         if (namespace) return namespace;
2586         namespace = imap_find_namespace_from_list(folder->ns_others, path);
2587         if (namespace) return namespace;
2588         namespace = imap_find_namespace_from_list(folder->ns_shared, path);
2589         if (namespace) return namespace;
2590
2591         return NULL;
2592 }
2593
2594 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
2595 {
2596         IMAPNameSpace *namespace;
2597         gchar separator = '/';
2598
2599         namespace = imap_find_namespace(folder, path);
2600         if (namespace && namespace->separator)
2601                 separator = namespace->separator;
2602
2603         return separator;
2604 }
2605
2606 static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
2607 {
2608         gchar *real_path;
2609         gchar separator;
2610
2611         g_return_val_if_fail(folder != NULL, NULL);
2612         g_return_val_if_fail(path != NULL, NULL);
2613
2614         real_path = imap_utf8_to_modified_utf7(path);
2615         separator = imap_get_path_separator(folder, path);
2616         imap_path_separator_subst(real_path, separator);
2617
2618         return real_path;
2619 }
2620
2621 static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
2622                               gchar *dest, gint dest_len, GString *str)
2623 {
2624         gchar *cur_pos = src;
2625         gchar *nextline;
2626
2627         g_return_val_if_fail(str != NULL, cur_pos);
2628
2629         /* read the next line if the current response buffer is empty */
2630         while (isspace(*(guchar *)cur_pos)) cur_pos++;
2631         while (*cur_pos == '\0') {
2632                 if ((nextline = imap_getline(sock)) == NULL)
2633                         return cur_pos;
2634                 g_string_assign(str, nextline);
2635                 cur_pos = str->str;
2636                 strretchomp(nextline);
2637                 /* log_print("IMAP4< %s\n", nextline); */
2638                 debug_print("IMAP4< %s\n", nextline);
2639                 g_free(nextline);
2640
2641                 while (isspace(*(guchar *)cur_pos)) cur_pos++;
2642         }
2643
2644         if (!strncmp(cur_pos, "NIL", 3)) {
2645                 *dest = '\0';
2646                 cur_pos += 3;
2647         } else if (*cur_pos == '\"') {
2648                 gchar *p;
2649
2650                 p = get_quoted(cur_pos, '\"', dest, dest_len);
2651                 cur_pos = p ? p : cur_pos + 2;
2652         } else if (*cur_pos == '{') {
2653                 gchar buf[32];
2654                 gint len;
2655                 gint line_len = 0;
2656
2657                 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
2658                 len = atoi(buf);
2659                 g_return_val_if_fail(len >= 0, cur_pos);
2660
2661                 g_string_truncate(str, 0);
2662                 cur_pos = str->str;
2663
2664                 do {
2665                         if ((nextline = imap_getline(sock)) == NULL)
2666                                 return cur_pos;
2667                         line_len += strlen(nextline);
2668                         g_string_append(str, nextline);
2669                         cur_pos = str->str;
2670                         strretchomp(nextline);
2671                         /* log_print("IMAP4< %s\n", nextline); */
2672                         debug_print("IMAP4< %s\n", nextline);
2673                         g_free(nextline);
2674                 } while (line_len < len);
2675
2676                 memcpy(dest, cur_pos, MIN(len, dest_len - 1));
2677                 dest[MIN(len, dest_len - 1)] = '\0';
2678                 cur_pos += len;
2679         }
2680
2681         return cur_pos;
2682 }
2683
2684 static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
2685                               GString *str)
2686 {
2687         gchar *nextline;
2688         gchar buf[32];
2689         gint len;
2690         gint block_len = 0;
2691
2692         *headers = NULL;
2693
2694         g_return_val_if_fail(str != NULL, cur_pos);
2695
2696         while (isspace(*(guchar *)cur_pos)) cur_pos++;
2697
2698         g_return_val_if_fail(*cur_pos == '{', cur_pos);
2699
2700         cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
2701         len = atoi(buf);
2702         g_return_val_if_fail(len >= 0, cur_pos);
2703
2704         g_string_truncate(str, 0);
2705         cur_pos = str->str;
2706
2707         do {
2708                 if ((nextline = sock_getline(sock)) == NULL) {
2709                         *headers = NULL;
2710                         return cur_pos;
2711                 }
2712                 block_len += strlen(nextline);
2713                 g_string_append(str, nextline);
2714                 cur_pos = str->str;
2715                 strretchomp(nextline);
2716                 /* debug_print("IMAP4< %s\n", nextline); */
2717                 g_free(nextline);
2718         } while (block_len < len);
2719
2720         debug_print("IMAP4< [contents of BODY.PEEK[HEADER_FIELDS (...)]\n");
2721
2722         *headers = g_strndup(cur_pos, len);
2723         cur_pos += len;
2724
2725         while (isspace(*(guchar *)cur_pos)) cur_pos++;
2726         while (*cur_pos == '\0') {
2727                 if ((nextline = sock_getline(sock)) == NULL)
2728                         return cur_pos;
2729                 g_string_assign(str, nextline);
2730                 cur_pos = str->str;
2731                 strretchomp(nextline);
2732                 debug_print("IMAP4< %s\n", nextline);
2733                 g_free(nextline);
2734
2735                 while (isspace(*(guchar *)cur_pos)) cur_pos++;
2736         }
2737
2738         return cur_pos;
2739 }
2740
2741 static MsgFlags imap_parse_flags(const gchar *flag_str)  
2742 {
2743         const gchar *p = flag_str;
2744         MsgFlags flags = {0, 0};
2745
2746         flags.perm_flags = MSG_UNREAD;
2747
2748         while ((p = strchr(p, '\\')) != NULL) {
2749                 p++;
2750
2751                 if (g_ascii_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
2752                         MSG_SET_PERM_FLAGS(flags, MSG_NEW);
2753                 } else if (g_ascii_strncasecmp(p, "Seen", 4) == 0) {
2754                         MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
2755                 } else if (g_ascii_strncasecmp(p, "Deleted", 7) == 0) {
2756                         MSG_SET_PERM_FLAGS(flags, MSG_DELETED);
2757                 } else if (g_ascii_strncasecmp(p, "Flagged", 7) == 0) {
2758                         MSG_SET_PERM_FLAGS(flags, MSG_MARKED);
2759                 } else if (g_ascii_strncasecmp(p, "Answered", 8) == 0) {
2760                         MSG_SET_PERM_FLAGS(flags, MSG_REPLIED);
2761                 }
2762         }
2763
2764         return flags;
2765 }
2766
2767 static MsgInfo *imap_parse_envelope(SockInfo *sock, FolderItem *item,
2768                                     GString *line_str)
2769 {
2770         gchar buf[IMAPBUFSIZE];
2771         MsgInfo *msginfo = NULL;
2772         gchar *cur_pos;
2773         gint msgnum;
2774         guint32 uid = 0;
2775         size_t size = 0;
2776         MsgFlags flags = {0, 0}, imap_flags = {0, 0};
2777
2778         g_return_val_if_fail(line_str != NULL, NULL);
2779         g_return_val_if_fail(line_str->str[0] == '*' &&
2780                              line_str->str[1] == ' ', NULL);
2781
2782         MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
2783         if (item->stype == F_QUEUE) {
2784                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
2785         } else if (item->stype == F_DRAFT) {
2786                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
2787         }
2788
2789         cur_pos = line_str->str + 2;
2790
2791 #define PARSE_ONE_ELEMENT(ch)                                   \
2792 {                                                               \
2793         cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf));    \
2794         if (cur_pos == NULL) {                                  \
2795                 g_warning("cur_pos == NULL\n");                 \
2796                 procmsg_msginfo_free(msginfo);                  \
2797                 return NULL;                                    \
2798         }                                                       \
2799 }
2800
2801         PARSE_ONE_ELEMENT(' ');
2802         msgnum = atoi(buf);
2803
2804         PARSE_ONE_ELEMENT(' ');
2805         g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
2806
2807         g_return_val_if_fail(*cur_pos == '(', NULL);
2808         cur_pos++;
2809
2810         while (*cur_pos != '\0' && *cur_pos != ')') {
2811                 while (*cur_pos == ' ') cur_pos++;
2812
2813                 if (!strncmp(cur_pos, "UID ", 4)) {
2814                         cur_pos += 4;
2815                         uid = strtoul(cur_pos, &cur_pos, 10);
2816                 } else if (!strncmp(cur_pos, "FLAGS ", 6)) {
2817                         cur_pos += 6;
2818                         if (*cur_pos != '(') {
2819                                 g_warning("*cur_pos != '('\n");
2820                                 procmsg_msginfo_free(msginfo);
2821                                 return NULL;
2822                         }
2823                         cur_pos++;
2824                         PARSE_ONE_ELEMENT(')');
2825                         imap_flags = imap_parse_flags(buf);
2826                 } else if (!strncmp(cur_pos, "RFC822.SIZE ", 12)) {
2827                         cur_pos += 12;
2828                         size = strtol(cur_pos, &cur_pos, 10);
2829                 } else if (!strncmp(cur_pos, "BODY[HEADER.FIELDS ", 19)) {
2830                         gchar *headers;
2831
2832                         cur_pos += 19;
2833                         if (*cur_pos != '(') {
2834                                 g_warning("*cur_pos != '('\n");
2835                                 procmsg_msginfo_free(msginfo);
2836                                 return NULL;
2837                         }
2838                         cur_pos++;
2839                         PARSE_ONE_ELEMENT(')');
2840                         if (*cur_pos != ']') {
2841                                 g_warning("*cur_pos != ']'\n");
2842                                 procmsg_msginfo_free(msginfo);
2843                                 return NULL;
2844                         }
2845                         cur_pos++;
2846                         cur_pos = imap_get_header(sock, cur_pos, &headers,
2847                                                   line_str);
2848                         msginfo = procheader_parse_str(headers, flags, FALSE, FALSE);
2849                         g_free(headers);
2850                 } else {
2851                         g_warning("invalid FETCH response: %s\n", cur_pos);
2852                         break;
2853                 }
2854         }
2855
2856         if (msginfo) {
2857                 msginfo->msgnum = uid;
2858                 msginfo->size = size;
2859                 msginfo->flags.tmp_flags |= imap_flags.tmp_flags;
2860                 msginfo->flags.perm_flags = imap_flags.perm_flags;
2861         }
2862
2863         return msginfo;
2864 }
2865
2866 static gchar *imap_get_flag_str(IMAPFlags flags)
2867 {
2868         GString *str;
2869         gchar *ret;
2870
2871         str = g_string_new(NULL);
2872
2873         if (IMAP_IS_SEEN(flags))        g_string_append(str, "\\Seen ");
2874         if (IMAP_IS_ANSWERED(flags))    g_string_append(str, "\\Answered ");
2875         if (IMAP_IS_FLAGGED(flags))     g_string_append(str, "\\Flagged ");
2876         if (IMAP_IS_DELETED(flags))     g_string_append(str, "\\Deleted ");
2877         if (IMAP_IS_DRAFT(flags))       g_string_append(str, "\\Draft");
2878
2879         if (str->len > 0 && str->str[str->len - 1] == ' ')
2880                 g_string_truncate(str, str->len - 1);
2881
2882         ret = str->str;
2883         g_string_free(str, FALSE);
2884
2885         return ret;
2886 }
2887
2888 static gint imap_set_message_flags(IMAPSession *session,
2889                                    MsgNumberList *numlist,
2890                                    IMAPFlags flags,
2891                                    gboolean is_set)
2892 {
2893         gchar *cmd;
2894         gchar *flag_str;
2895         gint ok = 0;
2896         GSList *seq_list;
2897         IMAPSet imapset;
2898
2899         flag_str = imap_get_flag_str(flags);
2900         cmd = g_strconcat(is_set ? "+FLAGS.SILENT (" : "-FLAGS.SILENT (",
2901                           flag_str, ")", NULL);
2902         g_free(flag_str);
2903
2904         seq_list = imap_get_seq_set_from_numlist(numlist);
2905         imapset = get_seq_set_from_seq_list(seq_list);
2906
2907         ok = imap_cmd_store(session, imapset, cmd);
2908         
2909         g_free(imapset);
2910         imap_seq_set_free(seq_list);
2911         g_free(cmd);
2912
2913         return ok;
2914 }
2915
2916 typedef struct _select_data {
2917         IMAPSession *session;
2918         gchar *real_path;
2919         gint *exists;
2920         gint *recent;
2921         gint *unseen;
2922         guint32 *uid_validity;
2923         gboolean done;
2924 } select_data;
2925
2926 static void *imap_select_thread(void *data)
2927 {
2928         select_data *stuff = (select_data *)data;
2929         IMAPSession *session = stuff->session;
2930         gchar *real_path = stuff->real_path;
2931         gint *exists = stuff->exists;
2932         gint *recent = stuff->recent;
2933         gint *unseen = stuff->unseen;
2934         guint32 *uid_validity = stuff->uid_validity;
2935         gint ok;
2936         
2937         ok = imap_cmd_select(session, real_path,
2938                              exists, recent, unseen, uid_validity, TRUE);
2939         stuff->done = TRUE;
2940         return GINT_TO_POINTER(ok);
2941 }
2942                 
2943 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
2944                         const gchar *path,
2945                         gint *exists, gint *recent, gint *unseen,
2946                         guint32 *uid_validity, gboolean block)
2947 {
2948         gchar *real_path;
2949         gint ok;
2950         gint exists_, recent_, unseen_;
2951         guint32 uid_validity_;
2952         
2953         if (!exists || !recent || !unseen || !uid_validity) {
2954                 if (session->mbox && strcmp(session->mbox, path) == 0)
2955                         return IMAP_SUCCESS;
2956                 exists = &exists_;
2957                 recent = &recent_;
2958                 unseen = &unseen_;
2959                 uid_validity = &uid_validity_;
2960         }
2961
2962         g_free(session->mbox);
2963         session->mbox = NULL;
2964
2965         real_path = imap_get_real_path(folder, path);
2966         
2967 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
2968
2969         if (block == FALSE) {
2970                 select_data *data = g_new0(select_data, 1);
2971                 void *tmp;
2972                 pthread_t pt;
2973                 data->session = session;
2974                 data->real_path = real_path;
2975                 data->exists = exists;
2976                 data->recent = recent;
2977                 data->unseen = unseen;
2978                 data->uid_validity = uid_validity;
2979                 data->done = FALSE;
2980                 
2981                 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
2982                                 imap_select_thread, data) != 0) {
2983                         ok = GPOINTER_TO_INT(imap_select_thread(data));
2984                         g_free(data);
2985                 } else {
2986                         debug_print("+++waiting for imap_select_thread...\n");
2987                         while(!data->done) {
2988                                 /* don't let the interface freeze while waiting */
2989                                 sylpheed_do_idle();
2990                         }
2991                         debug_print("---imap_select_thread done\n");
2992
2993                         /* get the thread's return value and clean its resources */
2994                         pthread_join(pt, &tmp);
2995                         ok = GPOINTER_TO_INT(tmp);
2996                         g_free(data);
2997                 }
2998         } else {
2999                 ok = imap_cmd_select(session, real_path,
3000                              exists, recent, unseen, uid_validity, block);
3001         }
3002 #else
3003         ok = imap_cmd_select(session, real_path,
3004                              exists, recent, unseen, uid_validity, block);
3005 #endif
3006         if (ok != IMAP_SUCCESS)
3007                 log_warning(_("can't select folder: %s\n"), real_path);
3008         else {
3009                 session->mbox = g_strdup(path);
3010                 session->folder_content_changed = FALSE;
3011         }
3012         g_free(real_path);
3013
3014         return ok;
3015 }
3016
3017 #define THROW(err) { ok = err; goto catch; }
3018
3019 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
3020                         const gchar *path,
3021                         gint *messages, gint *recent,
3022                         guint32 *uid_next, guint32 *uid_validity,
3023                         gint *unseen, gboolean block)
3024 {
3025         gchar *real_path;
3026         gchar *real_path_;
3027         gint ok;
3028         GPtrArray *argbuf = NULL;
3029         gchar *str;
3030
3031         if (messages && recent && uid_next && uid_validity && unseen) {
3032                 *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
3033                 argbuf = g_ptr_array_new();
3034         }
3035
3036         real_path = imap_get_real_path(folder, path);
3037         QUOTE_IF_REQUIRED(real_path_, real_path);
3038         imap_gen_send(session, "STATUS %s "
3039                           "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
3040                           real_path_);
3041
3042         ok = imap_cmd_ok_with_block(session, argbuf, block);
3043         if (ok != IMAP_SUCCESS || !argbuf) THROW(ok);
3044
3045         str = search_array_str(argbuf, "STATUS");
3046         if (!str) THROW(IMAP_ERROR);
3047
3048         str = strrchr(str, '(');
3049         if (!str) THROW(IMAP_ERROR);
3050         str++;
3051         while (*str != '\0' && *str != ')') {
3052                 while (*str == ' ') str++;
3053
3054                 if (!strncmp(str, "MESSAGES ", 9)) {
3055                         str += 9;
3056                         *messages = strtol(str, &str, 10);
3057                 } else if (!strncmp(str, "RECENT ", 7)) {
3058                         str += 7;
3059                         *recent = strtol(str, &str, 10);
3060                 } else if (!strncmp(str, "UIDNEXT ", 8)) {
3061                         str += 8;
3062                         *uid_next = strtoul(str, &str, 10);
3063                 } else if (!strncmp(str, "UIDVALIDITY ", 12)) {
3064                         str += 12;
3065                         *uid_validity = strtoul(str, &str, 10);
3066                 } else if (!strncmp(str, "UNSEEN ", 7)) {
3067                         str += 7;
3068                         *unseen = strtol(str, &str, 10);
3069                 } else {
3070                         g_warning("invalid STATUS response: %s\n", str);
3071                         break;
3072                 }
3073         }
3074
3075 catch:
3076         g_free(real_path);
3077         if (argbuf) {
3078                 ptr_array_free_strings(argbuf);
3079                 g_ptr_array_free(argbuf, TRUE);
3080         }
3081
3082         return ok;
3083 }
3084
3085 #undef THROW
3086
3087 static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
3088 {
3089         gchar **p;
3090         
3091         for (p = session->capability; *p != NULL; ++p) {
3092                 if (!g_ascii_strcasecmp(*p, cap))
3093                         return TRUE;
3094         }
3095
3096         return FALSE;
3097 }
3098
3099 static void imap_free_capabilities(IMAPSession *session)
3100 {
3101         g_strfreev(session->capability);
3102         session->capability = NULL;
3103 }
3104
3105 /* low-level IMAP4rev1 commands */
3106
3107 static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
3108                                   const gchar *pass, IMAPAuthType type)
3109 {
3110         gchar *auth_type;
3111         gint ok;
3112         gchar *buf = NULL;
3113         gchar *challenge;
3114         gint challenge_len;
3115         gchar hexdigest[33];
3116         gchar *response;
3117         gchar *response64;
3118
3119         auth_type = "CRAM-MD5";
3120
3121         imap_gen_send(session, "AUTHENTICATE %s", auth_type);
3122         ok = imap_gen_recv(session, &buf);
3123         if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
3124                 g_free(buf);
3125                 return IMAP_ERROR;
3126         }
3127
3128         challenge = g_malloc(strlen(buf + 2) + 1);
3129         challenge_len = base64_decode(challenge, buf + 2, -1);
3130         challenge[challenge_len] = '\0';
3131         g_free(buf);
3132         log_print("IMAP< [Decoded: %s]\n", challenge);
3133
3134         md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
3135         g_free(challenge);
3136
3137         response = g_strdup_printf("%s %s", user, hexdigest);
3138         log_print("IMAP> [Encoded: %s]\n", response);
3139         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
3140         base64_encode(response64, response, strlen(response));
3141         g_free(response);
3142
3143         log_print("IMAP> %s\n", response64);
3144         sock_puts(SESSION(session)->sock, response64);
3145         ok = imap_cmd_ok(session, NULL);
3146         if (ok != IMAP_SUCCESS)
3147                 log_warning(_("IMAP4 authentication failed.\n"));
3148
3149         return ok;
3150 }
3151
3152 static gint imap_cmd_login(IMAPSession *session,
3153                            const gchar *user, const gchar *pass)
3154 {
3155         gchar *ans;
3156         gint ok;
3157
3158         imap_gen_send(session, "LOGIN {%d}\r\n%s {%d}\r\n%s", 
3159                                 strlen(user), user, 
3160                                 strlen(pass), pass);
3161                                 
3162         ok = imap_gen_recv_with_block(session, &ans, TRUE);
3163         if (ok != IMAP_SUCCESS || ans[0] != '+' || ans[1] != ' ') {
3164                 g_free(ans);
3165                 return IMAP_ERROR;
3166         }
3167         g_free(ans);
3168         ok = imap_gen_recv_with_block(session, &ans, TRUE);
3169         if (ok != IMAP_SUCCESS || ans[0] != '+' || ans[1] != ' ') {
3170                 g_free(ans);
3171                 return IMAP_ERROR;
3172         }
3173         g_free(ans);
3174         ok = imap_cmd_ok(session, NULL);
3175         if (ok != IMAP_SUCCESS)
3176                 log_warning(_("IMAP4 login failed.\n"));
3177
3178         return ok;
3179 }
3180
3181 static gint imap_cmd_logout(IMAPSession *session)
3182 {
3183         imap_gen_send(session, "LOGOUT");
3184         return imap_cmd_ok(session, NULL);
3185 }
3186
3187 static gint imap_cmd_noop(IMAPSession *session)
3188 {
3189         imap_gen_send(session, "NOOP");
3190         return imap_cmd_ok(session, NULL);
3191 }
3192
3193 #if USE_OPENSSL
3194 static gint imap_cmd_starttls(IMAPSession *session)
3195 {
3196         imap_gen_send(session, "STARTTLS");
3197         return imap_cmd_ok(session, NULL);
3198 }
3199 #endif
3200
3201 #define THROW(err) { ok = err; goto catch; }
3202
3203 static gint imap_cmd_namespace(IMAPSession *session, gchar **ns_str)
3204 {
3205         gint ok;
3206         GPtrArray *argbuf;
3207         gchar *str;
3208
3209         argbuf = g_ptr_array_new();
3210
3211         imap_gen_send(session, "NAMESPACE");
3212         if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW(ok);
3213
3214         str = search_array_str(argbuf, "NAMESPACE");
3215         if (!str) THROW(IMAP_ERROR);
3216
3217         *ns_str = g_strdup(str);
3218
3219 catch:
3220         ptr_array_free_strings(argbuf);
3221         g_ptr_array_free(argbuf, TRUE);
3222
3223         return ok;
3224 }
3225
3226 #undef THROW
3227
3228 static gint imap_cmd_list(IMAPSession *session, const gchar *ref,
3229                           const gchar *mailbox, GPtrArray *argbuf)
3230 {
3231         gchar *ref_, *mailbox_;
3232
3233         if (!ref) ref = "\"\"";
3234         if (!mailbox) mailbox = "\"\"";
3235
3236         QUOTE_IF_REQUIRED(ref_, ref);
3237         QUOTE_IF_REQUIRED(mailbox_, mailbox);
3238         imap_gen_send(session, "LIST %s %s", ref_, mailbox_);
3239
3240         return imap_cmd_ok(session, argbuf);
3241 }
3242
3243 #define THROW goto catch
3244
3245 static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder,
3246                                gboolean examine,
3247                                gint *exists, gint *recent, gint *unseen,
3248                                guint32 *uid_validity, gboolean block)
3249 {
3250         gint ok;
3251         gchar *resp_str;
3252         GPtrArray *argbuf;
3253         gchar *select_cmd;
3254         gchar *folder_;
3255         unsigned int uid_validity_;
3256
3257         *exists = *recent = *unseen = *uid_validity = 0;
3258         argbuf = g_ptr_array_new();
3259
3260         if (examine)
3261                 select_cmd = "EXAMINE";
3262         else
3263                 select_cmd = "SELECT";
3264
3265         QUOTE_IF_REQUIRED(folder_, folder);
3266         imap_gen_send(session, "%s %s", select_cmd, folder_);
3267
3268         if ((ok = imap_cmd_ok_with_block(session, argbuf, block)) != IMAP_SUCCESS) THROW;
3269
3270         resp_str = search_array_contain_str(argbuf, "EXISTS");
3271         if (resp_str) {
3272                 if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
3273                         g_warning("imap_cmd_select(): invalid EXISTS line.\n");
3274                         THROW;
3275                 }
3276         }
3277
3278         resp_str = search_array_contain_str(argbuf, "RECENT");
3279         if (resp_str) {
3280                 if (sscanf(resp_str, "%d RECENT", recent) != 1) {
3281                         g_warning("imap_cmd_select(): invalid RECENT line.\n");
3282                         THROW;
3283                 }
3284         }
3285
3286         resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
3287         if (resp_str) {
3288                 if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", &uid_validity_)
3289                     != 1) {
3290                         g_warning("imap_cmd_select(): invalid UIDVALIDITY line.\n");
3291                         THROW;
3292                 }
3293                 *uid_validity = uid_validity_;
3294         }
3295
3296         resp_str = search_array_contain_str(argbuf, "UNSEEN");
3297         if (resp_str) {
3298                 if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
3299                         g_warning("imap_cmd_select(): invalid UNSEEN line.\n");
3300                         THROW;
3301                 }
3302         }
3303
3304 catch:
3305         ptr_array_free_strings(argbuf);
3306         g_ptr_array_free(argbuf, TRUE);
3307
3308         return ok;
3309 }
3310
3311 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
3312                             gint *exists, gint *recent, gint *unseen,
3313                             guint32 *uid_validity, gboolean block)
3314 {
3315         return imap_cmd_do_select(session, folder, FALSE,
3316                                   exists, recent, unseen, uid_validity, block);
3317 }
3318
3319 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
3320                              gint *exists, gint *recent, gint *unseen,
3321                              guint32 *uid_validity, gboolean block)
3322 {
3323         return imap_cmd_do_select(session, folder, TRUE,
3324                                   exists, recent, unseen, uid_validity, block);
3325 }
3326
3327 #undef THROW
3328
3329 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
3330 {
3331         gchar *folder_;
3332
3333         QUOTE_IF_REQUIRED(folder_, folder);
3334         imap_gen_send(session, "CREATE %s", folder_);
3335
3336         return imap_cmd_ok(session, NULL);
3337 }
3338
3339 static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
3340                             const gchar *new_folder)
3341 {
3342         gchar *old_folder_, *new_folder_;
3343
3344         QUOTE_IF_REQUIRED(old_folder_, old_folder);
3345         QUOTE_IF_REQUIRED(new_folder_, new_folder);
3346         imap_gen_send(session, "RENAME %s %s", old_folder_, new_folder_);
3347
3348         return imap_cmd_ok(session, NULL);
3349 }
3350
3351 static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
3352 {
3353         gchar *folder_;
3354
3355         QUOTE_IF_REQUIRED(folder_, folder);
3356         imap_gen_send(session, "DELETE %s", folder_);
3357
3358         return imap_cmd_ok(session, NULL);
3359 }
3360
3361 static gint imap_cmd_search(IMAPSession *session, const gchar *criteria, 
3362                             GSList **list, gboolean block)
3363 {
3364         gint ok;
3365         gchar *uidlist;
3366         GPtrArray *argbuf;
3367
3368         g_return_val_if_fail(criteria != NULL, IMAP_ERROR);
3369         g_return_val_if_fail(list != NULL, IMAP_ERROR);
3370
3371         *list = NULL;
3372         
3373         argbuf = g_ptr_array_new();
3374         imap_gen_send(session, "UID SEARCH %s", criteria);
3375
3376         ok = imap_cmd_ok_with_block(session, argbuf, block);
3377         if (ok != IMAP_SUCCESS) {
3378                 ptr_array_free_strings(argbuf);
3379                 g_ptr_array_free(argbuf, TRUE);
3380                 return ok;
3381         }
3382
3383         if ((uidlist = search_array_str(argbuf, "SEARCH ")) != NULL) {
3384                 gchar **strlist, **p;
3385
3386                 strlist = g_strsplit(uidlist + 7, " ", 0);
3387                 for (p = strlist; *p != NULL; ++p) {
3388                         guint msgnum;
3389
3390                         if (sscanf(*p, "%u", &msgnum) == 1)
3391                                 *list = g_slist_append(*list, GINT_TO_POINTER(msgnum));
3392                 }
3393                 g_strfreev(strlist);
3394         }
3395         ptr_array_free_strings(argbuf);
3396         g_ptr_array_free(argbuf, TRUE);
3397
3398         return IMAP_SUCCESS;
3399 }
3400
3401 typedef struct _fetch_data {
3402         IMAPSession *session;
3403         guint32 uid;
3404         const gchar *filename;
3405         gboolean headers;
3406         gboolean body;
3407         gboolean done;
3408 } fetch_data;
3409
3410 static void *imap_cmd_fetch_thread(void *data)
3411 {
3412         fetch_data *stuff = (fetch_data *)data;
3413         IMAPSession *session = stuff->session;
3414         guint32 uid = stuff->uid;
3415         const gchar *filename = stuff->filename;
3416         
3417         gint ok;
3418         gchar *buf = NULL;
3419         gchar *cur_pos;
3420         gchar size_str[32];
3421         glong size_num;
3422
3423         if (filename == NULL) {
3424                 stuff->done = TRUE;
3425                 return GINT_TO_POINTER(IMAP_ERROR);
3426         }
3427         
3428         if (stuff->body)
3429                 imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
3430         else
3431                 imap_gen_send(session, "UID FETCH %d BODY.PEEK[HEADER]",
3432                                 uid);
3433         while ((ok = imap_gen_recv_block(session, &buf)) == IMAP_SUCCESS) {
3434                 if (buf[0] != '*' || buf[1] != ' ') {
3435                         g_free(buf);
3436                         stuff->done = TRUE;
3437                         return GINT_TO_POINTER(IMAP_ERROR);
3438                 }
3439                 if (strstr(buf, "FETCH") != NULL) break;
3440                 g_free(buf);
3441         }
3442         if (ok != IMAP_SUCCESS) {
3443                 g_free(buf);
3444                 stuff->done = TRUE;
3445                 return GINT_TO_POINTER(ok);
3446         }
3447
3448 #define RETURN_ERROR_IF_FAIL(cond)      \
3449         if (!(cond)) {                  \
3450                 g_free(buf);            \
3451                 stuff->done = TRUE;     \
3452                 return GINT_TO_POINTER(IMAP_ERROR);     \
3453         }
3454
3455         cur_pos = strchr(buf, '{');
3456         RETURN_ERROR_IF_FAIL(cur_pos != NULL);
3457         cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
3458         RETURN_ERROR_IF_FAIL(cur_pos != NULL);
3459         size_num = atol(size_str);
3460         RETURN_ERROR_IF_FAIL(size_num >= 0);
3461
3462         RETURN_ERROR_IF_FAIL(*cur_pos == '\0');
3463
3464 #undef RETURN_ERROR_IF_FAIL
3465
3466         g_free(buf);
3467
3468         if (recv_bytes_write_to_file(SESSION(session)->sock,
3469                                      size_num, filename) != 0) {
3470                 stuff->done = TRUE;
3471                 return GINT_TO_POINTER(IMAP_ERROR);
3472         }
3473         if (imap_gen_recv_block(session, &buf) != IMAP_SUCCESS) {
3474                 g_free(buf);
3475                 stuff->done = TRUE;
3476                 return GINT_TO_POINTER(IMAP_ERROR);
3477         }
3478
3479         if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') {
3480                 g_free(buf);
3481                 stuff->done = TRUE;
3482                 return GINT_TO_POINTER(IMAP_ERROR);
3483         }
3484         g_free(buf);
3485
3486         ok = imap_cmd_ok_block(session, NULL);
3487
3488         stuff->done = TRUE;
3489         return GINT_TO_POINTER(ok);
3490 }
3491
3492 static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
3493                                 const gchar *filename, gboolean headers,
3494                                 gboolean body)
3495 {
3496         fetch_data *data = g_new0(fetch_data, 1);
3497         int result = 0;
3498 #ifdef USE_PTHREAD
3499         void *tmp;
3500         pthread_t pt;
3501 #endif
3502         data->done = FALSE;
3503         data->session = session;
3504         data->uid = uid;
3505         data->filename = filename;
3506         data->headers = headers;
3507         data->body = body;
3508
3509         if (prefs_common.work_offline && !imap_gtk_should_override()) {
3510                 g_free(data);
3511                 return -1;
3512         }
3513
3514 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
3515         MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
3516         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
3517                         imap_cmd_fetch_thread, data) != 0) {
3518                 result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3519                 g_free(data);
3520                 MUTEX_UNLOCK();
3521                 return result;
3522         }
3523         debug_print("+++waiting for imap_cmd_fetch_thread...\n");
3524         while(!data->done) {
3525                 /* don't let the interface freeze while waiting */
3526                 sylpheed_do_idle();
3527         }
3528         debug_print("---imap_cmd_fetch_thread done\n");
3529
3530         /* get the thread's return value and clean its resources */
3531         pthread_join(pt, &tmp);
3532         result = GPOINTER_TO_INT(tmp);
3533         MUTEX_UNLOCK();
3534 #else
3535         result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
3536 #endif
3537         g_free(data);
3538         return result;
3539 }
3540
3541 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
3542                             const gchar *file, IMAPFlags flags, 
3543                             guint32 *new_uid)
3544 {
3545         gint ok;
3546         gint size;
3547         gchar *destfolder_;
3548         gchar *flag_str;
3549         unsigned int new_uid_;
3550         gchar *ret = NULL;
3551         gchar buf[BUFFSIZE];
3552         FILE *fp;
3553         GPtrArray *argbuf;
3554         gchar *resp_str;
3555
3556         g_return_val_if_fail(file != NULL, IMAP_ERROR);
3557
3558         size = get_file_size_as_crlf(file);
3559         if ((fp = fopen(file, "rb")) == NULL) {
3560                 FILE_OP_ERROR(file, "fopen");
3561                 return -1;
3562         }
3563         QUOTE_IF_REQUIRED(destfolder_, destfolder);
3564         flag_str = imap_get_flag_str(flags);
3565         imap_gen_send(session, "APPEND %s (%s) {%d}", 
3566                       destfolder_, flag_str, size);
3567         g_free(flag_str);
3568
3569         ok = imap_gen_recv(session, &ret);
3570         if (ok != IMAP_SUCCESS || ret[0] != '+' || ret[1] != ' ') {
3571                 log_warning(_("can't append %s to %s\n"), file, destfolder_);
3572                 g_free(ret);
3573                 fclose(fp);
3574                 return IMAP_ERROR;
3575         }
3576         g_free(ret);
3577
3578         log_print("IMAP4> %s\n", "(sending file...)");
3579
3580         while (fgets(buf, sizeof(buf), fp) != NULL) {
3581                 strretchomp(buf);
3582                 if (sock_puts(SESSION(session)->sock, buf) < 0) {
3583                         fclose(fp);
3584                         return -1;
3585                 }
3586         }
3587
3588         if (ferror(fp)) {
3589                 FILE_OP_ERROR(file, "fgets");
3590                 fclose(fp);
3591                 return -1;
3592         }
3593
3594         sock_puts(SESSION(session)->sock, "");
3595
3596         fclose(fp);
3597
3598         if (new_uid != NULL)
3599                 *new_uid = 0;
3600
3601         if (new_uid != NULL && session->uidplus) {
3602                 argbuf = g_ptr_array_new();
3603
3604                 ok = imap_cmd_ok(session, argbuf);
3605                 if ((ok == IMAP_SUCCESS) && (argbuf->len > 0)) {
3606                         resp_str = g_ptr_array_index(argbuf, argbuf->len - 1);
3607                         if (resp_str &&
3608                             sscanf(resp_str, "%*u OK [APPENDUID %*u %u]",
3609                                    &new_uid_) == 1) {
3610                                 *new_uid = new_uid_;
3611                         }
3612                 }
3613
3614                 ptr_array_free_strings(argbuf);
3615                 g_ptr_array_free(argbuf, TRUE);
3616         } else
3617                 ok = imap_cmd_ok(session, NULL);
3618
3619         if (ok != IMAP_SUCCESS)
3620                 log_warning(_("can't append message to %s\n"),
3621                             destfolder_);
3622
3623         return ok;
3624 }
3625
3626 static MsgNumberList *imapset_to_numlist(IMAPSet imapset)
3627 {
3628         gchar **ranges, **range;
3629         unsigned int low, high;
3630         MsgNumberList *uids = NULL;
3631         
3632         ranges = g_strsplit(imapset, ",", 0);
3633         for (range = ranges; *range != NULL; range++) {
3634                 if (sscanf(*range, "%u:%u", &low, &high) == 1)
3635                         uids = g_slist_prepend(uids, GINT_TO_POINTER(low));
3636                 else {
3637                         int i;
3638                         for (i = low; i <= high; i++)
3639                                 uids = g_slist_prepend(uids, GINT_TO_POINTER(i));
3640                 }
3641         }
3642         uids = g_slist_reverse(uids);
3643         g_strfreev(ranges);
3644
3645         return uids;
3646 }
3647
3648 static gint imap_cmd_copy(IMAPSession *session, const gchar *seq_set,
3649                           const gchar *destfolder, GRelation *uid_mapping)
3650 {
3651         gint ok;
3652         gchar *destfolder_;
3653         
3654         g_return_val_if_fail(session != NULL, IMAP_ERROR);
3655         g_return_val_if_fail(seq_set != NULL, IMAP_ERROR);
3656         g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
3657
3658         QUOTE_IF_REQUIRED(destfolder_, destfolder);
3659         imap_gen_send(session, "UID COPY %s %s", seq_set, destfolder_);
3660
3661         if (uid_mapping != NULL && session->uidplus) {
3662                 GPtrArray *reply;               
3663                 gchar *resp_str = NULL, *olduids_str, *newuids_str;
3664                 MsgNumberList *olduids, *old_cur, *newuids, *new_cur;
3665
3666                 reply = g_ptr_array_new();
3667                 ok = imap_cmd_ok(session, reply);
3668                 if ((ok == IMAP_SUCCESS) && (reply->len > 0)) {
3669                         resp_str = g_ptr_array_index(reply, reply->len - 1);
3670                         if (resp_str) {
3671                                 olduids_str = g_new0(gchar, strlen(resp_str));
3672                                 newuids_str = g_new0(gchar, strlen(resp_str));
3673                                 if (sscanf(resp_str, "%*s OK [COPYUID %*u %[0-9,:] %[0-9,:]]",
3674                                            olduids_str, newuids_str) == 2) {
3675                                         olduids = imapset_to_numlist(olduids_str);
3676                                         newuids = imapset_to_numlist(newuids_str);
3677
3678                                         old_cur = olduids;
3679                                         new_cur = newuids;
3680                                         while(old_cur != NULL && new_cur != NULL) {
3681                                                 g_relation_insert(uid_mapping, 
3682                                                                   GPOINTER_TO_INT(old_cur->data),
3683                                                                   GPOINTER_TO_INT(new_cur->data));
3684                                                 old_cur = g_slist_next(old_cur);
3685                                                 new_cur = g_slist_next(new_cur);
3686                                         }
3687
3688                                         g_slist_free(olduids);
3689                                         g_slist_free(newuids);
3690                                 }
3691                                 g_free(olduids_str);
3692                                 g_free(newuids_str);
3693                         }
3694                 }
3695                 ptr_array_free_strings(reply);
3696                 g_ptr_array_free(reply, TRUE);
3697         } else
3698                 ok = imap_cmd_ok(session, NULL);
3699
3700         if (ok != IMAP_SUCCESS)
3701                 log_warning(_("can't copy %s to %s\n"), seq_set, destfolder_);
3702
3703         return ok;
3704 }
3705
3706 gint imap_cmd_envelope(IMAPSession *session, IMAPSet set)
3707 {
3708         static gchar *header_fields = 
3709                 "Date From To Cc Subject Message-ID References In-Reply-To" ;
3710
3711         imap_gen_send
3712                 (session, "UID FETCH %s (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])",
3713                  set, header_fields);
3714
3715         return IMAP_SUCCESS;
3716 }
3717
3718 static gint imap_cmd_store(IMAPSession *session, IMAPSet seq_set,
3719                            gchar *sub_cmd)
3720 {
3721         gint ok;
3722
3723         imap_gen_send(session, "UID STORE %s %s", seq_set, sub_cmd);
3724
3725         if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
3726                 log_warning(_("error while imap command: STORE %s %s\n"),
3727                             seq_set, sub_cmd);
3728                 return ok;
3729         }
3730
3731         return IMAP_SUCCESS;
3732 }
3733
3734 typedef struct _expunge_data {
3735         IMAPSession *session;
3736         IMAPSet seq_set;
3737         gboolean done;
3738 } expunge_data;
3739
3740 static void *imap_cmd_expunge_thread(void *data)
3741 {
3742         expunge_data *stuff = (expunge_data *)data;
3743         IMAPSession *session = stuff->session;
3744         IMAPSet seq_set = stuff->seq_set;
3745
3746         gint ok;
3747
3748         if (seq_set && session->uidplus)
3749                 imap_gen_send(session, "UID EXPUNGE %s", seq_set);
3750         else    
3751                 imap_gen_send(session, "EXPUNGE");
3752         if ((ok = imap_cmd_ok_with_block(session, NULL, TRUE)) != IMAP_SUCCESS) {
3753                 log_warning(_("error while imap command: EXPUNGE\n"));
3754                 stuff->done = TRUE;
3755                 return GINT_TO_POINTER(ok);
3756         }
3757
3758         stuff->done = TRUE;
3759         return GINT_TO_POINTER(IMAP_SUCCESS);
3760 }
3761
3762 static gint imap_cmd_expunge(IMAPSession *session, IMAPSet seq_set)
3763 {
3764         expunge_data *data = g_new0(expunge_data, 1);
3765         int result;
3766 #ifdef USE_PTHREAD
3767         void *tmp;
3768         pthread_t pt;
3769 #endif
3770         data->done = FALSE;
3771         data->session = session;
3772         data->seq_set = seq_set;
3773
3774         if (prefs_common.work_offline && !imap_gtk_should_override()) {
3775                 g_free(data);
3776                 return -1;
3777         }
3778
3779 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
3780         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
3781                         imap_cmd_expunge_thread, data) != 0) {
3782                 result = GPOINTER_TO_INT(imap_cmd_expunge_thread(data));
3783                 g_free(data);
3784                 return result;
3785         }
3786         debug_print("+++waiting for imap_cmd_expunge_thread...\n");
3787         while(!data->done) {
3788                 /* don't let the interface freeze while waiting */
3789                 sylpheed_do_idle();
3790         }
3791         debug_print("---imap_cmd_expunge_thread done\n");
3792
3793         /* get the thread's return value and clean its resources */
3794         pthread_join(pt, &tmp);
3795         result = GPOINTER_TO_INT(tmp);
3796 #else
3797         result = GPOINTER_TO_INT(imap_cmd_expunge_thread(data));
3798 #endif
3799         g_free(data);
3800         return result;
3801 }
3802
3803 static gint imap_cmd_close(IMAPSession *session)
3804 {
3805         gint ok;
3806
3807         imap_gen_send(session, "CLOSE");
3808         if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS)
3809                 log_warning(_("error while imap command: CLOSE\n"));
3810
3811         return ok;
3812 }
3813
3814 static gint imap_cmd_ok_with_block(IMAPSession *session, GPtrArray *argbuf, gboolean block)
3815 {
3816         gint ok = IMAP_SUCCESS;
3817         gchar *buf;
3818         gint cmd_num;
3819         gchar *data;
3820
3821         while ((ok = imap_gen_recv_with_block(session, &buf, block))
3822                == IMAP_SUCCESS) {
3823                 /* make sure data is long enough for any substring of buf */
3824                 data = alloca(strlen(buf) + 1);
3825
3826                 /* untagged line read */
3827                 if (buf[0] == '*' && buf[1] == ' ') {
3828                         gint num;
3829                         if (argbuf)
3830                                 g_ptr_array_add(argbuf, g_strdup(buf + 2));
3831
3832                         if (sscanf(buf + 2, "%d %s", &num, data) >= 2) {
3833                                 if (!strcmp(data, "EXISTS")) {
3834                                         session->exists = num;
3835                                         session->folder_content_changed = TRUE;
3836                                 }
3837
3838                                 if(!strcmp(data, "EXPUNGE")) {
3839                                         session->exists--;
3840                                         session->folder_content_changed = TRUE;
3841                                 }
3842                         }
3843                 /* tagged line with correct tag and OK response found */
3844                 } else if ((sscanf(buf, "%d %s", &cmd_num, data) >= 2) &&
3845                            (cmd_num == session->cmd_count) &&
3846                            !strcmp(data, "OK")) {
3847                         if (argbuf)
3848                                 g_ptr_array_add(argbuf, g_strdup(buf));
3849                         break;
3850                 /* everything else */
3851                 } else {
3852                         ok = IMAP_ERROR;
3853                         break;
3854                 }
3855                 g_free(buf);
3856         }
3857         g_free(buf);
3858
3859         return ok;
3860 }
3861 static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
3862 {
3863         return imap_cmd_ok_with_block(session, argbuf, FALSE);
3864 }
3865 static gint imap_cmd_ok_block(IMAPSession *session, GPtrArray *argbuf)
3866 {
3867         return imap_cmd_ok_with_block(session, argbuf, TRUE);
3868 }
3869 static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
3870 {
3871         gchar *buf;
3872         gchar *tmp;
3873         gchar *p;
3874         va_list args;
3875
3876         va_start(args, format);
3877         tmp = g_strdup_vprintf(format, args);
3878         va_end(args);
3879
3880         session->cmd_count++;
3881
3882         buf = g_strdup_printf("%d %s\r\n", session->cmd_count, tmp);
3883         if (!g_ascii_strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
3884                 *p = '\0';
3885                 log_print("IMAP4> %d %s ********\n", session->cmd_count, tmp);
3886         } else
3887                 log_print("IMAP4> %d %s\n", session->cmd_count, tmp);
3888
3889         sock_write_all(SESSION(session)->sock, buf, strlen(buf));
3890         g_free(tmp);
3891         g_free(buf);
3892 }
3893
3894 static gint imap_gen_recv_with_block(IMAPSession *session, gchar **ret, gboolean block)
3895 {
3896         if (!block) {
3897                 if ((*ret = imap_getline(SESSION(session)->sock)) == NULL)
3898                         return IMAP_SOCKET;
3899         } else {
3900                 if ((*ret = sock_getline(SESSION(session)->sock)) == NULL)
3901                         return IMAP_SOCKET;
3902         }
3903         strretchomp(*ret);
3904
3905         log_print("IMAP4< %s\n", *ret);
3906         
3907         session_set_access_time(SESSION(session));
3908
3909         return IMAP_SUCCESS;
3910 }
3911
3912 static gint imap_gen_recv_block(IMAPSession *session, gchar **ret)
3913 {
3914         return imap_gen_recv_with_block(session, ret, TRUE);
3915 }
3916
3917 static gint imap_gen_recv(IMAPSession *session, gchar **ret)
3918 {
3919         return imap_gen_recv_with_block(session, ret, FALSE);
3920 }
3921 /* misc utility functions */
3922
3923 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
3924 {
3925         gchar *tmp;
3926
3927         dest[0] = '\0';
3928         tmp = strchr(src, ch);
3929         if (!tmp)
3930                 return NULL;
3931
3932         memcpy(dest, src, MIN(tmp - src, len - 1));
3933         dest[MIN(tmp - src, len - 1)] = '\0';
3934
3935         return tmp + 1;
3936 }
3937
3938 static gchar *get_quoted(const gchar *src, gchar ch, gchar *dest, gint len)
3939 {
3940         const gchar *p = src;
3941         gint n = 0;
3942
3943         g_return_val_if_fail(*p == ch, NULL);
3944
3945         *dest = '\0';
3946         p++;
3947
3948         while (*p != '\0' && *p != ch) {
3949                 if (n < len - 1) {
3950                         if (*p == '\\' && *(p + 1) != '\0')
3951                                 p++;
3952                         *dest++ = *p++;
3953                 } else
3954                         p++;
3955                 n++;
3956         }
3957
3958         *dest = '\0';
3959         return (gchar *)(*p == ch ? p + 1 : p);
3960 }
3961
3962 static gchar *search_array_contain_str(GPtrArray *array, const gchar *str)
3963 {
3964         gint i;
3965
3966         for (i = 0; i < array->len; i++) {
3967                 gchar *tmp;
3968
3969                 tmp = g_ptr_array_index(array, i);
3970                 if (strstr(tmp, str) != NULL)
3971                         return tmp;
3972         }
3973
3974         return NULL;
3975 }
3976
3977 static gchar *search_array_str(GPtrArray *array, const gchar *str)
3978 {
3979         gint i;
3980         gint len;
3981
3982         len = strlen(str);
3983
3984         for (i = 0; i < array->len; i++) {
3985                 gchar *tmp;
3986
3987                 tmp = g_ptr_array_index(array, i);
3988                 if (!strncmp(tmp, str, len))
3989                         return tmp;
3990         }
3991
3992         return NULL;
3993 }
3994
3995 static void imap_path_separator_subst(gchar *str, gchar separator)
3996 {
3997         gchar *p;
3998         gboolean in_escape = FALSE;
3999
4000         if (!separator || separator == '/') return;
4001
4002         for (p = str; *p != '\0'; p++) {
4003                 if (*p == '/' && !in_escape)
4004                         *p = separator;
4005                 else if (*p == '&' && *(p + 1) != '-' && !in_escape)
4006                         in_escape = TRUE;
4007                 else if (*p == '-' && in_escape)
4008                         in_escape = FALSE;
4009         }
4010 }
4011
4012 static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
4013 {
4014         static iconv_t cd = (iconv_t)-1;
4015         static gboolean iconv_ok = TRUE;
4016         GString *norm_utf7;
4017         gchar *norm_utf7_p;
4018         size_t norm_utf7_len;
4019         const gchar *p;
4020         gchar *to_str, *to_p;
4021         size_t to_len;
4022         gboolean in_escape = FALSE;
4023
4024         if (!iconv_ok) return g_strdup(mutf7_str);
4025
4026         if (cd == (iconv_t)-1) {
4027                 cd = iconv_open(CS_INTERNAL, CS_UTF_7);
4028                 if (cd == (iconv_t)-1) {
4029                         g_warning("iconv cannot convert UTF-7 to %s\n",
4030                                   CS_INTERNAL);
4031                         iconv_ok = FALSE;
4032                         return g_strdup(mutf7_str);
4033                 }
4034         }
4035
4036         /* modified UTF-7 to normal UTF-7 conversion */
4037         norm_utf7 = g_string_new(NULL);
4038
4039         for (p = mutf7_str; *p != '\0'; p++) {
4040                 /* replace: '&'  -> '+',
4041                             "&-" -> '&',
4042                             escaped ','  -> '/' */
4043                 if (!in_escape && *p == '&') {
4044                         if (*(p + 1) != '-') {
4045                                 g_string_append_c(norm_utf7, '+');
4046                                 in_escape = TRUE;
4047                         } else {
4048                                 g_string_append_c(norm_utf7, '&');
4049                                 p++;
4050                         }
4051                 } else if (in_escape && *p == ',') {
4052                         g_string_append_c(norm_utf7, '/');
4053                 } else if (in_escape && *p == '-') {
4054                         g_string_append_c(norm_utf7, '-');
4055                         in_escape = FALSE;
4056                 } else {
4057                         g_string_append_c(norm_utf7, *p);
4058                 }
4059         }
4060
4061         norm_utf7_p = norm_utf7->str;
4062         norm_utf7_len = norm_utf7->len;
4063         to_len = strlen(mutf7_str) * 5;
4064         to_p = to_str = g_malloc(to_len + 1);
4065
4066         if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
4067                   &to_p, &to_len) == -1) {
4068                 g_warning(_("iconv cannot convert UTF-7 to %s\n"),
4069                           conv_get_locale_charset_str());
4070                 g_string_free(norm_utf7, TRUE);
4071                 g_free(to_str);
4072                 return g_strdup(mutf7_str);
4073         }
4074
4075         /* second iconv() call for flushing */
4076         iconv(cd, NULL, NULL, &to_p, &to_len);
4077         g_string_free(norm_utf7, TRUE);
4078         *to_p = '\0';
4079
4080         return to_str;
4081 }
4082
4083 static gchar *imap_utf8_to_modified_utf7(const gchar *from)
4084 {