2 ** anno.c -- annotate messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
8 ** Three new options have been added: delete, list, and number.
9 ** Message header fields are used by the new MIME attachment code in
10 ** the send command. Adding features to generalize the anno command
11 ** seemed to be a better approach than the creation of a new command
12 ** whose features would overlap with those of the anno command.
14 ** The -delete option deletes header elements that match the -component
15 ** field name. If -delete is used without the -text option, the first
16 ** header field whose field name matches the component name is deleted.
17 ** If the -delete is used with the -text option, and the -text argument
18 ** begins with a /, the first header field whose field name matches the
19 ** component name and whose field body matches the text is deleted. If
20 ** the -text argument does not begin with a /, then the text is assumed
21 ** to be the last component of a path name, and the first header field
22 ** whose field name matches the component name and a field body whose
23 ** last path name component matches the text is deleted. If the -delete
24 ** option is used with the new -number option described below, the nth
25 ** header field whose field name matches the component name is deleted.
26 ** No header fields are deleted if none of the above conditions are met.
28 ** The -list option outputs the field bodies from each header field whose
29 ** field name matches the component name, one per line. If no -text
30 ** option is specified, only the last path name component of each field
31 ** body is output. The entire field body is output if the -text option
32 ** is used; the contents of the -text argument are ignored. If the -list
33 ** option is used in conjuction with the new -number option described
34 ** below, each line is numbered starting with 1. A tab separates the
35 ** number from the field body.
37 ** The -number option works with both the -delete and -list options as
38 ** described above. The -number option takes an optional argument. A
39 ** value of 1 is assumed if this argument is absent.
50 static struct swit switches[] = {
52 { "component field", 0 },
73 #define NOPRESERVESW 11
81 static void make_comp(unsigned char **);
82 static int annosbr(int, char *, char *, char *, int, int, int);
83 static int annotate(char *, char *, char *, int, int, int, int);
84 static void annolist(char *, char *, char *, int);
88 main(int argc, char **argv)
94 unsigned char *comp = NULL;
95 char *text = NULL, *folder = NULL, buf[BUFSIZ];
97 char **argp, **arguments;
98 struct msgs_array msgs = { 0, 0, NULL };
100 int append = 0; /* append annotations instead of default prepend */
101 int delete = -2; /* delete header element if set */
102 int list = 0; /* list header elements if set */
103 int number = 0; /* delete specific number of like elements if set */
107 setlocale(LC_ALL, "");
109 invo_name = mhbasename(argv[0]);
111 /* read user profile/context */
114 arguments = getarguments(invo_name, argc, argv, 1);
117 while ((cp = *argp++)) {
119 switch (smatch(++cp, switches)) {
121 ambigsw(cp, switches);
124 adios(NULL, "-%s unknown", cp);
127 snprintf(buf, sizeof(buf),
128 "%s [+folder] [msgs] [switches]",
130 print_help(buf, switches, 1);
133 print_version(invo_name);
138 adios(NULL, "only one component at a time!");
139 if (!(comp = *argp++) || *comp == '-')
140 adios(NULL, "missing argument to %s",
153 adios(NULL, "only one body at a time!");
154 if (!(text = *argp++) || *text == '-')
155 adios(NULL, "missing argument to %s",
159 case DELETESW: /* delete annotations */
163 case LISTSW: /* produce a listing */
167 case NUMBERSW: /* number listing or delete by number */
169 adios(NULL, "only one number at a time!");
171 if (argp-arguments == argc-1 || **argp == '-')
175 if (strcmp(*argp, "all") == 0)
177 else if (!(number = atoi(*argp)))
179 ** `-list -number l:10'
180 ** but okay if we add `all'.
182 adios(NULL, "missing argument to %s", argp[-1]);
202 if (*cp == '+' || *cp == '@') {
204 adios(NULL, "only one folder at a time!");
206 folder = getcpy(expandfol(cp));
207 } else if (*cp == '/' || *cp == '.') {
209 adios(NULL, "only one file at a time!");
212 app_msgarg(&msgs, cp);
217 if (file && (folder || havemsgs)) {
218 adios(NULL, "Don't intermix files and messages.");
225 annolist(file, comp, text, number);
227 annotate(file, comp, text,
228 datesw, delete, append, preserve);
233 app_msgarg(&msgs, seq_cur);
235 folder = getcurfol();
236 maildir = toabsdir(folder);
238 if (chdir(maildir) == NOTOK)
239 adios(maildir, "unable to change directory to");
241 /* read folder and create message structure */
242 if (!(mp = folder_read(folder)))
243 adios(NULL, "unable to read folder %s", folder);
245 /* check for empty folder */
247 adios(NULL, "no messages in %s", folder);
249 /* parse all the message ranges/sequences and set SELECTED */
250 for (msgnum = 0; msgnum < msgs.size; msgnum++)
251 if (!m_convert(mp, msgs.msgs[msgnum]))
254 /* annotate all the SELECTED messages */
255 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
256 if (is_selected(mp, msgnum)) {
258 annolist(m_name(msgnum), comp, text, number);
260 annotate(m_name(msgnum), comp, text, datesw,
261 delete, append, preserve);
265 context_replace(curfolder, folder);
266 seq_setcur(mp, mp->lowsel);
275 make_comp(unsigned char **ap)
277 register unsigned char *cp;
281 printf("Enter component name: ");
284 if (fgets(buffer, sizeof buffer, stdin) == NULL)
286 *ap = trimcpy(buffer);
289 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
291 if (strlen(*ap) == 0)
292 adios(NULL, "null component name");
294 adios(NULL, "invalid component name %s", *ap);
295 if (strlen(*ap) >= NAMESZ)
296 adios(NULL, "too large component name %s", *ap);
298 for (cp = *ap; *cp; cp++)
299 if (!isalnum(*cp) && *cp != '-')
300 adios(NULL, "invalid component name %s", *ap);
305 annotate(char *file, char *comp, char *text, int datesw,
306 int delete, int append, int preserve)
312 /* open and lock the file to be annotated */
313 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
318 admonish(file, "unable to lock and open");
324 if (stat(file, &s) == -1) {
325 advise("can't get access and modification times for %s", file);
329 b.actime = s.st_atime;
330 b.modtime = s.st_mtime;
332 i = annosbr(fd, file, comp, text, datesw, delete, append);
334 if (preserve && utime(file, &b) == -1) {
335 advise("can't set access and modification times for %s", file);
342 ** Produce a listing of all header fields (annotations) whose field
343 ** name matches comp. Number the listing if number is set. Treat the
344 ** field bodies as path names and just output the last component unless
345 ** text is non-NULL. We don't care what text is set to.
348 annolist(char *file, char *comp, char *text, int number)
351 int count; /* header field (annotation) counter */
357 int n; /* number of bytes written */
360 if ((fp = fopen(file, "r")) == NULL) {
361 adios(file, "unable to open");
364 /* We'll grow this buffer as needed. */
365 field = (char *)mh_xmalloc(field_size = 256);
367 length = strlen(comp); /* Convenience copy. */
372 ** Get a line from the input file, growing the field buffer
373 ** as needed. We do this so that we can fit an entire line
374 ** in the buffer making it easy to do a string comparison
375 ** on both the field name and the field body which might be
378 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
379 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
384 if (++n >= field_size - 1) {
385 field = (char *)mh_xrealloc(field,
392 if (strncasecmp(field, comp, length)==0 &&
393 field[length] == ':') {
394 for (cp = field + length + 1;
395 *cp == ' ' || *cp == '\t'; cp++) {
399 printf("%d\t", ++count);
401 if (!text && (sp = strrchr(cp, '/'))) {
407 } while (*field && *field != '-');
416 annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
425 int count; /* header field (annotation) counter */
430 int n; /* number of bytes written */
432 mode = fstat(fd, &st) != NOTOK ? (int)(st.st_mode & 0777) : m_gmprot();
434 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
439 ** We're going to need to copy some of the message file to the
440 ** temporary file while examining the contents. Convert the
441 ** message file descriptor to a file pointer since it's a lot
442 ** easier and more efficient to use stdio for this. Also allocate
443 ** a buffer to hold the header components as they're read in.
444 ** This buffer is grown as needed later.
446 if (delete >= -1 || append != 0) {
447 if ((fp = fdopen(fd, "r")) == NULL) {
448 adios(NULL, "unable to fdopen file.");
450 field = (char *)mh_xmalloc(field_size = 256);
454 ** We're trying to delete a header field (annotation) if the
455 ** delete flag is greater -2. A value greater than zero
456 ** means that we're deleting the nth header field that matches
457 ** the field (component) name. A value of zero means that
458 ** we're deleting the first field in which both the field name
459 ** matches the component name and the field body matches the text.
460 ** The text is matched in its entirety if it begins with a slash;
461 ** otherwise the text is matched against whatever portion of the
462 ** field body follows the last slash. This allows matching of
463 ** both absolute and relative path names. This is because this
464 ** functionality was added to support attachments. It might be
465 ** worth having a separate flag to indicate path name matching
466 ** to make it more general. A value of -1 means to delete all
470 length = strlen(comp); /* convenience copy */
471 count = 0; /* Only used if we're deleting by number. */
474 ** Copy lines from the input file to the temporary file
475 ** until we either find the one that we're looking
476 ** for (which we don't copy) or we reach the end of
477 ** the headers. Both a blank line and a line beginning
478 ** with a - terminate the headers so that we can handle
479 ** both drafts and RFC-2822 format messages.
483 ** Get a line from the input file, growing the
484 ** field buffer as needed. We do this so that
485 ** we can fit an entire line in the buffer making
486 ** it easy to do a string comparison on both the
487 ** field name and the field body which might be
490 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
491 if (c == '\n' && (c = getc(fp)) != ' ' &&
498 if (++n >= field_size - 1) {
499 field = (char *) mh_xrealloc(field,
507 ** Check for a match on the field name. We delete
508 ** the line by not copying it to the temporary
511 ** o The delete flag is 0, meaning that we're
512 ** going to delete the first matching
513 ** field, and the text is NULL meaning that
514 ** we don't care about the field body.
516 ** o The delete flag is 0, meaning that we're
517 ** going to delete the first matching
518 ** field, and the text begins with a / meaning
519 ** that we're looking for a full path name,
520 ** and the text matches the field body.
522 ** o The delete flag is 0, meaning that we're
523 ** going to delete the first matching
524 ** field, the text does not begin with a /
525 ** meaning that we're looking for the last
526 ** path name component, and the last path
527 ** name component matches the text.
529 ** o The delete flag is positive meaning that
530 ** we're going to delete the nth field
531 ** with a matching field name, and this is
532 ** the nth matching field name.
534 ** o The delete flag is -1 meaning that we're
535 ** going to delete all fields with a
536 ** matching field name.
538 if (strncasecmp(field, comp, length)==0 &&
539 field[length] == ':') {
544 for (cp=field+length+1;
545 *cp==' ' || *cp=='\t';
550 strcmp(cp, text)==0) {
553 if ((sp = strrchr(cp, '/'))) {
556 if (strcmp(cp, text)==0) {
560 } else if (delete == -1) {
562 } else if (++count == delete) {
568 ** This line wasn't a match so copy it to the
571 if ((n = fputs(field, tmp)) == EOF ||
572 (c=='\n' && fputc('\n', tmp)==EOF)) {
573 adios(NULL, "unable to write temporary file.");
575 } while (*field && *field != '-');
580 ** Find the end of the headers before adding the
581 ** annotations if we're appending instead of the default
582 ** prepending. A special check for no headers is needed
587 ** Copy lines from the input file to the temporary
588 ** file until we reach the end of the headers.
590 if ((c = getc(fp)) == '\n') {
594 while ((c = getc(fp)) != EOF) {
597 ungetc(c = getc(fp), fp);
598 if (c == '\n' || c == '-') {
607 fprintf(tmp, "%s: %s\n", comp, dtimenow());
611 while (*cp == ' ' || *cp == '\t') {
615 while (*cp && *cp++ != '\n') {
619 fprintf(tmp, "%s: %*.*s", comp,
624 if (cp[-1] != '\n' && cp != text) {
632 ** We've been messing with the input file position. Move the
633 ** input file descriptor to the current place in the file
634 ** because the stock data copying routine uses the descriptor,
637 if (append || delete >= -1) {
638 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
639 adios(NULL, "can't seek.");
643 cpydata(fd, fileno(tmp), file, tmpfil);
646 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
647 adios(tmpfil, "unable to open for re-reading");
649 lseek(fd, (off_t) 0, SEEK_SET);
652 ** We're making the file shorter if we're deleting a header field
653 ** so the file has to be truncated or it will contain garbage.
655 if (delete >= -1 && ftruncate(fd, 0) == -1) {
656 adios(tmpfil, "unable to truncate.");
658 cpydata(tmpfd, fd, tmpfil, file);
663 ** Close the delete file so that we don't run out of file pointers if
664 ** we're doing piles of files. Note that this will make the close() in
665 ** lkclose() fail, but that failure is ignored so it's not a problem.