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 char *version=VERSION;
49 static char drft[BUFSIZ];
50 static struct msgs *mp = NULL;
56 static void add_forw_hdr(char *);
57 static int build_form(char *, char *, int, int);
61 main(int argc, char **argv)
63 int msgp = 0, anot = 0;
64 int issue = 0, volume = 0;
67 char *cp, *cwd, *maildir;
68 char *digest = NULL, *ed = NULL;
70 char *form = NULL, buf[BUFSIZ], value[10];
71 char **argp, **arguments, *msgs[MAXARGS];
75 setlocale(LC_ALL, "");
76 invo_name = mhbasename(argv[0]);
78 /* read user profile/context */
81 arguments = getarguments(invo_name, argc, argv, 1);
84 while ((cp = *argp++)) {
86 switch (smatch(++cp, switches)) {
88 ambigsw(cp, switches);
91 adios(EX_USAGE, NULL, "-%s unknown", cp);
94 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
95 print_help(buf, switches, 1);
96 exit(argc == 2 ? EX_OK : EX_USAGE);
98 print_version(invo_name);
99 exit(argc == 2 ? EX_OK : EX_USAGE);
109 if (!(ed = *argp++) || *ed == '-')
110 adios(EX_USAGE, NULL, "missing argument to %s",
115 if (!(whatnowproc = *argp++) ||
117 adios(EX_USAGE, NULL, "missing argument to %s",
126 if (!(form = *argp++) || *form == '-')
127 adios(EX_USAGE, NULL, "missing argument to %s",
132 if (!(digest = *argp++) || *digest == '-')
133 adios(EX_USAGE, NULL, "missing argument to %s",
137 if (!(cp = *argp++) || *cp == '-')
138 adios(EX_USAGE, NULL, "missing argument to %s",
140 if ((issue = atoi(cp)) < 1)
141 adios(EX_USAGE, NULL, "bad argument %s %s",
145 if (!(cp = *argp++) || *cp == '-')
146 adios(EX_USAGE, NULL, "missing argument to %s",
148 if ((volume = atoi(cp)) < 1)
149 adios(EX_USAGE, NULL, "bad argument %s %s",
154 if (*cp == '+' || *cp == '@') {
156 adios(EX_USAGE, NULL, "only one folder at a time!");
158 folder = mh_xstrdup(expandfol(cp));
164 cwd = mh_xstrdup(pwd());
165 strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
168 ** FIXME: (concerning MHE support (buildsw) only)
169 ** There's no check if the draft already exists. mmh has removed
170 ** this case by having the draft folder. I won't add code only to
171 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
175 ** Forwarding a message.
178 msgs[msgp++] = seq_cur;
180 folder = getcurfol();
181 maildir = toabsdir(folder);
183 if (chdir(maildir) == NOTOK)
184 adios(EX_OSERR, maildir, "unable to change directory to");
186 /* read folder and create message structure */
187 if (!(mp = folder_read(folder)))
188 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
190 /* check for empty folder */
192 adios(EX_DATAERR, NULL, "no messages in %s", folder);
194 /* parse all the message ranges/sequences and set SELECTED */
195 for (msgnum = 0; msgnum < msgp; msgnum++) {
196 if (!m_convert(mp, msgs[msgnum])) {
200 seq_setprev(mp); /* set the previous sequence */
202 if ((out = creat(drft, m_gmprot())) == NOTOK)
203 adios(EX_CANTCREAT, drft, "unable to create");
205 /* Open form (component) file. */
208 snprintf(buf, sizeof(buf), IFORMAT, digest);
209 if (volume == 0 && (cp = context_find(buf))
210 && ((issue = atoi(cp)) < 0))
215 snprintf(buf, sizeof(buf), VFORMAT, digest);
216 if ((cp = context_find(buf)) == NULL ||
217 (volume = atoi(cp)) <= 0)
222 in = build_form(form, digest, volume, issue);
223 cpydata(in, out, form, drft);
226 fmtstr = new_fs(form, forwcomps);
227 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
228 adios(EX_IOERR, drft, "error writing");
236 snprintf(buf, sizeof(buf), IFORMAT, digest);
237 snprintf(value, sizeof(value), "%d", issue);
238 context_replace(buf, mh_xstrdup(value));
239 snprintf(buf, sizeof(buf), VFORMAT, digest);
240 snprintf(value, sizeof(value), "%d", volume);
241 context_replace(buf, mh_xstrdup(value));
244 context_replace(curfolder, folder); /* update current folder */
245 seq_setcur(mp, mp->lowsel); /* update current message */
246 seq_save(mp); /* synchronize sequences */
247 context_save(); /* save the context file */
251 what_now(ed, NOUSE, drft, NULL, 0, mp,
252 anot ? "Forwarded" : NULL, cwd);
258 ** Create an attachment header for the to be forward messages.
261 add_forw_hdr(char *draft)
268 vec[vecp++] = "anno";
269 vec[vecp++] = "-append";
270 vec[vecp++] = "-nodate";
272 vec[vecp++] = "-comp";
273 vec[vecp++] = attach_hdr;
274 vec[vecp++] = "-text";
275 snprintf(buf, sizeof buf, "+%s", mp->foldpath);
276 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
277 if (!is_selected(mp, msgnum)) {
280 if (strlen(buf) + 1 + strlen(m_name(msgnum)) + 1
282 adios(EX_DATAERR, NULL, "Attachment header line too long. "
283 "Forward less messages.");
286 strcat(buf, m_name(msgnum));
290 if (execprog(*vec, vec) != 0) {
291 advise(NULL, "unable to add attachment header");
297 build_form(char *form, char *digest, int volume, int issue)
302 char *line, tmpfil[BUFSIZ];
309 /* Get new format string */
310 fmtstr = new_fs(form, NULL);
311 fmtsize = strlen(fmtstr) + 256;
313 /* Compile format string */
314 fmt_compile(fmtstr, &fmt);
316 FINDCOMP(cptr, "digest");
318 cptr->c_text = digest;
319 FINDCOMP(cptr, "date");
321 cptr->c_text = mh_xstrdup(dtimenow());
329 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
331 adios(EX_CANTCREAT, "forw", "unable to create temporary file");
333 strncpy(tmpfil, cp, sizeof(tmpfil));
335 if ((in = dup(fileno(tmp))) == NOTOK)
336 adios(EX_OSERR, "dup", "unable to");
338 line = mh_xcalloc(fmtsize, sizeof(char));
339 fmt_scan(fmt, line, fmtsize, dat);
343 adios(EX_IOERR, tmpfil, "error writing");
345 lseek(in, (off_t) 0, SEEK_SET);