common.c
13.1 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
/* 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <frm.h>
char *show_field; /* Show this header field instead of the default
`From: Subject:' pair. -f option */
int show_to; /* Additionally display To: field. -l option */
int show_number; /* Prefix each line with the message number. -n */
int frm_debug;
/* Get the number of columns on the screen
First try an ioctl() call, not all shells set the COLUMNS environ.
If ioctl does not succeed on stdout, try it on /dev/tty, as we
may work via a pipe.
This function was taken from mail/util.c. It should probably reside
in the library */
int
util_getcols (void)
{
struct winsize ws;
ws.ws_col = ws.ws_row = 0;
if (ioctl (1, TIOCGWINSZ, (char *) &ws) < 0)
{
int fd = open ("/dev/tty", O_RDWR);
ioctl (fd, TIOCGWINSZ, (char *) &ws);
close (fd);
}
if (ws.ws_row == 0)
{
const char *columns = getenv ("COLUMNS");
if (columns)
ws.ws_col = strtol (columns, NULL, 10);
}
return ws.ws_col;
}
/* Charset magic */
static char *output_charset = NULL;
const char *
get_charset ()
{
char *tmp;
if (!output_charset)
{
char locale[32];
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);
output_charset = strtok_r (NULL, "@", &sp);
if (output_charset)
output_charset = xstrdup (output_charset);
else
output_charset = mu_charset_lookup (lang, terr);
if (!output_charset)
output_charset = "ASCII";
}
}
return output_charset;
}
/* BIDI support (will be moved to lib when it's ready) */
#ifdef HAVE_LIBFRIBIDI
static int fb_charset_num = -1;
FriBidiChar *logical;
char *outstring;
size_t logical_size;
void
alloc_logical (size_t size)
{
logical = xmalloc (size * sizeof (logical[0]));
logical_size = size;
outstring = xmalloc (size);
}
void
puts_bidi (char *string)
{
if (fb_charset_num == -1)
{
fb_charset_num = fribidi_parse_charset (get_charset ());
if (fb_charset_num && frm_debug)
mu_error (_("fribidi failed to recognize charset `%s'"),
get_charset ());
}
if (fb_charset_num == 0)
puts (string);
else
{
FriBidiStrIndex len;
FriBidiCharType base = FRIBIDI_TYPE_ON;
fribidi_boolean log2vis;
static FriBidiChar *visual;
static size_t visual_size;
len = fribidi_charset_to_unicode (fb_charset_num,
string, strlen (string),
logical);
if (len + 1 > visual_size)
{
visual_size = len + 1;
visual = xrealloc (visual, visual_size * sizeof *visual);
}
/* Create a bidi string. */
log2vis = fribidi_log2vis (logical, len, &base,
/* output */
visual, NULL, NULL, NULL);
if (log2vis)
{
FriBidiStrIndex idx, st;
FriBidiStrIndex new_len;
for (idx = 0; idx < len;)
{
FriBidiStrIndex wid, inlen;
wid = 3 * logical_size;
st = idx;
if (fb_charset_num != FRIBIDI_CHARSET_CAP_RTL)
{
while (wid > 0 && idx < len)
wid -= fribidi_wcwidth (visual[idx++]);
}
else
{
while (wid > 0 && idx < len)
{
wid--;
idx++;
}
}
if (wid < 0 && idx > st + 1)
idx--;
inlen = idx - st;
new_len = fribidi_unicode_to_charset (fb_charset_num,
visual + st, inlen,
outstring);
printf ("%s", outstring);
}
putchar ('\n');
}
else
{
/* Print the string as is */
puts (string);
}
}
}
#else
# define alloc_logical(s)
# define puts_bidi puts
#endif
/* Output functions */
/* Number of columns in output:
Maximum 4 message number, to, from, subject -ln
Default 2 from, subject [none]
Minimum 1 FIELD -f FIELD
*/
static int numfields; /* Number of output fields */
static int fieldwidth[4]; /* Field start positions */
static char *linebuf; /* Output line buffer */
static size_t linemax; /* Size of linebuf */
static size_t linepos; /* Position in the output line buffer */
static int curfield; /* Current output field */
static int nextstart; /* Start position of the next field */
static int curcol; /* Current output column */
typedef void (*fmt_formatter) (const char *fmt, ...);
static fmt_formatter format_field;
void
print_line ()
{
if (linebuf)
{
puts_bidi (linebuf);
linebuf[0] = 0;
linepos = 0;
curcol = nextstart = 0;
}
else
putchar ('\n');
curfield = 0;
}
void
format_field_simple (const char *fmt, ...)
{
va_list ap;
if (curfield++)
putchar ('\t');
va_start (ap, fmt);
vprintf (fmt, ap);
va_end (ap);
}
void
format_field_align (const char *fmt, ...)
{
size_t n, width;
va_list ap;
va_start (ap, fmt);
if (nextstart != 0)
{
if (curcol >= nextstart)
{
if (curfield == numfields - 1)
{
puts_bidi (linebuf);
linepos = 0;
printf ("%*s", nextstart, "");
}
else
{
linebuf[linepos++] = ' ';
curcol++;
}
}
else if (nextstart != curcol)
{
/* align to field start */
n = snprintf (linebuf + linepos, linemax - linepos,
"%*s", nextstart - curcol, "");
linepos += n;
curcol = nextstart;
}
}
n = vsnprintf (linebuf + linepos, linemax - linepos, fmt, ap);
va_end (ap);
/* Compute output width */
if (curfield == numfields - 1)
{
for ( ; n > 0; n--)
{
int c = linebuf[linepos + n];
linebuf[linepos + n] = 0;
width = mbswidth (linebuf + linepos, 0);
if (width <= fieldwidth[curfield])
break;
linebuf[linepos + n] = c;
}
}
else
width = mbswidth (linebuf + linepos, 0);
/* Increment counters */
linepos += n;
curcol += width;
nextstart += fieldwidth[curfield++];
}
void
init_output (size_t s)
{
int i;
size_t width = 0;
if (s == 0)
{
format_field = format_field_simple;
return;
}
format_field = format_field_align;
/* Allocate the line buffer */
linemax = s * MB_LEN_MAX + 1;
linebuf = xmalloc (linemax);
alloc_logical (s);
/* Set up column widths */
if (show_number)
fieldwidth[numfields++] = 5;
if (show_to)
fieldwidth[numfields++] = 20;
if (show_field)
fieldwidth[numfields++] = 0;
else
{
fieldwidth[numfields++] = 20;
fieldwidth[numfields++] = 0;
}
for (i = 0; i < numfields; i++)
width += fieldwidth[i];
fieldwidth[numfields-1] = util_getcols () - width;
}
/*
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)
{
int rc;
char *tmp;
const char *charset = get_charset ();
if (strcmp (charset, "ASCII") == 0)
return strdup (buf);
rc = rfc2047_decode (charset, buf, &tmp);
if (rc)
{
if (frm_debug)
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)
{
char *hfield;
int status;
status = header_aget_value_unfold (hdr, field, &hfield);
if (status == 0)
{
address_t address = NULL;
char *s;
address_create (&address, hfield);
address_aget_personal (address, 1, &s);
address_destroy (&address);
if (s == NULL)
s = hfield;
else
free (hfield);
*personal = rfc2047_decode_wrapper (s, strlen (s));
free (s);
}
return status;
}
/* Observer action used to perform mailbox scanning. See the comment
to frm_scan below.
FIXME: The observer action paradygm does not allow for making
procedure-data closure, as it should. So, for the time being
the following static variables are used instead: */
static frm_select_t select_message; /* Message selection function */
static size_t msg_index; /* Index (1-based) of the current
message */
/* 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;
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;
msg_index++;
mailbox_get_message (mbox, msg_index, &msg);
message_get_attribute (msg, &attr);
message_get_header (msg, &hdr);
if (!select_message (msg_index, msg))
break;
if (show_number)
format_field ("%4lu:", (u_long) msg_index);
if (show_to)
{
char *hto;
status = get_personal (hdr, MU_HEADER_TO, &hto);
if (status == 0)
{
format_field ("(%s)", hto);
free (hto);
}
else
format_field ("(none)");
}
if (show_field) /* FIXME: This should be also rfc2047_decode. */
{
char *hfield;
status = header_aget_value_unfold (hdr, show_field, &hfield);
if (status == 0)
{
format_field ("%s", hfield);
free (hfield);
}
else
format_field ("");
}
else
{
char *tmp;
status = get_personal (hdr, MU_HEADER_FROM, &tmp);
if (status == 0)
{
format_field ("%s", tmp);
free (tmp);
}
else
format_field ("");
status = header_aget_value_unfold (hdr, MU_HEADER_SUBJECT,
&tmp);
if (status == 0)
{
char *s = rfc2047_decode_wrapper (tmp, strlen (tmp));
format_field ("%s", s);
free (s);
free (tmp);
}
}
print_line ();
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 mailbox `%s': %s"),
url_to_string (url), mu_strerror (status));
exit (3);
}
mailbox_destroy (mbox);
exit (3);
}
/* Scan the mailbox MAILBOX_NAME using FUN as the selection function.
FUN takes as its argument message number and the message itself
(message_t). It returns non-zero if that message is to be displayed
and zero otherwise.
Upon finishing scanning, the function places the overall number of
the messages processed into the memory location pointed to by
TOTAL */
void
frm_scan (char *mailbox_name, frm_select_t fun, size_t *total)
{
mailbox_t mbox;
int status;
url_t url;
status = mailbox_create_default (&mbox, mailbox_name);
if (status != 0)
{
if (mailbox_name)
mu_error (_("Could not create mailbox `%s': %s"),
mailbox_name, mu_strerror (status));
else
mu_error (_("Could not create default mailbox: %s"),
mu_strerror (status));
exit (3);
}
if (frm_debug)
{
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)
*total = 0;
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;
select_message = fun;
msg_index = 0;
observer_create (&observer, mbox);
observer_set_action (observer, action, mbox);
mailbox_get_observable (mbox, &observable);
observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
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 mailbox `%s': %s"),
url_to_string (url), mu_strerror (status));
exit (3);
}
}
mailbox_destroy (&mbox);
}