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