smbutil.c 43 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 2003 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

#define NOANALYSIS		(1L<<0)
43
#define NOCRC			(1L<<1)
44
45
46
47
48
49
50

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

51
52
53
54
55
56
#if defined(_WIN32)
	#include <ctype.h>	/* isdigit() */
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
57
#include <stdio.h>
rswindell's avatar
rswindell committed
58
59
60
61
#include <time.h>		/* time */
#include <errno.h>		/* errno */
#include <string.h>		/* strrchr */
#include <ctype.h>		/* toupper */
62

63
#include "sbbs.h"
64
#include "genwrap.h"	/* stricmp */
rswindell's avatar
rswindell committed
65
#include "dirwrap.h"	/* fexist */
rswindell's avatar
rswindell committed
66
#include "conwrap.h"	/* getch */
67
#include "filewrap.h"
68
#include "smblib.h"
69
#include "crc32.h"
70
#include "gen_defs.h"	/* MAX_PATH */
71
72
73
74
75

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

76
/* gets is dangerous */
77
#define gets(str)  fgets((str), sizeof(str), stdin)
78

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

81
82
83
84
85
86
/********************/
/* Global variables */
/********************/

smb_t smb;
ulong mode=0L;
87
ushort tzone=0;
88
89
90
91
92
93

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

char *usage=
94
"usage: smbutil [-opts] cmd <filespec.shd>\n"
95
96
97
98
99
"\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"
100
101
102
"       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"
103
104
"       s    = display msg base status\n"
"       c    = change msg base status\n"
105
"       d    = delete all msgs\n"
106
107
108
"       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
109
"       c    = create message base if it doesn't exist\n"
110
"       a    = always pack msg base (disable compression analysis)\n"
111
"       i    = ignore duplicate messages (do not store CRC)\n"
112
"       d    = use default values (no prompt) for to, from, and subject\n"
113
114
115
116
117
118
"       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"
119
"       z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
120
"       #    = set number of messages to view/list (e.g. -1)\n"
121
122
;

123
124
125
126
127
128
/*****************************************************************************/
// Expands Unix LF to CRLF
/*****************************************************************************/
ulong lf_expand(BYTE* inbuf, ulong inlen, BYTE* outbuf)
{
	ulong	i,j;
rswindell's avatar
rswindell committed
129

130
131
132
133
134
135
136
	for(i=j=0;i<inlen;i++) {
		if(inbuf[i]=='\n' && (!i || inbuf[i-1]!='\r'))
			outbuf[j++]='\r';
		outbuf[j++]=inbuf[i];
	}
	return(j);
}
rswindell's avatar
rswindell committed
137

138
139
140
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
141
142
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
143
{
144
145
146
147
	char	str[128];
	char	buf[128];
	char	pad=0;
	char*	msgtxt=NULL;
148
	char*	newtxt;
149
	long	msgtxtlen;
150
	ushort	xlat,net;
151
152
153
	int 	i;
	long	l,length;
	ulong	offset,crc;
154
	ushort		agent=AGENT_SMBUTIL;
155
156
	smbmsg_t	msg;

157
158
159
160
161
162
163
	/* 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) {
164
			fprintf(stderr,"\n\7malloc(%ld) failure\n",msgtxtlen+i);
165
166
167
168
169
170
			exit(1);
		}
		memcpy(msgtxt+msgtxtlen,buf,i);
		msgtxtlen+=i;
	}

171
172
173
174
175
176
177
178
179
180
	if((newtxt=(char*)malloc(msgtxtlen*2))==NULL) {
		fprintf(stderr,"\n\7malloc(%ld) failure\n",msgtxtlen*2);
		exit(1);
	}

	/* Expand LFs to CRLFs */
	msgtxtlen=lf_expand(msgtxt, msgtxtlen, newtxt);
	free(msgtxt);
	msgtxt=newtxt;

181
182
	/* Allocate space in message base */
	length=msgtxtlen+sizeof(xlat);	/* for translation string */
rswindell's avatar
rswindell committed
183
184
185
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		i=smb_open_da(&smb);
		if(i) {
186
			fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
187
188
			exit(1); 
		}
rswindell's avatar
rswindell committed
189
		offset=smb_allocdat(&smb,length,1);
190
191
		smb_close_da(&smb); 
	} else
