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