2 ** pick.c -- search for messages by content
4 ** This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
12 #include <h/scansbr.h>
13 #include <h/fmt_scan.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
26 static struct swit switches[] = {
40 { "date pattern", 0 },
42 { "from pattern", 0 },
44 { "search pattern", 0 },
46 { "subject pattern", 0 },
50 { "-othercomponent pattern", 0 },
56 { "datefield field", 5 }, /* 5 chars required to differ from -date */
58 { "sequence name", 0 },
72 { "format format", 0 },
74 { "width columns", 0 },
86 char *version=VERSION;
89 boolean (*action)(struct field *, int, void *);
90 void (*free)(struct nexus **);
91 void (*debug)(void *, size_t);
96 static struct nexus *head;
97 static boolean body = FALSE;
102 static int pcompile(char **, char *);
103 static int pmatches(FILE *, int);
104 static struct nexus * createonethread(char *);
105 static struct nexus * createpickthread(char *);
106 static void scan_mbox(char *, char *, int);
109 static int listsw = -1;
113 static void printmsg(FILE *, struct msgs *, int, char *, int);
116 main(int argc, char **argv)
118 int publicsw = -1, zerosw = 1, vecp = 0, width = 0;
119 unsigned int seqp = 0;
121 char *maildir, *folder = NULL, buf[100];
122 char *cp, **argp, **arguments;
123 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
124 struct msgs_array msgs = { 0, 0, NULL };
131 if (atexit(putzero_done) != 0) {
132 adios(EX_OSERR, NULL, "atexit failed");
135 setlocale(LC_ALL, "");
136 invo_name = mhbasename(argv[0]);
138 /* read user profile/context */
141 arguments = getarguments(invo_name, argc, argv, 1);
144 if (strcmp(invo_name, "scan")==0) {
148 while ((cp = *argp++)) {
154 switch (smatch(cp, switches)) {
156 ambigsw(cp, switches);
157 listsw = 0; /* HACK */
160 adios(EX_USAGE, NULL, "-%s unknown", cp);
163 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
164 print_help(buf, switches, 1);
165 listsw = 0; /* HACK */
166 exit(argc == 2 ? EX_OK : EX_USAGE);
168 print_version(invo_name);
169 listsw = 0; /* HACK */
170 exit(argc == 2 ? EX_OK : EX_USAGE);
184 if (!(cp = *argp++)) /* allow -xyz arguments */
185 adios(EX_USAGE, NULL, "missing argument to %s",
190 adios(EX_SOFTWARE, NULL, "internal error!");
201 if (!(cp = *argp++) || *cp == '-')
202 adios(EX_USAGE, NULL, "missing argument to %s",
205 /* check if too many sequences specified */
206 if (seqp >= NUMATTRS)
207 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
234 if (!(form = *argp++) || *form == '-') {
235 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
239 if (!(cp = *argp++) || *cp == '-') {
240 adios(EX_USAGE, NULL, "missing argument to %s",
246 if (!(cp = *argp++) || (cp[0] == '-' && cp[1])) {
247 adios(EX_USAGE, NULL, "missing argument to %s",
250 if (strcmp(file = cp, "-")!=0) {
251 file = mh_xstrdup(expanddir(cp));
256 if (*cp == '+' || *cp == '@') {
258 adios(EX_USAGE, NULL, "only one folder at a time!");
260 folder = mh_xstrdup(expandfol(cp));
262 app_msgarg(&msgs, cp);
266 fmtstr = new_fs(form, "pick.default");
270 adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
273 adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
276 adios(EX_USAGE, NULL, "section arguments not allowed with -file");
279 scan_mbox(file, fmtstr, width);
284 ** If we didn't specify which messages to search,
285 ** then search the whole folder.
288 app_msgarg(&msgs, seq_all);
291 folder = getcurfol();
292 maildir = toabsdir(folder);
294 if (chdir(maildir) == NOTOK)
295 adios(EX_OSERR, maildir, "unable to change directory to");
297 /* read folder and create message structure */
298 if (!(mp = folder_read(folder)))
299 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
301 /* check for empty folder */
303 adios(EX_DATAERR, NULL, "no messages in %s", folder);
305 /* parse all the message ranges/sequences and set SELECTED */
306 for (msgnum = 0; msgnum < msgs.size; msgnum++)
307 if (!m_convert(mp, msgs.msgs[msgnum]))
309 seq_setprev(mp); /* set the previous-sequence */
312 ** If we aren't saving the results to a sequence,
313 ** we default to list the results.
318 if (publicsw == 1 && is_readonly(mp))
319 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
322 if (!pcompile(vec, NULL))
329 ** If printing message numbers to standard out,
330 ** force line buffering on.
333 setvbuf(stdout, NULL, _IOLBF, 0);
336 ** Scan through all the SELECTED messages and check for a
337 ** match. If the message does not match, then unselect it.
339 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
340 if (is_selected(mp, msgnum)) {
341 if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
342 admonish(cp, "unable to read message");
343 if (fp && pmatches(fp, msgnum)) {
350 printmsg(fp, mp, msgnum, fmtstr, width);
353 /* if it doesn't match, then unselect it */
354 unset_selected(mp, msgnum);
369 adios(EX_DATAERR, NULL, "no messages match specification");
374 ** Add the matching messages to sequences
376 for (seqp = 0; seqs[seqp]; seqp++)
377 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
381 ** Print total matched if not printing each matched message number.
384 printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
387 context_replace(curfolder, folder); /* update current folder */
388 seq_save(mp); /* synchronize message sequences */
389 context_save(); /* save the context file */
390 folder_free(mp); /* free folder/message structure */
391 listsw = 0; /* HACK */
396 scan_mbox(char *file, char *fmtstr, int width)
402 if (strcmp(file, "-") == 0) {
405 } else if (!(in = fopen(file, "r"))) {
406 adios(EX_IOERR, file, "unable to open");
409 for (msgnum = 1; ;msgnum++) {
410 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
411 if (state != SCNMSG) {
422 if (listsw && !isatty(fileno(stdout)))
427 printmsg(FILE *f, struct msgs *mp, int msgnum, char *fmtstr, int width)
431 boolean unseen = FALSE;
433 fseek(f, 0L, SEEK_SET);
435 seqnum = seq_getnum(mp, seq_unseen);
436 unseen = in_sequence(mp, seqnum, msgnum);
438 switch (state = scan(f, msgnum, SCN_FOLD, fmtstr,
439 width, msgnum==mp->curmsg, unseen)) {
444 advise(NULL, "message %d: empty", msgnum);
447 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
452 static struct swit parswit[] = {
466 { "date pattern", 0 },
468 { "from pattern", 0 },
470 { "search pattern", 0 },
472 { "subject pattern", 0 },
476 { "-othercomponent pattern", 15 },
480 { "before date", 0 },
482 { "datefield field", 5 },
488 /* DEFINITIONS FOR PATTERN MATCHING */
491 ** We really should be using re_comp() and re_exec() here. Unfortunately,
492 ** pick advertises that lowercase characters matches characters of both
493 ** cases. Since re_exec() doesn't exhibit this behavior, we are stuck
494 ** with this version. Furthermore, we need to be able to save and restore
495 ** the state of the pattern matcher in order to do things "efficiently".
497 ** The matching power of this algorithm isn't as powerful as the re_xxx()
498 ** routines (no \(xxx\) and \n constructs). Such is life.
514 ** DEFINITIONS FOR NEXUS
517 #define nxtarg() (*argp ? *argp++ : NULL)
518 #define prvarg() argp--
520 #define padvise if (!talked++) advise
534 enum nexus_type type;
554 static int pdebug = 0;
561 ** prototypes for date routines
563 static struct tws *tws_parse(char *, int);
564 static struct tws *tws_special(char *);
569 static int gcompile(struct grep_data *, const char *);
570 static int tcompile(char *, struct tws *, int);
572 static struct nexus *parse(void);
573 static struct nexus *nexp1(void);
574 static struct nexus *nexp2(void);
575 static struct nexus *nexp3(void);
576 static struct nexus *newnexus(enum nexus_type);
578 static boolean BINaction(struct field *, int, void *);
579 static boolean NOTaction(struct field *, int, void *);
580 static boolean GREPaction(struct field *, int, void *);
581 static boolean DATEaction(struct field *, int, void *);
583 static void BINfree(struct nexus **);
584 static void GREPfree(struct nexus **);
585 static void DATEfree(struct nexus **);
587 static void BINdebug(void *, size_t);
588 static void GREPdebug(void *, size_t);
589 static void DATEdebug(void *, size_t);
592 pcompile(char **vec, char *date)
596 if ((cp = getenv("MHPDEBUG")) && *cp)
600 if ((datesw = date) == NULL)
604 if ((head = parse()) == NULL)
605 return (talked ? 0 : 1);
608 padvise(NULL, "%s unexpected", *argp);
616 static struct nexus *
621 struct bin_data *bin;
623 if ((n = nexp1()) == NULL || (cp = nxtarg()) == NULL)
627 padvise(NULL, "%s unexpected", cp);
633 switch (smatch(cp, parswit)) {
635 ambigsw(cp, parswit);
639 fprintf(stderr, "-%s unknown\n", cp);
644 o = newnexus(TYPE_OR);
647 if ((bin->right = parse()))
649 padvise(NULL, "missing disjunctive");
659 static struct nexus *
664 struct bin_data *bin;
666 if ((n = nexp2()) == NULL || (cp = nxtarg()) == NULL)
670 padvise(NULL, "%s unexpected", cp);
676 switch (smatch(cp, parswit)) {
678 ambigsw(cp, parswit);
682 fprintf(stderr, "-%s unknown\n", cp);
687 o = newnexus(TYPE_AND);
690 if ((bin->right = nexp1()))
692 padvise(NULL, "missing conjunctive");
703 static struct nexus *
708 struct bin_data *bin;
710 if ((cp = nxtarg()) == NULL)
720 switch (smatch(cp, parswit)) {
722 ambigsw(cp, parswit);
726 fprintf(stderr, "-%s unknown\n", cp);
731 n = newnexus(TYPE_NOT);
733 if ((bin->left = nexp3()))
735 padvise(NULL, "missing negation");
745 static struct nexus *
750 char buffer[BUFSIZ], temp[64];
752 struct grep_data *gdata;
753 struct date_data *twsd;
755 if ((cp = nxtarg()) == NULL)
759 padvise(NULL, "%s unexpected", cp);
767 switch (i = smatch(cp, parswit)) {
769 ambigsw(cp, parswit);
773 fprintf(stderr, "-%s unknown\n", cp);
778 if ((n = parse()) == NULL) {
779 padvise(NULL, "missing group");
782 if ((cp = nxtarg()) == NULL) {
783 padvise(NULL, "missing -rbrace");
786 if (*cp++ == '-' && smatch(cp, parswit) == PRRBR)
788 padvise(NULL, "%s unexpected", --cp);
796 if (!(cp = nxtarg())) { /* allow -xyz arguments */
797 padvise(NULL, "missing argument to %s", argp[-2]);
799 return createpickthread(cp);
805 strncpy(temp, parswit[i].sw, sizeof(temp));
806 temp[sizeof(temp) - 1] = '\0';
807 dp = *brkstring(temp, " ", NULL);
809 if (!(cp = nxtarg())) { /* allow -xyz arguments */
810 padvise(NULL, "missing argument to %s", argp[-2]);
813 n = newnexus(TYPE_GREP);
815 gdata->header = mh_xstrdup(dp);
816 snprintf(buffer, sizeof(buffer), "%s", cp);
821 n = newnexus(TYPE_GREP);
823 gdata->header = NULL;
825 if (!(cp = nxtarg())) { /* allow -xyz arguments */
826 padvise(NULL, "missing argument to %s", argp[-2]);
831 if (!gcompile(gdata, dp)) {
832 padvise("regcomp", "pattern error in %s %s", argp[-2], cp);
838 padvise(NULL, "internal error!");
842 if (!(datesw = nxtarg()) || *datesw == '-') {
843 padvise(NULL, "missing argument to %s",
851 if (!(cp = nxtarg())) { /* allow -xyz arguments */
852 padvise(NULL, "missing argument to %s", argp[-2]);
855 n = newnexus(TYPE_DATE);
857 twsd->datef = datesw;
858 if (!tcompile(cp, &twsd->tws, twsd->after = i == PRAFTR)) {
859 padvise(NULL, "unable to parse %s %s", argp[-2], cp);
867 static struct nexus *
868 newnexus(enum nexus_type t)
870 struct nexus *p = NULL;
871 struct bin_data *bin;
873 p = mh_xcalloc(1, sizeof(struct nexus));
877 p->action = NOTaction;
880 p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
885 p->action = BINaction;
888 p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
892 p->action = GREPaction;
893 p->debug = GREPdebug;
895 p->data = mh_xcalloc(1, sizeof(struct grep_data));
898 p->action = DATEaction;
899 p->debug = DATEdebug;
901 p->data = mh_xcalloc(1, sizeof(struct date_data));
904 adios(EX_SOFTWARE, NULL, "unknown nexus type %d", t);
912 pmatches(FILE *fp, int msgnum)
914 struct field f = {{0}};
920 if (!talked++ && pdebug && head->debug) {
921 head->debug(head->data, 0);
924 while (s == FLD2 || s == BODY2) {
925 switch (s = m_getfld2(s, &f, fp)) {
930 if (head->action(&f, msgnum, head->data)) {
938 if (head->action(&f, msgnum, head->data)) {
943 advise(NULL, "IOERR in message %d\n", msgnum);
948 adios(EX_SOFTWARE, "m_getfld2", "returned unknown state %d at message %d", s, msgnum);
955 print_debug_level(size_t level)
959 for (i = 0; i < level; i++) {
965 BINdebug(void *data, size_t level)
967 struct bin_data *bd = data;
969 print_debug_level(level);
973 fputs("OR\n", stderr);
976 fputs("AND\n", stderr);
979 fputs("NOT\n", stderr);
982 advise(NULL, "binary nexus has unknown type: %d\n", bd->type);
986 if (bd->left && bd->left->debug) {
987 bd->left->debug(bd->left->data, level+1);
989 print_debug_level(level+1);
990 fputs("can't debug left child\n", stderr);
993 if (bd->right && bd->right->debug) {
994 bd->right->debug(bd->right->data, level+1);
995 } else if (bd->type != TYPE_NOT) {
996 print_debug_level(level+1);
997 fputs("can't debug right child\n", stderr);
1002 NOTaction(struct field *f, int msgnum, void *data)
1004 struct bin_data *bin = data;
1005 return !bin->left->action(f, msgnum, bin->left->data);
1009 BINaction(struct field *f, int msgnum, void *data)
1011 struct bin_data *bin = data;
1013 if (bin->oldmsgnum != msgnum) {
1014 bin->oldmsgnum = msgnum;
1016 bin->leftmatch = FALSE;
1017 bin->rightmatch = FALSE;
1024 bin->leftmatch = bin->leftmatch || bin->left->action(f, msgnum, bin->left->data);
1025 bin->rightmatch = bin->rightmatch || bin->right->action(f, msgnum, bin->right->data);
1027 switch (bin->type) {
1029 bin->match = bin->leftmatch || bin->rightmatch;
1032 bin->match = bin->leftmatch && bin->rightmatch;
1035 adios(EX_SOFTWARE, NULL, "unknown nexus type: %d\n", bin->type);
1042 BINfree(struct nexus **n)
1044 struct bin_data *bd;
1052 if (bd->left && bd->left->free) {
1053 bd->left->free(&bd->left);
1055 advise(NULL, "BUG: can't free left child");
1058 if (bd->right && bd->right->free) {
1059 bd->right->free(&bd->right);
1061 advise(NULL, "BUG: can't free right child");
1068 gcompile(struct grep_data *g, const char *astr)
1070 regex_t *preg = mh_xcalloc(1, sizeof(regex_t));
1075 g->pattern = mh_xstrdup(astr);
1076 ret = regcomp(preg, astr, REG_ICASE | REG_NOSUB);
1078 buf = mh_xcalloc(BUFSIZ, sizeof(char));
1079 regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
1080 fprintf(stderr, "%s\n", buf);
1088 GREPaction(struct field *f, int msgnum, void *data)
1090 struct grep_data *g = data;
1094 if (!g->header && *f->name) {
1098 /* check for the right field */
1099 if (!(g->header && *g->header && mh_strcasecmp(g->header, f->name)==0)) {
1103 if(decode_rfc2047(f->value, buf, sizeof(buf))) {
1104 ret = regexec(g->preg, buf, 0, NULL, 0);
1106 ret = regexec(g->preg, f->value, 0, NULL, 0);
1114 regerror(ret, g->preg, buf, sizeof(buf));
1115 fprintf(stderr, "%s\n", buf);
1122 GREPfree(struct nexus **n)
1124 struct grep_data *gd;
1129 mh_free0(&gd->header);
1135 GREPdebug(void *data, size_t level)
1137 struct grep_data *gd = data;
1138 char *buf, *buf2, *pbuf, *pbuf2;
1140 pbuf = pbuf2 = mh_xstrdup(gd->pattern);
1142 for (;*pbuf2; pbuf2++) {
1143 *pbuf2 = tolower(*pbuf2);
1146 print_debug_level(level);
1149 buf = buf2 = mh_xstrdup(gd->header);
1150 for (;*buf2; buf2++) {
1151 *buf2 = tolower(*buf2);
1153 fprintf(stderr, "PETTERN(%s) %s\n", buf, pbuf);
1155 fprintf(stderr, "PETTERN(BODY) %s\n", pbuf);
1162 tcompile(char *ap, struct tws *tb, int isafter)
1166 if ((tw = tws_parse(ap, isafter)) == NULL)
1175 tws_parse(char *ap, int isafter)
1177 char buffer[BUFSIZ];
1178 struct tws *tw, *ts;
1180 if ((tw = tws_special(ap)) != NULL) {
1181 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
1182 tw->tw_hour = isafter ? 23 : 0;
1185 if ((tw = dparsetime(ap)) != NULL)
1188 if ((ts = dlocaltimenow()) == NULL)
1191 snprintf(buffer, sizeof(buffer), "%s %s", ap, dtwszone(ts));
1192 if ((tw = dparsetime(buffer)) != NULL)
1195 snprintf(buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
1196 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone(ts));
1197 if ((tw = dparsetime(buffer)) != NULL)
1200 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s",
1201 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
1202 if ((tw = dparsetime(buffer)) != NULL)
1205 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s %s",
1206 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
1208 if ((tw = dparsetime(buffer)) != NULL)
1216 tws_special(char *ap)
1223 if (!mh_strcasecmp(ap, "today"))
1224 return dlocaltime(&clock);
1225 if (!mh_strcasecmp(ap, "yesterday")) {
1226 clock -= (long) (60 * 60 * 24);
1227 return dlocaltime(&clock);
1229 if (!mh_strcasecmp(ap, "tomorrow")) {
1230 clock += (long) (60 * 60 * 24);
1231 return dlocaltime(&clock);
1234 for (i = 0; tw_ldotw[i]; i++)
1235 if (!mh_strcasecmp(ap, tw_ldotw[i]))
1238 if ((tw = dlocaltime(&clock)) == NULL)
1240 if ((i -= tw->tw_wday) > 0)
1246 else /* -ddd days ago */
1247 i = atoi(ap); /* we should error check this */
1249 clock += (long) ((60 * 60 * 24) * i);
1250 return dlocaltime(&clock);
1255 DATEaction(struct field *f, int msgnum, void *data)
1257 struct date_data *dd = data;
1258 boolean state = FALSE;
1262 if (mh_strcasecmp(f->name, dd->datef)!=0) {
1265 bp = mh_xstrdup(f->value);
1266 if ((tw = dparsetime(bp)) == NULL) {
1267 advise(NULL, "unable to parse %s field in message %d, not matching...", dd->datef, msgnum);
1269 } else if (dd->after) {
1270 state = twsort(tw, &dd->tws) > 0;
1272 state = twsort(tw, &dd->tws) < 0;
1281 DATEfree(struct nexus **n)
1283 struct date_data *dd;
1293 DATEdebug(void *data, size_t level)
1295 struct date_data *dd = data;
1296 print_debug_level(level);
1297 fprintf(stderr, "TEMPORAL(%s) %s: %s\n",dd->after ? "after" : "before", dd->datef, dasctime(&dd->tws));
1300 static struct nexus *
1301 createpickthread(char *msgs)
1303 char *folder = NULL;
1304 struct msgs_array msgarray = {0};
1305 struct msgs_array files = {0};
1306 struct nexus *ret = NULL;
1309 struct bin_data *bd;
1311 char **cp = brkstring(msgs, " \t", NULL);
1314 for (; cp && *cp; cp++) {
1322 folder = mh_xstrdup(*cp);
1325 app_msgarg(&msgarray, mh_xstrdup(*cp));
1329 parse_msgs(&msgarray, folder, &files);
1331 for (i = 0; i < files.size; i++) {
1332 buf = getthreadid(files.msgs[i]);
1334 adios(EX_DATAERR, NULL, "message %s is not part of a thread", basename(files.msgs[i]));
1338 c = createonethread(buf);
1346 or = newnexus(TYPE_OR);
1353 mh_free0(&(files.msgs));
1354 mh_free0(&(msgarray.msgs));
1359 static struct nexus *
1360 createonethread(char *c)
1362 struct nexus *ret = newnexus(TYPE_OR);
1363 struct nexus *left = newnexus(TYPE_GREP);
1364 struct nexus *right = newnexus(TYPE_GREP);
1365 struct bin_data *bd = ret->data;
1366 struct grep_data *gd = left->data;
1371 gd->header = mh_xstrdup("message-id");
1373 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1374 if(!gcompile(gd, buf)) {
1375 padvise(NULL, "pattern error %s", c);
1380 gd->header = mh_xstrdup("references");
1382 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1383 if(!gcompile(gd, buf)) {
1384 padvise(NULL, "pattern error in %s", c);