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