smbutil.c 42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* 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)		*
 *																			*
11
 * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * 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.33"
39
40
char	revision[16];
char	compiler[32];
41

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


47
#define NOANALYSIS		(1L<<0)
48
#define NOCRC			(1L<<1)
49
50
51
52
53
54
55

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

56
57
58
59
60
61
#if defined(_WIN32)
	#include <ctype.h>	/* isdigit() */
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
62
#include <stdio.h>
rswindell's avatar
rswindell committed
63
64
65
66
#include <time.h>		/* time */
#include <errno.h>		/* errno */
#include <string.h>		/* strrchr */
#include <ctype.h>		/* toupper */
67

68
#include "genwrap.h"	/* stricmp */
rswindell's avatar
rswindell committed
69
#include "dirwrap.h"	/* fexist */
rswindell's avatar
rswindell committed
70
#include "conwrap.h"	/* getch */
71
#include "filewrap.h"
72
#include "smblib.h"
73
#include "crc16.h"
74
#include "crc32.h"
75
#include "gen_defs.h"	/* MAX_PATH */
76
77
78
79
80

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

81
/* gets is dangerous */
82
#define gets(str)  fgets((str), sizeof(str), stdin)
83

84
85
#define CHSIZE_FP(fp,size)	if(chsize(fileno(fp),size)) printf("chsize failed!\n");

86
87
88
89
/********************/
/* Global variables */
/********************/

90
91
92
93
94
95
96
97
98
smb_t		smb;
ulong		mode=0L;
ushort		tzone=0;
ushort		xlat=XLAT_NONE;
FILE*		nulfp;
FILE*		errfp;
FILE*		statfp;
BOOL		pause_on_exit=FALSE;
BOOL		pause_on_error=FALSE;
99
char*		beep="";
100
101
102
103
104
105

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

char *usage=
106
"usage: smbutil [-opts] cmd <filespec.shd>\n"
107
108
109
110
111
"\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"
112
113
114
"       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"
rswindell's avatar
rswindell committed
115
"       h    = dump hash file\n"
116
117
"       s    = display msg base status\n"
"       c    = change msg base status\n"
118
"       d    = delete all msgs\n"
119
120
121
"       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"
122
"       c[m] = create message base if it doesn't exist (m=max msgs)\n"
123
"       a    = always pack msg base (disable compression analysis)\n"
124
"       i    = ignore dupes (do not store CRCs or search for duplicate hashes)\n"
125
"       d    = use default values (no prompt) for to, from, and subject\n"
126
"       l    = LZH-compress message text\n"
127
128
129
"       o    = print errors on stdout (instead of stderr)\n"
"       p    = wait for keypress (pause) on exit\n"
"       !    = wait for keypress (pause) on error\n"
130
"       b    = beep on error\n"
131
132
133
134
135
136
"       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"
137
"       z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
138
"       #    = set number of messages to view/list (e.g. -1)\n"
139
140
;

141
142
143
144
145
146
147
148
149
150
151
152
153
void bail(int code)
{

	if(pause_on_exit || (code && pause_on_error)) {
		fprintf(statfp,"\nHit enter to continue...");
		getchar();
	}

	if(code)
		fprintf(statfp,"\nReturning error code: %d\n",code);
	exit(code);
}

154
155
156
/*****************************************************************************/
// Expands Unix LF to CRLF
/*****************************************************************************/
157
ulong lf_expand(BYTE* inbuf, BYTE* outbuf)
158
159
{
	ulong	i,j;
rswindell's avatar
rswindell committed
160

161
	for(i=j=0;inbuf[i];i++) {
162
163
164
165
		if(inbuf[i]=='\n' && (!i || inbuf[i-1]!='\r'))
			outbuf[j++]='\r';
		outbuf[j++]=inbuf[i];
	}
166
	outbuf[j]=0;
167
168
	return(j);
}
rswindell's avatar
rswindell committed
169

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

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

200
	if(msgtxt!=NULL) {
201
202
203

		msgtxt[msgtxtlen]=0;	/* Must be NULL-terminated */

204
		if((newtxt=(char*)malloc((msgtxtlen*2)+1))==NULL) {
205
			fprintf(errfp,"\n%s!malloc(%ld) failure\n",beep,(msgtxtlen*2)+1);
206
			bail(1);
207
		}
rswindell's avatar
rswindell committed
208

209
210
211
212
213
		/* Expand LFs to CRLFs */
		msgtxtlen=lf_expand(msgtxt, newtxt);
		free(msgtxt);
		msgtxt=newtxt;
	}
