chirpnest/rules.go

427 lines
11 KiB
Go
Raw Permalink 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-06-25 17:22:54 +02:00
BotCommand: true,
2020-01-15 11:17:38 +01:00
}
rules2 = append(rules2, r)
2020-02-14 04:08:19 +01:00
r = MessageParsingRule{
Priority: 9999,
Description: "Shutdown",
Rule: "^/shutdown$",
MsgTypeID64: cacheObjSubType[`msg_bot_shutdown`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: 0,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-14 04:08:19 +01:00
}
rules2 = append(rules2, r)
2020-06-26 11:45:56 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "Shutdown",
Rule: "^/msg_refresh (?P<MsgID>[0-9]*)$",
MsgTypeID64: cacheObjSubType[`msg_refresh_cmd`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: 0,
BotCommand: true,
}
rules2 = append(rules2, r)
2020-02-10 03:43:24 +01:00
chats := make([]int64, 0)
users := make([]int64, 0)
chats = append(chats, cfg.Bot.Mainchat)
users = append(users, 0)
muxClients.RLock()
2020-02-10 03:44:23 +01:00
for id, c := range clients {
if c.Active {
chats = append(chats, id)
users = append(users, id)
2020-02-10 03:56:05 +01:00
r = MessageParsingRule{
Priority: 9999,
Description: "Config",
Rule: "^/config$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:56:05 +01:00
}
rules2 = append(rules2, r)
2020-02-10 05:28:16 +01:00
r = MessageParsingRule{
Priority: 9999,
Description: "Config",
Rule: "^/config_pillage_on$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_pillage_on`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 05:28:16 +01:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Config",
Rule: "^/config_pillage_off$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_pillage_off`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 05:28:16 +01:00
}
rules2 = append(rules2, r)
2020-02-29 09:43:04 +01:00
2020-04-07 04:40:53 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "Config",
Rule: "^/config_def_guild$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_def_guild`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-04-07 04:40:53 +02:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Config",
Rule: "^/config_def_castle$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_def_castle`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-04-07 04:40:53 +02:00
}
rules2 = append(rules2, r)
2020-02-29 09:43:04 +01:00
r = MessageParsingRule{
Priority: 9999,
Description: "Alch your supply",
Rule: "^/alch_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_alch_all`],
2020-02-29 09:48:47 +01:00
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-29 09:43:04 +01:00
}
rules2 = append(rules2, r)
2020-06-17 12:15:54 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "Turn auto deposit on",
Rule: "^/config_deposit_on$",
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_deposit_on`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-06-17 12:15:54 +02:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Turn auto deposit off",
2020-06-17 12:17:59 +02:00
Rule: "^/config_deposit_off$",
2020-06-17 12:15:54 +02:00
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_deposit_off`],
ChatID64: id,
SenderUserID64: id,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-06-17 12:15:54 +02:00
}
rules2 = append(rules2, r)
2020-02-10 03:44:23 +01:00
}
2020-02-10 03:43:24 +01:00
}
muxClients.RUnlock()
// commands
for id, _ := range chats {
muxObjItem.RLock()
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: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:43:24 +01:00
}
rules2 = append(rules2, r)
2021-01-21 15:20:08 +01:00
2021-01-21 15:22:01 +01:00
if len(o.Craft.Command) > 6 && o.Craft.Command[0:6] == "/brew_" {
2021-01-21 15:20:08 +01:00
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item brew %s", o.Code),
Rule: fmt.Sprintf("^\\/alch_(?P<Code>%s)$", regexp.QuoteMeta(o.Code)),
MsgTypeID64: cacheObjSubType[`msg_bot_brew_item`],
ChatID64: 0,
SenderUserID64: users[id],
BotCommand: true,
}
rules2 = append(rules2, r)
}
2020-01-26 11:30:37 +01:00
}
}
2020-02-10 03:43:24 +01:00
muxObjItem.RUnlock()
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:43:24 +01:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Shops list",
Rule: "^/shops$",
MsgTypeID64: cacheObjSubType[`msg_bot_shops`],
ChatID64: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:43:24 +01:00
}
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: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:43:24 +01:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-10 03:43:24 +01:00
}
rules2 = append(rules2, r)
2020-06-19 21:28:59 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "List exchange deals on hold",
Rule: "^/stash$",
MsgTypeID64: cacheObjSubType[`msg_bot_get_stash`],
ChatID64: chats[id],
SenderUserID64: users[id],
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-06-19 21:28:59 +02:00
}
rules2 = append(rules2, r)
2020-06-28 13:12:05 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "Vault valuation",
Rule: "^/vault_val$",
MsgTypeID64: cacheObjSubType[`msg_bot_vault_val`],
ChatID64: chats[id],
SenderUserID64: users[id],
BotCommand: true,
}
rules2 = append(rules2, r)
2020-08-08 12:02:21 +02:00
r = MessageParsingRule{
Priority: 9999,
Description: "Vault valuation (other)",
Rule: "^/vault_val_oth$",
MsgTypeID64: cacheObjSubType[`msg_bot_vault_val_oth`],
ChatID64: chats[id],
SenderUserID64: users[id],
BotCommand: true,
}
rules2 = append(rules2, r)
2020-01-26 11:30:37 +01:00
}
2020-02-10 03:43:24 +01:00
// chats
2020-02-22 05:30:16 +01:00
r = MessageParsingRule{
Priority: 9999,
Description: "Rage Up Withdrawal",
Rule: "^/rage_up$",
MsgTypeID64: cacheObjSubType[`msg_bot_rage_up`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-22 05:30:16 +01:00
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Peace Up Withdrawal",
Rule: "^/peace_up$",
MsgTypeID64: cacheObjSubType[`msg_bot_peace_up`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
2020-06-25 17:22:54 +02:00
BotCommand: true,
2020-02-22 05:30:16 +01:00
}
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)
2020-02-10 03:43:24 +01:00
// sanitize
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,
2020-06-25 17:22:54 +02:00
BotCommand: false,
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
}
2020-02-10 03:43:24 +01:00
// insert to SQL
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-06-25 17:22:54 +02:00
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, chat_id, user_id, descn, bot_cmd, rule)
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 {
2020-06-25 17:22:54 +02:00
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.ChatID64, r2.SenderUserID64, r2.Description, r2.BotCommand, 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
2020-06-25 17:22:54 +02:00
botCommand bool
2020-01-06 05:04:40 +01:00
)
log.Println("Loading message parsing rules...")
m = make(map[int]MessageParsingRule)
count := int(0)
2020-06-25 17:34:36 +02:00
countCommand := int(0)
2020-01-06 05:04:40 +01:00
defer func() {
if rec := recover(); rec != nil {
log.Println("Error parsing rules : ", rec)
err = errors.New("panic")
}
}()
2020-06-25 17:22:54 +02:00
rules, err := db.Query(`SELECT r.id, r.prio, r.descn, r.rule, r.msg_type_id, r.chat_id, r.user_id, r.bot_cmd FROM msg_rules r ORDER BY r.prio DESC;`)
2020-01-06 05:04:40 +01:00
if err != nil {
return m, err
}
defer rules.Close()
for rules.Next() {
2020-06-25 17:22:54 +02:00
err = rules.Scan(&id, &priority, &descn, &rule, &msgTypeID64, &chatID64, &userID64, &botCommand)
2020-01-06 05:04:40 +01:00
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
2020-06-25 17:22:54 +02:00
i.BotCommand = botCommand
2020-01-06 05:04:40 +01:00
i.re = regexp.MustCompile(rule)
m[count] = *i
// log.Printf("New rule : %s\n", rule)
count++
2020-06-25 17:34:36 +02:00
if botCommand {
countCommand++
}
2020-01-06 05:04:40 +01:00
}
2020-06-25 17:34:36 +02:00
log.Printf("Loaded %d rules (%d commands)\n", count, countCommand)
2020-01-06 05:04:40 +01:00
return m, nil
}