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;
89 if (idata->status == IMAP_FATAL) {
90 cmd_handle_fatal (idata);
94 buffer_reset(&cmd->buf);
95 if (mutt_socket_readln2(&cmd->buf, idata->conn) < 0) {
96 /* cmd_handle_fatal (idata); */
100 idata->lastread = time(NULL);
102 /* handle untagged messages. The caller still gets its shot afterwards. */
103 if (!m_strncmp(cmd->buf.data, "* ", 2) && cmd_handle_untagged (idata))
106 /* server demands a continuation response from us */
107 if (cmd->buf.data[0] == '+')
108 return IMAP_CMD_RESPOND;
110 /* tagged completion code */
111 if (!m_strncmp(cmd->buf.data, cmd->seq, SEQLEN)) {
112 imap_cmd_finish (idata);
113 return imap_code(cmd->buf.data) ? IMAP_CMD_OK : IMAP_CMD_NO;
116 return IMAP_CMD_CONTINUE;
119 /* imap_code: returns 1 if the command result was OK, or 0 if NO or BAD */
120 int imap_code (const char *s)
122 s = vskipspaces(s + SEQLEN);
123 return !ascii_strncasecmp("OK", s, 2);
126 /* imap_exec: execute a command, and wait for the response from the server.
127 * Also, handle untagged responses.
129 * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used
130 * for checking for a mailbox on append and login
131 * IMAP_CMD_PASS: command contains a password. Suppress logging.
132 * Return 0 on success, -1 on Failure, -2 on OK Failure
134 int imap_exec (IMAP_DATA * idata, const char *cmd, int flags)
141 mutt_error (_("No mailbox is open."));
146 if (idata->status == IMAP_FATAL) {
147 cmd_handle_fatal (idata);
151 /* create sequence for command */
152 cmd_make_sequence (idata);
153 /* seq, space, cmd, \r\n\0 */
154 outlen = m_strlen(idata->cmd.seq) + m_strlen(cmd) + 4;
155 out = p_new(char, outlen);
156 snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd);
158 rc = mutt_socket_write(idata->conn, out);
162 cmd_handle_fatal (idata);
167 rc = imap_cmd_step (idata);
168 while (rc == IMAP_CMD_CONTINUE);
170 if (rc == IMAP_CMD_BAD) {
171 if (imap_reconnect (idata->ctx) != 0) {
177 if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
180 if (rc != IMAP_CMD_OK) {
181 return (flags & IMAP_CMD_FAIL_OK) ? -2 : -1;
187 /* imap_cmd_finish: Attempts to perform cleanup (eg fetch new mail if
188 * detected, do expunge). Called automatically by imap_cmd_step, but
189 * may be called at any time. Called by imap_check_mailbox just before
190 * the index is refreshed, for instance. */
191 void imap_cmd_finish (IMAP_DATA * idata)
193 if (idata->status == IMAP_FATAL) {
194 cmd_handle_fatal (idata);
198 if (!(idata->state == IMAP_SELECTED) || idata->ctx->closing)
201 if (idata->reopen & IMAP_REOPEN_ALLOW) {
202 int count = idata->newMailCount;
204 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
205 (idata->reopen & IMAP_NEWMAIL_PENDING)
206 && count > idata->ctx->msgcount) {
207 /* read new mail messages */
208 /* check_status: curs_main uses imap_check_mailbox to detect
209 * whether the index needs updating */
210 idata->check_status = IMAP_NEWMAIL_PENDING;
211 imap_read_headers (idata, idata->ctx->msgcount, count - 1);
213 else if (idata->reopen & IMAP_EXPUNGE_PENDING) {
214 imap_expunge_mailbox (idata);
215 /* Detect whether we've gotten unexpected EXPUNGE messages */
216 if (idata->reopen & IMAP_EXPUNGE_PENDING &&
217 !(idata->reopen & IMAP_EXPUNGE_EXPECTED))
218 idata->check_status = IMAP_EXPUNGE_PENDING;
219 idata->reopen &= ~(IMAP_EXPUNGE_PENDING | IMAP_NEWMAIL_PENDING |
220 IMAP_EXPUNGE_EXPECTED);
227 /* cmd_handle_fatal: when IMAP_DATA is in fatal state, do what we can */
228 static void cmd_handle_fatal (IMAP_DATA * idata)
230 idata->status = IMAP_FATAL;
232 if ((idata->state == IMAP_SELECTED) &&
233 (idata->reopen & IMAP_REOPEN_ALLOW)) {
234 /* mx_fastclose_mailbox (idata->ctx); */
235 mutt_error (_("Mailbox closed"));
237 idata->state = IMAP_DISCONNECTED;
238 if (imap_reconnect (idata->ctx) != 0)
239 mx_fastclose_mailbox (idata->ctx);
242 if (idata->state != IMAP_SELECTED) {
243 idata->state = IMAP_DISCONNECTED;
244 mutt_socket_close (idata->conn);
249 /* cmd_handle_untagged: fallback parser for otherwise unhandled messages. */
250 static int cmd_handle_untagged (IMAP_DATA * idata)
256 s = imap_next_word (idata->cmd.buf.data);
258 if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) {
260 s = imap_next_word (s);
262 /* EXISTS and EXPUNGE are always related to the SELECTED mailbox for the
263 * connection, so update that one.
265 if (ascii_strncasecmp ("EXISTS", s, 6) == 0) {
266 /* new mail arrived */
269 if (!(idata->reopen & IMAP_EXPUNGE_PENDING) &&
270 count < idata->ctx->msgcount) {
271 /* something is wrong because the server reported fewer messages
272 * than we previously saw
274 mutt_error _("Fatal error. Message count is out of sync!");
276 idata->status = IMAP_FATAL;
279 /* at least the InterChange server sends EXISTS messages freely,
280 * even when there is no new mail */
281 else if (count == idata->ctx->msgcount)
284 if (!(idata->reopen & IMAP_EXPUNGE_PENDING)) {
285 idata->reopen |= IMAP_NEWMAIL_PENDING;
287 idata->newMailCount = count;
290 /* pn vs. s: need initial seqno */
291 else if (ascii_strncasecmp ("EXPUNGE", s, 7) == 0)
292 cmd_parse_expunge (idata, pn);
293 else if (ascii_strncasecmp ("FETCH", s, 5) == 0)
294 cmd_parse_fetch (idata, pn);
296 else if (ascii_strncasecmp ("CAPABILITY", s, 10) == 0)
297 cmd_parse_capabilities (idata, s);
298 else if (ascii_strncasecmp ("LSUB", s, 4) == 0)
299 cmd_parse_lsub (idata, s);
300 else if (ascii_strncasecmp ("MYRIGHTS", s, 8) == 0)
301 cmd_parse_myrights (idata, s);
302 else if (ascii_strncasecmp ("SEARCH", s, 6) == 0)
303 cmd_parse_search (idata, s);
304 else if (ascii_strncasecmp ("BYE", s, 3) == 0) {
305 /* check if we're logging out */
306 if (idata->status == IMAP_BYE)
309 /* server shut down our connection */
310 s = vskipspaces(s + 3);
311 mutt_error ("%s", s);
313 cmd_handle_fatal (idata);
316 else if (option (OPTIMAPSERVERNOISE)
317 && (ascii_strncasecmp ("NO", s, 2) == 0)) {
318 /* Display the warning message from the server */
319 mutt_error ("%s", s + 3);
326 /* This should be optimised (eg with a tree or hash) */
327 static int uid2msgno (IMAP_DATA* idata, int uid) {
330 for (i = 0; i < idata->ctx->msgcount; i++) {
331 HEADER* h = idata->ctx->hdrs[i];
332 if (HEADER_DATA(h)->uid == uid)
339 /* cmd_parse_search: store SEARCH response for later use */
340 static void cmd_parse_search (IMAP_DATA* idata, char* s) {
344 while ((s = imap_next_word (s)) && *s != '\0') {
346 msgno = uid2msgno (idata, uid);
349 idata->ctx->hdrs[uid2msgno (idata, uid)]->matched = 1;
353 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
354 static void cmd_make_sequence (IMAP_DATA * idata)
356 snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
358 if (idata->seqno > 9999)
362 /* cmd_parse_capabilities: set capability bits according to CAPABILITY
364 static void cmd_parse_capabilities (IMAP_DATA * idata, char *s)
368 s = imap_next_word (s);
369 p_delete(&idata->capstr);
370 idata->capstr = m_strdup(s);
372 p_clear(idata->capabilities, 1);
375 for (x = 0; x < CAPMAX; x++)
376 if (imap_wordcasecmp (Capabilities[x], s) == 0) {
377 mutt_bit_set (idata->capabilities, x);
380 s = imap_next_word (s);
384 /* cmd_parse_expunge: mark headers with new sequence ID and mark idata to
385 * be reopened at our earliest convenience */
386 static void cmd_parse_expunge (IMAP_DATA * idata, const char *s)
393 /* walk headers, zero seqno of expunged message, decrement seqno of those
394 * above. Possibly we could avoid walking the whole list by resorting
395 * and guessing a good starting point, but I'm guessing the resort would
396 * nullify the gains */
397 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
398 h = idata->ctx->hdrs[cur];
400 if (h->index + 1 == expno)
402 else if (h->index + 1 > expno)
406 idata->reopen |= IMAP_EXPUNGE_PENDING;
409 /* cmd_parse_fetch: Load fetch response into IMAP_DATA. Currently only
410 * handles unanticipated FETCH responses, and only FLAGS data. We get
411 * these if another client has changed flags for a mailbox we've selected.
412 * Of course, a lot of code here duplicates code in message.c. */
413 static void cmd_parse_fetch (IMAP_DATA * idata, char *s)
420 if (msgno <= idata->ctx->msgcount)
421 /* see cmd_parse_expunge */
422 for (cur = 0; cur < idata->ctx->msgcount; cur++) {
423 h = idata->ctx->hdrs[cur];
428 if (h->active && h->index + 1 == msgno) {
440 s = imap_next_word (s);
441 s = imap_next_word (s);
448 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
452 /* If server flags could conflict with mutt's flags, reopen the mailbox. */
454 idata->reopen |= IMAP_EXPUNGE_PENDING;
456 imap_set_flags (idata, h, s);
457 idata->check_status = IMAP_FLAGS_PENDING;
461 static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
466 if (!option (OPTIMAPCHECKSUBSCRIBED))
469 s = imap_next_word (s); /* flags */
477 for (ep = s; *ep && *ep != ')'; ep++);
479 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
481 while (s < ep && *s != ' ' && *s != ')')
487 s = imap_next_word (s); /* delim */
488 s = imap_next_word (s); /* name */
491 imap_unmunge_mbox_name (s);
493 mutt_account_tourl(&idata->conn->account, &url);
495 if (!m_strcmp(url.user, ImapUser))
497 url_ciss_tostring(&url, buf, sizeof(buf), 0);
498 buffy_do_mailboxes(buf, 1);
502 /* cmd_parse_myrights: set rights bits according to MYRIGHTS response */
503 static void cmd_parse_myrights (IMAP_DATA * idata, char *s)
505 s = imap_next_word (s);
506 s = imap_next_word (s);
508 /* zero out current rights set */
509 p_clear(idata->rights, 1);
511 while (*s && !isspace ((unsigned char) *s)) {
514 mutt_bit_set (idata->rights, ACL_LOOKUP);
517 mutt_bit_set (idata->rights, ACL_READ);
520 mutt_bit_set (idata->rights, ACL_SEEN);
523 mutt_bit_set (idata->rights, ACL_WRITE);
526 mutt_bit_set (idata->rights, ACL_INSERT);
529 mutt_bit_set (idata->rights, ACL_POST);
532 mutt_bit_set (idata->rights, ACL_CREATE);
535 mutt_bit_set (idata->rights, ACL_DELETE);
538 mutt_bit_set (idata->rights, ACL_ADMIN);