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"
30 #define IMAP_CMD_BUFSIZE 512
32 /* forward declarations */
33 static void cmd_handle_fatal (IMAP_DATA * idata);
34 static int cmd_handle_untagged (IMAP_DATA * idata);
35 static void cmd_make_sequence (IMAP_DATA * idata);
36 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
37 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
38 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
39 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
41 static char *Capabilities[] = {
56 /* imap_cmd_start: Given an IMAP command, send it to the server.
57 * Currently a minor convenience, but helps to route all IMAP commands
58 * through a single interface. */
59 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
65 if (idata->status == IMAP_FATAL) {
66 cmd_handle_fatal (idata);
70 cmd_make_sequence (idata);
71 /* seq, space, cmd, \r\n\0 */
72 outlen = safe_strlen (idata->cmd.seq) + safe_strlen (cmd) + 4;
73 out = (char *) safe_malloc (outlen);
74 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
76 rc = mutt_socket_write (idata->conn, out);
80 return (rc < 0) ? IMAP_CMD_BAD : 0;
83 /* imap_cmd_step: Reads server responses from an IMAP command, detects
84 * tagged completion response, handles untagged messages, can read
85 * arbitrarily large strings (using malloc, so don't make it _too_
87 int imap_cmd_step (IMAP_DATA * idata)
89 IMAP_COMMAND *cmd = &idata->cmd;
93 if (idata->status == IMAP_FATAL) {
94 cmd_handle_fatal (idata);
98 /* read into buffer, expanding buffer as necessary until we have a full
101 if (len == cmd->blen) {
102 safe_realloc (&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
103 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
104 dprint (3, (debugfile, "imap_cmd_step: grew buffer to %u bytes\n",
108 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
111 (debugfile, "imap_cmd_step: Error reading server response.\n"));
112 /* cmd_handle_fatal (idata); */
118 /* if we've read all the way to the end of the buffer, we haven't read a
119 * full line (mutt_socket_readln strips the \r, so we always have at least
120 * one character free when we've read a full line) */
121 while (len == cmd->blen);
123 /* don't let one large string make cmd->buf hog memory forever */
124 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
125 safe_realloc (&cmd->buf, IMAP_CMD_BUFSIZE);
126 cmd->blen = IMAP_CMD_BUFSIZE;
128 (debugfile, "imap_cmd_step: shrank buffer to %u bytes\n",
132 idata->lastread = time (NULL);
134 /* handle untagged messages. The caller still gets its shot afterwards. */
135 if (!ascii_strncmp (cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
138 /* server demands a continuation response from us */
139 if (!ascii_strncmp (cmd->buf, "+ ", 2)) {
140 return IMAP_CMD_RESPOND;
143 /* tagged completion code */
144 if (!ascii_strncmp (cmd->buf, cmd->seq, SEQLEN)) {
145 imap_cmd_finish (idata);
146 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
149 return IMAP_CMD_CONTINUE;
152 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
153 int imap_code (const char *s)
157 return (ascii_strncasecmp ("OK", s, 2) == 0);
160 /* imap_exec: execute a command, and wait for the response from the server.
161 * Also, handle untagged responses.
163 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
164 * for checking for a mailbox on append and login
165 * IMAP_CMD_PASS: command contains a password. Suppress logging.
166 * Return 0 on success, -1 on Failure, -2 on OK Failure
168 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
174 if (idata->status == IMAP_FATAL) {
175 cmd_handle_fatal (idata);
179 /* create sequence for command */
180 cmd_make_sequence (idata);
181 /* seq, space, cmd, \r\n\0 */
182 outlen = safe_strlen (idata->cmd.seq) + safe_strlen (cmd) + 4;
183 out = (char *) safe_malloc (outlen);
184 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
186 rc = mutt_socket_write_d (idata->conn, out,
187 flags & IMAP_CMD_PASS ? IMAP_LOG_PASS :
192 cmd_handle_fatal (idata);
197 rc = imap_cmd_step (idata);
198 while (rc == IMAP_CMD_CONTINUE);
200 if (rc == IMAP_CMD_BAD) {
201 if (imap_reconnect (idata->ctx) != 0) {
207 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
210 if (rc != IMAP_CMD_OK) {
211 if (flags & IMAP_CMD_FAIL_OK)
215 (debugfile, "imap_exec: command failed: %s\n", idata->cmd.buf));
222 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
223 int imap_cmd_running (IMAP_DATA * idata)
225 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
226 idata->cmd.state == IMAP_CMD_RESPOND)
232 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
233 * detected, do expunge). Called automatically by imap_cmd_step, but
234 * may be called at any time. Called by imap_check_mailbox just before
235 * the index is refreshed, for instance. */
236 void imap_cmd_finish (IMAP_DATA * idata)
238 if (idata->status == IMAP_FATAL) {
239 cmd_handle_fatal (idata);
243 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
246 if (idata->reopen & IMAP_REOPEN_ALLOW) {
247 int count = idata->newMailCount;
249 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
250 (idata->reopen & IMAP_NEWMAIL_PENDING)
251 && count > idata->ctx->msgcount) {
252 /* read new mail messages */
253 dprint (2, (debugfile, "imap_cmd_finish: Fetching new mail\n"));
254 /* check_status: curs_main uses imap_check_mailbox to detect
255 * whether the index needs updating */
256 idata->check_status = IMAP_NEWMAIL_PENDING;
257 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
259 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
260 dprint (2, (debugfile, "imap_cmd_finish: Expunging mailbox\n"));
261 imap_expunge_mailbox (idata);
262 /* Detect whether we've gotten unexpected EXPUNGE messages */
263 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
264 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
265 idata->check_status = IMAP_EXPUNGE_PENDING;
266 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
267 IMAP_EXPUNGE_EXPECTED);
274 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
275 static void cmd_handle_fatal (IMAP_DATA * idata)
277 idata->status = IMAP_FATAL;
279 if ((idata->state == IMAP_SELECTED) &&
280 (idata->reopen & IMAP_REOPEN_ALLOW) && !idata->ctx->closing) {
281 /*mx_fastclose_mailbox (idata->ctx); */
282 mutt_error (_("Mailbox closed"));
284 idata->state = IMAP_DISCONNECTED;
285 if (imap_reconnect (idata->ctx) != 0)
286 mx_fastclose_mailbox (idata->ctx);
289 if (idata->state != IMAP_SELECTED) {
290 idata->state = IMAP_DISCONNECTED;
295 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
296 static int cmd_handle_untagged (IMAP_DATA * idata)
302 s = imap_next_word (idata->cmd.buf);
304 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
306 s = imap_next_word (s);
308 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
309 * connection, so update that one.
311 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
312 dprint (2, (debugfile, "Handling EXISTS\n"));
314 /* new mail arrived */
317 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
318 count < idata->ctx->msgcount) {
319 /* something is wrong because the server reported fewer messages
320 * than we previously saw
322 mutt_error _("Fatal error. Message count is out of sync!");
324 idata->status = IMAP_FATAL;
327 /* at least the InterChange server sends EXISTS messages freely,
328 * even when there is no new mail */
329 else if (count == idata->ctx->msgcount)
330 dprint (3, (debugfile,
331 "cmd_handle_untagged: superfluous EXISTS message.\n"));
333 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
334 dprint (2, (debugfile,
335 "cmd_handle_untagged: New mail in %s - %d messages total.\n",
336 idata->mailbox, count));
337 idata->reopen |= IMAP_NEWMAIL_PENDING;
339 idata->newMailCount = count;
342 /* pn vs. s: need initial seqno */
343 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
344 cmd_parse_expunge (idata, pn);
345 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
346 cmd_parse_fetch (idata, pn);
348 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
349 cmd_parse_capabilities (idata, s);
350 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
351 cmd_parse_myrights (idata, s);
352 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
353 dprint (2, (debugfile, "Handling BYE\n"));
355 /* check if we're logging out */
356 if (idata->status == IMAP_BYE)
359 /* server shut down our connection */
362 mutt_error ("%s", s);
363 idata->status = IMAP_BYE;
365 /*if (imap_reconnect(idata->ctx)!=0) {
366 if (idata->state == IMAP_SELECTED)
367 mx_fastclose_mailbox (idata->ctx); *//* XXX memleak? */
368 mutt_socket_close (idata->conn);
369 idata->state = IMAP_DISCONNECTED;
375 else if (option (OPTIMAPSERVERNOISE)
376 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
377 dprint (2, (debugfile, "Handling untagged NO\n"));
379 /* Display the warning message from the server */
380 mutt_error ("%s", s + 3);
387 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
388 static void cmd_make_sequence (IMAP_DATA * idata)
390 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
392 if (idata->seqno > 9999)
396 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
398 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
402 dprint (2, (debugfile, "Handling CAPABILITY\n"));
404 s = imap_next_word (s);
405 FREE (&idata->capstr);
406 idata->capstr = safe_strdup (s);
408 memset (idata->capabilities, 0, sizeof (idata->capabilities));
411 for (x = 0; x < CAPMAX; x++)
412 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
413 mutt_bit_set (idata->capabilities, x);
416 s = imap_next_word (s);
420 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
421 * be reopened at our earliest convenience */
422 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
427 dprint (2, (debugfile, "Handling EXPUNGE\n"));
431 /* walk headers, zero seqno of expunged message, decrement seqno of those
432 * above. Possibly we could avoid walking the whole list by resorting
433 * and guessing a good starting point, but I'm guessing the resort would
434 * nullify the gains */
435 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
436 h = idata->ctx->hdrs[cur];
438 if (h->index + 1 == expno)
440 else if (h->index + 1 > expno)
444 idata->reopen |= IMAP_EXPUNGE_PENDING;
447 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
448 * handles unanticipated FETCH responses, and only FLAGS data. We get
449 * these if another client has changed flags for a mailbox we've selected.
450 * Of course, a lot of code here duplicates code in message.c. */
451 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
456 dprint (2, (debugfile, "Handling FETCH\n"));
460 if (msgno <= idata->ctx->msgcount)
461 /* see cmd_parse_expunge */
462 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
463 h = idata->ctx->hdrs[cur];
465 if (h->active && h->index + 1 == msgno) {
467 (debugfile, "Message UID %d updated\n",
468 HEADER_DATA (h)->uid));
476 dprint (1, (debugfile, "FETCH response ignored for this message\n"));
481 s = imap_next_word (s);
482 s = imap_next_word (s);
485 dprint (1, (debugfile, "Malformed FETCH response"));
490 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
491 dprint (2, (debugfile, "Only handle FLAGS updates\n"));
495 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
497 idata->reopen |= IMAP_EXPUNGE_PENDING;
499 imap_set_flags (idata, h, s);
500 idata->check_status = IMAP_FLAGS_PENDING;
504 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
505 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
507 dprint (2, (debugfile, "Handling MYRIGHTS\n"));
509 s = imap_next_word (s);
510 s = imap_next_word (s);
512 /* zero out current rights set */
513 memset (idata->rights, 0, sizeof (idata->rights));
515 while (*s && !isspace ((unsigned char) *s)) {
518 mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);
521 mutt_bit_set (idata->rights, IMAP_ACL_READ);
524 mutt_bit_set (idata->rights, IMAP_ACL_SEEN);
527 mutt_bit_set (idata->rights, IMAP_ACL_WRITE);
530 mutt_bit_set (idata->rights, IMAP_ACL_INSERT);
533 mutt_bit_set (idata->rights, IMAP_ACL_POST);
536 mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
539 mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
542 mutt_bit_set (idata->rights, IMAP_ACL_ADMIN);