]> diplodocus.org Git - nmh/blob - uip/dist.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / dist.c
1 /* dist.c -- re-distribute a message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include "sbr/m_maildir.h"
11 #include <fcntl.h>
12 #include "h/done.h"
13 #include "forwsbr.h"
14
15 #define DIST_SWITCHES \
16 X("annotate", 0, ANNOSW) \
17 X("noannotate", 0, NANNOSW) \
18 X("draftfolder +folder", 0, DFOLDSW) \
19 X("draftmessage msg", 0, DMSGSW) \
20 X("nodraftfolder", 0, NDFLDSW) \
21 X("editor editor", 0, EDITRSW) \
22 X("noedit", 0, NEDITSW) \
23 X("form formfile", 0, FORMSW) \
24 X("inplace", 0, INPLSW) \
25 X("noinplace", 0, NINPLSW) \
26 X("whatnowproc program", 0, WHATSW) \
27 X("nowhatnowproc", 0, NWHATSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30 X("file file", -4, FILESW) \
31 X("from address", 0, FROMSW) \
32 X("to address", 0, TOSW) \
33 X("cc address", 0, CCSW) \
34 X("fcc mailbox", 0, FCCSW) \
35 X("width columns", 0, WIDTHSW) \
36 X("atfile", 0, ATFILESW) \
37 X("noatfile", 0, NOATFILESW) \
38
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(DIST);
41 #undef X
42
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(DIST, switches);
45 #undef X
46
47 #define DISPO_SWITCHES \
48 X("quit", 0, NOSW) \
49 X("replace", 0, YESW) \
50 X("list", 0, LISTDSW) \
51 X("refile +folder", 0, REFILSW) \
52 X("new", 0, NEWSW) \
53
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(DISPO);
56 #undef X
57
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
60 #undef X
61
62
63 static struct swit aqrl[] = {
64 { "quit", 0, NOSW },
65 { "replace", 0, YESW },
66 { "list", 0, LISTDSW },
67 { "refile +folder", 0, REFILSW },
68 { NULL, 0, 0 }
69 };
70
71
72 int
73 main (int argc, char **argv)
74 {
75 bool anot = false;
76 bool inplace = true;
77 bool nedit = false;
78 bool nwhat = false;
79 int i, in, isdf = 0, out;
80 int outputlinelen = OUTPUTLINELEN;
81 int dat[5];
82 bool atfile = false;
83 char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
84 char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
85 char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
86 char *from = NULL, *to = NULL, *cc = NULL, *fcc = NULL;
87 char **argp, **arguments;
88 struct msgs *mp = NULL;
89 struct stat st;
90
91 if (nmh_init(argv[0], true, true)) { return 1; }
92
93 arguments = getarguments (invo_name, argc, argv, 1);
94 argp = arguments;
95
96 while ((cp = *argp++)) {
97 if (*cp == '-') {
98 switch (smatch (++cp, switches)) {
99 case AMBIGSW:
100 ambigsw (cp, switches);
101 done (1);
102 case UNKWNSW:
103 die("-%s unknown", cp);
104
105 case HELPSW:
106 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
107 invo_name);
108 print_help (buf, switches, 1);
109 done (0);
110 case VERSIONSW:
111 print_version(invo_name);
112 done (0);
113
114 case ANNOSW:
115 anot = true;
116 continue;
117 case NANNOSW:
118 anot = false;
119 continue;
120
121 case EDITRSW:
122 if (!(ed = *argp++) || *ed == '-')
123 die("missing argument to %s", argp[-2]);
124 nedit = false;
125 continue;
126 case NEDITSW:
127 nedit = true;
128 continue;
129
130 case WHATSW:
131 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
132 die("missing argument to %s", argp[-2]);
133 nwhat = false;
134 continue;
135 case NWHATSW:
136 nwhat = true;
137 continue;
138
139 case FILESW:
140 if (file)
141 die("only one file at a time!");
142 if (!(cp = *argp++) || *cp == '-')
143 die("missing argument to %s", argp[-2]);
144 file = path (cp, TFILE);
145 continue;
146 case FORMSW:
147 if (!(form = *argp++) || *form == '-')
148 die("missing argument to %s", argp[-2]);
149 continue;
150
151 case INPLSW:
152 inplace = true;
153 continue;
154 case NINPLSW:
155 inplace = false;
156 continue;
157
158 case DFOLDSW:
159 if (dfolder)
160 die("only one draft folder at a time!");
161 if (!(cp = *argp++) || *cp == '-')
162 die("missing argument to %s", argp[-2]);
163 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
164 *cp != '@' ? TFOLDER : TSUBCWF);
165 continue;
166 case DMSGSW:
167 if (dmsg)
168 die("only one draft message at a time!");
169 if (!(dmsg = *argp++) || *dmsg == '-')
170 die("missing argument to %s", argp[-2]);
171 continue;
172 case NDFLDSW:
173 dfolder = NULL;
174 isdf = NOTOK;
175 continue;
176
177 case FROMSW:
178 if (!(cp = *argp++) || *cp == '-')
179 die("missing argument to %s", argp[-2]);
180 from = addlist(from, cp);
181 continue;
182 case TOSW:
183 if (!(cp = *argp++) || *cp == '-')
184 die("missing argument to %s", argp[-2]);
185 to = addlist(to, cp);
186 continue;
187 case CCSW:
188 if (!(cp = *argp++) || *cp == '-')
189 die("missing argument to %s", argp[-2]);
190 cc = addlist(cc, cp);
191 continue;
192 case FCCSW:
193 if (!(cp = *argp++) || *cp == '-')
194 die("missing argument to %s", argp[-2]);
195 fcc = addlist(fcc, cp);
196 continue;
197
198 case WIDTHSW:
199 if (!(cp = *argp++) || *cp == '-')
200 die("missing argument to %s", argp[-2]);
201 if ((outputlinelen = atoi(cp)) < 10)
202 die("impossible width %d", outputlinelen);
203 continue;
204
205 case ATFILESW:
206 atfile = true;
207 continue;
208 case NOATFILESW:
209 atfile = false;
210 continue;
211 }
212 }
213 if (*cp == '+' || *cp == '@') {
214 if (folder)
215 die("only one folder at a time!");
216 folder = pluspath (cp);
217 } else {
218 if (msg)
219 die("only one message at a time!");
220 msg = cp;
221 }
222 }
223
224 cwd = mh_xstrdup(pwd ());
225
226 if (!context_find ("path"))
227 free (path ("./", TFOLDER));
228 if (file && (msg || folder))
229 die("can't mix files and folders/msgs");
230
231 try_it_again:
232 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
233
234 /* Check if draft already exists */
235 if (stat (drft, &st) != NOTOK) {
236 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
237 for (i = LISTDSW; i != YESW;) {
238 if (!(argp = read_switch_multiword ("\nDisposition? ",
239 isdf ? aqrnl : aqrl)))
240 done (1);
241 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
242 case NOSW:
243 done (0);
244 case NEWSW:
245 dmsg = NULL;
246 goto try_it_again;
247 case YESW:
248 break;
249 case LISTDSW:
250 showfile (++argp, drft);
251 break;
252 case REFILSW:
253 if (refile (++argp, drft) == 0)
254 i = YESW;
255 break;
256 default:
257 inform("say what?");
258 break;
259 }
260 }
261 }
262
263 if (file) {
264 /*
265 * Dist a file
266 */
267 anot = false; /* don't want to annotate a file */
268 } else {
269 /*
270 * Dist a message
271 */
272 if (!msg)
273 msg = "cur";
274 if (!folder)
275 folder = getfolder (1);
276 maildir = m_maildir (folder);
277
278 if (chdir (maildir) == NOTOK)
279 adios (maildir, "unable to change directory to");
280
281 /* read folder and create message structure */
282 if (!(mp = folder_read (folder, 1)))
283 die("unable to read folder %s", folder);
284
285 /* check for empty folder */
286 if (mp->nummsg == 0)
287 die("no messages in %s", folder);
288
289 /* parse the message range/sequence/name and set SELECTED */
290 if (!m_convert (mp, msg))
291 done (1);
292 seq_setprev (mp); /* set the previous-sequence */
293
294 if (mp->numsel > 1)
295 die("only one message at a time!");
296 }
297
298 msgnam = file ? file : mh_xstrdup(m_name (mp->lowsel));
299
300 dat[0] = mp ? mp->lowsel : 0;
301 dat[1] = 0;
302 dat[2] = 0;
303 dat[3] = outputlinelen;
304 dat[4] = 0;
305
306 if (!form)
307 form = distcomps;
308
309 in = build_form(form, NULL, dat, from, to, cc, fcc, NULL, msgnam);
310
311 if ((out = creat (drft, m_gmprot ())) == NOTOK)
312 adios (drft, "unable to create");
313
314 cpydata (in, out, form, drft);
315 close (in);
316 close (out);
317
318 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
319 adios (msgnam, "unable to open message");
320
321 if (!file) {
322 context_replace (pfolder, folder);/* update current folder */
323 seq_setcur (mp, mp->lowsel); /* update current message */
324 seq_save (mp); /* synchronize sequences */
325 context_save (); /* save the context file */
326 }
327
328 if (nwhat)
329 done (0);
330 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
331 inplace, cwd, atfile);
332 done (1);
333 return 1;
334 }