rswindell's avatar
rswindell committed
214
215
216
217
218
219

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

220
221
	if(to==NULL) {
		printf("To User Name: ");
222
		fgets(str,sizeof(str),stdin); 
223
	} else
224
		SAFECOPY(str,to);
rswindell's avatar
rswindell committed
225
	truncsp(str);
226
227

	if((i=smb_hfield_str(&msg,RECIPIENT,str))!=SMB_SUCCESS) {
228
229
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,RECIPIENT,i,smb.last_error);
230
		bail(1); 
rswindell's avatar
rswindell committed
231
	}
rswindell's avatar
rswindell committed
232
233
234
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
235
236
237
238
		if(to_number==NULL) {
			printf("To User Number (0=QWKnet or Internet): ");
			gets(str);
		} else
239
			SAFECOPY(str,to_number);
rswindell's avatar
rswindell committed
240
		truncsp(str);
241
		if((i=smb_hfield_str(&msg,RECIPIENTEXT,str))!=SMB_SUCCESS) {
242
243
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,RECIPIENTEXT,i,smb.last_error);
244
			bail(1); 
245
		}
rswindell's avatar
rswindell committed
246
247
		msg.idx.to=atoi(str); 
	}
rswindell's avatar
rswindell committed
248
249
	else {
		strlwr(str);
250
		msg.idx.to=crc16(str,0); 
251
	}
rswindell's avatar
rswindell committed
252
253

	if(smb.status.attr&SMB_EMAIL && (type=='N' || !msg.idx.to)) {
254
255
256
257
		if(to_address==NULL) {
			printf("To Address: ");
			gets(str);
		} else
258
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
259
260
261
262
263
264
		truncsp(str);
		if(*str) {
			if(strchr(str,'.'))
				net=NET_INTERNET;
			else
				net=NET_QWK;
265
			if((i=smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net))!=SMB_SUCCESS) {
266
267
				fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTNETTYPE,i,smb.last_error);
268
				bail(1); 
rswindell's avatar
rswindell committed
269
			}
270
			if((i=smb_hfield_str(&msg,RECIPIENTNETADDR,str))!=SMB_SUCCESS) {
271
272
				fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTNETADDR,i,smb.last_error);
273
				bail(1); 
274
275
276
			} 
		} 
	}
rswindell's avatar
rswindell committed
277

278
279
280
281
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
282
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
283
	truncsp(str);
284
	if((i=smb_hfield_str(&msg,SENDER,str))!=SMB_SUCCESS) {
285
286
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SENDER,i,smb.last_error);
287
		bail(1); 
rswindell's avatar
rswindell committed
288
	}
rswindell's avatar
rswindell committed
289
	if(smb.status.attr&SMB_EMAIL) {
290
291
292
293
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
294
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
295
		truncsp(str);
296
		if((i=smb_hfield_str(&msg,SENDEREXT,str))!=SMB_SUCCESS) {
297
298
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,SENDEREXT,i,smb.last_error);
299
			bail(1); 
300
301
302
		}
		msg.idx.from=atoi(str); 
	} else {
rswindell's avatar
rswindell committed
303
		strlwr(str);
304
		msg.idx.from=crc16(str,0); 
305
	}
306
	if((i=smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent))!=SMB_SUCCESS) {
307
308
		fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
			,beep,SENDERAGENT,i,smb.last_error);
309
		bail(1);
310
	}
rswindell's avatar
rswindell committed
311

312
313
314
315
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
316
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
317
	truncsp(str);
318
	if((i=smb_hfield_str(&msg,SUBJECT,str))!=SMB_SUCCESS) {
319
320
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SUBJECT,i,smb.last_error);
321
		bail(1); 
322
	}
323
	msg.idx.subj=smb_subject_crc(str);
rswindell's avatar
rswindell committed
324

325
	sprintf(str,"SMBUTIL %s-%s r%s %s %s"
326
327
328
329
330
331
		,SMBUTIL_VER
		,PLATFORM_DESC
		,revision
		,__DATE__
		,compiler
		);
