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