2 * Incoming Mail Checker
4 * (c) 2005--2010 Martin Mares <mj@ucw.cz>
21 #include <sys/types.h>
24 #ifdef CONFIG_WIDE_CURSES
25 #include <ncursesw/ncurses.h>
34 static int check_interval = 30;
35 static int force_refresh;
36 static int allow_bells = 1;
37 static int allow_osd = 1;
38 static int minimum_priority;
39 static time_t last_scan_time;
40 static char *run_cmd = "mutt -f %s";
69 static clist options, patterns;
70 static struct options global_options = {
83 int last_size, last_pos;
84 int total, new, flagged;
85 int last_total, last_new, last_flagged;
93 static struct mbox **mbox_array;
94 static int num_mboxes, mbox_array_size;
96 static void redraw_line(int i);
97 static void rethink_display(void);
100 add_pattern(char *patt)
102 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
103 strcpy(n->pattern, patt);
104 if (patt = strchr(n->pattern, '='))
111 clist_add_tail(&patterns, &n->n);
117 struct passwd *p = getpwuid(getuid());
119 die("You don't exist, go away!");
120 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
121 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
126 init_options(struct options *o)
130 o->hide_if_empty = -1;
134 o->show_flagged = -1;
135 o->sender_personal = -1;
143 setup_options(struct mbox *b)
145 b->o = global_options;
146 CLIST_FOR_EACH(struct option_node *, n, options)
147 if (!fnmatch(n->pattern, b->name, 0))
149 debug("\tApplied options %s\n", n->pattern);
150 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
153 MERGE(hide_if_empty);
158 MERGE(sender_personal);
167 mbox_name(char *path)
169 char *c = strrchr(path, '/');
170 return c ? (c+1) : path;
174 add_mbox(clist *l, char *path, char *name)
176 struct mbox *b = xmalloc(sizeof(*b));
177 bzero(b, sizeof(*b));
178 b->path = xstrdup(path);
179 b->name = xstrdup(name);
183 cnode *prev = l->head.prev;
184 while (prev != &l->head && strcmp(((struct mbox *)prev)->name, name) > 0)
186 clist_insert_after(&b->n, prev);
189 clist_add_tail(l, &b->n);
195 del_mbox(struct mbox *b)
204 find_mbox(clist *l, char *path)
206 CLIST_FOR_EACH(struct mbox *, b, *l)
207 if (!strcmp(b->path, path))
213 mbox_active_p(struct mbox *b)
215 if (b->o.priority < minimum_priority)
223 mbox_visible_p(struct mbox *b)
225 if (!mbox_active_p(b))
229 if (b->o.hide_if_empty && !b->total)
235 prepare_snippet(struct mbox *b, char *sender, char *subject)
237 while (*sender == ' ' || *sender == '\t')
239 while (*subject == ' ' || *subject == '\t')
242 char *pos = b->snippet;
243 char *term = b->snippet + sizeof(b->snippet) - 1;
244 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
246 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
247 add_snippet(&pos, term, ": ");
250 add_subject_snippet(&pos, term, subject);
252 add_snippet(&pos, term, "No subject");
255 static int mb_fd, mb_pos;
256 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
261 mb_cc = mb_end = mb_buf;
268 lseek(mb_fd, pos, SEEK_SET);
275 return mb_pos - (mb_end - mb_cc);
281 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
290 mb_end = mb_buf + len;
299 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
310 mb_check(const char *p, int len)
314 if (mb_get() != *p++)
321 scan_mbox(struct mbox *b, struct stat *st)
323 char buf[1024], sender[1024], subject[1024];
326 const char from[] = "\nFrom ";
330 b->total = b->new = b->flagged = 0;
335 /* FIXME: Should we do some locking? */
337 mb_fd = open(b->path, O_RDONLY);
340 debug("[open failed: %m] ");
341 b->total = b->new = b->flagged = -1;
346 c = read(mb_fd, signature, 2);
347 lseek(mb_fd, 0, SEEK_SET);
349 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
351 debug("[decompressing] ");
354 die("pipe failed: %m");
357 die("fork failed: %m");
360 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
361 die("dup2 failed: %m");
365 execlp("gzip", "gzip", "-cd", NULL);
366 die("Cannot execute gzip: %m");
376 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && !compressed)
378 mb_seek(b->last_pos);
379 if (mb_check(from, 6))
381 debug("[incremental] ");
386 debug("[incremental failed] ");
392 if (!mb_check(from+1, 5))
394 debug("[inconsistent] ");
395 b->total = b->new = b->flagged = -1;
398 b->total = b->new = b->flagged = 0;
399 b->last_total = b->last_new = b->last_flagged = 0;
400 b->snippet_is_new = 0;
404 b->total = b->last_total;
405 b->new = b->last_new;
406 b->flagged = b->last_flagged;
411 b->last_pos = mb_tell() - 5;
413 b->last_pos--; // last_pos should be the previous \n character
414 b->last_total = b->total;
415 b->last_new = b->new;
416 b->last_flagged = b->flagged;
417 while ((c = mb_get()) >= 0 && c != '\n')
432 debug("[truncated] ");
443 while (c == ' ' || c == '\t');
451 if (i < sizeof(buf) - 1)
457 if (!strncasecmp(buf, "Status:", 7))
459 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
461 else if (!strncasecmp(buf, "From:", 5))
462 strcpy(sender, buf+5);
463 else if (!strncasecmp(buf, "Subject:", 8))
464 strcpy(subject, buf+8);
472 if (new || (flagged && !b->snippet_is_new))
474 b->snippet_is_new = new;
475 prepare_snippet(b, sender, subject);
494 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
495 b->total = b->new = b->flagged = -1;
502 debug("Searching for mailboxes...\n");
503 last_scan_time = time(NULL);
504 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
506 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
508 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
509 if (err && err != GLOB_NOMATCH)
510 die("Failed to glob %s: %m", p->pattern);
511 for (uns i=0; i<g.gl_pathc; i++)
513 char *name = g.gl_pathv[i];
514 struct mbox *b = find_mbox(&mboxes, name);
517 b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
518 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
528 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
534 debug("Lost mailbox %s\n", b->name);
541 debug("Scanning mailboxes...\n");
542 CLIST_FOR_EACH(struct mbox *, b, mboxes)
545 debug("%s: ", b->name);
546 if (!mbox_active_p(b))
552 b->force_refresh = 1;
553 if (stat(b->path, &st) < 0)
555 b->total = b->new = b->flagged = -1;
558 else if (!b->last_time || st.st_mtime != b->last_time || st.st_size != b->last_size || b->force_refresh)
561 redraw_line(b->index);
565 b->last_time = st.st_mtime;
566 b->last_size = st.st_size;
567 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
570 redraw_line(b->index);
574 debug("not changed\n");
575 b->force_refresh = 0;
579 debug("Scan finished\n");
580 last_scan_time = time(NULL);
586 #include <X11/Xlib.h>
587 #include <X11/Xatom.h>
589 static Display *x11_dpy;
590 static unsigned leds_care, leds_have, leds_want;
591 static unsigned osd_care, osd_have, osd_want;
597 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
598 osd_care = (global_options.osd >= 0);
599 CLIST_FOR_EACH(struct option_node *, o, options)
602 leds_care |= (1 << o->o.led);
607 if (!leds_care && !osd_care)
609 debug("X11: No mailbox wants LEDs or OSD\n");
612 if (!getenv("DISPLAY"))
614 debug("X11: Do not have X display\n");
617 if (!(x11_dpy = XOpenDisplay(NULL)))
618 die("Cannot open X display, although the DISPLAY variable is set");
622 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
624 die("Cannot intern OSD_QUEUE atom");
628 debug("X11: Initialized\n");
634 if (leds_want == leds_have)
637 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
638 for (int i=1; i<10; i++)
639 if (leds_care & (leds_have ^ leds_want) & (1 << i))
643 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
644 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
647 leds_have = leds_want;
653 if (!osd_want || !allow_osd)
660 debug("OSD: Displaying\n");
662 char msg[] = ":You have new mail\n\n";
663 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) msg, sizeof(msg)-1);
675 CLIST_FOR_EACH(struct mbox *, b, mboxes)
677 if (b->o.led > 0 && b->new)
678 leds_want |= (1 << b->o.led);
679 if (b->o.osd > 0 && b->new)
698 static void x11_init(void) { }
699 static void rethink_leds(void) { }
700 static void x11_cleanup(void) { }
704 static int cursor_at, cursor_max;
714 static int attrs[2][2][M_MAX]; // active, hilite, status
722 struct mbox *b = mbox_array[i];
723 int cc = (cursor_at == i);
724 int hi = b->o.highlight;
726 attrset(attrs[cc][hi][M_IDLE]);
728 printw("%c ", b->o.hotkey);
734 attrset(attrs[cc][hi][M_NEW]);
735 else if (b->flagged && b->o.show_flagged)
736 attrset(attrs[cc][hi][M_FLAG]);
737 printw("%-20s ", b->name);
740 else if (b->scanning)
742 attrset(attrs[cc][hi][M_SCAN]);
743 printw("[SCANNING]");
745 else if (b->total < 0)
747 attrset(attrs[cc][hi][M_BAD]);
752 attrset(attrs[cc][hi][M_IDLE]);
753 printw("%6d ", b->total);
757 attrset(attrs[cc][hi][M_NEW]);
758 printw("%6d ", b->new);
759 attrset(attrs[cc][hi][M_IDLE]);
760 int age = (last_scan_time - b->last_time);
764 printw("%2d min ", age/60);
765 else if (age < 86400)
766 printw("%2d hrs ", age/3600);
771 else if (b->flagged && b->o.show_flagged)
773 attrset(attrs[cc][hi][M_FLAG]);
774 printw("%6d ", b->flagged);
775 attrset(attrs[cc][hi][M_IDLE]);
777 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
780 if (snip && b->o.snippets && b->snippet[0])
783 getyx(stdscr, yy, xx);
784 int remains = COLS-1-xx;
787 #ifdef CONFIG_WIDE_CURSES
788 size_t len = strlen(b->snippet)+1;
790 mbstowcs(snip, b->snippet, len);
791 addnwstr(snip, remains);
793 printw("%-.*s", remains, b->snippet);
799 attrset(attrs[0][0][M_IDLE]);
806 cursor_max = num_mboxes;
807 if (cursor_max > LINES-1)
808 cursor_max = LINES-1;
809 if (cursor_at >= cursor_max)
810 cursor_at = cursor_max - 1;
814 for (int i=0; i<cursor_max; i++)
819 printw("(no mailboxes found)");
827 rethink_display(void)
832 CLIST_FOR_EACH(struct mbox *, b, mboxes)
833 if (mbox_visible_p(b))
836 if (i >= num_mboxes || mbox_array[i] != b)
839 if (i >= mbox_array_size)
841 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
842 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
846 if (b->o.beep && b->new > b->last_beep_new)
848 b->last_beep_new = b->new;
861 if (beeeep && allow_bells)
872 intrflush(stdscr, FALSE);
873 keypad(stdscr, TRUE);
876 static const int attrs_mono[2][M_MAX] = {
877 [0] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_BOLD, [M_FLAG] = 0, [M_BAD] = A_DIM },
878 [1] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_REVERSE | A_BOLD, [M_FLAG] = A_REVERSE, [M_BAD] = A_DIM },
880 for (int i=0; i<2; i++)
881 for (int j=0; j<M_MAX; j++)
883 attrs[0][i][j] = attrs_mono[i][j];
884 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
890 if (COLOR_PAIRS >= 5)
892 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
893 init_pair(2, COLOR_RED, COLOR_BLACK);
894 init_pair(3, COLOR_WHITE, COLOR_BLUE);
895 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
896 init_pair(5, COLOR_RED, COLOR_BLUE);
897 init_pair(6, COLOR_GREEN, COLOR_BLACK);
898 init_pair(7, COLOR_GREEN, COLOR_BLUE);
899 static const int attrs_color[2][2][M_MAX] = {
900 [0][0] = { [M_IDLE] = 0, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1), [M_FLAG] = COLOR_PAIR(6), [M_BAD] = COLOR_PAIR(2) },
901 [0][1] = { [M_IDLE] = A_BOLD, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1) | A_BOLD, [M_FLAG] = COLOR_PAIR(6) | A_BOLD, [M_BAD] = COLOR_PAIR(2) | A_BOLD },
902 [1][0] = { [M_IDLE] = COLOR_PAIR(3), [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4), [M_FLAG] = COLOR_PAIR(7), [M_BAD] = COLOR_PAIR(5) },
903 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD, [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4) | A_BOLD, [M_FLAG] = COLOR_PAIR(7) | A_BOLD, [M_BAD] = COLOR_PAIR(5) | A_BOLD },
905 memcpy(attrs, attrs_color, sizeof(attrs));
917 print_status(char *status)
921 printw("%s", status);
927 scan_and_redraw(void)
929 print_status("Busy...");
937 if (i >= 0 && i < cursor_max && i != cursor_at)
947 next_active(int since, int step)
951 since = (since+cursor_max) % cursor_max;
952 step = (step+cursor_max) % cursor_max;
958 struct mbox *b = mbox_array[i];
959 if (b->new && b->o.priority > bestp)
962 bestp = b->o.priority;
964 i = (i+step) % cursor_max;
972 mbox_run(struct mbox *b)
974 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
975 sprintf(cmd, run_cmd, b->path);
981 b->force_refresh = 1;
986 #define STR(c) STR2(c)
991 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
994 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
995 -d\t\t\tLog debug messages to stderr\n\
996 -i\t\t\tInclude user's INBOX\n\
997 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
998 -o <pattern>=<opts>\tSet mailbox options\n\
999 -o <opts>\t\tSet default options for all mailboxes\n\
1000 -p <pri>\t\tSet minimum priority to show\n\
1002 Mailbox options (set with `-o', use upper case to negate):\n\
1003 0-9\t\t\tSet mailbox priority (0=default)\n\
1004 b\t\t\tBeep when a message arrives\n\
1005 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1006 e\t\t\tHide from display if empty\n\
1007 f\t\t\tShow flagged messages if there are no new ones\n\
1008 h\t\t\tHide from display\n\
1009 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1010 m\t\t\tShow mailbox name of the sender\n\
1011 p\t\t\tShow personal info (full name) of the sender\n\
1012 s\t\t\tShow message snippets\n\
1013 t\t\t\tHighlight the entry\n\
1014 !<key>\t\t\tSet hot key\n\
1016 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>\n\
1017 It can be freely distributed and used according to the GNU GPL v2.\n\
1023 parse_options(char *c)
1027 if (sep = strchr(c, '='))
1029 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1030 memcpy(n->pattern, c, sep-c);
1031 n->pattern[sep-c] = 0;
1032 clist_add_tail(&options, &n->n);
1038 o = &global_options;
1042 if (x >= '0' && x <= '9')
1043 o->priority = x - '0';
1044 else if (x == '!' && *c)
1046 else if (x == 'l' && *c >= '1' && *c <= '9')
1047 o->led = *c++ - '0';
1050 int value = !!islower(x);
1060 o->hide_if_empty = value;
1063 o->show_flagged = value;
1069 o->sender_mbox = value;
1072 o->sender_personal = value;
1075 o->snippets = value;
1078 o->highlight = value;
1081 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
1088 main(int argc, char **argv)
1090 clist_init(&mboxes);
1091 clist_init(&options);
1092 clist_init(&patterns);
1095 while ((c = getopt(argc, argv, "c:dim:o:p:")) >= 0)
1099 check_interval = atol(optarg);
1100 if (check_interval <= 0)
1113 parse_options(optarg);
1116 minimum_priority = atol(optarg);
1121 while (optind < argc)
1122 add_pattern(argv[optind++]);
1130 int should_exit = 0;
1132 while (!should_exit)
1134 time_t now = time(NULL);
1135 int remains = last_scan_time + check_interval - now;
1136 if (remains <= 0 || force_refresh)
1141 halfdelay((remains > 255) ? 255 : remains);
1143 for (int i=0; i<num_mboxes; i++)
1144 if (ch == mbox_array[i]->o.hotkey)
1148 mbox_run(mbox_array[i]);
1158 move_cursor(cursor_at+1);
1162 move_cursor(cursor_at-1);
1172 move_cursor(cursor_max-1);
1175 next_active(cursor_at+1, 1);
1178 next_active(cursor_at-1, -1);
1182 if (cursor_at < cursor_max)
1183 mbox_run(mbox_array[cursor_at]);
1186 clearok(stdscr, TRUE);
1195 print_status("Bells and whistles are now enabled. Toot!");
1199 print_status("Bells and whistles are now disabled. Pssst!");
1203 print_status("On-screen display is now enabled.");
1207 print_status("On-screen display is now disabled. Watch your step.");
1210 if (ch >= '0' && ch <= '9')
1212 minimum_priority = ch - '0';
1216 debug("Pressed unknown key %d\n", ch);