smbutil.c 50.6 KB
Newer Older
1
2
3
4
5
6
/* Synchronet message base (SMB) utility */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

22
#define SMBUTIL_VER "3.19"
23
char	compiler[32];
24

25
26
27
28
29
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"};


30
#define NOANALYSIS		(1L<<0)
31
#define NOCRC			(1L<<1)
32
33
34
35
36
37
38

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

39
40
41
42
43
#if defined(_WIN32)
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
44
#include <stdio.h>
rswindell's avatar
rswindell committed
45
46
47
48
#include <time.h>		/* time */
#include <errno.h>		/* errno */
#include <string.h>		/* strrchr */
#include <ctype.h>		/* toupper */
49

50
51
52
#include "fidodefs.h"
#include "smblib.h"
#include "str_util.h"
53
#include "utf8.h"
54
#include "conwrap.h"
55
56
57
#include "xpdatetime.h"
#include "git_branch.h"
#include "git_hash.h"
58

59
/* gets is dangerous */
60
#define gets(str)  fgets((str), sizeof(str), stdin)
61

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

64
65
66
67
/********************/
/* Global variables */
/********************/

68
69
70
71
72
73
74
75
76
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;
77
char*		beep="";
rswindell's avatar
rswindell committed
78
long		msgtxtmode = GETMSGTXT_ALL|GETMSGTXT_PLAIN;
79
80
81
82
83
84

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

char *usage=
85
"usage: smbutil [-opts] cmd <filespec.shd>\n"
86
87
88
89
"\n"
"cmd:\n"
"       l[n] = list msgs starting at number n\n"
"       r[n] = read msgs starting at number n\n"
90
"       x[n] = dump msg index at number n\n"
91
"       v[n] = view msg headers starting at number n\n"
92
"       V[n] = view msg headers starting at number n verbose\n"
93
94
95
"       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
96
"       h    = dump hash file\n"
97
98
"       s    = display msg base status\n"
"       c    = change msg base status\n"
rswindell's avatar
rswindell committed
99
"       R    = re-initialize/repair SMB/status headers\n"
100
"       d    = delete all msgs\n"
101
102
"       m    = maintain msg base - delete old msgs and msgs over max\n"
"       p[k] = pack msg base (k specifies minimum packable Kbytes)\n"
rswindell's avatar
rswindell committed
103
104
"       L    = lock a msg base for exclusive-access/backup\n"
"       U    = unlock a msg base\n"
105
106
107
108
109
"\n"
"            [n] may represent 1-based message index offset, or\n"
"            [#n] actual message number, or\n"
"            [-n] message age (in days)\n"
"\n"
110
"opts:\n"
rswindell's avatar
rswindell committed
111
112
113
114
115
116
117
118
119
"      -c[m] = create message base if it doesn't exist (m=max msgs)\n"
"      -a    = always pack msg base (disable compression analysis)\n"
"      -i    = ignore dupes (do not store CRCs or search for duplicate hashes)\n"
"      -d    = use default values (no prompt) for to, from, and subject\n"
"      -l    = LZH-compress message text\n"
"      -o    = print errors on stdout (instead of stderr)\n"
"      -p    = wait for keypress (pause) on exit\n"
"      -!    = wait for keypress (pause) on error\n"
"      -b    = beep on error\n"
rswindell's avatar
rswindell committed
120
"      -r    = display raw message body text (not MIME-decoded)\n"
rswindell's avatar
rswindell committed
121
122
123
124
125
126
127
128
"      -C    = continue after some (normally fatal) error conditions\n"
"      -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"
"      -z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
129
#ifdef __unix__
130
"      -U[n] = set umask to specified value (use leading 0 for octal, e.g. 022)\n"
131
#endif
rswindell's avatar
rswindell committed
132
"      -#    = set number of messages to view/list (e.g. -1)\n"
133
134
;

135
136
137
138
139
140
141
142
143
144
145
146
147
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);
}