rswindell's avatar
rswindell committed
192
193
194
195
		offset=smb_hallocdat(&smb);

	fseek(smb.sdt_fp,offset,SEEK_SET);
	xlat=XLAT_NONE;
196
197
	smb_fwrite(&smb,&xlat,sizeof(xlat),smb.sdt_fp);
	smb_fwrite(&smb,msgtxt,msgtxtlen,smb.sdt_fp);
198
	for(l=length;l%SDT_BLOCK_LEN;l++)
199
		smb_fwrite(&smb,&pad,sizeof(pad),smb.sdt_fp);
rswindell's avatar
rswindell committed
200
201
202
203
204
205
206
207
	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;

208
	if(smb.status.max_crcs && !(mode&NOCRC)) {
209
210
211
212
		crc=0xffffffffUL;
		for(l=0;l<msgtxtlen;l++) 
			crc=ucrc32(msgtxt[l],crc);
		crc=~crc;
rswindell's avatar
rswindell committed
213
214
		i=smb_addcrc(&smb,crc);
		if(i) {
215
			fprintf(stderr,"\n\7!smb_addcrc returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
216
			smb_freemsgdat(&smb,offset,length,1);
217
218
219
			exit(1); 
		} 
	}
rswindell's avatar
rswindell committed
220
221
222

	msg.hdr.offset=offset;

223
224
	if(to==NULL) {
		printf("To User Name: ");
225
		fgets(str,sizeof(str),stdin); 
226
	} else
227
		SAFECOPY(str,to);
228

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

	if(smb.status.attr&SMB_EMAIL && (type=='N' || !msg.idx.to)) {
259
260
261
262
		if(to_address==NULL) {
			printf("To Address: ");
			gets(str);
		} else
263
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
264
265
266
267
268
269
270
271
		truncsp(str);
		if(*str) {
			if(strchr(str,'.'))
				net=NET_INTERNET;
			else
				net=NET_QWK;
			i=smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net);
			if(i) {
272
				fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
273
				smb_freemsgdat(&smb,offset,length,1);
rswindell's avatar
rswindell committed
274
275
				exit(1); 
			}
276
			i=smb_hfield_str(&msg,RECIPIENTNETADDR,str);
rswindell's avatar
rswindell committed
277
			if(i) {
278
				fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
279
				smb_freemsgdat(&smb,offset,length,1);
280
281
282
283
				exit(1); 
			} 
		} 
	}
rswindell's avatar
rswindell committed
284

285
286
287
288
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
289
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
290
	truncsp(str);
291
	i=smb_hfield_str(&msg,SENDER,str);
rswindell's avatar
rswindell committed
292
	if(i) {
293
		fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
294
		smb_freemsgdat(&smb,offset,length,1);
rswindell's avatar
rswindell committed
295
296
		exit(1); 
	}
rswindell's avatar
rswindell committed
297
	if(smb.status.attr&SMB_EMAIL) {
298
299
300
301
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
302
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
303
		truncsp(str);
304
		i=smb_hfield_str(&msg,SENDEREXT,str);
305
		if(i) {
306
			fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
307
			smb_freemsgdat(&smb,offset,length,1);
308
309
310
311
			exit(1); 
		}
		msg.idx.from=atoi(str); 
	} else {
rswindell's avatar
rswindell committed
312
		strlwr(str);
313
		msg.idx.from=crc16(str,0); 
314
	}
315
	smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent);
rswindell's avatar
rswindell committed
316

317
318
319
320
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
321
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
322
	truncsp(str);
323
	i=smb_hfield_str(&msg,SUBJECT,str);
324
	if(i) {
325
		fprintf(stderr,"\n\7!smb_hfield returned %d: %s\n",i,smb.last_error);
326
		smb_freemsgdat(&smb,offset,length,1);
327
328
		exit(1); 
	}
329
	msg.idx.subj=subject_crc(str);
