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