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