148
/*****************************************************************************/
149
/* Expands Unix LF to CRLF													 */
150
/*****************************************************************************/
151
ulong lf_expand(uchar* inbuf, uchar* outbuf)
152
153
{
	ulong	i,j;
rswindell's avatar
rswindell committed
154

155
	for(i=j=0;inbuf[i];i++) {
156
157
158
159
		if(inbuf[i]=='\n' && (!i || inbuf[i-1]!='\r'))
			outbuf[j++]='\r';
		outbuf[j++]=inbuf[i];
	}
160
	outbuf[j]=0;
161
162
	return(j);
}
rswindell's avatar
rswindell committed
163

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
char* gen_msgid(smb_t* smb, smbmsg_t* msg, char* msgid, size_t maxlen)
{
	char* host = getenv(
#if defined(_WIN32)
		"COMPUTERNAME"
#else
		"HOSTNAME"
#endif
	);
	if(host == NULL)
		host = getenv(
#if defined(_WIN32)
		"USERNAME"
#else
		"USER"
#endif
	);
	safe_snprintf(msgid, maxlen
		,"<%08lX.%lu.%s@%s>"
183
184
		,(ulong)msg->hdr.when_imported.time
		,(ulong)smb->status.last_msg + 1
185
186
187
188
189
		,getfname(smb->file)
		,host);
	return msgid;
}

190
191
192
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
193
194
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
195
{
196
197
	char		str[128];
	char		buf[1024];
rswindell's avatar
rswindell committed
198
199
	char*		msgtxt=NULL;
	char*		newtxt;
200
	const char* charset = NULL;
201
202
	long		msgtxtlen;
	int 		i;
203
	ushort		agent=AGENT_SMBUTIL;
204
	smbmsg_t	msg;
205
	long		dupechk_hashes=SMB_HASH_SOURCE_DUPE;
206

207
208
209
	/* Read message text from stream (file or stdin) */
	msgtxtlen=0;
	while(!feof(fp)) {
210
		i=fread(buf,1,sizeof(buf),fp);
211
212
		if(i<1)
			break;
rswindell's avatar
rswindell committed
213
		if((msgtxt = realloc(msgtxt,msgtxtlen+i+1))==NULL) {
214
			fprintf(errfp,"\n%s!realloc(%ld) failure\n",beep,msgtxtlen+i+1);
215
			bail(1);
216
		}
217
		memcpy(msgtxt+msgtxtlen,buf,i);
218
219
220
		msgtxtlen+=i;
	}

221
	if(msgtxt!=NULL) {
222
223
224

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

rswindell's avatar
rswindell committed
225
		if((newtxt = malloc((msgtxtlen*2)+1))==NULL) {
226
			fprintf(errfp,"\n%s!malloc(%ld) failure\n",beep,(msgtxtlen*2)+1);
227
			bail(1);
228
		}
rswindell's avatar
rswindell committed
229

230
		/* Expand LFs to CRLFs */
rswindell's avatar
rswindell committed
231
		msgtxtlen=lf_expand((uchar*)msgtxt, (uchar*)newtxt);
232
233
		free(msgtxt);
		msgtxt=newtxt;
234
235
236
237
238
239
240

		if(!str_is_ascii(msgtxt) && utf8_str_is_valid(msgtxt))
			charset = FIDO_CHARSET_UTF8;
		else if(str_is_ascii(msgtxt))
			charset = FIDO_CHARSET_ASCII;
		else
			charset = FIDO_CHARSET_CP437;
241
	}
rswindell's avatar
rswindell committed
242
243

	memset(&msg,0,sizeof(smbmsg_t));
244
	msg.hdr.when_written.time=(uint32_t)time(NULL);
rswindell's avatar
rswindell committed
245
246
247
	msg.hdr.when_written.zone=tzone;
	msg.hdr.when_imported=msg.hdr.when_written;

248
249
	if((to==NULL || stricmp(to,"All")==0) && to_address!=NULL)
		to=to_address;
250
251
	if(to==NULL) {
		printf("To User Name: ");
252
		fgets(str,sizeof(str),stdin); 
253
	} else
254
		SAFECOPY(str,to);
rswindell's avatar
rswindell committed
255
	truncsp(str);
256
257

	if((i=smb_hfield_str(&msg,RECIPIENT,str))!=SMB_SUCCESS) {
258
259
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,RECIPIENT,i,smb.last_error);
260
		bail(1); 
rswindell's avatar
rswindell committed
261
	}
