aacd82676fb533518ce56d9fa9781e852f80f3d0
[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 msgnum;
84         char *cp, *maildir;
85         unsigned char *comp = NULL;
86         char *text = NULL, *folder = NULL, buf[BUFSIZ];
87         char **argp, **arguments;
88         struct msgs_array msgs = { 0, 0, NULL };
89         struct msgs *mp;
90         int append = 0;  /* append annotations instead of default prepend */
91         int delete = -2;  /* delete header element if set */
92         int list = 0;  /* list header elements if set */
93         int number = 0; /* delete specific number of like elements if set */
94
95 #ifdef LOCALE
96         setlocale(LC_ALL, "");
97 #endif
98         invo_name = mhbasename(argv[0]);
99
100         /* read user profile/context */
101         context_read();
102
103         arguments = getarguments(invo_name, argc, argv, 1);
104         argp = arguments;
105
106         while ((cp = *argp++)) {
107                 if (*cp == '-') {
108                         switch (smatch(++cp, switches)) {
109                         case AMBIGSW:
110                                 ambigsw(cp, switches);
111                                 done(1);
112                         case UNKWNSW:
113                                 adios(NULL, "-%s unknown", cp);
114
115                         case HELPSW:
116                                 snprintf(buf, sizeof(buf),
117                                         "%s [+folder] [msgs] [switches]",
118                                         invo_name);
119                                 print_help(buf, switches, 1);
120                                 done(1);
121                         case VERSIONSW:
122                                 print_version(invo_name);
123                                 done(1);
124
125                         case COMPSW:
126                                 if (comp)
127                                         adios(NULL, "only one component at a time!");
128                                 if (!(comp = *argp++) || *comp == '-')
129                                         adios(NULL, "missing argument to %s",
130                                                         argp[-2]);
131                                 continue;
132
133                         case DATESW:
134                                 datesw++;
135                                 continue;
136                         case NDATESW:
137                                 datesw = 0;
138                                 continue;
139
140                         case TEXTSW:
141                                 if (text)
142                                         adios(NULL, "only one body at a time!");
143                                 if (!(text = *argp++) || *text == '-')
144                                         adios(NULL, "missing argument to %s",
145                                                         argp[-2]);
146                                 continue;
147
148                         case DELETESW:  /* delete annotations */
149                                 delete = 0;
150                                 continue;
151
152                         case LISTSW:  /* produce a listing */
153                                 list = 1;
154                                 continue;
155
156                         case NUMBERSW: /* number listing or delete by number */
157                                 if (number != 0)
158                                         adios(NULL, "only one number at a time!");
159
160                                 if (argp-arguments == argc-1 || **argp == '-')
161                                         number = 1;
162
163                                 else {
164                                                 if (strcmp(*argp, "all") == 0)
165                                                         number = -1;
166
167                                                 else if (!(number = atoi(*argp)))
168                                                 adios(NULL, "missing argument to %s", argp[-2]);
169
170                                         argp++;
171                                 }
172
173                                 delete = number;
174                                 continue;
175
176                         case APPENDSW:  /* append annotations instead of default prepend */
177                                 append = 1;
178                                 continue;
179
180                         case PRESERVESW:  /* preserve access and modification times on annotated message */
181                                 annopreserve(1);
182                                 continue;
183
184                         case NOPRESERVESW:  /* don't preserve access and modification times on annotated message (default) */
185                                 annopreserve(0);
186                                 continue;
187                         }
188                 }
189                 if (*cp == '+' || *cp == '@') {
190                         if (folder)
191                                 adios(NULL, "only one folder at a time!");
192                         else
193                                 folder = getcpy(expandfol(cp));
194                 } else
195                         app_msgarg(&msgs, cp);
196         }
197
198         if (!msgs.size)
199                 app_msgarg(&msgs, seq_cur);
200         if (!folder)
201                 folder = getcurfol();
202         maildir = toabsdir(folder);
203
204         if (chdir(maildir) == NOTOK)
205                 adios(maildir, "unable to change directory to");
206
207         /* read folder and create message structure */
208         if (!(mp = folder_read(folder)))
209                 adios(NULL, "unable to read folder %s", folder);
210
211         /* check for empty folder */
212         if (mp->nummsg == 0)
213                 adios(NULL, "no messages in %s", folder);
214
215         /* parse all the message ranges/sequences and set SELECTED */
216         for (msgnum = 0; msgnum < msgs.size; msgnum++)
217                 if (!m_convert(mp, msgs.msgs[msgnum]))
218                         done(1);
219
220         make_comp(&comp);
221
222         /* annotate all the SELECTED messages */
223         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
224                 if (is_selected(mp, msgnum)) {
225                         if (list)
226                                 annolist(m_name(msgnum), comp, text, number);
227                         else
228                                 annotate(m_name(msgnum), comp, text, datesw, delete, append);
229                 }
230         }
231
232         context_replace(curfolder, folder);  /* update current folder  */
233         seq_setcur(mp, mp->lowsel);  /* update current message */
234         seq_save(mp);  /* synchronize message sequences */
235         folder_free(mp);  /* free folder/message structure */
236         context_save();  /* save the context file */
237         done(0);
238         return 1;
239 }
240
241 static void
242 make_comp(unsigned char **ap)
243 {
244         register unsigned char *cp;
245         char buffer[BUFSIZ];
246
247         if (*ap == NULL) {
248                 printf("Enter component name: ");
249                 fflush(stdout);
250
251                 if (fgets(buffer, sizeof buffer, stdin) == NULL)
252                         done(1);
253                 *ap = trimcpy(buffer);
254         }
255
256         if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
257                 *cp = 0;
258         if (strlen(*ap) == 0)
259                 adios(NULL, "null component name");
260         if (**ap == '-')
261                 adios(NULL, "invalid component name %s", *ap);
262         if (strlen(*ap) >= NAMESZ)
263                 adios(NULL, "too large component name %s", *ap);
264
265         for (cp = *ap; *cp; cp++)
266                 if (!isalnum(*cp) && *cp != '-')
267                         adios(NULL, "invalid component name %s", *ap);
268 }