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];
72 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(NULL, "-%s unknown", cp);
92 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
93 print_help(buf, switches, 1);
96 print_version(invo_name);
107 if (!(ed = *argp++) || *ed == '-')
108 adios(NULL, "missing argument to %s",
113 if (!(whatnowproc = *argp++) ||
115 adios(NULL, "missing argument to %s",
124 if (!(form = *argp++) || *form == '-')
125 adios(NULL, "missing argument to %s",
130 if (!(digest = *argp++) || *digest == '-')
131 adios(NULL, "missing argument to %s",
136 if (!(cp = *argp++) || *cp == '-')
137 adios(NULL, "missing argument to %s",
139 if ((issue = atoi(cp)) < 1)
140 adios(NULL, "bad argument %s %s",
144 if (!(cp = *argp++) || *cp == '-')
145 adios(NULL, "missing argument to %s",
147 if ((volume = atoi(cp)) < 1)
148 adios(NULL, "bad argument %s %s",
153 if (*cp == '+' || *cp == '@') {
155 adios(NULL, "only one folder at a time!");
157 folder = getcpy(expandfol(cp));
164 strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
167 ** FIXME: (concerning MHE support (buildsw) only)
168 ** There's no check if the draft already exists. mmh has removed
169 ** this case by having the draft folder. I won't add code only to
170 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
174 ** Forwarding a message.
177 msgs[msgp++] = seq_cur;
179 folder = getcurfol();
180 maildir = toabsdir(folder);
182 if (chdir(maildir) == NOTOK)
183 adios(maildir, "unable to change directory to");
185 /* read folder and create message structure */
186 if (!(mp = folder_read(folder)))
187 adios(NULL, "unable to read folder %s", folder);
189 /* check for empty folder */
191 adios(NULL, "no messages in %s", folder);
193 /* parse all the message ranges/sequences and set SELECTED */
194 for (msgnum = 0; msgnum < msgp; msgnum++)
195 if (!m_convert(mp, msgs[msgnum]))
197 seq_setprev(mp); /* set the previous sequence */
199 if ((out = creat(drft, m_gmprot())) == NOTOK)
200 adios(drft, "unable to create");
202 /* Open form (component) file. */
205 snprintf(buf, sizeof(buf), IFORMAT, digest);
206 if (volume == 0 && (cp = context_find(buf))
207 && ((issue = atoi(cp)) < 0))
212 snprintf(buf, sizeof(buf), VFORMAT, digest);
213 if ((cp = context_find(buf)) == NULL ||
214 (volume = atoi(cp)) <= 0)
219 in = build_form(form, digest, volume, issue);
220 cpydata(in, out, form, drft);
223 fmtstr = new_fs(form, forwcomps);
224 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
225 adios(drft, "error writing");
233 snprintf(buf, sizeof(buf), IFORMAT, digest);
234 snprintf(value, sizeof(value), "%d", issue);
235 context_replace(buf, getcpy(value));
236 snprintf(buf, sizeof(buf), VFORMAT, digest);
237 snprintf(value, sizeof(value), "%d", volume);
238 context_replace(buf, getcpy(value));
241 context_replace(curfolder, folder); /* update current folder */
242 seq_setcur(mp, mp->lowsel); /* update current message */
243 seq_save(mp); /* synchronize sequences */
244 context_save(); /* save the context file */
248 what_now(ed, NOUSE, drft, NULL, 0, mp,
249 anot ? "Forwarded" : NULL, cwd);
256 ** Create an attachment header for the to be forward messages.
259 add_forw_hdr(char *draft)
264 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
265 "-comp '%s' -text '+%s",
266 draft, attach_hdr, mp->foldpath);
267 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
268 if (!is_selected(mp, msgnum)) {
271 /* TODO: Check for buffer length! */
273 strcat(buffer, m_name(msgnum));
276 /* TODO: This check is bad, but better than nothing */
277 if (strlen(buffer) > BUFSIZ) {
278 adios(NULL, "Too long attachment header line. Forward less messages.");
280 if (system(buffer) != 0) {
281 advise(NULL, "unable to add attachment header");
287 build_form(char *form, char *digest, int volume, int issue)
291 register char *fmtstr;
292 char *line, tmpfil[BUFSIZ];
294 register struct comp *cptr;
299 /* Get new format string */
300 fmtstr = new_fs(form, NULL);
301 fmtsize = strlen(fmtstr) + 256;
303 /* Compile format string */
304 fmt_compile(fmtstr, &fmt);
306 FINDCOMP(cptr, "digest");
308 cptr->c_text = digest;
309 FINDCOMP(cptr, "date");
311 cptr->c_text = getcpy(dtimenow());
319 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
320 if (cp == NULL) adios("forw", "unable to create temporary file");
321 strncpy(tmpfil, cp, sizeof(tmpfil));
323 if ((in = dup(fileno(tmp))) == NOTOK)
324 adios("dup", "unable to");
326 line = mh_xmalloc((unsigned) fmtsize);
327 fmt_scan(fmt, line, fmtsize, dat);
331 adios(tmpfil, "error writing");
333 lseek(in, (off_t) 0, SEEK_SET);