File: msgform.c

package info (click to toggle)
citadel 917-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,976 kB
  • sloc: ansic: 44,751; sh: 4,209; yacc: 651; makefile: 399
file content (186 lines) | stat: -rw-r--r-- 3,264 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * This is simply a filter that converts Citadel binary message format
 * to readable, formatted output.
 * 
 * If the -q (quiet or qwk) flag is used, only the message text prints, and
 * then it stops at the end of the first message it prints.
 * 
 * This utility isn't very useful anymore.
 *
 */

#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <libcitadel.h>

int qwk = 0;

int fpgetfield(FILE * fp, char *string);
int fmout(int width, FILE * fp);



int main(int argc, char **argv)
{
	struct tm tm;
	int a, b, e, aflag;
	char bbb[1024];
	char subject[1024];
	FILE *fp;
	time_t now;

	if (argc == 2)
		if (!strcmp(argv[1], "-q"))
			qwk = 1;
	fp = stdin;
	if (argc == 2)
		if (strcmp(argv[1], "-q")) {
			fp = fopen(argv[1], "r");
			if (fp == NULL) {
				fprintf(stderr, "%s: cannot open %s: %s\n",
					argv[0], argv[1], strerror(errno));
				exit(errno);
			}
		}

TOP:	do {
		e = getc(fp);
		if (e < 0)
			exit(0);
	} while (e != 255);
	strcpy(subject, "");
	getc(fp);
	aflag = getc(fp);
	if (qwk == 0)
		printf(" ");

	do {
		b = getc(fp);
		if (b == 'M') {
			if (qwk == 0) {
				printf("\n");
				if (!IsEmptyStr(subject))
					printf("Subject: %s\n", subject);
			}
			if (aflag != 1)
				fmout(80, fp);
			else
				while (a = getc(fp), a > 0) {
					if (a == 13)
						putc(10, stdout);
					else
						putc(a, stdout);
				}
		}
		if ((b != 'M') && (b > 0))
			fpgetfield(fp, bbb);
		if (b == 'U')
			strcpy(subject, bbb);
		if (qwk == 0) {
			if (b == 'A')
				printf("from %s ", bbb);
			if (b == 'N')
				printf("@%s ", bbb);
			if (b == 'O')
				printf("in %s> ", bbb);
			if (b == 'R')
				printf("to %s ", bbb);
			if (b == 'T') {
				now = atol(bbb);
				localtime_r(&now, &tm);
				strcpy(bbb, asctime(&tm));
				bbb[strlen(bbb) - 1] = 0;
				printf("%s ", &bbb[4]);
			}
		}
	} while ((b != 'M') && (b > 0));
	if (qwk == 0)
		printf("\n");
	if (qwk == 1)
		exit(0);
	goto TOP;
}

int fpgetfield(FILE * fp, char *string)

{				/* level-2 break out next null-terminated string */
	int a, b;
	strcpy(string, "");
	a = 0;
	do {
		b = getc(fp);
		if (b < 1) {
			string[a] = 0;
			return (0);
		}
		string[a] = b;
		++a;
	} while (b != 0);
	return (0);
}

int fmout(int width, FILE * fp)
{
	int a, b, c;
	int real = 0;
	int old = 0;
	char aaa[140];

	strcpy(aaa, "");
	old = 255;
	c = 1;			/* c is the current pos */
FMTA:	old = real;
	a = getc(fp);
	real = a;
	if (a <= 0)
		goto FMTEND;

	if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
		a = 32;
	if (((old == 13) || (old == 10)) && (isspace(real))) {
		printf("\n");
		c = 1;
	}
	if (a > 126)
		goto FMTA;

	if (a > 32) {
		if (((strlen(aaa) + c) > (width - 5))
		    && (strlen(aaa) > (width - 5))) {
			printf("\n%s", aaa);
			c = strlen(aaa);
			aaa[0] = 0;
		}
		b = strlen(aaa);
		aaa[b] = a;
		aaa[b + 1] = 0;
	}
	if (a == 32) {
		if ((strlen(aaa) + c) > (width - 5)) {
			printf("\n");
			c = 1;
		}
		printf("%s ", aaa);
		++c;
		c = c + strlen(aaa);
		strcpy(aaa, "");
		goto FMTA;
	}
	if ((a == 13) || (a == 10)) {
		printf("%s\n", aaa);
		c = 1;
		strcpy(aaa, "");
		goto FMTA;
	}
	goto FMTA;

FMTEND:	printf("\n");
	return (0);
}