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.
16 static struct swit switches[] = {
22 { "editor editor", 0 },
24 { "form formfile", 0 },
26 { "whatnowproc program", 0 },
36 main(int argc, char **argv)
40 char *cp, *cwd, *maildir, *msgnam;
41 char *ed = NULL, *folder = NULL;
42 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
43 char **argp, **arguments;
44 struct msgs *mp = NULL;
47 setlocale(LC_ALL, "");
48 invo_name = mhbasename(argv[0]);
50 /* read user profile/context */
53 arguments = getarguments(invo_name, argc, argv, 1);
56 while ((cp = *argp++)) {
58 switch (smatch(++cp, switches)) {
60 ambigsw(cp, switches);
63 adios(EX_USAGE, NULL, "-%s unknown", cp);
66 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67 print_help(buf, switches, 1);
68 exit(argc == 2 ? EX_OK : EX_USAGE);
71 print_version(invo_name);
72 exit(argc == 2 ? EX_OK : EX_USAGE);
82 if (!(ed = *argp++) || *ed == '-')
83 adios(EX_USAGE, NULL, "missing argument to %s",
88 if (!(whatnowproc = *argp++) || *whatnowproc == '-') {
89 adios(EX_USAGE, NULL, "missing argument to %s",
95 if (!(form = *argp++) || *form == '-') {
96 adios(EX_USAGE, NULL, "missing argument to %s",
102 if (*cp == '+' || *cp == '@') {
104 adios(EX_USAGE, NULL, "only one folder at a time!");
106 folder = getcpy(expandfol(cp));
110 adios(EX_USAGE, NULL, "only one message at a time!");
119 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
120 if ((out = creat(drft, m_gmprot())) == NOTOK) {
121 adios(EX_CANTCREAT, drft, "unable to create");
124 fmtstr = new_fs(form, distcomps);
125 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
126 adios(EX_IOERR, drft, "error writing");
134 folder = getcurfol();
136 maildir = toabsdir(folder);
138 if (chdir(maildir) == NOTOK) {
139 adios(EX_OSERR, maildir, "unable to change directory to");
142 if (!(mp = folder_read(folder))) {
143 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
146 /* check for empty folder */
147 if (mp->nummsg == 0) {
148 adios(EX_NOINPUT, NULL, "no messages in %s", folder);
151 /* parse the message range/sequence/name and set SELECTED */
152 if (!m_convert(mp, msg)) {
157 if (mp->numsel > 1) {
158 adios(EX_USAGE, NULL, "only one message at a time!");
161 msgnam = getcpy(m_name(mp->lowsel));
162 if ((in = open(msgnam, O_RDONLY)) == NOTOK) {
163 adios(EX_IOERR, msgnam, "unable to open message");
166 context_replace(curfolder, folder);
167 seq_setcur(mp, mp->lowsel);
168 seq_save(mp); /* synchronize sequences */
171 what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);