package schalter import ( "time" "dev.sum7.eu/genofire/golang-lib/worker" "github.com/bdlm/log" "gosrc.io/xmpp" "gosrc.io/xmpp/stanza" "dev.sum7.eu/ccchb/ccchatbot/runtime" ) type Schalter struct { URL string `toml:"url"` Path string `toml:"path"` Nickname string `toml:"nickname"` Users []string `toml:"users"` MUCs []string `toml:"mucs"` Interval time.Duration `toml:"interval"` state bool spaceName string worker *worker.Worker } func (s *Schalter) updatePresence(c xmpp.Sender) { pres := stanza.Presence{ Show: stanza.PresenceShowChat, Status: s.stateString(), } if s.state { pres.Show = stanza.PresenceShowXA } c.Send(pres) } func (s *Schalter) Start(c xmpp.Sender) { if s.Nickname == "" { s.Nickname = "ccchalter" } s.fetchState() s.updatePresence(c) for _, m := range s.MUCs { runtime.JoinMUC(c, m, s.Nickname) } log.Infof("started schalter with state: %s", s.stateString()) if s.worker == nil { s.worker = worker.NewWorker(s.Interval, s.run(c)) s.worker.Start() } else { log.Error("worker already running?") } } func (s *Schalter) run(c xmpp.Sender) func() { return func() { if s.fetchState() { s.updatePresence(c) text := fmt.Sprintf("%s changed to closed", s.state) if s.state { text = fmt.Sprintf("%s changed to open", s.state) } runtime.SendText(c, s.Users, s.MUCs, text, text) log.Infof("worker detect changes of status: %s", text) } else { log.Debug("worker run, but no changes detected") } } } func (s *Schalter) Close() { if s.worker != nil { s.worker.Close() } }