]> diplodocus.org Git - nmh/blob - uip/dist.c
Simplified m_strn() per Ralph's suggestions.
[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 else
211 folder = pluspath (cp);
212 } else {
213 if (msg)
214 adios (NULL, "only one message at a time!");
215 else
216 msg = cp;
217 }
218 }
219
220 cwd = mh_xstrdup(pwd ());
221
222 if (!context_find ("path"))
223 free (path ("./", TFOLDER));
224 if (file && (msg || folder))
225 adios (NULL, "can't mix files and folders/msgs");
226
227 try_it_again:
228 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
229
230 /* Check if draft already exists */
231 if (stat (drft, &st) != NOTOK) {
232 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
233 for (i = LISTDSW; i != YESW;) {
234 if (!(argp = read_switch_multiword ("\nDisposition? ",
235 isdf ? aqrnl : aqrl)))
236 done (1);
237 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
238 case NOSW:
239 done (0);
240 case NEWSW:
241 dmsg = NULL;
242 goto try_it_again;
243 case YESW:
244 break;
245 case LISTDSW:
246 showfile (++argp, drft);
247 break;
248 case REFILSW:
249 if (refile (++argp, drft) == 0)
250 i = YESW;
251 break;
252 default:
253 inform("say what?");
254 break;
255 }
256 }
257 }
258
259 if (file) {
260 /*
261 * Dist a file
262 */
263 anot = 0; /* don't want to annotate a file */
264 } else {
265 /*
266 * Dist a message
267 */
268 if (!msg)
269 msg = "cur";
270 if (!folder)
271 folder = getfolder (1);
272 maildir = m_maildir (folder);
273
274 if (chdir (maildir) == NOTOK)
275 adios (maildir, "unable to change directory to");
276
277 /* read folder and create message structure */
278 if (!(mp = folder_read (folder, 1)))
279 adios (NULL, "unable to read folder %s", folder);
280
281 /* check for empty folder */
282 if (mp->nummsg == 0)
283 adios (NULL, "no messages in %s", folder);
284
285 /* parse the message range/sequence/name and set SELECTED */
286 if (!m_convert (mp, msg))
287 done (1);
288 seq_setprev (mp); /* set the previous-sequence */
289
290 if (mp->numsel > 1)
291 adios (NULL, "only one message at a time!");
292 }
293
294 msgnam = file ? file : mh_xstrdup(m_name (mp->lowsel));
295
296 dat[0] = mp ? mp->lowsel : 0;
297 dat[1] = 0;
298 dat[2] = 0;
299 dat[3] = outputlinelen;
300 dat[4] = 0;
301
302 if (!form)
303 form = distcomps;
304
305 in = build_form(form, NULL, dat, from, to, cc, fcc, NULL, msgnam);
306
307 if ((out = creat (drft, m_gmprot ())) == NOTOK)
308 adios (drft, "unable to create");
309
310 cpydata (in, out, form, drft);
311 close (in);
312 close (out);
313
314 if ((in = open (msgnam, O_RDONLY)) == NOTOK)
315 adios (msgnam, "unable to open message");
316
317 if (!file) {
318 context_replace (pfolder, folder);/* update current folder */
319 seq_setcur (mp, mp->lowsel); /* update current message */
320 seq_save (mp); /* synchronize sequences */
321 context_save (); /* save the context file */
322 }
323
324 if (nwhat)
325 done (0);
326 what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
327 inplace, cwd, atfile);
328 done (1);
329 return 1;
330 }