2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2012-08-17 20:38:15 +00:00
|
|
|
package main
|
|
|
|
|
2024-02-27 15:11:01 +00:00
|
|
|
//go:generate templ generate
|
|
|
|
|
2012-08-17 20:38:15 +00:00
|
|
|
import (
|
2013-05-09 22:30:15 +00:00
|
|
|
"flag"
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-03-07 16:53:49 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/connectors/discord"
|
2024-05-07 01:28:48 +00:00
|
|
|
"github.com/velour/catbase/plugins"
|
|
|
|
"io"
|
|
|
|
"os"
|
2019-03-07 16:35:42 +00:00
|
|
|
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
2019-02-06 03:52:49 +00:00
|
|
|
"github.com/velour/catbase/connectors/irc"
|
2013-06-02 01:59:55 +00:00
|
|
|
)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2019-01-21 19:24:03 +00:00
|
|
|
var (
|
2019-03-07 16:35:42 +00:00
|
|
|
key = flag.String("set", "", "Configuration key to set")
|
|
|
|
val = flag.String("val", "", "Configuration value to set")
|
2021-07-21 18:52:45 +00:00
|
|
|
initDB = flag.Bool("init", false, "Initialize the configuration db")
|
2019-03-07 16:35:42 +00:00
|
|
|
prettyLog = flag.Bool("pretty", false, "Use pretty console logger")
|
|
|
|
debug = flag.Bool("debug", false, "Turn on debug logging")
|
2024-05-07 01:28:48 +00:00
|
|
|
dbpath = flag.String("db", "catbase.db", "Database file to load. (Defaults to catbase.db)")
|
|
|
|
kvSpace = flag.String("kv", "catbase", "Namespace for the charm store")
|
2019-01-21 19:24:03 +00:00
|
|
|
)
|
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
func main() {
|
2012-08-17 20:38:15 +00:00
|
|
|
flag.Parse() // parses the logging flags.
|
|
|
|
|
2021-05-04 17:57:57 +00:00
|
|
|
var output io.Writer = os.Stdout
|
2019-03-07 16:35:42 +00:00
|
|
|
if *prettyLog {
|
2021-05-04 17:57:57 +00:00
|
|
|
output = zerolog.ConsoleWriter{Out: output}
|
2019-03-07 16:35:42 +00:00
|
|
|
}
|
2021-05-04 17:57:57 +00:00
|
|
|
log.Logger = log.Output(output).With().Caller().Stack().Logger()
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
if *debug {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2024-05-07 01:28:48 +00:00
|
|
|
c := config.ReadConfig(*dbpath, *kvSpace)
|
2019-01-21 19:24:03 +00:00
|
|
|
|
|
|
|
if *key != "" && *val != "" {
|
|
|
|
c.Set(*key, *val)
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Info().Msgf("Set config %s: %s", *key, *val)
|
2019-01-21 19:24:03 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-22 00:16:57 +00:00
|
|
|
if (*initDB && len(flag.Args()) != 2) || (!*initDB && c.GetInt("init", 0) != 1) {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Msgf(`You must run "catbase -init <channel> <nick>"`)
|
2019-01-21 19:24:03 +00:00
|
|
|
} else if *initDB {
|
|
|
|
c.SetDefaults(flag.Arg(0), flag.Arg(1))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:37:07 +00:00
|
|
|
var client bot.Connector
|
2013-06-02 01:59:55 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
switch c.Get("type", "slackapp") {
|
2016-03-10 18:37:07 +00:00
|
|
|
case "irc":
|
|
|
|
client = irc.New(c)
|
2020-08-31 16:20:52 +00:00
|
|
|
case "discord":
|
|
|
|
client = discord.New(c)
|
2016-03-10 18:37:07 +00:00
|
|
|
default:
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
|
2013-06-02 01:59:55 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 14:00:20 +00:00
|
|
|
b := bot.New(c, client)
|
2016-03-10 18:37:07 +00:00
|
|
|
|
2021-07-28 15:32:59 +00:00
|
|
|
if r, path := client.GetRouter(); r != nil {
|
2024-02-27 19:29:54 +00:00
|
|
|
b.GetWeb().RegisterWeb(r, path)
|
2021-07-28 15:32:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:53:49 +00:00
|
|
|
plugins.Register(b)
|
2013-01-23 00:22:32 +00:00
|
|
|
|
2019-02-07 19:45:59 +00:00
|
|
|
if err := client.Serve(); err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err)
|
2017-09-07 04:32:53 +00:00
|
|
|
}
|
2019-02-07 19:45:59 +00:00
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
log.Debug().Msgf("Sending bot.Startup message")
|
2021-02-14 21:57:22 +00:00
|
|
|
b.Receive(client, bot.Startup, msg.Message{})
|
|
|
|
|
2021-07-21 18:52:45 +00:00
|
|
|
b.ListenAndServe()
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|