2 * dist.c -- re-distribute a message
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 static struct swit switches[] = {
19 { "editor editor", 0 },
23 { "form formfile", 0 },
29 { "whatnowproc program", 0 },
31 { "nowhatnowproc", 0 },
37 { "file file", -4 }, /* interface from msh */
43 main (int argc, char **argv)
45 int anot = 0, inplace = 1, nedit = 0;
46 int nwhat = 0, in, out;
47 char *cp, *cwd, *maildir, *msgnam;
48 char *ed = NULL, *file = NULL, *folder = NULL;
49 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
50 char **argp, **arguments;
51 struct msgs *mp = NULL;
54 setlocale(LC_ALL, "");
56 invo_name = r1bindex (argv[0], '/');
58 /* read user profile/context */
61 arguments = getarguments (invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch (++cp, switches)) {
68 ambigsw (cp, switches);
71 adios (NULL, "-%s unknown", cp);
74 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
76 print_help (buf, switches, 1);
79 print_version(invo_name);
90 if (!(ed = *argp++) || *ed == '-')
91 adios (NULL, "missing argument to %s", argp[-2]);
99 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
109 adios (NULL, "only one file at a time!");
110 if (!(cp = *argp++) || *cp == '-')
111 adios (NULL, "missing argument to %s", argp[-2]);
112 file = path (cp, TFILE);
115 if (!(form = *argp++) || *form == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
127 if (*cp == '+' || *cp == '@') {
129 adios (NULL, "only one folder at a time!");
131 folder = pluspath (cp);
134 adios (NULL, "only one message at a time!");
140 cwd = getcpy (pwd ());
142 if (!context_find ("path"))
143 free (path ("./", TFOLDER));
144 if (file && (msg || folder))
145 adios (NULL, "can't mix files and folders/msgs");
147 in = open_form(&form, distcomps);
149 strncpy (drft, m_draft("new"), sizeof(drft));
151 if ((out = creat (drft, m_gmprot ())) == NOTOK)
152 adios (drft, "unable to create");
154 cpydata (in, out, form, drft);
162 anot = 0; /* don't want to annotate a file */
170 folder = getfolder (1);
171 maildir = m_maildir (folder);
173 if (chdir (maildir) == NOTOK)
174 adios (maildir, "unable to change directory to");
176 /* read folder and create message structure */
177 if (!(mp = folder_read (folder)))
178 adios (NULL, "unable to read folder %s", folder);
180 /* check for empty folder */
182 adios (NULL, "no messages in %s", folder);
184 /* parse the message range/sequence/name and set SELECTED */
185 if (!m_convert (mp, msg))
187 seq_setprev (mp); /* set the previous-sequence */
190 adios (NULL, "only one message at a time!");
193 msgnam = file ? file : getcpy (m_name (mp->lowsel));
194 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
195 adios (msgnam, "unable to open message");
198 context_replace (pfolder, folder); /* update current folder */
199 seq_setcur (mp, mp->lowsel); /* update current message */
200 seq_save (mp); /* synchronize sequences */
201 context_save (); /* save the context file */
206 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
207 anot ? "Resent" : NULL, inplace, cwd);