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