1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-imap-store.c : class for an imap store */
6 * Dan Winship <danw@ximian.com>
7 * Jeffrey Stedfast <fejj@ximian.com>
9 * Copyright 2000, 2003 Ximian, Inc.
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.
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.
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.
38 #include "e-util/e-path.h"
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"
63 #include "camel-imap-private.h"
64 #include "camel-private.h"
68 /* Specified in RFC 2060 */
70 #define SIMAP_PORT 993
73 extern int camel_verbose_debug;
75 static CamelDiscoStoreClass *parent_class = NULL;
77 static char imap_tag_prefix = 'A';
79 static void construct (CamelService *service, CamelSession *session,
80 CamelProvider *provider, CamelURL *url,
83 static int imap_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
84 static int imap_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
86 static char *imap_get_name (CamelService *service, gboolean brief);
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,
106 static CamelFolderInfo *get_folder_info_offline (CamelStore *store,
110 static gboolean folder_subscribed (CamelStore *store, const char *folder_name);
111 static void subscribe_folder (CamelStore *store, const char *folder_name,
113 static void unsubscribe_folder (CamelStore *store, const char *folder_name,
116 static void get_folders_online (CamelImapStore *imap_store, const char *pattern,
117 GPtrArray *folders, gboolean lsub, CamelException *ex);
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);
126 camel_imap_store_class_init (CamelImapStoreClass *camel_imap_store_class)
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);
137 parent_class = CAMEL_DISCO_STORE_CLASS (camel_type_get_global_classfuncs (camel_disco_store_get_type ()));
139 /* virtual method overload */
140 camel_object_class->setv = imap_setv;
141 camel_object_class->getv = imap_getv;
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;
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;
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;
172 free_key (gpointer key, gpointer value, gpointer user_data)
179 camel_imap_store_finalize (CamelObject *object)
181 CamelImapStore *imap_store = CAMEL_IMAP_STORE (object);
183 /* This frees current_folder, folders, authtypes, streams, and namespace. */
184 camel_service_disconnect((CamelService *)imap_store, TRUE, NULL);
186 if (imap_store->summary) {
187 camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
188 camel_object_unref(imap_store->summary);
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);
196 #ifdef ENABLE_THREADS
197 e_thread_destroy (imap_store->async_thread);
201 #ifdef ENABLE_THREADS
202 static void async_destroy(EThread *et, EMsg *em, void *data)
204 CamelImapStore *imap_store = data;
205 CamelImapMsg *msg = (CamelImapMsg *)em;
208 msg->free (imap_store, msg);
213 static void async_received(EThread *et, EMsg *em, void *data)
215 CamelImapStore *imap_store = data;
216 CamelImapMsg *msg = (CamelImapMsg *)em;
219 msg->receive(imap_store, msg);
222 CamelImapMsg *camel_imap_msg_new(void (*receive)(CamelImapStore *store, struct _CamelImapMsg *m),
223 void (*free)(CamelImapStore *store, struct _CamelImapMsg *m),
228 g_assert(size >= sizeof(*msg));
230 msg = g_malloc0(size);
231 msg->receive = receive;
237 void camel_imap_msg_queue(CamelImapStore *store, CamelImapMsg *msg)
239 e_thread_put(store->async_thread, (EMsg *)msg);
245 camel_imap_store_init (gpointer object, gpointer klass)
247 CamelImapStore *imap_store = CAMEL_IMAP_STORE (object);
249 imap_store->istream = NULL;
250 imap_store->ostream = NULL;
252 imap_store->dir_sep = '\0';
253 imap_store->current_folder = NULL;
254 imap_store->connected = FALSE;
255 imap_store->preauthed = FALSE;
257 imap_store->tag_prefix = imap_tag_prefix++;
258 if (imap_tag_prefix > 'Z')
259 imap_tag_prefix = 'A';
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 */
269 camel_imap_store_get_type (void)
271 static CamelType camel_imap_store_type = CAMEL_INVALID_TYPE;
273 if (camel_imap_store_type == CAMEL_INVALID_TYPE) {
274 camel_imap_store_type =
275 camel_type_register (CAMEL_DISCO_STORE_TYPE,
277 sizeof (CamelImapStore),
278 sizeof (CamelImapStoreClass),
279 (CamelObjectClassInitFunc) camel_imap_store_class_init,
281 (CamelObjectInitFunc) camel_imap_store_init,
282 (CamelObjectFinalizeFunc) camel_imap_store_finalize);
285 return camel_imap_store_type;
289 construct (CamelService *service, CamelSession *session,
290 CamelProvider *provider, CamelURL *url,
293 CamelImapStore *imap_store = CAMEL_IMAP_STORE (service);
294 CamelStore *store = CAMEL_STORE (service);
296 CamelURL *summary_url;
298 CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
299 if (camel_exception_is_set (ex))
302 imap_store->storage_path = camel_session_get_storage_path (session, service, ex);
303 if (!imap_store->storage_path)
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));
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"));
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;
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;
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);
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;
348 imap_store->capabilities = is->capabilities;
349 imap_set_server_level(imap_store);
354 imap_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
356 CamelImapStore *store = (CamelImapStore *) object;
360 for (i = 0; i < args->argc; i++) {
361 tag = args->argv[i].tag;
363 /* make sure this arg wasn't already handled */
364 if (tag & CAMEL_ARG_IGNORE)
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)
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;*/
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);
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;*/
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... */
403 /* let our parent know that we've handled this arg */
404 camel_argv_ignore (args, i);
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. */
411 return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
415 imap_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
417 CamelImapStore *store = (CamelImapStore *) object;
421 for (i = 0; i < args->argc; i++) {
422 tag = args->argv[i].tag;
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)
430 case CAMEL_IMAP_STORE_NAMESPACE:
431 /* get the username */
432 *args->argv[i].ca_str = store->namespace;
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;
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;
442 case CAMEL_IMAP_STORE_FILTER_INBOX:
444 *args->argv[i].ca_int = store->parameters & IMAP_PARAM_FILTER_INBOX ? TRUE : FALSE;
452 return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
456 imap_get_name (CamelService *service, gboolean brief)
459 return g_strdup_printf (_("IMAP server %s"), service->url->host);
461 return g_strdup_printf (_("IMAP service for %s on %s"),
462 service->url->user, service->url->host);
466 imap_set_server_level (CamelImapStore *store)
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;
474 store->server_level = IMAP_LEVEL_UNKNOWN;
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 },
493 imap_get_capability (CamelService *service, CamelException *ex)
495 CamelImapStore *store = CAMEL_IMAP_STORE (service);
496 CamelImapResponse *response;
497 char *result, *capa, *lasts;
500 CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
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");
509 result = camel_imap_response_extract (store, response, "CAPABILITY ", ex);
513 /* Skip over "* CAPABILITY ". */
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,
520 GINT_TO_POINTER (1));
523 for (i = 0; capabilities[i].name; i++) {
524 if (strcasecmp (capa, capabilities[i].name) == 0) {
525 store->capabilities |= capabilities[i].flag;
532 imap_set_server_level (store);
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);
546 USE_SSL_WHEN_POSSIBLE
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)
553 connect_to_server (CamelService *service, int ssl_mode, int try_starttls, CamelException *ex)
555 CamelImapStore *store = (CamelImapStore *) service;
556 CamelImapResponse *response;
557 CamelStream *tcp_stream;
563 h = camel_service_gethost (service, ex);
567 port = service->url->port ? service->url->port : 143;
569 if (ssl_mode != USE_SSL_NEVER) {
572 tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS);
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);
579 port = service->url->port ? service->url->port : 993;
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"));
589 #endif /* HAVE_SSL */
591 tcp_stream = camel_tcp_stream_raw_new ();
594 ret = camel_tcp_stream_connect (CAMEL_TCP_STREAM (tcp_stream), h, port);
598 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
599 _("Connection cancelled"));
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));
605 camel_object_unref (CAMEL_OBJECT (tcp_stream));
610 store->ostream = tcp_stream;
611 store->istream = camel_stream_buffer_new (tcp_stream, CAMEL_STREAM_BUFFER_READ);
613 store->connected = TRUE;
614 store->preauthed = FALSE;
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;
624 if (store->ostream) {
625 camel_object_unref (CAMEL_OBJECT (store->ostream));
626 store->ostream = NULL;
629 store->connected = FALSE;
633 if (!strncmp(buf, "* PREAUTH", 9))
634 store->preauthed = TRUE;
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;
644 if (store->ostream) {
645 camel_object_unref (CAMEL_OBJECT (store->ostream));
646 store->ostream = NULL;
649 store->connected = FALSE;
654 if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
655 if (store->capabilities & IMAP_CAPABILITY_STARTTLS)
657 } else if (ssl_mode == USE_SSL_ALWAYS) {
659 if (store->capabilities & IMAP_CAPABILITY_STARTTLS) {
660 /* attempt to toggle STARTTLS mode */
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 */
673 #endif /* HAVE_SSL */
680 /* as soon as we send a STARTTLS command, all hope is lost of a clean QUIT if problems arise */
683 response = camel_imap_command (store, NULL, ex, "STARTTLS");
685 camel_object_unref (CAMEL_OBJECT (store->istream));
686 camel_object_unref (CAMEL_OBJECT (store->ostream));
687 store->istream = store->ostream = NULL;
691 camel_imap_response_free_without_processing (store, response);
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"));
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;
709 if (store->ostream) {
710 camel_object_unref (CAMEL_OBJECT (store->ostream));
711 store->ostream = NULL;
714 store->connected = FALSE;
723 if (clean_quit && store->connected) {
724 /* try to disconnect cleanly */
725 response = camel_imap_command (store, NULL, ex, "LOGOUT");
727 camel_imap_response_free_without_processing (store, response);
730 if (store->istream) {
731 camel_object_unref (CAMEL_OBJECT (store->istream));
732 store->istream = NULL;
735 if (store->ostream) {
736 camel_object_unref (CAMEL_OBJECT (store->ostream));
737 store->ostream = NULL;
740 store->connected = FALSE;
743 #endif /* HAVE_SSL */
747 connect_to_server_process (CamelService *service, const char *cmd, CamelException *ex)
749 CamelImapStore *store = (CamelImapStore *) service;
750 CamelStream *cmd_stream;
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);
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);
774 /* Now do %h, %u, etc. substitution in cmd */
775 buf = cmd_copy = g_strdup(cmd);
777 full_cmd = g_strdup("");
785 pc = strchr(buf, '%');
788 tmp = g_strdup_printf("%s%s", full_cmd, buf);
800 var = service->url->host;
803 var = service->url->user;
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, '%');
812 tmp = g_strdup_printf("%s%.*s%s", full_cmd, len, buf, var);
820 cmd_stream = camel_stream_process_new ();
822 ret = camel_stream_process_connect (CAMEL_STREAM_PROCESS(cmd_stream),
823 full_cmd, (const char **)child_env);
826 g_free(child_env[--i]);
830 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
831 _("Connection cancelled"));
833 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
834 _("Could not connect with command \"%s\": %s"),
835 full_cmd, g_strerror (errno));
837 camel_object_unref (CAMEL_OBJECT (cmd_stream));
843 store->ostream = cmd_stream;
844 store->istream = camel_stream_buffer_new (cmd_stream, CAMEL_STREAM_BUFFER_READ);
846 store->connected = TRUE;
847 store->preauthed = FALSE;
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;
857 if (store->ostream) {
858 camel_object_unref (CAMEL_OBJECT (store->ostream));
859 store->ostream = NULL;
862 store->connected = FALSE;
865 if (!strncmp(buf, "* PREAUTH", 9))
866 store->preauthed = TRUE;
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;
876 if (store->ostream) {
877 camel_object_unref (CAMEL_OBJECT (store->ostream));
878 store->ostream = NULL;
881 store->connected = FALSE;
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 },
901 connect_to_server_wrapper (CamelService *service, CamelException *ex)
908 command = camel_url_get_param (service->url, "command");
910 return connect_to_server_process (service, command, ex);
913 use_ssl = camel_url_get_param (service->url, "use_ssl");
915 for (i = 0; ssl_options[i].value; i++)
916 if (!strcmp (ssl_options[i].value, use_ssl))
918 ssl_mode = ssl_options[i].mode;
920 ssl_mode = USE_SSL_NEVER;
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);
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);
939 /* User doesn't care about SSL */
940 return connect_to_server (service, ssl_mode, FALSE, ex);
943 return connect_to_server (service, USE_SSL_NEVER, FALSE, ex);
947 extern CamelServiceAuthType camel_imap_password_authtype;
950 query_auth_types (CamelService *service, CamelException *ex)
952 CamelImapStore *store = CAMEL_IMAP_STORE (service);
953 CamelServiceAuthType *authtype;
954 GList *sasl_types, *t, *next;
957 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
960 CAMEL_SERVICE_LOCK (store, connect_lock);
961 connected = connect_to_server_wrapper (service, ex);
962 CAMEL_SERVICE_UNLOCK (store, connect_lock);
966 sasl_types = camel_sasl_authtype_list (FALSE);
967 for (t = sasl_types; t; t = next) {
971 if (!g_hash_table_lookup (store->authtypes, authtype->authproto)) {
972 sasl_types = g_list_remove_link (sasl_types, t);
977 return g_list_prepend (sasl_types, &camel_imap_password_authtype);
980 /* folder_name is path name */
981 static CamelFolderInfo *
982 imap_build_folder_info(CamelImapStore *imap_store, const char *folder_name)
988 fi = g_malloc0(sizeof(*fi));
990 fi->full_name = g_strdup(folder_name);
991 fi->unread_message_count = 0;
993 url = camel_url_new (imap_store->base_url, NULL);
995 url->path = g_strdup_printf ("/%s", folder_name);
996 fi->url = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
998 fi->path = g_strdup_printf("/%s", folder_name);
999 name = strrchr (fi->path, '/');
1005 fi->name = g_strdup (name);
1011 imap_folder_effectively_unsubscribed(CamelImapStore *imap_store,
1012 const char *folder_name, CamelException *ex)
1014 CamelFolderInfo *fi;
1017 si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
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);
1024 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
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
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);
1041 imap_forget_folder (CamelImapStore *imap_store, const char *folder_name, CamelException *ex)
1043 CamelFolderSummary *summary;
1044 CamelImapMessageCache *cache;
1047 char *folder_dir, *storage_path;
1048 CamelFolderInfo *fi;
1051 name = strrchr (folder_name, imap_store->dir_sep);
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);
1065 summary_file = g_strdup_printf ("%s/summary", folder_dir);
1066 summary = camel_imap_summary_new (summary_file);
1068 g_free (summary_file);
1069 g_free (folder_dir);
1073 cache = camel_imap_message_cache_new (folder_dir, summary, ex);
1075 camel_imap_message_cache_clear (cache);
1077 camel_object_unref (cache);
1078 camel_object_unref (summary);
1080 unlink (summary_file);
1081 g_free (summary_file);
1083 journal_file = g_strdup_printf ("%s/summary", folder_dir);
1084 unlink (journal_file);
1085 g_free (journal_file);
1088 g_free (folder_dir);
1092 camel_store_summary_remove_path((CamelStoreSummary *)imap_store->summary, folder_name);
1093 camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
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);
1101 imap_check_folder_still_extant (CamelImapStore *imap_store, const char *full_name,
1104 CamelImapResponse *response;
1106 response = camel_imap_command (imap_store, NULL, ex, "LIST \"\" %S",
1110 gboolean stillthere = response->untagged->len != 0;
1112 camel_imap_response_free_without_processing (imap_store, response);
1117 /* if the command was rejected, there must be some other error,
1118 assume it worked so we dont blow away the folder unecessarily */
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 */
1131 imap_store_refresh_folders (CamelImapStore *store, CamelException *ex)
1136 folders = camel_object_bag_list(CAMEL_STORE (store)->folders);
1138 for (i = 0; i <folders->len; i++) {
1139 CamelFolder *folder = folders->pdata[i];
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);
1145 if (camel_exception_is_set (ex) &&
1146 imap_check_folder_still_extant (store, folder->full_name, ex) == FALSE) {
1149 /* the folder was deleted (may happen when we come back online
1150 * after being offline */
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);
1158 camel_object_unref(folder);
1161 g_ptr_array_free (folders, TRUE);
1165 try_auth (CamelImapStore *store, const char *mech, CamelException *ex)
1168 CamelImapResponse *response;
1172 CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
1174 response = camel_imap_command (store, NULL, ex, "AUTHENTICATE %s", mech);
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);
1184 sasl_resp = camel_sasl_challenge_base64 (sasl, imap_next_word (resp), ex);
1186 if (camel_exception_is_set (ex))
1187 goto break_and_lose;
1189 response = camel_imap_command_continuation (store, sasl_resp, strlen (sasl_resp), ex);
1195 resp = camel_imap_response_extract_continuation (store, response, NULL);
1197 /* Oops. SASL claims we're done, but the IMAP server
1198 * doesn't think so...
1204 camel_object_unref (CAMEL_OBJECT (sasl));
1209 /* Get the server out of "waiting for continuation data" mode. */
1210 response = camel_imap_command_continuation (store, "*", 1, NULL);
1212 camel_imap_response_free (store, response);
1215 if (!camel_exception_is_set (ex)) {
1216 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
1217 _("Bad authentication response from server."));
1220 camel_object_unref (CAMEL_OBJECT (sasl));
1226 imap_auth_loop (CamelService *service, CamelException *ex)
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;
1235 CAMEL_SERVICE_ASSERT_LOCKED (store, connect_lock);
1237 if (store->preauthed) {
1238 if (camel_verbose_debug)
1239 fprintf(stderr, "Server %s has preauthenticated us.\n",
1240 service->url->host);
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"),
1250 service->url->authmech);
1254 authtype = camel_sasl_authtype (service->url->authmech);
1256 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
1257 _("No support for authentication type %s"),
1258 service->url->authmech);
1262 if (!authtype->need_password) {
1263 authenticated = try_auth (store, authtype->authproto, ex);
1269 while (!authenticated) {
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;
1277 if (!service->url->passwd) {
1280 prompt = g_strdup_printf (_("%sPlease enter the IMAP "
1281 "password for %s@%s"),
1282 errbuf ? errbuf : "",
1284 service->url->host);
1285 service->url->passwd =
1286 camel_session_get_password (session, prompt, FALSE, TRUE,
1287 service, "password", ex);
1292 if (!service->url->passwd) {
1293 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
1294 _("You didn't enter a password."));
1299 if (!store->connected) {
1300 /* Some servers (eg, courier) will disconnect on
1301 * a bad password. So reconnect here.
1303 if (!connect_to_server_wrapper (service, ex))
1308 authenticated = try_auth (store, authtype->authproto, ex);
1310 response = camel_imap_command (store, NULL, ex,
1313 service->url->passwd);
1315 camel_imap_response_free (store, response);
1316 authenticated = TRUE;
1319 if (!authenticated) {
1320 if (camel_exception_get_id(ex) == CAMEL_EXCEPTION_USER_CANCEL)
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);
1334 can_work_offline (CamelDiscoStore *disco_store)
1336 CamelImapStore *store = CAMEL_IMAP_STORE (disco_store);
1338 return camel_store_summary_count((CamelStoreSummary *)store->summary) != 0;
1342 imap_connect_online (CamelService *service, CamelException *ex)
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;
1351 CamelImapStoreNamespace *ns;
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);
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");
1368 result = camel_imap_response_extract (store, response, "NAMESPACE", ex);
1374 namespaces = imap_parse_namespace_response (result);
1375 imap_namespaces_destroy (namespaces);
1379 name = camel_strstrcase (result, "NAMESPACE ((");
1384 store->namespace = imap_parse_string ((const char **) &name, &len);
1385 if (name && *name++ == ' ') {
1386 sep = imap_parse_string ((const char **) &name, &len);
1388 store->dir_sep = *sep;
1389 ((CamelStore *)store)->dir_sep = store->dir_sep;
1397 if (!store->namespace)
1398 store->namespace = g_strdup ("");
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.
1405 response = camel_imap_command (store, NULL, ex,
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 "").
1413 response = camel_imap_command (store, NULL, ex,
1420 result = camel_imap_response_extract (store, response, "LIST", NULL);
1422 imap_parse_list_response (store, result, NULL, &store->dir_sep, NULL);
1425 if (!store->dir_sep) {
1426 store->dir_sep = '/'; /* Guess */
1427 ((CamelStore *)store)->dir_sep = store->dir_sep;
1431 /* canonicalize the namespace to end with dir_sep */
1432 len = strlen (store->namespace);
1433 if (len && store->namespace[len - 1] != store->dir_sep) {
1436 tmp = g_strdup_printf ("%s%c", store->namespace, store->dir_sep);
1437 g_free (store->namespace);
1438 store->namespace = tmp;
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);
1444 if (CAMEL_STORE (store)->flags & CAMEL_STORE_SUBSCRIPTIONS) {
1445 gboolean haveinbox = FALSE;
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);
1455 for (i = 0; i < folders->len; i++) {
1456 CamelFolderInfo *fi = folders->pdata[i];
1458 haveinbox = haveinbox || !strcasecmp (fi->full_name, "INBOX");
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);
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;
1471 g_ptr_array_set_size (folders, 0);
1472 get_folders_online (store, "INBOX", folders, TRUE, ex);
1474 for (i = 0; i < folders->len; i++) {
1475 CamelFolderInfo *fi = folders->pdata[i];
1477 /* this should always be TRUE if folders->len > 0 */
1478 if (!strcasecmp (fi->full_name, "INBOX")) {
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;
1487 camel_folder_info_free (fi);
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;
1504 g_ptr_array_free (folders, TRUE);
1507 path = g_strdup_printf ("%s/journal", store->storage_path);
1508 disco_store->diary = camel_disco_diary_new (disco_store, path, ex);
1512 /* save any changes we had */
1513 camel_store_summary_save((CamelStoreSummary *)store->summary);
1515 CAMEL_SERVICE_UNLOCK (store, connect_lock);
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);
1522 return !camel_exception_is_set (ex);
1526 imap_connect_offline (CamelService *service, CamelException *ex)
1528 CamelImapStore *store = CAMEL_IMAP_STORE (service);
1529 CamelDiscoStore *disco_store = CAMEL_DISCO_STORE (service);
1532 path = g_strdup_printf ("%s/journal", store->storage_path);
1533 disco_store->diary = camel_disco_diary_new (disco_store, path, ex);
1535 if (!disco_store->diary)
1538 imap_store_refresh_folders (store, ex);
1540 store->connected = !camel_exception_is_set (ex);
1541 return store->connected;
1545 imap_disconnect_offline (CamelService *service, gboolean clean, CamelException *ex)
1547 CamelImapStore *store = CAMEL_IMAP_STORE (service);
1548 CamelDiscoStore *disco = CAMEL_DISCO_STORE (service);
1550 store->connected = FALSE;
1551 if (store->current_folder) {
1552 camel_object_unref (CAMEL_OBJECT (store->current_folder));
1553 store->current_folder = NULL;
1556 if (store->authtypes) {
1557 g_hash_table_foreach_remove (store->authtypes,
1559 g_hash_table_destroy (store->authtypes);
1560 store->authtypes = NULL;
1563 if (store->namespace && !(store->parameters & IMAP_PARAM_OVERRIDE_NAMESPACE)) {
1564 g_free (store->namespace);
1565 store->namespace = NULL;
1569 camel_object_unref (CAMEL_OBJECT (disco->diary));
1570 disco->diary = NULL;
1577 imap_disconnect_online (CamelService *service, gboolean clean, CamelException *ex)
1579 CamelImapStore *store = CAMEL_IMAP_STORE (service);
1580 CamelImapResponse *response;
1582 if (store->connected && clean) {
1583 response = camel_imap_command (store, NULL, NULL, "LOGOUT");
1584 camel_imap_response_free (store, response);
1587 if (store->istream) {
1588 camel_object_unref (CAMEL_OBJECT (store->istream));
1589 store->istream = NULL;
1592 if (store->ostream) {
1593 camel_object_unref (CAMEL_OBJECT (store->ostream));
1594 store->ostream = NULL;
1597 imap_disconnect_offline (service, clean, ex);
1604 imap_summary_is_dirty (CamelFolderSummary *summary)
1606 CamelMessageInfo *info;
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))
1620 imap_noop (CamelStore *store, CamelException *ex)
1622 CamelImapStore *imap_store = (CamelImapStore *) store;
1623 CamelDiscoStore *disco = (CamelDiscoStore *) store;
1624 CamelImapResponse *response;
1625 CamelFolder *current_folder;
1627 if (camel_disco_store_status (disco) != CAMEL_DISCO_STORE_ONLINE)
1630 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
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);
1637 response = camel_imap_command (imap_store, NULL, ex, "NOOP");
1639 camel_imap_response_free (imap_store, response);
1642 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1646 hash_folder_name (gconstpointer key)
1648 if (strcasecmp (key, "INBOX") == 0)
1649 return g_str_hash ("INBOX");
1651 return g_str_hash (key);
1655 compare_folder_name (gconstpointer a, gconstpointer b)
1657 gconstpointer aname = a, bname = b;
1659 if (strcasecmp (a, "INBOX") == 0)
1661 if (strcasecmp (b, "INBOX") == 0)
1663 return g_str_equal (aname, bname);
1666 static CamelFolder *
1667 no_such_folder (const char *name, CamelException *ex)
1669 camel_exception_setv (ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
1670 _("No such folder %s"), name);
1675 get_folder_status (CamelImapStore *imap_store, const char *folder_name, const char *type)
1677 CamelImapResponse *response;
1681 /* FIXME: we assume the server is STATUS-capable */
1683 response = camel_imap_command (imap_store, NULL, NULL,
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);
1696 camel_exception_clear (&ex);
1700 status = camel_imap_response_extract (imap_store, response,
1705 p = camel_strstrcase (status, type);
1707 out = strtoul (p + strlen (type), NULL, 10);
1715 static CamelFolder *
1716 get_folder_online (CamelStore *store, const char *folder_name,
1717 guint32 flags, CamelException *ex)
1719 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1720 CamelImapResponse *response;
1721 CamelFolder *new_folder;
1722 char *folder_dir, *storage_path;
1724 if (!camel_imap_store_connected (imap_store, ex))
1727 if (!strcasecmp (folder_name, "INBOX"))
1728 folder_name = "INBOX";
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;
1736 response = camel_imap_command (imap_store, NULL, NULL, "SELECT %F", folder_name);
1740 if (!flags & CAMEL_STORE_FOLDER_CREATE) {
1741 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1742 return no_such_folder (folder_name, ex);
1745 folder_real = camel_imap_store_summary_path_to_full(imap_store->summary, folder_name, store->dir_sep);
1747 response = camel_imap_command (imap_store, NULL, ex, "CREATE %S", folder_real);
1750 camel_imap_store_summary_add_from_full(imap_store->summary, folder_real, store->dir_sep);
1752 camel_imap_response_free (imap_store, response);
1754 response = camel_imap_command (imap_store, NULL, NULL, "SELECT %F", folder_name);
1756 g_free(folder_real);
1758 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
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);
1769 CamelException local_ex;
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);
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));
1784 camel_imap_response_free_without_processing (imap_store, response);
1786 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1791 static CamelFolder *
1792 get_folder_offline (CamelStore *store, const char *folder_name,
1793 guint32 flags, CamelException *ex)
1795 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1796 CamelFolder *new_folder;
1797 char *folder_dir, *storage_path;
1799 if (!imap_store->connected &&
1800 !camel_service_connect (CAMEL_SERVICE (store), ex))
1803 if (!strcasecmp (folder_name, "INBOX"))
1804 folder_name = "INBOX";
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);
1816 new_folder = camel_imap_folder_new (store, folder_name, folder_dir, ex);
1817 g_free (folder_dir);
1823 delete_folder (CamelStore *store, const char *folder_name, CamelException *ex)
1825 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1826 CamelImapResponse *response;
1828 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
1831 /* make sure this folder isn't currently SELECTed */
1832 response = camel_imap_command (imap_store, NULL, ex, "SELECT INBOX");
1834 camel_imap_response_free_without_processing (imap_store, response);
1836 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
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;
1843 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1847 response = camel_imap_command (imap_store, NULL, ex, "DELETE %F",
1851 camel_imap_response_free (imap_store, response);
1852 imap_forget_folder (imap_store, folder_name, ex);
1857 manage_subscriptions (CamelStore *store, const char *old_name, gboolean subscribe)
1859 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1861 int olen = strlen(old_name);
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);
1869 path = camel_store_info_path(imap_store->summary, si);
1870 if (strncmp(path, old_name, olen) == 0) {
1872 subscribe_folder(store, path, NULL);
1874 unsubscribe_folder(store, path, NULL);
1876 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
1882 rename_folder_info (CamelImapStore *imap_store, const char *old_name, const char *new_name)
1886 int olen = strlen(old_name);
1888 char *npath, *nfull;
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);
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);
1900 npath = g_strdup(new_name);
1901 nfull = camel_imap_store_summary_path_to_full(imap_store->summary, npath, imap_store->dir_sep);
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;
1908 response = camel_imap_command (imap_store, NULL, NULL, "RENAME %F %S", path, nfull);
1910 camel_imap_response_free (imap_store, response);
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);
1916 camel_store_summary_touch((CamelStoreSummary *)imap_store->summary);
1920 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
1925 rename_folder (CamelStore *store, const char *old_name, const char *new_name_in, CamelException *ex)
1927 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
1928 CamelImapResponse *response;
1929 char *oldpath, *newpath, *storage_path, *new_name;
1931 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
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");
1939 camel_imap_response_free_without_processing (imap_store, response);
1941 CAMEL_SERVICE_LOCK (imap_store, connect_lock);
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;
1948 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
1952 imap_store->renaming = TRUE;
1954 if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1955 manage_subscriptions(store, old_name, FALSE);
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);
1961 if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1962 manage_subscriptions(store, old_name, TRUE);
1964 imap_store->renaming = FALSE;
1968 camel_imap_response_free (imap_store, response);
1970 /* rename summary, and handle broken server */
1971 rename_folder_info(imap_store, old_name, new_name_in);
1973 if (store->flags & CAMEL_STORE_SUBSCRIPTIONS)
1974 manage_subscriptions(store, new_name_in, TRUE);
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);
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));
1991 imap_store->renaming = FALSE;
1994 static CamelFolderInfo *
1995 create_folder (CamelStore *store, const char *parent_name,
1996 const char *folder_name, CamelException *ex)
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;
2006 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
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);
2019 /* check if the parent allows inferiors */
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);
2029 need_convert = FALSE;
2030 response = camel_imap_command (imap_store, NULL, ex, "LIST \"\" %S",
2032 if (!response) /* whoa, this is bad */ {
2033 g_free(parent_real);
2037 /* FIXME: does not handle unexpected circumstances very well */
2038 for (i = 0; i < response->untagged->len; i++) {
2039 resp = response->untagged->pdata[i];
2041 if (!imap_parse_list_response (imap_store, resp, &flags, NULL, &thisone))
2044 if (strcmp (thisone, parent_name) == 0) {
2045 if (flags & CAMEL_FOLDER_NOINFERIORS)
2046 need_convert = TRUE;
2051 camel_imap_response_free (imap_store, response);
2053 camel_exception_init (&internal_ex);
2055 /* if not, check if we can delete it and recreate it */
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);
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);
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",
2080 g_free(parent_real);
2083 camel_imap_response_free (imap_store, response);
2085 root = imap_build_folder_info(imap_store, parent_name);
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);
2092 response = camel_imap_command (imap_store, NULL, ex, "CREATE %S", full_name);
2095 CamelImapStoreInfo *si;
2096 CamelFolderInfo *fi;
2098 camel_imap_response_free (imap_store, response);
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));
2109 camel_object_trigger_event (CAMEL_OBJECT (store), "folder_created", 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);
2118 g_free(parent_real);
2123 static CamelFolderInfo *
2124 parse_list_response_as_folder_info (CamelImapStore *imap_store,
2125 const char *response)
2127 CamelFolderInfo *fi;
2131 CamelImapStoreInfo *si;
2134 if (!imap_parse_list_response (imap_store, response, &flags, &sep, &dir))
2137 /* FIXME: should use imap_build_folder_info, note the differences with param setting tho */
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);
2146 fi = g_new0 (CamelFolderInfo, 1);
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);
2152 url = camel_url_new (imap_store->base_url, NULL);
2153 camel_url_set_path(url, fi->path);
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);
2160 /* FIXME: redundant */
2161 if (flags & CAMEL_IMAP_FOLDER_UNMARKED)
2162 fi->unread_message_count = -1;
2167 /* this is used when lsub doesn't provide very useful information */
2169 get_subscribed_folders (CamelImapStore *imap_store, const char *top, CamelException *ex)
2171 GPtrArray *names, *folders;
2172 int i, toplen = strlen (top);
2174 CamelImapResponse *response;
2175 CamelFolderInfo *fi;
2177 int haveinbox = FALSE;
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;
2186 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2190 g_ptr_array_add (names, "INBOX");
2192 for (i = 0; i < names->len; i++) {
2193 response = camel_imap_command (imap_store, NULL, ex,
2199 result = camel_imap_response_extract (imap_store, response, "LIST", NULL);
2201 camel_store_summary_remove_path((CamelStoreSummary *)imap_store->summary, names->pdata[i]);
2202 g_ptr_array_remove_index_fast (names, i);
2207 fi = parse_list_response_as_folder_info (imap_store, result);
2211 if (strncmp (top, fi->full_name, toplen) != 0) {
2212 camel_folder_info_free (fi);
2216 g_ptr_array_add (folders, fi);
2219 g_ptr_array_free (names, TRUE);
2224 static int imap_match_pattern(char dir_sep, const char *pattern, const char *name)
2234 } else if (p == '%') {
2240 } else if (p == '*') {
2246 return n == 0 && (p == '%' || p == 0);
2250 get_folders_online (CamelImapStore *imap_store, const char *pattern,
2251 GPtrArray *folders, gboolean lsub, CamelException *ex)
2253 CamelImapResponse *response;
2254 CamelFolderInfo *fi;
2257 GHashTable *present;
2260 response = camel_imap_command (imap_store, NULL, ex,
2261 "%s \"\" %S", lsub ? "LSUB" : "LIST",
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);
2271 g_ptr_array_add(folders, fi);
2272 g_hash_table_insert(present, fi->full_name, fi);
2275 camel_imap_response_free (imap_store, response);
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);
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);
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);
2297 camel_store_summary_remove((CamelStoreSummary *)imap_store->summary, si);
2303 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2305 g_hash_table_destroy(present);
2310 dumpfi(CamelFolderInfo *fi)
2313 CamelFolderInfo *n = fi;
2325 printf("%-40s %-30s %*s\n", fi->path, fi->full_name, depth*2+strlen(fi->url), fi->url);
2334 get_folder_counts(CamelImapStore *imap_store, CamelFolderInfo *fi, CamelException *ex)
2337 CamelFolder *folder;
2339 /* non-recursive breath first search */
2341 q = g_slist_append(NULL, fi);
2345 q = g_slist_remove_link(q, q);
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) ) {
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.
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);
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);
2372 camel_object_unref(folder);
2376 CAMEL_SERVICE_UNLOCK (imap_store, connect_lock);
2378 /* since its cheap, get it if they're open */
2379 folder = camel_object_bag_get(CAMEL_STORE(imap_store)->folders, fi->full_name);
2381 fi->unread_message_count = camel_folder_get_unread_message_count(folder);
2382 camel_object_unref(folder);
2384 fi->unread_message_count = -1;
2388 q = g_slist_append(q, fi->child);
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)
2400 if (strcasecmp(a, "INBOX") == 0)
2403 return g_str_hash(a);
2406 static int folder_eq(const void *ap, const void *bp)
2411 if (strcasecmp(a, "INBOX") == 0)
2413 if (strcasecmp(b, "INBOX") == 0)
2416 return g_str_equal(a, b);
2420 get_folders(CamelStore *store, const char *top, guint32 flags, CamelException *ex)
2422 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2426 GPtrArray *folders, *folders_out;
2427 CamelFolderInfo *fi;
2431 static int imap_max_depth = 0;
2433 if (!camel_imap_store_connected (imap_store, ex))
2436 /* allow megalomaniacs to override the max of 10 */
2437 if (imap_max_depth == 0) {
2438 name = getenv("CAMEL_IMAP_MAX_DEPTH");
2440 imap_max_depth = atoi (name);
2441 imap_max_depth = MIN (MAX (imap_max_depth, 0), 2);
2443 imap_max_depth = 10;
2446 infos = g_hash_table_new(folder_hash, folder_eq);
2448 /* get starting point & strip trailing '/' */
2450 if (imap_store->namespace) {
2451 top = imap_store->namespace;
2453 name = g_malloc(i+2);
2455 while (i>0 && name[i] == store->dir_sep)
2458 name = g_strdup("");
2460 name = camel_imap_store_summary_full_from_path(imap_store->summary, top);
2462 name = camel_imap_store_summary_path_to_full(imap_store->summary, top, store->dir_sep);
2465 d(printf("\n\nList '%s' %s\n", name, flags&CAMEL_STORE_FOLDER_INFO_RECURSIVE?"RECURSIVE":"NON-RECURSIVE"));
2467 folders_out = g_ptr_array_new();
2468 folders = g_ptr_array_new();
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))
2474 for (i=0; i<folders->len && !haveinbox; i++) {
2475 fi = folders->pdata[i];
2476 haveinbox = (strcasecmp(fi->full_name, "INBOX")) == 0;
2479 if (!haveinbox && top == imap_store->namespace) {
2480 get_folders_online (imap_store, "INBOX", folders,
2481 flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, ex);
2483 if (camel_exception_is_set (ex))
2487 for (i=0; i<folders->len; i++)
2488 p = g_slist_prepend(p, folders->pdata[i]);
2490 g_ptr_array_set_size(folders, 0);
2492 /* p is a reversed list of pending folders for the next level, q is the list of folders for this */
2494 GSList *q = g_slist_reverse(p);
2500 q = g_slist_remove_link(q, q);
2501 g_ptr_array_add(folders_out, fi);
2503 d(printf("Checking folder '%s'\n", fi->full_name));
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) {
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) {
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);
2523 if (folders->len > 0)
2524 fi->flags |= CAMEL_FOLDER_CHILDREN;
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);
2533 g_ptr_array_add(folders_out, fi);
2535 camel_folder_info_free(fi);
2538 g_ptr_array_set_size(folders, 0);
2544 g_ptr_array_free(folders, TRUE);
2545 g_hash_table_destroy(infos);
2550 g_ptr_array_free(folders, TRUE);
2551 g_ptr_array_free(folders_out, TRUE);
2552 g_hash_table_destroy(infos);
2558 static CamelFolderInfo *
2559 get_folder_info_online (CamelStore *store, const char *top, guint32 flags, CamelException *ex)
2561 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2562 CamelFolderInfo *tree;
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);
2573 folders = get_folders(store, top, flags, ex);
2575 if (folders == NULL)
2578 tree = camel_folder_info_build(folders, top, '/', TRUE);
2579 g_ptr_array_free(folders, TRUE);
2581 if (!(flags & CAMEL_STORE_FOLDER_INFO_FAST))
2582 get_folder_counts(imap_store, tree, ex);
2585 camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
2591 get_one_folder_offline (const char *physical_path, const char *path, gpointer data)
2593 GPtrArray *folders = data;
2594 CamelImapStore *imap_store = folders->pdata[0];
2595 CamelFolderInfo *fi;
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.
2607 si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, path+1);
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);
2616 camel_url_set_param (url, "noselect", "yes");
2618 fi->url = camel_url_to_string (url, 0);
2619 camel_url_free (url);
2621 g_ptr_array_add (folders, fi);
2623 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2629 static CamelFolderInfo *
2630 get_folder_info_offline (CamelStore *store, const char *top,
2631 guint32 flags, CamelException *ex)
2633 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2634 CamelFolderInfo *fi;
2638 if (!imap_store->connected &&
2639 !camel_service_connect (CAMEL_SERVICE (store), ex))
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);
2648 /* FIXME: obey other flags */
2650 folders = g_ptr_array_new ();
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);
2659 g_ptr_array_remove_index_fast (folders, 0);
2660 fi = camel_folder_info_build (folders, "", '/', TRUE);
2662 g_free(storage_path);
2664 g_ptr_array_free (folders, TRUE);
2669 folder_subscribed (CamelStore *store, const char *folder_name)
2671 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2675 si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
2677 truth = (si->flags & CAMEL_STORE_INFO_FOLDER_SUBSCRIBED) != 0;
2678 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
2684 /* Note: folder_name must match a folder as listed with get_folder_info() -> full_name */
2686 subscribe_folder (CamelStore *store, const char *folder_name,
2689 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2690 CamelImapResponse *response;
2691 CamelFolderInfo *fi;
2694 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
2696 if (!camel_imap_store_connected (imap_store, ex))
2699 response = camel_imap_command (imap_store, NULL, ex,
2700 "SUBSCRIBE %F", folder_name);
2703 camel_imap_response_free (imap_store, response);
2705 si = camel_store_summary_path((CamelStoreSummary *)imap_store->summary, folder_name);
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);
2712 camel_store_summary_info_free((CamelStoreSummary *)imap_store->summary, si);
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
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);
2728 unsubscribe_folder (CamelStore *store, const char *folder_name,
2731 CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
2732 CamelImapResponse *response;
2734 if (!camel_disco_store_check_online (CAMEL_DISCO_STORE (store), ex))
2736 if (!camel_imap_store_connected (imap_store, ex))
2739 response = camel_imap_command (imap_store, NULL, ex,
2740 "UNSUBSCRIBE %F", folder_name);
2743 camel_imap_response_free (imap_store, response);
2745 imap_folder_effectively_unsubscribed (imap_store, folder_name, ex);
2750 folder_flags_have_changed (CamelFolder *folder)
2752 CamelMessageInfo *info;
2755 max = camel_folder_summary_count (folder->summary);
2756 for (i = 0; i < max; i++) {
2757 info = camel_folder_summary_index (folder->summary, i);
2760 if (info->flags & CAMEL_MESSAGE_FOLDER_FLAGGED) {
2771 camel_imap_store_connected (CamelImapStore *store, CamelException *ex)
2773 if (store->istream == NULL || !store->connected)
2774 return camel_service_connect (CAMEL_SERVICE (store), ex);
2779 /* FIXME: please god, when will the hurting stop? Thus function is so
2780 fucking broken it's not even funny. */
2782 camel_imap_store_readline (CamelImapStore *store, char **dest, CamelException *ex)
2784 CamelStreamBuffer *stream;
2789 g_return_val_if_fail (CAMEL_IS_IMAP_STORE (store), -1);
2790 g_return_val_if_fail (dest, -1);
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.
2799 if (!camel_imap_store_connected (store, ex)) {
2800 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_NOT_CONNECTED,
2801 g_strerror (errno));
2805 stream = CAMEL_STREAM_BUFFER (store->istream);
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')
2816 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, _("Operation cancelled"));
2818 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
2819 _("Server unexpectedly disconnected: %s"),
2820 g_strerror (errno));
2822 camel_service_disconnect (CAMEL_SERVICE (store), FALSE, NULL);
2823 g_byte_array_free (ba, TRUE);
2827 if (camel_verbose_debug) {
2828 fprintf (stderr, "received: ");
2829 fwrite (ba->data, 1, ba->len, stderr);
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';
2842 g_byte_array_free (ba, FALSE);