]> git.notmuchmail.org Git - notmuch/blob - notmuch-index-message.cc
cf99e6e43d7762e25fa812f307ab3cf65d452827
[notmuch] / notmuch-index-message.cc
1 /*
2  * Copyright © 2009 Carl Worth
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see http://www.gnu.org/licenses/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <time.h>
25
26 #include <gmime/gmime.h>
27
28 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
29
30 /* These prefix values are specifically chosen to be compatible
31  * with sup, (http://sup.rubyforge.org), written by
32  * William Morgan <wmorgan-sup@masanjin.net>, and released
33  * under the GNU GPL v2.
34  */
35
36 typedef struct {
37     const char *name;
38     const char *prefix;
39 } prefix_t;
40
41 prefix_t NORMAL_PREFIX[] = {
42     { "subject", "S" },
43     { "body", "B" },
44     { "from_name", "FN" },
45     { "to_name", "TN" },
46     { "name", "N" },
47     { "attachment", "A" }
48 };
49
50 prefix_t BOOLEAN_PREFIX[] = {
51     { "type", "K" },
52     { "from_email", "FE" },
53     { "to_email", "TE" },
54     { "email", "E" },
55     { "date", "D" },
56     { "label", "L" },
57     { "source_id", "I" },
58     { "attachment_extension", "O" },
59     { "msgid", "Q" },
60     { "thread", "H" },
61     { "ref", "R" }
62 };
63
64 static const char *
65 find_prefix (const char *name)
66 {
67     unsigned int i;
68
69     for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
70         if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
71             return NORMAL_PREFIX[i].prefix;
72
73     for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
74         if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
75             return BOOLEAN_PREFIX[i].prefix;
76
77     return "";
78 }
79
80 int TERM_COMBINED = 0;
81
82 static void
83 print_term (const char *prefix_name, const char *value)
84 {
85     const char *prefix;
86
87     if (value == NULL)
88         return;
89
90     prefix = find_prefix (prefix_name);
91
92     if (TERM_COMBINED)
93         printf ("\"%s%s\", ", prefix, value);
94     else
95         printf ("[\"%s\", \"%s\"], ", value, prefix);
96 }
97
98 static void
99 add_address_name (InternetAddress *address, const char *prefix_name)
100 {
101     const char *name;
102
103     name = internet_address_get_name (address);
104
105     if (name)
106         print_term (prefix_name, name);
107 }
108
109 static void
110 add_address_names (InternetAddressList *addresses, const char *address_type)
111 {
112     int i;
113     InternetAddress *address;
114
115     for (i = 0; i < internet_address_list_length (addresses); i++) {
116         address = internet_address_list_get_address (addresses, i);
117         add_address_name (address, address_type);
118         add_address_name (address, "name");
119         add_address_name (address, "body");
120     }
121 }
122
123 static void
124 add_address_addr (InternetAddress *address, const char *prefix_name)
125 {
126     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
127     const char *addr;
128
129     addr = internet_address_mailbox_get_addr (mailbox);
130
131     if (addr)
132         print_term (prefix_name, addr);
133 }
134
135 static void
136 add_address_addrs (InternetAddressList *addresses, const char *address_type)
137 {
138     int i;
139     InternetAddress *address;
140
141     for (i = 0; i < internet_address_list_length (addresses); i++) {
142         address = internet_address_list_get_address (addresses, i);
143         add_address_addr (address, address_type);
144         add_address_addr (address, "email");
145     }
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151     GMimeStream *stream;
152     GMimeParser *parser;
153     GMimeMessage *message;
154     InternetAddressList *addresses;
155
156     const char *filename;
157     FILE *file;
158
159     const char *value, *from;
160
161     time_t time;
162     struct tm gm_time_tm;
163     char time_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */
164
165     if (argc < 2) {
166         fprintf (stderr, "Usage: %s <mail-message>\n",
167                  argv[0]);
168         exit (1);
169     }
170
171     filename = argv[1];
172
173     file = fopen (filename, "r");
174     if (! file) {
175         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
176         exit (1);
177     }
178
179     g_mime_init (0);
180
181     stream = g_mime_stream_file_new (file);
182
183     parser = g_mime_parser_new_with_stream (stream);
184
185     message = g_mime_parser_construct_message (parser);
186
187     printf ("text is:\n[");
188     from = g_mime_message_get_sender (message);
189     addresses = internet_address_list_parse_string (from);
190
191     add_address_names (addresses, "from_name");
192
193     add_address_names (g_mime_message_get_all_recipients (message),
194                        "to_name");
195
196     value = g_mime_message_get_subject (message);
197     print_term ("subject", value);
198     print_term ("body", value);
199
200     printf ("]\nterms is:\n[");
201
202     TERM_COMBINED = 1;
203
204     from = g_mime_message_get_sender (message);
205     addresses = internet_address_list_parse_string (from);
206
207     add_address_addrs (addresses, "from_email");
208
209     add_address_addrs (g_mime_message_get_all_recipients (message),
210                        "to_email");
211
212     g_mime_message_get_date (message, &time, NULL);
213
214     gmtime_r (&time, &gm_time_tm);
215
216     if (strftime (time_str, sizeof (time_str),
217                   "%Y%m%d%H%M%S", &gm_time_tm) == 0) {
218         fprintf (stderr, "Internal error formatting time\n");
219         exit (1);
220     }
221
222     print_term ("date", time_str);
223
224     print_term ("label", "inbox");
225     print_term ("label", "unread");
226     print_term ("type", "mail");
227
228     value = g_mime_message_get_message_id (message);
229     print_term ("msgid", value);
230
231     print_term ("source_id", "1");
232
233     value = g_mime_message_get_message_id (message);
234     print_term ("thread", value);
235
236     printf ("]\n");
237
238     g_object_unref (message);
239     g_object_unref (parser);
240     g_object_unref (stream);
241
242     return 0;
243 }