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;
51 setlocale(LC_ALL, "");
53 invo_name = mhbasename(argv[0]);
55 /* read user profile/context */
58 arguments = getarguments(invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
63 switch (smatch(++cp, switches)) {
65 ambigsw(cp, switches);
68 adios(NULL, "-%s unknown", cp);
71 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
72 print_help(buf, switches, 1);
75 print_version(invo_name);
86 if (!(ed = *argp++) || *ed == '-')
87 adios(NULL, "missing argument to %s",
96 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
97 adios(NULL, "missing argument to %s",
107 adios(NULL, "only one file at a time!");
108 if (!(cp = *argp++) || *cp == '-')
109 adios(NULL, "missing argument to %s",
111 file = getcpy(expanddir(cp));
114 if (!(form = *argp++) || *form == '-')
115 adios(NULL, "missing argument to %s",
120 if (*cp == '+' || *cp == '@') {
122 adios(NULL, "only one folder at a time!");
124 folder = getcpy(expandfol(cp));
127 adios(NULL, "only one message at a time!");
135 if (file && (msg || folder))
136 adios(NULL, "can't mix files and folders/msgs");
138 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
139 if ((out = creat(drft, m_gmprot())) == NOTOK)
140 adios(drft, "unable to create");
142 nfs = new_fs(form, distcomps);
143 if (write(out, nfs, strlen(nfs)) != strlen(nfs)) {
144 adios(drft, "error writing");
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,