00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __WLDB_H
00021 #define __WLDB_H
00022
00026 #include <exception>
00027 #include <string>
00028 #include <syslog.h>
00029 #include "triplet.h"
00030 #include "db.h"
00031 #include "wlmodule.h"
00032 #include "dbdefs.h"
00033 #include "pmatcher.h"
00034
00035 class WLDB
00036 {
00037 public:
00038 WLDB(class DB& db,
00039 bool initmode,
00040 const WLStaticDef& staticDef) throw(std::exception)
00041 : _db(db),
00042 _staticDef(staticDef)
00043 {
00044 if(initmode) {
00045 _db.createTable(_staticDef);
00046 }
00047 }
00048 bool check(const class Triplet& triplet) throw(std::exception) {
00049 switch(_staticDef._compareMode) {
00050 case WlModule::CM_exactMatch:
00051 return _db.exactMatch( "wl",_staticDef,triplet );
00052 case WlModule::CM_patternMatch:
00053 return patternMatch( "wl", triplet );
00054 default:
00055 return false;
00056 }
00057 }
00058 ~WLDB() {};
00059 protected:
00061 bool patternMatch(const std::string logprefix, const Triplet& triplet)
00062 {
00063 dbi_result result = _db.executeSQL(_staticDef._searchTable);
00064 if(!result)
00065 return false;
00066
00067 unsigned numrows = dbi_result_get_numrows(result);
00068 if(numrows == 0) {
00069 dbi_result_free(result);
00070 return false;
00071 }
00072 dbi_result_next_row(result);
00073 unsigned numfields = dbi_result_get_numfields(result);
00074
00075 unsigned commentIdx = numfields;
00076 unsigned primaryIdx = numfields;
00077 for(unsigned i=0;i < numfields;i++) {
00078 const char* colname = dbi_result_get_field_name(result,i+1);
00079
00080 if(strcmp(colname,_staticDef._primaryKey) == 0)
00081 primaryIdx = i;
00082 else
00083 if(strcmp(colname,_staticDef._commentField) == 0)
00084 commentIdx = i;
00085
00086
00087 }
00088 if((primaryIdx >= numfields) || (commentIdx >= numfields)) {
00089
00090 syslog(LOG_WARNING,"WlDb no primary/comment idx in table %s",_staticDef._tableName);
00091 dbi_result_free(result);
00092 return false;
00093 }
00094 PatternMatcher pmatcher(triplet);
00095 for(unsigned i=0;i < numrows;i++) {
00096 const char *pattern = dbi_result_get_string_idx(result,primaryIdx+1);
00097
00098 if(pmatcher.match(pattern)) {
00099 const char *comment = dbi_result_get_string_idx(result,commentIdx+1);
00100 syslog(LOG_INFO,"%s %s: %s -> %s, %s: %s",
00101 logprefix.c_str(),_staticDef._tableName,triplet.getSender().c_str(),
00102 triplet.getRecipient().c_str(),triplet.getClientAddress().c_str(),comment);
00103 dbi_result_free(result);
00104 return true;
00105 }
00106 if(dbi_result_next_row(result) == 0)
00107 break;
00108 }
00109 dbi_result_free(result);
00110 return false;
00111 }
00112 private:
00113 class DB& _db;
00114 const WLStaticDef& _staticDef;
00115 };
00116
00117 #endif