~ubuntu-branches/ubuntu/quantal/maildir-utils/quantal

« back to all changes in this revision

Viewing changes to msg/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2010-01-19 20:12:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100119201243-d8qmzgxgexhy1bs0
Tags: 0.6~beta1-1
* new upstream release 0.6-beta
  - that merges the several different programs under one binary mu
  - no sqlite storage is used anymore
* debian packaging changes:
  - debian/patches
    . remove all patches
  - remove debian/HOWTO (upstream document) it is completely outdated
  - debian/control:
    . adjust build-dep for gmime-2.4
    . remove build-dep on quilt and sqlite
    . adjust the description to new reality
  - debian/rules:
    . do not try to install doc files that are not present anymore
    . disable quilt adaptions
  - add debian/NEWS that explains that the separate programs are gone

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
** Copyright (C) 2008 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
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, write to the Free Software Foundation,
16
 
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
17
 
**  
18
 
*/
19
 
 
20
 
#include <stdio.h>
21
 
#include <string.h>
22
 
#include <alloca.h>
23
 
#include <stdlib.h>
24
 
#include <unistd.h>
25
 
#include <errno.h>
26
 
 
27
 
#include "mu/mu.h"
28
 
#include "mu/mu-msg-str.h"
29
 
#include "mu/mu-msg-flags.h"
30
 
#include "app/mu-app.h"
31
 
#include "msg/mu-msg-gmime.h"
32
 
 
33
 
enum {
34
 
        OPTION_FROM,
35
 
        OPTION_TO,
36
 
        OPTION_CC,
37
 
        OPTION_SUBJECT,
38
 
        OPTION_DATE,
39
 
        OPTION_BODY,
40
 
        OPTION_USER_AGENT,
41
 
        OPTION_MESSAGE_ID,
42
 
        OPTION_PRIORITY,
43
 
        OPTION_NUM
44
 
};
45
 
 
46
 
        
47
 
struct _MsgInfoOptions {
48
 
        gboolean _field[OPTION_NUM];
49
 
};
50
 
typedef struct _MsgInfoOptions MsgInfoOptions;
51
 
 
52
 
static MsgInfoOptions OPTIONS;
53
 
static GOptionEntry OPTION_ENTRIES [] = {
54
 
        {"subject", 's', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_SUBJECT],
55
 
         "get the message subject"},
56
 
        {"sender", 'f', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_FROM],
57
 
         "get the message sender (From:)"},
58
 
        {"to", 't', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_TO],
59
 
         "get the message recipients (To:)"},
60
 
        {"cc", 'c', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_CC],
61
 
         "get the carbon-copy recipients (Cc:)"},
62
 
        {"date", 'd', 0, G_OPTION_ARG_NONE,&OPTIONS._field[OPTION_DATE],
63
 
         "get the message date"},
64
 
        {"body", 'b', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_BODY],
65
 
         "get the message body (default)"},
66
 
        {"user-agent", 'u', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_USER_AGENT],
67
 
         "get the message user agent"},
68
 
        {"message-id", 'i', 0, G_OPTION_ARG_NONE,&OPTIONS._field[OPTION_MESSAGE_ID],
69
 
         "get the message-id"},
70
 
        {"priority", 'p', 0, G_OPTION_ARG_NONE, &OPTIONS._field[OPTION_PRIORITY],
71
 
         "get the message priority"},
72
 
        {NULL}
73
 
};
74
 
 
75
 
static gboolean
76
 
display_info_field (MuMsgGMime *msg, MsgInfoOptions *opts)
77
 
