]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-reply.c
notmuch reply: Don't reply to address belonging to the user.
[notmuch] / notmuch-reply.c
index 35a735f13f321924eec34ac9d0a2b26c74df04a9..f217577e8f9b6762e49d682ecfc88a6c4eeddaee 100644 (file)
@@ -71,8 +71,30 @@ reply_part(GMimeObject *part, int *part_count)
     }
 }
 
+/* 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;
+}
+
 static void
 add_recipients_for_address_list (GMimeMessage *message,
+                                notmuch_config_t *config,
                                 GMimeRecipientType type,
                                 InternetAddressList *list)
 {
@@ -90,7 +112,8 @@ add_recipients_for_address_list (GMimeMessage *message,
            if (group_list == NULL)
                continue;
 
-           add_recipients_for_address_list (message, type, group_list);
+           add_recipients_for_address_list (message, config,
+                                            type, group_list);
        } else {
            InternetAddressMailbox *mailbox;
            const char *name;
@@ -101,13 +124,15 @@ add_recipients_for_address_list (GMimeMessage *message,
            name = internet_address_get_name (address);
            addr = internet_address_mailbox_get_addr (mailbox);
 
-           g_mime_message_add_recipient (message, type, name, addr);
+           if (! address_is_users (addr, config))
+               g_mime_message_add_recipient (message, type, name, addr);
        }
     }
 }
 
 static void
 add_recipients_for_string (GMimeMessage *message,
+                          notmuch_config_t *config,
                           GMimeRecipientType type,
                           const char *recipients)
 {
@@ -117,16 +142,16 @@ add_recipients_for_string (GMimeMessage *message,
     if (list == NULL)
        return;
 
-    add_recipients_for_address_list (message, type, list);
+    add_recipients_for_address_list (message, config, type, list);
 }
 
 int
 notmuch_reply_command (void *ctx, int argc, char *argv[])
 {
-    void *local = talloc_new (ctx);
-    notmuch_query_t *query = NULL;
-    notmuch_database_t *notmuch = NULL;
-    GMimeMessage *reply = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
+    GMimeMessage *reply;
     char *query_string;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
@@ -145,24 +170,24 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
     };
     unsigned int i;
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
 
-    query_string = query_string_from_args (local, argc, argv);
+    query_string = query_string_from_args (ctx, argc, argv);
     if (query_string == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     for (messages = notmuch_query_search_messages (query);
@@ -175,8 +200,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
        reply = g_mime_message_new (1);
        if (reply == NULL) {
            fprintf (stderr, "Out of memory\n");
-           ret = 1;
-           goto DONE;
+           return 1;
        }
 
        /* XXX: We need a configured email address (or addresses) for
@@ -195,7 +219,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
        for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
            recipients = notmuch_message_get_header (message,
                                                     reply_to_map[i].header);
-           add_recipients_for_string (reply,
+           add_recipients_for_string (reply, config,
                                       reply_to_map[i].recipient_type,
                                       recipients);
        }
@@ -230,18 +254,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
        notmuch_message_destroy (message);
     }
 
-  DONE:
-    if (local)
-       talloc_free (local);
-
-    if (query)
-       notmuch_query_destroy (query);
-
-    if (notmuch)
-       notmuch_database_close (notmuch);
-
-    if (reply)
-       g_object_unref (G_OBJECT (reply));
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
 
     return ret;
 }