fmsgdump.c 4.5 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
// vi: tabstop=4

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

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

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
30 31 32 33
const char* fmsgattr_str(uint16_t attr)
{
	char str[64] = "";

34
	str[0] = '\0';
rswindell's avatar
rswindell committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#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 "";

54
	static char buf[128];
rswindell's avatar
rswindell committed
55 56 57 58
	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
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)
87
		fprintf(stderr,"%s Unterminated 'time' field\n", fname);
rswindell's avatar
rswindell committed
88

89
	TERMINATE(hdr.subj);
rswindell's avatar
rswindell committed
90
	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));
92
	TERMINATE(hdr.to);
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
	printf("Time: %.*s\n", (int)sizeof(hdr.time)-1, hdr.time);

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

106 107
	long len = end - sizeof(hdr);
	char* body = calloc(len + 1, 1);
rswindell's avatar
rswindell committed
108 109 110 111
	if(body == NULL) {
		fprintf(stderr, "!MALLOC failure\n");
		return __COUNTER__;
	}
rswindell's avatar
rswindell committed
112
	fseek(fp, sizeof(hdr), SEEK_SET);
113
	fread(body, len, 1, fp);
rswindell's avatar
rswindell committed
114 115
	fprintf(bodyfp, "\n-start of message text-\n");
	char* p = body;
116
	while(*p && p < body + len) {
rswindell's avatar
rswindell committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		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);
		}
	}
136 137 138 139
	if(p == (body + len) - 1)
		fprintf(bodyfp, "-end of message text-\n");
	else
		fprintf(bodyfp, "-PREMATURE end of message text-\n");
rswindell's avatar
rswindell committed
140

rswindell's avatar
rswindell committed
141
	free(body);
rswindell's avatar
rswindell committed
142 143 144 145
	printf("\n");
	return(0);
}

146
char* usage = "usage: fmsgdump [-body | -ctrl] <file1.msg> [file2.msg] [...]\n";
rswindell's avatar
rswindell committed
147 148 149 150 151 152 153

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

154
	sscanf("$Revision: 3.6 $", "%*s %s", revision);
rswindell's avatar
rswindell committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	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
170
	ctrlfp=nulfp;
rswindell's avatar
rswindell committed
171 172 173 174 175 176 177 178 179 180

	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
181
					bodyfp=stdout;
182
					// fall-through
rswindell's avatar
rswindell committed
183 184
				case 'c':
					ctrlfp=stdout;
rswindell's avatar
rswindell committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
					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);
}