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.
45 static struct swit switches[] = {
47 { "component field", 0 },
68 #define NOPRESERVESW 11
76 static void make_comp(unsigned char **);
80 main(int argc, char **argv)
86 unsigned char *comp = NULL;
87 char *text = NULL, *folder = NULL, buf[BUFSIZ];
89 char **argp, **arguments;
90 struct msgs_array msgs = { 0, 0, NULL };
92 int append = 0; /* append annotations instead of default prepend */
93 int delete = -2; /* delete header element if set */
94 int list = 0; /* list header elements if set */
95 int number = 0; /* delete specific number of like elements if set */
99 setlocale(LC_ALL, "");
101 invo_name = mhbasename(argv[0]);
103 /* read user profile/context */
106 arguments = getarguments(invo_name, argc, argv, 1);
109 while ((cp = *argp++)) {
111 switch (smatch(++cp, switches)) {
113 ambigsw(cp, switches);
116 adios(NULL, "-%s unknown", cp);
119 snprintf(buf, sizeof(buf),
120 "%s [+folder] [msgs] [switches]",
122 print_help(buf, switches, 1);
125 print_version(invo_name);
130 adios(NULL, "only one component at a time!");
131 if (!(comp = *argp++) || *comp == '-')
132 adios(NULL, "missing argument to %s",
145 adios(NULL, "only one body at a time!");
146 if (!(text = *argp++) || *text == '-')
147 adios(NULL, "missing argument to %s",
151 case DELETESW: /* delete annotations */
155 case LISTSW: /* produce a listing */
159 case NUMBERSW: /* number listing or delete by number */
161 adios(NULL, "only one number at a time!");
163 if (argp-arguments == argc-1 || **argp == '-')
167 if (strcmp(*argp, "all") == 0)
169 else if (!(number = atoi(*argp)))
171 ** `-list -number l:10'
172 ** but okay if we add `all'.
174 adios(NULL, "missing argument to %s", argp[-1]);
194 if (*cp == '+' || *cp == '@') {
196 adios(NULL, "only one folder at a time!");
198 folder = getcpy(expandfol(cp));
199 } else if (*cp == '/' || *cp == '.') {
201 adios(NULL, "only one file at a time!");
204 app_msgarg(&msgs, cp);
209 if (file && (folder || havemsgs)) {
210 adios(NULL, "Don't intermix files and messages.");
217 annolist(file, comp, text, number);
219 annotate(file, comp, text,
220 datesw, delete, append, preserve);
225 app_msgarg(&msgs, seq_cur);
227 folder = getcurfol();
228 maildir = toabsdir(folder);
230 if (chdir(maildir) == NOTOK)
231 adios(maildir, "unable to change directory to");
233 /* read folder and create message structure */
234 if (!(mp = folder_read(folder)))
235 adios(NULL, "unable to read folder %s", folder);
237 /* check for empty folder */
239 adios(NULL, "no messages in %s", folder);
241 /* parse all the message ranges/sequences and set SELECTED */
242 for (msgnum = 0; msgnum < msgs.size; msgnum++)
243 if (!m_convert(mp, msgs.msgs[msgnum]))
246 /* annotate all the SELECTED messages */
247 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248 if (is_selected(mp, msgnum)) {
250 annolist(m_name(msgnum), comp, text, number);
252 annotate(m_name(msgnum), comp, text, datesw,
253 delete, append, preserve);
257 context_replace(curfolder, folder);
258 seq_setcur(mp, mp->lowsel);
267 make_comp(unsigned char **ap)
269 register unsigned char *cp;
273 printf("Enter component name: ");
276 if (fgets(buffer, sizeof buffer, stdin) == NULL)
278 *ap = trimcpy(buffer);
281 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
283 if (strlen(*ap) == 0)
284 adios(NULL, "null component name");
286 adios(NULL, "invalid component name %s", *ap);
287 if (strlen(*ap) >= NAMESZ)
288 adios(NULL, "too large component name %s", *ap);
290 for (cp = *ap; *cp; cp++)
291 if (!isalnum(*cp) && *cp != '-')
292 adios(NULL, "invalid component name %s", *ap);