3 * dist.c -- re-distribute a message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 static struct swit switches[] = {
22 { "draftfolder +folder", 0 },
24 { "draftmessage msg", 0 },
26 { "nodraftfolder", 0 },
28 { "editor editor", 0 },
32 { "form formfile", 0 },
38 { "whatnowproc program", 0 },
40 { "nowhatnowproc", 0 },
46 { "file file", -4 }, /* interface from msh */
50 static struct swit aqrnl[] = {
58 { "refile +folder", 0 },
65 static struct swit aqrl[] = {
69 { "refile +folder", 0 },
75 main (int argc, char **argv)
77 int anot = 0, inplace = 1, nedit = 0;
78 int nwhat = 0, i, in, isdf = 0, out;
79 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
80 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
81 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
82 char **argp, **arguments;
83 struct msgs *mp = NULL;
87 setlocale(LC_ALL, "");
89 invo_name = r1bindex (argv[0], '/');
91 /* read user profile/context */
94 arguments = getarguments (invo_name, argc, argv, 1);
97 while ((cp = *argp++)) {
99 switch (smatch (++cp, switches)) {
101 ambigsw (cp, switches);
104 adios (NULL, "-%s unknown", cp);
107 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
109 print_help (buf, switches, 1);
112 print_version(invo_name);
123 if (!(ed = *argp++) || *ed == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
132 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
142 adios (NULL, "only one file at a time!");
143 if (!(cp = *argp++) || *cp == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
145 file = path (cp, TFILE);
148 if (!(form = *argp++) || *form == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
161 adios (NULL, "only one draft folder at a time!");
162 if (!(cp = *argp++) || *cp == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
164 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
165 *cp != '@' ? TFOLDER : TSUBCWF);
169 adios (NULL, "only one draft message at a time!");
170 if (!(dmsg = *argp++) || *dmsg == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
179 if (*cp == '+' || *cp == '@') {
181 adios (NULL, "only one folder at a time!");
183 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
186 adios (NULL, "only one message at a time!");
192 cwd = getcpy (pwd ());
194 if (!context_find ("path"))
195 free (path ("./", TFOLDER));
196 if (file && (msg || folder))
197 adios (NULL, "can't mix files and folders/msgs");
199 in = open_form(&form, distcomps);
202 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
204 /* Check if draft already exists */
205 if (stat (drft, &st) != NOTOK) {
206 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
207 for (i = LISTDSW; i != YESW;) {
208 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
210 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
219 showfile (++argp, drft);
222 if (refile (++argp, drft) == 0)
226 advise (NULL, "say what?");
231 if ((out = creat (drft, m_gmprot ())) == NOTOK)
232 adios (drft, "unable to create");
234 cpydata (in, out, form, drft);
242 anot = 0; /* don't want to annotate a file */
250 folder = getfolder (1);
251 maildir = m_maildir (folder);
253 if (chdir (maildir) == NOTOK)
254 adios (maildir, "unable to change directory to");
256 /* read folder and create message structure */
257 if (!(mp = folder_read (folder)))
258 adios (NULL, "unable to read folder %s", folder);
260 /* check for empty folder */
262 adios (NULL, "no messages in %s", folder);
264 /* parse the message range/sequence/name and set SELECTED */
265 if (!m_convert (mp, msg))
267 seq_setprev (mp); /* set the previous-sequence */
270 adios (NULL, "only one message at a time!");
273 msgnam = file ? file : getcpy (m_name (mp->lowsel));
274 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
275 adios (msgnam, "unable to open message");
278 context_replace (pfolder, folder);/* update current folder */
279 seq_setcur (mp, mp->lowsel); /* update current message */
280 seq_save (mp); /* synchronize sequences */
281 context_save (); /* save the context file */
286 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
287 anot ? "Resent" : NULL, inplace, cwd);