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