#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "spasm.h"
#include "dbfunc.h"
#include "oldvars.h"

void spasm_error(int syslevel, char *errstr, ...) {
    va_list arglist;

    va_start(arglist, errstr);
    vfprintf(stderr, errstr, arglist);
    fprintf(stderr, "\n");      /* spasm_error args aren't terminated by \n */
    va_end(arglist);
}

int main(int argc, char **argv) {
    char *old_userbl;
    long long int userbl, i;
    char *username;
    DB *db;
    DBT dbk, dbv;
    DBC *dbcp;
    int dbfd;
    int rc;

    if(argc < 2) {
	fprintf(stderr, "syntax: %s <userbl>\n", argv[0]);
	exit(1);
    }
    old_userbl = argv[1];

    memset(&dbk, '\0', sizeof(DBT));
    memset(&dbv, '\0', sizeof(DBT));

    if((db_create(&db, NULL, 0) != 0)) {
	fprintf(stderr, "Error creating DB handle: %m\n");
	exit(1);
    }
    if((rc = sp_db_open(db, NULL, old_userbl, NULL, DB_HASH, DB_RDONLY, 0) != 0)) {
	fprintf(stderr, "Error opening database: %s\n", db_strerror(rc));
        db->close(db, 0);
        exit(1);
    }
    if((rc = db->fd(db, &dbfd)) != 0) {
	fprintf(stderr, "Error obtaining file descriptor for database: %s\n", db_strerror(rc));
        db->close(db, 0);
        exit(1);
    }
    if((rc = db->cursor(db, NULL, &dbcp, 0)) != 0) {
	fprintf(stderr, "Error obtaining database cursor: %s\n", db_strerror(rc));
        db->close(db, 0);
        exit(1);
    }
    while(dbcp->c_get(dbcp, &dbk, &dbv, DB_NEXT) == 0) {
        if((username = calloc(dbk.size + 1, 1)) != NULL) {
	    int tagging = 0;
	    int quarantining = 0;
	    char buf[512];

	    memset(buf, 0, sizeof(buf));

	    strncpy(username, dbk.data, dbk.size);
	    strncpy(buf, dbv.data, dbv.size);
	    userbl = strtoll(buf, NULL, 10);

	    /* Already have new filters set? Don't modify! */
	    if(spasm_verify_filters(username)) {
		free(username);
		continue;
	    }
	    /* Tagging? Quarantining? */
	    for(i = 0; oldfilters[i].name != NULL; i++) {
		if(strcmp(oldfilters[i].name, "SF_TAG_ONLY") == 0) {
		    if(oldfilters[i].num & userbl) tagging = 1;
		} else if(strcmp(oldfilters[i].name, "SF_QUARANTINE") == 0) {
		    if(oldfilters[i].num & userbl) quarantining = 1;
		}
	    }
	    for(i = 0; oldfilters[i].name != NULL; i++) {
		/* Skip tag and quarantine filters */
		if(strcmp(oldfilters[i].name, "SF_TAG_ONLY") == 0 ||
		   strcmp(oldfilters[i].name, "SF_QUARANTINE") == 0) continue;

		if(userbl & oldfilters[i].num) {
		    if(quarantining) spasm_set_filter(username, oldfilters[i].name, SPASM_QUARANTINE);
		    else if(tagging) spasm_set_filter(username, oldfilters[i].name, SPASM_TAG);
		    else spasm_set_filter(username, oldfilters[i].name, SPASM_REJECT);
		} else spasm_set_filter(username, oldfilters[i].name, SPASM_OFF);
	    }
	    /* Set full logging */
	    spasm_set_filter(username, "USE_LOGGING", SPASM_REJECT);
	    free(username);
        } else {
	    fprintf(stderr, "Error allocating memory for username: %m\n");
	}
    }
    dbcp->c_close(dbcp);
    db->close(db, 0);

    exit(0);
}
