/* usersdb.c 
 *
 * This object will permit the ability to control access to/from various users
 * which require "special" exemptions or restriction.
 *
 * started Tue Aug 01 16:33:00 SAST 2000
 *
 *
 * The format of the database entries is effectively just a steam of
 * _ARB_PREFIX.details:[*]
 * ie, username@domain ft.exe:ft.mpg:fnprettypark:z100000
 *
 * sample lines...
 *
 * arb.co.za ftbmp:szo1000:z0
 * arb@arb.co.za ftexe:ftmpg:fnamemyftpprogram.exe:zo100000
 *
 *
 * DEPENDS ON block.o 
 *
 */
 
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "blocks.h"
#include "usersdb.h"

#define TYPE_prefix 't'
#define NAME_prefix 'n'
#define SIZE_prefix 'z'
#define BLOCK_prefix 'b'

#define _MATCH_none -1
#define _MATCH_sender 1
#define _MATCH_receiver 2

/* Globals */
int userblocked = 0;
int maxemailsize = 1024;




int UDB_setmaxemailsize( int maxsize ){
	maxemailsize = maxsize;
	
	return 0;
	}
	
	
	
int UDB_getmaxemailsize( void ){
	
	return maxemailsize;
	}

int UDB_userblock( void ){
	
	return userblocked;
	}
		
	
int UDB_parseline( char *line, char *sender, char *receiver ) {

	char *uname;
	char *token;
	int match = _MATCH_none;
		
	uname = strtok(line,":");
	
	if (strstr(sender, uname) != NULL) match = _MATCH_sender;
	else if (strstr(receiver, uname) != NULL) match = _MATCH_receiver;
	
	if (match != _MATCH_none){
		
		/* while there are more "tokens" to parse... */
		while ((token = strtok(NULL,":"))!= NULL){
			switch (token[0]) {
				
				case TYPE_prefix: /* file type prefix */
					token++;
					REMOVE_typeblock(token);
#ifdef DEBUG
					fprintf(stderr,"UDBParse: Removed Type - %s\n",token);
#endif
					break;
					
				case NAME_prefix:
					token++;
					REMOVE_nameblock(token);
#ifdef DEBUG
					fprintf(stderr,"UDBParse: Removed Name - %s\n",token);
#endif
					break;
					
				case SIZE_prefix:
					token++;
					maxemailsize = atoi(token);
#ifdef DEBUG
					fprintf(stderr,"UDBParse: Set MAX Email size to - %d\n",maxemailsize);
#endif
					break;

				case BLOCK_prefix:
					token++;
					userblocked++; /* from blocks.h */
#ifdef DEBUG
					fprintf(stderr,"UDBParse: Userblocked - %s\n",token);
#endif
					break;
					
				default: break;
				} /* switch */		 
				
			} /* while */
		
		} /* if (sender or receiver was in the database) */
		
	return 0;
	}
	


	
/*--------------------------------------------------
 * UDB_search
 *
 * This function searches through the supplied database
 * (text only file) and alters the "default" .conf
 * file parms to suit a specific user, useful if
 * you have a "brat" on your net work who seems to
 * always waste your space/time.
 */
int UDB_search( char *fname, char *sender, char *receiver ){
	
	char line[1024];
	FILE *f = fopen(fname,"r");
	
	if (f) {

#ifdef DEBUG
		fprintf(stderr,"\n------------------UDB Search/Adjust-----------\n");
#endif
		while (fgets(line,1023,f)){
			line[strlen(line)-1]='\0'; /* get rid of the annoying CR */
			UDB_parseline(line, sender, receiver);
			}
#ifdef DEBUG
		fprintf(stderr,"-------------------------------------------------\n");
#endif
		fclose(f);	
		return 0;		
		}
	else return -1;
	}


	