]> rtime.felk.cvut.cz Git - notmuch.git/blobdiff - notmuch-search.c
cli: address: Add --filter-by option to configure address filtering
[notmuch.git] / notmuch-search.c
index 14b9f01c5ad1d731d802e2f7a18bb5f9ef8e6597..8116cae84405a81c3ffaa2613ef6f41f2bf28e9b 100644 (file)
@@ -43,6 +43,14 @@ typedef enum {
     NOTMUCH_FORMAT_SEXP
 } format_sel_t;
 
+typedef enum {
+    FILTER_BY_NAMEADDR = 0,
+    FILTER_BY_NAME,
+    FILTER_BY_ADDR,
+    FILTER_BY_ADDRFOLD,
+    FILTER_BY_NAMEADDRFOLD,
+} filter_by_t;
+
 typedef struct {
     notmuch_database_t *notmuch;
     format_sel_t format_sel;
@@ -55,6 +63,7 @@ typedef struct {
     int limit;
     int dupe;
     GHashTable *addresses;
+    filter_by_t filter_by;
 } search_context_t;
 
 typedef struct {
@@ -243,16 +252,44 @@ do_search_threads (search_context_t *ctx)
     return 0;
 }
 
-/* Returns TRUE iff name and addr is duplicate. If not, stores the
- * name/addr pair in order to detect subsequent duplicates. */
+/* Returns TRUE iff name and/or addr is considered duplicate. If not,
+ * stores the name/addr pair in order to detect subsequent
+ * duplicates. */
 static notmuch_bool_t
 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
 {
     notmuch_bool_t duplicate;
     char *key;
+    gchar *addrfold = NULL;
     mailbox_t *mailbox;
 
-    key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
+    if (ctx->filter_by == FILTER_BY_ADDRFOLD ||
+       ctx->filter_by == FILTER_BY_NAMEADDRFOLD)
+       addrfold = g_utf8_casefold (addr, -1);
+
+    switch (ctx->filter_by) {
+    case FILTER_BY_NAMEADDR:
+       key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
+       break;
+    case FILTER_BY_NAMEADDRFOLD:
+       key = talloc_asprintf (ctx->format, "%s <%s>", name, addrfold);
+       break;
+    case FILTER_BY_NAME:
+       key = talloc_strdup (ctx->format, name); /* !name results in !key */
+       break;
+    case FILTER_BY_ADDR:
+       key = talloc_strdup (ctx->format, addr);
+       break;
+    case FILTER_BY_ADDRFOLD:
+       key = talloc_strdup (ctx->format, addrfold);
+       break;
+    default:
+       INTERNAL_ERROR("invalid --filter-by flags");
+    }
+
+    if (addrfold)
+       g_free (addrfold);
+
     if (! key)
        return FALSE;
 
@@ -727,10 +764,18 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
          (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
                                  { "false", NOTMUCH_EXCLUDE_FALSE },
                                  { 0, 0 } } },
+       { NOTMUCH_OPT_KEYWORD, &ctx->filter_by, "filter-by", 'b',
+         (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },
+                                 { "name", FILTER_BY_NAME },
+                                 { "addr", FILTER_BY_ADDR },
+                                 { "addrfold", FILTER_BY_ADDRFOLD },
+                                 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },
+                                 { 0, 0 } } },
        { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
+    ctx->filter_by = FILTER_BY_NAMEADDR,
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;