Initial import of mutt-ng.
[apps/madmutt.git] / imap / message.c
1 /*
2  * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3  * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
4  * 
5  *     This program is free software; you can redistribute it and/or modify
6  *     it under the terms of the GNU General Public License as published by
7  *     the Free Software Foundation; either version 2 of the License, or
8  *     (at your option) any later version.
9  * 
10  *     This program is distributed in the hope that it will be useful,
11  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *     GNU General Public License for more details.
14  * 
15  *     You should have received a copy of the GNU General Public License
16  *     along with this program; if not, write to the Free Software
17  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
18  */ 
19
20 /* message parsing/updating functions */
21
22 #include <stdlib.h>
23 #include <ctype.h>
24
25 #include "mutt.h"
26 #include "mutt_curses.h"
27 #include "imap_private.h"
28 #include "message.h"
29 #include "mx.h"
30
31 #ifdef HAVE_PGP
32 #include "pgp.h"
33 #endif
34
35 static void flush_buffer(char* buf, size_t* len, CONNECTION* conn);
36 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
37   FILE* fp);
38 static int msg_has_flag (LIST* flag_list, const char* flag);
39 static int msg_parse_fetch (IMAP_HEADER* h, char* s);
40 static char* msg_parse_flags (IMAP_HEADER* h, char* s);
41
42 /* imap_read_headers:
43  * Changed to read many headers instead of just one. It will return the
44  * msgno of the last message read. It will return a value other than
45  * msgend if mail comes in while downloading headers (in theory).
46  */
47 int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend)
48 {
49   CONTEXT* ctx;
50   char buf[LONG_STRING];
51   char hdrreq[STRING];
52   FILE *fp;
53   char tempfile[_POSIX_PATH_MAX];
54   int msgno;
55   IMAP_HEADER h;
56   int rc, mfhrc, oldmsgcount;
57   int fetchlast = 0;
58   const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO LINES X-LABEL";
59
60   ctx = idata->ctx;
61
62   if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
63   {
64     snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s)]", 
65       want_headers); 
66   } 
67   else if (mutt_bit_isset (idata->capabilities,IMAP4))
68   {
69     snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s)", 
70       want_headers);
71   }
72   else
73   {     /* Unable to fetch headers for lower versions */
74     mutt_error _("Unable to fetch headers from this IMAP server version.");
75     mutt_sleep (2);     /* pause a moment to let the user see the error */
76     return -1;
77   }
78
79   /* instead of downloading all headers and then parsing them, we parse them
80    * as they come in. */
81   mutt_mktemp (tempfile);
82   if (!(fp = safe_fopen (tempfile, "w+")))
83   {
84     mutt_error (_("Could not create temporary file %s"), tempfile);
85     mutt_sleep (2);
86     return -1;
87   }
88   unlink (tempfile);
89
90   /* make sure context has room to hold the mailbox */
91   while ((msgend) >= idata->ctx->hdrmax)
92     mx_alloc_memory (idata->ctx);
93
94   oldmsgcount = ctx->msgcount;
95   idata->reopen &= ~IMAP_NEWMAIL_PENDING;
96   idata->newMailCount = 0;
97
98   for (msgno = msgbegin; msgno <= msgend ; msgno++)
99   {
100     if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
101       mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
102         msgend + 1);
103
104     if (msgno + 1 > fetchlast)
105     {
106       /*
107        * Make one request for everything. This makes fetching headers an
108        * order of magnitude faster if you have a large mailbox.
109        *
110        * If we get more messages while doing this, we make another
111        * request for all the new messages.
112        */
113       snprintf (buf, sizeof (buf),
114         "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
115         msgend + 1, hdrreq);
116
117       imap_cmd_start (idata, buf);
118
119       fetchlast = msgend + 1;
120     }
121
122     /* freshen fp, h */
123     rewind (fp);
124     memset (&h, 0, sizeof (h));
125     h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
126
127     /* this DO loop does two things:
128      * 1. handles untagged messages, so we can try again on the same msg
129      * 2. fetches the tagged response at the end of the last message.
130      */
131     do
132     {
133       mfhrc = 0;
134
135       rc = imap_cmd_step (idata);
136       if (rc != IMAP_CMD_CONTINUE)
137         break;
138
139       if ((mfhrc = msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
140         continue;
141       else if (mfhrc < 0)
142         break;
143
144       /* make sure we don't get remnants from older larger message headers */
145       fputs ("\n\n", fp);
146
147       /* update context with message header */
148       ctx->hdrs[msgno] = mutt_new_header ();
149
150       ctx->hdrs[msgno]->index = h.sid - 1;
151       if (h.sid != ctx->msgcount + 1)
152         dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
153       /* messages which have not been expunged are ACTIVE (borrowed from mh 
154        * folders) */
155       ctx->hdrs[msgno]->active = 1;
156       ctx->hdrs[msgno]->read = h.read;
157       ctx->hdrs[msgno]->old = h.old;
158       ctx->hdrs[msgno]->deleted = h.deleted;
159       ctx->hdrs[msgno]->flagged = h.flagged;
160       ctx->hdrs[msgno]->replied = h.replied;
161       ctx->hdrs[msgno]->changed = h.changed;
162       ctx->hdrs[msgno]->received = h.received;
163       ctx->hdrs[msgno]->data = (void *) (h.data);
164
165       rewind (fp);
166       /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
167        *   on h.received being set */
168       ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
169         0, 0);
170       /* content built as a side-effect of mutt_read_rfc822_header */
171       ctx->hdrs[msgno]->content->length = h.content_length;
172
173       ctx->msgcount++;
174     }
175     while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
176       ((msgno + 1) >= fetchlast)));
177
178     if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
179     {
180       imap_free_header_data ((void**) &h.data);
181       fclose (fp);
182
183       return -1;
184     }
185         
186     /* in case we get new mail while fetching the headers */
187     if (idata->reopen & IMAP_NEWMAIL_PENDING)
188     {
189       msgend = idata->newMailCount - 1;
190       while ((msgend) >= ctx->hdrmax)
191         mx_alloc_memory (ctx);
192       idata->reopen &= ~IMAP_NEWMAIL_PENDING;
193       idata->newMailCount = 0;
194     }
195   }
196
197   fclose(fp);
198
199   if (ctx->msgcount > oldmsgcount)
200     mx_update_context (ctx, ctx->msgcount - oldmsgcount);
201
202   return msgend;
203 }
204
205 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
206 {
207   IMAP_DATA* idata;
208   HEADER* h;
209   char buf[LONG_STRING];
210   char path[_POSIX_PATH_MAX];
211   char *pc;
212   long bytes;
213   int uid;
214   int cacheno;
215   IMAP_CACHE *cache;
216   int read;
217   int rc;
218   /* Sam's weird courier server returns an OK response even when FETCH
219    * fails. Thanks Sam. */
220   short fetched = 0;
221
222   idata = (IMAP_DATA*) ctx->data;
223   h = ctx->hdrs[msgno];
224
225   /* see if we already have the message in our cache */
226   cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
227   cache = &idata->cache[cacheno];
228
229   if (cache->path)
230   {
231     if (cache->uid == HEADER_DATA(h)->uid)
232     {
233       /* yes, so just return a pointer to the message */
234       if (!(msg->fp = fopen (cache->path, "r")))
235       {
236         mutt_perror (cache->path);
237         return (-1);
238       }
239       return 0;
240     }
241     else
242     {
243       /* clear the previous entry */
244       unlink (cache->path);
245       FREE (&cache->path);
246     }
247   }
248
249   if (!isendwin())
250     mutt_message _("Fetching message...");
251
252   cache->uid = HEADER_DATA(h)->uid;
253   mutt_mktemp (path);
254   cache->path = safe_strdup (path);
255   if (!(msg->fp = safe_fopen (path, "w+")))
256   {
257     FREE (&cache->path);
258     return -1;
259   }
260
261   /* mark this header as currently inactive so the command handler won't
262    * also try to update it. HACK until all this code can be moved into the
263    * command handler */
264   h->active = 0;
265   
266   snprintf (buf, sizeof (buf), "UID FETCH %d %s", HEADER_DATA(h)->uid,
267             (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
268              (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
269              "RFC822"));
270
271   imap_cmd_start (idata, buf);
272   do
273   {
274     if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
275       break;
276
277     pc = idata->cmd.buf;
278     pc = imap_next_word (pc);
279     pc = imap_next_word (pc);
280
281     if (!ascii_strncasecmp ("FETCH", pc, 5))
282     {
283       while (*pc)
284       {
285         pc = imap_next_word (pc);
286         if (pc[0] == '(')
287           pc++;
288         if (ascii_strncasecmp ("UID", pc, 3) == 0)
289         {
290           pc = imap_next_word (pc);
291           uid = atoi (pc);
292           if (uid != HEADER_DATA(h)->uid)
293             mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
294         }
295         else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
296                  (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
297         {
298           pc = imap_next_word (pc);
299           if (imap_get_literal_count(pc, &bytes) < 0)
300           {
301             imap_error ("imap_fetch_message()", buf);
302             goto bail;
303           }
304           if (imap_read_literal (msg->fp, idata, bytes) < 0)
305             goto bail;
306           /* pick up trailing line */
307           if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
308             goto bail;
309           pc = idata->cmd.buf;
310
311           fetched = 1;
312         }
313         /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
314          * change (eg from \Unseen to \Seen).
315          * Uncommitted changes in mutt take precedence. If we decide to
316          * incrementally update flags later, this won't stop us syncing */
317         else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
318         {
319           if ((pc = imap_set_flags (idata, h, pc)) == NULL)
320             goto bail;
321         }
322       }
323     }
324   }
325   while (rc == IMAP_CMD_CONTINUE);
326
327   /* see comment before command start. */
328   h->active = 1;
329
330   if (rc != IMAP_CMD_OK)
331     goto bail;
332
333   if (!fetched || !imap_code (idata->cmd.buf))
334     goto bail;
335     
336   /* Update the header information.  Previously, we only downloaded a
337    * portion of the headers, those required for the main display.
338    */
339   rewind (msg->fp);
340   /* It may be that the Status header indicates a message is read, but the
341    * IMAP server doesn't know the message has been \Seen. So we capture
342    * the server's notion of 'read' and if it differs from the message info
343    * picked up in mutt_read_rfc822_header, we mark the message (and context
344    * changed). Another possiblity: ignore Status on IMAP?*/
345   read = h->read;
346   /* I hate do this here, since it's so low-level, but I'm not sure where
347    * I can abstract it. Problem: the id and subj hashes lose their keys when
348    * mutt_free_envelope gets called, but keep their spots in the hash. This
349    * confuses threading. Alternatively we could try to merge the new
350    * envelope into the old one. Also messy and lowlevel. */
351   if (ctx->id_hash && h->env->message_id)
352     hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
353   if (ctx->subj_hash && h->env->real_subj)
354     hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
355   mutt_free_envelope (&h->env);
356   h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
357   if (ctx->id_hash && h->env->message_id)
358     hash_insert (ctx->id_hash, h->env->message_id, h, 0);
359   if (ctx->subj_hash && h->env->real_subj)
360     hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
361
362   /* see above. We want the new status in h->read, so we unset it manually
363    * and let mutt_set_flag set it correctly, updating context. */
364   if (read != h->read)
365   {
366     h->read = read;
367     mutt_set_flag (ctx, h, M_NEW, read);
368   }
369
370   h->lines = 0;
371   fgets (buf, sizeof (buf), msg->fp);
372   while (!feof (msg->fp))
373   {
374     h->lines++;
375     fgets (buf, sizeof (buf), msg->fp);
376   }
377
378   h->content->length = ftell (msg->fp) - h->content->offset;
379
380   /* This needs to be done in case this is a multipart message */
381 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
382   h->security = crypt_query (h->content);
383 #endif
384
385   mutt_clear_error();
386   rewind (msg->fp);
387
388   return 0;
389
390 bail:
391   safe_fclose (&msg->fp);
392   if (cache->path)
393   {
394     unlink (cache->path);
395     FREE (&cache->path);
396   }
397
398   return -1;
399 }
400
401 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
402 {
403   IMAP_DATA* idata;
404   FILE *fp;
405   char buf[LONG_STRING];
406   char mbox[LONG_STRING];
407   char mailbox[LONG_STRING]; 
408   size_t len;
409   int c, last;
410   IMAP_MBOX mx;
411   int rc;
412
413   idata = (IMAP_DATA*) ctx->data;
414
415   if (imap_parse_path (ctx->path, &mx))
416     return -1;
417
418   imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
419   
420   if ((fp = fopen (msg->path, "r")) == NULL)
421   {
422     mutt_perror (msg->path);
423     goto fail;
424   }
425
426   /* currently we set the \Seen flag on all messages, but probably we
427    * should scan the message Status header for flag info. Since we're
428    * already rereading the whole file for length it isn't any more
429    * expensive (it'd be nice if we had the file size passed in already
430    * by the code that writes the file, but that's a lot of changes.
431    * Ideally we'd have a HEADER structure with flag info here... */
432   for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
433   {
434     if(c == '\n' && last != '\r')
435       len++;
436
437     len++;
438   }
439   rewind (fp);
440   
441   imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
442   snprintf (buf, sizeof (buf), "APPEND %s (\\Seen) {%d}", mbox, len);
443
444   imap_cmd_start (idata, buf);
445
446   do
447     rc = imap_cmd_step (idata);
448   while (rc == IMAP_CMD_CONTINUE);
449
450   if (rc != IMAP_CMD_RESPOND)
451   {
452     char *pc;
453
454     dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
455                 idata->cmd.buf));
456
457     pc = idata->cmd.buf + SEQLEN;
458     SKIPWS (pc);
459     pc = imap_next_word (pc);
460     mutt_error ("%s", pc);
461     mutt_sleep (1);
462     fclose (fp);
463     goto fail;
464   }
465
466   mutt_message _("Uploading message ...");
467
468   for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
469   {
470     if (c == '\n' && last != '\r')
471       buf[len++] = '\r';
472
473     buf[len++] = c;
474
475     if (len > sizeof(buf) - 3)
476       flush_buffer(buf, &len, idata->conn);
477   }
478   
479   if (len)
480     flush_buffer(buf, &len, idata->conn);
481
482   mutt_socket_write (idata->conn, "\r\n");
483   fclose (fp);
484
485   do
486     rc = imap_cmd_step (idata);
487   while (rc == IMAP_CMD_CONTINUE);
488
489   if (!imap_code (idata->cmd.buf))
490   {
491     char *pc;
492
493     dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
494                 idata->cmd.buf));
495     pc = idata->cmd.buf + SEQLEN;
496     SKIPWS (pc);
497     pc = imap_next_word (pc);
498     mutt_error ("%s", pc);
499     mutt_sleep (1);
500     goto fail;
501   }
502
503   FREE (&mx.mbox);
504   return 0;
505
506  fail:
507   FREE (&mx.mbox);
508   return -1;
509 }
510
511 /* imap_copy_messages: use server COPY command to copy messages to another
512  *   folder.
513  *   Return codes:
514  *      -1: error
515  *       0: success
516  *       1: non-fatal error - try fetch/append */
517 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
518 {
519   IMAP_DATA* idata;
520   BUFFER cmd;
521   char uid[11];
522   char mbox[LONG_STRING];
523   char mmbox[LONG_STRING];
524   int rc;
525   int n;
526   IMAP_MBOX mx;
527
528   idata = (IMAP_DATA*) ctx->data;
529
530   if (imap_parse_path (dest, &mx))
531   {
532     dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
533     return -1;
534   }
535
536   /* check that the save-to folder is in the same account */
537   if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
538   {
539     dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
540       dest, ctx->path));
541     return 1;
542   }
543
544   if (h && h->attach_del)
545   {
546     dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
547     return 1;
548   }
549   
550   imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
551
552   memset (&cmd, 0, sizeof (cmd));
553   mutt_buffer_addstr (&cmd, "UID COPY ");
554
555   /* Null HEADER* means copy tagged messages */
556   if (!h)
557   {
558     /* if any messages have attachments to delete, fall through to FETCH
559      * and APPEND. TODO: Copy what we can with COPY, fall through for the
560      * remainder. */
561     for (n = 0; n < ctx->msgcount; n++)
562     {
563       if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
564       {
565         dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
566         return 1;
567       }
568     }
569
570     rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
571     if (!rc)
572     {
573       dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
574       goto fail;
575     }
576     mutt_message (_("Copying %d messages to %s..."), rc, mbox);
577   }
578   else
579   {
580     mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
581     snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
582     mutt_buffer_addstr (&cmd, uid);
583   }
584
585   /* let's get it on */
586   mutt_buffer_addstr (&cmd, " ");
587   imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
588   mutt_buffer_addstr (&cmd, mmbox);
589
590   rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
591   if (rc == -2)
592   {
593     /* bail out if command failed for reasons other than nonexistent target */
594     if (ascii_strncasecmp (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11))
595     {
596       imap_error ("imap_copy_messages", idata->cmd.buf);
597       goto fail;
598     }
599     dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
600     snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
601     if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
602     {
603       mutt_clear_error ();
604       goto fail;
605     }
606     if (imap_create_mailbox (idata, mbox) < 0)
607       goto fail;
608
609     /* try again */
610     rc = imap_exec (idata, cmd.data, 0);
611   }
612   if (rc != 0)
613   {
614     imap_error ("imap_copy_messages", idata->cmd.buf);
615     goto fail;
616   }
617
618   /* cleanup */
619   if (delete)
620   {
621     if (!h)
622       for (n = 0; n < ctx->msgcount; n++)
623       {
624         if (ctx->hdrs[n]->tagged)
625         {
626           mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
627           mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
628           if (option (OPTDELETEUNTAG))
629             mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
630         }
631       }
632     else
633     {
634       mutt_set_flag (ctx, h, M_DELETE, 1);
635       mutt_set_flag (ctx, h, M_APPENDED, 1);
636       if (option (OPTDELETEUNTAG))
637         mutt_set_flag (ctx, h, M_TAG, 0);
638     }
639   }
640
641   if (cmd.data)
642     FREE (&cmd.data);
643   FREE (&mx.mbox);
644   return 0;
645
646  fail:
647   if (cmd.data)
648     FREE (&cmd.data);
649   FREE (&mx.mbox);
650   return -1;
651 }
652
653 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
654  *   appear in the folder flags list. Why wouldn't they? */
655 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
656 {
657   LIST *keywords;
658
659   if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
660     return;
661
662   keywords = HEADER_DATA(h)->keywords->next;
663
664   while (keywords)
665   {
666     if (msg_has_flag (mailbox_flags, keywords->data))
667     {
668       strncat (s, keywords->data, slen);
669       strncat (s, " ", slen);
670     }
671     keywords = keywords->next;
672   }
673 }
674
675 /* imap_free_header_data: free IMAP_HEADER structure */
676 void imap_free_header_data (void** data)
677 {
678   /* this should be safe even if the list wasn't used */
679   mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
680
681   FREE (data);
682 }
683
684 /* imap_set_flags: fill out the message header according to the flags from
685  *   the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
686 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
687 {
688   CONTEXT* ctx = idata->ctx;
689   IMAP_HEADER newh;
690   unsigned char readonly;
691
692   memset (&newh, 0, sizeof (newh));
693   newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
694
695   dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
696   if ((s = msg_parse_flags (&newh, s)) == NULL)
697   {
698     FREE (&newh.data);
699     return NULL;
700   }
701   
702   /* YAUH (yet another ugly hack): temporarily set context to
703    * read-write even if it's read-only, so *server* updates of
704    * flags can be processed by mutt_set_flag. ctx->changed must
705    * be restored afterwards */
706   readonly = ctx->readonly;
707   ctx->readonly = 0;
708             
709   mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
710   mutt_set_flag (ctx, h, M_OLD, newh.old);
711   mutt_set_flag (ctx, h, M_READ, newh.read);
712   mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
713   mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
714   mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
715
716   /* this message is now definitively *not* changed (mutt_set_flag
717    * marks things changed as a side-effect) */
718   h->changed = 0;
719   ctx->changed &= ~readonly;
720   ctx->readonly = readonly;
721
722   mutt_free_list (&(HEADER_DATA(h)->keywords));
723   HEADER_DATA(h)->keywords = newh.data->keywords;
724   FREE(&newh.data);
725
726   return s;
727 }
728
729 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
730  *   Expects string beginning with * n FETCH.
731  *   Returns:
732  *      0 on success
733  *     -1 if the string is not a fetch response
734  *     -2 if the string is a corrupt fetch response */
735 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
736 {
737   IMAP_DATA* idata;
738   long bytes;
739   int rc = -1; /* default now is that string isn't FETCH response*/
740
741   idata = (IMAP_DATA*) ctx->data;
742
743   if (buf[0] != '*')
744     return rc;
745   
746   /* skip to message number */
747   buf = imap_next_word (buf);
748   h->sid = atoi (buf);
749
750   /* find FETCH tag */
751   buf = imap_next_word (buf);
752   if (ascii_strncasecmp ("FETCH", buf, 5))
753     return rc;
754
755   rc = -2; /* we've got a FETCH response, for better or worse */
756   if (!(buf = strchr (buf, '(')))
757     return rc;
758   buf++;
759
760   /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
761    *   read header lines and call it again. Silly. */
762   if (msg_parse_fetch (h, buf) != -2)
763     return rc;
764   
765   if (imap_get_literal_count (buf, &bytes) < 0)
766     return rc;
767   imap_read_literal (fp, idata, bytes);
768
769   /* we may have other fields of the FETCH _after_ the literal
770    * (eg Domino puts FLAGS here). Nothing wrong with that, either.
771    * This all has to go - we should accept literals and nonliterals
772    * interchangeably at any time. */
773   if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
774     return -2;
775   
776   if (msg_parse_fetch (h, idata->cmd.buf) == -1)
777     return rc;
778
779   rc = 0; /* success */
780   
781   /* subtract headers from message size - unfortunately only the subset of
782    * headers we've requested. */
783   h->content_length -= bytes;
784
785   return rc;
786 }
787
788 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
789  *   return 1 if found or flag list has '\*', 0 otherwise */
790 static int msg_has_flag (LIST* flag_list, const char* flag)
791 {
792   if (!flag_list)
793     return 0;
794
795   flag_list = flag_list->next;
796   while (flag_list)
797   {
798     if (!ascii_strncasecmp (flag_list->data, flag, strlen (flag_list->data)))
799       return 1;
800
801     flag_list = flag_list->next;
802   }
803
804   return 0;
805 }
806
807 /* msg_parse_fetch: handle headers returned from header fetch */
808 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
809 {
810   char tmp[SHORT_STRING];
811   char *ptmp;
812
813   if (!s)
814     return -1;
815
816   while (*s)
817   {
818     SKIPWS (s);
819
820     if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
821     {
822       if ((s = msg_parse_flags (h, s)) == NULL)
823         return -1;
824     }
825     else if (ascii_strncasecmp ("UID", s, 3) == 0)
826     {
827       s += 3;
828       SKIPWS (s);
829       h->data->uid = (unsigned int) atoi (s);
830
831       s = imap_next_word (s);
832     }
833     else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
834     {
835       s += 12;
836       SKIPWS (s);
837       if (*s != '\"')
838       {
839         dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
840         return -1;
841       }
842       s++;
843       ptmp = tmp;
844       while (*s && *s != '\"')
845         *ptmp++ = *s++;
846       if (*s != '\"')
847         return -1;
848       s++; /* skip past the trailing " */
849       *ptmp = 0;
850       h->received = imap_parse_date (tmp);
851     }
852     else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
853     {
854       s += 11;
855       SKIPWS (s);
856       ptmp = tmp;
857       while (isdigit ((unsigned char) *s))
858         *ptmp++ = *s++;
859       *ptmp = 0;
860       h->content_length = atoi (tmp);
861     }
862     else if (!ascii_strncasecmp ("BODY", s, 4) ||
863       !ascii_strncasecmp ("RFC822.HEADER", s, 13))
864     {
865       /* handle above, in msg_fetch_header */
866       return -2;
867     }
868     else if (*s == ')')
869       s++; /* end of request */
870     else if (*s)
871     {
872       /* got something i don't understand */
873       imap_error ("msg_parse_fetch", s);
874       return -1;
875     }
876   }
877
878   return 0;
879 }
880
881 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
882 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
883 {
884   int recent = 0;
885
886   /* sanity-check string */
887   if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
888   {
889     dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
890       s));
891     return NULL;
892   }
893   s += 5;
894   SKIPWS(s);
895   if (*s != '(')
896   {
897     dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
898       s));
899     return NULL;
900   }
901   s++;
902
903   /* start parsing */
904   while (*s && *s != ')')
905   {
906     if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
907     {
908       s += 8;
909       h->deleted = 1;
910     }
911     else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
912     {
913       s += 8;
914       h->flagged = 1;
915     }
916     else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
917     {
918       s += 9;
919       h->replied = 1;
920     }
921     else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
922     {
923       s += 5;
924       h->read = 1;
925     }
926     else if (ascii_strncasecmp ("\\recent", s, 5) == 0)
927     {
928       s += 7;
929       recent = 1;
930     }
931     else
932     {
933       /* store custom flags as well */
934       char ctmp;
935       char* flag_word = s;
936
937       if (!h->data->keywords)
938         h->data->keywords = mutt_new_list ();
939
940       while (*s && !ISSPACE (*s) && *s != ')')
941         s++;
942       ctmp = *s;
943       *s = '\0';
944       mutt_add_list (h->data->keywords, flag_word);
945       *s = ctmp;
946     }
947     SKIPWS(s);
948   }
949
950   /* wrap up, or note bad flags response */
951   if (*s == ')')
952   {
953     /* if a message is neither seen nor recent, it is OLD. */
954     if (option (OPTMARKOLD) && !recent && !(h->read))
955       h->old = 1;
956     s++;
957   }
958   else
959   {
960     dprint (1, (debugfile,
961       "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
962     return NULL;
963   }
964
965   return s;
966 }
967
968 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
969 {
970   buf[*len] = '\0';
971   mutt_socket_write(conn, buf);
972   *len = 0;
973 }