smbutil.c 42.2 KB
Newer Older
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
/* smbutil.c */

/* Synchronet message base (SMB) utility */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

38
#define SMBUTIL_VER "2.30"
39
40
41
42
43
44
45
46
47

#define NOANALYSIS		(1L<<0)

#ifdef __WATCOMC__
	#define ffblk find_t
    #define findfirst(x,y,z) _dos_findfirst(x,z,y)
	#define findnext(x) _dos_findnext(x)
#endif

48
49
50
51
52
53
#if defined(_WIN32)
	#include <ctype.h>	/* isdigit() */
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
54
#include <stdio.h>
55
#include <time.h>	/* time */
56
#include <errno.h>	/* errno */
rswindell's avatar
rswindell committed
57
#include <string.h>	/* strrchr */
58

59
#include "genwrap.h"	/* stricmp */
rswindell's avatar
rswindell committed
60
#include "dirwrap.h"	/* fexist */
61
#include "filewrap.h"
62
#include "smblib.h"
63
#include "crc32.h"
64
#include "gen_defs.h"	/* MAX_PATH */
65
66
67
68
69

#ifdef __WATCOMC__
	#include <dos.h>
#endif

70
71
72
/* gets is dangerous */
#define gets(str)  fgets((str), sizeof(str) - 1, stdin)

73
74
75
76
77
78
79
80
81
82
83
84
85
/********************/
/* Global variables */
/********************/

smb_t smb;
ulong mode=0L;
ushort tzone=PST;

/************************/
/* Program usage/syntax */
/************************/

char *usage=
86
"usage: smbutil [-opts] cmd <filespec.shd>\n"
87
88
89
90
91
"\n"
"cmd:\n"
"       l[n] = list msgs starting at number n\n"
"       r[n] = read msgs starting at number n\n"
"       v[n] = view msg headers starting at number n\n"
92
93
94
"       i[f] = import msg from text file f (or use stdin)\n"
"       e[f] = import e-mail from text file f (or use stdin)\n"
"       n[f] = import netmail from text file f (or use stdin)\n"
95
96
97
98
99
"       s    = display msg base status\n"
"       c    = change msg base status\n"
"       m    = maintain msg base - delete old msgs and msgs over max\n"
"       p[k] = pack msg base (k specifies minimum packable Kbytes)\n"
"opts:\n"
rswindell's avatar
rswindell committed
100
"       c    = create message base if it doesn't exist\n"
101
"       a    = always pack msg base (disable compression analysis)\n"
102
103
104
105
106
107
"       t<s> = set 'to' user name for imported message\n"
"       n<s> = set 'to' netmail address for imported message\n"
"       u<s> = set 'to' user number for imported message\n"
"       f<s> = set 'from' user name for imported message\n"
"       e<s> = set 'from' user number for imported message\n"
"       s<s> = set 'subject' for imported message\n"
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"       z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
;

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
            ,"Jul","Aug","Sep","Oct","Nov","Dec"};

/****************************************************************************/
/* Updates 16-bit "rcrc" with character 'ch'                                */
/****************************************************************************/
void ucrc16(uchar ch, ushort *rcrc) {
	ushort i, cy;
    uchar nch=ch;
 
rswindell's avatar
rswindell committed
122
123
124
125
126
127
	for (i=0; i<8; i++) {
		cy=*rcrc & 0x8000;
		*rcrc<<=1;
		if (nch & 0x80) *rcrc |= 1;
		nch<<=1;
		if (cy) *rcrc ^= 0x1021; }
128
129
130
131
132
133
134
135
136
137
}

/****************************************************************************/
/* Returns CRC-16 of string (not including terminating NULL)				*/
/****************************************************************************/
ushort crc16(char *str)
{
	int 	i=0;
	ushort	crc=0;

rswindell's avatar
rswindell committed
138
139
140
141
142
143
	ucrc16(0,&crc);
	while(str[i])
		ucrc16(str[i++],&crc);
	ucrc16(0,&crc);
	ucrc16(0,&crc);
	return(crc);
144
145
146
147
}

void remove_re(char *str)
{
rswindell's avatar
rswindell committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	while(!strnicmp(str,"RE:",3)) {
		strcpy(str,str+3);
		while(str[0]==SP)
			strcpy(str,str+1); 
	}
}

/************************************************/
/* Truncates white-space chars off end of 'str' */
/************************************************/
static void truncsp(char *str)
{
	uint c;

	c=strlen(str);
	while(c && (uchar)str[c-1]<=' ') c--;
	str[c]=0;
165
166
}

rswindell's avatar
rswindell committed
167
168


169
170
171
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
172
173
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
174
{
175
176
177
178
179
	char	str[128];
	char	buf[128];
	char	pad=0;
	char*	msgtxt=NULL;
	long	msgtxtlen;
180
	ushort	xlat,net;
181
182
183
	int 	i;
	long	l,length;
	ulong	offset,crc;
184
185
	smbmsg_t	msg;

186
187
188
189
190
191
192
	/* Read message text from stream (file or stdin) */
	msgtxtlen=0;
	while(!feof(fp)) {
		i=fread(buf,1,sizeof(buf),fp);
		if(i<1)
			break;
		if((msgtxt=(char*)realloc(msgtxt,msgtxtlen+i))==NULL) {
193
			fprintf(stderr,"\n\7malloc(%ld) failure\n",msgtxtlen+i);
194
195
196
197
198
199
200
201
			exit(1);
		}
		memcpy(msgtxt+msgtxtlen,buf,i);
		msgtxtlen+=i;
	}

	/* Allocate space in message base */
	length=msgtxtlen+sizeof(xlat);	/* for translation string */
rswindell's avatar
rswindell committed
202
203
204
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		i=smb_open_da(&smb);
		if(i) {
205
			fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
206
207
			exit(1); 
		}
rswindell's avatar
rswindell committed
208
		offset=smb_allocdat(&smb,length,1);
209
210
		smb_close_da(&smb); 
	} else
rswindell's avatar
rswindell committed
211
212
213
214
		offset=smb_hallocdat(&smb);

	fseek(smb.sdt_fp,offset,SEEK_SET);
	xlat=XLAT_NONE;
