package main //go:generate go-bindata data/ import ( "database/sql" "flag" "fmt" "log" "math/rand" "sync" "time" "gopkg.in/gcfg.v1" //"github.com/robfig/cron/v3" "gopkg.in/robfig/cron.v3" _ "github.com/go-sql-driver/mysql" tb "gopkg.in/tucnak/telebot.v2" ) type Config struct { Telegram struct { URL string Token string } SQL struct { Driver string Type string Address string Username string Password string Database string } Rabbit struct { User string Password string Host string Path string } Bot struct { Admin int64 Guildname string Guild string Mainchat int64 Depositchat int64 Reportchat int64 } } var ( config = flag.String("config", "chirpnest.cfg", "config file path") initdb = flag.Bool("initdb", false, "initialize bot database") db *sql.DB bot *tb.Bot cr *cron.Cron cfg Config RndSrc *rand.Rand RndMux sync.Mutex MQCWMsgQueue chan ChatWarsMessage SQLMsgIdentifyQueue chan int64 TGCmdQueue chan TGCommand MQTGCmdQueue chan TGCommand JobQueue chan Job msgParsingRules map[int]MessageParsingRule clients map[int64]*ChirpClient muxClients sync.RWMutex callbacks map[int64]map[int64][]int64 muxCallbacks sync.Mutex gDepositForwardMux sync.Mutex gDepositForwardMsg []int64 ) func PrintText(m *tb.Message) { fmt.Printf("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text) return } func main() { log.Println("Starting Chirpnest...") // Parsing config flag.Parse() // randomize RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) err := gcfg.ReadFileInto(&cfg, *config) failOnError(err, "Parsing config") _, err = Asset("data/code_obj_type.json") failOnError(err, "data/code_obj_type.json") _, err = Asset("data/code_obj_type.json") failOnError(err, "data/code_obj_sub_type.json") _, err = Asset("data/code_obj_type.json") failOnError(err, "data/msg_rules.json") // Connecting to DB switch cfg.SQL.Driver { case "mysql": db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true") failOnError(err, "Opening MySQL connection") defer db.Close() } // Check if any issue err = db.Ping() failOnError(err, "Checking MySQL connection") log.Println("SQL connection initialized") if *initdb { initDB() _, _ = addObjCastle(`🦌`, `Deerhorn`) _, _ = addObjCastle(`🐉`, `Dragonscale`) _, _ = addObjCastle(`🦅`, `Highnest`) _, _ = addObjCastle(`🌑`, `Moonlight`) _, _ = addObjCastle(`🥔`, `Potato`) _, _ = addObjCastle(`🦈`, `Sharkteeth`) _, _ = addObjCastle(`🐺`, `Wolfpack`) _, _ = addObjCastle(`⛔`, `Unknwon`) _, _ = addObjGuild(``, `No Guild`) } initCache() // Registering bot bot, err := tb.NewBot(tb.Settings{ Token: cfg.Telegram.Token, URL: cfg.Telegram.URL, Poller: &tb.LongPoller{Timeout: 10 * time.Second}, }) failOnError(err, "Registering bot") log.Println("Bot registered") log.Printf("URL : %s\n", bot.URL) log.Printf("Token : %s\n", bot.Token) go BotHandlers(bot) u := tb.User{ ID: int(cfg.Bot.Admin), } bot.Send(&u, "Bot restarted") cr = startCron() MQCWMsgQueue = make(chan ChatWarsMessage, MQCWMsgQueueSize) SQLMsgIdentifyQueue = make(chan int64, SQLMsgIdentifyQueueSize) TGCmdQueue = make(chan TGCommand, TGCmdQueueSize) MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize) JobQueue = make(chan Job, JobQueueSize) clients = make(map[int64]*ChirpClient) callbacks = make(map[int64]map[int64][]int64) for w := 1; w <= MQGetMsgWorkers; w++ { go MQGetMsgWorker(w, MQCWMsgQueue) } for w := 1; w <= SQLCWMsgWorkers; w++ { go SQLCWMsgWorker(w, MQCWMsgQueue, SQLMsgIdentifyQueue) } for w := 1; w <= SQLIdentifyMsgWorkers; w++ { go SQLIdentifyMsgWorker(w, SQLMsgIdentifyQueue) } for w := 1; w <= JobWorkers; w++ { go JobWorker(w, JobQueue) } for w := 1; w <= TGCmdWorkers; w++ { go TGCmdWorker(w, bot, TGCmdQueue) } for w := 1; w <= MQTGCmdWorkers; w++ { go MQTGCmdWorker(w, MQTGCmdQueue) } go SQLJobWorker() go MQKeepAliveWorker() go MQTidyKeepAliveWorker() log.Println("Bot started !") // Main loop for { time.Sleep(1 * time.Second) } }