summaryrefslogtreecommitdiff
path: root/src/dbase.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dbase.c')
-rw-r--r--src/dbase.c281
1 files changed, 281 insertions, 0 deletions
diff --git a/src/dbase.c b/src/dbase.c
new file mode 100644
index 0000000..5eca1a9
--- /dev/null
+++ b/src/dbase.c
@@ -0,0 +1,281 @@
+/*
+
+ kbs - Simple, easily fooled, POP3 spam filter
+
+ Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
+
+ 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
+
+ -------------------------------------------------------------------------
+
+ Rules database
+
+*/
+static const char id[]="$Id$";
+
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#include "global.h"
+#include "dbase.h"
+#include "dstring.h"
+#include "config.h"
+#include "rexp.h"
+#include "util.h"
+
+
+/* ---------------------------------------- TYPES
+*/
+struct Domain
+ {
+ char *name;
+ int def_block;
+ int no_user;
+ int no_block;
+ int no_allow;
+ char **user;
+ char **block;
+ char **allow;
+ Domain *next;
+ Domain *prev;
+ };
+
+
+/* ---------------------------------------- GLOBALS
+*/
+static Domain *head;
+static Domain *tail;
+
+static int no_trusted_users=0;
+static int no_trusted_domains=0;
+
+static char **trusted_user=NULL;
+static char **trusted_domain=NULL;
+
+
+/* ---------------------------------------- PRVIVATE FUNCTIONS
+*/
+static int IsTrustedUser(const char *username)
+{
+ int f;
+
+ for(f=0;f<no_trusted_users;f++)
+ {
+ if (strcasecmp(username,trusted_user[f])==0)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+static int IsTrustedDomain(const char *domain)
+{
+ int f;
+
+ for(f=0;f<no_trusted_domains;f++)
+ {
+ if (strcasecmp(domain,trusted_domain[f])==0)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+static const Domain *GetDomain(const char *domain)
+{
+ Domain *dom;
+
+ dom=head;
+
+ while(dom)
+ {
+ if (RExpSearch(dom->name,domain)==RE_Found)
+ return dom;
+
+ dom=dom->next;
+ }
+
+ return NULL;
+}
+
+
+static void DeJunk(DString ds, const char *p)
+{
+ int last_ws=FALSE;
+
+ while(*p)
+ {
+ if (isspace(*p))
+ {
+ if (!last_ws)
+ DSAddChar(ds,' ');
+
+ last_ws=TRUE;
+ }
+ else
+ last_ws=FALSE;
+
+ if (isalnum(*p))
+ {
+ DSAddChar(ds,*p);
+ }
+
+ p++;
+ }
+}
+
+
+/* ---------------------------------------- INTERFACES
+*/
+Domain *DBNewDomain(const char *regexp)
+{
+ Domain *dom;
+
+ dom=Malloc(sizeof *dom);
+
+ dom->name=CopyStr(regexp);
+ dom->def_block=FALSE;
+
+ dom->no_user=0;
+ dom->no_block=0;
+ dom->no_allow=0;
+
+ dom->user=NULL;
+ dom->block=NULL;
+ dom->allow=NULL;
+
+ if (tail)
+ tail->next=dom;
+ else
+ head=tail=dom;
+
+ dom->prev=tail;
+ dom->next=NULL;
+
+ return dom;
+}
+
+
+void DBDefault(Domain *domain, int block)
+{
+ domain->def_block=block;
+}
+
+
+void DBBlockUser(Domain *domain, const char *username)
+{
+ domain->no_user++;
+ domain->user=Realloc(domain->user,sizeof(char *)*domain->no_user);
+ domain->user[domain->no_user-1]=CopyStr(username);
+}
+
+
+void DBAllowSubject(Domain *domain, const char *regexp)
+{
+ domain->no_allow++;
+ domain->allow=Realloc(domain->allow,sizeof(char *)*domain->no_allow);
+ domain->allow[domain->no_allow-1]=CopyStr(regexp);
+}
+
+
+void DBBlockSubject(Domain *domain, const char *regexp)
+{
+ domain->no_block++;
+ domain->block=Realloc(domain->block,sizeof(char *)*domain->no_block);
+ domain->block[domain->no_block-1]=CopyStr(regexp);
+}
+
+
+void DBTrustedUser(const char *username)
+{
+ no_trusted_users++;
+ trusted_user=Realloc(trusted_user,sizeof(char *)*no_trusted_users);
+ trusted_user[no_trusted_users-1]=CopyStr(username);
+}
+
+
+void DBTrustedDomain(const char *domain)
+{
+ no_trusted_domains++;
+ trusted_domain=Realloc(trusted_domain,sizeof(char *)*no_trusted_domains);
+ trusted_domain[no_trusted_domains-1]=CopyStr(domain);
+}
+
+
+int DBBlockMessage(const POP3Message *msg)
+{
+ DString ds;
+ const Domain *dom;
+ int f;
+
+ if (IsTrustedDomain(msg->from_domain))
+ return FALSE;
+
+ if (IsTrustedUser(msg->from_uname))
+ return FALSE;
+
+ if (!(dom=GetDomain(msg->from_domain)))
+ return FALSE;
+
+ for(f=0;f<dom->no_user;f++)
+ {
+ if (ConfigInt(CONFIG_CASESENSE))
+ {
+ if (strcmp(dom->user[f],msg->from_uname)==0)
+ return TRUE;
+ }
+ else
+ {
+ if (strcasecmp(dom->user[f],msg->from_uname)==0)
+ return TRUE;
+ }
+ }
+
+ ds=DSInit();
+
+ if (ConfigInt(CONFIG_DEJUNK))
+ DeJunk(ds,msg->subject);
+ else
+ DSAddCP(ds,msg->subject);
+
+ for(f=0;f<dom->no_allow;f++)
+ {
+ if (RExpSearch(dom->allow[f],ds->text)==RE_Found)
+ {
+ DSFree(ds);
+ return FALSE;
+ }
+ }
+
+ for(f=0;f<dom->no_block;f++)
+ {
+ if (RExpSearch(dom->block[f],ds->text)==RE_Found)
+ {
+ DSFree(ds);
+ return TRUE;
+ }
+ }
+
+ DSFree(ds);
+ return dom->def_block;
+}
+
+
+/* END OF FILE */