rswindell's avatar
rswindell committed
330
331
332

	i=smb_dfield(&msg,TEXT_BODY,length);
	if(i) {
333
		fprintf(stderr,"\n\7!smb_dfield returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
334
		smb_freemsgdat(&smb,offset,length,1);
rswindell's avatar
rswindell committed
335
336
		exit(1); 
	}
rswindell's avatar
rswindell committed
337

338
	sprintf(str,"SMBUTIL %s-%s r%s %s %s"
339
340
341
342
343
344
		,SMBUTIL_VER
		,PLATFORM_DESC
		,revision
		,__DATE__
		,compiler
		);
345
	smb_hfield_str(&msg,FIDOPID,str);
346

rswindell's avatar
rswindell committed
347
348
349
	i=smb_addmsghdr(&smb,&msg,smb.status.attr&SMB_HYPERALLOC);

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

356
	free(msgtxt);
357
358
359
360
361
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
362
void showstatus(void)
363
364
365
{
	int i;

rswindell's avatar
rswindell committed
366
367
	i=smb_locksmbhdr(&smb);
	if(i) {
368
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
369
370
		return; 
	}
rswindell's avatar
rswindell committed
371
372
373
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
374
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
375
376
		return; 
	}
rswindell's avatar
rswindell committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
	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
		   );
392
393
394
395
396
}

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

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

/****************************************************************************/
/* 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
464
465
466
467
468
469
470
471
	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) {
472
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
473
474
			break; 
		}
rswindell's avatar
rswindell committed
475
476
477
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
478
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",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
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
565
566
			break; 
		}
rswindell's avatar
rswindell committed
567
568
569
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
570
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
571
572
			break; 
		}
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
		printf("%-16.16s %ld\n"		,"index record",ftell(smb.sid_fp)/sizeof(idxrec_t));
		printf("%-16.16s %ld\n"		,"number"			,msg.hdr.number);

		/* convenience strings */
		if(msg.subj)
			printf("%-16.16s %s\n"	,"subject"			,msg.subj);
		if(msg.to) {
			printf("%-16.16s %s"	,"to"				,msg.to);
			if(msg.to_net.addr && strchr(msg.to,'@')==NULL)
				printf(" (%s)",net_addr(&msg.to_net)); 
			if(msg.to_ext)
				printf(" #%s",msg.to_ext);
			printf("\n");
		}
		if(msg.from) {
			printf("%-16.16s %s"	,"from"				,msg.from);
			if(msg.from_net.addr && strchr(msg.from,'@')==NULL)
				printf(" (%s)",net_addr(&msg.from_net)); 
			if(msg.from_ext)
				printf(" #%s",msg.from_ext);
			printf("\n");
		}
		if(msg.replyto) {
			printf("%-16.16s %s"	,"reply-to"			,msg.replyto);
			if(msg.replyto_net.addr && strchr(msg.replyto,'@')==NULL)
				printf(" (%s)",net_addr(&msg.replyto_net)); 
			if(msg.replyto_ext)
				printf(" #%s",msg.replyto_ext);
			printf("\n");
		}
		if(msg.summary)
			printf("%-16.16s %s\n"	,"summary"			,msg.summary);

		/* optional fixed fields */
		if(msg.hdr.thread_orig)
			printf("%-16.16s %ld\n"	,"thread_orig"		,msg.hdr.thread_orig);
		if(msg.hdr.thread_next)
			printf("%-16.16s %ld\n"	,"thread_next"		,msg.hdr.thread_next);
		if(msg.hdr.thread_first)
			printf("%-16.16s %ld\n"	,"thread_first"		,msg.hdr.thread_first);
		if(msg.hdr.delivery_attempts)
			printf("%-16.16s %hu\n"	,"delivery_attempts",msg.hdr.delivery_attempts);
		if(msg.hdr.times_downloaded)
			printf("%-16.16s %lu\n"	,"times_downloaded"	,msg.hdr.times_downloaded);
		if(msg.hdr.last_downloaded)
			printf("%-16.16s %s\n"	,"last_downloaded"	,my_timestr((time_t*)&msg.hdr.last_downloaded));

		/* convenience integers */
		if(msg.cost)
			printf("%-16.16s %lu\n"	,"cost"				,msg.cost);
		if(msg.priority)
			printf("%-16.16s %lu\n"	,"priority"			,msg.priority);
		if(msg.expiration)
			printf("%-16.16s %s\n"	,"expiration"	
				,my_timestr((time_t *)&msg.expiration));

		printf("%-16.16s %s %s\n"	,"when_written"	
			,my_timestr((time_t *)&msg.hdr.when_written.time), zonestr(msg.hdr.when_written.zone));
		printf("%-16.16s %s %s\n"	,"when_imported"	
			,my_timestr((time_t *)&msg.hdr.when_imported.time), zonestr(msg.hdr.when_imported.zone));
		printf("%-16.16s %04Xh\n"	,"type"				,msg.hdr.type);
		printf("%-16.16s %04Xh\n"	,"version"			,msg.hdr.version);
		printf("%-16.16s %04Xh\n"	,"attr"				,msg.hdr.attr);
		printf("%-16.16s %08lXh\n"	,"auxattr"			,msg.hdr.auxattr);
		printf("%-16.16s %08lXh\n"	,"netattr"			,msg.hdr.netattr);
		printf("%-16.16s %06lXh\n"	,"header offset"	,msg.idx.offset);
		printf("%-16.16s %u\n"		,"header length"	,msg.hdr.length);

		/* variable fields */
		for(i=0;i<msg.total_hfields;i++)
			printf("%-16.16s %s\n"
				,smb_hfieldtype(msg.hfield[i].type)
				,binstr((uchar *)msg.hfield_dat[i],msg.hfield[i].length));
