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