]>
diplodocus.org Git - nmh/blob - uip/anno.c
65aeb6542f5aae8be8502928812c390ef0304288
1 /* anno.c -- annotate messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
7 * Four new options have been added: delete, list, number, and draft.
8 * Message header fields are used by the new MIME attachment code in
9 * the send command. Adding features to generalize the anno command
10 * seemed to be a better approach than the creation of a new command
11 * whose features would overlap with those of the anno command.
13 * The -draft option causes anno to operate on the current draft file
14 * instead of on a message sequence.
16 * The -delete option deletes header elements that match the -component
17 * field name. If -delete is used without the -text option, the first
18 * header field whose field name matches the component name is deleted.
19 * If the -delete is used with the -text option, and the -text argument
20 * begins with a /, the first header field whose field name matches the
21 * component name and whose field body matches the text is deleted. If
22 * the -text argument does not begin with a /, then the text is assumed
23 * to be the last component of a path name, and the first header field
24 * whose field name matches the component name and a field body whose
25 * last path name component matches the text is deleted. If the -delete
26 * option is used with the new -number option described below, the nth
27 * header field whose field name matches the component name is deleted.
28 * No header fields are deleted if none of the above conditions are met.
30 * The -list option outputs the field bodies from each header field whose
31 * field name matches the component name, one per line. If no -text
32 * option is specified, only the last path name component of each field
33 * body is output. The entire field body is output if the -text option
34 * is used; the contents of the -text argument are ignored. If the -list
35 * option is used in conjunction with the new -number option described
36 * below, each line is numbered starting with 1. A tab separates the
37 * number from the field body.
39 * The -number option works with both the -delete and -list options as
40 * described above. The -number option takes an optional argument. A
41 * value of 1 is assumed if this argument is absent.
45 #include "sbr/seq_save.h"
46 #include "sbr/smatch.h"
47 #include "sbr/trimcpy.h"
48 #include "sbr/m_draft.h"
49 #include "sbr/m_convert.h"
50 #include "sbr/getfolder.h"
51 #include "sbr/folder_read.h"
52 #include "sbr/folder_free.h"
53 #include "sbr/context_save.h"
54 #include "sbr/context_replace.h"
55 #include "sbr/context_find.h"
56 #include "sbr/ambigsw.h"
58 #include "sbr/print_version.h"
59 #include "sbr/print_help.h"
60 #include "sbr/error.h"
63 #include "sbr/m_maildir.h"
65 #define ANNO_SWITCHES \
66 X("component field", 0, COMPSW) \
67 X("inplace", 0, INPLSW) \
68 X("noinplace", 0, NINPLSW) \
69 X("date", 0, DATESW) \
70 X("nodate", 0, NDATESW) \
71 X("text body", 0, TEXTSW) \
72 X("version", 0, VERSIONSW) \
73 X("help", 0, HELPSW) \
74 X("draft", 2, DRFTSW) \
75 X("list", 1, LISTSW) \
76 X("delete", 2, DELETESW) \
77 X("number", 2, NUMBERSW) \
78 X("append", 1, APPENDSW) \
79 X("preserve", 1, PRESERVESW) \
80 X("nopreserve", 3, NOPRESERVESW) \
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(ANNO
);
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(ANNO
, switches
);
93 static void make_comp (char **);
97 main (int argc
, char **argv
)
102 char *comp
= NULL
, *text
= NULL
, *folder
= NULL
, buf
[BUFSIZ
];
103 char **argp
, **arguments
;
104 struct msgs_array msgs
= { 0, 0, NULL
};
106 bool append
; /* append annotations instead of default prepend */
107 int delete = -2; /* delete header element if set */
108 char *draft
= NULL
; /* draft file name */
109 int isdf
= 0; /* return needed for m_draft() */
110 bool list
; /* list header elements if set */
111 int number
= 0; /* delete specific number of like elements if set */
113 if (nmh_init(argv
[0], true, true)) { return 1; }
115 arguments
= getarguments (invo_name
, argc
, argv
, 1);
118 append
= list
= false;
119 inplace
= datesw
= true;
120 while ((cp
= *argp
++)) {
122 switch (smatch (++cp
, switches
)) {
124 ambigsw (cp
, switches
);
127 die("-%s unknown", cp
);
130 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
132 print_help (buf
, switches
, 1);
135 print_version(invo_name
);
140 die("only one component at a time!");
141 if (!(comp
= *argp
++) || *comp
== '-')
142 die("missing argument to %s", argp
[-2]);
161 die("only one body at a time!");
162 if (!(text
= *argp
++) || *text
== '-')
163 die("missing argument to %s", argp
[-2]);
166 case DELETESW
: /* delete annotations */
170 case DRFTSW
: /* draft message specified */
174 case LISTSW
: /* produce a listing */
178 case NUMBERSW
: /* number listing or delete by number */
180 die("only one number at a time!");
182 if (argp
- arguments
== argc
- 1 || **argp
== '-')
186 if (strcmp(*argp
, "all") == 0)
189 else if (!(number
= atoi(*argp
)))
190 die("missing argument to %s", argp
[-1]);
198 case APPENDSW
: /* append annotations instead of default prepend */
202 case PRESERVESW
: /* preserve access and modification times on annotated message */
206 case NOPRESERVESW
: /* don't preserve access and modification times on annotated message (default) */
211 if (*cp
== '+' || *cp
== '@') {
213 die("only one folder at a time!");
214 folder
= pluspath (cp
);
216 app_msgarg(&msgs
, cp
);
220 * We're dealing with the draft message instead of message numbers.
221 * Get the name of the draft and deal with it just as we do with
222 * message numbers below.
227 die("can only have message numbers or -draft.");
229 draft
= mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
));
234 annolist(draft
, comp
, text
, number
);
236 annotate (draft
, comp
, text
, inplace
, datesw
, delete, append
);
242 if (!context_find ("path"))
243 free (path ("./", TFOLDER
));
245 app_msgarg(&msgs
, "cur");
247 folder
= getfolder (1);
248 maildir
= m_maildir (folder
);
250 if (chdir (maildir
) == NOTOK
)
251 adios (maildir
, "unable to change directory to");
253 /* read folder and create message structure */
254 if (!(mp
= folder_read (folder
, 1)))
255 die("unable to read folder %s", folder
);
257 /* check for empty folder */
259 die("no messages in %s", folder
);
261 /* parse all the message ranges/sequences and set SELECTED */
262 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
263 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
268 /* annotate all the SELECTED messages */
269 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
270 if (is_selected(mp
, msgnum
)) {
272 annolist(m_name(msgnum
), comp
, text
, number
);
274 annotate (m_name (msgnum
), comp
, text
, inplace
, datesw
, delete, append
);
278 context_replace (pfolder
, folder
); /* update current folder */
279 seq_setcur (mp
, mp
->lowsel
); /* update current message */
280 seq_save (mp
); /* synchronize message sequences */
281 folder_free (mp
); /* free folder/message structure */
282 context_save (); /* save the context file */
288 make_comp (char **ap
)
290 char *cp
, buffer
[BUFSIZ
];
293 fputs("Enter component name: ", stdout
);
296 if (fgets (buffer
, sizeof buffer
, stdin
) == NULL
)
298 *ap
= trimcpy (buffer
);
301 if ((cp
= *ap
+ strlen (*ap
) - 1) > *ap
&& *cp
== ':')
304 die("null component name");
306 die("invalid component name %s", *ap
);
307 if (strlen (*ap
) >= NAMESZ
)
308 die("too large component name %s", *ap
);
310 for (cp
= *ap
; *cp
; cp
++)
311 if (!isalnum ((unsigned char) *cp
) && *cp
!= '-')
312 die("invalid component name %s", *ap
);