3 * distsbr.c -- routines to do additional "dist-style" processing
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.
14 static int hdrfd = NOTOK;
15 static int txtfd = NOTOK;
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!"
25 static void ready_msg(char *);
28 distout (char *drft, char *msgnam, char *backup)
31 register unsigned char *dp;
32 register char *resent;
33 char name[NAMESZ], buffer[BUFSIZ];
34 register FILE *ifp, *ofp;
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");
41 if ((ofp = fopen (drft, "w")) == NULL)
42 adios (drft, "unable to create temporary file");
43 chmod (drft, m_gmprot ());
46 lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
47 cpydata (hdrfd, fileno (ofp), msgnam, drft);
49 for (state = FLD, resent = NULL;;)
51 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
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);
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);
79 for (dp = buffer; *dp; dp++)
81 advise (NULL, BADTXT, "draft");
90 advise (NULL, BADRFT, "draft");
95 if (rename (backup, drft) == NOTOK)
96 adios (drft, "unable to rename %s to", backup);
100 adios (NULL, "getfld() returned %d", state);
107 advise (NULL, BADMSG, "draft");
110 if (rename (backup, drft) == NOTOK)
111 adios (drft, "unable to rename %s to", backup);
116 if (txtfd != NOTOK) {
117 lseek (txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
118 cpydata (txtfd, fileno (ofp), msgnam, drft);
128 ready_msg (char *msgnam)
131 char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ];
132 register FILE *ifp, *ofp;
136 close (hdrfd), hdrfd = NOTOK;
138 close (txtfd), txtfd = NOTOK;
140 if ((ifp = fopen (msgnam, "r")) == NULL)
141 adios (msgnam, "unable to open message");
143 cp = m_mktemp2(NULL, "dist", &hdrfd, NULL);
145 adios("distsbr", "unable to create temporary file");
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");
156 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
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);
176 cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
178 adios("distsbr", "unable to create temporary file");
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");
186 fprintf (ofp, "\n%s", buffer);
187 while (state == BODY) {
188 state = m_getfld (state, name,
189 buffer, sizeof buffer, ifp);
197 adios (NULL, "format error in message %s", msgnam);
200 adios (NULL, "getfld() returned %d", state);