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. */
24 #include "mutt_curses.h"
31 #include "imap_private.h"
32 #if defined(USE_SSL) || defined(USE_GNUTLS)
33 # include "mutt_ssl.h"
40 #include <sys/types.h>
43 /* imap forward declarations */
44 static int imap_get_delim (IMAP_DATA *idata);
45 static char* imap_get_flags (LIST** hflags, char* s);
46 static int imap_check_acl (IMAP_DATA *idata);
47 static int imap_check_capabilities (IMAP_DATA* idata);
48 static void imap_set_flag (IMAP_DATA* idata, int aclbit, int flag,
49 const char* str, char* flags, size_t flsize);
51 /* imap_access: Check permissions on an IMAP mailbox. */
52 int imap_access (const char* path, int flags)
56 char buf[LONG_STRING];
57 char mailbox[LONG_STRING];
58 char mbox[LONG_STRING];
60 if (imap_parse_path (path, &mx))
63 if (!(idata = imap_conn_find (&mx.account,
64 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW : 0)))
70 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
72 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
74 /* TODO: ACL checks. Right now we assume if it exists we can mess with it. */
75 if (mutt_bit_isset (idata->capabilities, IMAP4REV1))
76 snprintf (buf, sizeof (buf), "STATUS %s (UIDVALIDITY)", mbox);
77 else if (mutt_bit_isset (idata->capabilities, STATUS))
78 snprintf (buf, sizeof (buf), "STATUS %s (UID-VALIDITY)", mbox);
81 dprint (2, (debugfile, "imap_access: STATUS not supported?\n"));
85 if (imap_exec (idata, buf, IMAP_CMD_FAIL_OK) < 0)
87 dprint (1, (debugfile, "imap_access: Can't check STATUS of %s\n", mbox));
94 int imap_create_mailbox (IMAP_DATA* idata, char* mailbox)
96 char buf[LONG_STRING], mbox[LONG_STRING];
98 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
99 snprintf (buf, sizeof (buf), "CREATE %s", mbox);
101 if (imap_exec (idata, buf, 0) != 0)
107 int imap_delete_mailbox (CONTEXT* ctx, IMAP_MBOX mx)
109 char buf[LONG_STRING], mbox[LONG_STRING];
112 if (!ctx || !ctx->data) {
113 if (!(idata = imap_conn_find (&mx.account,
114 option (OPTIMAPPASSIVE) ? M_IMAP_CONN_NONEW : 0)))
123 imap_munge_mbox_name (mbox, sizeof (mbox), mx.mbox);
124 snprintf (buf, sizeof (buf), "DELETE %s", mbox);
126 if (imap_exec ((IMAP_DATA*) idata, buf, 0) != 0)
132 /* imap_logout_all: close all open connections. Quick and dirty until we can
133 * make sure we've got all the context we need. */
134 void imap_logout_all (void)
139 conn = mutt_socket_head ();
145 if (conn->account.type == M_ACCT_TYPE_IMAP && conn->fd >= 0)
147 mutt_message (_("Closing connection to %s..."), conn->account.host);
148 imap_logout ((IMAP_DATA*) conn->data);
150 mutt_socket_close (conn);
151 mutt_socket_free (conn);
158 /* imap_read_literal: read bytes bytes from server into file. Not explicitly
159 * buffered, relies on FILE buffering. NOTE: strips \r from \r\n.
160 * Apparently even literals use \r\n-terminated strings ?! */
161 int imap_read_literal (FILE* fp, IMAP_DATA* idata, long bytes)
168 dprint (2, (debugfile, "imap_read_literal: reading %ld bytes\n", bytes));
170 for (pos = 0; pos < bytes; pos++)
172 if (mutt_socket_readchar (idata->conn, &c) != 1)
174 dprint (1, (debugfile, "imap_read_literal: error during read, %ld bytes read\n", pos));
175 idata->status = IMAP_FATAL;
181 if (r == 1 && c != '\n')
194 if (debuglevel >= IMAP_LOG_LTRL)
195 fputc (c, debugfile);
202 /* imap_expunge_mailbox: Purge IMAP portion of expunged messages from the
203 * context. Must not be done while something has a handle on any headers
204 * (eg inside pager or editor). That is, check IMAP_REOPEN_ALLOW. */
205 void imap_expunge_mailbox (IMAP_DATA* idata)
210 for (i = 0; i < idata->ctx->msgcount; i++)
212 h = idata->ctx->hdrs[i];
216 dprint (2, (debugfile, "Expunging message UID %d.\n", HEADER_DATA (h)->uid));
220 /* free cached body from disk, if neccessary */
221 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
222 if (idata->cache[cacheno].uid == HEADER_DATA(h)->uid &&
223 idata->cache[cacheno].path)
225 unlink (idata->cache[cacheno].path);
226 FREE (&idata->cache[cacheno].path);
229 imap_free_header_data (&h->data);
233 /* We may be called on to expunge at any time. We can't rely on the caller
234 * to always know to rethread */
235 mx_update_tables (idata->ctx, 0);
236 mutt_sort_headers (idata->ctx, 1);
239 static int imap_get_delim (IMAP_DATA *idata)
244 /* assume that the delim is /. If this fails, we're in bigger trouble
245 * than getting the delim wrong */
248 imap_cmd_start (idata, "LIST \"\" \"\"");
252 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
255 s = imap_next_word (idata->cmd.buf);
256 if (ascii_strncasecmp ("LIST", s, 4) == 0)
258 s = imap_next_word (s);
259 s = imap_next_word (s);
260 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
262 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
266 while (rc == IMAP_CMD_CONTINUE);
268 if (rc != IMAP_CMD_OK)
270 dprint (1, (debugfile, "imap_get_delim: failed.\n"));
274 dprint (2, (debugfile, "Delimiter: %c\n", idata->delim));
279 /* get rights for folder, let imap_handle_untagged do the rest */
280 static int imap_check_acl (IMAP_DATA *idata)
282 char buf[LONG_STRING];
283 char mbox[LONG_STRING];
285 imap_munge_mbox_name (mbox, sizeof(mbox), idata->mailbox);
286 snprintf (buf, sizeof (buf), "MYRIGHTS %s", mbox);
287 if (imap_exec (idata, buf, 0) != 0)
289 imap_error ("imap_check_acl", buf);
295 /* imap_check_capabilities: make sure we can log in to this server. */
296 static int imap_check_capabilities (IMAP_DATA* idata)
298 if (imap_exec (idata, "CAPABILITY", 0) != 0)
300 imap_error ("imap_check_capabilities", idata->cmd.buf);
304 if (!(mutt_bit_isset(idata->capabilities,IMAP4)
305 ||mutt_bit_isset(idata->capabilities,IMAP4REV1)))
307 mutt_error _("This IMAP server is ancient. Mutt does not work with it.");
308 mutt_sleep (2); /* pause a moment to let the user see the error */
316 /* imap_conn_find: Find an open IMAP connection matching account, or open
317 * a new one if none can be found. */
318 IMAP_DATA* imap_conn_find (const ACCOUNT* account, int flags)
324 if (!(conn = mutt_conn_find (NULL, account)))
327 /* if opening a new UNSELECTED connection, preserve existing creds */
328 creds = &(conn->account);
330 /* make sure this connection is not in SELECTED state, if neccessary */
331 if (flags & M_IMAP_CONN_NOSELECT)
332 while (conn->data && ((IMAP_DATA*) conn->data)->state == IMAP_SELECTED)
334 if (!(conn = mutt_conn_find (conn, account)))
336 memcpy (&(conn->account), creds, sizeof (ACCOUNT));
339 idata = (IMAP_DATA*) conn->data;
341 /* don't open a new connection if one isn't wanted */
342 if (flags & M_IMAP_CONN_NONEW)
346 mutt_socket_free (conn);
349 if (idata->state < IMAP_AUTHENTICATED)
355 /* The current connection is a new connection */
356 if (! (idata = imap_new_idata ()))
358 mutt_socket_free (conn);
366 if (idata->state == IMAP_DISCONNECTED)
367 imap_open_connection (idata);
368 if (idata->state == IMAP_CONNECTED)
370 if (!imap_authenticate (idata))
372 idata->state = IMAP_AUTHENTICATED;
373 if (idata->conn->ssf)
374 dprint (2, (debugfile, "Communication encrypted at %d bits\n",
378 mutt_account_unsetpass (&idata->conn->account);
380 FREE (&idata->capstr);
382 if (idata->state == IMAP_AUTHENTICATED)
383 imap_get_delim (idata);
388 int imap_open_connection (IMAP_DATA* idata)
390 char buf[LONG_STRING];
392 if (mutt_socket_open (idata->conn) < 0)
395 idata->state = IMAP_CONNECTED;
397 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE) {
398 mutt_error (_("Unexpected response received from server: %s"), idata->cmd.buf);
401 mutt_socket_close (idata->conn);
402 idata->state = IMAP_DISCONNECTED;
406 if (ascii_strncasecmp ("* OK", idata->cmd.buf, 4) == 0)
408 /* TODO: Parse new tagged CAPABILITY data (* OK [CAPABILITY...]) */
409 if (imap_check_capabilities (idata))
411 #if defined(USE_SSL) || defined(USE_GNUTLS)
412 /* Attempt STARTTLS if available and desired. */
413 if (mutt_bit_isset (idata->capabilities, STARTTLS) && !idata->conn->ssf)
417 if ((rc = query_quadoption (OPT_SSLSTARTTLS,
418 _("Secure connection with TLS?"))) == -1)
421 if ((rc = imap_exec (idata, "STARTTLS", IMAP_CMD_FAIL_OK)) == -1)
426 if (mutt_ssl_starttls (idata->conn))
428 if (mutt_gnutls_starttls (idata->conn))
431 mutt_error (_("Could not negotiate TLS connection"));
437 /* RFC 2595 demands we recheck CAPABILITY after TLS completes. */
438 if (imap_exec (idata, "CAPABILITY", 0))
446 else if (ascii_strncasecmp ("* PREAUTH", idata->cmd.buf, 9) == 0)
448 idata->state = IMAP_AUTHENTICATED;
449 if (imap_check_capabilities (idata) != 0)
451 FREE (&idata->capstr);
455 imap_error ("imap_open_connection()", buf);
462 mutt_socket_close (idata->conn);
464 FREE (&idata->capstr);
468 /* imap_get_flags: Make a simple list out of a FLAGS response.
469 * return stream following FLAGS response */
470 static char* imap_get_flags (LIST** hflags, char* s)
476 /* sanity-check string */
477 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
479 dprint (1, (debugfile, "imap_get_flags: not a FLAGS response: %s\n",
487 dprint (1, (debugfile, "imap_get_flags: bogus FLAGS response: %s\n",
492 /* create list, update caller's flags handle */
493 flags = mutt_new_list();
496 while (*s && *s != ')')
501 while (*s && (*s != ')') && !ISSPACE (*s))
506 mutt_add_list (flags, flag_word);
510 /* note bad flags response */
513 dprint (1, (debugfile,
514 "imap_get_flags: Unterminated FLAGS response: %s\n", s));
515 mutt_free_list (hflags);
525 int imap_open_mailbox (CONTEXT* ctx)
529 char buf[LONG_STRING];
530 char bufout[LONG_STRING];
535 if (imap_parse_path (ctx->path, &mx))
537 mutt_error (_("%s is an invalid IMAP path"), ctx->path);
541 /* we require a connection which isn't currently in IMAP_SELECTED state */
542 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NOSELECT)))
544 if (idata->state < IMAP_AUTHENTICATED)
549 /* once again the context is new */
552 /* Clean up path and replace the one in the ctx */
553 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
554 FREE(&(idata->mailbox));
555 idata->mailbox = safe_strdup (buf);
556 imap_qualify_path (buf, sizeof (buf), &mx, idata->mailbox);
559 ctx->path = safe_strdup (buf);
563 /* clear mailbox status */
565 memset (idata->rights, 0, (RIGHTSMAX+7)/8);
566 idata->newMailCount = 0;
568 mutt_message (_("Selecting %s..."), idata->mailbox);
569 imap_munge_mbox_name (buf, sizeof(buf), idata->mailbox);
570 snprintf (bufout, sizeof (bufout), "%s %s",
571 ctx->readonly ? "EXAMINE" : "SELECT", buf);
573 idata->state = IMAP_SELECTED;
575 imap_cmd_start (idata, bufout);
581 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
584 pc = idata->cmd.buf + 2;
586 /* Obtain list of available flags here, may be overridden by a
587 * PERMANENTFLAGS tag in the OK response */
588 if (ascii_strncasecmp ("FLAGS", pc, 5) == 0)
590 /* don't override PERMANENTFLAGS */
593 dprint (2, (debugfile, "Getting mailbox FLAGS\n"));
594 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
598 /* PERMANENTFLAGS are massaged to look like FLAGS, then override FLAGS */
599 else if (ascii_strncasecmp ("OK [PERMANENTFLAGS", pc, 18) == 0)
601 dprint (2, (debugfile, "Getting mailbox PERMANENTFLAGS\n"));
602 /* safe to call on NULL */
603 mutt_free_list (&(idata->flags));
604 /* skip "OK [PERMANENT" so syntax is the same as FLAGS */
606 if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)
611 pc = imap_next_word (pc);
612 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 (imap_get_qualifier (idata->cmd.buf), "[READ-ONLY]", 11) \
636 && !mutt_bit_isset (idata->capabilities, ACL))
638 dprint (2, (debugfile, "Mailbox is read-only.\n"));
643 /* dump the mailbox flags we've found */
647 dprint (3, (debugfile, "No folder flags found\n"));
650 LIST* t = idata->flags;
652 dprint (3, (debugfile, "Mailbox flags: "));
657 dprint (3, (debugfile, "[%s] ", t->data));
660 dprint (3, (debugfile, "\n"));
665 if (mutt_bit_isset (idata->capabilities, ACL))
667 if (imap_check_acl (idata))
669 if (!(mutt_bit_isset(idata->rights, IMAP_ACL_DELETE) ||
670 mutt_bit_isset(idata->rights, IMAP_ACL_SEEN) ||
671 mutt_bit_isset(idata->rights, IMAP_ACL_WRITE) ||
672 mutt_bit_isset(idata->rights, IMAP_ACL_INSERT)))
675 /* assume we have all rights if ACL is unavailable */
678 mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);
679 mutt_bit_set (idata->rights, IMAP_ACL_READ);
680 mutt_bit_set (idata->rights, IMAP_ACL_SEEN);
681 mutt_bit_set (idata->rights, IMAP_ACL_WRITE);
682 mutt_bit_set (idata->rights, IMAP_ACL_INSERT);
683 mutt_bit_set (idata->rights, IMAP_ACL_POST);
684 mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
685 mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
689 ctx->hdrs = safe_calloc (count, sizeof (HEADER *));
690 ctx->v2r = safe_calloc (count, sizeof (int));
692 if (count && (imap_read_headers (idata, 0, count-1) < 0))
694 mutt_error _("Error opening mailbox");
699 dprint (2, (debugfile, "imap_open_mailbox: msgcount is %d\n", ctx->msgcount));
704 if (idata->state == IMAP_SELECTED)
705 idata->state = IMAP_AUTHENTICATED;
711 int imap_open_mailbox_append (CONTEXT *ctx)
715 char buf[LONG_STRING], mbox[LONG_STRING];
716 char mailbox[LONG_STRING];
720 if (imap_parse_path (ctx->path, &mx))
723 /* in APPEND mode, we appear to hijack an existing IMAP connection -
724 * ctx is brand new and mostly empty */
726 if (!(idata = imap_conn_find (&(mx.account), 0)))
733 /* check mailbox existance */
735 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
737 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
739 if (mutt_bit_isset(idata->capabilities,IMAP4REV1))
740 snprintf (buf, sizeof (buf), "STATUS %s (UIDVALIDITY)", mbox);
741 else if (mutt_bit_isset(idata->capabilities,STATUS))
742 /* We have no idea what the other guy wants. UW imapd 8.3 wants this
743 * (but it does not work if another mailbox is selected) */
744 snprintf (buf, sizeof (buf), "STATUS %s (UID-VALIDITY)", mbox);
747 /* STATUS not supported */
748 mutt_message _("Unable to append to IMAP mailboxes at this server");
753 r = imap_exec (idata, buf, IMAP_CMD_FAIL_OK);
756 /* command failed cause folder doesn't exist */
757 snprintf (buf, sizeof (buf), _("Create %s?"), mailbox);
758 if (option (OPTCONFIRMCREATE) && mutt_yesorno (buf, 1) < 1)
761 if (imap_create_mailbox (idata, mailbox) < 0)
765 /* Hmm, some other failure */
776 /* imap_logout: Gracefully log out of server. */
777 void imap_logout (IMAP_DATA* idata)
779 /* we set status here to let imap_handle_untagged know we _expect_ to
780 * receive a bye response (so it doesn't freak out and close the conn) */
781 idata->status = IMAP_BYE;
782 imap_cmd_start (idata, "LOGOUT");
783 while (imap_cmd_step (idata) == IMAP_CMD_CONTINUE)
787 int imap_close_connection (CONTEXT *ctx)
789 dprint (1, (debugfile, "imap_close_connection(): closing connection\n"));
790 /* if the server didn't shut down on us, close the connection gracefully */
791 if (CTX_DATA->status != IMAP_BYE)
793 mutt_message _("Closing connection to IMAP server...");
794 imap_logout (CTX_DATA);
797 mutt_socket_close (CTX_DATA->conn);
798 CTX_DATA->state = IMAP_DISCONNECTED;
799 CTX_DATA->conn->data = NULL;
803 /* imap_set_flag: append str to flags if we currently have permission
804 * according to aclbit */
805 static void imap_set_flag (IMAP_DATA* idata, int aclbit, int flag,
806 const char *str, char *flags, size_t flsize)
808 if (mutt_bit_isset (idata->rights, aclbit))
810 strncat (flags, str, flsize);
813 /* imap_make_msg_set: make an IMAP4rev1 UID message set out of a set of
814 * headers, given a flag enum to filter on.
815 * Params: idata: IMAP_DATA containing context containing header set
816 * buf: to write message set into
817 * buflen: length of buffer
818 * flag: enum of flag type on which to filter
819 * changed: include only changed messages in message set
820 * Returns: number of messages in message set (0 if no matches) */
821 int imap_make_msg_set (IMAP_DATA* idata, BUFFER* buf, int flag, int changed)
823 HEADER** hdrs; /* sorted local copy */
824 int count = 0; /* number of messages in message set */
825 int match = 0; /* whether current message matches flag condition */
826 unsigned int setstart = 0; /* start of current message range */
828 short oldsort; /* we clobber reverse, must restore it */
829 /* assuming 32-bit UIDs */
833 /* make copy of header pointers to sort in natural order */
834 hdrs = safe_calloc (idata->ctx->msgcount, sizeof (HEADER*));
835 memcpy (hdrs, idata->ctx->hdrs, idata->ctx->msgcount * sizeof (HEADER*));
837 if (Sort != SORT_ORDER)
841 qsort ((void*) hdrs, idata->ctx->msgcount, sizeof (HEADER*),
842 mutt_get_sort_func (SORT_ORDER));
846 for (n = 0; n < idata->ctx->msgcount; n++)
849 /* don't include pending expunged messages */
854 if (hdrs[n]->deleted)
863 if (match && (!changed || hdrs[n]->changed))
868 setstart = HEADER_DATA (hdrs[n])->uid;
871 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (hdrs[n])->uid);
872 mutt_buffer_addstr (buf, uid);
877 snprintf (uid, sizeof (uid), ",%u", HEADER_DATA (hdrs[n])->uid);
878 mutt_buffer_addstr (buf, uid);
881 /* tie up if the last message also matches */
882 else if (n == idata->ctx->msgcount-1)
884 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n])->uid);
885 mutt_buffer_addstr (buf, uid);
888 /* this message is not expunged and doesn't match. End current set. */
889 else if (setstart && hdrs[n]->active)
891 if (HEADER_DATA (hdrs[n-1])->uid > setstart)
893 snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n-1])->uid);
894 mutt_buffer_addstr (buf, uid);
905 /* update the IMAP server to reflect message changes done within mutt.
907 * ctx: the current context
908 * expunge: 0 or 1 - do expunge?
911 int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* index_hint)
914 CONTEXT* appendctx = NULL;
916 char flags[LONG_STRING];
920 int err_continue = M_NO; /* continue on error? */
923 idata = (IMAP_DATA*) ctx->data;
925 if (idata->state != IMAP_SELECTED)
927 dprint (2, (debugfile, "imap_sync_mailbox: no mailbox selected\n"));
931 /* CLOSE purges deleted messages. If we don't want to purge them, we must
932 * tell imap_close_mailbox not to issue the CLOSE command */
938 /* This function is only called when the calling code expects the context
940 imap_allow_reopen (ctx);
942 if ((rc = imap_check_mailbox (ctx, index_hint, 0)) != 0)
945 memset (&cmd, 0, sizeof (cmd));
947 /* if we are expunging anyway, we can do deleted messages very quickly... */
948 if (expunge && mutt_bit_isset (idata->rights, IMAP_ACL_DELETE))
950 mutt_buffer_addstr (&cmd, "UID STORE ");
951 deleted = imap_make_msg_set (idata, &cmd, M_DELETE, 1);
953 /* if we have a message set, then let's delete */
956 mutt_message (_("Marking %d messages deleted..."), deleted);
957 mutt_buffer_addstr (&cmd, " +FLAGS.SILENT (\\Deleted)");
958 /* mark these messages as unchanged so second pass ignores them. Done
959 * here so BOGUS UW-IMAP 4.7 SILENT FLAGS updates are ignored. */
960 for (n = 0; n < ctx->msgcount; n++)
961 if (ctx->hdrs[n]->deleted && ctx->hdrs[n]->changed)
962 ctx->hdrs[n]->active = 0;
963 if (imap_exec (idata, cmd.data, 0) != 0)
965 mutt_error (_("Expunge failed"));
973 /* save status changes */
974 for (n = 0; n < ctx->msgcount; n++)
976 if (ctx->hdrs[n]->active && ctx->hdrs[n]->changed)
978 ctx->hdrs[n]->changed = 0;
980 mutt_message (_("Saving message status flags... [%d/%d]"), n+1,
983 snprintf (uid, sizeof (uid), "%u", HEADER_DATA(ctx->hdrs[n])->uid);
985 mutt_buffer_addstr (&cmd, "UID STORE ");
986 mutt_buffer_addstr (&cmd, uid);
988 /* if the message has been rethreaded or attachments have been deleted
989 * we delete the message and reupload it.
990 * This works better if we're expunging, of course. */
991 if (ctx->hdrs[n]->refs_changed || ctx->hdrs[n]->irt_changed ||
992 ctx->hdrs[n]->attach_del)
994 dprint (3, (debugfile, "imap_sync_mailbox: Attachments to be deleted, falling back to _mutt_save_message\n"));
996 appendctx = mx_open_mailbox (ctx->path, M_APPEND | M_QUIET, NULL);
999 dprint (1, (debugfile, "imap_sync_mailbox: Error opening mailbox in append mode\n"));
1002 _mutt_save_message (ctx->hdrs[n], appendctx, 1, 0, 0);
1006 imap_set_flag (idata, IMAP_ACL_SEEN, ctx->hdrs[n]->read, "\\Seen ",
1007 flags, sizeof (flags));
1008 imap_set_flag (idata, IMAP_ACL_WRITE, ctx->hdrs[n]->flagged,
1009 "\\Flagged ", flags, sizeof (flags));
1010 imap_set_flag (idata, IMAP_ACL_WRITE, ctx->hdrs[n]->replied,
1011 "\\Answered ", flags, sizeof (flags));
1012 imap_set_flag (idata, IMAP_ACL_DELETE, ctx->hdrs[n]->deleted,
1013 "\\Deleted ", flags, sizeof (flags));
1015 /* now make sure we don't lose custom tags */
1016 if (mutt_bit_isset (idata->rights, IMAP_ACL_WRITE))
1017 imap_add_keywords (flags, ctx->hdrs[n], idata->flags, sizeof (flags));
1019 mutt_remove_trailing_ws (flags);
1021 /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
1022 * explicitly revoke all system flags (if we have permission) */
1025 imap_set_flag (idata, IMAP_ACL_SEEN, 1, "\\Seen ", flags, sizeof (flags));
1026 imap_set_flag (idata, IMAP_ACL_WRITE, 1, "\\Flagged ", flags, sizeof (flags));
1027 imap_set_flag (idata, IMAP_ACL_WRITE, 1, "\\Answered ", flags, sizeof (flags));
1028 imap_set_flag (idata, IMAP_ACL_DELETE, 1, "\\Deleted ", flags, sizeof (flags));
1030 mutt_remove_trailing_ws (flags);
1032 mutt_buffer_addstr (&cmd, " -FLAGS.SILENT (");
1035 mutt_buffer_addstr (&cmd, " FLAGS.SILENT (");
1037 mutt_buffer_addstr (&cmd, flags);
1038 mutt_buffer_addstr (&cmd, ")");
1040 /* dumb hack for bad UW-IMAP 4.7 servers spurious FLAGS updates */
1041 ctx->hdrs[n]->active = 0;
1043 /* after all this it's still possible to have no flags, if you
1044 * have no ACL rights */
1045 if (*flags && (imap_exec (idata, cmd.data, 0) != 0) &&
1046 (err_continue != M_YES))
1048 err_continue = imap_continue ("imap_sync_mailbox: STORE failed",
1050 if (err_continue != M_YES)
1057 ctx->hdrs[n]->active = 1;
1062 /* We must send an EXPUNGE command if we're not closing. */
1063 if (expunge && !(ctx->closing) &&
1064 mutt_bit_isset(idata->rights, IMAP_ACL_DELETE))
1066 mutt_message _("Expunging messages from server...");
1067 /* Set expunge bit so we don't get spurious reopened messages */
1068 idata->reopen |= IMAP_EXPUNGE_EXPECTED;
1069 if (imap_exec (idata, "EXPUNGE", 0) != 0)
1071 imap_error (_("imap_sync_mailbox: EXPUNGE failed"), idata->cmd.buf);
1083 mx_fastclose_mailbox (appendctx);
1089 /* imap_close_mailbox: issue close command if neccessary, reset IMAP_DATA */
1090 void imap_close_mailbox (CONTEXT* ctx)
1095 idata = (IMAP_DATA*) ctx->data;
1096 /* Check to see if the mailbox is actually open */
1100 if ((idata->status != IMAP_FATAL) &&
1101 (idata->state == IMAP_SELECTED) &&
1102 (ctx == idata->ctx))
1104 if (!(idata->noclose) && imap_exec (idata, "CLOSE", 0))
1105 mutt_error (_("CLOSE failed"));
1107 idata->reopen &= IMAP_REOPEN_ALLOW;
1108 idata->state = IMAP_AUTHENTICATED;
1109 FREE (&(idata->mailbox));
1110 mutt_free_list (&idata->flags);
1114 /* free IMAP part of headers */
1115 for (i = 0; i < ctx->msgcount; i++)
1116 imap_free_header_data (&(ctx->hdrs[i]->data));
1118 for (i = 0; i < IMAP_CACHE_LEN; i++)
1120 if (idata->cache[i].path)
1122 unlink (idata->cache[i].path);
1123 FREE (&idata->cache[i].path);
1128 /* use the NOOP command to poll for new mail
1131 * M_REOPENED mailbox has been externally modified
1132 * M_NEW_MAIL new mail has arrived!
1136 int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force)
1138 /* overload keyboard timeout to avoid many mailbox checks in a row.
1139 * Most users don't like having to wait exactly when they press a key. */
1143 idata = (IMAP_DATA*) ctx->data;
1145 if ((force || time(NULL) > idata->lastread + Timeout)
1146 && imap_exec (idata, "NOOP", 0) != 0)
1149 /* We call this even when we haven't run NOOP in case we have pending
1150 * changes to process, since we can reopen here. */
1151 imap_cmd_finish (idata);
1153 if (idata->check_status & IMAP_EXPUNGE_PENDING)
1154 result = M_REOPENED;
1155 else if (idata->check_status & IMAP_NEWMAIL_PENDING)
1156 result = M_NEW_MAIL;
1157 else if (idata->check_status & IMAP_FLAGS_PENDING)
1160 idata->check_status = 0;
1165 /* returns count of recent messages if new = 1, else count of total messages.
1166 * (useful for at least postponed function)
1167 * Question of taste: use RECENT or UNSEEN for new?
1168 * 0+ number of messages in mailbox
1169 * -1 error while polling mailboxes
1171 int imap_mailbox_check (char* path, int new)
1175 char buf[LONG_STRING];
1176 char mbox[LONG_STRING];
1177 char mbox_unquoted[LONG_STRING];
1184 if (imap_parse_path (path, &mx))
1187 /* If imap_passive is set, don't open a connection to check for new mail */
1188 if (option (OPTIMAPPASSIVE))
1189 connflags = M_IMAP_CONN_NONEW;
1191 if (!(idata = imap_conn_find (&(mx.account), connflags)))
1198 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1201 imap_munge_mbox_name (mbox, sizeof(mbox), buf);
1202 strfcpy (mbox_unquoted, buf, sizeof (mbox_unquoted));
1204 /* The draft IMAP implementor's guide warns againts using the STATUS
1205 * command on a mailbox that you have selected
1208 if (mutt_strcmp (mbox_unquoted, idata->mailbox) == 0
1209 || (ascii_strcasecmp (mbox_unquoted, "INBOX") == 0
1210 && mutt_strcasecmp (mbox_unquoted, idata->mailbox) == 0))
1212 strfcpy (buf, "NOOP", sizeof (buf));
1214 else if (mutt_bit_isset(idata->capabilities,IMAP4REV1) ||
1215 mutt_bit_isset(idata->capabilities,STATUS))
1217 snprintf (buf, sizeof (buf), "STATUS %s (%s)", mbox,
1218 new ? "RECENT" : "MESSAGES");
1221 /* Server does not support STATUS, and this is not the current mailbox.
1222 * There is no lightweight way to check recent arrivals */
1225 imap_cmd_start (idata, buf);
1229 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
1232 s = imap_next_word (idata->cmd.buf);
1233 if (ascii_strncasecmp ("STATUS", s, 6) == 0)
1235 s = imap_next_word (s);
1236 /* The mailbox name may or may not be quoted here. We could try to
1237 * munge the server response and compare with quoted (or vise versa)
1238 * but it is probably more efficient to just strncmp against both. */
1239 if (mutt_strncmp (mbox_unquoted, s, mutt_strlen (mbox_unquoted)) == 0
1240 || mutt_strncmp (mbox, s, mutt_strlen (mbox)) == 0)
1242 s = imap_next_word (s);
1243 s = imap_next_word (s);
1244 if (isdigit ((unsigned char) *s))
1249 dprint (2, (debugfile, "%d new messages in %s\n", msgcount, path));
1254 dprint (1, (debugfile, "imap_mailbox_check: STATUS response doesn't match requested mailbox.\n"));
1257 while (rc == IMAP_CMD_CONTINUE);
1262 /* all this listing/browsing is a mess. I don't like that name is a pointer
1263 * into idata->buf (used to be a pointer into the passed in buffer, just
1264 * as bad), nor do I like the fact that the fetch is done here. This
1265 * code can't possibly handle non-LIST untagged responses properly.
1267 int imap_parse_list_response(IMAP_DATA* idata, char **name, int *noselect,
1268 int *noinferiors, char *delim)
1276 rc = imap_cmd_step (idata);
1277 if (rc == IMAP_CMD_OK)
1279 if (rc != IMAP_CMD_CONTINUE)
1282 s = imap_next_word (idata->cmd.buf);
1283 if ((ascii_strncasecmp ("LIST", s, 4) == 0) ||
1284 (ascii_strncasecmp ("LSUB", s, 4) == 0))
1289 s = imap_next_word (s); /* flags */
1296 while (*ep && *ep != ')') ep++;
1299 if (!ascii_strncasecmp (s, "\\NoSelect", 9))
1301 if (!ascii_strncasecmp (s, "\\NoInferiors", 12))
1303 /* See draft-gahrns-imap-child-mailbox-?? */
1304 if (!ascii_strncasecmp (s, "\\HasNoChildren", 14))
1308 while (*s && *s != '\\' && *s != ')') s++;
1313 s = imap_next_word (s); /* delim */
1314 /* Reset the delimiter, this can change */
1315 if (ascii_strncasecmp (s, "NIL", 3))
1317 if (s && s[0] == '\"' && s[1] && s[2] == '\"')
1319 else if (s && s[0] == '\"' && s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
1322 s = imap_next_word (s); /* name */
1323 if (s && *s == '{') /* Literal */
1325 if (imap_get_literal_count(idata->cmd.buf, &bytes) < 0)
1327 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1329 *name = idata->cmd.buf;
1338 int imap_subscribe (char *path, int subscribe)
1342 char buf[LONG_STRING];
1343 char mbox[LONG_STRING];
1346 if (!mx_is_imap (path) || imap_parse_path (path, &mx))
1348 mutt_error (_("Bad mailbox name"));
1353 if (!(idata = imap_conn_find (&(mx.account), 0)))
1358 imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
1360 mutt_message (_("Subscribing to %s..."), buf);
1362 mutt_message (_("Unsubscribing to %s..."), buf);
1363 imap_munge_mbox_name (mbox, sizeof(mbox), buf);
1365 snprintf (buf, sizeof (buf), "%s %s", subscribe ? "SUBSCRIBE" :
1366 "UNSUBSCRIBE", mbox);
1368 if (imap_exec (idata, buf, 0) < 0)
1379 /* imap_complete: given a partial IMAP folder path, return a string which
1380 * adds as much to the path as is unique */
1381 int imap_complete(char* dest, size_t dlen, char* path) {
1384 char list[LONG_STRING];
1385 char buf[LONG_STRING];
1386 char* list_word = NULL;
1387 int noselect, noinferiors;
1389 char completion[LONG_STRING];
1390 int clen, matchlen = 0;
1391 int completions = 0;
1395 /* verify passed in path is an IMAP path */
1396 if (imap_parse_path (path, &mx))
1398 dprint(2, (debugfile, "imap_complete: bad path %s\n", path));
1402 /* don't open a new socket just for completion */
1403 if (!(idata = imap_conn_find (&(mx.account), M_IMAP_CONN_NONEW)))
1407 /* reformat path for IMAP list, and append wildcard */
1408 /* don't use INBOX in place of "" */
1409 if (mx.mbox && mx.mbox[0])
1410 imap_fix_path (idata, mx.mbox, list, sizeof(list));
1414 /* fire off command */
1415 snprintf (buf, sizeof(buf), "%s \"\" \"%s%%\"",
1416 option (OPTIMAPLSUB) ? "LSUB" : "LIST", list);
1418 imap_cmd_start (idata, buf);
1420 /* and see what the results are */
1421 strfcpy (completion, NONULL(mx.mbox), sizeof(completion));
1424 if (imap_parse_list_response(idata, &list_word, &noselect, &noinferiors,
1430 /* store unquoted */
1431 imap_unmunge_mbox_name (list_word);
1433 /* if the folder isn't selectable, append delimiter to force browse
1434 * to enter it on second tab. */
1437 clen = strlen(list_word);
1438 list_word[clen++] = delim;
1439 list_word[clen] = '\0';
1441 /* copy in first word */
1444 strfcpy (completion, list_word, sizeof(completion));
1445 matchlen = strlen (completion);
1451 while (pos < matchlen && list_word[pos] &&
1452 completion[pos] == list_word[pos])
1454 completion[pos] = '\0';
1460 while (ascii_strncmp(idata->cmd.seq, idata->cmd.buf, SEQLEN));
1464 /* reformat output */
1465 imap_qualify_path (dest, dlen, &mx, completion);
1466 mutt_pretty_mailbox (dest);