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>
18 #define IFORMAT "digest-issue-%s"
19 #define VFORMAT "digest-volume-%s"
21 static struct swit switches[] = {
27 { "editor editor", 0 },
29 { "form formfile", 0 },
33 { "issue number", 0 },
35 { "volume number", 0 },
37 { "whatnowproc program", 0 },
43 { "build", 5 }, /* interface from mhe */
47 static char drft[BUFSIZ];
48 static struct msgs *mp = NULL;
54 static void add_forw_hdr(char *);
55 static int build_form(char *, char *, int, int);
59 main(int argc, char **argv)
61 int msgp = 0, anot = 0;
62 int issue = 0, volume = 0;
65 char *cp, *cwd, *maildir;
66 char *digest = NULL, *ed = NULL;
68 char *form = NULL, buf[BUFSIZ], value[10];
69 char **argp, **arguments, *msgs[MAXARGS];
73 setlocale(LC_ALL, "");
74 invo_name = mhbasename(argv[0]);
76 /* read user profile/context */
79 arguments = getarguments(invo_name, argc, argv, 1);
82 while ((cp = *argp++)) {
84 switch (smatch(++cp, switches)) {
86 ambigsw(cp, switches);
89 adios(EX_USAGE, NULL, "-%s unknown", cp);
92 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
93 print_help(buf, switches, 1);
94 exit(argc == 2 ? EX_OK : EX_USAGE);
96 print_version(invo_name);
97 exit(argc == 2 ? EX_OK : EX_USAGE);
107 if (!(ed = *argp++) || *ed == '-')
108 adios(EX_USAGE, NULL, "missing argument to %s",
113 if (!(whatnowproc = *argp++) ||
115 adios(EX_USAGE, NULL, "missing argument to %s",
124 if (!(form = *argp++) || *form == '-')
125 adios(EX_USAGE, NULL, "missing argument to %s",
130 if (!(digest = *argp++) || *digest == '-')
131 adios(EX_USAGE, NULL, "missing argument to %s",
135 if (!(cp = *argp++) || *cp == '-')
136 adios(EX_USAGE, NULL, "missing argument to %s",
138 if ((issue = atoi(cp)) < 1)
139 adios(EX_USAGE, NULL, "bad argument %s %s",
143 if (!(cp = *argp++) || *cp == '-')
144 adios(EX_USAGE, NULL, "missing argument to %s",
146 if ((volume = atoi(cp)) < 1)
147 adios(EX_USAGE, NULL, "bad argument %s %s",
152 if (*cp == '+' || *cp == '@') {
154 adios(EX_USAGE, NULL, "only one folder at a time!");
156 folder = getcpy(expandfol(cp));
163 strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
166 ** FIXME: (concerning MHE support (buildsw) only)
167 ** There's no check if the draft already exists. mmh has removed
168 ** this case by having the draft folder. I won't add code only to
169 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
173 ** Forwarding a message.
176 msgs[msgp++] = seq_cur;
178 folder = getcurfol();
179 maildir = toabsdir(folder);
181 if (chdir(maildir) == NOTOK)
182 adios(EX_OSERR, maildir, "unable to change directory to");
184 /* read folder and create message structure */
185 if (!(mp = folder_read(folder)))
186 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
188 /* check for empty folder */
190 adios(EX_DATAERR, NULL, "no messages in %s", folder);
192 /* parse all the message ranges/sequences and set SELECTED */
193 for (msgnum = 0; msgnum < msgp; msgnum++) {
194 if (!m_convert(mp, msgs[msgnum])) {
198 seq_setprev(mp); /* set the previous sequence */
200 if ((out = creat(drft, m_gmprot())) == NOTOK)
201 adios(EX_CANTCREAT, drft, "unable to create");
203 /* Open form (component) file. */
206 snprintf(buf, sizeof(buf), IFORMAT, digest);
207 if (volume == 0 && (cp = context_find(buf))
208 && ((issue = atoi(cp)) < 0))
213 snprintf(buf, sizeof(buf), VFORMAT, digest);
214 if ((cp = context_find(buf)) == NULL ||
215 (volume = atoi(cp)) <= 0)
220 in = build_form(form, digest, volume, issue);
221 cpydata(in, out, form, drft);
224 fmtstr = new_fs(form, forwcomps);
225 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
226 adios(EX_IOERR, drft, "error writing");
234 snprintf(buf, sizeof(buf), IFORMAT, digest);
235 snprintf(value, sizeof(value), "%d", issue);
236 context_replace(buf, getcpy(value));
237 snprintf(buf, sizeof(buf), VFORMAT, digest);
238 snprintf(value, sizeof(value), "%d", volume);
239 context_replace(buf, getcpy(value));
242 context_replace(curfolder, folder); /* update current folder */
243 seq_setcur(mp, mp->lowsel); /* update current message */
244 seq_save(mp); /* synchronize sequences */
245 context_save(); /* save the context file */
249 what_now(ed, NOUSE, drft, NULL, 0, mp,
250 anot ? "Forwarded" : NULL, cwd);
256 ** Create an attachment header for the to be forward messages.
259 add_forw_hdr(char *draft)
266 vec[vecp++] = "anno";
267 vec[vecp++] = "-append";
268 vec[vecp++] = "-nodate";
270 vec[vecp++] = "-comp";
271 vec[vecp++] = attach_hdr;
272 vec[vecp++] = "-text";
273 snprintf(buf, sizeof buf, "+%s", mp->foldpath);
274 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
275 if (!is_selected(mp, msgnum)) {
278 if (strlen(buf) + 1 + strlen(m_name(msgnum)) + 1
280 adios(EX_DATAERR, NULL, "Attachment header line too long. "
281 "Forward less messages.");
284 strcat(buf, m_name(msgnum));
288 if (execprog(*vec, vec) != 0) {
289 advise(NULL, "unable to add attachment header");
295 build_form(char *form, char *digest, int volume, int issue)
300 char *line, tmpfil[BUFSIZ];
307 /* Get new format string */
308 fmtstr = new_fs(form, NULL);
309 fmtsize = strlen(fmtstr) + 256;
311 /* Compile format string */
312 fmt_compile(fmtstr, &fmt);
314 FINDCOMP(cptr, "digest");
316 cptr->c_text = digest;
317 FINDCOMP(cptr, "date");
319 cptr->c_text = getcpy(dtimenow());
327 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
329 adios(EX_CANTCREAT, "forw", "unable to create temporary file");
331 strncpy(tmpfil, cp, sizeof(tmpfil));
333 if ((in = dup(fileno(tmp))) == NOTOK)
334 adios(EX_OSERR, "dup", "unable to");
336 line = mh_xmalloc((unsigned) fmtsize);
337 fmt_scan(fmt, line, fmtsize, dat);
341 adios(EX_IOERR, tmpfil, "error writing");
343 lseek(in, (off_t) 0, SEEK_SET);