From ad026c4b681e56ad8c9e667e216cbc4093f8bf4f Mon Sep 17 00:00:00 2001 From: shoopea Date: Wed, 31 Jul 2019 13:07:12 +0800 Subject: [PATCH] compilation fix for client revamping --- bot.go | 161 +++++++++++++++++++++++++++++++++++++++++++++++++----- client.go | 4 +- main.go | 2 +- 3 files changed, 151 insertions(+), 16 deletions(-) diff --git a/bot.go b/bot.go index beb6f65..8e69a9f 100644 --- a/bot.go +++ b/bot.go @@ -165,18 +165,19 @@ func botGetClients(m *tb.Message) { if !m.Private() { return } - if _, ok := clientsKeepAlive.Load(m.Chat.ID); ok { + if clt, ok := getLockedClient(m.Chat.ID, false); ok { + clt.Unlock() + muxClients.RLock() var ret string - clientsKeepAlive.Range(func(k, v interface{}) bool { - ka := v.(*MQKeepAlive) - if v2, ok2 := clientsCW.Load(ka.UserID64); ok2 { - clt := v2.(*ChatWarsClient) - ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d\n", ret, ka.Nickname, clt.UserID64, ka.UserID64) + for id, c := range clients { + if c.Active { + ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (online)\n", ret, c.MQ.User, c.CWUserID64, c.TGUserID64) } else { - ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d\n", ret, ka.Nickname, 0, ka.UserID64) + ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (offline)\n", ret, c.MQ.User, c.CWUserID64, c.TGUserID64) } - return true - }) + } + muxClients.RUnlock() + c := TGCommand{ Type: commandReplyMsg, Text: ret, @@ -201,7 +202,8 @@ func botMsgRescan(m *tb.Message) { if !m.Private() { return } - if _, ok := clientsKeepAlive.Load(m.Chat.ID); !ok && m.Chat.ID != cfg.Bot.Admin { + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", @@ -211,6 +213,17 @@ func botMsgRescan(m *tb.Message) { TGCmdQueue <- c return } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { p := JobPayloadRescanMsg{ @@ -251,7 +264,8 @@ func botMsgRescanAll(m *tb.Message) { if !m.Private() { return } - if _, ok := clientsKeepAlive.Load(m.Chat.ID); !ok && m.Chat.ID != cfg.Bot.Admin { + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", @@ -261,6 +275,17 @@ func botMsgRescanAll(m *tb.Message) { TGCmdQueue <- c return } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } p := JobPayloadRescanMsg{ Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d ORDER BY id ASC;", objTypeMessage, objSubTypeMessageUnknown), MsgID64: int64(m.ID), @@ -294,7 +319,8 @@ func botBackupExport(m *tb.Message) { if !m.Private() { return } - if _, ok := clientsKeepAlive.Load(m.Chat.ID); !ok && m.Chat.ID != cfg.Bot.Admin { + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", @@ -304,6 +330,17 @@ func botBackupExport(m *tb.Message) { TGCmdQueue <- c return } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } p := JobPayloadBackupExport{ MsgID64: int64(m.ID), @@ -320,7 +357,8 @@ func botBackupImport(m *tb.Message) { if !m.Private() { return } - if _, ok := clientsKeepAlive.Load(m.Chat.ID); !ok && m.Chat.ID != cfg.Bot.Admin { + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", @@ -330,6 +368,17 @@ func botBackupImport(m *tb.Message) { TGCmdQueue <- c return } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } r := regexp.MustCompile(`^((http[s]?\:)\/\/)?([^\?\:\/#]+)(\:([0-9]+))?(\/[^\?\#]*)?(\?([^#]*))?(#.*)?.zip$`) if !r.MatchString(m.Payload) { c := TGCommand{ @@ -356,6 +405,32 @@ func botBackupImport(m *tb.Message) { func botMsgDump(m *tb.Message) { var res string + if !m.Private() { + return + } + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } + r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { objId, _ := strconv.ParseInt(m.Payload, 10, 64) @@ -390,6 +465,28 @@ func botListParsingRules(m *tb.Message) { if !m.Private() { return } + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } for _, v := range msgParsingRules { s = fmt.Sprintf("%s[%d] %s\n", s, v.ID, v.Description) @@ -409,6 +506,28 @@ func botListParsingRule(m *tb.Message) { if !m.Private() { return } + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } + clt.Unlock() + if clt.TGUserID64 != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Admin only", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { i, _ := strconv.ParseInt(m.Payload, 10, 64) @@ -465,6 +584,22 @@ func botGStock(m *tb.Message) { } func botTimer(m *tb.Message) { + if !m.Private() { + return + } + clt, ok := getLockedClient(m.Chat.ID, false) + if !ok { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } + clt.Unlock() + r := regexp.MustCompile("^(?P([0-9]*(s|m|h))+) \"(?P(.*))\"$") if r.MatchString(m.Payload) { d, err := time.ParseDuration(r.ReplaceAllString(m.Payload, "${Duration}")) diff --git a/client.go b/client.go index ed2c833..a0027f9 100644 --- a/client.go +++ b/client.go @@ -6,7 +6,7 @@ import ( ) func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) { - muxClients.Lock() + muxClients.RLock() if c, ok := clients[id]; ok { c.Mux.Lock() return c, true @@ -20,7 +20,7 @@ func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) { } else { return 0, false } - defer muxClients.Unlock() + defer muxClients.RUnlock() } func clientSendCWMsg(userID64 int64, s string) { diff --git a/main.go b/main.go index bcaa6c6..15594e8 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ var ( msgParsingRules map[int]MessageParsingRule clients map[int64]*ChirpClient - muxClients sync.Mutex + muxClients sync.RWMutex ) func PrintText(m *tb.Message) {