2019-08-06 14:05:15 -05:00
|
|
|
package schalter
|
|
|
|
|
|
|
|
import (
|
2019-08-06 17:18:09 -05:00
|
|
|
"fmt"
|
2019-08-06 17:08:11 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/worker"
|
2019-08-06 14:05:15 -05:00
|
|
|
"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"`
|
|
|
|
|
2019-08-06 17:08:11 -05:00
|
|
|
Interval time.Duration `toml:"interval"`
|
|
|
|
|
2019-08-07 05:27:18 -05:00
|
|
|
ChangeEvent []func(bool)
|
|
|
|
|
2019-08-06 14:05:15 -05:00
|
|
|
state bool
|
|
|
|
spaceName string
|
2019-08-06 17:08:11 -05:00
|
|
|
worker *worker.Worker
|
2019-08-06 14:05:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Schalter) updatePresence(c xmpp.Sender) {
|
|
|
|
pres := stanza.Presence{
|
2019-08-06 17:45:24 -05:00
|
|
|
Show: stanza.PresenceShowXA,
|
2019-08-06 14:05:15 -05:00
|
|
|
Status: s.stateString(),
|
|
|
|
}
|
|
|
|
if s.state {
|
2019-08-06 17:45:24 -05:00
|
|
|
pres.Show = stanza.PresenceShowChat
|
2019-08-06 14:05:15 -05:00
|
|
|
}
|
|
|
|
c.Send(pres)
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:08:11 -05:00
|
|
|
func (s *Schalter) Start(c xmpp.Sender) {
|
2019-08-06 14:05:15 -05:00
|
|
|
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())
|
2019-08-06 17:08:11 -05:00
|
|
|
|
|
|
|
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() {
|
2019-08-07 05:27:18 -05:00
|
|
|
for _, f := range s.ChangeEvent {
|
|
|
|
f(s.state)
|
2019-08-06 17:08:11 -05:00
|
|
|
}
|
2019-08-07 05:27:18 -05:00
|
|
|
|
|
|
|
log.Infof("worker detect changes of status: %s", s.stateString())
|
2019-08-06 17:08:11 -05:00
|
|
|
} else {
|
|
|
|
log.Debug("worker run, but no changes detected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 05:27:18 -05:00
|
|
|
func (s *Schalter) XMPPChangeEvent(c xmpp.Sender) func(bool) {
|
|
|
|
return func(state bool) {
|
|
|
|
s.updatePresence(c)
|
|
|
|
text := fmt.Sprintf("%s changed to closed", s.spaceName)
|
|
|
|
if state {
|
|
|
|
text = fmt.Sprintf("%s changed to open", s.spaceName)
|
|
|
|
}
|
|
|
|
runtime.SendText(c, s.Users, s.MUCs, text, text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:08:11 -05:00
|
|
|
func (s *Schalter) Close() {
|
|
|
|
if s.worker != nil {
|
|
|
|
s.worker.Close()
|
|
|
|
}
|
2019-08-06 14:05:15 -05:00
|
|
|
}
|