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;
45 setlocale(LC_ALL, "");
47 invo_name = mhbasename(argv[0]);
49 /* read user profile/context */
52 arguments = getarguments(invo_name, argc, argv, 1);
55 while ((cp = *argp++)) {
57 switch (smatch(++cp, switches)) {
59 ambigsw(cp, switches);
62 adios(NULL, "-%s unknown", cp);
65 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
66 print_help(buf, switches, 1);
69 print_version(invo_name);
80 if (!(ed = *argp++) || *ed == '-')
81 adios(NULL, "missing argument to %s",
86 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
87 adios(NULL, "missing argument to %s",
92 if (!(form = *argp++) || *form == '-')
93 adios(NULL, "missing argument to %s",
98 if (*cp == '+' || *cp == '@') {
100 adios(NULL, "only one folder at a time!");
102 folder = getcpy(expandfol(cp));
105 adios(NULL, "only one message at a time!");
113 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
114 if ((out = creat(drft, m_gmprot())) == NOTOK)
115 adios(drft, "unable to create");
117 fmtstr = new_fs(form, distcomps);
118 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
119 adios(drft, "error writing");
126 folder = getcurfol();
127 maildir = toabsdir(folder);
129 if (chdir(maildir) == NOTOK)
130 adios(maildir, "unable to change directory to");
132 if (!(mp = folder_read(folder)))
133 adios(NULL, "unable to read folder %s", folder);
135 /* check for empty folder */
137 adios(NULL, "no messages in %s", folder);
139 /* parse the message range/sequence/name and set SELECTED */
140 if (!m_convert(mp, msg))
145 adios(NULL, "only one message at a time!");
147 msgnam = getcpy(m_name(mp->lowsel));
148 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
149 adios(msgnam, "unable to open message");
151 context_replace(curfolder, folder);
152 seq_setcur(mp, mp->lowsel);
153 seq_save(mp); /* synchronize sequences */
156 what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);