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 minimum_priority;
27 static time_t last_scan_time;
28 static char *run_cmd = "mutt -f %s";
51 static clist options, patterns;
52 static struct options global_options;
63 int last_size, last_pos;
65 int last_total, last_new;
72 static struct mbox **mbox_array;
73 static int num_mboxes, mbox_array_size;
75 static void redraw_line(int i);
76 static void rethink_display(void);
79 add_pattern(char *patt)
81 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
82 strcpy(n->pattern, patt);
83 if (patt = strchr(n->pattern, '='))
90 clist_add_tail(&patterns, &n->n);
96 struct passwd *p = getpwuid(getuid());
98 die("You don't exist, go away!");
99 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
100 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
105 init_options(struct options *o)
109 o->hide_if_empty = -1;
116 setup_options(struct mbox *b)
118 b->o = global_options;
119 CLIST_FOR_EACH(struct option_node *, n, options)
120 if (!fnmatch(n->pattern, b->name, 0))
122 debug("\tApplied options %s\n", n->pattern);
123 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
126 MERGE(hide_if_empty);
134 mbox_name(char *path)
136 char *c = strrchr(path, '/');
137 return c ? (c+1) : path;
141 add_mbox(clist *l, char *path, char *name)
143 struct mbox *b = xmalloc(sizeof(*b));
144 bzero(b, sizeof(*b));
145 b->path = xstrdup(path);
146 b->name = xstrdup(name);
150 cnode *prev = l->head.prev;
151 while (prev != &l->head && strcmp(((struct mbox *)prev)->name, name) > 0)
153 clist_insert_after(&b->n, prev);
156 clist_add_tail(l, &b->n);
162 del_mbox(struct mbox *b)
171 find_mbox(clist *l, char *path)
173 CLIST_FOR_EACH(struct mbox *, b, *l)
174 if (!strcmp(b->path, path))
180 mbox_active_p(struct mbox *b)
182 if (b->o.priority < minimum_priority)
190 mbox_visible_p(struct mbox *b)
192 if (!mbox_active_p(b))
196 if (b->o.hide_if_empty && !b->total)
202 add_snippet(char **ppos, char *term, char *add)
205 while (*add && pos < term)
211 prepare_snippet(struct mbox *b, char *sender, char *subject)
213 while (*sender == ' ' || *sender == '\t')
215 while (*subject == ' ' || *subject == '\t')
218 char *pos = b->snippet;
219 char *term = b->snippet + sizeof(b->snippet) - 1;
222 add_snippet(&pos, term, sender);
223 add_snippet(&pos, term, ": ");
226 add_snippet(&pos, term, subject);
228 add_snippet(&pos, term, "No subject");
231 static int mb_fd, mb_pos;
232 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
237 mb_cc = mb_end = mb_buf;
244 lseek(mb_fd, pos, SEEK_SET);
251 return mb_pos - (mb_end - mb_cc);
257 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
266 mb_end = mb_buf + len;
275 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
279 mb_check(const char *p, int len)
283 if (mb_get() != *p++)
290 scan_mbox(struct mbox *b, struct stat *st)
292 char buf[1024], sender[1024], subject[1024];
294 const char from[] = "\nFrom ";
298 b->total = b->new = 0;
303 /* FIXME: Locking! */
305 mb_fd = open(b->path, O_RDONLY);
308 debug("[open failed: %m] ");
309 b->total = b->new = -1;
315 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh)
317 mb_seek(b->last_pos);
318 if (mb_check(from, 6))
320 debug("[incremental] ");
325 debug("[incremental failed] ");
331 if (!mb_check(from+1, 5))
333 debug("[inconsistent] ");
334 b->total = b->new = -1;
337 b->total = b->new = 0;
338 b->last_total = b->last_new = 0;
342 b->total = b->last_total;
343 b->new = b->last_new;
348 b->last_pos = mb_tell() - 5;
350 b->last_pos--; // last_pos should be the previous \n character
351 b->last_total = b->total;
352 b->last_new = b->new;
353 while ((c = mb_get()) >= 0 && c != '\n')
367 debug("[truncated] ");
374 if (i < sizeof(buf) - 1)
380 if (!strncasecmp(buf, "Status:", 7))
382 else if (!strncasecmp(buf, "From:", 5))
383 strcpy(sender, buf+5);
384 else if (!strncasecmp(buf, "Subject:", 8))
385 strcpy(subject, buf+8);
392 prepare_snippet(b, sender, subject);
413 debug("Searching for mailboxes...\n");
414 last_scan_time = time(NULL);
415 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
417 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
419 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
420 if (err && err != GLOB_NOMATCH)
421 die("Failed to glob %s: %m", p->pattern);
422 for (uns i=0; i<g.gl_pathc; i++)
424 char *name = g.gl_pathv[i];
425 struct mbox *b = find_mbox(&mboxes, name);
428 b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
429 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
439 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
445 debug("Lost mailbox %s\n", b->name);
452 debug("Scanning mailboxes...\n");
453 CLIST_FOR_EACH(struct mbox *, b, mboxes)
456 debug("%s: ", b->name);
457 if (!mbox_active_p(b))
463 b->force_refresh = 1;
464 if (stat(b->path, &st) < 0)
466 b->total = b->new = -1;
469 else if (!b->last_time || st.st_mtime != b->last_time || st.st_size != b->last_size || b->force_refresh)
472 redraw_line(b->index);
476 b->last_time = st.st_mtime;
477 b->last_size = st.st_size;
478 debug("%d %d (stopped at %d of %d)\n", b->total, b->new, b->last_pos, b->last_size);
481 redraw_line(b->index);
485 debug("not changed\n");
486 b->force_refresh = 0;
490 debug("Scan finished\n");
491 last_scan_time = time(NULL);
495 static int cursor_at, cursor_max;
504 static int attrs[2][2][M_MAX]; // active, hilite, status
512 struct mbox *b = mbox_array[i];
513 int cc = (cursor_at == i);
514 int hi = b->o.highlight;
516 attrset(attrs[cc][hi][M_IDLE]);
522 attrset(attrs[cc][hi][M_NEW]);
523 printw("%-20s ", b->name);
526 else if (b->scanning)
528 attrset(attrs[cc][hi][M_SCAN]);
529 printw("[SCANNING]");
531 else if (b->total < 0)
533 attrset(attrs[cc][hi][M_BAD]);
538 attrset(attrs[cc][hi][M_IDLE]);
539 printw("%6d ", b->total);
542 attrset(attrs[cc][hi][M_NEW]);
543 printw("%6d ", b->new);
544 attrset(attrs[cc][hi][M_IDLE]);
545 int age = (last_scan_time - b->last_time);
549 printw("%2d min ", age/60);
550 else if (age < 86400)
551 printw("%2d hrs ", age/3600);
554 if (b->o.snippets && b->snippet[0])
557 getyx(stdscr, yy, xx);
558 int remains = COLS-1-xx;
560 printw("%-.*s", remains, b->snippet);
565 attrset(attrs[0][0][M_IDLE]);
572 cursor_max = num_mboxes;
573 if (cursor_max > LINES-1)
574 cursor_max = LINES-1;
575 if (cursor_at >= cursor_max)
576 cursor_at = cursor_max - 1;
580 for (int i=0; i<cursor_max; i++)
585 printw("(no mailboxes found)");
593 rethink_display(void)
598 CLIST_FOR_EACH(struct mbox *, b, mboxes)
599 if (mbox_visible_p(b))
602 if (i >= num_mboxes || mbox_array[i] != b)
605 if (i >= mbox_array_size)
607 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
608 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
612 if (b->o.beep && b->new > b->last_beep_new)
614 b->last_beep_new = b->new;
637 intrflush(stdscr, FALSE);
638 keypad(stdscr, TRUE);
641 static const int attrs_mono[2][M_MAX] = {
642 [0] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_BOLD, [M_BAD] = A_DIM },
643 [1] = { [M_IDLE] = 0, [M_SCAN] = A_BOLD, [M_NEW] = A_REVERSE | A_BOLD, [M_BAD] = A_DIM },
645 for (int i=0; i<2; i++)
646 for (int j=0; j<M_MAX; j++)
648 attrs[0][i][j] = attrs_mono[i][j];
649 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
655 if (COLOR_PAIRS >= 5)
657 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
658 init_pair(2, COLOR_RED, COLOR_BLACK);
659 init_pair(3, COLOR_WHITE, COLOR_BLUE);
660 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
661 init_pair(5, COLOR_RED, COLOR_BLUE);
662 static const int attrs_color[2][2][M_MAX] = {
663 [0][0] = { [M_IDLE] = 0, [M_SCAN] = COLOR_PAIR(1), [M_NEW] = COLOR_PAIR(1), [M_BAD] = COLOR_PAIR(2) },
664 [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 },
665 [1][0] = { [M_IDLE] = COLOR_PAIR(3), [M_SCAN] = COLOR_PAIR(4), [M_NEW] = COLOR_PAIR(4), [M_BAD] = COLOR_PAIR(5) },
666 [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 },
668 memcpy(attrs, attrs_color, sizeof(attrs));
680 scan_and_redraw(void)
694 if (i >= 0 && i < cursor_max && i != cursor_at)
704 next_active(int since, int step)
708 since = (since+cursor_max) % cursor_max;
709 step = (step+cursor_max) % cursor_max;
715 struct mbox *b = mbox_array[i];
716 if (b->new && b->o.priority > bestp)
719 bestp = b->o.priority;
721 i = (i+step) % cursor_max;
729 #define STR(c) STR2(c)
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 " STR(VERSION) ", (c) " STR(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);