📄 rulefile.cpp
字号:
newrule->value = (CHARPTR)CopyAndPtr(&(parameters[4])[1],search-parameters[4]);
}
else
{
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": 5. parameter must be a list pointer (@listname) or a value in quotes (\"val\")";
return 1;
}
// 6. parameter: action. sets what will be done if this rule matches
// must be 'pass' or 'isspam'
if (stricmp(parameters[5],"isspam") == 0)
newrule->action = A_ISSPAM;
else if (stricmp(parameters[5],"pass") == 0)
newrule->action = A_PASS;
else
{
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": 6. parameter must be 'isspam' or 'pass'";
return 1;
}
}
else // orig: if (newrule->field != F_SIZE)
{
// *** SIZE RULE
// 2. parameter must be '>' or '<'
if (((parameters[1])[0] == '>') || ((parameters[1])[0]== '<'))
{
// we use the negate element to store if it's
// > (greater than = true) or
// < (less than = false)
if ((parameters[1])[0] == '>')
newrule->negate = true;
else
newrule->negate = false;
}
else
{
// error
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": 2. parameter must be '>' (greater than) or '<' (less than)";
return 1;
}
// 3. parameter is message size in bytes
// try converting string to integer
i_conv = atoi(parameters[2]);
// if i_conv == 0 -> parameter is 0 or invalid -> error
if (i_conv == 0)
{
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": 3. parameter must be a non-zero integer value (message size).";
return 1;
}
else
{
// store value and save pointer in 'value'
newrule->value = new UINT;
*((UINT*)newrule->value) = i_conv;
}
// 4. parameter: action. sets what will be done if this rule matches
// must be 'pass' or 'isspam'
if (stricmp(parameters[3],"isspam") == 0)
newrule->action = A_ISSPAM;
else if (stricmp(parameters[3],"pass") == 0)
newrule->action = A_PASS;
else
{
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": 4. parameter must be 'isspam' or 'pass'";
return 1;
}
//
}
// if the code ran up to this point, the command is ok
// add to vector and return 0 (success)
ruleset->push_back(newrule);
return 0;
}
void SetFunctPointer(RULE* rule, RULE_TYPE type, bool issensitive)
// Sets the pointer of the comparison function according to the rule type
// and to the case sensitivity.
// About negation: Some comparison functions return zero if both strings match
// (e.g. strcmp), others return a non-zero values (e.g. strstr). The program
// expects a non-zero value for matching strings, so some results have to be negated
// Additionally, if we have negative rule type ("notequals", "notcontains") then
// we also have to negate the value (again)
{
// now we need to go through all possible combinations of rule types and
// case sensitivity values
switch (type)
{
case R_EQUALS:
// check if value is equal
if (issensitive == true)
rule->compare = strcmp; // case-sensitive equality check
else
rule->compare = stricmp; // case-insensitive equality check
rule->negate=false; // no negative funct
rule->negate=!rule->negate; // because strcmp returns 0 for matching strings
break;
case R_NOTEQUALS:
// same as R_EQUALS but negated
if (issensitive == true)
rule->compare = strcmp;
else
rule->compare = stricmp;
rule->negate=true; // negative funct !
rule->negate=!rule->negate; // because strcmp returns 0 for matching strings
break;
case R_CONTAINS:
// use function that return a non-zero value if a string
// is contained in another string
if (issensitive == true)
rule->compare = strstrcaps;
else
rule->compare = stristrcaps;
rule->negate = false; // No negative funct
break;
case R_NOTCONTAINS:
// same as R_CONTAINS but negated
if (issensitive == true)
rule->compare = strstrcaps;
else
rule->compare = stristrcaps;
rule->negate = true; // Negative funct!
break;
case R_PATTERN:
// no case-sensitive pattern match yet
rule->compare = strpatterncaps;
rule->negate = false; // no negative funct
break;
case R_NOTPATTERN:
// no case-sensitive pattern match yet
rule->compare = strpatterncaps;
rule->negate = true; // negative funct
break;
}
}
int strstrcaps (const char* x, const char* y)
// This function encapsulates strstr as strstr itself does not
// fit to our string comparision function signature CHECKFUNC
// (because it returns char* and we need int)
{
return (strstr(x,y) == 0) ? 0 : 1;
}
int stristrcaps (const char* x, const char* y)
// This function encapsulates stristr as stristr itself does not
// fit to our string comparision function signature CHECKFUNC
// (because it returns char* and we need int)
{
return (stristr(x,y) == 0) ? 0 : 1;
}
int strpatterncaps (const char* x, const char* y)
// encapsulates patternmatch() as it doesn't fit to the CHECKFUNC
// signature and returns bool
{
return (patternmatch(x,y) == true) ? 1 : 0;
}
UINT AddList(CHARPTR* parameters, LISTMAPPTR listmap)
// opens a list file and uses each line as one lsit item
// the list is added to 'listmap'
{
CHARPTR filecont=0, fileend=0;
UINT filelen;
CHARPTR search=0,search2=0;
UINT linecount=0, cur=0;
LIST* newlist = new LIST;
// read file contents (combine config file path and file location in parameter 1)
CHARPTR comb = CombinePaths(curfiledir,parameters[1]);
GetFileCont(comb,&filecont,&filelen);
delete comb;
// if filecont == 0 then failed opening file
if (filecont == 0)
{
cout << endl << "Error in " << curfile << "(" << curline << ")"
<< ": failed opening file '" << parameters[1] << "'";
return 1;
}
fileend = filecont + filelen - 2;
// now count number of lines
search = filecont;
while ((search != 0) && (search < fileend))
{
linecount++;
search++;
search = strchr(search,10);
}
// reserve array of ptrs large enough for all lines
newlist->count = linecount;
newlist->items = new CHARPTR[linecount];
// walk through all lines and save them to array
search = filecont;
cur = 0;
while (search < fileend)
{
// find end of line
search2 = strchr(search,10);
if (search2 == 0)
{
// last line
newlist->items[cur] = (CHARPTR)CopyAndPtr(search,fileend-search);
break;
}
search2--;
// copy string
newlist->items[cur] = (CHARPTR)CopyAndPtr(search,search2-search+1);
cur++;
// go to beginning of next line
search = search2 + 2;
}
// save this list in listmap
(*listmap)[parameters[0]] = newlist;
return 0;
}
RULE_TYPE StrToRuletype(CHARPTR command)
// determines which rule type from enum RULE_TYPE belongs to
// the string command in 'command'
{
if (stricmp(command,"contains") == 0)
return R_CONTAINS;
if (stricmp(command,"notcontains") == 0)
return R_NOTCONTAINS;
if (stricmp(command,"equals") == 0)
return R_EQUALS;
if (stricmp(command,"notequals") == 0)
return R_NOTEQUALS;
if (stricmp(command,"pattern") == 0)
return R_PATTERN;
if (stricmp(command,"notpattern") == 0)
return R_NOTPATTERN;
// if code up to this point 'command' does not contain a valid command
return R_UNKNOWN;
}
RULE_COMMAND StrToCommand(CHARPTR command)
// determines which rule number from enum RULE_COMMAND belongs to
// the command in 'command'
{
if (stricmp(command,"rule") == 0)
return C_RULE;
if (stricmp(command,"loadlist") == 0)
return C_LIST;
if (strlen(command) == 0)
return C_EMPTY;
return C_UNKNOWN;
}
CHARPTR stristr(const char *szsrc,const char *szfind )
// this function performs a case insensitive string search
{
int i, k, nfind, nsrc;
// if one of the strings is a zero-pointer or begins with a zero-byte -> no match
if( !szsrc || !szsrc[0] || !szfind || !szfind[0] )
return(0);
// determine lengths of strings
nsrc= strlen(szsrc);
nfind= strlen(szfind);
// now iterate through both strings and check byte for byte for
// matching strings. all characters are converted to lowercase before comparison
for( i=k=0 ; k<nfind && i<nsrc ; i++, k++ ) {
if( toupper(szsrc[i]) != toupper(szfind[k]) ) {
i -= k;
k = -1;
}
}
// k contains the max number of matching chars here
// if this value is equal to the length of the search string -> match!
if( k==nfind )
return(char*)(szsrc + i-nfind);
// if the code runs up to this point nothing the search string is not contained
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -