381 lines
9.5 KiB
Go
381 lines
9.5 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),
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Shutdown",
|
|
Rule: "^/shutdown$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_shutdown`],
|
|
ChatID64: cfg.Bot.Admin,
|
|
SenderUserID64: 0,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
chats := make([]int64, 0)
|
|
users := make([]int64, 0)
|
|
chats = append(chats, cfg.Bot.Mainchat)
|
|
users = append(users, 0)
|
|
|
|
muxClients.RLock()
|
|
for id, c := range clients {
|
|
if c.Active {
|
|
chats = append(chats, id)
|
|
users = append(users, id)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Config",
|
|
Rule: "^/config$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_user_config`],
|
|
ChatID64: id,
|
|
SenderUserID64: id,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Config",
|
|
Rule: "^/config_pillage_on$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_pillage_on`],
|
|
ChatID64: id,
|
|
SenderUserID64: id,
|
|
BotCommand: true,
|
|
}
|
|
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,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Config",
|
|
Rule: "^/config_def_guild$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_def_guild`],
|
|
ChatID64: id,
|
|
SenderUserID64: id,
|
|
BotCommand: true,
|
|
}
|
|
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,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Alch your supply",
|
|
Rule: "^/alch_all$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_alch_all`],
|
|
ChatID64: id,
|
|
SenderUserID64: id,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
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,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Turn auto deposit off",
|
|
Rule: "^/config_deposit_off$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_user_config_deposit_off`],
|
|
ChatID64: id,
|
|
SenderUserID64: id,
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
}
|
|
}
|
|
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],
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
}
|
|
}
|
|
muxObjItem.RUnlock()
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Get Stock",
|
|
Rule: "^/g_stock$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
|
|
ChatID64: chats[id],
|
|
SenderUserID64: users[id],
|
|
BotCommand: true,
|
|
}
|
|
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],
|
|
BotCommand: true,
|
|
}
|
|
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],
|
|
BotCommand: true,
|
|
}
|
|
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],
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "List exchange deals on hold",
|
|
Rule: "^/stash$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_get_stash`],
|
|
ChatID64: chats[id],
|
|
SenderUserID64: users[id],
|
|
BotCommand: true,
|
|
}
|
|
rules2 = append(rules2, r)
|
|
|
|
}
|
|
|
|
// chats
|
|
r = MessageParsingRule{
|
|
Priority: 9999,
|
|
Description: "Rage Up Withdrawal",
|
|
Rule: "^/rage_up$",
|
|
MsgTypeID64: cacheObjSubType[`msg_bot_rage_up`],
|
|
ChatID64: cfg.Bot.Mainchat,
|
|
SenderUserID64: 0,
|
|
BotCommand: true,
|
|
}
|
|
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,
|
|
BotCommand: true,
|
|
}
|
|
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)
|
|
|
|
// sanitize
|
|
for _, r := range rules {
|
|
r2 := MessageParsingRule{
|
|
Priority: r.Priority,
|
|
Description: r.Description,
|
|
Rule: r.Rule,
|
|
MsgType: r.MsgType,
|
|
ChatID64: r.ChatID64,
|
|
SenderUserID64: r.SenderUserID64,
|
|
BotCommand: false,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// insert to SQL
|
|
_, 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, bot_cmd, 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.BotCommand, 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
|
|
botCommand bool
|
|
)
|
|
|
|
log.Println("Loading message parsing rules...")
|
|
m = make(map[int]MessageParsingRule)
|
|
count := int(0)
|
|
countCommand := 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, r.bot_cmd 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, &botCommand)
|
|
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.BotCommand = botCommand
|
|
i.re = regexp.MustCompile(rule)
|
|
m[count] = *i
|
|
// log.Printf("New rule : %s\n", rule)
|
|
count++
|
|
if botCommand {
|
|
countCommand++
|
|
}
|
|
}
|
|
|
|
log.Printf("Loaded %d rules (%d commands)\n", count, countCommand)
|
|
|
|
return m, nil
|
|
}
|