chirpnest/rules.go
2020-01-08 17:27:19 +08:00

127 lines
2.8 KiB
Go

package main
import (
"encoding/json"
"errors"
"log"
"regexp"
)
func resetMsgParsingRules() error {
var rules, rules2 []MessageParsingRule
var count int64
b, err := Asset("data/msg_rules.json")
logOnError(err, "resetMsgParsingRules : load data/msg_rules.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &rules)
for _, r := range rules {
r2 := MessageParsingRule{
Priority: r.Priority,
Description: r.Description,
Rule: r.Rule,
MsgType: r.MsgType,
ChatID64: r.ChatID64,
SenderUserID64: r.SenderUserID64,
}
r2.MsgTypeID64, err = codeObjSubTypeId(r2.MsgType)
logOnError(err, "resetMsgParsingRules : codeObjSubTypeId("+r2.MsgType+")")
if err != nil {
return err
}
r2.re, err = regexp.Compile(r2.Rule)
logOnError(err, "resetMsgParsingRules : Compile("+r.Rule+")")
if err != nil {
return err
}
rules2 = append(rules2, r2)
}
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
if err != nil {
return err
}
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, chat_id, user_id, descn, rule)
VALUES (?, ?, ?, ?, ?, ?);`)
logOnError(err, "resetMsgParsingRules : prepare statement")
if err != nil {
return err
}
defer stmt.Close()
count = 0
for _, r2 := range rules2 {
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.ChatID64, r2.SenderUserID64, r2.Description, r2.Rule)
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
if err != nil {
return err
}
count += 1
}
log.Printf("%d rules inserted.\n", count)
return nil
}
func loadMsgParsingRules() (m map[int]MessageParsingRule, err error) {
var (
id int32
priority int32
descn string
rule string
msgTypeID64 int64
chatID64 int64
userID64 int64
)
log.Println("Loading message parsing rules...")
m = make(map[int]MessageParsingRule)
count := int(0)
defer func() {
if rec := recover(); rec != nil {
log.Println("Error parsing rules : ", rec)
err = errors.New("panic")
}
}()
rules, err := db.Query(`SELECT r.id, r.prio, r.descn, r.rule, r.msg_type_id, r.chat_id, r.user_id FROM msg_rules r ORDER BY r.prio DESC;`)
if err != nil {
return m, err
}
defer rules.Close()
for rules.Next() {
err = rules.Scan(&id, &priority, &descn, &rule, &msgTypeID64, &chatID64, &userID64)
if err != nil {
return m, err
}
i := new(MessageParsingRule)
i.ID = id
i.Priority = priority
i.Description = descn
i.Rule = rule
i.MsgTypeID64 = msgTypeID64
i.ChatID64 = chatID64
i.SenderUserID64 = userID64
i.re = regexp.MustCompile(rule)
m[count] = *i
// log.Printf("New rule : %s\n", rule)
count++
}
return m, nil
}