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 },
27 { "form formfile", 0 },
31 { "issue number", 0 },
33 { "volume number", 0 },
35 { "whatnowproc program", 0 },
41 { "build", 5 }, /* interface from mhe */
45 static char drft[BUFSIZ];
46 static struct msgs *mp = NULL;
52 static void add_forw_hdr(char *);
53 static int build_form(char *, char *, int, int);
57 main(int argc, char **argv)
59 int msgp = 0, anot = 0;
60 int issue = 0, volume = 0;
63 char *cp, *cwd, *maildir;
64 char *digest = NULL, *ed = NULL;
66 char *form = NULL, buf[BUFSIZ], value[10];
67 char **argp, **arguments, *msgs[MAXARGS];
71 setlocale(LC_ALL, "");
72 invo_name = mhbasename(argv[0]);
74 /* read user profile/context */
77 arguments = getarguments(invo_name, argc, argv, 1);
80 while ((cp = *argp++)) {
82 switch (smatch(++cp, switches)) {
84 ambigsw(cp, switches);
87 adios(NULL, "-%s unknown", cp);
90 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
91 print_help(buf, switches, 1);
94 print_version(invo_name);
105 if (!(ed = *argp++) || *ed == '-')
106 adios(NULL, "missing argument to %s",
111 if (!(whatnowproc = *argp++) ||
113 adios(NULL, "missing argument to %s",
122 if (!(form = *argp++) || *form == '-')
123 adios(NULL, "missing argument to %s",
128 if (!(digest = *argp++) || *digest == '-')
129 adios(NULL, "missing argument to %s",
134 if (!(cp = *argp++) || *cp == '-')
135 adios(NULL, "missing argument to %s",
137 if ((issue = atoi(cp)) < 1)
138 adios(NULL, "bad argument %s %s",
142 if (!(cp = *argp++) || *cp == '-')
143 adios(NULL, "missing argument to %s",
145 if ((volume = atoi(cp)) < 1)
146 adios(NULL, "bad argument %s %s",
151 if (*cp == '+' || *cp == '@') {
153 adios(NULL, "only one folder at a time!");
155 folder = getcpy(expandfol(cp));
162 strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
165 ** FIXME: (concerning MHE support (buildsw) only)
166 ** There's no check if the draft already exists. mmh has removed
167 ** this case by having the draft folder. I won't add code only to
168 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
172 ** Forwarding a message.
175 msgs[msgp++] = seq_cur;
177 folder = getcurfol();
178 maildir = toabsdir(folder);
180 if (chdir(maildir) == NOTOK)
181 adios(maildir, "unable to change directory to");
183 /* read folder and create message structure */
184 if (!(mp = folder_read(folder)))
185 adios(NULL, "unable to read folder %s", folder);
187 /* check for empty folder */
189 adios(NULL, "no messages in %s", folder);
191 /* parse all the message ranges/sequences and set SELECTED */
192 for (msgnum = 0; msgnum < msgp; msgnum++)
193 if (!m_convert(mp, msgs[msgnum]))
195 seq_setprev(mp); /* set the previous sequence */
197 if ((out = creat(drft, m_gmprot())) == NOTOK)
198 adios(drft, "unable to create");
200 /* Open form (component) file. */
203 snprintf(buf, sizeof(buf), IFORMAT, digest);
204 if (volume == 0 && (cp = context_find(buf))
205 && ((issue = atoi(cp)) < 0))
210 snprintf(buf, sizeof(buf), VFORMAT, digest);
211 if ((cp = context_find(buf)) == NULL ||
212 (volume = atoi(cp)) <= 0)
217 in = build_form(form, digest, volume, issue);
218 cpydata(in, out, form, drft);
221 fmtstr = new_fs(form, forwcomps);
222 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
223 adios(drft, "error writing");
231 snprintf(buf, sizeof(buf), IFORMAT, digest);
232 snprintf(value, sizeof(value), "%d", issue);
233 context_replace(buf, getcpy(value));
234 snprintf(buf, sizeof(buf), VFORMAT, digest);
235 snprintf(value, sizeof(value), "%d", volume);
236 context_replace(buf, getcpy(value));
239 context_replace(curfolder, folder); /* update current folder */
240 seq_setcur(mp, mp->lowsel); /* update current message */
241 seq_save(mp); /* synchronize sequences */
242 context_save(); /* save the context file */
246 what_now(ed, NOUSE, drft, NULL, 0, mp,
247 anot ? "Forwarded" : NULL, cwd);
254 ** Create an attachment header for the to be forward messages.
257 add_forw_hdr(char *draft)
262 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
263 "-comp '%s' -text '+%s",
264 draft, attach_hdr, mp->foldpath);
265 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
266 if (!is_selected(mp, msgnum)) {
269 /* TODO: Check for buffer length! */
271 strcat(buffer, m_name(msgnum));
274 /* TODO: This check is bad, but better than nothing */
275 if (strlen(buffer) > BUFSIZ) {
276 adios(NULL, "Too long attachment header line. Forward less messages.");
278 if (system(buffer) != 0) {
279 advise(NULL, "unable to add attachment header");
285 build_form(char *form, char *digest, int volume, int issue)
289 register char *fmtstr;
290 char *line, tmpfil[BUFSIZ];
292 register struct comp *cptr;
297 /* Get new format string */
298 fmtstr = new_fs(form, NULL);
299 fmtsize = strlen(fmtstr) + 256;
301 /* Compile format string */
302 fmt_compile(fmtstr, &fmt);
304 FINDCOMP(cptr, "digest");
306 cptr->c_text = digest;
307 FINDCOMP(cptr, "date");
309 cptr->c_text = getcpy(dtimenow());
317 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
318 if (cp == NULL) adios("forw", "unable to create temporary file");
319 strncpy(tmpfil, cp, sizeof(tmpfil));
321 if ((in = dup(fileno(tmp))) == NOTOK)
322 adios("dup", "unable to");
324 line = mh_xmalloc((unsigned) fmtsize);
325 fmt_scan(fmt, line, fmtsize, dat);
329 adios(tmpfil, "error writing");
331 lseek(in, (off_t) 0, SEEK_SET);