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 static struct swit switches[] = {
53 { "component field", 0 },
80 #define NOPRESERVESW 14
88 static void make_comp (unsigned char **);
92 main (int argc, char **argv)
94 int inplace = 1, datesw = 1;
97 unsigned char *comp = NULL;
98 char *text = NULL, *folder = NULL, buf[BUFSIZ];
99 char **argp, **arguments;
100 struct msgs_array msgs = { 0, 0, NULL };
102 int append = 0; /* append annotations instead of default prepend */
103 int delete = -2; /* 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);
120 while ((cp = *argp++)) {
122 switch (smatch (++cp, switches)) {
124 ambigsw (cp, switches);
127 adios (NULL, "-%s unknown", cp);
130 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
132 print_help (buf, switches, 1);
135 print_version(invo_name);
140 adios (NULL, "only one component at a time!");
141 if (!(comp = *argp++) || *comp == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
161 adios (NULL, "only one body at a time!");
162 if (!(text = *argp++) || *text == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
166 case DELETESW: /* delete annotations */
170 case DRFTSW: /* draft message specified */
174 case LISTSW: /* produce a listing */
178 case NUMBERSW: /* number listing or delete by number */
180 adios (NULL, "only one number at a time!");
182 if (argp - arguments == argc - 1 || **argp == '-')
186 if (strcmp(*argp, "all") == 0)
189 else if (!(number = atoi(*argp)))
190 adios (NULL, "missing argument to %s", argp[-2]);
198 case APPENDSW: /* append annotations instead of default prepend */
202 case PRESERVESW: /* preserve access and modification times on annotated message */
206 case NOPRESERVESW: /* don't preserve access and modification times on annotated message (default) */
211 if (*cp == '+' || *cp == '@') {
213 adios (NULL, "only one folder at a time!");
215 folder = pluspath (cp);
217 app_msgarg(&msgs, cp);
221 * We're dealing with the draft message instead of message numbers.
222 * Get the name of the draft and deal with it just as we do with
223 * message numbers below.
226 if (draft != (char *)0) {
228 adios(NULL, "can only have message numbers or -draft.");
230 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
235 annolist(draft, comp, text, number);
237 annotate (draft, comp, text, inplace, datesw, delete, append);
244 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER));
251 app_msgarg(&msgs, "cur");
253 folder = getfolder (1);
254 maildir = m_maildir (folder);
256 if (chdir (maildir) == NOTOK)
257 adios (maildir, "unable to change directory to");
259 /* read folder and create message structure */
260 if (!(mp = folder_read (folder)))
261 adios (NULL, "unable to read folder %s", folder);
263 /* check for empty folder */
265 adios (NULL, "no messages in %s", folder);
267 /* parse all the message ranges/sequences and set SELECTED */
268 for (msgnum = 0; msgnum < msgs.size; msgnum++)
269 if (!m_convert (mp, msgs.msgs[msgnum]))
274 /* annotate all the SELECTED messages */
275 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
276 if (is_selected(mp, msgnum)) {
278 annolist(m_name(msgnum), comp, text, number);
280 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
284 context_replace (pfolder, folder); /* update current folder */
285 seq_setcur (mp, mp->lowsel); /* update current message */
286 seq_save (mp); /* synchronize message sequences */
287 folder_free (mp); /* free folder/message structure */
288 context_save (); /* save the context file */
294 make_comp (unsigned char **ap)
296 register unsigned char *cp;
300 printf ("Enter component name: ");
303 if (fgets (buffer, sizeof buffer, stdin) == NULL)
305 *ap = trimcpy (buffer);
308 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
310 if (strlen (*ap) == 0)
311 adios (NULL, "null component name");
313 adios (NULL, "invalid component name %s", *ap);
314 if (strlen (*ap) >= NAMESZ)
315 adios (NULL, "too large component name %s", *ap);
317 for (cp = *ap; *cp; cp++)
318 if (!isalnum (*cp) && *cp != '-')
319 adios (NULL, "invalid component name %s", *ap);