Remove RCS keywords, since they no longer work after git migration.
[mmh] / uip / distsbr.c
1
2 /*
3  * distsbr.c -- routines to do additional "dist-style" processing
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/utils.h>
13
14 static int  hdrfd = NOTOK;
15 static int  txtfd = NOTOK;
16
17 #define BADHDR  "please re-edit %s to remove the ``%s'' header!"
18 #define BADTXT  "please re-edit %s to consist of headers only!"
19 #define BADMSG  "please re-edit %s to include a ``Resent-To:''!"
20 #define BADRFT  "please re-edit %s and fix that header!"
21
22 /*
23  * static prototypes
24  */
25 static void ready_msg(char *);
26
27 int
28 distout (char *drft, char *msgnam, char *backup)
29 {
30     int state;
31     register unsigned char *dp;
32     register char *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     char *cp = NULL;
134
135     if (hdrfd != NOTOK)
136         close (hdrfd), hdrfd = NOTOK;
137     if (txtfd != NOTOK)
138         close (txtfd), txtfd = NOTOK;
139
140     if ((ifp = fopen (msgnam, "r")) == NULL)
141         adios (msgnam, "unable to open message");
142
143     cp = m_mktemp2(NULL, "dist", &hdrfd, NULL);
144     if (cp == NULL) {
145         adios("distsbr", "unable to create temporary file");
146     }
147     fchmod(hdrfd, 0600);
148     strncpy(tmpfil, cp, sizeof(tmpfil));
149     if ((out = dup (hdrfd)) == NOTOK
150             || (ofp = fdopen (out, "w")) == NULL)
151         adios (NULL, "no file descriptors -- you lose big");
152     unlink (tmpfil);
153
154     for (state = FLD;;)
155         switch (state =
156                 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
157             case FLD: 
158             case FLDPLUS: 
159             case FLDEOF: 
160                 if (uprf (name, "resent"))
161                     fprintf (ofp, "Prev-");
162                 fprintf (ofp, "%s: %s", name, buffer);
163                 while (state == FLDPLUS) {
164                     state = m_getfld (state, name,
165                             buffer, sizeof buffer, ifp);
166                     fputs (buffer, ofp);
167                 }
168                 if (state == FLDEOF)
169                     goto process;
170                 break;
171
172             case BODY: 
173             case BODYEOF: 
174                 fclose (ofp);
175
176                 cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
177                 if (cp == NULL) {
178                     adios("distsbr", "unable to create temporary file");
179                 }
180                 fchmod(txtfd, 0600);
181                 strncpy (tmpfil, cp, sizeof(tmpfil));
182                 if ((out = dup (txtfd)) == NOTOK
183                         || (ofp = fdopen (out, "w")) == NULL)
184                     adios (NULL, "no file descriptors -- you lose big");
185                 unlink (tmpfil);
186                 fprintf (ofp, "\n%s", buffer);
187                 while (state == BODY) {
188                     state = m_getfld (state, name,
189                             buffer, sizeof buffer, ifp);
190                     fputs (buffer, ofp);
191                 }
192             case FILEEOF: 
193                 goto process;
194
195             case LENERR: 
196             case FMTERR: 
197                 adios (NULL, "format error in message %s", msgnam);
198
199             default: 
200                 adios (NULL, "getfld() returned %d", state);
201         }
202 process: ;
203     fclose (ifp);
204     fclose (ofp);
205 }