chirpnest/main.go

154 lines
3.3 KiB
Go
Raw Normal View History

2019-04-15 13:21:16 +02:00
package main
2019-12-26 04:59:52 +01:00
//go:generate go-bindata data/
2019-04-15 13:21:16 +02:00
import (
2019-05-02 12:11:27 +02:00
"database/sql"
2019-05-03 05:58:36 +02:00
"flag"
"log"
2019-06-03 03:19:06 +02:00
"math/rand"
2019-06-03 03:21:59 +02:00
"sync"
2019-05-03 05:58:36 +02:00
"time"
2019-06-03 03:21:59 +02:00
2019-12-12 10:44:18 +01:00
//"github.com/robfig/cron/v3"
"gopkg.in/robfig/cron.v3"
2019-06-03 03:21:59 +02:00
_ "github.com/go-sql-driver/mysql"
tb "gopkg.in/tucnak/telebot.v2"
2019-04-15 13:21:16 +02:00
)
2019-05-02 12:11:27 +02:00
var (
2020-01-14 03:40:41 +01:00
config = flag.String("config", "config.json", "config file path")
2019-05-03 05:58:36 +02:00
initdb = flag.Bool("initdb", false, "initialize bot database")
2019-05-02 12:11:27 +02:00
2020-01-16 16:50:57 +01:00
db *sql.DB
bot *tb.Bot
botUserID64 int64
2019-05-03 05:58:36 +02:00
2019-12-29 08:50:56 +01:00
cr *cron.Cron
2019-12-12 10:42:05 +01:00
2019-06-03 03:19:06 +02:00
cfg Config
RndSrc *rand.Rand
2019-07-26 12:04:28 +02:00
RndMux sync.Mutex
2019-06-03 03:19:06 +02:00
2019-05-06 09:25:57 +02:00
MQCWMsgQueue chan ChatWarsMessage
SQLMsgIdentifyQueue chan int64
2019-05-16 04:21:04 +02:00
TGCmdQueue chan TGCommand
2019-05-16 14:41:28 +02:00
MQTGCmdQueue chan TGCommand
2019-05-31 14:24:27 +02:00
JobQueue chan Job
2019-06-03 03:19:06 +02:00
2019-06-14 05:03:43 +02:00
msgParsingRules map[int]MessageParsingRule
2019-06-03 10:43:22 +02:00
2019-07-31 03:24:01 +02:00
clients map[int64]*ChirpClient
2019-07-31 07:07:12 +02:00
muxClients sync.RWMutex
2019-08-17 08:18:25 +02:00
2019-08-17 08:38:37 +02:00
callbacks map[int64]map[int64][]int64
2019-08-17 08:18:25 +02:00
muxCallbacks sync.Mutex
2019-08-30 04:14:30 +02:00
gDepositForwardMux sync.Mutex
gDepositForwardMsg []int64
2019-05-02 12:11:27 +02:00
)
2019-04-15 13:21:16 +02:00
func main() {
2019-05-02 12:11:27 +02:00
log.Println("Starting Chirpnest...")
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Parsing config
2019-05-03 05:58:36 +02:00
flag.Parse()
2019-06-03 03:19:06 +02:00
// randomize
2019-06-03 03:21:31 +02:00
RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
2019-06-03 03:19:06 +02:00
2020-01-14 03:34:46 +01:00
err := ReadConfig(*config)
2019-05-11 05:29:09 +02:00
failOnError(err, "Parsing config")
2019-05-03 05:58:36 +02:00
2019-12-26 06:09:48 +01:00
_, err = Asset("data/code_obj_type.json")
2019-12-26 05:34:39 +01:00
failOnError(err, "data/code_obj_type.json")
2019-12-26 06:09:15 +01:00
_, err = Asset("data/code_obj_type.json")
2019-12-26 05:34:39 +01:00
failOnError(err, "data/code_obj_sub_type.json")
2019-12-26 06:09:15 +01:00
_, err = Asset("data/code_obj_type.json")
2019-12-26 05:34:39 +01:00
failOnError(err, "data/msg_rules.json")
2019-05-02 12:11:27 +02:00
// Connecting to DB
switch cfg.SQL.Driver {
2019-05-03 05:58:36 +02:00
case "mysql":
2019-05-09 12:36:45 +02:00
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true")
2019-05-11 05:29:09 +02:00
failOnError(err, "Opening MySQL connection")
2019-05-03 05:58:36 +02:00
defer db.Close()
2019-05-02 12:11:27 +02:00
}
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Check if any issue
err = db.Ping()
2019-05-11 05:29:09 +02:00
failOnError(err, "Checking MySQL connection")
log.Println("SQL connection initialized")
2019-05-03 05:58:36 +02:00
if *initdb {
2019-05-02 12:16:05 +02:00
initDB()
2020-01-02 16:44:54 +01:00
}
2020-01-16 14:28:34 +01:00
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)
2020-01-16 12:25:13 +01:00
initCache(*initdb)
2020-01-02 17:00:51 +01:00
2019-05-11 05:29:09 +02:00
// Registering bot
2019-12-29 08:50:56 +01:00
bot, err := tb.NewBot(tb.Settings{
2019-05-11 05:29:09 +02:00
Token: cfg.Telegram.Token,
URL: cfg.Telegram.URL,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
2020-01-16 16:50:57 +01:00
botUserID64 = int64(bot.Me.ID)
2020-01-16 16:56:05 +01:00
log.Printf("Test %p.\n", bot)
2020-01-16 16:53:54 +01:00
2019-05-11 05:29:09 +02:00
failOnError(err, "Registering bot")
log.Println("Bot registered")
2019-12-29 08:50:56 +01:00
log.Printf("URL : %s\n", bot.URL)
log.Printf("Token : %s\n", bot.Token)
2019-06-09 13:48:52 +02:00
2019-12-29 08:50:56 +01:00
go BotHandlers(bot)
2019-05-11 05:36:17 +02:00
u := tb.User{
2019-05-11 05:36:38 +02:00
ID: int(cfg.Bot.Admin),
2019-05-11 05:36:17 +02:00
}
2019-12-29 08:50:56 +01:00
bot.Send(&u, "Bot restarted")
2019-05-04 11:01:57 +02:00
2019-12-29 08:50:56 +01:00
cr = startCron()
2019-12-12 10:42:05 +01:00
2019-05-06 09:25:57 +02:00
for w := 1; w <= MQGetMsgWorkers; w++ {
2019-05-04 11:15:33 +02:00
go MQGetMsgWorker(w, MQCWMsgQueue)
}
2019-05-06 09:25:57 +02:00
for w := 1; w <= SQLCWMsgWorkers; w++ {
2019-05-16 14:44:59 +02:00
go SQLCWMsgWorker(w, MQCWMsgQueue, SQLMsgIdentifyQueue)
2019-05-04 11:01:57 +02:00
}
2019-05-06 09:25:57 +02:00
for w := 1; w <= SQLIdentifyMsgWorkers; w++ {
go SQLIdentifyMsgWorker(w, SQLMsgIdentifyQueue)
2019-05-05 13:29:28 +02:00
}
2019-05-31 14:30:08 +02:00
for w := 1; w <= JobWorkers; w++ {
go JobWorker(w, JobQueue)
2019-05-10 11:23:47 +02:00
}
2019-05-16 04:16:35 +02:00
for w := 1; w <= TGCmdWorkers; w++ {
2019-12-29 09:43:56 +01:00
go TGCmdWorker(w, bot, TGCmdQueue)
2019-05-16 04:16:35 +02:00
}
2019-05-16 14:39:12 +02:00
for w := 1; w <= MQTGCmdWorkers; w++ {
go MQTGCmdWorker(w, MQTGCmdQueue)
}
2019-05-31 14:24:27 +02:00
go SQLJobWorker()
2019-05-17 09:02:35 +02:00
go MQKeepAliveWorker()
go MQTidyKeepAliveWorker()
2019-04-15 13:21:16 +02:00
2019-05-11 06:54:12 +02:00
log.Println("Bot started !")
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
// Main loop
for {
time.Sleep(1 * time.Second)
}
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
}