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 },
62 static struct swit aqrnl[] = {
70 { "refile +folder", 0 },
77 static struct swit aqrl[] = {
81 { "refile +folder", 0 },
87 main (int argc, char **argv)
89 int anot = 0, inplace = 1, nedit = 0;
90 int nwhat = 0, i, in, isdf = 0, out;
91 int outputlinelen = OUTPUTLINELEN;
92 int dat[5], atfile = 1;
93 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
94 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
95 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
96 char *from = NULL, *to = NULL, *cc = NULL, *fcc = NULL;
97 char **argp, **arguments;
98 struct msgs *mp = NULL;
102 setlocale(LC_ALL, "");
104 invo_name = r1bindex (argv[0], '/');
106 /* read user profile/context */
109 arguments = getarguments (invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch (++cp, switches)) {
116 ambigsw (cp, switches);
119 adios (NULL, "-%s unknown", cp);
122 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
124 print_help (buf, switches, 1);
127 print_version(invo_name);
138 if (!(ed = *argp++) || *ed == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
147 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
157 adios (NULL, "only one file at a time!");
158 if (!(cp = *argp++) || *cp == '-')
159 adios (NULL, "missing argument to %s", argp[-2]);
160 file = path (cp, TFILE);
163 if (!(form = *argp++) || *form == '-')
164 adios (NULL, "missing argument to %s", argp[-2]);
176 adios (NULL, "only one draft folder at a time!");
177 if (!(cp = *argp++) || *cp == '-')
178 adios (NULL, "missing argument to %s", argp[-2]);
179 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
180 *cp != '@' ? TFOLDER : TSUBCWF);
184 adios (NULL, "only one draft message at a time!");
185 if (!(dmsg = *argp++) || *dmsg == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
194 if (!(cp = *argp++) || *cp == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
196 from = addlist(from, cp);
199 if (!(cp = *argp++) || *cp == '-')
200 adios (NULL, "missing argument to %s", argp[-2]);
201 to = addlist(to, cp);
204 if (!(cp = *argp++) || *cp == '-')
205 adios (NULL, "missing argument to %s", argp[-2]);
206 cc = addlist(cc, cp);
209 if (!(cp = *argp++) || *cp == '-')
210 adios (NULL, "missing argument to %s", argp[-2]);
211 fcc = addlist(fcc, cp);
215 if (!(cp = *argp++) || *cp == '-')
216 adios (NULL, "missing argument to %s", argp[-2]);
217 if ((outputlinelen = atoi(cp)) < 10)
218 adios (NULL, "impossible width %d", outputlinelen);
229 if (*cp == '+' || *cp == '@') {
231 adios (NULL, "only one folder at a time!");
233 folder = pluspath (cp);
236 adios (NULL, "only one message at a time!");
242 cwd = getcpy (pwd ());
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER));
246 if (file && (msg || folder))
247 adios (NULL, "can't mix files and folders/msgs");
250 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
252 /* Check if draft already exists */
253 if (stat (drft, &st) != NOTOK) {
254 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
255 for (i = LISTDSW; i != YESW;) {
256 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
258 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
267 showfile (++argp, drft);
270 if (refile (++argp, drft) == 0)
274 advise (NULL, "say what?");
284 anot = 0; /* don't want to annotate a file */
292 folder = getfolder (1);
293 maildir = m_maildir (folder);
295 if (chdir (maildir) == NOTOK)
296 adios (maildir, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp = folder_read (folder)))
300 adios (NULL, "unable to read folder %s", folder);
302 /* check for empty folder */
304 adios (NULL, "no messages in %s", folder);
306 /* parse the message range/sequence/name and set SELECTED */
307 if (!m_convert (mp, msg))
309 seq_setprev (mp); /* set the previous-sequence */
312 adios (NULL, "only one message at a time!");
315 msgnam = file ? file : getcpy (m_name (mp->lowsel));
317 dat[0] = mp ? mp->lowsel : 0;
320 dat[3] = outputlinelen;
326 in = build_form(form, NULL, dat, from, to, cc, fcc, NULL, msgnam);
328 if ((out = creat (drft, m_gmprot ())) == NOTOK)
329 adios (drft, "unable to create");
331 cpydata (in, out, form, drft);
335 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
336 adios (msgnam, "unable to open message");
339 context_replace (pfolder, folder);/* update current folder */
340 seq_setcur (mp, mp->lowsel); /* update current message */
341 seq_save (mp); /* synchronize sequences */
342 context_save (); /* save the context file */
347 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
348 inplace, cwd, atfile);