]> mj.ucw.cz Git - checkmail.git/commitdiff
0.2
authorMartin Mares <mj@ucw.cz>
Mon, 23 May 2005 09:22:15 +0000 (09:22 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 23 May 2005 09:22:15 +0000 (09:22 +0000)
ChangeLog [new file with mode: 0644]
Makefile
cm.c
util.c
util.h

diff --git a/ChangeLog b/ChangeLog
new file mode 100644 (file)
index 0000000..1335fbc
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,23 @@
+2005-05-23  Martin Mares  <mj@ucw.cz>
+
+       * Replaced the `-h' switch by a general interface for setting options.
+
+       * Added `hide', `hide if empty', `beep' and `highlight' options.
+
+       * Introduced mbox priorities independent on highlighting. TAB selects
+         the highest priority mbox with new messages.
+
+       * Introduced hiding according to priority.
+
+       * Mailboxes can now carry custom names.
+
+       * Removed the non-functional `-l' switch.
+
+       * Introduced `snippet' option which shows sender and subject of the freshest
+         new message. So far very rough.
+
+       * Released as version 0.2.
+
+2005-05-20  Martin Mares  <mj@ucw.cz>
+
+       * Version 0.1 written.
index 4e09ab4e53caa00691762a74c02ee87f8cb504e2..f5d3cf77491b96a6cfe83d9008853d952162a325 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ util.o: util.c util.h
 
 clean:
        rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core`
-       rm -f cm
+       rm -f cm log
 
 distclean: clean
 
diff --git a/cm.c b/cm.c
index 09dce992e942ea22dbc6bf48e1ef663ac5b833dd..50f70717ebfe8f5320bec6b095122d3c474b5042 100644 (file)
--- a/cm.c
+++ b/cm.c
@@ -4,12 +4,17 @@
  *     (c) 2005 Martin Mares <mj@ucw.cz>
  */
 
+#define VERSION "0.2"
+#define YEAR "2005"
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <getopt.h>
 #include <fcntl.h>
 #include <glob.h>
+#include <fnmatch.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <pwd.h>
 
 static int check_interval = 30;
 static int force_refresh;
-static int lock_mboxes;
+static int minimum_priority;
 static time_t last_scan_time;
 static char *run_cmd = "mutt -f %s";
 
+struct options {
+  int priority;
+  int hide;
+  int hide_if_empty;
+  int highlight;
+  int beep;
+  int snippets;
+};
+
+struct option_node {
+  cnode n;
+  struct options o;
+  char pattern[1];
+};
+
+struct pattern_node {
+  cnode n;
+  char *name;
+  char pattern[1];
+};
+
+static clist options, patterns;
+static struct options global_options;
+
 struct mbox {
   cnode n;
+  struct options o;
+  char *name;
+  char *path;
   int index;
-  int hilited;
   int scanning;
   int seen;
   time_t last_time;
   int last_size, last_pos;
-  char *name;
-  char *path;
   int total, new;
   int last_total, last_new;
+  int last_beep_new;
   int force_refresh;
+  char snippet[256];
 };
 
-static clist mboxes, hilites, patterns;
+static clist mboxes;
 static struct mbox **mbox_array;
-static int num_mboxes;
+static int num_mboxes, mbox_array_size;
 
 static void redraw_line(int i);
-static void redraw_all(void);
+static void rethink_display(void);
+
+static void
+add_pattern(char *patt)
+{
+  struct pattern_node *n = xmalloc(sizeof(*n) + strlen(patt));
+  strcpy(n->pattern, patt);
+  if (patt = strchr(n->pattern, '='))
+    {
+      *patt++ = 0;
+      n->name = patt;
+    }
+  else
+    n->name = NULL;
+  clist_add_tail(&patterns, &n->n);
+}
+
+static void
+add_inbox(void)
+{
+  struct passwd *p = getpwuid(getuid());
+  if (!p)
+    die("You don't exist, go away!");
+  char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 7];
+  sprintf(buf, "/var/mail/%s=INBOX", p->pw_name);
+  add_pattern(buf);
+}
+
+static void
+init_options(struct options *o)
+{
+  o->priority = -1;
+  o->hide = -1;
+  o->hide_if_empty = -1;
+  o->beep = -1;
+  o->highlight = -1;
+  o->snippets = -1;
+}
+
+static void
+setup_options(struct mbox *b)
+{
+  b->o = global_options;
+  CLIST_FOR_EACH(struct option_node *, n, options)
+    if (!fnmatch(n->pattern, b->name, 0))
+      {
+       debug("\tApplied options %s\n", n->pattern);
+#define MERGE(f) if (n->o.f >= 0) b->o.f = n->o.f
+       MERGE(priority);
+       MERGE(hide);
+       MERGE(hide_if_empty);
+       MERGE(highlight);
+       MERGE(beep);
+       MERGE(snippets);
+      }
+}
 
 static char *
 mbox_name(char *path)
@@ -93,15 +179,56 @@ find_mbox(clist *l, char *path)
   return NULL;
 }
 
+static inline int
+mbox_active_p(struct mbox *b)
+{
+  if (b->o.priority < minimum_priority)
+    return 0;
+  if (b->o.hide)
+    return 0;
+  return 1;
+}
+
+static inline int
+mbox_visible_p(struct mbox *b)
+{
+  if (!mbox_active_p(b))
+    return 0;
+  if (b->scanning < 0)
+    return 1;
+  if (b->o.hide_if_empty && !b->total)
+    return 0;
+  return 1;
+}
+
 static void
-add_inbox(clist *l)
+add_snippet(char **ppos, char *term, char *add)
 {
-  struct passwd *p = getpwuid(getuid());
-  if (!p)
-    die("You don't exist, go away!");
-  char buf[sizeof("/var/mail/") + strlen(p->pw_name) + 1];
-  sprintf(buf, "/var/mail/%s", p->pw_name);
-  add_mbox(l, buf, "INBOX");
+  char *pos = *ppos;
+  while (*add && pos < term)
+    *pos++ = *add++;
+  *ppos = pos;
+}
+
+static void
+prepare_snippet(struct mbox *b, char *sender, char *subject)
+{
+  while (*sender == ' ' || *sender == '\t')
+    sender++;
+  while (*subject == ' ' || *subject == '\t')
+    subject++;
+
+  char *pos = b->snippet;
+  char *term = b->snippet + sizeof(b->snippet) - 1;
+  if (sender[0])
+    {
+      add_snippet(&pos, term, sender);
+      add_snippet(&pos, term, ": ");
+    }
+  if (subject[0])
+    add_snippet(&pos, term, subject);
+  else
+    add_snippet(&pos, term, "No subject");
 }
 
 static int mb_fd, mb_pos;
@@ -165,7 +292,7 @@ mb_check(const char *p, int len)
 static void
 scan_mbox(struct mbox *b, struct stat *st)
 {
-  char buf[1024];
+  char buf[1024], sender[1024], subject[1024];
   int c;
   const char from[] = "\nFrom ";
 
@@ -230,6 +357,8 @@ scan_mbox(struct mbox *b, struct stat *st)
        ;
 
       int new = 1;
+      sender[0] = 0;
+      subject[0] = 0;
       for (;;)
        {
          uns i = 0;
@@ -253,11 +382,18 @@ scan_mbox(struct mbox *b, struct stat *st)
            break;
          if (!strncasecmp(buf, "Status:", 7))
            new = 0;
+         else if (!strncasecmp(buf, "From:", 5))
+           strcpy(sender, buf+5);
+         else if (!strncasecmp(buf, "Subject:", 8))
+           strcpy(subject, buf+8);
        }
 
       b->total++;
       if (new)
-       b->new++;
+       {
+         b->new++;
+         prepare_snippet(b, sender, subject);
+       }
 
       int ct = 1;
       while (from[ct])
@@ -279,13 +415,13 @@ scan(void)
 {
   debug("Searching for mailboxes...\n");
   last_scan_time = time(NULL);
-  int changed = 0;
-  CLIST_FOR_EACH(struct mbox *, p, patterns)
+  CLIST_FOR_EACH(struct pattern_node *, p, patterns)
     {
+      debug("Trying pattern %s (name %s)\n", p->pattern, p->name);
       glob_t g;
-      int err = glob(p->path, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
+      int err = glob(p->pattern, GLOB_ERR | GLOB_NOSORT | GLOB_TILDE | GLOB_TILDE_CHECK, NULL, &g);
       if (err && err != GLOB_NOMATCH)
-       die("Failed to glob %s: %m", p->path);
+       die("Failed to glob %s: %m", p->pattern);
       for (uns i=0; i<g.gl_pathc; i++)
        {
          char *name = g.gl_pathv[i];
@@ -293,54 +429,39 @@ scan(void)
          if (!b)
            {
              b = add_mbox(&mboxes, name, (p->name ? p->name : mbox_name(name)));
-             if (find_mbox(&hilites, b->name))
-               b->hilited = 1;
-             debug("Discovered mailbox %s (%s) hilited=%d\n", b->name, b->path, b->hilited);
+             debug("Discovered mailbox %s (%s)\n", b->name, b->path);
+             setup_options(b);
              b->scanning = -1;
-             changed = 1;
            }
          b->seen = 1;
        }
       globfree(&g);
     }
 
-  num_mboxes = 0;
   struct mbox *tmp;
   CLIST_FOR_EACH_DELSAFE(struct mbox *, b, mboxes, tmp)
     {
       if (b->seen)
-       {
-         num_mboxes++;
-         b->seen = 0;
-       }
+       b->seen = 0;
       else
        {
          debug("Lost mailbox %s\n", b->name);
-         changed = 1;
          del_mbox(b);
        }
     }
 
-  if (changed)
-    {
-      debug("Reallocating mailbox array...\n");
-      free(mbox_array);
-      mbox_array = xmalloc(sizeof(struct mbox *) * (num_mboxes+1));
-      int i = 0;
-      CLIST_FOR_EACH(struct mbox *, b, mboxes)
-       {
-         b->index = i;
-         mbox_array[i++] = b;
-       }
-      redraw_all();
-      refresh();
-    }
+  rethink_display();
 
   debug("Scanning mailboxes...\n");
   CLIST_FOR_EACH(struct mbox *, b, mboxes)
     {
       struct stat st;
       debug("%s: ", b->name);
+      if (!mbox_active_p(b))
+       {
+         debug("inactive\n");
+         continue;
+       }
       if (force_refresh)
        b->force_refresh = 1;
       if (stat(b->path, &st) < 0)
@@ -371,6 +492,7 @@ scan(void)
 
   debug("Scan finished\n");
   last_scan_time = time(NULL);
+  rethink_display();
 }
 
 static int cursor_at, cursor_max;
@@ -392,7 +514,7 @@ redraw_line(int i)
     {
       struct mbox *b = mbox_array[i];
       int cc = (cursor_at == i);
-      int hi = b->hilited;
+      int hi = b->o.highlight;
 
       attrset(attrs[cc][hi][M_IDLE]);
       if (cc)
@@ -427,15 +549,23 @@ redraw_line(int i)
              if (age < 0)
                age = 0;
              if (age < 3600)
-               printw("%2d min", age/60);
+               printw("%2d min  ", age/60);
              else if (age < 86400)
-               printw("%2d hrs", age/3600);
+               printw("%2d hrs  ", age/3600);
+             else
+               printw("        ");
+             if (b->o.snippets && b->snippet[0])
+               {
+                 int xx, yy;
+                 getyx(stdscr, yy, xx);
+                 int remains = COLS-1-xx;
+                 if (remains > 2)
+                   printw("%-.*s", remains, b->snippet);
+               }
            }
        }
-      attrset(attrs[cc][hi][M_IDLE]);
     }
-  else
-    attrset(attrs[0][0][M_IDLE]);
+  attrset(attrs[0][0][M_IDLE]);
   clrtoeol();
 }
 
@@ -456,11 +586,50 @@ redraw_all(void)
   if (!cursor_max)
     {
       printw("(no mailboxes found)");
+      clrtoeol();
       move(1, 0);
     }
   clrtobot();
 }
 
+static void
+rethink_display(void)
+{
+  int i = 0;
+  int changed = 0;
+  int beeeep = 0;
+  CLIST_FOR_EACH(struct mbox *, b, mboxes)
+    if (mbox_visible_p(b))
+      {
+       b->index = i;
+       if (i >= num_mboxes || mbox_array[i] != b)
+         {
+           changed = 1;
+           if (i >= mbox_array_size)
+             {
+               mbox_array_size = (mbox_array_size ? 2*mbox_array_size : 16);
+               mbox_array = xrealloc(mbox_array, sizeof(struct mbox *) * mbox_array_size);
+             }
+           mbox_array[i] = b;
+         }
+       if (b->o.beep && b->new > b->last_beep_new)
+         beeeep = 1;
+       b->last_beep_new = b->new;
+       i++;
+      }
+  if (i != num_mboxes)
+    changed = 1;
+  num_mboxes = i;
+
+  if (changed)
+    {
+      redraw_all();
+      refresh();
+    }
+  if (beeeep)
+    beep();
+}
+
 static void
 term_init(void)
 {
@@ -502,7 +671,6 @@ term_init(void)
          memcpy(attrs, attrs_color, sizeof(attrs));
        }
     }
-  clear();
 }
 
 static void
@@ -518,7 +686,8 @@ scan_and_redraw(void)
   printw("Busy...");
   refresh();
   scan();
-  redraw_all();
+  move(LINES-1, 0);
+  clrtoeol();
   refresh();
 }
 
@@ -535,53 +704,116 @@ move_cursor(int i)
 }
 
 static void
-next_active(int since)
+next_active(int since, int step)
 {
   if (!cursor_max)
     return;
-  since %= cursor_max;
-  for (int pass=1; pass >= 0; pass--)
+  since = (since+cursor_max) % cursor_max;
+  step = (step+cursor_max) % cursor_max;
+  int besti = -1;
+  int bestp = -1;
+  int i = since;
+  do
     {
-      int i = since;
-      do {
-       if (mbox_array[i]->hilited >= pass && mbox_array[i]->new)
-         {
-           move_cursor(i);
-           return;
-         }
-       i = (i+1) % cursor_max;
-      } while (i != since);
+      struct mbox *b = mbox_array[i];
+      if (b->new && b->o.priority > bestp)
+       {
+         besti = i;
+         bestp = b->o.priority;
+       }
+      i = (i+step) % cursor_max;
     }
+  while (i != since);
+  if (besti >= 0)
+    move_cursor(besti);
 }
 
 static void NONRET
 usage(void)
 {
-  fprintf(stderr, "Usage: cm [<options>] <mbox-patterns>\n\
+  fprintf(stderr, "Usage: cm [<options>] [<mbox-pattern> | <mbox>[=<name>]] ...\n\
 \n\
 Options:\n\
 -c <interval>\t\tScan mailboxes every <interval> seconds (default is 30)\n\
 -d\t\t\tLog debug messages to stderr\n\
--h <mbox>\t\tHighlight and prefer the specified mailbox\n\
 -i\t\t\tInclude user's INBOX\n\
--l\t\t\tLock mailboxes when scanning\n\
 -m <cmd>\t\tCommand to run on the selected mailbox, %%s gets replaced by mailbox path\n\
+-o <pattern>=<opts>\tSet mailbox options\n\
+-o <opts>\t\tSet default options for all mailboxes\n\
+-p <pri>\t\tSet minimum priority to show\n\
+\n\
+Mailbox options (set with `-o', use upper case to negate):\n\
+0-9\t\t\tSet mailbox priority (0=default)\n\
+b\t\t\tBeep when a message arrives\n\
+e\t\t\tHide from display if empty\n\
+h\t\t\tHide from display\n\
+s\t\t\tShow message snippets\n\
+t\t\t\tHighlight the entry\n\
 \n\
-CheckMail 0.1, (c) 2005 Martin Mares <mj@ucw.cz>\n\
+CheckMail " VERSION ", (c) " YEAR " Martin Mares <mj@ucw.cz>\n\
 It can be freely distributed and used according to the GNU GPL v2.\n\
 ");
   exit(1);
 }
 
