]> diplodocus.org Git - nmh/blob - uip/anno.c
Convert the MIME content cache switches over to the smatch() New World Order.
[nmh] / uip / anno.c
1
2 /*
3 * anno.c -- annotate messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 *
9 * Four new options have been added: delete, list, number, and draft.
10 * Message header fields are used by the new MIME attachment code in
11 * the send command. Adding features to generalize the anno command
12 * seemed to be a better approach than the creation of a new command
13 * whose features would overlap with those of the anno command.
14 *
15 * The -draft option causes anno to operate on the current draft file
16 * instead of on a message sequence.
17 *
18 * The -delete option deletes header elements that match the -component
19 * field name. If -delete is used without the -text option, the first
20 * header field whose field name matches the component name is deleted.
21 * If the -delete is used with the -text option, and the -text argument
22 * begins with a /, the first header field whose field name matches the
23 * component name and whose field body matches the text is deleted. If
24 * the -text argument does not begin with a /, then the text is assumed
25 * to be the last component of a path name, and the first header field
26 * whose field name matches the component name and a field body whose
27 * last path name component matches the text is deleted. If the -delete
28 * option is used with the new -number option described below, the nth
29 * header field whose field name matches the component name is deleted.
30 * No header fields are deleted if none of the above conditions are met.
31 *
32 * The -list option outputs the field bodies from each header field whose
33 * field name matches the component name, one per line. If no -text
34 * option is specified, only the last path name component of each field
35 * body is output. The entire field body is output if the -text option
36 * is used; the contents of the -text argument are ignored. If the -list
37 * option is used in conjuction with the new -number option described
38 * below, each line is numbered starting with 1. A tab separates the
39 * number from the field body.
40 *
41 * The -number option works with both the -delete and -list options as
42 * described above. The -number option takes an optional argument. A
43 * value of 1 is assumed if this argument is absent.
44 */
45
46 #include <h/mh.h>
47 #include <h/utils.h>
48
49 #define ANNO_SWITCHES \
50 X("component field", 0, COMPSW) \
51 X("inplace", 0, INPLSW) \
52 X("noinplace", 0, NINPLSW) \
53 X("date", 0, DATESW) \
54 X("nodate", 0, NDATESW) \
55 X("text body", 0, TEXTSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("draft", 2, DRFTSW) \
59 X("list", 1, LISTSW) \
60 X("delete", 2, DELETESW) \
61 X("number", 2, NUMBERSW) \
62 X("append", 1, APPENDSW) \
63 X("preserve", 1, PRESERVESW) \
64 X("nopreserve", 3, NOPRESERVESW) \
65
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(ANNO);
68 #undef X
69
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(ANNO, switches);
72 #undef X
73
74 /*
75 * static prototypes
76 */
77 static void make_comp (unsigned char **);
78
79
80 int
81 main (int argc, char **argv)
82 {
83 int inplace = 1, datesw = 1;
84 int msgnum;
85 char *cp, *maildir;
86 unsigned char *comp = NULL;
87 char *text = NULL, *folder = NULL, buf[BUFSIZ];
88 char **argp, **arguments;
89 struct msgs_array msgs = { 0, 0, NULL };
90 struct msgs *mp;
91 int append = 0; /* append annotations instead of default prepend */
92 int delete = -2; /* delete header element if set */
93 char *draft = (char *)0; /* draft file name */
94 int isdf = 0; /* return needed for m_draft() */
95 int list = 0; /* list header elements if set */
96 int number = 0; /* delete specific number of like elements if set */
97
98 #ifdef LOCALE
99 setlocale(LC_ALL, "");
100 #endif
101 invo_name = r1bindex (argv[0], '/');
102
103 /* read user profile/context */
104 context_read();
105
106 arguments = getarguments (invo_name, argc, argv, 1);
107 argp = arguments;
108
109 while ((cp = *argp++)) {
110 if (*cp == '-') {
111 switch (smatch (++cp, switches)) {
112 case AMBIGSW:
113 ambigsw (cp, switches);
114 done (1);
115 case UNKWNSW:
116 adios (NULL, "-%s unknown", cp);
117
118 case HELPSW:
119 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
120 invo_name);
121 print_help (buf, switches, 1);
122 done (0);
123 case VERSIONSW:
124 print_version(invo_name);
125 done (0);
126
127 case COMPSW:
128 if (comp)
129 adios (NULL, "only one component at a time!");
130 if (!(comp = *argp++) || *comp == '-')
131 adios (NULL, "missing argument to %s", argp[-2]);
132 continue;
133
134 case DATESW:
135 datesw++;
136 continue;
137 case NDATESW:
138 datesw = 0;
139 continue;
140
141 case INPLSW:
142 inplace++;
143 continue;
144 case NINPLSW:
145 inplace = 0;
146 continue;
147
148 case TEXTSW:
149 if (text)
150 adios (NULL, "only one body at a time!");
151 if (!(text = *argp++) || *text == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 continue;
154
155 case DELETESW: /* delete annotations */
156 delete = 0;
157 continue;
158
159 case DRFTSW: /* draft message specified */
160 draft = "";
161 continue;
162
163 case LISTSW: /* produce a listing */
164 list = 1;
165 continue;
166
167 case NUMBERSW: /* number listing or delete by number */
168 if (number != 0)
169 adios (NULL, "only one number at a time!");
170
171 if (argp - arguments == argc - 1 || **argp == '-')
172 number = 1;
173
174 else {
175 if (strcmp(*argp, "all") == 0)
176 number = -1;
177
178 else if (!(number = atoi(*argp)))
179 adios (NULL, "missing argument to %s", argp[-2]);
180
181 argp++;
182 }
183
184 delete = number;
185 continue;
186
187 case APPENDSW: /* append annotations instead of default prepend */
188 append = 1;
189 continue;
190
191 case PRESERVESW: /* preserve access and modification times on annotated message */
192 annopreserve(1);
193 continue;
194
195 case NOPRESERVESW: /* don't preserve access and modification times on annotated message (default) */
196 annopreserve(0);
197 continue;
198 }
199 }
200 if (*cp == '+' || *cp == '@') {
201 if (folder)
202 adios (NULL, "only one folder at a time!");
203 else
204 folder = pluspath (cp);
205 } else
206 app_msgarg(&msgs, cp);
207 }
208
209 /*
210 * We're dealing with the draft message instead of message numbers.
211 * Get the name of the draft and deal with it just as we do with
212 * message numbers below.
213 */
214
215 if (draft != (char *)0) {
216 if (msgs.size != 0)
217 adios(NULL, "can only have message numbers or -draft.");
218
219 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
220
221 make_comp(&comp);
222
223 if (list)
224 annolist(draft, comp, text, number);
225 else
226 annotate (draft, comp, text, inplace, datesw, delete, append);
227
228 done(0);
229 return 1;
230 }
231
232 if (!context_find ("path"))
233 free (path ("./", TFOLDER));
234 if (!msgs.size)
235 app_msgarg(&msgs, "cur");
236 if (!folder)
237 folder = getfolder (1);
238 maildir = m_maildir (folder);
239
240 if (chdir (maildir) == NOTOK)
241 adios (maildir, "unable to change directory to");
242
243 /* read folder and create message structure */
244 if (!(mp = folder_read (folder)))
245 adios (NULL, "unable to read folder %s", folder);
246
247 /* check for empty folder */
248 if (mp->nummsg == 0)
249 adios (NULL, "no messages in %s", folder);
250
251 /* parse all the message ranges/sequences and set SELECTED */
252 for (msgnum = 0; msgnum < msgs.size; msgnum++)
253 if (!m_convert (mp, msgs.msgs[msgnum]))
254 done (1);
255
256 make_comp (&comp);
257
258 /* annotate all the SELECTED messages */
259 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
260 if (is_selected(mp, msgnum)) {
261 if (list)
262 annolist(m_name(msgnum), comp, text, number);
263 else
264 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
265 }
266 }
267
268 context_replace (pfolder, folder); /* update current folder */
269 seq_setcur (mp, mp->lowsel); /* update current message */
270 seq_save (mp); /* synchronize message sequences */
271 folder_free (mp); /* free folder/message structure */
272 context_save (); /* save the context file */
273 done (0);
274 return 1;
275 }
276
277 static void
278 make_comp (unsigned char **ap)
279 {
280 register unsigned char *cp;
281 char buffer[BUFSIZ];
282
283 if (*ap == NULL) {
284 printf ("Enter component name: ");
285 fflush (stdout);
286
287 if (fgets (buffer, sizeof buffer, stdin) == NULL)
288 done (1);
289 *ap = trimcpy (buffer);
290 }
291
292 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
293 *cp = 0;
294 if (strlen (*ap) == 0)
295 adios (NULL, "null component name");
296 if (**ap == '-')
297 adios (NULL, "invalid component name %s", *ap);
298 if (strlen (*ap) >= NAMESZ)
299 adios (NULL, "too large component name %s", *ap);
300
301 for (cp = *ap; *cp; cp++)
302 if (!isalnum (*cp) && *cp != '-')
303 adios (NULL, "invalid component name %s", *ap);
304 }