215
216
217
218
	smb_fwrite(&xlat,sizeof(xlat),smb.sdt_fp);
	smb_fwrite(msgtxt,msgtxtlen,smb.sdt_fp);
	for(l=length;l%SDT_BLOCK_LEN;l++)
		smb_fwrite(&pad,1,smb.sdt_fp);
rswindell's avatar
rswindell committed
219
220
221
222
223
224
225
226
227
	fflush(smb.sdt_fp);

	memset(&msg,0,sizeof(smbmsg_t));
	msg.hdr.version=smb_ver();
	msg.hdr.when_written.time=time(NULL);
	msg.hdr.when_written.zone=tzone;
	msg.hdr.when_imported=msg.hdr.when_written;

	if(smb.status.max_crcs) {
228
229
230
231
		crc=0xffffffffUL;
		for(l=0;l<msgtxtlen;l++) 
			crc=ucrc32(msgtxt[l],crc);
		crc=~crc;
rswindell's avatar
rswindell committed
232
233
		i=smb_addcrc(&smb,crc);
		if(i) {
234
			fprintf(stderr,"\n\7!smb_addcrc returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
235
			smb_freemsgdat(&smb,offset,length,1);
236
237
238
			exit(1); 
		} 
	}
rswindell's avatar
rswindell committed
239
240
241

	msg.hdr.offset=offset;

242
243
244
245
246
247
	if(to==NULL) {
		printf("To User Name: ");
		fgets(str,sizeof(str)-1,stdin); 
	} else
		sprintf(str,"%.*s",sizeof(str)-1,to);

rswindell's avatar
rswindell committed
248
249
	truncsp(str);
	i=smb_hfield(&msg,RECIPIENT,(ushort)strlen(str),str);
250
	if(i) {
251
		fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
252
253
		smb_freemsgdat(&smb,offset,length,1);
		exit(1); }
rswindell's avatar
rswindell committed
254
255
256
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
257
258
259
260
261
		if(to_number==NULL) {
			printf("To User Number (0=QWKnet or Internet): ");
			gets(str);
		} else
			sprintf(str,"%.*s",sizeof(str)-1,to_number);
rswindell's avatar
rswindell committed
262
263
		truncsp(str);
		i=smb_hfield(&msg,RECIPIENTEXT,(ushort)strlen(str),str);
264
		if(i) {
265
			fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
266
			smb_freemsgdat(&smb,offset,length,1);
267
268
			exit(1); 
		}
rswindell's avatar
rswindell committed
269
270
271
		msg.idx.to=atoi(str); }
	else {
		strlwr(str);
272
273
		msg.idx.to=crc16(str); 
	}
rswindell's avatar
rswindell committed
274
275

	if(smb.status.attr&SMB_EMAIL && (type=='N' || !msg.idx.to)) {
276
277
278
279
280
		if(to_address==NULL) {
			printf("To Address: ");
			gets(str);
		} else
			sprintf(str,"%.*s",sizeof(str)-1,to_address);
rswindell's avatar
rswindell committed
281
282
283
284
285
286
287
288
		truncsp(str);
		if(*str) {
			if(strchr(str,'.'))
				net=NET_INTERNET;
			else
				net=NET_QWK;
			i=smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net);
			if(i) {
289
				fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
290
291
292
293
				smb_freemsgdat(&smb,offset,length,1);
				exit(1); }
			i=smb_hfield(&msg,RECIPIENTNETADDR,(ushort)strlen(str),str);
			if(i) {
294
				fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
295
				smb_freemsgdat(&smb,offset,length,1);
296
297
298
299
				exit(1); 
			} 
		} 
	}
rswindell's avatar
rswindell committed
300

301
302
303
304
305
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
		sprintf(str,"%.*s",sizeof(str)-1,from);
rswindell's avatar
rswindell committed
306
307
308
	truncsp(str);
	i=smb_hfield(&msg,SENDER,(ushort)strlen(str),str);
	if(i) {
309
		fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
310
311
312
		smb_freemsgdat(&smb,offset,length,1);
		exit(1); }
	if(smb.status.attr&SMB_EMAIL) {
313
314
315
316
317
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
			sprintf(str,"%.*s",sizeof(str)-1,from_number);
rswindell's avatar
rswindell committed
318
319
		truncsp(str);
		i=smb_hfield(&msg,SENDEREXT,(ushort)strlen(str),str);
320
		if(i) {
321
			fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
322
			smb_freemsgdat(&smb,offset,length,1);
323
324
325
326
			exit(1); 
		}
		msg.idx.from=atoi(str); 
	} else {
rswindell's avatar
rswindell committed
327
		strlwr(str);
328
329
		msg.idx.from=crc16(str); 
	}
rswindell's avatar
rswindell committed
330

331
332
333
334
335
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
		sprintf(str,"%.*s",sizeof(str)-1,subject);
rswindell's avatar
rswindell committed
336
337
	truncsp(str);
	i=smb_hfield(&msg,SUBJECT,(ushort)strlen(str),str);
338
	if(i) {
339
		fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
340
		smb_freemsgdat(&smb,offset,length,1);
341
342
		exit(1); 
	}
rswindell's avatar
rswindell committed
343
	remove_re(str);
344
	strlwr(str);
