2 * Incoming Mail Checker
4 * (c) 2005--2010 Martin Mares <mj@ucw.cz>
21 #include <sys/types.h>
24 #ifdef CONFIG_WIDE_CURSES
25 #include <ncursesw/ncurses.h>
34 static int check_interval = 30;
35 static int force_refresh;
36 static int allow_bells = 1;
37 static int allow_osd = 1;
38 static int minimum_priority;
39 static time_t last_scan_time;
40 static char *run_cmd = "mutt -f %s";
69 static clist options, patterns;
70 static struct options global_options = {
83 time_t display_valid_until;
84 int last_size, last_pos;
85 int total, new, flagged;
86 int last_total, last_new, last_flagged;
90 char sender_snippet[128];
91 char subject_snippet[128];
95 static struct mbox **mbox_array;
96 static int num_mboxes, mbox_array_size;
104 static clist osd_opts;
106 static void redraw_line(int i);
107 static void rethink_display(int notify);
110 add_pattern(char *patt)
112 struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
113 strcpy(n->pattern, patt);
114 if (patt = strchr(n->pattern, '='))
121 clist_add_tail(&patterns, &n->n);
127 struct passwd *p = getpwuid(getuid());
129 die("You don't exist, go away!");
130 char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
131 sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
136 init_options(struct options *o)
140 o->hide_if_empty = -1;
144 o->show_flagged = -1;
145 o->sender_personal = -1;
153 setup_options(struct mbox *b)
155 b->o = global_options;
156 CLIST_FOR_EACH(struct option_node *, n, options)
157 if (!fnmatch(n->pattern, b->name, 0))
159 debug("\tApplied options %s\n", n->pattern);
160 #define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
163 MERGE(hide_if_empty);
168 MERGE(sender_personal);
177 add_osd_opt(char *arg)
179 struct osd_opt_node *n = xmalloc(sizeof(*n) + strlen(arg));
181 n->val = strchr(n->key, '=');
183 die("Malformed OSD option");
185 clist_add_tail(&osd_opts, &n->n);
189 mbox_name(char *path)
191 char *c = strrchr(path, '/');
192 return c ? (c+1) : path;
196 add_mbox(clist *l, char *path, char *name)
198 struct mbox *b = xmalloc(sizeof(*b));
199 bzero(b, sizeof(*b));
200 b->path = xstrdup(path);
201 b->name = xstrdup(name);
205 cnode *prev = l->head.prev;
206 while (prev != &l->head && strcmp(((struct mbox *)prev)->name, name) > 0)
208 clist_insert_after(&b->n, prev);
211 clist_add_tail(l, &b->n);
217 del_mbox(struct mbox *b)
226 find_mbox(clist *l, char *path)
228 CLIST_FOR_EACH(struct mbox *, b, *l)
229 if (!strcmp(b->path, path))
235 mbox_active_p(struct mbox *b)
237 if (b->o.priority < minimum_priority)
245 mbox_visible_p(struct mbox *b)
247 if (!mbox_active_p(b))
251 if (b->o.hide_if_empty && !b->total)
257 prepare_snippets(struct mbox *b, char *sender, char *subject)
261 while (*sender == ' ' || *sender == '\t')
263 while (*subject == ' ' || *subject == '\t')
266 pos = b->sender_snippet;
267 term = pos + sizeof(b->sender_snippet) - 1;
268 if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
269 add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
273 pos = b->subject_snippet;
274 term = pos + sizeof(b->subject_snippet) - 1;
276 add_subject_snippet(&pos, term, subject);
278 add_snippet_raw(&pos, term, "No subject");
282 build_snippet(char *buf, char *term, struct mbox *b)
284 if (b->sender_snippet[0])
286 add_snippet(&buf, term, b->sender_snippet);
287 add_snippet_raw(&buf, term, ": ");
289 add_snippet(&buf, term, b->subject_snippet);
292 static int mb_fd, mb_pos;
293 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
298 mb_cc = mb_end = mb_buf;
305 lseek(mb_fd, pos, SEEK_SET);
312 return mb_pos - (mb_end - mb_cc);
318 int len = read(mb_fd, mb_buf, sizeof(mb_buf));
327 mb_end = mb_buf + len;
336 return (mb_cc < mb_end) ? *mb_cc++ : mb_ll_get();
347 mb_check(const char *p, int len)
351 if (mb_get() != *p++)
362 int avail = mb_end - mb_cc;
371 int next = (avail < len) ? avail : len;
379 scan_mbox(struct mbox *b, struct stat *st)
381 char buf[1024], sender[1024], subject[1024];
384 const char from[] = "\nFrom ";
388 b->total = b->new = b->flagged = 0;
393 /* FIXME: Should we do some locking? */
395 mb_fd = open(b->path, O_RDONLY);
398 debug("[open failed: %m] ");
399 b->total = b->new = b->flagged = -1;
404 c = read(mb_fd, signature, 2);
405 lseek(mb_fd, 0, SEEK_SET);
407 if (c == 2 && !memcmp(signature, "\037\213", 2)) //gzip
409 debug("[decompressing] ");
412 die("pipe failed: %m");
415 die("fork failed: %m");
418 if (dup2(mb_fd, 0) < 0 || dup2(fds[1], 1) < 0)
419 die("dup2 failed: %m");
423 execlp("gzip", "gzip", "-cd", NULL);
424 die("Cannot execute gzip: %m");
434 if (b->last_size && b->last_pos && st->st_size > b->last_size && !b->force_refresh && !compressed)
436 mb_seek(b->last_pos);
437 if (mb_check(from, 6))
439 debug("[incremental] ");
444 debug("[incremental failed] ");
450 if (!mb_check(from+1, 5))
452 debug("[inconsistent] ");
453 b->total = b->new = b->flagged = -1;
456 b->total = b->new = b->flagged = 0;
457 b->last_total = b->last_new = b->last_flagged = 0;
458 b->snippet_is_new = 0;
462 b->total = b->last_total;
463 b->new = b->last_new;
464 b->flagged = b->last_flagged;
469 b->last_pos = mb_tell() - 5;
471 b->last_pos--; // last_pos should be the previous \n character
472 b->last_total = b->total;
473 b->last_new = b->new;
474 b->last_flagged = b->flagged;
475 while ((c = mb_get()) >= 0 && c != '\n')
478 int content_length = -1;
491 debug("[truncated] ");
502 while (c == ' ' || c == '\t');
510 if (i < sizeof(buf) - 1)
516 if (!strncasecmp(buf, "Status:", 7))
518 else if (!strncasecmp(buf, "X-Status:", 9) && strchr(buf+9, 'F'))
520 else if (!strncasecmp(buf, "From:", 5))
521 strcpy(sender, buf+5);
522 else if (!strncasecmp(buf, "Subject:", 8))
523 strcpy(subject, buf+8);
524 else if (!strncasecmp(buf, "Content-Length:", 15))
525 content_length = atoi(buf + 15);
534 debug("new=%d flagged=%d len=%d sender=<%s> subject=<%s>\n", new, flagged, content_length, sender, subject);
535 if (new || (flagged && !b->snippet_is_new))
537 b->snippet_is_new = new;
538 prepare_snippets(b, sender, subject);
541 if (content_length >= 0)
542 mb_skip(content_length);
560 if (wait(&status) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
561 b->total = b->new = b->flagged = -1;
568 debug("Searching for mailboxes (notify=%d)...\n", notify);
569 last_scan_time = time(NULL);
570 CLIST_FOR_EACH(struct pattern_node *, p, patterns)
572 debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
574 int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
575 if (err && err != GLOB_NOMATCH)
576 die("Failed to glob %s: %m", p->pattern);
577 for (uns i=0; i<g.gl_pathc; i++)
579 char *name = g.gl_pathv[i];
580 struct mbox *b = find_mbox(&mboxes, name);
583 b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
584 debug("Discovered mailbox %s (%s)\n", b->name, b->path);
594 CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
600 debug("Lost mailbox %s\n", b->name);
607 debug("Scanning mailboxes...\n");
608 CLIST_FOR_EACH(struct mbox *, b, mboxes)
611 debug("%s: ", b->name);
612 if (!mbox_active_p(b))
618 b->force_refresh = 1;
619 if (stat(b->path, &st) < 0)
621 b->total = b->new = b->flagged = -1;
624 else if (!b->last_time || st.st_mtime != b->last_time || st.st_size != b->last_size || b->force_refresh)
627 redraw_line(b->index);
631 b->last_time = st.st_mtime;
632 b->last_size = st.st_size;
633 debug("%d %d %d (stopped at %d of %d)\n", b->total, b->new, b->flagged, b->last_pos, b->last_size);
636 redraw_line(b->index);
639 else if (b->display_valid_until <= last_scan_time)
641 debug("not changed, but needs redraw\n");
642 redraw_line(b->index);
645 debug("not changed\n");
646 b->force_refresh = 0;
650 debug("Scan finished\n");
651 last_scan_time = time(NULL);
652 rethink_display(notify);
657 #include <X11/Xlib.h>
658 #include <X11/Xatom.h>
660 static Display *x11_dpy;
661 static unsigned leds_care, leds_have, leds_want;
664 static unsigned osd_care;
665 #define OSD_MSG_SIZE 1024
666 static char osd_last_msg[OSD_MSG_SIZE];
667 static time_t osd_last_time;
672 leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
673 osd_care = (global_options.osd >= 0);
674 CLIST_FOR_EACH(struct option_node *, o, options)
677 leds_care |= (1 << o->o.led);
682 if (!leds_care && !osd_care)
684 debug("X11: No mailbox wants LEDs or OSD\n");
687 if (!getenv("DISPLAY"))
689 debug("X11: Do not have X display\n");
692 if (!(x11_dpy = XOpenDisplay(NULL)))
693 die("Cannot open X display, although the DISPLAY variable is set");
697 osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
699 die("Cannot intern OSD_QUEUE atom");
701 // If OSD options contain no message, add one
703 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
708 add_osd_opt("=%40f");
709 add_osd_opt("=%40s");
711 add_osd_opt("=(and %m more)");
716 debug("X11: Initialized\n");
722 if (leds_want == leds_have)
725 debug("LEDS: have %02x, want %02x, care %02x\n", leds_have, leds_want, leds_care);
726 for (int i=1; i<10; i++)
727 if (leds_care & (leds_have ^ leds_want) & (1 << i))
731 cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
732 XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
735 leds_have = leds_want;
741 if (!leds_care || !x11_dpy)
745 CLIST_FOR_EACH(struct mbox *, b, mboxes)
746 if (b->o.led > 0 && b->new)
747 leds_want |= (1 << b->o.led);
757 format_osd_string(char *dest, char *src, struct osd_params *par)
759 char *stop = dest + OSD_MSG_SIZE - 1;
762 while (*src && dest < stop)
768 while (*src >= '0' && *src <= '9')
769 size = 10*size + *src++ - '0';
770 if (!size || size > stop-dest)
781 arg = par->mbox->sender_snippet;
784 arg = par->mbox->subject_snippet;
787 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new);
790 if (par->total_new < 2)
792 snprintf(numbuf, sizeof(numbuf), "%d", par->total_new - 1);
816 format_osd(char *msg, struct osd_params *par)
825 unsigned have_text = 0;
826 CLIST_FOR_EACH(struct osd_opt_node *, n, osd_opts)
828 char buf[OSD_MSG_SIZE];
829 if (!format_osd_string(buf, n->val, par))
831 if (!n->key[0] && buf[0])
833 pos += snprintf(msg+pos, OSD_MSG_SIZE-pos-1, "%s:%s\n", n->key, buf);
834 if (pos > OSD_MSG_SIZE-1)
836 pos = sprintf(msg, "OSD message too long!\n");
848 debug_osd_msg(char *msg)
852 fprintf(stderr, "OSD: <");
855 fputc((*msg != '\n' ? *msg : '|'), stderr);
858 fprintf(stderr, ">\n");
862 rethink_osd(int notify)
864 if (!osd_care || !x11_dpy || !allow_osd)
870 struct osd_params p = { .mbox = NULL, .total_new = 0 };
871 CLIST_FOR_EACH(struct mbox *, b, mboxes)
874 p.total_new += b->new;
875 if (b->new && b->last_time > osd_last_time && (!p.mbox || p.mbox->o.priority < b->o.priority))
879 char new_msg[OSD_MSG_SIZE];
880 format_osd(new_msg, &p);
881 debug_osd_msg(new_msg);
882 if (strcmp(new_msg, osd_last_msg))
884 strcpy(osd_last_msg, new_msg);
885 if (notify && new_msg[0])
887 debug("OSD: Sending to daemon\n");
888 XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) new_msg, strlen(new_msg));
892 debug("OSD: No changes\n");
893 osd_last_time = time(NULL);
909 static void x11_init(void) { }
910 static void rethink_leds(void) { }
911 static void rethink_osd(int notify UNUSED) { }
912 static void x11_cleanup(void) { }
916 static int cursor_at, cursor_max;
918 static unsigned is_active, is_pos; // incremental search
919 static char is_buf[64];
930 static int attrs[3][2][M_MAX]; // active (2=incsearch), hilite, status
938 struct mbox *b = mbox_array[i];
939 int cc = (cursor_at == i ? (is_active ? 2 : 1) : 0);
940 int hi = b->o.highlight;
941 unsigned namepos = 0;
942 unsigned namelen = strlen(b->name);
945 attrset(attrs[cc][hi][M_IDLE]);
947 printw("%c ", b->o.hotkey);
954 attrset(attrs[cc][hi][M_INCSEARCH]);
955 for (namepos=0; namepos < is_pos && namepos < 20; namepos++)
956 addch(is_buf[namepos]);
959 attrset(attrs[cc][hi][M_NEW]);
960 else if (b->flagged && b->o.show_flagged)
961 attrset(attrs[cc][hi][M_FLAG]);
963 attrset(attrs[cc][hi][M_IDLE]);
964 while (namepos < namelen)
965 addch(b->name[namepos++]);
966 while (namepos++ < 20)
970 else if (b->scanning)
972 attrset(attrs[cc][hi][M_SCAN]);
973 printw("[SCANNING]");
975 else if (b->total < 0)
977 attrset(attrs[cc][hi][M_BAD]);
982 attrset(attrs[cc][hi][M_IDLE]);
983 printw("%6d ", b->total);
987 attrset(attrs[cc][hi][M_NEW]);
988 printw("%6d ", b->new);
989 attrset(attrs[cc][hi][M_IDLE]);
990 int age = (last_scan_time - b->last_time);
995 printw("%2d min ", age/60);
998 else if (age < 86400)
999 printw("%2d hr%c ", age/3600, (age >= 7200 ? 's' : ' '));
1004 else if (b->flagged && b->o.show_flagged)
1006 attrset(attrs[cc][hi][M_FLAG]);
1007 printw("%6d ", b->flagged);
1008 attrset(attrs[cc][hi][M_IDLE]);
1010 attrset(attrs[cc][0][M_FLAG]); /* We avoid the highlight intentionally */
1013 if (snip && b->o.snippets)
1016 getyx(stdscr, yy, xx);
1017 int remains = COLS-1-xx;
1021 build_snippet(snip, snip + sizeof(snip) - 1, b);
1023 if (snip[0] && remains > 2)
1025 #ifdef CONFIG_WIDE_CURSES
1026 size_t len = strlen(snip)+1;
1028 mbstowcs(snip2, snip, len);
1029 addnwstr(snip2, remains);
1031 printw("%-.*s", remains, snip);
1036 b->display_valid_until = last_scan_time + valid;
1038 attrset(attrs[0][0][M_IDLE]);
1045 cursor_max = num_mboxes;
1046 if (cursor_max > LINES-1)
1047 cursor_max = LINES-1;
1048 if (cursor_at >= cursor_max)
1049 cursor_at = cursor_max - 1;
1053 for (int i=0; i<cursor_max; i++)
1055 move(cursor_max, 0);
1058 printw("(no mailboxes found)");
1066 rethink_display(int notify)
1071 CLIST_FOR_EACH(struct mbox *, b, mboxes)
1072 if (mbox_visible_p(b))
1075 if (i >= num_mboxes || mbox_array[i] != b)
1078 if (i >= mbox_array_size)
1080 mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
1081 mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
1085 if (b->o.beep && b->new > b->last_beep_new)
1087 b->last_beep_new = b->new;
1090 if (i != num_mboxes)
1100 rethink_osd(notify);
1101 if (beeeep && allow_bells && notify)
1112 intrflush(stdscr, FALSE);
1113 keypad(stdscr, TRUE);
1116 static const int attrs_mono[2][M_MAX] = {
1117 [0] = { [M_IDLE] = 0,
1122 [M_INCSEARCH] = A_REVERSE },
1123 [1] = { [M_IDLE] = 0,
1125 [M_NEW] = A_REVERSE | A_BOLD,
1126 [M_FLAG] = A_REVERSE,
1128 [M_INCSEARCH] = A_REVERSE },
1130 for (int i=0; i<2; i++)
1131 for (int j=0; j<M_MAX; j++)
1133 attrs[0][i][j] = attrs_mono[i][j];
1134 attrs[1][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1135 attrs[2][i][j] = attrs_mono[i][j] | A_UNDERLINE;
1141 if (COLOR_PAIRS >= 12)
1143 init_pair(1, COLOR_YELLOW, COLOR_BLACK);
1144 init_pair(2, COLOR_RED, COLOR_BLACK);
1145 init_pair(3, COLOR_WHITE, COLOR_BLUE);
1146 init_pair(4, COLOR_YELLOW, COLOR_BLUE);
1147 init_pair(5, COLOR_RED, COLOR_BLUE);
1148 init_pair(6, COLOR_GREEN, COLOR_BLACK);
1149 init_pair(7, COLOR_GREEN, COLOR_BLUE);
1150 init_pair(8, COLOR_WHITE, COLOR_MAGENTA);
1151 init_pair(9, COLOR_YELLOW, COLOR_MAGENTA);
1152 init_pair(10, COLOR_GREEN, COLOR_MAGENTA);
1153 init_pair(11, COLOR_RED, COLOR_MAGENTA);
1154 init_pair(12, COLOR_BLACK, COLOR_YELLOW);
1155 static const int attrs_color[3][2][M_MAX] = {
1156 [0][0] = { [M_IDLE] = 0,
1157 [M_SCAN] = COLOR_PAIR(1),
1158 [M_NEW] = COLOR_PAIR(1),
1159 [M_FLAG] = COLOR_PAIR(6),
1160 [M_BAD] = COLOR_PAIR(2) },
1161 [0][1] = { [M_IDLE] = A_BOLD,
1162 [M_SCAN] = COLOR_PAIR(1),
1163 [M_NEW] = COLOR_PAIR(1) | A_BOLD,
1164 [M_FLAG] = COLOR_PAIR(6) | A_BOLD,
1165 [M_BAD] = COLOR_PAIR(2) | A_BOLD },
1166 [1][0] = { [M_IDLE] = COLOR_PAIR(3),
1167 [M_SCAN] = COLOR_PAIR(4),
1168 [M_NEW] = COLOR_PAIR(4),
1169 [M_FLAG] = COLOR_PAIR(7),
1170 [M_BAD] = COLOR_PAIR(5) },
1171 [1][1] = { [M_IDLE] = COLOR_PAIR(3) | A_BOLD,
1172 [M_SCAN] = COLOR_PAIR(4),
1173 [M_NEW] = COLOR_PAIR(4) | A_BOLD,
1174 [M_FLAG] = COLOR_PAIR(7) | A_BOLD,
1175 [M_BAD] = COLOR_PAIR(5) | A_BOLD },
1176 [2][0] = { [M_IDLE] = COLOR_PAIR(8),
1177 [M_SCAN] = COLOR_PAIR(9),
1178 [M_NEW] = COLOR_PAIR(9),
1179 [M_FLAG] = COLOR_PAIR(10),
1180 [M_BAD] = COLOR_PAIR(11),
1181 [M_INCSEARCH] = COLOR_PAIR(12) | A_DIM },
1182 [2][1] = { [M_IDLE] = COLOR_PAIR(8) | A_BOLD,
1183 [M_SCAN] = COLOR_PAIR(9),
1184 [M_NEW] = COLOR_PAIR(9) | A_BOLD,
1185 [M_FLAG] = COLOR_PAIR(10) | A_BOLD,
1186 [M_BAD] = COLOR_PAIR(11) | A_BOLD,
1187 [M_INCSEARCH] = COLOR_PAIR(12) },
1189 memcpy(attrs, attrs_color, sizeof(attrs));
1201 print_status(char *status)
1205 printw("%s", status);
1211 scan_and_redraw(int notify)
1213 print_status("Busy...");
1221 if (i >= 0 && i < cursor_max && i != cursor_at)
1223 int old = cursor_at;
1231 next_active(int since, int step)
1235 since = (since+cursor_max) % cursor_max;
1236 step = (step+cursor_max) % cursor_max;
1242 struct mbox *b = mbox_array[i];
1243 if (b->new && b->o.priority > bestp)
1246 bestp = b->o.priority;
1248 i = (i+step) % cursor_max;
1256 mbox_run(struct mbox *b)
1258 char cmd[strlen(run_cmd) + strlen(b->path) + 16];
1259 sprintf(cmd, run_cmd, b->path);
1265 b->force_refresh = 1;
1270 enter_incsearch(void)
1272 print_status("Incremental search...");
1275 redraw_line(cursor_at);
1279 handle_incsearch(int ch)
1281 if ((ch == KEY_BACKSPACE || ch == KEY_DC) && is_pos)
1283 else if (ch >= ' ' && ch <= '~')
1285 if (is_pos < sizeof(is_buf) - 1)
1286 is_buf[is_pos++] = ch;
1293 redraw_line(cursor_at);
1298 for (int i=0; i<cursor_max; i++)
1300 struct mbox *b = mbox_array[i];
1301 if (!strncmp(b->name, is_buf, is_pos))
1312 redraw_line(cursor_at);
1317 #define STR(c) STR2(c)
1322 fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
1325 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
1326 -d\t\t\tLog debug messages to stderr\n\
1327 -i\t\t\tInclude user's INBOX\n\
1328 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
1329 -o <pattern>=<opts>\tSet mailbox options\n\
1330 -o <opts>\t\tSet default options for all mailboxes\n\
1331 -p <pri>\t\tSet minimum priority to show\n\
1332 -s <key>=<val>\t\tSet on-screen display options (consult OSDD docs)\n\
1334 Mailbox options (set with `-o', use upper case to negate):\n\
1335 0-9\t\t\tSet mailbox priority (0=default)\n\
1336 b\t\t\tBeep when a message arrives\n\
1337 d\t\t\tSend an on-screen-display message (requires OSDD)\n\
1338 e\t\t\tHide from display if empty\n\
1339 f\t\t\tShow flagged messages if there are no new ones\n\
1340 h\t\t\tHide from display\n\
1341 l<led>\t\t\tLight a keyboard led (1-9) if running on X display\n\
1342 m\t\t\tShow mailbox name of the sender\n\
1343 p\t\t\tShow personal info (full name) of the sender\n\
1344 s\t\t\tShow message snippets\n\
1345 t\t\t\tHighlight the entry\n\
1346 !<key>\t\t\tSet hot key\n\
1348 CheckMail " STR(VERSION) ", (c) " STR(YEAR) " Martin Mares <mj@ucw.cz>\n\
1349 It can be freely distributed and used according to the GNU GPL v2.\n\
1355 parse_options(char *c)
1359 if (sep = strchr(c, '='))
1361 struct option_node *n = xmalloc(sizeof(*n) + sep-c);
1362 memcpy(n->pattern, c, sep-c);
1363 n->pattern[sep-c] = 0;
1364 clist_add_tail(&options, &n->n);
1370 o = &global_options;
1374 if (x >= '0' && x <= '9')
1375 o->priority = x - '0';
1376 else if (x == '!' && *c)
1378 else if (x == 'l' && *c >= '1' && *c <= '9')
1379 o->led = *c++ - '0';
1382 int value = !!islower(x);
1392 o->hide_if_empty = value;
1395 o->show_flagged = value;
1401 o->sender_mbox = value;
1404 o->sender_personal = value;
1407 o->snippets = value;
1410 o->highlight = value;
1413 fprintf(stderr, "Invalid mailbox option `%c'\n", x);
1420 main(int argc, char **argv)
1422 clist_init(&mboxes);
1423 clist_init(&options);
1424 clist_init(&patterns);
1425 clist_init(&osd_opts);
1428 while ((c = getopt(argc, argv, "c:dim:o:p:s:")) >= 0)
1432 check_interval = atol(optarg);
1433 if (check_interval <= 0)
1446 parse_options(optarg);
1449 minimum_priority = atol(optarg);
1452 add_osd_opt(optarg);
1457 while (optind < argc)
1458 add_pattern(argv[optind++]);
1466 int should_exit = 0;
1468 while (!should_exit)
1470 time_t now = time(NULL);
1471 int remains = last_scan_time + check_interval - now;
1479 halfdelay((remains > 255) ? 255 : remains);
1483 if (is_active && handle_incsearch(ch))
1488 for (int i=0; i<num_mboxes; i++)
1489 if (ch == mbox_array[i]->o.hotkey)
1493 mbox_run(mbox_array[i]);
1503 move_cursor(cursor_at+1);
1507 move_cursor(cursor_at-1);
1517 move_cursor(cursor_max-1);
1520 next_active(cursor_at+1, 1);
1523 next_active(cursor_at-1, -1);
1527 if (cursor_at < cursor_max)
1528 mbox_run(mbox_array[cursor_at]);
1531 clearok(stdscr, TRUE);
1540 print_status("Bells and whistles are now enabled. Toot!");
1544 print_status("Bells and whistles are now disabled. Pssst!");
1548 print_status("On-screen display is now enabled.");
1552 print_status("On-screen display is now disabled. Watch your step.");
1558 if (ch >= '0' && ch <= '9')
1560 minimum_priority = ch - '0';
1564 debug("Pressed unknown key %d\n", ch);