From: Richard Russon Date: Sun, 20 Jan 2019 16:46:19 +0000 (+0000) Subject: move parse_mailboxes() X-Git-Tag: 2019-10-25~373^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=79fda72be3c5ecb46bd4973a39955d1fd0cad8c4;p=neomutt move parse_mailboxes() This function has dependencies on Context and inotify(). --- diff --git a/ChangeLog.md b/ChangeLog.md index e9305b3ce..6470d63f1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -484,7 +484,7 @@ - More sophisticated check for BDB version + support for DB6 (non default) * Tidy - drop VirtIncoming - - split mutt_parse_mailboxes into mutt_parse_unmailboxes + - split parse_mailboxes into parse_unmailboxes - tidy some mailbox code - tidy the version strings * Upstream diff --git a/init.c b/init.c index 7158d7e00..23309fed3 100644 --- a/init.c +++ b/init.c @@ -45,11 +45,13 @@ #include "email/lib.h" #include "mutt.h" #include "init.h" +#include "account.h" #include "alias.h" #include "context.h" #include "filter.h" #include "hcache/hcache.h" #include "keymap.h" +#include "monitor.h" #include "mutt_curses.h" #include "mutt_menu.h" #include "mutt_window.h" @@ -1321,6 +1323,92 @@ bail: return MUTT_CMD_ERROR; } +/** + * parse_mailboxes - Parse the 'mailboxes' command - Implements ::command_t + * + * This is also used by 'virtual-mailboxes'. + */ +static enum CommandResult parse_mailboxes(struct Buffer *buf, struct Buffer *s, + unsigned long data, struct Buffer *err) +{ + while (MoreArgs(s)) + { + struct Mailbox *m = mailbox_new(); + + if (data & MUTT_NAMED) + { + mutt_extract_token(buf, s, 0); + if (buf->data && *buf->data) + { + m->desc = mutt_str_strdup(buf->data); + } + else + { + mailbox_free(&m); + continue; + } + } + + mutt_extract_token(buf, s, 0); + if (mutt_buffer_is_empty(buf)) + { + /* Skip empty tokens. */ + mailbox_free(&m); + continue; + } + + mutt_str_strfcpy(m->path, buf->data, sizeof(m->path)); + /* int rc = */ mx_path_canon2(m, Folder); + + bool new_account = false; + struct Account *a = mx_ac_find(m); + if (!a) + { + a = account_new(); + a->magic = m->magic; + TAILQ_INSERT_TAIL(&AllAccounts, a, entries); + new_account = true; + } + + if (!new_account) + { + struct Mailbox *old_m = mx_mbox_find(a, m->realpath); + if (old_m) + { + if (old_m->flags == MB_HIDDEN) + { + old_m->flags = MB_NORMAL; + mutt_sb_notify_mailbox(old_m, true); + struct MailboxNode *mn = mutt_mem_calloc(1, sizeof(*mn)); + mn->m = old_m; + STAILQ_INSERT_TAIL(&AllMailboxes, mn, entries); + } + mailbox_free(&m); + continue; + } + } + + if (mx_ac_add(a, m) < 0) + { + //error + mailbox_free(&m); + continue; + } + + struct MailboxNode *mn = mutt_mem_calloc(1, sizeof(*mn)); + mn->m = m; + STAILQ_INSERT_TAIL(&AllMailboxes, mn, entries); + +#ifdef USE_SIDEBAR + mutt_sb_notify_mailbox(m, true); +#endif +#ifdef USE_INOTIFY + mutt_monitor_add(m); +#endif + } + return MUTT_CMD_SUCCESS; +} + /** * parse_my_hdr - Parse the 'my_hdr' command - Implements ::command_t */ @@ -2290,6 +2378,76 @@ static enum CommandResult parse_unlists(struct Buffer *buf, struct Buffer *s, return MUTT_CMD_SUCCESS; } +/** + * parse_unmailboxes - Parse the 'unmailboxes' command - Implements ::command_t + * + * This is also used by 'unvirtual-mailboxes' + */ +static enum CommandResult parse_unmailboxes(struct Buffer *buf, struct Buffer *s, + unsigned long data, struct Buffer *err) +{ + char tmp[PATH_MAX]; + bool tmp_valid = false; + bool clear_all = false; + + while (!clear_all && MoreArgs(s)) + { + mutt_extract_token(buf, s, 0); + + if (mutt_str_strcmp(buf->data, "*") == 0) + { + clear_all = true; + tmp_valid = false; + } + else + { + mutt_str_strfcpy(tmp, buf->data, sizeof(tmp)); + mutt_expand_path(tmp, sizeof(tmp)); + tmp_valid = true; + } + + struct MailboxNode *np = NULL; + struct MailboxNode *nptmp = NULL; + STAILQ_FOREACH_SAFE(np, &AllMailboxes, entries, nptmp) + { + /* Decide whether to delete all normal mailboxes or all virtual */ + bool virt = ((np->m->magic == MUTT_NOTMUCH) && (data & MUTT_VIRTUAL)); + bool norm = ((np->m->magic != MUTT_NOTMUCH) && !(data & MUTT_VIRTUAL)); + bool clear_this = clear_all && (virt || norm); + + /* Compare against path or desc? Ensure 'tmp' is valid */ + if (!clear_this && tmp_valid) + { + clear_this = (mutt_str_strcasecmp(tmp, np->m->path) == 0) || + (mutt_str_strcasecmp(tmp, np->m->desc) == 0); + } + + if (clear_this) + { +#ifdef USE_SIDEBAR + mutt_sb_notify_mailbox(np->m, false); +#endif +#ifdef USE_INOTIFY + mutt_monitor_remove(np->m); +#endif + if (Context && (Context->mailbox == np->m)) + { + np->m->flags |= MB_HIDDEN; + } + else + { + mx_ac_remove(np->m); + mailbox_free(&np->m); + } + STAILQ_REMOVE(&AllMailboxes, np, MailboxNode, entries); + FREE(&np); + continue; + } + } + } + return MUTT_CMD_SUCCESS; +} + /** * parse_unmy_hdr - Parse the 'unmy_hdr' command - Implements ::command_t */ diff --git a/init.h b/init.h index 83892262c..a8ea7c7d4 100644 --- a/init.h +++ b/init.h @@ -102,6 +102,10 @@ enum MuttSetCommand MUTT_SET_RESET, ///< default is to reset all vars to default }; +/* parameter to parse_mailboxes */ +#define MUTT_NAMED (1 << 0) +#define MUTT_VIRTUAL (1 << 1) + #define UL (unsigned long) #define IP (intptr_t) #endif /* _MAKEDOC */ @@ -4757,6 +4761,7 @@ static enum CommandResult parse_group (struct Buffer *buf, struct Buff static enum CommandResult parse_ifdef (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_ignore (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_lists (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); +static enum CommandResult parse_mailboxes (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_my_hdr (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); #ifdef USE_SIDEBAR static enum CommandResult parse_path_list (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); @@ -4780,6 +4785,7 @@ static enum CommandResult parse_unalternates (struct Buffer *buf, struct Buff static enum CommandResult parse_unattachments (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_unignore (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_unlists (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); +static enum CommandResult parse_unmailboxes (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_unmy_hdr (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_unreplace_list (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); static enum CommandResult parse_unstailq (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); @@ -4828,14 +4834,14 @@ const struct Command Commands[] = { { "lua-source", mutt_lua_source_file, 0 }, #endif { "macro", mutt_parse_macro, 0 }, - { "mailboxes", mutt_parse_mailboxes, 0 }, + { "mailboxes", parse_mailboxes, 0 }, { "mailto_allow", parse_stailq, UL &MailToAllow }, { "mbox-hook", mutt_parse_hook, MUTT_MBOX_HOOK }, { "message-hook", mutt_parse_hook, MUTT_MESSAGE_HOOK }, { "mime_lookup", parse_stailq, UL &MimeLookupList }, { "mono", mutt_parse_mono, 0 }, { "my_hdr", parse_my_hdr, 0 }, - { "named-mailboxes", mutt_parse_mailboxes, MUTT_NAMED }, + { "named-mailboxes", parse_mailboxes, MUTT_NAMED }, { "nospam", parse_spam_list, MUTT_NOSPAM }, #ifdef USE_COMPRESSED { "open-hook", mutt_parse_hook, MUTT_OPEN_HOOK }, @@ -4879,7 +4885,7 @@ const struct Command Commands[] = { { "unhook", mutt_parse_unhook, 0 }, { "unignore", parse_unignore, 0 }, { "unlists", parse_unlists, 0 }, - { "unmailboxes", mutt_parse_unmailboxes, 0 }, + { "unmailboxes", parse_unmailboxes, 0 }, { "unmailto_allow", parse_unstailq, UL &MailToAllow }, { "unmime_lookup", parse_unstailq, UL &MimeLookupList }, { "unmono", mutt_parse_unmono, 0 }, @@ -4896,8 +4902,8 @@ const struct Command Commands[] = { { "unsubscribe-from", parse_unsubscribe_from, 0 }, #endif #ifdef USE_NOTMUCH - { "unvirtual-mailboxes", mutt_parse_unmailboxes, MUTT_VIRTUAL }, - { "virtual-mailboxes", mutt_parse_mailboxes, MUTT_VIRTUAL | MUTT_NAMED }, + { "unvirtual-mailboxes", parse_unmailboxes, MUTT_VIRTUAL }, + { "virtual-mailboxes", parse_mailboxes, MUTT_VIRTUAL | MUTT_NAMED }, #endif { NULL, NULL, 0 }, }; diff --git a/mailbox.c b/mailbox.c index c7f83ef26..4ebef89b8 100644 --- a/mailbox.c +++ b/mailbox.c @@ -319,162 +319,6 @@ void mutt_update_mailbox(struct Mailbox *m) m->size = 0; } -/** - * mutt_parse_mailboxes - Parse the 'mailboxes' command - Implements ::command_t - * - * This is also used by 'virtual-mailboxes'. - */ -enum CommandResult mutt_parse_mailboxes(struct Buffer *buf, struct Buffer *s, - unsigned long data, struct Buffer *err) -{ - while (MoreArgs(s)) - { - struct Mailbox *m = mailbox_new(); - - if (data & MUTT_NAMED) - { - mutt_extract_token(buf, s, 0); - if (buf->data && *buf->data) - { - m->desc = mutt_str_strdup(buf->data); - } - else - { - mailbox_free(&m); - continue; - } - } - - mutt_extract_token(buf, s, 0); - if (mutt_buffer_is_empty(buf)) - { - /* Skip empty tokens. */ - mailbox_free(&m); - continue; - } - - mutt_str_strfcpy(m->path, buf->data, sizeof(m->path)); - /* int rc = */ mx_path_canon2(m, Folder); - - bool new_account = false; - struct Account *a = mx_ac_find(m); - if (!a) - { - a = account_new(); - a->magic = m->magic; - TAILQ_INSERT_TAIL(&AllAccounts, a, entries); - new_account = true; - } - - if (!new_account) - { - struct Mailbox *old_m = mx_mbox_find(a, m->realpath); - if (old_m) - { - if (old_m->flags == MB_HIDDEN) - { - old_m->flags = MB_NORMAL; - mutt_sb_notify_mailbox(old_m, true); - struct MailboxNode *mn = mutt_mem_calloc(1, sizeof(*mn)); - mn->m = old_m; - STAILQ_INSERT_TAIL(&AllMailboxes, mn, entries); - } - mailbox_free(&m); - continue; - } - } - - if (mx_ac_add(a, m) < 0) - { - //error - mailbox_free(&m); - continue; - } - - struct MailboxNode *mn = mutt_mem_calloc(1, sizeof(*mn)); - mn->m = m; - STAILQ_INSERT_TAIL(&AllMailboxes, mn, entries); - -#ifdef USE_SIDEBAR - mutt_sb_notify_mailbox(m, true); -#endif -#ifdef USE_INOTIFY - mutt_monitor_add(m); -#endif - } - return MUTT_CMD_SUCCESS; -} - -/** - * mutt_parse_unmailboxes - Parse the 'unmailboxes' command - Implements ::command_t - * - * This is also used by 'unvirtual-mailboxes' - */ -enum CommandResult mutt_parse_unmailboxes(struct Buffer *buf, struct Buffer *s, - unsigned long data, struct Buffer *err) -{ - char tmp[PATH_MAX]; - bool tmp_valid = false; - bool clear_all = false; - - while (!clear_all && MoreArgs(s)) - { - mutt_extract_token(buf, s, 0); - - if (mutt_str_strcmp(buf->data, "*") == 0) - { - clear_all = true; - tmp_valid = false; - } - else - { - mutt_str_strfcpy(tmp, buf->data, sizeof(tmp)); - mutt_expand_path(tmp, sizeof(tmp)); - tmp_valid = true; - } - - struct MailboxNode *np = NULL; - struct MailboxNode *nptmp = NULL; - STAILQ_FOREACH_SAFE(np, &AllMailboxes, entries, nptmp) - { - /* Decide whether to delete all normal mailboxes or all virtual */ - bool virt = ((np->m->magic == MUTT_NOTMUCH) && (data & MUTT_VIRTUAL)); - bool norm = ((np->m->magic != MUTT_NOTMUCH) && !(data & MUTT_VIRTUAL)); - bool clear_this = clear_all && (virt || norm); - - /* Compare against path or desc? Ensure 'tmp' is valid */ - if (!clear_this && tmp_valid) - { - clear_this = (mutt_str_strcasecmp(tmp, np->m->path) == 0) || - (mutt_str_strcasecmp(tmp, np->m->desc) == 0); - } - - if (clear_this) - { -#ifdef USE_SIDEBAR - mutt_sb_notify_mailbox(np->m, false); -#endif -#ifdef USE_INOTIFY - mutt_monitor_remove(np->m); -#endif - if (Context && (Context->mailbox == np->m)) - { - np->m->flags |= MB_HIDDEN; - } - else - { - mx_ac_remove(np->m); - mailbox_free(&np->m); - } - STAILQ_REMOVE(&AllMailboxes, np, MailboxNode, entries); - FREE(&np); - continue; - } - } - } - return MUTT_CMD_SUCCESS; -} - /** * mutt_mailbox_check - Check all AllMailboxes for new mail * @param m_cur Current Mailbox diff --git a/mailbox.h b/mailbox.h index c8ef9e82e..cb779f088 100644 --- a/mailbox.h +++ b/mailbox.h @@ -42,10 +42,6 @@ extern bool MailCheckStats; extern short MailCheckStatsInterval; extern bool MaildirCheckCur; -/* parameter to mutt_parse_mailboxes */ -#define MUTT_NAMED (1 << 0) -#define MUTT_VIRTUAL (1 << 1) - #define MB_NORMAL 0 #define MB_HIDDEN 1 @@ -179,8 +175,6 @@ void mutt_mailbox(struct Mailbox *m_cur, char *s, size_t slen); bool mutt_mailbox_list(void); int mutt_mailbox_check(struct Mailbox *m_cur, int force); bool mutt_mailbox_notify(struct Mailbox *m_cur); -enum CommandResult mutt_parse_mailboxes(struct Buffer *path, struct Buffer *s, unsigned long data, struct Buffer *err); -enum CommandResult mutt_parse_unmailboxes(struct Buffer *path, struct Buffer *s, unsigned long data, struct Buffer *err); void mutt_mailbox_changed(struct Mailbox *m, enum MailboxNotification action); #endif /* MUTT_MAILBOX_H */