332
	if((i=smb_hfield_str(&msg,FIDOPID,str))!=SMB_SUCCESS) {
333
334
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,FIDOPID,i,smb.last_error);
335
		bail(1); 
336
	}
rswindell's avatar
rswindell committed
337

338
	if((i=smb_addmsg(&smb,&msg,smb.status.attr&SMB_HYPERALLOC
339
340
		,mode&NOCRC ? SMB_HASH_SOURCE_NONE : SMB_HASH_SOURCE_ALL
		,xlat,msgtxt,NULL))!=SMB_SUCCESS) {
341
342
		fprintf(errfp,"\n%s!smb_addmsg returned %d: %s\n"
			,beep,i,smb.last_error);
343
		bail(1); 
rswindell's avatar
rswindell committed
344
	}
rswindell's avatar
rswindell committed
345
	smb_freemsgmem(&msg);
346

347
	FREE_AND_NULL(msgtxt);
348
349
350
351
352
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
353
void showstatus(void)
354
355
356
{
	int i;

rswindell's avatar
rswindell committed
357
358
	i=smb_locksmbhdr(&smb);
	if(i) {
359
360
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
361
362
		return; 
	}
rswindell's avatar
rswindell committed
363
364
365
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
366
367
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
368
369
		return; 
	}
rswindell's avatar
rswindell committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
	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
		   );
385
386
387
388
389
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
390
void config(void)
391
392
393
394
{
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;

rswindell's avatar
rswindell committed
395
396
	i=smb_locksmbhdr(&smb);
	if(i) {
397
398
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
399
400
		return; 
	}
rswindell's avatar
rswindell committed
401
402
403
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
404
405
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
406
407
		return; 
	}
rswindell's avatar
rswindell committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	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) {
425
426
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
427
428
		return; 
	}
rswindell's avatar
rswindell committed
429
430
	i=smb_getstatus(&smb);
	if(i) {
431
432
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
433
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
434
435
		return; 
	}
rswindell's avatar
rswindell committed
436
437
438
439
440
441
442
443
444
445
446
	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);
447
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
448
	if(i)
449
450
		fprintf(errfp,"\n%s!smb_putstatus returned %d: %s\n"
			,beep,i,smb.last_error);
451
452
453
454
455
456
457
458
459
460
461
}

/****************************************************************************/
/* 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
462
463
464
465
466
467
468
469
	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) {
470
471
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
472
473
			break; 
		}
rswindell's avatar
rswindell committed
474
475
476
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
477
478
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
479
480
			break; 
		}
rswindell's avatar
rswindell committed
481
482
483
		printf("%4lu %-25.25s %-25.25s %.20s\n"
			,msg.hdr.number,msg.from,msg.to,msg.subj);
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
484
485
		l++; 
	}
486
487
488
489
490
491
492
493
494
495
}

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

	str[0]=0;
	for(i=0;i<length;i++)
496
497
		if(buf[i] && (buf[i]<' ' || buf[i]>=0x7f) 
			&& buf[i]!='\r' && buf[i]!='\n' && buf[i]!='\t')
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
			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()                                        */
/****************************************************************************/
516
char *my_timestr(time_t *intime)
517
518
519
520
521
{
    static char str[256];
    char mer[3],hour;
    struct tm *gm;

rswindell's avatar
rswindell committed
522
523
524
	gm=localtime(intime);
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
rswindell's avatar
rswindell committed
525
526
		return(str); 
	}
rswindell's avatar
rswindell committed
527
528
529
530
531
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
rswindell's avatar
rswindell committed
532
533
		strcpy(mer,"pm"); 
	}
rswindell's avatar
rswindell committed
534
535
536
537
538
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
rswindell's avatar
rswindell committed
539
540
		strcpy(mer,"am"); 
	}
rswindell's avatar
rswindell committed
541
542
543
544
	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);
545
546
547
548
549
550
551
552
553
554
555
}

/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
void viewmsgs(ulong start, ulong count)
{
	int i;
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
556
557
558
559
560
561
562
563
	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) {
564
565
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
566
567
			break; 
		}
rswindell's avatar
rswindell committed
568
569
570
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
571
572
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
573
574
			break; 
		}
575

576
577
578
		printf("--------------------\n");
		printf("%-20.20s %ld\n"		,"index record",ftell(smb.sid_fp)/sizeof(idxrec_t));
		smb_dump_msghdr(stdout,&msg);
