chirpnest/rules.go

233 lines
5.8 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-26 11:31:07 +01:00
"fmt"
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-15 11:17:38 +01:00
r := MessageParsingRule{
Priority: 9999,
Description: "Withdrawal request",
2020-01-16 15:39:48 +01:00
Rule: "^To validate (@[A-Za-z0-9]*) withdrawal of\n([0-9]+ x .*\n)+(Click /withdraw_[a-f0-9]{32})$",
2020-01-17 03:47:03 +01:00
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_req`],
2020-01-15 11:17:38 +01:00
ChatID64: cfg.Bot.Mainchat,
2020-01-17 03:47:03 +01:00
SenderUserID64: int64(bot.Me.ID),
2020-01-15 11:17:38 +01:00
}
rules2 = append(rules2, r)
2020-01-15 11:22:41 +01:00
r = MessageParsingRule{
2020-01-15 11:17:38 +01:00
Priority: 9999,
Description: "Withdrawal acknowledgment",
2020-01-17 03:47:03 +01:00
Rule: "^/withdraw_(?P<Ref>[a-f0-9]{32})$",
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_ack`],
2020-01-15 11:17:38 +01:00
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
2020-01-26 11:30:37 +01:00
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)
2020-02-01 08:19:19 +01:00
r = MessageParsingRule{
Priority: 9998,
2020-02-01 08:22:18 +01:00
Description: "All item craft",
2020-02-01 08:19:19 +01:00
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
2020-01-26 11:30:37 +01:00
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),
2020-01-27 05:32:37 +01:00
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
2020-01-26 11:30:37 +01:00
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
2020-01-26 12:06:28 +01:00
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
2020-01-27 05:32:37 +01:00
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
2020-01-26 12:06:28 +01:00
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
2020-01-26 11:30:37 +01:00
}
}
2020-01-26 14:40:20 +01:00
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)
2020-01-26 15:12:27 +01:00
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)
2020-01-15 11:22:41 +01:00
r = MessageParsingRule{
2020-01-14 08:16:31 +01:00
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
}