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 { "draftfolder +folder", 0 },
21 { "draftmessage msg", 0 },
23 { "nodraftfolder", 0 },
25 { "editor editor", 0 },
29 { "form formfile", 0 },
35 { "whatnowproc program", 0 },
37 { "nowhatnowproc", 0 },
43 { "file file", -4 }, /* interface from msh */
47 static struct swit aqrnl[] = {
55 { "refile +folder", 0 },
62 static struct swit aqrl[] = {
66 { "refile +folder", 0 },
72 main (int argc, char **argv)
74 int anot = 0, inplace = 1, nedit = 0;
75 int nwhat = 0, i, in, isdf = 0, out;
76 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
77 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
78 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
79 char **argp, **arguments;
80 struct msgs *mp = NULL;
84 setlocale(LC_ALL, "");
86 invo_name = r1bindex (argv[0], '/');
88 /* read user profile/context */
91 arguments = getarguments (invo_name, argc, argv, 1);
94 while ((cp = *argp++)) {
96 switch (smatch (++cp, switches)) {
98 ambigsw (cp, switches);
101 adios (NULL, "-%s unknown", cp);
104 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
106 print_help (buf, switches, 1);
109 print_version(invo_name);
120 if (!(ed = *argp++) || *ed == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
129 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
139 adios (NULL, "only one file at a time!");
140 if (!(cp = *argp++) || *cp == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 file = path (cp, TFILE);
145 if (!(form = *argp++) || *form == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
158 adios (NULL, "only one draft folder at a time!");
159 if (!(cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
162 *cp != '@' ? TFOLDER : TSUBCWF);
166 adios (NULL, "only one draft message at a time!");
167 if (!(dmsg = *argp++) || *dmsg == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
176 if (*cp == '+' || *cp == '@') {
178 adios (NULL, "only one folder at a time!");
180 folder = pluspath (cp);
183 adios (NULL, "only one message at a time!");
189 cwd = getcpy (pwd ());
191 if (!context_find ("path"))
192 free (path ("./", TFOLDER));
193 if (file && (msg || folder))
194 adios (NULL, "can't mix files and folders/msgs");
196 in = open_form(&form, distcomps);
199 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
201 /* Check if draft already exists */
202 if (stat (drft, &st) != NOTOK) {
203 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
204 for (i = LISTDSW; i != YESW;) {
205 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
207 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
216 showfile (++argp, drft);
219 if (refile (++argp, drft) == 0)
223 advise (NULL, "say what?");
228 if ((out = creat (drft, m_gmprot ())) == NOTOK)
229 adios (drft, "unable to create");
231 cpydata (in, out, form, drft);
239 anot = 0; /* don't want to annotate a file */
247 folder = getfolder (1);
248 maildir = m_maildir (folder);
250 if (chdir (maildir) == NOTOK)
251 adios (maildir, "unable to change directory to");
253 /* read folder and create message structure */
254 if (!(mp = folder_read (folder)))
255 adios (NULL, "unable to read folder %s", folder);
257 /* check for empty folder */
259 adios (NULL, "no messages in %s", folder);
261 /* parse the message range/sequence/name and set SELECTED */
262 if (!m_convert (mp, msg))
264 seq_setprev (mp); /* set the previous-sequence */
267 adios (NULL, "only one message at a time!");
270 msgnam = file ? file : getcpy (m_name (mp->lowsel));
271 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
272 adios (msgnam, "unable to open message");
275 context_replace (pfolder, folder); /* update current folder */
276 seq_setcur (mp, mp->lowsel); /* update current message */
277 seq_save (mp); /* synchronize sequences */
278 context_save (); /* save the context file */
283 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
284 anot ? "Resent" : NULL, inplace, cwd);