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