{
78
 
        int i;
79
 
        for (i = 0; i != OPTION_NUM; ++i) {
80
 
 
81
 
                const char *str = NULL;
82
 
 
83
 
                if (!opts->_field[i])
84
 
                        continue;
85
 
                
86
 
                switch (i) {
87
 
        
88
 
                case OPTION_FROM:
89
 
                        str = mu_msg_gmime_get_from (msg); break;
90
 
                case OPTION_TO:
91
 
                        str = mu_msg_gmime_get_to (msg); break;
92
 
                case OPTION_CC:
93
 
                        str = mu_msg_gmime_get_cc (msg); break;
94
 
                case OPTION_SUBJECT:
95
 
                        str = mu_msg_gmime_get_subject (msg); break;
96
 
                case OPTION_BODY:
97
 
                        str = mu_msg_gmime_get_body_text (msg); 
98
 
                        if (!str)
99
 
                                str = mu_msg_gmime_get_body_html (msg);
100
 
                        break;
101
 
                case OPTION_USER_AGENT:
102
 
                        str = mu_msg_gmime_get_user_agent (msg); break;
103
 
                case OPTION_MESSAGE_ID:
104
 
                        str = mu_msg_gmime_get_msgid (msg); break;
105
 
                case OPTION_PRIORITY:
106
 
                        str = mu_msg_str_prio(
107
 
                                mu_msg_gmime_get_priority (msg)); break;
108
 
                case OPTION_DATE: {
109
 
                        time_t t = mu_msg_gmime_get_date (msg);
110
 
                        if (t) 
111
 
                                str = mu_msg_str_date_s(t);
112
 
                        break;
113
 
                }
114
 
                default:
115
 
                        g_warning ("%s: invalid field %d", __FUNCTION__, i);
116
 
                        return FALSE;
117
 
                }
118
 
 
119
 
                if (str)
120
 
                        g_print ("%s\n", str);
121
 
        }
122
 
        return TRUE;
123
 
}
124
 
 
125
 
 
126
 
static gboolean 
127
 
display_info (const char* file, MsgInfoOptions *opts)
128
 
{
129
 
        MuMsgGMime *msg;        
130
 
        g_return_val_if_fail (file, FALSE);
131
 
 
132
 
        if (access (file, R_OK) != 0) {
133
 
                g_printerr ("error opening %s: %s\n", file, strerror(errno));
134
 
                return FALSE;
135
 
        }
136
 
        
137
 
        msg = mu_msg_gmime_new (file);
138
 
        if (!msg) {
139
 
                g_printerr ("error: failed to analyze message %s\n", file);
140
 
                return FALSE;
141
 
        }
142
 
 
143
 
        display_info_field (msg, opts);
144
 
 
145
 
        mu_msg_gmime_destroy (msg);
146
 
        return TRUE;
147
 
}
148
 
 
149
 
 
150
 
gboolean
151
 
get_options (int *argcp, char ***argvp)
152
 
{       
153
 
        GError *err = NULL;
154
 
        int i;
155
 
        gboolean has_opt;
156
 
        
157
 
        memset (&OPTIONS, 0, sizeof(MsgInfoOptions));   
158
 
 
159
 
        if (!mu_conf_handle_options (mu_app_conf(),OPTION_ENTRIES, 
160
 
                                     argcp, argvp, &err)) {
161
 
                g_printerr ("option parsing failed: %s\n", err ? err->message : "");
162
 
                if (err)
163
 
                        g_error_free (err);
164
 
                return FALSE;
165
 
        }
166
 
 
167
 
        /* default to body, if no options are set... */
168
 
        has_opt = FALSE;
169
 
        for (i = 0; i != OPTION_NUM; ++i) {
170
 
                if (OPTIONS._field[i]) {
171
 
                        has_opt = TRUE;
172
 
                        break;
173
 
                }
174
 
        }
175
 
 
176
 
        if (!has_opt)
177
 
                OPTIONS._field[OPTION_BODY] = TRUE;
178
 
        
179
 
        if (*argcp <= 1) {
180
 
                g_printerr ("error: expected: some mailfile(s)\n");
181
 
                return FALSE;
182
 
        }
183
 
        
184
 
        return TRUE;
185
 
}
186
 
 
187
 
 
188
 
 
189
 
int
190
 
main (int argc, char *argv[])
191
 
{
192
 
        int retval; 
193
 
        int i;
194
 
 
195
 
        if (!mu_app_init (&argc, &argv, "mu-msginfo")) {
196
 
                g_printerr ("failed to init mu\n");
197
 
                return 1;
198
 
        }
199
 
 
200
 
        if (!get_options (&argc, &argv)) {
201
 
                g_printerr ("error: failed to handle options\n");
202
 
                mu_app_uninit();
203
 
                return 1;
204
 
        }
205
 
                
206
 
        mu_msg_gmime_init();
207
 
 
208
 
        retval = 0;
209
 
        for (i = 1; i != argc; ++i) {
210
 
                if (!display_info (argv[i], &OPTIONS)) {
211
 
                        retval = 1;
212
 
                        break;
213
 
                }
214
 
        }
215
 
        
216
 
        mu_msg_gmime_uninit ();
217
 
        mu_app_uninit ();
218
 
        
219
 
        return retval;
220
 
}