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 },
35 main(int argc, char **argv)
37 int anot = 0, nedit = 0;
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;
47 setlocale(LC_ALL, "");
49 invo_name = mhbasename(argv[0]);
51 /* read user profile/context */
54 arguments = getarguments(invo_name, argc, argv, 1);
57 while ((cp = *argp++)) {
59 switch (smatch(++cp, switches)) {
61 ambigsw(cp, switches);
64 adios(NULL, "-%s unknown", cp);
67 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
68 print_help(buf, switches, 1);
71 print_version(invo_name);
82 if (!(ed = *argp++) || *ed == '-')
83 adios(NULL, "missing argument to %s",
92 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
93 adios(NULL, "missing argument to %s",
98 if (!(form = *argp++) || *form == '-')
99 adios(NULL, "missing argument to %s",
104 if (*cp == '+' || *cp == '@') {
106 adios(NULL, "only one folder at a time!");
108 folder = getcpy(expandfol(cp));
111 adios(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(drft, "unable to create");
123 fmtstr = new_fs(form, distcomps);
124 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
125 adios(drft, "error writing");
132 folder = getcurfol();
133 maildir = toabsdir(folder);
135 if (chdir(maildir) == NOTOK)
136 adios(maildir, "unable to change directory to");
138 if (!(mp = folder_read(folder)))
139 adios(NULL, "unable to read folder %s", folder);
141 /* check for empty folder */
143 adios(NULL, "no messages in %s", folder);
145 /* parse the message range/sequence/name and set SELECTED */
146 if (!m_convert(mp, msg))
151 adios(NULL, "only one message at a time!");
153 msgnam = getcpy(m_name(mp->lowsel));
154 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
155 adios(msgnam, "unable to open message");
157 context_replace(curfolder, folder);
158 seq_setcur(mp, mp->lowsel);
159 seq_save(mp); /* synchronize sequences */
162 what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp,
163 anot ? "Resent" : NULL, cwd);