Enable GUI option for 'custom command' connection. Don't g_free strings in
[platform/upstream/evolution-data-server.git] / camel / providers / imap / camel-imap-store.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-imap-store.c : class for an imap store */
3
4 /*
5  *  Authors:
6  *    Dan Winship <danw@ximian.com>
7  *    Jeffrey Stedfast <fejj@ximian.com>
8  *
9  *  Copyright 2000, 2003 Ximian, Inc.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of version 2 of the GNU General Public
13  * License as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  */
26
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
37
38 #include "e-util/e-path.h"
39
40 #include "camel-imap-store.h"
41 #include "camel-imap-store-summary.h"
42 #include "camel-imap-folder.h"
43 #include "camel-imap-utils.h"
44 #include "camel-imap-command.h"
45 #include "camel-imap-summary.h"
46 #include "camel-imap-message-cache.h"
47 #include "camel-disco-diary.h"
48 #include "camel-file-utils.h"
49 #include "camel-folder.h"
50 #include "camel-exception.h"
51 #include "camel-session.h"
52 #include "camel-stream.h"
53 #include "camel-stream-buffer.h"
54 #include "camel-stream-fs.h"
55 #include "camel-stream-process.h"
56 #include "camel-tcp-stream-raw.h"
57 #include "camel-tcp-stream-ssl.h"
58 #include "camel-url.h"
59 #include "camel-sasl.h"
60 #include "camel-utf8.h"
61 #include "camel-string-utils.h"
62
63 #include "camel-imap-private.h"
64 #include "camel-private.h"
65
66 #define d(x) 
67
68 /* Specified in RFC 2060 */
69 #define IMAP_PORT 143
70 #define SIMAP_PORT 993
71
72
73 extern int camel_verbose_debug;
74
75 static CamelDiscoStoreClass *parent_class = NULL;
76
77 static char imap_tag_prefix = 'A';
78
79 static void construct (CamelService *service, CamelSession *session,
80                        CamelProvider *provider, CamelURL *url,
81                        CamelException *ex);
82
83 static int imap_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
84 static int imap_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
85
86 static char *imap_get_name (CamelService *service, gboolean brief);
87
88 static gboolean can_work_offline (CamelDiscoStore *disco_store);
89 static gboolean imap_connect_online (CamelService *service, CamelException *ex);
90 static gboolean imap_connect_offline (CamelService *service, CamelException *ex);
91 static gboolean imap_disconnect_online (CamelService *service, gboolean clean, CamelException *ex);
92 static gboolean imap_disconnect_offline (CamelService *service, gboolean clean, CamelException *ex);
93 static void imap_noop (CamelStore *store, CamelException *ex);
94 static GList *query_auth_types (CamelService *service, CamelException *ex);
95 static guint hash_folder_name (gconstpointer key);
96 static gint compare_folder_name (gconstpointer a, gconstpointer b);
97 static CamelFolder *get_folder_online (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex);
98 static CamelFolder *get_folder_offline (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex);
99 static CamelFolderInfo *create_folder (CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex);
100 static void             delete_folder (CamelStore *store, const char *folder_name, CamelException *ex);
101 static void             rename_folder (CamelStore *store, const char *old_name, const char *new_name, CamelException *ex);
102 static CamelFolderInfo *get_folder_info_online (CamelStore *store,
103                                                 const char *top,
104                                                 guint32 flags,
105                                                 CamelException *ex);
106 static CamelFolderInfo *get_folder_info_offline (CamelStore *store,
107                                                  const char *top,
108                                                  guint32 flags,
109                                                  CamelException *ex);
110 static gboolean folder_subscribed (CamelStore *store, const char *folder_name);
111 static void subscribe_folder (CamelStore *store, const char *folder_name,
112                               CamelException *ex);
113 static void unsubscribe_folder (CamelStore *store, const char *folder_name,
114                                 CamelException *ex);
115
116 static void get_folders_online (CamelImapStore *imap_store, const char *pattern,
117                                 GPtrArray *folders, gboolean lsub, CamelException *ex);
118
119
120 static void imap_folder_effectively_unsubscribed(CamelImapStore *imap_store, const char *folder_name, CamelException *ex);
121 static gboolean imap_check_folder_still_extant (CamelImapStore *imap_store, const char *full_name,  CamelException *ex);
122 static void imap_forget_folder(CamelImapStore *imap_store, const char *folder_name, CamelException *ex);
123 static void imap_set_server_level (CamelImapStore *store);
124
125 static void
126 camel_imap_store_class_init (CamelImapStoreClass *camel_imap_store_class)
127 {
128         CamelObjectClass *camel_object_class =
129                 CAMEL_OBJECT_CLASS (camel_imap_store_class);
130         CamelServiceClass *camel_service_class =
131                 CAMEL_SERVICE_CLASS (camel_imap_store_class);
132         CamelStoreClass *camel_store_class =
133                 CAMEL_STORE_CLASS (camel_imap_store_class);
134         CamelDiscoStoreClass *camel_disco_store_class =
135                 CAMEL_DISCO_STORE_CLASS (camel_imap_store_class);
136         
137         parent_class = CAMEL_DISCO_STORE_CLASS (camel_type_get_global_classfuncs (camel_disco_store_get_type ()));
138         
139         /* virtual method overload */
140         camel_object_class->setv = imap_setv;
141         camel_object_class->getv = imap_getv;
142         
143         camel_service_class->construct = construct;
144         camel_service_class->query_auth_types = query_auth_types;
145         camel_service_class->get_name = imap_get_name;
146         
147         camel_store_class->hash_folder_name = hash_folder_name;
148         camel_store_class->compare_folder_name = compare_folder_name;
149         camel_store_class->create_folder = create_folder;
150         camel_store_class->delete_folder = delete_folder;
151         camel_store_class->rename_folder = rename_folder;
152         camel_store_class->free_folder_info = camel_store_free_folder_info_full;
153         camel_store_class->folder_subscribed = folder_subscribed;
154         camel_store_class->subscribe_folder = subscribe_folder;
155         camel_store_class->unsubscribe_folder = unsubscribe_folder;
156         camel_store_class->noop = imap_noop;
157         
158         camel_disco_store_class->can_work_offline = can_work_offline;
159         camel_disco_store_class->connect_online = imap_connect_online;
160         camel_disco_store_class->connect_offline = imap_connect_offline;
161         camel_disco_store_class->disconnect_online = imap_disconnect_online;
162         camel_disco_store_class->disconnect_offline = imap_disconnect_offline;
163         camel_disco_store_class->get_folder_online = get_folder_online;
164         camel_disco_store_class->get_folder_offline = get_folder_offline;
165         camel_disco_store_class->get_folder_resyncing = get_folder_online;
166         camel_disco_store_class->get_folder_info_online = get_folder_info_online;
167         camel_disco_store_class->get_folder_info_offline = get_folder_info_offline;
168         camel_disco_store_class->get_folder_info_resyncing = get_folder_info_online;
169 }
170
171 static gboolean
172 free_key (gpointer key, gpointer value, gpointer user_data)
173 {
174         g_free (key);
175         return TRUE;
176 }
177
178 static void
179 camel_imap_store_finalize (CamelObject *object)
180 {
181         CamelImapStore *imap_store = CAMEL_IMAP_STORE (object);
182
183         /* This frees current_folder, folders, authtypes, streams, and namespace. */
184         camel_service_disconnect((CamelService *)imap_store, TRUE, NULL);
185
186         if (imap_store->summary) {
187                 camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
188                 camel_object_unref(imap_store->summary);
189         }
190         
191         if (imap_store->base_url)
192                 g_free (imap_store->base_url);
193         if (imap_store->storage_path)
194                 g_free (imap_store->storage_path);
195         
196 #ifdef ENABLE_THREADS
197         e_thread_destroy (imap_store->async_thread);
198 #endif
199 }
200
201 #ifdef ENABLE_THREADS
202 static void async_destroy(EThread *et, EMsg *em, void *data)
203 {
204         CamelImapStore *imap_store = data;
205         CamelImapMsg *msg = (CamelImapMsg *)em;
206         
207         if (msg->free)
208                 msg->free (imap_store, msg);
209         
210         g_free (msg);
211 }
212
213 static void async_received(EThread *et, EMsg *em, void *data)
214 {
215         CamelImapStore *imap_store = data;
216         CamelImapMsg *msg = (CamelImapMsg *)em;
217
218         if (msg->receive)
219                 msg->receive(imap_store, msg);
220 }
221
222 CamelImapMsg *camel_imap_msg_new(void (*receive)(CamelImapStore *store, struct _CamelImapMsg *m),
223                                  void (*free)(CamelImapStore *store, struct _CamelImapMsg *m),
224                                  size_t size)
225 {
226         CamelImapMsg *msg;
227
228         g_assert(size >= sizeof(*msg));
229
230         msg = g_malloc0(size);
231         msg->receive = receive;
232         msg->free = free;
233
234         return msg;
235 }
236
237 void camel_imap_msg_queue(CamelImapStore *store, CamelImapMsg *msg)
238 {
239         e_thread_put(store->async_thread, (EMsg *)msg);
240 }
241
242 #endif
243
244 static void
245 camel_imap_store_init (gpointer object, gpointer klass)
246 {
247         CamelImapStore *imap_store = CAMEL_IMAP_STORE (object);
248         
249         imap_store->istream = NULL;
250         imap_store->ostream = NULL;
251         
252         imap_store->dir_sep = '\0';
253         imap_store->current_folder = NULL;
254         imap_store->connected = FALSE;
255         imap_store->preauthed = FALSE;
256         
257         imap_store->tag_prefix = imap_tag_prefix++;
258         if (imap_tag_prefix > 'Z')
259                 imap_tag_prefix = 'A';
260         
261 #ifdef ENABLE_THREADS
262         imap_store->async_thread = e_thread_new(E_THREAD_QUEUE);
263         e_thread_set_msg_destroy(imap_store->async_thread, async_destroy, imap_store);
264         e_thread_set_msg_received(imap_store->async_thread, async_received, imap_store);
265 #endif /* ENABLE_THREADS */
266 }
267
268 CamelType
269 camel_imap_store_get_type (void)
270 {
271         static CamelType camel_imap_store_type = CAMEL_INVALID_TYPE;
272         
273         if (camel_imap_store_type == CAMEL_INVALID_TYPE)        {
274                 camel_imap_store_type =
275                         camel_type_register (CAMEL_DISCO_STORE_TYPE,
276                                              "CamelImapStore",
277                                              sizeof (CamelImapStore),
278                                              sizeof (CamelImapStoreClass),
279                                              (CamelObjectClassInitFunc) camel_imap_store_class_init,
280                                              NULL,
281                                              (CamelObjectInitFunc) camel_imap_store_init,
282                                              (CamelObjectFinalizeFunc) camel_imap_store_finalize);
283         }
284         
285         return camel_imap_store_type;
286 }
287
288 static void
289 construct (CamelService *service, CamelSession *session,
290            CamelProvider *provider, CamelURL *url,
291            CamelException *ex)
292 {
293         CamelImapStore *imap_store = CAMEL_IMAP_STORE (service);
294         CamelStore *store = CAMEL_STORE (service);
295         char *tmp;
296         CamelURL *summary_url;
297
298         CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
299         if (camel_exception_is_set (ex))
300                 return;
301
302         imap_store->storage_path = camel_session_get_storage_path (session, service, ex);
303         if (!imap_store->storage_path)
304                 return;
305
306         /* FIXME */
307         imap_store->base_url = camel_url_to_string (service->url, (CAMEL_URL_HIDE_PASSWORD |
308                                                                    CAMEL_URL_HIDE_PARAMS |
309                                                                    CAMEL_URL_HIDE_AUTH));
310
311         imap_store->parameters = 0;
312         if (camel_url_get_param (url, "use_lsub"))
313                 store->flags |= CAMEL_STORE_SUBSCRIPTIONS;
314         if (camel_url_get_param (url, "namespace")) {
315                 imap_store->parameters |= IMAP_PARAM_OVERRIDE_NAMESPACE;
316                 g_free(imap_store->namespace);
317                 imap_store->namespace = g_strdup (camel_url_get_param (url, "namespace"));
318         }
319         if (camel_url_get_param (url, "check_all"))
320                 imap_store->parameters |= IMAP_PARAM_CHECK_ALL;
321         if (camel_url_get_param (url, "filter")) {
322                 imap_store->parameters |= IMAP_PARAM_FILTER_INBOX;
323                 store->flags |= CAMEL_STORE_FILTER_INBOX;
324         }
325
326         /* setup/load the store summary */
327         tmp = alloca(strlen(imap_store->storage_path)+32);
328         sprintf(tmp, "%s/.ev-store-summary", imap_store->storage_path);
329         imap_store->summary = camel_imap_store_summary_new();
330         camel_store_summary_set_filename((CamelStoreSummary *)imap_store->summary, tmp);
331         summary_url = camel_url_new(imap_store->base_url, NULL);
332         camel_store_summary_set_uri_base((CamelStoreSummary *)imap_store->summary, summary_url);
333         camel_url_free(summary_url);
334         if (camel_store_summary_load((CamelStoreSummary *)imap_store->summary) == 0) {
335                 CamelImapStoreSummary *is = imap_store->summary;
336
337                 if (is->namespace) {
338                         /* if namespace has changed, clear folder list */
339                         if (imap_store->namespace && strcmp(imap_store->namespace, is->namespace->full_name) != 0) {
340                                 camel_store_summary_clear((CamelStoreSummary *)is);
341                         } else {
342                                 imap_store->namespace = g_strdup(is->namespace->full_name);
343                                 imap_store->dir_sep = is->namespace->sep;
344                                 store->dir_sep = is->namespace->sep;
345                         }
346                 }
347  
348                 imap_store->capabilities = is->capabilities;
349                 imap_set_server_level(imap_store);
350         }
351 }
352
353 static int
354 imap_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
355 {
356         CamelImapStore *store = (CamelImapStore *) object;
357         guint32 tag, flags;
358         int i;
359         
360         for (i = 0; i < args->argc; i++) {
361                 tag = args->argv[i].tag;
362                 
363                 /* make sure this arg wasn't already handled */
364                 if (tag & CAMEL_ARG_IGNORE)
365                         continue;
366                 
367                 /* make sure this is an arg we're supposed to handle */
368                 if ((tag & CAMEL_ARG_TAG) <= CAMEL_IMAP_STORE_ARG_FIRST ||
369                     (tag & CAMEL_ARG_TAG) >= CAMEL_IMAP_STORE_ARG_FIRST + 100)
370                         continue;
371                 
372                 if (tag == CAMEL_IMAP_STORE_NAMESPACE) {
373                         if (strcmp (store->namespace, args->argv[i].ca_str) != 0) {
374                                 g_free (store->namespace);
375                                 store->namespace = g_strdup (args->argv[i].ca_str);
376                                 /* the current imap code will need to do a reconnect for this to take effect */
377                                 /*reconnect = TRUE;*/
378                         }
379                 } else if (tag == CAMEL_IMAP_STORE_OVERRIDE_NAMESPACE) {
380                         flags = args->argv[i].ca_int ? IMAP_PARAM_OVERRIDE_NAMESPACE : 0;
381                         flags |= (store->parameters & ~IMAP_PARAM_OVERRIDE_NAMESPACE);
382                         
383                         if (store->parameters != flags) {
384                                 store->parameters = flags;
385                                 /* the current imap code will need to do a reconnect for this to take effect */
386                                 /*reconnect = TRUE;*/
387                         }
388                 } else if (tag == CAMEL_IMAP_STORE_CHECK_ALL) {
389                         flags = args->argv[i].ca_int ? IMAP_PARAM_CHECK_ALL : 0;
390                         flags |= (store->parameters & ~IMAP_PARAM_CHECK_ALL);
391                         store->parameters = flags;
392                         /* no need to reconnect for this option to take effect... */
393                 } else if (tag == CAMEL_IMAP_STORE_FILTER_INBOX) {
394                         flags = args->argv[i].ca_int ? IMAP_PARAM_FILTER_INBOX : 0;
395                         flags |= (store->parameters & ~IMAP_PARAM_FILTER_INBOX);
396                         store->parameters = flags;
397                         /* no need to reconnect for this option to take effect... */
398                 } else {
399                         /* error?? */
400                         continue;
401                 }
402                 
403                 /* let our parent know that we've handled this arg */
404                 camel_argv_ignore (args, i);
405         }
406         
407         /* FIXME: if we need to reconnect for a change to take affect,
408            we need to do it here... or, better yet, somehow chain it
409            up to CamelService's setv implementation. */
410         
411         return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
412 }
413
414 static int
415 imap_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
416 {
417         CamelImapStore *store = (CamelImapStore *) object;
418         guint32 tag;
419         int i;
420         
421         for (i = 0; i < args->argc; i++) {
422                 tag = args->argv[i].tag;
423                 
424                 /* make sure this is an arg we're supposed to handle */
425                 if ((tag & CAMEL_ARG_TAG) <= CAMEL_IMAP_STORE_ARG_FIRST ||
426                     (tag & CAMEL_ARG_TAG) >= CAMEL_IMAP_STORE_ARG_FIRST + 100)
427                         continue;
428                 
429                 switch (tag) {
430                 case CAMEL_IMAP_STORE_NAMESPACE:
431                         /* get the username */
432                         *args->argv[i].ca_str = store->namespace;
433                         break;
434                 case CAMEL_IMAP_STORE_OVERRIDE_NAMESPACE:
435                         /* get the auth mechanism */
436                         *args->argv[i].ca_int = store->parameters & IMAP_PARAM_OVERRIDE_NAMESPACE ? TRUE : FALSE;
437                         break;
438                 case CAMEL_IMAP_STORE_CHECK_ALL:
439                         /* get the hostname */
440                         *args->argv[i].ca_int = store->parameters & IMAP_PARAM_CHECK_ALL ? TRUE : FALSE;
441                         break;
442                 case CAMEL_IMAP_STORE_FILTER_INBOX:
443                         /* get the port */
444                         *args->argv[i].ca_int = store->parameters & IMAP_PARAM_FILTER_INBOX ? TRUE : FALSE;
445                         break;
446                 default:
447                         /* error? */
448                         break;
449                 }
450         }
451         
452         return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
453 }
454
455 static char *
456 imap_get_name (CamelService *service, gboolean brief)
457 {
458         if (brief)
459                 return g_strdup_printf (_("IMAP server %s"), service->url->host);
460         else
461                 return g_strdup_printf (_("IMAP service for %s on %s"),
462                                         service->url->user, service->url->host);
463 }
464
465 static void
466 imap_set_server_level (CamelImapStore *store)
467 {
468         if (store->capabilities & IMAP_CAPABILITY_IMAP4REV1) {
469                 store->server_level = IMAP_LEVEL_IMAP4REV1;
470                 store->capabilities |= IMAP_CAPABILITY_STATUS;
471         } else if (store->capabilities & IMAP_CAPABILITY_IMAP4)
472                 store->server_level = IMAP_LEVEL_IMAP4;
473         else
474                 store->server_level = IMAP_LEVEL_UNKNOWN;
475 }
476
477 static struct {
478         const char *name;
479         guint32 flag;
480 } capabilities[] = {
481         { "IMAP4",              IMAP_CAPABILITY_IMAP4 },
482         { "IMAP4REV1",          IMAP_CAPABILITY_IMAP4REV1 },
483         { "STATUS",             IMAP_CAPABILITY_STATUS },
484         { "NAMESPACE",          IMAP_CAPABILITY_NAMESPACE },
485         { "UIDPLUS",            IMAP_CAPABILITY_UIDPLUS },
486         { "LITERAL+",           IMAP_CAPABILITY_LITERALPLUS },
487         { "STARTTLS",           IMAP_CAPABILITY_STARTTLS },
488         { NULL, 0 }
489 };
490
491
492 static gboolean
493 imap_get_capability (CamelService *service, CamelException *ex)
494 {
495         CamelImapStore *store = CAMEL_IMAP_STORE (service);
496         CamelImapResponse *response;
497         char *result, *capa, *lasts;
498         int i;
499         
500         CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
501         
502         /* Find out the IMAP capabilities */
503         /* We assume we have utf8 capable search until a failed search tells us otherwise */
504         store->capabilities = IMAP_CAPABILITY_utf8_search;
505         store->authtypes = g_hash_table_new (g_str_hash, g_str_equal);
506         response = camel_imap_command (store, NULL, ex, "CAPABILITY");
507         if (!response)
508                 return FALSE;
509         result = camel_imap_response_extract (store, response, "CAPABILITY ", ex);
510         if (!result)
511                 return FALSE;
512         
513         /* Skip over "* CAPABILITY ". */
514         capa = result + 13;
515         for (capa = strtok_r (capa, " ", &lasts); capa;
516              capa = strtok_r (NULL, " ", &lasts)) {
517                 if (!strncmp (capa, "AUTH=", 5)) {
518                         g_hash_table_insert (store->authtypes,
519                                              g_strdup (capa + 5),
520                                              GINT_TO_POINTER (1));
521                         continue;
522                 }
523                 for (i = 0; capabilities[i].name; i++) {
524                         if (strcasecmp (capa, capabilities[i].name) == 0) {
525                                 store->capabilities |= capabilities[i].flag;
526                                 break;
527                         }
528                 }
529         }
530         g_free (result);
531         
532         imap_set_server_level (store);
533         
534         if (store->summary->capabilities != store->capabilities) {
535                 store->summary->capabilities = store->capabilities;
536                 camel_store_summary_touch((CamelStoreSummary *)store->summary);
537                 camel_store_summary_save((CamelStoreSummary *)store->summary);
538         }
539         
540         return TRUE;
541 }
542
543 enum {
544         USE_SSL_NEVER,
545         USE_SSL_ALWAYS,
546         USE_SSL_WHEN_POSSIBLE
547 };
548
549 #define SSL_PORT_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3)
550 #define STARTTLS_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_TLS)
551
552 static gboolean
553 connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelException *ex)
554 {
555         CamelImapStore *store = (CamelImapStore *) service;
556         CamelImapResponse *response;
557         CamelStream *tcp_stream;
558         struct hostent *h;
559         int clean_quit;
560         int port, ret;
561         char *buf;
562         
563         h = camel_service_gethost (service, ex);
564         if (!h)
565                 return FALSE;
566         
567         port = service->url->port ? service->url->port : 143;
568         
569         if (ssl_mode != USE_SSL_NEVER) {
570 #ifdef HAVE_SSL
571                 if (try_starttls) {
572                         tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS);
573                 } else {
574                         port = service->url->port ? service->url->port : 993;
575                         tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS);
576                 }
577 #else
578                 if (!try_starttls)
579                         port = service->url->port ? service->url->port : 993;
580                 
581                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
582                                       _("Could not connect to %s (port %d): %s"),
583                                       service->url->host, port,
584                                       _("SSL unavailable"));
585                 
586                 camel_free_host (h);
587                 
588                 return FALSE;
589 #endif /* HAVE_SSL */
590         } else {
591                 tcp_stream = camel_tcp_stream_raw_new ();
592         }
593         
594         ret = camel_tcp_stream_connect (CAMEL_TCP_STREAM (tcp_stream), h, port);
595         camel_free_host (h);
596         if (ret == -1) {
597                 if (errno == EINTR)
598                         camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
599                                              _("Connection cancelled"));
600                 else
601                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
602                                               _("Could not connect to %s (port %d): %s"),
603                                               service->url->host, port, g_strerror (errno));
604                 
605                 camel_object_unref (CAMEL_OBJECT (tcp_stream));
606                 
607                 return FALSE;
608         }
609         
610         store->ostream = tcp_stream;
611         store->istream = camel_stream_buffer_new (tcp_stream, CAMEL_STREAM_BUFFER_READ);
612         
613         store->connected = TRUE;
614         store->preauthed = FALSE;
615         store->command = 0;
616         
617         /* Read the greeting, if any, and deal with PREAUTH */
618         if (camel_imap_store_readline (store, &buf, ex) < 0) {
619                 if (store->istream) {
620                         camel_object_unref (CAMEL_OBJECT (store->istream));
621                         store->istream = NULL;
622                 }
623                 
624                 if (store->ostream) {
625                         camel_object_unref (CAMEL_OBJECT (store->ostream));
626                         store->ostream = NULL;
627                 }
628                 
629                 store->connected = FALSE;
630                 
631                 return FALSE;
632         }
633         if (!strncmp(buf, "* PREAUTH", 9))
634                 store->preauthed = TRUE;
635         g_free (buf);
636         
637         /* get the imap server capabilities */
638         if (!imap_get_capability (service, ex)) {
639                 if (store->istream) {
640                         camel_object_unref (CAMEL_OBJECT (store->istream));
641                         store->istream = NULL;
642                 }
643                 
644                 if (store->ostream) {
645                         camel_object_unref (CAMEL_OBJECT (store->ostream));
646                         store->ostream = NULL;
647                 }
648                 
649                 store->connected = FALSE;
650                 return FALSE;
651         }
652         
653 #ifdef HAVE_SSL
654         if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
655                 if (store->capabilities & IMAP_CAPABILITY_STARTTLS)
656                         goto starttls;
657         } else if (ssl_mode == USE_SSL_ALWAYS) {
658                 if (try_starttls) {
659                         if (store->capabilities & IMAP_CAPABILITY_STARTTLS) {
660                                 /* attempt to toggle STARTTLS mode */
661                                 goto starttls;
662                         } else {
663                                 /* server doesn't support STARTTLS, abort */
664                                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
665                                                       _("Failed to connect to IMAP server %s in secure mode: %s"),
666                                                       service->url->host, _("SSL/TLS extension not supported."));
667                                 /* we have the possibility of quitting cleanly here */
668                                 clean_quit = TRUE;
669                                 goto exception;
670                         }
671                 }
672         }
673 #endif /* HAVE_SSL */
674         
675         return TRUE;
676         
677 #ifdef HAVE_SSL
678  starttls:
679         
680         /* as soon as we send a STARTTLS command, all hope is lost of a clean QUIT if problems arise */
681         clean_quit = FALSE;
682         
683         response = camel_imap_command (store, NULL, ex, "STARTTLS");
684         if (!response) {
685                 camel_object_unref (CAMEL_OBJECT (store->istream));
686                 camel_object_unref (CAMEL_OBJECT (store->ostream));
687                 store->istream = store->ostream = NULL;
688                 return FALSE;
689         }
690         
691         camel_imap_response_free_without_processing (store, response);
692         
693         /* Okay, now toggle SSL/TLS mode */
694         if (camel_tcp_stream_ssl_enable_ssl (CAMEL_TCP_STREAM_SSL (tcp_stream)) == -1) {
695                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
696                                       _("Failed to connect to IMAP server %s in secure mode: %s"),
697                                       service->url->host, _("SSL negotiations failed"));
698                 goto exception;
699         }
700         
701         /* rfc2595, section 4 states that after a successful STLS
702            command, the client MUST discard prior CAPA responses */
703         if (!imap_get_capability (service, ex)) {
704                 if (store->istream) {
705                         camel_object_unref (CAMEL_OBJECT (store->istream));
706                         store->istream = NULL;
707                 }
708                 
709                 if (store->ostream) {
710                         camel_object_unref (CAMEL_OBJECT (store->ostream));
711                         store->ostream = NULL;
712                 }
713                 
714                 store->connected = FALSE;
715                 
716                 return FALSE;
717         }
718         
719         return TRUE;
720         
721  exception:
722         
723         if (clean_quit && store->connected) {
724                 /* try to disconnect cleanly */
725                 response = camel_imap_command (store, NULL, ex, "LOGOUT");
726                 if (response)
727                         camel_imap_response_free_without_processing (store, response);
728         }
729         
730         if (store->istream) {
731                 camel_object_unref (CAMEL_OBJECT (store->istream));
732                 store->istream = NULL;
733         }
734         
735         if (store->ostream) {
736                 camel_object_unref (CAMEL_OBJECT (store->ostream));
737                 store->ostream = NULL;
738         }
739         
740         store->connected = FALSE;
741         
742         return FALSE;
743 #endif /* HAVE_SSL */
744 }
745
746 static gboolean
747 connect_to_server_process (CamelService *service, const char *cmd, CamelException *ex)
748 {
749         CamelImapStore *store = (CamelImapStore *) service;
750         CamelStream *cmd_stream;
751         int ret, i = 0;
752         char *buf;
753         char *cmd_copy;
754         char *full_cmd;
755         char *child_env[7];
756
757         /* Put full details in the environment, in case the connection 
758            program needs them */
759         buf = camel_url_to_string(service->url, 0);
760         child_env[i++] = g_strdup_printf("URL=%s", buf);
761         g_free(buf);
762
763         child_env[i++] = g_strdup_printf("URLHOST=%s", service->url->host);
764         if (service->url->port)
765                 child_env[i++] = g_strdup_printf("URLPORT=%d", service->url->port);
766         if (service->url->user)
767                 child_env[i++] = g_strdup_printf("URLUSER=%s", service->url->user);
768         if (service->url->passwd)
769                 child_env[i++] = g_strdup_printf("URLPASSWD=%s", service->url->passwd);
770         if (service->url->path)
771                 child_env[i++] = g_strdup_printf("URLPATH=%s", service->url->path);
772         child_env[i] = NULL;
773
774         /* Now do %h, %u, etc. substitution in cmd */
775         buf = cmd_copy = g_strdup(cmd);
776
777         full_cmd = g_strdup("");
778
779         for(;;) {
780                 char *pc;
781                 char *tmp;
782                 char *var;
783                 int len;
784
785                 pc = strchr(buf, '%');
786         ignore:
787                 if (!pc) {
788                         tmp = g_strdup_printf("%s%s", full_cmd, buf);
789                         g_free(full_cmd);
790                         full_cmd = tmp;
791                         break;
792                 }
793                 
794                 len = pc - buf;
795
796                 var = NULL;
797
798                 switch(pc[1]) {
799                 case 'h':
800                         var = service->url->host;
801                         break;
802                 case 'u':
803                         var = service->url->user;
804                         break;
805                 }
806                 if (!var) {
807                         /* If there wasn't a valid %-code, with an actual
808                            variable to insert, pretend we didn't see the % */
809                         pc = strchr(pc + 1, '%');
810                         goto ignore;
811                 }
812                 tmp = g_strdup_printf("%s%.*s%s", full_cmd, len, buf, var);
813                 g_free(full_cmd);
814                 full_cmd = tmp;
815                 buf = pc + 2;
816         }
817                         
818         g_free(cmd_copy);
819
820         cmd_stream = camel_stream_process_new ();
821         
822         ret = camel_stream_process_connect (CAMEL_STREAM_PROCESS(cmd_stream),
823                                             full_cmd, (const char **)child_env);
824
825         while (i)
826                 g_free(child_env[--i]);
827                 
828         if (ret == -1) {
829                 if (errno == EINTR)
830                         camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
831                                              _("Connection cancelled"));
832                 else
833                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
834                                               _("Could not connect with command \"%s\": %s"),
835                                               full_cmd, g_strerror (errno));
836                 
837                 camel_object_unref (CAMEL_OBJECT (cmd_stream));
838                 g_free(full_cmd);
839                 return FALSE;
840         }
841         g_free(full_cmd);
842         
843         store->ostream = cmd_stream;
844         store->istream = camel_stream_buffer_new (cmd_stream, CAMEL_STREAM_BUFFER_READ);
845         
846         store->connected = TRUE;
847         store->preauthed = FALSE;
848         store->command = 0;
849         
850         /* Read the greeting, if any, and deal with PREAUTH */
851         if (camel_imap_store_readline (store, &buf, ex) < 0) {
852                 if (store->istream) {
853                         camel_object_unref (CAMEL_OBJECT (store->istream));
854                         store->istream = NULL;
855                 }
856                 
857                 if (store->ostream) {
858                         camel_object_unref (CAMEL_OBJECT (store->ostream));
859                         store->ostream = NULL;
860                 }
861                 
862                 store->connected = FALSE;
863                 return FALSE;
864         }
865         if (!strncmp(buf, "* PREAUTH", 9))
866                 store->preauthed = TRUE;
867         g_free (buf);
868         
869         /* get the imap server capabilities */
870         if (!imap_get_capability (service, ex)) {
871                 if (store->istream) {
872                         camel_object_unref (CAMEL_OBJECT (store->istream));
873                         store->istream = NULL;
874                 }
875                 
876                 if (store->ostream) {
877                         camel_object_unref (CAMEL_OBJECT (store->ostream));
878                         store->ostream = NULL;
879                 }
880                 
881                 store->connected = FALSE;
882                 return FALSE;
883         }
884         
885         return TRUE;
886         
887 }
888
889 static struct {
890         char *value;
891         int mode;
892 } ssl_options[] = {
893         { "",              USE_SSL_ALWAYS        },
894         { "always",        USE_SSL_ALWAYS        },
895         { "when-possible", USE_SSL_WHEN_POSSIBLE },
896         { "never",         USE_SSL_NEVER         },
897         { NULL,            USE_SSL_NEVER         },
898 };
899
900 static gboolean
901 connect_to_server_wrapper (CamelService *service, CamelException *ex)
902 {
903         const char *command;
904 #ifdef HAVE_SSL
905         const char *use_ssl;
906         int i, ssl_mode;
907 #endif
908         command = camel_url_get_param (service->url, "command");
909         if (command)
910                 return connect_to_server_process (service, command, ex);
911
912 #ifdef HAVE_SSL
913         use_ssl = camel_url_get_param (service->url, "use_ssl");
914         if (use_ssl) {
915                 for (i = 0; ssl_options[i].value; i++)
916                         if (!strcmp (ssl_options[i].value, use_ssl))
917                                 break;
918                 ssl_mode = ssl_options[i].mode;
919         } else
920                 ssl_mode = USE_SSL_NEVER;
921         
922         if (ssl_mode == USE_SSL_ALWAYS) {
923                 /* First try the ssl port */
924                 if (!connect_to_server (service, ssl_mode, FALSE, ex)) {
925                         if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) {
926                                 /* The ssl port seems to be unavailable, lets try STARTTLS */
927                                 camel_exception_clear (ex);
928                                 return connect_to_server (service, ssl_mode, TRUE, ex);
929                         } else {
930                                 return FALSE;
931                         }
932                 }
933                 
934                 return TRUE;
935         } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
936                 /* If the server supports STARTTLS, use it */
937                 return connect_to_server (service, ssl_mode, TRUE, ex);
938         } else {
939                 /* User doesn't care about SSL */
940                 return connect_to_server (service, ssl_mode, FALSE, ex);
941         }
942 #else
943         return connect_to_server (service, USE_SSL_NEVER, FALSE, ex);
944 #endif
945 }
946
947 extern CamelServiceAuthType camel_imap_password_authtype;
948
949 static GList *
950 query_auth_types (CamelService *service, CamelException *ex)
951 {
952         CamelImapStore *store = CAMEL_IMAP_STORE (service);
953         CamelServiceAuthType *authtype;
954         GList *sasl_types, *t, *next;
955         gboolean connected;
956         
957         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
958                 return NULL;
959         
960         CAMEL_SERVICE_LOCK (store, connect_lock);
961         connected = connect_to_server_wrapper (service, ex);
962         CAMEL_SERVICE_UNLOCK (store, connect_lock);
963         if (!connected)
964                 return NULL;
965         
966         sasl_types = camel_sasl_authtype_list (FALSE);
967         for (t = sasl_types; t; t = next) {
968                 authtype = t->data;
969                 next = t->next;
970                 
971                 if (!g_hash_table_lookup (store->authtypes, authtype->authproto)) {
972                         sasl_types = g_list_remove_link (sasl_types, t);
973                         g_list_free_1 (t);
974                 }
975         }
976         
977         return g_list_prepend (sasl_types, &camel_imap_password_authtype);
978 }
979
980 /* folder_name is path name */
981 static CamelFolderInfo *
982 imap_build_folder_info(CamelImapStore *imap_store, const char *folder_name)
983 {
984         CamelURL *url;
985         const char *name;
986         CamelFolderInfo *fi;
987
988         fi = g_malloc0(sizeof(*fi));
989
990         fi->full_name = g_strdup(folder_name);
991         fi->unread_message_count = 0;
992
993         url = camel_url_new (imap_store->base_url, NULL);
994         g_free (url->path);
995         url->path = g_strdup_printf ("/%s", folder_name);
996         fi->url = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
997         camel_url_free(url);
998         fi->path = g_strdup_printf("/%s", folder_name);
999         name = strrchr (fi->path, '/');
1000         if (name)
1001                 name++;
1002         else
1003                 name = fi->path;
1004
1005         fi->name = g_strdup (name);
1006
1007         return fi;
1008 }
1009
1010 static void
1011 imap_folder_effectively_unsubscribed(CamelImapStore *imap_store, 
1012                                      const char *folder_name, CamelException *ex)
1013 {
1014         CamelFolderInfo *fi;
1015         CamelStoreInfo *si;
1016
1017         si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
1018         if (si) {
1019                 if (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) {
1020                         si->flags &= ~CAMEL_STORE_INFO_FOLDER_SUBSCRIBED;
1021                         camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
1022                         camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
1023                 }
1024                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
1025         }
1026
1027         if (imap_store->renaming) {
1028                 /* we don't need to emit a "folder_unsubscribed" signal
1029                    if we are in the process of renaming folders, so we
1030                    are done here... */
1031                 return;
1032
1033         }
1034
1035         fi = imap_build_folder_info(imap_store, folder_name);
1036         camel_object_trigger_event (CAMEL_OBJECT (imap_store), "folder_unsubscribed", fi);
1037         camel_folder_info_free (fi);
1038 }
1039
1040 static void
1041 imap_forget_folder (CamelImapStore *imap_store, const char *folder_name, CamelException *ex)
1042 {
1043         CamelFolderSummary *summary;
1044         CamelImapMessageCache *cache;
1045         char *summary_file;
1046         char *journal_file;
1047         char *folder_dir, *storage_path;
1048         CamelFolderInfo *fi;
1049         const char *name;
1050
1051         name = strrchr (folder_name, imap_store->dir_sep);
1052         if (name)
1053                 name++;
1054         else
1055                 name = folder_name;
1056         
1057         storage_path = g_strdup_printf ("%s/folders", imap_store->storage_path);
1058         folder_dir = e_path_to_physical (storage_path, folder_name);
1059         g_free (storage_path);
1060         if (access (folder_dir, F_OK) != 0) {
1061                 g_free (folder_dir);
1062                 goto event;
1063         }
1064         
1065         summary_file = g_strdup_printf ("%s/summary", folder_dir);
1066         summary = camel_imap_summary_new (summary_file);
1067         if (!summary) {
1068                 g_free (summary_file);
1069                 g_free (folder_dir);
1070                 goto event;
1071         }
1072         
1073         cache = camel_imap_message_cache_new (folder_dir, summary, ex);
1074         if (cache)
1075                 camel_imap_message_cache_clear (cache);
1076         
1077         camel_object_unref (cache);
1078         camel_object_unref (summary);
1079         
1080         unlink (summary_file);
1081         g_free (summary_file);
1082         
1083         journal_file = g_strdup_printf ("%s/summary", folder_dir);
1084         unlink (journal_file);
1085         g_free (journal_file);
1086         
1087         rmdir (folder_dir);
1088         g_free (folder_dir);
1089         
1090  event:
1091
1092         camel_store_summary_remove_path((CamelStoreSummary *)imap_store->summary, folder_name);
1093         camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
1094
1095         fi = imap_build_folder_info(imap_store, folder_name);
1096         camel_object_trigger_event (CAMEL_OBJECT (imap_store), "folder_deleted", fi);
1097         camel_folder_info_free (fi);
1098 }
1099
1100 static gboolean
1101 imap_check_folder_still_extant (CamelImapStore *imap_store, const char *full_name, 
1102                                 CamelException *ex)
1103 {
1104         CamelImapResponse *response;
1105
1106         response = camel_imap_command (imap_store, NULL, ex, "LIST \"\" %S",
1107                                        full_name);
1108
1109         if (response) {
1110                 gboolean stillthere = response->untagged->len != 0;
1111
1112                 camel_imap_response_free_without_processing (imap_store, response);
1113
1114                 return stillthere;
1115         }
1116
1117         /* if the command was rejected, there must be some other error,
1118            assume it worked so we dont blow away the folder unecessarily */
1119         return TRUE;
1120 }
1121
1122 /* This is a little 'hack' to avoid the deadlock conditions that would otherwise
1123    ensue when calling camel_folder_refresh_info from inside a lock */
1124 /* NB: on second thougts this is probably not entirely safe, but it'll do for now */
1125 /* No, its definetly not safe.  So its been changed to copy the folders first */
1126 /* the alternative is to:
1127    make the camel folder->lock recursive (which should probably be done)
1128    or remove it from camel_folder_refresh_info, and use another locking mechanism */
1129 /* also see get_folder_info_online() for the same hack repeated */
1130 static void
1131 imap_store_refresh_folders (CamelImapStore *store, CamelException *ex)
1132 {
1133         GPtrArray *folders;
1134         int i;
1135         
1136         folders = camel_object_bag_list(CAMEL_STORE (store)->folders);
1137         
1138         for (i = 0; i <folders->len; i++) {
1139                 CamelFolder *folder = folders->pdata[i];
1140
1141                 CAMEL_IMAP_FOLDER (folder)->need_rescan = TRUE;
1142                 if (!camel_exception_is_set(ex))
1143                         CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(folder))->refresh_info(folder, ex);
1144
1145                 if (camel_exception_is_set (ex) &&
1146                     imap_check_folder_still_extant (store, folder->full_name, ex) == FALSE) {
1147                         gchar *namedup;
1148                         
1149                         /* the folder was deleted (may happen when we come back online
1150                          * after being offline */
1151                         
1152                         namedup = g_strdup (folder->full_name);
1153                         camel_object_unref(folder);
1154                         imap_folder_effectively_unsubscribed (store, namedup, ex);
1155                         imap_forget_folder (store, namedup, ex);
1156                         g_free (namedup);
1157                 } else
1158                         camel_object_unref(folder);
1159         }
1160         
1161         g_ptr_array_free (folders, TRUE);
1162 }       
1163
1164 static gboolean
1165 try_auth (CamelImapStore *store, const char *mech, CamelException *ex)
1166 {
1167         CamelSasl *sasl;
1168         CamelImapResponse *response;
1169         char *resp;
1170         char *sasl_resp;
1171         
1172         CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
1173         
1174         response = camel_imap_command (store, NULL, ex, "AUTHENTICATE %s", mech);
1175         if (!response)
1176                 return FALSE;
1177         
1178         sasl = camel_sasl_new ("imap", mech, CAMEL_SERVICE (store));
1179         while (!camel_sasl_authenticated (sasl)) {
1180                 resp = camel_imap_response_extract_continuation (store, response, ex);
1181                 if (!resp)
1182                         goto lose;
1183                 
1184                 sasl_resp = camel_sasl_challenge_base64 (sasl, imap_next_word (resp), ex);
1185                 g_free (resp);
1186                 if (camel_exception_is_set (ex))
1187                         goto break_and_lose;
1188                 
1189                 response = camel_imap_command_continuation (store, sasl_resp, strlen (sasl_resp), ex);
1190                 g_free (sasl_resp);
1191                 if (!response)
1192                         goto lose;
1193         }
1194         
1195         resp = camel_imap_response_extract_continuation (store, response, NULL);
1196         if (resp) {
1197                 /* Oops. SASL claims we're done, but the IMAP server
1198                  * doesn't think so...
1199                  */
1200                 g_free (resp);
1201                 goto lose;
1202         }
1203         
1204         camel_object_unref (CAMEL_OBJECT (sasl));
1205         
1206         return TRUE;
1207         
1208  break_and_lose:
1209         /* Get the server out of "waiting for continuation data" mode. */
1210         response = camel_imap_command_continuation (store, "*", 1, NULL);
1211         if (response)
1212                 camel_imap_response_free (store, response);
1213         
1214  lose:
1215         if (!camel_exception_is_set (ex)) {
1216                 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
1217                                      _("Bad authentication response from server."));
1218         }
1219         
1220         camel_object_unref (CAMEL_OBJECT (sasl));
1221         
1222         return FALSE;
1223 }
1224
1225 static gboolean
1226 imap_auth_loop (CamelService *service, CamelException *ex)
1227 {
1228         CamelImapStore *store = CAMEL_IMAP_STORE (service);
1229         CamelSession *session = camel_service_get_session (service);
1230         CamelServiceAuthType *authtype = NULL;
1231         CamelImapResponse *response;
1232         char *errbuf = NULL;
1233         gboolean authenticated = FALSE;
1234         
1235         CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
1236         
1237         if (store->preauthed) {
1238                 if (camel_verbose_debug)
1239                         fprintf(stderr, "Server %s has preauthenticated us.\n",
1240                                 service->url->host);
1241                 return TRUE;
1242         }
1243
1244         if (service->url->authmech) {
1245                 if (!g_hash_table_lookup (store->authtypes, service->url->authmech)) {
1246                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
1247                                               _("IMAP server %s does not support requested "
1248                                                 "authentication type %s"),
1249                                               service->url->host,
1250                                               service->url->authmech);
1251                         return FALSE;
1252                 }
1253                 
1254                 authtype = camel_sasl_authtype (service->url->authmech);
1255                 if (!authtype) {
1256                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
1257                                               _("No support for authentication type %s"),
1258                                               service->url->authmech);
1259                         return FALSE;
1260                 }
1261                 
1262                 if (!authtype->need_password) {
1263                         authenticated = try_auth (store, authtype->authproto, ex);
1264                         if (!authenticated)
1265                                 return FALSE;
1266                 }
1267         }
1268         
1269         while (!authenticated) {
1270                 if (errbuf) {
1271                         /* We need to un-cache the password before prompting again */
1272                         camel_session_forget_password (session, service, "password", ex);
1273                         g_free (service->url->passwd);
1274                         service->url->passwd = NULL;
1275                 }
1276                 
1277                 if (!service->url->passwd) {
1278                         char *prompt;
1279                         
1280                         prompt = g_strdup_printf (_("%sPlease enter the IMAP "
1281                                                     "password for %s@%s"),
1282                                                   errbuf ? errbuf : "",
1283                                                   service->url->user,
1284                                                   service->url->host);
1285                         service->url->passwd =
1286                                 camel_session_get_password (session, prompt, FALSE, TRUE,
1287                                                             service, "password", ex);
1288                         g_free (prompt);
1289                         g_free (errbuf);
1290                         errbuf = NULL;
1291                         
1292                         if (!service->url->passwd) {
1293                                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
1294                                                      _("You didn't enter a password."));
1295                                 return FALSE;
1296                         }
1297                 }
1298                 
1299                 if (!store->connected) {
1300                         /* Some servers (eg, courier) will disconnect on
1301                          * a bad password. So reconnect here.
1302                          */
1303                         if (!connect_to_server_wrapper (service, ex))
1304                                 return FALSE;
1305                 }
1306                 
1307                 if (authtype)
1308                         authenticated = try_auth (store, authtype->authproto, ex);
1309                 else {
1310                         response = camel_imap_command (store, NULL, ex,
1311                                                        "LOGIN %S %S",
1312                                                        service->url->user,
1313                                                        service->url->passwd);
1314                         if (response) {
1315                                 camel_imap_response_free (store, response);
1316                                 authenticated = TRUE;
1317                         }
1318                 }
1319                 if (!authenticated) {
1320                         if (camel_exception_get_id(ex) == CAMEL_EXCEPTION_USER_CANCEL)
1321                                 return FALSE;
1322                         
1323                         errbuf = g_strdup_printf (_("Unable to authenticate "
1324                                                     "to IMAP server.\n%s\n\n"),
1325                                                   camel_exception_get_description (ex));
1326                         camel_exception_clear (ex);
1327                 }
1328         }
1329         
1330         return TRUE;
1331 }
1332
1333 static gboolean
1334 can_work_offline (CamelDiscoStore *disco_store)
1335 {
1336         CamelImapStore *store = CAMEL_IMAP_STORE (disco_store);
1337         
1338         return camel_store_summary_count((CamelStoreSummary *)store->summary) != 0;
1339 }
1340
1341 static gboolean
1342 imap_connect_online (CamelService *service, CamelException *ex)
1343 {
1344         CamelImapStore *store = CAMEL_IMAP_STORE (service);
1345         CamelDiscoStore *disco_store = CAMEL_DISCO_STORE (service);
1346         CamelImapResponse *response;
1347         /*struct _namespaces *namespaces;*/
1348         char *result, *name, *path;
1349         int i;
1350         size_t len;
1351         CamelImapStoreNamespace *ns;
1352
1353         CAMEL_SERVICE_LOCK (store, connect_lock);
1354         if (!connect_to_server_wrapper (service, ex) ||
1355             !imap_auth_loop (service, ex)) {
1356                 CAMEL_SERVICE_UNLOCK (store, connect_lock);
1357                 camel_service_disconnect (service, TRUE, NULL);
1358                 return FALSE;
1359         }
1360         
1361         /* Get namespace and hierarchy separator */
1362         if ((store->capabilities & IMAP_CAPABILITY_NAMESPACE) &&
1363             !(store->parameters & IMAP_PARAM_OVERRIDE_NAMESPACE)) {
1364                 response = camel_imap_command (store, NULL, ex, "NAMESPACE");
1365                 if (!response)
1366                         goto done;
1367                 
1368                 result = camel_imap_response_extract (store, response, "NAMESPACE", ex);
1369                 if (!result)
1370                         goto done;
1371                 
1372 #if 0
1373                 /* new code... */
1374                 namespaces = imap_parse_namespace_response (result);
1375                 imap_namespaces_destroy (namespaces);
1376                 /* end new code */
1377 #endif
1378                 
1379                 name = camel_strstrcase (result, "NAMESPACE ((");
1380                 if (name) {
1381                         char *sep;
1382                         
1383                         name += 12;
1384                         store->namespace = imap_parse_string ((const char **) &name, &len);
1385                         if (name && *name++ == ' ') {
1386                                 sep = imap_parse_string ((const char **) &name, &len);
1387                                 if (sep) {
1388                                         store->dir_sep = *sep;
1389                                         ((CamelStore *)store)->dir_sep = store->dir_sep;
1390                                         g_free (sep);
1391                                 }
1392                         }
1393                 }
1394                 g_free (result);
1395         }
1396         
1397         if (!store->namespace)
1398                 store->namespace = g_strdup ("");
1399         
1400         if (!store->dir_sep) {
1401                 if (store->server_level >= IMAP_LEVEL_IMAP4REV1) {
1402                         /* This idiom means "tell me the hierarchy separator
1403                          * for the given path, even if that path doesn't exist.
1404                          */
1405                         response = camel_imap_command (store, NULL, ex,
1406                                                        "LIST %S \"\"",
1407                                                        store->namespace);
1408                 } else {
1409                         /* Plain IMAP4 doesn't have that idiom, so we fall back
1410                          * to "tell me about this folder", which will fail if
1411                          * the folder doesn't exist (eg, if namespace is "").
1412                          */
1413                         response = camel_imap_command (store, NULL, ex,
1414                                                        "LIST \"\" %S",
1415                                                        store->namespace);
1416                 }
1417                 if (!response)
1418                         goto done;
1419                 
1420                 result = camel_imap_response_extract (store, response, "LIST", NULL);
1421                 if (result) {
1422                         imap_parse_list_response (store, result, NULL, &store->dir_sep, NULL);
1423                         g_free (result);
1424                 }
1425                 if (!store->dir_sep) {
1426                         store->dir_sep = '/';   /* Guess */
1427                         ((CamelStore *)store)->dir_sep = store->dir_sep;
1428                 }
1429         }
1430         
1431         /* canonicalize the namespace to end with dir_sep */
1432         len = strlen (store->namespace);
1433         if (len && store->namespace[len - 1] != store->dir_sep) {
1434                 gchar *tmp;
1435                 
1436                 tmp = g_strdup_printf ("%s%c", store->namespace, store->dir_sep);
1437                 g_free (store->namespace);
1438                 store->namespace = tmp;
1439         }
1440         
1441         ns = camel_imap_store_summary_namespace_new(store->summary, store->namespace, store->dir_sep);
1442         camel_imap_store_summary_namespace_set(store->summary, ns);
1443         
1444         if (CAMEL_STORE (store)->flags & CAMEL_STORE_SUBSCRIPTIONS) {
1445                 gboolean haveinbox = FALSE;
1446                 GPtrArray *folders;
1447                 char *pattern;
1448                 
1449                 /* this pre-fills the summary, and checks that lsub is useful */
1450                 folders = g_ptr_array_new ();
1451                 pattern = g_strdup_printf ("%s*", store->namespace);
1452                 get_folders_online (store, pattern, folders, TRUE, ex);
1453                 g_free (pattern);
1454                 
1455                 for (i = 0; i < folders->len; i++) {
1456                         CamelFolderInfo *fi = folders->pdata[i];
1457                         
1458                         haveinbox = haveinbox || !strcasecmp (fi->full_name, "INBOX");
1459                         
1460                         if (fi->flags & (CAMEL_IMAP_FOLDER_MARKED | CAMEL_IMAP_FOLDER_UNMARKED))
1461                                 store->capabilities |= IMAP_CAPABILITY_useful_lsub;
1462                         camel_folder_info_free (fi);
1463                 }
1464                 
1465                 /* if the namespace is under INBOX, check INBOX explicitly */
1466                 if (!strncasecmp (store->namespace, "INBOX", 5) && !camel_exception_is_set (ex)) {
1467                         gboolean just_subscribed = FALSE;
1468                         gboolean need_subscribe = FALSE;
1469                         
1470                 recheck:
1471                         g_ptr_array_set_size (folders, 0);
1472                         get_folders_online (store, "INBOX", folders, TRUE, ex);
1473                         
1474                         for (i = 0; i < folders->len; i++) {
1475                                 CamelFolderInfo *fi = folders->pdata[i];
1476                                 
1477                                 /* this should always be TRUE if folders->len > 0 */
1478                                 if (!strcasecmp (fi->full_name, "INBOX")) {
1479                                         haveinbox = TRUE;
1480                                         
1481                                         /* if INBOX is marked as \NoSelect then it is probably
1482                                            because it has not been subscribed to */
1483                                         if (!need_subscribe)
1484                                                 need_subscribe = fi->flags & CAMEL_FOLDER_NOSELECT;
1485                                 }
1486                                 
1487                                 camel_folder_info_free (fi);
1488                         }
1489                         
1490                         need_subscribe = !haveinbox || need_subscribe;
1491                         if (need_subscribe && !just_subscribed && !camel_exception_is_set (ex)) {
1492                                 /* in order to avoid user complaints, force a subscription to INBOX */
1493                                 response = camel_imap_command (store, NULL, ex, "SUBSCRIBE INBOX");
1494                                 if (response != NULL) {
1495                                         /* force a re-check which will pre-fill the summary and
1496                                            also get any folder flags present on the INBOX */
1497                                         camel_imap_response_free (store, response);
1498                                         just_subscribed = TRUE;
1499                                         goto recheck;
1500                                 }
1501                         }
1502                 }
1503                 
1504                 g_ptr_array_free (folders, TRUE);
1505         }
1506         
1507         path = g_strdup_printf ("%s/journal", store->storage_path);
1508         disco_store->diary = camel_disco_diary_new (disco_store, path, ex);
1509         g_free (path);
1510         
1511  done:
1512         /* save any changes we had */
1513         camel_store_summary_save((CamelStoreSummary *)store->summary);
1514
1515         CAMEL_SERVICE_UNLOCK (store, connect_lock);
1516         
1517         if (camel_exception_is_set (ex))
1518                 camel_service_disconnect (service, TRUE, NULL);
1519         else if (camel_disco_diary_empty (disco_store->diary))
1520                 imap_store_refresh_folders (store, ex);
1521         
1522         return !camel_exception_is_set (ex);
1523 }
1524
1525 static gboolean
1526 imap_connect_offline (CamelService *service, CamelException *ex)
1527 {
1528         CamelImapStore *store = CAMEL_IMAP_STORE (service);
1529         CamelDiscoStore *disco_store = CAMEL_DISCO_STORE (service);
1530         char *path;
1531
1532         path = g_strdup_printf ("%s/journal", store->storage_path);
1533         disco_store->diary = camel_disco_diary_new (disco_store, path, ex);
1534         g_free (path);
1535         if (!disco_store->diary)
1536                 return FALSE;
1537         
1538         imap_store_refresh_folders (store, ex);
1539         
1540         store->connected = !camel_exception_is_set (ex);
1541         return store->connected;
1542 }
1543
1544 static gboolean
1545 imap_disconnect_offline (CamelService *service, gboolean clean, CamelException *ex)
1546 {
1547         CamelImapStore *store = CAMEL_IMAP_STORE (service);
1548         CamelDiscoStore *disco = CAMEL_DISCO_STORE (service);
1549         
1550         store->connected = FALSE;
1551         if (store->current_folder) {
1552                 camel_object_unref (CAMEL_OBJECT (store->current_folder));
1553                 store->current_folder = NULL;
1554         }
1555         
1556         if (store->authtypes) {
1557                 g_hash_table_foreach_remove (store->authtypes,
1558                                              free_key, NULL);
1559                 g_hash_table_destroy (store->authtypes);
1560                 store->authtypes = NULL;
1561         }
1562         
1563         if (store->namespace && !(store->parameters & IMAP_PARAM_OVERRIDE_NAMESPACE)) {
1564                 g_free (store->namespace);
1565                 store->namespace = NULL;
1566         }
1567         
1568         if (disco->diary) {
1569                 camel_object_unref (CAMEL_OBJECT (disco->diary));
1570                 disco->diary = NULL;
1571         }
1572         
1573         return TRUE;
1574 }
1575
1576 static gboolean
1577 imap_disconnect_online (CamelService *service, gboolean clean, CamelException *ex)
1578 {
1579         CamelImapStore *store = CAMEL_IMAP_STORE (service);
1580         CamelImapResponse *response;
1581         
1582         if (store->connected && clean) {
1583                 response = camel_imap_command (store, NULL, NULL, "LOGOUT");
1584                 camel_imap_response_free (store, response);
1585         }
1586         
1587         if (store->istream) {
1588                 camel_object_unref (CAMEL_OBJECT (store->istream));
1589                 store->istream = NULL;
1590         }
1591         
1592         if (store->ostream) {
1593                 camel_object_unref (CAMEL_OBJECT (store->ostream));
1594                 store->ostream = NULL;
1595         }
1596         
1597         imap_disconnect_offline (service, clean, ex);
1598         
1599         return TRUE;
1600 }
1601
1602
1603 static gboolean
1604 imap_summary_is_dirty (CamelFolderSummary *summary)
1605 {
1606         CamelMessageInfo *info;
1607         int max, i;
1608         
1609         max = camel_folder_summary_count (summary);
1610         for (i = 0; i < max; i++) {
1611                 info = camel_folder_summary_index (summary, i);
1612                 if (info && (info->flags & CAMEL_MESSAGE_FOLDER_FLAGGED))
1613                         return TRUE;
1614         }
1615         
1616         return FALSE;
1617 }
1618
1619 static void
1620 imap_noop (CamelStore *store, CamelException *ex)
1621 {
1622         CamelImapStore *imap_store = (CamelImapStore *) store;
1623         CamelDiscoStore *disco = (CamelDiscoStore *) store;
1624         CamelImapResponse *response;
1625         CamelFolder *current_folder;
1626         
1627         if (camel_disco_store_status (disco) != CAMEL_DISCO_STORE_ONLINE)
1628                 return;
1629         
1630         CAMEL_SERVICE_LOCK (imap_store, connect_lock);
1631         
1632         current_folder = imap_store->current_folder;
1633         if (current_folder && imap_summary_is_dirty (current_folder->summary)) {
1634                 /* let's sync the flags instead.  NB: must avoid folder lock */
1635                 ((CamelFolderClass *)((CamelObject *)current_folder)->klass)->sync(current_folder, FALSE, ex);
1636         } else {
1637                 response = camel_imap_command (imap_store, NULL, ex, "NOOP");
1638                 if (response)
1639                         camel_imap_response_free (imap_store, response);
1640         }
1641         
1642         CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1643 }
1644
1645 static guint
1646 hash_folder_name (gconstpointer key)
1647 {
1648         if (strcasecmp (key, "INBOX") == 0)
1649                 return g_str_hash ("INBOX");
1650         else
1651                 return g_str_hash (key);
1652 }
1653
1654 static gint
1655 compare_folder_name (gconstpointer a, gconstpointer b)
1656 {
1657         gconstpointer aname = a, bname = b;
1658
1659         if (strcasecmp (a, "INBOX") == 0)
1660                 aname = "INBOX";
1661         if (strcasecmp (b, "INBOX") == 0)
1662                 bname = "INBOX";
1663         return g_str_equal (aname, bname);
1664 }
1665
1666 static CamelFolder *
1667 no_such_folder (const char *name, CamelException *ex)
1668 {
1669         camel_exception_setv (ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
1670                               _("No such folder %s"), name);
1671         return NULL;
1672 }
1673
1674 static int
1675 get_folder_status (CamelImapStore *imap_store, const char *folder_name, const char *type)
1676 {
1677         CamelImapResponse *response;
1678         char *status, *p;
1679         int out;
1680
1681         /* FIXME: we assume the server is STATUS-capable */
1682
1683         response = camel_imap_command (imap_store, NULL, NULL,
1684                                        "STATUS %F (%s)",
1685                                        folder_name,
1686                                        type);
1687
1688         if (!response) {
1689                 CamelException ex;
1690
1691                 camel_exception_init (&ex);
1692                 if (imap_check_folder_still_extant (imap_store, folder_name, &ex) == FALSE) {
1693                         imap_folder_effectively_unsubscribed (imap_store, folder_name, &ex);
1694                         imap_forget_folder (imap_store, folder_name, &ex);
1695                 }
1696                 camel_exception_clear (&ex);
1697                 return -1;
1698         }
1699
1700         status = camel_imap_response_extract (imap_store, response,
1701                                               "STATUS", NULL);
1702         if (!status)
1703                 return -1;
1704
1705         p = camel_strstrcase (status, type);
1706         if (p)
1707                 out = strtoul (p + strlen (type), NULL, 10);
1708         else
1709                 out = -1;
1710
1711         g_free (status);
1712         return out;
1713 }
1714
1715 static CamelFolder *
1716 get_folder_online (CamelStore *store, const char *folder_name,
1717                    guint32 flags, CamelException *ex)
1718 {
1719         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1720         CamelImapResponse *response;
1721         CamelFolder *new_folder;
1722         char *folder_dir, *storage_path;
1723         
1724         if (!camel_imap_store_connected (imap_store, ex))
1725                 return NULL;
1726         
1727         if (!strcasecmp (folder_name, "INBOX"))
1728                 folder_name = "INBOX";
1729
1730         /* Lock around the whole lot to check/create atomically */
1731         CAMEL_SERVICE_LOCK (imap_store, connect_lock);
1732         if (imap_store->current_folder) {
1733                 camel_object_unref (CAMEL_OBJECT (imap_store->current_folder));
1734                 imap_store->current_folder = NULL;
1735         }
1736         response = camel_imap_command (imap_store, NULL, NULL, "SELECT %F", folder_name);
1737         if (!response) {
1738                 char *folder_real;
1739
1740                 if (!flags & CAMEL_STORE_FOLDER_CREATE) {
1741                         CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1742                         return no_such_folder (folder_name, ex);
1743                 }
1744
1745                 folder_real = camel_imap_store_summary_path_to_full(imap_store->summary, folder_name, store->dir_sep);
1746
1747                 response = camel_imap_command (imap_store, NULL, ex, "CREATE %S", folder_real);
1748
1749                 if (response) {
1750                         camel_imap_store_summary_add_from_full(imap_store->summary, folder_real, store->dir_sep);
1751
1752                         camel_imap_response_free (imap_store, response);
1753                         
1754                         response = camel_imap_command (imap_store, NULL, NULL, "SELECT %F", folder_name);
1755                 }
1756                 g_free(folder_real);
1757                 if (!response) {
1758                         CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1759                         return NULL;
1760                 }
1761         }
1762
1763         storage_path = g_strdup_printf("%s/folders", imap_store->storage_path);
1764         folder_dir = e_path_to_physical (storage_path, folder_name);
1765         g_free(storage_path);
1766         new_folder = camel_imap_folder_new (store, folder_name, folder_dir, ex);
1767         g_free (folder_dir);
1768         if (new_folder) {
1769                 CamelException local_ex;
1770
1771                 imap_store->current_folder = new_folder;
1772                 camel_object_ref (CAMEL_OBJECT (new_folder));
1773                 camel_exception_init (&local_ex);
1774                 camel_imap_folder_selected (new_folder, response, &local_ex);
1775
1776                 if (camel_exception_is_set (&local_ex)) {
1777                         camel_exception_xfer (ex, &local_ex);
1778                         camel_object_unref (CAMEL_OBJECT (imap_store->current_folder));
1779                         imap_store->current_folder = NULL;
1780                         camel_object_unref (CAMEL_OBJECT (new_folder));
1781                         new_folder = NULL;
1782                 }
1783         }
1784         camel_imap_response_free_without_processing (imap_store, response);
1785         
1786         CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1787         
1788         return new_folder;
1789 }
1790
1791 static CamelFolder *
1792 get_folder_offline (CamelStore *store, const char *folder_name,
1793                     guint32 flags, CamelException *ex)
1794 {
1795         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1796         CamelFolder *new_folder;
1797         char *folder_dir, *storage_path;
1798         
1799         if (!imap_store->connected &&
1800             !camel_service_connect (CAMEL_SERVICE (store), ex))
1801                 return NULL;
1802         
1803         if (!strcasecmp (folder_name, "INBOX"))
1804                 folder_name = "INBOX";
1805         
1806         storage_path = g_strdup_printf("%s/folders", imap_store->storage_path);
1807         folder_dir = e_path_to_physical (storage_path, folder_name);
1808         g_free(storage_path);
1809         if (!folder_dir || access (folder_dir, F_OK) != 0) {
1810                 g_free (folder_dir);
1811                 camel_exception_setv (ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
1812                                       _("No such folder %s"), folder_name);
1813                 return NULL;
1814         }
1815         
1816         new_folder = camel_imap_folder_new (store, folder_name, folder_dir, ex);
1817         g_free (folder_dir);
1818         
1819         return new_folder;
1820 }
1821
1822 static void
1823 delete_folder (CamelStore *store, const char *folder_name, CamelException *ex)
1824 {
1825         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1826         CamelImapResponse *response;
1827         
1828         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
1829                 return;
1830         
1831         /* make sure this folder isn't currently SELECTed */
1832         response = camel_imap_command (imap_store, NULL, ex, "SELECT INBOX");
1833         if (response) {
1834                 camel_imap_response_free_without_processing (imap_store, response);
1835                 
1836                 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
1837                 
1838                 if (imap_store->current_folder)
1839                         camel_object_unref (CAMEL_OBJECT (imap_store->current_folder));
1840                 /* no need to actually create a CamelFolder for INBOX */
1841                 imap_store->current_folder = NULL;
1842                 
1843                 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1844         } else
1845                 return;
1846         
1847         response = camel_imap_command (imap_store, NULL, ex, "DELETE %F",
1848                                        folder_name);
1849         
1850         if (response) {
1851                 camel_imap_response_free (imap_store, response);
1852                 imap_forget_folder (imap_store, folder_name, ex);
1853         }
1854 }
1855
1856 static void
1857 manage_subscriptions (CamelStore *store, const char *old_name, gboolean subscribe)
1858 {
1859         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1860         CamelStoreInfo *si;
1861         int olen = strlen(old_name);
1862         const char *path;
1863         int i, count;
1864
1865         count = camel_store_summary_count((CamelStoreSummary *)imap_store->summary);
1866         for (i=0;i<count;i++) {
1867                 si = camel_store_summary_index((CamelStoreSummary *)imap_store->summary, i);
1868                 if (si) {
1869                         path = camel_store_info_path(imap_store->summary, si);
1870                         if (strncmp(path, old_name, olen) == 0) {
1871                                 if (subscribe)
1872                                         subscribe_folder(store, path, NULL);
1873                                 else
1874                                         unsubscribe_folder(store, path, NULL);
1875                         }
1876                         camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
1877                 }
1878         }
1879 }
1880
1881 static void
1882 rename_folder_info (CamelImapStore *imap_store, const char *old_name, const char *new_name)
1883 {
1884         int i, count;
1885         CamelStoreInfo *si;
1886         int olen = strlen(old_name);
1887         const char *path;
1888         char *npath, *nfull;
1889
1890         count = camel_store_summary_count((CamelStoreSummary *)imap_store->summary);
1891         for (i=0;i<count;i++) {
1892                 si = camel_store_summary_index((CamelStoreSummary *)imap_store->summary, i);
1893                 if (si == NULL)
1894                         continue;
1895                 path = camel_store_info_path(imap_store->summary, si);
1896                 if (strncmp(path, old_name, olen) == 0) {
1897                         if (strlen(path) > olen)
1898                                 npath = g_strdup_printf("%s/%s", new_name, path+olen+1);
1899                         else
1900                                 npath = g_strdup(new_name);
1901                         nfull = camel_imap_store_summary_path_to_full(imap_store->summary, npath, imap_store->dir_sep);
1902                         
1903                         /* workaround for broken server (courier uses '.') that doesn't rename
1904                            subordinate folders as required by rfc 2060 */
1905                         if (imap_store->dir_sep == '.') {
1906                                 CamelImapResponse *response;
1907
1908                                 response = camel_imap_command (imap_store, NULL, NULL, "RENAME %F %S", path, nfull);
1909                                 if (response)
1910                                         camel_imap_response_free (imap_store, response);
1911                         }
1912
1913                         camel_store_info_set_string((CamelStoreSummary *)imap_store->summary, si, CAMEL_STORE_INFO_PATH, npath);
1914                         camel_store_info_set_string((CamelStoreSummary *)imap_store->summary, si, CAMEL_IMAP_STORE_INFO_FULL_NAME, nfull);
1915
1916                         camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
1917                         g_free(nfull);
1918                         g_free(npath);
1919                 }
1920                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
1921         }
1922 }
1923
1924 static void
1925 rename_folder (CamelStore *store, const char *old_name, const char *new_name_in, CamelException *ex)
1926 {
1927         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1928         CamelImapResponse *response;
1929         char *oldpath, *newpath, *storage_path, *new_name;
1930         
1931         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
1932                 return;
1933         
1934         /* make sure this folder isn't currently SELECTed - it's
1935            actually possible to rename INBOX but if you do another
1936            INBOX will immediately be created by the server */
1937         response = camel_imap_command (imap_store, NULL, ex, "SELECT INBOX");
1938         if (response) {
1939                 camel_imap_response_free_without_processing (imap_store, response);
1940                 
1941                 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
1942                 
1943                 if (imap_store->current_folder)
1944                         camel_object_unref (CAMEL_OBJECT (imap_store->current_folder));
1945                 /* no need to actually create a CamelFolder for INBOX */
1946                 imap_store->current_folder = NULL;
1947                 
1948                 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1949         } else
1950                 return;
1951         
1952         imap_store->renaming = TRUE;
1953         
1954         if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1955                 manage_subscriptions(store, old_name, FALSE);
1956
1957         new_name = camel_imap_store_summary_path_to_full(imap_store->summary, new_name_in, store->dir_sep);
1958         response = camel_imap_command (imap_store, NULL, ex, "RENAME %F %S", old_name, new_name);
1959         
1960         if (!response) {
1961                 if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1962                         manage_subscriptions(store, old_name, TRUE);
1963                 g_free(new_name);
1964                 imap_store->renaming = FALSE;
1965                 return;
1966         }
1967         
1968         camel_imap_response_free (imap_store, response);
1969
1970         /* rename summary, and handle broken server */
1971         rename_folder_info(imap_store, old_name, new_name_in);
1972
1973         if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1974                 manage_subscriptions(store, new_name_in, TRUE);
1975
1976         storage_path = g_strdup_printf("%s/folders", imap_store->storage_path);
1977         oldpath = e_path_to_physical (storage_path, old_name);
1978         newpath = e_path_to_physical (storage_path, new_name_in);
1979         g_free(storage_path);
1980
1981         /* So do we care if this didn't work?  Its just a cache? */
1982         if (rename (oldpath, newpath) == -1) {
1983                 g_warning ("Could not rename message cache '%s' to '%s': %s: cache reset",
1984                            oldpath, newpath, strerror (errno));
1985         }
1986         
1987         g_free (oldpath);
1988         g_free (newpath);
1989         g_free(new_name);
1990
1991         imap_store->renaming = FALSE;
1992 }
1993
1994 static CamelFolderInfo *
1995 create_folder (CamelStore *store, const char *parent_name,
1996                const char *folder_name, CamelException *ex)
1997 {
1998         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1999         char *full_name, *resp, *thisone, *parent_real, *real_name;
2000         CamelImapResponse *response;
2001         CamelException internal_ex;
2002         CamelFolderInfo *root = NULL;
2003         gboolean need_convert;
2004         int i = 0, flags;
2005         
2006         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
2007                 return NULL;
2008         if (!parent_name)
2009                 parent_name = "";
2010         
2011         if (strchr (folder_name, imap_store->dir_sep)) {
2012                 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_PATH,
2013                                       _("The folder name \"%s\" is invalid because "
2014                                         "it containes the character \"%c\""),
2015                                       folder_name, imap_store->dir_sep);
2016                 return NULL;
2017         }
2018         
2019         /* check if the parent allows inferiors */
2020
2021         /* FIXME: use storesummary directly */
2022         parent_real = camel_imap_store_summary_full_from_path(imap_store->summary, parent_name);
2023         if (parent_real == NULL) {
2024                 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_STATE,
2025                                      _("Unknown parent folder: %s"), parent_name);
2026                 return NULL;
2027         }
2028
2029         need_convert = FALSE;
2030         response = camel_imap_command (imap_store, NULL, ex, "LIST \"\" %S",
2031                                        parent_real);
2032         if (!response) /* whoa, this is bad */ {
2033                 g_free(parent_real);
2034                 return NULL;
2035         }
2036         
2037         /* FIXME: does not handle unexpected circumstances very well */
2038         for (i = 0; i < response->untagged->len; i++) {
2039                 resp = response->untagged->pdata[i];
2040                 
2041                 if (!imap_parse_list_response (imap_store, resp, &flags, NULL, &thisone))
2042                         continue;
2043                 
2044                 if (strcmp (thisone, parent_name) == 0) {
2045                         if (flags & CAMEL_FOLDER_NOINFERIORS)
2046                                 need_convert = TRUE;
2047                         break;
2048                 }
2049         }
2050         
2051         camel_imap_response_free (imap_store, response);
2052         
2053         camel_exception_init (&internal_ex);
2054         
2055         /* if not, check if we can delete it and recreate it */
2056         if (need_convert) {
2057                 char *name;
2058                 
2059                 if (get_folder_status (imap_store, parent_name, "MESSAGES")) {
2060                         camel_exception_set (ex, CAMEL_EXCEPTION_FOLDER_INVALID_STATE,
2061                                              _("The parent folder is not allowed to contain subfolders"));
2062                         g_free(parent_real);
2063                         return NULL;
2064                 }
2065                 
2066                 /* delete the old parent and recreate it */
2067                 delete_folder (store, parent_name, &internal_ex);
2068                 if (camel_exception_is_set (&internal_ex)) {
2069                         camel_exception_xfer (ex, &internal_ex);
2070                         return NULL;
2071                 }
2072                 
2073                 /* add the dirsep to the end of parent_name */
2074                 name = g_strdup_printf ("%s%c", parent_real, imap_store->dir_sep);
2075                 response = camel_imap_command (imap_store, NULL, ex, "CREATE %S",
2076                                                name);
2077                 g_free (name);
2078                 
2079                 if (!response) {
2080                         g_free(parent_real);
2081                         return NULL;
2082                 } else
2083                         camel_imap_response_free (imap_store, response);
2084
2085                 root = imap_build_folder_info(imap_store, parent_name);
2086         }
2087         
2088         /* ok now we can create the folder */
2089         real_name = camel_imap_store_summary_path_to_full(imap_store->summary, folder_name, store->dir_sep);
2090         full_name = imap_concat (imap_store, parent_real, real_name);
2091         g_free(real_name);
2092         response = camel_imap_command (imap_store, NULL, ex, "CREATE %S", full_name);
2093         
2094         if (response) {
2095                 CamelImapStoreInfo *si;
2096                 CamelFolderInfo *fi;
2097
2098                 camel_imap_response_free (imap_store, response);
2099
2100                 si = camel_imap_store_summary_add_from_full(imap_store->summary, full_name, store->dir_sep);
2101                 camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
2102                 fi = imap_build_folder_info(imap_store, camel_store_info_path(imap_store->summary, si));
2103                 if (root) {
2104                         root->child = fi;
2105                         fi->parent = root;
2106                 } else {
2107                         root = fi;
2108                 }
2109                 camel_object_trigger_event (CAMEL_OBJECT (store), "folder_created", root);
2110         } else if (root) {
2111                 /* need to re-recreate the folder we just deleted */
2112                 camel_object_trigger_event (CAMEL_OBJECT (store), "folder_created", root);
2113                 camel_folder_info_free(root);
2114                 root = NULL;
2115         }
2116
2117         g_free (full_name);
2118         g_free(parent_real);
2119         
2120         return root;
2121 }
2122
2123 static CamelFolderInfo *
2124 parse_list_response_as_folder_info (CamelImapStore *imap_store,
2125                                     const char *response)
2126 {
2127         CamelFolderInfo *fi;
2128         int flags;
2129         char sep, *dir;
2130         CamelURL *url;
2131         CamelImapStoreInfo *si;
2132         guint32 newflags;
2133
2134         if (!imap_parse_list_response (imap_store, response, &flags, &sep, &dir))
2135                 return NULL;
2136
2137         /* FIXME: should use imap_build_folder_info, note the differences with param setting tho */
2138
2139         si = camel_imap_store_summary_add_from_full(imap_store->summary, dir, sep?sep:'/');
2140         newflags = (si->info.flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) | (flags & ~CAMEL_STORE_INFO_FOLDER_SUBSCRIBED);
2141         if (si->info.flags != newflags) {
2142                 si->info.flags = newflags;
2143                 camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
2144         }
2145         
2146         fi = g_new0 (CamelFolderInfo, 1);
2147         fi->flags = flags;
2148         fi->name = g_strdup(camel_store_info_name(imap_store->summary, si));
2149         fi->path = g_strdup_printf("/%s", camel_store_info_path(imap_store->summary, si));
2150         fi->full_name = g_strdup(fi->path+1);
2151         
2152         url = camel_url_new (imap_store->base_url, NULL);
2153         camel_url_set_path(url, fi->path);
2154
2155         if (flags & CAMEL_FOLDER_NOSELECT || fi->name[0] == 0)
2156                 camel_url_set_param (url, "noselect", "yes");
2157         fi->url = camel_url_to_string (url, 0);
2158         camel_url_free (url);
2159
2160         /* FIXME: redundant */
2161         if (flags & CAMEL_IMAP_FOLDER_UNMARKED)
2162                 fi->unread_message_count = -1;
2163
2164         return fi;
2165 }
2166
2167 /* this is used when lsub doesn't provide very useful information */
2168 static GPtrArray *
2169 get_subscribed_folders (CamelImapStore *imap_store, const char *top, CamelException *ex)
2170 {
2171         GPtrArray *names, *folders;
2172         int i, toplen = strlen (top);
2173         CamelStoreInfo *si;
2174         CamelImapResponse *response;
2175         CamelFolderInfo *fi;
2176         char *result;
2177         int haveinbox = FALSE;
2178
2179         folders = g_ptr_array_new ();
2180         names = g_ptr_array_new ();
2181         for (i=0;(si = camel_store_summary_index((CamelStoreSummary *)imap_store->summary, i));i++) {
2182                 if (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) {
2183                         g_ptr_array_add(names, (char *)camel_imap_store_info_full_name(imap_store->summary, si));
2184                         haveinbox = haveinbox || strcasecmp(camel_imap_store_info_full_name(imap_store->summary, si), "INBOX") == 0;
2185                 }
2186                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2187         }
2188         
2189         if (!haveinbox)
2190                 g_ptr_array_add (names, "INBOX");
2191         
2192         for (i = 0; i < names->len; i++) {
2193                 response = camel_imap_command (imap_store, NULL, ex,
2194                                                "LIST \"\" %S",
2195                                                names->pdata[i]);
2196                 if (!response)
2197                         break;
2198                 
2199                 result = camel_imap_response_extract (imap_store, response, "LIST", NULL);
2200                 if (!result) {
2201                         camel_store_summary_remove_path((CamelStoreSummary *)imap_store->summary, names->pdata[i]);
2202                         g_ptr_array_remove_index_fast (names, i);
2203                         i--;
2204                         continue;
2205                 }
2206                 
2207                 fi = parse_list_response_as_folder_info (imap_store, result);
2208                 if (!fi)
2209                         continue;
2210                 
2211                 if (strncmp (top, fi->full_name, toplen) != 0) {
2212                         camel_folder_info_free (fi);
2213                         continue;
2214                 }
2215                 
2216                 g_ptr_array_add (folders, fi);
2217         }
2218         
2219         g_ptr_array_free (names, TRUE);
2220         
2221         return folders;
2222 }
2223
2224 static int imap_match_pattern(char dir_sep, const char *pattern, const char *name)
2225 {
2226         char p, n;
2227
2228         p = *pattern++;
2229         n = *name++;
2230         while (n && p) {
2231                 if (n == p) {
2232                         p = *pattern++;
2233                         n = *name++;
2234                 } else if (p == '%') {
2235                         if (n != dir_sep) {
2236                                 n = *name++;
2237                         } else {
2238                                 p = *pattern++;
2239                         }
2240                 } else if (p == '*') {
2241                         return TRUE;
2242                 } else
2243                         return FALSE;
2244         }
2245
2246         return n == 0 && (p == '%' || p == 0);
2247 }
2248
2249 static void
2250 get_folders_online (CamelImapStore *imap_store, const char *pattern,
2251                     GPtrArray *folders, gboolean lsub, CamelException *ex)
2252 {
2253         CamelImapResponse *response;
2254         CamelFolderInfo *fi;
2255         char *list;
2256         int i, count;
2257         GHashTable *present;
2258         CamelStoreInfo *si;
2259
2260         response = camel_imap_command (imap_store, NULL, ex,
2261                                        "%s \"\" %S", lsub ? "LSUB" : "LIST",
2262                                        pattern);
2263         if (!response)
2264                 return;
2265
2266         present = g_hash_table_new(g_str_hash, g_str_equal);
2267         for (i = 0; i < response->untagged->len; i++) {
2268                 list = response->untagged->pdata[i];
2269                 fi = parse_list_response_as_folder_info (imap_store, list);
2270                 if (fi) {
2271                         g_ptr_array_add(folders, fi);
2272                         g_hash_table_insert(present, fi->full_name, fi);
2273                 }
2274         }
2275         camel_imap_response_free (imap_store, response);
2276
2277         /* update our summary to match the server */
2278         count = camel_store_summary_count((CamelStoreSummary *)imap_store->summary);
2279         for (i=0;i<count;i++) {
2280                 si = camel_store_summary_index((CamelStoreSummary *)imap_store->summary, i);
2281                 if (si == NULL)
2282                         continue;
2283
2284                 if (imap_match_pattern(((CamelStore *)imap_store)->dir_sep, pattern, camel_imap_store_info_full_name(imap_store->summary, si))) {
2285                         if (g_hash_table_lookup(present, camel_store_info_path(imap_store->summary, si)) != NULL) {
2286                                 if (lsub && (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) == 0) {
2287                                         si->flags |= CAMEL_STORE_INFO_FOLDER_SUBSCRIBED;
2288                                         camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
2289                                 }
2290                         } else {
2291                                 if (lsub) {
2292                                         if (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) {
2293                                                 si->flags &= ~CAMEL_STORE_INFO_FOLDER_SUBSCRIBED;
2294                                                 camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
2295                                         }
2296                                 } else {
2297                                         camel_store_summary_remove((CamelStoreSummary *)imap_store->summary, si);
2298                                         count--;
2299                                         i--;
2300                                 }
2301                         }
2302                 }
2303                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2304         }
2305         g_hash_table_destroy(present);
2306 }
2307
2308 #if 0
2309 static void
2310 dumpfi(CamelFolderInfo *fi)
2311 {
2312         int depth;
2313         CamelFolderInfo *n = fi;
2314
2315         if (fi == NULL)
2316                 return;
2317
2318         depth = 0;
2319         while (n->parent) {
2320                 depth++;
2321                 n = n->parent;
2322         }
2323
2324         while (fi) {
2325                 printf("%-40s %-30s %*s\n", fi->path, fi->full_name, depth*2+strlen(fi->url), fi->url);
2326                 if (fi->child)
2327                         dumpfi(fi->child);
2328                 fi = fi->sibling;
2329         }
2330 }
2331 #endif
2332
2333 static void
2334 get_folder_counts(CamelImapStore *imap_store, CamelFolderInfo *fi, CamelException *ex)
2335 {
2336         GSList *q;
2337         CamelFolder *folder;
2338
2339         /* non-recursive breath first search */
2340
2341         q = g_slist_append(NULL, fi);
2342
2343         while (q) {
2344                 fi = q->data;
2345                 q = g_slist_remove_link(q, q);
2346
2347                 while (fi) {
2348                         /* ignore noselect folders, and check only inbox if we only check inbox */
2349                         if ((fi->flags & CAMEL_FOLDER_NOSELECT) == 0
2350                             && ( (imap_store->parameters & IMAP_PARAM_CHECK_ALL)
2351                                  || strcasecmp(fi->full_name, "inbox") == 0) ) {
2352
2353                                 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
2354                                 /* For the current folder, poke it to check for new     
2355                                  * messages and then report that number, rather than
2356                                  * doing a STATUS command.
2357                                  */
2358                                 if (imap_store->current_folder && strcmp(imap_store->current_folder->full_name, fi->full_name) == 0) {
2359                                         /* we bypass the folder locking otherwise we can deadlock.  we use the command lock for
2360                                            any operations anyway so this is 'safe'.  See comment above imap_store_refresh_folders() for info */
2361                                         CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(imap_store->current_folder))->refresh_info(imap_store->current_folder, ex);
2362                                         fi->unread_message_count = camel_folder_get_unread_message_count (imap_store->current_folder);
2363                                 } else {
2364                                         fi->unread_message_count = get_folder_status (imap_store, fi->full_name, "UNSEEN");
2365                                         /* if we have this folder open, and the unread count has changed, update */
2366                                         folder = camel_object_bag_get(CAMEL_STORE(imap_store)->folders, fi->full_name);
2367                                         if (folder && fi->unread_message_count != camel_folder_get_unread_message_count(folder)) {
2368                                                 CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(folder))->refresh_info(folder, ex);
2369                                                 fi->unread_message_count = camel_folder_get_unread_message_count(folder);
2370                                         }
2371                                         if (folder)
2372                                                 camel_object_unref(folder);
2373
2374                                 }
2375                 
2376                                 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
2377                         } else {
2378                                 /* since its cheap, get it if they're open */
2379                                 folder = camel_object_bag_get(CAMEL_STORE(imap_store)->folders, fi->full_name);
2380                                 if (folder) {
2381                                         fi->unread_message_count = camel_folder_get_unread_message_count(folder);
2382                                         camel_object_unref(folder);
2383                                 } else
2384                                         fi->unread_message_count = -1;
2385                         }
2386
2387                         if (fi->child)
2388                                 q = g_slist_append(q, fi->child);
2389                         fi = fi->sibling;
2390                 }
2391         }
2392 }
2393
2394 /* imap needs to treat inbox case insensitive */
2395 /* we'll assume the names are normalised already */
2396 static guint folder_hash(const void *ap)
2397 {
2398         const char *a = ap;
2399
2400         if (strcasecmp(a, "INBOX") == 0)
2401                 a = "INBOX";
2402
2403         return g_str_hash(a);
2404 }
2405
2406 static int folder_eq(const void *ap, const void *bp)
2407 {
2408         const char *a = ap;
2409         const char *b = bp;
2410
2411         if (strcasecmp(a, "INBOX") == 0)
2412                 a = "INBOX";
2413         if (strcasecmp(b, "INBOX") == 0)
2414                 b = "INBOX";
2415
2416         return g_str_equal(a, b);
2417 }
2418
2419 static GPtrArray *
2420 get_folders(CamelStore *store, const char *top, guint32 flags, CamelException *ex)
2421 {
2422         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2423         GSList *p = NULL;
2424         GHashTable *infos;
2425         int i;
2426         GPtrArray *folders, *folders_out;
2427         CamelFolderInfo *fi;
2428         char *name;
2429         int depth = 0;
2430         int haveinbox = 0;
2431         static int imap_max_depth = 0;
2432
2433         if (!camel_imap_store_connected (imap_store, ex))
2434                 return NULL;
2435
2436         /* allow megalomaniacs to override the max of 10 */
2437         if (imap_max_depth == 0) {
2438                 name = getenv("CAMEL_IMAP_MAX_DEPTH");
2439                 if (name) {
2440                         imap_max_depth = atoi (name);
2441                         imap_max_depth = MIN (MAX (imap_max_depth, 0), 2);
2442                 } else
2443                         imap_max_depth = 10;
2444         }
2445
2446         infos = g_hash_table_new(folder_hash, folder_eq);
2447
2448         /* get starting point & strip trailing '/' */
2449         if (top[0] == 0) {
2450                 if (imap_store->namespace) {
2451                         top = imap_store->namespace;
2452                         i = strlen(top)-1;
2453                         name = g_malloc(i+2);
2454                         strcpy(name, top);
2455                         while (i>0 && name[i] == store->dir_sep)
2456                                 name[i--] = 0;
2457                 } else
2458                         name = g_strdup("");
2459         } else {
2460                 name = camel_imap_store_summary_full_from_path(imap_store->summary, top);
2461                 if (name == NULL)
2462                         name = camel_imap_store_summary_path_to_full(imap_store->summary, top, store->dir_sep);
2463         }
2464
2465         d(printf("\n\nList '%s' %s\n", name, flags&CAMEL_STORE_FOLDER_INFO_RECURSIVE?"RECURSIVE":"NON-RECURSIVE"));
2466
2467         folders_out = g_ptr_array_new();
2468         folders = g_ptr_array_new();
2469         
2470         /* first get working list of names */
2471         get_folders_online (imap_store, name[0]?name:"%", folders, flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, ex);
2472         if (camel_exception_is_set(ex))
2473                 goto fail;
2474         for (i=0; i<folders->len && !haveinbox; i++) {
2475                 fi = folders->pdata[i];
2476                 haveinbox = (strcasecmp(fi->full_name, "INBOX")) == 0;
2477         }
2478
2479         if (!haveinbox && top == imap_store->namespace) {
2480                 get_folders_online (imap_store, "INBOX", folders,
2481                                     flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, ex);
2482                 
2483                 if (camel_exception_is_set (ex))
2484                         goto fail;
2485         }
2486
2487         for (i=0; i<folders->len; i++)
2488                 p = g_slist_prepend(p, folders->pdata[i]);
2489
2490         g_ptr_array_set_size(folders, 0);
2491
2492         /* p is a reversed list of pending folders for the next level, q is the list of folders for this */
2493         while (p) {
2494                 GSList *q = g_slist_reverse(p);
2495
2496                 p = NULL;
2497                 while (q) {
2498                         fi = q->data;
2499
2500                         q = g_slist_remove_link(q, q);
2501                         g_ptr_array_add(folders_out, fi);
2502
2503                         d(printf("Checking folder '%s'\n", fi->full_name));
2504
2505                         /* First if we're not recursive mode on the top level, and we know it has or doesn't
2506                             or can't have children, no need to go further - a bit ugly */
2507                         if ( top == imap_store->namespace
2508                              && (flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) == 0
2509                              && (fi->flags & (CAMEL_FOLDER_CHILDREN|CAMEL_IMAP_FOLDER_NOCHILDREN|CAMEL_FOLDER_NOINFERIORS)) != 0) {
2510                                 /* do nothing */
2511                         }
2512                                 /* Otherwise, if this has (or might have) children, scan it */
2513                         else if ( (fi->flags & (CAMEL_IMAP_FOLDER_NOCHILDREN|CAMEL_FOLDER_NOINFERIORS)) == 0
2514                                   || (fi->flags & CAMEL_FOLDER_CHILDREN) != 0) {
2515                                 char *n, *real;
2516
2517                                 real = camel_imap_store_summary_full_from_path(imap_store->summary, fi->full_name);
2518                                 n = imap_concat(imap_store, real?real:fi->full_name, "%");
2519                                 get_folders_online(imap_store, n, folders, flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, ex);
2520                                 g_free(n);
2521                                 g_free(real);
2522
2523                                 if (folders->len > 0)
2524                                         fi->flags |= CAMEL_FOLDER_CHILDREN;
2525
2526                                 for (i=0;i<folders->len;i++) {
2527                                         fi = folders->pdata[i];
2528                                         if (g_hash_table_lookup(infos, fi->full_name) == NULL) {
2529                                                 g_hash_table_insert(infos, fi->full_name, fi);
2530                                                 if ((flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) && depth<imap_max_depth)
2531                                                         p = g_slist_prepend(p, fi);
2532                                                 else
2533                                                         g_ptr_array_add(folders_out, fi);
2534                                         } else {
2535                                                 camel_folder_info_free(fi);
2536                                         }
2537                                 }
2538                                 g_ptr_array_set_size(folders, 0);
2539                         }
2540                 }
2541                 depth++;
2542         }
2543
2544         g_ptr_array_free(folders, TRUE);
2545         g_hash_table_destroy(infos);
2546         g_free(name);
2547
2548         return folders_out;
2549 fail:
2550         g_ptr_array_free(folders, TRUE);
2551         g_ptr_array_free(folders_out, TRUE);
2552         g_hash_table_destroy(infos);
2553         g_free(name);
2554
2555         return NULL;
2556 }
2557
2558 static CamelFolderInfo *
2559 get_folder_info_online (CamelStore *store, const char *top, guint32 flags, CamelException *ex)
2560 {
2561         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2562         CamelFolderInfo *tree;
2563         GPtrArray *folders;
2564         
2565         if (top == NULL)
2566                 top = "";
2567
2568         if ((flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED)
2569             && !(imap_store->capabilities & IMAP_CAPABILITY_useful_lsub)
2570             && (imap_store->parameters & IMAP_PARAM_CHECK_ALL))
2571                 folders = get_subscribed_folders(imap_store, top, ex);
2572         else
2573                 folders = get_folders(store, top, flags, ex);
2574
2575         if (folders == NULL)
2576                 return NULL;
2577         
2578         tree = camel_folder_info_build(folders, top, '/', TRUE);
2579         g_ptr_array_free(folders, TRUE);
2580         
2581         if (!(flags & CAMEL_STORE_FOLDER_INFO_FAST))
2582                 get_folder_counts(imap_store, tree, ex);
2583
2584         d(dumpfi(tree));
2585         camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
2586
2587         return tree;
2588 }
2589
2590 static gboolean
2591 get_one_folder_offline (const char *physical_path, const char *path, gpointer data)
2592 {
2593         GPtrArray *folders = data;
2594         CamelImapStore *imap_store = folders->pdata[0];
2595         CamelFolderInfo *fi;
2596         CamelStoreInfo *si;
2597
2598         if (*path != '/')
2599                 return TRUE;
2600
2601         /* folder_info_build will insert parent nodes as necessary and mark
2602          * them as noselect, which is information we actually don't have at
2603          * the moment. So let it do the right thing by bailing out if it's
2604          * not a folder we're explicitly interested in.
2605          */
2606
2607         si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, path+1);
2608         if (si) {
2609                 if ((((CamelStore *)imap_store)->flags & CAMEL_STORE_SUBSCRIPTIONS) == 0
2610                     || (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED)) {
2611                         fi = imap_build_folder_info(imap_store, path+1);
2612                         fi->flags = si->flags;
2613                         if (si->flags & CAMEL_FOLDER_NOSELECT) {
2614                                 CamelURL *url = camel_url_new(fi->url, NULL);
2615                                 
2616                                 camel_url_set_param (url, "noselect", "yes");
2617                                 g_free(fi->url);
2618                                 fi->url = camel_url_to_string (url, 0);
2619                                 camel_url_free (url);
2620                         }
2621                         g_ptr_array_add (folders, fi);
2622                 }
2623                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2624         }
2625
2626         return TRUE;
2627 }
2628
2629 static CamelFolderInfo *
2630 get_folder_info_offline (CamelStore *store, const char *top,
2631                          guint32 flags, CamelException *ex)
2632 {
2633         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2634         CamelFolderInfo *fi;
2635         GPtrArray *folders;
2636         char *storage_path;
2637
2638         if (!imap_store->connected &&
2639             !camel_service_connect (CAMEL_SERVICE (store), ex))
2640                 return NULL;
2641
2642         if ((store->flags & CAMEL_STORE_SUBSCRIPTIONS) &&
2643             !(flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED)) {
2644                 camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex);
2645                 return NULL;
2646         }
2647
2648         /* FIXME: obey other flags */
2649
2650         folders = g_ptr_array_new ();
2651
2652         /* A kludge to avoid having to pass a struct to the callback */
2653         g_ptr_array_add (folders, imap_store);
2654         storage_path = g_strdup_printf("%s/folders", imap_store->storage_path);
2655         if (!e_path_find_folders (storage_path, get_one_folder_offline, folders)) {
2656                 camel_disco_store_check_online (CAMEL_DISCO_STORE (imap_store), ex);
2657                 fi = NULL;
2658         } else {
2659                 g_ptr_array_remove_index_fast (folders, 0);
2660                 fi = camel_folder_info_build (folders, "", '/', TRUE);
2661         }
2662         g_free(storage_path);
2663
2664         g_ptr_array_free (folders, TRUE);
2665         return fi;
2666 }
2667
2668 static gboolean
2669 folder_subscribed (CamelStore *store, const char *folder_name)
2670 {
2671         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2672         CamelStoreInfo *si;
2673         int truth = FALSE;
2674
2675         si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
2676         if (si) {
2677                 truth = (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) != 0;
2678                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2679         }
2680
2681         return truth;
2682 }
2683
2684 /* Note: folder_name must match a folder as listed with get_folder_info() -> full_name */
2685 static void
2686 subscribe_folder (CamelStore *store, const char *folder_name,
2687                   CamelException *ex)
2688 {
2689         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2690         CamelImapResponse *response;
2691         CamelFolderInfo *fi;
2692         CamelStoreInfo *si;
2693
2694         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
2695                 return;
2696         if (!camel_imap_store_connected (imap_store, ex))
2697                 return;
2698         
2699         response = camel_imap_command (imap_store, NULL, ex,
2700                                        "SUBSCRIBE %F", folder_name);
2701         if (!response)
2702                 return;
2703         camel_imap_response_free (imap_store, response);
2704         
2705         si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
2706         if (si) {
2707                 if ((si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) == 0) {
2708                         si->flags |= CAMEL_STORE_INFO_FOLDER_SUBSCRIBED;
2709                         camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
2710                         camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
2711                 }
2712                 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2713         }
2714         
2715         if (imap_store->renaming) {
2716                 /* we don't need to emit a "folder_subscribed" signal
2717                    if we are in the process of renaming folders, so we
2718                    are done here... */
2719                 return;
2720         }
2721
2722         fi = imap_build_folder_info(imap_store, folder_name);
2723         camel_object_trigger_event (CAMEL_OBJECT (store), "folder_subscribed", fi);
2724         camel_folder_info_free (fi);
2725 }
2726
2727 static void
2728 unsubscribe_folder (CamelStore *store, const char *folder_name,
2729                     CamelException *ex)
2730 {
2731         CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2732         CamelImapResponse *response;
2733         
2734         if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
2735                 return;
2736         if (!camel_imap_store_connected (imap_store, ex))
2737                 return;
2738         
2739         response = camel_imap_command (imap_store, NULL, ex,
2740                                        "UNSUBSCRIBE %F", folder_name);
2741         if (!response)
2742                 return;
2743         camel_imap_response_free (imap_store, response);
2744
2745         imap_folder_effectively_unsubscribed (imap_store, folder_name, ex);
2746 }
2747
2748 #if 0
2749 static gboolean
2750 folder_flags_have_changed (CamelFolder *folder)
2751 {
2752         CamelMessageInfo *info;
2753         int i, max;
2754         
2755         max = camel_folder_summary_count (folder->summary);
2756         for (i = 0; i < max; i++) {
2757                 info = camel_folder_summary_index (folder->summary, i);
2758                 if (!info)
2759                         continue;
2760                 if (info->flags & CAMEL_MESSAGE_FOLDER_FLAGGED) {
2761                         return TRUE;
2762                 }
2763         }
2764         
2765         return FALSE;
2766 }
2767 #endif
2768
2769
2770 gboolean
2771 camel_imap_store_connected (CamelImapStore *store, CamelException *ex)
2772 {
2773         if (store->istream == NULL || !store->connected)
2774                 return camel_service_connect (CAMEL_SERVICE (store), ex);
2775         return TRUE;
2776 }
2777
2778
2779 /* FIXME: please god, when will the hurting stop? Thus function is so
2780    fucking broken it's not even funny. */
2781 ssize_t
2782 camel_imap_store_readline (CamelImapStore *store, char **dest, CamelException *ex)
2783 {
2784         CamelStreamBuffer *stream;
2785         char linebuf[1024];
2786         GByteArray *ba;
2787         ssize_t nread;
2788         
2789         g_return_val_if_fail (CAMEL_IS_IMAP_STORE (store), -1);
2790         g_return_val_if_fail (dest, -1);
2791         
2792         *dest = NULL;
2793         
2794         /* Check for connectedness. Failed (or cancelled) operations will
2795          * close the connection. We can't expect a read to have any
2796          * meaning if we reconnect, so always set an exception.
2797          */
2798         
2799         if (!camel_imap_store_connected (store, ex)) {
2800                 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_NOT_CONNECTED,
2801                                      g_strerror (errno));
2802                 return -1;
2803         }
2804         
2805         stream = CAMEL_STREAM_BUFFER (store->istream);
2806         
2807         ba = g_byte_array_new ();
2808         while ((nread = camel_stream_buffer_gets (stream, linebuf, sizeof (linebuf))) > 0) {
2809                 g_byte_array_append (ba, linebuf, nread);
2810                 if (linebuf[nread - 1] == '\n')
2811                         break;
2812         }
2813         
2814         if (nread <= 0) {
2815                 if (errno == EINTR)
2816                         camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, _("Operation cancelled"));
2817                 else
2818                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
2819                                               _("Server unexpectedly disconnected: %s"),
2820                                               g_strerror (errno));
2821                 
2822                 camel_service_disconnect (CAMEL_SERVICE (store), FALSE, NULL);
2823                 g_byte_array_free (ba, TRUE);
2824                 return -1;
2825         }
2826         
2827         if (camel_verbose_debug) {
2828                 fprintf (stderr, "received: ");
2829                 fwrite (ba->data, 1, ba->len, stderr);
2830         }
2831         
2832         /* camel-imap-command.c:imap_read_untagged expects the CRLFs
2833            to be stripped off and be nul-terminated *sigh* */
2834         nread = ba->len - 1;
2835         ba->data[nread] = '\0';
2836         if (ba->data[nread - 1] == '\r') {
2837                 ba->data[nread - 1] = '\0';
2838                 nread--;
2839         }
2840         
2841         *dest = ba->data;
2842         g_byte_array_free (ba, FALSE);
2843         
2844         return nread;
2845 }