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]))
345 ** If we aren't saving the results to a sequence,
346 ** we default to list the results.
351 if (publicsw == 1 && is_readonly(mp))
352 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
355 if (!pcompile(vec, NULL))
362 ** If printing message numbers to standard out,
363 ** force line buffering on.
366 setvbuf(stdout, NULL, _IOLBF, 0);
369 ** Scan through all the SELECTED messages and check for a
370 ** match. If the message does not match, then unselect it.
372 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
373 if (is_selected(mp, msgnum)) {
374 if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
375 admonish(cp, "unable to read message");
376 if (fp && pmatches(fp, msgnum)) {
383 printmsg(fp, mp, msgnum, fmtstr, width);
386 /* if it doesn't match, then unselect it */
387 unset_selected(mp, msgnum);
400 adios(EX_DATAERR, NULL, "no messages match specification");
405 ** Add the matching messages to sequences
407 for (seqp = 0; seqs[seqp]; seqp++)
408 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
412 ** Print total matched if not printing each matched message number.
415 printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
418 context_replace(curfolder, folder); /* update current folder */
419 seq_save(mp); /* synchronize message sequences */
420 context_save(); /* save the context file */
421 folder_free(mp); /* free folder/message structure */
422 listsw = 0; /* HACK */
427 scan_mbox(char *file, char *fmtstr, int width)
433 if (strcmp(file, "-") == 0) {
436 } else if (!(in = fopen(file, "r"))) {
437 adios(EX_IOERR, file, "unable to open");
440 for (msgnum = 1; ;msgnum++) {
441 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
442 if (state != SCNMSG) {
452 if (listsw && !isatty(fileno(stdout)))
457 printmsg(FILE *f, struct msgs *mp, int msgnum, char *fmtstr, int width)
461 boolean unseen = FALSE;
463 fseek(f, 0L, SEEK_SET);
465 seqnum = seq_getnum(mp, seq_unseen);
466 unseen = in_sequence(mp, seqnum, msgnum);
468 switch (state = scan(f, msgnum, SCN_FOLD, fmtstr,
469 width, msgnum==mp->curmsg, unseen)) {
474 advise(NULL, "message %d: empty", msgnum);
477 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
481 static struct swit parswit[] = {
495 { "date pattern", 0 },
497 { "from pattern", 0 },
499 { "search pattern", 0 },
501 { "subject pattern", 0 },
505 { "-othercomponent pattern", 15 },
509 { "before date", 0 },
511 { "datefield field", 5 },
518 ** DEFINITIONS FOR NEXUS
521 #define nxtarg() (*argp ? *argp++ : NULL)
522 #define prvarg() argp--
524 #define padvise if (!talked++) advise
527 static int pdebug = 0;
533 ** prototypes for date routines
535 static struct tws *tws_parse(char *, int);
536 static struct tws *tws_special(char *);
541 static int gcompile(struct grep_data *, const char *);
542 static int tcompile(char *, struct tws *, int);
544 static struct nexus *parse(void);
545 static struct nexus *nexp1(void);
546 static struct nexus *nexp2(void);
547 static struct nexus *nexp3(void);
548 static struct nexus *newnexus(enum nexus_type);
551 pcompile(char **vec, char *date)
555 if ((cp = getenv("MHPDEBUG")) && *cp)
559 if ((datesw = date) == NULL)
563 if ((head = parse()) == NULL)
564 return (talked ? 0 : 1);
567 padvise(NULL, "%s unexpected", *argp);
575 static struct nexus *
580 struct bin_data *bin;
582 if ((n = nexp1()) == NULL || (cp = nxtarg()) == NULL)
586 padvise(NULL, "%s unexpected", cp);
592 switch (smatch(cp, parswit)) {
594 ambigsw(cp, parswit);
598 fprintf(stderr, "-%s unknown\n", cp);
606 if ((bin->right = parse()))
608 padvise(NULL, "missing disjunctive");
618 static struct nexus *
623 struct bin_data *bin;
625 if ((n = nexp2()) == NULL || (cp = nxtarg()) == NULL)
629 padvise(NULL, "%s unexpected", cp);
635 switch (smatch(cp, parswit)) {
637 ambigsw(cp, parswit);
641 fprintf(stderr, "-%s unknown\n", cp);
649 if ((bin->right = nexp1()))
651 padvise(NULL, "missing conjunctive");
662 static struct nexus *
667 struct bin_data *bin;
669 if ((cp = nxtarg()) == NULL)
679 switch (smatch(cp, parswit)) {
681 ambigsw(cp, parswit);
685 fprintf(stderr, "-%s unknown\n", cp);
692 if ((bin->left = nexp3()))
694 padvise(NULL, "missing negation");
704 static struct nexus *
709 char buffer[BUFSIZ], temp[64];
711 struct grep_data *gdata;
712 struct date_data *twsd;
714 if ((cp = nxtarg()) == NULL)
718 padvise(NULL, "%s unexpected", cp);
726 switch (i = smatch(cp, parswit)) {
728 ambigsw(cp, parswit);
732 fprintf(stderr, "-%s unknown\n", cp);
737 if ((n = parse()) == NULL) {
738 padvise(NULL, "missing group");
741 if ((cp = nxtarg()) == NULL) {
742 padvise(NULL, "missing -rbrace");
745 if (*cp++ == '-' && smatch(cp, parswit) == PRRBR)
747 padvise(NULL, "%s unexpected", --cp);
755 if (!(cp = nxtarg())) { /* allow -xyz arguments */
756 padvise(NULL, "missing argument to %s", argp[-2]);
758 return createpickthread(cp);
764 strncpy(temp, parswit[i].sw, sizeof(temp));
765 temp[sizeof(temp) - 1] = '\0';
766 dp = *brkstring(temp, " ", NULL);
768 if (!(cp = nxtarg())) { /* allow -xyz arguments */
769 padvise(NULL, "missing argument to %s", argp[-2]);
772 n = newnexus(grep_t);
774 gdata->header = mh_xstrdup(dp);
775 snprintf(buffer, sizeof(buffer), "%s", cp);
780 n = newnexus(grep_t);
782 gdata->header = NULL;
784 if (!(cp = nxtarg())) { /* allow -xyz arguments */
785 padvise(NULL, "missing argument to %s", argp[-2]);
790 if (!gcompile(gdata, dp)) {
791 padvise("regcomp", "pattern error in %s %s", argp[-2], cp);
797 padvise(NULL, "internal error!");
801 if (!(datesw = nxtarg()) || *datesw == '-') {
802 padvise(NULL, "missing argument to %s",
810 if (!(cp = nxtarg())) { /* allow -xyz arguments */
811 padvise(NULL, "missing argument to %s", argp[-2]);
814 n = newnexus(date_t);
816 twsd->datef = datesw;
817 if (!tcompile(cp, &twsd->tws, twsd->after = i == PRAFTR)) {
818 padvise(NULL, "unable to parse %s %s", argp[-2], cp);
826 static struct nexus *
827 newnexus(enum nexus_type t)
829 struct nexus *p = NULL;
830 p = mh_xcalloc(1, sizeof(struct nexus));
836 static void nexus_clear(struct nexus *n)
842 nexus_clear(n->data.b.right);
845 nexus_clear(n->data.b.left);
853 pmatches(FILE *fp, int msgnum)
855 struct field f = {{0}};
863 if (!talked++ && pdebug) {
864 nexus_debug(head, 0);
867 while (s == FLD2 || s == BODY2) {
868 switch (s = m_getfld2(s, &f, fp)) {
873 nexus_match(&f, msgnum, head);
879 nexus_match(&f, msgnum, head);
882 advise(NULL, "IOERR in message %d\n", msgnum);
887 adios(EX_SOFTWARE, "m_getfld2", "returned unknown state %d at message %d", s, msgnum);
894 match_grep(struct field *f, struct grep_data *g)
899 if (!g->header && *f->name) {
904 ret = regexec(g->preg, f->value, 0, NULL, 0);
908 /* check for the right field */
909 if (!(g->header && *g->header && mh_strcasecmp(g->header, f->name)==0)) {
913 if (decode_rfc2047(f->value, buf, sizeof(buf))) {
914 ret = regexec(g->preg, buf, 0, NULL, 0);
916 ret = regexec(g->preg, f->value, 0, NULL, 0);
925 regerror(ret, g->preg, buf, sizeof(buf));
926 fprintf(stderr, "%s\n", buf);
932 match_date(struct field *f, int msgnum, struct date_data *dd)
938 if (mh_strcasecmp(f->name, dd->datef)!=0) {
941 bp = mh_xstrdup(f->value);
942 if ((tw = dparsetime(bp)) == NULL) {
943 advise(NULL, "unable to parse %s field in message %d, not matching...", dd->datef, msgnum);
944 } else if (dd->after) {
945 ret = twsort(tw, &dd->tws) > 0;
947 ret = twsort(tw, &dd->tws) < 0;
955 nexus_match(struct field *f, int msgnum, struct nexus *n)
959 n->match = nexus_match(f, msgnum, n->data.b.left);
960 n->match = nexus_match(f, msgnum, n->data.b.right) && n->match;
963 n->match = nexus_match(f, msgnum, n->data.b.left);
964 n->match = nexus_match(f, msgnum, n->data.b.right) || n->match;
967 n->match = !nexus_match(f, msgnum, n->data.b.left);
973 n->match = match_date(f, msgnum, &n->data.d);
979 n->match = match_grep(f, &n->data.g);
982 adios(EX_SOFTWARE, NULL, "nexus tree contains a unknown nexus_type (%d)", n->t);
988 nexus_debug(struct nexus *n, size_t level)
990 struct date_data *dd;
991 print_debug_level(level);
994 fputs("AND\n", stderr);
995 nexus_debug(n->data.b.left, level+1);
996 nexus_debug(n->data.b.right, level+1);
999 fputs("OR\n", stderr);
1000 nexus_debug(n->data.b.left, level+1);
1001 nexus_debug(n->data.b.right, level+1);
1004 fputs("NOT\n", stderr);
1005 nexus_debug(n->data.b.left, level+1);
1008 nexus_debug_grep(&n->data.g);
1012 fprintf(stderr, "TEMPORAL(%s) %s: %s\n",dd->after ? "after" : "before", dd->datef, dasctime(&dd->tws));
1015 adios(EX_SOFTWARE, NULL, "nexus tree contains a unknown nexus_type (%d)", n->t);
1020 nexus_debug_grep(struct grep_data *gd)
1022 char *buf, *buf2, *pbuf, *pbuf2;
1024 pbuf = pbuf2 = mh_xstrdup(gd->pattern);
1026 for (;*pbuf2; pbuf2++) {
1027 *pbuf2 = tolower(*pbuf2);
1031 buf = buf2 = mh_xstrdup(gd->header);
1032 for (;*buf2; buf2++) {
1033 *buf2 = tolower(*buf2);
1035 fprintf(stderr, "PETTERN(%s) %s\n", buf, pbuf);
1037 fprintf(stderr, "PETTERN(BODY) %s\n", pbuf);
1044 nexus_free(struct nexus **n)
1052 nexus_free(&(*n)->data.b.right);
1055 nexus_free(&(*n)->data.b.left);
1058 mh_free0(&(*n)->data.g.header);
1059 mh_free0(&(*n)->data.g.pattern);
1060 regfree((*n)->data.g.preg);
1064 advise(NULL, "Unknown nexus_type (%d) to free", (*n)->t);
1070 print_debug_level(size_t level)
1074 for (i = 0; i < level; i++) {
1075 fputs("| ", stderr);
1080 gcompile(struct grep_data *g, const char *astr)
1082 regex_t *preg = mh_xcalloc(1, sizeof(regex_t));
1087 g->pattern = mh_xstrdup(astr);
1088 ret = regcomp(preg, astr, REG_ICASE | REG_NOSUB);
1090 buf = mh_xcalloc(BUFSIZ, sizeof(char));
1091 regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
1092 fprintf(stderr, "%s\n", buf);
1100 tcompile(char *ap, struct tws *tb, int isafter)
1104 if ((tw = tws_parse(ap, isafter)) == NULL)
1113 tws_parse(char *ap, int isafter)
1115 char buffer[BUFSIZ];
1116 struct tws *tw, *ts;
1118 if ((tw = tws_special(ap)) != NULL) {
1119 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
1120 tw->tw_hour = isafter ? 23 : 0;
1123 if ((tw = dparsetime(ap)) != NULL)
1126 if ((ts = dlocaltimenow()) == NULL)
1129 snprintf(buffer, sizeof(buffer), "%s %s", ap, dtwszone(ts));
1130 if ((tw = dparsetime(buffer)) != NULL)
1133 snprintf(buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
1134 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone(ts));
1135 if ((tw = dparsetime(buffer)) != NULL)
1138 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s",
1139 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
1140 if ((tw = dparsetime(buffer)) != NULL)
1143 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s %s",
1144 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
1146 if ((tw = dparsetime(buffer)) != NULL)
1154 tws_special(char *ap)
1161 if (!mh_strcasecmp(ap, "today"))
1162 return dlocaltime(&clock);
1163 if (!mh_strcasecmp(ap, "yesterday")) {
1164 clock -= (long) (60 * 60 * 24);
1165 return dlocaltime(&clock);
1167 if (!mh_strcasecmp(ap, "tomorrow")) {
1168 clock += (long) (60 * 60 * 24);
1169 return dlocaltime(&clock);
1172 for (i = 0; tw_ldotw[i]; i++)
1173 if (!mh_strcasecmp(ap, tw_ldotw[i]))
1176 if ((tw = dlocaltime(&clock)) == NULL)
1178 if ((i -= tw->tw_wday) > 0)
1184 else /* -ddd days ago */
1185 i = atoi(ap); /* we should error check this */
1187 clock += (long) ((60 * 60 * 24) * i);
1188 return dlocaltime(&clock);
1192 static struct nexus *
1193 createpickthread(char *msgs)
1195 char *folder = NULL;
1196 struct msgs_array msgarray = {0};
1197 struct msgs_array files = {0};
1198 struct nexus *ret = NULL;
1201 struct bin_data *bd;
1203 char **cp = brkstring(msgs, " \t", NULL);
1206 for (; cp && *cp; cp++) {
1214 folder = mh_xstrdup(*cp);
1217 app_msgarg(&msgarray, mh_xstrdup(*cp));
1221 parse_msgs(&msgarray, folder, &files);
1223 for (i = 0; i < files.size; i++) {
1224 buf = getthreadid(files.msgs[i]);
1226 adios(EX_DATAERR, NULL, "message %s is not part of a thread", basename(files.msgs[i]));
1230 c = createonethread(buf);
1238 or = newnexus(or_t);
1245 mh_free0(&(files.msgs));
1246 mh_free0(&(msgarray.msgs));
1251 static struct nexus *
1252 createonethread(char *c)
1254 struct nexus *ret = newnexus(or_t);
1255 struct nexus *left = newnexus(grep_t);
1256 struct nexus *right = newnexus(grep_t);
1259 ret->data.b.left = left;
1260 ret->data.b.right = right;
1261 left->data.g.header = mh_xstrdup("message-id");
1264 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1265 if(!gcompile(&left->data.g, buf)) {
1266 padvise(NULL, "pattern error %s", c);
1270 right->data.g.header = mh_xstrdup("references");
1272 snprintf(buf, sizeof(buf), "^[ \t]*<%s>", c);
1273 if(!gcompile(&right->data.g, buf)) {
1274 padvise(NULL, "pattern error in %s", c);