rswindell's avatar
rswindell committed
262
263
264
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
265
266
267
268
269
270
271
272
273
274
275
276
		if(to_address==NULL) {
			if(to_number==NULL) {
				printf("To User Number: ");
				gets(str);
			} else
				SAFECOPY(str,to_number);
			truncsp(str);
			if((i=smb_hfield_str(&msg,RECIPIENTEXT,str))!=SMB_SUCCESS) {
				fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTEXT,i,smb.last_error);
				bail(1); 
			}
277
278
		}
	}
rswindell's avatar
rswindell committed
279

280
	if(smb.status.attr&SMB_EMAIL && (type=='N' || to_address!=NULL)) {
281
		if(to_address==NULL) {
282
			printf("To Address (e.g. user@host or 1:2/3): ");
283
284
			gets(str);
		} else
285
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
286
287
		truncsp(str);
		if(*str) {
288
289
			if((i=smb_hfield_netaddr(&msg,RECIPIENTNETADDR,str,NULL))!=SMB_SUCCESS) {
				fprintf(errfp,"\n%s!smb_hfield_netaddr(0x%02X) returned %d: %s\n"
290
					,beep,RECIPIENTNETADDR,i,smb.last_error);
291
				bail(1); 
292
			}
293
		}
294
	}
rswindell's avatar
rswindell committed
295

296
297
298
299
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
300
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
301
	truncsp(str);
302
	if((i=smb_hfield_str(&msg,SENDER,str))!=SMB_SUCCESS) {
303
304
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SENDER,i,smb.last_error);
305
		bail(1); 
rswindell's avatar
rswindell committed
306
	}
307
	if((smb.status.attr&SMB_EMAIL) || from_number!=NULL) {
308
309
310
311
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
312
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
313
		truncsp(str);
314
		if((i=smb_hfield_str(&msg,SENDEREXT,str))!=SMB_SUCCESS) {
315
316
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,SENDEREXT,i,smb.last_error);
317
			bail(1); 
318
319
		}
	}
320
	if((i=smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent))!=SMB_SUCCESS) {
321
322
		fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
			,beep,SENDERAGENT,i,smb.last_error);
323
		bail(1);
324
	}
rswindell's avatar
rswindell committed
325

326
327
328
329
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
330
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
331
	truncsp(str);
332
	if((i=smb_hfield_str(&msg,SUBJECT,str))!=SMB_SUCCESS) {
333
334
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SUBJECT,i,smb.last_error);
335
		bail(1); 
336
	}
rswindell's avatar
rswindell committed
337

338
	safe_snprintf(str,sizeof(str),"SMBUTIL %s-%s %s/%s %s %s"
339
340
		,SMBUTIL_VER
		,PLATFORM_DESC
341
		,GIT_BRANCH, GIT_HASH
342
343
344
		,__DATE__
		,compiler
		);
345
	if((i=smb_hfield_str(&msg,FIDOPID,str))!=SMB_SUCCESS) {
346
347
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,FIDOPID,i,smb.last_error);
348
		bail(1); 
349
	}
rswindell's avatar
rswindell committed
350

351
	smb_hfield_str(&msg, RFC822MSGID, gen_msgid(&smb, &msg, str, sizeof(str)-1));
