2 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2003 Brendan Cully <brendan@kublai.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 /* Support for IMAP4rev1, with the occasional nod to IMAP 4. */
28 #include "mutt_curses.h"
35 #include "imap_private.h"
36 #if defined(USE_SSL) || defined(USE_GNUTLS)
37 # include "mutt_ssl.h"
44 #include <sys/types.h>
47 /* imap forward declarations */
48 static int imap_get_delim (IMAP_DATA * idata);
49 static char *imap_get_flags (LIST ** hflags, char *s);
50 static int imap_check_acl (IMAP_DATA * idata);
51 static int imap_check_capabilities (IMAP_DATA * idata);
52 static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
53 const char *str, char *flags, size_t flsize);
55 /* imap_access: Check permissions on an IMAP mailbox. */
56 int imap_access (const char *path, int flags)
60 char buf[LONG_STRING];
61 char mailbox[LONG_STRING];
62 char mbox[LONG_STRING];
64 if (imap_parse_path (path, &mx))
67 if (!(idata = imap_conn_find (&mx.account,
68 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW :
74 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
76 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
78 /* TODO: ACL checks. Right now we assume if it exists we can mess with it. */
79 if (mutt_bit_isset (idata->capabilities, IMAP4REV1))
80 snprintf (buf, sizeof (buf), "STATUS %s (UIDVALIDITY)", mbox);
81 else if (mutt_bit_isset (idata->capabilities, STATUS))
82 snprintf (buf, sizeof (buf), "STATUS %s (UID-VALIDITY)", mbox);
84 dprint (2, (debugfile, "imap_access: STATUS not supported?\n"));
88 if (imap_exec (idata, buf, IMAP_CMD_FAIL_OK) < 0) {
89 dprint (1, (debugfile, "imap_access: Can't check STATUS of %s\n", mbox));
96 int imap_create_mailbox (IMAP_DATA * idata, char *mailbox)
98 char buf[LONG_STRING], mbox[LONG_STRING];
100 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
101 snprintf (buf, sizeof (buf), "CREATE %s", mbox);
103 if (imap_exec (idata, buf, 0) != 0)
109 int imap_rename_mailbox (IMAP_DATA * idata, IMAP_MBOX * mx,
112 char oldmbox[LONG_STRING];
113 char newmbox[LONG_STRING];
114 char buf[LONG_STRING];
116 imap_munge_mbox_name (oldmbox, sizeof (oldmbox), mx->mbox);
117 imap_munge_mbox_name (newmbox, sizeof (newmbox), newname);
119 snprintf (buf, sizeof (buf), "RENAME %s %s", oldmbox, newmbox);
121 if (imap_exec (idata, buf, 0) != 0)
127 int imap_delete_mailbox (CONTEXT * ctx, IMAP_MBOX mx)
129 char buf[LONG_STRING], mbox[LONG_STRING];
132 if (!ctx || !ctx->data) {
133 if (!(idata = imap_conn_find (&mx.account,
134 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW
144 imap_munge_mbox_name (mbox, sizeof (mbox), mx.mbox);
145 snprintf (buf, sizeof (buf), "DELETE %s", mbox);
147 if (imap_exec ((IMAP_DATA *) idata, buf, 0) != 0)
153 /* imap_logout_all: close all open connections. Quick and dirty until we can
154 * make sure we've got all the context we need. */
155 void imap_logout_all (void)
160 conn = mutt_socket_head ();
165 if (conn->account.type == M_ACCT_TYPE_IMAP && conn->fd >= 0) {
166 mutt_message (_("Closing connection to %s..."), conn->account.host);
167 imap_logout ((IMAP_DATA *) conn->data);
169 mutt_socket_close (conn);
170 mutt_socket_free (conn);
177 /* imap_read_literal: read bytes bytes from server into file. Not explicitly
178 * buffered, relies on FILE buffering. NOTE: strips \r from \r\n.
179 * Apparently even literals use \r\n-terminated strings ?! */
180 int imap_read_literal (FILE * fp, IMAP_DATA * idata, long bytes)
187 dprint (2, (debugfile, "imap_read_literal: reading %ld bytes\n", bytes));
189 for (pos = 0; pos < bytes; pos++) {
190 if (mutt_socket_readchar (idata->conn, &c) != 1) {
193 "imap_read_literal: error during read, %ld bytes read\n",
195 idata->status = IMAP_FATAL;
201 if (r == 1 && c != '\n')
213 if (debuglevel >= IMAP_LOG_LTRL)
214 fputc (c, debugfile);
221 /* imap_expunge_mailbox: Purge IMAP portion of expunged messages from the
222 * context. Must not be done while something has a handle on any headers
223 * (eg inside pager or editor). That is, check IMAP_REOPEN_ALLOW. */
224 void imap_expunge_mailbox (IMAP_DATA * idata)
229 for (i = 0; i < idata->ctx->msgcount; i++) {
230 h = idata->ctx->hdrs[i];
232 if (h->index == -1) {
234 (debugfile, "Expunging message UID %d.\n",
235 HEADER_DATA (h)->uid));
239 /* free cached body from disk, if neccessary */
240 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
241 if (idata->cache[cacheno].uid == HEADER_DATA (h)->uid &&
242 idata->cache[cacheno].path) {
243 unlink (idata->cache[cacheno].path);
244 FREE (&idata->cache[cacheno].path);
247 imap_free_header_data (&h->data);
251 /* We may be called on to expunge at any time. We can't rely on the caller
252 * to always know to rethread */
253 mx_update_tables (idata->ctx, 0);
254 mutt_sort_headers (idata->ctx, 1);
257 static int imap_get_delim (IMAP_DATA * idata)
262 /* assume that the delim is /. If this fails, we're in bigger trouble
263 * than getting the delim wrong */
266 imap_cmd_start (idata, "LIST \"\" \"\"");
269 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
272 s = imap_next_word (idata->cmd.buf);
273 if (ascii_strncasecmp ("LIST", s, 4) == 0) {
274 s = imap_next_word (s);
275 s = imap_next_word (s);
276 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
278 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2]
283 while (rc == IMAP_CMD_CONTINUE);
285 if (rc != IMAP_CMD_OK) {
286 dprint (1, (debugfile, "imap_get_delim: failed.\n"));
290 dprint (2, (debugfile, "Delimiter: %c\n", idata->delim));
295 /* get rights for folder, let imap_handle_untagged do the rest */
296 static int imap_check_acl (IMAP_DATA * idata)
298 char buf[LONG_STRING];
299 char mbox[LONG_STRING];
301 imap_munge_mbox_name (mbox, sizeof (mbox), idata->mailbox);
302 snprintf (buf, sizeof (buf), "MYRIGHTS %s", mbox);
303 if (imap_exec (idata, buf, 0) != 0) {
304 imap_error ("imap_check_acl", buf);
310 /* imap_check_capabilities: make sure we can log in to this server. */
311 static int imap_check_capabilities (IMAP_DATA * idata)
313 if (imap_exec (idata, "CAPABILITY", 0) != 0) {
314 imap_error ("imap_check_capabilities", idata->cmd.buf);
318 if (!(mutt_bit_isset (idata->capabilities, IMAP4)
319 || mutt_bit_isset (idata->capabilities, IMAP4REV1))) {
320 mutt_error _("This IMAP server is ancient. Mutt does not work with it.");
322 mutt_sleep (2); /* pause a moment to let the user see the error */
330 /* imap_conn_find: Find an open IMAP connection matching account, or open
331 * a new one if none can be found. */
332 IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags)
338 if (!(conn = mutt_conn_find (NULL, account)))
341 /* if opening a new UNSELECTED connection, preserve existing creds */
342 creds = &(conn->account);
344 /* make sure this connection is not in SELECTED state, if neccessary */
345 if (flags & M_IMAP_CONN_NOSELECT)
346 while (conn->data && ((IMAP_DATA *) conn->data)->state == IMAP_SELECTED) {
347 if (!(conn = mutt_conn_find (conn, account)))
349 memcpy (&(conn->account), creds, sizeof (ACCOUNT));
352 idata = (IMAP_DATA *) conn->data;
354 /* don't open a new connection if one isn't wanted */
355 if (flags & M_IMAP_CONN_NONEW) {
357 mutt_socket_free (conn);
360 if (idata->state < IMAP_AUTHENTICATED)
365 /* The current connection is a new connection */
366 if (!(idata = imap_new_idata ())) {
367 mutt_socket_free (conn);
375 if (idata->state == IMAP_DISCONNECTED)
376 imap_open_connection (idata);
377 if (idata->state == IMAP_CONNECTED) {
378 if (!imap_authenticate (idata)) {
379 idata->state = IMAP_AUTHENTICATED;
380 if (idata->conn->ssf)
381 dprint (2, (debugfile, "Communication encrypted at %d bits\n",
385 mutt_account_unsetpass (&idata->conn->account);
387 FREE (&idata->capstr);
389 if (idata->state == IMAP_AUTHENTICATED)
390 imap_get_delim (idata);
395 int imap_open_connection (IMAP_DATA * idata)
397 char buf[LONG_STRING];
399 if (mutt_socket_open (idata->conn) < 0)
402 idata->state = IMAP_CONNECTED;
404 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE) {
405 mutt_error (_("Unexpected response received from server: %s"),
409 mutt_socket_close (idata->conn);
410 idata->state = IMAP_DISCONNECTED;
414 if (ascii_strncasecmp ("* OK", idata->cmd.buf, 4) == 0) {
415 /* TODO: Parse new tagged CAPABILITY data (* OK [CAPABILITY...]) */
416 if (imap_check_capabilities (idata))
418 #if defined(USE_SSL) || defined(USE_GNUTLS)
419 /* Attempt STARTTLS if available and desired. */
420 if (mutt_bit_isset (idata->capabilities, STARTTLS) && !idata->conn->ssf) {
423 if ((rc = query_quadoption (OPT_SSLSTARTTLS,
424 _("Secure connection with TLS?"))) == -1)
427 if ((rc = imap_exec (idata, "STARTTLS", IMAP_CMD_FAIL_OK)) == -1)
431 if (mutt_ssl_starttls (idata->conn))
433 if (mutt_gnutls_starttls (idata->conn))
436 mutt_error (_("Could not negotiate TLS connection"));
441 /* RFC 2595 demands we recheck CAPABILITY after TLS completes. */
442 if (imap_exec (idata, "CAPABILITY", 0))
450 else if (ascii_strncasecmp ("* PREAUTH", idata->cmd.buf, 9) == 0) {
451 idata->state = IMAP_AUTHENTICATED;
452 if (imap_check_capabilities (idata) != 0)
454 FREE (&idata->capstr);
457 imap_error ("imap_open_connection()", buf);
464 mutt_socket_close (idata->conn);
465 idata->state = IMAP_DISCONNECTED;
467 FREE (&idata->capstr);
471 /* imap_get_flags: Make a simple list out of a FLAGS response.
472 * return stream following FLAGS response */
473 static char *imap_get_flags (LIST ** hflags, char *s)
479 /* sanity-check string */
480 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
481 dprint (1, (debugfile, "imap_get_flags: not a FLAGS response: %s\n", s));
487 dprint (1, (debugfile, "imap_get_flags: bogus FLAGS response: %s\n", s));
491 /* create list, update caller's flags handle */
492 flags = mutt_new_list ();
495 while (*s && *s != ')') {
499 while (*s && (*s != ')') && !ISSPACE (*s))
504 mutt_add_list (flags, flag_word);
508 /* note bad flags response */
510 dprint (1, (debugfile,
511 "imap_get_flags: Unterminated FLAGS response: %s\n", s));
512 mutt_free_list (hflags);
522 int imap_open_mailbox (CONTEXT * ctx)
526 char buf[LONG_STRING];
527 char bufout[LONG_STRING];
532 if (imap_parse_path (ctx->path, &mx)) {
533 mutt_error (_("%s is an invalid IMAP path"), ctx->path);
537 /* we require a connection which isn't currently in IMAP_SELECTED state */
538 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NOSELECT)))
540 if (idata->state < IMAP_AUTHENTICATED)
545 /* once again the context is new */
548 /* Clean up path and replace the one in the ctx */
549 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
550 FREE (&(idata->mailbox));
551 idata->mailbox = safe_strdup (buf);
552 imap_qualify_path (buf, sizeof (buf), &mx, idata->mailbox);
555 ctx->path = safe_strdup (buf);
559 /* clear mailbox status */
561 memset (idata->rights, 0, (RIGHTSMAX + 7) / 8);
562 idata->newMailCount = 0;
564 mutt_message (_("Selecting %s..."), idata->mailbox);
565 imap_munge_mbox_name (buf, sizeof (buf), idata->mailbox);
566 snprintf (bufout, sizeof (bufout), "%s %s",
567 ctx->readonly ? "EXAMINE" : "SELECT", buf);
569 idata->state = IMAP_SELECTED;
571 imap_cmd_start (idata, bufout);
576 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
579 pc = idata->cmd.buf + 2;
581 /* Obtain list of available flags here, may be overridden by a
582 * PERMANENTFLAGS tag in the OK response */
583 if (ascii_strncasecmp ("FLAGS", pc, 5) == 0) {
584 /* don't override PERMANENTFLAGS */
586 dprint (2, (debugfile, "Getting mailbox FLAGS\n"));
587 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
591 /* PERMANENTFLAGS are massaged to look like FLAGS, then override FLAGS */
592 else if (ascii_strncasecmp ("OK [PERMANENTFLAGS", pc, 18) == 0) {
593 dprint (2, (debugfile, "Getting mailbox PERMANENTFLAGS\n"));
594 /* safe to call on NULL */
595 mutt_free_list (&(idata->flags));
596 /* skip "OK [PERMANENT" so syntax is the same as FLAGS */
598 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
602 /* save UIDVALIDITY for the header cache */
603 else if (ascii_strncasecmp ("OK [UIDVALIDITY", pc, 14) == 0) {
604 dprint (2, (debugfile, "Getting mailbox UIDVALIDITY\n"));
606 pc = imap_next_word (pc);
608 sscanf (pc, "%u", &(idata->uid_validity));
612 pc = imap_next_word (pc);
613 if (!ascii_strncasecmp ("EXISTS", pc, 6)) {
614 count = idata->newMailCount;
615 idata->newMailCount = 0;
619 while (rc == IMAP_CMD_CONTINUE);
621 if (rc == IMAP_CMD_NO) {
624 s = imap_next_word (idata->cmd.buf); /* skip seq */
625 s = imap_next_word (s); /* Skip response */
626 mutt_error ("%s", s);
631 if (rc != IMAP_CMD_OK)
634 /* check for READ-ONLY notification */
635 if (!ascii_strncasecmp
636 (imap_get_qualifier (idata->cmd.buf), "[READ-ONLY]", 11)
637 && !mutt_bit_isset (idata->capabilities, ACL)) {
638 dprint (2, (debugfile, "Mailbox is read-only.\n"));
643 /* dump the mailbox flags we've found */
644 if (debuglevel > 2) {
646 dprint (3, (debugfile, "No folder flags found\n"));
648 LIST *t = idata->flags;
650 dprint (3, (debugfile, "Mailbox flags: "));
654 dprint (3, (debugfile, "[%s] ", t->data));
657 dprint (3, (debugfile, "\n"));
662 if (mutt_bit_isset (idata->capabilities, ACL)) {
663 if (imap_check_acl (idata))
665 if (!(mutt_bit_isset (idata->rights, IMAP_ACL_DELETE) ||
666 mutt_bit_isset (idata->rights, IMAP_ACL_SEEN) ||
667 mutt_bit_isset (idata->rights, IMAP_ACL_WRITE) ||
668 mutt_bit_isset (idata->rights, IMAP_ACL_INSERT)))
671 /* assume we have all rights if ACL is unavailable */
673 mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);
674 mutt_bit_set (idata->rights, IMAP_ACL_READ);
675 mutt_bit_set (idata->rights, IMAP_ACL_SEEN);
676 mutt_bit_set (idata->rights, IMAP_ACL_WRITE);
677 mutt_bit_set (idata->rights, IMAP_ACL_INSERT);
678 mutt_bit_set (idata->rights, IMAP_ACL_POST);
679 mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
680 mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
684 ctx->hdrs = safe_calloc (count, sizeof (HEADER *));
685 ctx->v2r = safe_calloc (count, sizeof (int));
687 if (count && (imap_read_headers (idata, 0, count - 1) < 0)) {
688 mutt_error _("Error opening mailbox");
695 (debugfile, "imap_open_mailbox: msgcount is %d\n", ctx->msgcount));
700 if (idata->state == IMAP_SELECTED)
701 idata->state = IMAP_AUTHENTICATED;
707 int imap_open_mailbox_append (CONTEXT * ctx)
711 char buf[LONG_STRING], mbox[LONG_STRING];
712 char mailbox[LONG_STRING];
716 if (imap_parse_path (ctx->path, &mx))
719 /* in APPEND mode, we appear to hijack an existing IMAP connection -
720 * ctx is brand new and mostly empty */
722 if (!(idata = imap_conn_find (&(mx.account), 0)))
729 /* check mailbox existance */
731 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
733 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
735 if (mutt_bit_isset (idata->capabilities, IMAP4REV1))
736 snprintf (buf, sizeof (buf), "STATUS %s (UIDVALIDITY)", mbox);
737 else if (mutt_bit_isset (idata->capabilities, STATUS))
738 /* We have no idea what the other guy wants. UW imapd 8.3 wants this
739 * (but it does not work if another mailbox is selected) */
740 snprintf (buf, sizeof (buf), "STATUS %s (UID-VALIDITY)", mbox);
742 /* STATUS not supported */
743 mutt_message _("Unable to append to IMAP mailboxes at this server");
748 r = imap_exec (idata, buf, IMAP_CMD_FAIL_OK);
750 /* command failed cause folder doesn't exist */
751 snprintf (buf, sizeof (buf), _("Create %s?"), mailbox);
752 if (option (OPTCONFIRMCREATE) && mutt_yesorno (buf, 1) < 1)
755 if (imap_create_mailbox (idata, mailbox) < 0)
759 /* Hmm, some other failure */
770 /* imap_logout: Gracefully log out of server. */
771 void imap_logout (IMAP_DATA * idata)
773 /* we set status here to let imap_handle_untagged know we _expect_ to
774 * receive a bye response (so it doesn't freak out and close the conn) */
775 idata->status = IMAP_BYE;
776 imap_cmd_start (idata, "LOGOUT");
777 while (imap_cmd_step (idata) == IMAP_CMD_CONTINUE);
778 FREE (&idata->cmd.buf);
783 int imap_close_connection (CONTEXT *ctx)
785 dprint (1, (debugfile, "imap_close_connection(): closing connection\n"));
786 if (CTX_DATA->status != IMAP_BYE)
788 mutt_message _("Closing connection to IMAP server...");
789 imap_logout (CTX_DATA);
792 mutt_socket_close (CTX_DATA->conn);
793 CTX_DATA->state = IMAP_DISCONNECTED;
794 CTX_DATA->conn->data = NULL;
799 /* imap_set_flag: append str to flags if we currently have permission
800 * according to aclbit */
801 static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
802 const char *str, char *flags, size_t flsize)
804 if (mutt_bit_isset (idata->rights, aclbit))
806 safe_strcat (flags, flsize, str);
809 /* imap_make_msg_set: make an IMAP4rev1 UID message set out of a set of
810 * headers, given a flag enum to filter on.
811 * Params: idata: IMAP_DATA containing context containing header set
812 * buf: to write message set into
813 * buflen: length of buffer
814 * flag: enum of flag type on which to filter
815 * changed: include only changed messages in message set
816 * Returns: number of messages in message set (0 if no matches) */
817 int imap_make_msg_set (IMAP_DATA * idata, BUFFER * buf, int flag, int changed)
819 HEADER **hdrs; /* sorted local copy */
820 int count = 0; /* number of messages in message set */
821 int match = 0; /* whether current message matches flag condition */
822 unsigned int setstart = 0; /* start of current message range */
824 short oldsort; /* we clobber reverse, must restore it */
826 /* assuming 32-bit UIDs */
830 /* make copy of header pointers to sort in natural order */
831 hdrs = safe_calloc (idata->ctx->msgcount, sizeof (HEADER *));
832 memcpy (hdrs, idata->ctx->hdrs, idata->ctx->msgcount * sizeof (HEADER *));
834 if (Sort != SORT_ORDER) {
837 qsort ((void *) hdrs, idata->ctx->msgcount, sizeof (HEADER *),
838 mutt_get_sort_func (SORT_ORDER));
842 for (n = 0; n < idata->ctx->msgcount; n++) {
844 /* don't include pending expunged messages */
848 if (hdrs[n]->deleted)
857 if (match && (!changed || hdrs[n]->changed)) {
860 setstart = HEADER_DATA (hdrs[n])->uid;
862 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (hdrs[n])->uid);
863 mutt_buffer_addstr (buf, uid);
867 snprintf (uid, sizeof (uid), ",%u", HEADER_DATA (hdrs[n])->uid);
868 mutt_buffer_addstr (buf, uid);
871 /* tie up if the last message also matches */
872 else if (n == idata->ctx->msgcount - 1) {
873 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n])->uid);
874 mutt_buffer_addstr (buf, uid);
877 /* this message is not expunged and doesn't match. End current set. */
878 else if (setstart && hdrs[n]->active) {
879 if (HEADER_DATA (hdrs[n - 1])->uid > setstart) {
880 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n - 1])->uid);
881 mutt_buffer_addstr (buf, uid);
892 /* update the IMAP server to reflect message changes done within mutt.
894 * ctx: the current context
895 * expunge: 0 or 1 - do expunge?
898 int imap_sync_mailbox (CONTEXT * ctx, int expunge, int *index_hint)
901 CONTEXT *appendctx = NULL;
903 char flags[LONG_STRING];
907 int err_continue = M_NO; /* continue on error? */
910 idata = (IMAP_DATA *) ctx->data;
912 if (idata->state != IMAP_SELECTED) {
913 dprint (2, (debugfile, "imap_sync_mailbox: no mailbox selected\n"));
917 /* CLOSE purges deleted messages. If we don't want to purge them, we must
918 * tell imap_close_mailbox not to issue the CLOSE command */
924 /* This function is only called when the calling code expects the context
926 imap_allow_reopen (ctx);
928 if ((rc = imap_check_mailbox (ctx, index_hint, 0)) != 0)
931 memset (&cmd, 0, sizeof (cmd));
933 /* if we are expunging anyway, we can do deleted messages very quickly... */
934 if (expunge && mutt_bit_isset (idata->rights, IMAP_ACL_DELETE)) {
935 mutt_buffer_addstr (&cmd, "UID STORE ");
936 deleted = imap_make_msg_set (idata, &cmd, M_DELETE, 1);
938 /* if we have a message set, then let's delete */
940 mutt_message (_("Marking %d messages deleted..."), deleted);
941 mutt_buffer_addstr (&cmd, " +FLAGS.SILENT (\\Deleted)");
942 /* mark these messages as unchanged so second pass ignores them. Done
943 * here so BOGUS UW-IMAP 4.7 SILENT FLAGS updates are ignored. */
944 for (n = 0; n < ctx->msgcount; n++)
945 if (ctx->hdrs[n]->deleted && ctx->hdrs[n]->changed)
946 ctx->hdrs[n]->active = 0;
947 if (imap_exec (idata, cmd.data, 0) != 0) {
948 mutt_error (_("Expunge failed"));
956 /* save status changes */
957 for (n = 0; n < ctx->msgcount; n++) {
958 if (ctx->hdrs[n]->active && ctx->hdrs[n]->changed) {
959 ctx->hdrs[n]->changed = 0;
961 mutt_message (_("Saving message status flags... [%d/%d]"), n + 1,
964 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (ctx->hdrs[n])->uid);
966 mutt_buffer_addstr (&cmd, "UID STORE ");
967 mutt_buffer_addstr (&cmd, uid);
969 /* if the message has been rethreaded or attachments have been deleted
970 * we delete the message and reupload it.
971 * This works better if we're expunging, of course. */
972 if (ctx->hdrs[n]->refs_changed || ctx->hdrs[n]->irt_changed ||
973 ctx->hdrs[n]->attach_del) {
976 "imap_sync_mailbox: Attachments to be deleted, falling back to _mutt_save_message\n"));
978 appendctx = mx_open_mailbox (ctx->path, M_APPEND | M_QUIET, NULL);
982 "imap_sync_mailbox: Error opening mailbox in append mode\n"));
985 _mutt_save_message (ctx->hdrs[n], appendctx, 1, 0, 0);
989 imap_set_flag (idata, IMAP_ACL_SEEN, ctx->hdrs[n]->read, "\\Seen ",
990 flags, sizeof (flags));
991 imap_set_flag (idata, IMAP_ACL_WRITE, ctx->hdrs[n]->flagged,
992 "\\Flagged ", flags, sizeof (flags));
993 imap_set_flag (idata, IMAP_ACL_WRITE, ctx->hdrs[n]->replied,
994 "\\Answered ", flags, sizeof (flags));
995 imap_set_flag (idata, IMAP_ACL_DELETE, ctx->hdrs[n]->deleted,
996 "\\Deleted ", flags, sizeof (flags));
998 /* now make sure we don't lose custom tags */
999 if (mutt_bit_isset (idata->rights, IMAP_ACL_WRITE))
1000 imap_add_keywords (flags, ctx->hdrs[n], idata->flags, sizeof (flags));
1002 mutt_remove_trailing_ws (flags);
1004 /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
1005 * explicitly revoke all system flags (if we have permission) */
1007 imap_set_flag (idata, IMAP_ACL_SEEN, 1, "\\Seen ", flags,
1009 imap_set_flag (idata, IMAP_ACL_WRITE, 1, "\\Flagged ", flags,
1011 imap_set_flag (idata, IMAP_ACL_WRITE, 1, "\\Answered ", flags,
1013 imap_set_flag (idata, IMAP_ACL_DELETE, 1, "\\Deleted ", flags,
1016 mutt_remove_trailing_ws (flags);
1018 mutt_buffer_addstr (&cmd, " -FLAGS.SILENT (");
1021 mutt_buffer_addstr (&cmd, " FLAGS.SILENT (");
1023 mutt_buffer_addstr (&cmd, flags);
1024 mutt_buffer_addstr (&cmd, ")");
1026 /* dumb hack for bad UW-IMAP 4.7 servers spurious FLAGS updates */
1027 ctx->hdrs[n]->active = 0;
1029 /* after all this it's still possible to have no flags, if you
1030 * have no ACL rights */
1031 if (*flags && (imap_exec (idata, cmd.data, 0) != 0) &&
1032 (err_continue != M_YES)) {
1033 err_continue = imap_continue ("imap_sync_mailbox: STORE failed",
1035 if (err_continue != M_YES) {
1041 ctx->hdrs[n]->active = 1;
1046 /* We must send an EXPUNGE command if we're not closing. */
1047 if (expunge && !(ctx->closing) &&
1048 mutt_bit_isset (idata->rights, IMAP_ACL_DELETE)) {
1049 mutt_message _("Expunging messages from server...");
1051 /* Set expunge bit so we don't get spurious reopened messages */
1052 idata->reopen |= IMAP_EXPUNGE_EXPECTED;
1053 if (imap_exec (idata, "EXPUNGE", 0) != 0) {
1054 imap_error (_("imap_sync_mailbox: EXPUNGE failed"), idata->cmd.buf);
1065 mx_fastclose_mailbox (appendctx);
1071 /* imap_close_mailbox: issue close command if neccessary, reset IMAP_DATA */
1072 void imap_close_mailbox (CONTEXT * ctx)
1077 idata = (IMAP_DATA *) ctx->data;
1078 /* Check to see if the mailbox is actually open */
1082 if ((idata->status != IMAP_FATAL) &&
1083 (idata->state == IMAP_SELECTED) && (ctx == idata->ctx)) {
1084 if (!(idata->noclose) && imap_exec (idata, "CLOSE", 0))
1085 mutt_error (_("CLOSE failed"));
1087 idata->reopen &= IMAP_REOPEN_ALLOW;
1088 idata->state = IMAP_AUTHENTICATED;
1089 FREE (&(idata->mailbox));
1090 mutt_free_list (&idata->flags);
1094 /* free IMAP part of headers */
1095 for (i = 0; i < ctx->msgcount; i++)
1096 imap_free_header_data (&(ctx->hdrs[i]->data));
1098 for (i = 0; i < IMAP_CACHE_LEN; i++) {
1099 if (idata->cache[i].path) {
1100 unlink (idata->cache[i].path);
1101 FREE (&idata->cache[i].path);
1106 /* use the NOOP command to poll for new mail
1109 * M_REOPENED mailbox has been externally modified
1110 * M_NEW_MAIL new mail has arrived!
1114 int imap_check_mailbox (CONTEXT * ctx, int *index_hint, int force)
1116 /* overload keyboard timeout to avoid many mailbox checks in a row.
1117 * Most users don't like having to wait exactly when they press a key. */
1121 idata = (IMAP_DATA *) ctx->data;
1123 if ((force || time (NULL) > idata->lastread + Timeout)
1124 && imap_exec (idata, "NOOP", 0) != 0)
1127 /* We call this even when we haven't run NOOP in case we have pending
1128 * changes to process, since we can reopen here. */
1129 imap_cmd_finish (idata);
1131 if (idata->check_status & IMAP_EXPUNGE_PENDING)
1132 result = M_REOPENED;
1133 else if (idata->check_status & IMAP_NEWMAIL_PENDING)
1134 result = M_NEW_MAIL;
1135 else if (idata->check_status & IMAP_FLAGS_PENDING)
1138 idata->check_status = 0;
1143 /* returns count of recent messages if new = 1, else count of total messages.
1144 * (useful for at least postponed function)
1145 * Question of taste: use RECENT or UNSEEN for new?
1146 * 0+ number of messages in mailbox
1147 * -1 error while polling mailboxes
1149 int imap_mailbox_check (char *path, int new)
1153 char buf[LONG_STRING];
1154 char mbox[LONG_STRING];
1155 char mbox_unquoted[LONG_STRING];
1162 if (imap_parse_path (path, &mx))
1165 /* If imap_passive is set, don't open a connection to check for new mail */
1166 if (option (OPTIMAPPASSIVE))
1167 connflags = M_IMAP_CONN_NONEW;
1169 if (!(idata = imap_conn_find (&(mx.account), connflags))) {
1175 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1178 imap_munge_mbox_name (mbox, sizeof (mbox), buf);
1179 strfcpy (mbox_unquoted, buf, sizeof (mbox_unquoted));
1181 /* The draft IMAP implementor's guide warns againts using the STATUS
1182 * command on a mailbox that you have selected
1185 if (mutt_strcmp (mbox_unquoted, idata->mailbox) == 0
1186 || (ascii_strcasecmp (mbox_unquoted, "INBOX") == 0
1187 && mutt_strcasecmp (mbox_unquoted, idata->mailbox) == 0)) {
1188 strfcpy (buf, "NOOP", sizeof (buf));
1190 else if (mutt_bit_isset (idata->capabilities, IMAP4REV1) ||
1191 mutt_bit_isset (idata->capabilities, STATUS)) {
1192 snprintf (buf, sizeof (buf), "STATUS %s (%s)", mbox,
1193 new ? "RECENT" : "MESSAGES");
1196 /* Server does not support STATUS, and this is not the current mailbox.
1197 * There is no lightweight way to check recent arrivals */
1200 imap_cmd_start (idata, buf);
1203 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
1206 s = imap_next_word (idata->cmd.buf);
1207 if (ascii_strncasecmp ("STATUS", s, 6) == 0) {
1208 s = imap_next_word (s);
1209 /* The mailbox name may or may not be quoted here. We could try to
1210 * munge the server response and compare with quoted (or vise versa)
1211 * but it is probably more efficient to just strncmp against both. */
1212 if (mutt_strncmp (mbox_unquoted, s, mutt_strlen (mbox_unquoted)) == 0
1213 || mutt_strncmp (mbox, s, mutt_strlen (mbox)) == 0) {
1214 s = imap_next_word (s);
1215 s = imap_next_word (s);
1216 if (isdigit ((unsigned char) *s)) {
1218 msgcount = atoi (s);
1220 (debugfile, "%d new messages in %s\n", msgcount, path));
1227 "imap_mailbox_check: STATUS response doesn't match requested mailbox.\n"));
1230 while (rc == IMAP_CMD_CONTINUE);
1235 /* all this listing/browsing is a mess. I don't like that name is a pointer
1236 * into idata->buf (used to be a pointer into the passed in buffer, just
1237 * as bad), nor do I like the fact that the fetch is done here. This
1238 * code can't possibly handle non-LIST untagged responses properly.
1240 int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect,
1241 int *noinferiors, char *delim)
1249 rc = imap_cmd_step (idata);
1250 if (rc == IMAP_CMD_OK)
1252 if (rc != IMAP_CMD_CONTINUE)
1255 s = imap_next_word (idata->cmd.buf);
1256 if ((ascii_strncasecmp ("LIST", s, 4) == 0) ||
1257 (ascii_strncasecmp ("LSUB", s, 4) == 0)) {
1261 s = imap_next_word (s); /* flags */
1267 while (*ep && *ep != ')')
1270 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
1272 if (!ascii_strncasecmp (s, "\\NoInferiors", 12))
1274 /* See draft-gahrns-imap-child-mailbox-?? */
1275 if (!ascii_strncasecmp (s, "\\HasNoChildren", 14))
1279 while (*s && *s != '\\' && *s != ')')
1285 s = imap_next_word (s); /* delim */
1286 /* Reset the delimiter, this can change */
1287 if (ascii_strncasecmp (s, "NIL", 3)) {
1288 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
1290 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2]
1294 s = imap_next_word (s); /* name */
1295 if (s && *s == '{') { /* Literal */
1296 if (imap_get_literal_count (idata->cmd.buf, &bytes) < 0)
1298 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1300 *name = idata->cmd.buf;
1309 int imap_subscribe (char *path, int subscribe)
1313 char buf[LONG_STRING];
1314 char mbox[LONG_STRING];
1317 if (!mx_is_imap (path) || imap_parse_path (path, &mx)) {
1318 mutt_error (_("Bad mailbox name"));
1323 if (!(idata = imap_conn_find (&(mx.account), 0)))
1328 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1330 mutt_message (_("Subscribing to %s..."), buf);
1332 mutt_message (_("Unsubscribing to %s..."), buf);
1333 imap_munge_mbox_name (mbox, sizeof (mbox), buf);
1335 snprintf (buf, sizeof (buf), "%s %s", subscribe ? "SUBSCRIBE" :
1336 "UNSUBSCRIBE", mbox);
1338 if (imap_exec (idata, buf, 0) < 0)
1349 /* imap_complete: given a partial IMAP folder path, return a string which
1350 * adds as much to the path as is unique */
1351 int imap_complete (char *dest, size_t dlen, char *path)
1355 char list[LONG_STRING];
1356 char buf[LONG_STRING];
1357 char *list_word = NULL;
1358 int noselect, noinferiors;
1360 char completion[LONG_STRING];
1361 int clen, matchlen = 0;
1362 int completions = 0;
1366 /* verify passed in path is an IMAP path */
1367 if (imap_parse_path (path, &mx)) {
1368 dprint (2, (debugfile, "imap_complete: bad path %s\n", path));
1372 /* don't open a new socket just for completion */
1373 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NONEW)))
1377 /* reformat path for IMAP list, and append wildcard */
1378 /* don't use INBOX in place of "" */
1379 if (mx.mbox && mx.mbox[0])
1380 imap_fix_path (idata, mx.mbox, list, sizeof (list));
1384 /* fire off command */
1385 snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
1386 option (OPTIMAPLSUB) ? "LSUB" : "LIST", list);
1388 imap_cmd_start (idata, buf);
1390 /* and see what the results are */
1391 strfcpy (completion, NONULL (mx.mbox), sizeof (completion));
1393 if (imap_parse_list_response (idata, &list_word, &noselect, &noinferiors,
1398 /* store unquoted */
1399 imap_unmunge_mbox_name (list_word);
1401 /* if the folder isn't selectable, append delimiter to force browse
1402 * to enter it on second tab. */
1404 clen = strlen (list_word);
1405 list_word[clen++] = delim;
1406 list_word[clen] = '\0';
1408 /* copy in first word */
1410 strfcpy (completion, list_word, sizeof (completion));
1411 matchlen = strlen (completion);
1417 while (pos < matchlen && list_word[pos] &&
1418 completion[pos] == list_word[pos])
1420 completion[pos] = '\0';
1426 while (ascii_strncmp (idata->cmd.seq, idata->cmd.buf, SEQLEN));
1429 /* reformat output */
1430 imap_qualify_path (dest, dlen, &mx, completion);
1431 mutt_pretty_mailbox (dest);
1442 /* reconnect if connection was lost */
1443 int imap_reconnect (CONTEXT * ctx)
1445 IMAP_DATA *imap_data = (IMAP_DATA *) ctx->data;
1448 if (imap_data->status == IMAP_CONNECTED)
1452 if (query_quadoption
1454 _("Connection lost. Reconnect to IMAP server?")) != M_YES)
1457 mx_open_mailbox (ctx->path, 0, ctx);