gocw/main.go
2019-05-02 23:18:24 +08:00

844 lines
46 KiB
Go

package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
"crypto/md5"
"encoding/base32"
"encoding/json"
"math/rand"
"github.com/Arman92/go-tdlib"
"github.com/streadway/amqp"
// "github.com/go-web/tokenizer"
// "database/sql"
// _ "github.com/go-sql-driver/mysql"
)
type MsgDirection int
const (
Incoming MsgDirection = 1
Outgoing MsgDirection = 2
)
type ChatWarsMessage struct {
UserID64 int64 `json:"user_id"`
Direction MsgDirection `json:"direction"`
MsgText string `json:"msg"`
MsgID64 int64 `json:"msg_id"`
}
type ChatWarsWithdraw struct {
Item string `json:"item"`
Quantity int64 `json:"quantity"`
UserID64 int64 `json:"user_id"`
Time time.Time `json:"time"`
}
const user_chtwrsbot = 408101137
const user_botnestbot = 541634211
const user_chtwrscastlebot = 526586204
const chat_darkwing = -1001152534174
const chat_guild_deposit = -1001179598662
const chat_guild = -1001152534174
const chat_private = -1001092914472
var ownUserID64 = int64(0)
var ownUserID32 = int32(0)
var msgMutex sync.Mutex
var msgID64 = int64(0)
var msgUserID64 = int64(0)
var msgText string
var vault Vault
var size int
var withdrawItem string
var withdrawUserID64 int64
var withdrawTime time.Time
var aesKey []byte
var hmacKey []byte
var withdrawals = map[string]ChatWarsWithdraw{}
var TgEncoding *base32.Encoding
func main() {
// msgMutex = &sync.Mutex{}
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./errors.txt")
TgEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
// Create new instance of client
client := tdlib.NewClient(tdlib.Config{
APIID: "187786",
APIHash: "e782045df67ba48e441ccb105da8fc85",
SystemLanguageCode: "en",
DeviceModel: "Shoogram",
SystemVersion: "1.3.3.7",
ApplicationVersion: "3.1.3.3.7",
UseMessageDatabase: true,
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseTestDataCenter: false,
DatabaseDirectory: "./tdlib-db",
FileDirectory: "./tdlib-files",
IgnoreFileNames: false,
})
for {
currentState, _ := client.Authorize()
if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
fmt.Print("Enter phone: ")
var number string
fmt.Scanln(&number)
_, err := client.SendPhoneNumber(number)
if err != nil {
fmt.Printf("Error sending phone number: %v", err)
}
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
fmt.Print("Enter code: ")
var code string
fmt.Scanln(&code)
_, err := client.SendAuthCode(code)
if err != nil {
fmt.Printf("Error sending auth code : %v", err)
}
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
fmt.Print("Enter Password: ")
var password string
fmt.Scanln(&password)
_, err := client.SendAuthPassword(password)
if err != nil {
fmt.Printf("Error sending auth password: %v", err)
}
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
fmt.Println("Authorization Ready! Let's rock")
break
}
}
ownUserID32 = OwnUserID(client)
ownUserID64 = int64(OwnUserID(client))
go PillageKnight(client)
go ListenDarkwing(client)
go ListenCW(client)
go Listen(client)
go ListenGuild(client)
// go ForwardOwnMsg(client)
fmt.Println("Started !")
// Main loop
for {
time.Sleep(30 * time.Second)
}
}
func PillageKnight(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
// viaBotUserID := updateMsg.Message.ViaBotUserID
// fmt.Println("UpdateNewMessage : chatID : ", chatID)
// fmt.Println("UpdateNewMessage : senderUserID : ", senderUserID)
// fmt.Println("UpdateNewMessage : viaBotUserID : ", viaBotUserID)
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) {
return true
}
return false
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
// senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
/*
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("Pillage:", err.Error())
continue
}
*/
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
match, _ := regexp.MatchString("You were strolling around on your horse.*To stop him click /go.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PILLAGE KNIGHT INCOMING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
fwdMsg := ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, ownUserID64)
delay := rand.Intn(20)
fmt.Println("Delaying reply by", delay + 5, "seconds")
time.Sleep(time.Duration(delay) * time.Second)
c.SetOption("online", tdlib.NewOptionValueBoolean(true))
time.Sleep(5 * time.Second)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText("/go\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ := c.SendMessage(updateMsg.Message.ChatID, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/go")
fmt.Println("send msgID64 (go) : ", msgID64)
delay = rand.Intn(10) + 20
time.Sleep(time.Duration(delay) * time.Second)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("🛡Defend\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(updateMsg.Message.ChatID, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("🛡Defend")
fmt.Println("send msgID64 (defend) : ", msgID64)
time.Sleep(1 * time.Second)
DeleteMsg(c, ownUserID64, fwdMsg)
time.Sleep(7 * time.Second)
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
break
}
match, _ = regexp.MatchString(".*/pledge.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PLEDGE INCOMING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
fwdMsg := ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, ownUserID64)
delay := rand.Intn(20)
fmt.Println("Delaying reply by", delay, "seconds")
time.Sleep(time.Duration(delay) * time.Second)
c.SetOption("online", tdlib.NewOptionValueBoolean(true))
time.Sleep(5 * time.Second)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText("/pledge\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ := c.SendMessage(updateMsg.Message.ChatID, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/pledge")
fmt.Println("send msgID64 (pledge) : ", msgID64)
time.Sleep(1 * time.Second)
DeleteMsg(c, ownUserID64, fwdMsg)
time.Sleep(7 * time.Second)
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
break
}
}
}
}
}
func ListenCW(c *tdlib.Client) {
var msgDisplay, msgDelete bool
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) {
return true
}
return false
}
conn, _ := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars")
defer conn.Close()
ch, _ := conn.Channel()
defer ch.Close()
q, _ := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("ListenCW:", err.Error())
continue
}
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
fmt.Println("============== CHAT WARS (", user.Username, ") ================================================================")
fmt.Println(txt, "\n")
fmt.Println("===============================================================================================================")
m := ChatWarsMessage{
UserID64 : ownUserID64,
MsgText : string(txt),
Direction : Incoming,
MsgID64 : updateMsg.Message.ID,
}
b, err := json.Marshal(m)
_ = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(b),
})
fmt.Println("recv msgID64 : ", msgID64)
if msgID64 != int64(0) {
switch msgText {
case "/g_stock_res": vault.Res = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_alch": vault.Alch = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_misc": vault.Misc = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_rec": vault.Rec = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_parts": vault.Parts = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_other": vault.Other = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_withdraw": time.Sleep(1 * time.Second) ; _ = ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, msgUserID64) ; msgDisplay = false; msgDelete = false
default: fmt.Println("No specific task for ", msgText) ; msgDisplay = true; msgDelete = false
}
time.Sleep(1 * time.Second)
if msgDelete {
DeleteMsg(c, user_chtwrsbot, msgID64)
time.Sleep(1 * time.Second)
DeleteMsg(c, user_chtwrsbot, updateMsg.Message.ID)
}
msgID64 = int64(0)
msgUserID64 = int64(0)
msgMutex.Unlock()
} else {
msgDisplay = true
}
if msgDisplay {
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
// fmt.Println("Testing line:", line)
match, _ := regexp.MatchString(".*Your result on the battlefield:.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! REPORT SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(user_botnestbot))
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(user_chtwrscastlebot))
break
}
match, _ = regexp.MatchString(".*Leaderboard of fighters are updated: /top5 & /top6. ((Cheer up)|(Congratulations))!.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FIGHT SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(user_chtwrscastlebot))
break
}
match, _ = regexp.MatchString("Deposited [⚡📕📗📘📙📒'a-zA-Z0-9+ ]* \\([0-9]*\\) successfully", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPOSIT SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(chat_guild_deposit))
break
}
match, _ = regexp.MatchString("Received [⚡📕📗📘📙📒'a-zA-Z0-9+ ]* x [0-9]*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RECEPTION SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(chat_guild_deposit))
break
}
}
}
}
}
}
func ListenDarkwing(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_botnestbot && chatID == chat_darkwing) {
return true
}
return false
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("ListenDarkwing:", err.Error())
continue
}
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
fmt.Println("============== DARKWING (", user.Username, ") =================================================================")
fmt.Println(txt, "\n")
fmt.Println("===============================================================================================================")
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
match, _ := regexp.MatchString(".*Your result on the battlefield:.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!! REPORT SPOTTED !!!!!!!!!!!!!!!!!")
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(user_botnestbot))
time.Sleep(1 * time.Second)
ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, int64(user_chtwrscastlebot))
break
}
}
}
}
}
func ListenGuild(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID
if (chatID == chat_guild) {
return true
}
return false
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("ListenGuild:", err.Error())
continue
}
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
fmt.Println("============== GUILD (", user.Username, ") =================================================================")
fmt.Println(txt, "\n")
fmt.Println("===============================================================================================================")
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
validID := regexp.MustCompile(`^/g_stock$`)
if validID.MatchString(line) {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GUILD STOCK REPORT SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_res\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ := c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_res")
fmt.Println("send msgID64 (g_stock_res) : ", msgID64)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_alch\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_alch")
fmt.Println("send msgID64 (g_stock_alch) : ", msgID64)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_misc\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_misc")
fmt.Println("send msgID64 (g_stock_misc) : ", msgID64)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_rec\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_rec")
fmt.Println("send msgID64 (g_stock_rec) : ", msgID64)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_parts\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_parts")
fmt.Println("send msgID64 (g_stock_parts) : ", msgID64)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText("/g_stock_other\n", nil), true, true)
msgMutex.Lock()
sendMsg, _ = c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_stock_other")
fmt.Println("send msgID64 (g_stock_other) : ", msgID64)
msgMutex.Lock()
msgMutex.Unlock()
r := regexp.MustCompile(`(?P<Code>[a-z0-9]+) (?P<Name>[⚡📕📗📘📙📒'a-zA-Z0-9+ ]+) x (?P<Quantity>[0-9]+)`)
s := regexp.MustCompile(`(?P<Modifier>⚡\+[0-9]+) (?P<BaseName>[a-zA-Z ]+)`)
fmt.Println("=====================> Exploring vault.Res")
vault.ResSize = 0
vault.ResCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Res, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.ResSize = vault.ResSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.ResCount = vault.ResCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Res : %d\n", vault.ResSize)
fmt.Println("=====================> Exploring vault.Alch")
vault.AlchSize = 0
vault.AlchCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Alch, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.AlchSize = vault.AlchSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.AlchCount = vault.AlchCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Alch : %d\n", vault.AlchSize)
fmt.Println("=====================> Exploring vault.Misc")
vault.MiscSize = 0
vault.MiscCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Misc, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.MiscSize = vault.MiscSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.MiscCount = vault.MiscCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Misc : %d\n", vault.MiscSize)
fmt.Println("=====================> Exploring vault.Rec")
vault.RecSize = 0
vault.RecCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Rec, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.RecSize = vault.RecSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.RecCount = vault.RecCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Rec : %d\n", vault.RecSize)
fmt.Println("=====================> Exploring vault.Parts")
vault.PartsSize = 0
vault.PartsCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Parts, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.PartsSize = vault.PartsSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.PartsCount = vault.PartsCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Parts : %d\n", vault.PartsSize)
fmt.Println("=====================> Exploring vault.Other")
vault.OtherSize = 0
vault.OtherCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Other, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.OtherSize = vault.OtherSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.OtherCount = vault.OtherCount + int32(size)
}
} else {
fmt.Printf("=====================> Item not found in map : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
itemName := r.ReplaceAllString(item, "${Name}")
if s.FindStringSubmatch(itemName) != nil {
for k, v := range items {
if strings.Compare(v.Name, s.ReplaceAllString(itemName, "${BaseName}")) == 0 {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.OtherSize = vault.OtherSize + items[k].Weight * int32(size)
vault.OtherCount = vault.OtherCount + int32(size)
fmt.Printf("=====================> Found in map : %s (%d)\n", items[k].Name, size)
}
}
}
}
} else {
if item != "Guild Warehouse:" {
fmt.Printf("=====================> Item not matched : %s\n", item)
}
}
}
fmt.Printf("=====================> Size for vault.Other : %d\n", vault.OtherSize)
msgText = fmt.Sprintf("Current stock [%d/38000] :\n - Resources : %d (%d)\n - Alchemist : %d (%d)\n - Misc stuff : %d (%d)\n - Recipes : %d (%d)\n - Parts : %d (%d)\n - Other : %d (%d)\n", vault.ResSize + vault.AlchSize + vault.MiscSize + vault.RecSize + vault.PartsSize + vault.OtherSize, vault.ResSize, vault.ResCount, vault.AlchSize, vault.AlchCount, vault.MiscSize, vault.MiscCount, vault.RecSize, vault.RecCount, vault.PartsSize, vault.PartsCount, vault.OtherSize, vault.OtherCount)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
sendMsg, _ = c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
break
}
validID = regexp.MustCompile(`^/g_withdraw ([a-z0-9]+) ([0-9]+)$`)
if validID.MatchString(line) {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GUILD WITHDRAWAL SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
t := regexp.MustCompile(`/g_withdraw (?P<Code>[a-z0-9]+) (?P<Quantity>[0-9]+)`)
if t.FindStringSubmatch(line) != nil {
size, _ = strconv.Atoi(t.ReplaceAllString(line, "${Quantity}"))
fmt.Printf("Withdrawing %s / %d\n", t.ReplaceAllString(line, "${Code}"), size)
m := ChatWarsWithdraw{
Item : t.ReplaceAllString(line, "${Code}"),
Quantity : int64(size),
UserID64 : int64(senderUserID),
Time : time.Now(),
}
b, _ := json.Marshal(m)
md5Bytes := md5.Sum(b)
md5String := TgEncoding.EncodeToString(md5Bytes[:])
withdrawals[md5String] = m;
msgText = fmt.Sprintf("To validate @%s withdrawal of %d x %s\nClick /withdraw_%s\n", user.Username, size, items[t.ReplaceAllString(line, "${Code}")].Name, md5String)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
} else {
fmt.Printf("Can't match withdrawing request.\n")
}
break
}
validID = regexp.MustCompile(`^/withdraw_([ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]{26})$`)
if validID.MatchString(line) {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GUILD WITHDRAW CONF SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
u := regexp.MustCompile(`^/withdraw_(?P<Reference>[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]{26})$`)
if u.FindStringSubmatch(line) != nil {
fmt.Printf("Withdrawal %s identified\n", u.ReplaceAllString(line, "${Reference}"))
ref := u.ReplaceAllString(line, "${Reference}")
if _, ok := withdrawals[ref]; ok {
fmt.Printf("Withdrawal %s found\n", ref)
t := time.Now()
if t.Sub(withdrawals[ref].Time) > 300 * time.Second {
msgText = fmt.Sprintf("Reference %s is expired.\n", ref)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
} else {
if withdrawals[ref].UserID64 != ownUserID64 && withdrawals[ref].UserID64 == int64(senderUserID) {
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText("You cannot validate your own withdrawal requests.\n", nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
} else {
msgText = fmt.Sprintf("/g_withdraw %s %d\n", withdrawals[ref].Item, withdrawals[ref].Quantity)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
msgMutex.Lock()
sendMsg, _ := c.SendMessage(user_chtwrsbot, 0, false, false, nil, inputMsgTxt)
msgID64 = sendMsg.ID
msgText = string("/g_withdraw")
msgUserID64 = withdrawals[ref].UserID64
fmt.Println("send msgID64 (g_withdraw) : ", msgID64)
delete(withdrawals, ref)
}
}
} else {
msgText = fmt.Sprintf("Reference %s is not found.\n", ref)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
}
} else {
msgText = fmt.Sprintf("Reference is malformed\n")
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
}
break
}
}
}
}
}
/*
func ForwardOwnMsg(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == ownUserID32 && chatID != ownUserID64) {
return true
}
return false
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
fwdMsg := ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, ownUserID64)
time.Sleep(2 * time.Second)
DeleteMsg(c, fwdMsg)
}
}
}
*/
func Listen(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID
if (chatID == user_chtwrsbot || chatID == user_botnestbot || chatID == user_chtwrscastlebot || chatID == chat_darkwing || chatID == chat_guild) {
return false
}
return true
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
username := "null"
if senderUserID != 0 {
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("Listen:", err.Error())
continue
} else {
username = user.Username
}
}
t := time.Now()
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
fmt.Println(" === SENT (", username, ") =====================================================================")
fmt.Println(txt, "\n")
fmt.Println("************ DETAILS ************")
fmt.Println("ID : ", updateMsg.Message.ID)
fmt.Println("SenderUserID : ", updateMsg.Message.SenderUserID)
fmt.Println("ChatID : ", updateMsg.Message.ChatID)
fmt.Println("SendingState : ", updateMsg.Message.SendingState)
fmt.Println("IsOutgoing : ", updateMsg.Message.IsOutgoing)
fmt.Println("CanBeEdited : ", updateMsg.Message.CanBeEdited)
fmt.Println("CanBeForwarded : ", updateMsg.Message.CanBeForwarded)
fmt.Println("IsChannelPost : ", updateMsg.Message.IsChannelPost)
fmt.Println("ContainsUnreadMention : ", updateMsg.Message.ContainsUnreadMention)
fmt.Println("ForwardInfo : ", updateMsg.Message.ForwardInfo)
fmt.Println("ReplyToMessageID : ", updateMsg.Message.ReplyToMessageID)
fmt.Println("ViaBotUserID : ", updateMsg.Message.ViaBotUserID)
fmt.Println("================================================================================================================")
}
}
}
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) (int64) {
msgIDs := make([]int64, 1)
msgIDs[0] = msgID
msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false)
if msgs != nil {
return msgs.Messages[0].ID
} else {
return 0
}
}
func DeleteMsg(c *tdlib.Client, chatID int64, msgID int64) {
msgIDs := make([]int64, 1)
msgIDs[0] = msgID
c.DeleteMessages(chatID, msgIDs, false)
fmt.Println("Deleting message ", msgID)
}
func OwnUserID(c *tdlib.Client) (int32) {
user, _ := c.GetMe()
return user.ID;
}