rswindell's avatar
rswindell committed
647

648
		printf("%-16.16s %06lXh\n"	,"data offset"		,msg.hdr.offset);
rswindell's avatar
rswindell committed
649
		for(i=0;i<msg.hdr.total_dfields;i++)
650
651
652
653
654
			printf("data field[%u]    %s, offset %lu, length %lu\n"
				,i
				,smb_dfieldtype(msg.dfield[i].type)
				,msg.dfield[i].offset
				,msg.dfield[i].length);
rswindell's avatar
rswindell committed
655
656
657

		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
658
659
		l++; 
	}
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
}

/****************************************************************************/
/* 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) {
678
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
679
680
		return; 
	}
681
682
683
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
684
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
685
686
		return; 
	}
687
688
689
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
690
691
		return; 
	}
692
693
694
695
	printf("Loading index...\n");
	if((idx=(idxrec_t *)LMALLOC(sizeof(idxrec_t)*smb.status.total_msgs))
		==NULL) {
		smb_unlocksmbhdr(&smb);
696
		fprintf(stderr,"\n\7!Error allocating %lu bytes of memory\n"
697
			,sizeof(idxrec_t)*smb.status.total_msgs);
rswindell's avatar
rswindell committed
698
699
		return; 
	}
700
701
702
703
704
	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
705
706
			break; 
	}
707
708
709
710
	printf("\nDone.\n\n");

	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
rswindell's avatar
rswindell committed
711
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
712
		if(idx[m].attr&MSG_DELETE)
rswindell's avatar
rswindell committed
713
714
			flagged++; 
	}
715
716
717
718
719
720
	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
721
			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
722
723
724
725
726
727
			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
728
729
730
731
732
				idx[m].attr|=MSG_DELETE; 
			} 
		}  /* mark for deletion */
		printf("\r100%% (%lu flagged for deletion)\n",f); 
	}
733
734
735

	printf("Scanning for read messages to be killed...\n");
	for(m=f=0;m<l;m++) {
rswindell's avatar
rswindell committed
736
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
737
738
739
740
741
		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
742
743
744
			idx[m].attr|=MSG_DELETE; 
		} 
	}
745
746
	printf("\r100%% (%lu flagged for deletion)\n",f);

747
	if(smb.status.max_msgs && l-flagged>smb.status.max_msgs) {
748
749
750
751
752
753
		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
754
755
756
757
			idx[m].attr|=MSG_DELETE; 
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
758
759
760
761

	if(!flagged) {				/* No messages to delete */
		LFREE(idx);
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
762
763
		return; 
	}
764
765
766
767
768
769
770
771

	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);
772
				fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
773
774
				exit(1); 
			}
775
776
777
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
778
				fprintf(stderr,"\n\7!smb_open_ha returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
779
780
781
				exit(1); 
			} 
		}
782
783
784
785
786
787
788

		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) {
789
					fprintf(stderr,"\n\7!smb_getmsgidx returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
790
791
					continue; 
				}
792
793
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
794
					fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
795
796
					break; 
				}
