A simple server for serving our SpaceAPI interactively.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spaceapi/xmpp/xmpp.go

65 lines
1.3 KiB

package xmpp
import (
ctls "crypto/tls"
"fmt"
"log"
"strings"
"git.hacknology.de/projekte/spaceapi"
"github.com/coyim/coyim/xmpp"
"github.com/coyim/coyim/xmpp/data"
)
type nullVerifier struct{}
func (nullVerifier) Verify(state ctls.ConnectionState, conf *ctls.Config, originDomain string) error {
return nil
}
func AddXMPPListener(storage *spaceapi.Storage, jid, password, target, handle string) error {
dialer := xmpp.DialerFactory(nullVerifier{})
dialer.SetJID(jid)
dialer.SetPassword(password)
conn, err := dialer.Dial()
if err != nil {
return fmt.Errorf("error dialing server: %s", err)
}
targetTokens := strings.SplitN(target, "@", 2)
if len(targetTokens) != 2 {
return fmt.Errorf("not a valid room JID: %s", target)
}
chat := conn.GetChatContext()
room := data.Room{
ID: targetTokens[0],
Service: targetTokens[1],
}
occupant := &data.Occupant{
Room: room,
Handle: handle,
}
if err := chat.EnterRoom(occupant); err != nil {
return fmt.Errorf("error joining room: %s", err)
}
storage.AddListener(func(status spaceapi.SpaceStatus) {
if status.State.Open == nil {
return
}
msg := "Space is now OPEN!"
if !*status.State.Open {
msg = "Space is now CLOSED!"
}
if err := chat.SendChatMessage(msg, &room); err != nil {
log.Printf("Error sending message: %s", err)
}
})
return nil
}