93a91bd917212608537edf442c1176280ff4035f
[citadel.git] / citadel / rcit.c
1 #define UNCOMPRESS "/usr/bin/gunzip"
2
3 /* Citadel/UX rnews
4  * version 2.8
5  *
6  * This program functions the same as the standard rnews program for
7  * UseNet. It accepts standard input, and looks for rooms to post messages
8  * (translated from UseNet format to the Citadel/UX binary message format)
9  * in that match the names of the newsgroups. network/rnews.xref is checked
10  * in case the sysop wants to cross-reference room names to newsgroup names.
11  * If standard input is already in binary, the -c flag should be used.
12  *   The netproc program is then called to do the processing.
13  *
14  * If you have a separate newsreader and don't want to use Citadel for news,
15  * just call this program something else (rcit, for example) -- but make sure
16  * to tell your Citadel network neighbors the new name of the program to call.
17  * 
18  * usage:
19  *      rnews [-c] [-z] [-s]
20  * flags:
21  *      -c      Input is already in Citadel binary format
22  *              (default is UseNet news format)
23  *      -z      Input is compressed, run uncompress on it before processing
24  *      -s      Don't run netproc now, just accept the input into spoolin
25  */
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <time.h>
34 #include "citadel.h"
35
36 void get_config();
37 struct config config;
38
39 char roomlist[MAXROOMS][20];
40
41 void load_roomlist() {
42         FILE *fp;
43         struct quickroom QRtemp;
44         int a;
45
46         for (a=0; a<MAXROOMS; ++a) strcpy(roomlist[a],TWITROOM);
47         fp=fopen("quickroom","r");
48         if (fp==NULL) return;
49         for (a=0; a<MAXROOMS; ++a) {
50                 fread((char *)&QRtemp,sizeof(struct quickroom),1,fp);
51                 if (QRtemp.QRflags & QR_INUSE)
52                         strcpy(roomlist[a],QRtemp.QRname);
53                 }
54         fclose(fp);
55         }
56
57
58
59 int rnewsxref(room,ngroup)              /* xref table */
60 char room[]; 
61 char ngroup[]; {
62         FILE *fp;
63         int a,b;
64         char aaa[50],bbb[50];
65         
66         strcpy(room,ngroup);
67         fp=fopen("network/rnews.xref","r");
68 GNA:    strcpy(aaa,""); strcpy(bbb,"");
69         do {
70                 a=getc(fp);
71                 if (a==',') a=0;
72                 if (a>0) { b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
73                 } while(a>0);
74         do {
75                 a=getc(fp);
76                 if (a==10) a=0;
77                 if (a>0) { b=strlen(bbb); bbb[b]=a; bbb[b+1]=0; }
78                 } while(a>0);
79         if (a<0) {
80                 fclose(fp);
81                 return(1);
82                 }
83         if (strcasecmp(ngroup,aaa)) goto GNA;
84         fclose(fp);
85         strcpy(room,bbb);
86         return(0);
87         }
88
89
90 void getroom(room,ngroup)
91 char room[];
92 char ngroup[]; {
93         char ngbuf[256];
94         char tryme[256];
95         int a,b;
96         struct quickroom qbuf;
97
98         strcpy(ngbuf,ngroup);
99         strcat(ngbuf,",");
100         for (a=0; a<strlen(ngbuf); ++a) {
101                 if ((ngbuf[a]==',')||(ngbuf[a]==0)) {
102                         strcpy(tryme,ngbuf);    
103                         tryme[a]=0;
104                         if (!rnewsxref(room,tryme)) return;
105                         room[sizeof(qbuf.QRname)-1]=0;
106                         for (b=0; b<MAXROOMS; ++b) {
107                                 if (!strcasecmp(roomlist[b],room)) return;
108                                 }
109                         strcpy(ngbuf,&ngbuf[a+1]);
110                         a=(-1);
111                         }
112                 }
113         }
114
115 void main(argc,argv)
116 int argc;
117 char *argv[]; {
118         char aaa[128],bbb[128],ccc[128];
119         char author[128],recipient[128],room[128],node[128],path[512];
120         char subject[128];
121         char orgname[128];
122         long mid = 0L;
123         long now,bcount,aa;
124         int a;
125         char flnm[128],tname[128];
126         FILE *minput,*mout,*mtemp;
127         char binary_input = 0;
128         char compressed_input = 0;
129         char spool_only = 0;
130
131         get_config();
132         sprintf(flnm,"./network/spoolin/rnews.%d",getpid());
133         sprintf(tname,"/tmp/rnews.%d",getpid());
134
135         for (a=1; a<argc; ++a) {
136                 if (!strcmp(argv[a],"-c")) binary_input = 1;
137                 if (!strcmp(argv[a],"-z")) compressed_input = 1;
138                 if (!strcmp(argv[a],"-s")) spool_only = 1;
139                 }
140
141         minput=stdin;
142         if (compressed_input) minput=popen(UNCOMPRESS,"r");
143         if (minput==NULL) fprintf(stderr,"rnews: can't open input!!!!\n");
144
145         mout=fopen(flnm,"w");
146
147         /* process Citadel/UX binary format input */
148         if (binary_input) {
149                 while ((a=getc(minput))>=0) putc(a,mout);
150                 goto END;
151                 }
152
153         /* process UseNet news input */
154         load_roomlist();
155 A:      if (fgets(aaa,128,minput)==NULL) goto END;
156         aaa[strlen(aaa)-1]=0;
157         if (strncmp(aaa,"#! rnews ",9)) goto A;
158         bcount=atol(&aaa[9]);
159         mtemp=fopen(tname,"w");
160         for (aa=0L; aa<bcount; ++aa) {
161                 a=getc(minput);
162                 if (a<0) goto NMA;
163                 if (a>=0) putc(a,mtemp);
164                 }
165 NMA:    fclose(mtemp);
166         if (a<0) {
167                 fprintf(stderr,"rnews: EOF unexpected\n");
168                 goto END;
169                 }
170
171         mtemp=fopen(tname,"r");
172         strcpy(author,"");
173         strcpy(recipient,"");
174         strcpy(room,"");
175         strcpy(node,"");
176         strcpy(path,"");
177         strcpy(orgname,"");
178         strcpy(subject,"");
179
180 B:      if (fgets(aaa,128,mtemp)==NULL) goto ABORT;
181         aaa[strlen(aaa)-1]=0;
182         if (strlen(aaa)==0) goto C;
183
184
185         if (!strncmp(aaa,"From: ",6)) {
186                 strcpy(author,&aaa[6]);
187                 while((author[0]==' ')&&(strlen(author)>0))
188                         strcpy(author,&author[1]);
189                 for (a=0; a<strlen(author); ++a) {
190                         if (author[a]=='<') author[a-1]=0;
191                         if (author[a]==')') author[a]=0;
192                         if (author[a]=='(') {
193                                 strcpy(author,&author[a+1]);
194                                 a=0;
195                                 }
196                         }
197                 if (!strcmp(author,")")) {
198                         strcpy(author,&aaa[6]);
199                         for (a=0; a<strlen(author); ++a)
200                                 if (author[a]=='@') author[a]=0;
201                         }
202                 strcpy(node,&aaa[6]);
203                 for (a=0; a<strlen(node); ++a) {
204                         if ((node[a]=='<')||(node[a]=='@')) {
205                                 strcpy(node,&node[a+1]);
206                                 a=0;
207                                 }
208                         if (node[a]=='>') node[a]=0;
209                         if (node[a]=='(') node[a-1]=0;
210                         }
211                 for (a=0; a<strlen(author); ++a)
212                         if (author[a]=='@') author[a]=0;
213                 }
214
215         if (!strncmp(aaa,"Path: ",6)) strcpy(path,&aaa[6]);
216         if (!strncmp(aaa,"To: ",4)) strcpy(recipient,&aaa[4]);
217         if (!strncmp(aaa,"Subject: ",9)) strcpy(subject,&aaa[9]);
218         if (!strncmp(aaa,"Organization: ",14)) strcpy(orgname,&aaa[14]);
219
220         if (!strncmp(aaa,"Newsgroups: ",11)) {
221                 strcpy(room,&aaa[12]);
222                 for (a=0; a<strlen(aaa); ++a) if (aaa[a]==',') aaa[a]=0;
223                 goto B;
224                 }
225
226         if (!strncmp(aaa,"Message-ID: ",11)) {
227                 strcpy(bbb,&aaa[13]);
228                 for (a=0; a<strlen(bbb); ++a) if (bbb[a]=='@') bbb[a]=0;
229                 mid=atol(bbb);
230                 while((aaa[0]!='@')&&(aaa[0]!=0)) {
231                         strcpy(&aaa[0],&aaa[1]);
232                         }
233                 strcpy(&aaa[0],&aaa[1]);
234                 for (a=0; a<strlen(aaa); ++a) if (aaa[a]=='>') aaa[a]=0;
235                 strcpy(node,aaa);
236                 goto B;
237                 }
238                 goto B;
239
240 C:      if ((author[0]==0)||(room[0]==0)||(node[0]==0)) goto ABORT;
241         putc(255,mout);                 /* start of message */
242         putc(MES_NORMAL,mout);          /* not anonymous */
243         putc(1,mout);                   /* not formatted */
244         time(&now);
245
246         fprintf(mout,"I%ld",mid); putc(0,mout);
247         fprintf(mout,"P%s",path); putc(0,mout);
248         fprintf(mout,"T%ld",now); putc(0,mout);
249         fprintf(mout,"A%s",author); putc(0,mout);
250         strcpy(ccc,room);
251         getroom(room,ccc);
252         fprintf(mout,"O%s",room); putc(0,mout);
253         fprintf(mout,"N%s",node); putc(0,mout);
254         if (orgname[0]!=0) {
255                 fprintf(mout,"H%s",orgname); putc(0,mout);
256                 }
257         if (recipient[0]!=0) {
258                 fprintf(mout,"R%s",recipient); putc(0,mout);
259                 }
260         if (subject[0]!=0) {
261                 fprintf(mout,"U%s",subject); putc(0,mout);
262                 }
263         fprintf(mout,"M");
264         a=0;
265         aaa[0]=0;
266
267         do {
268                 a=getc(mtemp);
269                 if (a>0) putc(a,mout);
270                 } while (a>0);
271         putc(0,mout);
272 ABORT:  fclose(mtemp);
273         unlink(tname);
274         goto A;
275
276 END:    putc(0,mout);
277         fclose(mout);
278         unlink(tname);
279         if (compressed_input) pclose(minput);
280         if (!spool_only) execlp("./netproc","netproc",NULL);
281         exit(0);
282 }
283
284