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 },
33 { "file file", -4 }, /* interface from msh */
39 main(int argc, char **argv)
41 int anot = 0, nedit = 0;
42 int nwhat = 0, in, out;
43 char *cp, *cwd, *maildir, *msgnam;
44 char *ed = NULL, *file = NULL, *folder = NULL;
45 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
46 char **argp, **arguments;
47 struct msgs *mp = NULL;
50 setlocale(LC_ALL, "");
52 invo_name = mhbasename(argv[0]);
54 /* read user profile/context */
57 arguments = getarguments(invo_name, argc, argv, 1);
60 while ((cp = *argp++)) {
62 switch (smatch(++cp, switches)) {
64 ambigsw(cp, switches);
67 adios(NULL, "-%s unknown", cp);
70 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
71 print_help(buf, switches, 1);
74 print_version(invo_name);
85 if (!(ed = *argp++) || *ed == '-')
86 adios(NULL, "missing argument to %s",
95 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
96 adios(NULL, "missing argument to %s",
106 adios(NULL, "only one file at a time!");
107 if (!(cp = *argp++) || *cp == '-')
108 adios(NULL, "missing argument to %s",
110 file = getcpy(expanddir(cp));
113 if (!(form = *argp++) || *form == '-')
114 adios(NULL, "missing argument to %s",
119 if (*cp == '+' || *cp == '@') {
121 adios(NULL, "only one folder at a time!");
123 folder = getcpy(expandfol(cp));
126 adios(NULL, "only one message at a time!");
134 if (file && (msg || folder))
135 adios(NULL, "can't mix files and folders/msgs");
137 in = open_form(&form, distcomps);
139 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
141 if ((out = creat(drft, m_gmprot())) == NOTOK)
142 adios(drft, "unable to create");
144 cpydata(in, out, form, drft);
152 anot = 0; /* don't want to annotate a file */
160 folder = getcurfol();
161 maildir = toabsdir(folder);
163 if (chdir(maildir) == NOTOK)
164 adios(maildir, "unable to change directory to");
166 /* read folder and create message structure */
167 if (!(mp = folder_read(folder)))
168 adios(NULL, "unable to read folder %s", folder);
170 /* check for empty folder */
172 adios(NULL, "no messages in %s", folder);
174 /* parse the message range/sequence/name and set SELECTED */
175 if (!m_convert(mp, msg))
177 seq_setprev(mp); /* set the previous-sequence */
180 adios(NULL, "only one message at a time!");
183 msgnam = file ? file : getcpy(m_name(mp->lowsel));
184 if ((in = open(msgnam, O_RDONLY)) == NOTOK)
185 adios(msgnam, "unable to open message");
188 context_replace(curfolder, folder); /* update current folder */
189 seq_setcur(mp, mp->lowsel); /* update current message */
190 seq_save(mp); /* synchronize sequences */
191 context_save(); /* save the context file */
196 what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,