2 * Copyright © 2009 Carl Worth
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.
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.
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/ .
17 * Author: Carl Worth <cworth@cworth.org>
26 #include <gmime/gmime.h>
28 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
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.
41 prefix_t NORMAL_PREFIX[] = {
44 { "from_name", "FN" },
50 prefix_t BOOLEAN_PREFIX[] = {
52 { "from_email", "FE" },
58 { "attachment_extension", "O" },
65 find_prefix (const char *name)
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;
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;
80 int TERM_COMBINED = 0;
83 print_term (const char *prefix_name, const char *value)
90 prefix = find_prefix (prefix_name);
93 printf ("\"%s%s\", ", prefix, value);
95 printf ("[\"%s\", \"%s\"], ", value, prefix);
99 add_address_name (InternetAddress *address, const char *prefix_name)
103 name = internet_address_get_name (address);
106 print_term (prefix_name, name);
110 add_address_names (InternetAddressList *addresses, const char *address_type)
113 InternetAddress *address;
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");
124 add_address_addr (InternetAddress *address, const char *prefix_name)
126 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
129 addr = internet_address_mailbox_get_addr (mailbox);
132 print_term (prefix_name, addr);
136 add_address_addrs (InternetAddressList *addresses, const char *address_type)
139 InternetAddress *address;
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");
149 main (int argc, char **argv)
153 GMimeMessage *message;
154 InternetAddressList *addresses;
156 const char *filename;
159 const char *value, *from;
162 struct tm gm_time_tm;
163 char time_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */
166 fprintf (stderr, "Usage: %s <mail-message>\n",
173 file = fopen (filename, "r");
175 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
181 stream = g_mime_stream_file_new (file);
183 parser = g_mime_parser_new_with_stream (stream);
185 message = g_mime_parser_construct_message (parser);
187 printf ("text is:\n[");
188 from = g_mime_message_get_sender (message);
189 addresses = internet_address_list_parse_string (from);
191 add_address_names (addresses, "from_name");
193 add_address_names (g_mime_message_get_all_recipients (message),
196 value = g_mime_message_get_subject (message);
197 print_term ("subject", value);
198 print_term ("body", value);
200 printf ("]\nterms is:\n[");
204 from = g_mime_message_get_sender (message);
205 addresses = internet_address_list_parse_string (from);
207 add_address_addrs (addresses, "from_email");
209 add_address_addrs (g_mime_message_get_all_recipients (message),
212 g_mime_message_get_date (message, &time, NULL);
214 gmtime_r (&time, &gm_time_tm);
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");
222 print_term ("date", time_str);
224 print_term ("label", "inbox");
225 print_term ("label", "unread");
226 print_term ("type", "mail");
228 value = g_mime_message_get_message_id (message);
229 print_term ("msgid", value);
231 print_term ("source_id", "1");
233 value = g_mime_message_get_message_id (message);
234 print_term ("thread", value);
238 g_object_unref (message);
239 g_object_unref (parser);
240 g_object_unref (stream);