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