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