2 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 /* command.c: routines for sending commands to an IMAP server and parsing
25 #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_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)
69 cmd_handle_fatal (idata);
73 cmd_make_sequence (idata);
74 /* seq, space, cmd, \r\n\0 */
75 outlen = strlen (idata->cmd.seq) + strlen (cmd) + 4;
76 out = (char*) safe_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)
98 cmd_handle_fatal (idata);
102 /* read into buffer, expanding buffer as necessary until we have a full
106 if (len == cmd->blen)
108 safe_realloc (&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
109 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
110 dprint (3, (debugfile, "imap_cmd_step: grew buffer to %u bytes\n",
114 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
117 dprint (1, (debugfile, "imap_cmd_step: Error reading server response.\n"));
118 cmd_handle_fatal (idata);
124 /* if we've read all the way to the end of the buffer, we haven't read a
125 * full line (mutt_socket_readln strips the \r, so we always have at least
126 * one character free when we've read a full line) */
127 while (len == cmd->blen);
129 /* don't let one large string make cmd->buf hog memory forever */
130 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE))
132 safe_realloc (&cmd->buf, IMAP_CMD_BUFSIZE);
133 cmd->blen = IMAP_CMD_BUFSIZE;
134 dprint (3, (debugfile, "imap_cmd_step: shrank buffer to %u bytes\n", cmd->blen));
137 idata->lastread = time(NULL);
139 /* handle untagged messages. The caller still gets its shot afterwards. */
140 if (!ascii_strncmp (cmd->buf, "* ", 2) &&
141 cmd_handle_untagged (idata))
144 /* server demands a continuation response from us */
145 if (!ascii_strncmp (cmd->buf, "+ ", 2))
147 return IMAP_CMD_RESPOND;
150 /* tagged completion code */
151 if (!ascii_strncmp (cmd->buf, cmd->seq, SEQLEN))
153 imap_cmd_finish (idata);
154 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
157 return IMAP_CMD_CONTINUE;
160 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
161 int imap_code (const char *s)
165 return (ascii_strncasecmp ("OK", s, 2) == 0);
168 /* imap_exec: execute a command, and wait for the response from the server.
169 * Also, handle untagged responses.
171 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
172 * for checking for a mailbox on append and login
173 * IMAP_CMD_PASS: command contains a password. Suppress logging.
174 * Return 0 on success, -1 on Failure, -2 on OK Failure
176 int imap_exec (IMAP_DATA* idata, const char* cmd, int flags)
182 if (idata->status == IMAP_FATAL)
184 cmd_handle_fatal (idata);
188 /* create sequence for command */
189 cmd_make_sequence (idata);
190 /* seq, space, cmd, \r\n\0 */
191 outlen = strlen (idata->cmd.seq) + strlen (cmd) + 4;
192 out = (char*) safe_malloc (outlen);
193 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
195 rc = mutt_socket_write_d (idata->conn, out,
196 flags & IMAP_CMD_PASS ? IMAP_LOG_PASS : IMAP_LOG_CMD);
201 cmd_handle_fatal (idata);
206 rc = imap_cmd_step (idata);
207 while (rc == IMAP_CMD_CONTINUE);
209 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
212 if (rc != IMAP_CMD_OK)
214 if (flags & IMAP_CMD_FAIL_OK)
217 dprint (1, (debugfile, "imap_exec: command failed: %s\n", idata->cmd.buf));
224 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
225 int imap_cmd_running (IMAP_DATA* idata)
227 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
228 idata->cmd.state == IMAP_CMD_RESPOND)
234 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
235 * detected, do expunge). Called automatically by imap_cmd_step, but
236 * may be called at any time. Called by imap_check_mailbox just before
237 * the index is refreshed, for instance. */
238 void imap_cmd_finish (IMAP_DATA* idata)
240 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)
251 int count = idata->newMailCount;
253 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
254 (idata->reopen & IMAP_NEWMAIL_PENDING)
255 && count > idata->ctx->msgcount)
257 /* read new mail messages */
258 dprint (2, (debugfile, "imap_cmd_finish: Fetching new mail\n"));
259 /* check_status: curs_main uses imap_check_mailbox to detect
260 * whether the index needs updating */
261 idata->check_status = IMAP_NEWMAIL_PENDING;
262 imap_read_headers (idata, idata->ctx->msgcount, count-1);
264 else if (idata->reopen & IMAP_EXPUNGE_PENDING)
266 dprint (2, (debugfile, "imap_cmd_finish: Expunging mailbox\n"));
267 imap_expunge_mailbox (idata);
268 /* Detect whether we've gotten unexpected EXPUNGE messages */
269 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
270 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
271 idata->check_status = IMAP_EXPUNGE_PENDING;
272 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
273 IMAP_EXPUNGE_EXPECTED);
280 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
281 static void cmd_handle_fatal (IMAP_DATA* idata)
283 idata->status = IMAP_FATAL;
285 if ((idata->state == IMAP_SELECTED) &&
286 (idata->reopen & IMAP_REOPEN_ALLOW) &&
287 !idata->ctx->closing)
289 mx_fastclose_mailbox (idata->ctx);
290 /*mutt_error (_("Mailbox closed"));
293 idata->state = IMAP_DISCONNECTED;
294 imap_reconnect(idata->ctx);
297 if (idata->state != IMAP_SELECTED)
299 idata->state = IMAP_DISCONNECTED;
304 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
305 static int cmd_handle_untagged (IMAP_DATA* idata)
311 s = imap_next_word (idata->cmd.buf);
313 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s))
316 s = imap_next_word (s);
318 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
319 * connection, so update that one.
321 if (ascii_strncasecmp ("EXISTS", s, 6) == 0)
323 dprint (2, (debugfile, "Handling EXISTS\n"));
325 /* new mail arrived */
328 if ( !(idata->reopen & IMAP_EXPUNGE_PENDING) &&
329 count < idata->ctx->msgcount)
331 /* something is wrong because the server reported fewer messages
332 * than we previously saw
334 mutt_error _("Fatal error. Message count is out of sync!");
335 idata->status = IMAP_FATAL;
338 /* at least the InterChange server sends EXISTS messages freely,
339 * even when there is no new mail */
340 else if (count == idata->ctx->msgcount)
341 dprint (3, (debugfile,
342 "cmd_handle_untagged: superfluous EXISTS message.\n"));
345 if (!(idata->reopen & IMAP_EXPUNGE_PENDING))
347 dprint (2, (debugfile,
348 "cmd_handle_untagged: New mail in %s - %d messages total.\n",
349 idata->mailbox, count));
350 idata->reopen |= IMAP_NEWMAIL_PENDING;
352 idata->newMailCount = count;
355 /* pn vs. s: need initial seqno */
356 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
357 cmd_parse_expunge (idata, pn);
358 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
359 cmd_parse_fetch (idata, pn);
361 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
362 cmd_parse_capabilities (idata, s);
363 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
364 cmd_parse_myrights (idata, s);
365 else if (ascii_strncasecmp ("BYE", s, 3) == 0)
367 dprint (2, (debugfile, "Handling BYE\n"));
369 /* check if we're logging out */
370 if (idata->status == IMAP_BYE)
373 /* server shut down our connection */
376 mutt_error ("%s", s);
377 idata->status = IMAP_BYE;
378 if (idata->state == IMAP_SELECTED)
379 mx_fastclose_mailbox (idata->ctx);
380 mutt_socket_close (idata->conn);
381 idata->state = IMAP_DISCONNECTED;
385 else if (option (OPTIMAPSERVERNOISE) && (ascii_strncasecmp ("NO", s, 2) == 0))
387 dprint (2, (debugfile, "Handling untagged NO\n"));
389 /* Display the warning message from the server */
390 mutt_error ("%s", s+3);
397 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
398 static void cmd_make_sequence (IMAP_DATA* idata)
400 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04d", idata->seqno++);
402 if (idata->seqno > 9999)
406 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
408 static void cmd_parse_capabilities (IMAP_DATA* idata, char* s)
412 dprint (2, (debugfile, "Handling CAPABILITY\n"));
414 s = imap_next_word (s);
415 FREE(&idata->capstr);
416 idata->capstr = safe_strdup (s);
418 memset (idata->capabilities, 0, sizeof (idata->capabilities));
422 for (x = 0; x < CAPMAX; x++)
423 if (imap_wordcasecmp(Capabilities[x], s) == 0)
425 mutt_bit_set (idata->capabilities, x);
428 s = imap_next_word (s);
432 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
433 * be reopened at our earliest convenience */
434 static void cmd_parse_expunge (IMAP_DATA* idata, const char* s)
439 dprint (2, (debugfile, "Handling EXPUNGE\n"));
443 /* walk headers, zero seqno of expunged message, decrement seqno of those
444 * above. Possibly we could avoid walking the whole list by resorting
445 * and guessing a good starting point, but I'm guessing the resort would
446 * nullify the gains */
447 for (cur = 0; cur < idata->ctx->msgcount; cur++)
449 h = idata->ctx->hdrs[cur];
451 if (h->index+1 == expno)
453 else if (h->index+1 > expno)
457 idata->reopen |= IMAP_EXPUNGE_PENDING;
460 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
461 * handles unanticipated FETCH responses, and only FLAGS data. We get
462 * these if another client has changed flags for a mailbox we've selected.
463 * Of course, a lot of code here duplicates code in message.c. */
464 static void cmd_parse_fetch (IMAP_DATA* idata, char* s)
469 dprint (2, (debugfile, "Handling FETCH\n"));
473 /* see cmd_parse_expunge */
474 for (cur = 0; cur < idata->ctx->msgcount; cur++)
476 h = idata->ctx->hdrs[cur];
478 if (h->active && h->index+1 == msgno)
480 dprint (2, (debugfile, "Message UID %d updated\n", HEADER_DATA(h)->uid));
489 dprint (1, (debugfile, "FETCH response ignored for this message\n"));
494 s = imap_next_word (s);
495 s = imap_next_word (s);
499 dprint (1, (debugfile, "Malformed FETCH response"));
504 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
506 dprint (2, (debugfile, "Only handle FLAGS updates\n"));
510 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
512 idata->reopen |= IMAP_EXPUNGE_PENDING;
514 imap_set_flags (idata, h, s);
515 idata->check_status = IMAP_FLAGS_PENDING;
519 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
520 static void cmd_parse_myrights (IMAP_DATA* idata, char* s)
522 dprint (2, (debugfile, "Handling MYRIGHTS\n"));
524 s = imap_next_word (s);
525 s = imap_next_word (s);
527 /* zero out current rights set */
528 memset (idata->rights, 0, sizeof (idata->rights));
530 while (*s && !isspace((unsigned char) *s))
535 mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);
538 mutt_bit_set (idata->rights, IMAP_ACL_READ);
541 mutt_bit_set (idata->rights, IMAP_ACL_SEEN);
544 mutt_bit_set (idata->rights, IMAP_ACL_WRITE);
547 mutt_bit_set (idata->rights, IMAP_ACL_INSERT);
550 mutt_bit_set (idata->rights, IMAP_ACL_POST);
553 mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
556 mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
559 mutt_bit_set (idata->rights, IMAP_ACL_ADMIN);