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