352
353
354
355
356
357
358
	if(charset != NULL)
		smb_hfield_str(&msg, FIDOCHARSET, charset);

	if((msg.to != NULL && !str_is_ascii(msg.to) && utf8_str_is_valid(msg.to))
		|| (msg.from != NULL && !str_is_ascii(msg.from) && utf8_str_is_valid(msg.from))
		|| (msg.subj != NULL && !str_is_ascii(msg.subj) && utf8_str_is_valid(msg.subj)))
		msg.hdr.auxattr |= MSG_HFIELDS_UTF8;
359

360
361
362
	if(mode&NOCRC || smb.status.max_crcs==0)	/* no CRC checking means no body text dupe checking */
		dupechk_hashes&=~(1<<SMB_HASH_SOURCE_BODY);

363
	if((i=smb_addmsg(&smb,&msg,smb.status.attr&SMB_HYPERALLOC
rswindell's avatar
rswindell committed
364
		,dupechk_hashes,xlat,(uchar*)msgtxt,NULL))!=SMB_SUCCESS) {
365
366
		fprintf(errfp,"\n%s!smb_addmsg returned %d: %s\n"
			,beep,i,smb.last_error);
367
		bail(1); 
rswindell's avatar
rswindell committed
368
	}
rswindell's avatar
rswindell committed
369
	smb_freemsgmem(&msg);
370

deuce's avatar
deuce committed
371
	// MSVC can't do %zu for size_t until MSVC 2017 it seems...
372
	fprintf(statfp, "Message (%ld bytes) added to %s successfully\n", msgtxtlen, smb.file);
373
	FREE_AND_NULL(msgtxt);
374
375
376
377
378
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
379
void showstatus(void)
380
381
382
{
	int i;

rswindell's avatar
rswindell committed
383
384
	i=smb_locksmbhdr(&smb);
	if(i) {
385
386
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
387
388
		return; 
	}
rswindell's avatar
rswindell committed
389
390
391
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
392
393
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
394
395
		return; 
	}
deuce's avatar
deuce committed
396
397
398
399
400
	printf("last_msg        =%"PRIu32"\n"
		   "total_msgs      =%"PRIu32"\n"
		   "header_offset   =%"PRIu32"\n"
		   "max_crcs        =%"PRIu32"\n"
		   "max_msgs        =%"PRIu32"\n"
rswindell's avatar
rswindell committed
401
402
403
404
405
406
407
408
409
410
		   "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
		   );
411
412
413
414
415
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
416
void config(void)
417
{
418
	char str[128];
419
420
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;
421
	uint32_t last_msg = 0;
422

rswindell's avatar
rswindell committed
423
424
	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
431
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
432
433
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
434
435
		return; 
	}
436
437
438
	printf("Last Message  =%-6"PRIu32" New Value (CR=No Change): "
		,smb.status.last_msg);
	gets(str);
439
	if(IS_DIGIT(str[0]))
440
		last_msg = atol(str);
deuce's avatar
deuce committed
441
	printf("Header offset =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
442
443
		,smb.status.header_offset);
	gets(header_offset);
deuce's avatar
deuce committed
444
	printf("Max msgs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
445
446
		,smb.status.max_msgs);
	gets(max_msgs);
deuce's avatar
deuce committed
447
	printf("Max crcs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
448
449
450
451
452
453
454
455
456
457
		,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) {
458
459
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
460
461
		return; 
	}
rswindell's avatar
rswindell committed
462
463
	i=smb_getstatus(&smb);
	if(i) {
464
465
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
466
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
467
468
		return; 
	}
469
470
	if(last_msg != 0)
		smb.status.last_msg = last_msg;
471
	if(IS_DIGIT(max_msgs[0]))
rswindell's avatar
rswindell committed
472
		smb.status.max_msgs=atol(max_msgs);
473
	if(IS_DIGIT(max_crcs[0]))
rswindell's avatar
rswindell committed
474
		smb.status.max_crcs=atol(max_crcs);
