2 * Incoming Mail Checker
4 * (c) 2005 Martin Mares <mj@ucw.cz>
27 static int check_interval = 30;
28 static int force_refresh;
29 static int minimum_priority;
30 static time_t last_scan_time;
31 static char *run_cmd = "mutt -f %s";
54 static clist options, patterns;
55 static struct options global_options;
66 int last_size, last_pos;
68 int last_total, last_new;
75 static struct mbox **mbox_array;
76 static int num_mboxes, mbox_array_size;
78 static void redraw_line(int i);
79 static void rethink_display(void);
82 add_pattern(char *patt)
84 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
85 strcpy(n->pattern, patt);
86 if (patt = strchr(n->pattern, '='))
93 clist_add_tail(&patterns, &n->n);
99 struct passwd *p = getpwuid(getuid());
101 die("You don't exist, go away!");
102 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
103 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
108 init_options(struct options *o)
112 o->hide_if_empty = -1;
119 setup_options(struct mbox *b)
121 b->o = global_options;
122 CLIST_FOR_EACH(struct option_node *, n, options)
123 if (!fnmatch(n->pattern, b->name, 0))
125 debug("\tApplied options %s\n", n->pattern);
126 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
129 MERGE(hide_if_empty);
137 mbox_name(char *path)
139 char *c = strrchr(path, '/');
140 return c ? (c+1) : path;
144 add_mbox(clist *l, char *path, char *name)
146 struct mbox *b = xmalloc(sizeof(*b));
147 bzero(b, sizeof(*b));
148 b->path = xstrdup(path);
149 b->name = xstrdup(name);
153 cnode *prev = l->head.prev;
154 while (prev != &l->head && strcmp(((struct mbox *)prev)->name, name) > 0)
156 clist_insert_after(&b->n, prev);
159 clist_add_tail(l, &b->n);
165 del_mbox(struct mbox *b)
174 find_mbox(clist *l, char *path)
176 CLIST_FOR_EACH(struct mbox *, b, *l)
177 if (!strcmp(b->path, path))
183 mbox_active_p(struct mbox *b)
185 if (b->o.priority < minimum_priority)
193 mbox_visible_p(struct mbox *b)
195 if (!mbox_active_p(b))
199 if (b->o.hide_if_empty && !b->total)
205 add_snippet(char **ppos, char *term, char *add)
208 while (*add && pos < term)
214 prepare_snippet(struct mbox *b, char *sender, char *subject)
216 while (*sender == ' ' || *sender == '\t')
218 while (*subject == ' ' || *subject == '\t')
221 char *pos = b->snippet;
222 char *term = b->snippet + sizeof(b->snippet) - 1;
225 add_snippet(&pos, term, sender);
226 add_snippet(&pos, term, ": ");
229 add_snippet(&pos, term, subject);
231 add_snippet(&pos, term, "No subject");
234 static int mb_fd, mb_pos;
235 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
240 mb_cc = mb_end = mb_buf;
247 lseek(mb_fd, pos, SEEK_SET);
254 return mb_pos - (mb_end - mb_cc);
260 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
269 mb_end = mb_buf + len;
278 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
282 mb_check(const char *p, int len)
286 if (mb_get() != *p++)
293 scan_mbox(struct mbox *b, struct stat *st)
295 char buf[1024], sender[1024], subject[1024];
297 const char from[] = "\nFrom ";
301 b->total = b->new = 0;
306 /* FIXME: Locking! */
308 mb_fd = open(b->path, O_RDONLY);
311 debug("[open failed: %m] ");
312 b->total = b->new = -1;
318 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh)
320 mb_seek(b->last_pos);
321 if (mb_check(from, 6))
323 debug("[incremental] ");
328 debug("[incremental failed] ");
334 if (!mb_check(from+1, 5))
336 debug("[inconsistent] ");
337 b->total = b->new = -1;
340 b->total = b->new = 0;
341 b->last_total = b->last_new = 0;
345 b->total = b->last_total;
346 b->new = b->last_new;
351 b->last_pos = mb_tell() - 5;
353 b->last_pos--; // last_pos should be the previous \n character
354 b->last_total = b->total;
355 b->last_new = b->new;
356 while ((c = mb_get()) >= 0 && c != '\n')
370 debug("[truncated] ");
377 if (i < sizeof(buf) - 1)
383 if (!strncasecmp(buf, "Status:", 7))
385 else if (!strncasecmp(buf, "From:", 5))
386 strcpy(sender, buf+5);
387 else if (!strncasecmp(buf, "Subject:", 8))
388 strcpy(subject, buf+8);
395 prepare_snippet(b, sender, subject);
416 debug("Searching for mailboxes...\n");
417 last_scan_time = time(NULL);
418 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
420 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
422 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
423 if (err && err != GLOB_NOMATCH)
424 die("Failed to glob %s: %m", p->pattern);
425 for (uns i=0; i<g.gl_pathc; i++)
427 char *name = g.gl_pathv[i];
428 struct mbox *b = find_mbox(&mboxes, name);
431 b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
432 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
442 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
448 debug("Lost mailbox %s\n", b->name);
455 debug("Scanning mailboxes...\n");
456 CLIST_FOR_EACH(struct mbox *, b, mboxes)
459 debug("%s: ", b->name);
460 if (!mbox_active_p(b))
466 b->force_refresh = 1;
467 if (stat(b->path, &st) < 0)
469 b->total = b->new = -1;
472 else if (!b->last_time || st.st_mtime != b->last_time || st.st_size != b->last_size || b->force_refresh)
475 redraw_line(b->index);
479 b->last_time = st.st_mtime;
480 b->last_size = st.st_size;
481 debug("%d %d (stopped at %d of %d)\n", b->total, b->new, b->last_pos, b->last_size);
484 redraw_line(b->index);
488 debug("not changed\n");
489 b->force_refresh = 0;
493 debug("Scan finished\n");
494 last_scan_time = time(NULL);
498 static int cursor_at, cursor_max;
507 static int attrs[2][2][M_MAX]; // active, hilite, status
515 struct mbox *b = mbox_array[i];
516 int cc = (cursor_at == i);
517 int hi = b->o.highlight;
519 attrset(attrs[cc][hi][M_IDLE]);
525 attrset(attrs[cc][hi][M_NEW]);
526 printw("%-20s ", b->name);
529 else if (b->scanning)
531 attrset(attrs[cc][hi][M_SCAN]);
532 printw("[SCANNING]");
534 else if (b->total < 0)
536 attrset(attrs[cc][hi][M_BAD]);
541 attrset(attrs[cc][hi][M_IDLE]);
542 printw("%6d ", b->total);
545 attrset(attrs[cc][hi][M_NEW]);
546 printw("%6d ", b->new);
547 attrset(attrs[cc][hi][M_IDLE]);
548 int age = (last_scan_time - b->last_time);
552 printw("%2d min ", age/60);
553 else if (age < 86400)
554 printw("%2d hrs ", age/3600);
557 if (b->o.snippets && b->snippet[0])
560 getyx(stdscr, yy, xx);
561 int remains = COLS-1-xx;
563 printw("%-.*s", remains, b->snippet);
568 attrset(attrs[0][0][M_IDLE]);
575 cursor_max = num_mboxes;
576 if (cursor_max > LINES-1)
577 cursor_max = LINES-1;
578 if (cursor_at >= cursor_max)
579 cursor_at = cursor_max - 1;
583 for (int i=0; i<cursor_max; i++)
588 printw("(no mailboxes found)");
596 rethink_display(void)
601 CLIST_FOR_EACH(struct mbox *, b, mboxes)
602 if (mbox_visible_p(b))
605 if (i >= num_mboxes || mbox_array[i] != b)
608 if (i >= mbox_array_size)
610 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
611 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
615 if (b->o.beep && b->new > b->last_beep_new)
617 b->last_beep_new = b->new;
640 intrflush(stdscr, FALSE);
641 keypad(stdscr, TRUE);
644 static const int attrs_mono[2][M_MAX] = {
645 [0] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_BOLD, [M_BAD] = A_DIM },
646 [1] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_REVERSE | A_BOLD, [M_BAD] = A_DIM },
648 for (int i=0; i<2; i++)
649 for (int j=0; j<M_MAX; j++)
651 attrs[0][i][j] = attrs_mono[i][j];
652 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
658 if (COLOR_PAIRS >= 5)
660 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
661 init_pair(2, COLOR_RED, COLOR_BLACK);
662 init_pair(3, COLOR_WHITE, COLOR_BLUE);
663 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
664 init_pair(5, COLOR_RED, COLOR_BLUE);
665 static const int attrs_color[2][2][M_MAX] = {
666 [0][0] = { [M_IDLE] = 0, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1), [M_BAD] = COLOR_PAIR(2) },
667 [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 },
668 [1][0] = { [M_IDLE] = COLOR_PAIR(3), [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4), [M_BAD] = COLOR_PAIR(5) },
669 [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 },
671 memcpy(attrs, attrs_color, sizeof(attrs));
683 scan_and_redraw(void)
697 if (i >= 0 && i < cursor_max && i != cursor_at)
707 next_active(int since, int step)
711 since = (since+cursor_max) % cursor_max;
712 step = (step+cursor_max) % cursor_max;
718 struct mbox *b = mbox_array[i];
719 if (b->new && b->o.priority > bestp)
722 bestp = b->o.priority;
724 i = (i+step) % cursor_max;
734 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
737 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
738 -d\t\t\tLog debug messages to stderr\n\
739 -i\t\t\tInclude user's INBOX\n\
740 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
741 -o <pattern>=<opts>\tSet mailbox options\n\
742 -o <opts>\t\tSet default options for all mailboxes\n\
743 -p <pri>\t\tSet minimum priority to show\n\
745 Mailbox options (set with `-o', use upper case to negate):\n\
746 0-9\t\t\tSet mailbox priority (0=default)\n\
747 b\t\t\tBeep when a message arrives\n\
748 e\t\t\tHide from display if empty\n\
749 h\t\t\tHide from display\n\
750 s\t\t\tShow message snippets\n\
751 t\t\t\tHighlight the entry\n\
753 CheckMail " VERSION ", (c) " YEAR " Martin Mares <mj@ucw.cz>\n\
754 It can be freely distributed and used according to the GNU GPL v2.\n\
760 parse_options(char *c)
764 if (sep = strchr(c, '='))
766 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
767 memcpy(n->pattern, c, sep-c);
768 n->pattern[sep-c] = 0;
769 clist_add_tail(&options, &n->n);
779 if (x >= '0' && x <= '9')
780 o->priority = x - '0';
783 int value = !!islower(x);
790 o->hide_if_empty = value;
799 o->highlight = value;
802 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
809 main(int argc, char **argv)
812 clist_init(&options);
813 clist_init(&patterns);
816 while ((c = getopt(argc, argv, "c:dim:o:p:")) >= 0)
820 check_interval = atol(optarg);
821 if (check_interval <= 0)
834 parse_options(optarg);
837 minimum_priority = atol(optarg);
842 while (optind < argc)
843 add_pattern(argv[optind++]);
852 time_t now = time(NULL);
853 int remains = last_scan_time + check_interval - now;
854 if (remains <= 0 || force_refresh)
859 halfdelay((remains > 255) ? 255 : remains);
868 move_cursor(cursor_at+1);
872 move_cursor(cursor_at-1);
882 move_cursor(cursor_max-1);
885 next_active(cursor_at+1, 1);
888 next_active(cursor_at-1, -1);
892 if (cursor_at < cursor_max)
894 struct mbox *b = mbox_array[cursor_at];
895 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
896 sprintf(cmd, run_cmd, b->path);
902 b->force_refresh = 1;
910 if (ch >= '0' && ch <= '9')
912 minimum_priority = ch - '0';
916 debug("Pressed unknown key %d\n", ch);