NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
imap.c
Go to the documentation of this file.
1
34
42
43#include "config.h"
44#include <errno.h>
45#include <limits.h>
46#include <stdbool.h>
47#include <stdint.h>
48#include <stdio.h>
49#include <string.h>
50#include <time.h>
51#include "private.h"
52#include "mutt/lib.h"
53#include "config/lib.h"
54#include "email/lib.h"
55#include "core/lib.h"
56#include "conn/lib.h"
57#include "mutt.h"
58#include "lib.h"
59#include "commands/lib.h"
60#include "editor/lib.h"
61#include "history/lib.h"
62#include "hooks/lib.h"
63#include "parse/lib.h"
64#include "progress/lib.h"
65#include "question/lib.h"
66#include "adata.h"
67#include "auth.h"
68#include "edata.h"
69#include "external.h"
70#include "mdata.h"
71#include "msg_set.h"
72#include "msn.h"
73#include "mutt_logging.h"
74#include "mutt_socket.h"
75#include "muttlib.h"
76#include "mx.h"
77#ifdef ENABLE_NLS
78#include <libintl.h>
79#endif
80
81struct Progress;
82struct stat;
83
93enum CommandResult parse_subscribe_to(const struct Command *cmd, struct Buffer *line,
94 const struct ParseContext *pc, struct ParseError *pe)
95{
96 struct Buffer *err = pe->message;
97
98 if (!MoreArgs(line))
99 {
100 buf_printf(err, _("%s: too few arguments"), cmd->name);
101 return MUTT_CMD_WARNING;
102 }
103
104 struct Buffer *token = buf_pool_get();
106
107 buf_reset(err);
108
110
111 if (MoreArgs(line))
112 {
113 buf_printf(err, _("%s: too many arguments"), cmd->name);
114 goto done;
115 }
116
117 // Expand and subscribe
118 expand_path(token, false);
119 if (imap_subscribe(buf_string(token), true) != 0)
120 {
121 buf_printf(err, _("Could not subscribe to %s"), buf_string(token));
122 rc = MUTT_CMD_ERROR;
123 goto done;
124 }
125
126 mutt_message(_("Subscribed to %s"), buf_string(token));
127 rc = MUTT_CMD_SUCCESS;
128
129done:
130 buf_pool_release(&token);
131 return rc;
132}
133
143enum CommandResult parse_unsubscribe_from(const struct Command *cmd, struct Buffer *line,
144 const struct ParseContext *pc,
145 struct ParseError *pe)
146{
147 struct Buffer *err = pe->message;
148
149 if (!MoreArgs(line))
150 {
151 buf_printf(err, _("%s: too few arguments"), cmd->name);
152 return MUTT_CMD_WARNING;
153 }
154
155 struct Buffer *token = buf_pool_get();
157
159
160 if (MoreArgs(line))
161 {
162 buf_printf(err, _("%s: too many arguments"), cmd->name);
163 goto done;
164 }
165
166 // Expand and unsubscribe
167 expand_path(token, false);
168 if (imap_subscribe(buf_string(token), false) != 0)
169 {
170 buf_printf(err, _("Could not unsubscribe from %s"), buf_string(token));
171 rc = MUTT_CMD_ERROR;
172 goto done;
173 }
174
175 mutt_message(_("Unsubscribed from %s"), buf_string(token));
176 rc = MUTT_CMD_SUCCESS;
177
178done:
179 buf_pool_release(&token);
180 return rc;
181}
182
186const struct Command ImapCommands[] = {
187 // clang-format off
188 { "subscribe-to", CMD_SUBSCRIBE_TO, parse_subscribe_to,
189 N_("Subscribe to an IMAP mailbox"),
190 N_("subscribe-to <imap-folder-uri>"),
191 "optionalfeatures.html#imap" },
192 { "unsubscribe-from", CMD_UNSUBSCRIBE_FROM, parse_unsubscribe_from,
193 N_("Unsubscribe from an IMAP mailbox"),
194 N_("unsubscribe-from <imap-folder-uri>"),
195 "optionalfeatures.html#imap" },
196
197 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NO_FLAGS },
198 // clang-format on
199};
200
207static int check_capabilities(struct ImapAccountData *adata)
208{
209 if (imap_exec(adata, "CAPABILITY", IMAP_CMD_NO_FLAGS) != IMAP_EXEC_SUCCESS)
210 {
211 imap_error("check_capabilities", adata->buf);
212 return -1;
213 }
214
215 if (!((adata->capabilities & IMAP_CAP_IMAP4) || (adata->capabilities & IMAP_CAP_IMAP4REV1)))
216 {
217 mutt_error(_("This IMAP server is ancient. NeoMutt does not work with it."));
218 return -1;
219 }
220
221 return 0;
222}
223
233static char *get_flags(struct ListHead *hflags, char *s)
234{
235 /* sanity-check string */
236 const size_t plen = mutt_istr_startswith(s, "FLAGS");
237 if (plen == 0)
238 {
239 mutt_debug(LL_DEBUG1, "not a FLAGS response: %s\n", s);
240 return NULL;
241 }
242 s += plen;
243 SKIPWS(s);
244 if (*s != '(')
245 {
246 mutt_debug(LL_DEBUG1, "bogus FLAGS response: %s\n", s);
247 return NULL;
248 }
249
250 /* update caller's flags handle */
251 while (*s && (*s != ')'))
252 {
253 s++;
254 SKIPWS(s);
255 const char *flag_word = s;
256 while (*s && (*s != ')') && !mutt_isspace(*s))
257 s++;
258 const char ctmp = *s;
259 *s = '\0';
260 if (*flag_word)
261 mutt_list_insert_tail(hflags, mutt_str_dup(flag_word));
262 *s = ctmp;
263 }
264
265 /* note bad flags response */
266 if (*s != ')')
267 {
268 mutt_debug(LL_DEBUG1, "Unterminated FLAGS response: %s\n", s);
269 mutt_list_free(hflags);
270
271 return NULL;
272 }
273
274 s++;
275
276 return s;
277}
278
287static void set_flag(struct Mailbox *m, AclFlags aclflag, bool flag,
288 const char *str, struct Buffer *flags)
289{
290 struct ImapMboxData *mdata = imap_mdata_get(m);
291 if (!mdata)
292 return;
293
294 if (m->rights & aclflag)
295 if (flag && imap_has_flag(&mdata->flags, str))
296 buf_addstr(flags, str);
297}
298
307static bool compare_flags_for_copy(struct Email *e)
308{
309 struct ImapEmailData *edata = e->edata;
310
311 if (e->read != edata->read)
312 return true;
313 if (e->old != edata->old)
314 return true;
315 if (e->flagged != edata->flagged)
316 return true;
317 if (e->replied != edata->replied)
318 return true;
319
320 return false;
321}
322
334static int select_email_uids(struct Email **emails, int num_emails, enum MessageType flag,
335 bool changed, bool invert, struct UidArray *uida)
336{
337 if (!emails || !uida)
338 return -1;
339
340 for (int i = 0; i < num_emails; i++)
341 {
342 struct Email *e = emails[i];
343 if (!e || (changed && !e->changed))
344 continue;
345
346 /* don't include pending expunged messages.
347 *
348 * TODO: can we unset active in cmd_parse_expunge() and
349 * cmd_parse_vanished() instead of checking for index != INT_MAX. */
350 if (!e || !e->active || (e->index == INT_MAX))
351 continue;
352
354
355 bool match = false;
356 switch (flag)
357 {
358 case MUTT_DELETED:
359 if (e->deleted != edata->deleted)
360 match = invert ^ e->deleted;
361 break;
362 case MUTT_FLAG:
363 if (e->flagged != edata->flagged)
364 match = invert ^ e->flagged;
365 break;
366 case MUTT_OLD:
367 if (e->old != edata->old)
368 match = invert ^ e->old;
369 break;
370 case MUTT_READ:
371 if (e->read != edata->read)
372 match = invert ^ e->read;
373 break;
374 case MUTT_REPLIED:
375 if (e->replied != edata->replied)
376 match = invert ^ e->replied;
377 break;
378 case MUTT_TRASH:
379 if (e->deleted && !e->purge)
380 match = true;
381 break;
382 default:
383 break;
384 }
385
386 if (match)
387 ARRAY_ADD(uida, edata->uid);
388 }
389
390 return ARRAY_SIZE(uida);
391}
392
404static int sync_helper(struct Mailbox *m, struct Email **emails, int num_emails,
405 AclFlags right, enum MessageType flag, const char *name)
406{
408 struct ImapMboxData *mdata = imap_mdata_get(m);
409 if (!adata || !mdata)
410 return -1;
411
412 if ((m->rights & right) == 0)
413 return 0;
414
415 if ((right == MUTT_ACL_WRITE) && !imap_has_flag(&mdata->flags, name))
416 return 0;
417
418 int count = 0;
419 char buf[1024] = { 0 };
420
421 struct UidArray uida = ARRAY_HEAD_INITIALIZER;
422
423 // Set the flag (+FLAGS) on matching emails
424 select_email_uids(emails, num_emails, flag, true, false, &uida);
425 snprintf(buf, sizeof(buf), "+FLAGS.SILENT (%s)", name);
426 int rc = imap_exec_msg_set(adata, "UID STORE", buf, &uida);
427 if (rc < 0)
428 return rc;
429 count += rc;
430 ARRAY_FREE(&uida);
431
432 // Clear the flag (-FLAGS) on non-matching emails
433 select_email_uids(emails, num_emails, flag, true, true, &uida);
434 buf[0] = '-';
435 rc = imap_exec_msg_set(adata, "UID STORE", buf, &uida);
436 if (rc < 0)
437 return rc;
438 count += rc;
439 ARRAY_FREE(&uida);
440
441 return count;
442}
443
453static size_t longest_common_prefix(struct Buffer *buf, const char *src, size_t start)
454{
455 size_t pos = start;
456
457 size_t len = buf_len(buf);
458 while ((pos < len) && (buf_at(buf, pos) != '\0') && (buf_at(buf, pos) == src[pos]))
459 pos++;
460 buf->data[pos] = '\0';
461
462 buf_fix_dptr(buf);
463
464 return pos;
465}
466
476static int complete_hosts(struct Buffer *buf)
477{
478 int rc = -1;
479 size_t matchlen;
480
481 matchlen = buf_len(buf);
482 struct MailboxArray ma = neomutt_mailboxes_get(NeoMutt, MUTT_MAILBOX_ANY);
483 struct Mailbox **mp = NULL;
484 ARRAY_FOREACH(mp, &ma)
485 {
486 struct Mailbox *m = *mp;
487
489 continue;
490
491 if (rc)
492 {
493 buf_strcpy(buf, mailbox_path(m));
494 rc = 0;
495 }
496 else
497 {
498 longest_common_prefix(buf, mailbox_path(m), matchlen);
499 }
500 }
501 ARRAY_FREE(&ma); // Clean up the ARRAY, but not the Mailboxes
502
503#if 0
504 TAILQ_FOREACH(conn, mutt_socket_head(), entries)
505 {
506 struct Url url = { 0 };
507 char urlstr[1024] = { 0 };
508
509 if (conn->account.type != MUTT_ACCT_TYPE_IMAP)
510 continue;
511
512 account_to_url(&conn->account, &url);
513 /* FIXME: how to handle multiple users on the same host? */
514 url.user = NULL;
515 url.path = NULL;
516 url_tostring(&url, urlstr, sizeof(urlstr), U_NO_FLAGS);
517 if (mutt_strn_equal(buf, urlstr, matchlen))
518 {
519 if (rc)
520 {
521 mutt_str_copy(buf, urlstr, buflen);
522 rc = 0;
523 }
524 else
525 {
526 longest_common_prefix(buf, urlstr, matchlen);
527 }
528 }
529 }
530#endif
531
532 return rc;
533}
534
542int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
543{
544 char buf[2048] = { 0 };
545 char mbox[1024] = { 0 };
546
547 imap_munge_mbox_name(adata->unicode, mbox, sizeof(mbox), mailbox);
548 snprintf(buf, sizeof(buf), "CREATE %s", mbox);
549
551 {
552 mutt_error(_("CREATE failed: %s"), imap_cmd_trailer(adata));
553 return -1;
554 }
555
556 return 0;
557}
558
569int imap_access(const char *path)
570{
571 if (imap_path_status(path, false) >= 0)
572 return 0;
573 return -1;
574}
575
584int imap_rename_mailbox(struct ImapAccountData *adata, char *oldname, const char *newname)
585{
586 char oldmbox[1024] = { 0 };
587 char newmbox[1024] = { 0 };
588 int rc = 0;
589
590 imap_munge_mbox_name(adata->unicode, oldmbox, sizeof(oldmbox), oldname);
591 imap_munge_mbox_name(adata->unicode, newmbox, sizeof(newmbox), newname);
592
593 struct Buffer *buf = buf_pool_get();
594 buf_printf(buf, "RENAME %s %s", oldmbox, newmbox);
595
597 rc = -1;
598
599 buf_pool_release(&buf);
600
601 return rc;
602}
603
611int imap_delete_mailbox(struct Mailbox *m, char *path)
612{
613 char buf[PATH_MAX + 7];
614 char mbox[PATH_MAX] = { 0 };
615
617 if (!adata)
618 return -1;
619
620 struct Url *url = url_parse(path);
621 if (!url)
622 return -1;
623
624 imap_munge_mbox_name(adata->unicode, mbox, sizeof(mbox), url->path);
625 url_free(&url);
626 snprintf(buf, sizeof(buf), "DELETE %s", mbox);
628 return -1;
629
630 return 0;
631}
632
637static void imap_logout(struct ImapAccountData *adata)
638{
639 if (adata->status != IMAP_FATAL)
640 {
641 /* we set status here to let imap_handle_untagged know we _expect_ to
642 * receive a bye response (so it doesn't freak out and close the conn) */
643 if (adata->state == IMAP_DISCONNECTED)
644 {
645 return;
646 }
647
648 adata->status = IMAP_BYE;
649 imap_cmd_start(adata, "LOGOUT");
650 const short c_imap_poll_timeout = cs_subset_number(NeoMutt->sub, "imap_poll_timeout");
651 if ((c_imap_poll_timeout <= 0) ||
652 (mutt_socket_poll(adata->conn, c_imap_poll_timeout) != 0))
653 {
654 while (imap_cmd_step(adata) == IMAP_RES_CONTINUE)
655 ; // do nothing
656 }
657 }
658 mutt_socket_close(adata->conn);
659 adata->state = IMAP_DISCONNECTED;
660}
661
668{
669 struct Account **ap = NULL;
671 {
672 struct Account *a = *ap;
673 if (a->type != MUTT_IMAP)
674 continue;
675
676 struct ImapAccountData *adata = a->adata;
677 if (!adata)
678 continue;
679
680 struct Connection *conn = adata->conn;
681 if (!conn || (conn->fd < 0))
682 continue;
683
684 mutt_message(_("Closing connection to %s..."), conn->account.host);
685 imap_logout(a->adata);
687 }
688}
689
704int imap_read_literal(FILE *fp, struct ImapAccountData *adata,
705 unsigned long bytes, struct Progress *progress)
706{
707 char c;
708 bool r = false;
709 struct Buffer buf = { 0 }; // Do not allocate, maybe it won't be used
710
711 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
712 if (c_debug_level >= IMAP_LOG_LTRL)
713 buf_alloc(&buf, bytes + 1);
714
715 mutt_debug(LL_DEBUG2, "reading %lu byte literal from server\n", bytes);
716
717 /* For large transfers, calculate checkpoint for progress logging */
718 unsigned long checkpoint = bytes / 10; // Log every 10%
719 if (checkpoint == 0)
720 checkpoint = bytes; // For small transfers, don't log progress
721
722 time_t start_time = mutt_date_now();
723 time_t last_progress = start_time;
724 time_t last_activity = start_time;
725
726 /* Get timeout value - use imap_poll_timeout or default to 60 seconds */
727 const short c_imap_poll_timeout = cs_subset_number(NeoMutt->sub, "imap_poll_timeout");
728 const int stall_timeout = (c_imap_poll_timeout > 0) ? c_imap_poll_timeout : 60;
729
730 for (unsigned long pos = 0; pos < bytes; pos++)
731 {
732 /* Check for user interrupt (Ctrl-C) periodically */
733 if ((pos % 4096) == 0)
734 {
735 if (SigInt)
736 {
737 mutt_debug(LL_DEBUG1, "Literal read interrupted by user at %lu/%lu bytes\n",
738 pos, bytes);
739 mutt_error(_("Download interrupted"));
740 SigInt = false;
741 adata->status = IMAP_FATAL;
742 buf_dealloc(&buf);
743 return -1;
744 }
745
746 /* Check for stalled transfer */
747 time_t now = mutt_date_now();
748 if ((now - last_activity) > stall_timeout)
749 {
750 mutt_debug(LL_DEBUG1, "Literal read stalled at %lu/%lu bytes (no data for %d seconds)\n",
751 pos, bytes, stall_timeout);
752 mutt_error(_("Download stalled - no data received for %d seconds"), stall_timeout);
753 adata->status = IMAP_FATAL;
754 buf_dealloc(&buf);
755 return -1;
756 }
757 }
758
759 if (mutt_socket_readchar(adata->conn, &c) != 1)
760 {
761 time_t duration = mutt_date_now() - start_time;
762 mutt_debug(LL_DEBUG1, "Error during literal read at byte %lu/%lu (%.1f%% complete)\n",
763 pos, bytes, (bytes > 0) ? ((double) pos / bytes * 100.0) : 0.0);
764 mutt_debug(LL_DEBUG1, "Read failed after %ld seconds (errno=%d: %s)\n",
765 (long) duration, errno, strerror(errno));
766 adata->status = IMAP_FATAL;
767
768 buf_dealloc(&buf);
769 return -1;
770 }
771
772 last_activity = mutt_date_now();
773
774 if (r && (c != '\n'))
775 fputc('\r', fp);
776
777 if (c == '\r')
778 {
779 r = true;
780 continue;
781 }
782 else
783 {
784 r = false;
785 }
786
787 fputc(c, fp);
788
789 if ((pos % 1024) == 0)
790 progress_update(progress, pos, -1);
791
792 /* Log progress every 10% for large transfers */
793 if ((checkpoint > 0) && ((pos % checkpoint) == 0) && (pos > 0))
794 {
795 time_t now = mutt_date_now();
796 if (now > last_progress)
797 {
798 mutt_debug(LL_DEBUG2, "Literal read progress: %lu/%lu bytes (%.1f%%)\n",
799 pos, bytes, ((double) pos / bytes * 100.0));
800 last_progress = now;
801 }
802 }
803
804 if (c_debug_level >= IMAP_LOG_LTRL)
805 buf_addch(&buf, c);
806 }
807
808 time_t duration = mutt_date_now() - start_time;
809 if (duration > 0)
810 {
811 mutt_debug(LL_DEBUG2, "Literal read complete: %lu bytes in %ld seconds\n",
812 bytes, (long) duration);
813 }
814
815 if (c_debug_level >= IMAP_LOG_LTRL)
816 {
817 mutt_debug(IMAP_LOG_LTRL, "\n%s", buf.data);
818 buf_dealloc(&buf);
819 }
820 return 0;
821}
822
828void imap_notify_delete_email(struct Mailbox *m, struct Email *e)
829{
830 struct ImapMboxData *mdata = imap_mdata_get(m);
832
833 if (!mdata || !edata)
834 return;
835
836 imap_msn_remove(&mdata->msn, edata->msn - 1);
837 edata->msn = 0;
838}
839
849void imap_expunge_mailbox(struct Mailbox *m, bool resort)
850{
852 struct ImapMboxData *mdata = imap_mdata_get(m);
853 if (!adata || !mdata)
854 return;
855
856 struct Email *e = NULL;
857
858#ifdef USE_HCACHE
859 imap_hcache_open(adata, mdata, false);
860#endif
861
862 for (int i = 0; i < m->msg_count; i++)
863 {
864 e = m->emails[i];
865 if (!e)
866 break;
867
868 if (e->index == INT_MAX)
869 {
870 mutt_debug(LL_DEBUG2, "Expunging message UID %u\n", imap_edata_get(e)->uid);
871
872 e->deleted = true;
873
874 imap_cache_del(m, e);
875#ifdef USE_HCACHE
876 imap_hcache_del(mdata, imap_edata_get(e)->uid);
877#endif
878
879 mutt_hash_int_delete(mdata->uid_hash, imap_edata_get(e)->uid, e);
880
881 imap_edata_free((void **) &e->edata);
882 }
883 else
884 {
885 /* NeoMutt has several places where it turns off e->active as a
886 * hack. For example to avoid FLAG updates, or to exclude from
887 * imap_exec_msg_set.
888 *
889 * Unfortunately, when a reopen is allowed and the IMAP_EXPUNGE_PENDING
890 * flag becomes set (e.g. a flag update to a modified header),
891 * this function will be called by imap_cmd_finish().
892 *
893 * The ctx_update_tables() will free and remove these "inactive" headers,
894 * despite that an EXPUNGE was not received for them.
895 * This would result in memory leaks and segfaults due to dangling
896 * pointers in the msn_index and uid_hash.
897 *
898 * So this is another hack to work around the hacks. We don't want to
899 * remove the messages, so make sure active is on. */
900 e->active = true;
901 }
902 }
903
904#ifdef USE_HCACHE
905 imap_hcache_close(mdata);
906#endif
907
909 if (resort)
910 {
912 }
913}
914
922{
923 if (mutt_socket_open(adata->conn) < 0)
924 return -1;
925
926 adata->state = IMAP_CONNECTED;
927
928 if (imap_cmd_step(adata) != IMAP_RES_OK)
929 {
931 return -1;
932 }
933
934 if (mutt_istr_startswith(adata->buf, "* OK"))
935 {
936 if (!mutt_istr_startswith(adata->buf, "* OK [CAPABILITY") && check_capabilities(adata))
937 {
938 goto bail;
939 }
940#ifdef USE_SSL
941 /* Attempt STARTTLS if available and desired. */
942 const bool c_ssl_force_tls = cs_subset_bool(NeoMutt->sub, "ssl_force_tls");
943 if ((adata->conn->ssf == 0) &&
944 (c_ssl_force_tls || (adata->capabilities & IMAP_CAP_STARTTLS)))
945 {
946 enum QuadOption ans;
947
948 if (c_ssl_force_tls)
949 {
950 ans = MUTT_YES;
951 }
952 else if ((ans = query_quadoption(_("Secure connection with TLS?"),
953 NeoMutt->sub, "ssl_starttls")) == MUTT_ABORT)
954 {
955 goto bail;
956 }
957 if (ans == MUTT_YES)
958 {
959 enum ImapExecResult rc = imap_exec(adata, "STARTTLS", IMAP_CMD_SINGLE);
960 // Clear any data after the STARTTLS acknowledgement
961 mutt_socket_empty(adata->conn);
962
963 if (rc == IMAP_EXEC_FATAL)
964 goto bail;
965 if (rc != IMAP_EXEC_ERROR)
966 {
967 if (mutt_ssl_starttls(adata->conn))
968 {
969 mutt_error(_("Could not negotiate TLS connection"));
970 goto bail;
971 }
972 else
973 {
974 /* RFC2595 demands we recheck CAPABILITY after TLS completes. */
975 if (imap_exec(adata, "CAPABILITY", IMAP_CMD_NO_FLAGS) != IMAP_EXEC_SUCCESS)
976 goto bail;
977 }
978 }
979 }
980 }
981
982 if (c_ssl_force_tls && (adata->conn->ssf == 0))
983 {
984 mutt_error(_("Encrypted connection unavailable"));
985 goto bail;
986 }
987#endif
988 }
989 else if (mutt_istr_startswith(adata->buf, "* PREAUTH"))
990 {
991#ifdef USE_SSL
992 /* Unless using a secure $tunnel, an unencrypted PREAUTH response may be a
993 * MITM attack. The only way to stop "STARTTLS" MITM attacks is via
994 * $ssl_force_tls: an attacker can easily spoof "* OK" and strip the
995 * STARTTLS capability. So consult $ssl_force_tls, not $ssl_starttls, to
996 * decide whether to abort. Note that if using $tunnel and
997 * $tunnel_is_secure, adata->conn->ssf will be set to 1. */
998 const bool c_ssl_force_tls = cs_subset_bool(NeoMutt->sub, "ssl_force_tls");
999 if ((adata->conn->ssf == 0) && c_ssl_force_tls)
1000 {
1001 mutt_error(_("Encrypted connection unavailable"));
1002 goto bail;
1003 }
1004#endif
1005
1006 adata->state = IMAP_AUTHENTICATED;
1007 if (check_capabilities(adata) != 0)
1008 goto bail;
1009 FREE(&adata->capstr);
1010 }
1011 else
1012 {
1013 imap_error("imap_open_connection()", adata->buf);
1014 goto bail;
1015 }
1016
1017 return 0;
1018
1019bail:
1020 imap_close_connection(adata);
1021 FREE(&adata->capstr);
1022 return -1;
1023}
1024
1030{
1031 if (adata->state != IMAP_DISCONNECTED)
1032 {
1033 mutt_socket_close(adata->conn);
1034 adata->state = IMAP_DISCONNECTED;
1035 }
1036 adata->seqno = 0;
1037 adata->nextcmd = 0;
1038 adata->lastcmd = 0;
1039 adata->status = 0;
1040 memset(adata->cmds, 0, sizeof(struct ImapCommand) * adata->cmdslots);
1041}
1042
1054bool imap_has_flag(struct ListHead *flag_list, const char *flag)
1055{
1056 if (STAILQ_EMPTY(flag_list))
1057 return false;
1058
1059 const size_t flaglen = mutt_str_len(flag);
1060 struct ListNode *np = NULL;
1061 STAILQ_FOREACH(np, flag_list, entries)
1062 {
1063 const size_t nplen = strlen(np->data);
1064 if ((flaglen >= nplen) && ((flag[nplen] == '\0') || (flag[nplen] == ' ')) &&
1065 mutt_istrn_equal(np->data, flag, nplen))
1066 {
1067 return true;
1068 }
1069
1070 if (mutt_str_equal(np->data, "\\*"))
1071 return true;
1072 }
1073
1074 return false;
1075}
1076
1080static int imap_sort_email_uid(const void *a, const void *b, void *sdata)
1081{
1082 const struct Email *ea = *(struct Email const *const *) a;
1083 const struct Email *eb = *(struct Email const *const *) b;
1084
1085 const unsigned int ua = imap_edata_get((struct Email *) ea)->uid;
1086 const unsigned int ub = imap_edata_get((struct Email *) eb)->uid;
1087
1088 return mutt_numeric_cmp(ua, ub);
1089}
1090
1107 struct Buffer *cmd, enum QuadOption *err_continue)
1108{
1110 struct ImapEmailData *edata = imap_edata_get(e);
1111
1112 if (!adata || (adata->mailbox != m) || !e)
1113 return -1;
1114
1115 if (!compare_flags_for_copy(e))
1116 {
1117 if (e->deleted == edata->deleted)
1118 e->changed = false;
1119 return 0;
1120 }
1121
1122 buf_printf(cmd, "UID STORE %u", edata->uid);
1123
1124 struct Buffer *flags = buf_pool_get();
1125
1126 set_flag(m, MUTT_ACL_SEEN, e->read, "\\Seen ", flags);
1127 set_flag(m, MUTT_ACL_WRITE, e->old, "Old ", flags);
1128 set_flag(m, MUTT_ACL_WRITE, e->flagged, "\\Flagged ", flags);
1129 set_flag(m, MUTT_ACL_WRITE, e->replied, "\\Answered ", flags);
1130 set_flag(m, MUTT_ACL_DELETE, edata->deleted, "\\Deleted ", flags);
1131
1132 if (m->rights & MUTT_ACL_WRITE)
1133 {
1134 /* restore system flags */
1135 if (edata->flags_system)
1136 buf_addstr(flags, edata->flags_system);
1137
1138 /* set custom flags */
1139 struct Buffer *tags = buf_pool_get();
1141 if (!buf_is_empty(tags))
1142 buf_addstr(flags, buf_string(tags));
1143 buf_pool_release(&tags);
1144 }
1145
1147 buf_fix_dptr(flags);
1148
1149 /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
1150 * explicitly revoke all system flags (if we have permission) */
1151 if (buf_is_empty(flags))
1152 {
1153 set_flag(m, MUTT_ACL_SEEN, true, "\\Seen ", flags);
1154 set_flag(m, MUTT_ACL_WRITE, true, "Old ", flags);
1155 set_flag(m, MUTT_ACL_WRITE, true, "\\Flagged ", flags);
1156 set_flag(m, MUTT_ACL_WRITE, true, "\\Answered ", flags);
1157 set_flag(m, MUTT_ACL_DELETE, !edata->deleted, "\\Deleted ", flags);
1158
1159 /* erase custom flags */
1160 if ((m->rights & MUTT_ACL_WRITE) && edata->flags_remote)
1161 buf_addstr(flags, edata->flags_remote);
1162
1164 buf_fix_dptr(flags);
1165
1166 buf_addstr(cmd, " -FLAGS.SILENT (");
1167 }
1168 else
1169 {
1170 buf_addstr(cmd, " FLAGS.SILENT (");
1171 }
1172
1173 buf_addstr(cmd, buf_string(flags));
1174 buf_addstr(cmd, ")");
1175
1176 int rc = -1;
1177
1178 /* after all this it's still possible to have no flags, if you
1179 * have no ACL rights */
1180 if (!buf_is_empty(flags) &&
1182 err_continue && (*err_continue != MUTT_YES))
1183 {
1184 *err_continue = imap_continue("imap_sync_message: STORE failed", adata->buf);
1185 if (*err_continue != MUTT_YES)
1186 goto done;
1187 }
1188
1189 /* server have now the updated flags */
1190 FREE(&edata->flags_remote);
1191 struct Buffer *flags_remote = buf_pool_get();
1192 driver_tags_get_with_hidden(&e->tags, flags_remote);
1193 edata->flags_remote = buf_strdup(flags_remote);
1194 buf_pool_release(&flags_remote);
1195
1196 if (e->deleted == edata->deleted)
1197 e->changed = false;
1198
1199 rc = 0;
1200
1201done:
1202 buf_pool_release(&flags);
1203 return rc;
1204}
1205
1212enum MxStatus imap_check_mailbox(struct Mailbox *m, bool force)
1213{
1214 if (!m || !m->account)
1215 return MX_STATUS_ERROR;
1216
1218 struct ImapMboxData *mdata = imap_mdata_get(m);
1219 if (!adata || !mdata)
1220 return MX_STATUS_ERROR;
1221
1222 /* overload keyboard timeout to avoid many mailbox checks in a row.
1223 * Most users don't like having to wait exactly when they press a key. */
1224 int rc = 0;
1225
1226 /* try IDLE first, unless force is set */
1227 const bool c_imap_idle = cs_subset_bool(NeoMutt->sub, "imap_idle");
1228 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
1229 if (!force && c_imap_idle && (adata->capabilities & IMAP_CAP_IDLE) &&
1230 ((adata->state != IMAP_IDLE) || (mutt_date_now() >= adata->lastread + c_imap_keep_alive)))
1231 {
1232 if (imap_cmd_idle(adata) < 0)
1233 return MX_STATUS_ERROR;
1234 }
1235 if (adata->state == IMAP_IDLE)
1236 {
1237 while ((rc = mutt_socket_poll(adata->conn, 0)) > 0)
1238 {
1239 if (imap_cmd_step(adata) != IMAP_RES_CONTINUE)
1240 {
1241 mutt_debug(LL_DEBUG1, "Error reading IDLE response\n");
1242 return MX_STATUS_ERROR;
1243 }
1244 }
1245 if (rc < 0)
1246 {
1247 mutt_debug(LL_DEBUG1, "Poll failed, disabling IDLE\n");
1248 adata->capabilities &= ~IMAP_CAP_IDLE; // Clear the flag
1249 }
1250 }
1251
1252 const short c_timeout = cs_subset_number(NeoMutt->sub, "timeout");
1253 if ((force || ((adata->state != IMAP_IDLE) && (mutt_date_now() >= adata->lastread + c_timeout))) &&
1254 (imap_exec(adata, "NOOP", IMAP_CMD_POLL) != IMAP_EXEC_SUCCESS))
1255 {
1256 return MX_STATUS_ERROR;
1257 }
1258
1259 /* We call this even when we haven't run NOOP in case we have pending
1260 * changes to process, since we can reopen here. */
1261 imap_cmd_finish(adata);
1262
1263 enum MxStatus check = MX_STATUS_OK;
1264 if (mdata->check_status & IMAP_EXPUNGE_PENDING)
1265 check = MX_STATUS_REOPENED;
1266 else if (mdata->check_status & IMAP_NEWMAIL_PENDING)
1267 check = MX_STATUS_NEW_MAIL;
1268 else if (mdata->check_status & IMAP_FLAGS_PENDING)
1269 check = MX_STATUS_FLAGS;
1270 else if (rc < 0)
1271 check = MX_STATUS_ERROR;
1272
1273 mdata->check_status = IMAP_OPEN_NO_FLAGS;
1274
1275 if (force)
1276 m->last_checked = 0; // force a check on the next mx_mbox_check() call
1277 return check;
1278}
1279
1287static int imap_status(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool queue)
1288{
1289 char *uidvalidity_flag = NULL;
1290 char cmd[2048] = { 0 };
1291
1292 if (!adata || !mdata)
1293 return -1;
1294
1295 /* Don't issue STATUS on the selected mailbox, it will be NOOPed or
1296 * IDLEd elsewhere.
1297 * adata->mailbox may be NULL for connections other than the current
1298 * mailbox's. */
1299 if (adata->mailbox && (adata->mailbox->mdata == mdata))
1300 {
1301 adata->mailbox->has_new = false;
1302 return mdata->messages;
1303 }
1304
1305 if (adata->mailbox && !adata->mailbox->poll_new_mail)
1306 return mdata->messages;
1307
1308 if (adata->capabilities & IMAP_CAP_IMAP4REV1)
1309 {
1310 uidvalidity_flag = "UIDVALIDITY";
1311 }
1312 else if (adata->capabilities & IMAP_CAP_STATUS)
1313 {
1314 uidvalidity_flag = "UID-VALIDITY";
1315 }
1316 else
1317 {
1318 mutt_debug(LL_DEBUG2, "Server doesn't support STATUS\n");
1319 return -1;
1320 }
1321
1322 snprintf(cmd, sizeof(cmd), "STATUS %s (UIDNEXT %s UNSEEN RECENT MESSAGES)",
1323 mdata->munge_name, uidvalidity_flag);
1324
1325 int rc = imap_exec(adata, cmd, queue ? IMAP_CMD_QUEUE : IMAP_CMD_POLL);
1326 if (rc != IMAP_EXEC_SUCCESS)
1327 {
1328 mutt_debug(LL_DEBUG1, "Error queueing command\n");
1329 return rc;
1330 }
1331 return mdata->messages;
1332}
1333
1337static enum MxStatus imap_mbox_check_stats(struct Mailbox *m, uint8_t flags)
1338{
1339 const bool queue = (flags & MUTT_MAILBOX_CHECK_IMMEDIATE) == 0;
1340 const int new_msgs = imap_mailbox_status(m, queue);
1341 if (new_msgs == -1)
1342 return MX_STATUS_ERROR;
1343 if (new_msgs == 0)
1344 return MX_STATUS_OK;
1345 return MX_STATUS_NEW_MAIL;
1346}
1347
1354int imap_path_status(const char *path, bool queue)
1355{
1356 struct Mailbox *m = mx_mbox_find2(path);
1357
1358 const bool is_temp = !m;
1359 if (is_temp)
1360 {
1361 m = mx_path_resolve(path);
1362 if (!mx_mbox_ac_link(m))
1363 {
1364 mailbox_free(&m);
1365 return 0;
1366 }
1367 }
1368
1369 int rc = imap_mailbox_status(m, queue);
1370
1371 if (is_temp)
1372 {
1373 mx_ac_remove(m, false);
1374 mailbox_free(&m);
1375 }
1376
1377 return rc;
1378}
1379
1389int imap_mailbox_status(struct Mailbox *m, bool queue)
1390{
1392 struct ImapMboxData *mdata = imap_mdata_get(m);
1393 if (!adata || !mdata)
1394 return -1;
1395 return imap_status(adata, mdata, queue);
1396}
1397
1405int imap_subscribe(const char *path, bool subscribe)
1406{
1407 struct ImapAccountData *adata = NULL;
1408 struct ImapMboxData *mdata = NULL;
1409
1410 if (imap_adata_find(path, &adata, &mdata) < 0)
1411 return -1;
1412
1413 if (subscribe)
1414 mutt_message(_("Subscribing to %s..."), mdata->name);
1415 else
1416 mutt_message(_("Unsubscribing from %s..."), mdata->name);
1417
1418 char buf[2048] = { 0 };
1419 snprintf(buf, sizeof(buf), "%sSUBSCRIBE %s", subscribe ? "" : "UN", mdata->munge_name);
1420
1421 if (imap_exec(adata, buf, IMAP_CMD_NO_FLAGS) != IMAP_EXEC_SUCCESS)
1422 {
1423 imap_mdata_free((void *) &mdata);
1424 return -1;
1425 }
1426
1427 const bool c_imap_check_subscribed = cs_subset_bool(NeoMutt->sub, "imap_check_subscribed");
1428 if (c_imap_check_subscribed)
1429 {
1430 if (subscribe)
1431 {
1432 struct Buffer *err = buf_pool_get();
1433 if (!mailbox_add_simple(path, err))
1434 mutt_debug(LL_DEBUG1, "Error adding subscribed mailbox: %s\n", buf_string(err));
1435 buf_pool_release(&err);
1436 }
1437 else
1438 {
1439 if (!mailbox_remove_simple(path))
1440 mutt_debug(LL_DEBUG1, "Error removing subscribed mailbox: %s\n", path);
1441 }
1442 }
1443
1444 if (subscribe)
1445 mutt_message(_("Subscribed to %s"), mdata->name);
1446 else
1447 mutt_message(_("Unsubscribed from %s"), mdata->name);
1448 imap_mdata_free((void *) &mdata);
1449 return 0;
1450}
1451
1462int imap_complete(struct Buffer *buf, const char *path)
1463{
1464 struct ImapAccountData *adata = NULL;
1465 struct ImapMboxData *mdata = NULL;
1466 char tmp[2048] = { 0 };
1467 struct ImapList listresp = { 0 };
1468 struct Buffer *completion_buf = NULL;
1469 size_t clen;
1470 int completions = 0;
1471 int rc;
1472
1473 if (imap_adata_find(path, &adata, &mdata) < 0)
1474 {
1475 buf_strcpy(buf, path);
1476 return complete_hosts(buf);
1477 }
1478
1479 /* fire off command */
1480 const bool c_imap_list_subscribed = cs_subset_bool(NeoMutt->sub, "imap_list_subscribed");
1481 snprintf(tmp, sizeof(tmp), "%s \"\" \"%s%%\"",
1482 c_imap_list_subscribed ? "LSUB" : "LIST", mdata->real_name);
1483
1484 imap_cmd_start(adata, tmp);
1485
1486 /* and see what the results are */
1487 completion_buf = buf_pool_get();
1488 buf_strcpy(completion_buf, mdata->name);
1489 imap_mdata_free((void *) &mdata);
1490
1491 adata->cmdresult = &listresp;
1492 do
1493 {
1494 listresp.name = NULL;
1495 rc = imap_cmd_step(adata);
1496
1497 if ((rc == IMAP_RES_CONTINUE) && listresp.name)
1498 {
1499 /* if the folder isn't selectable, append delimiter to force browse
1500 * to enter it on second tab. */
1501 if (listresp.noselect)
1502 {
1503 clen = strlen(listresp.name);
1504 listresp.name[clen++] = listresp.delim;
1505 listresp.name[clen] = '\0';
1506 }
1507 /* copy in first word */
1508 if (!completions)
1509 {
1510 buf_strcpy(completion_buf, listresp.name);
1511 completions++;
1512 continue;
1513 }
1514
1515 longest_common_prefix(completion_buf, listresp.name, 0);
1516 completions++;
1517 }
1518 } while (rc == IMAP_RES_CONTINUE);
1519 adata->cmdresult = NULL;
1520
1521 if (completions)
1522 {
1523 /* reformat output */
1524 imap_buf_qualify_path(buf, &adata->conn->account, completion_buf->data);
1525 pretty_mailbox(buf);
1526 buf_fix_dptr(buf);
1527 buf_pool_release(&completion_buf);
1528 return 0;
1529 }
1530
1531 buf_pool_release(&completion_buf);
1532 return -1;
1533}
1534
1543int imap_fast_trash(struct Mailbox *m, const char *dest)
1544{
1545 char prompt[1024] = { 0 };
1546 int rc = -1;
1547 bool triedcreate = false;
1548 enum QuadOption err_continue = MUTT_NO;
1549
1551 if (!adata)
1552 return -1;
1553
1554 struct ImapAccountData *dest_adata = NULL;
1555 struct ImapMboxData *dest_mdata = NULL;
1556
1557 if (imap_adata_find(dest, &dest_adata, &dest_mdata) < 0)
1558 return -1;
1559
1560 struct Buffer *sync_cmd = buf_pool_get();
1561
1562 /* check that the save-to folder is in the same account */
1563 if (!imap_account_match(&(adata->conn->account), &(dest_adata->conn->account)))
1564 {
1565 mutt_debug(LL_DEBUG3, "%s not same server as %s\n", dest, mailbox_path(m));
1566 goto out;
1567 }
1568
1569 for (int i = 0; i < m->msg_count; i++)
1570 {
1571 struct Email *e = m->emails[i];
1572 if (!e)
1573 break;
1574 if (e->active && e->changed && e->deleted && !e->purge)
1575 {
1576 rc = imap_sync_message_for_copy(m, e, sync_cmd, &err_continue);
1577 if (rc < 0)
1578 {
1579 mutt_debug(LL_DEBUG1, "could not sync\n");
1580 goto out;
1581 }
1582 }
1583 }
1584
1585 /* loop in case of TRYCREATE */
1586 do
1587 {
1588 struct UidArray uida = ARRAY_HEAD_INITIALIZER;
1589 select_email_uids(m->emails, m->msg_count, MUTT_TRASH, false, false, &uida);
1590 ARRAY_SORT(&uida, imap_sort_uid, NULL);
1591 rc = imap_exec_msg_set(adata, "UID COPY", dest_mdata->munge_name, &uida);
1592 if (rc == 0)
1593 {
1594 mutt_debug(LL_DEBUG1, "No messages to trash\n");
1595 rc = -1;
1596 goto out;
1597 }
1598 else if (rc < 0)
1599 {
1600 mutt_debug(LL_DEBUG1, "could not queue copy\n");
1601 goto out;
1602 }
1603 else if (m->verbose)
1604 {
1605 mutt_message(ngettext("Copying %d message to %s...", "Copying %d messages to %s...", rc),
1606 rc, dest_mdata->name);
1607 }
1608 ARRAY_FREE(&uida);
1609
1610 /* let's get it on */
1611 rc = imap_exec(adata, NULL, IMAP_CMD_NO_FLAGS);
1612 if (rc == IMAP_EXEC_ERROR)
1613 {
1614 if (triedcreate)
1615 {
1616 mutt_debug(LL_DEBUG1, "Already tried to create mailbox %s\n", dest_mdata->name);
1617 break;
1618 }
1619 /* bail out if command failed for reasons other than nonexistent target */
1620 if (!mutt_istr_startswith(imap_get_qualifier(adata->buf), "[TRYCREATE]"))
1621 break;
1622 mutt_debug(LL_DEBUG3, "server suggests TRYCREATE\n");
1623 snprintf(prompt, sizeof(prompt), _("Create %s?"), dest_mdata->name);
1624 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
1625 if (c_confirm_create &&
1626 (query_yesorno_help(prompt, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
1627 {
1629 goto out;
1630 }
1631 if (imap_create_mailbox(adata, dest_mdata->name) < 0)
1632 break;
1633 triedcreate = true;
1634 }
1635 } while (rc == IMAP_EXEC_ERROR);
1636
1637 if (rc != IMAP_EXEC_SUCCESS)
1638 {
1639 imap_error("imap_fast_trash", adata->buf);
1640 goto out;
1641 }
1642
1643 rc = IMAP_EXEC_SUCCESS;
1644
1645out:
1646 buf_pool_release(&sync_cmd);
1647 imap_mdata_free((void *) &dest_mdata);
1648
1649 return ((rc == IMAP_EXEC_SUCCESS) ? 0 : -1);
1650}
1651
1661enum MxStatus imap_sync_mailbox(struct Mailbox *m, bool expunge, bool close)
1662{
1663 if (!m)
1664 return -1;
1665
1666 struct Email **emails = NULL;
1667 int rc;
1668
1670 struct ImapMboxData *mdata = imap_mdata_get(m);
1671 if (!adata || !mdata)
1672 return MX_STATUS_ERROR;
1673
1674 if (adata->state < IMAP_SELECTED)
1675 {
1676 mutt_debug(LL_DEBUG2, "no mailbox selected\n");
1677 return -1;
1678 }
1679
1680 /* This function is only called when the calling code expects the context
1681 * to be changed. */
1683
1684 enum MxStatus check = imap_check_mailbox(m, false);
1685 if (check == MX_STATUS_ERROR)
1686 return check;
1687
1688 /* if we are expunging anyway, we can do deleted messages very quickly... */
1689 if (expunge && (m->rights & MUTT_ACL_DELETE))
1690 {
1691 struct UidArray uida = ARRAY_HEAD_INITIALIZER;
1692 select_email_uids(m->emails, m->msg_count, MUTT_DELETED, true, false, &uida);
1693 ARRAY_SORT(&uida, imap_sort_uid, NULL);
1694 rc = imap_exec_msg_set(adata, "UID STORE", "+FLAGS.SILENT (\\Deleted)", &uida);
1695 ARRAY_FREE(&uida);
1696 if (rc < 0)
1697 {
1698 mutt_error(_("Expunge failed"));
1699 return rc;
1700 }
1701
1702 if (rc > 0)
1703 {
1704 /* mark these messages as unchanged so second pass ignores them. Done
1705 * here so BOGUS UW-IMAP 4.7 SILENT FLAGS updates are ignored. */
1706 for (int i = 0; i < m->msg_count; i++)
1707 {
1708 struct Email *e = m->emails[i];
1709 if (!e)
1710 break;
1711 if (e->deleted && e->changed)
1712 e->active = false;
1713 }
1714 if (m->verbose)
1715 {
1716 mutt_message(ngettext("Marking %d message deleted...",
1717 "Marking %d messages deleted...", rc),
1718 rc);
1719 }
1720 }
1721 }
1722
1723#ifdef USE_HCACHE
1724 imap_hcache_open(adata, mdata, true);
1725#endif
1726
1727 /* save messages with real (non-flag) changes */
1728 for (int i = 0; i < m->msg_count; i++)
1729 {
1730 struct Email *e = m->emails[i];
1731 if (!e)
1732 break;
1733
1734 if (e->deleted)
1735 {
1736 imap_cache_del(m, e);
1737#ifdef USE_HCACHE
1738 imap_hcache_del(mdata, imap_edata_get(e)->uid);
1739#endif
1740 }
1741
1742 if (e->active && e->changed)
1743 {
1744#ifdef USE_HCACHE
1745 imap_hcache_put(mdata, e);
1746#endif
1747 /* if the message has been rethreaded or attachments have been deleted
1748 * we delete the message and reupload it.
1749 * This works better if we're expunging, of course. */
1750 if (e->env->changed || e->attach_del)
1751 {
1752 /* L10N: The plural is chosen by the last %d, i.e. the total number */
1753 if (m->verbose)
1754 {
1755 mutt_message(ngettext("Saving changed message... [%d/%d]",
1756 "Saving changed messages... [%d/%d]", m->msg_count),
1757 i + 1, m->msg_count);
1758 }
1759 bool save_append = m->append;
1760 m->append = true;
1762 m->append = save_append;
1763 e->env->changed = false;
1764 }
1765 }
1766 }
1767
1768#ifdef USE_HCACHE
1769 imap_hcache_close(mdata);
1770#endif
1771
1772 /* presort here to avoid doing 10 resorts in imap_exec_msg_set */
1773 emails = MUTT_MEM_MALLOC(m->msg_count, struct Email *);
1774 memcpy(emails, m->emails, m->msg_count * sizeof(struct Email *));
1775 mutt_qsort_r(emails, m->msg_count, sizeof(struct Email *), imap_sort_email_uid, NULL);
1776
1777 rc = sync_helper(m, emails, m->msg_count, MUTT_ACL_DELETE, MUTT_DELETED, "\\Deleted");
1778 if (rc >= 0)
1779 rc |= sync_helper(m, emails, m->msg_count, MUTT_ACL_WRITE, MUTT_FLAG, "\\Flagged");
1780 if (rc >= 0)
1781 rc |= sync_helper(m, emails, m->msg_count, MUTT_ACL_WRITE, MUTT_OLD, "Old");
1782 if (rc >= 0)
1783 rc |= sync_helper(m, emails, m->msg_count, MUTT_ACL_SEEN, MUTT_READ, "\\Seen");
1784 if (rc >= 0)
1785 rc |= sync_helper(m, emails, m->msg_count, MUTT_ACL_WRITE, MUTT_REPLIED, "\\Answered");
1786
1787 FREE(&emails);
1788
1789 /* Flush the queued flags if any were changed in sync_helper. */
1790 if (rc > 0)
1791 if (imap_exec(adata, NULL, IMAP_CMD_NO_FLAGS) != IMAP_EXEC_SUCCESS)
1792 rc = -1;
1793
1794 if (rc < 0)
1795 {
1796 if (close)
1797 {
1798 if (query_yesorno(_("Error saving flags. Close anyway?"), MUTT_NO) == MUTT_YES)
1799 {
1800 adata->state = IMAP_AUTHENTICATED;
1801 return 0;
1802 }
1803 }
1804 else
1805 {
1806 mutt_error(_("Error saving flags"));
1807 }
1808 return -1;
1809 }
1810
1811 /* Update local record of server state to reflect the synchronization just
1812 * completed. imap_read_headers always overwrites hcache-origin flags, so
1813 * there is no need to mutate the hcache after flag-only changes. */
1814 for (int i = 0; i < m->msg_count; i++)
1815 {
1816 struct Email *e = m->emails[i];
1817 if (!e)
1818 break;
1819 struct ImapEmailData *edata = imap_edata_get(e);
1820 edata->deleted = e->deleted;
1821 edata->flagged = e->flagged;
1822 edata->old = e->old;
1823 edata->read = e->read;
1824 edata->replied = e->replied;
1825 e->changed = false;
1826 }
1827 m->changed = false;
1828
1829 /* We must send an EXPUNGE command if we're not closing. */
1830 if (expunge && !close && (m->rights & MUTT_ACL_DELETE))
1831 {
1832 if (m->verbose)
1833 mutt_message(_("Expunging messages from server..."));
1834 /* Set expunge bit so we don't get spurious reopened messages */
1835 mdata->reopen |= IMAP_EXPUNGE_EXPECTED;
1836 if (imap_exec(adata, "EXPUNGE", IMAP_CMD_NO_FLAGS) != IMAP_EXEC_SUCCESS)
1837 {
1839 imap_error(_("imap_sync_mailbox: EXPUNGE failed"), adata->buf);
1840 return -1;
1841 }
1843 }
1844
1845 if (expunge && close)
1846 {
1847 adata->closing = true;
1848 imap_exec(adata, "CLOSE", IMAP_CMD_NO_FLAGS);
1849 adata->state = IMAP_AUTHENTICATED;
1850 }
1851
1852 const bool c_message_cache_clean = cs_subset_bool(NeoMutt->sub, "message_cache_clean");
1853 if (c_message_cache_clean)
1855
1856 return check;
1857}
1858
1862static bool imap_ac_owns_path(struct Account *a, const char *path)
1863{
1864 struct Url *url = url_parse(path);
1865 if (!url)
1866 return false;
1867
1868 struct ImapAccountData *adata = a->adata;
1869 struct ConnAccount *cac = &adata->conn->account;
1870
1871 const bool rc = mutt_istr_equal(url->host, cac->host) &&
1872 (!url->user || mutt_istr_equal(url->user, cac->user));
1873 url_free(&url);
1874 return rc;
1875}
1876
1880static bool imap_ac_add(struct Account *a, struct Mailbox *m)
1881{
1882 struct ImapAccountData *adata = a->adata;
1883
1884 if (!adata)
1885 {
1886 struct ConnAccount cac = { { 0 } };
1887 char mailbox[PATH_MAX] = { 0 };
1888
1889 if (imap_parse_path(mailbox_path(m), &cac, mailbox, sizeof(mailbox)) < 0)
1890 return false;
1891
1892 adata = imap_adata_new(a);
1893 adata->conn = mutt_conn_new(&cac);
1894 if (!adata->conn)
1895 {
1896 imap_adata_free((void **) &adata);
1897 return false;
1898 }
1899
1901
1902 if (imap_login(adata) < 0)
1903 {
1904 imap_adata_free((void **) &adata);
1905 return false;
1906 }
1907
1908 a->adata = adata;
1910 }
1911
1912 if (!m->mdata)
1913 {
1914 struct Url *url = url_parse(mailbox_path(m));
1915 if (!url)
1916 return false;
1917 struct ImapMboxData *mdata = imap_mdata_new(adata, url->path);
1918
1919 /* fixup path and realpath, mainly to replace / by /INBOX */
1920 char buf[1024] = { 0 };
1921 imap_qualify_path(buf, sizeof(buf), &adata->conn->account, mdata->name);
1922 buf_strcpy(&m->pathbuf, buf);
1924
1925 m->mdata = mdata;
1927 url_free(&url);
1928 }
1929 return true;
1930}
1931
1936static void imap_mbox_select(struct Mailbox *m)
1937{
1939 struct ImapMboxData *mdata = imap_mdata_get(m);
1940 if (!adata || !mdata)
1941 return;
1942
1943 const char *condstore = NULL;
1944#ifdef USE_HCACHE
1945 const bool c_imap_condstore = cs_subset_bool(NeoMutt->sub, "imap_condstore");
1946 if ((adata->capabilities & IMAP_CAP_CONDSTORE) && c_imap_condstore)
1947 condstore = " (CONDSTORE)";
1948 else
1949#endif
1950 condstore = "";
1951
1952 char buf[PATH_MAX] = { 0 };
1953 snprintf(buf, sizeof(buf), "%s %s%s", m->readonly ? "EXAMINE" : "SELECT",
1954 mdata->munge_name, condstore);
1955
1956 adata->state = IMAP_SELECTED;
1957
1958 imap_cmd_start(adata, buf);
1959}
1960
1969int imap_login(struct ImapAccountData *adata)
1970{
1971 if (!adata)
1972 return -1;
1973
1974 if (adata->state == IMAP_DISCONNECTED)
1975 {
1976 buf_reset(&adata->cmdbuf); // purge outstanding queued commands
1977 imap_open_connection(adata);
1978 }
1979 if (adata->state == IMAP_CONNECTED)
1980 {
1982 {
1983 adata->state = IMAP_AUTHENTICATED;
1984 FREE(&adata->capstr);
1985 if (adata->conn->ssf != 0)
1986 {
1987 mutt_debug(LL_DEBUG2, "Communication encrypted at %d bits\n",
1988 adata->conn->ssf);
1989 }
1990 }
1991 else
1992 {
1994 }
1995 }
1996 if (adata->state == IMAP_AUTHENTICATED)
1997 {
1998 /* capabilities may have changed */
1999 imap_exec(adata, "CAPABILITY", IMAP_CMD_PASS);
2000
2001#ifdef USE_ZLIB
2002 /* RFC4978 */
2003 const bool c_imap_deflate = cs_subset_bool(NeoMutt->sub, "imap_deflate");
2004 if ((adata->capabilities & IMAP_CAP_COMPRESS) && c_imap_deflate &&
2005 (imap_exec(adata, "COMPRESS DEFLATE", IMAP_CMD_PASS) == IMAP_EXEC_SUCCESS))
2006 {
2007 mutt_debug(LL_DEBUG2, "IMAP compression is enabled on connection to %s\n",
2008 adata->conn->account.host);
2009 mutt_zstrm_wrap_conn(adata->conn);
2010 }
2011#endif
2012
2013 /* enable RFC2971, if the server supports that */
2014 const bool c_imap_send_id = cs_subset_bool(NeoMutt->sub, "imap_send_id");
2015 if (c_imap_send_id && (adata->capabilities & IMAP_CAP_ID))
2016 {
2017 imap_exec(adata, "ID (\"name\" \"NeoMutt\" \"version\" \"" PACKAGE_VERSION "\")",
2019 }
2020
2021 /* enable RFC6855, if the server supports that */
2022 const bool c_imap_rfc5161 = cs_subset_bool(NeoMutt->sub, "imap_rfc5161");
2023 if (c_imap_rfc5161 && (adata->capabilities & IMAP_CAP_ENABLE))
2024 imap_exec(adata, "ENABLE UTF8=ACCEPT", IMAP_CMD_QUEUE);
2025
2026 /* enable QRESYNC. Advertising QRESYNC also means CONDSTORE
2027 * is supported (even if not advertised), so flip that bit. */
2028 if (adata->capabilities & IMAP_CAP_QRESYNC)
2029 {
2031 const bool c_imap_qresync = cs_subset_bool(NeoMutt->sub, "imap_qresync");
2032 if (c_imap_rfc5161 && c_imap_qresync)
2033 imap_exec(adata, "ENABLE QRESYNC", IMAP_CMD_QUEUE);
2034 }
2035
2036 /* get root delimiter, '/' as default */
2037 adata->delim = '/';
2038 imap_exec(adata, "LIST \"\" \"\"", IMAP_CMD_QUEUE);
2039
2040 /* we may need the root delimiter before we open a mailbox */
2041 imap_exec(adata, NULL, IMAP_CMD_NO_FLAGS);
2042
2043 /* select the mailbox that used to be open before disconnect */
2044 if (adata->mailbox)
2045 {
2046 imap_mbox_select(adata->mailbox);
2047 }
2048 }
2049
2050 if (adata->state < IMAP_AUTHENTICATED)
2051 return -1;
2052
2053 return 0;
2054}
2055
2060{
2061 if (!m->account || !m->mdata)
2062 return MX_OPEN_ERROR;
2063
2064 char buf[PATH_MAX] = { 0 };
2065 int count = 0;
2066 int rc;
2067
2069 struct ImapMboxData *mdata = imap_mdata_get(m);
2070 if (!adata || !mdata)
2071 return MX_OPEN_ERROR;
2072
2073 mutt_debug(LL_DEBUG3, "opening %s, stranding %p\n", m->pathbuf.data,
2074 (void *) adata->mailbox);
2075 adata->prev_mailbox = adata->mailbox;
2076 adata->mailbox = m;
2077
2078 /* clear mailbox status */
2079 adata->status = 0;
2080 m->rights = 0;
2081 mdata->new_mail_count = 0;
2082
2083 if (m->verbose)
2084 mutt_message(_("Selecting %s..."), mdata->name);
2085
2086 /* pipeline ACL test */
2087 if (adata->capabilities & IMAP_CAP_ACL)
2088 {
2089 snprintf(buf, sizeof(buf), "MYRIGHTS %s", mdata->munge_name);
2090 imap_exec(adata, buf, IMAP_CMD_QUEUE);
2091 }
2092 else
2093 {
2094 /* assume we have all rights if ACL is unavailable */
2097 }
2098
2099 /* pipeline the postponed count if possible */
2100 const char *const c_postponed = cs_subset_string(NeoMutt->sub, "postponed");
2101 struct Mailbox *m_postponed = mx_mbox_find2(c_postponed);
2102 struct ImapAccountData *postponed_adata = imap_adata_get(m_postponed);
2103 if (postponed_adata &&
2104 imap_account_match(&postponed_adata->conn->account, &adata->conn->account))
2105 {
2106 imap_mailbox_status(m_postponed, true);
2107 }
2108
2109 const bool c_imap_check_subscribed = cs_subset_bool(NeoMutt->sub, "imap_check_subscribed");
2110 if (c_imap_check_subscribed)
2111 imap_exec(adata, "LSUB \"\" \"*\"", IMAP_CMD_QUEUE);
2112
2114
2115 do
2116 {
2117 char *pc = NULL;
2118
2119 rc = imap_cmd_step(adata);
2120 if (rc != IMAP_RES_CONTINUE)
2121 break;
2122
2123 if (!mutt_strn_equal(adata->buf, "* ", 2))
2124 continue;
2125 pc = imap_next_word(adata->buf);
2126
2127 /* Obtain list of available flags here, may be overridden by a
2128 * PERMANENTFLAGS tag in the OK response */
2129 if (mutt_istr_startswith(pc, "FLAGS"))
2130 {
2131 /* don't override PERMANENTFLAGS */
2132 if (STAILQ_EMPTY(&mdata->flags))
2133 {
2134 mutt_debug(LL_DEBUG3, "Getting mailbox FLAGS\n");
2135 pc = get_flags(&mdata->flags, pc);
2136 if (!pc)
2137 goto fail;
2138 }
2139 }
2140 else if (mutt_istr_startswith(pc, "OK [PERMANENTFLAGS"))
2141 {
2142 /* PERMANENTFLAGS are massaged to look like FLAGS, then override FLAGS */
2143 mutt_debug(LL_DEBUG3, "Getting mailbox PERMANENTFLAGS\n");
2144 /* safe to call on NULL */
2145 mutt_list_free(&mdata->flags);
2146 /* skip "OK [PERMANENT" so syntax is the same as FLAGS */
2147 pc += 13;
2148 pc = get_flags(&(mdata->flags), pc);
2149 if (!pc)
2150 goto fail;
2151 }
2152 else if (mutt_istr_startswith(pc, "OK [UIDVALIDITY"))
2153 {
2154 /* save UIDVALIDITY for the header cache */
2155 mutt_debug(LL_DEBUG3, "Getting mailbox UIDVALIDITY\n");
2156 pc += 3;
2157 pc = imap_next_word(pc);
2158 if (!mutt_str_atoui(pc, &mdata->uidvalidity))
2159 goto fail;
2160 }
2161 else if (mutt_istr_startswith(pc, "OK [UIDNEXT"))
2162 {
2163 mutt_debug(LL_DEBUG3, "Getting mailbox UIDNEXT\n");
2164 pc += 3;
2165 pc = imap_next_word(pc);
2166 if (!mutt_str_atoui(pc, &mdata->uid_next))
2167 goto fail;
2168 }
2169 else if (mutt_istr_startswith(pc, "OK [HIGHESTMODSEQ"))
2170 {
2171 mutt_debug(LL_DEBUG3, "Getting mailbox HIGHESTMODSEQ\n");
2172 pc += 3;
2173 pc = imap_next_word(pc);
2174 if (!mutt_str_atoull(pc, &mdata->modseq))
2175 goto fail;
2176 }
2177 else if (mutt_istr_startswith(pc, "OK [NOMODSEQ"))
2178 {
2179 mutt_debug(LL_DEBUG3, "Mailbox has NOMODSEQ set\n");
2180 mdata->modseq = 0;
2181 }
2182 else
2183 {
2184 pc = imap_next_word(pc);
2185 if (mutt_istr_startswith(pc, "EXISTS"))
2186 {
2187 count = mdata->new_mail_count;
2188 mdata->new_mail_count = 0;
2189 }
2190 }
2191 } while (rc == IMAP_RES_CONTINUE);
2192
2193 if (rc == IMAP_RES_NO)
2194 {
2195 char *s = imap_next_word(adata->buf); /* skip seq */
2196 s = imap_next_word(s); /* Skip response */
2197 mutt_error("%s", s);
2198 goto fail;
2199 }
2200
2201 if (rc != IMAP_RES_OK)
2202 goto fail;
2203
2204 /* check for READ-ONLY notification */
2205 if (mutt_istr_startswith(imap_get_qualifier(adata->buf), "[READ-ONLY]") &&
2206 !(adata->capabilities & IMAP_CAP_ACL))
2207 {
2208 mutt_debug(LL_DEBUG2, "Mailbox is read-only\n");
2209 m->readonly = true;
2210 }
2211
2212 /* dump the mailbox flags we've found */
2213 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
2214 if (c_debug_level > LL_DEBUG2)
2215 {
2216 if (STAILQ_EMPTY(&mdata->flags))
2217 {
2218 mutt_debug(LL_DEBUG3, "No folder flags found\n");
2219 }
2220 else
2221 {
2222 struct ListNode *np = NULL;
2223 struct Buffer *flag_buffer = buf_pool_get();
2224 buf_printf(flag_buffer, "Mailbox flags: ");
2225 STAILQ_FOREACH(np, &mdata->flags, entries)
2226 {
2227 buf_add_printf(flag_buffer, "[%s] ", np->data);
2228 }
2229 mutt_debug(LL_DEBUG3, "%s\n", buf_string(flag_buffer));
2230 buf_pool_release(&flag_buffer);
2231 }
2232 }
2233
2234 if (!((m->rights & MUTT_ACL_DELETE) || (m->rights & MUTT_ACL_SEEN) ||
2235 (m->rights & MUTT_ACL_WRITE) || (m->rights & MUTT_ACL_INSERT)))
2236 {
2237 m->readonly = true;
2238 }
2239
2240 mx_alloc_memory(m, count);
2241
2242 m->msg_count = 0;
2243 m->msg_unread = 0;
2244 m->msg_flagged = 0;
2245 m->msg_new = 0;
2246 m->msg_deleted = 0;
2247 m->size = 0;
2248 m->vcount = 0;
2249
2250 if ((count > 0) && (imap_read_headers(m, 1, count, true) < 0))
2251 {
2252 mutt_error(_("Error opening mailbox"));
2253 goto fail;
2254 }
2255
2256 mutt_debug(LL_DEBUG2, "msg_count is %d\n", m->msg_count);
2257 return MX_OPEN_OK;
2258
2259fail:
2260 adata->mailbox = adata->prev_mailbox;
2261 adata->prev_mailbox = NULL;
2262 if (adata->state == IMAP_SELECTED)
2263 adata->state = IMAP_AUTHENTICATED;
2264 return MX_OPEN_ERROR;
2265}
2266
2271{
2272 if (!m->account)
2273 return false;
2274
2275 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2276 * mailbox is brand new and mostly empty */
2278 struct ImapMboxData *mdata = imap_mdata_get(m);
2279 if (!adata || !mdata)
2280 return false;
2281
2282 int rc = imap_mailbox_status(m, false);
2283 if (rc >= 0)
2284 return true;
2285 if (rc == -1)
2286 return false;
2287
2288 char buf[PATH_MAX + 64];
2289 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2290 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2291 if (c_confirm_create &&
2292 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2293 return false;
2294
2295 if (imap_create_mailbox(adata, mdata->name) < 0)
2296 return false;
2297
2298 return true;
2299}
2300
2307static enum MxStatus imap_mbox_check(struct Mailbox *m)
2308{
2310 enum MxStatus rc = imap_check_mailbox(m, false);
2311 /* NOTE - mv might have been changed at this point. In particular,
2312 * m could be NULL. Beware. */
2314
2315 return rc;
2316}
2317
2321static enum MxStatus imap_mbox_close(struct Mailbox *m)
2322{
2324 struct ImapMboxData *mdata = imap_mdata_get(m);
2325
2326 /* Check to see if the mailbox is actually open */
2327 if (!adata || !mdata)
2328 return MX_STATUS_OK;
2329
2330 /* imap_mbox_open_append() borrows the struct ImapAccountData temporarily,
2331 * just for the connection.
2332 *
2333 * So when these are equal, it means we are actually closing the
2334 * mailbox and should clean up adata. Otherwise, we don't want to
2335 * touch adata - it's still being used. */
2336 if (m == adata->mailbox)
2337 {
2338 if ((adata->status != IMAP_FATAL) && (adata->state >= IMAP_SELECTED))
2339 {
2340 /* mx_mbox_close won't sync if there are no deleted messages
2341 * and the mailbox is unchanged, so we may have to close here */
2342 if (m->msg_deleted == 0)
2343 {
2344 adata->closing = true;
2345 imap_exec(adata, "CLOSE", IMAP_CMD_NO_FLAGS);
2346 }
2347 adata->state = IMAP_AUTHENTICATED;
2348 }
2349
2350 mutt_debug(LL_DEBUG3, "closing %s, restoring %p\n", m->pathbuf.data,
2351 (void *) adata->prev_mailbox);
2352 adata->mailbox = adata->prev_mailbox;
2353 adata->prev_mailbox = NULL;
2354 imap_mbox_select(adata->mailbox);
2356 }
2357
2358 return MX_STATUS_OK;
2359}
2360
2364static bool imap_msg_open_new(struct Mailbox *m, struct Message *msg, const struct Email *e)
2365{
2366 bool success = false;
2367
2368 struct Buffer *tempfile = buf_pool_get();
2369 buf_mktemp(tempfile);
2370
2371 msg->fp = mutt_file_fopen(buf_string(tempfile), "w");
2372 if (!msg->fp)
2373 {
2374 mutt_perror("%s", buf_string(tempfile));
2375 goto cleanup;
2376 }
2377
2378 msg->path = buf_strdup(tempfile);
2379 success = true;
2380
2381cleanup:
2382 buf_pool_release(&tempfile);
2383 return success;
2384}
2385
2389static int imap_tags_edit(struct Mailbox *m, const char *tags, struct Buffer *buf)
2390{
2391 struct ImapMboxData *mdata = imap_mdata_get(m);
2392 if (!mdata)
2393 return -1;
2394
2395 char *new_tag = NULL;
2396 char *checker = NULL;
2397
2398 /* Check for \* flags capability */
2399 if (!imap_has_flag(&mdata->flags, NULL))
2400 {
2401 mutt_error(_("IMAP server doesn't support custom flags"));
2402 return -1;
2403 }
2404
2405 buf_reset(buf);
2406 if (tags)
2407 buf_strcpy(buf, tags);
2408
2409 if (mw_get_field("Tags: ", buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0)
2410 return -1;
2411
2412 /* each keyword must be atom defined by rfc822 as:
2413 *
2414 * atom = 1*<any CHAR except specials, SPACE and CTLs>
2415 * CHAR = ( 0.-127. )
2416 * specials = "(" / ")" / "<" / ">" / "@"
2417 * / "," / ";" / ":" / "\" / <">
2418 * / "." / "[" / "]"
2419 * SPACE = ( 32. )
2420 * CTLS = ( 0.-31., 127.)
2421 *
2422 * And must be separated by one space.
2423 */
2424
2425 new_tag = buf->data;
2426 checker = buf->data;
2427 SKIPWS(checker);
2428 while (*checker != '\0')
2429 {
2430 if ((*checker < 32) || (*checker >= 127) || // We allow space because it's the separator
2431 (*checker == 40) || // (
2432 (*checker == 41) || // )
2433 (*checker == 60) || // <
2434 (*checker == 62) || // >
2435 (*checker == 64) || // @
2436 (*checker == 44) || // ,
2437 (*checker == 59) || // ;
2438 (*checker == 58) || // :
2439 (*checker == 92) || // backslash
2440 (*checker == 34) || // "
2441 (*checker == 46) || // .
2442 (*checker == 91) || // [
2443 (*checker == 93)) // ]
2444 {
2445 mutt_error(_("Invalid IMAP flags"));
2446 return 0;
2447 }
2448
2449 /* Skip duplicate space */
2450 while ((checker[0] == ' ') && (checker[1] == ' '))
2451 checker++;
2452
2453 /* copy char to new_tag and go the next one */
2454 *new_tag++ = *checker++;
2455 }
2456 *new_tag = '\0';
2457 new_tag = buf->data; /* rewind */
2459
2460 return !mutt_str_equal(tags, buf_string(buf));
2461}
2462
2476static int imap_tags_commit(struct Mailbox *m, struct Email *e, const char *buf)
2477{
2478 char uid[11] = { 0 };
2479
2481 if (!adata)
2482 return -1;
2483
2484 if (*buf == '\0')
2485 buf = NULL;
2486
2487 if (!(m->rights & MUTT_ACL_WRITE))
2488 return 0;
2489
2490 snprintf(uid, sizeof(uid), "%u", imap_edata_get(e)->uid);
2491
2492 /* Remove old custom flags */
2493 if (imap_edata_get(e)->flags_remote)
2494 {
2495 struct Buffer *cmd = buf_pool_get();
2496 buf_addstr(cmd, "UID STORE ");
2497 buf_addstr(cmd, uid);
2498 buf_addstr(cmd, " -FLAGS.SILENT (");
2499 buf_addstr(cmd, imap_edata_get(e)->flags_remote);
2500 buf_addstr(cmd, ")");
2501
2502 /* Should we return here, or we are fine and we could
2503 * continue to add new flags */
2504 int rc = imap_exec(adata, buf_string(cmd), IMAP_CMD_NO_FLAGS);
2505 buf_pool_release(&cmd);
2506 if (rc != IMAP_EXEC_SUCCESS)
2507 {
2508 return -1;
2509 }
2510 }
2511
2512 /* Add new custom flags */
2513 if (buf)
2514 {
2515 struct Buffer *cmd = buf_pool_get();
2516 buf_addstr(cmd, "UID STORE ");
2517 buf_addstr(cmd, uid);
2518 buf_addstr(cmd, " +FLAGS.SILENT (");
2519 buf_addstr(cmd, buf);
2520 buf_addstr(cmd, ")");
2521
2522 int rc = imap_exec(adata, buf_string(cmd), IMAP_CMD_NO_FLAGS);
2523 buf_pool_release(&cmd);
2524 if (rc != IMAP_EXEC_SUCCESS)
2525 {
2526 mutt_debug(LL_DEBUG1, "fail to add new flags\n");
2527 return -1;
2528 }
2529 }
2530
2531 /* We are good sync them */
2532 mutt_debug(LL_DEBUG1, "NEW TAGS: %s\n", buf);
2533 driver_tags_replace(&e->tags, buf);
2534 FREE(&imap_edata_get(e)->flags_remote);
2535 struct Buffer *flags_remote = buf_pool_get();
2536 driver_tags_get_with_hidden(&e->tags, flags_remote);
2537 imap_edata_get(e)->flags_remote = buf_strdup(flags_remote);
2538 buf_pool_release(&flags_remote);
2540 return 0;
2541}
2542
2546enum MailboxType imap_path_probe(const char *path, const struct stat *st)
2547{
2548 if (mutt_istr_startswith(path, "imap://"))
2549 return MUTT_IMAP;
2550
2551 if (mutt_istr_startswith(path, "imaps://"))
2552 return MUTT_IMAP;
2553
2554 return MUTT_UNKNOWN;
2555}
2556
2560int imap_path_canon(struct Buffer *path)
2561{
2562 struct Url *url = url_parse(buf_string(path));
2563 if (!url)
2564 return 0;
2565
2566 char tmp[PATH_MAX] = { 0 };
2567 char tmp2[PATH_MAX] = { 0 };
2568 if (url->path)
2569 {
2570 struct ImapAccountData *adata = NULL;
2571 if (imap_adata_find(buf_string(path), &adata, NULL) == 0)
2572 {
2573 imap_fix_path_with_delim(adata->delim, url->path, tmp, sizeof(tmp));
2574 }
2575 else
2576 {
2577 imap_fix_path(url->path, tmp, sizeof(tmp));
2578 }
2579 url->path = tmp;
2580 }
2581 url_tostring(url, tmp2, sizeof(tmp2), U_NO_FLAGS);
2582 buf_strcpy(path, tmp2);
2583 url_free(&url);
2584
2585 return 0;
2586}
2587
2591static int imap_path_is_empty(struct Buffer *path)
2592{
2593 int rc = imap_path_status(buf_string(path), false);
2594 if (rc < 0)
2595 return -1;
2596 if (rc == 0)
2597 return 1;
2598 return 0;
2599}
2600
2604const struct MxOps MxImapOps = {
2605 // clang-format off
2606 .type = MUTT_IMAP,
2607 .name = "imap",
2608 .is_local = false,
2609 .ac_owns_path = imap_ac_owns_path,
2610 .ac_add = imap_ac_add,
2611 .mbox_open = imap_mbox_open,
2612 .mbox_open_append = imap_mbox_open_append,
2613 .mbox_check = imap_mbox_check,
2614 .mbox_check_stats = imap_mbox_check_stats,
2615 .mbox_sync = NULL, /* imap syncing is handled by imap_sync_mailbox */
2616 .mbox_close = imap_mbox_close,
2617 .msg_open = imap_msg_open,
2618 .msg_open_new = imap_msg_open_new,
2619 .msg_commit = imap_msg_commit,
2620 .msg_close = imap_msg_close,
2621 .msg_padding_size = NULL,
2622 .msg_save_hcache = imap_msg_save_hcache,
2623 .tags_edit = imap_tags_edit,
2624 .tags_commit = imap_tags_commit,
2625 .path_probe = imap_path_probe,
2626 .path_canon = imap_path_canon,
2627 .path_is_empty = imap_path_is_empty,
2628 // clang-format on
2629};
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:373
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
const char * mutt_str_atoull(const char *str, unsigned long long *dst)
Convert ASCII string to an unsigned long long.
Definition atoi.c:295
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition atoi.c:217
IMAP authenticator multiplexor.
@ IMAP_AUTH_SUCCESS
Authentication successful.
Definition auth.h:40
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition buffer.c:377
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:182
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:668
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition buffer.c:337
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
#define CF_NO_FLAGS
No flags are set.
Definition command.h:48
@ CMD_UNSUBSCRIBE_FROM
:unsubscribe-from
Definition command.h:146
@ CMD_SUBSCRIBE_TO
:subscribe-to
Definition command.h:118
@ CMD_NONE
No Command.
Definition command.h:59
CommandResult
Error codes for command_t parse functions.
Definition command.h:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
NeoMutt Commands.
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
#define mutt_numeric_cmp(a, b)
Compare two numbers, return -1, 0, or 1.
Definition sort.h:27
Connection Library.
void mutt_account_unsetpass(struct ConnAccount *cac)
Unset ConnAccount's password.
Convenience wrapper for the core headers.
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition mailbox.c:90
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition mailbox.c:232
#define MUTT_ACL_CREATE
Create a mailbox.
Definition mailbox.h:61
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition mailbox.h:180
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition mailbox.h:181
#define MUTT_ACL_POST
Post (submit messages to the server)
Definition mailbox.h:67
#define MUTT_ACL_LOOKUP
Lookup mailbox (visible to 'list')
Definition mailbox.h:66
#define MUTT_ACL_INSERT
Add/copy into the mailbox (used when editing a message)
Definition mailbox.h:65
#define MUTT_ACL_DELETE
Delete a message.
Definition mailbox.h:62
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
uint16_t AclFlags
ACL Rights - These show permission to...
Definition mailbox.h:58
#define MUTT_ACL_WRITE
Write to a message (for flagging or linking threads)
Definition mailbox.h:70
MailboxType
Supported mailbox formats.
Definition mailbox.h:40
@ MUTT_IMAP
'IMAP' Mailbox type
Definition mailbox.h:49
@ MUTT_MAILBOX_ANY
Match any Mailbox type.
Definition mailbox.h:41
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition mailbox.h:43
#define MUTT_ACL_SEEN
Change the 'seen' status of a message.
Definition mailbox.h:69
#define MUTT_ACL_READ
Read the mailbox.
Definition mailbox.h:68
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:96
Edit a string.
#define MUTT_COMP_NO_FLAGS
No flags are set.
Definition wdata.h:42
Structs that make up an email.
int mutt_save_message_mbox(struct Mailbox *m_src, struct Email *e, enum MessageSaveOpt save_opt, enum MessageTransformOpt transform_opt, struct Mailbox *m_dst)
Save a message to a given mailbox.
Definition external.c:739
Manage where the email is piped to external commands.
@ TRANSFORM_NONE
No transformation.
Definition external.h:43
@ SAVE_MOVE
Move message to another mailbox, removing the original.
Definition external.h:54
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
Definition gnutls.c:1172
void imap_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free() -.
Definition adata.c:101
enum CommandResult parse_subscribe_to(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'subscribe-to' command - Implements Command::parse() -.
Definition imap.c:93
enum CommandResult parse_unsubscribe_from(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unsubscribe-from' command - Implements Command::parse() -.
Definition imap.c:143
void imap_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free() -.
Definition edata.c:39
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:463
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
#define mutt_perror(...)
Definition logging2.h:95
void imap_mdata_free(void **ptr)
Free the private Mailbox data - Implements Mailbox::mdata_free() -.
Definition mdata.c:40
static bool imap_ac_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account - Implements MxOps::ac_add() -.
Definition imap.c:1880
static bool imap_ac_owns_path(struct Account *a, const char *path)
Check whether an Account owns a Mailbox path - Implements MxOps::ac_owns_path() -.
Definition imap.c:1862
const struct MxOps MxImapOps
IMAP Mailbox - Implements MxOps -.
Definition imap.c:2604
static enum MxStatus imap_mbox_check_stats(struct Mailbox *m, uint8_t flags)
Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
Definition imap.c:1337
static enum MxStatus imap_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition imap.c:2307
static enum MxStatus imap_mbox_close(struct Mailbox *m)
Close a Mailbox - Implements MxOps::mbox_close() -.
Definition imap.c:2321
static bool imap_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
Definition imap.c:2270
static enum MxOpenReturns imap_mbox_open(struct Mailbox *m)
Open a mailbox - Implements MxOps::mbox_open() -.
Definition imap.c:2059
int imap_msg_close(struct Mailbox *m, struct Message *msg)
Close an email - Implements MxOps::msg_close() -.
Definition message.c:2212
int imap_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition message.c:2198
static bool imap_msg_open_new(struct Mailbox *m, struct Message *msg, const struct Email *e)
Open a new message in a Mailbox - Implements MxOps::msg_open_new() -.
Definition imap.c:2364
bool imap_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
Open an email message in a Mailbox - Implements MxOps::msg_open() -.
Definition message.c:2005
int imap_msg_save_hcache(struct Mailbox *m, struct Email *e)
Save message to the header cache - Implements MxOps::msg_save_hcache() -.
Definition message.c:2220
int imap_path_canon(struct Buffer *path)
Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
Definition imap.c:2560
static int imap_path_is_empty(struct Buffer *path)
Is the mailbox empty - Implements MxOps::path_is_empty() -.
Definition imap.c:2591
enum MailboxType imap_path_probe(const char *path, const struct stat *st)
Is this an IMAP Mailbox?
Definition imap.c:2546
static int imap_tags_commit(struct Mailbox *m, struct Email *e, const char *buf)
Save the tags to a message - Implements MxOps::tags_commit() -.
Definition imap.c:2476
static int imap_tags_edit(struct Mailbox *m, const char *tags, struct Buffer *buf)
Prompt and validate new messages tags - Implements MxOps::tags_edit() -.
Definition imap.c:2389
int imap_sort_uid(const void *a, const void *b, void *sdata)
Compare two UIDs - Implements sort_t -.
Definition msg_set.c:54
static int imap_sort_email_uid(const void *a, const void *b, void *sdata)
Compare two Emails by UID - Implements sort_t -.
Definition imap.c:1080
void mutt_hash_int_delete(struct HashTable *table, unsigned int intkey, const void *data)
Remove an element from a Hash Table.
Definition hash.c:446
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition lib.h:60
void exec_account_hook(const char *url)
Perform an account hook.
Definition exec.c:323
Hook Commands.
struct ImapAccountData * imap_adata_new(struct Account *a)
Allocate and initialise a new ImapAccountData structure.
Definition adata.c:132
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition adata.c:158
Imap-specific Account data.
int imap_authenticate(struct ImapAccountData *adata)
Authenticate to an IMAP server.
Definition auth.c:116
int imap_cmd_start(struct ImapAccountData *adata, const char *cmdstr)
Given an IMAP command, send it to the server.
Definition command.c:1211
const char * imap_cmd_trailer(struct ImapAccountData *adata)
Extra information after tagged command response if any.
Definition command.c:1378
int imap_cmd_idle(struct ImapAccountData *adata)
Enter the IDLE state.
Definition command.c:1573
int imap_cmd_step(struct ImapAccountData *adata)
Reads server responses from an IMAP command.
Definition command.c:1225
int imap_exec(struct ImapAccountData *adata, const char *cmdstr, ImapCmdFlags flags)
Execute a command and wait for the response from the server.
Definition command.c:1415
void imap_cmd_finish(struct ImapAccountData *adata)
Attempt to perform cleanup.
Definition command.c:1498
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition edata.c:66
Imap-specific Email data.
IMAP network mailbox.
int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
Parse an IMAP mailbox name into ConnAccount, name.
Definition util.c:479
struct ImapMboxData * imap_mdata_new(struct ImapAccountData *adata, const char *name)
Allocate and initialise a new ImapMboxData structure.
Definition mdata.c:74
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition mdata.c:61
Imap-specific Mailbox data.
int imap_cache_clean(struct Mailbox *m)
Delete all the entries in the message cache.
Definition message.c:1916
int imap_cache_del(struct Mailbox *m, struct Email *e)
Delete an email from the body cache.
Definition message.c:1897
int imap_read_headers(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool initial_download)
Read headers from the server.
Definition message.c:1363
Shared constants/structs that are private to IMAP.
#define IMAP_CAP_ENABLE
RFC5161.
Definition private.h:135
#define IMAP_CAP_IDLE
RFC2177: IDLE.
Definition private.h:133
#define IMAP_CMD_NO_FLAGS
No flags are set.
Definition private.h:71
void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *conn_account, char *path)
Make an absolute IMAP folder target.
Definition util.c:857
#define IMAP_CAP_ID
RFC2971: IMAP4 ID extension.
Definition private.h:141
void imap_allow_reopen(struct Mailbox *m)
Allow re-opening a folder upon expunge.
Definition util.c:1075
void imap_disallow_reopen(struct Mailbox *m)
Disallow re-opening a folder upon expunge.
Definition util.c:1088
@ IMAP_DISCONNECTED
Disconnected from server.
Definition private.h:105
@ IMAP_IDLE
Connection is idle.
Definition private.h:111
@ IMAP_AUTHENTICATED
Connection is authenticated.
Definition private.h:107
@ IMAP_SELECTED
Mailbox is selected.
Definition private.h:108
@ IMAP_CONNECTED
Connected to server.
Definition private.h:106
#define IMAP_EXPUNGE_PENDING
Messages on the server have been expunged.
Definition private.h:66
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
Open a header cache.
Definition util.c:304
#define IMAP_RES_OK
<tag> OK ...
Definition private.h:54
#define IMAP_OPEN_NO_FLAGS
No flags are set.
Definition private.h:63
#define IMAP_EXPUNGE_EXPECTED
Messages will be expunged from the server.
Definition private.h:65
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition util.c:385
#define IMAP_LOG_LTRL
Log literal values.
Definition private.h:48
#define IMAP_CMD_POLL
Poll the tcp connection before running the imap command.
Definition private.h:74
void imap_mdata_cache_reset(struct ImapMboxData *mdata)
Release and clear cache data of ImapMboxData structure.
Definition util.c:110
#define IMAP_CAP_IMAP4
Server supports IMAP4.
Definition private.h:121
#define IMAP_CAP_STARTTLS
RFC2595: STARTTLS.
Definition private.h:131
#define IMAP_CAP_IMAP4REV1
Server supports IMAP4rev1.
Definition private.h:122
#define IMAP_CAP_STATUS
Server supports STATUS command.
Definition private.h:123
enum QuadOption imap_continue(const char *msg, const char *resp)
Display a message and ask the user if they want to go on.
Definition util.c:651
#define IMAP_CMD_PASS
Command contains a password. Suppress logging.
Definition private.h:72
void imap_buf_qualify_path(struct Buffer *buf, struct ConnAccount *conn_account, char *path)
Make an absolute IMAP folder target to a buffer.
Definition util.c:871
ImapExecResult
Imap_exec return code.
Definition private.h:81
@ IMAP_EXEC_SUCCESS
Imap command executed or queued successfully.
Definition private.h:82
@ IMAP_EXEC_ERROR
Imap command failure.
Definition private.h:83
@ IMAP_EXEC_FATAL
Imap connection failure.
Definition private.h:84
#define IMAP_CAP_ACL
RFC2086: IMAP4 ACL extension.
Definition private.h:124
#define IMAP_CAP_QRESYNC
RFC7162.
Definition private.h:137
#define IMAP_NEWMAIL_PENDING
New mail is waiting on the server.
Definition private.h:67
char * imap_fix_path(const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition util.c:683
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition util.c:662
#define IMAP_FLAGS_PENDING
Flags have changed on the server.
Definition private.h:68
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition util.c:345
@ IMAP_BYE
Logged out from server.
Definition private.h:96
@ IMAP_FATAL
Unrecoverable error occurred.
Definition private.h:95
char * imap_fix_path_with_delim(char delim, const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition util.c:715
#define IMAP_CAP_COMPRESS
RFC4978: COMPRESS=DEFLATE.
Definition private.h:139
int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
Delete an item from the header cache.
Definition util.c:403
#define IMAP_RES_NO
<tag> NO ...
Definition private.h:52
int imap_adata_find(const char *path, struct ImapAccountData **adata, struct ImapMboxData **mdata)
Find the Account data for this path.
Definition util.c:72
bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
Compare two Accounts.
Definition util.c:1103
void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
Quote awkward characters in a mailbox name.
Definition util.c:968
#define IMAP_CMD_SINGLE
Run a single command.
Definition private.h:75
#define IMAP_RES_CONTINUE
* ...
Definition private.h:55
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition util.c:826
#define IMAP_CAP_CONDSTORE
RFC7162.
Definition private.h:136
#define IMAP_CMD_QUEUE
Queue a command, do not execute.
Definition private.h:73
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition util.c:809
static void imap_logout(struct ImapAccountData *adata)
Gracefully log out of server.
Definition imap.c:637
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition imap.c:1389
int imap_path_status(const char *path, bool queue)
Refresh the number of total and new messages.
Definition imap.c:1354
void imap_notify_delete_email(struct Mailbox *m, struct Email *e)
Inform IMAP that an Email has been deleted.
Definition imap.c:828
void imap_close_connection(struct ImapAccountData *adata)
Close an IMAP connection.
Definition imap.c:1029
static int imap_status(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool queue)
Refresh the number of total and new messages.
Definition imap.c:1287
int imap_complete(struct Buffer *buf, const char *path)
Try to complete an IMAP folder path.
Definition imap.c:1462
int imap_subscribe(const char *path, bool subscribe)
Subscribe to a mailbox.
Definition imap.c:1405
int imap_delete_mailbox(struct Mailbox *m, char *path)
Delete a mailbox.
Definition imap.c:611
void imap_expunge_mailbox(struct Mailbox *m, bool resort)
Purge messages from the server.
Definition imap.c:849
static size_t longest_common_prefix(struct Buffer *buf, const char *src, size_t start)
Find longest prefix common to two strings.
Definition imap.c:453
int imap_open_connection(struct ImapAccountData *adata)
Open an IMAP connection.
Definition imap.c:921
static int sync_helper(struct Mailbox *m, struct Email **emails, int num_emails, AclFlags right, enum MessageType flag, const char *name)
Sync flag changes to the server.
Definition imap.c:404
int imap_rename_mailbox(struct ImapAccountData *adata, char *oldname, const char *newname)
Rename a mailbox.
Definition imap.c:584
static int complete_hosts(struct Buffer *buf)
Look for completion matches for mailboxes.
Definition imap.c:476
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition imap.c:542
static int check_capabilities(struct ImapAccountData *adata)
Make sure we can log in to this server.
Definition imap.c:207
int imap_fast_trash(struct Mailbox *m, const char *dest)
Use server COPY command to copy deleted messages to trash.
Definition imap.c:1543
int imap_sync_message_for_copy(struct Mailbox *m, struct Email *e, struct Buffer *cmd, enum QuadOption *err_continue)
Update server to reflect the flags of a single message.
Definition imap.c:1106
static int select_email_uids(struct Email **emails, int num_emails, enum MessageType flag, bool changed, bool invert, struct UidArray *uida)
Create a list of Email UIDs by type.
Definition imap.c:334
enum MxStatus imap_sync_mailbox(struct Mailbox *m, bool expunge, bool close)
Sync all the changes to the server.
Definition imap.c:1661
int imap_access(const char *path)
Check permissions on an IMAP mailbox with a new connection.
Definition imap.c:569
void imap_logout_all(void)
Close all open connections.
Definition imap.c:667
enum MxStatus imap_check_mailbox(struct Mailbox *m, bool force)
Use the NOOP or IDLE command to poll for new mail.
Definition imap.c:1212
static char * get_flags(struct ListHead *hflags, char *s)
Make a simple list out of a FLAGS response.
Definition imap.c:233
int imap_read_literal(FILE *fp, struct ImapAccountData *adata, unsigned long bytes, struct Progress *progress)
Read bytes bytes from server into file.
Definition imap.c:704
bool imap_has_flag(struct ListHead *flag_list, const char *flag)
Does the flag exist in the list.
Definition imap.c:1054
static void set_flag(struct Mailbox *m, AclFlags aclflag, bool flag, const char *str, struct Buffer *flags)
Append str to flags if we currently have permission according to aclflag.
Definition imap.c:287
static void imap_mbox_select(struct Mailbox *m)
Select a Mailbox.
Definition imap.c:1936
int imap_login(struct ImapAccountData *adata)
Open an IMAP connection.
Definition imap.c:1969
const struct Command ImapCommands[]
Imap Commands.
Definition imap.c:186
static bool compare_flags_for_copy(struct Email *e)
Compare local flags against the server.
Definition imap.c:307
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition list.c:65
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
bool mailbox_remove_simple(const char *mailbox)
Remove a Mailbox.
Definition mailboxes.c:397
bool mailbox_add_simple(const char *mailbox, struct Buffer *err)
Add a new Mailbox.
Definition mailboxes.c:157
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre, const char *post, struct UidArray *uida)
Execute a command using a set of UIDs.
Definition msg_set.c:132
IMAP Message Sets.
void imap_msn_remove(struct MSNArray *msn, int idx)
Remove an entry from the cache.
Definition msn.c:116
IMAP MSN helper functions.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#define _(a)
Definition message.h:28
void mutt_str_remove_trailing_ws(char *s)
Trim trailing whitespace from a string.
Definition string.c:570
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:429
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:586
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition string.c:246
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition string.c:457
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
Many unsorted constants and some structs.
MessageType
To set flags or match patterns.
Definition mutt.h:86
@ MUTT_TRASH
Trashed messages.
Definition mutt.h:104
@ MUTT_READ
Messages that have been read.
Definition mutt.h:92
@ MUTT_OLD
Old messages.
Definition mutt.h:90
@ MUTT_FLAG
Flagged messages.
Definition mutt.h:98
@ MUTT_DELETED
Deleted messages.
Definition mutt.h:97
@ MUTT_REPLIED
Messages that have been replied to.
Definition mutt.h:91
#define PATH_MAX
Definition mutt.h:49
void account_to_url(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
@ MUTT_ACCT_TYPE_IMAP
Imap Account.
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
NeoMutt Logging.
struct Connection * mutt_conn_new(const struct ConnAccount *cac)
Create a new Connection.
Definition mutt_socket.c:47
NeoMutt connections.
void pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition muttlib.c:428
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:122
Some miscellaneous functions.
void mx_alloc_memory(struct Mailbox *m, int req_size)
Create storage for the emails.
Definition mx.c:1208
int mx_ac_remove(struct Mailbox *m, bool keep_account)
Remove a Mailbox from an Account and delete Account if empty.
Definition mx.c:1754
struct Mailbox * mx_mbox_find2(const char *path)
Find a Mailbox on an Account.
Definition mx.c:1615
bool mx_mbox_ac_link(struct Mailbox *m)
Link a Mailbox to an existing or new Account.
Definition mx.c:248
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition mx.c:1647
API for mailboxes.
uint8_t OpenMailboxFlags
Flags for mutt_open_mailbox(), e.g. MUTT_NOSORT.
Definition mxapi.h:38
MxOpenReturns
Return values for mbox_open()
Definition mxapi.h:72
@ MX_OPEN_ERROR
Open failed with an error.
Definition mxapi.h:74
@ MX_OPEN_OK
Open succeeded.
Definition mxapi.h:73
#define MUTT_MAILBOX_CHECK_IMMEDIATE
Don't postpone the actual checking.
Definition mxapi.h:52
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_sync(), and mbox_close()
Definition mxapi.h:59
@ MX_STATUS_ERROR
An error occurred.
Definition mxapi.h:60
@ MX_STATUS_OK
No changes.
Definition mxapi.h:61
@ MX_STATUS_FLAGS
Nondestructive flags change (IMAP)
Definition mxapi.h:65
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition mxapi.h:64
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition mxapi.h:62
struct MailboxArray neomutt_mailboxes_get(struct NeoMutt *n, enum MailboxType type)
Get an Array of matching Mailboxes.
Definition neomutt.c:526
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
Progress Bar.
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition progress.c:80
void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
Sort an array, where the comparator has access to opaque data rather than requiring global variables.
Definition qsort_r.c:72
QuadOption
Possible values for a quad-option.
Definition quad.h:36
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition quad.h:37
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
Ask the user a question.
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition question.c:357
enum QuadOption query_quadoption(const char *prompt, struct ConfigSubset *sub, const char *name)
Ask the user a quad-question.
Definition question.c:384
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_EMPTY(head)
Definition queue.h:382
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition socket.c:100
int mutt_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block.
Definition socket.c:182
int mutt_socket_readchar(struct Connection *conn, char *c)
Simple read buffering to speed things up.
Definition socket.c:200
void mutt_socket_empty(struct Connection *conn)
Clear out any queued data.
Definition socket.c:306
int mutt_socket_open(struct Connection *conn)
Simple wrapper.
Definition socket.c:76
#define SKIPWS(ch)
Definition string2.h:52
A group of associated Mailboxes.
Definition account.h:36
enum MailboxType type
Type of Mailboxes this Account contains.
Definition account.h:37
char * name
Name of Account.
Definition account.h:38
void(* adata_free)(void **ptr)
Definition account.h:53
void * adata
Private data (for Mailbox backends)
Definition account.h:42
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
CommandFlags flags
Command flags, e.g. CF_SYNONYM.
Definition command.h:184
const char * name
Name of the Command.
Definition command.h:159
Login details for a remote server.
Definition connaccount.h:53
char user[128]
Username.
Definition connaccount.h:56
char host[128]
Server to login to.
Definition connaccount.h:54
unsigned int ssf
Security strength factor, in bits (see notes)
Definition connection.h:50
struct ConnAccount account
Account details: username, password, etc.
Definition connection.h:49
int fd
Socket file descriptor.
Definition connection.h:53
The envelope/body of an email.
Definition email.h:39
bool read
Email is read.
Definition email.h:50
bool purge
Skip trash folder when deleting.
Definition email.h:79
struct Envelope * env
Envelope information.
Definition email.h:68
void * edata
Driver-specific data.
Definition email.h:74
bool active
Message is not to be removed.
Definition email.h:76
bool old
Email is seen, but unread.
Definition email.h:49
bool changed
Email has been edited.
Definition email.h:77
bool attach_del
Has an attachment marked for deletion.
Definition email.h:99
bool flagged
Marked important?
Definition email.h:47
bool replied
Email has been replied to.
Definition email.h:51
struct TagList tags
For drivers that support server tagging.
Definition email.h:72
char * path
Path of Email (for local Mailboxes)
Definition email.h:70
bool deleted
Email is deleted.
Definition email.h:78
int index
The absolute (unsorted) message number.
Definition email.h:110
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition envelope.h:90
IMAP-specific Account data -.
Definition adata.h:40
char delim
Path delimiter.
Definition adata.h:78
struct Mailbox * prev_mailbox
Previously selected mailbox.
Definition adata.h:80
struct ImapList * cmdresult
Resuls of complicated commands.
Definition adata.h:69
int lastcmd
Last command in the queue.
Definition adata.h:75
bool closing
If true, we are waiting for CLOSE completion.
Definition adata.h:43
time_t lastread
last time we read a command for the server
Definition adata.h:58
bool unicode
If true, we can send UTF-8, and the server will use UTF8 rather than mUTF7.
Definition adata.h:63
ImapCapFlags capabilities
Capability flags.
Definition adata.h:55
int nextcmd
Next command to be sent.
Definition adata.h:74
unsigned char state
ImapState, e.g. IMAP_AUTHENTICATED.
Definition adata.h:44
struct Mailbox * mailbox
Current selected mailbox.
Definition adata.h:79
char * capstr
Capability string from the server.
Definition adata.h:54
struct ImapCommand * cmds
Queue of commands for the server.
Definition adata.h:72
unsigned char status
ImapFlags, e.g. IMAP_FATAL.
Definition adata.h:45
int cmdslots
Size of the command queue.
Definition adata.h:73
char * buf
Command buffer.
Definition adata.h:60
unsigned int seqno
tag sequence number, e.g. '{seqid}0001'
Definition adata.h:57
struct Connection * conn
Connection to IMAP server.
Definition adata.h:41
struct Buffer cmdbuf
Command queue.
Definition adata.h:76
IMAP command structure.
Definition private.h:160
IMAP-specific Email data -.
Definition edata.h:35
unsigned int uid
32-bit Message UID
Definition edata.h:45
char * flags_remote
Remote flags.
Definition edata.h:49
bool deleted
Email has been deleted.
Definition edata.h:39
char * flags_system
System flags.
Definition edata.h:48
Items in an IMAP browser.
Definition private.h:149
bool noselect
Mailbox is not selectable.
Definition private.h:152
char * name
Mailbox name.
Definition private.h:150
char delim
Hierarchy delimiter.
Definition private.h:151
IMAP-specific Mailbox data -.
Definition mdata.h:40
ImapOpenFlags reopen
Flags, e.g. IMAP_REOPEN_ALLOW.
Definition mdata.h:45
unsigned int uid_next
Next UID for new message.
Definition mdata.h:52
struct ListHead flags
List of permanent flags.
Definition mdata.h:50
char * real_name
Original Mailbox name, e.g.: INBOX can be just \0.
Definition mdata.h:43
unsigned int new_mail_count
Set when EXISTS notifies of new mail.
Definition mdata.h:47
struct HashTable * uid_hash
Hash Table: "uid" -> Email.
Definition mdata.h:60
unsigned long long modseq
Modification sequence number.
Definition mdata.h:53
char * munge_name
Munged version of the mailbox name.
Definition mdata.h:42
uint32_t uidvalidity
UID validity.
Definition mdata.h:51
char * name
Mailbox name.
Definition mdata.h:41
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
A mailbox.
Definition mailbox.h:78
void(* mdata_free)(void **ptr)
Definition mailbox.h:142
int vcount
The number of virtual messages.
Definition mailbox.h:98
bool changed
Mailbox has been modified.
Definition mailbox.h:109
bool has_new
Mailbox has new mail.
Definition mailbox.h:84
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition mailbox.h:80
bool append
Mailbox is opened in append mode.
Definition mailbox.h:108
int msg_new
Number of new messages.
Definition mailbox.h:91
time_t last_checked
Last time we checked this mailbox for new mail.
Definition mailbox.h:104
int msg_count
Total number of messages.
Definition mailbox.h:87
AclFlags rights
ACL bits, see AclFlags.
Definition mailbox.h:118
bool poll_new_mail
Check for new mail.
Definition mailbox.h:114
void * mdata
Driver specific data.
Definition mailbox.h:131
struct Email ** emails
Array of Emails.
Definition mailbox.h:95
struct Buffer pathbuf
Path of the Mailbox.
Definition mailbox.h:79
int msg_deleted
Number of deleted messages.
Definition mailbox.h:92
struct Account * account
Account that owns this Mailbox.
Definition mailbox.h:126
off_t size
Size of the Mailbox.
Definition mailbox.h:83
int msg_flagged
Number of flagged messages.
Definition mailbox.h:89
bool readonly
Don't allow changes to the mailbox.
Definition mailbox.h:115
bool verbose
Display status messages?
Definition mailbox.h:116
int msg_unread
Number of unread messages.
Definition mailbox.h:88
A local copy of an email.
Definition message.h:34
FILE * fp
pointer to the message data
Definition message.h:35
char * path
path to temp file
Definition message.h:36
Definition mxapi.h:87
Container for Accounts, Notifications.
Definition neomutt.h:41
struct AccountArray accounts
All Accounts.
Definition neomutt.h:50
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Context for config parsing (history/backtrace)
Definition pcontext.h:34
Detailed error information from config parsing.
Definition perror.h:34
struct Buffer * message
Error message.
Definition perror.h:35
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition url.h:69
char * user
Username.
Definition url.h:71
char * host
Host.
Definition url.h:73
char * path
Path.
Definition url.h:75
bool driver_tags_replace(struct TagList *tl, const char *tags)
Replace all tags.
Definition tags.c:202
void driver_tags_get_with_hidden(struct TagList *tl, struct Buffer *tags)
Get all tags, also hidden ones, separated by space.
Definition tags.c:175
#define buf_mktemp(buf)
Definition tmp.h:33
struct Url * url_parse(const char *src)
Fill in Url.
Definition url.c:239
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition url.c:124
int url_tostring(const struct Url *url, char *dest, size_t len, uint8_t flags)
Output the URL string for a given Url object.
Definition url.c:423
#define U_NO_FLAGS
No flags are set for URL parsing.
Definition url.h:49
void mutt_zstrm_wrap_conn(struct Connection *conn)
Wrap a compression layer around a Connection.
Definition zstrm.c:297