+static void
+parse_options(char *c)
+{
+  struct options *o;
+  char *sep;
+  if (sep = strchr(c, '='))
+    {
+      struct option_node *n = xmalloc(sizeof(*n) + sep-c);
+      memcpy(n->pattern, c, sep-c);
+      n->pattern[sep-c] = 0;
+      clist_add_tail(&options, &n->n);
+      o = &n->o;
+      init_options(o);
+      c = sep+1;
+    }
+  else
+    o = &global_options;
+
+  int x;
+  while (x = *c++)
+    if (x >= '0' && x <= '9')
+      o->priority = x - '0';
+    else
+      {
+       int value = !!islower(x);
+       switch (tolower(x))
+         {
+         case 'b':
+           o->beep = value;
+           break;
+         case 'e':
+           o->hide_if_empty = value;
+           break;
+         case 'h':
+           o->hide = value;
+           break;
+         case 's':
+           o->snippets = value;
+           break;
+         case 't':
+           o->highlight = value;
+           break;
+         default:
+           fprintf(stderr, "Invalid mailbox option `%c'\n", x);
+           usage();
+         }
+      }
+}
+
 int
 main(int argc, char **argv)
 {
   clist_init(&mboxes);
-  clist_init(&hilites);
+  clist_init(&options);
   clist_init(&patterns);
 
   int c;
-  while ((c = getopt(argc, argv, "c:dh:ilm:")) >= 0)
+  while ((c = getopt(argc, argv, "c:dim:o:p:")) >= 0)
     switch (c)
       {
       case 'c':
@@ -592,27 +824,27 @@ main(int argc, char **argv)
       case 'd':
        debug_mode++;
        break;
-      case 'h':
-       add_mbox(&hilites, optarg, NULL);
-       break;
       case 'i':
-       add_inbox(&patterns);
-       break;
-      case 'l':
-       lock_mboxes = 1;
+       add_inbox();
        break;
       case 'm':
        run_cmd = optarg;
        break;
+      case 'o':
+       parse_options(optarg);
+       break;
+      case 'p':
+       minimum_priority = atol(optarg);
+       break;
       default:
        usage();
       }
   while (optind < argc)
-    add_mbox(&patterns, argv[optind++], NULL);
+    add_pattern(argv[optind++]);
 
   term_init();
   scan_and_redraw();
-  next_active(0);
+  next_active(0, 1);
 
   int should_exit = 0;
   while (!should_exit)
@@ -639,7 +871,7 @@ main(int argc, char **argv)
            case KEY_UP:
              move_cursor(cursor_at-1);
              break;
-           case '0':
+           case '^':
            case KEY_HOME:
            case KEY_PPAGE:
              move_cursor(0);
@@ -650,7 +882,10 @@ main(int argc, char **argv)
              move_cursor(cursor_max-1);
              break;
            case '\t':
-             next_active(cursor_at+1);
+             next_active(cursor_at+1, 1);
+             break;
+           case '`':
+             next_active(cursor_at-1, -1);
              break;
            case '\r':
            case '\n':
@@ -671,6 +906,14 @@ main(int argc, char **argv)
            case 'r' & 0x1f:
              force_refresh = 1;
              break;
+           default:
+             if (ch >= '0' && ch <= '9')
+               {
+                 minimum_priority = ch - '0';
+                 scan_and_redraw();
+               }
+             else
+               debug("Pressed unknown key %d\n", ch);
            }
          refresh();
        }
diff --git a/util.c b/util.c
index e211755461be58e75375b95739b9a1ce68c391c3..82067997b5242f34e7491d0f103ce6793e64c675 100644 (file)
--- a/util.c
+++ b/util.c
@@ -47,6 +47,15 @@ xmalloc(uns size)
   return buf;
 }
 
+void *
+xrealloc(void *old, uns size)
+{
+  void *buf = realloc(old, size);
+  if (!buf)
+    die("Unable to allocate %d bytes of memory", size);
+  return buf;
+}
+
 char *
 xstrdup(char *s)
 {
diff --git a/util.h b/util.h
index 47b19ad2ba5a9aabc878447b3706b5fe33fdf87d..2c0b3597aa54a74bdd94813047094153b62ace0f 100644 (file)
--- a/util.h
+++ b/util.h
@@ -23,4 +23,5 @@ extern int debug_mode;
 void NONRET die(char *x, ...);
 void debug(char *x, ...);
 void *xmalloc(uns size);
+void *xrealloc(void *old, uns size);
 char *xstrdup(char *s);