]> code.citadel.org Git - citadel.git/blob - citadel/cux2ascii.c
Changed all of the proprietary strucmp() and struncmp() calls to the more
[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
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <time.h>
13 #include "citadel.h"
14
15 long finduser();
16
17 void get_config();
18 struct config config;
19
20
21 main(argc,argv)
22 int argc;
23 char *argv[]; {
24         struct tm *tm;
25         int a,b,e,mtype,aflag;
26         char bbb[100],ngn[100];
27         long tmid;
28         char tsid[64];
29         char tuid[64];
30         FILE *fp,*tfp;
31         long now,msglen;
32         char tflnm[16];
33
34         int cuunbatch = 0;
35
36         /* experimental cuunbatch header generation */
37         for (a=0; a<argc; ++a) {
38                 if (!strcmp(argv[a],"-c")) cuunbatch = 1;
39                 }
40
41         get_config();
42
43         sprintf(tflnm,"/tmp/c2a%d",getpid());
44
45         fp=stdin;
46    while (1) {
47         do {
48                 e=getc(fp);
49                 if (e<0) exit(0);
50                 } while(e!=255);
51         mtype=getc(fp); aflag=getc(fp);
52
53         tmid = 0L;
54         strcpy(tsid,FQDN);
55         strcpy(tuid,"postmaster");
56
57         tfp=fopen(tflnm,"w");
58    do {
59         b=getc(fp);
60         if (b=='M') {
61                 fprintf(tfp,"Message-ID: <%ld@%s>\n",tmid,tsid);
62                 fprintf(tfp,"\n");
63                 if (aflag!=1) fmout(80,fp,tfp);
64                    else while(a=getc(fp), a>0) {
65                         putc(a,tfp); if (a==13) putc(10,tfp);
66                         }
67                 }
68         if ((b!='M')&&(b>0)) fpgetfield(fp,bbb);
69         if (b=='I') tmid=atol(bbb);
70         if (b=='N') {
71                 strcpy(tsid,bbb);
72                 if (!strcmp(tsid,NODENAME)) strcpy(tsid,FQDN);
73                 for (a=0; a<strlen(tuid); ++a) if (tuid[a]==' ') tuid[a]='_';
74                 fprintf(tfp,"From: %s@%s ",tuid,tsid);
75                 for (a=0; a<strlen(tuid); ++a) if (tuid[a]=='_') tuid[a]=' ';
76                 fprintf(tfp,"(%s)\n",tuid);
77                 }
78         if (b=='P') fprintf(tfp,"Path: %s\n",bbb);
79         if (b=='A') strcpy(tuid,bbb);
80         if (b=='O') {
81                 xref(bbb,ngn);
82                 fprintf(tfp,"Newsgroups: %s\n",ngn);
83                 }
84         if (b=='R') fprintf(tfp,"To: %s\n",bbb);
85         if (b=='U') fprintf(tfp,"Subject: %s\n",bbb);
86         if (b=='T') {
87                 now=atol(bbb);
88                 tm=(struct tm *)localtime(&now);
89                 fprintf(tfp,"Date: %s",asctime(tm));
90                 }
91            } while ((b!='M')&&(b>0));
92         msglen=ftell(tfp);
93         fclose(tfp);
94
95         if (cuunbatch) {
96                 printf("#! cuunbatch %ld\n",msglen);
97                 }
98         else {
99                 printf("#! rnews %ld\n",msglen);
100                 }
101
102         tfp=fopen(tflnm,"r");
103         while(msglen--) putc(getc(tfp),stdout);
104         fclose(tfp);
105         unlink(tflnm);
106    }
107 exit(0);
108 }
109
110 fpgetfield(fp,string)   /* level-2 break out next null-terminated string */
111 FILE *fp;
112 char string[];
113 {
114 int a,b;
115 strcpy(string,"");
116 a=0;
117         do {
118                 b=getc(fp);
119                 if (b<1) { string[a]=0; return(0); }
120                 string[a]=b;
121                 ++a;
122                 } while(b!=0);
123         return(0);
124 }
125
126 fmout(width,fp,mout)
127 int width;
128 FILE *fp,*mout;
129         {
130         int a,b,c,real,old;
131         char aaa[140];
132         
133         strcpy(aaa,""); old=255;
134         c=1; /* c is the current pos */
135 FMTA:   old=real; a=getc(fp); real=a;
136         if (a<=0) goto FMTEND;
137         
138         if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
139         if ( ((old==13)||(old==10)) && (isspace(real)) ) {
140                                                 fprintf(mout,"\n"); c=1; }
141         if (a>126) goto FMTA;
142
143         if (a>32) {
144         if ( ((strlen(aaa)+c)>(width-1)) && (strlen(aaa)>(width-1)) )
145                 { fprintf(mout,"\n%s",aaa); c=strlen(aaa); aaa[0]=0; }
146          b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
147         if (a==32) {    if ((strlen(aaa)+c)>(width-1)) { 
148                                                         fprintf(mout,"\n");
149                                                         c=1;
150                                                         }
151                         fprintf(mout,"%s ",aaa); ++c; c=c+strlen(aaa);
152                         strcpy(aaa,""); goto FMTA; }
153         if ((a==13)||(a==10)) {
154                                 fprintf(mout,"%s\n",aaa); c=1;
155                                 strcpy(aaa,""); goto FMTA; }
156         goto FMTA;
157
158 FMTEND: fprintf(mout,"\n");
159         return(0);
160 }
161
162 xref(roomname,newsgroup)
163 char *roomname,*newsgroup; {
164         char tbuf[128];
165         FILE *fp;
166         int commapos,a;
167
168         strcpy(newsgroup,roomname);
169         fp=fopen("./network/rnews.xref","r");
170         if (fp==NULL) return(1);
171         while (fgets(tbuf,128,fp)!=NULL) {
172                 tbuf[strlen(tbuf)-1]=0;         /* strip off the newline */
173                 a=strlen(tbuf);
174                 while (a--) if (tbuf[a]==',') commapos=a;
175                 tbuf[commapos]=0;
176                 if (!strcasecmp(&tbuf[commapos+1],roomname))
177                         strcpy(newsgroup,tbuf);
178                 }
179         fclose(fp);
180         return(0);
181         }