]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
compat: Add implementation of getline from gnulib.
[notmuch] / notmuch-search.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 static void
24 do_search_threads (const void *ctx,
25                    notmuch_query_t *query,
26                    notmuch_sort_t sort)
27 {
28     notmuch_thread_t *thread;
29     notmuch_threads_t *threads;
30     notmuch_tags_t *tags;
31     time_t date;
32     const char *relative_date;
33
34     for (threads = notmuch_query_search_threads (query);
35          notmuch_threads_has_more (threads);
36          notmuch_threads_advance (threads))
37     {
38         int first_tag = 1;
39
40         thread = notmuch_threads_get (threads);
41
42         if (sort == NOTMUCH_SORT_OLDEST_FIRST)
43             date = notmuch_thread_get_oldest_date (thread);
44         else
45             date = notmuch_thread_get_newest_date (thread);
46
47         relative_date = notmuch_time_relative_date (ctx, date);
48
49         printf ("thread:%s %12s [%d/%d] %s; %s",
50                 notmuch_thread_get_thread_id (thread),
51                 relative_date,
52                 notmuch_thread_get_matched_messages (thread),
53                 notmuch_thread_get_total_messages (thread),
54                 notmuch_thread_get_authors (thread),
55                 notmuch_thread_get_subject (thread));
56
57         printf (" (");
58         for (tags = notmuch_thread_get_tags (thread);
59              notmuch_tags_has_more (tags);
60              notmuch_tags_advance (tags))
61         {
62             if (! first_tag)
63                 printf (" ");
64             printf ("%s", notmuch_tags_get (tags));
65             first_tag = 0;
66         }
67         printf (")\n");
68
69         notmuch_thread_destroy (thread);
70     }
71 }
72
73 int
74 notmuch_search_command (void *ctx, int argc, char *argv[])
75 {
76     notmuch_config_t *config;
77     notmuch_database_t *notmuch;
78     notmuch_query_t *query;
79     char *query_str;
80     char *opt;
81     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
82     int i;
83
84     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
85         if (strcmp (argv[i], "--") == 0) {
86             i++;
87             break;
88         }
89         if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
90             opt = argv[i] + sizeof ("--sort=") - 1;
91             if (strcmp (opt, "oldest-first") == 0) {
92                 sort = NOTMUCH_SORT_OLDEST_FIRST;
93             } else if (strcmp (opt, "newest-first") == 0) {
94                 sort = NOTMUCH_SORT_NEWEST_FIRST;
95             } else {
96                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
97                 return 1;
98             }
99         } else {
100             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
101             return 1;
102         }
103     }
104
105     argc -= i;
106     argv += i;
107
108     config = notmuch_config_open (ctx, NULL, NULL);
109     if (config == NULL)
110         return 1;
111
112     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
113                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
114     if (notmuch == NULL)
115         return 1;
116
117     query_str = query_string_from_args (ctx, argc, argv);
118     if (query_str == NULL) {
119         fprintf (stderr, "Out of memory.\n");
120         return 1;
121     }
122     if (*query_str == '\0') {
123         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
124         return 1;
125     }
126
127     query = notmuch_query_create (notmuch, query_str);
128     if (query == NULL) {
129         fprintf (stderr, "Out of memory\n");
130         return 1;
131     }
132
133     notmuch_query_set_sort (query, sort);
134
135     do_search_threads (ctx, query, sort);
136
137     notmuch_query_destroy (query);
138     notmuch_database_close (notmuch);
139
140     return 0;
141 }