#include <stdlib.h>
#include <string.h>
#include "smitetype.h"
#include "mycrc.h"
#include "defines.h"

#ifndef REGANS_LIB
	extern ncpy(char *src, char *dst, int bfsz);
#else
	extern scpy(char *src, char *dst, int bfsz);
#	define ncpy scpy
#endif

SpamType _SpamTypes[] = {
	{"Advertising",				SPAM_TYPE_ADVERT},
	{"Porn",					SPAM_TYPE_PORN},
	{"Scam (Get rich quick)",	SPAM_TYPE_SCAM},
	{"Virus",					SPAM_TYPE_VIRUS},
	{"Robot-reported",			SPAM_TYPE_BOT},

	{NULL,0}
};

SpamType _isSpamFlags[] = {
	{"Notify",		ISSPAM_NOTIFY},
	{"Suspected",	ISSPAM_SUSPECT},
	{"Spam",		ISSPAM_SPAM},
	{"Deleted",		ISSPAM_DELETE},

	{NULL,0}
};

#define isalphalwr(c) (c>='a' && c<='z')
#define isalphaupr(c) (c>='A' && c<='Z')

int validatecrc(char *crc,int *body)
{
	static char lcpy[BFSZ];
	unsigned long m,expcrc;
	int pos;
	char *lastdelim;

	if(body) *body = 0;

	ncpy(lcpy,crc,BFSZ-1);
	for(pos=0;*(lcpy+pos);pos++) {
		if(isalphaupr(*(lcpy+pos))) *(lcpy+pos)+='a'-'A';
		else if(*(lcpy+pos)=='#') *(lcpy+pos)='$';
		if(body && (*(lcpy+pos)=='$')) lastdelim=lcpy+pos+1;
	}
	expcrc=do_crc(lcpy,strlen(lcpy));

	for(pos=0,m=0x80000000L;*(crc+pos);pos++) {
		if(isalphaupr(*(crc+pos))||*(crc+pos)=='#') {
			if(!(m&expcrc)) return 0;
			m/=2;
		} else if(isalphalwr(*(crc+pos))||*(crc+pos)=='$') {
			if(m&expcrc) return 0;
			m/=2;
		}
		if(!m) m=0x80000000L;
	}

	if(body) *body=strtoul(lastdelim,NULL,36);

	return 1;
}
