pax_global_header00006660000000000000000000000064143220240610014504gustar00rootroot0000000000000052 comment=569becc95f1252b1b30d373b391459154ba5cffe notmuch-addrlookup-c-10/000077500000000000000000000000001432202406100153255ustar00rootroot00000000000000notmuch-addrlookup-c-10/CHANGELOG.md000066400000000000000000000063021432202406100171370ustar00rootroot00000000000000# Change Log All notable changes to this project will be documented in this file. ## [Unreleased] ## [10] - 2022-10-13 ### Added - A new `-c`/`--config` option allows specifying the Notmuch configuration file path. (Patch by Lars Haalck <[lars.haalck@uni-muenster.de](lars.haalck@uni-muenster.de)>, and follow up fix by Bence Ferdinandy <[bence@ferdinandy.com](bence@ferdinandy.com)>). - A new `-f`/`--format` option supersedes `--mutt`, and also allows using `--format=aerc` to be used with the [aerc](https://aerc-mail.org/) user agent. (Patch by Bence Ferdinandy <[bence@ferdinandy.com](bence@ferdinandy.com)>). ### Fixed - Fix more uses of deprecated Notmuch functions. ## [9] - 2017-10-20 ### Fixed - Fix crash in when running queries and the program is build with Notmuch 0.25. (Patch by Joshua Krusell <[joshua.krusell@v-dem.net](joshua.krusell@v-dem.net)>). - Fix conditional compilation when using Notmuch 0.25. (Patch by David Bremner <[bremner@debian.org](bremner@debian.org)>). ### Changed - Do not use the deprecated `notmuch_query_count_messages_st` function when using Notmuch 0.25. (Patch by Adam Ruzicka <[a.ruzicka@outlook.com](a.ruzicka@outlook.com)>.) ## [8] - 2017-02-07 ### Fixed - Avoid a segmentation fault when `notmuch_message_get_header()` returns `NULL`. (Patch by Víctor M. Jáquez <[vjaquez@igalia.com](mailto:vjaquez@igalia.com)>). ## [7] - 2016-01-09 ### Fixed - Do not use functions deprecated starting with `libnotmuch` 4.3; building against older versions is still possible. ## [6] - 2015-11-06 ### Added - This change log file. ### Fixed - Fix sorting of results. (Patch by Jeremy Simon <[jtsymon@gmail.com](mailto:jtsymon@gmail.com)>.) ## [5] - 2015-06-05 ### Fixed - Normal output was missing angle brackets around e-mail addresses. This issue is fixed in this release. ## [4] - 2015-05-21 ### Fixed - Allow building with versions of Glib older than 2.44. (Patch by `cellscape` <[me@nooff.info](mailto:me@nooff.info)>.) ## [3] - 2015-05-19 ### Added - Support output in a Mutt-compatible format. This allows using `notmuch-addrlookup --mutt` directly in for Mutt's `query_command` setting. ### Changed - Parsing of command line arguments is now done using Glib. ## [2] - 2015-05-12 ### Fixed - Reverse sorting of results, so most frequently contacted addresses are sorted first. (Patch by Leonhard Markert.) ## [1] - 2015-01-05 ### Added - First working version of `notmuch-addrlookup`. [Unreleased]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v9...HEAD [10]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v9...v10 [9]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v8...v9 [8]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v7...v8 [7]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v6...v7 [6]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v5...v6 [5]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v4...v5 [4]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v3...v4 [3]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v2...v3 [2]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/v1...v2 [1]: https://github.com/aperezdc/notmuch-addrlookup-c/compare/a9364d4...v1 notmuch-addrlookup-c-10/Makefile000066400000000000000000000011331432202406100167630ustar00rootroot00000000000000# # Makefile # Adrian Perez, 2014-01-06 12:23 # GLIB_CFLAGS := $(shell pkg-config glib-2.0 --cflags) GLIB_LDLIBS := $(shell pkg-config glib-2.0 --libs) OPTFLAGS ?= -O2 LDLIBS += -lnotmuch $(GLIB_LDLIBS) CFLAGS = -Wall $(OPTFLAGS) $(GLIB_CFLAGS) -std=c99 all: notmuch-addrlookup dist: @ P=notmuch-addrlookup-$$(git describe --tag --exact-match | sed -e 's/^v//') ; \ git archive --prefix="$$P/" -o "$$P.tar.gz" @ ; \ echo "$$P.tar.gz" notmuch-addrlookup: notmuch-addrlookup.o clean: $(RM) notmuch-addrlookup notmuch-addrlookup.o print-cflags: @echo "$(CFLAGS)" .PHONY: print-cflags dist notmuch-addrlookup-c-10/README.md000066400000000000000000000114261432202406100166100ustar00rootroot00000000000000# Notmuch Address Lookup tool — in C This is an address lookup tool similar to the `addrlookup` one found in the (apparently unmaintained) [Vala Notmuch binding](https://github.com/spaetz/vala-notmuch), using the [Notmuch](http://notmuchmail.org) database as source. The one and only feature is accepting a string as first argument, and it will use the Notmuch database to output the names and e-mail addresses of the people you have exchanged e-mails with, most used ones first. This works neatly as [address completer for the Notmuch Emacs interface](http://notmuchmail.org/emacstips/#index15h2). One more thing: I plan to keep the tool up-to-date when new Notmuch releases change the API of the library. Precisely the reasons why this small project was started are: * The [Go](http://golang.org) implementation included with Notmuch insists in outputting all the text in lowercase. Plus, requires a working Go development setup to build it. - There used to be similar tools included with Notmuch, written in Vala and Python, but they have been removed due to lack of maintenance. ## `notmuch address` Starting with version 0.19 a new `notmuch address` subcommand is available, which can be used to search for e-mail addresses as well. While similar, `notmuch-addrlookup-c` may still be preferred because: - The output format can be set to Mutt-compatible, which many existing tools know how to handle. - The output can also be set to aerc compatible. - It automatically searches for addresses in the `To:`, `Cc:`, `Bcc:`, and `From:` message headers, and sorts them out by most frequently seen. This usually renders more relevant results. ## Building Make sure you have the following installed, plus their development packages: * `libnotmuch` — Included with Notmuch. * GLib — It is a dependency of Notmuch as well. * `pkg-config` * GNU Make and a working C compiler. Then just build and install it: ```sh make sudo cp notmuch-addrlookup /usr/local/bin ``` ## Using You can check that the program works by invoking from a shell. The only argument it takes is a string, which is searched for in the e-mail addresses contained in the headers of the messages indexed by Notmuch. Example output (names and addresses are not real): ``` % notmuch-addrlookup peter Peter Jones John Peters Peter Tosh Noren Petersen % ``` ### Vim The plug-in [vim-notmuch-addrlookup](https://github.com/Konfekt/vim-notmuch-addrlookup) adds completion of e-mail addresses via `notmuch-addrlookup-c` (by default by hitting ``) when composing an e-mail. ### Emacs UI The Emacs Notmuch UI [can be configured](http://notmuchmail.org/emacstips/#index13h2) to use the tool for completing addresses when composing. ### Mutt The [Mutt UA](http://www.mutt.org/) can be configured to use `notmuch-addrlookup` by setting the following options: ``` set query_command="notmuch-addrlookup --format=mutt '%s'" ``` ### aerc The [aerc](https://aerc-mail.org/) can be configured to use `notmuch-addrlookup` by setting the following options: ``` address-book-cmd=notmuch-addrlookup --format=aerc %s ``` ### alot The [alot](https://github.com/pazz/alot) MUA can be configured to use `notmuch-addrlookup`, using an `[accounts]` section in the configuration file similar to this: ``` [accounts] [[youraccountname]] realname = Your Name address = your@address.com ... [[[abook]]] type = shellcommand regexp = '^(?P[^@]+@[^\t]+)\t+(?P[^\t]+)' command = notmuch-addrlookup --format=mutt ignorecase = True ``` ## License This software is distributed under the terms of the [MIT license](http://opensource.org/licenses/MIT): ``` Copyright (c) 2014 Adrian Perez de Castro Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` notmuch-addrlookup-c-10/notmuch-addrlookup.c000066400000000000000000000343171432202406100213200ustar00rootroot00000000000000/* * notmuch-addrlookup.c * Copyright (C) 2014-2016 Adrian Perez * * Distributed under terms of the MIT license. */ #include #include #include #include #include #define NUM_QUERIES 3 static gchar* notmuch_user_abook_tag = NULL; static gchar* notmuch_database_path = NULL; static gchar* notmuch_user_email = NULL; static gchar** search_terms = NULL; static gboolean mutt_output = FALSE; static gchar* notmuch_config_path_lookupvar = NULL; static gchar* output_format_name = "default"; static const GOptionEntry option_entries[] = { { "format", 'f', 0, G_OPTION_ARG_STRING, &output_format_name, "Set format output (values: default, mutt, aerc)", NULL }, { "mutt", 'm', 0, G_OPTION_ARG_NONE, &mutt_output, "Format output for Mutt (deprecated, use --format instead)", NULL }, { "config", 'c', 0, G_OPTION_ARG_STRING, ¬much_config_path_lookupvar, "Path to config file .notmuch-config", NULL }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &search_terms, "Search terms", NULL }, { NULL } }; typedef struct { gchar *mail; gchar *name; guint occurrences[NUM_QUERIES]; } ContactInfo; /* * Note: Takes ownership of the passed strings. */ static ContactInfo* contact_info_new (gchar *mail, gchar* name) { ContactInfo* info = g_slice_new0 (ContactInfo); info->mail = mail; info->name = name; return info; } static void contact_info_free (ContactInfo *info) { if (info == NULL) return; g_free (info->name); g_free (info->mail); g_slice_free (ContactInfo, info); } static int sort_by_frequency (gconstpointer data1, gconstpointer data2) { const ContactInfo *info1 = (ContactInfo*) data1; const ContactInfo *info2 = (ContactInfo*) data2; if (info1->occurrences[0] == info2->occurrences[0] && info1->occurrences[1] == info2->occurrences[1] && info1->occurrences[2] == info2->occurrences[2]) return 0; if ((info1->occurrences[0] < info2->occurrences[0]) || (info1->occurrences[0] == info2->occurrences[0] && info1->occurrences[1] < info2->occurrences[1]) || (info1->occurrences[0] == info2->occurrences[0] && info1->occurrences[1] == info2->occurrences[1] && info1->occurrences[2] < info2->occurrences[2])) return 1; return -1; } static gboolean load_notmuch_settings (void) { GKeyFile *key_file = g_key_file_new (); gchar *config_path = NULL; gboolean success = FALSE; if (g_getenv ("NOTMUCH_CONFIG")) config_path = g_strdup (g_getenv ("NOTMUCH_CONFIG")); else if(!notmuch_config_path_lookupvar) config_path = g_strdup_printf ("%s/.notmuch-config", g_get_home_dir ()); else config_path = notmuch_config_path_lookupvar; if (!g_key_file_load_from_file (key_file, config_path, G_KEY_FILE_NONE, NULL)) goto bailout; notmuch_database_path = g_key_file_get_string (key_file, "database", "path", NULL); notmuch_user_email = g_key_file_get_string (key_file, "user", "primary_email", NULL); notmuch_user_abook_tag = g_key_file_get_string (key_file, "user", "addrbook_tag", NULL); if (!notmuch_user_abook_tag) notmuch_user_abook_tag = g_strdup ("addressbook"); success = (notmuch_database_path != NULL); bailout: g_key_file_free (key_file); g_free (config_path); return success; } static void global_cleanup (void) { g_strfreev (search_terms); g_free (notmuch_user_abook_tag); g_free (notmuch_database_path); g_free (notmuch_user_email); } static void create_queries (notmuch_database_t *db, notmuch_query_t *queries[NUM_QUERIES], const gchar *name) { /* Pass 1: looks at all the "from:" addresses with the address book tag */ GString *qs = g_string_new ("tag:"); g_string_append (qs, notmuch_user_abook_tag); if (name) g_string_append_printf (qs, " and from:%s*", name); gchar *sbuf = g_string_free (qs, FALSE); queries[0] = notmuch_query_create (db, sbuf); g_free (sbuf); /* Pass 2: looks at all "to:" addresses sent from the primary e-mail */ qs = g_string_new (""); if (name) g_string_append_printf (qs, "to:%s*", name); if (notmuch_user_email) g_string_append_printf (qs, " from:%s", notmuch_user_email); sbuf = g_string_free (qs, FALSE); queries[1] = notmuch_query_create (db, sbuf); g_free (sbuf); #if LIBNOTMUCH_CHECK_VERSION(5, 0, 0) unsigned int count = 0; unsigned int tmp; if (notmuch_query_count_messages (queries[0], &tmp) == NOTMUCH_STATUS_SUCCESS) count += tmp; if (notmuch_query_count_messages (queries[1], &tmp) == NOTMUCH_STATUS_SUCCESS) count += tmp; #elif LIBNOTMUCH_CHECK_VERSION(4, 3, 0) unsigned int count = 0; unsigned int tmp; if (notmuch_query_count_messages_st (queries[0], &tmp) == NOTMUCH_STATUS_SUCCESS) count += tmp; if (notmuch_query_count_messages_st (queries[1], &tmp) == NOTMUCH_STATUS_SUCCESS) count += tmp; #else unsigned int count = notmuch_query_count_messages (queries[0]) + notmuch_query_count_messages (queries[1]); #endif /* Pass 3: With only a few hits, add all the "from:" addresses */ if (count < 10) { qs = g_string_new (""); if (name) g_string_append_printf (qs, "from:%s*", name); sbuf = g_string_free (qs, FALSE); queries[2] = notmuch_query_create (db, sbuf); g_free (sbuf); } } static void print_contact_info (gpointer data, gpointer userdata) { ContactInfo *info = (ContactInfo*) data; g_print ("%s <%s>\n", info->name, info->mail); } static void print_contact_info_mutt (gpointer data, gpointer userdata) { ContactInfo *info = (ContactInfo*) data; g_print ("%s\t%s\n", info->mail, info->name); } static void print_contact_info_aerc (gpointer data, gpointer userdata) { static GRegex *replace_re = NULL; if (!replace_re) replace_re = g_regex_new ("\\\"?([^\\\"]+)\\\"?", G_REGEX_OPTIMIZE, 0, NULL); ContactInfo *info = (ContactInfo*) data; g_print ("%s\t%s\n", info->mail, g_regex_replace( replace_re, info->name, -1, 0, "\\1", G_REGEX_MATCH_ANCHORED, NULL) ); } static void print_summary_mutt (gpointer addrs) { g_print ("Searching database ... %u matching entries:\n", g_list_length (addrs)); } struct PrintFormat { const char *name; void (*print_item)(gpointer, gpointer); void (*print_header)(gpointer addrs); }; static const struct PrintFormat s_formats[] = { { .name = "default", .print_item = print_contact_info }, { .name = "aerc", .print_item = print_contact_info_aerc }, { .name = "mutt", .print_item = print_contact_info_mutt, .print_header = print_summary_mutt }, }; static void run_queries (notmuch_database_t *db, notmuch_query_t *queries[NUM_QUERIES], const gchar *query_name, const struct PrintFormat *output_format) { static const gchar *headers_pass0[] = { "from", NULL }; static const gchar *headers_pass1[] = { "to", "cc", "bcc", NULL }; GHashTable *frequencies = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) contact_info_free); GRegex *match_re = g_regex_new ("\\s*((?P(\\\"(\\\\.|[^\\\\\"])*\\\"|[^,<])*)" "\\b\\w+([-+.]\\w+)*\\@\\w+[-\\.\\w]*\\.([-\\.\\w]+)*\\w\\b)>?)", G_REGEX_OPTIMIZE, 0, NULL); for (guint i = 0; i < NUM_QUERIES; i++) { if (!queries[i]) continue; const gchar **headers = (i == 1) ? headers_pass1 : headers_pass0; notmuch_messages_t *messages = NULL; #if LIBNOTMUCH_CHECK_VERSION(5, 0, 0) if (notmuch_query_search_messages (queries[i], &messages) != NOTMUCH_STATUS_SUCCESS) continue; #elif LIBNOTMUCH_CHECK_VERSION(4, 3, 0) if (notmuch_query_search_messages_st (queries[i], &messages) != NOTMUCH_STATUS_SUCCESS) continue; #else if (!(messages = notmuch_query_search_messages (queries[i]))) continue; #endif for (; notmuch_messages_valid (messages); notmuch_messages_move_to_next (messages)) { notmuch_message_t *msg = notmuch_messages_get (messages); for (guint j = 0; headers[j] != NULL; j++) { const gchar* froms = notmuch_message_get_header (msg, headers[j]); if (!froms || *froms == '\0') continue; GMatchInfo *matches; g_regex_match (match_re, froms, 0, &matches); while (g_match_info_matches (matches)) { gchar *from = g_match_info_fetch (matches, 1); gboolean has_space = strchr (from, ' ') != NULL; gchar *name = has_space ? g_match_info_fetch_named (matches, "name") : g_strdup (from); gchar *addr = has_space ? g_match_info_fetch_named (matches, "mail") : g_strdup (from); g_strstrip(name); /* Name should be 'Bob', not 'Bob ' */ gchar *patt = g_strdup_printf ("\\b%s", query_name); if (g_regex_match_simple (patt, from, G_REGEX_CASELESS, 0)) { gchar* addr_fold = g_utf8_casefold (addr, -1); gchar* addr_key = g_utf8_collate_key (addr_fold, -1); g_free (addr_fold); ContactInfo *info = (ContactInfo*) g_hash_table_lookup (frequencies, addr_key); if (info == NULL) { info = contact_info_new (addr, name); g_hash_table_insert (frequencies, addr_key, info); /* * The ContactInfo takes the "from" string, and * the hash table takes ownership of the key. Set * them NULL to avoid the g_free() calls below to * free the memory. */ addr_key = from = NULL; } info->occurrences[i]++; g_free (addr_key); } g_free (patt); g_free (from); g_match_info_next (matches, NULL); } } notmuch_message_destroy (msg); } notmuch_query_destroy (queries[i]); } g_regex_unref (match_re); GList *addrs = g_hash_table_get_values (frequencies); addrs = g_list_sort (addrs, sort_by_frequency); if (output_format->print_header) (*output_format->print_header) (addrs); g_list_foreach (addrs, output_format->print_item, NULL); g_list_free (addrs); g_hash_table_destroy (frequencies); } int main (int argc, char **argv) { setlocale(LC_CTYPE, ""); atexit (global_cleanup); GOptionContext *option_context = g_option_context_new ("search-string"); #if GLIB_CHECK_VERSION(2, 44, 0) g_option_context_set_strict_posix (option_context, TRUE); #endif g_option_context_add_main_entries (option_context, option_entries, "notmuch-addrlookup-c"); GError *error = NULL; if (!g_option_context_parse (option_context, &argc, &argv, &error)) { g_printerr ("%s: %s.\n", g_get_prgname (), error->message); g_option_context_free (option_context); g_error_free (error); return EXIT_FAILURE; } g_option_context_free (option_context); if (mutt_output) { g_warning ("Option -m/--mutt is deprecated, use --format=mutt instead."); output_format_name = "mutt"; } const struct PrintFormat *output_format = NULL; for (unsigned i = 0; i < G_N_ELEMENTS(s_formats); i++) { if (strcmp(s_formats[i].name, output_format_name) == 0) { output_format = &s_formats[i]; break; } } if (!output_format) { g_printerr ("%s: %s is an invalid format name.\n", g_get_prgname (),output_format_name); return EXIT_FAILURE; } if (!search_terms || g_strv_length (search_terms) == 0) { g_printerr ("%s: No search terms provided.\n", g_get_prgname ()); return EXIT_FAILURE; } if (!load_notmuch_settings ()) { g_printerr ("Could not load Notmuch settings (~/.notmuch-config missing?)\n"); return EXIT_FAILURE; } notmuch_database_t *db; g_autofree char *error_msg = NULL; #if LIBNOTMUCH_CHECK_VERSION(5, 4, 0) notmuch_status_t status = notmuch_database_open_with_config (notmuch_database_path, NOTMUCH_DATABASE_MODE_READ_ONLY, NULL, NULL, &db, &error_msg); #else notmuch_status_t status = notmuch_database_open (notmuch_database_path, NOTMUCH_DATABASE_MODE_READ_ONLY, &db); #endif if (status != NOTMUCH_STATUS_SUCCESS) { g_printerr ("Could not open Notmuch database: %s (%s)\n", notmuch_status_to_string (status), error_msg ? error_msg : "no error message"); return EXIT_FAILURE; } notmuch_query_t *queries[NUM_QUERIES] = { NULL, NULL, NULL }; gchar *name = g_strjoinv (" ", search_terms); create_queries (db, queries, name); run_queries (db, queries, name, output_format); g_free (name); notmuch_database_close (db); return EXIT_SUCCESS; }