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.
15 static struct swit switches[] = {
21 { "draftfolder +folder", 0 },
23 { "draftmessage msg", 0 },
25 { "nodraftfolder", 0 },
27 { "editor editor", 0 },
31 { "form formfile", 0 },
37 { "whatnowproc program", 0 },
39 { "nowhatnowproc", 0 },
45 { "file file", -4 }, /* interface from msh */
49 static struct swit aqrnl[] = {
57 { "refile +folder", 0 },
64 static struct swit aqrl[] = {
68 { "refile +folder", 0 },
74 main (int argc, char **argv)
76 int anot = 0, inplace = 1, nedit = 0;
77 int nwhat = 0, i, in, isdf = 0, out;
78 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
79 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
80 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
81 char **argp, **arguments;
82 struct msgs *mp = NULL;
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
96 while ((cp = *argp++)) {
98 switch (smatch (++cp, switches)) {
100 ambigsw (cp, switches);
103 adios (NULL, "-%s unknown", cp);
106 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
108 print_help (buf, switches, 1);
111 print_version(invo_name);
122 if (!(ed = *argp++) || *ed == '-')
123 adios (NULL, "missing argument to %s", argp[-2]);
131 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
141 adios (NULL, "only one file at a time!");
142 if (!(cp = *argp++) || *cp == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
144 file = path (cp, TFILE);
147 if (!(form = *argp++) || *form == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
160 adios (NULL, "only one draft folder at a time!");
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
164 *cp != '@' ? TFOLDER : TSUBCWF);
168 adios (NULL, "only one draft message at a time!");
169 if (!(dmsg = *argp++) || *dmsg == '-')
170 adios (NULL, "missing argument to %s", argp[-2]);
178 if (*cp == '+' || *cp == '@') {
180 adios (NULL, "only one folder at a time!");
182 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
185 adios (NULL, "only one message at a time!");
191 cwd = getcpy (pwd ());
193 if (!context_find ("path"))
194 free (path ("./", TFOLDER));
195 if (file && (msg || folder))
196 adios (NULL, "can't mix files and folders/msgs");
199 if ((in = open (etcpath (form), O_RDONLY)) == NOTOK)
200 adios (form, "unable to open form file");
202 if ((in = open (etcpath (distcomps), O_RDONLY)) == NOTOK)
203 adios (distcomps, "unable to open default components file");
208 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
210 /* Check if draft already exists */
211 if (stat (drft, &st) != NOTOK) {
212 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
213 for (i = LISTDSW; i != YESW;) {
214 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
216 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
225 showfile (++argp, drft);
228 if (refile (++argp, drft) == 0)
232 advise (NULL, "say what?");
237 if ((out = creat (drft, m_gmprot ())) == NOTOK)
238 adios (drft, "unable to create");
240 cpydata (in, out, form, drft);
248 anot = 0; /* don't want to annotate a file */
256 folder = getfolder (1);
257 maildir = m_maildir (folder);
259 if (chdir (maildir) == NOTOK)
260 adios (maildir, "unable to change directory to");
262 /* read folder and create message structure */
263 if (!(mp = folder_read (folder)))
264 adios (NULL, "unable to read folder %s", folder);
266 /* check for empty folder */
268 adios (NULL, "no messages in %s", folder);
270 /* parse the message range/sequence/name and set SELECTED */
271 if (!m_convert (mp, msg))
273 seq_setprev (mp); /* set the previous-sequence */
276 adios (NULL, "only one message at a time!");
279 msgnam = file ? file : getcpy (m_name (mp->lowsel));
280 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
281 adios (msgnam, "unable to open message");
284 context_replace (pfolder, folder);/* update current folder */
285 seq_setcur (mp, mp->lowsel); /* update current message */
286 seq_save (mp); /* synchronize sequences */
287 context_save (); /* save the context file */
292 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
293 anot ? "Resent" : NULL, inplace, cwd);