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