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;
124 static struct nexus *head;
125 static boolean body = FALSE;
130 static int pcompile(char **, char *);
131 static int pmatches(FILE *, int);
132 static boolean nexus_match(struct field *, int, struct nexus *);
133 static void nexus_free(struct nexus **);
134 static void nexus_clear(struct nexus *);
135 static void nexus_debug(struct nexus *, size_t);
136 static void nexus_debug_grep(struct grep_data *);
137 static void print_debug_level(size_t);
138 static struct nexus * createonethread(char *);
139 static struct nexus * createpickthread(char *);
140 static void scan_mbox(char *, char *, int);
143 static int listsw = -1;
147 static void printmsg(FILE *, struct msgs *, int, char *, int);
150 main(int argc, char **argv)
152 int publicsw = -1, zerosw = 1, vecp = 0, width = 0;
153 unsigned int seqp = 0;
155 char *maildir, *folder = NULL, buf[100];
156 char *cp, **argp, **arguments;
157 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
158 struct msgs_array msgs = { 0, 0, NULL };
165 if (atexit(putzero_done) != 0) {
166 adios(EX_OSERR, NULL, "atexit failed");
169 setlocale(LC_ALL, "");
170 invo_name = mhbasename(argv[0]);
172 /* read user profile/context */
175 arguments = getarguments(invo_name, argc, argv, 1);
178 if (strcmp(invo_name, "scan")==0) {
182 while ((cp = *argp++)) {
188 switch (smatch(cp, switches)) {
190 ambigsw(cp, switches);
191 listsw = 0; /* HACK */
194 adios(EX_USAGE, NULL, "-%s unknown", cp);
197 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
198 print_help(buf, switches, 1);
199 listsw = 0; /* HACK */
200 exit(argc == 2 ? EX_OK : EX_USAGE);
202 print_version(invo_name);
203 listsw = 0; /* HACK */
204 exit(argc == 2 ? EX_OK : EX_USAGE);
218 if (!(cp = *argp++)) /* allow -xyz arguments */
219 adios(EX_USAGE, NULL, "missing argument to %s",
224 adios(EX_SOFTWARE, NULL, "internal error!");
235 if (!(cp = *argp++) || *cp == '-')
236 adios(EX_USAGE, NULL, "missing argument to %s",
239 /* check if too many sequences specified */
240 if (seqp >= NUMATTRS)
241 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
268 if (!(form = *argp++) || *form == '-') {
269 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
273 if (!(cp = *argp++) || *cp == '-') {
274 adios(EX_USAGE, NULL, "missing argument to %s",
280 if (!(cp = *argp++) || (cp[0] == '-' && cp[1])) {
281 adios(EX_USAGE, NULL, "missing argument to %s",
284 if (strcmp(file = cp, "-")!=0) {
285 file = mh_xstrdup(expanddir(cp));
290 if (*cp == '+' || *cp == '@') {
292 adios(EX_USAGE, NULL, "only one folder at a time!");
294 folder = mh_xstrdup(expandfol(cp));
296 app_msgarg(&msgs, cp);
300 fmtstr = new_fs(form, "pick.default");
304 adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
307 adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
310 adios(EX_USAGE, NULL, "section arguments not allowed with -file");
313 scan_mbox(file, fmtstr, width);
318 ** If we didn't specify which messages to search,
319 ** then search the whole folder.
322 app_msgarg(&msgs, seq_all);
325 folder = getcurfol();
326 maildir = toabsdir(folder);
328 if (chdir(maildir) == NOTOK)
329 adios(EX_OSERR, maildir, "unable to change directory to");
331 /* read folder and create message structure */
332 if (!(mp = folder_read(folder)))
333 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
335 /* check for empty folder */
337 adios(EX_DATAERR, NULL, "no messages in %s", folder);
339 /* parse all the message ranges/sequences and set SELECTED */
340 for (msgnum = 0; msgnum < msgs.size; msgnum++)
341 if (!m_convert(mp, msgs.msgs[msgnum]))
343 seq_setprev(mp); /* set the previous-sequence */
346 ** If we aren't saving the results to a sequence,
347 ** we default to list the results.
352 if (publicsw == 1 && is_readonly(mp))
353 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
356 if (!pcompile(vec, NULL))
363 ** If printing message numbers to standard out,
364 ** force line buffering on.
367 setvbuf(stdout, NULL, _IOLBF, 0);
370 ** Scan through all the SELECTED messages and check for a
371 ** match. If the message does not match, then unselect it.
373 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
374 if (is_selected(mp, msgnum)) {
375 if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
376 admonish(cp, "unable to read message");
377 if (fp && pmatches(fp, msgnum)) {
384 printmsg(fp, mp, msgnum, fmtstr, width);
387 /* if it doesn't match, then unselect it */
388 unset_selected(mp, msgnum);
401 adios(EX_DATAERR, NULL, "no messages match specification");
406 ** Add the matching messages to sequences
408 for (seqp = 0; seqs[seqp]; seqp++)
409 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
413 ** Print total matched if not printing each matched message number.
416 printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
419 context_replace(curfolder, folder); /* update current folder */
420 seq_save(mp); /* synchronize message sequences */
421 context_save(); /* save the context file */
422 folder_free(mp); /* free folder/message structure */
423 listsw = 0; /* HACK */
428 scan_mbox(char *file, char *fmtstr, int width)
434 if (strcmp(file, "-") == 0) {
437 } else if (!(in = fopen(file, "r"))) {
438 adios(EX_IOERR, file, "unable to open");
441 for (msgnum = 1; ;msgnum++) {
442 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
443 if (state != SCNMSG) {
453 if (listsw && !isatty(fileno(stdout)))
458 printmsg(FILE *f, struct msgs *mp, int msgnum, char *fmtstr, int width)
462 boolean unseen = FALSE;
464 fseek(f, 0L, SEEK_SET);
466 seqnum = seq_getnum(mp, seq_unseen);
467 unseen = in_sequence(mp, seqnum, msgnum);
469 switch (state = scan(f, msgnum, SCN_FOLD, fmtstr,
470 width, msgnum==mp->curmsg, unseen)) {
475 advise(NULL, "message %d: empty", msgnum);
478 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
482 static struct swit parswit[] = {
496 { "date pattern", 0 },
498 { "from pattern", 0 },
500 { "search pattern", 0 },
502 { "subject pattern", 0 },
506 { "-othercomponent pattern", 15 },
510 { "before date", 0 },
512 { "datefield field", 5 },
519 ** DEFINITIONS FOR NEXUS
522 #define nxtarg() (*argp ? *argp++ : NULL)
523 #define prvarg() argp--
525 #define padvise if (!talked++) advise
528 static int pdebug = 0;
534 ** prototypes for date routines
536 static struct tws *tws_parse(char *, int);
537 static struct tws *tws_special(char *);
542 static int gcompile(struct grep_data *, const char *);
543 static int tcompile(char *, struct tws *, int);
545 static struct nexus *parse(void);
546 static struct nexus *nexp1(void);
547 static struct nexus *nexp2(void);
548 static struct nexus *nexp3(void);
549 static struct nexus *newnexus(enum nexus_type);
552 pcompile(char **vec, char *date)
556 if ((cp = getenv("MHPDEBUG")) && *cp)
560 if ((datesw = date) == NULL)
564 if ((head = parse()) == NULL)
565 return (talked ? 0 : 1);
568 padvise(NULL, "%s unexpected", *argp);
576 static struct nexus *
581 struct bin_data *bin;
583 if ((n = nexp1()) == NULL || (cp = nxtarg()) == NULL)
587 padvise(NULL, "%s unexpected", cp);
593 switch (smatch(cp, parswit)) {
595 ambigsw(cp, parswit);
599 fprintf(stderr, "-%s unknown\n", cp);
607 if ((bin->right = parse()))
609 padvise(NULL, "missing disjunctive");
619 static struct nexus *
624 struct bin_data *bin;
626 if ((n = nexp2()) == NULL || (cp = nxtarg()) == NULL)
630 padvise(NULL, "%s unexpected", cp);
636 switch (smatch(cp, parswit)) {
638 ambigsw(cp, parswit);
642 fprintf(stderr, "-%s unknown\n", cp);
650 if ((bin->right = nexp1()))
652 padvise(NULL, "missing conjunctive");
663 static struct nexus *
668 struct bin_data *bin;
670 if ((cp = nxtarg()) == NULL)
680 switch (smatch(cp, parswit)) {
682 ambigsw(cp, parswit);
686 fprintf(stderr, "-%s unknown\n", cp);
693 if ((bin->left = nexp3()))
695 padvise(NULL, "missing negation");
705 static struct nexus *
710 char buffer[BUFSIZ], temp[64];
712 struct grep_data *gdata;
713 struct date_data *twsd;
715 if ((cp = nxtarg()) == NULL)
719 padvise(NULL, "%s unexpected", cp);
727 switch (i = smatch(cp, parswit)) {
729 ambigsw(cp, parswit);
733 fprintf(stderr, "-%s unknown\n", cp);
738 if ((n = parse()) == NULL) {
739 padvise(NULL, "missing group");
742 if ((cp = nxtarg()) == NULL) {
743 padvise(NULL, "missing -rbrace");
746 if (*cp++ == '-' && smatch(cp, parswit) == PRRBR)
748 padvise(NULL, "%s unexpected", --cp);
756 if (!(cp = nxtarg())) { /* allow -xyz arguments */
757 padvise(NULL, "missing argument to %s", argp[-2]);
759 return createpickthread(cp);
765 strncpy(temp, parswit[i].sw, sizeof(temp));
766 temp[sizeof(temp) - 1] = '\0';
767 dp = *brkstring(temp, " ", NULL);
769 if (!(cp = nxtarg())) { /* allow -xyz arguments */
770 padvise(NULL, "missing argument to %s", argp[-2]);
773 n = newnexus(grep_t);
775 gdata->header = mh_xstrdup(dp);
776 snprintf(buffer, sizeof(buffer), "%s", cp);
781 n = newnexus(grep_t);
783 gdata->header = NULL;
785 if (!(cp = nxtarg())) { /* allow -xyz arguments */
786 padvise(NULL, "missing argument to %s", argp[-2]);
791 if (!gcompile(gdata, dp)) {
792 padvise("regcomp", "pattern error in %s %s", argp[-2], cp);
798 padvise(NULL, "internal error!");
802 if (!(datesw = nxtarg()) || *datesw == '-') {
803 padvise(NULL, "missing argument to %s",
811 if (!(cp = nxtarg())) { /* allow -xyz arguments */
812 padvise(NULL, "missing argument to %s", argp[-2]);
815 n = newnexus(date_t);
817 twsd->datef = datesw;
818 if (!tcompile(cp, &twsd->tws, twsd->after = i == PRAFTR)) {
819 padvise(NULL, "unable to parse %s %s", argp[-2], cp);
827 static struct nexus *
828 newnexus(enum nexus_type t)
830 struct nexus *p = NULL;
831 p = mh_xcalloc(1, sizeof(struct nexus));
837 static void nexus_clear(struct nexus *n)
843 nexus_clear(n->data.b.right);
846 nexus_clear(n->data.b.left);
854 pmatches(FILE *fp, int msgnum)
856 struct field f = {{0}};
864 if (!talked++ && pdebug) {
865 nexus_debug(head, 0);
868 while (s == FLD2 || s == BODY2) {
869 switch (s = m_getfld2(s, &f, fp)) {
874 nexus_match(&f, msgnum, head);
880 nexus_match(&f, msgnum, head);
883 advise(NULL, "IOERR in message %d\n", msgnum);
888 adios(EX_SOFTWARE, "m_getfld2", "returned unknown state %d at message %d", s, msgnum);
895 match_grep(struct field *f, struct grep_data *g)
900 if (!g->header && *f->name) {
905 ret = regexec(g->preg, f->value, 0, NULL, 0);
909 /* check for the right field */
910 if (!(g->header && *g->header && mh_strcasecmp(g->header, f->name)==0)) {
914 if (decode_rfc2047(f->value, buf, sizeof(buf))) {
915 ret = regexec(g->preg, buf, 0, NULL, 0);
917 ret = regexec(g->preg, f->value, 0, NULL, 0);
926 regerror(ret, g->preg, buf, sizeof(buf));
927 fprintf(stderr, "%s\n", buf);
933 match_date(struct field *f, int msgnum, struct date_data *dd)
939 if (mh_strcasecmp(f->name, dd->datef)!=0) {
942 bp = mh_xstrdup(f->value);
943 if ((tw = dparsetime(bp)) == NULL) {
944 advise(NULL, "unable to parse %s field in message %d, not matching...", dd->datef, msgnum);
945 } else if (dd->after) {
946 ret = twsort(tw, &dd->tws) > 0;
948 ret = twsort(tw, &dd->tws) < 0;
956 nexus_match(struct field *f, int msgnum, struct nexus *n)
960 n->match = nexus_match(f, msgnum, n->data.b.left);
961 n->match = nexus_match(f, msgnum, n->data.b.right) && n->match;
964 n->match = nexus_match(f, msgnum, n->data.b.left);
965 n->match = nexus_match(f, msgnum, n->data.b.right) || n->match;
968 n->match = !nexus_match(f, msgnum, n->data.b.left);
974 n->match = match_date(f, msgnum, &n->data.d);
980 n->match = match_grep(f, &n->data.g);
983 adios(EX_SOFTWARE, NULL, "nexus tree contains a unknown nexus_type (%d)", n->t);
989 nexus_debug(struct nexus *n, size_t level)
991 struct date_data *dd;
992 print_debug_level(level);
995 fputs("AND\n", stderr);
996 nexus_debug(n->data.b.left, level+1);
997 nexus_debug(n->data.b.right, level+1);
1000 fputs("OR\n", stderr);
1001 nexus_debug(n->data.b.left, level+1);
1002 nexus_debug(n->data.b.right, level+1);
1005 fputs("NOT\n", stderr);
1006 nexus_debug(n->data.b.left, level+1);
1009 nexus_debug_grep(&n->data.g);
1013 fprintf(stderr, "TEMPORAL(%s) %s: %s\n",dd->after ? "after" : "before", dd->datef, dasctime(&dd->tws));
1016 adios(EX_SOFTWARE, NULL, "nexus tree contains a unknown nexus_type (%d)", n->t);
1021 nexus_debug_grep(struct grep_data *gd)
1023 char *buf, *buf2, *pbuf, *pbuf2;
1025 pbuf = pbuf2 = mh_xstrdup(gd->pattern);
1027 for (;*pbuf2; pbuf2++) {
1028 *pbuf2 = tolower(*pbuf2);
1032 buf = buf2 = mh_xstrdup(gd->header);
1033 for (;*buf2; buf2++) {
1034 *buf2 = tolower(*buf2);
1036 fprintf(stderr, "PETTERN(%s) %s\n", buf, pbuf);
1038 fprintf(stderr, "PETTERN(BODY) %s\n", pbuf);
1045 nexus_free(struct nexus **n)
1053 nexus_free(&(*n)->data.b.right);
1056 nexus_free(&(*n)->data.b.left);
1059 mh_free0(&(*n)->data.g.header);
1060 mh_free0(&(*n)->data.g.pattern);
1061 regfree((*n)->data.g.preg);
1065 advise(NULL, "Unknown nexus_type (%d) to free", (*n)->t);
1071 print_debug_level(size_t level)
1075 for (i = 0; i < level; i++) {
1076 fputs("| ", stderr);
1081 gcompile(struct grep_data *g, const char *astr)
1083 regex_t *preg = mh_xcalloc(1, sizeof(regex_t));
1088 g->pattern = mh_xstrdup(astr);
1089 ret = regcomp(preg, astr, REG_ICASE | REG_NOSUB);
1091 buf = mh_xcalloc(BUFSIZ, sizeof(char));
1092 regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
1093 fprintf(stderr, "%s\n", buf);
1101 tcompile(char *ap, struct tws *tb, int isafter)
1105 if ((tw = tws_parse(ap, isafter)) == NULL)
1114 tws_parse(char *ap, int isafter)
1116 char buffer[BUFSIZ];
1117 struct tws *tw, *ts;
1119 if ((tw = tws_special(ap)) != NULL) {
1120 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
1121 tw->tw_hour = isafter ? 23 : 0;
1124 if ((tw = dparsetime(ap)) != NULL)
1127 if ((ts = dlocaltimenow()) == NULL)
1130 snprintf(buffer, sizeof(buffer), "%s %s", ap, dtwszone(ts));
1131 if ((tw = dparsetime(buffer)) != NULL)
1134 snprintf(buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
1135 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone(ts));
1136 if ((tw = dparsetime(buffer)) != NULL)
1139 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s",
1140 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
1141 if ((tw = dparsetime(buffer)) != NULL)
1144 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s %s",
1145 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
1147 if ((tw = dparsetime(buffer)) != NULL)
1155 tws_special(char *ap)
1162 if (!mh_strcasecmp(ap, "today"))
1163 return dlocaltime(&clock);
1164 if (!mh_strcasecmp(ap, "yesterday")) {
1165 clock -= (long) (60 * 60 * 24);
1166 return dlocaltime(&clock);
1168 if (!mh_strcasecmp(ap, "tomorrow")) {
1169 clock += (long) (60 * 60 * 24);
1170 return dlocaltime(&clock);
1173 for (i = 0; tw_ldotw[i]; i++)
1174 if (!mh_strcasecmp(ap, tw_ldotw[i]))
1177 if ((tw = dlocaltime(&clock)) == NULL)
1179 if ((i -= tw->tw_wday) > 0)
1185 else /* -ddd days ago */
1186 i = atoi(ap); /* we should error check this */
1188 clock += (long) ((60 * 60 * 24) * i);
1189 return dlocaltime(&clock);
1193 static struct nexus *
1194 createpickthread(char *msgs)
1196 char *folder = NULL;
1197 struct msgs_array msgarray = {0};
1198 struct msgs_array files = {0};
1199 struct nexus *ret = NULL;
1202 struct bin_data *bd;
1204 char **cp = brkstring(msgs, " \t", NULL);
1207 for (; cp && *cp; cp++) {
1215 folder = mh_xstrdup(*cp);
1218 app_msgarg(&msgarray, mh_xstrdup(*cp));
1222 parse_msgs(&msgarray, folder, &files);
1224 for (i = 0; i < files.size; i++) {
1225 buf = getthreadid(files.msgs[i]);
1227 adios(EX_DATAERR, NULL, "message %s is not part of a thread", basename(files.msgs[i]));
1231 c = createonethread(buf);
1239 or = newnexus(or_t);
1246 mh_free0(&(files.msgs));
1247 mh_free0(&(msgarray.msgs));
1252 static struct nexus *
1253 createonethread(char *c)
1255 struct nexus *ret = newnexus(or_t);
1256 struct nexus *left = newnexus(grep_t);
1257 struct nexus *right = newnexus(grep_t);
1260 ret->data.b.left = left;
1261 ret->data.b.right = right;
1262 left->data.g.header = mh_xstrdup("message-id");
1265 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1266 if(!gcompile(&left->data.g, buf)) {
1267 padvise(NULL, "pattern error %s", c);
1271 right->data.g.header = mh_xstrdup("references");
1273 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1274 if(!gcompile(&right->data.g, buf)) {
1275 padvise(NULL, "pattern error in %s", c);