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 },
43 { "build", 5 }, /* interface from mhe */
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];
79 setlocale(LC_ALL, "");
81 invo_name = mhbasename(argv[0]);
83 /* read user profile/context */
86 arguments = getarguments(invo_name, argc, argv, 1);
89 while ((cp = *argp++)) {
91 switch (smatch(++cp, switches)) {
93 ambigsw(cp, switches);
96 adios(NULL, "-%s unknown", cp);
99 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
100 print_help(buf, switches, 1);
103 print_version(invo_name);
114 if (!(ed = *argp++) || *ed == '-')
115 adios(NULL, "missing argument to %s",
120 if (!(whatnowproc = *argp++) ||
122 adios(NULL, "missing argument to %s",
127 buildsw++; /* fall... */
132 if (!(form = *argp++) || *form == '-')
133 adios(NULL, "missing argument to %s",
138 if (!(digest = *argp++) || *digest == '-')
139 adios(NULL, "missing argument to %s",
144 if (!(cp = *argp++) || *cp == '-')
145 adios(NULL, "missing argument to %s",
147 if ((issue = atoi(cp)) < 1)
148 adios(NULL, "bad argument %s %s",
152 if (!(cp = *argp++) || *cp == '-')
153 adios(NULL, "missing argument to %s",
155 if ((volume = atoi(cp)) < 1)
156 adios(NULL, "bad argument %s %s",
161 if (*cp == '+' || *cp == '@') {
163 adios(NULL, "only one folder at a time!");
165 folder = getcpy(expandfol(cp));
174 strncpy(drft, buildsw ? toabsdir("draft")
175 : m_draft(seq_beyond), sizeof(drft));
177 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
181 ** Forwarding a message.
184 msgs[msgp++] = seq_cur;
186 folder = getcurfol();
187 maildir = toabsdir(folder);
189 if (chdir(maildir) == NOTOK)
190 adios(maildir, "unable to change directory to");
192 /* read folder and create message structure */
193 if (!(mp = folder_read(folder)))
194 adios(NULL, "unable to read folder %s", folder);
196 /* check for empty folder */
198 adios(NULL, "no messages in %s", folder);
200 /* parse all the message ranges/sequences and set SELECTED */
201 for (msgnum = 0; msgnum < msgp; msgnum++)
202 if (!m_convert(mp, msgs[msgnum]))
204 seq_setprev(mp); /* set the previous sequence */
206 if ((out = creat(drft, m_gmprot())) == NOTOK)
207 adios(drft, "unable to create");
209 /* Open form (component) file. */
212 snprintf(buf, sizeof(buf), IFORMAT, digest);
213 if (volume == 0 && (cp = context_find(buf))
214 && ((issue = atoi(cp)) < 0))
219 snprintf(buf, sizeof(buf), VFORMAT, digest);
220 if ((cp = context_find(buf)) == NULL ||
221 (volume = atoi(cp)) <= 0)
226 in = build_form(form, digest, volume, issue);
227 cpydata(in, out, form, drft);
230 fmtstr = new_fs(form, forwcomps);
231 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
232 adios(drft, "error writing");
240 snprintf(buf, sizeof(buf), IFORMAT, digest);
241 snprintf(value, sizeof(value), "%d", issue);
242 context_replace(buf, getcpy(value));
243 snprintf(buf, sizeof(buf), VFORMAT, digest);
244 snprintf(value, sizeof(value), "%d", volume);
245 context_replace(buf, getcpy(value));
248 context_replace(curfolder, folder); /* update current folder */
249 seq_setcur(mp, mp->lowsel); /* update current message */
250 seq_save(mp); /* synchronize sequences */
251 context_save(); /* save the context file */
255 what_now(ed, NOUSE, drft, NULL, 0, mp,
256 anot ? "Forwarded" : NULL, cwd);
263 ** Create an attachment header for the to be forward messages.
266 add_forw_hdr(char *draft)
271 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
272 "-comp '%s' -text '+%s",
273 draft, attach_hdr, mp->foldpath);
274 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
275 if (!is_selected(mp, msgnum)) {
278 /* TODO: Check for buffer length! */
280 strcat(buffer, m_name(msgnum));
283 /* TODO: This check is bad, but better than nothing */
284 if (strlen(buffer) > BUFSIZ) {
285 adios(NULL, "Too long attachment header line. Forward less messages.");
287 if (system(buffer) != 0) {
288 advise(NULL, "unable to add attachment header");
294 build_form(char *form, char *digest, int volume, int issue)
298 register char *fmtstr;
299 char *line, tmpfil[BUFSIZ];
301 register struct comp *cptr;
306 /* Get new format string */
307 fmtstr = new_fs(form, NULL);
308 fmtsize = strlen(fmtstr) + 256;
310 /* Compile format string */
311 fmt_compile(fmtstr, &fmt);
313 FINDCOMP(cptr, "digest");
315 cptr->c_text = digest;
316 FINDCOMP(cptr, "date");
318 cptr->c_text = getcpy(dtimenow());
326 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
327 if (cp == NULL) adios("forw", "unable to create temporary file");
328 strncpy(tmpfil, cp, sizeof(tmpfil));
330 if ((in = dup(fileno(tmp))) == NOTOK)
331 adios("dup", "unable to");
333 line = mh_xmalloc((unsigned) fmtsize);
334 fmt_scan(fmt, line, fmtsize, dat);
338 adios(tmpfil, "error writing");
340 lseek(in, (off_t) 0, SEEK_SET);