db1b96cff8f312795b0a0d7c7635c42e452b7ff8
[mmh] / uip / anno.c
1 /*
2  * anno.c -- annotate messages
3  *
4  * This code is Copyright (c) 2002, by the authors of nmh.  See the
5  * COPYRIGHT file in the root directory of the nmh distribution for
6  * complete copyright information.
7  *
8  * Four new options have been added: delete, list, number, and draft.
9  * Message header fields are used by the new MIME attachment code in
10  * the send command.  Adding features to generalize the anno command
11  * seemed to be a better approach than the creation of a new command
12  * whose features would overlap with those of the anno command.
13  *
14  * The -draft option causes anno to operate on the current draft file
15  * instead of on a message sequence.
16  *
17  * The -delete option deletes header elements that match the -component
18  * field name.  If -delete is used without the -text option, the first
19  * header field whose field name matches the component name is deleted.
20  * If the -delete is used with the -text option, and the -text argument
21  * begins with a /, the first header field whose field name matches the
22  * component name and whose field body matches the text is deleted.  If
23  * the -text argument does not begin with a /, then the text is assumed
24  * to be the last component of a path name, and the first header field
25  * whose field name matches the component name and a field body whose
26  * last path name component matches the text is deleted.  If the -delete
27  * option is used with the new -number option described below, the nth
28  * header field whose field name matches the component name is deleted.
29  * No header fields are deleted if none of the above conditions are met.
30  *
31  * The -list option outputs the field bodies from each header field whose
32  * field name matches the component name, one per line.  If no -text
33  * option is specified, only the last path name component of each field
34  * body is output.  The entire field body is output if the -text option
35  * is used; the contents of the -text argument are ignored.  If the -list
36  * option is used in conjuction with the new -number option described
37  * below, each line is numbered starting with 1.  A tab separates the
38  * number from the field body.
39  *
40  * The -number option works with both the -delete and -list options as
41  * described above.  The -number option takes an optional argument.  A
42  * value of 1 is assumed if this argument is absent.
43  */
44
45 #include <h/mh.h>
46 #include <h/utils.h>
47
48 static struct swit switches[] = {
49 #define COMPSW 0
50         { "component field", 0 },
51 #define INPLSW 1
52         { "inplace", 0 },
53 #define NINPLSW 2
54         { "noinplace", 0 },
55 #define DATESW 3
56         { "date", 0 },
57 #define NDATESW 4
58         { "nodate", 0 },
59 #define TEXTSW 5
60         { "text body", 0 },
61 #define VERSIONSW 6
62         { "version", 0 },
63 #define HELPSW 7
64         { "help", 0 },
65 #define DRFTSW 8
66         { "draft", 2 },
67 #define LISTSW 9
68         { "list", 1 },
69 #define DELETESW 10
70         { "delete", 2 },
71 #define NUMBERSW 11
72         { "number", 2 },
73 #define APPENDSW 12
74         { "append", 1 },
75 #define PRESERVESW 13
76         { "preserve", 1 },
77 #define NOPRESERVESW 14
78         { "nopreserve", 3 },
79         { NULL, 0 }
80 };
81
82 /*
83  * static prototypes
84  */
85 static void make_comp (unsigned char **);
86
87
88 int
89 main (int argc, char **argv)
90 {
91         int inplace = 1, datesw = 1;
92         int msgnum;
93         char *cp, *maildir;
94         unsigned char *comp = NULL;
95         char *text = NULL, *folder = NULL, buf[BUFSIZ];
96         char **argp, **arguments;
97         struct msgs_array msgs = { 0, 0, NULL };
98         struct msgs *mp;
99         int append = 0;  /* append annotations instead of default prepend */
100         int delete = -2;  /* delete header element if set */
101         char *draft = (char *)0;  /* draft file name */
102         int isdf = 0; /* return needed for m_draft() */
103         int list = 0;  /* list header elements if set */
104         int number = 0; /* delete specific number of like elements if set */
105
106 #ifdef LOCALE
107         setlocale(LC_ALL, "");
108 #endif
109         invo_name = r1bindex (argv[0], '/');
110
111         /* read user profile/context */
112         context_read();
113
114         arguments = getarguments (invo_name, argc, argv, 1);
115         argp = arguments;
116
117         while ((cp = *argp++)) {
118                 if (*cp == '-') {
119                         switch (smatch (++cp, switches)) {
120                                 case AMBIGSW:
121                                         ambigsw (cp, switches);
122                                         done (1);
123                                 case UNKWNSW:
124                                         adios (NULL, "-%s unknown", cp);
125
126                                 case HELPSW:
127                                         snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
128                                                 invo_name);
129                                         print_help (buf, switches, 1);
130                                         done (1);
131                                 case VERSIONSW:
132                                         print_version(invo_name);
133                                         done (1);
134
135                                 case COMPSW:
136                                         if (comp)
137                                                 adios (NULL, "only one component at a time!");
138                                         if (!(comp = *argp++) || *comp == '-')
139                                                 adios (NULL, "missing argument to %s", argp[-2]);
140                                         continue;
141
142                                 case DATESW:
143                                         datesw++;
144                                         continue;
145                                 case NDATESW:
146                                         datesw = 0;
147                                         continue;
148
149                                 case INPLSW:
150                                         inplace++;
151                                         continue;
152                                 case NINPLSW:
153                                         inplace = 0;
154                                         continue;
155
156                                 case TEXTSW:
157                                         if (text)
158                                                 adios (NULL, "only one body at a time!");
159                                         if (!(text = *argp++) || *text == '-')
160                                                 adios (NULL, "missing argument to %s", argp[-2]);
161                                         continue;
162
163                                 case DELETESW:  /* delete annotations */
164                                         delete = 0;
165                                         continue;
166
167                                 case DRFTSW:  /* draft message specified */
168                                         draft = "";
169                                         continue;
170
171                                 case LISTSW:  /* produce a listing */
172                                         list = 1;
173                                         continue;
174
175                                 case NUMBERSW:  /* number listing or delete by number */
176                                         if (number != 0)
177                                                 adios (NULL, "only one number at a time!");
178
179                                         if (argp - arguments == argc - 1 || **argp == '-')
180                                                 number = 1;
181
182                                         else {
183                                                         if (strcmp(*argp, "all") == 0)
184                                                                 number = -1;
185
186                                                         else if (!(number = atoi(*argp)))
187                                                         adios (NULL, "missing argument to %s", argp[-2]);
188
189                                                 argp++;
190                                         }
191
192                                         delete = number;
193                                         continue;
194
195                                 case APPENDSW:  /* append annotations instead of default prepend */
196                                         append = 1;
197                                         continue;
198
199                                 case PRESERVESW:  /* preserve access and modification times on annotated message */
200                                         annopreserve(1);
201                                         continue;
202
203                                 case NOPRESERVESW:  /* don't preserve access and modification times on annotated message (default) */
204                                         annopreserve(0);
205                                         continue;
206                         }
207                 }
208                 if (*cp == '+' || *cp == '@') {
209                         if (folder)
210                                 adios (NULL, "only one folder at a time!");
211                         else
212                                 folder = pluspath (cp);
213                 } else
214                                 app_msgarg(&msgs, cp);
215         }
216
217         /*
218          * We're dealing with the draft message instead of message numbers.
219          * Get the name of the draft and deal with it just as we do with
220          * message numbers below.
221          */
222
223         if (draft != (char *)0) {
224                 if (msgs.size != 0)
225                         adios(NULL, "can only have message numbers or -draft.");
226
227                 draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
228
229                 make_comp(&comp);
230
231                 if (list)
232                         annolist(draft, comp, text, number);
233                 else
234                         annotate (draft, comp, text, inplace, datesw, delete, append);
235
236                 done(0);
237                 return 1;
238         }
239
240 #ifdef UCI
241         if (strcmp(invo_name, "fanno") == 0)  /* ugh! */
242                 datesw = 0;
243 #endif /* UCI */
244
245         if (!context_find ("path"))
246                 free (path ("./", TFOLDER));
247         if (!msgs.size)
248                 app_msgarg(&msgs, "cur");
249         if (!folder)
250                 folder = getfolder (1);
251         maildir = m_maildir (folder);
252
253         if (chdir (maildir) == NOTOK)
254                 adios (maildir, "unable to change directory to");
255
256         /* read folder and create message structure */
257         if (!(mp = folder_read (folder)))
258                 adios (NULL, "unable to read folder %s", folder);
259
260         /* check for empty folder */
261         if (mp->nummsg == 0)
262                 adios (NULL, "no messages in %s", folder);
263
264         /* parse all the message ranges/sequences and set SELECTED */
265         for (msgnum = 0; msgnum < msgs.size; msgnum++)
266                 if (!m_convert (mp, msgs.msgs[msgnum]))
267                         done (1);
268
269         make_comp (&comp);
270
271         /* annotate all the SELECTED messages */
272         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
273                 if (is_selected(mp, msgnum)) {
274                         if (list)
275                                 annolist(m_name(msgnum), comp, text, number);
276                         else
277                                 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
278                 }
279         }
280
281         context_replace (pfolder, folder);  /* update current folder  */
282         seq_setcur (mp, mp->lowsel);  /* update current message */
283         seq_save (mp);  /* synchronize message sequences */
284         folder_free (mp);  /* free folder/message structure */
285         context_save ();  /* save the context file */
286         done (0);
287         return 1;
288 }
289
290 static void
291 make_comp (unsigned char **ap)
292 {
293         register unsigned char *cp;
294         char buffer[BUFSIZ];
295
296         if (*ap == NULL) {
297                 printf ("Enter component name: ");
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 (strlen (*ap) == 0)
308                 adios (NULL, "null component name");
309         if (**ap == '-')
310                 adios (NULL, "invalid component name %s", *ap);
311         if (strlen (*ap) >= NAMESZ)
312                 adios (NULL, "too large component name %s", *ap);
313
314         for (cp = *ap; *cp; cp++)
315                 if (!isalnum (*cp) && *cp != '-')
316                         adios (NULL, "invalid component name %s", *ap);
317 }