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