rswindell's avatar
rswindell committed
579
580
		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
581
582
		l++; 
	}
583
584
}

585
586
587
588
/****************************************************************************/
/****************************************************************************/
void dump_hashes(void)
{
rswindell's avatar
rswindell committed
589
	char	tmp[128];
590
591
592
593
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
594
595
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
596
597
598
599
600
601
		return;
	}

	while(!smb_feof(smb.hash_fp)) {
		if(smb_fread(&smb,&hash,sizeof(hash),smb.hash_fp)!=sizeof(hash))
			break;
rswindell's avatar
rswindell committed
602
603
		printf("\n");
		printf("%-10s: %lu\n",		"Number",	hash.number);
604
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
rswindell's avatar
rswindell committed
605
		printf("%-10s: %s\n",		"Time",		my_timestr((time_t*)&hash.time));
rswindell's avatar
rswindell committed
606
607
608
609
610
611
612
		printf("%-10s: %x\n",		"Flags",	hash.flags);
		if(hash.flags&SMB_HASH_CRC16)
			printf("%-10s: %04x\n",	"CRC-16",	hash.crc16);
		if(hash.flags&SMB_HASH_CRC32)
			printf("%-10s: %08lx\n","CRC-32",	hash.crc32);
		if(hash.flags&SMB_HASH_MD5)
			printf("%-10s: %s\n",	"MD5",		MD5_hex(tmp,hash.md5));
613
	}
614
615

	smb_close_hash(&smb);
616
617
}

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/****************************************************************************/
/* 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) {
634
635
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
636
637
		return; 
	}
638
639
640
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
641
642
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
643
644
		return; 
	}
645
646
647
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
648
649
		return; 
	}
650
651
652
653
	printf("Loading index...\n");
	if((idx=(idxrec_t *)LMALLOC(sizeof(idxrec_t)*smb.status.total_msgs))
		==NULL) {
		smb_unlocksmbhdr(&smb);
654
655
		fprintf(errfp,"\n%s!Error allocating %lu bytes of memory\n"
			,beep,sizeof(idxrec_t)*smb.status.total_msgs);
rswindell's avatar
rswindell committed
656
657
		return; 
	}
658
659
660
661
662
	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))
rswindell's avatar
rswindell committed
663
664
			break; 
	}
665
666
667
668
	printf("\nDone.\n\n");

	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
rswindell's avatar
rswindell committed
669
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
670
		if(idx[m].attr&MSG_DELETE)
rswindell's avatar
rswindell committed
671
672
			flagged++; 
	}
673
674
675
676
677
678
	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++) {
rswindell's avatar
rswindell committed
679
			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
680
681
682
683
684
685
			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++;
rswindell's avatar
rswindell committed
686
687
688
689
690
				idx[m].attr|=MSG_DELETE; 
			} 
		}  /* mark for deletion */
		printf("\r100%% (%lu flagged for deletion)\n",f); 
	}
691
692
693

	printf("Scanning for read messages to be killed...\n");
	for(m=f=0;m<l;m++) {
rswindell's avatar
rswindell committed
694
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
695
696
697
698
699
		if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
			continue;
		if((idx[m].attr&(MSG_READ|MSG_KILLREAD))==(MSG_READ|MSG_KILLREAD)) {
			f++;
			flagged++;
rswindell's avatar
rswindell committed
700
701
702
			idx[m].attr|=MSG_DELETE; 
		} 
	}
703
704
	printf("\r100%% (%lu flagged for deletion)\n",f);

705
	if(smb.status.max_msgs && l-flagged>smb.status.max_msgs) {
706
707
708
709
710
711
		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++;
rswindell's avatar
rswindell committed
712
713
714
715
			idx[m].attr|=MSG_DELETE; 
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
716
717
718
719

	if(!flagged) {				/* No messages to delete */
		LFREE(idx);
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
720
721
		return; 
	}
722
723
724
725
726
727
728
729

	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);
730
731
				fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
					,beep,i,smb.last_error);
732
				bail(1); 
rswindell's avatar
rswindell committed
733
			}
734
735
736
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
737
738
				fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
					,beep,i,smb.last_error);
739
				bail(1); 
rswindell's avatar
rswindell committed
740
741
			} 
		}
