1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-imap-folder.c: Abstract class for an imap folder */
/*
* Authors: Jeffrey Stedfast <fejj@helixcode.com>
*
* Copyright (C) 2000 Helix Code, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <config.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <gal/util/e-util.h>
#include "camel-imap-folder.h"
#include "camel-imap-command.h"
#include "camel-imap-store.h"
#include "camel-imap-stream.h"
#include "camel-imap-summary.h"
#include "camel-imap-utils.h"
#include "string-utils.h"
#include "camel-stream.h"
#include "camel-stream-fs.h"
#include "camel-stream-mem.h"
#include "camel-stream-buffer.h"
#include "camel-data-wrapper.h"
#include "camel-mime-message.h"
#include "camel-stream-filter.h"
#include "camel-mime-filter-from.h"
#include "camel-mime-filter-crlf.h"
#include "camel-exception.h"
#include "camel-mime-utils.h"
#define d(x) x
#define CF_CLASS(o) (CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(o)))
static CamelFolderClass *parent_class = NULL;
static void imap_finalize (CamelObject *object);
static void imap_refresh_info (CamelFolder *folder, CamelException *ex);
static void imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex);
static void imap_expunge (CamelFolder *folder, CamelException *ex);
/* message counts */
static gint imap_get_message_count (CamelFolder *folder);
static gint imap_get_unread_message_count (CamelFolder *folder);
/* message manipulation */
static CamelMimeMessage *imap_get_message (CamelFolder *folder, const gchar *uid,
CamelException *ex);
static void imap_append_message (CamelFolder *folder, CamelMimeMessage *message,
const CamelMessageInfo *info, CamelException *ex);
static void imap_copy_message_to (CamelFolder *source, const char *uid,
CamelFolder *destination, CamelException *ex);
static void imap_move_message_to (CamelFolder *source, const char *uid,
CamelFolder *destination, CamelException *ex);
/* summary info */
static GPtrArray *imap_get_uids (CamelFolder *folder);
static GPtrArray *imap_get_summary (CamelFolder *folder);
static const CamelMessageInfo *imap_get_message_info (CamelFolder *folder, const char *uid);
static void imap_update_summary (CamelFolder *folder, int first, int last,
CamelException *ex);
/* searching */
static GPtrArray *imap_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex);
/* flag methods */
static guint32 imap_get_message_flags (CamelFolder *folder, const char *uid);
static void imap_set_message_flags (CamelFolder *folder, const char *uid, guint32 flags, guint32 set);
static gboolean imap_get_message_user_flag (CamelFolder *folder, const char *uid, const char *name);
static void imap_set_message_user_flag (CamelFolder *folder, const char *uid, const char *name,
gboolean value);
static void
camel_imap_folder_class_init (CamelImapFolderClass *camel_imap_folder_class)
{
CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_imap_folder_class);
parent_class = CAMEL_FOLDER_CLASS(camel_type_get_global_classfuncs (camel_folder_get_type ()));
/* virtual method definition */
/* virtual method overload */
camel_folder_class->refresh_info = imap_refresh_info;
camel_folder_class->sync = imap_sync;
camel_folder_class->expunge = imap_expunge;
camel_folder_class->get_uids = imap_get_uids;
camel_folder_class->free_uids = camel_folder_free_nop;
camel_folder_class->get_message_count = imap_get_message_count;
camel_folder_class->get_unread_message_count = imap_get_unread_message_count;
camel_folder_class->get_message = imap_get_message;
camel_folder_class->append_message = imap_append_message;
camel_folder_class->copy_message_to = imap_copy_message_to;
camel_folder_class->move_message_to = imap_move_message_to;
camel_folder_class->get_summary = imap_get_summary;
camel_folder_class->get_message_info = imap_get_message_info;
camel_folder_class->free_summary = camel_folder_free_nop;
camel_folder_class->search_by_expression = imap_search_by_expression;
camel_folder_class->get_message_flags = imap_get_message_flags;
camel_folder_class->set_message_flags = imap_set_message_flags;
camel_folder_class->get_message_user_flag = imap_get_message_user_flag;
camel_folder_class->set_message_user_flag = imap_set_message_user_flag;
}
static void
camel_imap_folder_init (gpointer object, gpointer klass)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (object);
CamelFolder *folder = CAMEL_FOLDER (object);
folder->has_summary_capability = TRUE;
folder->has_search_capability = TRUE;
imap_folder->summary = NULL;
}
CamelType
camel_imap_folder_get_type (void)
{
static CamelType camel_imap_folder_type = CAMEL_INVALID_TYPE;
if (camel_imap_folder_type == CAMEL_INVALID_TYPE) {
camel_imap_folder_type =
camel_type_register (CAMEL_FOLDER_TYPE, "CamelImapFolder",
sizeof (CamelImapFolder),
sizeof (CamelImapFolderClass),
(CamelObjectClassInitFunc) camel_imap_folder_class_init,
NULL,
(CamelObjectInitFunc) camel_imap_folder_init,
(CamelObjectFinalizeFunc) imap_finalize);
}
return camel_imap_folder_type;
}
CamelFolder *
camel_imap_folder_new (CamelStore *parent, const char *folder_name,
const char *summary_file, CamelException *ex)
{
CamelImapStore *imap_store = CAMEL_IMAP_STORE (parent);
CamelFolder *folder = CAMEL_FOLDER (camel_object_new (camel_imap_folder_get_type ()));
CamelImapFolder *imap_folder = (CamelImapFolder *)folder;
CamelImapResponse *response;
const char *dir_sep, *short_name, *resp;
guint32 validity = 0;
int i;
dir_sep = CAMEL_IMAP_STORE (parent)->dir_sep;
short_name = strrchr (folder_name, *dir_sep);
if (short_name)
short_name++;
else
short_name = folder_name;
camel_folder_construct (folder, parent, folder_name, short_name);
imap_folder->summary = camel_imap_summary_new (summary_file, validity);
if (!imap_folder->summary) {
camel_object_unref (CAMEL_OBJECT (folder));
camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
"Could not load summary for %s",
folder_name);
return NULL;
}
response = camel_imap_command (imap_store, folder, ex, NULL);
if (!response) {
camel_object_unref ((CamelObject *)folder);
return NULL;
}
for (i = 0; i < response->untagged->len; i++) {
resp = response->untagged->pdata[i] + 2;
if (!g_strncasecmp (resp, "FLAGS ", 6)) {
folder->permanent_flags =
imap_parse_flag_list (resp + 6);
} else if (!g_strncasecmp (resp, "OK [PERMANENTFLAGS ", 19)) {
folder->permanent_flags =
imap_parse_flag_list (resp + 19);
} else if (!g_strncasecmp (resp, "OK [UIDVALIDITY ", 16)) {
validity = strtoul (resp + 16, NULL, 10);
} else if (isdigit ((unsigned char)*resp)) {
unsigned long num = strtoul (resp, (char **)&resp, 10);
if (!g_strncasecmp (resp, " EXISTS", 7))
imap_folder->exists = num;
}
}
camel_imap_response_free (response);
imap_refresh_info (folder, ex);
if (camel_exception_is_set (ex)) {
camel_object_unref (CAMEL_OBJECT (folder));
return NULL;
}
return folder;
}
static void
imap_finalize (CamelObject *object)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (object);
camel_object_unref ((CamelObject *)imap_folder->summary);
}
static void
imap_refresh_info (CamelFolder *folder, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (folder->parent_store);
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
CamelImapResponse *response;
struct {
char *uid;
guint32 flags;
} *new;
char *resp, *uid, *p, *flags;
int i, seq, summary_len;
CamelMessageInfo *info;
if (imap_folder->exists == 0) {
camel_folder_summary_clear (imap_folder->summary);
return;
}
/* Get UIDs and flags of all messages. */
response = camel_imap_command (store, folder, ex,
"FETCH 1:%d (UID FLAGS)",
imap_folder->exists);
if (!response)
return;
new = g_malloc0 (imap_folder->exists * sizeof (*new));
for (i = 0; i < response->untagged->len; i++) {
resp = response->untagged->pdata[i];
seq = strtoul (resp + 2, &resp, 10);
if (g_strncasecmp (resp, " FETCH ", 7) != 0)
continue;
uid = e_strstrcase (resp, "UID ");
if (uid) {
uid += 4;
strtoul (uid, &p, 10);
new[seq - 1].uid = g_strndup (uid, p - uid);
}
flags = e_strstrcase (resp, "FLAGS ");
if (flags) {
flags += 6;
new[seq - 1].flags = imap_parse_flag_list (flags);
}
}
/* Theoretically, the UIDs could get arbitrarily reordered,
* but that won't normally happen. We assume that if we find a
* UID in the summary that doesn't correspond to the UID in
* the folder, that it means the message was deleted on the
* server, so we remove it from the summary.
*/
summary_len = camel_folder_summary_count (imap_folder->summary);
for (i = 0; i < summary_len && i < imap_folder->exists; i++) {
info = camel_folder_summary_index (imap_folder->summary, i);
/* Shouldn't happen, but... */
if (!new[i].uid)
continue;
if (strcmp (info->uid, new[i].uid) != 0) {
camel_folder_summary_remove (imap_folder->summary,
info);
i--;
summary_len--;
continue;
}
/* Update summary flags */
info->flags = new[i].flags;
g_free (new[i].uid);
}
if (i < imap_folder->exists) {
/* Fetch full summary for the remaining messages. */
imap_update_summary (folder, i + 1, imap_folder->exists, ex);
}
for (; i < imap_folder->exists; i++)
g_free (new[i].uid);
g_free (new);
}
static void
imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (folder->parent_store);
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
CamelImapResponse *response;
int i, max;
/* Set the flags on any messages that have changed this session */
max = camel_folder_summary_count (imap_folder->summary);
for (i = 0; i < max; i++) {
CamelMessageInfo *info;
info = camel_folder_summary_index (imap_folder->summary, i);
if (info->flags & CAMEL_MESSAGE_FOLDER_FLAGGED) {
char *flags;
flags = imap_create_flag_list (info->flags);
if (flags) {
response = camel_imap_command (
store, folder, ex,
"UID STORE %s FLAGS.SILENT %s",
info->uid, flags);
g_free (flags);
if (!response)
return;
camel_imap_response_free (response);
}
info->flags &= ~CAMEL_MESSAGE_FOLDER_FLAGGED;
}
}
if (expunge) {
response = camel_imap_command (store, folder, ex, "EXPUNGE");
camel_imap_response_free (response);
}
camel_folder_summary_save (imap_folder->summary);
}
static void
imap_expunge (CamelFolder *folder, CamelException *ex)
{
imap_sync (folder, TRUE, ex);
}
static gint
imap_get_message_count (CamelFolder *folder)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
return camel_folder_summary_count (imap_folder->summary);
}
static gint
imap_get_unread_message_count (CamelFolder *folder)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
CamelMessageInfo *info;
int i, max, count = 0;
max = camel_folder_summary_count (imap_folder->summary);
for (i = 0; i < max; i++) {
info = camel_folder_summary_index (imap_folder->summary, i);
if (!(info->flags & CAMEL_MESSAGE_SEEN))
count++;
}
return count;
}
static void
imap_append_message (CamelFolder *folder, CamelMimeMessage *message,
const CamelMessageInfo *info, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (folder->parent_store);
CamelImapResponse *response;
CamelStream *memstream;
CamelMimeFilter *crlf_filter;
CamelStreamFilter *streamfilter;
GByteArray *ba;
char *folder_path, *flagstr, *result;
folder_path = camel_imap_store_folder_path (store, folder->full_name);
/* create flag string param */
if (info && info->flags)
flagstr = imap_create_flag_list (info->flags);
else
flagstr = NULL;
/* FIXME: We could avoid this if we knew how big the message was. */
memstream = camel_stream_mem_new ();
ba = g_byte_array_new ();
camel_stream_mem_set_byte_array (CAMEL_STREAM_MEM (memstream), ba);
streamfilter = camel_stream_filter_new_with_stream (memstream);
crlf_filter = camel_mime_filter_crlf_new (
CAMEL_MIME_FILTER_CRLF_ENCODE,
CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
camel_stream_filter_add (streamfilter, crlf_filter);
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message),
CAMEL_STREAM (streamfilter));
camel_object_unref (CAMEL_OBJECT (streamfilter));
camel_object_unref (CAMEL_OBJECT (crlf_filter));
camel_object_unref (CAMEL_OBJECT (memstream));
response = camel_imap_command (store, NULL, ex, "APPEND %s%s%s {%d}",
folder_path, flagstr ? " " : "",
flagstr ? flagstr : "", ba->len);
g_free (folder_path);
g_free (flagstr);
if (!response) {
g_byte_array_free (ba, TRUE);
return;
}
result = camel_imap_response_extract_continuation (response, ex);
if (!result) {
g_byte_array_free (ba, TRUE);
return;
}
g_free (result);
/* send the rest of our data - the mime message */
g_byte_array_append (ba, "\0", 3);
response = camel_imap_command_continuation (store, ex, ba->data);
g_byte_array_free (ba, TRUE);
if (!response)
return;
camel_imap_response_free (response);
}
static void
imap_copy_message_to (CamelFolder *source, const char *uid,
CamelFolder *destination, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (source->parent_store);
CamelImapResponse *response;
char *folder_path;
folder_path = camel_imap_store_folder_path (store, destination->full_name);
response = camel_imap_command (store, source, ex, "UID COPY %s \"%s\"",
uid, folder_path);
camel_imap_response_free (response);
g_free (folder_path);
}
/* FIXME: Duplication of code! */
static void
imap_move_message_to (CamelFolder *source, const char *uid,
CamelFolder *destination, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (source->parent_store);
CamelImapResponse *response;
char *folder_path;
folder_path = camel_imap_store_folder_path (store, destination->full_name);
response = camel_imap_command (store, source, ex, "UID COPY %s \"%s\"",
uid, folder_path);
camel_imap_response_free (response);
g_free (folder_path);
if (camel_exception_is_set (ex))
return;
camel_folder_delete_message (source, uid);
}
static GPtrArray *
imap_get_uids (CamelFolder *folder)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
const CamelMessageInfo *info;
GPtrArray *array;
int i, count;
count = camel_folder_summary_count (imap_folder->summary);
array = g_ptr_array_new ();
g_ptr_array_set_size (array, count);
for (i = 0; i < count; i++) {
info = camel_folder_summary_index (imap_folder->summary, i);
array->pdata[i] = g_strdup (info->uid);
}
return array;
}
static CamelMimeMessage *
imap_get_message (CamelFolder *folder, const gchar *uid, CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (folder->parent_store);
CamelImapResponse *response;
CamelStream *msgstream;
CamelMimeMessage *msg;
char *result, *mesg, *p;
int len;
response = camel_imap_command (store, folder, ex,
"UID FETCH %s RFC822", uid);
if (!response)
return NULL;
result = camel_imap_response_extract (response, "FETCH", ex);
if (!result)
return NULL;
p = strstr (result, "RFC822");
if (p) {
p += 7;
mesg = imap_parse_nstring (&p, &len);
}
if (!p) {
camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
"Could not find message body in FETCH "
"response.");
g_free (result);
return NULL;
}
g_free (result);
msgstream = camel_stream_mem_new_with_buffer (mesg, len);
msg = camel_mime_message_new ();
camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg),
msgstream);
camel_object_unref (CAMEL_OBJECT (msgstream));
g_free (mesg);
return msg;
}
/**
* imap_protocol_get_summary_specifier
*
* Make a data item specifier for the header lines we need,
* appropriate to the server level.
*
* IMAP4rev1: UID FLAGS BODY.PEEK[HEADER.FIELDS (SUBJECT FROM .. IN-REPLY-TO)]
* IMAP4: UID FLAGS RFC822.HEADER.LINES (SUBJECT FROM .. IN-REPLY-TO)
**/
static char *
imap_protocol_get_summary_specifier (CamelImapStore *store)
{
char *sect_begin, *sect_end;
char *headers_wanted = "SUBJECT FROM TO CC DATE MESSAGE-ID REFERENCES IN-REPLY-TO";
if (store->server_level >= IMAP_LEVEL_IMAP4REV1) {
sect_begin = "BODY.PEEK[HEADER.FIELDS";
sect_end = "]";
} else {
sect_begin = "RFC822.HEADER.LINES";
sect_end = "";
}
return g_strdup_printf ("UID FLAGS %s (%s)%s", sect_begin,
headers_wanted, sect_end);
}
static void
imap_update_summary (CamelFolder *folder, int first, int last,
CamelException *ex)
{
CamelImapStore *store = CAMEL_IMAP_STORE (folder->parent_store);
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
CamelImapResponse *response;
GPtrArray *headers = NULL;
char *q, *summary_specifier;
struct _header_raw *h = NULL;
int i;
/* If the range we're updating overlaps with the range we already
* know about, then fetch just flags + uids first. If uids
* aren't "right", reorder them. Update flags appropriately.
* If that returned unknown UIDs, or we're updating unknown
* sequence numbers, do the full fetch for those.
*/
summary_specifier = imap_protocol_get_summary_specifier (store);
if (first == last) {
response = camel_imap_command (store, folder, ex,
"FETCH %d (%s)", first,
summary_specifier);
} else {
response = camel_imap_command (store, folder, ex,
"FETCH %d:%d (%s)", first,
last, summary_specifier);
}
g_free (summary_specifier);
if (!response)
return;
headers = response->untagged;
for (i = 0; i < headers->len; i++) {
CamelMessageInfo *info;
char *uid, *flags, *header;
/* Grab the UID... */
if (!(uid = strstr (headers->pdata[i], "UID "))) {
d(fprintf (stderr, "Cannot get a uid for %d\n\n%s\n\n", i+1, (char *) headers->pdata[i]));
break;
}
for (uid += 4; *uid && (*uid < '0' || *uid > '9'); uid++)
;
for (q = uid; *q && *q >= '0' && *q <= '9'; q++)
;
/* construct the header list */
/* fast-forward to beginning of header info... */
header = strchr (headers->pdata[i], '\n') + 1;
h = NULL;
do {
char *line;
int len;
len = strcspn (header, "\n");
while (header[len + 1] == ' ' ||
header[len + 1] == '\t')
len += 1 + strcspn (header + len + 1, "\n");
line = g_strndup (header, len);
header_raw_append_parse (&h, line, -1);
g_free (line);
header += len;
} while (*header++ == '\n' && *header != '\n');
/* We can't just call camel_folder_summary_add_from_parser
* because it will assign the wrong UID, and thus get the
* uid hash table wrong and all that. FIXME some day.
*/
info = ((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(imap_folder->summary)))->message_info_new(imap_folder->summary, h);
header_raw_clear (&h);
info->uid = g_strndup (uid, q - uid);
/* now lets grab the FLAGS */
if (!(flags = strstr (headers->pdata[i], "FLAGS "))) {
d(fprintf (stderr, "We didn't seem to get any flags for %d...\n", i));
} else {
for (flags += 6; *flags && *flags != '('; flags++)
;
info->flags = imap_parse_flag_list (flags);
}
camel_folder_summary_add (imap_folder->summary, info);
}
camel_imap_response_free (response);
}
static GPtrArray *
imap_get_summary (CamelFolder *folder)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
return imap_folder->summary->messages;
}
/* get a single message info, by uid */
static const CamelMessageInfo *
imap_get_message_info (CamelFolder *folder, const char *uid)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
return camel_folder_summary_uid (imap_folder->summary, uid);
}
static GPtrArray *
imap_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex)
{
CamelImapResponse *response;
GPtrArray *uids = NULL;
char *result, *sexp, *p;
d(fprintf (stderr, "camel sexp: '%s'\n", expression));
sexp = imap_translate_sexp (expression);
d(fprintf (stderr, "imap sexp: '%s'\n", sexp));
uids = g_ptr_array_new ();
if (!folder->has_search_capability) {
g_free (sexp);
return uids;
}
response = camel_imap_command (CAMEL_IMAP_STORE (folder->parent_store),
folder, NULL, "UID SEARCH %s", sexp);
g_free (sexp);
if (!response)
return uids;
result = camel_imap_response_extract (response, "SEARCH", NULL);
if (!result)
return uids;
if ((p = strstr (result, "* SEARCH"))) {
char *word;
word = imap_next_word (p); /* word now points to SEARCH */
for (word = imap_next_word (word); *word && *word != '*'; word = imap_next_word (word)) {
gboolean word_is_numeric = TRUE;
char *ep;
/* find the end of this word and make sure it's a numeric uid */
for (ep = word; *ep && *ep != ' ' && *ep != '\n'; ep++)
if (*ep < '0' || *ep > '9')
word_is_numeric = FALSE;
if (word_is_numeric)
g_ptr_array_add (uids, g_strndup (word, (gint)(ep - word)));
}
}
g_free (result);
return uids;
}
static guint32
imap_get_message_flags (CamelFolder *folder, const char *uid)
{
const CamelMessageInfo *info;
info = imap_get_message_info (folder, uid);
g_return_val_if_fail (info != NULL, 0);
return info->flags;
}
static void
imap_set_message_flags (CamelFolder *folder, const char *uid, guint32 flags, guint32 set)
{
CamelImapFolder *imap_folder = (CamelImapFolder *)folder;
CamelMessageInfo *info;
info = camel_folder_summary_uid (imap_folder->summary, uid);
g_return_if_fail (info != NULL);
if ((info->flags & set) == flags)
return;
info->flags = (info->flags & ~flags) | (set & flags) | CAMEL_MESSAGE_FOLDER_FLAGGED;
camel_folder_summary_touch (imap_folder->summary);
camel_object_trigger_event (CAMEL_OBJECT (folder), "message_changed",
(gpointer)uid);
}
static gboolean
imap_get_message_user_flag (CamelFolder *folder, const char *uid, const char *name)
{
/* FIXME */
return FALSE;
}
static void
imap_set_message_user_flag (CamelFolder *folder, const char *uid, const char *name, gboolean value)
{
/* FIXME */
camel_object_trigger_event (CAMEL_OBJECT (folder), "message_changed",
(gpointer)uid);
}
void
camel_imap_folder_changed (CamelFolder *folder, int exists,
GArray *expunged, CamelException *ex)
{
CamelImapFolder *imap_folder = CAMEL_IMAP_FOLDER (folder);
if (expunged) {
int i, id;
for (i = 0; i < expunged->len; i++) {
id = g_array_index (expunged, int, i);
d(fprintf (stderr, "Expunging message %d from the summary (i = %d)\n", id + i, i));
camel_folder_summary_remove_index (imap_folder->summary, id - 1);
}
}
if (exists > imap_folder->exists) {
int old = imap_folder->exists;
imap_folder->exists = exists;
imap_update_summary (folder, old + 1, exists, ex);
}
camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", NULL);
}
|