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 */
48 static struct swit aqrnl[] = {
56 { "refile +folder", 0 },
63 static struct swit aqrl[] = {
67 { "refile +folder", 0 },
73 main (int argc, char **argv)
75 int anot = 0, inplace = 1, nedit = 0;
76 int nwhat = 0, i, in, isdf = 0, out;
77 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
78 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
79 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
80 char **argp, **arguments;
81 struct msgs *mp = NULL;
85 setlocale(LC_ALL, "");
87 invo_name = r1bindex (argv[0], '/');
89 /* read user profile/context */
92 arguments = getarguments (invo_name, argc, argv, 1);
95 while ((cp = *argp++)) {
97 switch (smatch (++cp, switches)) {
99 ambigsw (cp, switches);
102 adios (NULL, "-%s unknown", cp);
105 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
107 print_help (buf, switches, 1);
110 print_version(invo_name);
121 if (!(ed = *argp++) || *ed == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
130 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
131 adios (NULL, "missing argument to %s", argp[-2]);
140 adios (NULL, "only one file at a time!");
141 if (!(cp = *argp++) || *cp == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
143 file = path (cp, TFILE);
146 if (!(form = *argp++) || *form == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
159 adios (NULL, "only one draft folder at a time!");
160 if (!(cp = *argp++) || *cp == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
163 *cp != '@' ? TFOLDER : TSUBCWF);
167 adios (NULL, "only one draft message at a time!");
168 if (!(dmsg = *argp++) || *dmsg == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
177 if (*cp == '+' || *cp == '@') {
179 adios (NULL, "only one folder at a time!");
181 folder = pluspath (cp);
184 adios (NULL, "only one message at a time!");
190 cwd = getcpy (pwd ());
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER));
194 if (file && (msg || folder))
195 adios (NULL, "can't mix files and folders/msgs");
197 in = open_form(&form, distcomps);
200 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
202 /* Check if draft already exists */
203 if (stat (drft, &st) != NOTOK) {
204 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
205 for (i = LISTDSW; i != YESW;) {
206 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
208 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
217 showfile (++argp, drft);
220 if (refile (++argp, drft) == 0)
224 advise (NULL, "say what?");
229 if ((out = creat (drft, m_gmprot ())) == NOTOK)
230 adios (drft, "unable to create");
232 cpydata (in, out, form, drft);
240 anot = 0; /* don't want to annotate a file */
248 folder = getfolder (1);
249 maildir = m_maildir (folder);
251 if (chdir (maildir) == NOTOK)
252 adios (maildir, "unable to change directory to");
254 /* read folder and create message structure */
255 if (!(mp = folder_read (folder)))
256 adios (NULL, "unable to read folder %s", folder);
258 /* check for empty folder */
260 adios (NULL, "no messages in %s", folder);
262 /* parse the message range/sequence/name and set SELECTED */
263 if (!m_convert (mp, msg))
265 seq_setprev (mp); /* set the previous-sequence */
268 adios (NULL, "only one message at a time!");
271 msgnam = file ? file : getcpy (m_name (mp->lowsel));
272 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
273 adios (msgnam, "unable to open message");
276 context_replace (pfolder, folder);/* update current folder */
277 seq_setcur (mp, mp->lowsel); /* update current message */
278 seq_save (mp); /* synchronize sequences */
279 context_save (); /* save the context file */
284 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
285 anot ? "Resent" : NULL, inplace, cwd);