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 * Four new options have been added: delete, list, number, and draft.
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 -draft option causes anno to operate on the current draft file
15 * instead of on a message sequence.
17 * The -delete option deletes header elements that match the -component
18 * field name. If -delete is used without the -text option, the first
19 * header field whose field name matches the component name is deleted.
20 * If the -delete is used with the -text option, and the -text argument
21 * begins with a /, the first header field whose field name matches the
22 * component name and whose field body matches the text is deleted. If
23 * the -text argument does not begin with a /, then the text is assumed
24 * to be the last component of a path name, and the first header field
25 * whose field name matches the component name and a field body whose
26 * last path name component matches the text is deleted. If the -delete
27 * option is used with the new -number option described below, the nth
28 * header field whose field name matches the component name is deleted.
29 * No header fields are deleted if none of the above conditions are met.
31 * The -list option outputs the field bodies from each header field whose
32 * field name matches the component name, one per line. If no -text
33 * option is specified, only the last path name component of each field
34 * body is output. The entire field body is output if the -text option
35 * is used; the contents of the -text argument are ignored. If the -list
36 * option is used in conjuction with the new -number option described
37 * below, each line is numbered starting with 1. A tab separates the
38 * number from the field body.
40 * The -number option works with both the -delete and -list options as
41 * described above. The -number option takes an optional argument. A
42 * value of 1 is assumed if this argument is absent.
48 static struct swit switches[] = {
50 { "component field", 0 },
77 #define NOPRESERVESW 14
85 static void make_comp (unsigned char **);
89 main (int argc, char **argv)
91 int inplace = 1, datesw = 1;
94 unsigned char *comp = NULL;
95 char *text = NULL, *folder = NULL, buf[BUFSIZ];
96 char **argp, **arguments;
97 struct msgs_array msgs = { 0, 0, NULL };
99 int append = 0; /* append annotations instead of default prepend */
100 int delete = -2; /* delete header element if set */
101 char *draft = (char *)0; /* draft file name */
102 int isdf = 0; /* return needed for m_draft() */
103 int list = 0; /* list header elements if set */
104 int number = 0; /* delete specific number of like elements if set */
107 setlocale(LC_ALL, "");
109 invo_name = r1bindex (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), "%s [+folder] [msgs] [switches]",
129 print_help (buf, switches, 1);
132 print_version(invo_name);
137 adios (NULL, "only one component at a time!");
138 if (!(comp = *argp++) || *comp == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
158 adios (NULL, "only one body at a time!");
159 if (!(text = *argp++) || *text == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
163 case DELETESW: /* delete annotations */
167 case DRFTSW: /* draft message specified */
171 case LISTSW: /* produce a listing */
175 case NUMBERSW: /* number listing or delete by number */
177 adios (NULL, "only one number at a time!");
179 if (argp - arguments == argc - 1 || **argp == '-')
183 if (strcmp(*argp, "all") == 0)
186 else if (!(number = atoi(*argp)))
187 adios (NULL, "missing argument to %s", argp[-2]);
195 case APPENDSW: /* append annotations instead of default prepend */
199 case PRESERVESW: /* preserve access and modification times on annotated message */
203 case NOPRESERVESW: /* don't preserve access and modification times on annotated message (default) */
208 if (*cp == '+' || *cp == '@') {
210 adios (NULL, "only one folder at a time!");
212 folder = pluspath (cp);
214 app_msgarg(&msgs, cp);
218 * We're dealing with the draft message instead of message numbers.
219 * Get the name of the draft and deal with it just as we do with
220 * message numbers below.
223 if (draft != (char *)0) {
225 adios(NULL, "can only have message numbers or -draft.");
227 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
232 annolist(draft, comp, text, number);
234 annotate (draft, comp, text, inplace, datesw, delete, append);
241 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
245 if (!context_find ("path"))
246 free (path ("./", TFOLDER));
248 app_msgarg(&msgs, "cur");
250 folder = getfolder (1);
251 maildir = m_maildir (folder);
253 if (chdir (maildir) == NOTOK)
254 adios (maildir, "unable to change directory to");
256 /* read folder and create message structure */
257 if (!(mp = folder_read (folder)))
258 adios (NULL, "unable to read folder %s", folder);
260 /* check for empty folder */
262 adios (NULL, "no messages in %s", folder);
264 /* parse all the message ranges/sequences and set SELECTED */
265 for (msgnum = 0; msgnum < msgs.size; msgnum++)
266 if (!m_convert (mp, msgs.msgs[msgnum]))
271 /* annotate all the SELECTED messages */
272 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
273 if (is_selected(mp, msgnum)) {
275 annolist(m_name(msgnum), comp, text, number);
277 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
281 context_replace (pfolder, folder); /* update current folder */
282 seq_setcur (mp, mp->lowsel); /* update current message */
283 seq_save (mp); /* synchronize message sequences */
284 folder_free (mp); /* free folder/message structure */
285 context_save (); /* save the context file */
291 make_comp (unsigned char **ap)
293 register unsigned char *cp;
297 printf ("Enter component name: ");
300 if (fgets (buffer, sizeof buffer, stdin) == NULL)
302 *ap = trimcpy (buffer);
305 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
307 if (strlen (*ap) == 0)
308 adios (NULL, "null component name");
310 adios (NULL, "invalid component name %s", *ap);
311 if (strlen (*ap) >= NAMESZ)
312 adios (NULL, "too large component name %s", *ap);
314 for (cp = *ap; *cp; cp++)
315 if (!isalnum (*cp) && *cp != '-')
316 adios (NULL, "invalid component name %s", *ap);