2 ** dist.c -- re-distribute a message
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.
13 static struct swit switches[] = {
19 { "editor editor", 0 },
21 { "form formfile", 0 },
23 { "whatnowproc program", 0 },
33 main(int argc, char **argv)
37 char *cp, *cwd, *maildir, *msgnam;
38 char *ed = NULL, *folder = NULL;
39 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
40 char **argp, **arguments;
41 struct msgs *mp = NULL;
44 setlocale(LC_ALL, "");
45 invo_name = mhbasename(argv[0]);
47 /* read user profile/context */
50 arguments = getarguments(invo_name, argc, argv, 1);
53 while ((cp = *argp++)) {
55 switch (smatch(++cp, switches)) {
57 ambigsw(cp, switches);
60 adios(NULL, "-%s unknown", cp);
63 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
64 print_help(buf, switches, 1);
67 print_version(invo_name);
78 if (!(ed = *argp++) || *ed == '-')
79 adios(NULL, "missing argument to %s",
84 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
85 adios(NULL, "missing argument to %s",
90 if (!(form = *argp++) || *form == '-')
91 adios(NULL, "missing argument to %s",
96 if (*cp == '+' || *cp == '@') {
98 adios(NULL, "only one folder at a time!");
100 folder = getcpy(expandfol(cp));
103 adios(NULL, "only one message at a time!");
111 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
112 if ((out = creat(drft, m_gmprot())) == NOTOK)
113 adios(drft, "unable to create");
115 fmtstr = new_fs(form, distcomps);
116 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
117 adios(drft, "error writing");
124 folder = getcurfol();
125 maildir = toabsdir(folder);
127 if (chdir(maildir) == NOTOK)
128 adios(maildir, "unable to change directory to");
130 if (!(mp = folder_read(folder)))
131 adios(NULL, "unable to read folder %s", folder);
133 /* check for empty folder */
135 adios(NULL, "no messages in %s", folder);
137 /* parse the message range/sequence/name and set SELECTED */
138 if (!m_convert(mp, msg))
143 adios(NULL, "only one message at a time!");
145 msgnam = getcpy(m_name(mp->lowsel));
146 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
147 adios(msgnam, "unable to open message");
149 context_replace(curfolder, folder);
150 seq_setcur(mp, mp->lowsel);
151 seq_save(mp); /* synchronize sequences */
154 what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);