2021-09-09 23:12:29 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2021-09-11 17:16:38 +02:00
|
|
|
"io"
|
2021-09-12 00:31:03 +02:00
|
|
|
"net"
|
2021-09-09 23:12:29 +02:00
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"mellium.im/xmlstream"
|
|
|
|
"mellium.im/xmpp"
|
2021-09-12 00:31:03 +02:00
|
|
|
"mellium.im/xmpp/component"
|
2021-09-09 23:12:29 +02:00
|
|
|
"mellium.im/xmpp/jid"
|
2021-09-11 17:16:38 +02:00
|
|
|
"mellium.im/xmpp/mux"
|
2021-09-09 23:12:29 +02:00
|
|
|
"mellium.im/xmpp/stanza"
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/unified-push-xmpp/messages"
|
2021-09-09 23:12:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type XMPPService struct {
|
2021-09-12 00:31:03 +02:00
|
|
|
Addr string `toml:"address"`
|
|
|
|
JID string `toml:"jid"`
|
|
|
|
Secret string `toml:"secret"`
|
2021-09-12 12:14:09 +02:00
|
|
|
// hidden here for beautiful config file
|
|
|
|
EndpointURL string `toml:"-"`
|
2021-09-11 17:16:38 +02:00
|
|
|
session *xmpp.Session
|
2021-09-09 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 17:16:38 +02:00
|
|
|
func (s *XMPPService) Run() error {
|
|
|
|
var err error
|
2021-09-12 00:31:03 +02:00
|
|
|
j := jid.MustParse(s.JID)
|
|
|
|
ctx := context.TODO()
|
|
|
|
conn, err := net.Dial("tcp", s.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.session, err = component.NewSession(
|
|
|
|
ctx, j.Domain(),
|
|
|
|
[]byte(s.Secret), conn,
|
2021-09-11 17:16:38 +02:00
|
|
|
); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
log.Info("Closing session…")
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := s.session.Close(); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Errorf("Error closing session: %q", err)
|
|
|
|
}
|
|
|
|
log.Println("Closing conn…")
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := s.session.Conn().Close(); err != nil {
|
2021-09-09 23:12:29 +02:00
|
|
|
log.Errorf("Error closing connection: %q", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-12 00:31:03 +02:00
|
|
|
/* Send initial presence to let the server know we want to receive messages.
|
2021-09-11 17:16:38 +02:00
|
|
|
err = s.session.Send(context.TODO(), stanza.Presence{Type: stanza.AvailablePresence}.Wrap(nil))
|
2021-09-09 23:12:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-12 00:31:03 +02:00
|
|
|
}*/
|
2021-09-11 17:16:38 +02:00
|
|
|
log.Infof("connected with %s", s.session.LocalAddr())
|
2021-09-12 00:31:03 +02:00
|
|
|
return s.session.Serve(mux.New(
|
2021-09-11 17:16:38 +02:00
|
|
|
// register - get + set
|
|
|
|
mux.IQFunc(stanza.SetIQ, xml.Name{Local: messages.LocalRegister, Space: messages.Space}, s.handleRegister),
|
|
|
|
mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalRegister, Space: messages.Space}, s.handleRegister),
|
|
|
|
// unregister - get + set
|
|
|
|
mux.IQFunc(stanza.SetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister),
|
|
|
|
mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister),
|
2021-09-12 00:31:03 +02:00
|
|
|
// mux.IQFunc("", xml.Name{}, s.handleDisco),
|
2021-09-11 17:16:38 +02:00
|
|
|
))
|
2021-09-12 00:31:03 +02:00
|
|
|
/* -
|
|
|
|
return s.session.Serve(xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
|
|
|
log.Info(start)
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
*/
|
2021-09-09 23:12:29 +02:00
|
|
|
}
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
|
|
|
reply := messages.RegisterIQ{
|
|
|
|
IQ: stanza.IQ{
|
|
|
|
ID: iq.ID,
|
|
|
|
Type: stanza.ErrorIQ,
|
2021-09-12 00:31:03 +02:00
|
|
|
From: iq.To,
|
2021-09-11 17:16:38 +02:00
|
|
|
To: iq.From,
|
|
|
|
},
|
|
|
|
}
|
2021-09-12 00:31:03 +02:00
|
|
|
defer func() {
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := t.Encode(reply); err != nil {
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Errorf("sending register response: %v", err)
|
2021-09-11 17:16:38 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.Infof("recieved iq: %v", iq)
|
|
|
|
|
|
|
|
tokenData := messages.TokenData{}
|
|
|
|
err := xml.NewTokenDecoder(t).Decode(&tokenData)
|
|
|
|
if err != nil && err != io.EOF {
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Errorf("decoding message: %q", err)
|
|
|
|
reply.Register.Error = &messages.ErrorData{Body: "unable decode"}
|
2021-09-11 17:16:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
token := tokenData.Body
|
|
|
|
if token == "" {
|
|
|
|
log.Errorf("no token found: %v", token)
|
2021-09-12 00:31:03 +02:00
|
|
|
reply.Register.Error = &messages.ErrorData{Body: "no token"}
|
2021-09-11 17:16:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-12 12:14:09 +02:00
|
|
|
endpoint := s.EndpointURL+"/UP?token=" + token + "&to=" + iq.From.String()
|
2021-09-11 17:16:38 +02:00
|
|
|
reply.IQ.Type = stanza.ResultIQ
|
2021-09-12 00:31:03 +02:00
|
|
|
reply.Register.Endpoint = &messages.EndpointData{Body: endpoint}
|
2021-09-11 17:16:38 +02:00
|
|
|
log.Infof("generate respone: %v", endpoint)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
|
|
|
reply := messages.UnregisterIQ{
|
|
|
|
IQ: stanza.IQ{
|
|
|
|
ID: iq.ID,
|
|
|
|
Type: stanza.ErrorIQ,
|
2021-09-12 00:31:03 +02:00
|
|
|
From: iq.To,
|
2021-09-11 17:16:38 +02:00
|
|
|
To: iq.From,
|
|
|
|
},
|
|
|
|
}
|
2021-09-12 00:31:03 +02:00
|
|
|
defer func() {
|
2021-09-11 17:16:38 +02:00
|
|
|
if err := t.Encode(reply); err != nil {
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Errorf("sending unregister response: %v", err)
|
2021-09-11 17:16:38 +02:00
|
|
|
}
|
|
|
|
}()
|
2021-09-12 00:31:03 +02:00
|
|
|
log.Infof("unregistered unhandled: %v", start)
|
2021-09-11 17:16:38 +02:00
|
|
|
|
|
|
|
reply.Unregister.Error = "not implemented"
|
2021-09-09 23:12:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-11 17:16:38 +02:00
|
|
|
|
2021-09-12 00:31:03 +02:00
|
|
|
func (s *XMPPService) handleDisco(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
|
|
|
|
reply := stanza.IQ{
|
|
|
|
ID: iq.ID,
|
|
|
|
Type: stanza.ErrorIQ,
|
|
|
|
From: iq.To,
|
|
|
|
To: iq.From,
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := t.Encode(reply); err != nil {
|
|
|
|
log.Errorf("sending response: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.Infof("recieved iq: %v", iq)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-11 17:16:38 +02:00
|
|
|
// SendMessage of an UP Notification
|
|
|
|
func (s *XMPPService) SendMessage(to, token, content string) error {
|
2021-09-12 12:14:09 +02:00
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"to": to,
|
|
|
|
"token": token,
|
|
|
|
}).Info("forward message to xmpp")
|
2021-09-11 17:16:38 +02:00
|
|
|
return s.session.Encode(context.TODO(), messages.Message{
|
|
|
|
Message: stanza.Message{
|
2021-09-12 00:31:03 +02:00
|
|
|
To: jid.MustParse(to),
|
|
|
|
From: jid.MustParse(s.JID),
|
2021-09-11 17:16:38 +02:00
|
|
|
// Type: stanza.ChatMessage,
|
|
|
|
Type: stanza.NormalMessage,
|
|
|
|
},
|
|
|
|
Token: token,
|
|
|
|
Body: content,
|
|
|
|
})
|
|
|
|
}
|