Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / repl.c
1 /* repl.c - reply to a message */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: repl.c,v 1.9 1995/12/06 21:07:03 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #ifdef LOCALE
11 #include        <locale.h>
12 #endif
13
14 #ifndef MIME
15 #define MIMEminc(a)     (a)
16 #else   /* MIME */
17 #define MIMEminc(a)     0
18 #endif  /* MIME */
19
20 /* \f */
21
22 static struct swit switches[] = {
23 #define ANNOSW  0
24     "annotate", 0,
25 #define NANNOSW 1
26     "noannotate", 0,
27
28 #define CCSW    2
29     "cc type", 0,
30 #define NCCSW   3
31     "nocc type", 0,
32
33 #define DFOLDSW 4
34     "draftfolder +folder", 0,
35 #define DMSGSW  5
36     "draftmessage msg", 0,
37 #define NDFLDSW 6
38     "nodraftfolder", 0,
39
40 #define EDITRSW 7
41     "editor editor", 0,
42 #define NEDITSW 8
43     "noedit", 0,
44
45 #define FCCSW   9
46     "fcc folder", 0,
47
48 #define FILTSW  10
49     "filter filterfile", 0,
50 #define FORMSW  11
51     "form formfile", 0,
52
53 #define FRMTSW  12
54     "format", -5,
55 #define NFRMTSW 13
56     "noformat", -7,
57
58 #define INPLSW  14
59     "inplace", 0,
60 #define NINPLSW 15
61     "noinplace", 0,
62
63 #define MIMESW  16
64     "mime", MIMEminc(-4),
65 #define NMIMESW 17
66     "nomime", MIMEminc(-6),
67
68 #define QURYSW  18
69     "query", 0,
70 #define NQURYSW 19
71     "noquery", 0,
72
73 #define WHATSW  20
74     "whatnowproc program", 0,
75 #define NWHATSW 21
76     "nowhatnowproc", 0,
77
78 #define WIDTHSW 22
79     "width columns", 0,
80
81 #define HELPSW  23
82     "help", 4,
83
84 #define FILESW  24
85     "file file", -4,            /* interface from msh */
86
87 #ifdef  MHE
88 #define BILDSW  25
89     "build", -5,                /* interface from mhe */
90 #endif  /* MHE */
91
92     NULL, 0
93 };
94
95
96 static struct swit ccswitches[] = {
97 #define CTOSW   0
98     "to", 0,
99 #define CCCSW   1
100     "cc", 0,
101 #define CMESW   2
102     "me", 0,
103 #define CALSW   3
104     "all", 0,
105
106     NULL, 0
107 };
108
109 /* \f */
110
111 static struct swit aqrnl[] = {
112 #define NOSW    0
113     "quit", 0,
114 #define YESW    1
115     "replace", 0,
116 #define LISTDSW 2
117     "list", 0,
118 #define REFILSW 3
119     "refile +folder", 0,
120 #define NEWSW   4
121     "new", 0,
122
123     NULL, 0
124 };
125
126
127 static struct swit aqrl[] = {
128     "quit", 0,
129     "replace", 0,
130     "list", 0,
131     "refile +folder", 0,
132
133     NULL, 0
134 };
135
136 /* \f */
137
138 #ifndef ATHENA
139 #define CCDFLT  1
140 #else   /* ATHENA */
141 #define CCDFLT  0
142 #endif  /* ATHENA */
143
144 short   ccto = CCDFLT;          /* global for replsbr */
145 short   cccc = CCDFLT;
146 short   ccme = CCDFLT;
147 short   format = 1;
148 short   outputlinelen = OUTPUTLINELEN;
149 short   querysw = 0;
150
151 extern  int     mime;
152
153 char   *fcc = NULL;             /* global for replsbr */
154 char   *filter = NULL;
155 char   *form = NULL;
156
157 /* \f */
158
159 /* ARGSUSED */
160
161 main (argc, argv)
162 int     argc;
163 char   *argv[];
164 {
165     int     i,
166             isdf = 0,
167             anot = 0,
168             inplace = 0,
169 #ifdef  MHE
170             buildsw = 0,
171 #endif  /* MHE */
172             nedit = 0,
173             nwhat = 0;
174     char   *cp,
175            *cwd,
176            *dp,
177            *maildir,
178            *file = NULL,
179            *folder = NULL,
180            *msg = NULL,
181            *dfolder = NULL,
182            *dmsg = NULL,
183            *ed = NULL,
184             drft[BUFSIZ],
185             buf[100],
186           **ap,
187           **argp,
188            *arguments[MAXARGS];
189     struct msgs *mp = NULL;
190     struct stat st;
191     FILE        *in;
192
193 #ifdef LOCALE
194         setlocale(LC_ALL, "");
195 #endif
196     invo_name = r1bindex (argv[0], '/');
197     if ((cp = m_find (invo_name)) != NULL) {
198         ap = brkstring (cp = getcpy (cp), " ", "\n");
199         ap = copyip (ap, arguments);
200     }
201     else
202         ap = arguments;
203     (void) copyip (argv + 1, ap);
204     argp = arguments;
205
206 /* \f */
207
208     while (cp = *argp++) {
209         if (*cp == '-')
210             switch (smatch (++cp, switches)) {
211                 case AMBIGSW: 
212                     ambigsw (cp, switches);
213                     done (1);
214                 case UNKWNSW: 
215                     adios (NULLCP, "-%s unknown", cp);
216                 case HELPSW: 
217                     (void) sprintf (buf, "%s: [+folder] [msg] [switches]",
218                         invo_name);
219                     help (buf, switches);
220                     done (0);
221
222                 case ANNOSW: 
223                     anot++;
224                     continue;
225                 case NANNOSW: 
226                     anot = 0;
227                     continue;
228
229                 case CCSW: 
230                     if (!(cp = *argp++) || *cp == '-')
231                         adios (NULLCP, "missing argument to %s", argp[-2]);
232                     docc (cp, 1);
233                     continue;
234                 case NCCSW: 
235                     if (!(cp = *argp++) || *cp == '-')
236                         adios (NULLCP, "missing argument to %s", argp[-2]);
237                     docc (cp, 0);
238                     continue;
239
240                 case EDITRSW: 
241                     if (!(ed = *argp++) || *ed == '-')
242                         adios (NULLCP, "missing argument to %s", argp[-2]);
243                     nedit = 0;
244                     continue;
245                 case NEDITSW:
246                     nedit++;
247                     continue;
248                     
249                 case WHATSW: 
250                     if (!(whatnowproc = *argp++) || *whatnowproc == '-')
251                         adios (NULLCP, "missing argument to %s", argp[-2]);
252                     nwhat = 0;
253                     continue;
254 #ifdef  MHE
255                 case BILDSW: 
256                     buildsw++;  /* fall... */
257 #endif  /* MHE */
258                 case NWHATSW: 
259                     nwhat++;
260                     continue;
261
262                 case FCCSW: 
263                     if (!(cp = *argp++) || *cp == '-')
264                         adios (NULLCP, "missing argument to %s", argp[-2]);
265                     dp = NULL;
266                     if (*cp == '@')
267                         cp = dp = path (cp + 1, TSUBCWF);
268                     if (fcc)
269                         fcc = add (", ", fcc);
270                     fcc = add (cp, fcc);
271                     if (dp)
272                         free (dp);
273                     continue;
274
275                 case FILESW: 
276                     if (file)
277                         adios (NULLCP, "only one file at a time!");
278                     if (!(cp = *argp++) || *cp == '-')
279                         adios (NULLCP, "missing argument to %s", argp[-2]);
280                     file = path (cp, TFILE);
281                     continue;
282                 case FILTSW:
283                     if (!(cp = *argp++) || *cp == '-')
284                         adios (NULLCP, "missing argument to %s", argp[-2]);
285                     filter = getcpy (libpath (cp));
286                     mime = 0;
287                     continue;
288                 case FORMSW: 
289                     if (!(form = *argp++) || *form == '-')
290                         adios (NULLCP, "missing argument to %s", argp[-2]);
291                     continue;
292
293                 case FRMTSW: 
294                     format++;
295                     continue;
296                 case NFRMTSW: 
297                     format = 0;
298                     continue;
299
300                 case INPLSW: 
301                     inplace++;
302                     continue;
303                 case NINPLSW: 
304                     inplace = 0;
305                     continue;
306
307                 case MIMESW:
308 #ifdef  MIME
309                     mime++;
310                     filter = NULL;
311 #endif
312                     continue;
313                 case NMIMESW: 
314                     mime = 0;
315                     continue;
316
317                 case QURYSW: 
318                     querysw++;
319                     continue;
320                 case NQURYSW: 
321                     querysw = 0;
322                     continue;
323
324                 case WIDTHSW: 
325                     if (!(cp = *argp++) || *cp == '-')
326                         adios (NULLCP, "missing argument to %s", argp[-2]);
327                     if ((outputlinelen = atoi (cp)) < 10)
328                         adios (NULLCP, "impossible width %d", outputlinelen);
329                     continue;
330
331                 case DFOLDSW: 
332                     if (dfolder)
333                         adios (NULLCP, "only one draft folder at a time!");
334                     if (!(cp = *argp++) || *cp == '-')
335                         adios (NULLCP, "missing argument to %s", argp[-2]);
336                     dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
337                                     *cp != '@' ? TFOLDER : TSUBCWF);
338                     continue;
339                 case DMSGSW:
340                     if (dmsg)
341                         adios (NULLCP, "only one draft message at a time!");
342                     if (!(dmsg = *argp++) || *dmsg == '-')
343                         adios (NULLCP, "missing argument to %s", argp[-2]);
344                     continue;
345                 case NDFLDSW: 
346                     dfolder = NULL;
347                     isdf = NOTOK;
348                     continue;
349             }
350         if (*cp == '+' || *cp == '@') {
351             if (folder)
352                 adios (NULLCP, "only one folder at a time!");
353             else
354                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
355         }
356         else
357             if (msg)
358                 adios (NULLCP, "only one message at a time!");
359             else
360                 msg = cp;
361     }
362
363 /* \f */
364
365     cwd = getcpy (pwd ());
366
367     if (!m_find ("path"))
368         free (path ("./", TFOLDER));
369     if (file && (msg || folder))
370         adios (NULLCP, "can't mix files and folders/msgs");
371
372 try_it_again: ;
373 #ifndef MHE
374     (void) strcpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf));
375     if (stat (drft, &st) != NOTOK) {
376 #else   /* MHE */
377     (void) strcpy (drft, buildsw ? m_maildir ("reply")
378                           : m_draft (dfolder, NULLCP, NOUSE, &isdf));
379     if (!buildsw && stat (drft, &st) != NOTOK) {
380 #endif  /* MHE */
381         printf ("Draft \"%s\" exists (%ld bytes).", drft, st.st_size);
382         for (i = LISTDSW; i != YESW;) {
383             if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
384                 done (1);
385             switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
386                 case NOSW: 
387                     done (0);
388                 case NEWSW: 
389                     dmsg = NULL;
390                     goto try_it_again;
391                 case YESW: 
392                     break;
393                 case LISTDSW: 
394                     (void) showfile (++argp, drft);
395                     break;
396                 case REFILSW: 
397                     if (refile (++argp, drft) == 0)
398                         i = YESW;
399                     break;
400                 default: 
401                     advise (NULLCP, "say what?");
402                     break;
403             }
404         }
405     }
406
407 /* \f */
408
409     if (file) {
410         anot = 0;
411         goto go_to_it;
412     }
413
414     if (!msg)
415         msg = "cur";
416     if (!folder)
417         folder = m_getfolder ();
418     maildir = m_maildir (folder);
419
420     if (chdir (maildir) == NOTOK)
421         adios (maildir, "unable to change directory to");
422     if (!(mp = m_gmsg (folder)))
423         adios (NULLCP, "unable to read folder %s", folder);
424     if (mp -> hghmsg == 0)
425         adios (NULLCP, "no messages in %s", folder);
426
427     if (!m_convert (mp, msg))
428         done (1);
429     m_setseq (mp);
430
431     if (mp -> numsel > 1)
432         adios (NULLCP, "only one message at a time!");
433
434     m_replace (pfolder, folder);
435     if (mp -> lowsel != mp -> curmsg)
436         m_setcur (mp, mp -> lowsel);
437     m_sync (mp);
438     m_update ();
439
440 go_to_it: ;
441     msg = file ? file : getcpy (m_name (mp -> lowsel));
442
443     if ((in = fopen (msg, "r")) == NULL)
444         adios (msg, "unable to open");
445
446     replout (in, msg, drft, mp);
447     (void) fclose (in);
448
449     if (nwhat)
450         done (0);
451     (void) what_now (ed, nedit, NOUSE, drft, msg, 0, mp,
452             anot ? "Replied" : NULLCP, inplace, cwd);
453     done (1);
454 }
455
456 /* \f */
457
458 docc (cp, ccflag)
459 register char   *cp;
460 int     ccflag;
461 {
462     switch (smatch (cp, ccswitches)) {
463         case AMBIGSW: 
464             ambigsw (cp, ccswitches);
465             done (1);
466         case UNKWNSW: 
467             adios (NULLCP, "-%scc %s unknown", ccflag ? "" : "no", cp);
468
469         case CTOSW: 
470             ccto = ccflag;
471             break;
472
473         case CCCSW: 
474             cccc = ccflag;
475             break;
476
477         case CMESW: 
478             ccme = ccflag;
479             break;
480
481         case CALSW: 
482             ccto = cccc = ccme = ccflag;
483             break;
484     }
485 }