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