]> diplodocus.org Git - nmh/blob - uip/anno.c
We're not using the .Bu macro anymore.
[nmh] / uip / anno.c
1
2 /*
3 * anno.c -- annotate messages
4 *
5 * $Id$
6 *
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.
10 *
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.
16 *
17 * The -draft option causes anno to operate on the current draft file
18 * instead of on a message sequence.
19 *
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.
33 *
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.
42 *
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.
46 */
47
48 #include <h/mh.h>
49 #include <h/utils.h>
50
51 static struct swit switches[] = {
52 #define COMPSW 0
53 { "component field", 0 },
54 #define INPLSW 1
55 { "inplace", 0 },
56 #define NINPLSW 2
57 { "noinplace", 0 },
58 #define DATESW 3
59 { "date", 0 },
60 #define NDATESW 4
61 { "nodate", 0 },
62 #define TEXTSW 5
63 { "text body", 0 },
64 #define VERSIONSW 6
65 { "version", 0 },
66 #define HELPSW 7
67 { "help", 0 },
68 #define DRFTSW 8
69 { "draft", 2 },
70 #define LISTSW 9
71 { "list", 1 },
72 #define DELETESW 10
73 { "delete", 2 },
74 #define NUMBERSW 11
75 { "number", 2 },
76 #define APPENDSW 12
77 { "append", 1 },
78 #define PRESERVESW 13
79 { "preserve", 1 },
80 #define NOPRESERVESW 14
81 { "nopreserve", 3 },
82 { NULL, 0 }
83 };
84
85 /*
86 * static prototypes
87 */
88 static void make_comp (char **);
89
90
91 int
92 main (int argc, char **argv)
93 {
94 int inplace = 1, datesw = 1;
95 int msgnum;
96 char *cp, *maildir, *comp = NULL;
97 char *text = NULL, *folder = NULL, buf[BUFSIZ];
98 char **argp, **arguments;
99 struct msgs_array msgs = { 0, 0, NULL };
100 struct msgs *mp;
101 int append = 0; /* append annotations instead of default prepend */
102 int delete = -2; /* delete header element if set */
103 char *draft = (char *)0; /* draft file name */
104 int isdf = 0; /* return needed for m_draft() */
105 int list = 0; /* list header elements if set */
106 int number = 0; /* delete specific number of like elements if set */
107
108 #ifdef LOCALE
109 setlocale(LC_ALL, "");
110 #endif
111 invo_name = r1bindex (argv[0], '/');
112
113 /* read user profile/context */
114 context_read();
115
116 arguments = getarguments (invo_name, argc, argv, 1);
117 argp = arguments;
118
119 while ((cp = *argp++)) {
120 if (*cp == '-') {
121 switch (smatch (++cp, switches)) {
122 case AMBIGSW:
123 ambigsw (cp, switches);
124 done (1);
125 case UNKWNSW:
126 adios (NULL, "-%s unknown", cp);
127
128 case HELPSW:
129 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
130 invo_name);
131 print_help (buf, switches, 1);
132 done (1);
133 case VERSIONSW:
134 print_version(invo_name);
135 done (1);
136
137 case COMPSW:
138 if (comp)
139 adios (NULL, "only one component at a time!");
140 if (!(comp = *argp++) || *comp == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 continue;
143
144 case DATESW:
145 datesw++;
146 continue;
147 case NDATESW:
148 datesw = 0;
149 continue;
150
151 case INPLSW:
152 inplace++;
153 continue;
154 case NINPLSW:
155 inplace = 0;
156 continue;
157
158 case TEXTSW:
159 if (text)
160 adios (NULL, "only one body at a time!");
161 if (!(text = *argp++) || *text == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 continue;
164
165 case DELETESW: /* delete annotations */
166 delete = 0;
167 continue;
168
169 case DRFTSW: /* draft message specified */
170 draft = "";
171 continue;
172
173 case LISTSW: /* produce a listing */
174 list = 1;
175 continue;
176
177 case NUMBERSW: /* number listing or delete by number */
178 if (number != 0)
179 adios (NULL, "only one number at a time!");
180
181 if (argp - arguments == argc - 1 || **argp == '-')
182 number = 1;
183
184 else {
185 if (strcmp(*argp, "all") == 0)
186 number = -1;
187
188 else if (!(number = atoi(*argp)))
189 adios (NULL, "missing argument to %s", argp[-2]);
190
191 argp++;
192 }
193
194 delete = number;
195 continue;
196
197 case APPENDSW: /* append annotations instead of default prepend */
198 append = 1;
199 continue;
200
201 case PRESERVESW: /* preserve access and modification times on annotated message */
202 annopreserve(1);
203 continue;
204
205 case NOPRESERVESW: /* don't preserve access and modification times on annotated message (default) */
206 annopreserve(0);
207 continue;
208 }
209 }
210 if (*cp == '+' || *cp == '@') {
211 if (folder)
212 adios (NULL, "only one folder at a time!");
213 else
214 folder = pluspath (cp);
215 } else
216 app_msgarg(&msgs, cp);
217 }
218
219 /*
220 * We're dealing with the draft message instead of message numbers.
221 * Get the name of the draft and deal with it just as we do with
222 * message numbers below.
223 */
224
225 if (draft != (char *)0) {
226 if (msgs.size != 0)
227 adios(NULL, "can only have message numbers or -draft.");
228
229 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
230
231 make_comp(&comp);
232
233 if (list)
234 annolist(draft, comp, text, number);
235 else
236 annotate (draft, comp, text, inplace, datesw, delete, append);
237
238 return (done(0));
239 }
240
241 #ifdef UCI
242 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
243 datesw = 0;
244 #endif /* UCI */
245
246 if (!context_find ("path"))
247 free (path ("./", TFOLDER));
248 if (!msgs.size)
249 app_msgarg(&msgs, "cur");
250 if (!folder)
251 folder = getfolder (1);
252 maildir = m_maildir (folder);
253
254 if (chdir (maildir) == NOTOK)
255 adios (maildir, "unable to change directory to");
256
257 /* read folder and create message structure */
258 if (!(mp = folder_read (folder)))
259 adios (NULL, "unable to read folder %s", folder);
260
261 /* check for empty folder */
262 if (mp->nummsg == 0)
263 adios (NULL, "no messages in %s", folder);
264
265 /* parse all the message ranges/sequences and set SELECTED */
266 for (msgnum = 0; msgnum < msgs.size; msgnum++)
267 if (!m_convert (mp, msgs.msgs[msgnum]))
268 done (1);
269
270 make_comp (&comp);
271
272 /* annotate all the SELECTED messages */
273 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
274 if (is_selected(mp, msgnum)) {
275 if (list)
276 annolist(m_name(msgnum), comp, text, number);
277 else
278 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
279 }
280 }
281
282 context_replace (pfolder, folder); /* update current folder */
283 seq_setcur (mp, mp->lowsel); /* update current message */
284 seq_save (mp); /* synchronize message sequences */
285 folder_free (mp); /* free folder/message structure */
286 context_save (); /* save the context file */
287 return done (0);
288 }
289
290 static void
291 make_comp (char **ap)
292 {
293 register char *cp;
294 char buffer[BUFSIZ];
295
296 if (*ap == NULL) {
297 printf ("Enter component name: ");
298 fflush (stdout);
299
300 if (fgets (buffer, sizeof buffer, stdin) == NULL)
301 done (1);
302 *ap = trimcpy (buffer);
303 }
304
305 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
306 *cp = 0;
307 if (strlen (*ap) == 0)
308 adios (NULL, "null component name");
309 if (**ap == '-')
310 adios (NULL, "invalid component name %s", *ap);
311 if (strlen (*ap) >= NAMESZ)
312 adios (NULL, "too large component name %s", *ap);
313
314 for (cp = *ap; *cp; cp++)
315 if (!isalnum (*cp) && *cp != '-')
316 adios (NULL, "invalid component name %s", *ap);
317 }