2015-06-25 07:34:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2015-06-25 23:15:28 +02:00
|
|
|
"fmt"
|
2015-06-25 07:34:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2015-06-25 23:15:28 +02:00
|
|
|
Token string `json:"token"`
|
|
|
|
Plugins map[string]string `json:"plugins"`
|
|
|
|
EnabledPlugins map[string]bool `json:"enabled"`
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Plugin interface {
|
2015-06-25 21:20:02 +02:00
|
|
|
GetName() string
|
|
|
|
GetCommands() []string
|
|
|
|
GetHelpText() []string
|
|
|
|
GotCommand(string, Message, []string)
|
2015-06-25 23:15:28 +02:00
|
|
|
Setup()
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var bot *BotApi
|
2015-06-25 21:20:02 +02:00
|
|
|
var plugins []Plugin
|
|
|
|
var config Config
|
2015-06-25 23:15:28 +02:00
|
|
|
var configPath *string
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
func main() {
|
2015-06-25 23:15:28 +02:00
|
|
|
configPath = flag.String("config", "config.json", "path to config.json")
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2015-06-25 21:20:02 +02:00
|
|
|
json.Unmarshal(data, &config)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
bot = NewBotApi(BotConfig{
|
2015-06-25 21:20:02 +02:00
|
|
|
token: config.Token,
|
2015-06-25 07:34:05 +02:00
|
|
|
debug: true,
|
|
|
|
})
|
|
|
|
|
2015-06-25 23:15:28 +02:00
|
|
|
plugins = []Plugin{&HelpPlugin{}, &FAPlugin{}, &ManagePlugin{}}
|
|
|
|
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
val, ok := config.EnabledPlugins[plugin.GetName()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
fmt.Printf("Enable '%s'? [y/N] ", plugin.GetName())
|
|
|
|
|
|
|
|
var enabled string
|
|
|
|
fmt.Scanln(&enabled)
|
|
|
|
|
|
|
|
if strings.ToLower(enabled) == "y" {
|
|
|
|
plugin.Setup()
|
|
|
|
log.Printf("Plugin '%s' started!\n", plugin.GetName())
|
|
|
|
|
|
|
|
config.EnabledPlugins[plugin.GetName()] = true
|
|
|
|
} else {
|
|
|
|
config.EnabledPlugins[plugin.GetName()] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val {
|
|
|
|
plugin.Setup()
|
|
|
|
log.Printf("Plugin '%s' started!\n", plugin.GetName())
|
|
|
|
}
|
|
|
|
|
|
|
|
saveConfig()
|
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 21:20:02 +02:00
|
|
|
ticker := time.NewTicker(time.Second)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
lastUpdate := 0
|
|
|
|
|
|
|
|
for range ticker.C {
|
2015-06-25 21:20:02 +02:00
|
|
|
update := NewUpdate(lastUpdate + 1)
|
|
|
|
update.Timeout = 30
|
|
|
|
|
|
|
|
updates, err := bot.getUpdates(update)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range updates {
|
|
|
|
lastUpdate = update.UpdateId
|
|
|
|
|
|
|
|
if update.Message.Text == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, plugin := range plugins {
|
2015-06-25 23:15:28 +02:00
|
|
|
val, _ := config.EnabledPlugins[plugin.GetName()]
|
|
|
|
if !val {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-25 07:34:05 +02:00
|
|
|
parts := strings.Split(update.Message.Text, " ")
|
2015-06-25 23:15:28 +02:00
|
|
|
command := parts[0]
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 21:20:02 +02:00
|
|
|
for _, cmd := range plugin.GetCommands() {
|
2015-06-25 23:15:28 +02:00
|
|
|
if cmd == command {
|
|
|
|
if bot.config.debug {
|
|
|
|
log.Printf("'%s' matched plugin '%s'", update.Message.Text, plugin.GetName())
|
|
|
|
}
|
|
|
|
|
2015-06-25 21:20:02 +02:00
|
|
|
args := append(parts[:0], parts[1:]...)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 23:15:28 +02:00
|
|
|
plugin.GotCommand(command, update.Message, args)
|
2015-06-25 21:20:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 23:15:28 +02:00
|
|
|
|
|
|
|
func saveConfig() {
|
|
|
|
data, _ := json.MarshalIndent(config, "", " ")
|
|
|
|
|
|
|
|
ioutil.WriteFile(*configPath, data, 0600)
|
|
|
|
}
|