]> code.citadel.org Git - citadel.git/blob - citadel/cux2ascii.c
* oops
[citadel.git] / citadel / cux2ascii.c
1 /*
2  * cux2ascii v2.3
3  * see copyright.doc for copyright information
4  *
5  * This program is a filter which converts Citadel/UX binary message format
6  * to standard UseNet news format.  Useful for Citadel<->News gateways.
7  *
8  * $Id$
9  *
10  */
11
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <time.h>
16 #include "citadel.h"
17
18 long finduser();
19
20 void get_config();
21 struct config config;
22
23
24 main(argc, argv)
25 int argc;
26 char *argv[];
27 {
28         struct tm *tm;
29         int a, b, e, mtype, aflag;
30         char bbb[100], ngn[100];
31         long tmid;
32         char tsid[64];
33         char tuid[64];
34         FILE *fp, *tfp;
35         long now, msglen;
36
37         int cuunbatch = 0;
38
39         /* experimental cuunbatch header generation */
40         for (a = 0; a < argc; ++a) {
41                 if (!strcmp(argv[a], "-c"))
42                         cuunbatch = 1;
43         }
44
45         get_config();
46
47         fp = stdin;
48         while (1) {
49                 do {
50                         e = getc(fp);
51                         if (e < 0)
52                                 exit(0);
53                 } while (e != 255);
54                 mtype = getc(fp);
55                 aflag = getc(fp);
56
57                 tmid = 0L;
58                 strcpy(tsid, FQDN);
59                 strcpy(tuid, "postmaster");
60
61                 tfp = tmpfile();
62                 do {
63                         b = getc(fp);
64                         if (b == 'M') {
65                                 fprintf(tfp, "Message-ID: <%ld@%s>\n", tmid, tsid);
66                                 fprintf(tfp, "\n");
67                                 if (aflag != 1)
68                                         fmout(80, fp, tfp);
69                                 else
70                                         while (a = getc(fp), a > 0) {
71                                                 putc(a, tfp);
72                                                 if (a == 13)
73                                                         putc(10, tfp);
74                                         }
75                         }
76                         if ((b != 'M') && (b > 0))
77                                 fpgetfield(fp, bbb);
78                         if (b == 'I')
79                                 tmid = atol(bbb);
80                         if (b == 'N') {
81                                 strcpy(tsid, bbb);
82                                 if (!strcmp(tsid, NODENAME))
83                                         strcpy(tsid, FQDN);
84                                 for (a = 0; a < strlen(tuid); ++a)
85                                         if (tuid[a] == ' ')
86                                                 tuid[a] = '_';
87                                 fprintf(tfp, "From: %s@%s ", tuid, tsid);
88                                 for (a = 0; a < strlen(tuid); ++a)
89                                         if (tuid[a] == '_')
90                                                 tuid[a] = ' ';
91                                 fprintf(tfp, "(%s)\n", tuid);
92                         }
93                         if (b == 'P')
94                                 fprintf(tfp, "Path: %s\n", bbb);
95                         if (b == 'A')
96                                 strcpy(tuid, bbb);
97                         if (b == 'O') {
98                                 xref(bbb, ngn);
99                                 fprintf(tfp, "Newsgroups: %s\n", ngn);
100                         }
101                         if (b == 'R')
102                                 fprintf(tfp, "To: %s\n", bbb);
103                         if (b == 'U')
104                                 fprintf(tfp, "Subject: %s\n", bbb);
105                         if (b == 'T') {
106                                 now = atol(bbb);
107                                 tm = (struct tm *) localtime(&now);
108                                 fprintf(tfp, "Date: %s", asctime(tm));
109                         }
110                 } while ((b != 'M') && (b > 0));
111                 msglen = ftell(tfp);
112
113                 if (cuunbatch) {
114                         printf("#! cuunbatch %ld\n", msglen);
115                 } else {
116                         printf("#! rnews %ld\n", msglen);
117                 }
118
119                 rewind(tfp);
120                 while (msglen--)
121                         putc(getc(tfp), stdout);
122                 fclose(tfp);
123         }
124         exit(0);
125 }
126
127 fpgetfield(fp, string)          /* level-2 break out next null-terminated string */
128 FILE *fp;
129 char string[];
130 {
131         int a, b;
132         strcpy(string, "");
133         a = 0;
134         do {
135                 b = getc(fp);
136                 if (b < 1) {
137                         string[a] = 0;
138                         return (0);
139                 }
140                 string[a] = b;
141                 ++a;
142         } while (b != 0);
143         return (0);
144 }
145
146 fmout(width, fp, mout)
147 int width;
148 FILE *fp, *mout;
149 {
150         int a, b, c, real, old;
151         char aaa[140];
152
153         strcpy(aaa, "");
154         old = 255;
155         c = 1;                  /* c is the current pos */
156       FMTA:old = real;
157         a = getc(fp);
158         real = a;
159         if (a <= 0)
160                 goto FMTEND;
161
162         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
163                 a = 32;
164         if (((old == 13) || (old == 10)) && (isspace(real))) {
165                 fprintf(mout, "\n");
166                 c = 1;
167         }
168         if (a > 126)
169                 goto FMTA;
170
171         if (a > 32) {
172                 if (((strlen(aaa) + c) > (width - 1)) && (strlen(aaa) > (width - 1))) {
173                         fprintf(mout, "\n%s", aaa);
174                         c = strlen(aaa);
175                         aaa[0] = 0;
176                 }
177                 b = strlen(aaa);
178                 aaa[b] = a;
179                 aaa[b + 1] = 0;
180         }
181         if (a == 32) {
182                 if ((strlen(aaa) + c) > (width - 1)) {
183                         fprintf(mout, "\n");
184                         c = 1;
185                 }
186                 fprintf(mout, "%s ", aaa);
187                 ++c;
188                 c = c + strlen(aaa);
189                 strcpy(aaa, "");
190                 goto FMTA;
191         }
192         if ((a == 13) || (a == 10)) {
193                 fprintf(mout, "%s\n", aaa);
194                 c = 1;
195                 strcpy(aaa, "");
196                 goto FMTA;
197         }
198         goto FMTA;
199
200       FMTEND:fprintf(mout, "\n");
201         return (0);
202 }
203
204 xref(roomname, newsgroup)
205 char *roomname, *newsgroup;
206 {
207         char tbuf[128];
208         FILE *fp;
209         int commapos, a;
210
211         strcpy(newsgroup, roomname);
212         fp = fopen("./network/rnews.xref", "r");
213         if (fp == NULL)
214                 return (1);
215         while (fgets(tbuf, 128, fp) != NULL) {
216                 tbuf[strlen(tbuf) - 1] = 0;     /* strip off the newline */
217                 a = strlen(tbuf);
218                 while (a--)
219                         if (tbuf[a] == ',')
220                                 commapos = a;
221                 tbuf[commapos] = 0;
222                 if (!strcasecmp(&tbuf[commapos + 1], roomname))
223                         strcpy(newsgroup, tbuf);
224         }
225         fclose(fp);
226         return (0);
227 }