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