ccchatbot/xmpp.go

59 lines
1.6 KiB
Go
Raw Normal View History

2019-08-06 13:31:58 -05:00
package main
import (
"github.com/bdlm/log"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
func handlePresence(s xmpp.Sender, p stanza.Packet) {
pres, ok := p.(stanza.Presence)
if !ok {
log.Errorf("blame gosrc.io/xmpp for routing: %s", p)
return
}
from, err := xmpp.NewJid(pres.From)
if err != nil {
log.Errorf("blame gosrc.io/xmpp for jid encoding: %s", pres.From)
return
}
fromBare := from.Bare()
logPres := log.WithField("from", from)
switch pres.Type {
case stanza.PresenceTypeSubscribe:
logPres.Debugf("recv presence subscribe")
if err := s.Send(stanza.Presence{Attrs: stanza.Attrs{
Type: stanza.PresenceTypeSubscribed,
To: fromBare,
Id: pres.Id,
}}); err != nil {
logPres.WithField("user", pres.From).Errorf("answer of subscribe not send: %s", err)
return
}
logPres.Debugf("accept new subscribe")
if err := s.Send(stanza.Presence{Attrs: stanza.Attrs{
Type: stanza.PresenceTypeSubscribe,
To: fromBare,
}}); err != nil {
logPres.WithField("user", pres.From).Errorf("request of subscribe not send: %s", err)
return
}
logPres.Info("request also subscribe")
case stanza.PresenceTypeSubscribed:
logPres.Info("recv presence accepted subscribe")
case stanza.PresenceTypeUnsubscribe:
logPres.Info("recv presence remove subscribe")
case stanza.PresenceTypeUnsubscribed:
logPres.Info("recv presence removed subscribe")
case stanza.PresenceTypeUnavailable:
logPres.Debug("recv presence unavailable")
case "":
logPres.Debug("recv empty presence, maybe from joining muc")
return
default:
logPres.Warnf("recv presence unsupported: %s -> %v", pres.Type, pres)
}
}