3c0a6d0fa58b53bd83dc94dbac9abd349bb708c3
[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  * 
10  * This utility isn't very useful anymore.
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 #include "tools.h"
35
36
37 int qwk = 0;
38
39 int fpgetfield(FILE * fp, char *string);
40 int fmout(int width, FILE * fp);
41
42
43 #ifndef HAVE_STRERROR
44 /*
45  * replacement strerror() for systems that don't have it
46  */
47 char *strerror(int e)
48 {
49         static char buf[32];
50
51         snprintf(buf, sizeof buf, "errno = %d", e);
52         return (buf);
53 }
54 #endif
55
56 int main(int argc, char **argv)
57 {
58         struct tm tm;
59         int a, b, e, mtype, aflag;
60         char bbb[1024];
61         char subject[1024];
62         FILE *fp;
63         time_t now;
64
65         if (argc == 2)
66                 if (!strcmp(argv[1], "-q"))
67                         qwk = 1;
68         fp = stdin;
69         if (argc == 2)
70                 if (strcmp(argv[1], "-q")) {
71                         fp = fopen(argv[1], "r");
72                         if (fp == NULL) {
73                                 fprintf(stderr, "%s: cannot open %s: %s\n",
74                                         argv[0], argv[1], strerror(errno));
75                                 exit(errno);
76                         }
77                 }
78
79 TOP:    do {
80                 e = getc(fp);
81                 if (e < 0)
82                         exit(0);
83         } while (e != 255);
84         strcpy(subject, "");
85         mtype = getc(fp);
86         aflag = getc(fp);
87         if (qwk == 0)
88                 printf(" ");
89
90         do {
91                 b = getc(fp);
92                 if (b == 'M') {
93                         if (qwk == 0) {
94                                 printf("\n");
95                                 if (!IsEmptyStr(subject))
96                                         printf("Subject: %s\n", subject);
97                         }
98                         if (aflag != 1)
99                                 fmout(80, fp);
100                         else
101                                 while (a = getc(fp), a > 0) {
102                                         if (a == 13)
103                                                 putc(10, stdout);
104                                         else
105                                                 putc(a, stdout);
106                                 }
107                 }
108                 if ((b != 'M') && (b > 0))
109                         fpgetfield(fp, bbb);
110                 if (b == 'U')
111                         strcpy(subject, bbb);
112                 if (qwk == 0) {
113                         if (b == 'A')
114                                 printf("from %s ", bbb);
115                         if (b == 'N')
116                                 printf("@%s ", bbb);
117                         if (b == 'O')
118                                 printf("in %s> ", bbb);
119                         if (b == 'R')
120                                 printf("to %s ", bbb);
121                         if (b == 'T') {
122                                 now = atol(bbb);
123                                 localtime_r(&now, &tm);
124                                 strcpy(bbb, asctime(&tm));
125                                 bbb[strlen(bbb) - 1] = 0;
126                                 printf("%s ", &bbb[4]);
127                         }
128                 }
129         } while ((b != 'M') && (b > 0));
130         if (qwk == 0)
131                 printf("\n");
132         if (qwk == 1)
133                 exit(0);
134         goto TOP;
135 }
136
137 int fpgetfield(FILE * fp, char *string)
138
139 {                               /* level-2 break out next null-terminated string */
140         int a, b;
141         strcpy(string, "");
142         a = 0;
143         do {
144                 b = getc(fp);
145                 if (b < 1) {
146                         string[a] = 0;
147                         return (0);
148                 }
149                 string[a] = b;
150                 ++a;
151         } while (b != 0);
152         return (0);
153 }
154
155 int fmout(int width, FILE * fp)
156 {
157         int a, b, c;
158         int real = 0;
159         int old = 0;
160         char aaa[140];
161
162         strcpy(aaa, "");
163         old = 255;
164         c = 1;                  /* c is the current pos */
165 FMTA:   old = real;
166         a = getc(fp);
167         real = a;
168         if (a <= 0)
169                 goto FMTEND;
170
171         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
172                 a = 32;
173         if (((old == 13) || (old == 10)) && (isspace(real))) {
174                 printf("\n");
175                 c = 1;
176         }
177         if (a > 126)
178                 goto FMTA;
179
180         if (a > 32) {
181                 if (((strlen(aaa) + c) > (width - 5))
182                     && (strlen(aaa) > (width - 5))) {
183                         printf("\n%s", aaa);
184                         c = strlen(aaa);
185                         aaa[0] = 0;
186                 }
187                 b = strlen(aaa);
188                 aaa[b] = a;
189                 aaa[b + 1] = 0;
190         }
191         if (a == 32) {
192                 if ((strlen(aaa) + c) > (width - 5)) {
193                         printf("\n");
194                         c = 1;
195                 }
196                 printf("%s ", aaa);
197                 ++c;
198                 c = c + strlen(aaa);
199                 strcpy(aaa, "");
200                 goto FMTA;
201         }
202         if ((a == 13) || (a == 10)) {
203                 printf("%s\n", aaa);
204                 c = 1;
205                 strcpy(aaa, "");
206                 goto FMTA;
207         }
208         goto FMTA;
209
210 FMTEND: printf("\n");
211         return (0);
212 }