chirpnest/rules.go

147 lines
3.3 KiB
Go
Raw Normal View History

2019-05-19 06:06:06 +02:00
package main
import (
2019-12-29 09:42:54 +01:00
"encoding/json"
2020-01-06 05:05:19 +01:00
"errors"
2020-01-02 16:53:54 +01:00
"log"
2019-12-29 12:49:20 +01:00
"regexp"
2019-05-19 06:06:06 +02:00
)
2019-12-31 01:32:06 +01:00
func resetMsgParsingRules() error {
2019-12-29 14:00:54 +01:00
var rules, rules2 []MessageParsingRule
2020-01-02 16:17:22 +01:00
var count int64
2019-12-29 12:45:26 +01:00
b, err := Asset("data/msg_rules.json")
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : load data/msg_rules.json")
2019-12-29 12:45:26 +01:00
if err != nil {
return err
}
err = json.Unmarshal(b, &rules)
2020-01-14 08:16:31 +01:00
r := MessageParsingRule{
Priority: 1,
Description: "Default Main chat",
Rule: "(?s:.*)",
2020-01-14 08:17:21 +01:00
MsgTypeID64: cacheObjSubType[`msg_guild_main`],
2020-01-14 08:16:31 +01:00
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
2020-01-14 08:17:36 +01:00
r = MessageParsingRule{
2020-01-14 08:16:31 +01:00
Priority: 1,
Description: "Default Deposit chat",
Rule: "(?s:.*)",
2020-01-14 08:17:21 +01:00
MsgTypeID64: cacheObjSubType[`msg_guild_deposit`],
2020-01-14 08:16:31 +01:00
ChatID64: cfg.Bot.Depositchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
2019-12-29 14:01:27 +01:00
for _, r := range rules {
2019-12-29 14:00:54 +01:00
r2 := MessageParsingRule{
2020-01-06 04:58:46 +01:00
Priority: r.Priority,
Description: r.Description,
Rule: r.Rule,
MsgType: r.MsgType,
ChatID64: r.ChatID64,
SenderUserID64: r.SenderUserID64,
2019-12-29 14:00:54 +01:00
}
2019-12-29 13:51:08 +01:00
2019-12-29 14:00:54 +01:00
r2.MsgTypeID64, err = codeObjSubTypeId(r2.MsgType)
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : codeObjSubTypeId("+r2.MsgType+")")
2019-12-29 12:45:26 +01:00
if err != nil {
return err
}
2019-12-29 14:00:54 +01:00
r2.re, err = regexp.Compile(r2.Rule)
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : Compile("+r.Rule+")")
2019-12-29 12:45:26 +01:00
if err != nil {
return err
}
2019-12-29 14:02:20 +01:00
2019-12-29 14:00:54 +01:00
rules2 = append(rules2, r2)
2019-12-29 12:45:26 +01:00
}
2019-12-29 12:45:49 +01:00
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
2019-12-26 09:16:19 +01:00
if err != nil {
return err
}
2020-01-06 05:09:38 +01:00
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, chat_id, user_id, descn, rule)
2020-01-06 04:58:46 +01:00
VALUES (?, ?, ?, ?, ?, ?);`)
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : prepare statement")
2019-12-29 08:50:56 +01:00
if err != nil {
return err
}
2019-12-29 12:45:26 +01:00
defer stmt.Close()
2020-01-02 16:17:22 +01:00
count = 0
2019-12-29 14:00:54 +01:00
for _, r2 := range rules2 {
2019-12-29 13:54:41 +01:00
2020-01-06 04:58:46 +01:00
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.ChatID64, r2.SenderUserID64, r2.Description, r2.Rule)
2019-12-31 01:32:06 +01:00
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
2019-12-29 12:45:26 +01:00
if err != nil {
return err
}
2020-01-02 16:17:22 +01:00
count += 1
2019-12-29 09:40:52 +01:00
2019-12-29 12:45:26 +01:00
}
2019-12-29 08:50:56 +01:00
2020-01-08 10:27:19 +01:00
log.Printf("%d rules inserted.\n", count)
2020-01-02 16:17:22 +01:00
2019-12-26 09:16:19 +01:00
return nil
}
2020-01-06 05:04:40 +01:00
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
}