From caafb32d07e73c92297a1031478531cc1e4ff486 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Thu, 16 May 2019 13:29:38 +0000 Subject: [PATCH] Fix and refactor mutt_is_list_recipient and mutt_is_subscribed_list_recipient --- hdrline.c | 2 +- hdrline.h | 2 +- pattern.c | 53 +++++++++++++++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/hdrline.c b/hdrline.c index e830ced85..b231a1c25 100644 --- a/hdrline.c +++ b/hdrline.c @@ -110,7 +110,7 @@ bool mutt_is_mail_list(const struct Address *addr) * @param addr Address to test * @retval true If it's a subscribed mailing list */ -bool mutt_is_subscribed_list(struct Address *addr) +bool mutt_is_subscribed_list(const struct Address *addr) { if (!mutt_regexlist_match(&UnMailLists, addr->mailbox) && !mutt_regexlist_match(&UnSubscribedLists, addr->mailbox)) diff --git a/hdrline.h b/hdrline.h index aeefbd52d..a18018171 100644 --- a/hdrline.h +++ b/hdrline.h @@ -49,7 +49,7 @@ struct HdrFormatInfo }; bool mutt_is_mail_list(const struct Address *addr); -bool mutt_is_subscribed_list(struct Address *addr); +bool mutt_is_subscribed_list(const struct Address *addr); void mutt_make_string_flags(char *buf, size_t buflen, const char *s, struct Context *ctx, struct Mailbox *m, struct Email *e, MuttFormatFlags flags); diff --git a/pattern.c b/pattern.c index 371381104..385dd1502 100644 --- a/pattern.c +++ b/pattern.c @@ -1729,27 +1729,50 @@ static bool match_reference(struct Pattern *pat, struct ListHead *refs) } /** - * mutt_is_subscribed_list_recipient - Matches subscribed mailing lists - * @param alladdr If true, ALL Addresses must be on the subscribed list + * typedef addr_predicate_t - Test an Address for some condition + * @param a Address to test + * @retval bool True if Address matches the test + */ +typedef bool (*addr_predicate_t)(const struct Address *a); + +/** + * mutt_is_predicate_recipient - Test an Envelopes Addresses using a predicate function + * @param alladdr If true, ALL Addresses must match * @param e Envelope - * @retval true One Address is subscribed (alladdr is false) - * @retval true All the Addresses are subscribed (alladdr is true) + * @param p Predicate function, e.g. mutt_is_subscribed_list() + * @retval true One Address matches (alladdr is false) + * @retval true All the Addresses match (alladdr is true) + * + * Test the 'To' and 'Cc' fields of an Address using a test function (the predicate). */ -int mutt_is_subscribed_list_recipient(bool alladdr, struct Envelope *e) +static int mutt_is_predicate_recipient(bool alladdr, struct Envelope *e, addr_predicate_t p) { - struct AddressList *als[] = { &e->to, &e->cc, NULL }; - struct AddressNode *an = NULL; - for (struct AddressList *al = *als; al; ++al) + struct AddressList *als[] = { &e->to, &e->cc }; + for (size_t i = 0; i < mutt_array_size(als); ++i) { + struct AddressList *al = als[i]; + struct AddressNode *an = NULL; TAILQ_FOREACH(an, al, entries) { - if (alladdr ^ mutt_is_subscribed_list(an->addr)) + if (alladdr ^ p(an->addr)) return !alladdr; } } return alladdr; } +/** + * mutt_is_subscribed_list_recipient - Matches subscribed mailing lists + * @param alladdr If true, ALL Addresses must be on the subscribed list + * @param e Envelope + * @retval true One Address is subscribed (alladdr is false) + * @retval true All the Addresses are subscribed (alladdr is true) + */ +int mutt_is_subscribed_list_recipient(bool alladdr, struct Envelope *e) +{ + return mutt_is_predicate_recipient(alladdr, e, &mutt_is_subscribed_list); +} + /** * mutt_is_list_recipient - Matches known mailing lists * @param alladdr If true, ALL Addresses must be mailing lists @@ -1759,17 +1782,7 @@ int mutt_is_subscribed_list_recipient(bool alladdr, struct Envelope *e) */ int mutt_is_list_recipient(bool alladdr, struct Envelope *e) { - struct AddressList *als[] = { &e->to, &e->cc, NULL }; - struct AddressNode *an = NULL; - for (struct AddressList *al = *als; al; ++al) - { - TAILQ_FOREACH(an, al, entries) - { - if (alladdr ^ mutt_is_mail_list(an->addr)) - return !alladdr; - } - } - return alladdr; + return mutt_is_predicate_recipient(alladdr, e, &mutt_is_mail_list); } /** -- 2.40.0