fmsgdump.c 4.38 KB
Newer Older
1
/* $Id: fmsgdump.c,v 3.6 2020/04/26 21:01:55 rswindell Exp $ */
rswindell's avatar
rswindell committed
2
3
4
5
6
7
8
9
10
11
12
// vi: tabstop=4

#include "gen_defs.h"
#include "fidodefs.h"
#include "xpendian.h"	/* swap */
#include "dirwrap.h"	/* _PATH_DEVNULL */
#include <stdio.h>
#include <string.h>

FILE* nulfp;
FILE* bodyfp;
rswindell's avatar
rswindell committed
13
FILE* ctrlfp;
rswindell's avatar
rswindell committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

char* freadstr(FILE* fp, char* str, size_t maxlen)
{
	int		ch;
	size_t	len=0;

	while((ch=fgetc(fp))!=EOF && len<maxlen) {
		str[len++]=ch;
		if(ch==0)
			break;
	}

	str[maxlen-1]=0;

	return(str);
}

rswindell's avatar
rswindell committed
31
32
33
34
const char* fmsgattr_str(uint16_t attr)
{
	char str[64] = "";

35
	str[0] = '\0';
rswindell's avatar
rswindell committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define FIDO_ATTR_CHECK(a, f) if(a&FIDO_##f)	sprintf(str + strlen(str), "%s%s", str[0] == 0 ? "" : ", ", #f);
	FIDO_ATTR_CHECK(attr, PRIVATE);
	FIDO_ATTR_CHECK(attr, CRASH);
	FIDO_ATTR_CHECK(attr, RECV);
	FIDO_ATTR_CHECK(attr, SENT);
	FIDO_ATTR_CHECK(attr, FILE);
	FIDO_ATTR_CHECK(attr, INTRANS);
	FIDO_ATTR_CHECK(attr, ORPHAN);
	FIDO_ATTR_CHECK(attr, KILLSENT);
	FIDO_ATTR_CHECK(attr, LOCAL);
	FIDO_ATTR_CHECK(attr, HOLD);
	FIDO_ATTR_CHECK(attr, FREQ);
	FIDO_ATTR_CHECK(attr, RRREQ);
	FIDO_ATTR_CHECK(attr, RR);
	FIDO_ATTR_CHECK(attr, AUDIT);
	FIDO_ATTR_CHECK(attr, FUPREQ);
	if(str[0] == 0)
		return "";

55
	static char buf[128];
rswindell's avatar
rswindell committed
56
57
58
59
	sprintf(buf, "(%s)", str);
	return buf;
}

rswindell's avatar
rswindell committed
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
int msgdump(FILE* fp, const char* fname)
{
	int			ch;
	long		end;
	fmsghdr_t	hdr;

	if(fread(&hdr,sizeof(hdr),1,fp) != 1) {
		fprintf(stderr,"%s !Error reading msg hdr (%" XP_PRIsize_t "u bytes)\n"
			,fname,sizeof(hdr));
		return(__COUNTER__);
	}

	fseek(fp,-1L,SEEK_END);
	ch = fgetc(fp);
	end = ftell(fp);
	if(ch != FIDO_STORED_MSG_TERMINATOR) {
		fprintf(stderr,"%s !Message missing terminator (%02X instead of %02X)\n"
			,fname, (uchar)ch, FIDO_STORED_MSG_TERMINATOR);
//		return(-2);
	}

	if(hdr.from[sizeof(hdr.from)-1] != 0)
		fprintf(stderr,"%s Unterminated 'from' field\n", fname);
	if(hdr.to[sizeof(hdr.to)-1] != 0)
		fprintf(stderr,"%s Unterminated 'to' field\n", fname);
	if(hdr.subj[sizeof(hdr.subj)-1] != 0)
		fprintf(stderr,"%s Unterminated 'subj' field\n", fname);
	if(hdr.time[sizeof(hdr.time)-1] != 0)
		fprintf(stderr,"%s Untermianted 'time' field\n", fname);


	printf("Subj: %.*s\n", (int)sizeof(hdr.subj)-1, hdr.subj);
rswindell's avatar
rswindell committed
92
	printf("Attr: 0x%04hX %s\n", hdr.attr, fmsgattr_str(hdr.attr));
93
	printf("To  : %.*s (%u:%u/%u.%u)\n", (int)sizeof(hdr.to)-1, hdr.to
rswindell's avatar
rswindell committed
94
		,hdr.destzone, hdr.destnet, hdr.destnode, hdr.destpoint);
95
	TERMINATE(hdr.from);
96
	printf("From: %.*s (%u:%u/%u.%u)\n", (int)sizeof(hdr.from)-1, hdr.from
rswindell's avatar
rswindell committed
97
		,hdr.origzone, hdr.orignet, hdr.orignode, hdr.origpoint);
98
	TERMINATE(hdr.time);
rswindell's avatar
rswindell committed
99
100
101
102
103
104
105
106
	printf("Time: %.*s\n", (int)sizeof(hdr.time)-1, hdr.time);

	if(end <= sizeof(hdr)+1) {
		fprintf(stderr, "!No body text\n");
		return(__COUNTER__);
	}

	char* body = calloc((end - sizeof(hdr)) + 1, 1);
rswindell's avatar
rswindell committed
107
108
109
110
	if(body == NULL) {
		fprintf(stderr, "!MALLOC failure\n");
		return __COUNTER__;
	}
rswindell's avatar
rswindell committed
111
112
	fseek(fp, sizeof(hdr), SEEK_SET);
	fread(body, end - sizeof(hdr), 1, fp);
rswindell's avatar
rswindell committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	fprintf(bodyfp, "\n-start of message text-\n");
	char* p = body;
	while(*p) {
		if((p == body || *(p - 1) == '\r') && *p == 1) {
			fputc('@', ctrlfp);
			p++;
			while(*p && *p != '\r')
				fputc(*(p++), ctrlfp);
			if(*p)
				p++;
			fputc('\n', ctrlfp);
			continue;
		}
		for(; *p && *p != '\r'; p++) {
			if(*p != '\n')
				fputc(*p, bodyfp);
		}
		if(*p) {
			p++;
			fputc('\n', bodyfp);
		}
	}
	fprintf(bodyfp, "-end of message text-\n");
rswindell's avatar
rswindell committed
136

rswindell's avatar
rswindell committed
137
	free(body);
rswindell's avatar
rswindell committed
138
139
140
141
	printf("\n");
	return(0);
}

142
char* usage = "usage: fmsgdump [-body | -ctrl] <file1.msg> [file2.msg] [...]\n";
rswindell's avatar
rswindell committed
143
144
145
146
147
148
149

int main(int argc, char** argv)
{
	FILE*	fp;
	int		i;
	char	revision[16];

150
	sscanf("$Revision: 3.6 $", "%*s %s", revision);
rswindell's avatar
rswindell committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

	fprintf(stderr,"fmsgdump rev %s - Dump FidoNet Stored Messages\n\n"
		,revision
		);

	if(argc<2) {
		fprintf(stderr,"%s",usage);
		return -1;
	}

	if((nulfp=fopen(_PATH_DEVNULL,"w+"))==NULL) {
		perror(_PATH_DEVNULL);
		return -1;
	}
	bodyfp=nulfp;
rswindell's avatar
rswindell committed
166
	ctrlfp=nulfp;
rswindell's avatar
rswindell committed
167
168
169
170
171
172
173
174
175
176

	if(sizeof(fmsghdr_t)!=FIDO_STORED_MSG_HDR_LEN) {
		printf("sizeof(fmsghdr_t)=%" XP_PRIsize_t "u, expected: %d\n",sizeof(fmsghdr_t),FIDO_STORED_MSG_HDR_LEN);
		return(-1);
	}

	for(i=1;i<argc;i++) {
		if(argv[i][0]=='-') {
			switch(tolower(argv[i][1])) {
				case 'b':
rswindell's avatar
rswindell committed
177
					bodyfp=stdout;
178
					// fall-through
rswindell's avatar
rswindell committed
179
180
				case 'c':
					ctrlfp=stdout;
rswindell's avatar
rswindell committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
					break;
				default:
					printf("%s",usage);
					return(0);
			}
			continue;
		}
		fprintf(stdout,"Opening %s\n",argv[i]);
		if((fp=fopen(argv[i],"rb"))==NULL) {
			perror(argv[i]);
			continue;
		}
		msgdump(fp, argv[i]);
		fclose(fp);
	}

	return(0);
}