frm.c
7.46 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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "getopt.h"
#include <mailutils/mailbox.h>
#include <mailutils/header.h>
#include <mailutils/registrar.h>
#include <mailutils/observer.h>
#include <mailutils/address.h>
static int action (observer_t, size_t);
static int counter;
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"to", no_argument, 0, 'l'},
{"number", no_argument, 0, 'n'},
{"Quiet", no_argument, 0, 'Q'},
{"query", no_argument, 0, 'q'},
{"status", required_argument, 0, 's'},
{"align", no_argument, 0, 't'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
const char *short_options ="hlnQqs:tv";
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 be_silent;
static int align = 1;
static int show_query;
static int have_new_mail;
#define IS_READ 0x001
#define IS_OLD 0x010
#define IS_NEW 0x100
static int select_attribute;
static int counter;
/* Retrieve the Personal Name from the header To: or From: */
static int
get_personal (header_t hdr, char *field, char *personal, size_t buflen)
{
char hfield[512];
int status;
/* Empty string. */
*hfield = '\0';
status = header_get_value (hdr, field, hfield, sizeof (hfield), NULL);
if (status == 0)
{
address_t address = NULL;
size_t len = 0;
address_create (&address, hfield);
address_get_personal (address, 1, personal, buflen, &len);
address_destroy (&address);
if (len == 0)
strncpy (personal, hfield, buflen)[buflen - 1] = '\0';
}
return status;
}
/* Observable Action this is being call at every message discover. */
/* FIXME: The format of the display is poorly done, please correct. */
static int
action (observer_t o, size_t type)
{
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;
char hsubject[512];
counter++;
mailbox_get_message (mbox, counter, &msg);
message_get_attribute (msg, &attr);
message_get_header (msg, &hdr);
if (select_attribute
&& (select_attribute & IS_READ)
&& (!attribute_is_read (attr)))
{
break;
}
else if (select_attribute
&& (select_attribute & IS_NEW)
&& (!attribute_is_recent (attr)))
{
break;
}
else if (select_attribute
&& (select_attribute & IS_OLD)
&& (!attribute_is_seen (attr)))
{
break;
}
if (attribute_is_recent (attr))
have_new_mail = 1;
if (be_quiet)
break;
if (show_number)
printf ("%d: ", counter);
if (show_to)
{
char hto[16];
int status = get_personal (hdr, MU_HEADER_TO, hto, sizeof (hto));
if (status == 0)
printf ("(%s) ", hto);
else
printf ("(------)");
}
if (show_from)
{
char hfrom[32];
int status = get_personal (hdr, MU_HEADER_FROM, hfrom,
sizeof (hfrom));
if (status == 0)
printf ("%s\t", hfrom);
else
printf ("-----\t");
}
if (show_subject)
{
char hsubject[64];
int status = header_get_value (hdr, MU_HEADER_SUBJECT, hsubject,
sizeof (hsubject), NULL);
printf("%s\n", hsubject);
}
break;
}
case MU_EVT_MAILBOX_PROGRESS:
/* Noop. */
break;
}
return 0;
}
void
usage (const char *argv)
{
printf ("GNU Mailutils.\n");
printf ("Usage: %s [OPTIONS]\n\n", argv);
printf (" -h, --help display this help and exit\n");
printf (" -l, --to include the To: information\n");
printf (" -n, --number display the message numberd\n");
printf (" -Q, --Quiet very quiet\n");
printf (" -q, --query print a message if unread mail\n");
printf (" -s, --status=[nor] select message with the specific \
attribute\n");
printf (" [n]ew, [r]ead, [u]nread.\n");
printf (" -t, --align Try to align\n");
printf (" -v, --version display version information and exit\n");
printf ("\nReport bugs to bug-mailutils@gnu.org\n");
exit (0);
}
/* This is a clone of the elm program call "frm". It is a good example on
how to use the observable(callback) of libmailutils. "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
find a new message we want to know about it, this is done by registering
an observable type MU_MAILBOX_MSG_ADD. The rest is formating code. */
int
main(int argc, char **argv)
{
char *mailbox_name = NULL;
int c;
while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
{
switch (c)
{
case 'h':
usage (argv[0]);
break;
case 'l':
show_to = 1;
break;
case 'n':
show_number = 1;
break;
case 'Q':
/* Very silent. */
be_quiet += 2;
break;
case 'q':
be_quiet = show_query = 1;
break;
case 'S':
show_summary = 1;
break;
case 's':
if (optarg)
switch (*optarg)
{
case 'r':
select_attribute |= IS_READ;
break;
case 'o':
select_attribute |= IS_OLD;
break;
case 'n':
select_attribute |= IS_NEW;
break;
}
break;
case 't':
align = 1;
break;
case 'v':
printf ("Mailutils 0.0.0: frm\n");
exit (0);
break;
default:
break;
}
}
/* have an argument */
if (optind < argc)
mailbox_name = argv[optind];
/* register the formats. */
{
list_t bookie;
registrar_get_list (&bookie);
list_append (bookie, mbox_record);
list_append (bookie, path_record);
list_append (bookie, pop_record);
list_append (bookie, imap_record);
}
/* Construct the mailbox_t, attach a notification and destroy */
{
mailbox_t mbox;
observer_t observer;
observable_t observable;
int status;
if ((status = mailbox_create_default (&mbox, mailbox_name) != 0)
|| (status = mailbox_open (mbox, MU_STREAM_READ) != 0))
{
fprintf (stderr, "could not open mailbox\n");
exit (1);
}
observer_create (&observer, mbox);
observer_set_action (observer, action, mbox);
mailbox_get_observable (mbox, &observable);
observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
mailbox_scan (mbox, 1, NULL);
observable_detach (observable, observer);
observer_destroy (&observer, mbox);
mailbox_close(mbox);
mailbox_destroy(&mbox);
}
if (show_summary && be_quiet < 1)
printf ("You have %d messages\n", counter);
if (show_query && have_new_mail)
printf ("You have new mail\n");
return 0;
}