3 * anno.c -- annotate messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
11 * Four new options have been added: delete, list, number, and draft.
12 * Message header fields are used by the new MIME attachment code in
13 * the send command. Adding features to generalize the anno command
14 * seemed to be a better approach than the creation of a new command
15 * whose features would overlap with those of the anno command.
17 * The -draft option causes anno to operate on the current draft file
18 * instead of on a message sequence.
20 * The -delete option deletes header elements that match the -component
21 * field name. If -delete is used without the -text option, the first
22 * header field whose field name matches the component name is deleted.
23 * If the -delete is used with the -text option, and the -text argument
24 * begins with a /, the first header field whose field name matches the
25 * component name and whose field body matches the text is deleted. If
26 * the -text argument does not begin with a /, then the text is assumed
27 * to be the last component of a path name, and the first header field
28 * whose field name matches the component name and a field body whose
29 * last path name component matches the text is deleted. If the -delete
30 * option is used with the new -number option described below, the nth
31 * header field whose field name matches the component name is deleted.
32 * No header fields are deleted if none of the above conditions are met.
34 * The -list option outputs the field bodies from each header field whose
35 * field name matches the component name, one per line. If no -text
36 * option is specified, only the last path name component of each field
37 * body is output. The entire field body is output if the -text option
38 * is used; the contents of the -text argument are ignored. If the -list
39 * option is used in conjuction with the new -number option described
40 * below, each line is numbered starting with 1. A tab separates the
41 * number from the field body.
43 * The -number option works with both the -delete and -list options as
44 * described above. The -number option takes an optional argument. A
45 * value of 1 is assumed if this argument is absent.
51 * We allocate space for messages (msgs array)
52 * this number of elements at a time.
57 static struct swit switches[] = {
59 { "component field", 0 },
90 static void make_comp (char **);
94 main (int argc, char **argv)
96 int inplace = 1, datesw = 1;
97 int nummsgs, maxmsgs, msgnum;
98 char *cp, *maildir, *comp = NULL;
99 char *text = NULL, *folder = NULL, buf[BUFSIZ];
100 char **argp, **arguments, **msgs;
102 int append = 0; /* append annotations instead of default prepend */
103 int delete = -1; /* delete header element if set */
104 char *draft = (char *)0; /* draft file name */
105 int isdf = 0; /* return needed for m_draft() */
106 int list = 0; /* list header elements if set */
107 int number = 0; /* delete specific number of like elements if set */
110 setlocale(LC_ALL, "");
112 invo_name = r1bindex (argv[0], '/');
114 /* read user profile/context */
117 arguments = getarguments (invo_name, argc, argv, 1);
121 * Allocate the initial space to record message
126 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
127 adios (NULL, "unable to allocate storage");
129 while ((cp = *argp++)) {
131 switch (smatch (++cp, switches)) {
133 ambigsw (cp, switches);
136 adios (NULL, "-%s unknown", cp);
139 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
141 print_help (buf, switches, 1);
144 print_version(invo_name);
149 adios (NULL, "only one component at a time!");
150 if (!(comp = *argp++) || *comp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
170 adios (NULL, "only one body at a time!");
171 if (!(text = *argp++) || *text == '-')
172 adios (NULL, "missing argument to %s", argp[-2]);
175 case DELETESW: /* delete annotations */
179 case DRFTSW: /* draft message specified */
183 case LISTSW: /* produce a listing */
187 case NUMBERSW: /* number listing or delete by number */
189 adios (NULL, "only one number at a time!");
191 if (argp - arguments == argc - 1 || **argp == '-')
194 else if (!(number = atoi(*argp++)))
195 adios (NULL, "missing argument to %s", argp[-2]);
200 case APPENDSW: /* append annotations instead of default prepend */
205 if (*cp == '+' || *cp == '@') {
207 adios (NULL, "only one folder at a time!");
209 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
212 * Check if we need to allocate more space
213 * for message name/ranges.
215 if (nummsgs >= maxmsgs) {
217 if (!(msgs = (char **) realloc (msgs,
218 (size_t) (maxmsgs * sizeof(*msgs)))))
219 adios (NULL, "unable to reallocate msgs storage");
221 msgs[nummsgs++] = cp;
226 * We're dealing with the draft message instead of message numbers.
227 * Get the name of the draft and deal with it just as we do with
228 * message numbers below.
231 if (draft != (char *)0) {
233 adios(NULL, "can only have message numbers or -draft.");
235 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
240 annolist(draft, comp, text, number);
242 annotate (draft, comp, text, inplace, datesw, delete, append);
248 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
252 if (!context_find ("path"))
253 free (path ("./", TFOLDER));
255 msgs[nummsgs++] = "cur";
257 folder = getfolder (1);
258 maildir = m_maildir (folder);
260 if (chdir (maildir) == NOTOK)
261 adios (maildir, "unable to change directory to");
263 /* read folder and create message structure */
264 if (!(mp = folder_read (folder)))
265 adios (NULL, "unable to read folder %s", folder);
267 /* check for empty folder */
269 adios (NULL, "no messages in %s", folder);
271 /* parse all the message ranges/sequences and set SELECTED */
272 for (msgnum = 0; msgnum < nummsgs; msgnum++)
273 if (!m_convert (mp, msgs[msgnum]))
278 /* annotate all the SELECTED messages */
279 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
280 if (is_selected(mp, msgnum)) {
282 annolist(m_name(msgnum), comp, text, number);
284 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
288 context_replace (pfolder, folder); /* update current folder */
289 seq_setcur (mp, mp->lowsel); /* update current message */
290 seq_save (mp); /* synchronize message sequences */
291 folder_free (mp); /* free folder/message structure */
292 context_save (); /* save the context file */
297 make_comp (char **ap)
303 printf ("Enter component name: ");
306 if (fgets (buffer, sizeof buffer, stdin) == NULL)
308 *ap = trimcpy (buffer);
311 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
313 if (strlen (*ap) == 0)
314 adios (NULL, "null component name");
316 adios (NULL, "invalid component name %s", *ap);
317 if (strlen (*ap) >= NAMESZ)
318 adios (NULL, "too large component name %s", *ap);
320 for (cp = *ap; *cp; cp++)
321 if (!isalnum (*cp) && *cp != '-')
322 adios (NULL, "invalid component name %s", *ap);