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