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