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