475
	if(IS_DIGIT(max_age[0]))
rswindell's avatar
rswindell committed
476
		smb.status.max_age=atoi(max_age);
477
	if(IS_DIGIT(header_offset[0]))
rswindell's avatar
rswindell committed
478
		smb.status.header_offset=atol(header_offset);
479
	if(IS_DIGIT(attr[0]))
rswindell's avatar
rswindell committed
480
481
		smb.status.attr=atoi(attr);
	i=smb_putstatus(&smb);
482
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
483
	if(i)
484
485
		fprintf(errfp,"\n%s!smb_putstatus returned %d: %s\n"
			,beep,i,smb.last_error);
486
487
488
489
490
491
492
493
494
495
}

/****************************************************************************/
/* Lists messages' to, from, and subject                                    */
/****************************************************************************/
void listmsgs(ulong start, ulong count)
{
	int i;
	ulong l=0;
	smbmsg_t msg;
496
	size_t idxreclen = smb_idxreclen(&smb);
497

rswindell's avatar
rswindell committed
498
499
	if(!start)
		start=1;
500
501
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
502
	while(l<count) {
503
		fseek(smb.sid_fp,((start-1L) + l)*idxreclen,SEEK_SET);
504
		if(!fread(&msg.idx,sizeof(msg.idx),1,smb.sid_fp))
rswindell's avatar
rswindell committed
505
506
507
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
508
509
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
510
511
			break; 
		}
rswindell's avatar
rswindell committed
512
513
514
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
515
516
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
517
518
			break; 
		}
519
520
		printf("%4lu/#%-4"PRIu32" %-25.25s %-25.25s %s\n"
			,start + l, msg.hdr.number,msg.from,msg.to,msg.subj);
rswindell's avatar
rswindell committed
521
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
522
523
		l++; 
	}
524
525
526
527
528
529
}

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

536
	gm=localtime(&intime);
rswindell's avatar
rswindell committed
537
538
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
rswindell's avatar
rswindell committed
539
540
		return(str); 
	}
rswindell's avatar
rswindell committed
541
542
543
544
545
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
rswindell's avatar
rswindell committed
546
547
		strcpy(mer,"pm"); 
	}
rswindell's avatar
rswindell committed
548
549
550
551
552
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
rswindell's avatar
rswindell committed
553
554
		strcpy(mer,"am"); 
	}
rswindell's avatar
rswindell committed
555
556
557
558
	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);
559
560
}

561
562
563
564
/****************************************************************************/
/****************************************************************************/
void dumpindex(ulong start, ulong count)
{
565
	char tmp[128];
566
567
	ulong l=0;
	idxrec_t idx;
568
	size_t idxreclen = smb_idxreclen(&smb);
569
570
571
572
573
574

	if(!start)
		start=1;
	if(!count)
		count=~0;
	while(l<count) {
575
		fseek(smb.sid_fp,((start-1L) + l) * idxreclen,SEEK_SET);
576
		if(!fread(&idx,sizeof(idx),1,smb.sid_fp))
577
			break;
578
		printf("%10"PRIu32"  ", idx.number);
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
		switch(smb_msg_type(idx.attr)) {
			case SMB_MSG_TYPE_FILE:
				printf("F %10lu  ", (ulong)idx.size);
				break;
			case SMB_MSG_TYPE_BALLOT:
				printf("V  %04hX  %-10"PRIu32, idx.votes,idx.remsg);
				break;
			default:
				printf("%c  %04hX  %04hX  %04X"
					,(idx.attr&MSG_POLL_VOTE_MASK) == MSG_POLL_CLOSURE ? 'C' : (idx.attr&MSG_POLL ? 'P':' ')
					,idx.from, idx.to, idx.subj);
				break;
		}
		printf("  %04X  %06X  %s", idx.attr, idx.offset
			,xpDate_to_isoDateStr(time_to_xpDate(idx.time), "-", tmp, sizeof(tmp)));
		if(smb_msg_type(idx.attr) == SMB_MSG_TYPE_FILE && idxreclen == sizeof(fileidxrec_t)) {
			fileidxrec_t fidx;
			fseek(smb.sid_fp,((start-1L) + l) * idxreclen,SEEK_SET);
597
			if(!fread(&fidx,sizeof(fidx),1,smb.sid_fp))
598
599
600
601
				break;
			printf("  %02X  %.*s", fidx.hash.flags, (int)sizeof(fidx.name), fidx.name);
		}
		printf("\n");
602
603
604
605
		l++; 
	}
}

