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