6b47b83ef99e53b0aaf75d9903135060de2c544f
[citadel.git] / citadel / msgform.c
1 /*
2  * msgform.c v2.1
3  * see copyright.doc for copyright information
4  * 
5  * This is simply a filter that converts Citadel binary message format
6  * to readable, formatted output.
7  * 
8  * If the -q (quiet or qwk) flag is used, only the message text prints, and
9  * then it stops at the end of the first message it prints.
10  * This is used by the QWK reader for Citadel/UX during message format
11  * translation.
12  */
13
14 #include "sysdep.h"
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <time.h>
22 #include <errno.h>
23
24 long finduser();
25 int fmout();
26 int fpgetfield();
27
28 int qwk = 0;
29
30
31 #ifdef NO_STRERROR
32 /*
33  * replacement strerror() for systems that don't have it
34  */
35 char *strerror(e)
36 int e; {
37         static char buf[32];
38
39         sprintf(buf,"errno = %d",e);
40         return(buf);
41         }
42 #endif
43
44 void main(argc,argv)
45 int argc;
46 char *argv[]; {
47         struct tm *tm;
48         int a,b,e,mtype,aflag;
49         char bbb[1024];
50         char subject[1024];
51         FILE *fp;
52         long now;
53
54         if (argc==2) if (!strcmp(argv[1],"-q")) qwk = 1;
55         fp=stdin;
56         if (argc==2) if (strcmp(argv[1],"-q")) {
57                 fp = fopen(argv[1],"r");
58                 if (fp==NULL) {
59                         fprintf(stderr,"%s: cannot open %s: %s\n",
60                                 argv[0],argv[1],strerror(errno));
61                         exit(errno);
62                         }
63                 }
64
65 TOP:    do {
66                 e=getc(fp);
67                 if (e<0) exit(0);
68                 } while(e!=255);
69         strcpy(subject,"");
70         mtype=getc(fp); aflag=getc(fp);
71         if (qwk == 0) printf(" ");
72
73    do {
74         b=getc(fp);
75         if (b=='M') {
76                 if (qwk==0) {
77                                 printf("\n");
78                                 if (strlen(subject)!=0)
79                                         printf("Subject: %s\n",subject);
80                                 }
81                 if (aflag!=1) fmout(80,fp);
82                    else while(a=getc(fp), a>0) {
83                         if (a==13) putc(10,stdout);
84                         else putc(a,stdout);
85                         }
86                 }
87         if ((b!='M')&&(b>0)) fpgetfield(fp,bbb);
88         if (b=='U') strcpy(subject,bbb);
89         if (qwk==0) {
90                 if (b=='A') printf("from %s ",bbb);
91                 if (b=='N') printf("@%s ",bbb);
92                 if (b=='O') printf("in %s> ",bbb);
93                 if (b=='R') printf("to %s ",bbb);
94                 if (b=='T') {
95                         now=atol(bbb);
96                         tm=(struct tm *)localtime(&now);
97                         strcpy(bbb,asctime(tm)); bbb[strlen(bbb)-1]=0;
98                         printf("%s ",&bbb[4]);
99                         }
100                 }
101            } while ((b!='M')&&(b>0));
102         if (qwk==0) printf("\n"); 
103         if (qwk==1) exit(0);
104         goto TOP;
105 }
106
107 int fpgetfield(fp,string) /* level-2 break out next null-terminated string */
108 FILE *fp;
109 char string[];
110 {
111 int a,b;
112 strcpy(string,"");
113 a=0;
114         do {
115                 b=getc(fp);
116                 if (b<1) { string[a]=0; return(0); }
117                 string[a]=b;
118                 ++a;
119                 } while(b!=0);
120         return(0);
121 }
122
123 int fmout(width,fp)
124 int width;
125 FILE *fp;
126         {
127         int a,b,c;
128         int real = 0;
129         int old = 0;
130         char aaa[140];
131         
132         strcpy(aaa,""); old=255;
133         c=1; /* c is the current pos */
134 FMTA:   old=real; a=getc(fp); real=a;
135         if (a<=0) goto FMTEND;
136         
137         if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
138         if ( ((old==13)||(old==10)) && (isspace(real)) ) {
139                                                 printf("\n"); c=1; }
140         if (a>126) goto FMTA;
141
142         if (a>32) {
143         if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
144                 { printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0; }
145          b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
146         if (a==32) {    if ((strlen(aaa)+c)>(width-5)) { 
147                                                         printf("\n");
148                                                         c=1;
149                                                         }
150                         printf("%s ",aaa); ++c; c=c+strlen(aaa);
151                         strcpy(aaa,""); goto FMTA; }
152         if ((a==13)||(a==10)) {
153                                 printf("%s\n",aaa); c=1;
154                                 strcpy(aaa,""); goto FMTA; }
155         goto FMTA;
156
157 FMTEND: printf("\n");
158         return(0);
159 }