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-2005 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
21 #include "lib/debug.h"
27 #include "imap_private.h"
32 #define IMAP_CMD_BUFSIZE 512
34 /* forward declarations */
35 static void cmd_handle_fatal (IMAP_DATA * idata);
36 static int cmd_handle_untagged (IMAP_DATA * idata);
37 static void cmd_make_sequence (IMAP_DATA * idata);
38 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
39 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
40 static void cmd_parse_lsub (IMAP_DATA* idata, char* s);
41 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
42 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
44 static char *Capabilities[] = {
59 /* imap_cmd_start: Given an IMAP command, send it to the server.
60 * Currently a minor convenience, but helps to route all IMAP commands
61 * through a single interface. */
62 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
68 if (idata->status == IMAP_FATAL) {
69 cmd_handle_fatal (idata);
73 cmd_make_sequence (idata);
74 /* seq, space, cmd, \r\n\0 */
75 outlen = str_len (idata->cmd.seq) + str_len (cmd) + 4;
76 out = (char *) mem_malloc (outlen);
77 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
79 rc = mutt_socket_write (idata->conn, out);
83 return (rc < 0) ? IMAP_CMD_BAD : 0;
86 /* imap_cmd_step: Reads server responses from an IMAP command, detects
87 * tagged completion response, handles untagged messages, can read
88 * arbitrarily large strings (using malloc, so don't make it _too_
90 int imap_cmd_step (IMAP_DATA * idata)
92 IMAP_COMMAND *cmd = &idata->cmd;
96 if (idata->status == IMAP_FATAL) {
97 cmd_handle_fatal (idata);
101 /* read into buffer, expanding buffer as necessary until we have a full
104 if (len == cmd->blen) {
105 mem_realloc (&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
106 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
107 debug_print (3, ("grew buffer to %u bytes\n", cmd->blen));
110 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
112 debug_print (1, ("Error reading server response.\n"));
113 /* cmd_handle_fatal (idata); */
119 /* if we've read all the way to the end of the buffer, we haven't read a
120 * full line (mutt_socket_readln strips the \r, so we always have at least
121 * one character free when we've read a full line) */
122 while (len == cmd->blen);
124 /* don't let one large string make cmd->buf hog memory forever */
125 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
126 mem_realloc (&cmd->buf, IMAP_CMD_BUFSIZE);
127 cmd->blen = IMAP_CMD_BUFSIZE;
128 debug_print (3, ("shrank buffer to %u bytes\n", cmd->blen));
131 idata->lastread = time (NULL);
133 /* handle untagged messages. The caller still gets its shot afterwards. */
134 if (!ascii_strncmp (cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
137 /* server demands a continuation response from us */
138 if (!ascii_strncmp (cmd->buf, "+ ", 2)) {
139 return IMAP_CMD_RESPOND;
142 /* tagged completion code */
143 if (!ascii_strncmp (cmd->buf, cmd->seq, SEQLEN)) {
144 imap_cmd_finish (idata);
145 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
148 return IMAP_CMD_CONTINUE;
151 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
152 int imap_code (const char *s)
156 return (ascii_strncasecmp ("OK", s, 2) == 0);
159 /* imap_exec: execute a command, and wait for the response from the server.
160 * Also, handle untagged responses.
162 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
163 * for checking for a mailbox on append and login
164 * IMAP_CMD_PASS: command contains a password. Suppress logging.
165 * Return 0 on success, -1 on Failure, -2 on OK Failure
167 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
174 mutt_error (_("No mailbox is open."));
179 if (idata->status == IMAP_FATAL) {
180 cmd_handle_fatal (idata);
184 /* create sequence for command */
185 cmd_make_sequence (idata);
186 /* seq, space, cmd, \r\n\0 */
187 outlen = str_len (idata->cmd.seq) + str_len (cmd) + 4;
188 out = (char *) mem_malloc (outlen);
189 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
191 rc = mutt_socket_write_d (idata->conn, out,
192 flags & IMAP_CMD_PASS ? IMAP_LOG_PASS :
197 cmd_handle_fatal (idata);
202 rc = imap_cmd_step (idata);
203 while (rc == IMAP_CMD_CONTINUE);
205 if (rc == IMAP_CMD_BAD) {
206 if (imap_reconnect (idata->ctx) != 0) {
212 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
215 if (rc != IMAP_CMD_OK) {
216 if (flags & IMAP_CMD_FAIL_OK)
219 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
226 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
227 int imap_cmd_running (IMAP_DATA * idata)
229 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
230 idata->cmd.state == IMAP_CMD_RESPOND)
236 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
237 * detected, do expunge). Called automatically by imap_cmd_step, but
238 * may be called at any time. Called by imap_check_mailbox just before
239 * the index is refreshed, for instance. */
240 void imap_cmd_finish (IMAP_DATA * idata)
242 if (idata->status == IMAP_FATAL) {
243 cmd_handle_fatal (idata);
247 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
250 if (idata->reopen & IMAP_REOPEN_ALLOW) {
251 int count = idata->newMailCount;
253 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
254 (idata->reopen & IMAP_NEWMAIL_PENDING)
255 && count > idata->ctx->msgcount) {
256 /* read new mail messages */
257 debug_print (2, ("Fetching new mail\n"));
258 /* check_status: curs_main uses imap_check_mailbox to detect
259 * whether the index needs updating */
260 idata->check_status = IMAP_NEWMAIL_PENDING;
261 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
263 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
264 debug_print (2, ("Expunging mailbox\n"));
265 imap_expunge_mailbox (idata);
266 /* Detect whether we've gotten unexpected EXPUNGE messages */
267 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
268 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
269 idata->check_status = IMAP_EXPUNGE_PENDING;
270 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
271 IMAP_EXPUNGE_EXPECTED);
278 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
279 static void cmd_handle_fatal (IMAP_DATA * idata)
281 idata->status = IMAP_FATAL;
283 if ((idata->state == IMAP_SELECTED) &&
284 (idata->reopen & IMAP_REOPEN_ALLOW)) {
285 /* mx_fastclose_mailbox (idata->ctx); */
286 mutt_error (_("Mailbox closed"));
288 idata->state = IMAP_DISCONNECTED;
289 if (imap_reconnect (idata->ctx) != 0)
290 mx_fastclose_mailbox (idata->ctx);
293 if (idata->state != IMAP_SELECTED) {
294 idata->state = IMAP_DISCONNECTED;
295 mutt_socket_close (idata->conn);
300 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
301 static int cmd_handle_untagged (IMAP_DATA * idata)
307 s = imap_next_word (idata->cmd.buf);
309 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
311 s = imap_next_word (s);
313 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
314 * connection, so update that one.
316 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
317 debug_print (2, ("Handling EXISTS\n"));
319 /* new mail arrived */
322 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
323 count < idata->ctx->msgcount) {
324 /* something is wrong because the server reported fewer messages
325 * than we previously saw
327 mutt_error _("Fatal error. Message count is out of sync!");
329 idata->status = IMAP_FATAL;
332 /* at least the InterChange server sends EXISTS messages freely,
333 * even when there is no new mail */
334 else if (count == idata->ctx->msgcount)
335 debug_print (3, ("superfluous EXISTS message.\n"));
337 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
338 debug_print (2, ("New mail in %s - %d messages total.\n", idata->mailbox, count));
339 idata->reopen |= IMAP_NEWMAIL_PENDING;
341 idata->newMailCount = count;
344 /* pn vs. s: need initial seqno */
345 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
346 cmd_parse_expunge (idata, pn);
347 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
348 cmd_parse_fetch (idata, pn);
350 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
351 cmd_parse_capabilities (idata, s);
352 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
353 cmd_parse_lsub (idata, s);
354 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
355 cmd_parse_myrights (idata, s);
356 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
357 debug_print (2, ("Handling BYE\n"));
359 /* check if we're logging out */
360 if (idata->status == IMAP_BYE)
363 /* server shut down our connection */
366 mutt_error ("%s", s);
368 cmd_handle_fatal (idata);
371 else if (option (OPTIMAPSERVERNOISE)
372 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
373 debug_print (2, ("Handling untagged NO\n"));
375 /* Display the warning message from the server */
376 mutt_error ("%s", s + 3);
383 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
384 static void cmd_make_sequence (IMAP_DATA * idata)
386 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
388 if (idata->seqno > 9999)
392 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
394 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
398 debug_print (2, ("Handling CAPABILITY\n"));
400 s = imap_next_word (s);
401 mem_free (&idata->capstr);
402 idata->capstr = str_dup (s);
404 memset (idata->capabilities, 0, sizeof (idata->capabilities));
407 for (x = 0; x < CAPMAX; x++)
408 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
409 mutt_bit_set (idata->capabilities, x);
412 s = imap_next_word (s);
416 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
417 * be reopened at our earliest convenience */
418 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
423 debug_print (2, ("Handling EXPUNGE\n"));
427 /* walk headers, zero seqno of expunged message, decrement seqno of those
428 * above. Possibly we could avoid walking the whole list by resorting
429 * and guessing a good starting point, but I'm guessing the resort would
430 * nullify the gains */
431 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
432 h = idata->ctx->hdrs[cur];
434 if (h->index + 1 == expno)
436 else if (h->index + 1 > expno)
440 idata->reopen |= IMAP_EXPUNGE_PENDING;
443 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
444 * handles unanticipated FETCH responses, and only FLAGS data. We get
445 * these if another client has changed flags for a mailbox we've selected.
446 * Of course, a lot of code here duplicates code in message.c. */
447 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
452 debug_print (2, ("Handling FETCH\n"));
456 if (msgno <= idata->ctx->msgcount)
457 /* see cmd_parse_expunge */
458 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
459 h = idata->ctx->hdrs[cur];
461 if (h->active && h->index + 1 == msgno) {
462 debug_print (2, ("Message UID %d updated\n", HEADER_DATA (h)->uid));
470 debug_print (1, ("FETCH response ignored for this message\n"));
475 s = imap_next_word (s);
476 s = imap_next_word (s);
479 debug_print (1, ("Malformed FETCH response\n"));
484 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
485 debug_print (2, ("Only handle FLAGS updates\n"));
489 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
491 idata->reopen |= IMAP_EXPUNGE_PENDING;
493 imap_set_flags (idata, h, s);
494 idata->check_status = IMAP_FLAGS_PENDING;
498 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
505 if (!option (OPTIMAPCHECKSUBSCRIBED))
508 s = imap_next_word (s); /* flags */
511 debug_print (1, ("Bad LSUB response\n"));
517 for (ep = s; *ep && *ep != ')'; ep++);
519 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
521 while (s < ep && *s != ' ' && *s != ')')
527 s = imap_next_word (s); /* delim */
528 s = imap_next_word (s); /* name */
531 imap_unmunge_mbox_name (s);
532 debug_print (2, ("Subscribing to %s\n", s));
534 strfcpy (buf, "mailboxes \"", sizeof (buf));
535 mutt_account_tourl (&idata->conn->account, &url);
537 if (!str_cmp (url.user, ImapUser))
539 url_ciss_tostring (&url, buf + 11, sizeof (buf) - 10, 0);
540 str_cat (buf, sizeof (buf), "\"");
541 memset (&token, 0, sizeof (token));
543 err.dsize = sizeof (errstr);
544 if (mutt_parse_rc_line (buf, &token, &err))
545 debug_print (1, ("Error adding subscribed mailbox: %s\n", errstr));
546 mem_free (&token.data);
549 debug_print (1, ("Bad LSUB response\n"));
552 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
553 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
555 debug_print (2, ("Handling MYRIGHTS\n"));
557 s = imap_next_word (s);
558 s = imap_next_word (s);
560 /* zero out current rights set */
561 memset (idata->rights, 0, sizeof (idata->rights));
563 while (*s && !isspace ((unsigned char) *s)) {
566 mutt_bit_set (idata->rights, ACL_LOOKUP);
569 mutt_bit_set (idata->rights, ACL_READ);
572 mutt_bit_set (idata->rights, ACL_SEEN);
575 mutt_bit_set (idata->rights, ACL_WRITE);
578 mutt_bit_set (idata->rights, ACL_INSERT);
581 mutt_bit_set (idata->rights, ACL_POST);
584 mutt_bit_set (idata->rights, ACL_CREATE);
587 mutt_bit_set (idata->rights, ACL_DELETE);
590 mutt_bit_set (idata->rights, ACL_ADMIN);