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