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>
16 #include <lib-mx/mx.h>
21 #include "imap_private.h"
26 #define IMAP_CMD_BUFSIZE 512
28 /* forward declarations */
29 static void cmd_handle_fatal (IMAP_DATA * idata);
30 static int cmd_handle_untagged (IMAP_DATA * idata);
31 static void cmd_make_sequence (IMAP_DATA * idata);
32 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s);
33 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s);
34 static void cmd_parse_lsub (IMAP_DATA* idata, char* s);
35 static void cmd_parse_fetch (IMAP_DATA * idata, char *s);
36 static void cmd_parse_myrights (IMAP_DATA * idata, char *s);
37 static void cmd_parse_search (IMAP_DATA* idata, char* s);
39 static const char *Capabilities[] = {
54 /* imap_cmd_start: Given an IMAP command, send it to the server.
55 * Currently a minor convenience, but helps to route all IMAP commands
56 * through a single interface. */
57 int imap_cmd_start (IMAP_DATA * idata, const char *cmd)
63 if (idata->status == IMAP_FATAL) {
64 cmd_handle_fatal (idata);
68 cmd_make_sequence (idata);
69 /* seq, space, cmd, \r\n\0 */
70 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
71 out = p_new(char, outlen);
72 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
74 rc = mutt_socket_write (idata->conn, out);
78 return (rc < 0) ? IMAP_CMD_BAD : 0;
81 /* imap_cmd_step: Reads server responses from an IMAP command, detects
82 * tagged completion response, handles untagged messages, can read
83 * arbitrarily large strings (using malloc, so don't make it _too_
85 int imap_cmd_step (IMAP_DATA * idata)
87 IMAP_COMMAND *cmd = &idata->cmd;
91 if (idata->status == IMAP_FATAL) {
92 cmd_handle_fatal (idata);
96 /* read into buffer, expanding buffer as necessary until we have a full
99 if (len == cmd->blen) {
100 p_realloc(&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE);
101 cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE;
107 c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn);
109 /* cmd_handle_fatal (idata); */
115 /* if we've read all the way to the end of the buffer, we haven't read a
116 * full line (mutt_socket_readln strips the \r, so we always have at least
117 * one character free when we've read a full line) */
118 while (len == cmd->blen);
120 /* don't let one large string make cmd->buf hog memory forever */
121 if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) {
122 p_realloc(&cmd->buf, IMAP_CMD_BUFSIZE);
123 cmd->blen = IMAP_CMD_BUFSIZE;
126 idata->lastread = time (NULL);
128 /* handle untagged messages. The caller still gets its shot afterwards. */
129 if (!m_strncmp(cmd->buf, "* ", 2) && cmd_handle_untagged (idata))
132 /* server demands a continuation response from us */
133 if (cmd->buf[0] == '+')
134 return IMAP_CMD_RESPOND;
136 /* tagged completion code */
137 if (!m_strncmp(cmd->buf, cmd->seq, SEQLEN)) {
138 imap_cmd_finish (idata);
139 return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO;
142 return IMAP_CMD_CONTINUE;
145 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
146 int imap_code (const char *s)
148 s = vskipspaces(s + SEQLEN);
149 return !ascii_strncasecmp("OK", s, 2);
152 /* imap_exec: execute a command, and wait for the response from the server.
153 * Also, handle untagged responses.
155 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
156 * for checking for a mailbox on append and login
157 * IMAP_CMD_PASS: command contains a password. Suppress logging.
158 * Return 0 on success, -1 on Failure, -2 on OK Failure
160 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
167 mutt_error (_("No mailbox is open."));
172 if (idata->status == IMAP_FATAL) {
173 cmd_handle_fatal (idata);
177 /* create sequence for command */
178 cmd_make_sequence (idata);
179 /* seq, space, cmd, \r\n\0 */
180 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
181 out = p_new(char, outlen);
182 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
184 rc = mutt_socket_write(idata->conn, out);
188 cmd_handle_fatal (idata);
193 rc = imap_cmd_step (idata);
194 while (rc == IMAP_CMD_CONTINUE);
196 if (rc == IMAP_CMD_BAD) {
197 if (imap_reconnect (idata->ctx) != 0) {
203 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
206 if (rc != IMAP_CMD_OK) {
207 return (flags & IMAP_CMD_FAIL_OK) ? -2 : -1;
213 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
214 * detected, do expunge). Called automatically by imap_cmd_step, but
215 * may be called at any time. Called by imap_check_mailbox just before
216 * the index is refreshed, for instance. */
217 void imap_cmd_finish (IMAP_DATA * idata)
219 if (idata->status == IMAP_FATAL) {
220 cmd_handle_fatal (idata);
224 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
227 if (idata->reopen & IMAP_REOPEN_ALLOW) {
228 int count = idata->newMailCount;
230 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
231 (idata->reopen & IMAP_NEWMAIL_PENDING)
232 && count > idata->ctx->msgcount) {
233 /* read new mail messages */
234 /* check_status: curs_main uses imap_check_mailbox to detect
235 * whether the index needs updating */
236 idata->check_status = IMAP_NEWMAIL_PENDING;
237 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
239 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
240 imap_expunge_mailbox (idata);
241 /* Detect whether we've gotten unexpected EXPUNGE messages */
242 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
243 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
244 idata->check_status = IMAP_EXPUNGE_PENDING;
245 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
246 IMAP_EXPUNGE_EXPECTED);
253 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
254 static void cmd_handle_fatal (IMAP_DATA * idata)
256 idata->status = IMAP_FATAL;
258 if ((idata->state == IMAP_SELECTED) &&
259 (idata->reopen & IMAP_REOPEN_ALLOW)) {
260 /* mx_fastclose_mailbox (idata->ctx); */
261 mutt_error (_("Mailbox closed"));
263 idata->state = IMAP_DISCONNECTED;
264 if (imap_reconnect (idata->ctx) != 0)
265 mx_fastclose_mailbox (idata->ctx);
268 if (idata->state != IMAP_SELECTED) {
269 idata->state = IMAP_DISCONNECTED;
270 mutt_socket_close (idata->conn);
275 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
276 static int cmd_handle_untagged (IMAP_DATA * idata)
282 s = imap_next_word (idata->cmd.buf);
284 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
286 s = imap_next_word (s);
288 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
289 * connection, so update that one.
291 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
292 /* new mail arrived */
295 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
296 count < idata->ctx->msgcount) {
297 /* something is wrong because the server reported fewer messages
298 * than we previously saw
300 mutt_error _("Fatal error. Message count is out of sync!");
302 idata->status = IMAP_FATAL;
305 /* at least the InterChange server sends EXISTS messages freely,
306 * even when there is no new mail */
307 else if (count == idata->ctx->msgcount)
310 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
311 idata->reopen |= IMAP_NEWMAIL_PENDING;
313 idata->newMailCount = count;
316 /* pn vs. s: need initial seqno */
317 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
318 cmd_parse_expunge (idata, pn);
319 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
320 cmd_parse_fetch (idata, pn);
322 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
323 cmd_parse_capabilities (idata, s);
324 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
325 cmd_parse_lsub (idata, s);
326 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
327 cmd_parse_myrights (idata, s);
328 else if (ascii_strncasecmp ("SEARCH", s, 6) == 0)
329 cmd_parse_search (idata, s);
330 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
331 /* check if we're logging out */
332 if (idata->status == IMAP_BYE)
335 /* server shut down our connection */
336 s = vskipspaces(s + 3);
337 mutt_error ("%s", s);
339 cmd_handle_fatal (idata);
342 else if (option (OPTIMAPSERVERNOISE)
343 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
344 /* Display the warning message from the server */
345 mutt_error ("%s", s + 3);
352 /* This should be optimised (eg with a tree or hash) */
353 static int uid2msgno (IMAP_DATA* idata, int uid) {
356 for (i = 0; i < idata->ctx->msgcount; i++) {
357 HEADER* h = idata->ctx->hdrs[i];
358 if (HEADER_DATA(h)->uid == uid)
365 /* cmd_parse_search: store SEARCH response for later use */
366 static void cmd_parse_search (IMAP_DATA* idata, char* s) {
370 while ((s = imap_next_word (s)) && *s != '\0') {
372 msgno = uid2msgno (idata, uid);
375 idata->ctx->hdrs[uid2msgno (idata, uid)]->matched = 1;
379 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
380 static void cmd_make_sequence (IMAP_DATA * idata)
382 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
384 if (idata->seqno > 9999)
388 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
390 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
394 s = imap_next_word (s);
395 p_delete(&idata->capstr);
396 idata->capstr = m_strdup(s);
398 p_clear(idata->capabilities, 1);
401 for (x = 0; x < CAPMAX; x++)
402 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
403 mutt_bit_set (idata->capabilities, x);
406 s = imap_next_word (s);
410 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
411 * be reopened at our earliest convenience */
412 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
419 /* walk headers, zero seqno of expunged message, decrement seqno of those
420 * above. Possibly we could avoid walking the whole list by resorting
421 * and guessing a good starting point, but I'm guessing the resort would
422 * nullify the gains */
423 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
424 h = idata->ctx->hdrs[cur];
426 if (h->index + 1 == expno)
428 else if (h->index + 1 > expno)
432 idata->reopen |= IMAP_EXPUNGE_PENDING;
435 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
436 * handles unanticipated FETCH responses, and only FLAGS data. We get
437 * these if another client has changed flags for a mailbox we've selected.
438 * Of course, a lot of code here duplicates code in message.c. */
439 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
446 if (msgno <= idata->ctx->msgcount)
447 /* see cmd_parse_expunge */
448 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
449 h = idata->ctx->hdrs[cur];
454 if (h->active && h->index + 1 == msgno) {
466 s = imap_next_word (s);
467 s = imap_next_word (s);
474 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
478 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
480 idata->reopen |= IMAP_EXPUNGE_PENDING;
482 imap_set_flags (idata, h, s);
483 idata->check_status = IMAP_FLAGS_PENDING;
487 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
492 if (!option (OPTIMAPCHECKSUBSCRIBED))
495 s = imap_next_word (s); /* flags */
503 for (ep = s; *ep && *ep != ')'; ep++);
505 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
507 while (s < ep && *s != ' ' && *s != ')')
513 s = imap_next_word (s); /* delim */
514 s = imap_next_word (s); /* name */
517 imap_unmunge_mbox_name (s);
519 mutt_account_tourl(&idata->conn->account, &url);
521 if (!m_strcmp(url.user, ImapUser))
523 url_ciss_tostring(&url, buf, sizeof(buf), 0);
524 buffy_do_mailboxes(buf, 1);
528 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
529 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
531 s = imap_next_word (s);
532 s = imap_next_word (s);
534 /* zero out current rights set */
535 p_clear(idata->rights, 1);
537 while (*s && !isspace ((unsigned char) *s)) {
540 mutt_bit_set (idata->rights, ACL_LOOKUP);
543 mutt_bit_set (idata->rights, ACL_READ);
546 mutt_bit_set (idata->rights, ACL_SEEN);
549 mutt_bit_set (idata->rights, ACL_WRITE);
552 mutt_bit_set (idata->rights, ACL_INSERT);
555 mutt_bit_set (idata->rights, ACL_POST);
558 mutt_bit_set (idata->rights, ACL_CREATE);
561 mutt_bit_set (idata->rights, ACL_DELETE);
564 mutt_bit_set (idata->rights, ACL_ADMIN);