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