]> git.notmuchmail.org Git - notmuch/blob - g_mime_test.c
Initial commit of a test program to form the basis of notmuch.
[notmuch] / g_mime_test.c
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
25 #include <gmime/gmime.h>
26
27 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
28
29 static void
30 print_header (const char *name, const char *value)
31 {
32     printf ("%s:", name);
33     if (value)
34         printf ("\t%s", value);
35     printf ("\n");
36 }
37
38 int
39 main (int argc, char **argv)
40 {
41     GMimeStream *stream;
42     GMimeParser *parser;
43     GMimeMessage *message;
44
45     const char *filename;
46     FILE *file;
47
48     const char *sup_entry_headers[] = {
49         "From",
50         "Subject",
51         "Date",
52         "References",
53         "CC",
54         "To",
55         "In-Reply-To"
56     };
57     const char *value;
58     int i;
59
60     if (argc < 2) {
61         fprintf (stderr, "Usage: %s <mail-message>\n",
62                  argv[0]);
63         exit (1);
64     }
65
66     filename = argv[1];
67
68     file = fopen (filename, "r");
69     if (! file) {
70         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
71         exit (1);
72     }
73     
74     g_mime_init (0);
75     
76     stream = g_mime_stream_file_new (file);
77
78     parser = g_mime_parser_new_with_stream (stream);
79
80     message = g_mime_parser_construct_message (parser);
81
82     value = g_mime_message_get_message_id (message);
83     print_header ("message_id", value);
84
85     for (i = 0; i < ARRAY_SIZE (sup_entry_headers); i++) {
86         value = g_mime_object_get_header (GMIME_OBJECT (message),
87                                           sup_entry_headers[i]);
88         print_header (sup_entry_headers[i], value);
89     }
90
91     g_object_unref (message);
92     g_object_unref (parser);
93     g_object_unref (stream);
94     
95     return 0;
96 }