797
798
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
799
					fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
800
801
					break; 
				}
802
803
804
805
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
806
					fprintf(stderr,"\n\7!smb_putmsg returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
807
808
					break; 
				}
809
810
811
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
812
					fprintf(stderr,"\n\7!smb_freemsg returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
813
814
815
816
817
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
818
819
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
820
821
822
823
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
824
825
826

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
827
	CHSIZE_FP(smb.sid_fp,0);
828
829
830
831
	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
832
833
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
	}
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
	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;
857
	BOOL		error=FALSE;
858
859
860
861
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;

rswindell's avatar
rswindell committed
862
863
864
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
865
	if(i) {
866
		fprintf(stderr,"\n\7!smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
867
868
		return; 
	}
rswindell's avatar
rswindell committed
869
	i=smb_getstatus(&smb);
870
871
	if(i) {
		smb_unlocksmbhdr(&smb);
872
		fprintf(stderr,"\n\7!smb_getstatus returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
873
874
		return; 
	}
rswindell's avatar
rswindell committed
875

876
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
877
878
879
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
880
			fprintf(stderr,"\n\7!smb_open_ha returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
881
882
			return; 
		}
rswindell's avatar
rswindell committed
883
884
885
886
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
887
			fprintf(stderr,"\n\7!smb_open_da returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
888
889
890
			return; 
		} 
	}
891

rswindell's avatar
rswindell committed
892
893
894
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
895
		CHSIZE_FP(smb.shd_fp,smb.status.header_offset);
rswindell's avatar
rswindell committed
896
		rewind(smb.sdt_fp);
897
		CHSIZE_FP(smb.sdt_fp,0);
rswindell's avatar
rswindell committed
898
		rewind(smb.sid_fp);
899
		CHSIZE_FP(smb.sid_fp,0);
rswindell's avatar
rswindell committed
900
901
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
902
			CHSIZE_FP(smb.sha_fp,0);
rswindell's avatar
rswindell committed
903
			rewind(smb.sda_fp);
904
			CHSIZE_FP(smb.sda_fp,0);
rswindell's avatar
rswindell committed
905
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
906
907
			smb_close_da(&smb); 
		}
rswindell's avatar
rswindell committed
908
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
909
910
		return; 
	}
911
912


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

rswindell's avatar
rswindell committed
916
		length=filelength(fileno(smb.sda_fp));
917

rswindell's avatar
rswindell committed
918
919
		fseek(smb.sda_fp,0L,SEEK_SET);
		for(l=m=0;l<length;l+=2) {
rswindell's avatar
rswindell committed
920
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
rswindell's avatar
rswindell committed
921
922
923
924
			i=0;
			if(!fread(&i,2,1,smb.sda_fp))
				break;
			if(!i)
rswindell's avatar
rswindell committed
925
926
				m++; 
		}
927

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

rswindell's avatar
rswindell committed
930
		length=filelength(fileno(smb.sha_fp));
931

rswindell's avatar
rswindell committed
932
933
		fseek(smb.sha_fp,0L,SEEK_SET);
		for(l=n=0;l<length;l++) {
rswindell's avatar
rswindell committed
934
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
rswindell's avatar
rswindell committed
935
936
937
938
			ch=0;
			if(!fread(&ch,1,1,smb.sha_fp))
				break;
			if(!ch)
rswindell's avatar
rswindell committed
939
940
				n++; 
		}
rswindell's avatar
rswindell committed
941
942
943
944
945
946

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

rswindell's avatar
rswindell committed
950
		if(packable && (m*SDT_BLOCK_LEN)+(n*SHD_BLOCK_LEN)<packable*1024L) {
951
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
952
953
954
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
955
956
			return; 
		}
rswindell's avatar
rswindell committed
957
958
959

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
960
961
				  ,m,m*SDT_BLOCK_LEN,n,n*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
962
963
964

	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		rewind(smb.sha_fp);
965
		CHSIZE_FP(smb.sha_fp,0);		/* Reset both allocation tables */
rswindell's avatar
rswindell committed
966
		rewind(smb.sda_fp);
967
		CHSIZE_FP(smb.sda_fp,0); 
