This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/main.go

255 lines
7 KiB
Go
Raw Normal View History

2014-12-09 06:45:22 +01:00
/*
* MumbleDJ
* By Matthieu Grieger
* main.go
2016-06-21 02:16:05 +02:00
* Copyright (c) 2016 Matthieu Grieger (MIT License)
2014-12-09 06:45:22 +01:00
*/
package main
import (
2016-06-21 02:16:05 +02:00
"io/ioutil"
2015-04-09 22:23:21 +02:00
"os"
"strings"
2015-04-09 22:23:21 +02:00
2016-06-21 02:16:05 +02:00
"github.com/Sirupsen/logrus"
"github.com/matthieugrieger/mumbledj/bot"
"github.com/matthieugrieger/mumbledj/commands"
"github.com/matthieugrieger/mumbledj/services"
"github.com/spf13/viper"
"github.com/urfave/cli"
2014-12-09 06:45:22 +01:00
)
2016-06-21 02:16:05 +02:00
// DJ is a global variable that holds various details about the bot's state.
var DJ = bot.NewMumbleDJ()
2016-06-21 02:16:05 +02:00
func init() {
DJ.Commands = commands.Commands
DJ.AvailableServices = services.Services
2014-12-16 01:40:31 +01:00
2016-06-21 02:16:05 +02:00
// Injection into sub-packages.
commands.DJ = DJ
services.DJ = DJ
bot.DJ = DJ
2016-07-12 01:03:02 +02:00
DJ.Version = "v3.1.1"
2016-06-21 02:16:05 +02:00
logrus.SetLevel(logrus.WarnLevel)
}
2016-06-21 02:16:05 +02:00
func main() {
app := cli.NewApp()
app.Name = "MumbleDJ"
app.Usage = "A Mumble bot that plays audio from various media sites."
app.Version = DJ.Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: os.ExpandEnv("$HOME/.config/mumbledj/config.yaml"),
Usage: "location of MumbleDJ configuration file",
},
cli.StringFlag{
Name: "server, s",
Value: "127.0.0.1",
Usage: "address of Mumble server to connect to",
},
cli.StringFlag{
Name: "port, o",
Value: "64738",
Usage: "port of Mumble server to connect to",
},
cli.StringFlag{
Name: "username, u",
Value: "MumbleDJ",
Usage: "username for the bot",
},
cli.StringFlag{
Name: "password, p",
Value: "",
Usage: "password for the Mumble server",
},
cli.StringFlag{
Name: "channel, n",
Value: "",
Usage: "channel the bot enters after connecting to the Mumble server",
},
cli.StringFlag{
Name: "p12",
Value: "",
Usage: "path to user p12 file for authenticating as a registered user",
},
2016-06-21 02:16:05 +02:00
cli.StringFlag{
Name: "cert, e",
Value: "",
Usage: "path to PEM certificate",
},
cli.StringFlag{
Name: "key, k",
Value: "",
Usage: "path to PEM key",
},
cli.StringFlag{
Name: "accesstokens, a",
Value: "",
Usage: "list of access tokens separated by spaces",
},
cli.BoolFlag{
Name: "insecure, i",
Usage: "if present, the bot will not check Mumble certs for consistency",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "if present, all debug messages will be shown",
},
}
hiddenFlags := make([]cli.Flag, len(viper.AllKeys()))
for i, configValue := range viper.AllKeys() {
hiddenFlags[i] = cli.StringFlag{
Name: configValue,
Hidden: true,
}
}
app.Flags = append(app.Flags, hiddenFlags...)
2016-06-21 02:16:05 +02:00
app.Action = func(c *cli.Context) error {
if c.Bool("debug") {
logrus.SetLevel(logrus.InfoLevel)
}
2014-12-16 01:40:31 +01:00
for _, configValue := range viper.AllKeys() {
if c.GlobalIsSet(configValue) {
if strings.Contains(c.String(configValue), ",") {
viper.Set(configValue, strings.Split(c.String(configValue), ","))
} else {
viper.Set(configValue, c.String(configValue))
}
}
}
2016-06-21 02:16:05 +02:00
viper.SetConfigFile(c.String("config"))
if err := viper.ReadInConfig(); err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err.Error(),
}).Warnln("An error occurred while reading the configuration file. Using default configuration...")
if _, err := os.Stat(c.String("config")); os.IsNotExist(err) {
createConfigWhenNotExists()
}
2016-06-21 02:16:05 +02:00
} else {
if duplicateErr := bot.CheckForDuplicateAliases(); duplicateErr != nil {
logrus.WithFields(logrus.Fields{
"issue": duplicateErr.Error(),
}).Fatalln("An issue was discoverd in your configuration.")
2014-12-16 01:40:31 +01:00
}
2016-06-21 02:16:05 +02:00
createNewConfigIfNeeded()
viper.WatchConfig()
2014-12-16 01:40:31 +01:00
}
2014-12-13 05:52:07 +01:00
2016-06-21 02:16:05 +02:00
if c.GlobalIsSet("server") {
viper.Set("connection.address", c.String("server"))
}
if c.GlobalIsSet("port") {
viper.Set("connection.port", c.String("port"))
}
if c.GlobalIsSet("username") {
viper.Set("connection.username", c.String("username"))
}
if c.GlobalIsSet("password") {
viper.Set("connection.password", c.String("password"))
}
if c.GlobalIsSet("channel") {
viper.Set("defaults.channel", c.String("channel"))
}
if c.GlobalIsSet("p12") {
viper.Set("connection.user_p12", c.String("p12"))
}
2016-06-21 02:16:05 +02:00
if c.GlobalIsSet("cert") {
viper.Set("connection.cert", c.String("cert"))
}
if c.GlobalIsSet("key") {
viper.Set("connection.key", c.String("key"))
}
if c.GlobalIsSet("accesstokens") {
viper.Set("connection.access_tokens", c.String("accesstokens"))
}
if c.GlobalIsSet("insecure") {
viper.Set("connection.insecure", c.Bool("insecure"))
}
2016-06-21 02:16:05 +02:00
if err := DJ.Connect(); err != nil {
logrus.WithFields(logrus.Fields{
"error": err.Error(),
}).Fatalln("An error occurred while connecting to the server.")
}
2016-06-21 02:16:05 +02:00
if viper.GetString("defaults.channel") != "" {
defaultChannel := strings.Split(viper.GetString("defaults.channel"), "/")
DJ.Client.Do(func() {
DJ.Client.Self.Move(DJ.Client.Channels.Find(defaultChannel...))
})
}
2016-06-21 02:16:05 +02:00
DJ.Client.Do(func() {
DJ.Client.Self.SetComment(viper.GetString("defaults.comment"))
})
<-DJ.KeepAlive
2016-06-21 02:16:05 +02:00
return nil
}
2016-06-21 02:16:05 +02:00
app.Run(os.Args)
2014-12-09 06:45:22 +01:00
}
2014-12-10 00:41:50 +01:00
2016-06-21 02:16:05 +02:00
func createConfigWhenNotExists() {
configFile, err := Asset("config.yaml")
if err != nil {
logrus.Warnln("An error occurred while accessing config binary data. A new config file will not be written.")
2015-04-09 03:47:39 +02:00
} else {
2016-06-21 02:16:05 +02:00
filePath := os.ExpandEnv("$HOME/.config/mumbledj/config.yaml")
os.MkdirAll(os.ExpandEnv("$HOME/.config/mumbledj"), 0777)
2016-06-21 02:16:05 +02:00
writeErr := ioutil.WriteFile(filePath, configFile, 0644)
if writeErr == nil {
logrus.WithFields(logrus.Fields{
"file_path": filePath,
}).Infoln("A default configuration file has been written.")
} else {
2016-06-21 02:16:05 +02:00
logrus.WithFields(logrus.Fields{
"error": writeErr.Error(),
}).Warnln("An error occurred while writing a new config file.")
}
}
2016-06-21 02:16:05 +02:00
}
2016-06-21 02:16:05 +02:00
func createNewConfigIfNeeded() {
newConfigPath := os.ExpandEnv("$HOME/.config/mumbledj/config.yaml.new")
// Check if we should write an updated config file to config.yaml.new.
if assetInfo, err := AssetInfo("config.yaml"); err == nil {
asset, _ := Asset("config.yaml")
if configFile, err := os.Open(os.ExpandEnv("$HOME/.config/mumbledj/config.yaml")); err == nil {
configInfo, _ := configFile.Stat()
defer configFile.Close()
if configNewFile, err := os.Open(newConfigPath); err == nil {
defer configNewFile.Close()
configNewInfo, _ := configNewFile.Stat()
if assetInfo.ModTime().Unix() > configNewInfo.ModTime().Unix() {
// The config asset is newer than the config.yaml.new file.
// Write a new config.yaml.new file.
ioutil.WriteFile(os.ExpandEnv(newConfigPath), asset, 0644)
logrus.WithFields(logrus.Fields{
"file_path": newConfigPath,
}).Infoln("An updated default configuration file has been written.")
}
} else if assetInfo.ModTime().Unix() > configInfo.ModTime().Unix() {
// The config asset is newer than the existing config file.
// Write a config.yaml.new file.
ioutil.WriteFile(os.ExpandEnv(newConfigPath), asset, 0644)
logrus.WithFields(logrus.Fields{
"file_path": newConfigPath,
}).Infoln("An updated default configuration file has been written.")
}
}
2014-12-10 00:41:50 +01:00
}
}