Add/update copyright notice in all source code files.
[mmh] / uip / distsbr.c
1
2 /*
3  * distsbr.c -- routines to do additional "dist-style" processing
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14
15 static int  hdrfd = NOTOK;
16 static int  txtfd = NOTOK;
17
18 #define BADHDR  "please re-edit %s to remove the ``%s'' header!"
19 #define BADTXT  "please re-edit %s to consist of headers only!"
20 #define BADMSG  "please re-edit %s to include a ``Resent-To:''!"
21 #define BADRFT  "please re-edit %s and fix that header!"
22
23 /*
24  * static prototypes
25  */
26 static void ready_msg(char *);
27
28 int
29 distout (char *drft, char *msgnam, char *backup)
30 {
31     int state;
32     register char *dp, *resent;
33     char name[NAMESZ], buffer[BUFSIZ];
34     register FILE *ifp, *ofp;
35
36     if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
37         adios (backup, "unable to rename %s to",drft);
38     if ((ifp = fopen (backup, "r")) == NULL)
39         adios (backup, "unable to read");
40
41     if ((ofp = fopen (drft, "w")) == NULL)
42         adios (drft, "unable to create temporary file");
43     chmod (drft, m_gmprot ());
44
45     ready_msg (msgnam);
46     lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
47     cpydata (hdrfd, fileno (ofp), msgnam, drft);
48
49     for (state = FLD, resent = NULL;;)
50         switch (state =
51                 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
52             case FLD: 
53             case FLDPLUS: 
54             case FLDEOF: 
55                 if (uprf (name, "distribute-"))
56                     snprintf (name, sizeof(name), "%s%s", "Resent", &name[10]);
57                 if (uprf (name, "distribution-"))
58                     snprintf (name, sizeof(name), "%s%s", "Resent", &name[12]);
59                 if (!uprf (name, "resent")) {
60                     advise (NULL, BADHDR, "draft", name);
61                     goto leave_bad;
62                 }
63                 if (state == FLD)
64                     resent = add (":", add (name, resent));
65                 resent = add (buffer, resent);
66                 fprintf (ofp, "%s: %s", name, buffer);
67                 while (state == FLDPLUS) {
68                     state = m_getfld (state, name,
69                             buffer, sizeof buffer, ifp);
70                     resent = add (buffer, resent);
71                     fputs (buffer, ofp);
72                 }
73                 if (state == FLDEOF)
74                     goto process;
75                 break;
76
77             case BODY: 
78             case BODYEOF: 
79                 for (dp = buffer; *dp; dp++)
80                     if (!isspace (*dp)) {
81                         advise (NULL, BADTXT, "draft");
82                         goto leave_bad;
83                     }
84
85             case FILEEOF: 
86                 goto process;
87
88             case LENERR: 
89             case FMTERR: 
90                 advise (NULL, BADRFT, "draft");
91         leave_bad: ;
92                 fclose (ifp);
93                 fclose (ofp);
94                 unlink (drft);
95                 if (rename (backup, drft) == NOTOK)
96                     adios (drft, "unable to rename %s to", backup);
97                 return NOTOK;
98
99             default: 
100                 adios (NULL, "getfld() returned %d", state);
101         }
102 process: ;
103     fclose (ifp);
104     fflush (ofp);
105
106     if (!resent) {
107         advise (NULL, BADMSG, "draft");
108         fclose (ofp);
109         unlink (drft);
110         if (rename (backup, drft) == NOTOK)
111             adios (drft, "unable to rename %s to", backup);
112         return NOTOK;
113     }
114     free (resent);
115
116     if (txtfd != NOTOK) {
117         lseek (txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
118         cpydata (txtfd, fileno (ofp), msgnam, drft);
119     }
120
121     fclose (ofp);
122
123     return OK;
124 }
125
126
127 static void
128 ready_msg (char *msgnam)
129 {
130     int state, out;
131     char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ];
132     register FILE *ifp, *ofp;
133
134     if (hdrfd != NOTOK)
135         close (hdrfd), hdrfd = NOTOK;
136     if (txtfd != NOTOK)
137         close (txtfd), txtfd = NOTOK;
138
139     if ((ifp = fopen (msgnam, "r")) == NULL)
140         adios (msgnam, "unable to open message");
141
142     strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
143     if ((hdrfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
144         adios (tmpfil, "unable to re-open temporary file");
145     if ((out = dup (hdrfd)) == NOTOK
146             || (ofp = fdopen (out, "w")) == NULL)
147         adios (NULL, "no file descriptors -- you lose big");
148     unlink (tmpfil);
149
150     for (state = FLD;;)
151         switch (state =
152                 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
153             case FLD: 
154             case FLDPLUS: 
155             case FLDEOF: 
156                 if (uprf (name, "resent"))
157                     fprintf (ofp, "Prev-");
158                 fprintf (ofp, "%s: %s", name, buffer);
159                 while (state == FLDPLUS) {
160                     state = m_getfld (state, name,
161                             buffer, sizeof buffer, ifp);
162                     fputs (buffer, ofp);
163                 }
164                 if (state == FLDEOF)
165                     goto process;
166                 break;
167
168             case BODY: 
169             case BODYEOF: 
170                 fclose (ofp);
171
172                 strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
173                 if ((txtfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
174                     adios (tmpfil, "unable to open temporary file");
175                 if ((out = dup (txtfd)) == NOTOK
176                         || (ofp = fdopen (out, "w")) == NULL)
177                     adios (NULL, "no file descriptors -- you lose big");
178                 unlink (tmpfil);
179                 fprintf (ofp, "\n%s", buffer);
180                 while (state == BODY) {
181                     state = m_getfld (state, name,
182                             buffer, sizeof buffer, ifp);
183                     fputs (buffer, ofp);
184                 }
185             case FILEEOF: 
186                 goto process;
187
188             case LENERR: 
189             case FMTERR: 
190                 adios (NULL, "format error in message %s", msgnam);
191
192             default: 
193                 adios (NULL, "getfld() returned %d", state);
194         }
195 process: ;
196     fclose (ifp);
197     fclose (ofp);
198 }