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