HUGE PATCH. This moves all of mime_parser.c and all
[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 <libcitadel.h>
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)
65                 if (!strcmp(argv[1], "-q"))
66                         qwk = 1;
67         fp = stdin;
68         if (argc == 2)
69                 if (strcmp(argv[1], "-q")) {
70                         fp = fopen(argv[1], "r");
71                         if (fp == NULL) {
72                                 fprintf(stderr, "%s: cannot open %s: %s\n",
73                                         argv[0], argv[1], strerror(errno));
74                                 exit(errno);
75                         }
76                 }
77
78 TOP:    do {
79                 e = getc(fp);
80                 if (e < 0)
81                         exit(0);
82         } while (e != 255);
83         strcpy(subject, "");
84         mtype = getc(fp);
85         aflag = getc(fp);
86         if (qwk == 0)
87                 printf(" ");
88
89         do {
90                 b = getc(fp);
91                 if (b == 'M') {
92                         if (qwk == 0) {
93                                 printf("\n");
94                                 if (!IsEmptyStr(subject))
95                                         printf("Subject: %s\n", subject);
96                         }
97                         if (aflag != 1)
98                                 fmout(80, fp);
99                         else
100                                 while (a = getc(fp), a > 0) {
101                                         if (a == 13)
102                                                 putc(10, stdout);
103                                         else
104                                                 putc(a, stdout);
105                                 }
106                 }
107                 if ((b != 'M') && (b > 0))
108                         fpgetfield(fp, bbb);
109                 if (b == 'U')
110                         strcpy(subject, bbb);
111                 if (qwk == 0) {
112                         if (b == 'A')
113                                 printf("from %s ", bbb);
114                         if (b == 'N')
115                                 printf("@%s ", bbb);
116                         if (b == 'O')
117                                 printf("in %s> ", bbb);
118                         if (b == 'R')
119                                 printf("to %s ", bbb);
120                         if (b == 'T') {
121                                 now = atol(bbb);
122                                 localtime_r(&now, &tm);
123                                 strcpy(bbb, asctime(&tm));
124                                 bbb[strlen(bbb) - 1] = 0;
125                                 printf("%s ", &bbb[4]);
126                         }
127                 }
128         } while ((b != 'M') && (b > 0));
129         if (qwk == 0)
130                 printf("\n");
131         if (qwk == 1)
132                 exit(0);
133         goto TOP;
134 }
135
136 int fpgetfield(FILE * fp, char *string)
137
138 {                               /* level-2 break out next null-terminated string */
139         int a, b;
140         strcpy(string, "");
141         a = 0;
142         do {
143                 b = getc(fp);
144                 if (b < 1) {
145                         string[a] = 0;
146                         return (0);
147                 }
148                 string[a] = b;
149                 ++a;
150         } while (b != 0);
151         return (0);
152 }
153
154 int fmout(int width, FILE * fp)
155 {
156         int a, b, c;
157         int real = 0;
158         int old = 0;
159         char aaa[140];
160
161         strcpy(aaa, "");
162         old = 255;
163         c = 1;                  /* c is the current pos */
164 FMTA:   old = real;
165         a = getc(fp);
166         real = a;
167         if (a <= 0)
168                 goto FMTEND;
169
170         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
171                 a = 32;
172         if (((old == 13) || (old == 10)) && (isspace(real))) {
173                 printf("\n");
174                 c = 1;
175         }
176         if (a > 126)
177                 goto FMTA;
178
179         if (a > 32) {
180                 if (((strlen(aaa) + c) > (width - 5))
181                     && (strlen(aaa) > (width - 5))) {
182                         printf("\n%s", aaa);
183                         c = strlen(aaa);
184                         aaa[0] = 0;
185                 }
186                 b = strlen(aaa);
187                 aaa[b] = a;
188                 aaa[b + 1] = 0;
189         }
190         if (a == 32) {
191                 if ((strlen(aaa) + c) > (width - 5)) {
192                         printf("\n");
193                         c = 1;
194                 }
195                 printf("%s ", aaa);
196                 ++c;
197                 c = c + strlen(aaa);
198                 strcpy(aaa, "");
199                 goto FMTA;
200         }
201         if ((a == 13) || (a == 10)) {
202                 printf("%s\n", aaa);
203                 c = 1;
204                 strcpy(aaa, "");
205                 goto FMTA;
206         }
207         goto FMTA;
208
209 FMTEND: printf("\n");
210         return (0);
211 }