grim/purple-plugin-pack
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA. #include "ignorance_rule.h" #include "ignorance_internal.h" gboolean assign_rule_token(ignorance_rule *rule, const char *tokentxt); ignorance_rule* ignorance_rule_new() { ignorance_rule *ir=(ignorance_rule*)g_malloc(sizeof(ignorance_rule)); ir->name=g_string_new(""); ir->type=IGNORANCE_RULE_SIMPLETEXT; ignorance_rule* ignorance_rule_newp(const GString *name, gint type, const gchar *value, gint score, gint flags, gboolean enabled, const gchar *message, const gchar *sound, const gchar *command) { ignorance_rule *ir=(ignorance_rule*)g_malloc(sizeof(ignorance_rule)); ir->name=g_string_new(name->str); if(ignorance_rule_has_type(type)) ir->type=IGNORANCE_RULE_INVALID; ir->value=g_strdup(value); ir->message=g_strdup(message); ir->sound=g_strdup(sound); ir->command=g_strdup(command); void ignorance_rule_free(ignorance_rule *ir) { g_string_free(ir->name,TRUE); void ignorance_rule_free_g(gpointer ir,gpointer user_data) { ignorance_rule_free((ignorance_rule*)ir); gboolean ignorance_rule_has_type(gint type) { if((type>=IGNORANCE_RULE_MINVALID) || (type<=IGNORANCE_RULE_INVALID)) gint ignorance_rule_rulecheck(ignorance_rule *rule, const GString *text, if((flags & rule->flags) && rule->enabled){ case IGNORANCE_RULE_SIMPLETEXT: return simple_text_rulecheck(rule,text); case IGNORANCE_RULE_REGEX: return regex_rulecheck(rule,text); gint simple_text_rulecheck(ignorance_rule *rule,const GString *text) { const gchar *rulevalue=(const gchar*)(rule->value); if(NULL!=g_strstr_len(text->str,text->len,rulevalue)) gint regex_rulecheck(ignorance_rule *rule, const GString *text) { if(regcomp(®,(const gchar*)rule->value,REG_EXTENDED | REG_NOSUB)) purple_debug_error("ignorance", "Error parsing regex %s\n", (const gchar*)(rule->value)); else if(!regexec(®,text->str,1,NULL,0)) gint repeat_rulecheck(ignorance_rule *rule, gint repeats) { gint allowed_repeats = atoi((gchar*)(rule->value)); if(repeats >= allowed_repeats) ignorance_rule* ignorance_rule_read_old(const gchar *ruletext) { gchar *tokptr=strchr((gchar*)ruletext,' '), **tokens=NULL; ignorance_rule *rule=ignorance_rule_new(); ignorance_rule_free(rule); tokens=g_strsplit(ruletext," ",INT_MAX); assign_rule_token(rule,tokens[i]); if(rule->score > 9 || rule->score < -9) rule->score=IGNORANCE_FLAG_IGNORE; rule->score=IGNORANCE_FLAG_FILTER; ignorance_rule* ignorance_rule_read(const gchar *ruletext) { gchar*tokptr=strchr((gchar*)ruletext,'\n'), **tokens; ignorance_rule *rule=ignorance_rule_new(); ignorance_rule_free(rule); return ignorance_rule_read_old(ruletext); tokens=g_strsplit(ruletext,"\n",INT_MAX); assign_rule_token(rule,tokens[i]); * Parses out a token of the form tokenname="value" and assigns it to a rulename * rule is the rule to be updated * tokentxt is the token string * true returned if token is valid and successfully added to the rule * level name1="value1" name2="value2" ... gboolean assign_rule_token(ignorance_rule *rule,const gchar *tokentxt) { gchar *name=(gchar*)tokentxt, *value=NULL; value=strchr(tokentxt,'='); if(!strncasecmp(name,"name",BUFSIZ)) g_string_assign(rule->name,value); else if(!strncasecmp(name,"type",BUFSIZ)) else if(!strncasecmp(name,"value",BUFSIZ)) { rule->value=(gchar*)g_malloc((strlen(value)+1)*sizeof(gchar)); strncpy(rule->value,value,strlen(value)+1); }else if(!strncasecmp(name,"score",BUFSIZ)) else if(!strncasecmp(name,"flags",BUFSIZ)) else if(!strncasecmp(name,"enabled",BUFSIZ)) rule->enabled=(gboolean)atoi(value); else if(!strncasecmp(name,"message",BUFSIZ)) { rule->message=(gchar*)g_malloc((strlen(value)+1)*sizeof(gchar)); strncpy(rule->message,value,strlen(value)+1); } else if(!strncasecmp(name,"command",BUFSIZ)) { rule->command=(gchar*)g_malloc((strlen(value)+1)*sizeof(gchar)); strncpy(rule->command,value,strlen(value)+1); } else if(!strncasecmp(name,"sound",BUFSIZ)) { rule->sound=(gchar*)g_malloc((strlen(value)+1)*sizeof(gchar)); strncpy(rule->sound,value,strlen(value)+1); gboolean ignorance_rule_write(ignorance_rule *rule,FILE *f){ fprintf(f,"rule\nname=\"%s\"\ntype=\"%d\"\nscore=\"%d\"\nvalue=\"%s\"\nflags=\"%d\"\nenabled=\"%d\"\nmessage=\"%s\"\ncommand=\"%s\"\nsound=\"%s\"\n/rule\n", rule->name->str, rule->type, rule->score, (gchar*)(rule->value), rule->flags, rule->enabled, rule->message, rule->command, rule->sound);