/*
**    spasm - The spasm anti-spam milter
**    Copyright (C) 2002-2004 Sean Hanson <aradia@theasylum.org>
**
**    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.
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include "spasm.h"
#include "dbfunc.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 data[256], *val;
    struct spasm_list *entries = NULL, *tmp;
    struct spasm_list *first = NULL;

    if(argc < 2) {
	printf("syntax: spasmdb <dbname> < inputfile.txt\n");
	exit(1);
    }

    while(fgets(data, sizeof(data), stdin) != NULL) {
	if (data[0] == '#') continue;
	val = data;
	while(isspace(*val) == 0) val++;
	*val = '\0';
        if((tmp = calloc(sizeof(struct spasm_list), 1)) != NULL) {
            if((tmp->entry = calloc(strlen(data) + 1, 1)) != NULL) {
                strncpy(tmp->entry, data, strlen(data));
                if (! first) {
                    first = entries = tmp;
                } else {
                    entries->next = tmp;
                    entries = tmp;
                }
            } else {
                spasm_error(LOG_ERR, "Error allocating memory for tmp->entry: %m");
                exit (1);  /* no memory -- game over */
            }
        } else {
            spasm_error(LOG_ERR, "Error allocating memory for tmp: %m");
            exit (1);      /* no memory -- game over */
        }
    }
    return (! spasm_create_bl(argv[1], first));
}
