updated on Thu Jan 19 12:17:07 UTC 2012
[aur-mirror.git] / mailcheck / 002_advanced_counting.diff
blobd4c53c5ee3271750d6421793789064736a5d6780
1 ## 002_advanced_counting.dpatch by Tomas Hoger <thoger@pobox.sk>
2 ##
3 ## DP: Advanced mail counting, brief mode, maildir handling fixes,
4 ## DP: possibility to specify alternate config file.
6 diff -ruN mailcheck-1.91.1/mailcheck.1 mailcheck-1.91.1+2/mailcheck.1
7 --- mailcheck-1.91.1/mailcheck.1 2001-05-11 22:46:07.000000000 +0200
8 +++ mailcheck-1.91.1+2/mailcheck.1 2005-07-02 16:31:01.000000000 +0200
9 @@ -1,91 +1,85 @@
10 -.TH MAILCHECK 1
11 +.TH MAILCHECK 1 "2 July 2005"
13 .SH NAME
14 -mailcheck \- Check multiple mailboxes and/or Maildirs for mail
15 +mailcheck \- Check multiple mailboxes and/or Maildirs for new mail
17 .SH SYNOPSIS
18 -.B mailcheck
19 -.I "[-l]"
20 -.SH "DESCRIPTION"
21 -.B mailcheck
22 -is a simple, configurable tool that allows multiple mailboxes to be
23 -checked for the existence of mail. For local mail, it supports both
24 -the traditional mbox format and the newer Maildir (qmail) format.
25 -Mail can also be checked for on remote servers using either the POP3 or
26 -IMAP protocol.
27 -.PP
28 -Typically, one would invoke
29 -.BR mailcheck
30 -in /etc/profile or a user-specific login script.
31 -E-mail junkies may also find it useful to invoke
32 -.BR mailcheck
33 -occasionally to check for new mail in alternate mailboxes.
34 -.PP
35 -The author uses
36 -.BR mailcheck
37 -to keep track of messages arriving in mailboxes corresponding
38 -to several mailing lists he subscribes to.
39 +\fBmailcheck\fP [-lbcsh] [-f rcfile]
41 +.SH DESCRIPTION
42 +\fBmailcheck\fP is a simple, configurable tool that allows multiple
43 +mailboxes to be checked for the existence of mail. For local mail, it
44 +supports both the traditional mbox format and the newer Maildir format. Mail
45 +can also be checked for on remote servers using either the POP3 or IMAP
46 +protocol.
47 +.PP
48 +Typically, one would invoke \fBmailcheck\fP in /etc/profile or a
49 +user-specific login script. E-mail junkies may also find it useful to
50 +invoke \fBmailcheck\fP occasionally to check for new mail in alternate
51 +mailboxes.
52 +.PP
53 +The author uses \fBmailcheck\fP to keep track of messages arriving in
54 +mailboxes corresponding to several mailing lists he subscribes to.
56 .SH OPTIONS
57 .TP
58 -.B \-l
59 -Runs
60 -.B mailcheck
61 -in login mode. If a
62 -.B ~/.hushlogin
63 -file exists, mailcheck will exit silently. This option is intended
64 -to be used on systems that invoke mailcheck from a global login script
65 -such as
66 -.B /etc/profile.
67 +\fB\-l\fP
68 +Runs \fBmailcheck\fP in login mode. If a \fI~/.hushlogin\fP file exists,
69 +mailcheck will exit silently. This option is intended to be used on
70 +systems that invoke mailcheck from a global login script such as
71 +\fI/etc/profile\fP.
72 +.TP
73 +\fB\-b\fP
74 +Brief mode. Produces less verbose output. If mailbox or Maildir is inside
75 +user's home direcory, only relative path is printed to output.
76 +.TP
77 +\fB\-c\fP
78 +Use more advanced counting method. While counting mails, \fBmailcheck\fP
79 +looks inside mboxes and Maildirs and count new and unread messages
80 +separately. If mbox/maildir does not contain any new or unread mail, it's
81 +excluded from report. Produced output contains more valuable information, but
82 +this method is more time-consuming.
83 +.TP
84 +\fB\-s\fP
85 +Print "no mail" summary. If no new mail message is found, print at least "no
86 +mail message" at the end. Only makes sense in combination with \fB\-c\fP.
87 +.TP
88 +\fB\-f\fP
89 +Specify alternative rc file location. If provided, default locations (see
90 +\fBFILES\fP) are not checked.
91 +.TP
92 +\fB\-h\fP
93 +Print short usage information.
95 .SH CONFIGURATION
96 -.PP
97 -Configuring
98 -.B mailcheck
99 -is simple. Upon startup,
100 -.B mailcheck
101 -looks for a file called
102 -.B .mailcheckrc
103 -in the user's home directory. If that file does not exist, the
104 -default configuration file
105 -.B /etc/mailcheckrc
106 +Configuring \fBmailcheck\fP is simple. Upon startup, \fBmailcheck\fP looks
107 +for a file called \fB.mailcheckrc\fP in the user's home directory. If that
108 +file does not exist, the default configuration file \fB/etc/mailcheckrc\fP
109 is used instead.
111 -Lines beginning with a hash sign (
112 -.B #
113 -) are treated as comments and will not be processed.
114 -Lines beginning with
115 -.B pop3:
117 -.B imap:
118 -are parsed like URLs and used to connect to network mail servers.
119 -All other lines
120 -are treated as pathnames to mailbox files or Maildir directories.
121 -.PP
122 -Environment variables in the format
123 -.B $(NAME)
124 -will be expanded inline. For example:
125 -.TP
126 -.B /var/spool/mail/$(USER)
127 -Will check the user's mailbox in
128 -.B /var/spool/mail.
129 -.TP
130 -.B $(HOME)/Mailbox
131 -Will check the default Maildir used by qmail installations.
132 -.PP
133 -When connecting to POP3 or IMAP servers, the account password is not
134 -stored in the mailcheckrc file. Instead, the
135 -.B .netrc
136 -file in the user's home directory is used. This file, originally
137 -intended for use with
138 -.IR ftp (1)
139 -and later used by
140 -.IR fetchmail (1),
141 -should be readable only by the user owning it. It stores
142 -server/user/password combinations in the form:
144 -machine
145 -.I servername
146 -login
147 -.I username
148 -password
149 -.I password
150 +Lines beginning with a hash sign (\fB#\fP) are treated as comments and will
151 +not be processed. Lines beginning with \fBpop3:\fP or \fBimap:\fP are
152 +parsed like URLs and used to connect to network mail servers. All other
153 +lines are treated as pathnames to mailbox files or Maildir directories.
154 +.PP
155 +Environment variables in the format \fB$(NAME)\fP will be expanded inline.
156 +For example:
157 +.TP
158 +\fB/var/spool/mail/$(USER)\fP
159 +Will check the user's mailbox in \fB/var/spool/mail\fP.
160 +.TP
161 +\fB$(HOME)/Mailbox\fP
162 +Will check the default mailbox used by qmail installations.
163 +.PP
164 +When connecting to POP3 or IMAP servers, the account password is not stored
165 +in the mailcheckrc file. Instead, the \fB.netrc\fP file in the user's home
166 +directory is used. This file, originally intended for use with
167 +\fIftp\fP(1) and later used by \fIfetchmail\fP(1), should be readable only
168 +by the user owning it. It stores server/user/password combinations in the
169 +form:
171 +machine \fIservername\fP login \fIusername\fP password \fIpassword\fP
173 .SH FILES
175 .B /etc/mailcheckrc
176 @@ -99,10 +93,9 @@
177 be used.
179 .B ~/.netrc
180 -This tells
181 -.B mailcheck
182 -what password to use for a given server/user combination when checking
183 -POP3 or IMAP mail.
184 +This tells \fBmailcheck\fP what password to use for a given server/user
185 +combination when checking POP3 or IMAP mail.
187 .SH COPYRIGHT
188 Copyright (C) 1996, 1997, 1998, 2001, Jefferson E. Noxon.
190 @@ -119,14 +112,22 @@
191 the Free Software Foundation; either version 2 of the License, or
192 (at your option) any later version.
194 -On Debian GNU/Linux see /usr/doc/copyright/GPL
195 +On Debian GNU/Linux see /usr/share/common-licenses/GPL
197 .SH AUTHOR
198 Mailcheck was written for Debian GNU/Linux by Jefferson E. Noxon
199 <jeff@planetfall.com>.
201 .SH ACKNOWLEDGEMENTS
202 POP3 and IMAP support was added by Rob Funk <rfunk@funknet.net>.
203 +.PP
204 +Several enhancements by Tomas Hoger <thoger@pobox.sk>.
206 .SH BUGS
207 It is probably not a good idea to store passwords in a .netrc file.
208 +.PP
209 +No SSL/TLS support for POP3 and IMAP.
211 .SH SEE ALSO
212 -biff(1), mail(1), fetchmail(1), netrc(5), ftp(1)
213 +netrc(5), mbox(5), maildir(5), login(1), fetchmail(1)
215 diff -ruN mailcheck-1.91.1/mailcheck.c mailcheck-1.91.1+2/mailcheck.c
216 --- mailcheck-1.91.1/mailcheck.c 2001-05-11 22:38:50.000000000 +0200
217 +++ mailcheck-1.91.1+2/mailcheck.c 2005-10-27 21:14:58.000000000 +0200
218 @@ -1,6 +1,8 @@
219 /* mailcheck.c
221 * Copyright 1996, 1997, 1998, 2001 Jefferson E. Noxon <jeff@planetfall.com>
222 + * 2001 Rob Funk <rfunk@funknet.net>
223 + * 2003, 2005 Tomas Hoger <thoger@pobox.sk>
225 * This file may be copied under the terms of the GNU Public License
226 * version 2, incorporated herein by reference.
227 @@ -8,6 +10,11 @@
229 /* Command line parameters:
230 * -l: login mode; program exits quietly if ~/.hushlogin exists.
231 + * -b: brief mode; less verbose output mode
232 + * -c: use more advanced counting method
233 + * -s: print "no mail" summary if needed
234 + * -f: specify alternative rc file location
235 + * -h: print usage
238 #include <stdlib.h>
239 @@ -25,25 +32,69 @@
241 #include "netrc.h"
243 +#define BUF_SIZE (2048)
245 extern int sock_connect (char *hostname, int port);
248 /* Global variables */
249 char *Homedir; /* Home directory pathname */
250 -int Login_mode; /* TRUE if the '-l' switch is set */
252 -#define BUF_SIZE (2048)
253 +unsigned short have_mail= 0; /* Any mail found? */
254 +struct {
255 + unsigned short login_mode; /* see '-l' option */
256 + unsigned short brief_mode; /* see '-b' option */
257 + unsigned short advanced_count; /* see '-c' option */
258 + unsigned short show_summary; /* see '-s' option */
259 + char *rcfile_path; /* see '-f' option */
260 +} Options= {0, 0, 0, 0, NULL};
263 +/* Print usage information. */
264 +void
265 +print_usage(void)
267 + printf("Usage: mailcheck [-bchls] [-f rcfile]\n"
268 + "\n"
269 + "Options:\n"
270 + " -b - brief output mode\n"
271 + " -c - use more advanced counting method for mboxes and maildirs\n"
272 + " -l - login mode, honor ~/.hushlogin file\n"
273 + " -s - show \"no mail\" summary, if no new mail was found\n"
274 + " -f - specify alternative rcfile location\n"
275 + " -h - show this help screen\n"
276 + "\n");
279 -/* Open an rc file. Return NULL if we can't find one. */
280 +/* Open an rc file. Exit with error message, if attempt to open rcfile failed.
281 + * Otherwise, return valid FILE* .
282 + */
283 FILE *
284 open_rcfile (void)
286 char namebuf[256];
287 + FILE *rcfile;
289 - snprintf (namebuf, sizeof (namebuf), "%s/.mailcheckrc", Homedir);
290 - if (!access (namebuf, R_OK))
291 - return fopen (namebuf, "r");
293 - return fopen ("/etc/mailcheckrc", "r");
294 + /* if rcfile path was provided, do not try default locations */
295 + if (Options.rcfile_path != NULL) {
296 + if ((rcfile= fopen(Options.rcfile_path, "r")) == NULL) {
297 + fprintf(stderr, "error: couldn't open rcfile '%s'\n",
298 + Options.rcfile_path);
299 + exit(1);
302 + else {
303 + snprintf(namebuf, sizeof (namebuf), "%s/.mailcheckrc", Homedir);
305 + if ((rcfile= fopen(namebuf, "r")) == NULL) {
306 + if ((rcfile= fopen("/etc/mailcheckrc", "r")) == NULL) {
307 + fprintf (stderr, "mailcheck: couldn't open /etc/mailcheckrc "
308 + "nor %s/.mailcheckrc\n", Homedir);
309 + exit(1);
314 + return rcfile;
318 @@ -88,6 +139,44 @@
319 return expand_envstr (path);
322 +/* Should entry in maildir be ignored? */
323 +inline int
324 +ignore_maildir_entry(const char *dir, const struct dirent *entry) {
325 + char fname[BUF_SIZE];
326 + struct stat filestat;
328 + /* *all* dotfiles should be ignored in maildir, not only . and .. ! */
329 + if (entry->d_name[0] == '.')
330 + return 1;
332 + /* also count only regular files
333 + * use dirent's d_type if possible, otherwise stat file (which is much
334 + * slower) */
335 +#ifdef _DIRENT_HAVE_D_TYPE
336 + if (entry->d_type != DT_UNKNOWN) {
337 + if (entry->d_type != DT_REG)
338 + return 1;
340 + else {
341 +#endif /* _DIRENT_HAVE_D_TYPE */
342 + snprintf(fname, sizeof(fname), "%s/%s", dir, entry->d_name);
343 + fname[sizeof(fname) - 1]= '\0';
345 + if (stat(fname, &filestat) != 0) {
346 + fprintf(stderr, "mailcheck: failed to stat file: %s\n", fname);
347 + return 1;
350 + if (!S_ISREG(filestat.st_mode))
351 + return 1;
352 +#ifdef _DIRENT_HAVE_D_TYPE
354 +#endif /* _DIRENT_HAVE_D_TYPE */
356 + return 0;
359 +/* Count files in subdir of maildir (new/cur/tmp). */
361 count_entries (char *path)
363 @@ -95,17 +184,22 @@
364 struct dirent *entry;
365 int count = 0;
367 - mdir = opendir (path);
368 - if (!mdir)
369 + if ((mdir= opendir(path)) == NULL)
370 return -1;
372 - while ((entry = readdir (mdir)))
373 - ++count;
374 + while ((entry = readdir (mdir))) {
375 + if (ignore_maildir_entry(path, entry))
376 + continue;
378 + count++;
381 - return count - 2;
383 + closedir(mdir);
385 + return count;
388 +/* Get password for given account on given host from ~/.netrc file. */
389 char *
390 getpw (char *host, char *account)
392 @@ -227,6 +321,124 @@
393 return (port);
396 +/* Count mails in unix mbox. */
397 +int
398 +check_mbox(const char *path, int *new, int *read, int *unread)
400 + char linebuf[BUF_SIZE];
401 + FILE *mbox;
402 + int linelen;
403 + unsigned short in_header= 0; /* do we parse mail header or mail body? */
405 + if ((mbox= fopen(path, "r")) == NULL) {
406 + fprintf(stderr, "mailcheck: unable to open mbox %s\n", path);
407 + return -1;
410 + *new= 0;
411 + *read= 0;
412 + *unread= 0;
414 + while (fgets(linebuf, sizeof(linebuf), mbox)) {
415 + if (!in_header) {
416 + if (strncmp(linebuf, "From ", 5) == 0) { /* 5 == strlen("From ") */
417 + in_header= 1;
418 + (*new)++;
421 + else {
422 + if (linebuf[0] == '\n') {
423 + in_header= 0;
425 + else if (strncmp(linebuf, "Status: ", 8) == 0) { /* 8 == strlen("Status: ") */
426 + linelen= strlen(linebuf);
428 + if (linelen >= 10 &&
429 + ((linebuf[8] == 'R' && linebuf[9] == 'O') ||
430 + (linebuf[8] == 'O' && linebuf[9] == 'R'))) {
431 + (*new)--;
432 + (*read)++;
434 + else if (linelen >= 9 && linebuf[8] == 'O') {
435 + (*new)--;
436 + (*unread)++;
442 + fclose(mbox);
444 + return 0;
447 +/* Count mails in maildir. Slightely modified original Jeff's version. Just
448 + * counts files in maildir/new and maildir/cur. */
449 +int
450 +check_maildir_old(const char *path, int *new, int *cur)
452 + char dir[BUF_SIZE];
454 + snprintf(dir, sizeof(dir), "%s/new", path);
455 + *new= count_entries(dir);
456 + snprintf(dir, sizeof(dir), "%s/cur", path);
457 + *cur= count_entries(dir);
459 + if (*new == -1 || *cur == -1)
460 + return -1;
461 + else
462 + return 0;
465 +/* Count mails in maildir. Newer, more sophisticated, but also more time
466 + * consuming version. */
467 +int
468 +check_maildir(const char *path, int *new, int *read, int *unread) {
469 + char dir[BUF_SIZE];
470 + DIR *mdir;
471 + struct dirent *entry;
472 + char *pos;
474 + /* new mail - standard way */
475 + snprintf(dir, sizeof(dir), "%s/new", path);
476 + *new= count_entries(dir);
477 + if (*new == -1)
478 + return -1;
480 + /* older mail - check also mail status */
481 + snprintf(dir, sizeof(dir), "%s/cur", path);
482 + if ((mdir= opendir(dir)) == NULL)
483 + return -1;
485 + *read= 0;
486 + *unread= 0;
487 + while ((entry = readdir (mdir))) {
488 + if (ignore_maildir_entry(dir, entry))
489 + continue;
491 + if ((pos= strchr(entry->d_name, ':')) == NULL) {
492 + (*unread)++;
493 + }
494 + else if (*(pos + 1) != '2') {
495 + fprintf(stderr, "mailcheck: ooops, unsupported experimental info "
496 + "semantics on %s/%s\n", dir, entry->d_name);
497 + continue;
499 + else if (strchr(pos, 'S') == NULL) {
500 + /* search for seen ('S') flag */
501 + (*unread)++;
503 + else {
504 + (*read)++;
508 + closedir(mdir);
510 + return 0;
513 +/* Count mails in pop3 mailbox. */
515 check_pop3 (char *path, int *new_p, int *cur_p)
517 @@ -314,6 +526,7 @@
518 return 0;
521 +/* Count mails in imap mailbox. */
523 check_imap (char *path, int *new_p, int *cur_p)
525 @@ -330,13 +543,15 @@
526 port = getnetinfo (path, hostname, box, user, pass);
527 if (port == 0)
529 - fprintf (stderr, "mailcheck: Unable to get login information for %s\n", path);
530 + fprintf (stderr, "mailcheck: Unable to get login information for %s\n",
531 + path);
532 return 1;
535 if ((fd = sock_connect (hostname, port)) == -1)
537 - fprintf (stderr, "mailcheck: Not Connected To Server '%s:%d'\n", hostname, port);
538 + fprintf (stderr, "mailcheck: Not Connected To Server '%s:%d'\n",
539 + hostname, port);
540 return 1;
543 @@ -397,103 +612,243 @@
544 return 0;
548 +/* Check for mail in given mail path (could be mbox, maildir, pop3 or imap). */
549 void
550 check_for_mail (char *tmppath)
552 struct stat st;
553 - char *mailpath = expand_envstr (tmppath);
554 - char maildir[BUF_SIZE];
555 - int new = 0, cur = 0;
556 - int retval = 1;
558 - if (!stat (mailpath, &st))
560 - /* Is it a maildir? */
561 - if (S_ISDIR (st.st_mode))
563 - sprintf (maildir, "%s/cur", mailpath);
564 - cur = count_entries (maildir);
565 - sprintf (maildir, "%s/new", mailpath);
566 - new = count_entries (maildir);
568 - if ((cur < 0) || (new < 0))
570 - fprintf (stderr,
571 - "mailcheck: %s is not a valid maildir -- skipping.\n",
572 - mailpath);
573 - return;
576 - if (cur && new)
578 - printf ("You have %d new and %d saved messages in %s\n",
579 - new, cur, mailpath);
580 - return;
583 - if (cur)
585 - printf ("You have %d saved messages in %s\n", cur, mailpath);
586 - return;
589 - if (new)
591 - printf ("You have %d new messages in %s\n", new, mailpath);
592 - return;
594 + char *mailpath;
595 + int brief_name_offset= 0;
597 + /* expand environment variables in path specifier */
598 + mailpath= expand_envstr (tmppath);
600 + /* in brief mode, print relative paths for mailboxes/maildirs inside home
601 + * directory */
602 + if (Options.brief_mode &&
603 + strncmp(mailpath, Homedir, strlen(Homedir)) == 0) {
604 + brief_name_offset= strlen(Homedir) + 1;
607 + if (!stat (mailpath, &st)) {
608 + /* Is it regular file? (if yes, it should be mailbox ;) */
609 + if (S_ISREG (st.st_mode)) {
610 + /* Use advanced counting? */
611 + if (!Options.advanced_count) {
612 + if (st.st_size != 0) {
613 + if (!Options.brief_mode) {
614 + printf ("You have %smail in %s\n",
615 + (st.st_mtime > st.st_atime) ? "new " : "", mailpath);
616 + } else {
617 + printf ("%s: %smail message(s)\n", mailpath + brief_name_offset,
618 + (st.st_mtime > st.st_atime) ? "new " : "contains saved ");
620 + have_mail= 1;
622 + } else { /* advanced count */
623 + int new, read, unread;
624 + if (check_mbox(mailpath, &new, &read, &unread) == -1)
625 + return;
627 - /* It's an mbox. */
628 - else if (st.st_size != 0)
629 - printf ("You have %smail in %s\n",
630 - (st.st_mtime > st.st_atime) ? "new " : "", mailpath);
631 + if (!Options.brief_mode) {
632 + if (new > 0 && unread > 0) {
633 + printf("You have %d new and %d unread messages in %s\n",
634 + new, unread, mailpath);
635 + have_mail= 1;
636 + } else if (new > 0) {
637 + printf("You have %d new messages in %s\n",
638 + new, mailpath);
639 + have_mail= 1;
640 + } else if (unread > 0) {
641 + printf("You have %d unread messages in %s\n",
642 + unread, mailpath);
643 + have_mail= 1;
646 + else {
647 + if (new > 0 && unread > 0) {
648 + printf("%s: %d new and %d unread message(s)\n",
649 + mailpath + brief_name_offset, new, unread);
650 + have_mail= 1;
651 + } else if (new > 0) {
652 + printf("%s: %d new message(s)\n",
653 + mailpath + brief_name_offset, new);
654 + have_mail= 1;
655 + } else if (unread > 0) {
656 + printf("%s: no new mail, %d unread message(s)\n",
657 + mailpath + brief_name_offset, unread);
658 + have_mail= 1;
663 - else
665 - /* Is it POP3 or IMAP? */
666 - if (!strncmp (mailpath, "pop3:", 5))
667 - retval = check_pop3 (mailpath, &new, &cur);
668 - else if (!strncmp (mailpath, "imap:", 5))
669 - retval = check_imap (mailpath, &new, &cur);
671 - if (!retval)
673 - if (cur && new)
674 + /* Is it directory? (if yes, it should be maildir ;) */
675 + /* for maildir specification, see: http://cr.yp.to/proto/maildir.html */
676 + else if (S_ISDIR (st.st_mode)) {
677 + if (!Options.advanced_count) { /* use old counting method */
678 + int new, cur;
680 + if (check_maildir_old(mailpath, &new, &cur) == -1) {
681 + fprintf (stderr,
682 + "mailcheck: %s is not a valid maildir -- skipping.\n", mailpath);
683 + return;
686 + if (!Options.brief_mode) { /* traditional output */
687 + if (cur > 0 && new > 0) {
688 printf ("You have %d new and %d saved messages in %s\n",
689 - new, cur, mailpath);
690 - else if (cur)
691 - printf ("You have %d saved messages in %s\n", cur, mailpath);
692 - else if (new)
693 + new, cur, mailpath);
694 + have_mail= 1;
695 + } else if (new > 0) {
696 printf ("You have %d new messages in %s\n", new, mailpath);
697 + have_mail= 1;
698 + } else if (cur > 0) {
699 + printf ("You have %d saved messages in %s\n", cur, mailpath);
700 + have_mail= 1;
702 + } else { /* brief output */
703 + if (cur > 0 && new > 0) {
704 + printf ("%s: %d new and %d saved message(s)\n",
705 + mailpath + brief_name_offset, new, cur);
706 + have_mail= 1;
707 + } else if (new > 0) {
708 + printf ("%s: %d new message(s)\n",
709 + mailpath + brief_name_offset, new);
710 + have_mail= 1;
711 + } else if (cur > 0) {
712 + printf ("%s: %d saved message(s)\n",
713 + mailpath + brief_name_offset, cur);
714 + have_mail= 1;
719 + }
720 + else { /* new counting method */
721 + int new, read, unread;
723 + if (check_maildir(mailpath, &new, &read, &unread) == -1) {
724 + fprintf (stderr,
725 + "mailcheck: %s is not a valid maildir -- skipping.\n", mailpath);
726 + return;
729 + if (!Options.brief_mode) {
730 + if (new > 0 && unread > 0) {
731 + printf("You have %d new and %d unread messages in %s\n",
732 + new, unread, mailpath);
733 + have_mail= 1;
734 + } else if (new > 0) {
735 + printf("You have %d new messages in %s\n",
736 + new, mailpath);
737 + have_mail= 1;
738 + } else if (unread > 0) {
739 + printf("You have %d unread messages in %s\n",
740 + unread, mailpath);
741 + have_mail= 1;
744 + else {
745 + if (new > 0 && unread > 0) {
746 + printf("%s: %d new and %d unread message(s)\n",
747 + mailpath + brief_name_offset, new, unread);
748 + have_mail= 1;
749 + } else if (new > 0) {
750 + printf("%s: %d new message(s)\n",
751 + mailpath + brief_name_offset, new);
752 + have_mail= 1;
753 + } else if (unread > 0) {
754 + printf("%s: no new mail, %d unread message(s)\n",
755 + mailpath + brief_name_offset, unread);
756 + have_mail= 1;
760 + } else {
761 + fprintf(stderr, "mailcheck: error, %s is not mbox or maildir\n",
762 + mailpath);
765 + else if (strncmp(mailpath, "pop3:", 5) == 0 ||
766 + strncmp(mailpath, "imap:", 5) == 0) {
767 + int retval= 1;
768 + int new= 0, cur= 0;
770 + /* Is it POP3 or IMAP? */
771 + if (!strncmp (mailpath, "pop3:", 5))
772 + retval = check_pop3 (mailpath, &new, &cur);
773 + else
774 + retval = check_imap (mailpath, &new, &cur);
776 + if (retval)
777 + return;
779 + if (!Options.brief_mode) { /* traditional output */
780 + if (cur > 0 && new > 0) {
781 + printf ("You have %d new and %d saved messages in %s\n",
782 + new, cur, mailpath);
783 + have_mail= 1;
784 + } else if (new > 0) {
785 + printf ("You have %d new messages in %s\n", new, mailpath);
786 + have_mail= 1;
787 + } else if (cur > 0) {
788 + printf ("You have %d saved messages in %s\n", cur, mailpath);
789 + have_mail= 1;
791 + } else { /* brief output */
792 + if (cur > 0 && new > 0) {
793 + printf ("%s: %d new and %d saved message(s)\n",
794 + mailpath + brief_name_offset, new, cur);
795 + have_mail= 1;
796 + } else if (new > 0) {
797 + printf ("%s: %d new message(s)\n",
798 + mailpath + brief_name_offset, new);
799 + have_mail= 1;
800 + } else if (cur > 0) {
801 + printf ("%s: %d saved message(s)\n",
802 + mailpath + brief_name_offset, cur);
803 + have_mail= 1;
807 + else {
808 + fprintf(stderr, "mailcheck: invalid line '%s' in rc-file\n", mailpath);
812 /* Process command-line options */
813 void
814 process_options (int argc, char *argv[])
816 - int result;
818 - while (1)
819 + int opt;
821 + while ((opt= getopt(argc, argv, "bchlsf:")) != -1)
823 - result = getopt (argc, argv, "l");
825 - switch (result)
826 + switch (opt)
828 - case EOF:
829 - return;
830 + case 'b':
831 + Options.brief_mode= 1;
832 + break;
833 + case 'c':
834 + Options.advanced_count= 1;
835 + break;
836 + case 'h':
837 + print_usage();
838 + exit(0);
839 + break;
840 case 'l':
841 - Login_mode = 1;
842 + Options.login_mode= 1;
843 + break;
844 + case 's':
845 + Options.show_summary= 1;
846 + break;
847 + case 'f':
848 + Options.rcfile_path= optarg;
849 break;
854 +/* main */
856 main (int argc, char *argv[])
858 @@ -501,43 +856,53 @@
859 FILE *rcfile;
860 struct stat st;
862 - Homedir = getenv ("HOME");
863 - if (!Homedir)
865 - fprintf (stderr, "%s: couldn't read environment variable HOME.\n",
866 - argv[0]);
867 - return 1;
869 + ptr= getenv ("HOME");
870 + if (!ptr) {
871 + fprintf (stderr, "mailcheck: couldn't read environment variable HOME.\n");
872 + return 1;
873 + } else {
874 + Homedir= strdup(ptr);
877 process_options (argc, argv);
879 - if (Login_mode)
880 + if (Options.login_mode)
882 - /* If we can stat .hushlogin successfully, we should exit. */
883 + /* If we can stat .hushlogin successfully and it is regular file, we
884 + * should exit. */
885 snprintf (buf, sizeof (buf), "%s/.hushlogin", Homedir);
886 - if (!stat (buf, &st))
887 + if (!stat (buf, &st) && S_ISREG(st.st_mode))
888 return 0;
891 - rcfile = open_rcfile ();
892 - if (!rcfile)
894 - fprintf (stderr, "%s: couldn't open /etc/mailcheckrc "
895 - "or %s/.mailcheckrc.\n", argv[0], Homedir);
896 - return 1;
898 + rcfile= open_rcfile();
900 while (fgets (buf, sizeof (buf), rcfile))
902 /* eliminate newline */
903 ptr = strchr (buf, '\n');
904 if (ptr)
905 - *ptr = 0;
906 + *ptr = '\0';
908 /* If it's not a blank line or comment, look for mail in it */
909 if (strlen (buf) && (*buf != '#'))
910 check_for_mail (buf);
913 + if (Options.show_summary && !have_mail) {
914 + if (Options.brief_mode) {
915 + printf("no new mail\n");
917 + else {
918 + printf("No new mail.\n");
922 + fclose(rcfile);
923 + free(Homedir);
925 return 0;
928 +/* vim:set ts=8 sw=2: */
930 diff -ruN mailcheck-1.91.1/mailcheckrc mailcheck-1.91.1+2/mailcheckrc
931 --- mailcheck-1.91.1/mailcheckrc 2001-05-11 21:58:56.000000000 +0200
932 +++ mailcheck-1.91.1+2/mailcheckrc 2005-06-30 21:48:17.000000000 +0200
933 @@ -10,6 +10,9 @@
935 # If you're using qmail's Maildir feature, you'll probably want to
936 # enable this line:
937 +#$(HOME)/Maildir/
939 +# For qmail's mbox file in user's home directory:
940 #$(HOME)/Mailbox
942 # Mailcheck also supports remote POP3 and IMAP mailboxes. Most users
943 @@ -19,11 +22,15 @@
944 # If you have a remote POP3 mailbox, use a line like the following
945 # if your username is the same there as here.
946 #pop3://servername
948 # If your POP3 username is different there than here:
949 #pop3://username@servernameint
951 # In either case, you need to put an entry in $HOME/.netrc for the password.
952 # .netrc is in the form:
954 # machine mail.example.com login rmf1 password MyPasWrd
956 # where mail.example.com, rmf1, and MyPasWrd are the values for your account,
957 # and machine, login, and password are literal text in the file.
959 diff -ruN mailcheck-1.91.1/netrc.c mailcheck-1.91.1+2/netrc.c
960 --- mailcheck-1.91.1/netrc.c 2001-05-11 22:16:25.000000000 +0200
961 +++ mailcheck-1.91.1+2/netrc.c 2005-06-30 21:36:54.000000000 +0200
962 @@ -228,8 +228,8 @@
963 premature_token);
964 #else
965 fprintf (stderr,
967 - ("mailcheck: %s:%d: warning: found \"%s\" before any host names\n"),
968 + _("mailcheck: %s:%d: warning: found \"%s\" before any "
969 + "host names\n"),
970 file, ln, premature_token);
971 #endif
972 premature_token = NULL;
973 @@ -269,8 +269,8 @@
974 else
976 fprintf (stderr,
977 - _("mailcheck: %s:%d: warning: unknown token \"%s\"\n"), file,
978 - ln, tok);
979 + _("mailcheck: %s:%d: warning: unknown token "
980 + "\"%s\"\n"), file, ln, tok);