rswindell's avatar
rswindell committed
968
	}
rswindell's avatar
rswindell committed
969
970
971
972
973
974
975

	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
976
			printf("\r%2lu%%  ",(long)(100.0/((float)length/l)));
rswindell's avatar
rswindell committed
977
978
979
980
			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
981
982
				continue; 
			}
rswindell's avatar
rswindell committed
983
984
985
986
			if((i=smb_getmsghdr(&smb,&msg))!=0) {
				smb_unlockmsghdr(&smb,&msg);
				m++;
				size=SHD_BLOCK_LEN;
rswindell's avatar
rswindell committed
987
988
				continue; 
			}
rswindell's avatar
rswindell committed
989
990
991
			smb_unlockmsghdr(&smb,&msg);
			if(msg.hdr.attr&MSG_DELETE) {
				m+=smb_hdrblocks(msg.hdr.length);
rswindell's avatar
rswindell committed
992
993
994
995
996
				n+=smb_datblocks(smb_getmsgdatlen(&msg)); 
			}
			size=smb_hdrblocks(smb_getmsghdrlen(&msg))*SHD_BLOCK_LEN;
			smb_freemsgmem(&msg); 
		}
rswindell's avatar
rswindell committed
997
998
999
1000
1001


		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
1002
1003
			return; 
		}
rswindell's avatar
rswindell committed
1004
1005

		if(packable && (n*SDT_BLOCK_LEN)+(m*SHD_BLOCK_LEN)<packable*1024L) {
1006
			printf("\rLess than %luk compressible bytes.\n\n",packable);
rswindell's avatar
rswindell committed
1007
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
1008
1009
			return; 
		}
rswindell's avatar
rswindell committed
1010
1011
1012

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
1013
1014
				  ,n,n*SDT_BLOCK_LEN,m,m*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

	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
1026
1027
			smb_close_da(&smb); 
		}
1028
		fprintf(stderr,"\n\7!Error opening temp files\n");
rswindell's avatar
rswindell committed
1029
1030
		return; 
	}
rswindell's avatar
rswindell committed
1031
1032
1033
1034
1035
1036
1037
	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);
1038
1039
		smb_close_ha(&smb);
		smb_close_da(&smb);
rswindell's avatar
rswindell committed
1040
1041
1042
		fclose(tmp_sdt);
		fclose(tmp_shd);
		fclose(tmp_sid);
1043
		fprintf(stderr,"\n\7!Error allocating memory\n");
rswindell's avatar
rswindell committed
1044
1045
		return; 
	}
rswindell's avatar
rswindell committed
1046
1047
1048
1049
1050
1051
	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
1052
1053
		fwrite(&ch,1,1,tmp_shd); 
	}
rswindell's avatar
rswindell committed
1054
1055
1056
1057
1058
1059
1060
1061
	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
1062
1063
			continue; 
		}
rswindell's avatar
rswindell committed
1064
1065
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
1066
			fprintf(stderr,"\n\7!smb_lockmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1067
1068
			continue; 
		}
rswindell's avatar
rswindell committed
1069
		i=smb_getmsghdr(&smb,&msg);
1070
		smb_unlockmsghdr(&smb,&msg);
rswindell's avatar
rswindell committed
1071
		if(i) {
1072
			fprintf(stderr,"\n\7!smb_getmsghdr returned %d: %s\n",i,smb.last_error);
rswindell's avatar
rswindell committed
1073
1074
			continue; 
		}
1075
		if(msg.hdr.attr&MSG_DELETE) {
rswindell's avatar
rswindell committed
1076
1077
			printf("\nDeleted header.\n");
			smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
1078
1079
			continue; 
		}
1080
		if(msg.expiration && msg.expiration<=now) {
rswindell's avatar
rswindell committed
1081
1082
			printf("\nExpired message.\n");
			smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
1083
1084
			continue; 
		}
rswindell's avatar
rswindell committed
1085
1086
1087
1088
1089
1090
		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;
1091
			smb_incmsgdat(&smb,datoffset[m].new,smb_getmsgdatlen(&msg),1); 
rswindell's avatar
rswindell committed
1092
		} else {
1093

rswindell's avatar
rswindell committed
1094
1095
1096
1097
1098
1099