unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 32e5afcf18de03766e898f45721c3b25474e51ec 5047 bytes (raw)
name: notmuch-search-tags.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 
/* notmuch - Not much of an email program, (just index and search)
 *
 * Copyright © 2009 Carl Worth
 * Copyright © 2009 Jan Janak
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/ .
 *
 * Author: Jan Janak <jan@ryngle.com>
 */

#include "notmuch-client.h"

static void
print_tags (notmuch_tags_t *tags)
{
    const char *t;

    while ((t = notmuch_tags_get (tags))) {
	printf ("%s\n", t);
	notmuch_tags_advance (tags);
    }
}

int
notmuch_search_tags_command (void *ctx, int argc, char *argv[])
{
    notmuch_messages_t *msgs;
    notmuch_tags_t *tags;
    notmuch_config_t *config;
    notmuch_database_t *db;
    notmuch_query_t *query;
    char *query_str;

    tags = NULL;
    config = NULL;
    db = NULL;
    query = NULL;

    if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {
	goto error;
    }

    db = notmuch_database_open (notmuch_config_get_database_path (config),
				NOTMUCH_DATABASE_MODE_READ_ONLY);
    if (db == NULL) {
	goto error;
    }

    if (argc > 0) {
	if ((query_str = query_string_from_args (ctx, argc, argv)) == NULL) {
	    fprintf (stderr, "Out of memory.\n");
	    goto error;
	}

	if (*query_str == '\0') {
	    fprintf (stderr, "Error: Invalid search string.\n");
	    goto error;
	}

	if ((query = notmuch_query_create (db, query_str)) == NULL) {
	    fprintf (stderr, "Out of memory\n");
	    goto error;
	}


	msgs = notmuch_query_search_messages (query);
	if ((tags = notmuch_messages_collect_tags (msgs)) == NULL) goto error;
    } else {
	if ((tags = notmuch_database_get_all_tags (db)) == NULL) {
	    fprintf (stderr, "Error while getting tags from the database.\n");
	    goto error;
	}
    }

    print_tags (tags);

    notmuch_tags_destroy (tags);
    if (query) notmuch_query_destroy (query);
    notmuch_database_close (db);
    return 0;

error:
    if (tags) notmuch_tags_destroy (tags);
    if (query) notmuch_query_destroy (query);
    if (db) notmuch_database_close (db);
    return 1;
}

/* TODO: only take headers that match regexp? */
/* TODO: limit the max number of queries? */
void *
notmuch_messages_print_headers (notmuch_messages_t *messages, const char *header)
{
    notmuch_tags_t *tags, *msg_tags;
    notmuch_message_t *msg;
    GHashTable *htable;
    GList *keys, *l;
    const char *tag;
    const char *msg_header;


    htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);

    while ((msg = notmuch_messages_get (messages))) {
      msg_header = notmuch_message_get_header (msg, header);
      g_hash_table_insert (htable, xstrdup (msg_header), NULL);
      notmuch_message_destroy (msg);
      notmuch_messages_advance (messages);
    }

    keys = g_hash_table_get_keys (htable);

    /* TODO: sort by alphabetical order? */
    for (l = keys; l; l = l->next) {
      printf("%s\n", (char *)l->data);
    }

    g_list_free (keys);
    g_hash_table_destroy (htable);
}

int
notmuch_search_headers_command (void *ctx, int argc, char *argv[])
{
    notmuch_messages_t *msgs;
    notmuch_tags_t *tags;
    notmuch_config_t *config;
    notmuch_database_t *db;
    notmuch_query_t *query;
    char *query_str;

    tags = NULL;
    config = NULL;
    db = NULL;
    query = NULL;

    if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {
	goto error;
    }

    db = notmuch_database_open (notmuch_config_get_database_path (config),
				NOTMUCH_DATABASE_MODE_READ_ONLY);
    if (db == NULL) {
	goto error;
    }

    if (argc < 3) {
         fprintf( stderr, "Wrong number of arguments\n");
         fprintf( stderr, "Arguments: <header> <regexp> <query>\n");
	 goto error;
    }

    const char *header = argv[0];
    const char *regexp = argv[1]; // Ignored for now.

    if(( query_str = query_string_from_args (ctx, argc-2, &argv[2])) == NULL) {
      fprintf (stderr, "Out of memory.\n");
      goto error;
    }

    if (*query_str == '\0') {
      fprintf (stderr, "Error: Invalid search string.\n");
      goto error;
    }

    if ((query = notmuch_query_create (db, query_str)) == NULL) {
      fprintf (stderr, "Out of memory\n");
      goto error;
    }

    msgs = notmuch_query_search_messages (query);
    if ((notmuch_messages_print_headers (msgs, header)) == NULL) goto error;

    notmuch_query_destroy (query);
    notmuch_database_close (db);
    return 0;

error:
    if (tags) notmuch_tags_destroy (tags);
    if (query) notmuch_query_destroy (query);
    if (db) notmuch_database_close (db);
    return 1;
}

debug log:

solving 32e5afc ...
found 32e5afc in https://yhetil.org/notmuch/1261649236.4b333d54a22d3@imp.free.fr/
found 7a1305e in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 7a1305e2fed98368ae42995a1bf3f28fcaae997e	notmuch-search-tags.c

applying [1/1] https://yhetil.org/notmuch/1261649236.4b333d54a22d3@imp.free.fr/
diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c
index 7a1305e..32e5afc 100644

Checking patch notmuch-search-tags.c...
Applied patch notmuch-search-tags.c cleanly.

index at:
100644 32e5afcf18de03766e898f45721c3b25474e51ec	notmuch-search-tags.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).