76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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, sender_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 loaded.\n", count)
|
|
|
|
return nil
|
|
|
|
}
|