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