scan.c
6.14 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003 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 */
/* MH scan command */
#include <mh.h>
#ifdef HAVE_TERMCAP_H
# include <termcap.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <mailutils/observer.h>
const char *argp_program_version = "scan (" PACKAGE_STRING ")";
static char doc[] = N_("GNU MH scan\v"
"Use -help to obtain the list of traditional MH options.");
static char args_doc[] = N_("[+folder] [msgs]");
/* GNU options */
static struct argp_option options[] = {
{"folder", 'f', N_("FOLDER"), 0, N_("Specify folder to scan")},
{"clear", 'c', N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("Clear screen after displaying the list")},
{"form", 'F', N_("FILE"), 0, N_("Read format from given file")},
{"format", 't', N_("FORMAT"), 0, N_("Use this format string")},
{"header", 'H', N_("BOOL"), OPTION_ARG_OPTIONAL, N_("Display header")},
{"width", 'w', N_("NUMBER"), 0, N_("Set output width")},
{"reverse", 'r', N_("BOOL"), OPTION_ARG_OPTIONAL, N_("List messages in reverse order")},
{"file", 'i', N_("FILE"), 0, N_("[Not yet implemented]")},
{"license", 'l', 0, 0, N_("Display software license"), -1},
{ 0 }
};
/* Traditional MH options */
struct mh_option mh_option[] = {
{"clear", 1, MH_OPT_BOOL},
{"form", 4, MH_OPT_ARG, "formatfile"},
{"format", 5, MH_OPT_ARG, "string"},
{"header", 3, MH_OPT_BOOL},
{"width", 1, MH_OPT_ARG, "number"},
{"reverse", 1, MH_OPT_BOOL},
{"file", 4, MH_OPT_ARG, "file"},
{ 0 }
};
static int clear;
static char *format_str = mh_list_format;
static int width = 80;
static int reverse;
static int header;
static mh_format_t format;
static mh_msgset_t msgset;
void list_message __P((mailbox_t mbox, message_t msg, size_t num, void *data));
void print_header __P((mailbox_t mbox));
void clear_screen __P((void));
static int
opt_handler (int key, char *arg, void *unused)
{
switch (key)
{
case '+':
case 'f':
current_folder = arg;
break;
case 'c':
clear = is_true(arg);
break;
case 'F':
mh_read_formfile (arg, &format_str);
break;
case 't':
format_str = arg;
break;
case 'H':
header = is_true(arg);
break;
case 'w':
width = strtoul (arg, NULL, 0);
if (!width)
{
mh_error (_("Invalid width"));
exit (1);
}
break;
case 'r':
reverse = is_true(arg);
break;
case 'i':
mh_error (_("'i' is not yet implemented."));
break;
case 'l':
mh_license (argp_program_version);
break;
default:
return 1;
}
return 0;
}
/* Observable Action this is being call at every message discover. */
static int
action (observer_t o, size_t type)
{
static int counter;
mailbox_t mbox;
message_t msg = NULL;
size_t num;
if (type == MU_EVT_MESSAGE_ADD)
{
mbox = observer_get_owner (o);
counter++;
mailbox_get_message (mbox, counter, &msg);
mh_message_number (msg, &num);
list_message (mbox, msg, num, NULL);
}
return 0;
}
int
main (int argc, char **argv)
{
int index;
mailbox_t mbox;
int status;
/* Native Language Support */
mu_init_nls ();
mh_argp_parse (argc, argv, options, mh_option, args_doc, doc,
opt_handler, NULL, &index);
if (mh_format_parse (format_str, &format))
{
mh_error (_("Bad format string"));
exit (1);
}
mbox = mh_open_folder (current_folder, 0);
argc -= index;
argv += index;
if ((argc == 0 || strcmp (argv[0], "all") == 0) && !reverse)
{
/* Fast approach */
observer_t observer;
observable_t observable;
size_t total;
print_header (mbox);
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);
}
else
{
mh_msgset_parse (mbox, &msgset, argc, argv, "all");
if (reverse)
mh_msgset_reverse (&msgset);
print_header (mbox);
status = mh_iterate (mbox, &msgset, list_message, NULL);
}
clear_screen ();
mh_global_save_state ();
return status;
}
void
print_header (mailbox_t mbox)
{
if (header)
{
url_t url = NULL;
char datestr[64];
time_t t;
mailbox_get_url (mbox, &url);
time (&t);
strftime (datestr, sizeof datestr, "%c", localtime (&t));
printf (_("Folder %s %s\n"), url_to_string (url), datestr);
}
}
#ifdef HAVE_TERMCAP_H
/* A subroutine for tputs */
int
putstdout(char c)
{
return putc(c, stdout);
}
#endif
void
clear_screen ()
{
if (clear)
{
#ifdef HAVE_TERMCAP_H
if (isatty (1))
{
char termcap_buf[1024];
char *buffer = termcap_buf;
char *termname;
if ((termname = getenv("TERM")) == NULL)
/* No terminal; Try ansi */
termname = "ansi";
if (tgetent(termcap_buf, termname) == 1)
{
char *clr = tgetstr ("cl", &buffer);
if (clr)
{
tputs(clr, 1, (int (*)())putstdout);
return;
}
}
}
#endif
/* Fall back to formfeed */
fprintf (stdout, "\f");
}
}
void
list_message (mailbox_t mbox, message_t msg, size_t num, void *data)
{
char *buffer;
mh_format (&format, msg, num, width, &buffer);
printf ("%s\n", buffer);
free (buffer);
}