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 },
29 { "form formfile", 0 },
33 { "issue number", 0 },
35 { "volume number", 0 },
37 { "whatnowproc program", 0 },
45 { "build", 5 }, /* interface from mhe */
51 static char drft[BUFSIZ];
52 static struct msgs *mp = NULL;
58 static void add_forw_hdr(char *);
59 static int build_form(char *, char *, int, int);
63 main(int argc, char **argv)
65 int msgp = 0, anot = 0;
66 int issue = 0, volume = 0;
69 char *cp, *cwd, *maildir;
70 char *digest = NULL, *ed = NULL;
72 char *form = NULL, buf[BUFSIZ], value[10];
73 char **argp, **arguments, *msgs[MAXARGS];
81 setlocale(LC_ALL, "");
83 invo_name = mhbasename(argv[0]);
85 /* read user profile/context */
88 arguments = getarguments(invo_name, argc, argv, 1);
91 while ((cp = *argp++)) {
93 switch (smatch(++cp, switches)) {
95 ambigsw(cp, switches);
98 adios(NULL, "-%s unknown", cp);
101 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
102 print_help(buf, switches, 1);
105 print_version(invo_name);
116 if (!(ed = *argp++) || *ed == '-')
117 adios(NULL, "missing argument to %s",
126 if (!(whatnowproc = *argp++) ||
128 adios(NULL, "missing argument to %s",
133 buildsw++; /* fall... */
138 if (!(form = *argp++) || *form == '-')
139 adios(NULL, "missing argument to %s",
144 if (!(digest = *argp++) || *digest == '-')
145 adios(NULL, "missing argument to %s",
150 if (!(cp = *argp++) || *cp == '-')
151 adios(NULL, "missing argument to %s",
153 if ((issue = atoi(cp)) < 1)
154 adios(NULL, "bad argument %s %s",
158 if (!(cp = *argp++) || *cp == '-')
159 adios(NULL, "missing argument to %s",
161 if ((volume = atoi(cp)) < 1)
162 adios(NULL, "bad argument %s %s",
167 if (*cp == '+' || *cp == '@') {
169 adios(NULL, "only one folder at a time!");
171 folder = getcpy(expandfol(cp));
180 strncpy(drft, buildsw ? toabsdir("draft")
181 : m_draft(seq_beyond), sizeof(drft));
183 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
187 ** Forwarding a message.
190 msgs[msgp++] = seq_cur;
192 folder = getcurfol();
193 maildir = toabsdir(folder);
195 if (chdir(maildir) == NOTOK)
196 adios(maildir, "unable to change directory to");
198 /* read folder and create message structure */
199 if (!(mp = folder_read(folder)))
200 adios(NULL, "unable to read folder %s", folder);
202 /* check for empty folder */
204 adios(NULL, "no messages in %s", folder);
206 /* parse all the message ranges/sequences and set SELECTED */
207 for (msgnum = 0; msgnum < msgp; msgnum++)
208 if (!m_convert(mp, msgs[msgnum]))
210 seq_setprev(mp); /* set the previous sequence */
212 if ((out = creat(drft, m_gmprot())) == NOTOK)
213 adios(drft, "unable to create");
215 /* Open form (component) file. */
218 snprintf(buf, sizeof(buf), IFORMAT, digest);
219 if (volume == 0 && (cp = context_find(buf))
220 && ((issue = atoi(cp)) < 0))
225 snprintf(buf, sizeof(buf), VFORMAT, digest);
226 if ((cp = context_find(buf)) == NULL ||
227 (volume = atoi(cp)) <= 0)
232 in = build_form(form, digest, volume, issue);
233 cpydata(in, out, form, drft);
236 fmtstr = new_fs(form, forwcomps);
237 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
238 adios(drft, "error writing");
246 snprintf(buf, sizeof(buf), IFORMAT, digest);
247 snprintf(value, sizeof(value), "%d", issue);
248 context_replace(buf, getcpy(value));
249 snprintf(buf, sizeof(buf), VFORMAT, digest);
250 snprintf(value, sizeof(value), "%d", volume);
251 context_replace(buf, getcpy(value));
254 context_replace(curfolder, folder); /* update current folder */
255 seq_setcur(mp, mp->lowsel); /* update current message */
256 seq_save(mp); /* synchronize sequences */
257 context_save(); /* save the context file */
261 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
262 anot ? "Forwarded" : NULL, cwd);
269 ** Create an attachment header for the to be forward messages.
272 add_forw_hdr(char *draft)
277 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
278 "-comp '%s' -text '+%s",
279 draft, attach_hdr, mp->foldpath);
280 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
281 if (!is_selected(mp, msgnum)) {
284 /* TODO: Check for buffer length! */
286 strcat(buffer, m_name(msgnum));
289 /* TODO: This check is bad, but better than nothing */
290 if (strlen(buffer) > BUFSIZ) {
291 adios(NULL, "Too long attachment header line. Forward less messages.");
293 if (system(buffer) != 0) {
294 advise(NULL, "unable to add attachment header");
300 build_form(char *form, char *digest, int volume, int issue)
304 register char *fmtstr;
305 char *line, tmpfil[BUFSIZ];
307 register struct comp *cptr;
312 /* Get new format string */
313 fmtstr = new_fs(form, NULL);
314 fmtsize = strlen(fmtstr) + 256;
316 /* Compile format string */
317 fmt_compile(fmtstr, &fmt);
319 FINDCOMP(cptr, "digest");
321 cptr->c_text = digest;
322 FINDCOMP(cptr, "date");
324 cptr->c_text = getcpy(dtimenow());
332 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
333 if (cp == NULL) adios("forw", "unable to create temporary file");
334 strncpy(tmpfil, cp, sizeof(tmpfil));
336 if ((in = dup(fileno(tmp))) == NOTOK)
337 adios("dup", "unable to");
339 line = mh_xmalloc((unsigned) fmtsize);
340 fmt_scan(fmt, line, fmtsize, dat);
344 adios(tmpfil, "error writing");
346 lseek(in, (off_t) 0, SEEK_SET);