1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
2 /* camel-imap-folder.c: Abstract class for an imap folder */
5 * Authors: Jeffrey Stedfast <fejj@helixcode.com>
7 * Copyright (C) 2000 Helix Code, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
37 #include <e-util/e-util.h>
39 #include "camel-imap-folder.h"
40 #include "camel-imap-store.h"
41 #include "camel-imap-stream.h"
42 #include "camel-imap-utils.h"
43 #include "string-utils.h"
44 #include "camel-stream.h"
45 #include "camel-stream-fs.h"
46 #include "camel-stream-mem.h"
47 #include "camel-stream-buffer.h"
48 #include "camel-data-wrapper.h"
49 #include "camel-mime-message.h"
50 #include "camel-stream-filter.h"
51 #include "camel-mime-filter-from.h"
52 #include "camel-mime-filter-crlf.h"
53 #include "camel-exception.h"
54 #include "camel-mime-utils.h"
58 #define CF_CLASS(o) (CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(o)))
60 static CamelFolderClass *parent_class = NULL;
62 static void imap_init (CamelFolder *folder, CamelStore *parent_store,
63 CamelFolder *parent_folder, const gchar *name,
64 gchar *separator, gboolean path_begns_with_sep,
66 static void imap_finalize (CamelObject *object);
67 static void imap_refresh_info (CamelFolder *folder, CamelException *ex);
68 static void imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex);
69 static void imap_expunge (CamelFolder *folder, CamelException *ex);
72 static gint imap_get_message_count_internal (CamelFolder *folder, CamelException *ex);
73 static gint imap_get_message_count (CamelFolder *folder);
74 static gint imap_get_unread_message_count (CamelFolder *folder);
76 /* message manipulation */
77 static CamelMimeMessage *imap_get_message (CamelFolder *folder, const gchar *uid,
79 static void imap_append_message (CamelFolder *folder, CamelMimeMessage *message,
80 const CamelMessageInfo *info, CamelException *ex);
81 static void imap_copy_message_to (CamelFolder *source, const char *uid,
82 CamelFolder *destination, CamelException *ex);
83 static void imap_move_message_to (CamelFolder *source, const char *uid,
84 CamelFolder *destination, CamelException *ex);
86 /* subfolder listing */
87 static GPtrArray *imap_get_subfolder_names_internal (CamelFolder *folder, CamelException *ex);
88 static GPtrArray *imap_get_subfolder_names (CamelFolder *folder);
91 static GPtrArray *imap_get_uids (CamelFolder *folder);
92 static GPtrArray *imap_get_summary_internal (CamelFolder *folder, CamelException *ex);
93 static GPtrArray *imap_get_summary (CamelFolder *folder);
94 static const CamelMessageInfo *imap_get_message_info (CamelFolder *folder, const char *uid);
97 static GPtrArray *imap_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex);
100 /*static guint32 imap_get_permanent_flags (CamelFolder *folder, CamelException *ex);*/
101 static guint32 imap_get_message_flags (CamelFolder *folder, const char *uid);
102 static void imap_set_message_flags (CamelFolder *folder, const char *uid, guint32 flags, guint32 set);
103 static gboolean imap_get_message_user_flag (CamelFolder *folder, const char *uid, const char *name);
104 static void imap_set_message_user_flag (CamelFolder *folder, const char *uid, const char *name,
109 camel_imap_folder_class_init (CamelImapFolderClass *camel_imap_folder_class)
111 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_imap_folder_class);
113 parent_class = CAMEL_FOLDER_CLASS(camel_type_get_global_classfuncs (camel_folder_get_type ()));
115 /* virtual method definition */
117 /* virtual method overload */
118 camel_folder_class->init = imap_init;
119 camel_folder_class->refresh_info = imap_refresh_info;
120 camel_folder_class->sync = imap_sync;
121 camel_folder_class->expunge = imap_expunge;
123 camel_folder_class->get_uids = imap_get_uids;
124 camel_folder_class->free_uids = camel_folder_free_nop;
125 camel_folder_class->get_subfolder_names = imap_get_subfolder_names;
126 camel_folder_class->free_subfolder_names = camel_folder_free_nop;
128 camel_folder_class->get_message_count = imap_get_message_count;
129 camel_folder_class->get_unread_message_count = imap_get_unread_message_count;
130 camel_folder_class->get_message = imap_get_message;
131 camel_folder_class->append_message = imap_append_message;
132 camel_folder_class->copy_message_to = imap_copy_message_to;
133 camel_folder_class->move_message_to = imap_move_message_to;
135 camel_folder_class->get_summary = imap_get_summary;
136 camel_folder_class->get_message_info = imap_get_message_info;
137 camel_folder_class->free_summary = camel_folder_free_nop;
139 camel_folder_class->search_by_expression = imap_search_by_expression;
141 /*camel_folder_class->get_permanent_flags = imap_get_permanent_flags;*/
142 camel_folder_class->get_message_flags = imap_get_message_flags;
143 camel_folder_class->set_message_flags = imap_set_message_flags;
144 camel_folder_class->get_message_user_flag = imap_get_message_user_flag;
145 camel_folder_class->set_message_user_flag = imap_set_message_user_flag;
149 camel_imap_folder_init (gpointer object, gpointer klass)
151 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (object);
152 CamelFolder *folder = CAMEL_FOLDER (object);
154 folder->can_hold_messages = TRUE;
155 folder->can_hold_folders = TRUE;
156 folder->has_summary_capability = TRUE;
157 folder->has_search_capability = TRUE;
159 imap_folder->summary = NULL;
160 imap_folder->summary_hash = NULL;
161 imap_folder->lsub = NULL;
165 camel_imap_folder_get_type (void)
167 static CamelType camel_imap_folder_type = CAMEL_INVALID_TYPE;
169 if (camel_imap_folder_type == CAMEL_INVALID_TYPE) {
170 camel_imap_folder_type =
171 camel_type_register (CAMEL_FOLDER_TYPE, "CamelImapFolder",
172 sizeof (CamelImapFolder),
173 sizeof (CamelImapFolderClass),
174 (CamelObjectClassInitFunc) camel_imap_folder_class_init,
176 (CamelObjectInitFunc) camel_imap_folder_init,
177 (CamelObjectFinalizeFunc) imap_finalize);
180 return camel_imap_folder_type;
184 camel_imap_folder_new (CamelStore *parent, char *folder_name, CamelException *ex)
186 CamelFolder *folder = CAMEL_FOLDER (camel_object_new (camel_imap_folder_get_type ()));
187 CamelURL *url = CAMEL_SERVICE (parent)->url;
190 dir_sep = CAMEL_IMAP_STORE (parent)->dir_sep;
192 CF_CLASS (folder)->init (folder, parent, NULL, folder_name, dir_sep, FALSE, ex);
194 if (camel_exception_is_set (ex)) {
195 camel_object_unref (CAMEL_OBJECT (folder));
199 if (!strcmp (folder_name, url->path + 1))
200 folder->can_hold_messages = FALSE;
202 /*CF_CLASS (folder)->refresh_info (folder, ex);*/
204 if (camel_exception_is_set (ex)) {
205 camel_object_unref (CAMEL_OBJECT (folder));
213 imap_summary_free (GPtrArray **summary)
215 CamelMessageInfo *info;
216 GPtrArray *array = *summary;
221 for (i = 0; i < max; i++) {
222 info = g_ptr_array_index (array, i);
223 g_free (info->subject);
228 g_free (info->message_id);
229 header_references_list_clear (&info->references);
234 g_ptr_array_free (array, TRUE);
240 imap_folder_summary_free (CamelImapFolder *imap_folder)
242 if (imap_folder->summary_hash) {
243 g_hash_table_destroy (imap_folder->summary_hash);
244 imap_folder->summary_hash = NULL;
247 imap_summary_free (&imap_folder->summary);
251 imap_finalize (CamelObject *object)
253 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (object);
256 imap_folder_summary_free (imap_folder);
258 if (imap_folder->lsub) {
259 max = imap_folder->lsub->len;
261 for (i = 0; i < max; i++) {
262 g_free (imap_folder->lsub->pdata[i]);
263 imap_folder->lsub->pdata[i] = NULL;
266 g_ptr_array_free (imap_folder->lsub, TRUE);
271 imap_init (CamelFolder *folder, CamelStore *parent_store, CamelFolder *parent_folder,
272 const gchar *name, gchar *separator, gboolean path_begins_with_sep, CamelException *ex)
274 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
276 /* call parent method */
277 parent_class->init (folder, parent_store, parent_folder, name, separator, path_begins_with_sep, ex);
278 if (camel_exception_get_id (ex))
281 /* we assume that the parent init
282 method checks for the existance of @folder */
283 folder->can_hold_messages = TRUE;
284 folder->can_hold_folders = TRUE;
285 folder->has_summary_capability = TRUE;
286 folder->has_search_capability = TRUE;
288 /* some IMAP daemons support user-flags *
289 * I would not, however, rely on this feature as *
290 * most IMAP daemons do not support all the features */
291 folder->permanent_flags = CAMEL_MESSAGE_SEEN |
292 CAMEL_MESSAGE_ANSWERED |
293 CAMEL_MESSAGE_FLAGGED |
294 CAMEL_MESSAGE_DELETED |
295 CAMEL_MESSAGE_DRAFT |
298 imap_folder->search = NULL;
299 imap_folder->summary = NULL;
300 imap_folder->summary_hash = NULL;
301 imap_folder->lsub = NULL;
305 imap_refresh_info (CamelFolder *folder, CamelException *ex)
307 imap_get_subfolder_names_internal (folder, ex);
309 if (folder->can_hold_messages)
310 imap_get_summary_internal (folder, ex);
314 imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
316 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
320 imap_expunge (folder, ex);
324 /* Set the flags on any messages that have changed this session */
325 if (imap_folder->summary) {
326 max = imap_folder->summary->len;
327 for (i = 0; i < max; i++) {
328 CamelMessageInfo *info;
330 info = (CamelMessageInfo *) g_ptr_array_index (imap_folder->summary, i);
331 if (info->flags & CAMEL_MESSAGE_FOLDER_FLAGGED) {
334 flags = g_strconcat (info->flags & CAMEL_MESSAGE_SEEN ? "\\Seen " : "",
335 info->flags & CAMEL_MESSAGE_DRAFT ? "\\Draft " : "",
336 info->flags & CAMEL_MESSAGE_DELETED ? "\\Deleted " : "",
337 info->flags & CAMEL_MESSAGE_ANSWERED ? "\\Answered " : "",
343 *(flags + strlen (flags) - 1) = '\0';
344 s = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store),
346 "UID STORE %s FLAGS.SILENT (%s)",
349 if (s != CAMEL_IMAP_OK)
361 imap_expunge (CamelFolder *folder, CamelException *ex)
363 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
367 imap_sync (folder, FALSE, ex);
369 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), folder,
370 &result, ex, "EXPUNGE");
375 imap_get_message_count_internal (CamelFolder *folder, CamelException *ex)
377 CamelStore *store = CAMEL_STORE (folder->parent_store);
378 CamelURL *url = CAMEL_SERVICE (store)->url;
379 gchar *result, *msg_count, *folder_path, *dir_sep;
380 gint status, count = 0;
382 g_return_val_if_fail (folder != NULL, 0);
383 g_return_val_if_fail (folder->can_hold_messages, 0);
385 dir_sep = CAMEL_IMAP_STORE (folder->parent_store)->dir_sep;
387 if (url && url->path && *(url->path + 1) && strcmp (folder->full_name, "INBOX"))
388 folder_path = g_strdup_printf ("%s%s%s", url->path + 1, dir_sep, folder->full_name);
390 folder_path = g_strdup (folder->full_name);
392 if (CAMEL_IMAP_STORE (store)->has_status_capability)
393 status = camel_imap_command_extended (CAMEL_IMAP_STORE (store), folder,
394 &result, ex, "STATUS %s (MESSAGES)", folder_path);
396 status = camel_imap_command_extended (CAMEL_IMAP_STORE (store), folder,
397 &result, ex, "EXAMINE %s", folder_path);
399 g_free (folder_path);
401 if (status != CAMEL_IMAP_OK)
404 /* parse out the message count */
405 if (result && *result == '*') {
406 if (CAMEL_IMAP_STORE (store)->has_status_capability) {
407 /* should come in the form: "* STATUS <folder> (MESSAGES <count>)" */
408 if ((msg_count = strstr (result, "MESSAGES")) != NULL) {
409 msg_count = imap_next_word (msg_count);
411 /* we should now be pointing to the message count */
412 count = atoi (msg_count);
415 /* should come in the form: "* <count> EXISTS" */
416 if ((msg_count = strstr (result, "EXISTS")) != NULL) {
417 for ( ; msg_count > result && *msg_count != '*'; msg_count--);
419 msg_count = imap_next_word (msg_count);
421 /* we should now be pointing to the message count */
422 count = atoi (msg_count);
432 imap_get_message_count (CamelFolder *folder)
434 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
436 if (imap_folder->summary)
437 return imap_folder->summary->len;
443 imap_get_unread_message_count (CamelFolder *folder)
445 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
446 CamelMessageInfo *info;
450 g_return_val_if_fail (folder != NULL, 0);
452 /* If we don't have a message count, return 0 */
453 if (!imap_folder->summary)
456 infolist = imap_folder->summary;
458 for (i = 0; i < infolist->len; i++) {
459 info = (CamelMessageInfo *) g_ptr_array_index (infolist, i);
460 if (!(info->flags & CAMEL_MESSAGE_SEEN))
468 imap_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, CamelException *ex)
470 CamelStore *store = CAMEL_STORE (folder->parent_store);
471 CamelURL *url = CAMEL_SERVICE (store)->url;
472 CamelStream *memstream;
474 gchar *result, *cmdid, *dir_sep;
475 gchar *folder_path, *flagstr = NULL;
478 g_return_if_fail (folder != NULL);
479 g_return_if_fail (message != NULL);
481 dir_sep = CAMEL_IMAP_STORE (folder->parent_store)->dir_sep;
483 if (url && url->path && *(url->path + 1) && strcmp (folder->full_name, "INBOX"))
484 folder_path = g_strdup_printf ("%s%s%s", url->path + 1, dir_sep, folder->full_name);
486 folder_path = g_strdup (folder->full_name);
488 /* create flag string param */
489 if (info && info->flags) {
490 flagstr = g_strconcat (" (", info->flags & CAMEL_MESSAGE_SEEN ? "\\Seen " : "",
491 info->flags & CAMEL_MESSAGE_DRAFT ? "\\Draft " : "",
492 info->flags & CAMEL_MESSAGE_DELETED ? "\\Answered " : "",
495 *(flagstr + strlen (flagstr) - 1) = ')';
498 ba = g_byte_array_new ();
499 memstream = camel_stream_mem_new_with_byte_array (ba);
500 /* FIXME: we need to crlf/dot filter */
501 camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), memstream);
502 camel_stream_write_string (memstream, "\r\n");
503 camel_stream_reset (memstream);
505 status = camel_imap_command_preliminary (CAMEL_IMAP_STORE (folder->parent_store),
506 &cmdid, ex, "APPEND %s%s {%d}",
507 folder_path, flagstr ? flagstr : "", ba->len - 2);
508 g_free (folder_path);
510 if (status != CAMEL_IMAP_PLUS) {
515 /* send the rest of our data - the mime message */
516 status = camel_imap_command_continuation_with_stream (CAMEL_IMAP_STORE (folder->parent_store),
517 &result, cmdid, memstream, ex);
520 if (status != CAMEL_IMAP_OK)
525 camel_object_unref (CAMEL_OBJECT (memstream));
526 camel_imap_folder_changed (folder, 1, NULL, ex);
530 imap_copy_message_to (CamelFolder *source, const char *uid, CamelFolder *destination, CamelException *ex)
532 CamelStore *store = CAMEL_STORE (source->parent_store);
533 CamelURL *url = CAMEL_SERVICE (store)->url;
534 char *result, *folder_path, *dir_sep;
537 dir_sep = CAMEL_IMAP_STORE (source->parent_store)->dir_sep;
539 if (url && url->path && *(url->path + 1) && strcmp (destination->full_name, "INBOX"))
540 folder_path = g_strdup_printf ("%s%s%s", url->path + 1, dir_sep, destination->full_name);
542 folder_path = g_strdup (destination->full_name);
544 status = camel_imap_command_extended (CAMEL_IMAP_STORE (store), source, &result, ex,
545 "UID COPY %s %s", uid, folder_path);
546 g_free (folder_path);
548 if (status != CAMEL_IMAP_OK)
553 camel_imap_folder_changed (destination, 1, NULL, ex);
556 /* FIXME: Duplication of code! */
558 imap_move_message_to (CamelFolder *source, const char *uid, CamelFolder *destination, CamelException *ex)
560 CamelStore *store = CAMEL_STORE (source->parent_store);
561 CamelURL *url = CAMEL_SERVICE (store)->url;
562 CamelMessageInfo *info;
563 char *result, *folder_path, *dir_sep;
566 dir_sep = CAMEL_IMAP_STORE (source->parent_store)->dir_sep;
568 if (url && url->path && *(url->path + 1) && strcmp (destination->full_name, "INBOX"))
569 folder_path = g_strdup_printf ("%s%s%s", url->path + 1, dir_sep, destination->full_name);
571 folder_path = g_strdup (destination->full_name);
573 status = camel_imap_command_extended (CAMEL_IMAP_STORE (store), source, &result, ex,
574 "UID COPY %s %s", uid, folder_path);
575 g_free (folder_path);
577 if (status != CAMEL_IMAP_OK)
582 if (!(info = (CamelMessageInfo *)imap_get_message_info (source, uid))) {
583 CamelService *service = CAMEL_SERVICE (store);
585 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
586 "Could not set flags for message %s on IMAP server %s: %s",
587 uid, service->url->host, "Unknown error");
591 imap_set_message_flags (source, uid, CAMEL_MESSAGE_DELETED, ~(info->flags));
593 camel_imap_folder_changed (destination, 1, NULL, ex);
597 imap_get_uids (CamelFolder *folder)
599 CamelMessageInfo *info;
600 GPtrArray *array, *infolist;
603 infolist = imap_get_summary (folder);
605 count = infolist ? infolist->len : 0;
607 array = g_ptr_array_new ();
608 g_ptr_array_set_size (array, count);
610 for (i = 0; i < count; i++) {
611 info = (CamelMessageInfo *) g_ptr_array_index (infolist, i);
612 array->pdata[i] = g_strdup (info->uid);
619 imap_get_subfolder_names_internal (CamelFolder *folder, CamelException *ex)
621 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
622 CamelStore *store = CAMEL_STORE (folder->parent_store);
623 CamelURL *url = CAMEL_SERVICE (store)->url;
625 gboolean found_inbox = FALSE;
627 gchar *result, *namespace, *dir_sep;
629 g_return_val_if_fail (folder != NULL, g_ptr_array_new ());
631 dir_sep = CAMEL_IMAP_STORE (folder->parent_store)->dir_sep;
633 if (url && url->path) {
634 char *path = url->path + 1;
637 if (!strcmp (folder->full_name, path))
638 namespace = g_strdup (path);
639 else if (!g_strcasecmp (folder->full_name, "INBOX"))
640 namespace = g_strdup (path); /* FIXME: erm...not sure */
642 namespace = g_strdup_printf ("%s%s%s", path, dir_sep, folder->full_name);
644 if (!strcmp (folder->full_name, "/"))
645 namespace = g_strdup ("");
647 namespace = g_strdup (folder->full_name);
650 namespace = g_strdup (folder->full_name);
653 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), NULL,
654 &result, ex, "LIST \"\" \"%s%s*\"", namespace,
655 *namespace ? dir_sep : "");
657 if (status != CAMEL_IMAP_OK) {
660 imap_folder->lsub = g_ptr_array_new ();
661 return imap_folder->lsub;
664 /* parse out the subfolders */
665 listing = g_ptr_array_new ();
669 while (ptr && *ptr == '*') {
670 gchar *flags, *sep, *dir, *buf, *end;
672 for (end = ptr; *end && *end != '\n'; end++);
673 buf = g_strndup (ptr, (gint)(end - ptr));
676 if (!imap_parse_list_response (buf, namespace, &flags, &sep, &dir)) {
692 d(fprintf (stderr, "adding folder: %s\n", dir));
693 if (!g_strcasecmp (dir, "INBOX"))
695 g_ptr_array_add (listing, dir);
705 if (!strcmp (folder->name, namespace) && !found_inbox) {
706 g_ptr_array_add (listing, g_strdup ("INBOX"));
712 imap_folder->lsub = listing;
718 imap_get_subfolder_names (CamelFolder *folder)
720 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
722 return imap_folder->lsub;
725 static CamelMimeMessage *
726 imap_get_message (CamelFolder *folder, const gchar *uid, CamelException *ex)
728 CamelStream *msgstream = NULL;
729 CamelMimeMessage *msg = NULL;
730 gchar *result, *header, *body, *mesg, *p, *q, *data_item;
731 int status, part_len;
733 if (CAMEL_IMAP_STORE (folder->parent_store)->server_level >= IMAP_LEVEL_IMAP4REV1)
734 data_item = "BODY.PEEK[HEADER]";
736 data_item = "RFC822.HEADER";
738 status = camel_imap_fetch_command (CAMEL_IMAP_STORE (folder->parent_store), folder,
739 &result, ex, "UID FETCH %s %s", uid,
742 if (!result || status != CAMEL_IMAP_OK)
745 /* parse out the message part */
746 for (p = result; *p && *p != '{' && *p != '"' && *p != '\n'; p++);
749 /* a quoted string - section 4.3 */
751 for (q = p; *q && *q != '"' && *q != '\n'; q++);
752 part_len = (gint) (q - p);
756 /* a literal string - section 4.3 */
757 part_len = atoi (p + 1);
758 for ( ; *p && *p != '\n'; p++);
764 /* calculate the new part-length */
765 for (q = p; *q && (q - p) <= part_len; q++) {
770 /* FIXME: This is a hack for IMAP daemons that send us a UID at the end of each FETCH */
771 for ( ; q > p && *(q-1) != '\n'; q--, part_len--);
781 header = g_strndup (p, part_len);
784 d(fprintf (stderr, "*** We got the header ***\n"));
786 if (CAMEL_IMAP_STORE (folder->parent_store)->server_level >= IMAP_LEVEL_IMAP4REV1)
787 data_item = "BODY[TEXT]";
789 data_item = "RFC822.TEXT";
791 status = camel_imap_fetch_command (CAMEL_IMAP_STORE (folder->parent_store), folder,
792 &result, ex, "UID FETCH %s %s", uid,
795 if (!result || status != CAMEL_IMAP_OK) {
800 /* parse out the message part */
801 for (p = result; *p && *p != '{' && *p != '"' && *p != '\n'; p++);
804 /* a quoted string - section 4.3 */
806 for (q = p; *q && *q != '"' && *q != '\n'; q++);
807 part_len = (gint) (q - p);
811 /* a literal string - section 4.3 */
812 part_len = atoi (p + 1);
813 for ( ; *p && *p != '\n'; p++);
820 /* calculate the new part-length */
821 for (q = p; *q && (q - p) <= part_len; q++) {
826 /* FIXME: This is a hack for IMAP daemons that send us a UID at the end of each FETCH */
827 for ( ; q > p && *(q-1) != '\n'; q--, part_len--);
838 body = g_strndup (p, part_len);
841 d(fprintf (stderr, "*** We got the body ***\n"));
843 mesg = g_strdup_printf ("%s\n%s", header, body);
846 d(fprintf (stderr, "*** We got the mesg ***\n"));
848 d(fprintf (stderr, "Message:\n%s\n", mesg));
850 msgstream = camel_stream_mem_new_with_buffer (mesg, strlen (mesg) + 1);
852 f_stream = camel_stream_filter_new_with_stream (msgstream);
853 filter = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS);
854 id = camel_stream_filter_add (f_stream, CAMEL_MIME_FILTER (filter));
856 msg = camel_mime_message_new ();
857 d(fprintf (stderr, "*** We created the camel_mime_message ***\n"));
859 camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg), msgstream);
861 camel_stream_filter_remove (f_stream, id);
862 camel_stream_close (CAMEL_STREAM (f_stream));
864 camel_object_unref (CAMEL_OBJECT (msgstream));
865 /*camel_object_unref (CAMEL_OBJECT (f_stream));*/
867 d(fprintf (stderr, "*** We're returning... ***\n"));
873 /* This probably shouldn't go here...but it will for now */
875 get_header_field (gchar *header, gchar *field)
877 gchar *part, *index, *p, *q;
879 index = (char *) e_strstrcase (header, field);
883 p = index + strlen (field) + 1;
885 if (*q == '\n' && (*(q + 1) != ' ' && *(q + 1) != '\t'))
888 part = g_strndup (p, (gint)(q - p));
890 /* it may be wrapped on multiple lines, so lets strip out \n's */
891 for (p = part; *p; ) {
893 memmove (p, p + 1, strlen (p));
901 static char *header_fields[] = { "subject", "from", "to", "cc", "date",
902 "received", "message-id", "references",
905 * imap_protocol_get_summary_specifier
907 * Make a data item specifier for the header lines we need,
908 * appropriate to the server level.
910 * IMAP4rev1: UID FLAGS BODY.PEEK[HEADER.FIELDS (SUBJECT FROM .. IN-REPLY-TO)]
911 * IMAP4: UID FLAGS RFC822.HEADER.LINES (SUBJECT FROM .. IN-REPLY-TO)
914 imap_protocol_get_summary_specifier (CamelFolder *folder)
916 char *sect_begin, *sect_end;
917 char *headers_wanted = "SUBJECT FROM TO CC DATE MESSAGE-ID REFERENCES IN-REPLY-TO";
919 if (CAMEL_IMAP_STORE (folder->parent_store)->server_level >= IMAP_LEVEL_IMAP4REV1) {
920 sect_begin = "BODY.PEEK[HEADER.FIELDS";
923 sect_begin = "RFC822.HEADER.LINES";
927 return g_strdup_printf ("UID FLAGS %s (%s)%s", sect_begin, headers_wanted, sect_end);
931 imap_get_summary_internal (CamelFolder *folder, CamelException *ex)
933 /* This ALWAYS updates the summary except on fail */
934 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
935 GPtrArray *summary = NULL, *headers = NULL;
936 GHashTable *hash = NULL;
937 gint num, i, j, status = 0;
938 char *result, *q, *node;
939 const char *received;
940 char *summary_specifier;
941 struct _header_raw *h = NULL, *tail = NULL;
943 num = imap_get_message_count_internal (folder, ex);
945 /* sync any previously set/changed message flags */
946 imap_sync (folder, FALSE, ex);
949 /* clean up any previous summary data */
950 imap_folder_summary_free (imap_folder);
952 imap_folder->summary = g_ptr_array_new ();
953 imap_folder->summary_hash = g_hash_table_new (g_str_hash, g_str_equal);
955 return imap_folder->summary;
958 summary_specifier = imap_protocol_get_summary_specifier (folder);
960 /* We use camel_imap_command_extended here because it's safe */
962 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), folder,
963 &result, ex, "FETCH 1 (%s)", summary_specifier);
965 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), folder,
966 &result, ex, "FETCH 1:%d (%s)", num, summary_specifier);
968 g_free (summary_specifier);
970 if (status != CAMEL_IMAP_OK) {
971 if (!imap_folder->summary) {
972 imap_folder->summary = g_ptr_array_new ();
973 imap_folder->summary_hash = g_hash_table_new (g_str_hash, g_str_equal);
976 return imap_folder->summary;
979 /* initialize our new summary-to-be */
980 summary = g_ptr_array_new ();
981 hash = g_hash_table_new (g_str_hash, g_str_equal);
983 /* create our array of headers from the server response */
984 headers = g_ptr_array_new ();
986 for (i = 1; node; i++) {
989 if ((end = strstr (node + 2, "\n*"))) {
990 g_ptr_array_add (headers, g_strndup (node, (gint)(end - node)));
992 g_ptr_array_add (headers, g_strdup (node));
997 d(fprintf (stderr, "IMAP server didn't respond with as many headers as we expected...\n"));
998 /* should we error?? */
1004 for (i = 0; i < headers->len; i++) {
1005 CamelMessageInfo *info;
1006 char *uid, *flags, *header;
1008 info = g_malloc0 (sizeof (CamelMessageInfo));
1010 /* lets grab the UID... */
1011 if (!(uid = strstr (headers->pdata[i], "UID "))) {
1012 d(fprintf (stderr, "Cannot get a uid for %d\n\n%s\n\n", i+1, (char *) headers->pdata[i]));
1017 for (uid += 4; *uid && (*uid < '0' || *uid > '9'); uid++); /* advance to <uid> */
1018 for (q = uid; *q && *q >= '0' && *q <= '9'; q++); /* find the end of the <uid> */
1019 info->uid = g_strndup (uid, (gint)(q - uid));
1020 /*d(fprintf (stderr, "*** info->uid = %s\n", info->uid));*/
1022 /* now lets grab the FLAGS */
1023 if (!(flags = strstr (headers->pdata[i], "FLAGS "))) {
1024 d(fprintf (stderr, "We didn't seem to get any flags for %d...\n", i));
1030 for (flags += 6; *flags && *flags != '('; flags++); /* advance to <flags> */
1031 for (q = flags; *q && *q != ')'; q++); /* find the end of <flags> */
1032 flags = g_strndup (flags, (gint)(q - flags + 1));
1033 /*d(fprintf (stderr, "*** info->flags = %s\n", flags));*/
1035 /* now we gotta parse for the flags */
1037 if (strstr (flags, "\\Seen"))
1038 info->flags |= CAMEL_MESSAGE_SEEN;
1039 if (strstr (flags, "\\Answered"))
1040 info->flags |= CAMEL_MESSAGE_ANSWERED;
1041 if (strstr (flags, "\\Flagged"))
1042 info->flags |= CAMEL_MESSAGE_FLAGGED;
1043 if (strstr (flags, "\\Deleted"))
1044 info->flags |= CAMEL_MESSAGE_DELETED;
1045 if (strstr (flags, "\\Draft"))
1046 info->flags |= CAMEL_MESSAGE_DRAFT;
1050 /* construct the header list */
1051 /* fast-forward to beginning of header info... */
1052 for (header = headers->pdata[i]; *header && *header != '\n'; header++);
1054 for (j = 0; *header_fields[j]; j++) {
1055 struct _header_raw *raw;
1056 char *field, *value;
1058 field = g_strdup_printf ("\n%s:", header_fields[j]);
1059 value = get_header_field (header, field);
1064 raw = g_malloc0 (sizeof (struct _header_raw));
1066 raw->name = g_strdup (header_fields[j]);
1079 /* construct the CamelMessageInfo */
1080 info->subject = camel_summary_format_string (h, "subject");
1081 info->from = camel_summary_format_address (h, "from");
1082 info->to = camel_summary_format_address (h, "to");
1083 info->cc = camel_summary_format_address (h, "cc");
1084 info->user_flags = NULL;
1085 info->date_sent = header_decode_date (header_raw_find (&h, "date", NULL), NULL);
1086 received = header_raw_find (&h, "received", NULL);
1088 received = strrchr (received, ';');
1090 info->date_received = header_decode_date (received + 1, NULL);
1092 info->date_received = 0;
1093 info->message_id = header_msgid_decode (header_raw_find (&h, "message-id", NULL));
1094 /* if we have a references, use that, otherwise, see if we have an in-reply-to
1095 header, with parsable content, otherwise *shrug* */
1096 info->references = header_references_decode (header_raw_find (&h, "references", NULL));
1097 if (info->references == NULL)
1098 info->references = header_references_decode (header_raw_find (&h, "in-reply-to", NULL));
1101 struct _header_raw *next = h->next;
1109 g_ptr_array_add (summary, info);
1110 g_hash_table_insert (hash, info->uid, info);
1113 for (i = 0; i < headers->len; i++)
1114 g_free (headers->pdata[i]);
1115 g_ptr_array_free (headers, TRUE);
1117 /* clean up any previous summary data */
1118 imap_folder_summary_free (imap_folder);
1120 imap_folder->summary = summary;
1121 imap_folder->summary_hash = hash;
1123 return imap_folder->summary;
1127 imap_get_summary (CamelFolder *folder)
1129 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
1131 return imap_folder->summary;
1134 /* get a single message info from the server */
1135 static CamelMessageInfo *
1136 imap_get_message_info_internal (CamelFolder *folder, guint id, CamelException *ex)
1138 CamelMessageInfo *info = NULL;
1139 struct _header_raw *h, *tail = NULL;
1140 const char *received;
1141 char *result, *uid, *flags, *header, *q;
1142 char *summary_specifier;
1145 /* we don't have a cached copy, so fetch it */
1146 summary_specifier = imap_protocol_get_summary_specifier (folder);
1148 /* again, we use camel_imap_command_extended here because it's safe to do so */
1149 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), folder,
1150 &result, ex, "FETCH %d (%s)", id, summary_specifier);
1152 g_free (summary_specifier);
1154 if (status != CAMEL_IMAP_OK)
1157 /* lets grab the UID... */
1158 if (!(uid = (char *) e_strstrcase (result, "UID "))) {
1159 d(fprintf (stderr, "Cannot get a uid for %d\n\n%s\n\n", id, result));
1164 for (uid += 4; *uid && (*uid < '0' || *uid > '9'); uid++); /* advance to <uid> */
1165 for (q = uid; *q && *q >= '0' && *q <= '9'; q++); /* find the end of the <uid> */
1166 uid = g_strndup (uid, (gint)(q - uid));
1168 info = g_malloc0 (sizeof (CamelMessageInfo));
1170 d(fprintf (stderr, "*** info->uid = %s\n", info->uid));
1172 /* now lets grab the FLAGS */
1173 if (!(flags = strstr (q, "FLAGS "))) {
1174 d(fprintf (stderr, "We didn't seem to get any flags for %s...\n", uid));
1181 for (flags += 6; *flags && *flags != '('; flags++); /* advance to <flags> */
1182 for (q = flags; *q && *q != ')'; q++); /* find the end of <flags> */
1183 flags = g_strndup (flags, (gint)(q - flags + 1));
1184 d(fprintf (stderr, "*** info->flags = %s\n", flags));
1186 /* now we gotta parse for the flags */
1188 if (strstr (flags, "\\Seen"))
1189 info->flags |= CAMEL_MESSAGE_SEEN;
1190 if (strstr (flags, "\\Answered"))
1191 info->flags |= CAMEL_MESSAGE_ANSWERED;
1192 if (strstr (flags, "\\Flagged"))
1193 info->flags |= CAMEL_MESSAGE_FLAGGED;
1194 if (strstr (flags, "\\Deleted"))
1195 info->flags |= CAMEL_MESSAGE_DELETED;
1196 if (strstr (flags, "\\Draft"))
1197 info->flags |= CAMEL_MESSAGE_DRAFT;
1201 /* construct the header list */
1202 /* fast-forward to beginning of header info... */
1203 for (header = q; *header && *header != '\n'; header++);
1205 for (j = 0; *header_fields[j]; j++) {
1206 struct _header_raw *raw;
1207 char *field, *value;
1209 field = g_strdup_printf ("\n%s:", header_fields[j]);
1210 value = get_header_field (header, field);
1215 raw = g_malloc0 (sizeof (struct _header_raw));
1217 raw->name = g_strdup (header_fields[j]);
1230 /* construct the CamelMessageInfo */
1231 info->subject = camel_summary_format_string (h, "subject");
1232 info->from = camel_summary_format_address (h, "from");
1233 info->to = camel_summary_format_address (h, "to");
1234 info->cc = camel_summary_format_address (h, "cc");
1235 info->user_flags = NULL;
1236 info->date_sent = header_decode_date (header_raw_find (&h, "date", NULL), NULL);
1237 received = header_raw_find (&h, "received", NULL);
1239 received = strrchr (received, ';');
1241 info->date_received = header_decode_date (received + 1, NULL);
1243 info->date_received = 0;
1244 info->message_id = header_msgid_decode (header_raw_find (&h, "message-id", NULL));
1245 /* if we have a references, use that, otherwise, see if we have an in-reply-to
1246 header, with parsable content, otherwise *shrug* */
1247 info->references = header_references_decode (header_raw_find (&h, "references", NULL));
1248 if (info->references == NULL)
1249 info->references = header_references_decode (header_raw_find (&h, "in-reply-to", NULL));
1252 struct _header_raw *next = h->next;
1265 /* get a single message info, by uid */
1266 static const CamelMessageInfo *
1267 imap_get_message_info (CamelFolder *folder, const char *uid)
1269 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
1271 g_return_val_if_fail (*uid != '\0', NULL);
1273 if (imap_folder->summary)
1274 return (CamelMessageInfo *) g_hash_table_lookup (imap_folder->summary_hash, uid);
1280 imap_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex)
1282 GPtrArray *uids = NULL;
1283 char *result, *sexp, *p;
1286 d(fprintf (stderr, "camel sexp: '%s'\n", expression));
1287 sexp = imap_translate_sexp (expression);
1288 d(fprintf (stderr, "imap sexp: '%s'\n", sexp));
1290 uids = g_ptr_array_new ();
1292 if (!folder->has_search_capability) {
1297 status = camel_imap_command_extended (CAMEL_IMAP_STORE (folder->parent_store), folder,
1298 &result, ex, "UID SEARCH %s", sexp);
1300 if (status != CAMEL_IMAP_OK) {
1305 if ((p = strstr (result, "* SEARCH"))) {
1308 word = imap_next_word (p); /* word now points to SEARCH */
1310 for (word = imap_next_word (word); *word && *word != '*'; word = imap_next_word (word)) {
1311 gboolean word_is_numeric = TRUE;
1314 /* find the end of this word and make sure it's a numeric uid */
1315 for (ep = word; *ep && *ep != ' ' && *ep != '\n'; ep++)
1316 if (*ep < '0' || *ep > '9')
1317 word_is_numeric = FALSE;
1319 if (word_is_numeric)
1320 g_ptr_array_add (uids, g_strndup (word, (gint)(ep - word)));
1332 imap_get_permanent_flags (CamelFolder *folder, CamelException *ex)
1334 /* return permamnant flags */
1335 return folder->permanent_flags;
1340 imap_get_message_flags (CamelFolder *folder, const char *uid)
1342 const CamelMessageInfo *info;
1344 info = imap_get_message_info (folder, uid);
1345 g_return_val_if_fail (info != NULL, 0);
1351 imap_set_message_flags (CamelFolder *folder, const char *uid, guint32 flags, guint32 set)
1353 CamelMessageInfo *info;
1355 info = (CamelMessageInfo*)imap_get_message_info (folder, uid);
1356 g_return_if_fail (info != NULL);
1358 info->flags = (info->flags & ~flags) | (set & flags) | CAMEL_MESSAGE_FOLDER_FLAGGED;
1360 /*gtk_signal_emit_by_name (GTK_OBJECT (folder), "message_changed", uid);*/
1361 camel_object_trigger_event (CAMEL_OBJECT (folder), "message_changed", (gpointer *) uid);
1365 imap_get_message_user_flag (CamelFolder *folder, const char *uid, const char *name)
1371 imap_set_message_user_flag (CamelFolder *folder, const char *uid, const char *name, gboolean value)
1373 /*gtk_signal_emit_by_name (GTK_OBJECT (folder), "message_changed", uid);*/
1374 camel_object_trigger_event (CAMEL_OBJECT (folder), "message_changed", (gpointer *) uid);
1378 camel_imap_folder_changed (CamelFolder *folder, gint recent, GPtrArray *expunged, CamelException *ex)
1380 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
1385 for (i = 0; i < expunged->len; i++) {
1386 id = atoi (expunged->pdata[i]);
1387 d(fprintf (stderr, "Expunging message %d from the summary (i = %d)\n", id + i, i));
1389 if (id <= imap_folder->summary->len) {
1390 CamelMessageInfo *info;
1392 info = (CamelMessageInfo *) imap_folder->summary->pdata[id - 1];
1394 /* remove from the lookup table and summary */
1395 g_hash_table_remove (imap_folder->summary_hash, info->uid);
1396 g_ptr_array_remove_index (imap_folder->summary, id - 1);
1398 /* free the info data */
1399 g_free (info->subject);
1400 g_free (info->from);
1404 g_free (info->message_id);
1405 header_references_list_clear (&info->references);
1409 /* Hopefully this should never happen */
1410 d(fprintf (stderr, "imap expunge-error: message %d is out of range\n", id));
1416 CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
1417 CamelMessageInfo *info;
1418 gint i, j, last, slast;
1420 if (!imap_folder->summary) {
1421 imap_folder->summary = g_ptr_array_new ();
1422 imap_folder->summary_hash = g_hash_table_new (g_str_hash, g_str_equal);
1425 last = imap_folder->summary->len + 1;
1426 slast = imap_get_message_count_internal (folder, ex);
1427 fprintf (stderr, "calculated next message is: %d\n", last);
1428 fprintf (stderr, "server says %d mesgs total\n", slast);
1429 slast -= (recent - 1);
1430 fprintf (stderr, "based on total, new guess is: %d\n", slast);
1432 for (i = slast, j = 0; j < recent; i++, j++) {
1433 info = imap_get_message_info_internal (folder, i, ex);
1435 if (!imap_get_message_info (folder, info->uid)) {
1436 /* add to our summary */
1437 g_ptr_array_add (imap_folder->summary, info);
1438 g_hash_table_insert (imap_folder->summary_hash, info->uid, info);
1440 /* we already have a record of it */
1441 g_free (info->subject);
1442 g_free (info->from);
1446 g_free (info->message_id);
1447 header_references_list_clear (&info->references);
1450 d(fprintf (stderr, "we already had message %d!!\n", i));
1453 /* our hack failed so now we need to do it the old fashioned way */
1454 /*imap_get_summary_internal (folder, ex);*/
1455 d(fprintf (stderr, "*** we tried to get message %d but failed\n", i));
1461 /*gtk_signal_emit_by_name (GTK_OBJECT (folder), "folder_changed", 0);*/
1462 camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", GINT_TO_POINTER (0));