Tue Aug 18 00:42:33 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[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
25 int qwk = 0;
26
27 int fpgetfield(FILE *fp, char *string);
28 int fmout(int width, FILE *fp);
29
30
31 #ifdef NO_STRERROR
32 /*
33  * replacement strerror() for systems that don't have it
34  */
35 char *strerror(int e)
36 {
37         static char buf[32];
38
39         sprintf(buf,"errno = %d",e);
40         return(buf);
41         }
42 #endif
43
44 void main(int argc, char **argv)
45 {
46         struct tm *tm;
47         int a,b,e,mtype,aflag;
48         char bbb[1024];
49         char subject[1024];
50         FILE *fp;
51         long now;
52
53         if (argc==2) if (!strcmp(argv[1],"-q")) qwk = 1;
54         fp=stdin;
55         if (argc==2) if (strcmp(argv[1],"-q")) {
56                 fp = fopen(argv[1],"r");
57                 if (fp==NULL) {
58                         fprintf(stderr,"%s: cannot open %s: %s\n",
59                                 argv[0],argv[1],strerror(errno));
60                         exit(errno);
61                         }
62                 }
63
64 TOP:    do {
65                 e=getc(fp);
66                 if (e<0) exit(0);
67                 } while(e!=255);
68         strcpy(subject,"");
69         mtype=getc(fp); aflag=getc(fp);
70         if (qwk == 0) printf(" ");
71
72    do {
73         b=getc(fp);
74         if (b=='M') {
75                 if (qwk==0) {
76                                 printf("\n");
77                                 if (strlen(subject)!=0)
78                                         printf("Subject: %s\n",subject);
79                                 }
80                 if (aflag!=1) fmout(80,fp);
81                    else while(a=getc(fp), a>0) {
82                         if (a==13) putc(10,stdout);
83                         else putc(a,stdout);
84                         }
85                 }
86         if ((b!='M')&&(b>0)) fpgetfield(fp,bbb);
87         if (b=='U') strcpy(subject,bbb);
88         if (qwk==0) {
89                 if (b=='A') printf("from %s ",bbb);
90                 if (b=='N') printf("@%s ",bbb);
91                 if (b=='O') printf("in %s> ",bbb);
92                 if (b=='R') printf("to %s ",bbb);
93                 if (b=='T') {
94                         now=atol(bbb);
95                         tm=(struct tm *)localtime(&now);
96                         strcpy(bbb,asctime(tm)); bbb[strlen(bbb)-1]=0;
97                         printf("%s ",&bbb[4]);
98                         }
99                 }
100            } while ((b!='M')&&(b>0));
101         if (qwk==0) printf("\n"); 
102         if (qwk==1) exit(0);
103         goto TOP;
104 }
105
106 int fpgetfield(FILE *fp, char *string) /* level-2 break out next null-terminated string */
107          
108               
109 {
110 int a,b;
111 strcpy(string,"");
112 a=0;
113         do {
114                 b=getc(fp);
115                 if (b<1) { string[a]=0; return(0); }
116                 string[a]=b;
117                 ++a;
118                 } while(b!=0);
119         return(0);
120 }
121
122 int fmout(int width, FILE *fp)
123 {
124         int a,b,c;
125         int real = 0;
126         int old = 0;
127         char aaa[140];
128         
129         strcpy(aaa,""); old=255;
130         c=1; /* c is the current pos */
131 FMTA:   old=real; a=getc(fp); real=a;
132         if (a<=0) goto FMTEND;
133         
134         if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
135         if ( ((old==13)||(old==10)) && (isspace(real)) ) {
136                                                 printf("\n"); c=1; }
137         if (a>126) goto FMTA;
138
139         if (a>32) {
140         if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
141                 { printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0; }
142          b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
143         if (a==32) {    if ((strlen(aaa)+c)>(width-5)) { 
144                                                         printf("\n");
145                                                         c=1;
146                                                         }
147                         printf("%s ",aaa); ++c; c=c+strlen(aaa);
148                         strcpy(aaa,""); goto FMTA; }
149         if ((a==13)||(a==10)) {
150                                 printf("%s\n",aaa); c=1;
151                                 strcpy(aaa,""); goto FMTA; }
152         goto FMTA;
153
154 FMTEND: printf("\n");
155         return(0);
156 }