-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailcheck.c
938 lines (830 loc) · 25.4 KB
/
mailcheck.c
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/* mailcheck.c
*
* Copyright 1996, 1997, 1998, 2001 Jefferson E. Noxon <[email protected]>
* 2001 Rob Funk <[email protected]>
* 2003, 2005 Tomas Hoger <[email protected]>
* 2019 Ryan Dotson <[email protected]>
*
* This file may be copied under the terms of the GNU Public License
* version 2, incorporated herein by reference.
*/
/* Command line parameters:
* -l: login mode; program exits quietly if ~/.hushlogin exists.
* -b: brief mode; less verbose output mode
* -c: use more advanced counting method
* -s: print "no mail" summary if needed
* -f: specify alternative rc file location
* -h: print usage
* -n: nopath mode, more brief than brief; not useful with multiple accounts
*/
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "netrc.h"
#define BUF_SIZE (2048)
extern int sock_connect(char *hostname, int port);
/* Global variables */
char *Homedir; /* Home directory pathname */
unsigned short have_mail = 0; /* Any mail found? */
struct {
unsigned short login_mode; /* see '-l' option */
unsigned short brief_mode; /* see '-b' option */
unsigned short nopath_mode; /* see '-n' option */
unsigned short advanced_count; /* see '-c' option */
unsigned short show_summary; /* see '-s' option */
char *rcfile_path; /* see '-f' option */
} Options = {0, 0, 0, 0, 0, NULL};
/* Print usage information. */
void print_usage(void) {
printf("Usage: mailcheck [-bchls] [-f rcfile]\n"
"\n"
"Options:\n"
" -b - brief output mode\n"
" -n - brief output with no paths\n (not useful with multiple "
"accounts)\n"
" -c - use more advanced counting method for mboxes and maildirs\n"
" -l - login mode, honor ~/.hushlogin file\n"
" -s - show \"no mail\" summary, if no new mail was found\n"
" -f - specify alternative rcfile location\n"
" -h - show this help screen\n"
"\n");
}
/* Open an rc file. Exit with error message, if attempt to open rcfile failed.
* Otherwise, return valid FILE* .
*/
FILE *open_rcfile(void) {
char namebuf[256];
FILE *rcfile;
/* if rcfile path was provided, do not try default locations */
if (Options.rcfile_path != NULL) {
if ((rcfile = fopen(Options.rcfile_path, "r")) == NULL) {
fprintf(stderr, "error: couldn't open rcfile '%s'\n",
Options.rcfile_path);
exit(1);
}
} else {
snprintf(namebuf, sizeof(namebuf), "%s/.mailcheckrc", Homedir);
if ((rcfile = fopen(namebuf, "r")) == NULL) {
if ((rcfile = fopen("/etc/mailcheckrc", "r")) == NULL) {
fprintf(stderr,
"mailcheck: couldn't open /etc/mailcheckrc "
"nor %s/.mailcheckrc\n",
Homedir);
exit(1);
}
}
}
return rcfile;
}
/* Expand environment variables. Input buffer must be long enough
* to hold output. Nested $()'s don't work.
*/
char *expand_envstr(char *path) {
char *srcptr, *envptr, *tmpptr, *dup, *envname;
int len;
srcptr = strstr(path, "$(");
if (!srcptr)
return path;
len = strlen(path);
if (!len)
return path;
dup = malloc(len + 1);
if (!dup)
return path;
strcpy(dup, path);
envptr = strstr(dup, "$(") + 2;
tmpptr = strchr(envptr, ')');
if (!tmpptr) {
free(dup);
return path;
}
*tmpptr = 0;
*srcptr = 0;
envname = getenv(envptr);
if (envname)
strcat(srcptr, envname);
strcat(srcptr, tmpptr + 1);
free(dup);
return expand_envstr(path);
}
/* Should entry in maildir be ignored? */
inline int ignore_maildir_entry(const char *dir, const struct dirent *entry) {
char fname[BUF_SIZE];
struct stat filestat;
/* *all* dotfiles should be ignored in maildir, not only . and .. ! */
if (entry->d_name[0] == '.')
return 1;
/* also count only regular files
* use dirent's d_type if possible, otherwise stat file (which is much
* slower) */
#ifdef _DIRENT_HAVE_D_TYPE
if (entry->d_type != DT_UNKNOWN) {
if (entry->d_type != DT_REG)
return 1;
} else {
#endif /* _DIRENT_HAVE_D_TYPE */
snprintf(fname, sizeof(fname), "%s/%s", dir, entry->d_name);
fname[sizeof(fname) - 1] = '\0';
if (stat(fname, &filestat) != 0) {
fprintf(stderr, "mailcheck: failed to stat file: %s\n", fname);
return 1;
}
if (!S_ISREG(filestat.st_mode))
return 1;
#ifdef _DIRENT_HAVE_D_TYPE
}
#endif /* _DIRENT_HAVE_D_TYPE */
return 0;
}
/* Count files in subdir of maildir (new/cur/tmp). */
int count_entries(char *path) {
DIR *mdir;
struct dirent *entry;
int count = 0;
if ((mdir = opendir(path)) == NULL)
return -1;
while ((entry = readdir(mdir))) {
if (ignore_maildir_entry(path, entry))
continue;
count++;
}
closedir(mdir);
return count;
}
/* Get password for given account on given host from ~/.netrc file. */
char *getpw(char *host, char *account) {
char file[256];
struct stat sb;
netrc_entry *head, *a;
snprintf(file, sizeof(file), "%s/.netrc", Homedir);
if (stat(file, &sb))
return 0;
if (sb.st_mode & 077) {
static int issued_warning = 0;
if (!issued_warning++)
fprintf(stderr,
"mailcheck: WARNING! %s may be readable by other users.\n"
"mailcheck: Type \"chmod 0600 %s\" to correct the permissions.\n",
file, file);
}
head = parse_netrc(file);
if (!head) {
static int issued_warning = 0;
if (!issued_warning++)
fprintf(stderr, "mailcheck: WARNING! %s could not be read.\n", file);
return 0;
}
if (host && account) {
a = search_netrc(head, host, account);
if (a && a->password)
return (a->password);
}
return 0;
}
/* returns port number, or zero on error */
/* returns hostname, box, user, and pass through pointers */
int getnetinfo(const char *path, char *hostname, char *box, char *user,
char *pass) {
char buf[BUF_SIZE];
int port = 0;
char *p, *q, *h, *proto;
strncpy(buf, path, BUF_SIZE - 1);
/* first separate "protocol:" part */
p = strchr(buf, ':');
if (!p)
return (0);
*p = '\0';
proto = buf;
h = p + 1;
if (!strcmp(proto, "pop3"))
port = 110;
else if (!strcmp(proto, "imap"))
port = 143;
/* handle "pop3://hostname" form */
while (*h == '/')
h++;
/* change "hostname/" or "hostname/something" to "hostname" */
p = strchr(h, '/');
if (p) {
*p = '\0';
p++;
if (*p != '\0')
strncpy(box, p, BUF_SIZE - 1);
else
strcpy(box, "INBOX");
} else
strcpy(box, "INBOX");
/* determine username -- look for user@hostname, else use USER */
p = strrchr(h, '@');
if (p) {
*p = '\0';
p++;
q = h;
h = p;
} else {
/* default to getenv("USER") */
q = getenv("USER");
if (!q)
return (0);
}
strncpy(user, q, 127);
/* check for port specification */
p = strchr(h, ':');
if (p) {
*p = '\0';
p++;
if (isdigit(*p)) {
int n = atoi(p);
if (n > 0)
port = n;
}
}
strncpy(hostname, h, 127);
/* get password for this hostname and username from $HOME/.netrc */
p = getpw(hostname, user);
if (p)
strncpy(pass, p, 127);
return (port);
}
/* Count mails in unix mbox. */
int check_mbox(const char *path, int *new, int *read, int *unread) {
char linebuf[BUF_SIZE];
FILE *mbox;
int linelen;
unsigned short in_header = 0; /* do we parse mail header or mail body? */
if ((mbox = fopen(path, "r")) == NULL) {
fprintf(stderr, "mailcheck: unable to open mbox %s\n", path);
return -1;
}
*new = 0;
*read = 0;
*unread = 0;
while (fgets(linebuf, sizeof(linebuf), mbox)) {
if (!in_header) {
if (strncmp(linebuf, "From ", 5) == 0) { /* 5 == strlen("From ") */
in_header = 1;
(*new)++;
}
} else {
if (linebuf[0] == '\n') {
in_header = 0;
} else if (strncmp(linebuf, "Status: ", 8) ==
0) { /* 8 == strlen("Status: ") */
linelen = strlen(linebuf);
if (linelen >= 10 && ((linebuf[8] == 'R' && linebuf[9] == 'O') ||
(linebuf[8] == 'O' && linebuf[9] == 'R'))) {
(*new)--;
(*read)++;
} else if (linelen >= 9 && linebuf[8] == 'O') {
(*new)--;
(*unread)++;
}
}
}
}
fclose(mbox);
return 0;
}
/* Count mails in maildir. Slightely modified original Jeff's version. Just
* counts files in maildir/new and maildir/cur. */
int check_maildir_old(const char *path, int *new, int *cur) {
char dir[BUF_SIZE];
snprintf(dir, sizeof(dir), "%s/new", path);
*new = count_entries(dir);
snprintf(dir, sizeof(dir), "%s/cur", path);
*cur = count_entries(dir);
if (*new == -1 || *cur == -1)
return -1;
else
return 0;
}
/* Count mails in maildir. Newer, more sophisticated, but also more time
* consuming version. */
int check_maildir(const char *path, int *new, int *read, int *unread) {
char dir[BUF_SIZE];
DIR *mdir;
struct dirent *entry;
char *pos;
/* new mail - standard way */
snprintf(dir, sizeof(dir), "%s/new", path);
*new = count_entries(dir);
if (*new == -1)
return -1;
/* older mail - check also mail status */
snprintf(dir, sizeof(dir), "%s/cur", path);
if ((mdir = opendir(dir)) == NULL)
return -1;
*read = 0;
*unread = 0;
while ((entry = readdir(mdir))) {
if (ignore_maildir_entry(dir, entry))
continue;
if ((pos = strchr(entry->d_name, ':')) == NULL) {
(*unread)++;
} else if (*(pos + 1) != '2') {
fprintf(stderr,
"mailcheck: ooops, unsupported experimental info "
"semantics on %s/%s\n",
dir, entry->d_name);
continue;
} else if (strchr(pos, 'S') == NULL) {
/* search for seen ('S') flag */
(*unread)++;
} else {
(*read)++;
}
}
closedir(mdir);
return 0;
}
/* Count mails in pop3 mailbox. */
int check_pop3(char *path, int *new_p, int *cur_p) {
int port;
int fd;
FILE *fp;
char buf[BUF_SIZE];
char hostname[BUF_SIZE];
char box[BUF_SIZE]; /* not actually used for pop3 */
char user[128];
char pass[128];
int total = 0;
port = getnetinfo(path, hostname, box, user, pass);
/* connect to host */
if ((fd = sock_connect(hostname, port)) == -1)
return 1;
fp = fdopen(fd, "r+");
fgets(buf, BUF_SIZE, fp);
fflush(fp);
fprintf(fp, "USER %s\r\n", user);
fflush(fp);
fgets(buf, BUF_SIZE, fp);
if (buf[0] != '+') {
fprintf(stderr, "mailcheck: Invalid User Name '%s@%s:%d'\n", user, hostname,
port);
#ifdef DEBUG_POP3
fprintf(stderr, "%s\n", buf);
#endif
fprintf(fp, "QUIT\r\n");
fclose(fp);
return 1;
};
fflush(fp);
fprintf(fp, "PASS %s\r\n", pass);
fflush(fp);
fgets(buf, BUF_SIZE, fp);
if (buf[0] != '+') {
fprintf(stderr, "mailcheck: Incorrect Password for user '%s@%s:%d'\n", user,
hostname, port);
fprintf(stderr, "mailcheck: Server said %s", buf);
fprintf(fp, "QUIT\r\n");
fclose(fp);
return 1;
};
fflush(fp);
fprintf(fp, "STAT\r\n");
fflush(fp);
fgets(buf, BUF_SIZE, fp);
if (buf[0] != '+') {
fprintf(stderr, "mailcheck: Error Receiving STAT '%s@%s:%d'\n", user,
hostname, port);
return 1;
} else {
sscanf(buf, "+OK %d", &total);
}
fflush(fp);
fprintf(fp, "LAST\r\n");
fflush(fp);
fgets(buf, BUF_SIZE, fp);
if (buf[0] != '+') {
/* Server does not support LAST. Assume total as new */
*new_p = total;
*cur_p = 0;
} else {
sscanf(buf, "+OK %d", cur_p);
*new_p = total - *cur_p;
}
fprintf(fp, "QUIT\r\n");
fclose(fp);
return 0;
}
/* Count mails in imap mailbox. */
int check_imap(char *path, int *new_p, int *cur_p) {
int port;
int fd;
FILE *fp;
char buf[BUF_SIZE];
char hostname[BUF_SIZE];
char box[BUF_SIZE];
char user[128];
char pass[128];
int total = 0;
port = getnetinfo(path, hostname, box, user, pass);
if (port == 0) {
fprintf(stderr, "mailcheck: Unable to get login information for %s\n",
path);
return 1;
}
if ((fd = sock_connect(hostname, port)) == -1) {
fprintf(stderr, "mailcheck: Not Connected To Server '%s:%d'\n", hostname,
port);
return 1;
}
fp = fdopen(fd, "r+");
fgets(buf, BUF_SIZE, fp);
/* Login to the server */
fflush(fp);
fprintf(fp, "a001 LOGIN %s %s\r\n", user, pass);
/* Ensure that the buffer is not an informational line */
do {
fflush(fp);
fgets(buf, BUF_SIZE, fp);
} while (buf[0] == '*');
if (buf[5] != 'O') { /* Looking for "a001 OK" */
fprintf(fp, "a002 LOGOUT\r\n");
fclose(fp);
fprintf(stderr, "mailcheck: Unable to check IMAP mailbox '%s@%s:%d'\n",
user, hostname, port);
fprintf(stderr, "mailcheck: Server said %s", buf);
return 1;
};
fflush(fp);
fprintf(fp, "a003 STATUS %s (MESSAGES UNSEEN)\r\n", box);
fflush(fp);
fgets(buf, BUF_SIZE, fp);
if (buf[0] != '*') { /* Looking for "* STATUS ..." */
fprintf(stderr, "mailcheck: Error Receiving Stats '%s@%s:%d'\n\t%s\n", user,
hostname, port, buf);
fclose(fp);
return 1;
} else {
sscanf(buf, "* STATUS %*s (MESSAGES %d UNSEEN %d)", &total, new_p);
#ifdef DEBUG_IMAP4
fprintf(stderr, "[%s:%d] %s", __FILE__, __LINE__, buf);
#endif
fgets(buf, BUF_SIZE, fp);
#ifdef DEBUG_IMAP4
fprintf(stderr, "[%s:%d] %s", __FILE__, __LINE__, buf);
#endif
*cur_p = total - *new_p;
}
fflush(fp);
fprintf(fp, "a004 LOGOUT\r\n");
fclose(fp);
return 0;
}
/* Check for mail in given mail path (could be mbox, maildir, pop3 or imap). */
void check_for_mail(char *tmppath) {
struct stat st;
char *mailpath;
int brief_name_offset = 0;
char *new_plural = "";
char *cur_plural = "";
char *unread_plural = "";
/* expand environment variables in path specifier */
mailpath = expand_envstr(tmppath);
/* in brief mode, print relative paths for mailboxes/maildirs inside home
* directory */
if (Options.brief_mode && strncmp(mailpath, Homedir, strlen(Homedir)) == 0) {
brief_name_offset = strlen(Homedir) + 1;
}
if (!stat(mailpath, &st)) {
/* Is it regular file? (if yes, it should be mailbox ;) */
if (S_ISREG(st.st_mode)) {
/* Use advanced counting? */
if (!Options.advanced_count) {
if (st.st_size != 0) {
if (!Options.brief_mode) {
printf("You have %smail in %s\n",
(st.st_mtime > st.st_atime) ? "new " : "", mailpath);
} else {
printf("%s: %smail message(s)\n", mailpath + brief_name_offset,
(st.st_mtime > st.st_atime) ? "new " : "contains saved ");
}
have_mail = 1;
}
} else { /* advanced count */
int new, read, unread;
if (check_mbox(mailpath, &new, &read, &unread) == -1)
return;
/* rd: plurals */
if (new > 1) {
new_plural = "s";
}
if (unread > 1) {
unread_plural = "s";
}
if (Options.brief_mode) {
if (new > 0 && unread > 0) {
printf("%s: %d new message%s and %d unread message%s\n",
mailpath + brief_name_offset, new, new_plural, unread,
unread_plural);
have_mail = 1;
} else if (new > 0) {
printf("%s: %d new message%s\n", mailpath + brief_name_offset, new,
new_plural);
have_mail = 1;
} else if (unread > 0) {
printf("%s: no new mail, %d unread message%s\n",
mailpath + brief_name_offset, unread, unread_plural);
have_mail = 1;
}
} else if (Options.nopath_mode) {
if (unread > 0 && new > 0) {
printf("%d new message%s and %d saved message%s.\n", new,
new_plural, unread, unread_plural);
have_mail = 1;
} else if (new > 0) {
printf("%d new message%s.\n", new, new_plural);
have_mail = 1;
} else if (unread > 0) {
printf("%d saved message%s.\n", unread, unread_plural);
have_mail = 1;
}
} else { /*traditional*/
if (new > 0 && unread > 0) {
printf("You have %d new and %d unread messages in %s\n", new,
unread, mailpath);
have_mail = 1;
} else if (new > 0) {
printf("You have %d new messages in %s\n", new, mailpath);
have_mail = 1;
} else if (unread > 0) {
printf("You have %d unread messages in %s\n", unread, mailpath);
have_mail = 1;
}
}
}
}
/* Is it directory? (if yes, it should be maildir ;) */
/* for maildir specification, see: http://cr.yp.to/proto/maildir.html */
else if (S_ISDIR(st.st_mode)) {
if (!Options.advanced_count) { /* use old counting method */
int new, cur;
if (check_maildir_old(mailpath, &new, &cur) == -1) {
fprintf(stderr, "mailcheck: %s is not a valid maildir -- skipping.\n",
mailpath);
return;
}
/* rd: plurals */
if (new > 1) {
new_plural = "s";
}
if (cur > 1) {
cur_plural = "s";
}
if (Options.brief_mode) { /* brief output */
if (cur > 0 && new > 0) {
printf("%s: %d new message%s and %d saved message%s\n",
mailpath + brief_name_offset, new, new_plural, cur,
cur_plural);
have_mail = 1;
} else if (new > 0) {
printf("%s: %d new message%s\n", mailpath + brief_name_offset, new,
new_plural);
have_mail = 1;
} else if (cur > 0) {
printf("%s: %d saved message%s\n", mailpath + brief_name_offset,
cur, cur_plural);
have_mail = 1;
}
} // end if brief mode
else if (Options.nopath_mode) { /* nopath mode */
if (cur > 0 && new > 0) {
printf("%d new message%s and %d saved message%s.\n", new,
new_plural, cur, cur_plural);
have_mail = 1;
} else if (new > 0) {
printf("%d new message%s.\n", new, new_plural);
have_mail = 1;
} else if (cur > 0) {
printf("%d saved message%s.\n", cur, cur_plural);
have_mail = 1;
}
} // end if nopath mode
else { /* traditional output */
if (cur > 0 && new > 0) {
printf("You have %d new message%s and %d saved message%s in %s\n",
new, new_plural, cur, cur_plural, mailpath);
have_mail = 1;
} else if (new > 0) {
printf("You have %d new message%s in %s\n", new, new_plural,
mailpath);
have_mail = 1;
} else if (cur > 0) {
printf("You have %d saved message%s in %s\n", cur, cur_plural,
mailpath);
have_mail = 1;
}
} // end traditional mode
} // end old counting method
else { /* new counting method */
int new, read, unread;
if (check_maildir(mailpath, &new, &read, &unread) == -1) {
fprintf(stderr, "mailcheck: %s is not a valid maildir -- skipping.\n",
mailpath);
return;
}
/* rd: plurals */
if (new > 1) {
new_plural = "s";
}
if (unread > 1) {
unread_plural = "s";
}
if (!Options.brief_mode &&
!Options.nopath_mode) { /* traditional output */
if (new > 0 && unread > 0) {
printf("You have %d new message%s and %d unread message%s in %s\n",
new, new_plural, unread, unread_plural, mailpath);
have_mail = 1;
} else if (new > 0) {
printf("You have %d new message%s in %s\n", new, new_plural,
mailpath);
have_mail = 1;
} else if (unread > 0) {
printf("You have %d unread message%s in %s\n", unread,
unread_plural, mailpath);
have_mail = 1;
}
} else if (Options.brief_mode) { /* brief output */
if (new > 0 && unread > 0) {
printf("%s: %d new message%s and %d unread message%s\n",
mailpath + brief_name_offset, new, new_plural, unread,
unread_plural);
have_mail = 1;
} else if (new > 0) {
printf("%s: %d new message%s\n", mailpath + brief_name_offset, new);
have_mail = 1;
} else if (unread > 0) {
printf("%s: no new mail, %d unread message%s\n",
mailpath + brief_name_offset, unread, unread_plural);
have_mail = 1;
}
} else if (Options.nopath_mode) { /* rd: nopath mode */
if (unread > 0 && new > 0) {
printf("%d new message%s and %d unread message%s.\n", new,
new_plural, unread, unread_plural);
have_mail = 1;
} else if (new > 0) {
printf("%d new message%s.\n", new, new_plural);
have_mail = 1;
} else if (unread > 0) {
printf("No new mail, %d unread message%s.\n", unread,
unread_plural);
have_mail = 1;
}
} else {
fprintf(stderr, "mailcheck: error, %s is not mbox or maildir\n",
mailpath);
}
} /* end if new counting method */
} else if (strncmp(mailpath, "pop3:", 5) == 0 ||
strncmp(mailpath, "imap:", 5) == 0) { /* if pop3 or imap */
int retval = 1;
int new = 0, cur = 0;
/* Is it POP3 or IMAP? */
if (!strncmp(mailpath, "pop3:", 5))
retval = check_pop3(mailpath, &new, &cur);
else
retval = check_imap(mailpath, &new, &cur);
if (retval)
return;
/* rd: plurals */
char *new_plural = "";
char *cur_plural = "";
if (new > 1) {
new_plural = "s";
}
if (cur > 1) {
cur_plural = "s";
}
if (!Options.brief_mode &&
!Options.nopath_mode) { /* traditional output */
if (cur > 0 && new > 0) {
printf("You have %d new message%s and %d saved message%s in %s\n",
new, new_plural, cur, cur_plural, mailpath);
have_mail = 1;
} else if (new > 0) {
printf("You have %d new message%s in %s\n", new, new_plural,
mailpath);
have_mail = 1;
} else if (cur > 0) {
printf("You have %d saved message%s in %s\n", cur, cur_plural,
mailpath);
have_mail = 1;
}
} else if (Options.brief_mode) { /* brief output */
if (cur > 0 && new > 0) {
printf("%s: %d new message%s and %d saved message%s.\n",
mailpath + brief_name_offset, new, new_plural, cur,
cur_plural);
have_mail = 1;
} else if (new > 0) {
printf("%s: %d new message%s.\n", mailpath + brief_name_offset, new,
new_plural);
have_mail = 1;
} else if (cur > 0) {
printf("%s: %d saved message%s.\n", mailpath + brief_name_offset, cur,
cur_plural);
have_mail = 1;
}
} else if (Options.nopath_mode) {
if (cur > 0 && new > 0) {
printf("%d new message%s and %d saved message%s.\n", new, new_plural,
cur, cur_plural);
have_mail = 1;
} else if (new > 0) {
printf("%d new message%s.\n", new, new_plural);
have_mail = 1;
} else if (cur > 0) {
printf("%d saved message%s.\n", cur, cur_plural);
have_mail = 1;
}
}
}
else {
fprintf(stderr, "mailcheck: invalid line '%s' in rc-file\n", mailpath);
}
}
}
/* Process command-line options */
void process_options(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "bchlnsf:")) != -1) {
switch (opt) {
case 'b':
Options.brief_mode = 1;
break;
case 'c':
Options.advanced_count = 1;
break;
case 'h':
print_usage();
exit(0);
break;
case 'l':
Options.login_mode = 1;
break;
case 'n':
Options.nopath_mode = 1;
break;
case 's':
Options.show_summary = 1;
break;
case 'f':
Options.rcfile_path = optarg;
break;
}
}
}
/* main */
int main(int argc, char *argv[]) {
char buf[1024], *ptr;
FILE *rcfile;
struct stat st;
ptr = getenv("HOME");
if (!ptr) {
fprintf(stderr, "mailcheck: couldn't read environment variable HOME.\n");
return 1;
} else {
Homedir = strdup(ptr);
}
process_options(argc, argv);
if (Options.login_mode) {
/* If we can stat .hushlogin successfully and it is regular file, we
* should exit. */
snprintf(buf, sizeof(buf), "%s/.hushlogin", Homedir);
if (!stat(buf, &st) && S_ISREG(st.st_mode))
return 0;
}
rcfile = open_rcfile();
while (fgets(buf, sizeof(buf), rcfile)) {
/* eliminate newline */
ptr = strchr(buf, '\n');
if (ptr)
*ptr = '\0';
/* If it's not a blank line or comment, look for mail in it */
if (strlen(buf) && (*buf != '#'))
check_for_mail(buf);
}
if (Options.show_summary && !have_mail) {
if (Options.brief_mode) {
printf("no new mail\n");
} else {
printf("No new mail.\n");
}
}
fclose(rcfile);
free(Homedir);
return 0;
}
/* vim:set ts=8 sw=2: */