Fixed a bug where anno -append put the headers in the wrong place
[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
50 /*
51  * We allocate space for messages (msgs array)
52  * this number of elements at a time.
53  */
54 #define MAXMSGS  256
55
56
57 static struct swit switches[] = {
58 #define COMPSW  0
59     { "component field", 0 },
60 #define INPLSW  1
61     { "inplace", 0 },
62 #define NINPLSW 2
63     { "noinplace", 0 },
64 #define DATESW  3
65     { "date", 0 },
66 #define NDATESW 4
67     { "nodate", 0 },
68 #define TEXTSW  5
69     { "text body", 0 },
70 #define VERSIONSW 6
71     { "version", 0 },
72 #define HELPSW  7
73     { "help", 0 },
74 #define DRFTSW                   8
75     { "draft", 2 },
76 #define LISTSW                   9
77     { "list", 1 },
78 #define DELETESW                10
79     { "delete", 2 },
80 #define NUMBERSW                11
81     { "number", 2 },
82 #define APPENDSW                12
83     { "append", 1 },
84 #define PRESERVESW              13
85     { "preserve", 1 },
86 #define NOPRESERVESW            14
87     { "nopreserve", 3 },
88     { NULL, 0 }
89 };
90
91 /*
92  * static prototypes
93  */
94 static void make_comp (char **);
95
96
97 int
98 main (int argc, char **argv)
99 {
100     int inplace = 1, datesw = 1;
101     int nummsgs, maxmsgs, msgnum;
102     char *cp, *maildir, *comp = NULL;
103     char *text = NULL, *folder = NULL, buf[BUFSIZ];
104     char **argp, **arguments, **msgs;
105     struct msgs *mp;
106     int         append = 0;             /* append annotations instead of default prepend */
107     int         delete = -2;            /* delete header element if set */
108     char        *draft = (char *)0;     /* draft file name */
109     int         isdf = 0;               /* return needed for m_draft() */
110     int         list = 0;               /* list header elements if set */
111     int         number = 0;             /* delete specific number of like elements if set */
112
113 #ifdef LOCALE
114     setlocale(LC_ALL, "");
115 #endif
116     invo_name = r1bindex (argv[0], '/');
117
118     /* read user profile/context */
119     context_read();
120
121     arguments = getarguments (invo_name, argc, argv, 1);
122     argp = arguments;
123
124     /*
125      * Allocate the initial space to record message
126      * names and ranges.
127      */
128     nummsgs = 0;
129     maxmsgs = MAXMSGS;
130     if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
131         adios (NULL, "unable to allocate storage");
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                 if (!(msgs = (char **) realloc (msgs,
237                         (size_t) (maxmsgs * sizeof(*msgs)))))
238                     adios (NULL, "unable to reallocate msgs storage");
239             }
240             msgs[nummsgs++] = cp;
241         }
242     }
243
244     /*
245      *  We're dealing with the draft message instead of message numbers.
246      *  Get the name of the draft and deal with it just as we do with
247      *  message numbers below.
248      */
249
250     if (draft != (char *)0) {
251         if (nummsgs != 0)
252             adios(NULL, "can only have message numbers or -draft.");
253
254         draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
255
256         make_comp(&comp);
257
258         if (list)
259             annolist(draft, comp, text, number);
260         else
261             annotate (draft, comp, text, inplace, datesw, delete, append);
262
263         return (done(0));
264     }
265
266 #ifdef UCI
267     if (strcmp(invo_name, "fanno") == 0)        /* ugh! */
268         datesw = 0;
269 #endif  /* UCI */
270
271     if (!context_find ("path"))
272         free (path ("./", TFOLDER));
273     if (!nummsgs)
274         msgs[nummsgs++] = "cur";
275     if (!folder)
276         folder = getfolder (1);
277     maildir = m_maildir (folder);
278
279     if (chdir (maildir) == NOTOK)
280         adios (maildir, "unable to change directory to");
281
282     /* read folder and create message structure */
283     if (!(mp = folder_read (folder)))
284         adios (NULL, "unable to read folder %s", folder);
285
286     /* check for empty folder */
287     if (mp->nummsg == 0)
288         adios (NULL, "no messages in %s", folder);
289
290     /* parse all the message ranges/sequences and set SELECTED */
291     for (msgnum = 0; msgnum < nummsgs; msgnum++)
292         if (!m_convert (mp, msgs[msgnum]))
293             done (1);
294
295     make_comp (&comp);
296
297     /* annotate all the SELECTED messages */
298     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
299         if (is_selected(mp, msgnum)) {
300             if (list)
301                 annolist(m_name(msgnum), comp, text, number);
302             else
303                 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
304         }
305     }
306
307     context_replace (pfolder, folder);  /* update current folder  */
308     seq_setcur (mp, mp->lowsel);        /* update current message */
309     seq_save (mp);      /* synchronize message sequences */
310     folder_free (mp);   /* free folder/message structure */
311     context_save ();    /* save the context file         */
312     return done (0);
313 }
314
315 static void
316 make_comp (char **ap)
317 {
318     register char *cp;
319     char buffer[BUFSIZ];
320
321     if (*ap == NULL) {
322         printf ("Enter component name: ");
323         fflush (stdout);
324
325         if (fgets (buffer, sizeof buffer, stdin) == NULL)
326             done (1);
327         *ap = trimcpy (buffer);
328     }
329
330     if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
331         *cp = 0;
332     if (strlen (*ap) == 0)
333         adios (NULL, "null component name");
334     if (**ap == '-')
335         adios (NULL, "invalid component name %s", *ap);
336     if (strlen (*ap) >= NAMESZ)
337         adios (NULL, "too large component name %s", *ap);
338
339     for (cp = *ap; *cp; cp++)
340         if (!isalnum (*cp) && *cp != '-')
341             adios (NULL, "invalid component name %s", *ap);
342 }