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 },
29 { "whatnowproc program", 0 },
31 { "nowhatnowproc", 0 },
37 { "file file", -4 }, /* interface from msh */
43 main(int argc, char **argv)
45 int anot = 0, inplace = 1, nedit = 0;
46 int nwhat = 0, in, out;
47 char *cp, *cwd, *maildir, *msgnam;
48 char *ed = NULL, *file = NULL, *folder = NULL;
49 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
50 char **argp, **arguments;
51 struct msgs *mp = NULL;
54 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* read user profile/context */
61 arguments = getarguments(invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
71 adios(NULL, "-%s unknown", cp);
74 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
75 print_help(buf, switches, 1);
78 print_version(invo_name);
89 if (!(ed = *argp++) || *ed == '-')
90 adios(NULL, "missing argument to %s", argp[-2]);
98 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
99 adios(NULL, "missing argument to %s", argp[-2]);
108 adios(NULL, "only one file at a time!");
109 if (!(cp = *argp++) || *cp == '-')
110 adios(NULL, "missing argument to %s", argp[-2]);
111 file = getcpy(expanddir(cp));
114 if (!(form = *argp++) || *form == '-')
115 adios(NULL, "missing argument to %s", argp[-2]);
126 if (*cp == '+' || *cp == '@') {
128 adios(NULL, "only one folder at a time!");
130 folder = getcpy(expandfol(cp));
133 adios(NULL, "only one message at a time!");
141 if (file && (msg || folder))
142 adios(NULL, "can't mix files and folders/msgs");
144 in = open_form(&form, distcomps);
146 strncpy(drft, m_draft("new"), sizeof(drft));
148 if ((out = creat(drft, m_gmprot())) == NOTOK)
149 adios(drft, "unable to create");
151 cpydata(in, out, form, drft);
159 anot = 0; /* don't want to annotate a file */
167 folder = getcurfol();
168 maildir = toabsdir(folder);
170 if (chdir(maildir) == NOTOK)
171 adios(maildir, "unable to change directory to");
173 /* read folder and create message structure */
174 if (!(mp = folder_read(folder)))
175 adios(NULL, "unable to read folder %s", folder);
177 /* check for empty folder */
179 adios(NULL, "no messages in %s", folder);
181 /* parse the message range/sequence/name and set SELECTED */
182 if (!m_convert(mp, msg))
184 seq_setprev(mp); /* set the previous-sequence */
187 adios(NULL, "only one message at a time!");
190 msgnam = file ? file : getcpy(m_name(mp->lowsel));
191 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
192 adios(msgnam, "unable to open message");
195 context_replace(curfolder, folder); /* update current folder */
196 seq_setcur(mp, mp->lowsel); /* update current message */
197 seq_save(mp); /* synchronize sequences */
198 context_save(); /* save the context file */
203 what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,