From: Richard Russon Date: Mon, 21 Jan 2019 22:52:26 +0000 (+0000) Subject: simplify bitfields in Mailbox X-Git-Tag: 2019-10-25~372^2~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f813128888496ae348197358669c29c845ac06bf;p=neomutt simplify bitfields in Mailbox --- diff --git a/flags.c b/flags.c index 8b62a2e96..d19ec791b 100644 --- a/flags.c +++ b/flags.c @@ -71,7 +71,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, { case MUTT_DELETE: - if (!mutt_bit_isset(m->rights, MUTT_ACL_DELETE)) + if (!(m->rights & MUTT_ACL_DELETE)) return; if (bf) @@ -123,7 +123,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_PURGE: - if (!mutt_bit_isset(m->rights, MUTT_ACL_DELETE)) + if (!(m->rights & MUTT_ACL_DELETE)) return; if (bf) @@ -137,7 +137,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_NEW: - if (!mutt_bit_isset(m->rights, MUTT_ACL_SEEN)) + if (!(m->rights & MUTT_ACL_SEEN)) return; if (bf) @@ -176,7 +176,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_OLD: - if (!mutt_bit_isset(m->rights, MUTT_ACL_SEEN)) + if (!(m->rights & MUTT_ACL_SEEN)) return; if (bf) @@ -208,7 +208,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_READ: - if (!mutt_bit_isset(m->rights, MUTT_ACL_SEEN)) + if (!(m->rights & MUTT_ACL_SEEN)) return; if (bf) @@ -244,7 +244,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_REPLIED: - if (!mutt_bit_isset(m->rights, MUTT_ACL_WRITE)) + if (!(m->rights & MUTT_ACL_WRITE)) return; if (bf) @@ -279,7 +279,7 @@ void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, case MUTT_FLAG: - if (!mutt_bit_isset(m->rights, MUTT_ACL_WRITE)) + if (!(m->rights & MUTT_ACL_WRITE)) return; if (bf) diff --git a/imap/command.c b/imap/command.c index ce724f3ce..7a5b349e0 100644 --- a/imap/command.c +++ b/imap/command.c @@ -691,54 +691,52 @@ static void cmd_parse_myrights(struct ImapAccountData *adata, const char *s) s = imap_next_word((char *) s); /* zero out current rights set */ - memset(adata->mailbox->rights, 0, sizeof(adata->mailbox->rights)); + adata->mailbox->rights = 0; while (*s && !isspace((unsigned char) *s)) { switch (*s) { case 'a': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_ADMIN); + adata->mailbox->rights |= MUTT_ACL_ADMIN; break; case 'e': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_EXPUNGE); + adata->mailbox->rights |= MUTT_ACL_EXPUNGE; break; case 'i': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_INSERT); + adata->mailbox->rights |= MUTT_ACL_INSERT; break; case 'k': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_CREATE); + adata->mailbox->rights |= MUTT_ACL_CREATE; break; case 'l': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_LOOKUP); + adata->mailbox->rights |= MUTT_ACL_LOOKUP; break; case 'p': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_POST); + adata->mailbox->rights |= MUTT_ACL_POST; break; case 'r': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_READ); + adata->mailbox->rights |= MUTT_ACL_READ; break; case 's': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_SEEN); + adata->mailbox->rights |= MUTT_ACL_SEEN; break; case 't': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_DELETE); + adata->mailbox->rights |= MUTT_ACL_DELETE; break; case 'w': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_WRITE); + adata->mailbox->rights |= MUTT_ACL_WRITE; break; case 'x': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_DELMX); + adata->mailbox->rights |= MUTT_ACL_DELMX; break; /* obsolete rights */ case 'c': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_CREATE); - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_DELMX); + adata->mailbox->rights |= MUTT_ACL_CREATE | MUTT_ACL_DELMX; break; case 'd': - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_DELETE); - mutt_bit_set(adata->mailbox->rights, MUTT_ACL_EXPUNGE); + adata->mailbox->rights |= MUTT_ACL_DELETE | MUTT_ACL_EXPUNGE; break; default: mutt_debug(1, "Unknown right: %c\n", *s); diff --git a/imap/imap.c b/imap/imap.c index 1fa6413ab..94ba8c9df 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -153,18 +153,18 @@ static char *get_flags(struct ListHead *hflags, char *s) } /** - * set_flag - append str to flags if we currently have permission according to aclbit - * @param[in] m Selected Imap Mailbox - * @param[in] aclbit Permissions, e.g. #MUTT_ACL_WRITE - * @param[in] flag Does the email have the flag set? - * @param[in] str Server flag name - * @param[out] flags Buffer for server command - * @param[in] flsize Length of buffer + * set_flag - append str to flags if we currently have permission according to aclflag + * @param[in] m Selected Imap Mailbox + * @param[in] aclflag Permissions, e.g. #MUTT_ACL_WRITE + * @param[in] flag Does the email have the flag set? + * @param[in] str Server flag name + * @param[out] flags Buffer for server command + * @param[in] flsize Length of buffer */ -static void set_flag(struct Mailbox *m, int aclbit, int flag, const char *str, +static void set_flag(struct Mailbox *m, int aclflag, int flag, const char *str, char *flags, size_t flsize) { - if (mutt_bit_isset(m->rights, aclbit)) + if (m->rights & aclflag) if (flag && imap_has_flag(&imap_mdata_get(m)->flags, str)) mutt_str_strcat(flags, flsize, str); } @@ -310,7 +310,7 @@ static int sync_helper(struct Mailbox *m, int right, int flag, const char *name) if (!m) return -1; - if (!mutt_bit_isset(m->rights, right)) + if ((m->rights & right) == 0) return 0; if (right == MUTT_ACL_WRITE && !imap_has_flag(&imap_mdata_get(m)->flags, name)) @@ -1111,7 +1111,7 @@ int imap_sync_message_for_copy(struct Mailbox *m, struct Email *e, set_flag(m, MUTT_ACL_DELETE, imap_edata_get(e)->deleted, "\\Deleted ", flags, sizeof(flags)); - if (mutt_bit_isset(m->rights, MUTT_ACL_WRITE)) + if (m->rights & MUTT_ACL_WRITE) { /* restore system flags */ if (imap_edata_get(e)->flags_system) @@ -1139,7 +1139,7 @@ int imap_sync_message_for_copy(struct Mailbox *m, struct Email *e, flags, sizeof(flags)); /* erase custom flags */ - if (mutt_bit_isset(m->rights, MUTT_ACL_WRITE) && imap_edata_get(e)->flags_remote) + if ((m->rights & MUTT_ACL_WRITE) && imap_edata_get(e)->flags_remote) mutt_str_strcat(flags, sizeof(flags), imap_edata_get(e)->flags_remote); mutt_str_remove_trailing_ws(flags); @@ -1656,7 +1656,7 @@ int imap_sync_mailbox(struct Mailbox *m, bool expunge, bool close) return rc; /* if we are expunging anyway, we can do deleted messages very quickly... */ - if (expunge && mutt_bit_isset(m->rights, MUTT_ACL_DELETE)) + if (expunge && (m->rights & MUTT_ACL_DELETE)) { rc = imap_exec_msgset(m, "UID STORE", "+FLAGS.SILENT (\\Deleted)", MUTT_DELETED, true, false); @@ -1788,7 +1788,7 @@ int imap_sync_mailbox(struct Mailbox *m, bool expunge, bool close) m->changed = false; /* We must send an EXPUNGE command if we're not closing. */ - if (expunge && !close && mutt_bit_isset(m->rights, MUTT_ACL_DELETE)) + if (expunge && !close && (m->rights & MUTT_ACL_DELETE)) { mutt_message(_("Expunging messages from server...")); /* Set expunge bit so we don't get spurious reopened messages */ @@ -1972,7 +1972,7 @@ static int imap_mbox_open(struct Mailbox *m) /* clear mailbox status */ adata->status = 0; - memset(m->rights, 0, sizeof(m->rights)); + m->rights = 0; mdata->new_mail_count = 0; mutt_message(_("Selecting %s..."), mdata->name); @@ -1986,14 +1986,8 @@ static int imap_mbox_open(struct Mailbox *m) /* assume we have all rights if ACL is unavailable */ else { - mutt_bit_set(m->rights, MUTT_ACL_LOOKUP); - mutt_bit_set(m->rights, MUTT_ACL_READ); - mutt_bit_set(m->rights, MUTT_ACL_SEEN); - mutt_bit_set(m->rights, MUTT_ACL_WRITE); - mutt_bit_set(m->rights, MUTT_ACL_INSERT); - mutt_bit_set(m->rights, MUTT_ACL_POST); - mutt_bit_set(m->rights, MUTT_ACL_CREATE); - mutt_bit_set(m->rights, MUTT_ACL_DELETE); + m->rights |= MUTT_ACL_LOOKUP | MUTT_ACL_READ | MUTT_ACL_SEEN | MUTT_ACL_WRITE | + MUTT_ACL_INSERT | MUTT_ACL_POST | MUTT_ACL_CREATE | MUTT_ACL_DELETE; } /* pipeline the postponed count if possible */ @@ -2135,9 +2129,8 @@ static int imap_mbox_open(struct Mailbox *m) } } - if (!(mutt_bit_isset(m->rights, MUTT_ACL_DELETE) || - mutt_bit_isset(m->rights, MUTT_ACL_SEEN) || mutt_bit_isset(m->rights, MUTT_ACL_WRITE) || - mutt_bit_isset(m->rights, MUTT_ACL_INSERT))) + if (!((m->rights & MUTT_ACL_DELETE) || (m->rights & MUTT_ACL_SEEN) || + (m->rights & MUTT_ACL_WRITE) || (m->rights & MUTT_ACL_INSERT))) { m->readonly = true; } @@ -2385,7 +2378,7 @@ static int imap_tags_commit(struct Mailbox *m, struct Email *e, char *buf) if (*buf == '\0') buf = NULL; - if (!mutt_bit_isset(adata->mailbox->rights, MUTT_ACL_WRITE)) + if (!(adata->mailbox->rights & MUTT_ACL_WRITE)) return 0; snprintf(uid, sizeof(uid), "%u", imap_edata_get(e)->uid); diff --git a/index.c b/index.c index 77ec40a62..c4b3fce3d 100644 --- a/index.c +++ b/index.c @@ -153,7 +153,7 @@ static const char *NoVisible = N_("No visible messages"); } #define CHECK_ACL(aclbit, action) \ - if (!mutt_bit_isset(Context->mailbox->rights, aclbit)) \ + if (!(Context->mailbox->rights & aclbit)) \ { \ mutt_flushinp(); \ /* L10N: %s is one of the CHECK_ACL entries below. */ \ @@ -3103,8 +3103,7 @@ int mutt_index_menu(void) edit = true; } else if (op == OP_EDIT_OR_VIEW_RAW_MESSAGE) - edit = !Context->mailbox->readonly && - mutt_bit_isset(Context->mailbox->rights, MUTT_ACL_INSERT); + edit = !Context->mailbox->readonly && (Context->mailbox->rights & MUTT_ACL_INSERT); else edit = false; diff --git a/mailbox.h b/mailbox.h index f4fcdd4e7..3f63d2a01 100644 --- a/mailbox.h +++ b/mailbox.h @@ -58,25 +58,21 @@ enum MailboxNotification }; /** - * enum AclRights - ACL Rights - * - * These show permission to... + * ACL Rights - These show permission to... */ -enum AclRights -{ - MUTT_ACL_ADMIN = 0, ///< administer the account (get/set permissions) - MUTT_ACL_CREATE, ///< create a mailbox - MUTT_ACL_DELETE, ///< delete a message - MUTT_ACL_DELMX, ///< delete a mailbox - MUTT_ACL_EXPUNGE, ///< expunge messages - MUTT_ACL_INSERT, ///< add/copy into the mailbox (used when editing a message) - MUTT_ACL_LOOKUP, ///< lookup mailbox (visible to 'list') - MUTT_ACL_POST, ///< post (submit messages to the server) - MUTT_ACL_READ, ///< read the mailbox - MUTT_ACL_SEEN, ///< change the 'seen' status of a message - MUTT_ACL_WRITE, ///< write to a message (for flagging, or linking threads) - MUTT_ACL_MAX, -}; +#define MUTT_ACL_ADMIN (1 << 0) ///< administer the account (get/set permissions) +#define MUTT_ACL_CREATE (1 << 1) ///< create a mailbox +#define MUTT_ACL_DELETE (1 << 2) ///< delete a message +#define MUTT_ACL_DELMX (1 << 3) ///< delete a mailbox +#define MUTT_ACL_EXPUNGE (1 << 4) ///< expunge messages +#define MUTT_ACL_INSERT (1 << 5) ///< add/copy into the mailbox (used when editing a message) +#define MUTT_ACL_LOOKUP (1 << 6) ///< lookup mailbox (visible to 'list') +#define MUTT_ACL_POST (1 << 7) ///< post (submit messages to the server) +#define MUTT_ACL_READ (1 << 8) ///< read the mailbox +#define MUTT_ACL_SEEN (1 << 9) ///< change the 'seen' status of a message +#define MUTT_ACL_WRITE (1 << 10) ///< write to a message (for flagging or linking threads) + +#define MUTT_ACL_ALL ((1 << 11) - 1) /** * struct Mailbox - A mailbox @@ -120,7 +116,7 @@ struct Mailbox bool quiet : 1; /**< inhibit status messages? */ bool readonly : 1; /**< don't allow changes to the mailbox */ - unsigned char rights[(MUTT_ACL_MAX + 7) / 8]; /**< ACL bits */ + unsigned int rights; /**< ACL bits */ #ifdef USE_COMPRESSED void *compress_info; /**< compressed mbox module private data */ diff --git a/mx.c b/mx.c index af0fa4d0f..8a50d1a30 100644 --- a/mx.c +++ b/mx.c @@ -288,9 +288,7 @@ struct Context *mx_mbox_open(struct Mailbox *m, int flags) m->msg_unread = 0; m->msg_flagged = 0; - - for (int i = 0; i < MUTT_ACL_MAX; i++) - mutt_bit_set(m->rights, i); + m->rights = MUTT_ACL_ALL; if (flags & MUTT_QUIET) m->quiet = true; diff --git a/nntp/nntp.c b/nntp/nntp.c index b9e0e5d70..b7720cb77 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -2483,7 +2483,7 @@ static int nntp_mbox_open(struct Mailbox *m) return -1; } - mutt_bit_unset(m->rights, MUTT_ACL_INSERT); + m->rights &= ~MUTT_ACL_INSERT; // Clear the flag if (!mdata->newsrc_ent && !mdata->subscribed && !SaveUnsubscribed) m->readonly = true; @@ -2561,10 +2561,8 @@ static int nntp_mbox_open(struct Mailbox *m) nntp_hcache_update(mdata, hc); #endif if (!hc) - { - mutt_bit_unset(m->rights, MUTT_ACL_WRITE); - mutt_bit_unset(m->rights, MUTT_ACL_DELETE); - } + m->rights &= ~(MUTT_ACL_WRITE | MUTT_ACL_DELETE); // Clear the flags + nntp_newsrc_close(adata); rc = nntp_fetch_headers(m, hc, first, mdata->last_message, false); #ifdef USE_HCACHE diff --git a/pager.c b/pager.c index 84615e524..cedf8b72d 100644 --- a/pager.c +++ b/pager.c @@ -135,7 +135,7 @@ static struct Email *OldHdr = NULL; } #define CHECK_ACL(aclbit, action) \ - if (!Context || !mutt_bit_isset(Context->mailbox->rights, aclbit)) \ + if (!Context || !(Context->mailbox->rights & aclbit)) \ { \ mutt_flushinp(); \ /* L10N: %s is one of the CHECK_ACL entries below. */ \ diff --git a/pop/pop.c b/pop/pop.c index e85afaab1..a1d35e05e 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -863,13 +863,11 @@ static int pop_mbox_open(struct Mailbox *m) adata->bcache = mutt_bcache_open(&acct, NULL); /* init (hard-coded) ACL rights */ - memset(m->rights, 0, sizeof(m->rights)); - mutt_bit_set(m->rights, MUTT_ACL_SEEN); - mutt_bit_set(m->rights, MUTT_ACL_DELETE); + m->rights = MUTT_ACL_SEEN | MUTT_ACL_DELETE; #ifdef USE_HCACHE /* flags are managed using header cache, so it only makes sense to * enable them in that case */ - mutt_bit_set(m->rights, MUTT_ACL_WRITE); + m->rights |= MUTT_ACL_WRITE; #endif while (true)