fmsgdump.c 4.24 KB
Newer Older
rswindell's avatar
rswindell committed
1
2
3
4
5
6
7
8
9
10
11
12
/* $Id$ */
// 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const char* fmsgattr_str(uint16_t attr)
{
	char str[64] = "";

#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 "";

	static char buf[64];
	sprintf(buf, "(%s)", str);
	return buf;
}

rswindell's avatar
rswindell committed
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
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
91
	printf("Attr: 0x%04hX %s\n", hdr.attr, fmsgattr_str(hdr.attr));
rswindell's avatar
rswindell committed
92
93
94
95
96
97
98
99
100
101
102
103
	printf("To  : %.*s (%u.%u/%u.%u)\n", (int)sizeof(hdr.to)-1, hdr.to
		,hdr.destzone, hdr.destnet, hdr.destnode, hdr.destpoint);
	printf("From: %.*s (%u.%u/%u.%u)\n", (int)sizeof(hdr.from)-1, hdr.from
		,hdr.origzone, hdr.orignet, hdr.orignode, hdr.origpoint);
	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
104
105
106
107
	if(body == NULL) {
		fprintf(stderr, "!MALLOC failure\n");
		return __COUNTER__;
	}
rswindell's avatar
rswindell committed
108
109
	fseek(fp, sizeof(hdr), SEEK_SET);
	fread(body, end - sizeof(hdr), 1, fp);
rswindell's avatar
rswindell committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	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
133

rswindell's avatar
rswindell committed
134
	free(body);
rswindell's avatar
rswindell committed
135
136
137
138
	printf("\n");
	return(0);
}

rswindell's avatar
rswindell committed
139
char* usage = "usage: fmsgdump [-body] [-ctrl] <file1.msg> [file2.msg] [...]\n";
rswindell's avatar
rswindell committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

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

	sscanf("$Revision$", "%*s %s", revision);

	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
163
	ctrlfp=nulfp;
rswindell's avatar
rswindell committed
164
165
166
167
168
169
170
171
172
173

	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
174
175
176
					bodyfp=stdout;
				case 'c':
					ctrlfp=stdout;
rswindell's avatar
rswindell committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
					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);
}