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