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