742
743
744
745
746
747
748

		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) {
749
750
					fprintf(errfp,"\n%s!smb_getmsgidx returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
751
752
					continue; 
				}
753
754
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
755
756
					fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
757
758
					break; 
				}
759
760
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
761
762
					fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
763
764
					break; 
				}
765
766
767
768
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
769
770
					fprintf(errfp,"\n%s!smb_putmsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
771
772
					break; 
				}
773
774
775
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
776
777
					fprintf(errfp,"\n%s!smb_freemsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
778
779
780
781
782
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
783
784
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
785
786
787
788
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
789
790
791

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
792
	CHSIZE_FP(smb.sid_fp,0);
793
794
795
796
	for(m=n=0;m<l;m++) {
		if(idx[m].attr&MSG_DELETE)
			continue;
		printf("%lu of %lu\r",++n,l-flagged);
rswindell's avatar
rswindell committed
797
798
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
	}
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
	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;
822
	BOOL		error=FALSE;
823
824
825
826
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;

rswindell's avatar
rswindell committed
827
828
829
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
830
	if(i) {
831
832
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
833
834
		return; 
	}
rswindell's avatar
rswindell committed
835
	i=smb_getstatus(&smb);
836
837
	if(i) {
		smb_unlocksmbhdr(&smb);
838
839
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
840
841
		return; 
	}
rswindell's avatar
rswindell committed
842

843
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
844
845
846
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
847
848
			fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
849
850
			return; 
		}
rswindell's avatar
rswindell committed
851
852
853
854
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
855
856
			fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
857
858
859
			return; 
		} 
	}
860

rswindell's avatar
rswindell committed
861
862
863
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
864
		CHSIZE_FP(smb.shd_fp,smb.status.header_offset);
rswindell's avatar
rswindell committed
865
		rewind(smb.sdt_fp);
866
		CHSIZE_FP(smb.sdt_fp,0);
rswindell's avatar
rswindell committed
867
		rewind(smb.sid_fp);
868
		CHSIZE_FP(smb.sid_fp,0);
rswindell's avatar
rswindell committed
869
870
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
871
			CHSIZE_FP(smb.sha_fp,0);
rswindell's avatar
rswindell committed
872
			rewind(smb.sda_fp);
873
			CHSIZE_FP(smb.sda_fp,0);
rswindell's avatar
rswindell committed
874
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
875
876
			smb_close_da(&smb); 
		}
rswindell's avatar
rswindell committed
877
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
878
879
		return; 
	}
880
881


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

rswindell's avatar
rswindell committed
885
		length=filelength(fileno(smb.sda_fp));
886

rswindell's avatar
rswindell committed
887
888
		fseek(smb.sda_fp,0L,SEEK_SET);
		for(l=m=0;l<length;l+=2) {
rswindell's avatar
rswindell committed
889
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
rswindell's avatar
rswindell committed
890
891
892
893
			i=0;
			if(!fread(&i,2,1,smb.sda_fp))
				break;
			if(!i)
rswindell's avatar
rswindell committed
894
895
				m++; 
		}
896

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

rswindell's avatar
rswindell committed
899
		length=filelength(fileno(smb.sha_fp));
900

rswindell's avatar
rswindell committed
901
902
		fseek(smb.sha_fp,0L,SEEK_SET);
		for(l=n=0;l<length;l++) {
rswindell's avatar
rswindell committed
903
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
rswindell's avatar
rswindell committed
904
905
906
907
			ch=0;
			if(!fread(&ch,1,1,smb.sha_fp))
				break;
			if(!ch)
rswindell's avatar
rswindell committed
908
909
				n++; 
		}
rswindell's avatar
rswindell committed
910
911
912
913
914
915

		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
916
917
			return; 
		}
918

rswindell's avatar
rswindell committed
919
		if(packable && (m*SDT_BLOCK_LEN)+(n*SHD_BLOCK_LEN)<packable*1024L) {
920
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
921
922
923
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
924
925
			return; 
		}
rswindell's avatar
rswindell committed
926
927
928

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
929
930
				  ,m,m*SDT_BLOCK_LEN,n,n*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
931
932
933

	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		rewind(smb.sha_fp);
934
		CHSIZE_FP(smb.sha_fp,0);		/* Reset both allocation tables */
