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
15 #include <lib-lib/lib-lib.h>
20 #include "imap_private.h"
25 #define IMAP_CMD_BUFSIZE 512
27 /* forward declarations */
28 static void cmd_handle_fatal (IMAP_DATA * idata);
29 static int cmd_handle_untagged (IMAP_DATA * idata);
30 static void cmd_make_sequence (IMAP_DATA * idata);
31 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
32 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
33 static void cmd_parse_lsub (IMAP_DATA* idata, char* s);
34 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
35 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
36 static void cmd_parse_search (IMAP_DATA* idata, char* s);
38 static const char *Capabilities[] = {
53 /* imap_cmd_start: Given an IMAP command, send it to the server.
54 * Currently a minor convenience, but helps to route all IMAP commands
55 * through a single interface. */
56 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
62 if (idata->status == IMAP_FATAL) {
63 cmd_handle_fatal (idata);
67 cmd_make_sequence (idata);
68 /* seq, space, cmd, \r\n\0 */
69 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
70 out = p_new(char, outlen);
71 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
73 rc = mutt_socket_write (idata->conn, out);
77 return (rc < 0) ? IMAP_CMD_BAD : 0;
80 /* imap_cmd_step: Reads server responses from an IMAP command, detects
81 * tagged completion response, handles untagged messages, can read
82 * arbitrarily large strings (using malloc, so don't make it _too_
84 int imap_cmd_step (IMAP_DATA * idata)
86 IMAP_COMMAND *cmd = &idata->cmd;
90 if (idata->status == IMAP_FATAL) {
91 cmd_handle_fatal (idata);
95 /* read into buffer, expanding buffer as necessary until we have a full
98 if (len == cmd->blen) {
99 p_realloc(&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
100 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
106 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
108 /* cmd_handle_fatal (idata); */
114 /* if we've read all the way to the end of the buffer, we haven't read a
115 * full line (mutt_socket_readln strips the \r, so we always have at least
116 * one character free when we've read a full line) */
117 while (len == cmd->blen);
119 /* don't let one large string make cmd->buf hog memory forever */
120 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
121 p_realloc(&cmd->buf, IMAP_CMD_BUFSIZE);
122 cmd->blen = IMAP_CMD_BUFSIZE;
125 idata->lastread = time (NULL);
127 /* handle untagged messages. The caller still gets its shot afterwards. */
128 if (!m_strncmp(cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
131 /* server demands a continuation response from us */
132 if (cmd->buf[0] == '+')
133 return IMAP_CMD_RESPOND;
135 /* tagged completion code */
136 if (!m_strncmp(cmd->buf, cmd->seq, SEQLEN)) {
137 imap_cmd_finish (idata);
138 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
141 return IMAP_CMD_CONTINUE;
144 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
145 int imap_code (const char *s)
147 s = vskipspaces(s + SEQLEN);
148 return !ascii_strncasecmp("OK", s, 2);
151 /* imap_exec: execute a command, and wait for the response from the server.
152 * Also, handle untagged responses.
154 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
155 * for checking for a mailbox on append and login
156 * IMAP_CMD_PASS: command contains a password. Suppress logging.
157 * Return 0 on success, -1 on Failure, -2 on OK Failure
159 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
166 mutt_error (_("No mailbox is open."));
171 if (idata->status == IMAP_FATAL) {
172 cmd_handle_fatal (idata);
176 /* create sequence for command */
177 cmd_make_sequence (idata);
178 /* seq, space, cmd, \r\n\0 */
179 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
180 out = p_new(char, outlen);
181 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
183 rc = mutt_socket_write(idata->conn, out);
187 cmd_handle_fatal (idata);
192 rc = imap_cmd_step (idata);
193 while (rc == IMAP_CMD_CONTINUE);
195 if (rc == IMAP_CMD_BAD) {
196 if (imap_reconnect (idata->ctx) != 0) {
202 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
205 if (rc != IMAP_CMD_OK) {
206 return (flags & IMAP_CMD_FAIL_OK) ? -2 : -1;
212 /* imap_cmd_running: Returns whether an IMAP command is in progress. */
213 int imap_cmd_running (IMAP_DATA * idata)
215 if (idata->cmd.state == IMAP_CMD_CONTINUE ||
216 idata->cmd.state == IMAP_CMD_RESPOND)
222 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
223 * detected, do expunge). Called automatically by imap_cmd_step, but
224 * may be called at any time. Called by imap_check_mailbox just before
225 * the index is refreshed, for instance. */
226 void imap_cmd_finish (IMAP_DATA * idata)
228 if (idata->status == IMAP_FATAL) {
229 cmd_handle_fatal (idata);
233 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
236 if (idata->reopen & IMAP_REOPEN_ALLOW) {
237 int count = idata->newMailCount;
239 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
240 (idata->reopen & IMAP_NEWMAIL_PENDING)
241 && count > idata->ctx->msgcount) {
242 /* read new mail messages */
243 /* check_status: curs_main uses imap_check_mailbox to detect
244 * whether the index needs updating */
245 idata->check_status = IMAP_NEWMAIL_PENDING;
246 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
248 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
249 imap_expunge_mailbox (idata);
250 /* Detect whether we've gotten unexpected EXPUNGE messages */
251 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
252 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
253 idata->check_status = IMAP_EXPUNGE_PENDING;
254 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
255 IMAP_EXPUNGE_EXPECTED);
262 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
263 static void cmd_handle_fatal (IMAP_DATA * idata)
265 idata->status = IMAP_FATAL;
267 if ((idata->state == IMAP_SELECTED) &&
268 (idata->reopen & IMAP_REOPEN_ALLOW)) {
269 /* mx_fastclose_mailbox (idata->ctx); */
270 mutt_error (_("Mailbox closed"));
272 idata->state = IMAP_DISCONNECTED;
273 if (imap_reconnect (idata->ctx) != 0)
274 mx_fastclose_mailbox (idata->ctx);
277 if (idata->state != IMAP_SELECTED) {
278 idata->state = IMAP_DISCONNECTED;
279 mutt_socket_close (idata->conn);
284 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
285 static int cmd_handle_untagged (IMAP_DATA * idata)
291 s = imap_next_word (idata->cmd.buf);
293 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
295 s = imap_next_word (s);
297 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
298 * connection, so update that one.
300 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
301 /* new mail arrived */
304 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
305 count < idata->ctx->msgcount) {
306 /* something is wrong because the server reported fewer messages
307 * than we previously saw
309 mutt_error _("Fatal error. Message count is out of sync!");
311 idata->status = IMAP_FATAL;
314 /* at least the InterChange server sends EXISTS messages freely,
315 * even when there is no new mail */
316 else if (count == idata->ctx->msgcount)
319 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
320 idata->reopen |= IMAP_NEWMAIL_PENDING;
322 idata->newMailCount = count;
325 /* pn vs. s: need initial seqno */
326 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
327 cmd_parse_expunge (idata, pn);
328 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
329 cmd_parse_fetch (idata, pn);
331 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
332 cmd_parse_capabilities (idata, s);
333 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
334 cmd_parse_lsub (idata, s);
335 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
336 cmd_parse_myrights (idata, s);
337 else if (ascii_strncasecmp ("SEARCH", s, 6) == 0)
338 cmd_parse_search (idata, s);
339 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
340 /* check if we're logging out */
341 if (idata->status == IMAP_BYE)
344 /* server shut down our connection */
345 s = vskipspaces(s + 3);
346 mutt_error ("%s", s);
348 cmd_handle_fatal (idata);
351 else if (option (OPTIMAPSERVERNOISE)
352 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
353 /* Display the warning message from the server */
354 mutt_error ("%s", s + 3);
361 /* This should be optimised (eg with a tree or hash) */
362 static int uid2msgno (IMAP_DATA* idata, int uid) {
365 for (i = 0; i < idata->ctx->msgcount; i++) {
366 HEADER* h = idata->ctx->hdrs[i];
367 if (HEADER_DATA(h)->uid == uid)
374 /* cmd_parse_search: store SEARCH response for later use */
375 static void cmd_parse_search (IMAP_DATA* idata, char* s) {
379 while ((s = imap_next_word (s)) && *s != '\0') {
381 msgno = uid2msgno (idata, uid);
384 idata->ctx->hdrs[uid2msgno (idata, uid)]->matched = 1;
388 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
389 static void cmd_make_sequence (IMAP_DATA * idata)
391 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
393 if (idata->seqno > 9999)
397 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
399 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
403 s = imap_next_word (s);
404 p_delete(&idata->capstr);
405 idata->capstr = m_strdup(s);
407 p_clear(idata->capabilities, 1);
410 for (x = 0; x < CAPMAX; x++)
411 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
412 mutt_bit_set (idata->capabilities, x);
415 s = imap_next_word (s);
419 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
420 * be reopened at our earliest convenience */
421 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
428 /* walk headers, zero seqno of expunged message, decrement seqno of those
429 * above. Possibly we could avoid walking the whole list by resorting
430 * and guessing a good starting point, but I'm guessing the resort would
431 * nullify the gains */
432 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
433 h = idata->ctx->hdrs[cur];
435 if (h->index + 1 == expno)
437 else if (h->index + 1 > expno)
441 idata->reopen |= IMAP_EXPUNGE_PENDING;
444 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
445 * handles unanticipated FETCH responses, and only FLAGS data. We get
446 * these if another client has changed flags for a mailbox we've selected.
447 * Of course, a lot of code here duplicates code in message.c. */
448 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
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];
463 if (h->active && h->index + 1 == msgno) {
475 s = imap_next_word (s);
476 s = imap_next_word (s);
483 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
487 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
489 idata->reopen |= IMAP_EXPUNGE_PENDING;
491 imap_set_flags (idata, h, s);
492 idata->check_status = IMAP_FLAGS_PENDING;
496 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
503 if (!option (OPTIMAPCHECKSUBSCRIBED))
506 s = imap_next_word (s); /* flags */
514 for (ep = s; *ep && *ep != ')'; ep++);
516 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
518 while (s < ep && *s != ' ' && *s != ')')
524 s = imap_next_word (s); /* delim */
525 s = imap_next_word (s); /* name */
528 imap_unmunge_mbox_name (s);
530 m_strcpy(buf, sizeof(buf), "mailboxes \"");
531 mutt_account_tourl (&idata->conn->account, &url);
533 if (!m_strcmp(url.user, ImapUser))
535 url_ciss_tostring (&url, buf + 11, sizeof (buf) - 10, 0);
536 m_strcat(buf, sizeof(buf), "\"");
539 err.dsize = sizeof (errstr);
540 mutt_parse_rc_line (buf, &token, &err);
541 p_delete(&token.data);
545 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
546 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
548 s = imap_next_word (s);
549 s = imap_next_word (s);
551 /* zero out current rights set */
552 p_clear(idata->rights, 1);
554 while (*s && !isspace ((unsigned char) *s)) {
557 mutt_bit_set (idata->rights, ACL_LOOKUP);
560 mutt_bit_set (idata->rights, ACL_READ);
563 mutt_bit_set (idata->rights, ACL_SEEN);
566 mutt_bit_set (idata->rights, ACL_WRITE);
569 mutt_bit_set (idata->rights, ACL_INSERT);
572 mutt_bit_set (idata->rights, ACL_POST);
575 mutt_bit_set (idata->rights, ACL_CREATE);
578 mutt_bit_set (idata->rights, ACL_DELETE);
581 mutt_bit_set (idata->rights, ACL_ADMIN);