+/* 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)
+{
+ const char *primary;
+ char **other;
+ unsigned int i, other_len;
+
+ primary = notmuch_config_get_user_primary_email (config);
+ if (strcmp (primary, address) == 0)
+ return 1;
+
+ other = notmuch_config_get_user_other_email (config, &other_len);
+ for (i = 0; i < other_len; i++)
+ if (strcmp (other[i], address) == 0)
+ return 1;
+
+ return 0;
+}
+
+/* For each address in 'list' that is not configured as one of the
+ * user's addresses in 'config', add that address to 'message' as an
+ * address of 'type'.
+ *
+ * The first address encountered that *is* the user's address will be
+ * returned, (otherwise NULL is returned).
+ */
+static const char *
+add_recipients_for_address_list (GMimeMessage *message,
+ notmuch_config_t *config,
+ GMimeRecipientType type,
+ InternetAddressList *list)
+{
+ InternetAddress *address;
+ int i;
+ const char *ret = NULL;
+
+ for (i = 0; i < internet_address_list_length (list); i++) {
+ address = internet_address_list_get_address (list, i);
+ if (INTERNET_ADDRESS_IS_GROUP (address)) {
+ InternetAddressGroup *group;
+ InternetAddressList *group_list;
+
+ group = INTERNET_ADDRESS_GROUP (address);
+ group_list = internet_address_group_get_members (group);
+ if (group_list == NULL)
+ continue;
+
+ add_recipients_for_address_list (message, config,
+ type, group_list);
+ } else {
+ InternetAddressMailbox *mailbox;
+ const char *name;
+ const char *addr;
+
+ mailbox = INTERNET_ADDRESS_MAILBOX (address);
+
+ name = internet_address_get_name (address);
+ addr = internet_address_mailbox_get_addr (mailbox);
+
+ if (address_is_users (addr, config)) {
+ if (ret == NULL)
+ ret = addr;
+ } else {
+ g_mime_message_add_recipient (message, type, name, addr);
+ }
+ }
+ }
+
+ return ret;
+}
+
+/* For each address in 'recipients' that is not configured as one of
+ * the user's addresses in 'config', add that address to 'message' as
+ * an address of 'type'.
+ *
+ * The first address encountered that *is* the user's address will be
+ * returned, (otherwise NULL is returned).
+ */
+static const char *
+add_recipients_for_string (GMimeMessage *message,
+ notmuch_config_t *config,
+ GMimeRecipientType type,
+ const char *recipients)
+{
+ InternetAddressList *list;
+
+ list = internet_address_list_parse_string (recipients);
+ if (list == NULL)
+ return NULL;
+
+ return add_recipients_for_address_list (message, config, type, list);
+}
+