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 osd_repeat_interval = 0; // 0 is disable
39 static int force_refresh;
40 static int allow_bells = 1;
41 static int allow_osd = 1;
42 static int minimum_priority;
43 static time_t last_scan_time;
44 static time_t last_osd_repeat_time;
45 static char *run_cmd = "mutt -f %s";
46 static int simple_tab;
79 static clist options, patterns;
80 static struct options global_options = {
87 #define MDIR_MAX_NAME_LEN 128
98 enum inotify_status inotify_status;
107 time_t display_valid_until;
108 int last_size, last_pos;
109 int total, new, flagged;
110 int last_total, last_new, last_flagged;
114 char sender_snippet[128];
115 char subject_snippet[128];
116 char mdir_best[MDIR_MAX_NAME_LEN];
117 int osd_repeat_last_total, osd_repeat_last_new, osd_repeat_last_flagged;
118 char osd_repeat_last_sender_snippet[128];
119 char osd_repeat_last_subject_snippet[128];
123 static struct mbox **mbox_array;
124 static int num_mboxes, mbox_array_size;
126 struct osd_opt_node {
132 static clist osd_opts;
133 static clist osd_repeat_opts;
134 static char *osd_mbox_opt="%a";
135 static char *osd_mbox_opt_separator=" ";
137 static void redraw_line(int i);
138 static void rethink_display(int notify);
140 static int inotify_fd;
141 static bool inotify_active;
142 static void inotify_cm_init(void);
143 static void inotify_add_mbox(struct mbox * b);
144 static void inotify_delete_mbox(struct mbox * b);
147 add_pattern(char *patt)
149 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
150 strcpy(n->pattern, patt);
151 if (patt = strchr(n->pattern, '='))
158 clist_add_tail(&patterns, &n->n);
164 struct passwd *p = getpwuid(getuid());
166 die("You don't exist, go away!");
167 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
168 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
173 init_options(struct options *o)
177 o->hide_if_empty = -1;
181 o->show_flagged = -1;
182 o->sender_personal = -1;
189 o->unread_is_new = -1;
194 setup_options(struct mbox *b)
196 b->o = global_options;
197 CLIST_FOR_EACH(struct option_node *, n, options)
198 if (!fnmatch(n->pattern, b->name, 0))
200 debug("\tApplied options %s\n", n->pattern);
201 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
204 MERGE(hide_if_empty);
209 MERGE(sender_personal);
216 MERGE(unread_is_new);
222 add_osd_opt(char *arg, struct clist *opts)
224 struct osd_opt_node *n = xmalloc(sizeof(*n) + strlen(arg));
226 n->val = strchr(n->key, '=');
228 die("Malformed OSD option");
230 clist_add_tail(opts, &n->n);
234 mbox_name(char *path)
236 char *c = strrchr(path, '/');
247 new_mbox(char *path, char *name)
249 struct mbox *b = xmalloc(sizeof(*b));
250 bzero(b, sizeof(*b));
251 b->inotify_status = INOTIFY_OFF;
252 b->path = xstrdup(path);
253 b->name = xstrdup(name);
258 add_mbox(clist *l, struct mbox *b)
262 cnode *prev = l->head.prev;
263 struct mbox *prev_mbox;
264 while (prev != &l->head &&
265 ((prev_mbox = (struct mbox *)prev)->o.sort_order > b->o.sort_order ||
266 prev_mbox->o.sort_order == b->o.sort_order && strcmp(((struct mbox *)prev)->name, b->name) > 0))
268 clist_insert_after(&b->n, prev);
271 clist_add_tail(l, &b->n);
275 del_mbox(struct mbox *b)
284 find_mbox(clist *l, char *path)
286 CLIST_FOR_EACH(struct mbox *, b, *l)
287 if (!strcmp(b->path, path))
293 mbox_active_p(struct mbox *b)
295 if (b->o.priority < minimum_priority)
303 mbox_visible_p(struct mbox *b)
305 if (!mbox_active_p(b))
309 if (b->o.hide_if_empty && !b->total)
315 prepare_snippets(struct mbox *b, char *sender, char *subject)
319 while (*sender == ' ' || *sender == '\t')
321 while (*subject == ' ' || *subject == '\t')
324 pos = b->sender_snippet;
325 term = pos + sizeof(b->sender_snippet) - 1;
326 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
327 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
331 pos = b->subject_snippet;
332 term = pos + sizeof(b->subject_snippet) - 1;
334 add_subject_snippet(&pos, term, subject);
336 add_snippet_raw(&pos, term, "No subject");
340 build_snippet(char *buf, char *term, struct mbox *b)
342 if (b->sender_snippet[0])
344 add_snippet(&buf, term, b->sender_snippet);
345 add_snippet_raw(&buf, term, ": ");
347 add_snippet(&buf, term, b->subject_snippet);
350 static int mb_fd, mb_pos, mb_seekable;
351 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
356 mb_cc = mb_end = mb_buf;
363 lseek(mb_fd, pos, SEEK_SET);
370 return mb_pos - (mb_end - mb_cc);
376 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
385 mb_end = mb_buf + len;
394 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
405 mb_check(const char *p, int len)
409 if (mb_get() != *p++)
420 int avail = mb_end - mb_cc;
423 if (mb_seekable && len >= (int) sizeof(mb_buf))
425 int next = len - len % sizeof(mb_buf);
426 mb_seek(mb_tell() + next);
436 int next = (avail < len) ? avail : len;
443 #define HDR_BUF_SIZE 1024
446 read_hdr_field(char *buf)
454 debug("[truncated] ");
467 while (c == ' ' || c == '\t');
475 if (i < HDR_BUF_SIZE - 1)
480 debug("Header: <%s>\n", buf);
481 return buf[0] ? 1 : 0;
485 scan_mbox(struct mbox *b, struct stat *st)
487 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
490 const char from[] = "\nFrom ";
492 b->best_time = st->st_mtime;
495 b->total = b->new = b->flagged = 0;
500 mb_fd = open(b->path, O_RDONLY);
503 debug("[open failed: %m] ");
504 b->total = b->new = b->flagged = -1;
510 c = read(mb_fd, signature, 2);
511 lseek(mb_fd, 0, SEEK_SET);
513 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
515 debug("[decompressing] ");
518 die("pipe failed: %m");
521 die("fork failed: %m");
524 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
525 die("dup2 failed: %m");
529 execlp("gzip", "gzip", "-cd", NULL);
530 die("Cannot execute gzip: %m");
541 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && mb_seekable)
543 mb_seek(b->last_pos);
544 if (mb_check(from, 6))
546 debug("[incremental] ");
551 debug("[incremental failed] ");
557 if (!mb_check(from+1, 5))
559 debug("[inconsistent] ");
560 b->total = b->new = b->flagged = -1;
563 b->total = b->new = b->flagged = 0;
564 b->last_total = b->last_new = b->last_flagged = 0;
569 b->total = b->last_total;
570 b->new = b->last_new;
571 b->flagged = b->last_flagged;
576 b->last_pos = mb_tell() - 5;
578 b->last_pos--; // last_pos should be the previous \n character
579 b->last_total = b->total;
580 b->last_new = b->new;
581 b->last_flagged = b->flagged;
582 while ((c = mb_get()) >= 0 && c != '\n')
585 int content_length = -1;
592 int c = read_hdr_field(buf);
597 if (!strncasecmp(buf, "Status:", 7))
599 if (!b->o.unread_is_new || strchr(buf + 7, 'R'))
602 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
604 else if (!strncasecmp(buf, "From:", 5))
605 strcpy(sender, buf+5);
606 else if (!strncasecmp(buf, "Subject:", 8))
607 strcpy(subject, buf+8);
608 else if (!strncasecmp(buf, "Content-Length:", 15))
609 content_length = atoi(buf + 15);
618 debug("new=%d flagged=%d len=%d sender=<%s> subject=<%s>\n", new, flagged, content_length, sender, subject);
619 if (new || (flagged && !b->best_is_new))
621 b->best_is_new = new;
622 prepare_snippets(b, sender, subject);
625 if (content_length >= 0)
626 mb_skip(content_length);
644 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
645 b->total = b->new = b->flagged = -1;
650 mdir_mtime(struct mbox *b)
653 char path[strlen(b->path) + 5];
655 for (int new=0; new<2; new++)
657 sprintf(path, "%s/%s", b->path, (new ? "new" : "cur"));
659 if (stat(path, &st) < 0)
660 debug("[cannot stat %s] ", path);
661 else if (st.st_mtime > mtime)
668 mdir_get_snippet(struct mbox *b)
670 char buf[HDR_BUF_SIZE], sender[HDR_BUF_SIZE], subject[HDR_BUF_SIZE];
674 char path[strlen(b->path) + MDIR_MAX_NAME_LEN];
675 sprintf(path, "%s/%s", b->path, b->mdir_best);
676 mb_fd = open(path, O_RDONLY);
679 debug("[open failed: %m] ");
680 prepare_snippets(b, sender, subject);
686 while (read_hdr_field(buf) > 0)
688 if (!strncasecmp(buf, "From:", 5))
689 strcpy(sender, buf+5);
690 else if (!strncasecmp(buf, "Subject:", 8))
691 strcpy(subject, buf+8);
695 prepare_snippets(b, sender, subject);
699 scan_mdir(struct mbox *b)
701 int dir_len = strlen(b->path);
702 char path[dir_len + MDIR_MAX_NAME_LEN];
703 strcpy(path, b->path);
705 b->total = b->new = b->flagged = 0;
709 for (int new=0; new<2; new++)
711 strcpy(path + dir_len, (new ? "/new" : "/cur"));
712 DIR *d = opendir(path);
715 debug("[cannot open %s: %m] ");
719 while (de = readdir(d))
721 char *name = de->d_name;
722 if (name[0] == '.' || strlen(name) + 10 > MDIR_MAX_NAME_LEN)
724 path[dir_len + 4] = '/';
725 strcpy(path + dir_len + 5, name);
727 char *colon = strchr(name, ':');
730 if (colon && colon[1] == '2' && colon[2] == ',')
732 // Another comma can separate extended flags (Dovecot extension)
733 for (int i=3; colon[i] && colon[i] != ','; i++)
746 if (b->o.unread_is_new && !seen)
755 debug("%s: new=%d flagged=%d seen=%d\n", path, is_new, flagged, seen);
757 if (is_new || flagged)
759 // Need to pick the best message to display
761 if (stat(path, &st) < 0)
762 debug("[cannot stat %s: %m] ", path);
763 else if (is_new > b->best_is_new || is_new == b->best_is_new && st.st_mtime > b->best_time)
765 b->best_is_new = is_new;
766 b->best_time = st.st_mtime;
767 strcpy(b->mdir_best, path + dir_len + 1);
774 if (b->best_time && b->o.snippets)
776 debug("best <%s> ", b->mdir_best);
784 debug("Searching for mailboxes (notify=%d)...\n", notify);
785 last_scan_time = time(NULL);
786 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
788 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
790 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
791 if (err && err != GLOB_NOMATCH)
792 die("Failed to glob %s: %m", p->pattern);
793 for (uns i=0; i<g.gl_pathc; i++)
795 char *name = g.gl_pathv[i];
796 struct mbox *b = find_mbox(&mboxes, name);
799 b = new_mbox(name, (p->name ? p->name : mbox_name(name)));
800 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
802 add_mbox(&mboxes, b);
812 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
814 if(b->inotify_status != INOTIFY_ON)
819 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
825 debug("Lost mailbox %s\n", b->name);
826 inotify_delete_mbox(b);
833 debug("Scanning mailboxes...\n");
834 CLIST_FOR_EACH(struct mbox *, b, mboxes)
837 debug("%s: ", b->name);
838 if (!mbox_active_p(b))
844 b->force_refresh = 1;
845 if (stat(b->path, &st) < 0)
847 b->total = b->new = b->flagged = -1;
852 time_t current_mtime;
855 if (S_ISREG(st.st_mode))
859 current_mtime = st.st_mtime;
860 current_size = st.st_size;
863 else if (S_ISDIR(st.st_mode))
867 current_mtime = mdir_mtime(b);
873 debug("neither file nor directory\n");
877 if (!b->last_time || current_mtime != b->last_time || current_size != b->last_size || b->force_refresh)
880 redraw_line(b->index);
887 b->last_time = current_mtime;
888 b->last_size = current_size;
889 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
892 redraw_line(b->index);
894 b->force_refresh = 0;
896 else if (b->display_valid_until <= last_scan_time)
898 debug("not changed, but needs redraw\n");
899 redraw_line(b->index);
902 debug("not changed\n");
906 debug("Scan finished\n");
907 last_scan_time = time(NULL);
908 rethink_display(notify);
913 #include <X11/Xlib.h>
914 #include <X11/Xatom.h>
916 static Display *x11_dpy;
917 static unsigned leds_care, leds_have, leds_want;
920 static unsigned osd_care;
921 #define OSD_MSG_SIZE 1024
922 static char osd_last_msg[OSD_MSG_SIZE];
923 static time_t osd_last_time;
928 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
929 osd_care = (global_options.osd >= 0);
930 CLIST_FOR_EACH(struct option_node *, o, options)
933 leds_care |= (1 << o->o.led);
937 if (osd_repeat_interval)
940 if (!leds_care && !osd_care)
942 debug("X11: No mailbox wants LEDs or OSD\n");
945 if (!getenv("DISPLAY"))
947 debug("X11: Do not have X display\n");
950 if (!(x11_dpy = XOpenDisplay(NULL)))
951 die("Cannot open X display, although the DISPLAY variable is set");
955 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
957 die("Cannot intern OSD_QUEUE atom");
959 // If OSD options contain no message, add one
961 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
966 add_osd_opt("=%40f", &osd_opts);
967 add_osd_opt("=%40s", &osd_opts);
968 add_osd_opt("=", &osd_opts);
969 add_osd_opt("=(and %N more)", &osd_opts);
974 debug("X11: Initialized\n");
980 if (leds_want == leds_have)
983 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
984 for (int i=1; i<10; i++)
985 if (leds_care & (leds_have ^ leds_want) & (1 << i))
989 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
990 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
993 leds_have = leds_want;
999 if (!leds_care || !x11_dpy)
1003 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1004 if (b->o.led > 0 && b->new)
1005 leds_want |= (1 << b->o.led);
1009 static bool // 1 if string is to long
1010 append_osd_string(char * dest, unsigned *pos, char * source)
1012 *pos += snprintf(dest+*pos, OSD_MSG_SIZE-*pos-1, "%s", source);
1013 if (*pos > OSD_MSG_SIZE-1)
1015 *pos = sprintf(dest, "OSD message too long!");
1029 static int format_osd_string(char *dest, char *src, struct osd_params *par);
1032 format_osd_string_mailboxes(char * dest, bool all, struct osd_params *par)
1036 sprintf(dest, "[no mboxes]");
1040 struct osd_params p = *par;
1042 CLIST_FOR_EACH(struct mbox *, b, *par->mboxes)
1050 p.flagged = b->flagged;
1052 char buf[OSD_MSG_SIZE];
1055 if (format_osd_string(buf, osd_mbox_opt_separator, &p))
1056 if (append_osd_string(dest, &pos, buf)) break;
1058 if (!format_osd_string(buf, osd_mbox_opt, &p))
1060 if (append_osd_string(dest, &pos, buf)) break;
1068 format_osd_string(char *dest, char *src, struct osd_params *par)
1070 char *stop = dest + OSD_MSG_SIZE - 1;
1071 char buf[OSD_MSG_SIZE];
1073 while (*src && dest < stop)
1079 while (*src >= '0' && *src <= '9')
1080 size = 10*size + *src++ - '0';
1081 if (!size || size > stop-dest)
1092 arg = par->mbox ? par->mbox->sender_snippet : "";
1095 arg = par->mbox ? par->mbox->subject_snippet : "";
1098 arg = par->mbox ? par->mbox->name : "";
1101 arg = par->mbox ? par->mbox->path : "";
1104 snprintf(buf, sizeof(buf), "%d", par->new);
1107 snprintf(buf, sizeof(buf), "%d", par->flagged);
1112 snprintf(buf, sizeof(buf), "%d", par->new - 1);
1115 snprintf(buf, sizeof(buf), "%d", par->total);
1118 snprintf(buf, sizeof(buf), "%lld", (long long)time(NULL));
1121 format_osd_string_mailboxes(buf, 0, par);
1124 format_osd_string_mailboxes(buf, 1, par);
1134 while (*arg && size)
1148 format_osd(char *msg, struct clist *opts, struct osd_params *par)
1151 unsigned have_text = 0;
1152 CLIST_FOR_EACH(struct osd_opt_node *, n, *opts)
1154 char buf[OSD_MSG_SIZE];
1155 if (!format_osd_string(buf, n->val, par))
1157 if (!n->key[0] && buf[0])
1159 pos += snprintf(msg+pos, OSD_MSG_SIZE-pos-1, "%s:%s\n", n->key, buf);
1160 if (pos > OSD_MSG_SIZE-1)
1162 pos = sprintf(msg, "OSD message too long!\n");
1174 debug_osd_msg(char *msg)
1178 fprintf(stderr, "OSD: <");
1181 fputc((*msg != '\n' ? *msg : '|'), stderr);
1184 fprintf(stderr, ">\n");
1188 osd_send(char * msg)
1192 debug("OSD: Sending to daemon\n");
1193 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) msg, strlen(msg));
1198 static struct osd_params
1199 create_osd_params(bool (* if_use_box)(struct mbox *))
1201 struct osd_params p = { .mbox = NULL, .new = 0, .flagged=0, .total=0, .mboxes=&mboxes };
1202 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1206 p.flagged += b->flagged;
1208 if (b->new && b->best_time > osd_last_time && (!p.mbox || p.mbox->o.priority < b->o.priority))
1214 static bool if_use_box_osd(struct mbox *b)
1216 return b->o.osd > 0;
1219 static bool if_use_box_osd_repeat(struct mbox *b)
1221 return b->o.osd_repeat > 0;
1225 rethink_osd(int notify)
1227 if (!osd_care || !x11_dpy || !allow_osd)
1229 osd_last_msg[0] = 0;
1233 struct osd_params p = create_osd_params(if_use_box_osd);
1235 char new_msg[OSD_MSG_SIZE];
1237 format_osd(new_msg, &osd_opts, &p);
1240 debug_osd_msg(new_msg);
1241 if (strcmp(new_msg, osd_last_msg))
1243 strcpy(osd_last_msg, new_msg);
1247 debug("OSD: No changes\n");
1248 osd_last_time = time(NULL);
1265 if(!osd_repeat_interval) return;
1266 last_osd_repeat_time = time(NULL);
1267 char new_msg[OSD_MSG_SIZE];
1268 struct osd_params p = create_osd_params(if_use_box_osd_repeat);
1269 format_osd(new_msg, &osd_repeat_opts, &p);
1270 debug_osd_msg(new_msg);
1272 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1274 b->osd_repeat_last_total = b->total ;
1275 b->osd_repeat_last_new = b->new ;
1276 b->osd_repeat_last_flagged = b->flagged ;
1277 strcpy(b->osd_repeat_last_sender_snippet , b->sender_snippet );
1278 strcpy(b->osd_repeat_last_subject_snippet , b->subject_snippet);
1283 rethink_osd_repeat(void)
1285 if(!osd_repeat_interval) return;
1287 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1289 if( b->osd_repeat_last_total != b->total ) change = 1;
1290 if( b->osd_repeat_last_new != b->new ) change = 1;
1291 if( b->osd_repeat_last_flagged != b->flagged ) change = 1;
1292 if(strcmp(b->osd_repeat_last_sender_snippet , b->sender_snippet )) change = 1;
1293 if(strcmp(b->osd_repeat_last_subject_snippet , b->subject_snippet)) change = 1;
1295 if(change) osd_repeat();
1300 static void x11_init(void) { }
1301 static void rethink_leds(void) { }
1302 static void rethink_osd(int notify UNUSED) { }
1303 static void x11_cleanup(void) { }
1304 static void osd_repeat(void) { }
1305 static void rethink_osd_repeat(void) { }
1309 static int cursor_at, cursor_max;
1311 static unsigned is_active, is_pos; // incremental search
1312 static char is_buf[64];
1323 static int attrs[3][2][M_MAX]; // active (2=incsearch), hilite, status
1331 struct mbox *b = mbox_array[i];
1332 int cc = (cursor_at == i ? (is_active ? 2 : 1) : 0);
1333 int hi = b->o.highlight;
1334 unsigned namepos = 0;
1335 unsigned namelen = strlen(b->name);
1338 attrset(attrs[cc][hi][M_IDLE]);
1340 printw("%c ", b->o.hotkey);
1347 attrset(attrs[cc][hi][M_INCSEARCH]);
1348 for (namepos=0; namepos < is_pos && namepos < 20; namepos++)
1349 addch(is_buf[namepos]);
1352 attrset(attrs[cc][hi][M_NEW]);
1353 else if (b->flagged && b->o.show_flagged)
1354 attrset(attrs[cc][hi][M_FLAG]);
1356 attrset(attrs[cc][hi][M_IDLE]);
1357 while (namepos < namelen)
1358 addch(b->name[namepos++]);
1359 while (namepos++ < 20)
1361 if (b->scanning < 0)
1363 else if (b->scanning)
1365 attrset(attrs[cc][hi][M_SCAN]);
1366 printw("[SCANNING]");
1368 else if (b->total < 0)
1370 attrset(attrs[cc][hi][M_BAD]);
1375 attrset(attrs[cc][hi][M_IDLE]);
1376 printw("%6d ", b->total);
1380 attrset(attrs[cc][hi][M_NEW]);
1381 printw("%6d ", b->new);
1382 attrset(attrs[cc][hi][M_IDLE]);
1383 int age = (last_scan_time - b->best_time);
1388 printw("%2d min ", age/60);
1391 else if (age < 86400)
1392 printw("%2d hr%c ", age/3600, (age >= 7200 ? 's' : ' '));
1397 else if (b->flagged && b->o.show_flagged)
1399 attrset(attrs[cc][hi][M_FLAG]);
1400 printw("%6d ", b->flagged);
1401 attrset(attrs[cc][hi][M_IDLE]);
1403 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
1406 if (snip && b->o.snippets)
1409 getyx(stdscr, yy, xx);
1410 int remains = COLS-1-xx;
1414 build_snippet(snip, snip + sizeof(snip) - 1, b);
1416 if (snip[0] && remains > 2)
1418 #ifdef CONFIG_WIDE_CURSES
1419 size_t len = strlen(snip)+1;
1421 mbstowcs(snip2, snip, len);
1422 addnwstr(snip2, remains);
1424 printw("%-.*s", remains, snip);
1429 b->display_valid_until = last_scan_time + valid;
1431 attrset(attrs[0][0][M_IDLE]);
1438 cursor_max = num_mboxes;
1439 if (cursor_max > LINES-1)
1440 cursor_max = LINES-1;
1441 if (cursor_at >= cursor_max)
1442 cursor_at = cursor_max - 1;
1446 for (int i=0; i<cursor_max; i++)
1448 move(cursor_max, 0);
1451 printw("(no mailboxes found)");
1459 rethink_display(int notify)
1464 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1465 if (mbox_visible_p(b))
1468 if (i >= num_mboxes || mbox_array[i] != b)
1471 if (i >= mbox_array_size)
1473 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
1474 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
1478 if (b->o.beep && b->new > b->last_beep_new)
1480 b->last_beep_new = b->new;
1483 if (i != num_mboxes)
1493 rethink_osd(notify);
1494 rethink_osd_repeat();
1495 if (beeeep && allow_bells && notify)
1506 intrflush(stdscr, FALSE);
1507 keypad(stdscr, TRUE);
1510 static const int attrs_mono[2][M_MAX] = {
1511 [0] = { [M_IDLE] = 0,
1516 [M_INCSEARCH] = A_REVERSE },
1517 [1] = { [M_IDLE] = 0,
1519 [M_NEW] = A_REVERSE | A_BOLD,
1520 [M_FLAG] = A_REVERSE,
1522 [M_INCSEARCH] = A_REVERSE },
1524 for (int i=0; i<2; i++)
1525 for (int j=0; j<M_MAX; j++)
1527 attrs[0][i][j] = attrs_mono[i][j];
1528 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1529 attrs[2][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1535 if (COLOR_PAIRS >= 12)
1537 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
1538 init_pair(2, COLOR_RED, COLOR_BLACK);
1539 init_pair(3, COLOR_WHITE, COLOR_BLUE);
1540 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
1541 init_pair(5, COLOR_RED, COLOR_BLUE);
1542 init_pair(6, COLOR_GREEN, COLOR_BLACK);
1543 init_pair(7, COLOR_GREEN, COLOR_BLUE);
1544 init_pair(8, COLOR_WHITE, COLOR_MAGENTA);
1545 init_pair(9, COLOR_YELLOW, COLOR_MAGENTA);
1546 init_pair(10, COLOR_GREEN, COLOR_MAGENTA);
1547 init_pair(11, COLOR_RED, COLOR_MAGENTA);
1548 init_pair(12, COLOR_BLACK, COLOR_YELLOW);
1549 static const int attrs_color[3][2][M_MAX] = {
1550 [0][0] = { [M_IDLE] = 0,
1551 [M_SCAN] = COLOR_PAIR(1),
1552 [M_NEW] = COLOR_PAIR(1),
1553 [M_FLAG] = COLOR_PAIR(6),
1554 [M_BAD] = COLOR_PAIR(2) },
1555 [0][1] = { [M_IDLE] = A_BOLD,
1556 [M_SCAN] = COLOR_PAIR(1),
1557 [M_NEW] = COLOR_PAIR(1) | A_BOLD,
1558 [M_FLAG] = COLOR_PAIR(6) | A_BOLD,
1559 [M_BAD] = COLOR_PAIR(2) | A_BOLD },
1560 [1][0] = { [M_IDLE] = COLOR_PAIR(3),
1561 [M_SCAN] = COLOR_PAIR(4),
1562 [M_NEW] = COLOR_PAIR(4),
1563 [M_FLAG] = COLOR_PAIR(7),
1564 [M_BAD] = COLOR_PAIR(5) },
1565 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD,
1566 [M_SCAN] = COLOR_PAIR(4),
1567 [M_NEW] = COLOR_PAIR(4) | A_BOLD,
1568 [M_FLAG] = COLOR_PAIR(7) | A_BOLD,
1569 [M_BAD] = COLOR_PAIR(5) | A_BOLD },
1570 [2][0] = { [M_IDLE] = COLOR_PAIR(8),
1571 [M_SCAN] = COLOR_PAIR(9),
1572 [M_NEW] = COLOR_PAIR(9),
1573 [M_FLAG] = COLOR_PAIR(10),
1574 [M_BAD] = COLOR_PAIR(11),
1575 [M_INCSEARCH] = COLOR_PAIR(12) | A_DIM },
1576 [2][1] = { [M_IDLE] = COLOR_PAIR(8) | A_BOLD,
1577 [M_SCAN] = COLOR_PAIR(9),
1578 [M_NEW] = COLOR_PAIR(9) | A_BOLD,
1579 [M_FLAG] = COLOR_PAIR(10) | A_BOLD,
1580 [M_BAD] = COLOR_PAIR(11) | A_BOLD,
1581 [M_INCSEARCH] = COLOR_PAIR(12) },
1583 memcpy(attrs, attrs_color, sizeof(attrs));
1595 print_status(char *status)
1599 printw("%s", status);
1605 scan_and_redraw(int notify)
1607 print_status("Busy...");
1615 if (i >= 0 && i < cursor_max && i != cursor_at)
1617 int old = cursor_at;
1625 next_active(int since, int step)
1629 since = (since+cursor_max) % cursor_max;
1630 step = (step+cursor_max) % cursor_max;
1636 struct mbox *b = mbox_array[i];
1647 if (b->new && b->o.priority > bestp)
1650 bestp = b->o.priority;
1653 i = (i+step) % cursor_max;
1661 mbox_run(struct mbox *b)
1663 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
1664 sprintf(cmd, run_cmd, b->path);
1670 b->force_refresh = 1;
1675 enter_incsearch(void)
1677 print_status("Incremental search...");
1680 redraw_line(cursor_at);
1684 handle_incsearch(int ch)
1686 if ((ch == KEY_BACKSPACE || ch == KEY_DC) && is_pos)
1688 else if (ch >= ' ' && ch <= '~')
1690 if (is_pos < sizeof(is_buf) - 1)
1691 is_buf[is_pos++] = ch;
1698 redraw_line(cursor_at);
1703 for (int i=0; i<cursor_max; i++)
1705 struct mbox *b = mbox_array[i];
1706 if (!strncmp(b->name, is_buf, is_pos))
1717 redraw_line(cursor_at);
1722 without_inotify_wait(int timeout)
1725 halfdelay((timeout > 255) ? 255 : timeout);
1727 #ifdef CONFIG_INOTIFY
1729 #include <sys/inotify.h>
1732 inotify_cm_init(void)
1734 if (!inotify_active) return;
1735 inotify_fd = inotify_init();
1737 die("Inotify init faild.\n");
1741 inotify_add_mbox(struct mbox * b)
1743 if (!inotify_active) return;
1744 b->inotify_status = INOTIFY_ON;
1745 char *path = xmalloc(strlen(b->path)+100);
1747 if (stat(b->path, &st) < 0)
1749 debug("Get stat of %s faild\n", b->path);
1752 if (S_ISREG(st.st_mode))
1755 sprintf(path, "%s", b->path);
1756 if (inotify_add_watch(inotify_fd, path, IN_MOVED_TO | IN_MOVED_FROM | IN_DELETE | IN_CLOSE_WRITE ) < 0)
1758 debug("Inotify add %s faild\n", path);
1759 b->inotify_status = INOTIFY_ERROR;
1762 debug("Inotify add %s OK\n", path);
1764 else if (S_ISDIR(st.st_mode))
1767 char *watch_dir[] = {"cur", "new"};
1768 for (int i=0; i < (int)(sizeof(watch_dir)/sizeof(*watch_dir)); i++)
1770 sprintf(path, "%s/%s", b->path, watch_dir[i]);
1771 if (inotify_add_watch(inotify_fd, path, IN_MOVED_TO | IN_MOVED_FROM | IN_DELETE | IN_CLOSE_WRITE ) < 0)
1773 debug("Inotify add %s faild\n", path);
1774 b->inotify_status = INOTIFY_ERROR;
1780 debug("neither file nor directory\n");
1781 b->inotify_status = INOTIFY_ERROR;
1788 inotify_delete_mbox(struct mbox * b)
1794 static bool // true if inotify change detected
1795 inotify_wait(int timeout)
1799 without_inotify_wait(timeout);
1803 bool inotify_change = 0;
1806 tv.tv_sec = timeout;
1813 FD_SET(inotify_fd, &rfds);
1815 debug("Select begin (timeout %d %d)\n", tv.tv_sec, tv.tv_usec);
1816 int ret = select(1+inotify_fd, &rfds, NULL, NULL, &tv);
1817 debug("Select end\n");
1818 if (ret>0 && FD_ISSET(0, &rfds))
1819 return inotify_change;
1820 if (ret>0 && FD_ISSET(inotify_fd, &rfds))
1822 debug("Read notify begin\n");
1823 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
1824 ssize_t len = read(inotify_fd, buf, sizeof(buf));
1825 if (len == -1 && errno != EAGAIN)
1826 perror ("read inotify faild");
1828 debug("Read notify end\n");
1831 return inotify_change;
1840 inotify_cm_init(void){}
1843 inotify_add_mbox(struct mbox *){}
1846 inotify_delete_mbox(struct mbox *){}
1849 inotify_wait(int timeout)
1851 without_inotify_wait(timeout);
1858 #define STR(c) STR2(c)
1863 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
1866 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
1867 -d\t\t\tLog debug messages to stderr\n\
1868 -i\t\t\tInclude user's INBOX (/var/mail/<user>=INBOX)\n\
1869 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
1870 -o <pattern>=<opts>\tSet mailbox options\n\
1871 -o <opts>\t\tSet default options for all mailboxes\n\
1872 -p <pri>\t\tSet minimum priority to show\n\
1873 -s <key>=<val>\t\tSet on-screen display options (consult OSDD docs)\n\
1874 -r <key>=<val>\t\tSet on-screen display repeat notification options (consult OSDD docs)\n\
1875 -b <val>\t\tSet on-screen display mailbox specification string (for %%b %%B expansions)\n\
1876 -B <val>\t\tSet on-screen display mailbox specification string separator (for %%b %%B expansions)\n\
1877 -C <interval>\t\tSend repeat notifications every <interval> seconds (default is disable repeat not.)\n\
1878 -t\t\t\tLet TAB select the next mailbox with new mail, no matter what priority it has\n\
1879 -f\t\t\tEnable inotify (wait for file system changes)\n\
1881 On-screen display replacement (in all OSD values, notification lines and -b -B options):\n\
1883 %%f\t\tFrom - mail author name (last unread mail if any exist)\n\
1884 %%s\t\tMail subject (last unread mail if any exist)\n\
1885 %%a\t\tMailbox name of last unread mail (if any exit) or actual mailbox in -b\n\
1886 %%A\t\tMailbox path of last unread mail (if any exit) or actual mailbox in -b\n\
1887 %%n\t\tCount of new mails\n\
1888 %%N\t\tCount of new mails minus one; if is less than one, whole line will be hidden\n\
1889 %%F\t\tCount of flagged mails\n\
1890 %%m\t\tCount of all mails\n\
1891 %%t\t\tActual unix time\n\
1892 %%b\t\tPrint all mailboxes with at least one new mail (format by -b -B options)\n\
1893 %%B\t\tPrint all mailbox (format by -b -B options)\n\
1895 Mailbox options (set with `-o', use upper case to negate):\n\
1896 0-9\t\t\tSet mailbox priority (0=default)\n\
1897 b\t\t\tBeep when a message arrives\n\
1898 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1899 x\t\t\tCount this mailbox in on-screen display repeat notification (default on)\n\
1900 y\t\t\tUse this mailbox in notification mailbox list (%%b %%B expansions) (default on)\n\
1901 e\t\t\tHide from display if empty\n\
1902 f\t\t\tShow flagged messages if there are no new ones\n\
1903 h\t\t\tHide from display\n\
1904 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1905 m\t\t\tShow mailbox name of the sender\n\
1906 o\t\t\tCount old, but unread messages as new\n\
1907 p\t\t\tShow personal info (full name) of the sender\n\
1908 r<int>\t\t\tSort order (default=1000)\n\
1909 s\t\t\tShow message snippets\n\
1910 t\t\t\tHighlight the entry\n\
1911 !<key>\t\t\tSet hot key\n\
1913 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>, Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>\n\
1914 It can be freely distributed and used according to the GNU GPL v2.\n\
1920 parse_options(char *c)
1924 if (sep = strchr(c, '='))
1926 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1927 memcpy(n->pattern, c, sep-c);
1928 n->pattern[sep-c] = 0;
1929 clist_add_tail(&options, &n->n);
1935 o = &global_options;
1939 if (x >= '0' && x <= '9')
1940 o->priority = x - '0';
1941 else if (x == '!' && *c)
1943 else if (x == 'l' && *c >= '1' && *c <= '9')
1944 o->led = *c++ - '0';
1945 else if (x == 'r' && *c >= '0' && *c <= '9')
1948 while (*c >= '0' && *c <= '9')
1949 o->sort_order = 10*o->sort_order + *c++ - '0';
1953 int value = !!islower(x);
1960 o->osd_repeat = value;
1963 o->osd_list = value;
1969 o->hide_if_empty = value;
1972 o->show_flagged = value;
1978 o->sender_mbox = value;
1981 o->unread_is_new = value;
1984 o->sender_personal = value;
1987 o->snippets = value;
1990 o->highlight = value;
1993 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
2000 main(int argc, char **argv)
2002 clist_init(&mboxes);
2003 clist_init(&options);
2004 clist_init(&patterns);
2005 clist_init(&osd_opts);
2006 clist_init(&osd_repeat_opts);
2009 while ((c = getopt(argc, argv, "c:C:dim:o:p:s:r:b:B:tif")) >= 0)
2013 check_interval = atol(optarg);
2014 if (check_interval <= 0)
2018 osd_repeat_interval = atol(optarg);
2019 if (check_interval <= 0)
2032 parse_options(optarg);
2035 minimum_priority = atol(optarg);
2038 add_osd_opt(optarg, &osd_opts);
2041 add_osd_opt(optarg, &osd_repeat_opts);
2044 osd_mbox_opt = optarg;
2047 osd_mbox_opt_separator = optarg;
2058 while (optind < argc)
2059 add_pattern(argv[optind++]);
2069 int inotify_rescan = 0;
2070 int should_exit = 0;
2072 while (!should_exit)
2074 time_t now = time(NULL);
2075 int remains = last_scan_time + check_interval - now;
2076 int osd_repeat_remains = last_osd_repeat_time + osd_repeat_interval - now;
2077 if(!osd_repeat_interval) osd_repeat_remains = 1<<30;
2080 if (remains <= 0 || inotify_rescan)
2082 if (inotify_rescan) debug("Inotify rescan\n");
2086 if (osd_repeat_remains <= 0)
2096 int min_remains = remains;
2097 if(min_remains > osd_repeat_remains) min_remains = osd_repeat_remains;
2098 inotify_rescan |= inotify_wait(min_remains);
2103 if (is_active && handle_incsearch(ch))
2108 for (int i=0; i<num_mboxes; i++)
2109 if (ch == mbox_array[i]->o.hotkey)
2113 mbox_run(mbox_array[i]);
2123 move_cursor(cursor_at+1);
2127 move_cursor(cursor_at-1);
2137 move_cursor(cursor_max-1);
2140 next_active(cursor_at+1, 1);
2143 next_active(cursor_at-1, -1);
2147 if (cursor_at < cursor_max)
2148 mbox_run(mbox_array[cursor_at]);
2151 clearok(stdscr, TRUE);
2160 print_status("Bells and whistles are now enabled. Toot!");
2164 print_status("Bells and whistles are now disabled. Pssst!");
2168 print_status("On-screen display is now enabled.");
2172 print_status("On-screen display is now disabled. Watch your step.");
2178 if (ch >= '0' && ch <= '9')
2180 minimum_priority = ch - '0';
2184 debug("Pressed unknown key %d\n", ch);