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, '/');
215 new_mbox(char *path, char *name)
217 struct mbox *b = xmalloc(sizeof(*b));
218 bzero(b, sizeof(*b));
219 b->path = xstrdup(path);
220 b->name = xstrdup(name);
225 add_mbox(clist *l, struct mbox *b)
229 cnode *prev = l->head.prev;
230 struct mbox *prev_mbox;
231 while (prev != &l->head &&
232 ((prev_mbox = (struct mbox *)prev)->o.sort_order > b->o.sort_order ||
233 prev_mbox->o.sort_order == b->o.sort_order && strcmp(((struct mbox *)prev)->name, b->name) > 0))
235 clist_insert_after(&b->n, prev);
238 clist_add_tail(l, &b->n);
242 del_mbox(struct mbox *b)
251 find_mbox(clist *l, char *path)
253 CLIST_FOR_EACH(struct mbox *, b, *l)
254 if (!strcmp(b->path, path))
260 mbox_active_p(struct mbox *b)
262 if (b->o.priority < minimum_priority)
270 mbox_visible_p(struct mbox *b)
272 if (!mbox_active_p(b))
276 if (b->o.hide_if_empty && !b->total)
282 prepare_snippets(struct mbox *b, char *sender, char *subject)
286 while (*sender == ' ' || *sender == '\t')
288 while (*subject == ' ' || *subject == '\t')
291 pos = b->sender_snippet;
292 term = pos + sizeof(b->sender_snippet) - 1;
293 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
294 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
298 pos = b->subject_snippet;
299 term = pos + sizeof(b->subject_snippet) - 1;
301 add_subject_snippet(&pos, term, subject);
303 add_snippet_raw(&pos, term, "No subject");
307 build_snippet(char *buf, char *term, struct mbox *b)
309 if (b->sender_snippet[0])
311 add_snippet(&buf, term, b->sender_snippet);
312 add_snippet_raw(&buf, term, ": ");
314 add_snippet(&buf, term, b->subject_snippet);
317 static int mb_fd, mb_pos, mb_seekable;
318 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
323 mb_cc = mb_end = mb_buf;
330 lseek(mb_fd, pos, SEEK_SET);
337 return mb_pos - (mb_end - mb_cc);
343 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
352 mb_end = mb_buf + len;
361 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
372 mb_check(const char *p, int len)
376 if (mb_get() != *p++)
387 int avail = mb_end - mb_cc;
390 if (mb_seekable && len >= (int) sizeof(mb_buf))
392 int next = len - len % sizeof(mb_buf);
393 mb_seek(mb_tell() + next);
403 int next = (avail < len) ? avail : len;
410 #define HDR_BUF_SIZE 1024
413 read_hdr_field(char *buf)
421 debug("[truncated] ");
434 while (c == ' ' || c == '\t');
442 if (i < HDR_BUF_SIZE - 1)
447 debug("Header: <%s>\n", buf);
448 return buf[0] ? 1 : 0;
452 scan_mbox(struct mbox *b, struct stat *st)
454 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
457 const char from[] = "\nFrom ";
459 b->best_time = st->st_mtime;
462 b->total = b->new = b->flagged = 0;
467 mb_fd = open(b->path, O_RDONLY);
470 debug("[open failed: %m] ");
471 b->total = b->new = b->flagged = -1;
477 c = read(mb_fd, signature, 2);
478 lseek(mb_fd, 0, SEEK_SET);
480 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
482 debug("[decompressing] ");
485 die("pipe failed: %m");
488 die("fork failed: %m");
491 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
492 die("dup2 failed: %m");
496 execlp("gzip", "gzip", "-cd", NULL);
497 die("Cannot execute gzip: %m");
508 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && mb_seekable)
510 mb_seek(b->last_pos);
511 if (mb_check(from, 6))
513 debug("[incremental] ");
518 debug("[incremental failed] ");
524 if (!mb_check(from+1, 5))
526 debug("[inconsistent] ");
527 b->total = b->new = b->flagged = -1;
530 b->total = b->new = b->flagged = 0;
531 b->last_total = b->last_new = b->last_flagged = 0;
536 b->total = b->last_total;
537 b->new = b->last_new;
538 b->flagged = b->last_flagged;
543 b->last_pos = mb_tell() - 5;
545 b->last_pos--; // last_pos should be the previous \n character
546 b->last_total = b->total;
547 b->last_new = b->new;
548 b->last_flagged = b->flagged;
549 while ((c = mb_get()) >= 0 && c != '\n')
552 int content_length = -1;
559 int c = read_hdr_field(buf);
564 if (!strncasecmp(buf, "Status:", 7))
566 if (!b->o.unread_is_new || strchr(buf + 7, 'R'))
569 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
571 else if (!strncasecmp(buf, "From:", 5))
572 strcpy(sender, buf+5);
573 else if (!strncasecmp(buf, "Subject:", 8))
574 strcpy(subject, buf+8);
575 else if (!strncasecmp(buf, "Content-Length:", 15))
576 content_length = atoi(buf + 15);
585 debug("new=%d flagged=%d len=%d sender=<%s> subject=<%s>\n", new, flagged, content_length, sender, subject);
586 if (new || (flagged && !b->best_is_new))
588 b->best_is_new = new;
589 prepare_snippets(b, sender, subject);
592 if (content_length >= 0)
593 mb_skip(content_length);
611 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
612 b->total = b->new = b->flagged = -1;
617 mdir_mtime(struct mbox *b)
620 char path[strlen(b->path) + 5];
622 for (int new=0; new<2; new++)
624 sprintf(path, "%s/%s", b->path, (new ? "new" : "cur"));
626 if (stat(path, &st) < 0)
627 debug("[cannot stat %s] ", path);
628 else if (st.st_mtime > mtime)
635 mdir_get_snippet(struct mbox *b)
637 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
641 char path[strlen(b->path) + MDIR_MAX_NAME_LEN];
642 sprintf(path, "%s/%s", b->path, b->mdir_best);
643 mb_fd = open(path, O_RDONLY);
646 debug("[open failed: %m] ");
647 prepare_snippets(b, sender, subject);
653 while (read_hdr_field(buf) > 0)
655 if (!strncasecmp(buf, "From:", 5))
656 strcpy(sender, buf+5);
657 else if (!strncasecmp(buf, "Subject:", 8))
658 strcpy(subject, buf+8);
662 prepare_snippets(b, sender, subject);
666 scan_mdir(struct mbox *b)
668 int dir_len = strlen(b->path);
669 char path[dir_len + MDIR_MAX_NAME_LEN];
670 strcpy(path, b->path);
672 b->total = b->new = b->flagged = 0;
676 for (int new=0; new<2; new++)
678 strcpy(path + dir_len, (new ? "/new" : "/cur"));
679 DIR *d = opendir(path);
682 debug("[cannot open %s: %m] ");
686 while (de = readdir(d))
688 char *name = de->d_name;
689 if (name[0] == '.' || strlen(name) + 10 > MDIR_MAX_NAME_LEN)
691 path[dir_len + 4] = '/';
692 strcpy(path + dir_len + 5, name);
694 char *colon = strchr(name, ':');
697 if (colon && colon[1] == '2' && colon[2] == ',')
699 // Another comma can separate extended flags (Dovecot extension)
700 for (int i=3; colon[i] && colon[i] != ','; i++)
713 if (b->o.unread_is_new && !seen)
722 debug("%s: new=%d flagged=%d seen=%d\n", path, is_new, flagged, seen);
724 if (is_new || flagged)
726 // Need to pick the best message to display
728 if (stat(path, &st) < 0)
729 debug("[cannot stat %s: %m] ", path);
730 else if (is_new > b->best_is_new || is_new == b->best_is_new && st.st_mtime > b->best_time)
732 b->best_is_new = is_new;
733 b->best_time = st.st_mtime;
734 strcpy(b->mdir_best, path + dir_len + 1);
741 if (b->best_time && b->o.snippets)
743 debug("best <%s> ", b->mdir_best);
751 debug("Searching for mailboxes (notify=%d)...\n", notify);
752 last_scan_time = time(NULL);
753 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
755 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
757 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
758 if (err && err != GLOB_NOMATCH)
759 die("Failed to glob %s: %m", p->pattern);
760 for (uns i=0; i<g.gl_pathc; i++)
762 char *name = g.gl_pathv[i];
763 struct mbox *b = find_mbox(&mboxes, name);
766 b = new_mbox(name, (p->name ? p->name : mbox_name(name)));
767 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
769 add_mbox(&mboxes, b);
778 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
784 debug("Lost mailbox %s\n", b->name);
791 debug("Scanning mailboxes...\n");
792 CLIST_FOR_EACH(struct mbox *, b, mboxes)
795 debug("%s: ", b->name);
796 if (!mbox_active_p(b))
802 b->force_refresh = 1;
803 if (stat(b->path, &st) < 0)
805 b->total = b->new = b->flagged = -1;
810 time_t current_mtime;
813 if (S_ISREG(st.st_mode))
817 current_mtime = st.st_mtime;
818 current_size = st.st_size;
821 else if (S_ISDIR(st.st_mode))
825 current_mtime = mdir_mtime(b);
831 debug("neither file nor directory\n");
835 if (!b->last_time || current_mtime != b->last_time || current_size != b->last_size || b->force_refresh)
838 redraw_line(b->index);
845 b->last_time = current_mtime;
846 b->last_size = current_size;
847 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
850 redraw_line(b->index);
852 b->force_refresh = 0;
854 else if (b->display_valid_until <= last_scan_time)
856 debug("not changed, but needs redraw\n");
857 redraw_line(b->index);
860 debug("not changed\n");
864 debug("Scan finished\n");
865 last_scan_time = time(NULL);
866 rethink_display(notify);
871 #include <X11/Xlib.h>
872 #include <X11/Xatom.h>
874 static Display *x11_dpy;
875 static unsigned leds_care, leds_have, leds_want;
878 static unsigned osd_care;
879 #define OSD_MSG_SIZE 1024
880 static char osd_last_msg[OSD_MSG_SIZE];
881 static time_t osd_last_time;
886 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
887 osd_care = (global_options.osd >= 0);
888 CLIST_FOR_EACH(struct option_node *, o, options)
891 leds_care |= (1 << o->o.led);
896 if (!leds_care && !osd_care)
898 debug("X11: No mailbox wants LEDs or OSD\n");
901 if (!getenv("DISPLAY"))
903 debug("X11: Do not have X display\n");
906 if (!(x11_dpy = XOpenDisplay(NULL)))
907 die("Cannot open X display, although the DISPLAY variable is set");
911 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
913 die("Cannot intern OSD_QUEUE atom");
915 // If OSD options contain no message, add one
917 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
922 add_osd_opt("=%40f");
923 add_osd_opt("=%40s");
925 add_osd_opt("=(and %m more)");
930 debug("X11: Initialized\n");
936 if (leds_want == leds_have)
939 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
940 for (int i=1; i<10; i++)
941 if (leds_care & (leds_have ^ leds_want) & (1 << i))
945 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
946 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
949 leds_have = leds_want;
955 if (!leds_care || !x11_dpy)
959 CLIST_FOR_EACH(struct mbox *, b, mboxes)
960 if (b->o.led > 0 && b->new)
961 leds_want |= (1 << b->o.led);
971 format_osd_string(char *dest, char *src, struct osd_params *par)
973 char *stop = dest + OSD_MSG_SIZE - 1;
976 while (*src && dest < stop)
982 while (*src >= '0' && *src <= '9')
983 size = 10*size + *src++ - '0';
984 if (!size || size > stop-dest)
995 arg = par->mbox->sender_snippet;
998 arg = par->mbox->subject_snippet;
1001 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new);
1004 if (par->total_new < 2)
1006 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new - 1);
1016 while (*arg && size)
1030 format_osd(char *msg, struct osd_params *par)
1039 unsigned have_text = 0;
1040 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
1042 char buf[OSD_MSG_SIZE];
1043 if (!format_osd_string(buf, n->val, par))
1045 if (!n->key[0] && buf[0])
1047 pos += snprintf(msg+pos, OSD_MSG_SIZE-pos-1, "%s:%s\n", n->key, buf);
1048 if (pos > OSD_MSG_SIZE-1)
1050 pos = sprintf(msg, "OSD message too long!\n");
1062 debug_osd_msg(char *msg)
1066 fprintf(stderr, "OSD: <");
1069 fputc((*msg != '\n' ? *msg : '|'), stderr);
1072 fprintf(stderr, ">\n");
1076 rethink_osd(int notify)
1078 if (!osd_care || !x11_dpy || !allow_osd)
1080 osd_last_msg[0] = 0;
1084 struct osd_params p = { .mbox = NULL, .total_new = 0 };
1085 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1088 p.total_new += b->new;
1089 if (b->new && b->best_time > osd_last_time && (!p.mbox || p.mbox->o.priority < b->o.priority))
1093 char new_msg[OSD_MSG_SIZE];
1094 format_osd(new_msg, &p);
1095 debug_osd_msg(new_msg);
1096 if (strcmp(new_msg, osd_last_msg))
1098 strcpy(osd_last_msg, new_msg);
1099 if (notify && new_msg[0])
1101 debug("OSD: Sending to daemon\n");
1102 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) new_msg, strlen(new_msg));
1106 debug("OSD: No changes\n");
1107 osd_last_time = time(NULL);
1123 static void x11_init(void) { }
1124 static void rethink_leds(void) { }
1125 static void rethink_osd(int notify UNUSED) { }
1126 static void x11_cleanup(void) { }
1130 static int cursor_at, cursor_max;
1132 static unsigned is_active, is_pos; // incremental search
1133 static char is_buf[64];
1144 static int attrs[3][2][M_MAX]; // active (2=incsearch), hilite, status
1152 struct mbox *b = mbox_array[i];
1153 int cc = (cursor_at == i ? (is_active ? 2 : 1) : 0);
1154 int hi = b->o.highlight;
1155 unsigned namepos = 0;
1156 unsigned namelen = strlen(b->name);
1159 attrset(attrs[cc][hi][M_IDLE]);
1161 printw("%c ", b->o.hotkey);
1168 attrset(attrs[cc][hi][M_INCSEARCH]);
1169 for (namepos=0; namepos < is_pos && namepos < 20; namepos++)
1170 addch(is_buf[namepos]);
1173 attrset(attrs[cc][hi][M_NEW]);
1174 else if (b->flagged && b->o.show_flagged)
1175 attrset(attrs[cc][hi][M_FLAG]);
1177 attrset(attrs[cc][hi][M_IDLE]);
1178 while (namepos < namelen)
1179 addch(b->name[namepos++]);
1180 while (namepos++ < 20)
1182 if (b->scanning < 0)
1184 else if (b->scanning)
1186 attrset(attrs[cc][hi][M_SCAN]);
1187 printw("[SCANNING]");
1189 else if (b->total < 0)
1191 attrset(attrs[cc][hi][M_BAD]);
1196 attrset(attrs[cc][hi][M_IDLE]);
1197 printw("%6d ", b->total);
1201 attrset(attrs[cc][hi][M_NEW]);
1202 printw("%6d ", b->new);
1203 attrset(attrs[cc][hi][M_IDLE]);
1204 int age = (last_scan_time - b->best_time);
1209 printw("%2d min ", age/60);
1212 else if (age < 86400)
1213 printw("%2d hr%c ", age/3600, (age >= 7200 ? 's' : ' '));
1218 else if (b->flagged && b->o.show_flagged)
1220 attrset(attrs[cc][hi][M_FLAG]);
1221 printw("%6d ", b->flagged);
1222 attrset(attrs[cc][hi][M_IDLE]);
1224 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
1227 if (snip && b->o.snippets)
1230 getyx(stdscr, yy, xx);
1231 int remains = COLS-1-xx;
1235 build_snippet(snip, snip + sizeof(snip) - 1, b);
1237 if (snip[0] && remains > 2)
1239 #ifdef CONFIG_WIDE_CURSES
1240 size_t len = strlen(snip)+1;
1242 mbstowcs(snip2, snip, len);
1243 addnwstr(snip2, remains);
1245 printw("%-.*s", remains, snip);
1250 b->display_valid_until = last_scan_time + valid;
1252 attrset(attrs[0][0][M_IDLE]);
1259 cursor_max = num_mboxes;
1260 if (cursor_max > LINES-1)
1261 cursor_max = LINES-1;
1262 if (cursor_at >= cursor_max)
1263 cursor_at = cursor_max - 1;
1267 for (int i=0; i<cursor_max; i++)
1269 move(cursor_max, 0);
1272 printw("(no mailboxes found)");
1280 rethink_display(int notify)
1285 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1286 if (mbox_visible_p(b))
1289 if (i >= num_mboxes || mbox_array[i] != b)
1292 if (i >= mbox_array_size)
1294 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
1295 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
1299 if (b->o.beep && b->new > b->last_beep_new)
1301 b->last_beep_new = b->new;
1304 if (i != num_mboxes)
1314 rethink_osd(notify);
1315 if (beeeep && allow_bells && notify)
1326 intrflush(stdscr, FALSE);
1327 keypad(stdscr, TRUE);
1330 static const int attrs_mono[2][M_MAX] = {
1331 [0] = { [M_IDLE] = 0,
1336 [M_INCSEARCH] = A_REVERSE },
1337 [1] = { [M_IDLE] = 0,
1339 [M_NEW] = A_REVERSE | A_BOLD,
1340 [M_FLAG] = A_REVERSE,
1342 [M_INCSEARCH] = A_REVERSE },
1344 for (int i=0; i<2; i++)
1345 for (int j=0; j<M_MAX; j++)
1347 attrs[0][i][j] = attrs_mono[i][j];
1348 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1349 attrs[2][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1355 if (COLOR_PAIRS >= 12)
1357 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
1358 init_pair(2, COLOR_RED, COLOR_BLACK);
1359 init_pair(3, COLOR_WHITE, COLOR_BLUE);
1360 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
1361 init_pair(5, COLOR_RED, COLOR_BLUE);
1362 init_pair(6, COLOR_GREEN, COLOR_BLACK);
1363 init_pair(7, COLOR_GREEN, COLOR_BLUE);
1364 init_pair(8, COLOR_WHITE, COLOR_MAGENTA);
1365 init_pair(9, COLOR_YELLOW, COLOR_MAGENTA);
1366 init_pair(10, COLOR_GREEN, COLOR_MAGENTA);
1367 init_pair(11, COLOR_RED, COLOR_MAGENTA);
1368 init_pair(12, COLOR_BLACK, COLOR_YELLOW);
1369 static const int attrs_color[3][2][M_MAX] = {
1370 [0][0] = { [M_IDLE] = 0,
1371 [M_SCAN] = COLOR_PAIR(1),
1372 [M_NEW] = COLOR_PAIR(1),
1373 [M_FLAG] = COLOR_PAIR(6),
1374 [M_BAD] = COLOR_PAIR(2) },
1375 [0][1] = { [M_IDLE] = A_BOLD,
1376 [M_SCAN] = COLOR_PAIR(1),
1377 [M_NEW] = COLOR_PAIR(1) | A_BOLD,
1378 [M_FLAG] = COLOR_PAIR(6) | A_BOLD,
1379 [M_BAD] = COLOR_PAIR(2) | A_BOLD },
1380 [1][0] = { [M_IDLE] = COLOR_PAIR(3),
1381 [M_SCAN] = COLOR_PAIR(4),
1382 [M_NEW] = COLOR_PAIR(4),
1383 [M_FLAG] = COLOR_PAIR(7),
1384 [M_BAD] = COLOR_PAIR(5) },
1385 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD,
1386 [M_SCAN] = COLOR_PAIR(4),
1387 [M_NEW] = COLOR_PAIR(4) | A_BOLD,
1388 [M_FLAG] = COLOR_PAIR(7) | A_BOLD,
1389 [M_BAD] = COLOR_PAIR(5) | A_BOLD },
1390 [2][0] = { [M_IDLE] = COLOR_PAIR(8),
1391 [M_SCAN] = COLOR_PAIR(9),
1392 [M_NEW] = COLOR_PAIR(9),
1393 [M_FLAG] = COLOR_PAIR(10),
1394 [M_BAD] = COLOR_PAIR(11),
1395 [M_INCSEARCH] = COLOR_PAIR(12) | A_DIM },
1396 [2][1] = { [M_IDLE] = COLOR_PAIR(8) | A_BOLD,
1397 [M_SCAN] = COLOR_PAIR(9),
1398 [M_NEW] = COLOR_PAIR(9) | A_BOLD,
1399 [M_FLAG] = COLOR_PAIR(10) | A_BOLD,
1400 [M_BAD] = COLOR_PAIR(11) | A_BOLD,
1401 [M_INCSEARCH] = COLOR_PAIR(12) },
1403 memcpy(attrs, attrs_color, sizeof(attrs));
1415 print_status(char *status)
1419 printw("%s", status);
1425 scan_and_redraw(int notify)
1427 print_status("Busy...");
1435 if (i >= 0 && i < cursor_max && i != cursor_at)
1437 int old = cursor_at;
1445 next_active(int since, int step)
1449 since = (since+cursor_max) % cursor_max;
1450 step = (step+cursor_max) % cursor_max;
1456 struct mbox *b = mbox_array[i];
1467 if (b->new && b->o.priority > bestp)
1470 bestp = b->o.priority;
1473 i = (i+step) % cursor_max;
1481 mbox_run(struct mbox *b)
1483 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
1484 sprintf(cmd, run_cmd, b->path);
1490 b->force_refresh = 1;
1495 enter_incsearch(void)
1497 print_status("Incremental search...");
1500 redraw_line(cursor_at);
1504 handle_incsearch(int ch)
1506 if ((ch == KEY_BACKSPACE || ch == KEY_DC) && is_pos)
1508 else if (ch >= ' ' && ch <= '~')
1510 if (is_pos < sizeof(is_buf) - 1)
1511 is_buf[is_pos++] = ch;
1518 redraw_line(cursor_at);
1523 for (int i=0; i<cursor_max; i++)
1525 struct mbox *b = mbox_array[i];
1526 if (!strncmp(b->name, is_buf, is_pos))
1537 redraw_line(cursor_at);
1542 #define STR(c) STR2(c)
1547 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
1550 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
1551 -d\t\t\tLog debug messages to stderr\n\
1552 -i\t\t\tInclude user's INBOX\n\
1553 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
1554 -o <pattern>=<opts>\tSet mailbox options\n\
1555 -o <opts>\t\tSet default options for all mailboxes\n\
1556 -p <pri>\t\tSet minimum priority to show\n\
1557 -s <key>=<val>\t\tSet on-screen display options (consult OSDD docs)\n\
1558 -t\t\t\tLet TAB select the next mailbox with new mail, no matter what priority it has\n\
1560 Mailbox options (set with `-o', use upper case to negate):\n\
1561 0-9\t\t\tSet mailbox priority (0=default)\n\
1562 b\t\t\tBeep when a message arrives\n\
1563 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1564 e\t\t\tHide from display if empty\n\
1565 f\t\t\tShow flagged messages if there are no new ones\n\
1566 h\t\t\tHide from display\n\
1567 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1568 m\t\t\tShow mailbox name of the sender\n\
1569 o\t\t\tCount old, but unread messages as new\n\
1570 p\t\t\tShow personal info (full name) of the sender\n\
1571 r<int>\t\t\tSort order (default=1000)\n\
1572 s\t\t\tShow message snippets\n\
1573 t\t\t\tHighlight the entry\n\
1574 !<key>\t\t\tSet hot key\n\
1576 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>\n\
1577 It can be freely distributed and used according to the GNU GPL v2.\n\
1583 parse_options(char *c)
1587 if (sep = strchr(c, '='))
1589 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1590 memcpy(n->pattern, c, sep-c);
1591 n->pattern[sep-c] = 0;
1592 clist_add_tail(&options, &n->n);
1598 o = &global_options;
1602 if (x >= '0' && x <= '9')
1603 o->priority = x - '0';
1604 else if (x == '!' && *c)
1606 else if (x == 'l' && *c >= '1' && *c <= '9')
1607 o->led = *c++ - '0';
1608 else if (x == 'r' && *c >= '0' && *c <= '9')
1611 while (*c >= '0' && *c <= '9')
1612 o->sort_order = 10*o->sort_order + *c++ - '0';
1616 int value = !!islower(x);
1626 o->hide_if_empty = value;
1629 o->show_flagged = value;
1635 o->sender_mbox = value;
1638 o->unread_is_new = value;
1641 o->sender_personal = value;
1644 o->snippets = value;
1647 o->highlight = value;
1650 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
1657 main(int argc, char **argv)
1659 clist_init(&mboxes);
1660 clist_init(&options);
1661 clist_init(&patterns);
1662 clist_init(&osd_opts);
1665 while ((c = getopt(argc, argv, "c:dim:o:p:s:t")) >= 0)
1669 check_interval = atol(optarg);
1670 if (check_interval <= 0)
1683 parse_options(optarg);
1686 minimum_priority = atol(optarg);
1689 add_osd_opt(optarg);
1697 while (optind < argc)
1698 add_pattern(argv[optind++]);
1706 int should_exit = 0;
1708 while (!should_exit)
1710 time_t now = time(NULL);
1711 int remains = last_scan_time + check_interval - now;
1719 halfdelay((remains > 255) ? 255 : remains);
1723 if (is_active && handle_incsearch(ch))
1728 for (int i=0; i<num_mboxes; i++)
1729 if (ch == mbox_array[i]->o.hotkey)
1733 mbox_run(mbox_array[i]);
1743 move_cursor(cursor_at+1);
1747 move_cursor(cursor_at-1);
1757 move_cursor(cursor_max-1);
1760 next_active(cursor_at+1, 1);
1763 next_active(cursor_at-1, -1);
1767 if (cursor_at < cursor_max)
1768 mbox_run(mbox_array[cursor_at]);
1771 clearok(stdscr, TRUE);
1780 print_status("Bells and whistles are now enabled. Toot!");
1784 print_status("Bells and whistles are now disabled. Pssst!");
1788 print_status("On-screen display is now enabled.");
1792 print_status("On-screen display is now disabled. Watch your step.");
1798 if (ch >= '0' && ch <= '9')
1800 minimum_priority = ch - '0';
1804 debug("Pressed unknown key %d\n", ch);