2 * Incoming Mail Checker
4 * (c) 2005--2015 Martin Mares <mj@ucw.cz>
5 * (c) 2020--2021 Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
21 #include <sys/types.h>
27 #ifdef CONFIG_WIDE_CURSES
37 static int check_interval = 30;
38 static int force_refresh;
39 static int allow_bells = 1;
40 static int allow_osd = 1;
41 static int minimum_priority;
42 static time_t last_scan_time;
43 static char *run_cmd = "mutt -f %s";
44 static int simple_tab;
75 static clist options, patterns;
76 static struct options global_options = {
81 #define MDIR_MAX_NAME_LEN 128
92 enum inotify_status inotify_status;
101 time_t display_valid_until;
102 int last_size, last_pos;
103 int total, new, flagged;
104 int last_total, last_new, last_flagged;
108 char sender_snippet[128];
109 char subject_snippet[128];
110 char mdir_best[MDIR_MAX_NAME_LEN];
114 static struct mbox **mbox_array;
115 static int num_mboxes, mbox_array_size;
117 struct osd_opt_node {
123 static clist osd_opts;
125 static void redraw_line(int i);
126 static void rethink_display(int notify);
128 static int inotify_fd;
129 static bool inotify_active;
130 static void inotify_cm_init(void);
131 static void inotify_add_mbox(struct mbox * b);
132 static void inotify_delete_mbox(struct mbox * b);
135 add_pattern(char *patt)
137 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
138 strcpy(n->pattern, patt);
139 if (patt = strchr(n->pattern, '='))
146 clist_add_tail(&patterns, &n->n);
152 struct passwd *p = getpwuid(getuid());
154 die("You don't exist, go away!");
155 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
156 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
161 init_options(struct options *o)
165 o->hide_if_empty = -1;
169 o->show_flagged = -1;
170 o->sender_personal = -1;
175 o->unread_is_new = -1;
180 setup_options(struct mbox *b)
182 b->o = global_options;
183 CLIST_FOR_EACH(struct option_node *, n, options)
184 if (!fnmatch(n->pattern, b->name, 0))
186 debug("\tApplied options %s\n", n->pattern);
187 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
190 MERGE(hide_if_empty);
195 MERGE(sender_personal);
200 MERGE(unread_is_new);
206 add_osd_opt(char *arg)
208 struct osd_opt_node *n = xmalloc(sizeof(*n) + strlen(arg));
210 n->val = strchr(n->key, '=');
212 die("Malformed OSD option");
214 clist_add_tail(&osd_opts, &n->n);
218 mbox_name(char *path)
220 char *c = strrchr(path, '/');
231 new_mbox(char *path, char *name)
233 struct mbox *b = xmalloc(sizeof(*b));
234 bzero(b, sizeof(*b));
235 b->inotify_status = INOTIFY_OFF;
236 b->path = xstrdup(path);
237 b->name = xstrdup(name);
242 add_mbox(clist *l, struct mbox *b)
246 cnode *prev = l->head.prev;
247 struct mbox *prev_mbox;
248 while (prev != &l->head &&
249 ((prev_mbox = (struct mbox *)prev)->o.sort_order > b->o.sort_order ||
250 prev_mbox->o.sort_order == b->o.sort_order && strcmp(((struct mbox *)prev)->name, b->name) > 0))
252 clist_insert_after(&b->n, prev);
255 clist_add_tail(l, &b->n);
259 del_mbox(struct mbox *b)
268 find_mbox(clist *l, char *path)
270 CLIST_FOR_EACH(struct mbox *, b, *l)
271 if (!strcmp(b->path, path))
277 mbox_active_p(struct mbox *b)
279 if (b->o.priority < minimum_priority)
287 mbox_visible_p(struct mbox *b)
289 if (!mbox_active_p(b))
293 if (b->o.hide_if_empty && !b->total)
299 prepare_snippets(struct mbox *b, char *sender, char *subject)
303 while (*sender == ' ' || *sender == '\t')
305 while (*subject == ' ' || *subject == '\t')
308 pos = b->sender_snippet;
309 term = pos + sizeof(b->sender_snippet) - 1;
310 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
311 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
315 pos = b->subject_snippet;
316 term = pos + sizeof(b->subject_snippet) - 1;
318 add_subject_snippet(&pos, term, subject);
320 add_snippet_raw(&pos, term, "No subject");
324 build_snippet(char *buf, char *term, struct mbox *b)
326 if (b->sender_snippet[0])
328 add_snippet(&buf, term, b->sender_snippet);
329 add_snippet_raw(&buf, term, ": ");
331 add_snippet(&buf, term, b->subject_snippet);
334 static int mb_fd, mb_pos, mb_seekable;
335 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
340 mb_cc = mb_end = mb_buf;
347 lseek(mb_fd, pos, SEEK_SET);
354 return mb_pos - (mb_end - mb_cc);
360 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
369 mb_end = mb_buf + len;
378 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
389 mb_check(const char *p, int len)
393 if (mb_get() != *p++)
404 int avail = mb_end - mb_cc;
407 if (mb_seekable && len >= (int) sizeof(mb_buf))
409 int next = len - len % sizeof(mb_buf);
410 mb_seek(mb_tell() + next);
420 int next = (avail < len) ? avail : len;
427 #define HDR_BUF_SIZE 1024
430 read_hdr_field(char *buf)
438 debug("[truncated] ");
451 while (c == ' ' || c == '\t');
459 if (i < HDR_BUF_SIZE - 1)
464 debug("Header: <%s>\n", buf);
465 return buf[0] ? 1 : 0;
469 scan_mbox(struct mbox *b, struct stat *st)
471 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
474 const char from[] = "\nFrom ";
476 b->best_time = st->st_mtime;
479 b->total = b->new = b->flagged = 0;
484 mb_fd = open(b->path, O_RDONLY);
487 debug("[open failed: %m] ");
488 b->total = b->new = b->flagged = -1;
494 c = read(mb_fd, signature, 2);
495 lseek(mb_fd, 0, SEEK_SET);
497 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
499 debug("[decompressing] ");
502 die("pipe failed: %m");
505 die("fork failed: %m");
508 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
509 die("dup2 failed: %m");
513 execlp("gzip", "gzip", "-cd", NULL);
514 die("Cannot execute gzip: %m");
525 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && mb_seekable)
527 mb_seek(b->last_pos);
528 if (mb_check(from, 6))
530 debug("[incremental] ");
535 debug("[incremental failed] ");
541 if (!mb_check(from+1, 5))
543 debug("[inconsistent] ");
544 b->total = b->new = b->flagged = -1;
547 b->total = b->new = b->flagged = 0;
548 b->last_total = b->last_new = b->last_flagged = 0;
553 b->total = b->last_total;
554 b->new = b->last_new;
555 b->flagged = b->last_flagged;
560 b->last_pos = mb_tell() - 5;
562 b->last_pos--; // last_pos should be the previous \n character
563 b->last_total = b->total;
564 b->last_new = b->new;
565 b->last_flagged = b->flagged;
566 while ((c = mb_get()) >= 0 && c != '\n')
569 int content_length = -1;
576 int c = read_hdr_field(buf);
581 if (!strncasecmp(buf, "Status:", 7))
583 if (!b->o.unread_is_new || strchr(buf + 7, 'R'))
586 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
588 else if (!strncasecmp(buf, "From:", 5))
589 strcpy(sender, buf+5);
590 else if (!strncasecmp(buf, "Subject:", 8))
591 strcpy(subject, buf+8);
592 else if (!strncasecmp(buf, "Content-Length:", 15))
593 content_length = atoi(buf + 15);
602 debug("new=%d flagged=%d len=%d sender=<%s> subject=<%s>\n", new, flagged, content_length, sender, subject);
603 if (new || (flagged && !b->best_is_new))
605 b->best_is_new = new;
606 prepare_snippets(b, sender, subject);
609 if (content_length >= 0)
610 mb_skip(content_length);
628 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
629 b->total = b->new = b->flagged = -1;
634 mdir_mtime(struct mbox *b)
637 char path[strlen(b->path) + 5];
639 for (int new=0; new<2; new++)
641 sprintf(path, "%s/%s", b->path, (new ? "new" : "cur"));
643 if (stat(path, &st) < 0)
644 debug("[cannot stat %s] ", path);
645 else if (st.st_mtime > mtime)
652 mdir_get_snippet(struct mbox *b)
654 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
658 char path[strlen(b->path) + MDIR_MAX_NAME_LEN];
659 sprintf(path, "%s/%s", b->path, b->mdir_best);
660 mb_fd = open(path, O_RDONLY);
663 debug("[open failed: %m] ");
664 prepare_snippets(b, sender, subject);
670 while (read_hdr_field(buf) > 0)
672 if (!strncasecmp(buf, "From:", 5))
673 strcpy(sender, buf+5);
674 else if (!strncasecmp(buf, "Subject:", 8))
675 strcpy(subject, buf+8);
679 prepare_snippets(b, sender, subject);
683 scan_mdir(struct mbox *b)
685 int dir_len = strlen(b->path);
686 char path[dir_len + MDIR_MAX_NAME_LEN];
687 strcpy(path, b->path);
689 b->total = b->new = b->flagged = 0;
693 for (int new=0; new<2; new++)
695 strcpy(path + dir_len, (new ? "/new" : "/cur"));
696 DIR *d = opendir(path);
699 debug("[cannot open %s: %m] ");
703 while (de = readdir(d))
705 char *name = de->d_name;
706 if (name[0] == '.' || strlen(name) + 10 > MDIR_MAX_NAME_LEN)
708 path[dir_len + 4] = '/';
709 strcpy(path + dir_len + 5, name);
711 char *colon = strchr(name, ':');
714 if (colon && colon[1] == '2' && colon[2] == ',')
716 // Another comma can separate extended flags (Dovecot extension)
717 for (int i=3; colon[i] && colon[i] != ','; i++)
730 if (b->o.unread_is_new && !seen)
739 debug("%s: new=%d flagged=%d seen=%d\n", path, is_new, flagged, seen);
741 if (is_new || flagged)
743 // Need to pick the best message to display
745 if (stat(path, &st) < 0)
746 debug("[cannot stat %s: %m] ", path);
747 else if (is_new > b->best_is_new || is_new == b->best_is_new && st.st_mtime > b->best_time)
749 b->best_is_new = is_new;
750 b->best_time = st.st_mtime;
751 strcpy(b->mdir_best, path + dir_len + 1);
758 if (b->best_time && b->o.snippets)
760 debug("best <%s> ", b->mdir_best);
768 debug("Searching for mailboxes (notify=%d)...\n", notify);
769 last_scan_time = time(NULL);
770 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
772 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
774 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
775 if (err && err != GLOB_NOMATCH)
776 die("Failed to glob %s: %m", p->pattern);
777 for (uns i=0; i<g.gl_pathc; i++)
779 char *name = g.gl_pathv[i];
780 struct mbox *b = find_mbox(&mboxes, name);
783 b = new_mbox(name, (p->name ? p->name : mbox_name(name)));
784 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
786 add_mbox(&mboxes, b);
796 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
798 if(b->inotify_status != INOTIFY_ON)
803 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
809 debug("Lost mailbox %s\n", b->name);
810 inotify_delete_mbox(b);
817 debug("Scanning mailboxes...\n");
818 CLIST_FOR_EACH(struct mbox *, b, mboxes)
821 debug("%s: ", b->name);
822 if (!mbox_active_p(b))
828 b->force_refresh = 1;
829 if (stat(b->path, &st) < 0)
831 b->total = b->new = b->flagged = -1;
836 time_t current_mtime;
839 if (S_ISREG(st.st_mode))
843 current_mtime = st.st_mtime;
844 current_size = st.st_size;
847 else if (S_ISDIR(st.st_mode))
851 current_mtime = mdir_mtime(b);
857 debug("neither file nor directory\n");
861 if (!b->last_time || current_mtime != b->last_time || current_size != b->last_size || b->force_refresh)
864 redraw_line(b->index);
871 b->last_time = current_mtime;
872 b->last_size = current_size;
873 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
876 redraw_line(b->index);
878 b->force_refresh = 0;
880 else if (b->display_valid_until <= last_scan_time)
882 debug("not changed, but needs redraw\n");
883 redraw_line(b->index);
886 debug("not changed\n");
890 debug("Scan finished\n");
891 last_scan_time = time(NULL);
892 rethink_display(notify);
897 #include <X11/Xlib.h>
898 #include <X11/Xatom.h>
900 static Display *x11_dpy;
901 static unsigned leds_care, leds_have, leds_want;
904 static unsigned osd_care;
905 #define OSD_MSG_SIZE 1024
906 static char osd_last_msg[OSD_MSG_SIZE];
907 static time_t osd_last_time;
912 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
913 osd_care = (global_options.osd >= 0);
914 CLIST_FOR_EACH(struct option_node *, o, options)
917 leds_care |= (1 << o->o.led);
922 if (!leds_care && !osd_care)
924 debug("X11: No mailbox wants LEDs or OSD\n");
927 if (!getenv("DISPLAY"))
929 debug("X11: Do not have X display\n");
932 if (!(x11_dpy = XOpenDisplay(NULL)))
933 die("Cannot open X display, although the DISPLAY variable is set");
937 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
939 die("Cannot intern OSD_QUEUE atom");
941 // If OSD options contain no message, add one
943 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
948 add_osd_opt("=%40f");
949 add_osd_opt("=%40s");
951 add_osd_opt("=(and %m more)");
956 debug("X11: Initialized\n");
962 if (leds_want == leds_have)
965 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
966 for (int i=1; i<10; i++)
967 if (leds_care & (leds_have ^ leds_want) & (1 << i))
971 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
972 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
975 leds_have = leds_want;
981 if (!leds_care || !x11_dpy)
985 CLIST_FOR_EACH(struct mbox *, b, mboxes)
986 if (b->o.led > 0 && b->new)
987 leds_want |= (1 << b->o.led);
997 format_osd_string(char *dest, char *src, struct osd_params *par)
999 char *stop = dest + OSD_MSG_SIZE - 1;
1002 while (*src && dest < stop)
1008 while (*src >= '0' && *src <= '9')
1009 size = 10*size + *src++ - '0';
1010 if (!size || size > stop-dest)
1021 arg = par->mbox->sender_snippet;
1024 arg = par->mbox->subject_snippet;
1027 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new);
1030 if (par->total_new < 2)
1032 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new - 1);
1042 while (*arg && size)
1056 format_osd(char *msg, struct osd_params *par)
1065 unsigned have_text = 0;
1066 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
1068 char buf[OSD_MSG_SIZE];
1069 if (!format_osd_string(buf, n->val, par))
1071 if (!n->key[0] && buf[0])
1073 pos += snprintf(msg+pos, OSD_MSG_SIZE-pos-1, "%s:%s\n", n->key, buf);
1074 if (pos > OSD_MSG_SIZE-1)
1076 pos = sprintf(msg, "OSD message too long!\n");
1088 debug_osd_msg(char *msg)
1092 fprintf(stderr, "OSD: <");
1095 fputc((*msg != '\n' ? *msg : '|'), stderr);
1098 fprintf(stderr, ">\n");
1102 rethink_osd(int notify)
1104 if (!osd_care || !x11_dpy || !allow_osd)
1106 osd_last_msg[0] = 0;
1110 struct osd_params p = { .mbox = NULL, .total_new = 0 };
1111 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1114 p.total_new += b->new;
1115 if (b->new && b->best_time > osd_last_time && (!p.mbox || p.mbox->o.priority < b->o.priority))
1119 char new_msg[OSD_MSG_SIZE];
1120 format_osd(new_msg, &p);
1121 debug_osd_msg(new_msg);
1122 if (strcmp(new_msg, osd_last_msg))
1124 strcpy(osd_last_msg, new_msg);
1125 if (notify && new_msg[0])
1127 debug("OSD: Sending to daemon\n");
1128 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) new_msg, strlen(new_msg));
1132 debug("OSD: No changes\n");
1133 osd_last_time = time(NULL);
1149 static void x11_init(void) { }
1150 static void rethink_leds(void) { }
1151 static void rethink_osd(int notify UNUSED) { }
1152 static void x11_cleanup(void) { }
1156 static int cursor_at, cursor_max;
1158 static unsigned is_active, is_pos; // incremental search
1159 static char is_buf[64];
1170 static int attrs[3][2][M_MAX]; // active (2=incsearch), hilite, status
1178 struct mbox *b = mbox_array[i];
1179 int cc = (cursor_at == i ? (is_active ? 2 : 1) : 0);
1180 int hi = b->o.highlight;
1181 unsigned namepos = 0;
1182 unsigned namelen = strlen(b->name);
1185 attrset(attrs[cc][hi][M_IDLE]);
1187 printw("%c ", b->o.hotkey);
1194 attrset(attrs[cc][hi][M_INCSEARCH]);
1195 for (namepos=0; namepos < is_pos && namepos < 20; namepos++)
1196 addch(is_buf[namepos]);
1199 attrset(attrs[cc][hi][M_NEW]);
1200 else if (b->flagged && b->o.show_flagged)
1201 attrset(attrs[cc][hi][M_FLAG]);
1203 attrset(attrs[cc][hi][M_IDLE]);
1204 while (namepos < namelen)
1205 addch(b->name[namepos++]);
1206 while (namepos++ < 20)
1208 if (b->scanning < 0)
1210 else if (b->scanning)
1212 attrset(attrs[cc][hi][M_SCAN]);
1213 printw("[SCANNING]");
1215 else if (b->total < 0)
1217 attrset(attrs[cc][hi][M_BAD]);
1222 attrset(attrs[cc][hi][M_IDLE]);
1223 printw("%6d ", b->total);
1227 attrset(attrs[cc][hi][M_NEW]);
1228 printw("%6d ", b->new);
1229 attrset(attrs[cc][hi][M_IDLE]);
1230 int age = (last_scan_time - b->best_time);
1235 printw("%2d min ", age/60);
1238 else if (age < 86400)
1239 printw("%2d hr%c ", age/3600, (age >= 7200 ? 's' : ' '));
1244 else if (b->flagged && b->o.show_flagged)
1246 attrset(attrs[cc][hi][M_FLAG]);
1247 printw("%6d ", b->flagged);
1248 attrset(attrs[cc][hi][M_IDLE]);
1250 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
1253 if (snip && b->o.snippets)
1256 getyx(stdscr, yy, xx);
1257 int remains = COLS-1-xx;
1261 build_snippet(snip, snip + sizeof(snip) - 1, b);
1263 if (snip[0] && remains > 2)
1265 #ifdef CONFIG_WIDE_CURSES
1266 size_t len = strlen(snip)+1;
1268 mbstowcs(snip2, snip, len);
1269 addnwstr(snip2, remains);
1271 printw("%-.*s", remains, snip);
1276 b->display_valid_until = last_scan_time + valid;
1278 attrset(attrs[0][0][M_IDLE]);
1285 cursor_max = num_mboxes;
1286 if (cursor_max > LINES-1)
1287 cursor_max = LINES-1;
1288 if (cursor_at >= cursor_max)
1289 cursor_at = cursor_max - 1;
1293 for (int i=0; i<cursor_max; i++)
1295 move(cursor_max, 0);
1298 printw("(no mailboxes found)");
1306 rethink_display(int notify)
1311 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1312 if (mbox_visible_p(b))
1315 if (i >= num_mboxes || mbox_array[i] != b)
1318 if (i >= mbox_array_size)
1320 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
1321 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
1325 if (b->o.beep && b->new > b->last_beep_new)
1327 b->last_beep_new = b->new;
1330 if (i != num_mboxes)
1340 rethink_osd(notify);
1341 if (beeeep && allow_bells && notify)
1352 intrflush(stdscr, FALSE);
1353 keypad(stdscr, TRUE);
1356 static const int attrs_mono[2][M_MAX] = {
1357 [0] = { [M_IDLE] = 0,
1362 [M_INCSEARCH] = A_REVERSE },
1363 [1] = { [M_IDLE] = 0,
1365 [M_NEW] = A_REVERSE | A_BOLD,
1366 [M_FLAG] = A_REVERSE,
1368 [M_INCSEARCH] = A_REVERSE },
1370 for (int i=0; i<2; i++)
1371 for (int j=0; j<M_MAX; j++)
1373 attrs[0][i][j] = attrs_mono[i][j];
1374 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1375 attrs[2][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1381 if (COLOR_PAIRS >= 12)
1383 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
1384 init_pair(2, COLOR_RED, COLOR_BLACK);
1385 init_pair(3, COLOR_WHITE, COLOR_BLUE);
1386 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
1387 init_pair(5, COLOR_RED, COLOR_BLUE);
1388 init_pair(6, COLOR_GREEN, COLOR_BLACK);
1389 init_pair(7, COLOR_GREEN, COLOR_BLUE);
1390 init_pair(8, COLOR_WHITE, COLOR_MAGENTA);
1391 init_pair(9, COLOR_YELLOW, COLOR_MAGENTA);
1392 init_pair(10, COLOR_GREEN, COLOR_MAGENTA);
1393 init_pair(11, COLOR_RED, COLOR_MAGENTA);
1394 init_pair(12, COLOR_BLACK, COLOR_YELLOW);
1395 static const int attrs_color[3][2][M_MAX] = {
1396 [0][0] = { [M_IDLE] = 0,
1397 [M_SCAN] = COLOR_PAIR(1),
1398 [M_NEW] = COLOR_PAIR(1),
1399 [M_FLAG] = COLOR_PAIR(6),
1400 [M_BAD] = COLOR_PAIR(2) },
1401 [0][1] = { [M_IDLE] = A_BOLD,
1402 [M_SCAN] = COLOR_PAIR(1),
1403 [M_NEW] = COLOR_PAIR(1) | A_BOLD,
1404 [M_FLAG] = COLOR_PAIR(6) | A_BOLD,
1405 [M_BAD] = COLOR_PAIR(2) | A_BOLD },
1406 [1][0] = { [M_IDLE] = COLOR_PAIR(3),
1407 [M_SCAN] = COLOR_PAIR(4),
1408 [M_NEW] = COLOR_PAIR(4),
1409 [M_FLAG] = COLOR_PAIR(7),
1410 [M_BAD] = COLOR_PAIR(5) },
1411 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD,
1412 [M_SCAN] = COLOR_PAIR(4),
1413 [M_NEW] = COLOR_PAIR(4) | A_BOLD,
1414 [M_FLAG] = COLOR_PAIR(7) | A_BOLD,
1415 [M_BAD] = COLOR_PAIR(5) | A_BOLD },
1416 [2][0] = { [M_IDLE] = COLOR_PAIR(8),
1417 [M_SCAN] = COLOR_PAIR(9),
1418 [M_NEW] = COLOR_PAIR(9),
1419 [M_FLAG] = COLOR_PAIR(10),
1420 [M_BAD] = COLOR_PAIR(11),
1421 [M_INCSEARCH] = COLOR_PAIR(12) | A_DIM },
1422 [2][1] = { [M_IDLE] = COLOR_PAIR(8) | A_BOLD,
1423 [M_SCAN] = COLOR_PAIR(9),
1424 [M_NEW] = COLOR_PAIR(9) | A_BOLD,
1425 [M_FLAG] = COLOR_PAIR(10) | A_BOLD,
1426 [M_BAD] = COLOR_PAIR(11) | A_BOLD,
1427 [M_INCSEARCH] = COLOR_PAIR(12) },
1429 memcpy(attrs, attrs_color, sizeof(attrs));
1441 print_status(char *status)
1445 printw("%s", status);
1451 scan_and_redraw(int notify)
1453 print_status("Busy...");
1461 if (i >= 0 && i < cursor_max && i != cursor_at)
1463 int old = cursor_at;
1471 next_active(int since, int step)
1475 since = (since+cursor_max) % cursor_max;
1476 step = (step+cursor_max) % cursor_max;
1482 struct mbox *b = mbox_array[i];
1493 if (b->new && b->o.priority > bestp)
1496 bestp = b->o.priority;
1499 i = (i+step) % cursor_max;
1507 mbox_run(struct mbox *b)
1509 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
1510 sprintf(cmd, run_cmd, b->path);
1516 b->force_refresh = 1;
1521 enter_incsearch(void)
1523 print_status("Incremental search...");
1526 redraw_line(cursor_at);
1530 handle_incsearch(int ch)
1532 if ((ch == KEY_BACKSPACE || ch == KEY_DC) && is_pos)
1534 else if (ch >= ' ' && ch <= '~')
1536 if (is_pos < sizeof(is_buf) - 1)
1537 is_buf[is_pos++] = ch;
1544 redraw_line(cursor_at);
1549 for (int i=0; i<cursor_max; i++)
1551 struct mbox *b = mbox_array[i];
1552 if (!strncmp(b->name, is_buf, is_pos))
1563 redraw_line(cursor_at);
1568 without_inotify_wait(int timeout)
1571 halfdelay((timeout > 255) ? 255 : timeout);
1573 #ifdef CONFIG_INOTIFY
1575 #include <sys/inotify.h>
1578 inotify_cm_init(void)
1580 if (!inotify_active) return;
1581 inotify_fd = inotify_init();
1583 die("Inotify init faild.\n");
1587 inotify_add_mbox(struct mbox * b)
1589 if (!inotify_active) return;
1590 b->inotify_status = INOTIFY_ON;
1591 char *path = xmalloc(strlen(b->path)+100);
1593 if (stat(b->path, &st) < 0)
1595 debug("Get stat of %s faild\n", b->path);
1598 if (S_ISREG(st.st_mode))
1601 sprintf(path, "%s", b->path);
1602 if (inotify_add_watch(inotify_fd, path, IN_MOVED_TO | IN_MOVED_FROM | IN_DELETE | IN_CLOSE_WRITE ) < 0)
1604 debug("Inotify add %s faild\n", path);
1605 b->inotify_status = INOTIFY_ERROR;
1608 debug("Inotify add %s OK\n", path);
1610 else if (S_ISDIR(st.st_mode))
1613 char *watch_dir[] = {"cur", "new"};
1614 for (int i=0; i < (int)(sizeof(watch_dir)/sizeof(*watch_dir)); i++)
1616 sprintf(path, "%s/%s", b->path, watch_dir[i]);
1617 if (inotify_add_watch(inotify_fd, path, IN_MOVED_TO | IN_MOVED_FROM | IN_DELETE | IN_CLOSE_WRITE ) < 0)
1619 debug("Inotify add %s faild\n", path);
1620 b->inotify_status = INOTIFY_ERROR;
1626 debug("neither file nor directory\n");
1627 b->inotify_status = INOTIFY_ERROR;
1634 inotify_delete_mbox(struct mbox * b)
1640 static bool // true if inotify change detected
1641 inotify_wait(int timeout)
1645 without_inotify_wait(timeout);
1649 bool inotify_change = 0;
1652 tv.tv_sec = timeout;
1659 FD_SET(inotify_fd, &rfds);
1661 debug("Select begin (timeout %d %d)\n", tv.tv_sec, tv.tv_usec);
1662 int ret = select(1+inotify_fd, &rfds, NULL, NULL, &tv);
1663 debug("Select end\n");
1664 if (ret>0 && FD_ISSET(0, &rfds))
1665 return inotify_change;
1666 if (ret>0 && FD_ISSET(inotify_fd, &rfds))
1668 debug("Read notify begin\n");
1669 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
1670 ssize_t len = read(inotify_fd, buf, sizeof(buf));
1671 if (len == -1 && errno != EAGAIN)
1672 perror ("read inotify faild");
1674 debug("Read notify end\n");
1677 return inotify_change;
1686 inotify_cm_init(void){}
1689 inotify_add_mbox(struct mbox *){}
1692 inotify_delete_mbox(struct mbox *){}
1695 inotify_wait(int timeout)
1697 without_inotify_wait(timeout);
1704 #define STR(c) STR2(c)
1709 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
1712 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
1713 -d\t\t\tLog debug messages to stderr\n\
1714 -i\t\t\tInclude user's INBOX (/var/mail/<user>=INBOX)\n\
1715 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
1716 -o <pattern>=<opts>\tSet mailbox options\n\
1717 -o <opts>\t\tSet default options for all mailboxes\n\
1718 -p <pri>\t\tSet minimum priority to show\n\
1719 -s <key>=<val>\t\tSet on-screen display options (consult OSDD docs)\n\
1720 -t\t\t\tLet TAB select the next mailbox with new mail, no matter what priority it has\n\
1721 -f\t\t\tEnable inotify (wait for file system changes)\n\
1723 On-screen display replacement (in all OSD values and notification lines):\n\
1725 %%f\t\tFrom - mail author name\n\
1726 %%s\t\tMail subject\n\
1727 %%n\t\tCount of new mails\n\
1728 %%m\t\tCount of new mails minus one; if is less than one, whole line will be hidden\n\
1730 Mailbox options (set with `-o', use upper case to negate):\n\
1731 0-9\t\t\tSet mailbox priority (0=default)\n\
1732 b\t\t\tBeep when a message arrives\n\
1733 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1734 e\t\t\tHide from display if empty\n\
1735 f\t\t\tShow flagged messages if there are no new ones\n\
1736 h\t\t\tHide from display\n\
1737 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1738 m\t\t\tShow mailbox name of the sender\n\
1739 o\t\t\tCount old, but unread messages as new\n\
1740 p\t\t\tShow personal info (full name) of the sender\n\
1741 r<int>\t\t\tSort order (default=1000)\n\
1742 s\t\t\tShow message snippets\n\
1743 t\t\t\tHighlight the entry\n\
1744 !<key>\t\t\tSet hot key\n\
1746 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>, Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>\n\
1747 It can be freely distributed and used according to the GNU GPL v2.\n\
1753 parse_options(char *c)
1757 if (sep = strchr(c, '='))
1759 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1760 memcpy(n->pattern, c, sep-c);
1761 n->pattern[sep-c] = 0;
1762 clist_add_tail(&options, &n->n);
1768 o = &global_options;
1772 if (x >= '0' && x <= '9')
1773 o->priority = x - '0';
1774 else if (x == '!' && *c)
1776 else if (x == 'l' && *c >= '1' && *c <= '9')
1777 o->led = *c++ - '0';
1778 else if (x == 'r' && *c >= '0' && *c <= '9')
1781 while (*c >= '0' && *c <= '9')
1782 o->sort_order = 10*o->sort_order + *c++ - '0';
1786 int value = !!islower(x);
1796 o->hide_if_empty = value;
1799 o->show_flagged = value;
1805 o->sender_mbox = value;
1808 o->unread_is_new = value;
1811 o->sender_personal = value;
1814 o->snippets = value;
1817 o->highlight = value;
1820 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
1827 main(int argc, char **argv)
1829 clist_init(&mboxes);
1830 clist_init(&options);
1831 clist_init(&patterns);
1832 clist_init(&osd_opts);
1835 while ((c = getopt(argc, argv, "c:dim:o:p:s:tif")) >= 0)
1839 check_interval = atol(optarg);
1840 if (check_interval <= 0)
1853 parse_options(optarg);
1856 minimum_priority = atol(optarg);
1859 add_osd_opt(optarg);
1870 while (optind < argc)
1871 add_pattern(argv[optind++]);
1881 int inotify_rescan = 0;
1882 int should_exit = 0;
1884 while (!should_exit)
1886 time_t now = time(NULL);
1887 int remains = last_scan_time + check_interval - now;
1890 if (remains <= 0 || inotify_rescan)
1892 if (inotify_rescan) debug("Inotify rescan\n");
1902 inotify_rescan |= inotify_wait(remains);
1907 if (is_active && handle_incsearch(ch))
1912 for (int i=0; i<num_mboxes; i++)
1913 if (ch == mbox_array[i]->o.hotkey)
1917 mbox_run(mbox_array[i]);
1927 move_cursor(cursor_at+1);
1931 move_cursor(cursor_at-1);
1941 move_cursor(cursor_max-1);
1944 next_active(cursor_at+1, 1);
1947 next_active(cursor_at-1, -1);
1951 if (cursor_at < cursor_max)
1952 mbox_run(mbox_array[cursor_at]);
1955 clearok(stdscr, TRUE);
1964 print_status("Bells and whistles are now enabled. Toot!");
1968 print_status("Bells and whistles are now disabled. Pssst!");
1972 print_status("On-screen display is now enabled.");
1976 print_status("On-screen display is now disabled. Watch your step.");
1982 if (ch >= '0' && ch <= '9')
1984 minimum_priority = ch - '0';
1988 debug("Pressed unknown key %d\n", ch);