rswindell's avatar
rswindell committed
345
346
347
348
	msg.idx.subj=crc16(str);

	i=smb_dfield(&msg,TEXT_BODY,length);
	if(i) {
349
		fprintf(stderr,"\n\7!smb_dfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
350
351
352
353
354
355
		smb_freemsgdat(&smb,offset,length,1);
		exit(1); }

	i=smb_addmsghdr(&smb,&msg,smb.status.attr&SMB_HYPERALLOC);

	if(i) {
356
		fprintf(stderr,"\n\7!smb_addmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
357
358
359
		smb_freemsgdat(&smb,offset,length,1);
		exit(1); }
	smb_freemsgmem(&msg);
360

361
	free(msgtxt);
362
363
364
365
366
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
367
void showstatus(void)
368
369
370
{
	int i;

rswindell's avatar
rswindell committed
371
372
	i=smb_locksmbhdr(&smb);
	if(i) {
373
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
374
375
376
377
		return; }
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
378
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
		return; }
	printf("last_msg        =%lu\n"
		   "total_msgs      =%lu\n"
		   "header_offset   =%lu\n"
		   "max_crcs        =%lu\n"
		   "max_msgs        =%lu\n"
		   "max_age         =%u\n"
		   "attr            =%04Xh\n"
		   ,smb.status.last_msg
		   ,smb.status.total_msgs
		   ,smb.status.header_offset
		   ,smb.status.max_crcs
		   ,smb.status.max_msgs
		   ,smb.status.max_age
		   ,smb.status.attr
		   );
395
396
397
398
399
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
400
void config(void)
401
402
403
404
{
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;

rswindell's avatar
rswindell committed
405
406
	i=smb_locksmbhdr(&smb);
	if(i) {
407
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
408
409
410
411
		return; }
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
412
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
		return; }
	printf("Header offset =%-5lu  New value (CR=No Change): "
		,smb.status.header_offset);
	gets(header_offset);
	printf("Max msgs      =%-5lu  New value (CR=No Change): "
		,smb.status.max_msgs);
	gets(max_msgs);
	printf("Max crcs      =%-5lu  New value (CR=No Change): "
		,smb.status.max_crcs);
	gets(max_crcs);
	printf("Max age       =%-5u  New value (CR=No Change): "
		,smb.status.max_age);
	gets(max_age);
	printf("Attributes    =%-5u  New value (CR=No Change): "
		,smb.status.attr);
	gets(attr);
	i=smb_locksmbhdr(&smb);
	if(i) {
431
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
432
433
434
		return; }
	i=smb_getstatus(&smb);
	if(i) {
435
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
436
437
438
439
440
441
442
443
444
445
446
447
448
		smb_unlocksmbhdr(&smb);
		return; }
	if(isdigit(max_msgs[0]))
		smb.status.max_msgs=atol(max_msgs);
	if(isdigit(max_crcs[0]))
		smb.status.max_crcs=atol(max_crcs);
	if(isdigit(max_age[0]))
		smb.status.max_age=atoi(max_age);
	if(isdigit(header_offset[0]))
		smb.status.header_offset=atol(header_offset);
	if(isdigit(attr[0]))
		smb.status.attr=atoi(attr);
	i=smb_putstatus(&smb);
449
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
450
	if(i)
451
		fprintf(stderr,"\n\7!smb_putstatus returned %d: %s\n",i,smb.last_error);
452
453
454
455
456
457
458
459
460
461
462
}

/****************************************************************************/
/* Lists messages' to, from, and subject                                    */
/****************************************************************************/
void listmsgs(ulong start, ulong count)
{
	int i;
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
463
464
465
466
467
468
469
470
	if(!start)
		start=1;
	fseek(smb.sid_fp,(start-1L)*sizeof(idxrec_t),SEEK_SET);
	while(l<count) {
		if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
471
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
472
473
474
475
			break; }
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
476
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
477
478
479
480
481
			break; }
		printf("%4lu %-25.25s %-25.25s %.20s\n"
			,msg.hdr.number,msg.from,msg.to,msg.subj);
		smb_freemsgmem(&msg);
		l++; }
482
483
484
485
486
487
488
489
490
491
}

/****************************************************************************/
/* Returns an ASCII string for FidoNet address 'addr'                       */
/****************************************************************************/
char *faddrtoa(fidoaddr_t addr)
{
	static char str[25];
	char point[25];

492
	sprintf(str,"%hu:%hu/%hu",addr.zone,addr.net,addr.node);
rswindell's avatar
rswindell committed
493
494
495
496
	if(addr.point) {
		sprintf(point,".%u",addr.point);
		strcat(str,point); }
	return(str);
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
}

char *binstr(uchar *buf, ushort length)
{
	static char str[512];
	char tmp[128];
	int i;

	str[0]=0;
	for(i=0;i<length;i++)
		if(buf[i] && (buf[i]<SP || buf[i]>=0x7f) && buf[i]!=CR && buf[i]!=LF)
			break;
	if(i==length)		/* not binary */
		return(buf);
	for(i=0;i<length;i++) {
		sprintf(tmp,"%02X ",buf[i]);
		strcat(str,tmp); 
		if(i>=100) {
			strcat(str,"...");
			break;
		}
	}
	return(str);
}

/****************************************************************************/
/* Generates a 24 character ASCII string that represents the time_t pointer */
/* Used as a replacement for ctime()                                        */
/****************************************************************************/
char *timestr(time_t *intime)
{
    static char str[256];
    char mer[3],hour;
    struct tm *gm;

rswindell's avatar
rswindell committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
	gm=localtime(intime);
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
		return(str); }
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
		strcpy(mer,"pm"); }
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
		strcpy(mer,"am"); }
	sprintf(str,"%s %s %02d %4d %02d:%02d %s"
		,wday[gm->tm_wday],mon[gm->tm_mon],gm->tm_mday,1900+gm->tm_year
		,hour,gm->tm_min,mer);
	return(str);
552
553
554
555
556
557
558
559
560
561
}


/****************************************************************************/
/* Converts when_t.zone into ASCII format									*/
/****************************************************************************/
char *zonestr(short zone)
{
	static char str[32];

rswindell's avatar
rswindell committed
562
	switch((ushort)zone) {
563
		case 0: 	return("UTC");
rswindell's avatar
rswindell committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
		case AST:	return("AST");
		case EST:	return("EST");
		case CST:	return("CST");
		case MST:	return("MST");
		case PST:	return("PST");
		case YST:	return("YST");
		case HST:	return("HST");
		case BST:	return("BST");
		case ADT:	return("ADT");
		case EDT:	return("EDT");
		case CDT:	return("CDT");
		case MDT:	return("MDT");
		case PDT:	return("PDT");
		case YDT:	return("YDT");
		case HDT:	return("HDT");
		case BDT:	return("BDT");
		case MID:	return("MID");
		case VAN:	return("VAN");
		case EDM:	return("EDM");
		case WIN:	return("WIN");
		case BOG:	return("BOG");
		case CAR:	return("CAR");
		case RIO:	return("RIO");
		case FER:	return("FER");
		case AZO:	return("AZO");
		case LON:	return("LON");
		case BER:	return("BER");
		case ATH:	return("ATH");
		case MOS:	return("MOS");
		case DUB:	return("DUB");
		case KAB:	return("KAB");
		case KAR:	return("KAR");
		case BOM:	return("BOM");
		case KAT:	return("KAT");
		case DHA:	return("DHA");
		case BAN:	return("BAN");
		case HON:	return("HON");
		case TOK:	return("TOK");
		case SYD:	return("SYD");
		case NOU:	return("NOU");
		case WEL:	return("WEL");
		}
606

rswindell's avatar
rswindell committed
607
608
	sprintf(str,"%02d:%02u",zone/60,zone<0 ? (-zone)%60 : zone%60);
	return(str);
609
610
611
612
613
614
615
616
617
618
619
620
621
622
}
			 