rswindell's avatar
rswindell committed
935
		rewind(smb.sda_fp);
936
		CHSIZE_FP(smb.sda_fp,0); 
rswindell's avatar
rswindell committed
937
	}
rswindell's avatar
rswindell committed
938
939
940
941
942
943
944

	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) {
rswindell's avatar
rswindell committed
945
			printf("\r%2lu%%  ",(long)(100.0/((float)length/l)));
rswindell's avatar
rswindell committed
946
947
948
949
			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;
rswindell's avatar
rswindell committed
950
951
				continue; 
			}
rswindell's avatar
rswindell committed
952
953
954
955
			if((i=smb_getmsghdr(&smb,&msg))!=0) {
				smb_unlockmsghdr(&smb,&msg);
				m++;
				size=SHD_BLOCK_LEN;
rswindell's avatar
rswindell committed
956
957
				continue; 
			}
rswindell's avatar
rswindell committed
958
959
960
			smb_unlockmsghdr(&smb,&msg);
			if(msg.hdr.attr&MSG_DELETE) {
				m+=smb_hdrblocks(msg.hdr.length);
rswindell's avatar
rswindell committed
961
962
963
964
965
				n+=smb_datblocks(smb_getmsgdatlen(&msg)); 
			}
			size=smb_hdrblocks(smb_getmsghdrlen(&msg))*SHD_BLOCK_LEN;
			smb_freemsgmem(&msg); 
		}
rswindell's avatar
rswindell committed
966
967
968
969
970


		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
971
972
			return; 
		}
rswindell's avatar
rswindell committed
973
974

		if(packable && (n*SDT_BLOCK_LEN)+(m*SHD_BLOCK_LEN)<packable*1024L) {
975
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
976
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
977
978
			return; 
		}
rswindell's avatar
rswindell committed
979
980
981

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
982
983
				  ,n,n*SDT_BLOCK_LEN,m,m*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
984
985
986
987
988
989
990
991
992
993
994

	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);
rswindell's avatar
rswindell committed
995
996
			smb_close_da(&smb); 
		}
997
		fprintf(errfp,"\n%s!Error opening temp files\n",beep);
rswindell's avatar
rswindell committed
998
999
		return; 
	}
rswindell's avatar
rswindell committed
1000
1001
1002
1003
1004
1005
1006
	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);
1007
1008
		smb_close_ha(&smb);
		smb_close_da(&smb);
rswindell's avatar
rswindell committed
1009
1010
1011
		fclose(tmp_sdt);
		fclose(tmp_shd);
		fclose(tmp_sid);
1012
		fprintf(errfp,"\n%s!Error allocating memory\n",beep);
rswindell's avatar
rswindell committed
1013
1014
		return; 
	}
rswindell's avatar
rswindell committed
1015
1016
1017
1018
1019
1020
	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 */
rswindell's avatar
rswindell committed
1021
1022
		fwrite(&ch,1,1,tmp_shd); 
	}
rswindell's avatar
rswindell committed
1023
1024
1025
1026
1027
1028
1029
1030
	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");
rswindell's avatar
rswindell committed
1031
1032
			continue; 
		}
rswindell's avatar
rswindell committed
1033
1034
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
1035
1036
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
1037
1038
			continue; 
		}
rswindell's avatar
rswindell committed
1039
		i=smb_getmsghdr(&smb,&msg);
1040
		smb_unlockmsghdr(&smb,&msg);
rswindell's avatar
rswindell committed
1041
		if(i) {
1042
1043
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
1044
1045
			continue; 
		}
1046
		if(msg.hdr.attr&MSG_DELETE) {
rswindell's avatar
rswindell committed
1047
1048
			printf("\nDeleted header.\n");
			smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
1049
1050
			continue; 
		}
1051
		if(msg.expiration && msg.expiration<=now) {
rswindell's avatar
rswindell committed
1052
1053
			printf("\nExpired message.\n");
			smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
1054
1055
			continue; 
		}
rswindell's avatar
rswindell committed
1056
1057
1058
1059
1060
1061
		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;
1062
			smb_incmsgdat(&smb,datoffset[m].new,smb_getmsgdatlen(&msg),1); 
rswindell's avatar
rswindell committed
1063
		} else {
1064

rswindell's avatar
rswindell committed
1065
1066
1067
1068
1069
1070
1071
			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) {