606
607
608
/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
609
void viewmsgs(ulong start, ulong count, BOOL verbose)
610
{
611
	int i,j;
612
613
	ulong l=0;
	smbmsg_t msg;
614
	size_t idxreclen = smb_idxreclen(&smb);
615

rswindell's avatar
rswindell committed
616
617
	if(!start)
		start=1;
618
619
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
620
	while(l<count) {
621
		fseek(smb.sid_fp,((start-1L) + l) * idxreclen,SEEK_SET);
622
		if(!fread(&msg.idx,sizeof(msg.idx),1,smb.sid_fp))
rswindell's avatar
rswindell committed
623
624
625
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
626
627
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
628
629
			break; 
		}
rswindell's avatar
rswindell committed
630
631
632
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
633
634
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
635
636
			break; 
		}
637

638
		printf("--------------------\n");
639
		printf("%-16.16s %ld\n"		,"index record",ftell(smb.sid_fp)/idxreclen);
640
		smb_dump_msghdr(stdout,&msg);
641
642
643
644
645
646
647
648
649
		if(verbose) {
			for(i=0; i<msg.total_hfields; i++) {
				printf("hdr field[%02u]        type %02Xh, length %u, data:"
					,i, msg.hfield[i].type, msg.hfield[i].length);
				for(j=0; j<msg.hfield[i].length; j++)
					printf(" %02X", *((uint8_t*)msg.hfield_dat[i]+j));
				printf("\n");
			}
		}
rswindell's avatar
rswindell committed
650
651
		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
652
653
		l++; 
	}
654
655
}

656
657
658
659
/****************************************************************************/
/****************************************************************************/
void dump_hashes(void)
{
rswindell's avatar
rswindell committed
660
	char	tmp[128];
661
662
663
664
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
665
666
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
667
668
669
670
671
672
		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
673
		printf("\n");
deuce's avatar
deuce committed
674
		printf("%-10s: %"PRIu32"\n","Number",	hash.number);
675
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
deuce's avatar
deuce committed
676
		printf("%-10s: %"PRIu32"\n","Length",	hash.length);
677
		printf("%-10s: %s\n",		"Time",		my_timestr(hash.time));
678
		printf("%-10s: %02x\n",		"Flags",	hash.flags);
rswindell's avatar
rswindell committed
679
		if(hash.flags&SMB_HASH_CRC16)
680
			printf("%-10s: %04x\n",	"CRC-16",	hash.data.crc16);
rswindell's avatar
rswindell committed
681
		if(hash.flags&SMB_HASH_CRC32)
682
			printf("%-10s: %08"PRIx32"\n","CRC-32",	hash.data.crc32);
rswindell's avatar
rswindell committed
683
		if(hash.flags&SMB_HASH_MD5)
684
685
686
			printf("%-10s: %s\n",	"MD5",		MD5_hex(tmp,hash.data.md5));
		if(hash.flags&SMB_HASH_SHA1)
			printf("%-10s: %s\n",	"SHA-1",	SHA1_hex(tmp,hash.data.sha1));
687
	}
688
689

	smb_close_hash(&smb);
690
691
}

