From 4640fc8406154525d2fb8b4047706106355b1950 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Wed, 7 Aug 2019 00:08:11 +0200 Subject: [PATCH] first finale version --- README.md | 28 ++++++++++++++-------------- config.toml | 2 +- main.go | 4 +++- schalter/main.go | 37 ++++++++++++++++++++++++++++++++++++- schalter/spaceapi.go | 35 +++++++++++++++++++++++++++++++++++ schalter/state.go | 29 +++++++++++++++++++++++++++-- 6 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 schalter/spaceapi.go diff --git a/README.md b/README.md index cd1197f..3fa1019 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,26 @@ -# hook2xmpp +# ccchatbot -[![pipeline status](https://dev.sum7.eu/genofire/hook2xmpp/badges/master/pipeline.svg)](https://dev.sum7.eu/genofire/hook2xmpp/pipelines) -[![coverage report](https://dev.sum7.eu/genofire/hook2xmpp/badges/master/coverage.svg)](https://dev.sum7.eu/genofire/hook2xmpp/pipelines) -[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/hook2xmpp)](https://goreportcard.com/report/dev.sum7.eu/genofire/hook2xmpp) -[![GoDoc](https://godoc.org/dev.sum7.eu/genofire/hook2xmpp?status.svg)](https://godoc.org/dev.sum7.eu/genofire/hook2xmpp) +[![pipeline status](https://dev.sum7.eu/ccchb/ccchatbot/badges/master/pipeline.svg)](https://dev.sum7.eu/ccchb/ccchatbot/pipelines) +[![coverage report](https://dev.sum7.eu/ccchb/ccchatbot/badges/master/coverage.svg)](https://dev.sum7.eu/ccchb/ccchatbot/pipelines) +[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/ccchb/ccchatbot)](https://goreportcard.com/report/dev.sum7.eu/ccchb/ccchatbot) +[![GoDoc](https://godoc.org/dev.sum7.eu/ccchb/ccchatbot?status.svg)](https://godoc.org/dev.sum7.eu/ccchb/ccchatbot) -## Get hook2xmpp +## Get ccchatbot #### Download Latest Build binary from ci here: -[Download All](https://dev.sum7.eu/genofire/hook2xmpp/-/jobs/artifacts/master/download/?job=build-my-project) (with config example) +[Download All](https://dev.sum7.eu/ccchb/ccchatbot/-/jobs/artifacts/master/download/?job=build-my-project) (with config example) -[Download Binary](https://dev.sum7.eu/genofire/hook2xmpp/-/jobs/artifacts/master/raw/bin/hook2xmpp?inline=false&job=build-my-project) +[Download Binary](https://dev.sum7.eu/ccchb/ccchatbot/-/jobs/artifacts/master/raw/bin/ccchatbot?inline=false&job=build-my-project) #### Build ```bash -go get -u dev.sum7.eu/genofire/hook2xmpp +go get -u dev.sum7.eu/ccchb/ccchatbot ``` ## Configure @@ -29,10 +29,10 @@ see `config_example.toml` ## Start / Boot -_/lib/systemd/system/hook2xmpp.service_ : +_/lib/systemd/system/ccchatbot.service_ : ``` [Unit] -Description=hook2xmpp +Description=ccchatbot After=network.target # After=ejabberd.service # After=prosody.service @@ -40,7 +40,7 @@ After=network.target [Service] Type=simple # User=notRoot -ExecStart=/opt/go/bin/hook2xmpp --config /etc/hook2xmpp.conf +ExecStart=/opt/go/bin/ccchatbot --config /etc/ccchatbot.conf Restart=always RestartSec=5sec @@ -48,5 +48,5 @@ RestartSec=5sec WantedBy=multi-user.target ``` -Start: `systemctl start hook2xmpp` -Autostart: `systemctl enable hook2xmpp` +Start: `systemctl start ccchatbot` +Autostart: `systemctl enable ccchatbot` diff --git a/config.toml b/config.toml index 1825865..21f37bb 100644 --- a/config.toml +++ b/config.toml @@ -7,5 +7,5 @@ password = "test" [schalter] url = "https://schalter.ccchb.de/spaceapi.json" - +interval = 5000000000 mucs = ["ffhb_events@conference.chat.sum7.eu","#ccchb@irc.hackint.org"] diff --git a/main.go b/main.go index 6f3fdbf..9e5fe1f 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { } cm := xmpp.NewStreamManager(client, func(s xmpp.Sender) { - config.Schalter.Run(s) + config.Schalter.Start(s) }) go func() { err := cm.Run() @@ -64,6 +64,8 @@ func main() { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) sig := <-sigs + config.Schalter.Close() + runtime.LeaveAllMUCs(client) log.Infof("closed by receiving: %s", sig) diff --git a/schalter/main.go b/schalter/main.go index 4fd947d..7029bbb 100644 --- a/schalter/main.go +++ b/schalter/main.go @@ -1,6 +1,9 @@ package schalter import ( + "time" + + "dev.sum7.eu/genofire/golang-lib/worker" "github.com/bdlm/log" "gosrc.io/xmpp" "gosrc.io/xmpp/stanza" @@ -17,8 +20,11 @@ type Schalter struct { 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) { @@ -32,7 +38,7 @@ func (s *Schalter) updatePresence(c xmpp.Sender) { c.Send(pres) } -func (s *Schalter) Run(c xmpp.Sender) { +func (s *Schalter) Start(c xmpp.Sender) { if s.Nickname == "" { s.Nickname = "ccchalter" } @@ -42,4 +48,33 @@ func (s *Schalter) Run(c xmpp.Sender) { 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() + } } diff --git a/schalter/spaceapi.go b/schalter/spaceapi.go new file mode 100644 index 0000000..963c212 --- /dev/null +++ b/schalter/spaceapi.go @@ -0,0 +1,35 @@ +package schalter + +type SpaceAPI struct { + API string `json:"api"` + Space string `json:"space"` + Logo string `json:"logo"` + URL string `json:"url"` + Location struct { + Address string `json:"address,omitempty"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + } `json:"location"` + SpaceFed struct { + SpaceNet bool `json:"spacenet"` + Spacesaml bool `json:"spacesaml"` + SpacePhone bool `json:"spacephone"` + } `json:"spacefed,omitempty"` + Cam []string `json:"cam,omitempty"` + Stream struct { + M4 bool `json:"m4,omitempty"` + MJPEG bool `json:"mjpeg,omitempty"` + UStream bool `json:"ustream,omitempty"` + } `json:"stream,omitempty"` + State struct { + Open bool `json:"open"` + Lastchange float64 `json:"lastchange,omitempty"` + TriggerPerson string `json:"trigger_person,omitempty"` + Message string `json:"message,omitempty"` + Icon struct { + Open string `json:"open"` + Closed string `json:"closed"` + } `json:"icon,omitempty"` + } `json:"state"` + // .. +} diff --git a/schalter/state.go b/schalter/state.go index c8d79ce..5e4ccce 100644 --- a/schalter/state.go +++ b/schalter/state.go @@ -1,6 +1,12 @@ package schalter -import "fmt" +import ( + "fmt" + + "dev.sum7.eu/genofire/golang-lib/file" + "dev.sum7.eu/genofire/golang-lib/http" + "github.com/bdlm/log" +) func (s *Schalter) stateString() string { if s.state { @@ -9,5 +15,24 @@ func (s *Schalter) stateString() string { return fmt.Sprintf("%s is closed", s.spaceName) } func (s *Schalter) fetchState() bool { - return false + if s.Path != "" && s.URL != "" { + log.Panic("it is not possible to set path and url to ") + } + api := SpaceAPI{} + if s.Path != "" { + if err := file.ReadJSON(s.Path, &api); err != nil { + log.WithField("path", s.Path).Errorf("unable to read spaceapi from file: %s", err) + return false + } + } else { + if err := http.JSONRequest(s.URL, &api); err != nil { + log.WithField("url", s.URL).Errorf("unable to http request spaceapi: %s", err) + return false + } + } + + changed := api.State.Open != s.state + s.state = api.State.Open + s.spaceName = api.Space + return changed }