/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
void viewmsgs(ulong start, ulong count)
{
	char when_written[128]
		,when_imported[128];
	int i;
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
623
624
625
626
627
628
629
630
	if(!start)
		start=1;
	fseek(smb.sid_fp,(start-1L)*sizeof(idxrec_t),SEEK_SET);
	while(l<count) {
		if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
631
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
632
633
634
635
			break; }
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
636
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
637
			break; }
638

rswindell's avatar
rswindell committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
		sprintf(when_written,"%.24s %s"
			,timestr((time_t*)&msg.hdr.when_written.time)
			,zonestr(msg.hdr.when_written.zone));
		sprintf(when_imported,"%.24s %s"
			,timestr((time_t*)&msg.hdr.when_imported.time)
			,zonestr(msg.hdr.when_imported.zone));

		printf( "%-20.20s %s\n"
				"%-20.20s %s\n"
				"%-20.20s %s\n"
				"%-20.20s %04Xh\n"
				"%-20.20s %04Xh\n"
				"%-20.20s %u\n"
				"%-20.20s %04Xh\n"
				"%-20.20s %08lXh\n"
				"%-20.20s %08lXh\n"
				"%-20.20s %s\n"
				"%-20.20s %s\n"
				"%-20.20s %ld (%ld)\n"
				"%-20.20s %ld\n"
				"%-20.20s %ld\n"
				"%-20.20s %ld\n"
				"%-20.20s %s\n"
				"%-20.20s %06lXh\n"
				"%-20.20s %u\n",

			"subj",
			msg.subj,

			"from",
			msg.from,

			"to",
			msg.to,

			"type",
			msg.hdr.type,

			"version",
			msg.hdr.version,

			"length",
			msg.hdr.length,

			"attr",
			msg.hdr.attr,

			"auxattr",
			msg.hdr.auxattr,

			"netattr",
			msg.hdr.netattr,

			"when_written",
			when_written,

			"when_imported",
			when_imported,

			"number",
			msg.hdr.number,
			ftell(smb.sid_fp)/sizeof(idxrec_t),

			"thread_orig",
			msg.hdr.thread_orig,

			"thread_next",
			msg.hdr.thread_next,

			"thread_first",
			msg.hdr.thread_first,

			"reserved[16]",
			binstr(msg.hdr.reserved,16),

			"offset",
			msg.hdr.offset,

			"total_dfields",
			msg.hdr.total_dfields
			);
		for(i=0;i<msg.hdr.total_dfields;i++)
			printf("dfield[%02u].type      %02Xh\n"
				   "dfield[%02u].offset    %lu\n"
				   "dfield[%02u].length    %lu\n"
				   ,i,msg.dfield[i].type
				   ,i,msg.dfield[i].offset
				   ,i,msg.dfield[i].length);

		for(i=0;i<msg.total_hfields;i++)
			printf("hfield[%02u].type      %02Xh\n"
				   "hfield[%02u].length    %d\n"
				   "hfield[%02u]_dat       %s\n"
				   ,i,msg.hfield[i].type
				   ,i,msg.hfield[i].length
				   ,i,binstr(msg.hfield_dat[i],msg.hfield[i].length));
735

rswindell's avatar
rswindell committed
736
737
738
739
740
741
		if(msg.from_net.type)
			printf("from_net.type        %02Xh\n"
				   "from_net.addr        %s\n"
				,msg.from_net.type
				,msg.from_net.type==NET_FIDO
				? faddrtoa(*(fidoaddr_t *)msg.from_net.addr) : msg.from_net.addr);
742

rswindell's avatar
rswindell committed
743
744
745
746
747
748
		if(msg.to_net.type)
			printf("to_net.type          %02Xh\n"
				   "to_net.addr          %s\n"
				,msg.to_net.type
				,msg.to_net.type==NET_FIDO
				? faddrtoa(*(fidoaddr_t *)msg.to_net.addr) : msg.to_net.addr);
749

rswindell's avatar
rswindell committed
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
		if(msg.replyto_net.type)
			printf("replyto_net.type     %02Xh\n"
				   "replyto_net.addr     %s\n"
				,msg.replyto_net.type
				,msg.replyto_net.type==NET_FIDO
				? faddrtoa(*(fidoaddr_t *)msg.replyto_net.addr)
					: msg.replyto_net.addr);

		printf("from_agent           %02Xh\n"
			   "to_agent             %02Xh\n"
			   "replyto_agent        %02Xh\n"
			   ,msg.from_agent
			   ,msg.to_agent
			   ,msg.replyto_agent);

		printf("\n");
		smb_freemsgmem(&msg);
		l++; }
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
}

