From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id AADE5431FC2 for ; Fri, 11 May 2012 07:33:11 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MG+GfKjqnVDV for ; Fri, 11 May 2012 07:33:11 -0700 (PDT) Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id D8F01431FAE for ; Fri, 11 May 2012 07:33:10 -0700 (PDT) Received: by lbbgk8 with SMTP id gk8so2103376lbb.26 for ; Fri, 11 May 2012 07:33:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:x-mailer:x-gm-message-state; bh=M2f2d5cmAPw4V4gj0YK3vSVhSNrLGgKTEfzeTx+q9Eg=; b=V8q+6g/Uik4CQwtaCnJJ9sR28ipd5JkP9n/U210+c2snAm0RzgcRdNVOVxiN38Hdz6 /BYqiVp0L7uEtBPj6NkwA1I+EV45Yns17pw8/ode9GdiupZrrt5veQfsETJJobq/5Thv avHYUU0+n0ehia0x7Y2CdJMAWS5JIFY6k5dhVJTlByZxXB8t+NOlNQXy60i8s4b3tryD q/5yHqHKGKIVTA0FG9AonbSl1m1EF8TcqojjgnSGPgiv2xr+85hB+1F4PL3aDbKgVU8a lqegBJs+IfDUJR5QB55c9ftg9AS4nNa6TnpAofG/UqC41KOAQwleQIpTQ0W+X1RlHDKg Bjlw== Received: by 10.112.47.161 with SMTP id e1mr1913440lbn.42.1336746789256; Fri, 11 May 2012 07:33:09 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi. [80.220.80.68]) by mx.google.com with ESMTPS id pb13sm9590340lab.16.2012.05.11.07.33.07 (version=SSLv3 cipher=OTHER); Fri, 11 May 2012 07:33:08 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH 1/2] cli: add user address matching helpers for notmuch reply Date: Fri, 11 May 2012 17:33:04 +0300 Message-Id: X-Mailer: git-send-email 1.7.9.5 X-Gm-Message-State: ALoCoQnRNmuvxBP3pSN8oQ7osmZ/MqoMGaSLF7eHMwdAgCVGtQw3fjOBYE+8rx6BepcrAheO9ZG/ X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 May 2012 14:33:11 -0000 Add a multi-purpose address_match() function for matching strings against user's configured primary and other email addresses. Add thin wrappers user_address_in_string() and string_in_user_address() for ease of use, and also convert existing address_is_users() to wrapper for the same. No functional changes. Signed-off-by: Jani Nikula --- notmuch-reply.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/notmuch-reply.c b/notmuch-reply.c index 7184a5d..0c82755 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -98,25 +98,77 @@ format_part_reply (mime_node_t *node) format_part_reply (mime_node_child (node, i)); } -/* Is the given address configured as one of the user's "personal" or - * "other" addresses. */ -static int -address_is_users (const char *address, notmuch_config_t *config) +typedef enum { + USER_ADDRESS_IN_STRING, + STRING_IN_USER_ADDRESS, + STRING_IS_USER_ADDRESS, +} address_match_t; + +/* Match given string against given address according to mode. */ +static notmuch_bool_t +match_address (const char *str, const char *address, address_match_t mode) +{ + switch (mode) { + case USER_ADDRESS_IN_STRING: + return strcasestr (str, address) != NULL; + case STRING_IN_USER_ADDRESS: + return strcasestr (address, str) != NULL; + case STRING_IS_USER_ADDRESS: + return strcasecmp (address, str) == 0; + } + + return FALSE; +} + +/* Match given string against user's configured "primary" and "other" + * addresses according to mode. */ +static const char * +address_match (const char *str, notmuch_config_t *config, address_match_t mode) { const char *primary; const char **other; size_t i, other_len; + if (!str || *str == '\0') + return NULL; + primary = notmuch_config_get_user_primary_email (config); - if (strcasecmp (primary, address) == 0) - return 1; + if (match_address (str, primary, mode)) + return primary; other = notmuch_config_get_user_other_email (config, &other_len); - for (i = 0; i < other_len; i++) - if (strcasecmp (other[i], address) == 0) - return 1; + for (i = 0; i < other_len; i++) { + if (match_address (str, other[i], mode)) + return other[i]; + } - return 0; + return NULL; +} + +/* Does the given string contain an address configured as one of the + * user's "primary" or "other" addresses. If so, return the matching + * address, NULL otherwise. */ +static const char * +user_address_in_string (const char *str, notmuch_config_t *config) +{ + return address_match (str, config, USER_ADDRESS_IN_STRING); +} + +/* Do any of the addresses configured as one of the user's "primary" + * or "other" addresses contain the given string. If so, return the + * matching address, NULL otherwise. */ +static const char * +string_in_user_address (const char *str, notmuch_config_t *config) +{ + return address_match (str, config, STRING_IN_USER_ADDRESS); +} + +/* Is the given address configured as one of the user's "primary" or + * "other" addresses. */ +static notmuch_bool_t +address_is_users (const char *address, notmuch_config_t *config) +{ + return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL; } /* Scan addresses in 'list'. -- 1.7.9.5