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