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/mqtt" "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) var mqttService *mqtt.Service if config.MQTT != nil { mqttService = mqtt.Connect(config.MQTT) } 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 ok := config.Schalter.HandleBotMessage(s, msg); ok { return } if mqttService != nil { if ok := mqttService.HandleBotMessage(s, msg); ok { return } } log.Debugf("no bot has handle message of %s: %s", msg.From, msg.Body) }) var err error client, err := xmpp.NewClient(xmpp.Config{ Address: config.XMPP.Host, Jid: config.XMPP.JID, Password: config.XMPP.Password, }, router) config.Schalter.ChangeEvent = append(config.Schalter.ChangeEvent, config.Schalter.XMPPChangeEvent(client)) if mqttService != nil { config.Schalter.ChangeEvent = append(config.Schalter.ChangeEvent, mqttService.HandleSchalterStateChange) } if err != nil { log.Panicf("error on startup xmpp client: %s", err) } cm := xmpp.NewStreamManager(client, func(s xmpp.Sender) { config.Schalter.Start(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 if mqttService != nil { mqttService.Close() } config.Schalter.Close() runtime.LeaveAllMUCs(client) log.Infof("closed by receiving: %s", sig) }