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 },
39 { "nowhatnowproc", 0 },
47 { "build", 5 }, /* interface from mhe */
53 static char drft[BUFSIZ];
54 static struct msgs *mp = NULL;
60 static void add_forw_hdr(char *);
61 static int build_form(char *, char *, int, int);
65 main(int argc, char **argv)
67 int msgp = 0, anot = 0;
68 int issue = 0, volume = 0;
69 int nedit = 0, nwhat = 0, in;
71 char *cp, *cwd, *maildir;
72 char *digest = NULL, *ed = NULL;
74 char *form = NULL, buf[BUFSIZ], value[10];
75 char **argp, **arguments, *msgs[MAXARGS];
83 setlocale(LC_ALL, "");
85 invo_name = mhbasename(argv[0]);
87 /* read user profile/context */
90 arguments = getarguments(invo_name, argc, argv, 1);
93 while ((cp = *argp++)) {
95 switch (smatch(++cp, switches)) {
97 ambigsw(cp, switches);
100 adios(NULL, "-%s unknown", cp);
103 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
104 print_help(buf, switches, 1);
107 print_version(invo_name);
118 if (!(ed = *argp++) || *ed == '-')
119 adios(NULL, "missing argument to %s",
128 if (!(whatnowproc = *argp++) ||
130 adios(NULL, "missing argument to %s",
136 buildsw++; /* fall... */
143 if (!(form = *argp++) || *form == '-')
144 adios(NULL, "missing argument to %s",
149 if (!(digest = *argp++) || *digest == '-')
150 adios(NULL, "missing argument to %s",
155 if (!(cp = *argp++) || *cp == '-')
156 adios(NULL, "missing argument to %s",
158 if ((issue = atoi(cp)) < 1)
159 adios(NULL, "bad argument %s %s",
163 if (!(cp = *argp++) || *cp == '-')
164 adios(NULL, "missing argument to %s",
166 if ((volume = atoi(cp)) < 1)
167 adios(NULL, "bad argument %s %s",
172 if (*cp == '+' || *cp == '@') {
174 adios(NULL, "only one folder at a time!");
176 folder = getcpy(expandfol(cp));
185 strncpy(drft, buildsw ? toabsdir("draft")
186 : m_draft(seq_beyond), sizeof(drft));
188 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
192 ** Forwarding a message.
195 msgs[msgp++] = seq_cur;
197 folder = getcurfol();
198 maildir = toabsdir(folder);
200 if (chdir(maildir) == NOTOK)
201 adios(maildir, "unable to change directory to");
203 /* read folder and create message structure */
204 if (!(mp = folder_read(folder)))
205 adios(NULL, "unable to read folder %s", folder);
207 /* check for empty folder */
209 adios(NULL, "no messages in %s", folder);
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum = 0; msgnum < msgp; msgnum++)
213 if (!m_convert(mp, msgs[msgnum]))
215 seq_setprev(mp); /* set the previous sequence */
217 if ((out = creat(drft, m_gmprot())) == NOTOK)
218 adios(drft, "unable to create");
220 /* Open form (component) file. */
223 snprintf(buf, sizeof(buf), IFORMAT, digest);
224 if (volume == 0 && (cp = context_find(buf))
225 && ((issue = atoi(cp)) < 0))
230 snprintf(buf, sizeof(buf), VFORMAT, digest);
231 if ((cp = context_find(buf)) == NULL ||
232 (volume = atoi(cp)) <= 0)
236 in = build_form(form, digest, volume, issue);
237 cpydata(in, out, form, drft);
240 fmtstr = new_fs(form, forwcomps);
241 if (write(out, fmtstr, strlen(fmtstr)) != strlen(fmtstr)) {
242 adios(drft, "error writing");
250 snprintf(buf, sizeof(buf), IFORMAT, digest);
251 snprintf(value, sizeof(value), "%d", issue);
252 context_replace(buf, getcpy(value));
253 snprintf(buf, sizeof(buf), VFORMAT, digest);
254 snprintf(value, sizeof(value), "%d", volume);
255 context_replace(buf, getcpy(value));
258 context_replace(curfolder, folder); /* update current folder */
259 seq_setcur(mp, mp->lowsel); /* update current message */
260 seq_save(mp); /* synchronize sequences */
261 context_save(); /* save the context file */
265 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
266 anot ? "Forwarded" : NULL, cwd);
273 ** Create an attachment header for the to be forward messages.
276 add_forw_hdr(char *draft)
281 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
282 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
283 if (!is_selected(mp, msgnum)) {
286 /* TODO: improve the code */
287 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
288 strncat(buffer, m_name(msgnum),
289 sizeof(buffer)-strlen(buffer)-1);
291 annotate(draft, attach_hdr, buffer, 0, -2, 1, 0);
296 build_form(char *form, char *digest, int volume, int issue)
300 register char *fmtstr;
301 char *line, tmpfil[BUFSIZ];
303 register struct comp *cptr;
308 /* Get new format string */
309 fmtstr = new_fs(form, NULL);
310 fmtsize = strlen(fmtstr) + 256;
312 /* Compile format string */
313 fmt_compile(fmtstr, &fmt);
315 FINDCOMP(cptr, "digest");
317 cptr->c_text = digest;
318 FINDCOMP(cptr, "date");
320 cptr->c_text = getcpy(dtimenow(0));
328 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
329 if (cp == NULL) adios("forw", "unable to create temporary file");
330 strncpy(tmpfil, cp, sizeof(tmpfil));
332 if ((in = dup(fileno(tmp))) == NOTOK)
333 adios("dup", "unable to");
335 line = mh_xmalloc((unsigned) fmtsize);
336 fmt_scan(fmt, line, fmtsize, dat);
340 adios(tmpfil, "error writing");
342 lseek(in, (off_t) 0, SEEK_SET);