2 * Incoming Mail Checker
4 * (c) 2005 Martin Mares <mj@ucw.cz>
24 static int check_interval = 30;
25 static int force_refresh;
26 static int allow_bells = 1;
27 static int minimum_priority;
28 static time_t last_scan_time;
29 static char *run_cmd = "mutt -f %s";
52 static clist options, patterns;
53 static struct options global_options;
64 int last_size, last_pos;
66 int last_total, last_new;
73 static struct mbox **mbox_array;
74 static int num_mboxes, mbox_array_size;
76 static void redraw_line(int i);
77 static void rethink_display(void);
80 add_pattern(char *patt)
82 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
83 strcpy(n->pattern, patt);
84 if (patt = strchr(n->pattern, '='))
91 clist_add_tail(&patterns, &n->n);
97 struct passwd *p = getpwuid(getuid());
99 die("You don't exist, go away!");
100 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
101 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
106 init_options(struct options *o)
110 o->hide_if_empty = -1;
117 setup_options(struct mbox *b)
119 b->o = global_options;
120 CLIST_FOR_EACH(struct option_node *, n, options)
121 if (!fnmatch(n->pattern, b->name, 0))
123 debug("\tApplied options %s\n", n->pattern);
124 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
127 MERGE(hide_if_empty);
135 mbox_name(char *path)
137 char *c = strrchr(path, '/');
138 return c ? (c+1) : path;
142 add_mbox(clist *l, char *path, char *name)
144 struct mbox *b = xmalloc(sizeof(*b));
145 bzero(b, sizeof(*b));
146 b->path = xstrdup(path);
147 b->name = xstrdup(name);
151 cnode *prev = l->head.prev;
152 while (prev != &l->head && strcmp(((struct mbox *)prev)->name, name) > 0)
154 clist_insert_after(&b->n, prev);
157 clist_add_tail(l, &b->n);
163 del_mbox(struct mbox *b)
172 find_mbox(clist *l, char *path)
174 CLIST_FOR_EACH(struct mbox *, b, *l)
175 if (!strcmp(b->path, path))
181 mbox_active_p(struct mbox *b)
183 if (b->o.priority < minimum_priority)
191 mbox_visible_p(struct mbox *b)
193 if (!mbox_active_p(b))
197 if (b->o.hide_if_empty && !b->total)
203 add_snippet(char **ppos, char *term, char *add)
206 while (*add && pos < term)
213 prepare_snippet(struct mbox *b, char *sender, char *subject)
215 while (*sender == ' ' || *sender == '\t')
217 while (*subject == ' ' || *subject == '\t')
220 char *pos = b->snippet;
221 char *term = b->snippet + sizeof(b->snippet) - 1;
224 add_snippet(&pos, term, sender);
225 add_snippet(&pos, term, ": ");
228 add_snippet(&pos, term, subject);
230 add_snippet(&pos, term, "No subject");
233 static int mb_fd, mb_pos;
234 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
239 mb_cc = mb_end = mb_buf;
246 lseek(mb_fd, pos, SEEK_SET);
253 return mb_pos - (mb_end - mb_cc);
259 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
268 mb_end = mb_buf + len;
277 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
281 mb_check(const char *p, int len)
285 if (mb_get() != *p++)
292 scan_mbox(struct mbox *b, struct stat *st)
294 char buf[1024], sender[1024], subject[1024];
296 const char from[] = "\nFrom ";
300 b->total = b->new = 0;
305 /* FIXME: Locking! */
307 mb_fd = open(b->path, O_RDONLY);
310 debug("[open failed: %m] ");
311 b->total = b->new = -1;
317 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh)
319 mb_seek(b->last_pos);
320 if (mb_check(from, 6))
322 debug("[incremental] ");
327 debug("[incremental failed] ");
333 if (!mb_check(from+1, 5))
335 debug("[inconsistent] ");
336 b->total = b->new = -1;
339 b->total = b->new = 0;
340 b->last_total = b->last_new = 0;
344 b->total = b->last_total;
345 b->new = b->last_new;
350 b->last_pos = mb_tell() - 5;
352 b->last_pos--; // last_pos should be the previous \n character
353 b->last_total = b->total;
354 b->last_new = b->new;
355 while ((c = mb_get()) >= 0 && c != '\n')
369 debug("[truncated] ");
376 if (i < sizeof(buf) - 1)
382 if (!strncasecmp(buf, "Status:", 7))
384 else if (!strncasecmp(buf, "From:", 5))
385 strcpy(sender, buf+5);
386 else if (!strncasecmp(buf, "Subject:", 8))
387 strcpy(subject, buf+8);
394 prepare_snippet(b, sender, subject);
415 debug("Searching for mailboxes...\n");
416 last_scan_time = time(NULL);
417 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
419 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
421 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
422 if (err && err != GLOB_NOMATCH)
423 die("Failed to glob %s: %m", p->pattern);
424 for (uns i=0; i<g.gl_pathc; i++)
426 char *name = g.gl_pathv[i];
427 struct mbox *b = find_mbox(&mboxes, name);
430 b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
431 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
441 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
447 debug("Lost mailbox %s\n", b->name);
454 debug("Scanning mailboxes...\n");
455 CLIST_FOR_EACH(struct mbox *, b, mboxes)
458 debug("%s: ", b->name);
459 if (!mbox_active_p(b))
465 b->force_refresh = 1;
466 if (stat(b->path, &st) < 0)
468 b->total = b->new = -1;
471 else if (!b->last_time || st.st_mtime != b->last_time || st.st_size != b->last_size || b->force_refresh)
474 redraw_line(b->index);
478 b->last_time = st.st_mtime;
479 b->last_size = st.st_size;
480 debug("%d %d (stopped at %d of %d)\n", b->total, b->new, b->last_pos, b->last_size);
483 redraw_line(b->index);
487 debug("not changed\n");
488 b->force_refresh = 0;
492 debug("Scan finished\n");
493 last_scan_time = time(NULL);
497 static int cursor_at, cursor_max;
506 static int attrs[2][2][M_MAX]; // active, hilite, status
514 struct mbox *b = mbox_array[i];
515 int cc = (cursor_at == i);
516 int hi = b->o.highlight;
518 attrset(attrs[cc][hi][M_IDLE]);
524 attrset(attrs[cc][hi][M_NEW]);
525 printw("%-20s ", b->name);
528 else if (b->scanning)
530 attrset(attrs[cc][hi][M_SCAN]);
531 printw("[SCANNING]");
533 else if (b->total < 0)
535 attrset(attrs[cc][hi][M_BAD]);
540 attrset(attrs[cc][hi][M_IDLE]);
541 printw("%6d ", b->total);
544 attrset(attrs[cc][hi][M_NEW]);
545 printw("%6d ", b->new);
546 attrset(attrs[cc][hi][M_IDLE]);
547 int age = (last_scan_time - b->last_time);
551 printw("%2d min ", age/60);
552 else if (age < 86400)
553 printw("%2d hrs ", age/3600);
556 if (b->o.snippets && b->snippet[0])
559 getyx(stdscr, yy, xx);
560 int remains = COLS-1-xx;
562 printw("%-.*s", remains, b->snippet);
567 attrset(attrs[0][0][M_IDLE]);
574 cursor_max = num_mboxes;
575 if (cursor_max > LINES-1)
576 cursor_max = LINES-1;
577 if (cursor_at >= cursor_max)
578 cursor_at = cursor_max - 1;
582 for (int i=0; i<cursor_max; i++)
587 printw("(no mailboxes found)");
595 rethink_display(void)
600 CLIST_FOR_EACH(struct mbox *, b, mboxes)
601 if (mbox_visible_p(b))
604 if (i >= num_mboxes || mbox_array[i] != b)
607 if (i >= mbox_array_size)
609 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
610 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
614 if (b->o.beep && b->new > b->last_beep_new)
616 b->last_beep_new = b->new;
628 if (beeeep && allow_bells)
639 intrflush(stdscr, FALSE);
640 keypad(stdscr, TRUE);
643 static const int attrs_mono[2][M_MAX] = {
644 [0] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_BOLD, [M_BAD] = A_DIM },
645 [1] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_REVERSE | A_BOLD, [M_BAD] = A_DIM },
647 for (int i=0; i<2; i++)
648 for (int j=0; j<M_MAX; j++)
650 attrs[0][i][j] = attrs_mono[i][j];
651 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
657 if (COLOR_PAIRS >= 5)
659 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
660 init_pair(2, COLOR_RED, COLOR_BLACK);
661 init_pair(3, COLOR_WHITE, COLOR_BLUE);
662 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
663 init_pair(5, COLOR_RED, COLOR_BLUE);
664 static const int attrs_color[2][2][M_MAX] = {
665 [0][0] = { [M_IDLE] = 0, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1), [M_BAD] = COLOR_PAIR(2) },
666 [0][1] = { [M_IDLE] = A_BOLD, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1) | A_BOLD, [M_BAD] = COLOR_PAIR(2) | A_BOLD },
667 [1][0] = { [M_IDLE] = COLOR_PAIR(3), [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4), [M_BAD] = COLOR_PAIR(5) },
668 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD, [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4) | A_BOLD, [M_BAD] = COLOR_PAIR(5) | A_BOLD },
670 memcpy(attrs, attrs_color, sizeof(attrs));
682 print_status(char *status)
686 printw("%s", status);
692 scan_and_redraw(void)
694 print_status("Busy...");
702 if (i >= 0 && i < cursor_max && i != cursor_at)
712 next_active(int since, int step)
716 since = (since+cursor_max) % cursor_max;
717 step = (step+cursor_max) % cursor_max;
723 struct mbox *b = mbox_array[i];
724 if (b->new && b->o.priority > bestp)
727 bestp = b->o.priority;
729 i = (i+step) % cursor_max;
737 #define STR(c) STR2(c)
742 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
745 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
746 -d\t\t\tLog debug messages to stderr\n\
747 -i\t\t\tInclude user's INBOX\n\
748 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
749 -o <pattern>=<opts>\tSet mailbox options\n\
750 -o <opts>\t\tSet default options for all mailboxes\n\
751 -p <pri>\t\tSet minimum priority to show\n\
753 Mailbox options (set with `-o', use upper case to negate):\n\
754 0-9\t\t\tSet mailbox priority (0=default)\n\
755 b\t\t\tBeep when a message arrives\n\
756 e\t\t\tHide from display if empty\n\
757 h\t\t\tHide from display\n\
758 s\t\t\tShow message snippets\n\
759 t\t\t\tHighlight the entry\n\
761 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>\n\
762 It can be freely distributed and used according to the GNU GPL v2.\n\
768 parse_options(char *c)
772 if (sep = strchr(c, '='))
774 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
775 memcpy(n->pattern, c, sep-c);
776 n->pattern[sep-c] = 0;
777 clist_add_tail(&options, &n->n);
787 if (x >= '0' && x <= '9')
788 o->priority = x - '0';
791 int value = !!islower(x);
798 o->hide_if_empty = value;
807 o->highlight = value;
810 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
817 main(int argc, char **argv)
820 clist_init(&options);
821 clist_init(&patterns);
824 while ((c = getopt(argc, argv, "c:dim:o:p:")) >= 0)
828 check_interval = atol(optarg);
829 if (check_interval <= 0)
842 parse_options(optarg);
845 minimum_priority = atol(optarg);
850 while (optind < argc)
851 add_pattern(argv[optind++]);
860 time_t now = time(NULL);
861 int remains = last_scan_time + check_interval - now;
862 if (remains <= 0 || force_refresh)
867 halfdelay((remains > 255) ? 255 : remains);
876 move_cursor(cursor_at+1);
880 move_cursor(cursor_at-1);
890 move_cursor(cursor_max-1);
893 next_active(cursor_at+1, 1);
896 next_active(cursor_at-1, -1);
900 if (cursor_at < cursor_max)
902 struct mbox *b = mbox_array[cursor_at];
903 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
904 sprintf(cmd, run_cmd, b->path);
910 b->force_refresh = 1;
915 clearok(stdscr, TRUE);
924 print_status("Bells and whistles are now enabled. Toot!");
928 print_status("Bells and whistles are now disabled. Pssst!");
931 if (ch >= '0' && ch <= '9')
933 minimum_priority = ch - '0';
937 debug("Pressed unknown key %d\n", ch);