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.
15 static struct swit switches[] = {
21 { "editor editor", 0 },
23 { "form formfile", 0 },
25 { "whatnowproc program", 0 },
35 main(int argc, char **argv)
39 char *cp, *cwd, *maildir, *msgnam;
40 char *ed = NULL, *folder = NULL;
41 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
42 char **argp, **arguments;
43 struct msgs *mp = NULL;
46 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);
60 /* sysexits.h EX_USAGE*/
63 adios(NULL, "-%s unknown", cp);
66 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67 print_help(buf, switches, 1);
70 print_version(invo_name);
81 if (!(ed = *argp++) || *ed == '-')
82 adios(NULL, "missing argument to %s",
87 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
88 adios(NULL, "missing argument to %s",
93 if (!(form = *argp++) || *form == '-')
94 adios(NULL, "missing argument to %s",
99 if (*cp == '+' || *cp == '@') {
101 adios(NULL, "only one folder at a time!");
103 folder = getcpy(expandfol(cp));
106 adios(NULL, "only one message at a time!");
114 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
115 if ((out = creat(drft, m_gmprot())) == NOTOK)
116 adios(drft, "unable to create");
118 fmtstr = new_fs(form, distcomps);
119 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
120 adios(drft, "error writing");
127 folder = getcurfol();
128 maildir = toabsdir(folder);
130 if (chdir(maildir) == NOTOK)
131 adios(maildir, "unable to change directory to");
133 if (!(mp = folder_read(folder)))
134 adios(NULL, "unable to read folder %s", folder);
136 /* check for empty folder */
138 adios(NULL, "no messages in %s", folder);
140 /* parse the message range/sequence/name and set SELECTED */
141 if (!m_convert(mp, msg))
142 /* sysexits.h EX_USAGE*/
147 adios(NULL, "only one message at a time!");
149 msgnam = getcpy(m_name(mp->lowsel));
150 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
151 adios(msgnam, "unable to open message");
153 context_replace(curfolder, folder);
154 seq_setcur(mp, mp->lowsel);
155 seq_save(mp); /* synchronize sequences */
158 what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);
159 /*sysexits.h EX_SOFTWARE*/