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
20 #include "imap_private.h"
26 #include "lib/debug.h"
31 #define IMAP_CMD_BUFSIZE 512
33 /* forward declarations */
34 static void cmd_handle_fatal (IMAP_DATA * idata);
35 static int cmd_handle_untagged (IMAP_DATA * idata);
36 static void cmd_make_sequence (IMAP_DATA * idata);
37 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
38 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
39 static void cmd_parse_lsub (IMAP_DATA* idata, char* s);
40 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
41 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
43 static char *Capabilities[] = {
58 /* imap_cmd_start: Given an IMAP command, send it to the server.
59 * Currently a minor convenience, but helps to route all IMAP commands
60 * through a single interface. */
61 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
67 if (idata->status == IMAP_FATAL) {
68 cmd_handle_fatal (idata);
72 cmd_make_sequence (idata);
73 /* seq, space, cmd, \r\n\0 */
74 outlen = str_len (idata->cmd.seq) + str_len (cmd) + 4;
75 out = (char *) mem_malloc (outlen);
76 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
78 rc = mutt_socket_write (idata->conn, out);
82 return (rc < 0) ? IMAP_CMD_BAD : 0;
85 /* imap_cmd_step: Reads server responses from an IMAP command, detects
86 * tagged completion response, handles untagged messages, can read
87 * arbitrarily large strings (using malloc, so don't make it _too_
89 int imap_cmd_step (IMAP_DATA * idata)
91 IMAP_COMMAND *cmd = &idata->cmd;
95 if (idata->status == IMAP_FATAL) {
96 cmd_handle_fatal (idata);
100 /* read into buffer, expanding buffer as necessary until we have a full
103 if (len == cmd->blen) {
104 mem_realloc (&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
105 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
106 debug_print (3, ("grew buffer to %u bytes\n", cmd->blen));
109 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
111 debug_print (1, ("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 mem_realloc (&cmd->buf, IMAP_CMD_BUFSIZE);
126 cmd->blen = IMAP_CMD_BUFSIZE;
127 debug_print (3, ("shrank buffer to %u bytes\n", cmd->blen));
130 idata->lastread = time (NULL);
132 /* handle untagged messages. The caller still gets its shot afterwards. */
133 if (!ascii_strncmp (cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
136 /* server demands a continuation response from us */
137 if (!ascii_strncmp (cmd->buf, "+ ", 2)) {
138 return IMAP_CMD_RESPOND;
141 /* tagged completion code */
142 if (!ascii_strncmp (cmd->buf, cmd->seq, SEQLEN)) {
143 imap_cmd_finish (idata);
144 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
147 return IMAP_CMD_CONTINUE;
150 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
151 int imap_code (const char *s)
155 return (ascii_strncasecmp ("OK", s, 2) == 0);
158 /* imap_exec: execute a command, and wait for the response from the server.
159 * Also, handle untagged responses.
161 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
162 * for checking for a mailbox on append and login
163 * IMAP_CMD_PASS: command contains a password. Suppress logging.
164 * Return 0 on success, -1 on Failure, -2 on OK Failure
166 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
173 mutt_error (_("No mailbox is open."));
178 if (idata->status == IMAP_FATAL) {
179 cmd_handle_fatal (idata);
183 /* create sequence for command */
184 cmd_make_sequence (idata);
185 /* seq, space, cmd, \r\n\0 */
186 outlen = str_len (idata->cmd.seq) + str_len (cmd) + 4;
187 out = (char *) mem_malloc (outlen);
188 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
190 rc = mutt_socket_write_d (idata->conn, out,
191 flags & IMAP_CMD_PASS ? IMAP_LOG_PASS :
196 cmd_handle_fatal (idata);
201 rc = imap_cmd_step (idata);
202 while (rc == IMAP_CMD_CONTINUE);
204 if (rc == IMAP_CMD_BAD) {
205 if (imap_reconnect (idata->ctx) != 0) {
211 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
214 if (rc != IMAP_CMD_OK) {
215 if (flags & IMAP_CMD_FAIL_OK)
218 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
225 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
226 int imap_cmd_running (IMAP_DATA * idata)
228 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
229 idata->cmd.state == IMAP_CMD_RESPOND)
235 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
236 * detected, do expunge). Called automatically by imap_cmd_step, but
237 * may be called at any time. Called by imap_check_mailbox just before
238 * the index is refreshed, for instance. */
239 void imap_cmd_finish (IMAP_DATA * idata)
241 if (idata->status == IMAP_FATAL) {
242 cmd_handle_fatal (idata);
246 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
249 if (idata->reopen & IMAP_REOPEN_ALLOW) {
250 int count = idata->newMailCount;
252 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
253 (idata->reopen & IMAP_NEWMAIL_PENDING)
254 && count > idata->ctx->msgcount) {
255 /* read new mail messages */
256 debug_print (2, ("Fetching new mail\n"));
257 /* check_status: curs_main uses imap_check_mailbox to detect
258 * whether the index needs updating */
259 idata->check_status = IMAP_NEWMAIL_PENDING;
260 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
262 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
263 debug_print (2, ("Expunging mailbox\n"));
264 imap_expunge_mailbox (idata);
265 /* Detect whether we've gotten unexpected EXPUNGE messages */
266 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
267 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
268 idata->check_status = IMAP_EXPUNGE_PENDING;
269 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
270 IMAP_EXPUNGE_EXPECTED);
277 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
278 static void cmd_handle_fatal (IMAP_DATA * idata)
280 idata->status = IMAP_FATAL;
282 if ((idata->state == IMAP_SELECTED) &&
283 (idata->reopen & IMAP_REOPEN_ALLOW)) {
284 /* mx_fastclose_mailbox (idata->ctx); */
285 mutt_error (_("Mailbox closed"));
287 idata->state = IMAP_DISCONNECTED;
288 if (imap_reconnect (idata->ctx) != 0)
289 mx_fastclose_mailbox (idata->ctx);
292 if (idata->state != IMAP_SELECTED) {
293 idata->state = IMAP_DISCONNECTED;
294 mutt_socket_close (idata->conn);
299 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
300 static int cmd_handle_untagged (IMAP_DATA * idata)
306 s = imap_next_word (idata->cmd.buf);
308 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
310 s = imap_next_word (s);
312 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
313 * connection, so update that one.
315 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
316 debug_print (2, ("Handling EXISTS\n"));
318 /* new mail arrived */
321 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
322 count < idata->ctx->msgcount) {
323 /* something is wrong because the server reported fewer messages
324 * than we previously saw
326 mutt_error _("Fatal error. Message count is out of sync!");
328 idata->status = IMAP_FATAL;
331 /* at least the InterChange server sends EXISTS messages freely,
332 * even when there is no new mail */
333 else if (count == idata->ctx->msgcount)
334 debug_print (3, ("superfluous EXISTS message.\n"));
336 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
337 debug_print (2, ("New mail in %s - %d messages total.\n", idata->mailbox, count));
338 idata->reopen |= IMAP_NEWMAIL_PENDING;
340 idata->newMailCount = count;
343 /* pn vs. s: need initial seqno */
344 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
345 cmd_parse_expunge (idata, pn);
346 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
347 cmd_parse_fetch (idata, pn);
349 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
350 cmd_parse_capabilities (idata, s);
351 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
352 cmd_parse_lsub (idata, s);
353 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
354 cmd_parse_myrights (idata, s);
355 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
356 debug_print (2, ("Handling BYE\n"));
358 /* check if we're logging out */
359 if (idata->status == IMAP_BYE)
362 /* server shut down our connection */
365 mutt_error ("%s", s);
367 cmd_handle_fatal (idata);
370 else if (option (OPTIMAPSERVERNOISE)
371 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
372 debug_print (2, ("Handling untagged NO\n"));
374 /* Display the warning message from the server */
375 mutt_error ("%s", s + 3);
382 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
383 static void cmd_make_sequence (IMAP_DATA * idata)
385 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
387 if (idata->seqno > 9999)
391 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
393 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
397 debug_print (2, ("Handling CAPABILITY\n"));
399 s = imap_next_word (s);
400 mem_free (&idata->capstr);
401 idata->capstr = str_dup (s);
403 memset (idata->capabilities, 0, sizeof (idata->capabilities));
406 for (x = 0; x < CAPMAX; x++)
407 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
408 mutt_bit_set (idata->capabilities, x);
411 s = imap_next_word (s);
415 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
416 * be reopened at our earliest convenience */
417 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
422 debug_print (2, ("Handling EXPUNGE\n"));
426 /* walk headers, zero seqno of expunged message, decrement seqno of those
427 * above. Possibly we could avoid walking the whole list by resorting
428 * and guessing a good starting point, but I'm guessing the resort would
429 * nullify the gains */
430 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
431 h = idata->ctx->hdrs[cur];
433 if (h->index + 1 == expno)
435 else if (h->index + 1 > expno)
439 idata->reopen |= IMAP_EXPUNGE_PENDING;
442 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
443 * handles unanticipated FETCH responses, and only FLAGS data. We get
444 * these if another client has changed flags for a mailbox we've selected.
445 * Of course, a lot of code here duplicates code in message.c. */
446 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
451 debug_print (2, ("Handling FETCH\n"));
455 if (msgno <= idata->ctx->msgcount)
456 /* see cmd_parse_expunge */
457 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
458 h = idata->ctx->hdrs[cur];
460 if (h->active && h->index + 1 == msgno) {
461 debug_print (2, ("Message UID %d updated\n", HEADER_DATA (h)->uid));
469 debug_print (1, ("FETCH response ignored for this message\n"));
474 s = imap_next_word (s);
475 s = imap_next_word (s);
478 debug_print (1, ("Malformed FETCH response\n"));
483 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
484 debug_print (2, ("Only handle FLAGS updates\n"));
488 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
490 idata->reopen |= IMAP_EXPUNGE_PENDING;
492 imap_set_flags (idata, h, s);
493 idata->check_status = IMAP_FLAGS_PENDING;
497 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
504 if (!option (OPTIMAPCHECKSUBSCRIBED))
507 s = imap_next_word (s); /* flags */
510 debug_print (1, ("Bad LSUB response\n"));
516 for (ep = s; *ep && *ep != ')'; ep++);
518 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
520 while (s < ep && *s != ' ' && *s != ')')
526 s = imap_next_word (s); /* delim */
527 s = imap_next_word (s); /* name */
530 imap_unmunge_mbox_name (s);
531 debug_print (2, ("Subscribing to %s\n", s));
533 strfcpy (buf, "mailboxes \"", sizeof (buf));
534 mutt_account_tourl (&idata->conn->account, &url);
536 if (!str_cmp (url.user, ImapUser))
538 url_ciss_tostring (&url, buf + 11, sizeof (buf) - 10, 0);
539 str_cat (buf, sizeof (buf), "\"");
540 memset (&token, 0, sizeof (token));
542 err.dsize = sizeof (errstr);
543 if (mutt_parse_rc_line (buf, &token, &err))
544 debug_print (1, ("Error adding subscribed mailbox: %s\n", errstr));
545 mem_free (&token.data);
548 debug_print (1, ("Bad LSUB response\n"));
551 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
552 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
554 debug_print (2, ("Handling MYRIGHTS\n"));
556 s = imap_next_word (s);
557 s = imap_next_word (s);
559 /* zero out current rights set */
560 memset (idata->rights, 0, sizeof (idata->rights));
562 while (*s && !isspace ((unsigned char) *s)) {
565 mutt_bit_set (idata->rights, ACL_LOOKUP);
568 mutt_bit_set (idata->rights, ACL_READ);
571 mutt_bit_set (idata->rights, ACL_SEEN);
574 mutt_bit_set (idata->rights, ACL_WRITE);
577 mutt_bit_set (idata->rights, ACL_INSERT);
580 mutt_bit_set (idata->rights, ACL_POST);
583 mutt_bit_set (idata->rights, ACL_CREATE);
586 mutt_bit_set (idata->rights, ACL_DELETE);
589 mutt_bit_set (idata->rights, ACL_ADMIN);