/****************************************************************************/
/* Maintain message base - deletes messages older than max age (in days)	*/
/* or messages that exceed maximum											*/
/****************************************************************************/
void maint(void)
{
	int i;
	ulong l,m,n,f,flagged=0;
	time_t now;
	smbmsg_t msg;
	idxrec_t HUGE16 *idx;

	printf("Maintaining %s\r\n",smb.file);
	now=time(NULL);
	i=smb_locksmbhdr(&smb);
	if(i) {
786
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
787
788
789
790
		return; }
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
791
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
792
793
794
795
796
797
798
799
800
		return; }
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
		return; }
	printf("Loading index...\n");
	if((idx=(idxrec_t *)LMALLOC(sizeof(idxrec_t)*smb.status.total_msgs))
		==NULL) {
		smb_unlocksmbhdr(&smb);
801
		fprintf(stderr,"\n\7!Error allocating %lu bytes of memory\n"
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
			,sizeof(idxrec_t)*smb.status.total_msgs);
		return; }
	fseek(smb.sid_fp,0L,SEEK_SET);
	for(l=0;l<smb.status.total_msgs;l++) {
		printf("%lu of %lu\r"
			,l+1,smb.status.total_msgs);
		if(!fread(&idx[l],1,sizeof(idxrec_t),smb.sid_fp))
			break; }
	printf("\nDone.\n\n");

	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
		printf("\r%2u%%",m ? (long)(100.0/((float)l/m)) : 0);
		if(idx[m].attr&MSG_DELETE)
			flagged++; }
	printf("\r100%% (%lu pre-flagged for deletion)\n",flagged);

	if(smb.status.max_age) {
		printf("Scanning for messages more than %u days old...\n"
			,smb.status.max_age);
		for(m=f=0;m<l;m++) {
			printf("\r%2u%%",m ? (long)(100.0/((float)l/m)) : 0);
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
			if((ulong)now>idx[m].time && (now-idx[m].time)/(24L*60L*60L)
				>smb.status.max_age) {
				f++;
				flagged++;
				idx[m].attr|=MSG_DELETE; } }  /* mark for deletion */
		printf("\r100%% (%lu flagged for deletion)\n",f); }

	printf("Scanning for read messages to be killed...\n");
	for(m=f=0;m<l;m++) {
		printf("\r%2u%%",m ? (long)(100.0/((float)l/m)) : 0);
		if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
			continue;
		if((idx[m].attr&(MSG_READ|MSG_KILLREAD))==(MSG_READ|MSG_KILLREAD)) {
			f++;
			flagged++;
			idx[m].attr|=MSG_DELETE; } }
	printf("\r100%% (%lu flagged for deletion)\n",f);

	if(l-flagged>smb.status.max_msgs) {
		printf("Flagging excess messages for deletion...\n");
		for(m=n=0,f=flagged;l-flagged>smb.status.max_msgs && m<l;m++) {
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
			printf("%lu of %lu\r",++n,(l-f)-smb.status.max_msgs);
			flagged++;
			idx[m].attr|=MSG_DELETE; }			/* mark for deletion */
		printf("\nDone.\n\n"); }

	if(!flagged) {				/* No messages to delete */
		LFREE(idx);
		smb_unlocksmbhdr(&smb);
		return; }

	if(!(mode&NOANALYSIS)) {

		printf("Freeing allocated header and data blocks for deleted messages...\n");
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			i=smb_open_da(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
866
				fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
867
868
869
870
				exit(1); }
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
871
				fprintf(stderr,"\n\7!smb_open_ha returned %d: %s\n",i,smb.last_error);
872
873
874
875
876
877
878
879
				exit(1); } }

		for(m=n=0;m<l;m++) {
			if(idx[m].attr&MSG_DELETE) {
				printf("%lu of %lu\r",++n,flagged);
				msg.idx=idx[m];
				msg.hdr.number=msg.idx.number;
				if((i=smb_getmsgidx(&smb,&msg))!=0) {
880
					fprintf(stderr,"\n\7!smb_getmsgidx returned %d: %s\n",i,smb.last_error);
881
882
883
					continue; }
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
884
					fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
885
886
887
					break; }
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
888
					fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
889
890
891
892
893
					break; }
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
894
					fprintf(stderr,"\n\7!smb_putmsg returned %d: %s\n",i,smb.last_error);
895
896
897
898
					break; }
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
899
					fprintf(stderr,"\n\7!smb_freemsg returned %d: %s\n",i,smb.last_error);
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
					break; }
				smb_freemsgmem(&msg); } }
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
			smb_close_da(&smb); }
		printf("\nDone.\n\n"); }

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
	if(chsize(fileno(smb.sid_fp),0L))
		printf("chsize failed!\n");
	for(m=n=0;m<l;m++) {
		if(idx[m].attr&MSG_DELETE)
			continue;
		printf("%lu of %lu\r",++n,l-flagged);
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); }
	printf("\nDone.\n\n");
	fflush(smb.sid_fp);

	LFREE(idx);
	smb.status.total_msgs-=flagged;
	smb_putstatus(&smb);
	smb_unlocksmbhdr(&smb);
}


typedef struct {
	ulong old,new;
	} datoffset_t;

/****************************************************************************/
/* Removes all unused blocks from SDT and SHD files 						*/
/****************************************************************************/
void packmsgs(ulong packable)
{
	uchar str[128],buf[SDT_BLOCK_LEN],ch,fname[128],tmpfname[128];
	int i,size;
	ulong l,m,n,datoffsets=0,length,total,now;
	FILE *tmp_sdt,*tmp_shd,*tmp_sid;
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;

rswindell's avatar
rswindell committed
943
944
945
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
946
	if(i) {
947
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
948
		return; }
rswindell's avatar
rswindell committed
949
	i=smb_getstatus(&smb);
950
951
	if(i) {
		smb_unlocksmbhdr(&smb);
952
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
953
954
		return; }

955
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
956
957
958
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
959
			fprintf(stderr,"\n\7!smb_open_ha returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
960
961
962
963
964
			return; }
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
965
			fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
966
			return; } }
967

rswindell's avatar
rswindell committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
		chsize(fileno(smb.shd_fp),smb.status.header_offset);
		rewind(smb.sdt_fp);
		chsize(fileno(smb.sdt_fp),0L);
		rewind(smb.sid_fp);
		chsize(fileno(smb.sid_fp),0L);
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
			chsize(fileno(smb.sha_fp),0L);
			rewind(smb.sda_fp);
			chsize(fileno(smb.sda_fp),0L);
			smb_close_ha(&smb);
			smb_close_da(&smb); }
		smb_unlocksmbhdr(&smb);
		return; }
985
986


