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
19 #include <lib-lib/mem.h>
20 #include <lib-lib/ascii.h>
21 #include <lib-lib/macros.h>
26 #include "imap_private.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);
42 static void cmd_parse_search (IMAP_DATA* idata, char* s);
44 static const 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 = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
76 out = p_new(char, 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 p_realloc(&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
106 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
112 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
114 /* cmd_handle_fatal (idata); */
120 /* if we've read all the way to the end of the buffer, we haven't read a
121 * full line (mutt_socket_readln strips the \r, so we always have at least
122 * one character free when we've read a full line) */
123 while (len == cmd->blen);
125 /* don't let one large string make cmd->buf hog memory forever */
126 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
127 p_realloc(&cmd->buf, IMAP_CMD_BUFSIZE);
128 cmd->blen = IMAP_CMD_BUFSIZE;
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 (cmd->buf[0] == '+')
139 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)
153 s = vskipspaces(s + SEQLEN);
154 return !ascii_strncasecmp("OK", s, 2);
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)
172 mutt_error (_("No mailbox is open."));
177 if (idata->status == IMAP_FATAL) {
178 cmd_handle_fatal (idata);
182 /* create sequence for command */
183 cmd_make_sequence (idata);
184 /* seq, space, cmd, \r\n\0 */
185 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
186 out = p_new(char, outlen);
187 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
189 rc = mutt_socket_write(idata->conn, out);
193 cmd_handle_fatal (idata);
198 rc = imap_cmd_step (idata);
199 while (rc == IMAP_CMD_CONTINUE);
201 if (rc == IMAP_CMD_BAD) {
202 if (imap_reconnect (idata->ctx) != 0) {
208 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
211 if (rc != IMAP_CMD_OK) {
212 return (flags & IMAP_CMD_FAIL_OK) ? -2 : -1;
218 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
219 int imap_cmd_running (IMAP_DATA * idata)
221 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
222 idata->cmd.state == IMAP_CMD_RESPOND)
228 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
229 * detected, do expunge). Called automatically by imap_cmd_step, but
230 * may be called at any time. Called by imap_check_mailbox just before
231 * the index is refreshed, for instance. */
232 void imap_cmd_finish (IMAP_DATA * idata)
234 if (idata->status == IMAP_FATAL) {
235 cmd_handle_fatal (idata);
239 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
242 if (idata->reopen & IMAP_REOPEN_ALLOW) {
243 int count = idata->newMailCount;
245 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
246 (idata->reopen & IMAP_NEWMAIL_PENDING)
247 && count > idata->ctx->msgcount) {
248 /* read new mail messages */
249 /* check_status: curs_main uses imap_check_mailbox to detect
250 * whether the index needs updating */
251 idata->check_status = IMAP_NEWMAIL_PENDING;
252 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
254 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
255 imap_expunge_mailbox (idata);
256 /* Detect whether we've gotten unexpected EXPUNGE messages */
257 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
258 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
259 idata->check_status = IMAP_EXPUNGE_PENDING;
260 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
261 IMAP_EXPUNGE_EXPECTED);
268 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
269 static void cmd_handle_fatal (IMAP_DATA * idata)
271 idata->status = IMAP_FATAL;
273 if ((idata->state == IMAP_SELECTED) &&
274 (idata->reopen & IMAP_REOPEN_ALLOW)) {
275 /* mx_fastclose_mailbox (idata->ctx); */
276 mutt_error (_("Mailbox closed"));
278 idata->state = IMAP_DISCONNECTED;
279 if (imap_reconnect (idata->ctx) != 0)
280 mx_fastclose_mailbox (idata->ctx);
283 if (idata->state != IMAP_SELECTED) {
284 idata->state = IMAP_DISCONNECTED;
285 mutt_socket_close (idata->conn);
290 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
291 static int cmd_handle_untagged (IMAP_DATA * idata)
297 s = imap_next_word (idata->cmd.buf);
299 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
301 s = imap_next_word (s);
303 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
304 * connection, so update that one.
306 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
307 /* new mail arrived */
310 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
311 count < idata->ctx->msgcount) {
312 /* something is wrong because the server reported fewer messages
313 * than we previously saw
315 mutt_error _("Fatal error. Message count is out of sync!");
317 idata->status = IMAP_FATAL;
320 /* at least the InterChange server sends EXISTS messages freely,
321 * even when there is no new mail */
322 else if (count == idata->ctx->msgcount)
325 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
326 idata->reopen |= IMAP_NEWMAIL_PENDING;
328 idata->newMailCount = count;
331 /* pn vs. s: need initial seqno */
332 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
333 cmd_parse_expunge (idata, pn);
334 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
335 cmd_parse_fetch (idata, pn);
337 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
338 cmd_parse_capabilities (idata, s);
339 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
340 cmd_parse_lsub (idata, s);
341 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
342 cmd_parse_myrights (idata, s);
343 else if (ascii_strncasecmp ("SEARCH", s, 6) == 0)
344 cmd_parse_search (idata, s);
345 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
346 /* check if we're logging out */
347 if (idata->status == IMAP_BYE)
350 /* server shut down our connection */
351 s = vskipspaces(s + 3);
352 mutt_error ("%s", s);
354 cmd_handle_fatal (idata);
357 else if (option (OPTIMAPSERVERNOISE)
358 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
359 /* Display the warning message from the server */
360 mutt_error ("%s", s + 3);
367 /* This should be optimised (eg with a tree or hash) */
368 static int uid2msgno (IMAP_DATA* idata, int uid) {
371 for (i = 0; i < idata->ctx->msgcount; i++) {
372 HEADER* h = idata->ctx->hdrs[i];
373 if (HEADER_DATA(h)->uid == uid)
380 /* cmd_parse_search: store SEARCH response for later use */
381 static void cmd_parse_search (IMAP_DATA* idata, char* s) {
385 while ((s = imap_next_word (s)) && *s != '\0') {
387 msgno = uid2msgno (idata, uid);
390 idata->ctx->hdrs[uid2msgno (idata, uid)]->matched = 1;
394 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
395 static void cmd_make_sequence (IMAP_DATA * idata)
397 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
399 if (idata->seqno > 9999)
403 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
405 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
409 s = imap_next_word (s);
410 p_delete(&idata->capstr);
411 idata->capstr = m_strdup(s);
413 p_clear(idata->capabilities, 1);
416 for (x = 0; x < CAPMAX; x++)
417 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
418 mutt_bit_set (idata->capabilities, x);
421 s = imap_next_word (s);
425 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
426 * be reopened at our earliest convenience */
427 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
434 /* walk headers, zero seqno of expunged message, decrement seqno of those
435 * above. Possibly we could avoid walking the whole list by resorting
436 * and guessing a good starting point, but I'm guessing the resort would
437 * nullify the gains */
438 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
439 h = idata->ctx->hdrs[cur];
441 if (h->index + 1 == expno)
443 else if (h->index + 1 > expno)
447 idata->reopen |= IMAP_EXPUNGE_PENDING;
450 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
451 * handles unanticipated FETCH responses, and only FLAGS data. We get
452 * these if another client has changed flags for a mailbox we've selected.
453 * Of course, a lot of code here duplicates code in message.c. */
454 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
461 if (msgno <= idata->ctx->msgcount)
462 /* see cmd_parse_expunge */
463 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
464 h = idata->ctx->hdrs[cur];
469 if (h->active && h->index + 1 == msgno) {
481 s = imap_next_word (s);
482 s = imap_next_word (s);
489 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
493 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
495 idata->reopen |= IMAP_EXPUNGE_PENDING;
497 imap_set_flags (idata, h, s);
498 idata->check_status = IMAP_FLAGS_PENDING;
502 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
509 if (!option (OPTIMAPCHECKSUBSCRIBED))
512 s = imap_next_word (s); /* flags */
520 for (ep = s; *ep && *ep != ')'; ep++);
522 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
524 while (s < ep && *s != ' ' && *s != ')')
530 s = imap_next_word (s); /* delim */
531 s = imap_next_word (s); /* name */
534 imap_unmunge_mbox_name (s);
536 m_strcpy(buf, sizeof(buf), "mailboxes \"");
537 mutt_account_tourl (&idata->conn->account, &url);
539 if (!m_strcmp(url.user, ImapUser))
541 url_ciss_tostring (&url, buf + 11, sizeof (buf) - 10, 0);
542 m_strcat(buf, sizeof(buf), "\"");
545 err.dsize = sizeof (errstr);
546 mutt_parse_rc_line (buf, &token, &err);
547 p_delete(&token.data);
551 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
552 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
554 s = imap_next_word (s);
555 s = imap_next_word (s);
557 /* zero out current rights set */
558 p_clear(idata->rights, 1);
560 while (*s && !isspace ((unsigned char) *s)) {
563 mutt_bit_set (idata->rights, ACL_LOOKUP);
566 mutt_bit_set (idata->rights, ACL_READ);
569 mutt_bit_set (idata->rights, ACL_SEEN);
572 mutt_bit_set (idata->rights, ACL_WRITE);
575 mutt_bit_set (idata->rights, ACL_INSERT);
578 mutt_bit_set (idata->rights, ACL_POST);
581 mutt_bit_set (idata->rights, ACL_CREATE);
584 mutt_bit_set (idata->rights, ACL_DELETE);
587 mutt_bit_set (idata->rights, ACL_ADMIN);