3 * dist.c -- re-distribute a message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 static struct swit switches[] = {
20 { "draftfolder +folder", 0 },
22 { "draftmessage msg", 0 },
24 { "nodraftfolder", 0 },
26 { "editor editor", 0 },
30 { "form formfile", 0 },
36 { "whatnowproc program", 0 },
38 { "nowhatnowproc", 0 },
44 { "file file", -4 }, /* interface from msh */
46 { "from address", 0 },
54 { "width columns", 0 },
58 static struct swit aqrnl[] = {
66 { "refile +folder", 0 },
73 static struct swit aqrl[] = {
77 { "refile +folder", 0 },
83 main (int argc, char **argv)
85 int anot = 0, inplace = 1, nedit = 0;
86 int nwhat = 0, i, in, isdf = 0, out;
87 int outputlinelen = OUTPUTLINELEN;
89 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
90 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
91 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
92 char *from = NULL, *to = NULL, *cc = NULL, *fcc = NULL;
93 char **argp, **arguments;
94 struct msgs *mp = NULL;
98 setlocale(LC_ALL, "");
100 invo_name = r1bindex (argv[0], '/');
102 /* read user profile/context */
105 arguments = getarguments (invo_name, argc, argv, 1);
108 while ((cp = *argp++)) {
110 switch (smatch (++cp, switches)) {
112 ambigsw (cp, switches);
115 adios (NULL, "-%s unknown", cp);
118 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
120 print_help (buf, switches, 1);
123 print_version(invo_name);
134 if (!(ed = *argp++) || *ed == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
143 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
153 adios (NULL, "only one file at a time!");
154 if (!(cp = *argp++) || *cp == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
156 file = path (cp, TFILE);
159 if (!(form = *argp++) || *form == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
172 adios (NULL, "only one draft folder at a time!");
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
176 *cp != '@' ? TFOLDER : TSUBCWF);
180 adios (NULL, "only one draft message at a time!");
181 if (!(dmsg = *argp++) || *dmsg == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
190 if (!(cp = *argp++) || *cp == '-')
191 adios (NULL, "missing argument to %s", argp[-2]);
192 from = addlist(from, cp);
195 if (!(cp = *argp++) || *cp == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
197 to = addlist(to, cp);
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
202 cc = addlist(cc, cp);
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
207 fcc = addlist(fcc, cp);
211 if (!(cp = *argp++) || *cp == '-')
212 adios (NULL, "missing argument to %s", argp[-2]);
213 if ((outputlinelen = atoi(cp)) < 10)
214 adios (NULL, "impossible width %d", outputlinelen);
218 if (*cp == '+' || *cp == '@') {
220 adios (NULL, "only one folder at a time!");
222 folder = pluspath (cp);
225 adios (NULL, "only one message at a time!");
231 cwd = getcpy (pwd ());
233 if (!context_find ("path"))
234 free (path ("./", TFOLDER));
235 if (file && (msg || folder))
236 adios (NULL, "can't mix files and folders/msgs");
239 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
241 /* Check if draft already exists */
242 if (stat (drft, &st) != NOTOK) {
243 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
244 for (i = LISTDSW; i != YESW;) {
245 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
247 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
256 showfile (++argp, drft);
259 if (refile (++argp, drft) == 0)
263 advise (NULL, "say what?");
273 anot = 0; /* don't want to annotate a file */
281 folder = getfolder (1);
282 maildir = m_maildir (folder);
284 if (chdir (maildir) == NOTOK)
285 adios (maildir, "unable to change directory to");
287 /* read folder and create message structure */
288 if (!(mp = folder_read (folder)))
289 adios (NULL, "unable to read folder %s", folder);
291 /* check for empty folder */
293 adios (NULL, "no messages in %s", folder);
295 /* parse the message range/sequence/name and set SELECTED */
296 if (!m_convert (mp, msg))
298 seq_setprev (mp); /* set the previous-sequence */
301 adios (NULL, "only one message at a time!");
304 msgnam = file ? file : getcpy (m_name (mp->lowsel));
306 dat[0] = mp ? mp->lowsel : 0;
309 dat[3] = outputlinelen;
315 in = build_form(form, NULL, dat, from, to, cc, fcc, NULL, msgnam);
317 if ((out = creat (drft, m_gmprot ())) == NOTOK)
318 adios (drft, "unable to create");
320 cpydata (in, out, form, drft);
324 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
325 adios (msgnam, "unable to open message");
328 context_replace (pfolder, folder);/* update current folder */
329 seq_setcur (mp, mp->lowsel); /* update current message */
330 seq_save (mp); /* synchronize sequences */
331 context_save (); /* save the context file */
336 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
337 anot ? "Resent" : NULL, inplace, cwd);