692
693
694
695
696
697
698
699
700
701
/****************************************************************************/
/* 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;
deuce's avatar
deuce committed
702
	idxrec_t *idx;
703
704
	size_t idxreclen = smb_idxreclen(&smb);
	uint8_t* idxbuf;
705
706
707
708
709

	printf("Maintaining %s\r\n",smb.file);
	now=time(NULL);
	i=smb_locksmbhdr(&smb);
	if(i) {
710
711
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
712
713
		return; 
	}
714
715
716
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
717
718
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
719
720
		return; 
	}
721
	if((smb.status.max_msgs || smb.status.max_crcs) && smb_open_hash(&smb) == SMB_SUCCESS)
722
723
724
725
726
	{
		ulong max_hashes=0;

		printf("Maintaining %s hash file\r\n", smb.file);

727
		if((smb.status.attr&(SMB_EMAIL|SMB_NOHASH|SMB_FILE_DIRECTORY)) == 0) {
728
729
730
731
732
733
734
735
736
737
738
739
740
			max_hashes = smb.status.max_msgs;
			if(smb.status.max_crcs > max_hashes)
				max_hashes = smb.status.max_crcs;
		}
		if(!max_hashes) {
			CHSIZE_FP(smb.hash_fp,0);
		} else if(filelength(fileno(smb.hash_fp)) > (long)(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t))) {
			if(fseek(smb.hash_fp, -((long)(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t))), SEEK_END) == 0) {
				hash_t*	hashes = malloc(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t));
				if(hashes != NULL) {
					if(fread(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp) == max_hashes * SMB_HASH_SOURCE_TYPES) {
						rewind(smb.hash_fp);
						fwrite(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp);
741
742
						fflush(smb.hash_fp);
						CHSIZE_FP(smb.hash_fp, sizeof(hash_t) * max_hashes * SMB_HASH_SOURCE_TYPES);
743
744
745
746
747
748
749
					}
					free(hashes);
				}
			}
		}
		smb_close_hash(&smb);
	}
750
751
752
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
753
754
		return; 
	}
755
	printf("Loading index...\n");
756
	if((idxbuf = malloc(idxreclen * smb.status.total_msgs))
757
758
		==NULL) {
		smb_unlocksmbhdr(&smb);
deuce's avatar
deuce committed
759
		fprintf(errfp,"\n%s!Error allocating %" XP_PRIsize_t "u bytes of memory\n"
760
			,beep,idxreclen * smb.status.total_msgs);
rswindell's avatar
rswindell committed
761
762
		return; 
	}
763
	fseek(smb.sid_fp,0L,SEEK_SET);
764
	l = fread(idxbuf, idxreclen, smb.status.total_msgs, smb.sid_fp);
765

766
	printf("\nDone.\n\n");
767
768
	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
769
		idx = (idxrec_t*)(idxbuf + (m * idxreclen));
770
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
771
		if(idx->attr&MSG_DELETE)
rswindell's avatar
rswindell committed
772
773
			flagged++; 
	}
774
775
776
777
778
779
	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++) {
780
			idx = (idxrec_t*)(idxbuf + (m * idxreclen));
781
//			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
782
			if(idx->attr&(MSG_PERMANENT|MSG_DELETE))
783
				continue;
784
			if((ulong)now>idx->time && (now-idx->time)/(24L*60L*60L)
785
786
787
				>smb.status.max_age) {
				f++;
				flagged++;
788
				idx->attr|=MSG_DELETE; 
rswindell's avatar
rswindell committed
789
790
			} 
		}  /* mark for deletion */
791
		printf("\r100%% (%lu flagged for deletion due to age)\n",f); 
rswindell's avatar
rswindell committed
792
	}
793
794

	printf("Scanning for read messages to be killed...\n");
795
	uint32_t total_msgs = 0;