rswindell's avatar
rswindell committed
987
988
	if(!(smb.status.attr&SMB_HYPERALLOC) && !(mode&NOANALYSIS)) {
		printf("Analyzing data blocks...\n");
989

rswindell's avatar
rswindell committed
990
		length=filelength(fileno(smb.sda_fp));
991

rswindell's avatar
rswindell committed
992
993
994
995
996
997
998
999
		fseek(smb.sda_fp,0L,SEEK_SET);
		for(l=m=0;l<length;l+=2) {
			printf("\r%2u%%  ",l ? (long)(100.0/((float)length/l)) : 0);
			i=0;
			if(!fread(&i,2,1,smb.sda_fp))
				break;
			if(!i)
				m++; }
1000

rswindell's avatar
rswindell committed
1001
		printf("\rAnalyzing header blocks...\n");
1002

rswindell's avatar
rswindell committed
1003
		length=filelength(fileno(smb.sha_fp));
1004

rswindell's avatar
rswindell committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
		fseek(smb.sha_fp,0L,SEEK_SET);
		for(l=n=0;l<length;l++) {
			printf("\r%2u%%  ",l ? (long)(100.0/((float)length/l)) : 0);
			ch=0;
			if(!fread(&ch,1,1,smb.sha_fp))
				break;
			if(!ch)
				n++; }

		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
			return; }
1020

rswindell's avatar
rswindell committed
1021
		if(packable && (m*SDT_BLOCK_LEN)+(n*SHD_BLOCK_LEN)<packable*1024L) {
1022
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
			return; }

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
				  ,m,m*SDT_BLOCK_LEN,n,n*SHD_BLOCK_LEN); }

	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		rewind(smb.sha_fp);
		chsize(fileno(smb.sha_fp),0L);		/* Reset both allocation tables */
		rewind(smb.sda_fp);
		chsize(fileno(smb.sda_fp),0L); }

	if(smb.status.attr&SMB_HYPERALLOC && !(mode&NOANALYSIS)) {
		printf("Analyzing %s\n",smb.file);

		length=filelength(fileno(smb.shd_fp));
		m=n=0;
		for(l=smb.status.header_offset;l<length;l+=size) {
			printf("\r%2u%%  ",(long)(100.0/((float)length/l)));
			msg.idx.offset=l;
			if((i=smb_lockmsghdr(&smb,&msg))!=0) {
				printf("\n(%06lX) smb_lockmsghdr returned %d\n",l,i);
				size=SHD_BLOCK_LEN;
				continue; }
			if((i=smb_getmsghdr(&smb,&msg))!=0) {
				smb_unlockmsghdr(&smb,&msg);
				m++;
				size=SHD_BLOCK_LEN;
				continue; }
			smb_unlockmsghdr(&smb,&msg);
			if(msg.hdr.attr&MSG_DELETE) {
				m+=smb_hdrblocks(msg.hdr.length);
				total=0;
				for(i=0;i<msg.hdr.total_dfields;i++)
					total+=msg.dfield[i].length;
				n+=smb_datblocks(total); }
			size=smb_getmsghdrlen(&msg);
			if(size<1) size=SHD_BLOCK_LEN;
			while(size%SHD_BLOCK_LEN)
				size++;
			smb_freemsgmem(&msg); }


		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_unlocksmbhdr(&smb);
			return; }

		if(packable && (n*SDT_BLOCK_LEN)+(m*SHD_BLOCK_LEN)<packable*1024L) {
1075
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
			smb_unlocksmbhdr(&smb);
			return; }

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
				  ,n,n*SDT_BLOCK_LEN,m,m*SHD_BLOCK_LEN); }

	sprintf(fname,"%s.sd$",smb.file);
	tmp_sdt=fopen(fname,"wb");
	sprintf(fname,"%s.sh$",smb.file);
	tmp_shd=fopen(fname,"wb");
	sprintf(fname,"%s.si$",smb.file);
	tmp_sid=fopen(fname,"wb");
	if(!tmp_sdt || !tmp_shd || !tmp_sid) {
		smb_unlocksmbhdr(&smb);
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
			smb_close_da(&smb); }
1094
		fprintf(stderr,"\n\7!Error opening temp files\n");
rswindell's avatar
rswindell committed
1095
1096
1097
1098
1099
1100
1101
1102
		return; }
	setvbuf(tmp_sdt,NULL,_IOFBF,2*1024);
	setvbuf(tmp_shd,NULL,_IOFBF,2*1024);
	setvbuf(tmp_sid,NULL,_IOFBF,2*1024);
	if(!(smb.status.attr&SMB_HYPERALLOC)
		&& (datoffset=(datoffset_t *)LMALLOC(sizeof(datoffset_t)*smb.status.total_msgs))
		==NULL) {
		smb_unlocksmbhdr(&smb);
1103
1104
		smb_close_ha(&smb);
		smb_close_da(&smb);
rswindell's avatar
rswindell committed
1105
1106
1107
		fclose(tmp_sdt);
		fclose(tmp_shd);
		fclose(tmp_sid);
1108
		fprintf(stderr,"\n\7!Error allocating memory\n");
1109
		return; }
rswindell's avatar
rswindell committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
	fseek(smb.shd_fp,0L,SEEK_SET);
	fread(&hdr,1,sizeof(smbhdr_t),smb.shd_fp);
	fwrite(&hdr,1,sizeof(smbhdr_t),tmp_shd);
	fwrite(&(smb.status),1,sizeof(smbstatus_t),tmp_shd);
	for(l=sizeof(smbhdr_t)+sizeof(smbstatus_t);l<smb.status.header_offset;l++) {
		fread(&ch,1,1,smb.shd_fp);			/* copy additional base header records */
		fwrite(&ch,1,1,tmp_shd); }
	fseek(smb.sid_fp,0L,SEEK_SET);
	total=0;
	for(l=0;l<smb.status.total_msgs;l++) {
		printf("%lu of %lu\r",l+1,smb.status.total_msgs);
		if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
			break;
		if(msg.idx.attr&MSG_DELETE) {
			printf("\nDeleted index.\n");
1125
			continue; }
rswindell's avatar
rswindell committed
1126
1127
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
1128
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
1129
			continue; }
rswindell's avatar
rswindell committed
1130
		i=smb_getmsghdr(&smb,&msg);
1131
		smb_unlockmsghdr(&smb,&msg);
rswindell's avatar
rswindell committed
1132
		if(i) {
1133
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1134
			continue; }
