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 },
23 { "form formfile", 0 },
25 { "whatnowproc program", 0 },
27 { "nowhatnowproc", 0 },
37 main(int argc, char **argv)
39 int anot = 0, nedit = 0;
40 int nwhat = 0, in, out;
41 char *cp, *cwd, *maildir, *msgnam;
42 char *ed = NULL, *folder = NULL;
43 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
44 char **argp, **arguments;
45 struct msgs *mp = NULL;
49 setlocale(LC_ALL, "");
51 invo_name = mhbasename(argv[0]);
53 /* read user profile/context */
56 arguments = getarguments(invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
61 switch (smatch(++cp, switches)) {
63 ambigsw(cp, switches);
66 adios(NULL, "-%s unknown", cp);
69 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
70 print_help(buf, switches, 1);
73 print_version(invo_name);
84 if (!(ed = *argp++) || *ed == '-')
85 adios(NULL, "missing argument to %s",
94 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
95 adios(NULL, "missing argument to %s",
104 if (!(form = *argp++) || *form == '-')
105 adios(NULL, "missing argument to %s",
110 if (*cp == '+' || *cp == '@') {
112 adios(NULL, "only one folder at a time!");
114 folder = getcpy(expandfol(cp));
117 adios(NULL, "only one message at a time!");
125 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
126 if ((out = creat(drft, m_gmprot())) == NOTOK)
127 adios(drft, "unable to create");
129 fmtstr = new_fs(form, distcomps);
130 if (write(out, fmtstr, strlen(fmtstr)) != strlen(fmtstr)) {
131 adios(drft, "error writing");
138 folder = getcurfol();
139 maildir = toabsdir(folder);
141 if (chdir(maildir) == NOTOK)
142 adios(maildir, "unable to change directory to");
144 if (!(mp = folder_read(folder)))
145 adios(NULL, "unable to read folder %s", folder);
147 /* check for empty folder */
149 adios(NULL, "no messages in %s", folder);
151 /* parse the message range/sequence/name and set SELECTED */
152 if (!m_convert(mp, msg))
157 adios(NULL, "only one message at a time!");
159 msgnam = getcpy(m_name(mp->lowsel));
160 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
161 adios(msgnam, "unable to open message");
163 context_replace(curfolder, folder);
164 seq_setcur(mp, mp->lowsel);
165 seq_save(mp); /* synchronize sequences */
170 what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp,
171 anot ? "Resent" : NULL, cwd);