796
	for(m=f=0;m<l;m++) {
797
798
		idx = (idxrec_t*)(idxbuf + (m * idxreclen));
		enum smb_msg_type type = smb_msg_type(idx->attr);
799
		if(type == SMB_MSG_TYPE_NORMAL || type == SMB_MSG_TYPE_POLL)
800
			total_msgs++;
801
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
802
		if(idx->attr&(MSG_PERMANENT|MSG_DELETE))
803
			continue;
804
		if((idx->attr&(MSG_READ|MSG_KILLREAD))==(MSG_READ|MSG_KILLREAD)) {
805
806
			f++;
			flagged++;
807
808
			idx->attr|=MSG_DELETE; 
		} 
rswindell's avatar
rswindell committed
809
	}
810
	printf("\r100%% (%lu flagged for deletion due to read status)\n",f);
811

812
	if(smb.status.max_msgs && total_msgs - flagged > smb.status.max_msgs) {
813
		printf("Flagging excess messages for deletion...\n");
814
815
816
		for(m=n=0,f=flagged;l-flagged>smb.status.max_msgs && m<l;m++) {
			idx = (idxrec_t*)(idxbuf + (m * idxreclen));
			if(idx->attr&(MSG_PERMANENT|MSG_DELETE))
817
				continue;
818
			printf("%lu of %lu\r",++n,(total_msgs - f)-smb.status.max_msgs);
819
			flagged++;
820
			idx->attr|=MSG_DELETE; 
rswindell's avatar
rswindell committed
821
822
823
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
824
825

	if(!flagged) {				/* No messages to delete */
826
		free(idxbuf);
827
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
828
829
		return; 
	}
830
831
832
833
834
835
836
837

	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);
838
839
				fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
					,beep,i,smb.last_error);
840
				bail(1); 
rswindell's avatar
rswindell committed
841
			}
842
843
844
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
845
846
				fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
					,beep,i,smb.last_error);
847
				bail(1); 
rswindell's avatar
rswindell committed
848
849
			} 
		}
850
851

		for(m=n=0;m<l;m++) {
852
853
			idx = (idxrec_t*)(idxbuf + (m * idxreclen));
			if(idx->attr&MSG_DELETE) {
854
				printf("%lu of %lu\r",++n,flagged);
855
				msg.idx=*idx;
856
857
				msg.hdr.number=msg.idx.number;
				if((i=smb_getmsgidx(&smb,&msg))!=0) {
858
859
					fprintf(errfp,"\n%s!smb_getmsgidx returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
860
861
					continue; 
				}
862
863
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
864
865
					fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
866
867
					break; 
				}
868
869
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
870
871
					fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
872
873
					break; 
				}
874
875
876
877
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
878
879
					fprintf(errfp,"\n%s!smb_putmsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
880
881
					break; 
				}
882
883
884
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
885
886
					fprintf(errfp,"\n%s!smb_freemsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
887
888
889
890
891
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
892
893
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
894
895
896
897
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
898
899
900
901

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
	for(m=n=0;m<l;m++) {
902
903
		idx = (idxrec_t*)(idxbuf + (m * idxreclen));
		if(idx->attr&MSG_DELETE)
904
			continue;
905
906
		n++;
		printf("%lu of %lu\r", n, l-flagged);
907
		fwrite(idx, idxreclen ,1 ,smb.sid_fp); 
rswindell's avatar
rswindell committed
908
	}
909
	fflush(smb.sid_fp);
910
	CHSIZE_FP(smb.sid_fp, n * idxreclen);
911
	printf("\nDone.\n\n");
912

913
	free(idxbuf);
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
	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)
{
929
	uchar	buf[SDT_BLOCK_LEN],ch;
930
	char	fname[MAX_PATH+1],tmpfname[MAX_PATH+1];
931
	int i,size;
932
933
	ulong l,m,n,datoffsets=0,total;
	off_t length;
934
	FILE *tmp_sdt,*tmp_shd,*tmp_sid;
935
	BOOL		error=FALSE;
936
937
938
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;
939
	time_t		now;
940
	size_t		idxreclen = smb_idxreclen(&smb);
941