117 lines
2.1 KiB
Go
117 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"flag"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"gopkg.in/gcfg.v1"
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
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
|
|
Queue string
|
|
}
|
|
Bot struct {
|
|
Admin uint64
|
|
Guildname string
|
|
Guild string
|
|
}
|
|
}
|
|
|
|
var (
|
|
config = flag.String("config", "chirpnest.cfg", "config file path")
|
|
initdb = flag.Bool("initdb", false, "initialize bot database")
|
|
|
|
db *sql.DB
|
|
|
|
cfg Config
|
|
MQCWMsgQueue chan ChatWarsMessage
|
|
SQLMsgIdentifyQueue chan int64
|
|
msgParsingRules map[int]MessageParsingRule
|
|
)
|
|
|
|
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()
|
|
|
|
err := gcfg.ReadFileInto(&cfg, *config)
|
|
if err != nil {
|
|
log.Fatalf("Failed to parse gcfg data: %s", err)
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
}
|
|
|
|
// Check if any issue
|
|
err = db.Ping()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
} else {
|
|
log.Println("SQL connection initialized")
|
|
}
|
|
|
|
if *initdb {
|
|
initDB()
|
|
}
|
|
|
|
msgParsingRules, err := loadMsgParsingRules()
|
|
failOnError(err, "Message parsing rules")
|
|
|
|
go StartBot()
|
|
|
|
MQCWMsgQueue = make(chan ChatWarsMessage, 100)
|
|
SQLMsgIdentifyQueue = make(chan int64, 100)
|
|
|
|
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)
|
|
}
|
|
|
|
fmt.Println("Started !")
|
|
|
|
// Main loop
|
|
for {
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
|
|
}
|