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];
82 setlocale(LC_ALL, "");
84 invo_name = mhbasename(argv[0]);
86 /* read user profile/context */
89 arguments = getarguments(invo_name, argc, argv, 1);
92 while ((cp = *argp++)) {
94 switch (smatch(++cp, switches)) {
96 ambigsw(cp, switches);
99 adios(NULL, "-%s unknown", cp);
102 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
103 print_help(buf, switches, 1);
106 print_version(invo_name);
117 if (!(ed = *argp++) || *ed == '-')
118 adios(NULL, "missing argument to %s",
127 if (!(whatnowproc = *argp++) ||
129 adios(NULL, "missing argument to %s",
135 buildsw++; /* fall... */
142 if (!(form = *argp++) || *form == '-')
143 adios(NULL, "missing argument to %s",
148 if (!(digest = *argp++) || *digest == '-')
149 adios(NULL, "missing argument to %s",
154 if (!(cp = *argp++) || *cp == '-')
155 adios(NULL, "missing argument to %s",
157 if ((issue = atoi(cp)) < 1)
158 adios(NULL, "bad argument %s %s",
162 if (!(cp = *argp++) || *cp == '-')
163 adios(NULL, "missing argument to %s",
165 if ((volume = atoi(cp)) < 1)
166 adios(NULL, "bad argument %s %s",
171 if (*cp == '+' || *cp == '@') {
173 adios(NULL, "only one folder at a time!");
175 folder = getcpy(expandfol(cp));
184 strncpy(drft, buildsw ? toabsdir("draft")
185 : m_draft(seq_beyond), sizeof(drft));
187 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
191 ** Forwarding a message.
194 msgs[msgp++] = seq_cur;
196 folder = getcurfol();
197 maildir = toabsdir(folder);
199 if (chdir(maildir) == NOTOK)
200 adios(maildir, "unable to change directory to");
202 /* read folder and create message structure */
203 if (!(mp = folder_read(folder)))
204 adios(NULL, "unable to read folder %s", folder);
206 /* check for empty folder */
208 adios(NULL, "no messages in %s", folder);
210 /* parse all the message ranges/sequences and set SELECTED */
211 for (msgnum = 0; msgnum < msgp; msgnum++)
212 if (!m_convert(mp, msgs[msgnum]))
214 seq_setprev(mp); /* set the previous sequence */
216 /* Open form (component) file. */
219 snprintf(buf, sizeof(buf), IFORMAT, digest);
220 if (volume == 0 && (cp = context_find(buf))
221 && ((issue = atoi(cp)) < 0))
226 snprintf(buf, sizeof(buf), VFORMAT, digest);
227 if ((cp = context_find(buf)) == NULL ||
228 (volume = atoi(cp)) <= 0)
232 in = build_form(form, digest, volume, issue);
234 in = open_form(&form, forwcomps);
236 if ((out = creat(drft, m_gmprot())) == NOTOK)
237 adios(drft, "unable to create");
240 ** copy the components into the draft
242 cpydata(in, out, form, drft);
248 snprintf(buf, sizeof(buf), IFORMAT, digest);
249 snprintf(value, sizeof(value), "%d", issue);
250 context_replace(buf, getcpy(value));
251 snprintf(buf, sizeof(buf), VFORMAT, digest);
252 snprintf(value, sizeof(value), "%d", volume);
253 context_replace(buf, getcpy(value));
256 context_replace(curfolder, folder); /* update current folder */
257 seq_setcur(mp, mp->lowsel); /* update current message */
258 seq_save(mp); /* synchronize sequences */
259 context_save(); /* save the context file */
263 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
264 anot ? "Forwarded" : NULL, cwd);
271 ** Create an attachment header for the to be forward messages.
274 add_forw_hdr(char *draft)
279 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
280 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
281 if (!is_selected(mp, msgnum)) {
284 /* TODO: improve the code */
285 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
286 strncat(buffer, m_name(msgnum),
287 sizeof(buffer)-strlen(buffer)-1);
289 annotate(draft, attach_hdr, buffer, 0, -2, 1);
294 build_form(char *form, char *digest, int volume, int issue)
299 char *line, tmpfil[BUFSIZ];
301 register struct comp *cptr;
306 /* Get new format string */
307 nfs = new_fs(form, NULL, NULL);
308 fmtsize = strlen(nfs) + 256;
310 /* Compile format string */
311 fmt_compile(nfs, &fmt);
313 FINDCOMP(cptr, "digest");
315 cptr->c_text = digest;
316 FINDCOMP(cptr, "date");
318 cptr->c_text = getcpy(dtimenow(0));
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);