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