chirpnest/rules.go
2020-02-01 15:22:18 +08:00

233 lines
5.8 KiB
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"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)
r := MessageParsingRule{
Priority: 9999,
Description: "Withdrawal request",
Rule: "^To validate (@[A-Za-z0-9]*) withdrawal of\n([0-9]+ x .*\n)+(Click /withdraw_[a-f0-9]{32})$",
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_req`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: int64(bot.Me.ID),
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Withdrawal acknowledgment",
Rule: "^/withdraw_(?P<Ref>[a-f0-9]{32})$",
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_ack`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9998,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
muxObjItem.RLock()
defer muxObjItem.RUnlock()
for _, o := range objItems {
if o.Craft != nil {
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
}
}
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 1,
Description: "Default Main chat",
Rule: "(?s:.*)",
MsgTypeID64: cacheObjSubType[`msg_guild_main`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 1,
Description: "Default Deposit chat",
Rule: "(?s:.*)",
MsgTypeID64: cacheObjSubType[`msg_guild_deposit`],
ChatID64: cfg.Bot.Depositchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
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
}