Close space using XMPP message.

change-name
Robert Jacob 5 years ago
parent f262dc74c2
commit b5ce5395d8
  1. 51
      xmpp/xmpp.go

@ -11,6 +11,12 @@ import (
"github.com/mattn/go-xmpp"
)
const (
commandOpen = "!open"
commandClose = "!closed"
openNotPossible = "Bist du auch wirklich da? Ich traue dir nicht."
)
func AddXMPPListener(storage *spaceapi.Storage, jid, password, target, handle string) error {
xmppHost, err := lookupHost(jid)
if err != nil {
@ -39,6 +45,31 @@ func AddXMPPListener(storage *spaceapi.Storage, jid, password, target, handle st
return fmt.Errorf("can not join room: %s", err)
}
go func() {
for {
chat, err := client.Recv()
if err != nil {
log.Printf("Error receiving XMPP message: %s", err)
continue
}
switch v := chat.(type) {
case xmpp.Chat:
switch v.Text {
case commandOpen:
if err := sendGroupMessage(client, target, openNotPossible); err != nil {
log.Printf("Error sending message: %s", err)
}
case commandClose:
open := false
storage.Modify(func(status *spaceapi.SpaceStatus) {
status.State.Open = &open
})
}
}
}
}()
storage.AddListener(func(status spaceapi.SpaceStatus) {
if status.State.Open == nil {
return
@ -49,11 +80,7 @@ func AddXMPPListener(storage *spaceapi.Storage, jid, password, target, handle st
msg = "Space is now CLOSED!"
}
if _, err := client.Send(xmpp.Chat{
Remote: target,
Type: "groupchat",
Text: msg,
}); err != nil {
if err := sendGroupMessage(client, target, msg); err != nil {
log.Printf("Error sending status update: %s", err)
}
})
@ -81,3 +108,17 @@ func lookupHost(jid string) (string, error) {
return fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port), nil
}
func sendGroupMessage(client *xmpp.Client, target, msg string) error {
chat := xmpp.Chat{
Remote: target,
Type: "groupchat",
Text: msg,
}
if _, err := client.Send(chat); err != nil {
return err
}
return nil
}

Loading…
Cancel
Save