1135
		if(msg.hdr.attr&MSG_DELETE) {
rswindell's avatar
rswindell committed
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
			printf("\nDeleted header.\n");
			smb_freemsgmem(&msg);
			continue; }
		if(msg.expiration.time && msg.expiration.time<=now) {
			printf("\nExpired message.\n");
			smb_freemsgmem(&msg);
			continue; }
		for(m=0;m<datoffsets;m++)
			if(msg.hdr.offset==datoffset[m].old)
				break;
		if(m<datoffsets) {				/* another index pointed to this data */
			printf("duplicate index\n");
			msg.hdr.offset=datoffset[m].new;
			smb_incdat(&smb,datoffset[m].new,smb_getmsgdatlen(&msg),1); }
		else {
1151

rswindell's avatar
rswindell committed
1152
1153
1154
1155
1156
1157
1158
			if(!(smb.status.attr&SMB_HYPERALLOC))
				datoffset[datoffsets].old=msg.hdr.offset;

			fseek(smb.sdt_fp,msg.hdr.offset,SEEK_SET);

			m=smb_getmsgdatlen(&msg);
			if(m>16L*1024L*1024L) {
1159
				fprintf(stderr,"\n\7!Invalid data length (%lu)\n",m);
rswindell's avatar
rswindell committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
				continue; }

			if(!(smb.status.attr&SMB_HYPERALLOC)) {
				datoffset[datoffsets].new=msg.hdr.offset
					=smb_fallocdat(&smb,m,1);
				datoffsets++;
				fseek(tmp_sdt,msg.hdr.offset,SEEK_SET); }
			else {
				fseek(tmp_sdt,0L,SEEK_END);
				msg.hdr.offset=ftell(tmp_sdt); }

			/* Actually copy the data */

			n=smb_datblocks(m);
			for(m=0;m<n;m++) {
				fread(buf,1,SDT_BLOCK_LEN,smb.sdt_fp);
				if(!m && *(ushort *)buf!=XLAT_NONE && *(ushort *)buf!=XLAT_LZH) {
					printf("\nUnsupported translation type (%04X)\n"
						,*(ushort *)buf);
					break; }
				fwrite(buf,1,SDT_BLOCK_LEN,tmp_sdt); }
			if(m<n)
				continue; }

		/* Write the new index entry */
		length=smb_getmsghdrlen(&msg);
		if(smb.status.attr&SMB_HYPERALLOC)
			msg.idx.offset=ftell(tmp_shd);
		else
			msg.idx.offset=smb_fallochdr(&smb,length)+smb.status.header_offset;
		msg.idx.number=msg.hdr.number;
		msg.idx.attr=msg.hdr.attr;
		msg.idx.time=msg.hdr.when_imported.time;
		sprintf(str,"%.128s",msg.subj);
		strlwr(str);
		remove_re(str);
		msg.idx.subj=crc16(str);
		if(smb.status.attr&SMB_EMAIL) {
			if(msg.to_ext)
				msg.idx.to=atoi(msg.to_ext);
			else
				msg.idx.to=0;
			if(msg.from_ext)
				msg.idx.from=atoi(msg.from_ext);
			else
				msg.idx.from=0; }
		else {
			sprintf(str,"%.128s",msg.to);
			strlwr(str);
			msg.idx.to=crc16(str);
			sprintf(str,"%.128s",msg.from);
			strlwr(str);
			msg.idx.from=crc16(str); }
		fwrite(&msg.idx,1,sizeof(idxrec_t),tmp_sid);

		/* Write the new header entry */
		fseek(tmp_shd,msg.idx.offset,SEEK_SET);
		fwrite(&msg.hdr,1,sizeof(msghdr_t),tmp_shd);
		for(n=0;n<msg.hdr.total_dfields;n++)
			fwrite(&msg.dfield[n],1,sizeof(dfield_t),tmp_shd);
		for(n=0;n<msg.total_hfields;n++) {
			fwrite(&msg.hfield[n],1,sizeof(hfield_t),tmp_shd);
			fwrite(msg.hfield_dat[n],1,msg.hfield[n].length,tmp_shd); }
		while(length%SHD_BLOCK_LEN) {	/* pad with NULLs */
			fputc(0,tmp_shd);
			length++; }
		total++;
		smb_freemsgmem(&msg); }
1228

rswindell's avatar
rswindell committed
1229
1230
	if(datoffset)
		LFREE(datoffset);
1231
1232
1233
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		smb_close_ha(&smb);
		smb_close_da(&smb); }
rswindell's avatar
rswindell committed
1234
1235
1236

	/* Change *.sh$ into *.shd */
	fclose(smb.shd_fp);
1237
	fclose(tmp_shd);
rswindell's avatar
rswindell committed
1238
1239
	sprintf(fname,"%s.shd",smb.file);
	if(remove(fname)!=0)
1240
		fprintf(stderr,"\n\7!Error %d removing %s\n",errno,fname);
rswindell's avatar
rswindell committed
1241
1242
	sprintf(tmpfname,"%s.sh$",smb.file);
	if(rename(tmpfname,fname)!=0)
1243
		fprintf(stderr,"\n\7!Error %d renaming %s to %s\n",errno,tmpfname,fname);
1244
1245


rswindell's avatar
rswindell committed
1246
1247
1248
1249
1250
	/* Change *.sd$ into *.sdt */
	fclose(smb.sdt_fp);
	fclose(tmp_sdt);
	sprintf(fname,"%s.sdt",smb.file);
	if(remove(fname)!=0)
1251
		fprintf(stderr,"\n\7!Error %d removing %s\n",errno,fname);
1252

rswindell's avatar
rswindell committed
1253
1254
	sprintf(tmpfname,"%s.sd$",smb.file);
	if(rename(tmpfname,fname)!=0)
1255
		fprintf(stderr,"\n\7!Error %d renaming %s to %s\n",errno,tmpfname,fname);
1256

rswindell's avatar
rswindell committed
1257
1258
1259
1260
1261
	/* Change *.si$ into *.sid */
	fclose(smb.sid_fp);
	fclose(tmp_sid);
	sprintf(fname,"%s.sid",smb.file);
	if(remove(fname)!=0)
