2 ** forw.c -- forward a message, or group of messages.
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.
11 #include <h/fmt_scan.h>
16 #define IFORMAT "digest-issue-%s"
17 #define VFORMAT "digest-volume-%s"
19 static struct swit switches[] = {
25 { "editor editor", 0 },
29 { "form formfile", 0 },
33 { "issue number", 0 },
35 { "volume number", 0 },
37 { "whatnowproc program", 0 },
39 { "nowhatnowproc", 0 },
47 { "build", 5 }, /* interface from mhe */
53 static char drft[BUFSIZ];
54 static struct msgs *mp = NULL;
60 static void add_forw_hdr(char *);
61 static int build_form(char *, char *, int, int);
65 main(int argc, char **argv)
67 int msgp = 0, anot = 0;
68 int issue = 0, volume = 0;
69 int nedit = 0, nwhat = 0, in;
71 char *cp, *cwd, *maildir;
72 char *digest = NULL, *ed = NULL;
74 char *form = NULL, buf[BUFSIZ], value[10];
75 char **argp, **arguments, *msgs[MAXARGS];
83 setlocale(LC_ALL, "");
85 invo_name = mhbasename(argv[0]);
87 /* read user profile/context */
90 arguments = getarguments(invo_name, argc, argv, 1);
93 while ((cp = *argp++)) {
95 switch (smatch(++cp, switches)) {
97 ambigsw(cp, switches);
100 adios(NULL, "-%s unknown", cp);
103 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
104 print_help(buf, switches, 1);
107 print_version(invo_name);
118 if (!(ed = *argp++) || *ed == '-')
119 adios(NULL, "missing argument to %s",
128 if (!(whatnowproc = *argp++) ||
130 adios(NULL, "missing argument to %s",
136 buildsw++; /* fall... */
143 if (!(form = *argp++) || *form == '-')
144 adios(NULL, "missing argument to %s",
149 if (!(digest = *argp++) || *digest == '-')
150 adios(NULL, "missing argument to %s",
155 if (!(cp = *argp++) || *cp == '-')
156 adios(NULL, "missing argument to %s",
158 if ((issue = atoi(cp)) < 1)
159 adios(NULL, "bad argument %s %s",
163 if (!(cp = *argp++) || *cp == '-')
164 adios(NULL, "missing argument to %s",
166 if ((volume = atoi(cp)) < 1)
167 adios(NULL, "bad argument %s %s",
172 if (*cp == '+' || *cp == '@') {
174 adios(NULL, "only one folder at a time!");
176 folder = getcpy(expandfol(cp));
185 strncpy(drft, buildsw ? toabsdir("draft")
186 : m_draft(seq_beyond), sizeof(drft));
188 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
192 ** Forwarding a message.
195 msgs[msgp++] = seq_cur;
197 folder = getcurfol();
198 maildir = toabsdir(folder);
200 if (chdir(maildir) == NOTOK)
201 adios(maildir, "unable to change directory to");
203 /* read folder and create message structure */
204 if (!(mp = folder_read(folder)))
205 adios(NULL, "unable to read folder %s", folder);
207 /* check for empty folder */
209 adios(NULL, "no messages in %s", folder);
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum = 0; msgnum < msgp; msgnum++)
213 if (!m_convert(mp, msgs[msgnum]))
215 seq_setprev(mp); /* set the previous sequence */
217 if ((out = creat(drft, m_gmprot())) == NOTOK)
218 adios(drft, "unable to create");
220 /* Open form (component) file. */
223 snprintf(buf, sizeof(buf), IFORMAT, digest);
224 if (volume == 0 && (cp = context_find(buf))
225 && ((issue = atoi(cp)) < 0))
230 snprintf(buf, sizeof(buf), VFORMAT, digest);
231 if ((cp = context_find(buf)) == NULL ||
232 (volume = atoi(cp)) <= 0)
236 in = build_form(form, digest, volume, issue);
237 cpydata(in, out, form, drft);
240 fmtstr = new_fs(form, forwcomps);
241 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
242 adios(drft, "error writing");
250 snprintf(buf, sizeof(buf), IFORMAT, digest);
251 snprintf(value, sizeof(value), "%d", issue);
252 context_replace(buf, getcpy(value));
253 snprintf(buf, sizeof(buf), VFORMAT, digest);
254 snprintf(value, sizeof(value), "%d", volume);
255 context_replace(buf, getcpy(value));
258 context_replace(curfolder, folder); /* update current folder */
259 seq_setcur(mp, mp->lowsel); /* update current message */
260 seq_save(mp); /* synchronize sequences */
261 context_save(); /* save the context file */
265 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
266 anot ? "Forwarded" : NULL, cwd);
273 ** Create an attachment header for the to be forward messages.
276 add_forw_hdr(char *draft)
281 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
282 "-comp '%s' -text '+%s",
283 draft, attach_hdr, mp->foldpath);
284 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
285 if (!is_selected(mp, msgnum)) {
288 /* TODO: Check for buffer length! */
290 strcat(buffer, m_name(msgnum));
293 /* TODO: This check is bad, but better than nothing */
294 if (strlen(buffer) > BUFSIZ) {
295 adios(NULL, "Too long attachment header line. Forward less messages.");
297 if (system(buffer) != 0) {
298 advise(NULL, "unable to add attachment header");
304 build_form(char *form, char *digest, int volume, int issue)
308 register char *fmtstr;
309 char *line, tmpfil[BUFSIZ];
311 register struct comp *cptr;
316 /* Get new format string */
317 fmtstr = new_fs(form, NULL);
318 fmtsize = strlen(fmtstr) + 256;
320 /* Compile format string */
321 fmt_compile(fmtstr, &fmt);
323 FINDCOMP(cptr, "digest");
325 cptr->c_text = digest;
326 FINDCOMP(cptr, "date");
328 cptr->c_text = getcpy(dtimenow(0));
336 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
337 if (cp == NULL) adios("forw", "unable to create temporary file");
338 strncpy(tmpfil, cp, sizeof(tmpfil));
340 if ((in = dup(fileno(tmp))) == NOTOK)
341 adios("dup", "unable to");
343 line = mh_xmalloc((unsigned) fmtsize);
344 fmt_scan(fmt, line, fmtsize, dat);
348 adios(tmpfil, "error writing");
350 lseek(in, (off_t) 0, SEEK_SET);