frm.c
15.4 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003,
2004, 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <mailutils/address.h>
#include <mailutils/argp.h>
#include <mailutils/attribute.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/header.h>
#include <mailutils/list.h>
#include <mailutils/mailbox.h>
#include <mailutils/message.h>
#include <mailutils/observer.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
#include <mailutils/nls.h>
#include <mailutils/tls.h>
#include <mailutils/error.h>
#include <mailutils/mutil.h>
#include <mailutils/mime.h>
static char *show_field;
static int show_to;
static int show_from = 1;
static int show_subject = 1;
static int show_number;
static int show_summary;
static int be_quiet;
static int align = 1;
static int show_query;
static int dbug;
#define IS_READ 0x001
#define IS_OLD 0x010
#define IS_NEW 0x100
static int select_attribute;
static int selected;
static int action (observer_t, size_t);
const char *program_version = "frm (" PACKAGE_STRING ")";
static char doc[] = N_("GNU frm -- display From: lines");
static struct attr_tab {
char *name; /* Attribute name */
int code; /* Corresponding IS_.* flag */
size_t len; /* Minimum abbreviation length */
} attr_tab[] = {
/* TRANSLATORS: See comment marked [frm status] below */
{ N_("new"), IS_NEW, 0 },
/* TRANSLATORS: See comment marked [frm status] below */
{ N_("old"), IS_OLD, 0 },
/* TRANSLATORS: See comment marked [frm status] below */
{ N_("unread"), IS_OLD, 0 },
/* TRANSLATORS: See comment marked [frm status] below */
{ N_("read"), IS_READ, 0 },
{ NULL }
};
static char attr_help[] =
/* TRANSLATORS: [frm status]
1) Please make sure the words "new", "unread", "old" and
"read" are translated exactly as in four preceeding messages.
2) If possible, select such translations for these words, that
differ by the first letter.
*/
N_("Select messages with the specific attribute. STATUS is one \
of the following: new, unread, old (same as unread) or read. Any unambiguous \
abbreviation of those is also accepted.");
/* Attribute table handling */
/* Prepares the table for use. Computes minimum abbreviation lengths. */
static void
prepare_attrs (void)
{
struct attr_tab *p, *q;
for (p = attr_tab; p->name; p++)
{
const char *name = gettext (p->name);
size_t len = strlen (name);
size_t n = 1;
for (q = attr_tab; q->name; q++)
{
if (p != q)
{
const char *str = gettext (q->name);
size_t slen = strlen (str);
if (memcmp (name, str, n) == 0)
{
for (n++;
memcmp (name, str, n) == 0
&& n < len
&& n < slen; n++)
;
q->len = n < slen ? n : slen;
}
}
}
p->len = n < len ? n : len;
}
}
/* Translates the textual status representation to the corresponding
IS_.* flag */
static int
decode_attr (char *arg)
{
struct attr_tab *p;
int len = strlen (arg);
int pretendents = 0;
for (p = attr_tab; p->name; p++)
{
const char *str = gettext (p->name);
if (str[0] == arg[0])
{
if (len < p->len)
pretendents++;
else if (len > strlen (str))
continue;
if (memcmp (str, arg, p->len) == 0)
return p->code;
}
}
if (pretendents)
mu_error (_("%s: ambiguous abbreviation"), arg);
else
mu_error (_("%s: unknown attribute"), arg);
return 0;
}
static struct argp_option options[] = {
{"debug", 'd', NULL, 0, N_("Enable debugging output"), 0},
{"field", 'f', N_("NAME"), 0, N_("Header field to display"), 0},
{"to", 'l', NULL, 0, N_("Include the To: information"), 0},
{"number", 'n', NULL, 0, N_("Display message numbers"), 0},
{"Quiet", 'Q', NULL, 0, N_("Very quiet"), 0},
{"query", 'q', NULL, 0, N_("Print a message if the mailbox contains some unread mail"), 0},
{"summary",'S', NULL, 0, N_("Print a summary of messages"), 0},
{"status", 's', N_("STATUS"), 0, attr_help, 0},
{"align", 't', NULL, 0, N_("Try to align"), 0},
{0, 0, 0, 0}
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
dbug++;
break;
case 'f':
show_field = arg;
show_from = 0;
show_subject = 0;
align = 0;
break;
case 'l':
show_to = 1;
break;
case 'n':
show_number = 1;
break;
case 'Q':
/* Very silent. */
be_quiet += 2;
if (freopen ("/dev/null", "w", stdout) == NULL)
{
perror (_("Cannot be very quiet"));
exit (3);
}
break;
case 'q':
be_quiet++;
show_query = 1;
break;
case 'S':
show_summary = 1;
break;
case 's':
select_attribute = decode_attr (arg);
break;
case 't':
align = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options,
parse_opt,
N_("[URL ...]"),
doc,
NULL,
NULL, NULL
};
static const char *frm_argp_capa[] = {
"common",
"license",
"mailbox",
#ifdef WITH_TLS
"tls",
#endif
NULL
};
/*
FIXME: Generalize this function and move it
to `mailbox/locale.c'. Do the same with the one
from `from/from.c' and `mail/util.c'...
*/
static char *
rfc2047_decode_wrapper (char *buf, size_t buflen)
{
char locale[32];
char *charset = NULL;
char *tmp;
int rc;
memset (locale, 0, sizeof (locale));
/* Try to deduce the charset from LC_ALL or LANG variables */
tmp = getenv ("LC_ALL");
if (!tmp)
tmp = getenv ("LANG");
if (tmp)
{
char *sp = NULL;
char *lang;
char *terr;
strncpy (locale, tmp, sizeof (locale) - 1);
lang = strtok_r (locale, "_", &sp);
terr = strtok_r (NULL, ".", &sp);
charset = strtok_r (NULL, "@", &sp);
if (!charset)
charset = mu_charset_lookup (lang, terr);
}
if (!charset)
return strdup (buf);
rc = rfc2047_decode (charset, buf, &tmp);
if (rc)
{
if (dbug)
mu_error (_("Cannot decode line `%s': %s"),
buf, mu_strerror (rc));
return strdup (buf);
}
return tmp;
}
/* Retrieve the Personal Name from the header To: or From: */
static int
get_personal (header_t hdr, const char *field, char *personal, size_t buflen)
{
char hfield[512];
int status;
/* Empty string. */
*hfield = '\0';
status = header_get_value_unfold (hdr, field, hfield, sizeof (hfield), NULL);
if (status == 0)
{
address_t address = NULL;
size_t len = 0;
char *s = rfc2047_decode_wrapper (hfield, strlen (hfield));
address_create (&address, s);
free (s);
address_get_personal (address, 1, personal, buflen, &len);
address_destroy (&address);
if (len == 0)
strncpy (personal, hfield, buflen)[buflen - 1] = '\0';
}
return status;
}
static struct {
size_t index;
size_t new;
size_t read;
size_t unread;
} counter;
/*
* Get the number of columns on the screen
* First try an ioctl() call not all shells set the COLUMNS environ.
* This function was taken from mail/util.c.
*/
int
util_getcols (void)
{
struct winsize ws;
ws.ws_col = ws.ws_row = 0;
if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_row == 0)
{
const char *columns = getenv ("COLUMNS");
if (columns)
ws.ws_col = strtol (columns, NULL, 10);
}
/* FIXME: Should we exit()/abort() if col <= 0 ? */
return ws.ws_col;
}
/* Observable action is being called on discovery of each message. */
/* FIXME: The format of the display is poorly done, please correct. */
static int
action (observer_t o, size_t type)
{
int status;
int col_cnt = 0;
switch (type)
{
case MU_EVT_MESSAGE_ADD:
{
mailbox_t mbox = observer_get_owner (o);
message_t msg = NULL;
header_t hdr = NULL;
attribute_t attr = NULL;
counter.index++;
mailbox_get_message (mbox, counter.index, &msg);
message_get_attribute (msg, &attr);
message_get_header (msg, &hdr);
if (attribute_is_read (attr))
counter.read++;
else if (attribute_is_seen (attr))
counter.unread++;
else if (attribute_is_recent (attr))
counter.new++;
if (((select_attribute & IS_READ) && (!attribute_is_read (attr)))
|| ((select_attribute & IS_NEW) && (!attribute_is_recent (attr)))
|| ((select_attribute & IS_OLD) && (!attribute_is_seen (attr))))
break;
if (select_attribute)
selected = 1;
if (be_quiet)
break;
if (show_number)
{
printf ("%4lu: ", (u_long) counter.index);
col_cnt += 6;
}
if (show_field) /* FIXME: This should be also rfc2047_decode. */
{
char hfield[256];
status = header_get_value_unfold (hdr, show_field, hfield,
sizeof (hfield), NULL);
if (status == 0)
printf ("%s", hfield);
}
if (show_to)
{
char hto[16];
status = get_personal (hdr, MU_HEADER_TO, hto, sizeof (hto));
if (status == 0)
{
printf ("(%s) ", hto);
col_cnt += strlen (hto) + 3;
}
else
{
printf ("(-----) ");
col_cnt += 8;
}
}
if (show_from)
{
char hfrom[32];
status = get_personal (hdr, MU_HEADER_FROM, hfrom, sizeof (hfrom));
if (status == 0)
{
printf ("%s\t", hfrom);
col_cnt += strlen (hfrom) + 4; // tab=4, sigh.
}
else
{
printf ("-----\t");
col_cnt += 9;
}
}
/*
A temporary fix for (correct) displaying:
util_getcols() - col_cnt - ~1. It's ugly, I know.
*/
if (show_subject)
{
char *hsubject;
status = header_aget_value_unfold (hdr, MU_HEADER_SUBJECT,
&hsubject);
if (status == 0)
{
char *s = rfc2047_decode_wrapper (hsubject, strlen (hsubject));
char out[80];
int fspace = util_getcols () - col_cnt - 1;
fspace = (fspace > (sizeof (out) - 1))
? (sizeof (out) - 1) : fspace;
memset (out, 0, sizeof (out));
strncpy (out, s, fspace);
printf ("%s", out);
free (s);
}
}
putchar ('\n');
break;
}
case MU_EVT_MAILBOX_PROGRESS:
/* Noop. */
break;
}
return 0;
}
static void
frm_abort (mailbox_t *mbox)
{
int status;
if ((status = mailbox_close (*mbox)) != 0)
{
url_t url;
mu_error (_("Could not close <%s>: %s."),
url_to_string (url), mu_strerror (status));
exit (3);
}
mailbox_destroy (mbox);
exit (3);
}
/* This is a clone of the elm program call "frm". It is a good example on
how to use the observable(callback) of libmailbox. "frm" has to
be very interactive, it is not possible to call mailbox_messages_count()
and wait for the scanning to finish before displaying. As soon as the scan
finds a new message we want to know about it. This is done by registering
an observable type MU_MAILBOX_MSG_ADD. The rest is formatting code. */
int
frm (char *mailbox_name)
{
int status;
mailbox_t mbox;
url_t url = NULL;
size_t total = 0;
status = mailbox_create_default (&mbox, mailbox_name);
if (status != 0)
{
mu_error (_("Could not create mailbox <%s>: %s."),
mailbox_name ? mailbox_name : _("default"),
mu_strerror (status));
exit (3);
}
if (dbug)
{
mu_debug_t debug;
mailbox_get_debug (mbox, &debug);
mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
}
mailbox_get_url (mbox, &url);
status = mailbox_open (mbox, MU_STREAM_READ);
if (status == ENOENT)
/* nothing to do */;
else if (status != 0)
{
mu_error (_("Could not open mailbox <%s>: %s."),
url_to_string (url), mu_strerror (status));
frm_abort (&mbox);
}
else
{
observer_t observer;
observable_t observable;
observer_create (&observer, mbox);
observer_set_action (observer, action, mbox);
mailbox_get_observable (mbox, &observable);
observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
memset (&counter, 0, sizeof counter);
status = mailbox_scan (mbox, 1, &total);
if (status != 0)
{
mu_error (_("Could not scan mailbox <%s>: %s."),
url_to_string (url), mu_strerror (status));
frm_abort (&mbox);
}
observable_detach (observable, observer);
observer_destroy (&observer, mbox);
if ((status = mailbox_close (mbox)) != 0)
{
mu_error (_("Could not close <%s>: %s."),
url_to_string (url), mu_strerror (status));
exit (3);
}
}
mailbox_destroy (&mbox);
if (show_summary)
{
if (total == 0)
printf (_("Folder contains no messages."));
else
{
char *delim = "";
printf (_("Folder contains "));
if (counter.new)
{
printf (ngettext ("%lu new message",
"%lu new messages",
counter.new),
(u_long) counter.new);
delim = ", ";
}
if (counter.unread)
{
printf ("%s", delim);
printf (ngettext ("%lu unread message",
"%lu unread messages",
counter.unread),
(u_long) counter.unread);
delim = ", ";
}
if (counter.read)
{
printf ("%s", delim);
printf (ngettext ("%lu read message",
"%lu read messages",
counter.read),
(u_long) counter.read);
}
/* TRANSLATORS: This dot finishes the sentence
"Folder contains XXX messages."
Leave it as it is unless your language requires to reorder
the parts of speach in the message
*/
printf (_("."));
}
printf ("\n");
}
else if (show_query)
{
if (total > 0)
printf (_("There are messages in that folder.\n"));
else
printf (_("No messages in that folder!\n"));
}
/* EXIT STATUS
Frm returns a zero status ("true") if messages matching `status' are
present. Frm returns 1 if no messages matching `status' are present,
but there are some messages, returns 2 if there are no messages at
all, or returns 3 if an error occurred. */
if (selected)
status = 0;
else if (total > 0)
status = 1;
else
status = 2;
return status;
}
int
main (int argc, char **argv)
{
int c;
int status = 0;
/* Native Language Support */
mu_init_nls ();
prepare_attrs ();
/* register the formats. */
mu_register_all_mbox_formats ();
mu_argp_init (program_version, NULL);
#ifdef WITH_TLS
mu_tls_init_client_argp ();
#endif
mu_argp_parse (&argp, &argc, &argv, 0, frm_argp_capa, &c, NULL);
/* have an argument */
if (c == argc)
status = frm (NULL);
else if (c + 1 == argc)
status = frm (argv[c]);
else
for (; c < argc; c++)
{
printf ("%s:\n", argv[c]);
status = frm (argv[c]);
}
return status;
}