* Removed code from some of the utilities which was still attempting
[citadel.git] / citadel / rcit.c
1 #define UNCOMPRESS "/usr/bin/gunzip"
2
3 /* Citadel/UX rcit
4  * version 2.9
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(void);
37 struct config config;
38
39 /*
40  * Cross-reference newsgroup names to Citadel room names
41  */
42 int rnewsxref(char *room, char *ngroup) {
43         FILE *fp;
44         int a,b;
45         char aaa[50],bbb[50];
46         
47         strcpy(room,ngroup);
48         fp=fopen("network/rnews.xref","r");
49 GNA:    strcpy(aaa,""); strcpy(bbb,"");
50         do {
51                 a=getc(fp);
52                 if (a==',') a=0;
53                 if (a>0) { b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
54                 } while(a>0);
55         do {
56                 a=getc(fp);
57                 if (a==10) a=0;
58                 if (a>0) { b=strlen(bbb); bbb[b]=a; bbb[b+1]=0; }
59                 } while(a>0);
60         if (a<0) {
61                 fclose(fp);
62                 return(1);
63                 }
64         if (strcasecmp(ngroup,aaa)) goto GNA;
65         fclose(fp);
66         strcpy(room,bbb);
67         return(0);
68         }
69
70
71 void main(int argc, char **argv)
72 {
73         char aaa[128],bbb[128],ccc[128];
74         char author[128],recipient[128],room[128],node[128],path[512];
75         char subject[128];
76         char orgname[128];
77         long mid = 0L;
78         long now,bcount,aa;
79         int a;
80         char flnm[128],tname[128];
81         FILE *minput,*mout,*mtemp;
82         char binary_input = 0;
83         char compressed_input = 0;
84         char spool_only = 0;
85
86         get_config();
87         sprintf(flnm,"./network/spoolin/rnews.%d",getpid());
88         sprintf(tname,"/tmp/rnews.%d",getpid());
89
90         for (a=1; a<argc; ++a) {
91                 if (!strcmp(argv[a],"-c")) binary_input = 1;
92                 if (!strcmp(argv[a],"-z")) compressed_input = 1;
93                 if (!strcmp(argv[a],"-s")) spool_only = 1;
94                 }
95
96         minput=stdin;
97         if (compressed_input) minput=popen(UNCOMPRESS,"r");
98         if (minput==NULL) fprintf(stderr,"rnews: can't open input!!!!\n");
99
100         mout=fopen(flnm,"w");
101
102         /* process Citadel/UX binary format input */
103         if (binary_input) {
104                 while ((a=getc(minput))>=0) putc(a,mout);
105                 goto END;
106                 }
107
108         /* process UseNet news input */
109 A:      if (fgets(aaa,128,minput)==NULL) goto END;
110         aaa[strlen(aaa)-1]=0;
111         if (strncmp(aaa,"#! rnews ",9)) goto A;
112         bcount=atol(&aaa[9]);
113         mtemp=fopen(tname,"w");
114         for (aa=0L; aa<bcount; ++aa) {
115                 a=getc(minput);
116                 if (a<0) goto NMA;
117                 if (a>=0) putc(a,mtemp);
118                 }
119 NMA:    fclose(mtemp);
120         if (a<0) {
121                 fprintf(stderr,"rnews: EOF unexpected\n");
122                 goto END;
123                 }
124
125         mtemp=fopen(tname,"r");
126         strcpy(author,"");
127         strcpy(recipient,"");
128         strcpy(room,"");
129         strcpy(node,"");
130         strcpy(path,"");
131         strcpy(orgname,"");
132         strcpy(subject,"");
133
134 B:      if (fgets(aaa,128,mtemp)==NULL) goto ABORT;
135         aaa[strlen(aaa)-1]=0;
136         if (strlen(aaa)==0) goto C;
137
138
139         if (!strncmp(aaa,"From: ",6)) {
140                 strcpy(author,&aaa[6]);
141                 while((author[0]==' ')&&(strlen(author)>0))
142                         strcpy(author,&author[1]);
143                 for (a=0; a<strlen(author); ++a) {
144                         if (author[a]=='<') author[a-1]=0;
145                         if (author[a]==')') author[a]=0;
146                         if (author[a]=='(') {
147                                 strcpy(author,&author[a+1]);
148                                 a=0;
149                                 }
150                         }
151                 if (!strcmp(author,")")) {
152                         strcpy(author,&aaa[6]);
153                         for (a=0; a<strlen(author); ++a)
154                                 if (author[a]=='@') author[a]=0;
155                         }
156                 strcpy(node,&aaa[6]);
157                 for (a=0; a<strlen(node); ++a) {
158                         if ((node[a]=='<')||(node[a]=='@')) {
159                                 strcpy(node,&node[a+1]);
160                                 a=0;
161                                 }
162                         if (node[a]=='>') node[a]=0;
163                         if (node[a]=='(') node[a-1]=0;
164                         }
165                 for (a=0; a<strlen(author); ++a)
166                         if (author[a]=='@') author[a]=0;
167                 }
168
169         if (!strncmp(aaa,"Path: ",6)) strcpy(path,&aaa[6]);
170         if (!strncmp(aaa,"To: ",4)) strcpy(recipient,&aaa[4]);
171         if (!strncmp(aaa,"Subject: ",9)) strcpy(subject,&aaa[9]);
172         if (!strncmp(aaa,"Organization: ",14)) strcpy(orgname,&aaa[14]);
173
174         if (!strncmp(aaa,"Newsgroups: ",11)) {
175                 strcpy(room,&aaa[12]);
176                 for (a=0; a<strlen(aaa); ++a) if (aaa[a]==',') aaa[a]=0;
177                 goto B;
178                 }
179
180         if (!strncmp(aaa,"Message-ID: ",11)) {
181                 strcpy(bbb,&aaa[13]);
182                 for (a=0; a<strlen(bbb); ++a) if (bbb[a]=='@') bbb[a]=0;
183                 mid=atol(bbb);
184                 while((aaa[0]!='@')&&(aaa[0]!=0)) {
185                         strcpy(&aaa[0],&aaa[1]);
186                         }
187                 strcpy(&aaa[0],&aaa[1]);
188                 for (a=0; a<strlen(aaa); ++a) if (aaa[a]=='>') aaa[a]=0;
189                 strcpy(node,aaa);
190                 goto B;
191                 }
192                 goto B;
193
194 C:      if ((author[0]==0)||(room[0]==0)||(node[0]==0)) goto ABORT;
195         putc(255,mout);                 /* start of message */
196         putc(MES_NORMAL,mout);          /* not anonymous */
197         putc(1,mout);                   /* not formatted */
198         time(&now);
199
200         fprintf(mout,"I%ld",mid); putc(0,mout);
201         fprintf(mout,"P%s",path); putc(0,mout);
202         fprintf(mout,"T%ld",now); putc(0,mout);
203         fprintf(mout,"A%s",author); putc(0,mout);
204         strcpy(ccc,room);
205         rnewsxref(room,ccc);
206         fprintf(mout,"O%s",room); putc(0,mout);
207         fprintf(mout,"N%s",node); putc(0,mout);
208         if (orgname[0]!=0) {
209                 fprintf(mout,"H%s",orgname); putc(0,mout);
210                 }
211         if (recipient[0]!=0) {
212                 fprintf(mout,"R%s",recipient); putc(0,mout);
213                 }
214         if (subject[0]!=0) {
215                 fprintf(mout,"U%s",subject); putc(0,mout);
216                 }
217         fprintf(mout,"M");
218         a=0;
219         aaa[0]=0;
220
221         do {
222                 a=getc(mtemp);
223                 if (a>0) putc(a,mout);
224                 } while (a>0);
225         putc(0,mout);
226 ABORT:  fclose(mtemp);
227         unlink(tname);
228         goto A;
229
230 END:    putc(0,mout);
231         fclose(mout);
232         unlink(tname);
233         if (compressed_input) pclose(minput);
234         if (!spool_only) execlp("./netproc","netproc",NULL);
235         exit(0);
236 }
237
238