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