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