1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-imap-store.c : class for a imap store */
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * Copyright (C) 2000-2002 Ximian, Inc. (www.ximian.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
38 #include "camel/camel-operation.h"
40 #include "camel/camel-stream-buffer.h"
41 #include "camel/camel-session.h"
42 #include "camel/camel-exception.h"
43 #include "camel/camel-url.h"
44 #include "camel/camel-sasl.h"
45 #include "camel/camel-data-cache.h"
46 #include "camel/camel-tcp-stream.h"
47 #include "camel/camel-tcp-stream-raw.h"
49 #include "camel/camel-tcp-stream-ssl.h"
52 #include "camel-imapp-store-summary.h"
53 #include "camel-imapp-store.h"
54 #include "camel-imapp-folder.h"
55 #include "camel-imapp-engine.h"
56 #include "camel-imapp-exception.h"
57 #include "camel-imapp-utils.h"
58 #include "camel-imapp-driver.h"
60 /* Specified in RFC 2060 section 2.1 */
63 static CamelStoreClass *parent_class = NULL;
65 static void finalize (CamelObject *object);
67 static void imap_construct(CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex);
68 /* static char *imap_get_name(CamelService *service, gboolean brief);*/
69 static gboolean imap_connect (CamelService *service, CamelException *ex);
70 static gboolean imap_disconnect (CamelService *service, gboolean clean, CamelException *ex);
71 static GList *imap_query_auth_types (CamelService *service, CamelException *ex);
73 static void imap_init_trash (CamelStore *store);
74 static CamelFolder *imap_get_trash (CamelStore *store, CamelException *ex);
76 static CamelFolder *imap_get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex);
77 static CamelFolder *imap_get_inbox (CamelStore *store, CamelException *ex);
78 static void imap_rename_folder(CamelStore *store, const char *old_name, const char *new_name, CamelException *ex);
79 static CamelFolderInfo *imap_get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
80 static void imap_delete_folder(CamelStore *store, const char *folder_name, CamelException *ex);
81 static void imap_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex);
82 static CamelFolderInfo *imap_create_folder(CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex);
84 static int store_resp_list(CamelIMAPPEngine *ie, guint32 id, void *data);
86 /* yet to see if this should go global or not */
87 void camel_imapp_store_folder_selected(CamelIMAPPStore *store, CamelIMAPPFolder *folder, CamelIMAPPSelectResponse *select);
90 camel_imapp_store_class_init (CamelIMAPPStoreClass *camel_imapp_store_class)
92 CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_imapp_store_class);
93 CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_imapp_store_class);
95 parent_class = CAMEL_STORE_CLASS(camel_type_get_global_classfuncs(camel_store_get_type()));
97 /* virtual method overload */
98 camel_service_class->construct = imap_construct;
99 /*camel_service_class->get_name = imap_get_name;*/
100 camel_service_class->query_auth_types = imap_query_auth_types;
101 camel_service_class->connect = imap_connect;
102 camel_service_class->disconnect = imap_disconnect;
104 camel_store_class->init_trash = imap_init_trash;
105 camel_store_class->get_trash = imap_get_trash;
107 camel_store_class->get_folder = imap_get_folder;
108 camel_store_class->get_inbox = imap_get_inbox;
110 camel_store_class->create_folder = imap_create_folder;
111 camel_store_class->rename_folder = imap_rename_folder;
112 camel_store_class->delete_folder = imap_delete_folder;
113 camel_store_class->get_folder_info = imap_get_folder_info;
117 camel_imapp_store_init (gpointer object, gpointer klass)
119 CamelIMAPPStore *istore = object;
123 camel_imapp_store_get_type (void)
125 static CamelType camel_imapp_store_type = CAMEL_INVALID_TYPE;
127 if (!camel_imapp_store_type) {
128 camel_imapp_store_type = camel_type_register(CAMEL_STORE_TYPE,
130 sizeof (CamelIMAPPStore),
131 sizeof (CamelIMAPPStoreClass),
132 (CamelObjectClassInitFunc) camel_imapp_store_class_init,
134 (CamelObjectInitFunc) camel_imapp_store_init,
138 return camel_imapp_store_type;
142 finalize (CamelObject *object)
144 CamelIMAPPStore *imap_store = CAMEL_IMAPP_STORE (object);
146 /* force disconnect so we dont have it run later, after we've cleaned up some stuff */
149 camel_service_disconnect((CamelService *)imap_store, TRUE, NULL);
151 if (imap_store->driver)
152 camel_object_unref(imap_store->driver);
153 if (imap_store->cache)
154 camel_object_unref(imap_store->cache);
157 static void imap_construct(CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
159 char *root, *summary;
160 CamelIMAPPStore *store = (CamelIMAPPStore *)service;
162 CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
163 if (camel_exception_is_set(ex))
167 store->summary = camel_imapp_store_summary_new();
168 root = camel_session_get_storage_path(service->session, service, ex);
170 summary = g_build_filename(root, ".ev-store-summary", NULL);
171 camel_store_summary_set_filename((CamelStoreSummary *)store->summary, summary);
172 /* FIXME: need to remove params, passwords, etc */
173 camel_store_summary_set_uri_base((CamelStoreSummary *)store->summary, service->url);
174 camel_store_summary_load((CamelStoreSummary *)store->summary);
177 camel_exception_xfer(ex, e);
184 USE_SSL_WHEN_POSSIBLE
187 #define SSL_PORT_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3)
188 #define STARTTLS_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_TLS)
191 connect_to_server (CamelService *service, int ssl_mode, int try_starttls)
192 /* throws IO exception */
194 CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);
195 CamelStream * volatile tcp_stream = NULL;
196 CamelIMAPPStream * volatile imap_stream = NULL;
197 struct hostent *h = NULL;
201 ex = camel_exception_new();
203 /* parent class connect initialization */
204 CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex);
206 camel_exception_throw_ex(ex);
208 h = camel_service_gethost(service, ex);
210 camel_exception_throw_ex(ex);
212 port = service->url->port ? service->url->port : IMAP_PORT;
215 if (camel_url_get_param (service->url, "use_ssl")) {
217 tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS);
219 port = service->url->port ? service->url->port : 995;
220 tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS);
223 tcp_stream = camel_tcp_stream_raw_new ();
226 tcp_stream = camel_tcp_stream_raw_new ();
227 #endif /* HAVE_SSL */
229 ret = camel_tcp_stream_connect (CAMEL_TCP_STREAM (tcp_stream), h, port);
233 camel_exception_throw(CAMEL_EXCEPTION_USER_CANCEL, _("Connection cancelled"));
235 camel_exception_throw(CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
236 _("Could not connect to %s (port %d): %s"),
237 service->url->host, port, strerror(errno));
240 imap_stream = (CamelIMAPPStream *)camel_imapp_stream_new(tcp_stream);
241 store->driver = camel_imapp_driver_new(imap_stream);
243 camel_object_unref(imap_stream);
244 camel_object_unref(tcp_stream);
247 camel_object_unref(tcp_stream);
249 camel_object_unref((CamelObject *)imap_stream);
250 camel_exception_throw_ex(e);
253 camel_exception_free(ex);
258 /* leave this stuff out for now */
265 { "", USE_SSL_ALWAYS },
266 { "always", USE_SSL_ALWAYS },
267 { "when-possible", USE_SSL_WHEN_POSSIBLE },
268 { "never", USE_SSL_NEVER },
269 { NULL, USE_SSL_NEVER },
273 connect_to_server_wrapper (CamelService *service, CamelException *ex)
279 use_ssl = camel_url_get_param (service->url, "use_ssl");
281 for (i = 0; ssl_options[i].value; i++)
282 if (!strcmp (ssl_options[i].value, use_ssl))
284 ssl_mode = ssl_options[i].mode;
286 ssl_mode = USE_SSL_NEVER;
288 if (ssl_mode == USE_SSL_ALWAYS) {
289 /* First try the ssl port */
290 if (!connect_to_server (service, ssl_mode, FALSE, ex)) {
291 if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) {
292 /* The ssl port seems to be unavailable, lets try STARTTLS */
293 camel_exception_clear (ex);
294 return connect_to_server (service, ssl_mode, TRUE, ex);
301 } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
302 /* If the server supports STARTTLS, use it */
303 return connect_to_server (service, ssl_mode, TRUE, ex);
305 /* User doesn't care about SSL */
306 return connect_to_server (service, ssl_mode, FALSE, ex);
309 return connect_to_server (service, USE_SSL_NEVER, FALSE, ex);
314 extern CamelServiceAuthType camel_imapp_password_authtype;
315 extern CamelServiceAuthType camel_imapp_apop_authtype;
318 imap_query_auth_types (CamelService *service, CamelException *ex)
320 /*CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);*/
323 types = CAMEL_SERVICE_CLASS (parent_class)->query_auth_types (service, ex);
328 if (connect_to_server_wrapper (service, NULL)) {
329 types = g_list_concat(types, g_list_copy(store->engine->auth));
330 imap_disconnect (service, TRUE, NULL);
332 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
333 _("Could not connect to POP server on %s"),
341 store_get_pass(CamelIMAPPStore *store)
343 if (((CamelService *)store)->url->passwd == NULL) {
347 camel_exception_init(&ex);
349 prompt = g_strdup_printf (_("%sPlease enter the IMAP password for %s@%s"),
350 store->login_error?store->login_error:"",
351 ((CamelService *)store)->url->user,
352 ((CamelService *)store)->url->host);
353 ((CamelService *)store)->url->passwd = camel_session_get_password(camel_service_get_session((CamelService *)store),
354 prompt, FALSE, TRUE, (CamelService*)store, "password", &ex);
356 if (camel_exception_is_set(&ex))
357 camel_exception_throw_ex(&ex);
361 static struct _CamelSasl *
362 store_get_sasl(struct _CamelIMAPPDriver *driver, CamelIMAPPStore *store)
364 store_get_pass(store);
366 if (((CamelService *)store)->url->authmech)
367 return camel_sasl_new("imap", ((CamelService *)store)->url->authmech, (CamelService *)store);
373 store_get_login(struct _CamelIMAPPDriver *driver, char **login, char **pass, CamelIMAPPStore *store)
375 store_get_pass(store);
377 *login = g_strdup(((CamelService *)store)->url->user);
378 *pass = g_strdup(((CamelService *)store)->url->passwd);
382 imap_connect (CamelService *service, CamelException *ex)
384 CamelIMAPPStore *store = (CamelIMAPPStore *)service;
385 volatile int ret = FALSE;
388 volatile int retry = TRUE;
390 if (store->cache == NULL) {
393 root = camel_session_get_storage_path(service->session, service, ex);
395 store->cache = camel_data_cache_new(root, 0, ex);
398 /* Default cache expiry - 1 week or not visited in a day */
399 camel_data_cache_set_expire_age(store->cache, 60*60*24*7);
400 camel_data_cache_set_expire_access(store->cache, 60*60*24);
403 if (camel_exception_is_set(ex))
404 camel_exception_throw_ex(ex);
407 connect_to_server(service, USE_SSL_NEVER, FALSE);
409 camel_imapp_driver_set_sasl_factory(store->driver, (CamelIMAPPSASLFunc)store_get_sasl, store);
410 camel_imapp_driver_set_login_query(store->driver, (CamelIMAPPLoginFunc)store_get_login, store);
411 store->login_error = NULL;
415 if (store->driver->engine->state != IMAP_ENGINE_AUTH)
416 camel_imapp_driver_login(store->driver);
420 g_free(store->login_error);
421 store->login_error = NULL;
423 case CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE:
424 store->login_error = g_strdup_printf("%s\n\n", e->desc);
425 camel_session_forget_password(service->session, service, "password", ex);
426 camel_url_set_passwd(service->url, NULL);
429 camel_exception_throw_ex(e);
435 camel_exception_xfer(ex, e);
436 camel_service_disconnect(service, TRUE, NULL);
440 g_free(store->login_error);
441 store->login_error = NULL;
447 imap_disconnect (CamelService *service, gboolean clean, CamelException *ex)
449 CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);
453 if (!CAMEL_SERVICE_CLASS (parent_class)->disconnect (service, clean, ex))
456 /* logout/disconnect */
458 camel_object_unref(store->driver);
459 store->driver = NULL;
466 imap_init_trash (CamelStore *store)
473 imap_get_trash (CamelStore *store, CamelException *ex)
480 imap_get_folder (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex)
482 CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
483 CamelIMAPPFolder * volatile folder = NULL;
487 /* 1. create the folder */
489 /* 3. update the folder */
492 folder = (CamelIMAPPFolder *)camel_imapp_folder_new(store, folder_name);
493 camel_imapp_driver_select(istore->driver, folder);
496 camel_object_unref(folder);
499 camel_exception_xfer(ex, e);
502 return (CamelFolder *)folder;
506 imap_get_inbox(CamelStore *store, CamelException *ex)
508 camel_exception_setv(ex, 1, "get_inbox::unimplemented");
513 /* 8 bit, string compare */
514 static int folders_build_cmp(const void *app, const void *bpp)
516 struct _list_info *a = *((struct _list_info **)app);
517 struct _list_info *b = *((struct _list_info **)bpp);
518 unsigned char *ap = (unsigned char *)(a->name);
519 unsigned char *bp = (unsigned char *)(b->name);
521 printf("qsort, cmp '%s' <> '%s'\n", ap, bp);
523 while (*ap && *ap == *bp) {
535 /* FIXME: this should go via storesummary? */
536 static CamelFolderInfo *
537 folders_build_info(CamelURL *base, struct _list_info *li)
539 char *path, *full_name, *name;
542 full_name = imapp_list_get_path(li);
543 name = strrchr(full_name, '/');
549 path = alloca(strlen(full_name)+2);
550 sprintf(path, "/%s", full_name);
551 camel_url_set_path(base, path);
553 fi = g_malloc0(sizeof(*fi));
554 fi->url = camel_url_to_string(base, CAMEL_URL_HIDE_ALL);
555 fi->name = g_strdup(name);
556 fi->full_name = full_name;
557 fi->path = g_strdup(path);
558 fi->unread_message_count = -1;
559 fi->flags = li->flags;
561 /* TODO: could look up count here ... */
563 /*folder = camel_object_bag_get(store->folders, "INBOX");*/
578 /* note, pname is the raw name, not the folderinfo name */
579 /* note also this free's as we go, since we never go 'backwards' */
580 static CamelFolderInfo *
581 folders_build_rec(CamelURL *base, GPtrArray *folders, int *ip, CamelFolderInfo *pfi, char *pname)
584 CamelFolderInfo *last = NULL, *first = NULL;
587 plen = strlen(pname);
589 for(;(*ip)<(int)folders->len;) {
591 struct _list_info *li;
593 li = folders->pdata[*ip];
594 printf("checking '%s' is child of '%s'\n", li->name, pname);
596 /* is this a child of the parent? */
598 && (strncmp(pname, li->name, strlen(pname)) != 0
599 || li->name[plen] != li->separator)) {
605 /* is this not an immediate child of the parent? */
609 && li->separator != 0
610 && (p = strchr(li->name + plen + 1, li->separator)) != NULL) {
612 struct _list_info tli;
614 tli.flags = CAMEL_FOLDER_NOSELECT|CAMEL_FOLDER_CHILDREN;
615 tli.separator = li->separator;
616 tli.name = g_strndup(li->name, p-li->name+1);
617 fi = folders_build_info(base, &tli);
619 if (pfi && pfi->child == NULL)
621 i = folders_build_rec(folders, i, fi, tli.name);
627 fi = folders_build_info(base, li);
636 fi->child = folders_build_rec(base, folders, ip, fi, li->name);
644 folder_info_dump(CamelFolderInfo *fi, int depth)
649 memset(s, ' ', depth);
652 printf("%s%s (%s)\n", s, fi->name, fi->url);
654 folder_info_dump(fi->child, depth+2);
660 static CamelFolderInfo *
661 imap_get_folder_info(CamelStore *store, const char *top, guint32 flags, CamelException *ex)
663 CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
664 CamelFolderInfo * fi= NULL;
667 /* FIXME: temporary, since this is not a disco store */
668 if (istore->driver == NULL
669 && !camel_service_connect((CamelService *)store, ex))
673 if (name == NULL || name[0] == 0) {
685 /* FIXME: subscriptions? lsub? */
686 folders = camel_imapp_driver_list(istore->driver, name, flags);
688 /* this greatly simplifies the tree algorithm ... but it might
689 be faster just to use a hashtable to find parents? */
690 qsort(folders->pdata, folders->len, sizeof(folders->pdata[0]), folders_build_cmp);
693 base = camel_url_copy(((CamelService *)store)->url);
694 fi = folders_build_rec(base, folders, &i, NULL, NULL);
695 camel_url_free(base);
696 g_ptr_array_free(folders, TRUE);
698 camel_exception_xfer(ex, e);
701 printf("built folder info:\n");
702 folder_info_dump(fi, 2);
707 if (top == NULL || !g_ascii_strcasecmp(top, "inbox")) {
708 CamelURL *uri = camel_url_copy(((CamelService *)store)->url);
710 camel_url_set_path(uri, "/INBOX");
711 fi = g_malloc0(sizeof(*fi));
712 fi->url = camel_url_to_string(uri, CAMEL_URL_HIDE_ALL);
714 fi->name = g_strdup("INBOX");
715 fi->full_name = g_strdup("INBOX");
716 fi->path = g_strdup("/INBOX");
717 fi->unread_message_count = -1;
720 folder = camel_object_bag_get(store->folders, "INBOX");
722 /*if (!cflags & FAST)*/
723 camel_imapp_driver_update(istore->driver, (CamelIMAPPFolder *)folder);
724 fi->unread_message_count = camel_folder_get_unread_message_count(folder);
725 camel_object_unref(folder);
728 camel_exception_setv(ex, 1, "not implemented");
734 istore->pending_list = g_ptr_array_new();
737 ic = camel_imapp_engine_command_new(istore->driver->engine, "LIST", NULL, "LIST \"\" %f", top);
738 camel_imapp_engine_command_queue(istore->driver->engine, ic);
739 while (camel_imapp_engine_iterate(istore->driver->engine, ic) > 0)
742 if (ic->status->result != IMAP_OK)
743 camel_exception_throw(1, "list failed: %s", ic->status->text);
745 camel_exception_xfer(ex, e);
748 camel_imapp_engine_command_free(istore->driver->engine, ic);
750 printf("got folder list:\n");
751 for (i=0;i<(int)istore->pending_list->len;i++) {
752 struct _list_info *linfo = istore->pending_list->pdata[i];
754 printf("%s (%c)\n", linfo->name, linfo->separator);
755 imap_free_list(linfo);
757 istore->pending_list = NULL;
764 imap_delete_folder(CamelStore *store, const char *folder_name, CamelException *ex)
766 camel_exception_setv(ex, 1, "delete_folder::unimplemented");
770 imap_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex)
772 camel_exception_setv(ex, 1, "rename_folder::unimplemented");
775 static CamelFolderInfo *
776 imap_create_folder(CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex)
778 camel_exception_setv(ex, 1, "create_folder::unimplemented");
782 /* ********************************************************************** */
784 static int store_resp_fetch(CamelIMAPPEngine *ie, guint32 id, void *data)
786 struct _fetch_info *finfo;
787 CamelIMAPPStore *istore = data;
788 CamelMessageInfo *info;
789 struct _pending_fetch *pending;
791 finfo = imap_parse_fetch(ie->stream);
792 if (istore->selected) {
793 if ((finfo->got & FETCH_UID) == 0) {
794 printf("didn't get uid in fetch response?\n");
796 info = camel_folder_summary_index(((CamelFolder *)istore->selected)->summary, id-1);
797 /* exists, check/update */
799 if (strcmp(finfo->uid, camel_message_info_uid(info)) != 0) {
800 printf("summary at index %d has uid %s expected %s\n", id, camel_message_info_uid(info), finfo->uid);
801 /* uid mismatch??? try do it based on uid instead? try to reorder? i dont know? */
802 camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
803 info = camel_folder_summary_uid(((CamelFolder *)istore->selected)->summary, finfo->uid);
808 if (finfo->got & (FETCH_FLAGS)) {
809 printf("updating flags for uid '%s'\n", finfo->uid);
810 info->flags = finfo->flags;
811 camel_folder_change_info_change_uid(istore->selected->changes, finfo->uid);
813 if (finfo->got & FETCH_MINFO) {
814 printf("got envelope unexpectedly?\n");
816 /* other things go here, like body fetches */
818 pending = g_hash_table_lookup(istore->pending_fetch_table, finfo->uid);
820 /* we need to create a new info, we only care about flags and minfo */
823 info = pending->info;
825 info = camel_folder_summary_info_new(((CamelFolder *)istore->selected)->summary);
826 camel_message_info_set_uid(info, g_strdup(finfo->uid));
829 if (finfo->got & FETCH_FLAGS)
830 info->flags = finfo->flags;
832 if (finfo->got & FETCH_MINFO) {
833 /* if we only use ENVELOPE? */
834 camel_message_info_set_subject(info, g_strdup(camel_message_info_subject(finfo->minfo)));
835 camel_message_info_set_from(info, g_strdup(camel_message_info_from(finfo->minfo)));
836 camel_message_info_set_to(info, g_strdup(camel_message_info_to(finfo->minfo)));
837 camel_message_info_set_cc(info, g_strdup(camel_message_info_cc(finfo->minfo)));
838 info->date_sent = finfo->minfo->date_sent;
839 camel_folder_summary_add(((CamelFolder *)istore->selected)->summary, info);
840 camel_folder_change_info_add_uid(istore->selected->changes, finfo->uid);
842 e_dlist_remove((EDListNode *)pending);
843 g_hash_table_remove(istore->pending_fetch_table, finfo->uid);
844 /*e_memchunk_free(istore->pending_fetch_chunks, pending);*/
846 } else if (finfo->got & FETCH_HEADER) {
847 /* if we only use HEADER? */
851 camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
852 mp = camel_mime_parser_new();
853 camel_mime_parser_init_with_stream(mp, finfo->header);
854 info = camel_folder_summary_info_new_from_parser(((CamelFolder *)istore->selected)->summary, mp);
855 camel_object_unref(mp);
856 camel_message_info_set_uid(info, g_strdup(finfo->uid));
858 camel_folder_summary_add(((CamelFolder *)istore->selected)->summary, info);
859 camel_folder_change_info_add_uid(istore->selected->changes, finfo->uid);
861 /* FIXME: use a dlist */
862 e_dlist_remove((EDListNode *)pending);
863 g_hash_table_remove(istore->pending_fetch_table, camel_message_info_uid(pending->info));
864 camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, pending->info);
865 /*e_memchunk_free(istore->pending_fetch_chunks, pending);*/
867 } else if (finfo->got & FETCH_FLAGS) {
868 if (pending == NULL) {
869 pending = e_memchunk_alloc(istore->pending_fetch_chunks);
870 pending->info = info;
871 g_hash_table_insert(istore->pending_fetch_table, (char *)camel_message_info_uid(info), pending);
872 e_dlist_addtail(&istore->pending_fetch_list, (EDListNode *)pending);
876 camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
877 printf("got unexpected fetch response?\n");
878 imap_dump_fetch(finfo);
883 printf("unexpected fetch response, no folder selected?\n");
885 /*imap_dump_fetch(finfo);*/
886 imap_free_fetch(finfo);
888 return camel_imapp_engine_skip(ie);
892 /* ********************************************************************** */
894 /* should be moved to imapp-utils?
895 stuff in imapp-utils should be moved to imapp-parse? */
897 /* ********************************************************************** */
901 camel_imapp_store_folder_selected(CamelIMAPPStore *store, CamelIMAPPFolder *folder, CamelIMAPPSelectResponse *select)
903 CamelIMAPPCommand * volatile ic = NULL;
904 CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
906 struct _uidset_state ss;
908 CamelMessageInfo *info;
909 struct _pending_fetch *fw, *fn;
911 printf("imap folder selected\n");
913 if (select->uidvalidity == folder->uidvalidity
914 && select->exists == folder->exists
915 && select->recent == folder->recent
916 && select->unseen == folder->unseen) {
921 istore->pending_fetch_table = g_hash_table_new(g_str_hash, g_str_equal);
922 istore->pending_fetch_chunks = e_memchunk_new(256, sizeof(struct _pending_fetch));
924 /* perform an update - flags first (and see what we have) */
926 ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "FETCH 1:%d (UID FLAGS)", select->exists);
927 camel_imapp_engine_command_queue(istore->engine, ic);
928 while (camel_imapp_engine_iterate(istore->engine, ic) > 0)
931 if (ic->status->result != IMAP_OK)
932 camel_exception_throw(1, "fetch failed: %s", ic->status->text);
934 /* pending_fetch_list now contains any new messages */
935 /* FIXME: how do we work out no-longer present messages? */
936 printf("now fetching info for messages?\n");
937 uidset_init(&ss, store->engine);
938 ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "UID FETCH ");
939 fw = (struct _pending_fetch *)istore->pending_fetch_list.head;
943 /* if the uid set fills, then flush the command out */
944 if (uidset_add(&ss, ic, camel_message_info_uid(info))
945 || (fn->next == NULL && uidset_done(&ss, ic))) {
946 camel_imapp_engine_command_add(istore->engine, ic, " (FLAGS RFC822.HEADER)");
947 camel_imapp_engine_command_queue(istore->engine, ic);
948 while (camel_imapp_engine_iterate(istore->engine, ic) > 0)
950 if (ic->status->result != IMAP_OK)
951 camel_exception_throw(1, "fetch failed: %s", ic->status->text);
953 camel_imapp_engine_command_free(istore->engine, ic);
954 ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "UID FETCH ");
960 printf("The pending list should now be empty: %s\n", e_dlist_empty(&istore->pending_fetch_list)?"TRUE":"FALSE");
962 info = camel_folder_summary_index(((CamelFolder *)istore->selected)->summary, i);
964 printf("message info [%d] =\n", i);
965 camel_message_info_dump(info);
966 camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
971 camel_exception_throw_ex(e);
974 g_hash_table_destroy(istore->pending_fetch_table);
975 istore->pending_fetch_table = NULL;
976 e_memchunk_destroy(istore->pending_fetch_chunks);
978 camel_imapp_engine_command_free(istore->engine, ic);
983 /*char *uids[] = {"1", "2", "4", "5", "6", "7", "9", "11", "12", 0};*/
984 /*char *uids[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 0};*/
985 char *uids[] = {"1", "3", "5", "7", "9", "11", "12", "13", "14", "15", "20", "21", "24", "25", "26", 0};
988 uidset_test(CamelIMAPPEngine *ie)
990 struct _uidset_state ss;
991 CamelIMAPPCommand *ic;
994 /*ic = camel_imapp_engine_command_new(ie, 0, "FETCH", NULL, "FETCH ");*/
995 uidset_init(&ss, 0, 0);
996 for (i=0;uids[i];i++) {
997 if (uidset_add(&ss, uids[i])) {
998 printf("\n[%d] flushing uids\n", i);
1002 if (uidset_done(&ss)) {
1003 printf("\nflushing uids\n");