/* 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 ------------------------------------------------------------------------- */ static const char id[]="$Id$"; #include #include #include #include #include #include #include #include "global.h" #include "config.h" #include "pop3.h" #include "dbase.h" #include "dstring.h" #include "util.h" static const char header_id[]=KBS_GLOBAL_H; /* ---------------------------------------- PROTOS */ static void Log(FILE *fp, const char *fmt, ...); /* ---------------------------------------- MAIN */ int main(int argc, char *argv[]) { FILE *fp; const char *name; POP3Message *msg; int f; if (strchr(argv[0],'/')) name=strchr(argv[0],'/')+1; else name=argv[0]; if (!ConfigLoad(argv[1])) { fprintf(stderr,"%s: %s\n",name,ConfigError()); exit(EXIT_FAILURE); } switch(POP3Connect(ConfigString(CONFIG_HOSTNAME), ConfigInt(CONFIG_PORT), ConfigString(CONFIG_USERNAME), ConfigString(CONFIG_PASSWORD), ConfigInt(CONFIG_TIMEOUT))) { case POP3_OK: break; case POP3_COMMERROR: fprintf(stderr,"%s: Comms error (errno = %d)\n",name,errno); exit(EXIT_FAILURE); break; case POP3_NOCONNECT: fprintf(stderr,"%s: No connection " "to host (errno = %d)\n",name,errno); exit(EXIT_FAILURE); break; case POP3_BADUSER: fprintf(stderr,"%s: Bad username\n",name); exit(EXIT_FAILURE); break; case POP3_BADPASSWD: fprintf(stderr,"%s: Bad password\n",name); exit(EXIT_FAILURE); break; default: break; } if (strcmp(ConfigString(CONFIG_LOG),"-")!=0) { if (!(fp=fopen(ConfigString(CONFIG_LOG),"a"))) { DString ds; ds=DSInit(); if (getenv("HOME")) DSAddCP(ds,getenv("HOME")); else DSAddChar(ds,'.'); DSAddCP(ds,"/.kbs-deletelog"); if (!(fp=fopen(ds->text,"a"))) fp=stderr; } } else { fp=NULL; } if ((msg=POP3GetList())) { int tot=0; int del=0; for(f=0;msg[f].to;f++) { tot++; if (DBBlockMessage(msg+f)) { del++; if (ConfigInt(CONFIG_VERBOSE)) { Log(fp,"-----\n"); Log(fp,"From %s@%s\n",msg[f].from_uname, msg[f].from_domain); Log(fp,"Subject %.40s\n",msg[f].subject); Log(fp,"Reason %s\n",DBBlockReason()); } else { Log(fp,"%s@%s (%s)\n",msg[f].from_uname, msg[f].from_domain, DBBlockReason()); } if (!ConfigInt(CONFIG_TESTMODE)) { POP3Delete(msg[f].id); } } } if (ConfigInt(CONFIG_VERBOSE)) { Log(fp,"*****\n"); Log(fp,"Processed %d messages, %sdeleted %d\n", tot,ConfigInt(CONFIG_TESTMODE) ? "would have ":"",del); } POP3FreeList(msg); } POP3Logoff(); if (fp && fp!=stderr) { fclose(fp); } DBClose(); ConfigClose(); return EXIT_SUCCESS; } static void Log(FILE *fp, const char *fmt,...) { char buff[128]; time_t t; struct tm *ts; if (!fp) return; t=time(NULL); ts=localtime(&t); strftime(buff,sizeof buff, "%Y-%m-%d %H:%M:%S: ", ts); fprintf(fp,"%s",buff); va_list va; va_start(va,fmt); vfprintf(fp,fmt,va); va_end(va); } /* END OF FILE */