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