diff options
author | xengineering <me@xengineering.eu> | 2023-07-05 21:26:56 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-05 21:26:56 +0200 |
commit | d614346ae08e5384e1dca7306ba64fbdc9931d2e (patch) | |
tree | 8730a5d66eee402e2c552b904060ed7b495b07c4 | |
parent | 48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (diff) | |
parent | 4dc3723f68c390ed05ddeff13f2378da7ebf2b8f (diff) | |
download | limox-d614346ae08e5384e1dca7306ba64fbdc9931d2e.tar limox-d614346ae08e5384e1dca7306ba64fbdc9931d2e.tar.zst limox-d614346ae08e5384e1dca7306ba64fbdc9931d2e.zip |
Merge branch 'msg-backend'
This adds the XMPP functionality to send and receive XMPP text messages
in one-to-one chats.
-rw-r--r-- | xmpp/message.go | 45 | ||||
-rw-r--r-- | xmpp/routing.go | 4 |
2 files changed, 49 insertions, 0 deletions
diff --git a/xmpp/message.go b/xmpp/message.go new file mode 100644 index 0000000..e1ff8b6 --- /dev/null +++ b/xmpp/message.go @@ -0,0 +1,45 @@ +package xmpp + +import ( + "encoding/xml" + "fmt" + "log" + "math/rand" +) + +type message struct { + XMLName xml.Name `xml:"jabber:client message"` + Type string `xml:"type,attr,omitempty"` + Id string `xml:"id,attr,omitempty"` + From string `xml:"from,attr,omitempty"` + To string `xml:"to,attr,omitempty"` + // FIXME The lang attribute should have the `xml` prefix for the standard + // XML namespace. There was no option found so far which allows this with + // the standard library XML implementation and the xml.Encoder.Encode(v + // any) function. + Lang string `xml:"lang,attr,omitempty"` + Body string `xml:"body,omitempty"` +} + +func handleMessage(s *session, m message) { + if m.Type == "chat" && m.Body != "" { + reply := fmt.Sprintf("Got %s", m.Body) + err := s.sendMessage(reply, m.From) + if err != nil { + log.Printf("Could not send message: %v\n", err) + } + } +} + +func (s *session) sendMessage(m, j string) error { + msg := message{ + From: s.jid, + Id: fmt.Sprintf("%016x", rand.Uint64()), + To: j, + Type: "chat", + Lang: "en", + Body: m, + } + + return s.tx.Encode(msg) +} diff --git a/xmpp/routing.go b/xmpp/routing.go index 5cd2040..2f2347a 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -13,6 +13,8 @@ func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) { parse(saslSuccess{}, s, d, c) case xml.Name{`jabber:client`, `iq`}: parse(iqResponse{}, s, d, c) + case xml.Name{`jabber:client`, `message`}: + parse(message{}, s, d, c) default: d.Skip() } @@ -35,6 +37,8 @@ func handle(s *session, element any) { handleSaslSuccess(s) case iqResponse: handleIqResponse(s, t) + case message: + handleMessage(s, t) default: log.Printf("Unknown parsed element: %v", t) } |