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 /* Support for IMAP4rev1, with the occasional nod to IMAP 4. */
14 #include <lib-lib/lib-lib.h>
15 #include <lib-mx/mx.h>
22 #include "imap_private.h"
25 /* imap forward declarations */
26 static int imap_get_delim (IMAP_DATA * idata);
27 static char *imap_get_flags (string_list_t ** hflags, char *s);
28 static int imap_check_acl (IMAP_DATA * idata);
29 static int imap_check_capabilities (IMAP_DATA * idata);
30 static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
31 const char *str, char *flags, size_t flsize);
33 /* imap_access: Check permissions on an IMAP mailbox.
34 * TODO: ACL checks. Right now we assume if it exists we can
36 static int imap_access (const char *path, int flags __attribute__ ((unused)))
40 char buf[LONG_STRING];
41 char mailbox[LONG_STRING];
42 char mbox[LONG_STRING];
44 if (imap_parse_path (path, &mx))
47 if (!(idata = imap_conn_find (&mx.account,
48 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW :
54 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
56 /* we may already be in the folder we're checking */
57 if (!m_strcmp(idata->mailbox, mx.mbox)) {
63 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
65 if (mutt_bit_isset (idata->capabilities, IMAP4REV1))
66 snprintf (buf, sizeof (buf), "STATUS %s (UIDVALIDITY)", mbox);
67 else if (mutt_bit_isset (idata->capabilities, STATUS))
68 snprintf (buf, sizeof (buf), "STATUS %s (UID-VALIDITY)", mbox);
73 if (imap_exec (idata, buf, IMAP_CMD_FAIL_OK) < 0) {
80 int imap_create_mailbox (IMAP_DATA * idata, char *mailbox)
82 char buf[LONG_STRING], mbox[LONG_STRING];
84 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
85 snprintf (buf, sizeof (buf), "CREATE %s", mbox);
87 if (imap_exec (idata, buf, 0) != 0)
93 int imap_rename_mailbox (IMAP_DATA * idata, IMAP_MBOX * mx,
96 char oldmbox[LONG_STRING];
97 char newmbox[LONG_STRING];
98 char buf[LONG_STRING];
100 imap_munge_mbox_name (oldmbox, sizeof (oldmbox), mx->mbox);
101 imap_munge_mbox_name (newmbox, sizeof (newmbox), newname);
103 snprintf (buf, sizeof (buf), "RENAME %s %s", oldmbox, newmbox);
105 if (imap_exec (idata, buf, 0) != 0)
111 int imap_delete_mailbox (CONTEXT * ctx, IMAP_MBOX mx)
113 char buf[LONG_STRING], mbox[LONG_STRING];
116 if (!ctx || !ctx->data) {
117 if (!(idata = imap_conn_find (&mx.account,
118 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW
128 imap_munge_mbox_name (mbox, sizeof (mbox), mx.mbox);
129 snprintf (buf, sizeof (buf), "DELETE %s", mbox);
131 if (imap_exec ((IMAP_DATA *) idata, buf, 0) != 0)
137 /* imap_logout_all: close all open connections. Quick and dirty until we can
138 * make sure we've got all the context we need. */
139 void imap_logout_all (void)
144 conn = mutt_socket_head ();
149 if (conn->account.type == M_ACCT_TYPE_IMAP && conn->fd >= 0) {
150 mutt_message (_("Closing connection to %s..."), conn->account.host);
151 imap_logout ((IMAP_DATA *) conn->data);
153 mutt_socket_close (conn);
154 mutt_socket_free (conn);
161 /* imap_read_literal: read bytes bytes from server into file. Not explicitly
162 * buffered, relies on FILE buffering. NOTE: strips \r from \r\n.
163 * Apparently even literals use \r\n-terminated strings ?! */
164 int imap_read_literal (FILE * fp, IMAP_DATA * idata, long bytes, progress_t* bar)
171 for (pos = 0; pos < bytes; pos++) {
172 if (mutt_socket_readchar (idata->conn, &c) != 1) {
173 idata->status = IMAP_FATAL;
178 if (r == 1 && c != '\n')
189 if (bar && !(pos % 1024))
190 mutt_progress_bar (bar, pos);
196 /* imap_expunge_mailbox: Purge IMAP portion of expunged messages from the
197 * context. Must not be done while something has a handle on any headers
198 * (eg inside pager or editor). That is, check IMAP_REOPEN_ALLOW. */
199 void imap_expunge_mailbox (IMAP_DATA * idata)
204 for (i = 0; i < idata->ctx->msgcount; i++) {
205 h = idata->ctx->hdrs[i];
207 if (h->index == -1) {
210 /* free cached body from disk, if neccessary */
211 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
212 if (idata->cache[cacheno].uid == HEADER_DATA (h)->uid &&
213 idata->cache[cacheno].path) {
214 unlink (idata->cache[cacheno].path);
215 p_delete(&idata->cache[cacheno].path);
218 imap_free_header_data (&h->data);
222 /* We may be called on to expunge at any time. We can't rely on the caller
223 * to always know to rethread */
224 mx_update_tables (idata->ctx, 0);
225 mutt_sort_headers (idata->ctx, 1);
228 static int imap_get_delim (IMAP_DATA * idata)
233 /* assume that the delim is /. If this fails, we're in bigger trouble
234 * than getting the delim wrong */
237 imap_cmd_start (idata, "LIST \"\" \"\"");
240 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
243 s = imap_next_word (idata->cmd.buf.data);
244 if (ascii_strncasecmp ("LIST", s, 4) == 0) {
245 s = imap_next_word (s);
246 s = imap_next_word (s);
247 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
249 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2]
254 while (rc == IMAP_CMD_CONTINUE);
259 /* get rights for folder, let imap_handle_untagged do the rest */
260 static int imap_check_acl (IMAP_DATA * idata)
262 char buf[LONG_STRING];
263 char mbox[LONG_STRING];
265 imap_munge_mbox_name (mbox, sizeof (mbox), idata->mailbox);
266 snprintf (buf, sizeof (buf), "MYRIGHTS %s", mbox);
267 if (imap_exec (idata, buf, 0) != 0) {
268 imap_error ("imap_check_acl", buf);
274 /* imap_check_capabilities: make sure we can log in to this server. */
275 static int imap_check_capabilities (IMAP_DATA * idata)
277 if (imap_exec (idata, "CAPABILITY", 0) != 0) {
278 imap_error ("imap_check_capabilities", idata->cmd.buf.data);
282 if (!(mutt_bit_isset (idata->capabilities, IMAP4)
283 || mutt_bit_isset (idata->capabilities, IMAP4REV1))) {
284 mutt_error _("This IMAP server is ancient. Mutt does not work with it.");
286 mutt_sleep (2); /* pause a moment to let the user see the error */
294 /* imap_conn_find: Find an open IMAP connection matching account, or open
295 * a new one if none can be found. */
296 IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags)
302 if (!(conn = mutt_conn_find (NULL, account)))
305 /* if opening a new UNSELECTED connection, preserve existing creds */
306 creds = &(conn->account);
308 /* make sure this connection is not in SELECTED state, if neccessary */
309 if (flags & M_IMAP_CONN_NOSELECT)
310 while (conn->data && ((IMAP_DATA *) conn->data)->state == IMAP_SELECTED) {
311 if (!(conn = mutt_conn_find (conn, account)))
313 memcpy (&(conn->account), creds, sizeof (ACCOUNT));
316 idata = (IMAP_DATA *) conn->data;
318 /* don't open a new connection if one isn't wanted */
319 if (flags & M_IMAP_CONN_NONEW) {
321 mutt_socket_free (conn);
324 if (idata->state < IMAP_AUTHENTICATED)
329 /* The current connection is a new connection */
330 idata = imap_new_idata();
335 if (idata->state == IMAP_DISCONNECTED)
336 imap_open_connection (idata);
337 if (idata->state == IMAP_CONNECTED) {
338 if (!imap_authenticate(idata)) {
339 idata->state = IMAP_AUTHENTICATED;
341 mutt_socket_close(idata->conn);
342 idata->state = IMAP_DISCONNECTED;
343 idata->conn->account.has_pass = 0;
346 p_delete(&idata->capstr);
348 if (idata->isnew && idata->state == IMAP_AUTHENTICATED) {
349 imap_get_delim (idata);
350 if (option (OPTIMAPCHECKSUBSCRIBED)) {
351 mutt_message _("Checking mailbox subscriptions");
352 imap_exec (idata, "LSUB \"\" \"*\"", 0);
360 int imap_open_connection (IMAP_DATA * idata)
362 char buf[LONG_STRING];
364 if (mutt_socket_open (idata->conn) < 0)
367 idata->state = IMAP_CONNECTED;
369 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE) {
370 mutt_socket_close (idata->conn);
371 idata->state = IMAP_DISCONNECTED;
375 if (ascii_strncasecmp ("* OK", idata->cmd.buf.data, 4) == 0) {
376 /* TODO: Parse new tagged CAPABILITY data (* OK [CAPABILITY...]) */
377 if (imap_check_capabilities (idata))
379 /* Attempt STARTTLS if available and desired. */
380 if (!idata->conn->ssf && (mod_ssl.force_tls ||
381 mutt_bit_isset (idata->capabilities, STARTTLS))) {
384 if (mod_ssl.force_tls)
387 if ((rc = imap_exec (idata, "STARTTLS", IMAP_CMD_FAIL_OK)) == -1)
390 if (mutt_ssl_starttls (idata->conn)) {
391 mutt_error (_("Could not negotiate TLS connection"));
395 /* RFC 2595 demands we recheck CAPABILITY after TLS completes. */
396 if (imap_exec (idata, "CAPABILITY", 0))
403 if (mod_ssl.force_tls && ! idata->conn->ssf) {
404 mutt_error _("Encrypted connection unavailable");
409 else if (ascii_strncasecmp ("* PREAUTH", idata->cmd.buf.data, 9) == 0) {
410 idata->state = IMAP_AUTHENTICATED;
411 if (imap_check_capabilities (idata) != 0)
413 p_delete(&idata->capstr);
416 imap_error ("imap_open_connection()", buf);
423 mutt_socket_close (idata->conn);
424 idata->state = IMAP_DISCONNECTED;
426 p_delete(&idata->capstr);
430 /* imap_get_flags: Make a simple list out of a FLAGS response.
431 * return stream following FLAGS response */
432 static char *imap_get_flags (string_list_t ** hflags, char *s)
434 string_list_t *flags;
438 /* sanity-check string */
439 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
442 s = vskipspaces(s + 5);
447 /* create list, update caller's flags handle */
448 flags = string_item_new();
451 while (*s && *s != ')') {
452 s = vskipspaces(s + 1);
454 while (*s && (*s != ')') && !ISSPACE (*s))
459 mutt_add_list (flags, flag_word);
463 /* note bad flags response */
465 string_list_wipe(hflags);
475 static int imap_open_mailbox (CONTEXT * ctx)
479 char buf[LONG_STRING];
480 char bufout[LONG_STRING];
485 if (imap_parse_path (ctx->path, &mx)) {
486 mutt_error (_("%s is an invalid IMAP path"), ctx->path);
490 /* we require a connection which isn't currently in IMAP_SELECTED state */
491 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NOSELECT)))
493 if (idata->state < IMAP_AUTHENTICATED)
498 /* once again the context is new */
501 /* Clean up path and replace the one in the ctx */
502 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
503 p_delete(&(idata->mailbox));
504 idata->mailbox = m_strdup(buf);
505 imap_qualify_path (buf, sizeof (buf), &mx, idata->mailbox);
507 p_delete(&(ctx->path));
508 ctx->path = m_strdup(buf);
512 /* clear mailbox status */
514 memset (idata->rights, 0, (RIGHTSMAX + 7) / 8);
515 idata->newMailCount = 0;
517 mutt_message (_("Selecting %s..."), idata->mailbox);
518 imap_munge_mbox_name (buf, sizeof (buf), idata->mailbox);
519 snprintf (bufout, sizeof (bufout), "%s %s",
520 ctx->readonly ? "EXAMINE" : "SELECT", buf);
522 idata->state = IMAP_SELECTED;
524 imap_cmd_start (idata, bufout);
529 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
532 pc = idata->cmd.buf.data + 2;
534 /* Obtain list of available flags here, may be overridden by a
535 * PERMANENTFLAGS tag in the OK response */
536 if (ascii_strncasecmp ("FLAGS", pc, 5) == 0) {
537 /* don't override PERMANENTFLAGS */
539 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
543 /* PERMANENTFLAGS are massaged to look like FLAGS, then override FLAGS */
544 else if (ascii_strncasecmp ("OK [PERMANENTFLAGS", pc, 18) == 0) {
545 /* safe to call on NULL */
546 string_list_wipe(&(idata->flags));
547 /* skip "OK [PERMANENT" so syntax is the same as FLAGS */
549 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
553 /* save UIDVALIDITY for the header cache */
554 else if (ascii_strncasecmp ("OK [UIDVALIDITY", pc, 14) == 0) {
556 pc = imap_next_word (pc);
558 sscanf (pc, "%lu", &(idata->uid_validity));
562 pc = imap_next_word (pc);
563 if (!ascii_strncasecmp ("EXISTS", pc, 6)) {
564 count = idata->newMailCount;
565 idata->newMailCount = 0;
569 while (rc == IMAP_CMD_CONTINUE);
571 if (rc == IMAP_CMD_NO) {
574 s = imap_next_word (idata->cmd.buf.data); /* skip seq */
575 s = imap_next_word (s); /* Skip response */
576 mutt_error ("%s", s);
581 if (rc != IMAP_CMD_OK)
584 /* check for READ-ONLY notification */
585 if (!ascii_strncasecmp
586 (imap_get_qualifier (idata->cmd.buf.data), "[READ-ONLY]", 11)
587 && !mutt_bit_isset (idata->capabilities, ACL)) {
591 if (mutt_bit_isset (idata->capabilities, ACL)) {
592 if (imap_check_acl (idata))
594 if (!(mutt_bit_isset (idata->rights, ACL_DELETE) ||
595 mutt_bit_isset (idata->rights, ACL_SEEN) ||
596 mutt_bit_isset (idata->rights, ACL_WRITE) ||
597 mutt_bit_isset (idata->rights, ACL_INSERT)))
600 /* assume we have all rights if ACL is unavailable */
602 mutt_bit_set (idata->rights, ACL_LOOKUP);
603 mutt_bit_set (idata->rights, ACL_READ);
604 mutt_bit_set (idata->rights, ACL_SEEN);
605 mutt_bit_set (idata->rights, ACL_WRITE);
606 mutt_bit_set (idata->rights, ACL_INSERT);
607 mutt_bit_set (idata->rights, ACL_POST);
608 mutt_bit_set (idata->rights, ACL_CREATE);
609 mutt_bit_set (idata->rights, ACL_DELETE);
613 ctx->hdrs = p_new(HEADER *, count);
614 ctx->v2r = p_new(int, count);
616 if (count && (imap_read_headers (idata, 0, count - 1) < 0)) {
617 mutt_error _("Error opening mailbox");
627 if (idata->state == IMAP_SELECTED)
628 idata->state = IMAP_AUTHENTICATED;
634 int imap_open_mailbox_append (CONTEXT * ctx)
638 char buf[LONG_STRING];
639 char mailbox[LONG_STRING];
642 if (imap_parse_path (ctx->path, &mx))
645 /* in APPEND mode, we appear to hijack an existing IMAP connection -
646 * ctx is brand new and mostly empty */
648 if (!(idata = imap_conn_find (&(mx.account), 0))) {
657 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
661 /* really we should also check for W_OK */
662 if (!imap_access (ctx->path, F_OK))
665 snprintf (buf, sizeof (buf), _("Create %s?"), mailbox);
666 if (option (OPTCONFIRMCREATE) && mutt_yesorno (buf, 1) < 1)
669 if (imap_create_mailbox (idata, mailbox) < 0)
675 /* imap_logout: Gracefully log out of server. */
676 void imap_logout (IMAP_DATA * idata)
678 /* we set status here to let imap_handle_untagged know we _expect_ to
679 * receive a bye response (so it doesn't freak out and close the conn) */
680 idata->status = IMAP_BYE;
681 imap_cmd_start (idata, "LOGOUT");
682 while (imap_cmd_step (idata) == IMAP_CMD_CONTINUE);
683 imap_free_idata(&idata);
686 /* imap_set_flag: append str to flags if we currently have permission
687 * according to aclbit */
688 static void imap_set_flag(IMAP_DATA *idata, int aclbit, int flag,
689 const char *str, char *flags, size_t flsize)
691 if (mutt_bit_isset(idata->rights, aclbit)) {
693 m_strcat(flags, flsize, str);
697 /* imap_make_msg_set: make an IMAP4rev1 UID message set out of a set of
698 * headers, given a flag enum to filter on.
699 * Params: idata: IMAP_DATA containing context containing header set
700 * buf: to write message set into
701 * buflen: length of buffer
702 * flag: enum of flag type on which to filter
703 * changed: include only changed messages in message set
704 * Returns: number of messages in message set (0 if no matches) */
705 int imap_make_msg_set (IMAP_DATA * idata, BUFFER * buf, int flag, int changed)
707 HEADER **hdrs; /* sorted local copy */
708 int count = 0; /* number of messages in message set */
709 int match = 0; /* whether current message matches flag condition */
710 int setstart = 0; /* start of current message range */
712 short oldsort; /* we clobber reverse, must restore it */
714 /* assuming 32-bit UIDs */
718 /* make copy of header pointers to sort in natural order */
719 hdrs = p_dup(idata->ctx->hdrs, idata->ctx->msgcount);
721 if (Sort != SORT_ORDER) {
724 qsort ((void *) hdrs, idata->ctx->msgcount, sizeof (HEADER *),
725 mutt_get_sort_func (SORT_ORDER));
729 for (n = 0; n < idata->ctx->msgcount; n++) {
731 /* don't include pending expunged messages */
735 if (hdrs[n]->deleted)
744 if (match && (!changed || hdrs[n]->changed)) {
747 setstart = HEADER_DATA (hdrs[n])->uid;
749 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (hdrs[n])->uid);
750 mutt_buffer_addstr (buf, uid);
754 snprintf (uid, sizeof (uid), ",%u", HEADER_DATA (hdrs[n])->uid);
755 mutt_buffer_addstr (buf, uid);
758 /* tie up if the last message also matches */
759 else if (n == idata->ctx->msgcount - 1) {
760 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n])->uid);
761 mutt_buffer_addstr (buf, uid);
764 /* this message is not expunged and doesn't match. End current set. */
765 else if (setstart && hdrs[n]->active) {
766 if (HEADER_DATA (hdrs[n - 1])->uid > setstart) {
767 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n - 1])->uid);
768 mutt_buffer_addstr (buf, uid);
779 /* Update the IMAP server to reflect the flags a single message. */
781 int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd,
784 char flags[LONG_STRING];
789 snprintf (uid, sizeof (uid), "%u", HEADER_DATA(hdr)->uid);
790 cmd->dptr = cmd->data;
791 mutt_buffer_addstr (cmd, "UID STORE ");
792 mutt_buffer_addstr (cmd, uid);
796 imap_set_flag (idata, ACL_SEEN, hdr->read, "\\Seen ",
797 flags, sizeof (flags));
798 imap_set_flag (idata, ACL_WRITE, hdr->flagged,
799 "\\Flagged ", flags, sizeof (flags));
800 imap_set_flag (idata, ACL_WRITE, hdr->replied,
801 "\\Answered ", flags, sizeof (flags));
802 imap_set_flag (idata, ACL_DELETE, hdr->deleted,
803 "\\Deleted ", flags, sizeof (flags));
805 /* now make sure we don't lose custom tags */
806 if (mutt_bit_isset (idata->rights, ACL_WRITE))
807 imap_add_keywords (flags, hdr, idata->flags, sizeof (flags));
811 /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
812 * explicitly revoke all system flags (if we have permission) */
815 imap_set_flag (idata, ACL_SEEN, 1, "\\Seen ", flags, sizeof (flags));
816 imap_set_flag (idata, ACL_WRITE, 1, "\\Flagged ", flags, sizeof (flags));
817 imap_set_flag (idata, ACL_WRITE, 1, "\\Answered ", flags, sizeof (flags));
818 imap_set_flag (idata, ACL_DELETE, 1, "\\Deleted ", flags, sizeof (flags));
822 mutt_buffer_addstr (cmd, " -FLAGS.SILENT (");
824 mutt_buffer_addstr (cmd, " FLAGS.SILENT (");
826 mutt_buffer_addstr (cmd, flags);
827 mutt_buffer_addstr (cmd, ")");
829 /* dumb hack for bad UW-IMAP 4.7 servers spurious FLAGS updates */
832 /* after all this it's still possible to have no flags, if you
833 * have no ACL rights */
834 if (*flags && (imap_exec (idata, cmd->data, 0) != 0) &&
835 err_continue && (*err_continue != M_YES))
837 *err_continue = imap_continue ("imap_sync_message: STORE failed",
838 idata->cmd.buf.data);
839 if (*err_continue != M_YES)
844 idata->ctx->changed--;
849 /* update the IMAP server to reflect message changes done within mutt.
851 * ctx: the current context
852 * expunge: 0 or 1 - do expunge?
855 int imap_sync_mailbox (CONTEXT * ctx, int expunge, int *index_hint)
858 CONTEXT *appendctx = NULL;
862 int err_continue = M_NO; /* continue on error? */
865 idata = (IMAP_DATA *) ctx->data;
867 if (idata->state != IMAP_SELECTED) {
871 /* This function is only called when the calling code expects the context
873 imap_allow_reopen (ctx);
875 if ((rc = imap_check_mailbox (ctx, index_hint, 0)) != 0)
880 /* if we are expunging anyway, we can do deleted messages very quickly... */
881 if (expunge && mutt_bit_isset (idata->rights, ACL_DELETE)) {
882 mutt_buffer_addstr (&cmd, "UID STORE ");
883 deleted = imap_make_msg_set (idata, &cmd, M_DELETE, 1);
885 /* if we have a message set, then let's delete */
887 mutt_message (_("Marking %d messages deleted..."), deleted);
888 mutt_buffer_addstr (&cmd, " +FLAGS.SILENT (\\Deleted)");
889 /* mark these messages as unchanged so second pass ignores them. Done
890 * here so BOGUS UW-IMAP 4.7 SILENT FLAGS updates are ignored. */
891 for (n = 0; n < ctx->msgcount; n++)
892 if (ctx->hdrs[n]->deleted && ctx->hdrs[n]->changed)
893 ctx->hdrs[n]->active = 0;
894 if (imap_exec (idata, cmd.data, 0) != 0) {
895 mutt_error (_("Expunge failed"));
903 /* save status changes */
904 for (n = 0; n < ctx->msgcount; n++) {
905 if (ctx->hdrs[n]->active && ctx->hdrs[n]->changed) {
907 mutt_message (_("Saving message status flags... [%d/%d]"), n + 1,
910 /* if the message has been rethreaded or attachments have been deleted
911 * we delete the message and reupload it.
912 * This works better if we're expunging, of course. */
913 if ((ctx->hdrs[n]->env && (ctx->hdrs[n]->env->refs_changed || ctx->hdrs[n]->env->irt_changed)) ||
914 ctx->hdrs[n]->attach_del) {
916 appendctx = mx_open_mailbox (ctx->path, M_APPEND | M_QUIET, NULL);
918 _mutt_save_message (ctx->hdrs[n], appendctx, 1, 0, 0);
922 if (imap_sync_message (idata, ctx->hdrs[n], &cmd, &err_continue) < 0) {
930 /* We must send an EXPUNGE command if we're not closing. */
931 if (expunge && !(ctx->closing) &&
932 mutt_bit_isset (idata->rights, ACL_DELETE)) {
933 mutt_message _("Expunging messages from server...");
935 /* Set expunge bit so we don't get spurious reopened messages */
936 idata->reopen |= IMAP_EXPUNGE_EXPECTED;
937 if (imap_exec (idata, "EXPUNGE", 0) != 0) {
938 imap_error (_("imap_sync_mailbox: EXPUNGE failed"), idata->cmd.buf.data);
939 rc = imap_reconnect (ctx);
944 if (expunge && ctx->closing) {
945 if (imap_exec (idata, "CLOSE", 0))
946 mutt_error (_("CLOSE failed"));
947 idata->state = IMAP_AUTHENTICATED;
955 mx_fastclose_mailbox (appendctx);
956 p_delete(&appendctx);
961 /* imap_close_mailbox: clean up IMAP data in CONTEXT */
962 static void imap_close_mailbox (CONTEXT * ctx)
967 idata = (IMAP_DATA *) ctx->data;
968 /* Check to see if the mailbox is actually open */
972 if (ctx == idata->ctx) {
973 if (idata->state != IMAP_FATAL && idata->state == IMAP_SELECTED) {
974 /* mx_close_mailbox won't sync if there are no deleted messages
975 * and the mailbox is unchanged, so we may have to close here */
976 if (!ctx->deleted && imap_exec (idata, "CLOSE", 0))
977 mutt_error (_("CLOSE failed"));
978 idata->state = IMAP_AUTHENTICATED;
981 idata->reopen &= IMAP_REOPEN_ALLOW;
982 p_delete(&(idata->mailbox));
983 string_list_wipe(&idata->flags);
987 /* free IMAP part of headers */
988 for (i = 0; i < ctx->msgcount; i++)
989 imap_free_header_data (&(ctx->hdrs[i]->data));
991 for (i = 0; i < IMAP_CACHE_LEN; i++) {
992 if (idata->cache[i].path) {
993 unlink (idata->cache[i].path);
994 p_delete(&idata->cache[i].path);
999 /* use the NOOP command to poll for new mail
1002 * M_REOPENED mailbox has been externally modified
1003 * M_NEW_MAIL new mail has arrived!
1007 int imap_check_mailbox (CONTEXT * ctx, int *index_hint __attribute__ ((unused)), int force)
1009 /* overload keyboard timeout to avoid many mailbox checks in a row.
1010 * Most users don't like having to wait exactly when they press a key. */
1014 idata = (IMAP_DATA *) ctx->data;
1016 if ((force || time (NULL) >= idata->lastread + Timeout)
1017 && imap_exec (idata, "NOOP", 0) != 0)
1020 /* We call this even when we haven't run NOOP in case we have pending
1021 * changes to process, since we can reopen here. */
1022 imap_cmd_finish (idata);
1024 if (idata->check_status & IMAP_EXPUNGE_PENDING)
1025 result = M_REOPENED;
1026 else if (idata->check_status & IMAP_NEWMAIL_PENDING)
1027 result = M_NEW_MAIL;
1028 else if (idata->check_status & IMAP_FLAGS_PENDING)
1031 idata->check_status = 0;
1042 * 0+ number of messages in mailbox
1043 * -1 error while polling mailboxes
1045 int imap_mailbox_check (char *path, int new)
1049 char buf[LONG_STRING];
1050 char mbox[LONG_STRING];
1051 char mbox_unquoted[LONG_STRING];
1058 if (imap_parse_path (path, &mx))
1061 /* If imap_passive is set, don't open a connection to check for new mail */
1062 if (option (OPTIMAPPASSIVE))
1063 connflags = M_IMAP_CONN_NONEW;
1065 if (!(idata = imap_conn_find (&(mx.account), connflags))) {
1071 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1074 imap_munge_mbox_name (mbox, sizeof (mbox), buf);
1075 m_strcpy(mbox_unquoted, sizeof(mbox_unquoted), buf);
1077 /* The draft IMAP implementor's guide warns againts using the STATUS
1078 * command on a mailbox that you have selected
1081 if (m_strcmp(mbox_unquoted, idata->mailbox) == 0
1082 || (ascii_strcasecmp (mbox_unquoted, "INBOX") == 0
1083 && m_strcasecmp(mbox_unquoted, idata->mailbox) == 0)) {
1084 m_strcpy(buf, sizeof(buf), "NOOP");
1086 else if (mutt_bit_isset (idata->capabilities, IMAP4REV1) ||
1087 mutt_bit_isset (idata->capabilities, STATUS)) {
1088 snprintf (buf, sizeof (buf), "STATUS %s (%s)", mbox,
1089 new == 1 ? "RECENT" : (new == 2 ? "UNSEEN" : "MESSAGES"));
1092 /* Server does not support STATUS, and this is not the current mailbox.
1093 * There is no lightweight way to check recent arrivals */
1096 imap_cmd_start (idata, buf);
1099 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
1102 s = imap_next_word (idata->cmd.buf.data);
1103 if (ascii_strncasecmp ("STATUS", s, 6) == 0) {
1104 s = imap_next_word (s);
1105 /* The mailbox name may or may not be quoted here. We could try to
1106 * munge the server response and compare with quoted (or vise versa)
1107 * but it is probably more efficient to just strncmp against both. */
1108 if (m_strncmp(mbox_unquoted, s, m_strlen(mbox_unquoted)) == 0
1109 || m_strncmp(mbox, s, m_strlen(mbox)) == 0) {
1110 s = imap_next_word (s);
1111 s = imap_next_word (s);
1112 if (isdigit ((unsigned char) *s)) {
1114 msgcount = atoi (s);
1120 while (rc == IMAP_CMD_CONTINUE);
1125 /* returns number of patterns in the search that should be done server-side
1126 * (eg are full-text) */
1127 static int do_search (const pattern_t* search, int allpats)
1130 const pattern_t* pat;
1132 for (pat = search; pat; pat = pat->next) {
1137 if (pat->stringmatch)
1141 if (pat->child && do_search (pat->child, 1))
1152 /* convert mutt pattern_t to IMAP SEARCH command containing only elements
1153 * that require full-text search (mutt already has what it needs for most
1154 * match types, and does a better job (eg server doesn't support regexps). */
1155 static int imap_compile_search (const pattern_t* pat, BUFFER* buf)
1157 if (! do_search (pat, 0))
1161 mutt_buffer_addstr (buf, "NOT ");
1166 if ((clauses = do_search (pat->child, 1)) > 0) {
1167 const pattern_t* clause = pat->child;
1169 mutt_buffer_addch (buf, '(');
1172 if (do_search (clause, 0)) {
1173 if (pat->op == M_OR && clauses > 1)
1174 mutt_buffer_addstr (buf, "OR ");
1176 if (imap_compile_search (clause, buf) < 0)
1180 mutt_buffer_addch (buf, ' ');
1182 clause = clause->next;
1186 mutt_buffer_addch (buf, ')');
1194 mutt_buffer_addstr (buf, "HEADER ");
1196 /* extract header name */
1197 if (! (delim = strchr (pat->str, ':'))) {
1198 mutt_error (_("Header search without header name: %s"), pat->str);
1202 imap_quote_string (term, sizeof (term), pat->str);
1203 mutt_buffer_addstr (buf, term);
1204 mutt_buffer_addch (buf, ' ');
1208 delim = vskipspaces(delim);
1209 imap_quote_string (term, sizeof (term), delim);
1210 mutt_buffer_addstr (buf, term);
1214 mutt_buffer_addstr (buf, "BODY ");
1215 imap_quote_string (term, sizeof (term), pat->str);
1216 mutt_buffer_addstr (buf, term);
1220 mutt_buffer_addstr (buf, "TEXT ");
1221 imap_quote_string (term, sizeof (term), pat->str);
1222 mutt_buffer_addstr (buf, term);
1230 int imap_search (CONTEXT* ctx, const pattern_t* pat) {
1232 IMAP_DATA* idata = (IMAP_DATA*)ctx->data;
1235 for (i = 0; i < ctx->msgcount; i++)
1236 ctx->hdrs[i]->matched = 0;
1238 if (!do_search (pat, 1))
1242 mutt_buffer_addstr (&buf, "UID SEARCH ");
1243 if (imap_compile_search (pat, &buf) < 0) {
1244 p_delete(&buf.data);
1247 if (imap_exec (idata, buf.data, 0) < 0) {
1248 p_delete(&buf.data);
1252 p_delete(&buf.data);
1256 /* all this listing/browsing is a mess. I don't like that name is a pointer
1257 * into idata->buf (used to be a pointer into the passed in buffer, just
1258 * as bad), nor do I like the fact that the fetch is done here. This
1259 * code can't possibly handle non-string_list_t untagged responses properly.
1261 int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect,
1262 int *noinferiors, char *delim)
1270 rc = imap_cmd_step (idata);
1271 if (rc == IMAP_CMD_OK)
1273 if (rc != IMAP_CMD_CONTINUE)
1276 s = imap_next_word (idata->cmd.buf.data);
1277 if ((ascii_strncasecmp ("LIST", s, 4) == 0) ||
1278 (ascii_strncasecmp ("LSUB", s, 4) == 0)) {
1282 s = imap_next_word (s); /* flags */
1288 while (*ep && *ep != ')')
1291 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
1293 if (!ascii_strncasecmp (s, "\\NoInferiors", 12))
1295 /* See draft-gahrns-imap-child-mailbox-?? */
1296 if (!ascii_strncasecmp (s, "\\HasNoChildren", 14))
1300 while (*s && *s != '\\' && *s != ')')
1306 s = imap_next_word (s); /* delim */
1307 /* Reset the delimiter, this can change */
1308 if (ascii_strncasecmp (s, "NIL", 3)) {
1309 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
1311 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2]
1315 s = imap_next_word (s); /* name */
1316 if (s && *s == '{') { /* Literal */
1317 if (imap_get_literal_count (idata->cmd.buf.data, &bytes) < 0)
1319 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1321 *name = idata->cmd.buf.data;
1330 int imap_subscribe (char *path, int subscribe)
1334 char buf[LONG_STRING];
1335 char mbox[LONG_STRING];
1338 if (mx_get_magic (path) != M_IMAP || imap_parse_path (path, &mx) < 0) {
1339 mutt_error (_("Bad mailbox name"));
1343 if (!(idata = imap_conn_find (&(mx.account), 0)))
1348 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1350 if (option (OPTIMAPCHECKSUBSCRIBED)) {
1351 buffy_do_mailboxes(path, subscribe);
1355 mutt_message (_("Subscribing to %s..."), buf);
1357 mutt_message (_("Unsubscribing to %s..."), buf);
1358 imap_munge_mbox_name (mbox, sizeof (mbox), buf);
1360 snprintf (buf, sizeof (buf), "%sSUBSCRIBE %s", subscribe ? "" : "UN", mbox);
1362 if (imap_exec (idata, buf, 0) < 0)
1373 /* trim dest to the length of the longest prefix it shares with src,
1374 * returning the length of the trimmed string */
1375 static int longest_common_prefix (char *dest, const char* src,
1376 int start, ssize_t dlen) {
1379 while (pos < dlen && dest[pos] && dest[pos] == src[pos])
1386 /* look for IMAP URLs to complete from defined mailboxes. Could be extended
1387 * to complete over open connections and account/folder hooks too. */
1388 static int imap_complete_hosts (char *dest, ssize_t len) {
1395 matchlen = m_strlen(dest);
1398 for (i = 0; i < Incoming.len; i++) {
1399 mailbox = Incoming.arr[i];
1400 if (!m_strncmp(dest, mailbox->path, matchlen)) {
1402 m_strcpy(dest, len, mailbox->path);
1405 longest_common_prefix (dest, mailbox->path, matchlen, len);
1409 for (conn = mutt_socket_head (); conn && conn->next; conn = conn->next) {
1411 char urlstr[LONG_STRING];
1413 if (conn->account.type != M_ACCT_TYPE_IMAP)
1416 mutt_account_tourl (&conn->account, &url);
1417 /* FIXME: how to handle multiple users on the same host? */
1420 url_ciss_tostring (&url, urlstr, sizeof (urlstr), 0);
1421 if (!m_strncmp(dest, urlstr, matchlen)) {
1423 m_strcpy(dest, len, urlstr);
1426 longest_common_prefix (dest, urlstr, matchlen, len);
1433 /* imap_complete: given a partial IMAP folder path, return a string which
1434 * adds as much to the path as is unique */
1435 int imap_complete (char *dest, size_t dlen, char *path) {
1438 char list[LONG_STRING];
1439 char buf[LONG_STRING];
1440 char *list_word = NULL;
1441 int noselect, noinferiors;
1443 char completion[LONG_STRING];
1444 int clen, matchlen = 0;
1445 int completions = 0;
1448 if (imap_parse_path (path, &mx) || !mx.mbox) {
1449 m_strcpy(dest, dlen, path);
1450 return imap_complete_hosts (dest, dlen);
1453 /* don't open a new socket just for completion. Instead complete over
1454 * known mailboxes/hooks/etc */
1455 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NONEW))) {
1457 m_strcpy(dest, dlen, path);
1458 return imap_complete_hosts (dest, dlen);
1462 /* reformat path for IMAP list, and append wildcard */
1463 /* don't use INBOX in place of "" */
1464 if (mx.mbox && mx.mbox[0])
1465 imap_fix_path (idata, mx.mbox, list, sizeof (list));
1469 /* fire off command */
1470 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
1471 option (OPTIMAPLSUB) ? "LSUB" : "LIST", list);
1473 imap_cmd_start (idata, buf);
1475 /* and see what the results are */
1476 m_strcpy(completion, sizeof(completion), NONULL(mx.mbox));
1478 if (imap_parse_list_response (idata, &list_word, &noselect, &noinferiors,
1483 /* store unquoted */
1484 imap_unmunge_mbox_name (list_word);
1486 /* if the folder isn't selectable, append delimiter to force browse
1487 * to enter it on second tab. */
1489 clen = m_strlen(list_word);
1490 list_word[clen++] = delim;
1491 list_word[clen] = '\0';
1493 /* copy in first word */
1495 m_strcpy(completion, sizeof(completion), list_word);
1496 matchlen = m_strlen(completion);
1501 matchlen = longest_common_prefix (completion, list_word, 0, matchlen);
1505 while (m_strncmp(idata->cmd.seq, idata->cmd.buf.data, SEQLEN));
1508 /* reformat output */
1509 imap_qualify_path (dest, dlen, &mx, completion);
1510 mutt_pretty_mailbox (dest);
1519 /* reconnect if connection was lost */
1520 int imap_reconnect (CONTEXT * ctx)
1522 IMAP_DATA *imap_data;
1527 imap_data = (IMAP_DATA *) ctx->data;
1530 if (imap_data->status == IMAP_CONNECTED)
1534 if (query_quadoption
1536 _("Connection lost. Reconnect to IMAP server?")) != M_YES)
1539 mx_open_mailbox (ctx->path, 0, ctx);
1543 static int imap_is_magic (const char* path, struct stat* st __attribute__ ((unused))) {
1545 if (!path || !*path)
1547 s = url_check_scheme (NONULL (path));
1548 return (s == U_IMAP || s == U_IMAPS) ? M_IMAP : -1;
1551 static int acl_check_imap (CONTEXT* ctx, int bit) {
1552 return (!mutt_bit_isset (((IMAP_DATA*) ctx->data)->capabilities, ACL) ||
1553 mutt_bit_isset (((IMAP_DATA*) ctx->data)->rights, bit));
1556 static int imap_open_new_message (MESSAGE * msg,
1557 CONTEXT * dest __attribute__ ((unused)),
1558 HEADER * hdr __attribute__ ((unused)))
1560 char tmp[_POSIX_PATH_MAX];
1562 msg->fp = m_tempfile(tmp, sizeof(tmp), NONULL(mod_core.tmpdir), NULL);
1568 msg->path = m_strdup(tmp);
1572 /* this ugly kludge is required since the last int to
1573 * imap_check_mailbox() doesn't mean 'lock' but 'force'... */
1574 static int _imap_check_mailbox (CONTEXT* ctx,
1576 int lock __attribute__ ((unused))) {
1577 return imap_check_mailbox (ctx, index_hint, 0);
1580 static int imap_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1583 if ((r = m_fclose(&msg->fp)) == 0)
1584 r = imap_append_message (ctx, msg);
1588 mx_t const imap_mx = {
1595 imap_open_new_message,
1598 _imap_check_mailbox,
1601 imap_commit_message,