Refactored rules.c

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2018-11-03 17:12:07 +01:00
parent a3226764c6
commit 53249db1eb

View File

@ -194,21 +194,9 @@ struct rule *create_rule_item(struct rule_cond *rule_cond){
} }
char *check_againt_ruleset(struct node *xhash[], struct parser_state *state, int size, int spam){ unsigned int count_match(struct rule *p, struct parser_state *state, int size, int spam){
unsigned int ismatch=0;
size_t nmatch=0; size_t nmatch=0;
struct rule *p;
struct node *q;
int ismatch;
q = xhash[0];
while(q != NULL){
if(q->str){
p = q->str;
if(p){
ismatch = 0;
ismatch += check_spam_rule(spam, p->spam); ismatch += check_spam_rule(spam, p->spam);
ismatch += check_size_rule(size, p->size, p->_size); ismatch += check_size_rule(size, p->size, p->_size);
@ -234,14 +222,26 @@ char *check_againt_ruleset(struct node *xhash[], struct parser_state *state, int
ismatch += RULE_UNDEF; ismatch += RULE_UNDEF;
} }
else if(regexec(&(p->body), state->b_body, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH; else if(regexec(&(p->body), state->b_body, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
} }
if(ismatch > 0){ return ismatch;
}
char *check_againt_ruleset(struct node *xhash[], struct parser_state *state, int size, int spam){
struct rule *p;
struct node *q;
q = xhash[0];
while(q != NULL){
if(q->str){
p = q->str;
if(p && count_match(p, state, size, spam) > 0){
return p->rulestr; return p->rulestr;
} }
}
} }
q = q->r; q = q->r;
@ -252,10 +252,8 @@ char *check_againt_ruleset(struct node *xhash[], struct parser_state *state, int
time_t query_retain_period(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg){ time_t query_retain_period(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg){
size_t nmatch=0;
struct rule *p; struct rule *p;
struct node *q; struct node *q;
int ismatch;
q = data->retention_rules[0]; q = data->retention_rules[0];
@ -264,48 +262,16 @@ time_t query_retain_period(struct data *data, struct parser_state *state, int si
if(q->str){ if(q->str){
p = q->str; p = q->str;
ismatch = 0;
if(p->domainlen > 2){ if(p->domainlen > 2){
if(strcasestr(state->b_to_domain, p->domain) || strcasestr(state->b_from_domain, p->domain)){ if(strcasestr(state->b_to_domain, p->domain) || strcasestr(state->b_from_domain, p->domain)){
state->retention = p->days; state->retention = p->days;
return (time_t)(state->retention) * (time_t)86400; return (time_t)(state->retention) * (time_t)86400;
} }
} }
else { else if(count_match(p, state, size, spam) > 0){
ismatch += check_spam_rule(spam, p->spam);
ismatch += check_size_rule(size, p->size, p->_size);
ismatch += check_attachment_rule(state, p);
if(p->compiled == 1){
if(p->emptyfrom == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->from), state->b_from, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptyto == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->to), state->b_to, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptysubject == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->subject), state->b_subject, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptybody == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->body), state->b_body, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
}
if(ismatch > 0){
state->retention = p->days; state->retention = p->days;
return (time_t)(state->retention) * (time_t)86400; return (time_t)(state->retention) * (time_t)86400;
} }
}
} }
@ -320,10 +286,8 @@ time_t query_retain_period(struct data *data, struct parser_state *state, int si
int get_folder_id_by_rule(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg){ int get_folder_id_by_rule(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg){
size_t nmatch=0;
struct rule *p; struct rule *p;
struct node *q; struct node *q;
int ismatch;
if(cfg->enable_folders == 0) return 0; if(cfg->enable_folders == 0) return 0;
@ -334,46 +298,14 @@ int get_folder_id_by_rule(struct data *data, struct parser_state *state, int siz
if(q->str){ if(q->str){
p = q->str; p = q->str;
ismatch = 0;
if(p->domainlen > 2){ if(p->domainlen > 2){
if(strcasestr(state->b_to_domain, p->domain) || strcasestr(state->b_from_domain, p->domain)){ if(strcasestr(state->b_to_domain, p->domain) || strcasestr(state->b_from_domain, p->domain)){
return p->folder_id; return p->folder_id;
} }
} }
else { else if(count_match(p, state, size, spam) > 0){
ismatch += check_spam_rule(spam, p->spam);
ismatch += check_size_rule(size, p->size, p->_size);
ismatch += check_attachment_rule(state, p);
if(p->compiled == 1){
if(p->emptyfrom == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->from), state->b_from, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptyto == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->to), state->b_to, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptysubject == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->subject), state->b_subject, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
if(p->emptybody == 1){
ismatch += RULE_UNDEF;
}
else if(regexec(&(p->body), state->b_body, nmatch, NULL, 0) == 0) ismatch += RULE_MATCH; else ismatch += RULE_NO_MATCH;
}
if(ismatch > 0){
return p->folder_id; return p->folder_id;
} }
}
} }
@ -476,4 +408,3 @@ void clearrules(struct node *xhash[]){
xhash[0] = NULL; xhash[0] = NULL;
} }