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