1262
		fprintf(stderr,"\n\7!Error %d removing %s\n",errno,fname);
1263

rswindell's avatar
rswindell committed
1264
1265
	sprintf(tmpfname,"%s.si$",smb.file);
	if(rename(tmpfname,fname)!=0)
1266
		fprintf(stderr,"\n\7!Error %d renaming %s to %s\n",errno,tmpfname,fname);
1267

rswindell's avatar
rswindell committed
1268
	if((i=smb_open(&smb))!=0) {
1269
		fprintf(stderr,"\n\7!Error %d reopening %s\n",i,smb.file);
rswindell's avatar
rswindell committed
1270
		return; }
1271

rswindell's avatar
rswindell committed
1272
1273
	smb.status.total_msgs=total;
	if((i=smb_putstatus(&smb))!=0)
1274
		fprintf(stderr,"\n\7!smb_putstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1275
	printf("\nDone.\n\n");
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
}


/****************************************************************************/
/* Read messages in message base											*/
/****************************************************************************/
void readmsgs(ulong start)
{
	char	HUGE16 *inbuf;
	int 	i,done=0,domsg=1;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
	if(start)
		msg.offset=start-1;
	else
		msg.offset=0;
	while(!done) {
		if(domsg) {
			fseek(smb.sid_fp,msg.offset*sizeof(idxrec_t),SEEK_SET);
			if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
				break;
			i=smb_lockmsghdr(&smb,&msg);
			if(i) {
1299
				fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1300
1301
1302
				break; }
			i=smb_getmsghdr(&smb,&msg);
			if(i) {
1303
				fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1304
				break; }
1305

rswindell's avatar
rswindell committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
			printf("\n%lu (%lu)\n",msg.hdr.number,msg.offset+1);
			printf("Subj : %s\n",msg.subj);
			printf("To   : %s",msg.to);
			if(msg.to_net.type)
				printf(" (%s)",msg.to_net.type==NET_FIDO
					? faddrtoa(*(fidoaddr_t *)msg.to_net.addr) : msg.to_net.addr);
			printf("\nFrom : %s",msg.from);
			if(msg.from_net.type)
				printf(" (%s)",msg.from_net.type==NET_FIDO
					? faddrtoa(*(fidoaddr_t *)msg.from_net.addr)
						: msg.from_net.addr);
			printf("\nDate : %.24s %s"
				,timestr((time_t*)&msg.hdr.when_written.time)
				,zonestr(msg.hdr.when_written.zone));

			printf("\n\n");

			if((inbuf=smb_getmsgtxt(&smb,&msg,GETMSGTXT_TAILS))!=NULL) {
				printf("%s",inbuf);
				FREE(inbuf); }

			i=smb_unlockmsghdr(&smb,&msg);
			if(i) {
1329
				fprintf(stderr,"\n\7!smb_unlockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
				break; }
			smb_freemsgmem(&msg); }
		domsg=1;
		printf("\nReading %s (?=Menu): ",smb.file);
		switch(toupper(getch())) {
			case '?':
				printf("\n"
					   "\n"
					   "(R)e-read current message\n"
					   "(L)ist messages\n"
					   "(T)en more titles\n"
					   "(V)iew message headers\n"
					   "(Q)uit\n"
					   "(+/-) Forward/Backward\n"
					   "\n");
				domsg=0;
				break;
			case 'Q':
				printf("Quit\n");
				done=1;
				break;
			case 'R':
				printf("Re-read\n");
				break;
			case '-':
				printf("Backwards\n");
				if(msg.offset)
					msg.offset--;
				break;
			case 'T':
				printf("Ten titles\n");
				listmsgs(msg.offset+2,10);
				msg.offset+=10;
				domsg=0;
				break;
			case 'L':
				printf("List messages\n");
				listmsgs(1,-1);
				domsg=0;
				break;
			case 'V':
				printf("View message headers\n");
				viewmsgs(1,-1);
				domsg=0;
				break;
			case CR:
			case '+':
				printf("Next\n");
				msg.offset++;
				break; } }
1380
1381
}

1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
time_t checktime(void)
{
	struct tm tm;

    memset(&tm,0,sizeof(tm));
    tm.tm_year=94;
    tm.tm_mday=1;
    return(mktime(&tm)-0x2D24BD00L);
}

1392
1393
1394
1395
1396
/***************/
/* Entry point */
/***************/
int main(int argc, char **argv)
{
1397
	char	cmd[128]="",*p,*s;
1398
	char	path[MAX_PATH+1];
1399
1400
1401
1402
1403
1404
1405
	char*	to=NULL;
	char*	to_number=NULL;
	char*	to_address=NULL;
	char*	from=NULL;
	char*	from_number=NULL;
	char*	subj=NULL;
	FILE*	fp;
1406
1407
1408
1409
	int		i,j,x,y;
	time_t	t;
	BOOL	create=FALSE;

rswindell's avatar
rswindell committed
1410
	setvbuf(stdout,0,_IONBF,0);
1411

rswindell's avatar
rswindell committed
1412
	smb.file[0]=0;
1413
	fprintf(stderr,"\nSMBUTIL Version %s (%s) SMBLIB %s - Synchronet Message Base "\
rswindell's avatar
rswindell committed
1414
1415
		"Utility\n\n"
		,SMBUTIL_VER
1416
#if defined(__linux__)
rswindell's avatar
rswindell committed
1417
		,"Linux"
1418
#elif defined(__unix__)
rswindell's avatar
rswindell committed
1419
		,"Unix"
1420
#elif defined(__OS2__)
rswindell's avatar
rswindell committed
1421
		,"OS/2"
1422
#elif defined(_WIN32)
rswindell's avatar
rswindell committed
1423
		,"Win32"
1424
#elif defined(__DOS4G__)
rswindell's avatar
rswindell committed
1425
		,"DOS4G"
1426
#elif defined(__FLAT__)
rswindell's avatar
rswindell committed
1427
		,"DOS32"
1428
#else
rswindell's avatar
rswindell committed
1429
		,"DOS16"
1430
#endif
rswindell's avatar
rswindell committed
1431
1432
		,smb_lib_ver()
		);
1433

1434