71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"syscall"
|
||
|
|
||
|
"dev.sum7.eu/genofire/golang-lib/file"
|
||
|
"github.com/bdlm/log"
|
||
|
"gosrc.io/xmpp"
|
||
|
"gosrc.io/xmpp/stanza"
|
||
|
|
||
|
"dev.sum7.eu/ccchb/ccchatbot/runtime"
|
||
|
)
|
||
|
|
||
|
var config = Config{}
|
||
|
|
||
|
func main() {
|
||
|
configFile := "config.toml"
|
||
|
flag.StringVar(&configFile, "config", configFile, "path of configuration file")
|
||
|
flag.Parse()
|
||
|
|
||
|
if err := file.ReadTOML(configFile, &config); err != nil {
|
||
|
log.WithField("tip", "maybe call me with: ccchatbot --config /etc/ccchatbot.conf").Panicf("error on read config: %s", err)
|
||
|
}
|
||
|
|
||
|
log.SetLevel(config.LogLevel)
|
||
|
|
||
|
router := xmpp.NewRouter()
|
||
|
router.HandleFunc("presence", handlePresence)
|
||
|
router.HandleFunc("message", func(s xmpp.Sender, p stanza.Packet) {
|
||
|
msg, ok := p.(stanza.Message)
|
||
|
if !ok {
|
||
|
log.Errorf("ignoring wrong routed packet: %T", p)
|
||
|
return
|
||
|
}
|
||
|
if err := config.Schalter.HandleBotMessage(s, msg); err != nil {
|
||
|
log.Debugf("bot could not handle message: %s", err)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
var err error
|
||
|
client, err := xmpp.NewClient(xmpp.Config{
|
||
|
Address: config.XMPP.Host,
|
||
|
Jid: config.XMPP.JID,
|
||
|
Password: config.XMPP.Password,
|
||
|
}, router)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Panicf("error on startup xmpp client: %s", err)
|
||
|
}
|
||
|
|
||
|
cm := xmpp.NewStreamManager(client, func(s xmpp.Sender) {
|
||
|
config.Schalter.Run(s)
|
||
|
})
|
||
|
go func() {
|
||
|
err := cm.Run()
|
||
|
log.Panic("closed connection:", err)
|
||
|
}()
|
||
|
|
||
|
// Wait for system signal
|
||
|
sigs := make(chan os.Signal, 1)
|
||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||
|
sig := <-sigs
|
||
|
|
||
|
runtime.LeaveAllMUCs(client)
|
||
|
|
||
|
log.Infof("closed by receiving: %s", sig)
|
||
|
}
|