-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution-notmuch-server.c
More file actions
196 lines (160 loc) · 4.98 KB
/
Copy pathevolution-notmuch-server.c
File metadata and controls
196 lines (160 loc) · 4.98 KB
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
195
196
#include <glib.h>
#include <gio/gio.h>
#include <notmuch.h>
typedef void (*crawler_callback_t) (notmuch_database_t*, GFile*);
static void index_subfolders_cb (notmuch_database_t*, GFile*);
static void
folder_crawler (notmuch_database_t *db,
GFile *basedir,
crawler_callback_t cb)
{
GError *error = NULL;
GFileEnumerator *erator;
GFileInfo *info;
erator = g_file_enumerate_children (basedir,
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
NULL,
&error);
if (error)
{
g_error (error->message);
g_error_free (error);
return;
}
info = g_file_enumerator_next_file (erator, NULL, &error);
while (info)
{
const gchar *name = g_file_info_get_name (info);
GFileType type = g_file_info_get_file_type (info);
GFile *item = g_file_get_child (basedir, name);
g_message (name);
cb (db, item);
g_object_unref (item);
g_object_unref (info);
info = g_file_enumerator_next_file (erator, NULL, &error);
}
g_object_unref (erator);
}
static void
index_email_cb (notmuch_database_t *db, GFile *mailfile)
{
notmuch_status_t status;
gchar *path = g_file_get_path (mailfile);
//g_message ("Indexing %s", g_file_get_path (mailfile));
status = notmuch_database_add_message (db, path, NULL);
if (status != NOTMUCH_STATUS_SUCCESS &&
status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
g_warning ("Could not index %s: %d", path, status);
g_free (path);
}
static void
index_container_folder_cb (notmuch_database_t *db, GFile *folder)
{
folder_crawler (db, folder, index_email_cb);
}
static void
index_subfolders_cb (notmuch_database_t *db, GFile *dir)
{
int i = 0;
char *stdfolders[] = {"cur", "tmp", "new", "subfolders", NULL};
while (stdfolders[i])
{
GFile *folder = g_file_get_child (dir, stdfolders[i]);
if (g_file_query_exists (folder, NULL))
{
if (g_str_equal (stdfolders[i], "subfolders"))
{
folder_crawler (db, folder, index_subfolders_cb);
}
else
{
folder_crawler (db, folder, index_container_folder_cb);
}
}
g_object_unref (folder);
i++;
}
}
static void
scan_directory (notmuch_database_t *db, GFile *db_folder)
{
GFile *folders;
folders = g_file_get_child (db_folder, "folders");
if (!g_file_query_exists (folders, NULL))
{
gchar *path = g_file_get_path (folders);
g_error ("%s does not exists", path);
g_free (path);
}
else
{
folder_crawler (db, folders, index_subfolders_cb);
}
g_object_unref (folders);
}
int main (int argc, char** argv) {
GFile *db_dir, *db_file;
notmuch_status_t status;
notmuch_database_t *db = NULL;
notmuch_query_t *query = NULL;
notmuch_messages_t *messages = NULL;
notmuch_message_t *message = NULL;
GMainLoop *loop = NULL;
const char *query_string = "date:2014-02-01..";
if (argc != 2)
{
g_warning ("Usage: %s EVOLUTION_MAILDIR", argv[0]);
return 1;
}
db_dir = g_file_new_for_path (argv[1]);
db_file = g_file_get_child (db_dir, ".notmuch");
if (!g_file_query_exists (db_dir, NULL))
{
g_object_unref (db_dir);
g_object_unref (db_file);
g_error ("directory %s does not exists");
return 2;
}
if (!g_file_query_exists (db_file, NULL))
status = notmuch_database_create (argv[1], &db);
else
status = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);
if (status)
{
g_error ("Could not open database: %d", status);
g_object_unref (db_dir);
g_object_unref (db_file);
notmuch_database_destroy (db);
return 3;
}
scan_directory (db, db_dir);
//loop = g_main_loop_new (NULL, FALSE);
//g_main_loop_run (loop);
query = notmuch_query_create (db, query_string);
if (!query) {
g_error ("Could not create query from string = \"%s\"", query_string);
notmuch_database_destroy (db);
g_object_unref (db_file);
g_object_unref (db_dir);
return 4;
}
g_message ("Query results -\n\n");
for (messages = notmuch_query_search_messages (query);
notmuch_messages_valid (messages);
notmuch_messages_move_to_next (messages)) {
message = notmuch_messages_get (messages);
g_message ("Message file: %s", notmuch_message_get_filename (message));
g_message ("Message ID: %s", notmuch_message_get_message_id (message));
g_message ("Message Sender: %s", notmuch_message_get_header (message, "from"));
g_message ("Message Recipients: %s", notmuch_message_get_header (message, "to"));
g_message ("Message Subject: %s", notmuch_message_get_header (message, "subject"));
g_message ("Message date: %s\n", notmuch_message_get_header (message, "date"));
notmuch_message_destroy (message);
}
notmuch_query_destroy (query);
notmuch_database_destroy (db);
g_object_unref (db_file);
g_object_unref (db_dir);
return 0;
}