2 * Incoming Mail Checker
4 * (c) 2005--2015 Martin Mares <mj@ucw.cz>
20 #include <sys/types.h>
25 #ifdef CONFIG_WIDE_CURSES
26 #include <ncursesw/ncurses.h>
35 static int check_interval = 30;
36 static int force_refresh;
37 static int allow_bells = 1;
38 static int allow_osd = 1;
39 static int minimum_priority;
40 static time_t last_scan_time;
41 static char *run_cmd = "mutt -f %s";
42 static int simple_tab;
73 static clist options, patterns;
74 static struct options global_options = {
79 #define MDIR_MAX_NAME_LEN 128
91 time_t display_valid_until;
92 int last_size, last_pos;
93 int total, new, flagged;
94 int last_total, last_new, last_flagged;
98 char sender_snippet[128];
99 char subject_snippet[128];
100 char mdir_best[MDIR_MAX_NAME_LEN];
104 static struct mbox **mbox_array;
105 static int num_mboxes, mbox_array_size;
107 struct osd_opt_node {
113 static clist osd_opts;
115 static void redraw_line(int i);
116 static void rethink_display(int notify);
119 add_pattern(char *patt)
121 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
122 strcpy(n->pattern, patt);
123 if (patt = strchr(n->pattern, '='))
130 clist_add_tail(&patterns, &n->n);
136 struct passwd *p = getpwuid(getuid());
138 die("You don't exist, go away!");
139 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
140 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
145 init_options(struct options *o)
149 o->hide_if_empty = -1;
153 o->show_flagged = -1;
154 o->sender_personal = -1;
159 o->unread_is_new = -1;
164 setup_options(struct mbox *b)
166 b->o = global_options;
167 CLIST_FOR_EACH(struct option_node *, n, options)
168 if (!fnmatch(n->pattern, b->name, 0))
170 debug("\tApplied options %s\n", n->pattern);
171 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
174 MERGE(hide_if_empty);
179 MERGE(sender_personal);
184 MERGE(unread_is_new);
190 add_osd_opt(char *arg)
192 struct osd_opt_node *n = xmalloc(sizeof(*n) + strlen(arg));
194 n->val = strchr(n->key, '=');
196 die("Malformed OSD option");
198 clist_add_tail(&osd_opts, &n->n);
202 mbox_name(char *path)
204 char *c = strrchr(path, '/');
205 return c ? (c+1) : path;
209 new_mbox(char *path, char *name)
211 struct mbox *b = xmalloc(sizeof(*b));
212 bzero(b, sizeof(*b));
213 b->path = xstrdup(path);
214 b->name = xstrdup(name);
219 add_mbox(clist *l, struct mbox *b)
223 cnode *prev = l->head.prev;
224 struct mbox *prev_mbox;
225 while (prev != &l->head &&
226 ((prev_mbox = (struct mbox *)prev)->o.sort_order > b->o.sort_order ||
227 prev_mbox->o.sort_order == b->o.sort_order && strcmp(((struct mbox *)prev)->name, b->name) > 0))
229 clist_insert_after(&b->n, prev);
232 clist_add_tail(l, &b->n);
236 del_mbox(struct mbox *b)
245 find_mbox(clist *l, char *path)
247 CLIST_FOR_EACH(struct mbox *, b, *l)
248 if (!strcmp(b->path, path))
254 mbox_active_p(struct mbox *b)
256 if (b->o.priority < minimum_priority)
264 mbox_visible_p(struct mbox *b)
266 if (!mbox_active_p(b))
270 if (b->o.hide_if_empty && !b->total)
276 prepare_snippets(struct mbox *b, char *sender, char *subject)
280 while (*sender == ' ' || *sender == '\t')
282 while (*subject == ' ' || *subject == '\t')
285 pos = b->sender_snippet;
286 term = pos + sizeof(b->sender_snippet) - 1;
287 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
288 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
292 pos = b->subject_snippet;
293 term = pos + sizeof(b->subject_snippet) - 1;
295 add_subject_snippet(&pos, term, subject);
297 add_snippet_raw(&pos, term, "No subject");
301 build_snippet(char *buf, char *term, struct mbox *b)
303 if (b->sender_snippet[0])
305 add_snippet(&buf, term, b->sender_snippet);
306 add_snippet_raw(&buf, term, ": ");
308 add_snippet(&buf, term, b->subject_snippet);
311 static int mb_fd, mb_pos, mb_seekable;
312 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
317 mb_cc = mb_end = mb_buf;
324 lseek(mb_fd, pos, SEEK_SET);
331 return mb_pos - (mb_end - mb_cc);
337 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
346 mb_end = mb_buf + len;
355 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
366 mb_check(const char *p, int len)
370 if (mb_get() != *p++)
381 int avail = mb_end - mb_cc;
384 if (mb_seekable && len >= (int) sizeof(mb_buf))
386 int next = len - len % sizeof(mb_buf);
387 mb_seek(mb_tell() + next);
397 int next = (avail < len) ? avail : len;
404 #define HDR_BUF_SIZE 1024
407 read_hdr_field(char *buf)
415 debug("[truncated] ");
428 while (c == ' ' || c == '\t');
436 if (i < HDR_BUF_SIZE - 1)
441 debug("Header: <%s>\n", buf);
442 return buf[0] ? 1 : 0;
446 scan_mbox(struct mbox *b, struct stat *st)
448 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
451 const char from[] = "\nFrom ";
453 b->best_time = st->st_mtime;
456 b->total = b->new = b->flagged = 0;
461 mb_fd = open(b->path, O_RDONLY);
464 debug("[open failed: %m] ");
465 b->total = b->new = b->flagged = -1;
471 c = read(mb_fd, signature, 2);
472 lseek(mb_fd, 0, SEEK_SET);
474 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
476 debug("[decompressing] ");
479 die("pipe failed: %m");
482 die("fork failed: %m");
485 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
486 die("dup2 failed: %m");
490 execlp("gzip", "gzip", "-cd", NULL);
491 die("Cannot execute gzip: %m");
502 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && mb_seekable)
504 mb_seek(b->last_pos);
505 if (mb_check(from, 6))
507 debug("[incremental] ");
512 debug("[incremental failed] ");
518 if (!mb_check(from+1, 5))
520 debug("[inconsistent] ");
521 b->total = b->new = b->flagged = -1;
524 b->total = b->new = b->flagged = 0;
525 b->last_total = b->last_new = b->last_flagged = 0;
530 b->total = b->last_total;
531 b->new = b->last_new;
532 b->flagged = b->last_flagged;
537 b->last_pos = mb_tell() - 5;
539 b->last_pos--; // last_pos should be the previous \n character
540 b->last_total = b->total;
541 b->last_new = b->new;
542 b->last_flagged = b->flagged;
543 while ((c = mb_get()) >= 0 && c != '\n')
546 int content_length = -1;
553 int c = read_hdr_field(buf);
558 if (!strncasecmp(buf, "Status:", 7))
560 if (!b->o.unread_is_new || strchr(buf + 7, 'R'))
563 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
565 else if (!strncasecmp(buf, "From:", 5))
566 strcpy(sender, buf+5);
567 else if (!strncasecmp(buf, "Subject:", 8))
568 strcpy(subject, buf+8);
569 else if (!strncasecmp(buf, "Content-Length:", 15))
570 content_length = atoi(buf + 15);
579 debug("new=%d flagged=%d len=%d sender=<%s> subject=<%s>\n", new, flagged, content_length, sender, subject);
580 if (new || (flagged && !b->best_is_new))
582 b->best_is_new = new;
583 prepare_snippets(b, sender, subject);
586 if (content_length >= 0)
587 mb_skip(content_length);
605 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
606 b->total = b->new = b->flagged = -1;
611 mdir_mtime(struct mbox *b)
614 char path[strlen(b->path) + 5];
616 for (int new=0; new<2; new++)
618 sprintf(path, "%s/%s", b->path, (new ? "new" : "cur"));
620 if (stat(path, &st) < 0)
621 debug("[cannot stat %s] ", path);
622 else if (st.st_mtime > mtime)
629 mdir_get_snippet(struct mbox *b)
631 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
635 char path[strlen(b->path) + MDIR_MAX_NAME_LEN];
636 sprintf(path, "%s/%s", b->path, b->mdir_best);
637 mb_fd = open(path, O_RDONLY);
640 debug("[open failed: %m] ");
641 prepare_snippets(b, sender, subject);
647 while (read_hdr_field(buf) > 0)
649 if (!strncasecmp(buf, "From:", 5))
650 strcpy(sender, buf+5);
651 else if (!strncasecmp(buf, "Subject:", 8))
652 strcpy(subject, buf+8);
656 prepare_snippets(b, sender, subject);
660 scan_mdir(struct mbox *b)
662 int dir_len = strlen(b->path);
663 char path[dir_len + MDIR_MAX_NAME_LEN];
664 strcpy(path, b->path);
666 b->total = b->new = b->flagged = 0;
670 for (int new=0; new<2; new++)
672 strcpy(path + dir_len, (new ? "/new" : "/cur"));
673 DIR *d = opendir(path);
676 debug("[cannot open %s: %m] ");
680 while (de = readdir(d))
682 char *name = de->d_name;
683 if (name[0] == '.' || strlen(name) + 10 > MDIR_MAX_NAME_LEN)
685 path[dir_len + 4] = '/';
686 strcpy(path + dir_len + 5, name);
688 char *colon = strchr(name, ':');
691 if (colon && colon[1] == '2' && colon[2] == ',')
693 // Another comma can separate extended flags (Dovecot extension)
694 for (int i=3; colon[i] && colon[i] != ','; i++)
707 if (b->o.unread_is_new && !seen)
716 debug("%s: new=%d flagged=%d seen=%d\n", path, is_new, flagged, seen);
718 if (is_new || flagged)
720 // Need to pick the best message to display
722 if (stat(path, &st) < 0)
723 debug("[cannot stat %s: %m] ", path);
724 else if (is_new > b->best_is_new || is_new == b->best_is_new && st.st_mtime > b->best_time)
726 b->best_is_new = is_new;
727 b->best_time = st.st_mtime;
728 strcpy(b->mdir_best, path + dir_len + 1);
735 if (b->best_time && b->o.snippets)
737 debug("best <%s> ", b->mdir_best);
745 debug("Searching for mailboxes (notify=%d)...\n", notify);
746 last_scan_time = time(NULL);
747 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
749 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
751 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
752 if (err && err != GLOB_NOMATCH)
753 die("Failed to glob %s: %m", p->pattern);
754 for (uns i=0; i<g.gl_pathc; i++)
756 char *name = g.gl_pathv[i];
757 struct mbox *b = find_mbox(&mboxes, name);
760 b = new_mbox(name, (p->name ? p->name : mbox_name(name)));
761 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
763 add_mbox(&mboxes, b);
772 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
778 debug("Lost mailbox %s\n", b->name);
785 debug("Scanning mailboxes...\n");
786 CLIST_FOR_EACH(struct mbox *, b, mboxes)
789 debug("%s: ", b->name);
790 if (!mbox_active_p(b))
796 b->force_refresh = 1;
797 if (stat(b->path, &st) < 0)
799 b->total = b->new = b->flagged = -1;
804 time_t current_mtime;
807 if (S_ISREG(st.st_mode))
811 current_mtime = st.st_mtime;
812 current_size = st.st_size;
815 else if (S_ISDIR(st.st_mode))
819 current_mtime = mdir_mtime(b);
825 debug("neither file nor directory\n");
829 if (!b->last_time || current_mtime != b->last_time || current_size != b->last_size || b->force_refresh)
832 redraw_line(b->index);
839 b->last_time = current_mtime;
840 b->last_size = current_size;
841 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
844 redraw_line(b->index);
846 b->force_refresh = 0;
848 else if (b->display_valid_until <= last_scan_time)
850 debug("not changed, but needs redraw\n");
851 redraw_line(b->index);
854 debug("not changed\n");
858 debug("Scan finished\n");
859 last_scan_time = time(NULL);
860 rethink_display(notify);
865 #include <X11/Xlib.h>
866 #include <X11/Xatom.h>
868 static Display *x11_dpy;
869 static unsigned leds_care, leds_have, leds_want;
872 static unsigned osd_care;
873 #define OSD_MSG_SIZE 1024
874 static char osd_last_msg[OSD_MSG_SIZE];
875 static time_t osd_last_time;
880 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
881 osd_care = (global_options.osd >= 0);
882 CLIST_FOR_EACH(struct option_node *, o, options)
885 leds_care |= (1 << o->o.led);
890 if (!leds_care && !osd_care)
892 debug("X11: No mailbox wants LEDs or OSD\n");
895 if (!getenv("DISPLAY"))
897 debug("X11: Do not have X display\n");
900 if (!(x11_dpy = XOpenDisplay(NULL)))
901 die("Cannot open X display, although the DISPLAY variable is set");
905 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
907 die("Cannot intern OSD_QUEUE atom");
909 // If OSD options contain no message, add one
911 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
916 add_osd_opt("=%40f");
917 add_osd_opt("=%40s");
919 add_osd_opt("=(and %m more)");
924 debug("X11: Initialized\n");
930 if (leds_want == leds_have)
933 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
934 for (int i=1; i<10; i++)
935 if (leds_care & (leds_have ^ leds_want) & (1 << i))
939 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
940 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
943 leds_have = leds_want;
949 if (!leds_care || !x11_dpy)
953 CLIST_FOR_EACH(struct mbox *, b, mboxes)
954 if (b->o.led > 0 && b->new)
955 leds_want |= (1 << b->o.led);
965 format_osd_string(char *dest, char *src, struct osd_params *par)
967 char *stop = dest + OSD_MSG_SIZE - 1;
970 while (*src && dest < stop)
976 while (*src >= '0' && *src <= '9')
977 size = 10*size + *src++ - '0';
978 if (!size || size > stop-dest)
989 arg = par->mbox->sender_snippet;
992 arg = par->mbox->subject_snippet;
995 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new);
998 if (par->total_new < 2)
1000 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new - 1);
1010 while (*arg && size)
1024 format_osd(char *msg, struct osd_params *par)
1033 unsigned have_text = 0;
1034 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
1036 char buf[OSD_MSG_SIZE];
1037 if (!format_osd_string(buf, n->val, par))
1039 if (!n->key[0] && buf[0])
1041 pos += snprintf(msg+pos, OSD_MSG_SIZE-pos-1, "%s:%s\n", n->key, buf);
1042 if (pos > OSD_MSG_SIZE-1)
1044 pos = sprintf(msg, "OSD message too long!\n");
1056 debug_osd_msg(char *msg)
1060 fprintf(stderr, "OSD: <");
1063 fputc((*msg != '\n' ? *msg : '|'), stderr);
1066 fprintf(stderr, ">\n");
1070 rethink_osd(int notify)
1072 if (!osd_care || !x11_dpy || !allow_osd)
1074 osd_last_msg[0] = 0;
1078 struct osd_params p = { .mbox = NULL, .total_new = 0 };
1079 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1082 p.total_new += b->new;
1083 if (b->new && b->best_time > osd_last_time && (!p.mbox || p.mbox->o.priority < b->o.priority))
1087 char new_msg[OSD_MSG_SIZE];
1088 format_osd(new_msg, &p);
1089 debug_osd_msg(new_msg);
1090 if (strcmp(new_msg, osd_last_msg))
1092 strcpy(osd_last_msg, new_msg);
1093 if (notify && new_msg[0])
1095 debug("OSD: Sending to daemon\n");
1096 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) new_msg, strlen(new_msg));
1100 debug("OSD: No changes\n");
1101 osd_last_time = time(NULL);
1117 static void x11_init(void) { }
1118 static void rethink_leds(void) { }
1119 static void rethink_osd(int notify UNUSED) { }
1120 static void x11_cleanup(void) { }
1124 static int cursor_at, cursor_max;
1126 static unsigned is_active, is_pos; // incremental search
1127 static char is_buf[64];
1138 static int attrs[3][2][M_MAX]; // active (2=incsearch), hilite, status
1146 struct mbox *b = mbox_array[i];
1147 int cc = (cursor_at == i ? (is_active ? 2 : 1) : 0);
1148 int hi = b->o.highlight;
1149 unsigned namepos = 0;
1150 unsigned namelen = strlen(b->name);
1153 attrset(attrs[cc][hi][M_IDLE]);
1155 printw("%c ", b->o.hotkey);
1162 attrset(attrs[cc][hi][M_INCSEARCH]);
1163 for (namepos=0; namepos < is_pos && namepos < 20; namepos++)
1164 addch(is_buf[namepos]);
1167 attrset(attrs[cc][hi][M_NEW]);
1168 else if (b->flagged && b->o.show_flagged)
1169 attrset(attrs[cc][hi][M_FLAG]);
1171 attrset(attrs[cc][hi][M_IDLE]);
1172 while (namepos < namelen)
1173 addch(b->name[namepos++]);
1174 while (namepos++ < 20)
1176 if (b->scanning < 0)
1178 else if (b->scanning)
1180 attrset(attrs[cc][hi][M_SCAN]);
1181 printw("[SCANNING]");
1183 else if (b->total < 0)
1185 attrset(attrs[cc][hi][M_BAD]);
1190 attrset(attrs[cc][hi][M_IDLE]);
1191 printw("%6d ", b->total);
1195 attrset(attrs[cc][hi][M_NEW]);
1196 printw("%6d ", b->new);
1197 attrset(attrs[cc][hi][M_IDLE]);
1198 int age = (last_scan_time - b->best_time);
1203 printw("%2d min ", age/60);
1206 else if (age < 86400)
1207 printw("%2d hr%c ", age/3600, (age >= 7200 ? 's' : ' '));
1212 else if (b->flagged && b->o.show_flagged)
1214 attrset(attrs[cc][hi][M_FLAG]);
1215 printw("%6d ", b->flagged);
1216 attrset(attrs[cc][hi][M_IDLE]);
1218 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
1221 if (snip && b->o.snippets)
1224 getyx(stdscr, yy, xx);
1225 int remains = COLS-1-xx;
1229 build_snippet(snip, snip + sizeof(snip) - 1, b);
1231 if (snip[0] && remains > 2)
1233 #ifdef CONFIG_WIDE_CURSES
1234 size_t len = strlen(snip)+1;
1236 mbstowcs(snip2, snip, len);
1237 addnwstr(snip2, remains);
1239 printw("%-.*s", remains, snip);
1244 b->display_valid_until = last_scan_time + valid;
1246 attrset(attrs[0][0][M_IDLE]);
1253 cursor_max = num_mboxes;
1254 if (cursor_max > LINES-1)
1255 cursor_max = LINES-1;
1256 if (cursor_at >= cursor_max)
1257 cursor_at = cursor_max - 1;
1261 for (int i=0; i<cursor_max; i++)
1263 move(cursor_max, 0);
1266 printw("(no mailboxes found)");
1274 rethink_display(int notify)
1279 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1280 if (mbox_visible_p(b))
1283 if (i >= num_mboxes || mbox_array[i] != b)
1286 if (i >= mbox_array_size)
1288 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
1289 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
1293 if (b->o.beep && b->new > b->last_beep_new)
1295 b->last_beep_new = b->new;
1298 if (i != num_mboxes)
1308 rethink_osd(notify);
1309 if (beeeep && allow_bells && notify)
1320 intrflush(stdscr, FALSE);
1321 keypad(stdscr, TRUE);
1324 static const int attrs_mono[2][M_MAX] = {
1325 [0] = { [M_IDLE] = 0,
1330 [M_INCSEARCH] = A_REVERSE },
1331 [1] = { [M_IDLE] = 0,
1333 [M_NEW] = A_REVERSE | A_BOLD,
1334 [M_FLAG] = A_REVERSE,
1336 [M_INCSEARCH] = A_REVERSE },
1338 for (int i=0; i<2; i++)
1339 for (int j=0; j<M_MAX; j++)
1341 attrs[0][i][j] = attrs_mono[i][j];
1342 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1343 attrs[2][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1349 if (COLOR_PAIRS >= 12)
1351 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
1352 init_pair(2, COLOR_RED, COLOR_BLACK);
1353 init_pair(3, COLOR_WHITE, COLOR_BLUE);
1354 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
1355 init_pair(5, COLOR_RED, COLOR_BLUE);
1356 init_pair(6, COLOR_GREEN, COLOR_BLACK);
1357 init_pair(7, COLOR_GREEN, COLOR_BLUE);
1358 init_pair(8, COLOR_WHITE, COLOR_MAGENTA);
1359 init_pair(9, COLOR_YELLOW, COLOR_MAGENTA);
1360 init_pair(10, COLOR_GREEN, COLOR_MAGENTA);
1361 init_pair(11, COLOR_RED, COLOR_MAGENTA);
1362 init_pair(12, COLOR_BLACK, COLOR_YELLOW);
1363 static const int attrs_color[3][2][M_MAX] = {
1364 [0][0] = { [M_IDLE] = 0,
1365 [M_SCAN] = COLOR_PAIR(1),
1366 [M_NEW] = COLOR_PAIR(1),
1367 [M_FLAG] = COLOR_PAIR(6),
1368 [M_BAD] = COLOR_PAIR(2) },
1369 [0][1] = { [M_IDLE] = A_BOLD,
1370 [M_SCAN] = COLOR_PAIR(1),
1371 [M_NEW] = COLOR_PAIR(1) | A_BOLD,
1372 [M_FLAG] = COLOR_PAIR(6) | A_BOLD,
1373 [M_BAD] = COLOR_PAIR(2) | A_BOLD },
1374 [1][0] = { [M_IDLE] = COLOR_PAIR(3),
1375 [M_SCAN] = COLOR_PAIR(4),
1376 [M_NEW] = COLOR_PAIR(4),
1377 [M_FLAG] = COLOR_PAIR(7),
1378 [M_BAD] = COLOR_PAIR(5) },
1379 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD,
1380 [M_SCAN] = COLOR_PAIR(4),
1381 [M_NEW] = COLOR_PAIR(4) | A_BOLD,
1382 [M_FLAG] = COLOR_PAIR(7) | A_BOLD,
1383 [M_BAD] = COLOR_PAIR(5) | A_BOLD },
1384 [2][0] = { [M_IDLE] = COLOR_PAIR(8),
1385 [M_SCAN] = COLOR_PAIR(9),
1386 [M_NEW] = COLOR_PAIR(9),
1387 [M_FLAG] = COLOR_PAIR(10),
1388 [M_BAD] = COLOR_PAIR(11),
1389 [M_INCSEARCH] = COLOR_PAIR(12) | A_DIM },
1390 [2][1] = { [M_IDLE] = COLOR_PAIR(8) | A_BOLD,
1391 [M_SCAN] = COLOR_PAIR(9),
1392 [M_NEW] = COLOR_PAIR(9) | A_BOLD,
1393 [M_FLAG] = COLOR_PAIR(10) | A_BOLD,
1394 [M_BAD] = COLOR_PAIR(11) | A_BOLD,
1395 [M_INCSEARCH] = COLOR_PAIR(12) },
1397 memcpy(attrs, attrs_color, sizeof(attrs));
1409 print_status(char *status)
1413 printw("%s", status);
1419 scan_and_redraw(int notify)
1421 print_status("Busy...");
1429 if (i >= 0 && i < cursor_max && i != cursor_at)
1431 int old = cursor_at;
1439 next_active(int since, int step)
1443 since = (since+cursor_max) % cursor_max;
1444 step = (step+cursor_max) % cursor_max;
1450 struct mbox *b = mbox_array[i];
1461 if (b->new && b->o.priority > bestp)
1464 bestp = b->o.priority;
1467 i = (i+step) % cursor_max;
1475 mbox_run(struct mbox *b)
1477 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
1478 sprintf(cmd, run_cmd, b->path);
1484 b->force_refresh = 1;
1489 enter_incsearch(void)
1491 print_status("Incremental search...");
1494 redraw_line(cursor_at);
1498 handle_incsearch(int ch)
1500 if ((ch == KEY_BACKSPACE || ch == KEY_DC) && is_pos)
1502 else if (ch >= ' ' && ch <= '~')
1504 if (is_pos < sizeof(is_buf) - 1)
1505 is_buf[is_pos++] = ch;
1512 redraw_line(cursor_at);
1517 for (int i=0; i<cursor_max; i++)
1519 struct mbox *b = mbox_array[i];
1520 if (!strncmp(b->name, is_buf, is_pos))
1531 redraw_line(cursor_at);
1536 #define STR(c) STR2(c)
1541 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
1544 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
1545 -d\t\t\tLog debug messages to stderr\n\
1546 -i\t\t\tInclude user's INBOX\n\
1547 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
1548 -o <pattern>=<opts>\tSet mailbox options\n\
1549 -o <opts>\t\tSet default options for all mailboxes\n\
1550 -p <pri>\t\tSet minimum priority to show\n\
1551 -s <key>=<val>\t\tSet on-screen display options (consult OSDD docs)\n\
1552 -t\t\t\tLet TAB select the next mailbox with new mail, no matter what priority it has\n\
1554 Mailbox options (set with `-o', use upper case to negate):\n\
1555 0-9\t\t\tSet mailbox priority (0=default)\n\
1556 b\t\t\tBeep when a message arrives\n\
1557 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1558 e\t\t\tHide from display if empty\n\
1559 f\t\t\tShow flagged messages if there are no new ones\n\
1560 h\t\t\tHide from display\n\
1561 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1562 m\t\t\tShow mailbox name of the sender\n\
1563 o\t\t\tCount old, but unread messages as new\n\
1564 p\t\t\tShow personal info (full name) of the sender\n\
1565 r<int>\t\t\tSort order (default=1000)\n\
1566 s\t\t\tShow message snippets\n\
1567 t\t\t\tHighlight the entry\n\
1568 !<key>\t\t\tSet hot key\n\
1570 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>\n\
1571 It can be freely distributed and used according to the GNU GPL v2.\n\
1577 parse_options(char *c)
1581 if (sep = strchr(c, '='))
1583 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1584 memcpy(n->pattern, c, sep-c);
1585 n->pattern[sep-c] = 0;
1586 clist_add_tail(&options, &n->n);
1592 o = &global_options;
1596 if (x >= '0' && x <= '9')
1597 o->priority = x - '0';
1598 else if (x == '!' && *c)
1600 else if (x == 'l' && *c >= '1' && *c <= '9')
1601 o->led = *c++ - '0';
1602 else if (x == 'r' && *c >= '0' && *c <= '9')
1605 while (*c >= '0' && *c <= '9')
1606 o->sort_order = 10*o->sort_order + *c++ - '0';
1610 int value = !!islower(x);
1620 o->hide_if_empty = value;
1623 o->show_flagged = value;
1629 o->sender_mbox = value;
1632 o->unread_is_new = value;
1635 o->sender_personal = value;
1638 o->snippets = value;
1641 o->highlight = value;
1644 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
1651 main(int argc, char **argv)
1653 clist_init(&mboxes);
1654 clist_init(&options);
1655 clist_init(&patterns);
1656 clist_init(&osd_opts);
1659 while ((c = getopt(argc, argv, "c:dim:o:p:s:t")) >= 0)
1663 check_interval = atol(optarg);
1664 if (check_interval <= 0)
1677 parse_options(optarg);
1680 minimum_priority = atol(optarg);
1683 add_osd_opt(optarg);
1691 while (optind < argc)
1692 add_pattern(argv[optind++]);
1700 int should_exit = 0;
1702 while (!should_exit)
1704 time_t now = time(NULL);
1705 int remains = last_scan_time + check_interval - now;
1713 halfdelay((remains > 255) ? 255 : remains);
1717 if (is_active && handle_incsearch(ch))
1722 for (int i=0; i<num_mboxes; i++)
1723 if (ch == mbox_array[i]->o.hotkey)
1727 mbox_run(mbox_array[i]);
1737 move_cursor(cursor_at+1);
1741 move_cursor(cursor_at-1);
1751 move_cursor(cursor_max-1);
1754 next_active(cursor_at+1, 1);
1757 next_active(cursor_at-1, -1);
1761 if (cursor_at < cursor_max)
1762 mbox_run(mbox_array[cursor_at]);
1765 clearok(stdscr, TRUE);
1774 print_status("Bells and whistles are now enabled. Toot!");
1778 print_status("Bells and whistles are now disabled. Pssst!");
1782 print_status("On-screen display is now enabled.");
1786 print_status("On-screen display is now disabled. Watch your step.");
1792 if (ch >= '0' && ch <= '9')
1794 minimum_priority = ch - '0';
1798 debug("Pressed unknown key %d\n", ch);