readmsg.c
8.45 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2007 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "readmsg.h"
#include "xalloc.h"
#define WEEDLIST_SEPARATOR " :,"
static void print_unix_header (mu_message_t);
static void print_header (mu_message_t, int, int, char **);
static void print_body (mu_message_t);
static int string_starts_with (const char * s1, const char *s2);
const char *program_version = "readmsg (" PACKAGE_STRING ")";
static char doc[] = N_("GNU readmsg -- print messages");
static error_t readmsg_parse_opt (int key, char *arg, struct argp_state *astate);
static struct argp_option options[] =
{
{ "debug", 'd', 0, 0, N_("Display debugging information"), 1 },
{ "header", 'h', 0, 0, N_("Display entire header"), 1 },
{ "weedlist", 'w', N_("LIST"), 0,
N_("List of header names separated by whitespace or commas"), 1 },
{ "folder", 'f', N_("FOLDER"), 0, N_("Folder to use"), 1 },
{ "no-header", 'n', 0, 0, N_("Exclude all headers"), 1 },
{ "form-feeds", 'p', 0, 0, N_("Output formfeeds between messages"), 1 },
{ "show-all-match", 'a', NULL, 0,
N_("Print all messages matching pattern, not just the first"), 1 },
{0, 0, 0, 0}
};
static struct argp argp = {
options,
readmsg_parse_opt,
NULL,
doc,
NULL,
NULL, NULL
};
static const char *readmsg_argp_capa[] = {
"common",
"mailbox",
#ifdef WITH_TLS
"tls",
#endif
NULL
};
int dbug = 0;
const char *mailbox_name = NULL;
const char *weedlist = NULL;
int no_header = 0;
int all_header = 0;
int form_feed = 0;
int show_all = 0;
static error_t
readmsg_parse_opt (int key, char *arg, struct argp_state *astate)
{
switch (key)
{
case 'd':
dbug++;
break;
case 'h':
all_header = 1;
break;
case 'f':
mailbox_name = arg;
break;
case 'w':
weedlist = arg;
break;
case 'n':
no_header = 1;
break;
case 'p':
form_feed = 1;
break;
case 'a':
show_all = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static int
string_starts_with (const char * s1, const char *s2)
{
const unsigned char * p1 = (const unsigned char *) s1;
const unsigned char * p2 = (const unsigned char *) s2;
int n = 0;
/* Sanity. */
if (s1 == NULL || s2 == NULL)
return n;
while (*p1 && *p2)
{
if ((n = toupper (*p1++) - toupper (*p2++)) != 0)
break;
}
return (n == 0);
}
static void
print_unix_header (mu_message_t message)
{
static size_t bufsize;
static char *buf;
size_t size;
mu_envelope_t envelope = NULL;
mu_message_get_envelope (message, &envelope);
if (mu_envelope_sender (envelope, NULL, 0, &size) || size == 0)
return;
if (size > bufsize)
{
bufsize = size + 1;
buf = xrealloc (buf, bufsize);
}
mu_envelope_sender (envelope, buf, bufsize, 0);
printf ("From %s ", buf);
if (mu_envelope_date (envelope, NULL, 0, &size) == 0 && size > 0)
{
if (size > bufsize)
{
bufsize = size + 1;
buf = xrealloc (buf, bufsize);
}
mu_envelope_date (envelope, buf, bufsize, NULL);
}
else
{
time_t t;
struct tm *tm;
t = time (NULL);
tm = localtime (&t);
mu_strftime (buf, bufsize, "%a %b %d %H:%M:%S %Y", tm);
}
printf ("%s", buf);
size = strlen (buf);
if (size > 1 && buf[size-1] != '\n')
putchar ('\n');
}
static void
print_header (mu_message_t message, int unix_header, int weedc, char **weedv)
{
mu_header_t header = NULL;
mu_message_get_header (message, &header);
if (weedc == 0)
{
mu_stream_t stream = NULL;
off_t offset = 0;
size_t len = 0;
char buf[128];
mu_header_get_stream (header, &stream);
while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0
&& len != 0)
{
buf[len] ='\0';
printf ("%s", buf);
offset += len;
}
}
else
{
size_t count;
size_t i;
mu_header_get_field_count (header, &count);
for (i = 1; i <= count; i++)
{
int j;
char *name = NULL;
char *value = NULL;
mu_header_aget_field_name (header, i, &name);
mu_header_aget_field_value (header, i, &value);
for (j = 0; j < weedc; j++)
{
if (weedv[j][0] == '!')
{
if (string_starts_with (name, weedv[j]+1))
break;
}
else if (string_starts_with (name, weedv[j]))
{
/* Check if mu_header_aget_value return an empty string. */
if (value && *value)
printf ("%s: %s\n", name, value);
}
}
free (value);
free (name);
}
putchar ('\n');
}
}
static void
print_body (mu_message_t message)
{
char buf[128];
mu_body_t body = NULL;
mu_stream_t stream = NULL;
off_t offset = 0;
size_t len = 0;
mu_message_get_body (message, &body);
mu_body_get_stream (body, &stream);
while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0
&& len != 0)
{
buf[len] ='\0';
printf ("%s", buf);
offset += len;
}
}
/* This is still work in progress */
/* FIXME: Parse options: See readmsg(1) part of elm:
readmsg 1 3 0
extracts three messages from the folder: the first, the third, and
the last. */
int
main (int argc, char **argv)
{
int status;
int *set = NULL;
int n = 0;
int i;
int index;
mu_mailbox_t mbox = NULL;
char **weedv;
int weedc;
int unix_header = 0;
/* Native Language Support */
mu_init_nls ();
mu_argp_init (program_version, NULL);
#ifdef WITH_TLS
mu_tls_init_client_argp ();
#endif
mu_argp_parse (&argp, &argc, &argv, 0, readmsg_argp_capa, &index, NULL);
/* register the formats. */
mu_register_all_mbox_formats ();
mu_register_extra_formats ();
status = mu_mailbox_create_default (&mbox, mailbox_name);
if (status != 0)
{
if (mailbox_name)
fprintf (stderr, _("Could not create mailbox `%s': %s\n"),
mailbox_name,
mu_strerror(status));
else
fprintf (stderr, _("Could not create default mailbox: %s\n"),
mu_strerror(status));
exit (2);
}
/* Debuging Trace. */
if (dbug)
{
mu_debug_t debug;
mu_mailbox_get_debug (mbox, &debug);
mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
}
status = mu_mailbox_open (mbox, MU_STREAM_READ);
if (status != 0)
{
mu_url_t url = NULL;
mu_mailbox_get_url (mbox, &url);
mu_error (_("Could not open mailbox `%s': %s\n"),
mu_url_to_string (url), mu_strerror (status));
exit (2);
}
if (weedlist == NULL)
weedlist = "Date To Cc Subject From Apparently-";
status = mu_argcv_get (weedlist, WEEDLIST_SEPARATOR, NULL,
&weedc, &weedv);
if (status)
{
mu_error (_("Cannot parse weedlist: %s"), mu_strerror (status));
exit (2);
}
for (i = 0; i < weedc; i++)
{
if (strcasecmp (weedv[i], "From_") == 0)
{
int j;
unix_header = 1;
free (weedv[i]);
for (j = i; j < weedc; j++)
weedv[j] = weedv[j+1];
weedc--;
if (weedc == 0 && !all_header)
no_header = 1;
}
}
if (all_header)
{
weedc = 0;
weedv = NULL;
}
/* Build an array containing the message number. */
argc -= index;
if (argc > 0)
msglist (mbox, show_all, argc, &argv[index], &set, &n);
for (i = 0; i < n; ++i)
{
mu_message_t msg = NULL;
status = mu_mailbox_get_message (mbox, set[i], &msg);
if (status != 0)
{
mu_error ("mu_mailbox_get_message: %s",
mu_strerror (status));
exit (2);
}
if (unix_header)
print_unix_header (msg);
if (!no_header)
print_header (msg, unix_header, weedc, weedv);
print_body (msg);
if (form_feed)
putchar ('\f');
else
putchar ('\n');
}
putchar ('\n');
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
return 0;
}