Use trim() to strip whitespace from the end of header fields
[mmh] / uip / anno.c
1 /*
2 ** anno.c -- annotate messages
3 **
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.
7 **
8 ** Three new options have been added: delete, list, and number. Adding
9 ** features to generalize the anno command seemed to be a better approach
10 ** than the creation of a new command whose features would overlap with
11 ** those of the anno command.
12 */
13
14 #include <h/mh.h>
15 #include <h/utils.h>
16 #include <h/tws.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <utime.h>
20
21 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
22
23 static struct swit switches[] = {
24 #define COMPSW 0
25         { "component field", 0 },
26 #define DATESW 1
27         { "date", 0 },
28 #define NDATESW 2
29         { "nodate", 2 },
30 #define TEXTSW 3
31         { "text body", 0 },
32 #define VERSIONSW 4
33         { "Version", 0 },
34 #define HELPSW 5
35         { "help", 0 },
36 #define LISTSW 6
37         { "list", 0 },
38 #define DELETESW 7
39         { "delete", 0 },
40 #define NUMBERSW 8
41         { "number", 0 },
42 #define APPENDSW 9
43         { "append", 0 },
44 #define PRESERVESW 10
45         { "preserve", 0 },
46 #define NOPRESERVESW 11
47         { "nopreserve", 2 },
48         { NULL, 0 }
49 };
50
51 /*
52 ** static prototypes
53 */
54 static void make_comp(unsigned char **);
55 static int annotate(char *, unsigned char *, char *, int, int, int, int);
56 static void annolist(char *, unsigned char *, int);
57 static void dodel(int, unsigned char *, char *, FILE *, int);
58 static void doadd(int, unsigned char *, char *, FILE *, int, int);
59
60
61 int
62 main(int argc, char **argv)
63 {
64         int datesw = 1;
65         int preserve = 0;
66         int msgnum;
67         char *cp, *maildir;
68         unsigned char *comp = NULL;
69         char *text = NULL, *folder = NULL, buf[BUFSIZ];
70         char *file = NULL;
71         char **argp, **arguments;
72         struct msgs_array msgs = { 0, 0, NULL };
73         struct msgs *mp;
74         int append = 0;  /* append annotations instead of default prepend */
75         int number = 0; /* delete specific number of like elements if set */
76
77         setlocale(LC_ALL, "");
78         invo_name = mhbasename(argv[0]);
79         context_read();
80
81         arguments = getarguments(invo_name, argc, argv, 1);
82         argp = arguments;
83
84         while ((cp = *argp++)) {
85                 if (*cp == '-') {
86                         switch (smatch(++cp, switches)) {
87                         case AMBIGSW:
88                                 ambigsw(cp, switches);
89                                 done(1);
90                         case UNKWNSW:
91                                 adios(NULL, "-%s unknown", cp);
92
93                         case HELPSW:
94                                 snprintf(buf, sizeof(buf),
95                                         "%s [+folder] [msgs] [switches]",
96                                         invo_name);
97                                 print_help(buf, switches, 1);
98                                 done(1);
99                         case VERSIONSW:
100                                 print_version(invo_name);
101                                 done(1);
102
103                         case DELETESW:  /* delete annotations */
104                                 mode = MODE_DEL;
105                                 continue;
106
107                         case LISTSW:  /* produce a listing */
108                                 mode = MODE_LIST;
109                                 continue;
110
111                         case COMPSW:
112                                 if (comp)
113                                         adios(NULL, "only one component at a time!");
114                                 if (!(comp = *argp++) || *comp == '-')
115                                         adios(NULL, "missing argument to %s",
116                                                         argp[-2]);
117                                 continue;
118
119                         case TEXTSW:
120                                 if (text)
121                                         adios(NULL, "only one body at a time!");
122                                 if (!(text = *argp++) || *text == '-')
123                                         adios(NULL, "missing argument to %s",
124                                                         argp[-2]);
125                                 continue;
126
127                         case NUMBERSW: /* number listing or delete by number */
128                                 if (mode == MODE_ADD) {
129                                         adios(NULL, "-number switch must appear after -list or -delete, only.");
130                                 }
131                                 if (mode == MODE_LIST) {
132                                         number = 1;
133                                         continue;
134                                 }
135                                 /* MODE_DEL */
136                                 if (number) {
137                                         adios(NULL, "only one number at a time!");
138                                 }
139                                 if (*argp && strcmp(*argp, "all")==0) {
140                                         number = -1;
141                                         argp++;
142                                         continue;
143                                 }
144                                 if (!*argp || !(number = atoi(*argp))) {
145                                         adios(NULL, "missing argument to %s",
146                                                         argp[-1]);
147                                 }
148                                 if (number < 0) {
149                                         adios(NULL, "invalid number (%d).",
150                                                         number);
151                                 }
152                                 argp++;
153                                 continue;
154
155                         case DATESW:
156                                 datesw++;
157                                 continue;
158                         case NDATESW:
159                                 datesw = 0;
160                                 continue;
161
162                         case APPENDSW:
163                                 append = 1;
164                                 continue;
165
166                         case PRESERVESW:
167                                 preserve = 1;
168                                 continue;
169
170                         case NOPRESERVESW:
171                                 preserve = 0;
172                                 continue;
173                         }
174                 }
175                 if (*cp == '+' || *cp == '@') {
176                         if (folder)
177                                 adios(NULL, "only one folder at a time!");
178                         else
179                                 folder = getcpy(expandfol(cp));
180                 } else if (*cp == '/' || *cp == '.') {
181                         if (file)
182                                 adios(NULL, "only one file at a time!");
183                         file = cp;
184                 } else {
185                         app_msgarg(&msgs, cp);
186                 }
187         }
188
189         if (file && (folder || msgs.size)) {
190                 adios(NULL, "Don't intermix files and messages.");
191         }
192         if (!datesw && !text) {
193                 adios(NULL, "-nodate without -text is a no-op.");
194         }
195         if (number && text) {
196                 adios(NULL, "Don't combine -number with -text.");
197         }
198
199         if (file) {
200                 if (mode == MODE_LIST)
201                         annolist(file, comp, number);
202                 else
203                         annotate(file, comp, text, datesw, number,
204                                         append, preserve);
205                 done(0);
206         }
207
208         if (!msgs.size)
209                 app_msgarg(&msgs, seq_cur);
210         if (!folder)
211                 folder = getcurfol();
212         maildir = toabsdir(folder);
213
214         if (chdir(maildir) == NOTOK)
215                 adios(maildir, "unable to change directory to");
216
217         /* read folder and create message structure */
218         if (!(mp = folder_read(folder)))
219                 adios(NULL, "unable to read folder %s", folder);
220
221         /* check for empty folder */
222         if (mp->nummsg == 0)
223                 adios(NULL, "no messages in %s", folder);
224
225         /* parse all the message ranges/sequences and set SELECTED */
226         for (msgnum = 0; msgnum < msgs.size; msgnum++)
227                 if (!m_convert(mp, msgs.msgs[msgnum]))
228                         done(1);
229
230         /* annotate all the SELECTED messages */
231         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
232                 if (is_selected(mp, msgnum)) {
233                         if (mode == MODE_LIST)
234                                 annolist(m_name(msgnum), comp, number);
235                         else
236                                 annotate(m_name(msgnum), comp, text, datesw,
237                                                 number, append, preserve);
238                 }
239         }
240
241         context_replace(curfolder, folder);
242         seq_setcur(mp, mp->lowsel);
243         seq_save(mp);
244         folder_free(mp);
245         context_save();
246         done(0);
247         return 1;
248 }
249
250 static void
251 make_comp(unsigned char **ap)
252 {
253         unsigned char *cp;
254         char buffer[BUFSIZ];
255
256         if (!*ap) {
257                 printf("Enter component name: ");
258                 fflush(stdout);
259
260                 if (!fgets(buffer, sizeof buffer, stdin)) {
261                         done(1);
262                 }
263                 *ap = trimcpy(buffer);
264         }
265
266         if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
267                 *cp = '\0';
268         if (strlen(*ap) == 0)
269                 adios(NULL, "null component name");
270         if (**ap == '-')
271                 adios(NULL, "invalid component name %s", *ap);
272         if (strlen(*ap) >= NAMESZ)
273                 adios(NULL, "too large component name %s", *ap);
274
275         for (cp = *ap; *cp; cp++)
276                 if (!isalnum(*cp) && *cp != '-')
277                         adios(NULL, "invalid component name %s", *ap);
278 }
279
280
281 /*
282 **  Produce a listing of all header fields (annotations) whose field
283 **  name matches comp.  Number the listing if number is set.
284 */
285 static void
286 annolist(char *file, unsigned char *comp, int number)
287 {
288         int c;
289         int count = 1;  /* header field (annotation) counter */
290         char *cp;
291         char *field;
292         int field_size;
293         FILE *fp;
294         int length;
295         int n;  /* number of bytes written */
296
297         if ((fp = fopen(file, "r")) == NULL) {
298                 adios(file, "unable to open");
299         }
300
301         /* We'll grow this buffer as needed. */
302         field = (char *)mh_xmalloc(field_size = 256);
303
304         make_comp(&comp);
305         length = strlen(comp); /* Convenience copy. */
306
307         do {
308                 /*
309                 ** Get a line from the input file, growing the field buffer
310                 ** as needed.  We do this so that we can fit an entire line
311                 ** in the buffer making it easy to do a string comparison
312                 ** on both the field name and the field body which might be
313                 ** a long path name.
314                 */
315                 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
316                         if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
317                                 ungetc(c, fp);
318                                 c = '\n';
319                                 break;
320                         }
321                         if (++n >= field_size - 1) {
322                                 field = (char *)mh_xrealloc(field,
323                                                 field_size += 256);
324                                 cp = field + n - 1;
325                         }
326                 }
327                 *cp = '\0';
328
329                 if (strncasecmp(field, comp, length)==0 &&
330                                 field[length] == ':') {
331                         cp = trim(field + length + 1);
332                         if (number) {
333                                 printf("%d\t", count++);
334                         }
335                         printf("%s\n", cp);
336                 }
337
338         } while (*field && *field != '-');
339
340         free(field);
341         fclose(fp);
342
343         return;
344 }
345
346
347 static int
348 annotate(char *file, unsigned char *comp, char *text, int datesw,
349                 int number, int append, int preserve)
350 {
351         int fd;
352         struct utimbuf b;
353         int perms, tmpfd;
354         char tmpfil[BUFSIZ];
355         struct stat st;
356         FILE *tmp;
357
358         /* open and lock the file to be annotated */
359         if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
360                 switch (errno) {
361                 case ENOENT:
362                         break;
363                 default:
364                         admonish(file, "unable to lock and open");
365                         break;
366                 }
367                 return 1;
368         }
369
370         if (stat(file, &st) == -1) {
371                 advise("can't get access and modification times for %s", file);
372                 preserve = 0;
373         }
374         b.actime = st.st_atime;
375         b.modtime = st.st_mtime;
376
377         perms = fstat(fd, &st) != NOTOK ?
378                         (int)(st.st_mode & 0777) : m_gmprot();
379
380         strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
381                         sizeof(tmpfil));
382         chmod(tmpfil, perms);
383
384         make_comp(&comp);
385
386         if (mode == MODE_DEL) {
387                 dodel(fd, comp, text, tmp, number);
388         }
389         if (mode == MODE_ADD) {
390                 doadd(fd, comp, text, tmp, datesw, append);
391         }
392
393         cpydata(fd, fileno(tmp), file, tmpfil);
394         fclose(tmp);
395
396         if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
397                 adios(tmpfil, "unable to open for re-reading");
398         }
399         lseek(fd, (off_t) 0, SEEK_SET);
400
401         /*
402         **  We're making the file shorter if we're deleting a header field
403         **  so the file has to be truncated or it will contain garbage.
404         */
405         if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
406                 adios(tmpfil, "unable to truncate.");
407         }
408         cpydata(tmpfd, fd, tmpfil, file);
409         close(tmpfd);
410         unlink(tmpfil);
411
412         if (preserve && utime(file, &b) == -1) {
413                 advise("can't set access and modification times for %s", file);
414         }
415         lkclose(fd, file);
416         return 0;
417 }
418
419 /*
420 ** We're trying to delete a header field (annotation).
421 **
422 ** - If number is greater than zero,
423 **   we're deleting the nth header field that matches
424 **   the field (component) name.
425 ** - If number is zero and text is NULL,
426 **   we're deleting the first field in which the field name
427 **   matches the component name.
428 ** - If number is zero and text is set,
429 **   we're deleting the first field in which both the field name
430 **   matches the component name and the field body matches the text.
431 ** - If number is -1,
432 **   we delete all matching fields.
433 */
434 static void
435 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
436 {
437         int length = strlen(comp);  /* convenience copy */
438         int count = 1;  /* Number of matching header line. */
439         int c, n;
440         char *cp;
441         char *field = NULL;
442         int field_size = 256;
443         FILE *fp;
444
445         /*
446         ** We're going to need to copy some of the message file to the
447         ** temporary file while examining the contents.  Convert the
448         ** message file descriptor to a file pointer since it's a lot
449         ** easier and more efficient to use stdio for this.  Also allocate
450         ** a buffer to hold the header components as they're read in.
451         ** This buffer is grown as needed later.
452         */
453         if ((fp = fdopen(fd, "r")) == NULL) {
454                 adios(NULL, "unable to fdopen file.");
455         }
456         field = (char *)mh_xmalloc(field_size);
457
458         /*
459         **  Copy lines from the input file to the temporary file
460         **  until we either find the one that we're looking
461         **  for (which we don't copy) or we reach the end of
462         **  the headers.  Both a blank line and a line beginning
463         **  with a - terminate the headers so that we can handle
464         **  both drafts and RFC-2822 format messages.
465         */
466         do {
467                 /*
468                 ** Get a line from the input file, growing the
469                 ** field buffer as needed.  We do this so that
470                 ** we can fit an entire line in the buffer making
471                 ** it easy to do a string comparison on both the
472                 ** field name and the field body which might be
473                 ** a long path name.
474                 */
475                 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
476                         if (c == '\n' && (c = getc(fp)) != ' ' &&
477                                         c != '\t') {
478                                 ungetc(c, fp);
479                                 c = '\n';
480                                 break;
481                         }
482
483                         if (++n >= field_size - 1) {
484                                 field = (char *) mh_xrealloc(field,
485                                                 field_size *= 2);
486                                 cp = field + n - 1;
487                         }
488                 }
489                 *cp = '\0';
490
491                 if (strncasecmp(field, comp, length)==0 &&
492                                 field[length] == ':') {
493                         /*
494                         ** This component matches and thus is a candidate.
495                         ** We delete the line by not copying it to the
496                         ** temporary file. Thus:
497                         ** - Break if we've found the one to delete.
498                         ** - Continue if this is one to delete, but
499                         **   there'll be further ones.
500                         */
501
502                         if (!number && !text) {
503                                 /* this first one is it */
504                                 break;
505                         }
506
507                         if (number == -1) {
508                                 /* delete all of them */
509                                 continue;
510                         } else if (number == count++) {
511                                 /* delete this specific one */
512                                 break;
513                         }
514
515                         if (text) {
516                                 /* delete the first matching one */
517                                 cp = field+length+1;
518                                 while (*cp==' ' || *cp=='\t') {
519                                         cp++;  /* eat leading whitespace */
520                                 }
521                                 if (*text == '/' && strcmp(text, cp)==0) {
522                                         break;  /* full path matches */
523                                 } else if (strcmp(text, mhbasename(cp))==0) {
524                                         break;  /* basename matches */
525                                 }
526                         }
527                         /*
528                         ** Although the compoment name mached, it
529                         ** wasn't the right one.
530                         */
531                 }
532
533                 /* Copy it. */
534                 if ((n = fputs(field, tmp)) == EOF ||
535                                 (c=='\n' && fputc('\n', tmp)==EOF)) {
536                         adios(NULL, "unable to write temporary file.");
537                 }
538
539         } while (*field && *field != '-');
540
541         free(field);
542
543         fflush(tmp);
544         fflush(fp); /* The underlying fd will be closed by lkclose() */
545
546         /*
547         ** We've been messing with the input file position.  Move the
548         ** input file descriptor to the current place in the file
549         ** because the stock data copying routine uses the descriptor,
550         ** not the pointer.
551         */
552         if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
553                 adios(NULL, "can't seek.");
554         }
555 }
556
557
558 static void
559 doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
560                 int append)
561 {
562         char *cp, *sp;
563         int c;
564         FILE *fp = NULL;
565
566         if (append) {
567                 /*
568                 ** We're going to need to copy some of the message
569                 ** file to the temporary file while examining the
570                 ** contents.  Convert the message file descriptor to
571                 ** a file pointer since it's a lot easier and more
572                 ** efficient to use stdio for this.  Also allocate
573                 ** a buffer to hold the header components as they're
574                 ** read in.  This buffer is grown as needed later.
575                 */
576                 if ((fp = fdopen(fd, "r")) == NULL) {
577                         adios(NULL, "unable to fdopen file.");
578                 }
579                 /* Find the end of the headers. */
580                 if ((c = getc(fp)) == '\n') {
581                         /* Special check for no headers is needed. */
582                         rewind(fp);
583                 } else {
584                         /*
585                         ** Copy lines from the input file to the
586                         ** temporary file until we reach the end
587                         ** of the headers.
588                         */
589                         putc(c, tmp);
590                         while ((c = getc(fp)) != EOF) {
591                                 putc(c, tmp);
592                                 if (c == '\n') {
593                                         ungetc(c = getc(fp), fp);
594                                         if (c == '\n' || c == '-') {
595                                                 break;
596                                         }
597                                 }
598                         }
599                 }
600         }
601
602         if (datesw) {
603                 fprintf(tmp, "%s: %s\n", comp, dtimenow());
604         }
605         if ((cp = text)) {
606                 /* Add body text header */
607                 do {
608                         while (*cp == ' ' || *cp == '\t') {
609                                 cp++;
610                         }
611                         sp = cp;
612                         while (*cp && *cp++ != '\n') {
613                                 continue;
614                         }
615                         if (cp - sp) {
616                                 fprintf(tmp, "%s: %*.*s", comp,
617                                         (int)(cp - sp),
618                                         (int)(cp - sp), sp);
619                         }
620                 } while (*cp);
621                 if (cp[-1] != '\n' && cp != text) {
622                         putc('\n', tmp);
623                 }
624         }
625         fflush(tmp);
626
627         /*
628         ** We've been messing with the input file position.  Move the
629         ** input file descriptor to the current place in the file
630         ** because the stock data copying routine uses the descriptor,
631         ** not the pointer.
632         */
633         if (append) {
634                 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
635                         adios(NULL, "can't seek.");
636                 }
637         }
638 }