first finale version

This commit is contained in:
Martin/Geno 2019-08-07 00:08:11 +02:00
parent 10f1fae475
commit 4640fc8406
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
6 changed files with 116 additions and 19 deletions

View File

@ -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) [![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/genofire/hook2xmpp/badges/master/coverage.svg)](https://dev.sum7.eu/genofire/hook2xmpp/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/genofire/hook2xmpp)](https://goreportcard.com/report/dev.sum7.eu/genofire/hook2xmpp) [![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/genofire/hook2xmpp?status.svg)](https://godoc.org/dev.sum7.eu/genofire/hook2xmpp) [![GoDoc](https://godoc.org/dev.sum7.eu/ccchb/ccchatbot?status.svg)](https://godoc.org/dev.sum7.eu/ccchb/ccchatbot)
## Get hook2xmpp ## Get ccchatbot
#### Download #### Download
Latest Build binary from ci here: 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 #### Build
```bash ```bash
go get -u dev.sum7.eu/genofire/hook2xmpp go get -u dev.sum7.eu/ccchb/ccchatbot
``` ```
## Configure ## Configure
@ -29,10 +29,10 @@ see `config_example.toml`
## Start / Boot ## Start / Boot
_/lib/systemd/system/hook2xmpp.service_ : _/lib/systemd/system/ccchatbot.service_ :
``` ```
[Unit] [Unit]
Description=hook2xmpp Description=ccchatbot
After=network.target After=network.target
# After=ejabberd.service # After=ejabberd.service
# After=prosody.service # After=prosody.service
@ -40,7 +40,7 @@ After=network.target
[Service] [Service]
Type=simple Type=simple
# User=notRoot # User=notRoot
ExecStart=/opt/go/bin/hook2xmpp --config /etc/hook2xmpp.conf ExecStart=/opt/go/bin/ccchatbot --config /etc/ccchatbot.conf
Restart=always Restart=always
RestartSec=5sec RestartSec=5sec
@ -48,5 +48,5 @@ RestartSec=5sec
WantedBy=multi-user.target WantedBy=multi-user.target
``` ```
Start: `systemctl start hook2xmpp` Start: `systemctl start ccchatbot`
Autostart: `systemctl enable hook2xmpp` Autostart: `systemctl enable ccchatbot`

View File

@ -7,5 +7,5 @@ password = "test"
[schalter] [schalter]
url = "https://schalter.ccchb.de/spaceapi.json" url = "https://schalter.ccchb.de/spaceapi.json"
interval = 5000000000
mucs = ["ffhb_events@conference.chat.sum7.eu","#ccchb@irc.hackint.org"] mucs = ["ffhb_events@conference.chat.sum7.eu","#ccchb@irc.hackint.org"]

View File

@ -52,7 +52,7 @@ func main() {
} }
cm := xmpp.NewStreamManager(client, func(s xmpp.Sender) { cm := xmpp.NewStreamManager(client, func(s xmpp.Sender) {
config.Schalter.Run(s) config.Schalter.Start(s)
}) })
go func() { go func() {
err := cm.Run() err := cm.Run()
@ -64,6 +64,8 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs sig := <-sigs
config.Schalter.Close()
runtime.LeaveAllMUCs(client) runtime.LeaveAllMUCs(client)
log.Infof("closed by receiving: %s", sig) log.Infof("closed by receiving: %s", sig)

View File

@ -1,6 +1,9 @@
package schalter package schalter
import ( import (
"time"
"dev.sum7.eu/genofire/golang-lib/worker"
"github.com/bdlm/log" "github.com/bdlm/log"
"gosrc.io/xmpp" "gosrc.io/xmpp"
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
@ -17,8 +20,11 @@ type Schalter struct {
Users []string `toml:"users"` Users []string `toml:"users"`
MUCs []string `toml:"mucs"` MUCs []string `toml:"mucs"`
Interval time.Duration `toml:"interval"`
state bool state bool
spaceName string spaceName string
worker *worker.Worker
} }
func (s *Schalter) updatePresence(c xmpp.Sender) { func (s *Schalter) updatePresence(c xmpp.Sender) {
@ -32,7 +38,7 @@ func (s *Schalter) updatePresence(c xmpp.Sender) {
c.Send(pres) c.Send(pres)
} }
func (s *Schalter) Run(c xmpp.Sender) { func (s *Schalter) Start(c xmpp.Sender) {
if s.Nickname == "" { if s.Nickname == "" {
s.Nickname = "ccchalter" s.Nickname = "ccchalter"
} }
@ -42,4 +48,33 @@ func (s *Schalter) Run(c xmpp.Sender) {
runtime.JoinMUC(c, m, s.Nickname) runtime.JoinMUC(c, m, s.Nickname)
} }
log.Infof("started schalter with state: %s", s.stateString()) 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()
}
} }

35
schalter/spaceapi.go Normal file
View File

@ -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"`
// ..
}

View File

@ -1,6 +1,12 @@
package schalter 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 { func (s *Schalter) stateString() string {
if s.state { if s.state {
@ -9,5 +15,24 @@ func (s *Schalter) stateString() string {
return fmt.Sprintf("%s is closed", s.spaceName) return fmt.Sprintf("%s is closed", s.spaceName)
} }
func (s *Schalter) fetchState() bool { 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
} }