2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 /* command.c: routines for sending commands to an IMAP server and parsing
20 #include "imap_private.h"
27 #define IMAP_CMD_BUFSIZE 512
29 /* forward declarations */
30 static void cmd_handle_fatal (IMAP_DATA * idata);
31 static int cmd_handle_untagged (IMAP_DATA * idata);
32 static void cmd_make_sequence (IMAP_DATA * idata);
33 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
34 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
35 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
36 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
38 static char *Capabilities[] = {
53 /* imap_cmd_start: Given an IMAP command, send it to the server.
54 * Currently a minor convenience, but helps to route all IMAP commands
55 * through a single interface. */
56 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
62 if (idata->status == IMAP_FATAL) {
63 cmd_handle_fatal (idata);
67 cmd_make_sequence (idata);
68 /* seq, space, cmd, \r\n\0 */
69 outlen = mutt_strlen (idata->cmd.seq) + mutt_strlen (cmd) + 4;
70 out = (char *) safe_malloc (outlen);
71 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
73 rc = mutt_socket_write (idata->conn, out);
77 return (rc < 0) ? IMAP_CMD_BAD : 0;
80 /* imap_cmd_step: Reads server responses from an IMAP command, detects
81 * tagged completion response, handles untagged messages, can read
82 * arbitrarily large strings (using malloc, so don't make it _too_
84 int imap_cmd_step (IMAP_DATA * idata)
86 IMAP_COMMAND *cmd = &idata->cmd;
90 if (idata->status == IMAP_FATAL) {
91 cmd_handle_fatal (idata);
95 /* read into buffer, expanding buffer as necessary until we have a full
98 if (len == cmd->blen) {
99 safe_realloc (&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
100 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
101 dprint (3, (debugfile, "imap_cmd_step: grew buffer to %u bytes\n",
105 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
108 (debugfile, "imap_cmd_step: Error reading server response.\n"));
109 /* cmd_handle_fatal (idata); */
115 /* if we've read all the way to the end of the buffer, we haven't read a
116 * full line (mutt_socket_readln strips the \r, so we always have at least
117 * one character free when we've read a full line) */
118 while (len == cmd->blen);
120 /* don't let one large string make cmd->buf hog memory forever */
121 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
122 safe_realloc (&cmd->buf, IMAP_CMD_BUFSIZE);
123 cmd->blen = IMAP_CMD_BUFSIZE;
125 (debugfile, "imap_cmd_step: shrank buffer to %u bytes\n",
129 idata->lastread = time (NULL);
131 /* handle untagged messages. The caller still gets its shot afterwards. */
132 if (!ascii_strncmp (cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
135 /* server demands a continuation response from us */
136 if (!ascii_strncmp (cmd->buf, "+ ", 2)) {
137 return IMAP_CMD_RESPOND;
140 /* tagged completion code */
141 if (!ascii_strncmp (cmd->buf, cmd->seq, SEQLEN)) {
142 imap_cmd_finish (idata);
143 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
146 return IMAP_CMD_CONTINUE;
149 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
150 int imap_code (const char *s)
154 return (ascii_strncasecmp ("OK", s, 2) == 0);
157 /* imap_exec: execute a command, and wait for the response from the server.
158 * Also, handle untagged responses.
160 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
161 * for checking for a mailbox on append and login
162 * IMAP_CMD_PASS: command contains a password. Suppress logging.
163 * Return 0 on success, -1 on Failure, -2 on OK Failure
165 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
171 if (idata->status == IMAP_FATAL) {
172 cmd_handle_fatal (idata);
176 /* create sequence for command */
177 cmd_make_sequence (idata);
178 /* seq, space, cmd, \r\n\0 */
179 outlen = mutt_strlen (idata->cmd.seq) + mutt_strlen (cmd) + 4;
180 out = (char *) safe_malloc (outlen);
181 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
183 rc = mutt_socket_write_d (idata->conn, out,
184 flags & IMAP_CMD_PASS ? IMAP_LOG_PASS :
189 cmd_handle_fatal (idata);
194 rc = imap_cmd_step (idata);
195 while (rc == IMAP_CMD_CONTINUE);
197 if (rc == IMAP_CMD_BAD) {
198 if (imap_reconnect (idata->ctx) != 0) {
204 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
207 if (rc != IMAP_CMD_OK) {
208 if (flags & IMAP_CMD_FAIL_OK)
212 (debugfile, "imap_exec: command failed: %s\n", idata->cmd.buf));
219 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
220 int imap_cmd_running (IMAP_DATA * idata)
222 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
223 idata->cmd.state == IMAP_CMD_RESPOND)
229 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
230 * detected, do expunge). Called automatically by imap_cmd_step, but
231 * may be called at any time. Called by imap_check_mailbox just before
232 * the index is refreshed, for instance. */
233 void imap_cmd_finish (IMAP_DATA * idata)
235 if (idata->status == IMAP_FATAL) {
236 cmd_handle_fatal (idata);
240 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
243 if (idata->reopen & IMAP_REOPEN_ALLOW) {
244 int count = idata->newMailCount;
246 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
247 (idata->reopen & IMAP_NEWMAIL_PENDING)
248 && count > idata->ctx->msgcount) {
249 /* read new mail messages */
250 dprint (2, (debugfile, "imap_cmd_finish: Fetching new mail\n"));
251 /* check_status: curs_main uses imap_check_mailbox to detect
252 * whether the index needs updating */
253 idata->check_status = IMAP_NEWMAIL_PENDING;
254 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
256 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
257 dprint (2, (debugfile, "imap_cmd_finish: Expunging mailbox\n"));
258 imap_expunge_mailbox (idata);
259 /* Detect whether we've gotten unexpected EXPUNGE messages */
260 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
261 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
262 idata->check_status = IMAP_EXPUNGE_PENDING;
263 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
264 IMAP_EXPUNGE_EXPECTED);
271 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
272 static void cmd_handle_fatal (IMAP_DATA * idata)
274 idata->status = IMAP_FATAL;
276 if ((idata->state == IMAP_SELECTED) &&
277 (idata->reopen & IMAP_REOPEN_ALLOW) && !idata->ctx->closing) {
278 /*mx_fastclose_mailbox (idata->ctx); */
279 mutt_error (_("Mailbox closed"));
281 idata->state = IMAP_DISCONNECTED;
282 if (imap_reconnect (idata->ctx) != 0)
283 mx_fastclose_mailbox (idata->ctx);
286 if (idata->state != IMAP_SELECTED) {
287 idata->state = IMAP_DISCONNECTED;
292 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
293 static int cmd_handle_untagged (IMAP_DATA * idata)
299 s = imap_next_word (idata->cmd.buf);
301 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
303 s = imap_next_word (s);
305 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
306 * connection, so update that one.
308 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
309 dprint (2, (debugfile, "Handling EXISTS\n"));
311 /* new mail arrived */
314 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
315 count < idata->ctx->msgcount) {
316 /* something is wrong because the server reported fewer messages
317 * than we previously saw
319 mutt_error _("Fatal error. Message count is out of sync!");
321 idata->status = IMAP_FATAL;
324 /* at least the InterChange server sends EXISTS messages freely,
325 * even when there is no new mail */
326 else if (count == idata->ctx->msgcount)
327 dprint (3, (debugfile,
328 "cmd_handle_untagged: superfluous EXISTS message.\n"));
330 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
331 dprint (2, (debugfile,
332 "cmd_handle_untagged: New mail in %s - %d messages total.\n",
333 idata->mailbox, count));
334 idata->reopen |= IMAP_NEWMAIL_PENDING;
336 idata->newMailCount = count;
339 /* pn vs. s: need initial seqno */
340 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
341 cmd_parse_expunge (idata, pn);
342 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
343 cmd_parse_fetch (idata, pn);
345 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
346 cmd_parse_capabilities (idata, s);
347 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
348 cmd_parse_myrights (idata, s);
349 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
350 dprint (2, (debugfile, "Handling BYE\n"));
352 /* check if we're logging out */
353 if (idata->status == IMAP_BYE)
356 /* server shut down our connection */
359 mutt_error ("%s", s);
360 idata->status = IMAP_BYE;
362 /*if (imap_reconnect(idata->ctx)!=0) {
363 if (idata->state == IMAP_SELECTED)
364 mx_fastclose_mailbox (idata->ctx); *//* XXX memleak? */
365 mutt_socket_close (idata->conn);
366 idata->state = IMAP_DISCONNECTED;
372 else if (option (OPTIMAPSERVERNOISE)
373 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
374 dprint (2, (debugfile, "Handling untagged NO\n"));
376 /* Display the warning message from the server */
377 mutt_error ("%s", s + 3);
384 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
385 static void cmd_make_sequence (IMAP_DATA * idata)
387 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
389 if (idata->seqno > 9999)
393 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
395 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
399 dprint (2, (debugfile, "Handling CAPABILITY\n"));
401 s = imap_next_word (s);
402 FREE (&idata->capstr);
403 idata->capstr = safe_strdup (s);
405 memset (idata->capabilities, 0, sizeof (idata->capabilities));
408 for (x = 0; x < CAPMAX; x++)
409 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
410 mutt_bit_set (idata->capabilities, x);
413 s = imap_next_word (s);
417 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
418 * be reopened at our earliest convenience */
419 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
424 dprint (2, (debugfile, "Handling EXPUNGE\n"));
428 /* walk headers, zero seqno of expunged message, decrement seqno of those
429 * above. Possibly we could avoid walking the whole list by resorting
430 * and guessing a good starting point, but I'm guessing the resort would
431 * nullify the gains */
432 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
433 h = idata->ctx->hdrs[cur];
435 if (h->index + 1 == expno)
437 else if (h->index + 1 > expno)
441 idata->reopen |= IMAP_EXPUNGE_PENDING;
444 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
445 * handles unanticipated FETCH responses, and only FLAGS data. We get
446 * these if another client has changed flags for a mailbox we've selected.
447 * Of course, a lot of code here duplicates code in message.c. */
448 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
453 dprint (2, (debugfile, "Handling FETCH\n"));
457 if (msgno <= idata->ctx->msgcount)
458 /* see cmd_parse_expunge */
459 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
460 h = idata->ctx->hdrs[cur];
462 if (h->active && h->index + 1 == msgno) {
464 (debugfile, "Message UID %d updated\n",
465 HEADER_DATA (h)->uid));
473 dprint (1, (debugfile, "FETCH response ignored for this message\n"));
478 s = imap_next_word (s);
479 s = imap_next_word (s);
482 dprint (1, (debugfile, "Malformed FETCH response"));
487 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
488 dprint (2, (debugfile, "Only handle FLAGS updates\n"));
492 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
494 idata->reopen |= IMAP_EXPUNGE_PENDING;
496 imap_set_flags (idata, h, s);
497 idata->check_status = IMAP_FLAGS_PENDING;
501 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
502 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
504 dprint (2, (debugfile, "Handling MYRIGHTS\n"));
506 s = imap_next_word (s);
507 s = imap_next_word (s);
509 /* zero out current rights set */
510 memset (idata->rights, 0, sizeof (idata->rights));
512 while (*s && !isspace ((unsigned char) *s)) {
515 mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);
518 mutt_bit_set (idata->rights, IMAP_ACL_READ);
521 mutt_bit_set (idata->rights, IMAP_ACL_SEEN);
524 mutt_bit_set (idata->rights, IMAP_ACL_WRITE);
527 mutt_bit_set (idata->rights, IMAP_ACL_INSERT);
530 mutt_bit_set (idata->rights, IMAP_ACL_POST);
533 mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
536 mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
539 mutt_bit_set (idata->rights, IMAP_ACL_ADMIN);