msgchk.c
8.77 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2013-2016 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 3, 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, see <http://www.gnu.org/licenses/>. */
#include "mh.h"
#include "mailutils/kwd.h"
#include "mailutils/folder.h"
#include "mailutils/auth.h"
#include "mailutils/datetime.h"
#include <pwd.h>
static char prog_doc[] = N_("GNU MH msgchk");
static char args_doc[] = N_("USER [USER...]");
int date_option = 1;
int apop_option;
char *remote_host;
char *remote_user;
#define NOTIFY_MAIL 0x1
#define NOTIFY_NOMAIL 0x2
#define NOTIFY_ALL (NOTIFY_MAIL|NOTIFY_NOMAIL)
int notify = NOTIFY_ALL;
static struct mu_kwd notifytab[] = {
{ "mail", NOTIFY_MAIL },
{ "nomail", NOTIFY_NOMAIL },
{ "all", NOTIFY_ALL },
{ NULL }
};
static void
set_notify (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
int n;
if (mu_kwd_xlat_name (notifytab, arg, &n))
{
mu_parseopt_error (po, "unknown notify argument: %s", arg);
exit (po->po_exit_error);
}
notify |= n;
}
static void
clr_notify (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
int n;
if (mu_kwd_xlat_name (notifytab, arg, &n))
{
mu_parseopt_error (po, "unknown notify argument: %s", arg);
exit (po->po_exit_error);
}
notify &= ~n;
}
static struct mu_option options[] = {
{ "date", 0, NULL, MU_OPTION_DEFAULT,
N_("print out the last date mail was read"),
mu_c_bool, &date_option },
{ "notify", 0, "all|mail|nomail", MU_OPTION_DEFAULT,
N_("produce a message upon these events"),
mu_c_string, NULL, set_notify },
{ "nonotify", 0, "all|mail|nomail", MU_OPTION_DEFAULT,
N_("disable notification"),
mu_c_string, NULL, clr_notify },
{ "host", 0, N_("URL"), MU_OPTION_DEFAULT,
N_("check mail on this host or URL"),
mu_c_string, &remote_host },
{ "user", 0, N_("NAME"), MU_OPTION_DEFAULT,
N_("set user name for remote mailbox access"),
mu_c_string, &remote_user },
{ "apop", 0, NULL, MU_OPTION_DEFAULT,
N_("enable APOP"),
mu_c_bool, &apop_option },
MU_OPTION_END
};
static char *
attach_auth_ticket (mu_mailbox_t mbox)
{
char *filename = NULL;
mu_folder_t folder = NULL;
mu_authority_t auth = NULL;
if (mu_mailbox_get_folder (mbox, &folder) == 0
&& mu_folder_get_authority (folder, &auth) == 0
&& auth)
{
mu_wicket_t wicket;
int rc;
filename = mu_tilde_expansion (mu_ticket_file, MU_HIERARCHY_DELIMITER,
NULL);
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
("Reading user ticket file %s", filename));
if ((rc = mu_file_wicket_create (&wicket, filename)) == 0)
{
mu_ticket_t ticket;
if ((rc = mu_wicket_get_ticket (wicket, NULL, &ticket)) == 0)
{
rc = mu_authority_set_ticket (auth, ticket);
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
("Retrieved and set ticket: %d", rc));
}
else
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
("Error retrieving ticket: %s\n",
mu_strerror (rc)));
mu_wicket_destroy (&wicket);
}
else
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
("Error creating wicket: %s\n", mu_strerror (rc)));
if (rc)
{
free (filename);
filename = NULL;
}
}
return filename;
}
int
checkmail (const char *username, int personal)
{
int rc;
mu_url_t url;
mu_mailbox_t mbox;
size_t recent = 0, count = 0;
int status = 1;
int have_user;
if (remote_host)
{
static mu_url_t pop_hint;
if (!pop_hint)
{
rc = mu_url_create (&pop_hint, "pop://");
if (rc)
{
mu_error ("cannot create URL hint: %s", mu_strerror (rc));
exit (2);
}
}
rc = mu_url_create_hint (&url, remote_host, MU_URL_PARSE_DEFAULT,
pop_hint);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create_hint", remote_host,
rc);
exit (2);
}
if (apop_option)
{
rc = mu_url_set_auth (url, "+APOP");
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_url_set_auth", "+APOP", rc);
exit (2);
}
}
}
else
{
char *s;
rc = mu_construct_user_mailbox_url (&s, username);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_construct_user_mailbox_url",
username, rc);
exit (2);
}
rc = mu_url_create (&url, s);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", s, rc);
exit (2);
}
free (s);
}
rc = mu_mailbox_create_from_url (&mbox, url);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_create_from_url",
mu_url_to_string (url), rc);
exit (2);
}
if (personal)
{
char *filename = attach_auth_ticket (mbox);
have_user = mu_url_has_flag (url, MU_URL_USER);
if (!have_user)
{
mu_url_t tickurl;
if (mu_wicket_file_match_url (filename, url, MU_URL_PARSE_ALL,
&tickurl) == 0)
{
have_user = mu_url_has_flag (tickurl, MU_URL_USER);
mu_url_destroy (&tickurl);
}
}
free (filename);
}
else
have_user = 0;
if (!have_user)
{
mu_url_t turl;
mu_mailbox_get_url (mbox, &turl);
rc = mu_url_set_user (turl, username);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_url_set_user", username, rc);
exit (2);
}
}
rc = mu_mailbox_open (mbox, MU_STREAM_READ);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_open",
mu_url_to_string (url), rc);
exit (2);
}
rc = mu_mailbox_messages_count (mbox, &count);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_messages_count",
mu_url_to_string (url), rc);
if (count)
{
status = 0;
if (notify & NOTIFY_MAIL)
{
mu_off_t mbsiz = 0;
int newmail = 0;
rc = mu_mailbox_message_unseen (mbox, &recent);
switch (rc)
{
case 0:
newmail = 1;
break;
case MU_ERR_NOENT:
newmail = 0;
break;
default:
if (rc != ENOSYS && rc != MU_ERR_INFO_UNAVAILABLE)
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_messages_unseen",
mu_url_to_string (url), rc);
rc = mu_mailbox_messages_recent (mbox, &recent);
if (rc == 0)
newmail = recent > 0;
}
if (rc)
{
if (rc != ENOSYS && rc != MU_ERR_INFO_UNAVAILABLE)
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_messages_recent",
mu_url_to_string (url), rc);
mu_mailbox_get_size (mbox, &mbsiz);
if (personal)
mu_printf (ngettext ("You have %lu message",
"You have %lu messages",
count),
(unsigned long) count);
else
mu_printf (ngettext ("%s has %lu message",
"%s has %lu messages",
count),
username, (unsigned long) count);
mu_printf (ngettext (" (%lu byte)",
" (%lu bytes)",
mbsiz), (unsigned long) mbsiz);
}
else
{
if (personal)
mu_printf (newmail ? _("You have new mail waiting") :
_("You have old mail waiting"));
else
mu_printf (newmail ? _("%s has new mail waiting") :
_("%s has old mail waiting"), username);
}
if (date_option)
{
time_t t;
if (mu_mailbox_access_time (mbox, &t) == 0)
mu_c_streamftime (mu_strout,
_("; last read on %a, %d %b %Y %H:%M:%S %z"),
localtime (&t), NULL);
}
mu_printf ("\n");
}
}
else
{
status = 1;
if (notify & NOTIFY_NOMAIL)
{
if (personal)
mu_printf (_("You don't have any mail waiting\n"));
else
mu_printf (_("%s doesn't have any mail waiting\n"),
username);
}
}
mu_mailbox_destroy (&mbox);
return status;
}
int
main (int argc, char **argv)
{
int rc = 0;
MU_APP_INIT_NLS ();
mh_getopt (&argc, &argv, options, 0, args_doc, prog_doc, NULL);
if (argc == 0)
{
if (remote_user)
rc |= checkmail (remote_user, 1);
else
{
struct passwd *pw = getpwuid (getuid ());
if (!pw)
{
mu_error (_("cannot determine my username"));
return 2;
}
rc |= checkmail (pw->pw_name, 1);
}
}
else if (remote_user)
{
mu_error (_("no arguments are allowed when the -user option is given"));
return 2;
}
else
while (argc--)
rc |= checkmail (*argv++, 0);
return rc;
}