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