From 107f97f2cd3ff8dbbdb39269e622462c48b0d41c Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 22:30:44 +0200 Subject: Implement (chat) message receiving backend This sets a handler and the routing up to be able to log chat messages with body and sender reference. This can later be used for the GUI. --- xmpp/message.go | 17 +++++++++++++++++ xmpp/routing.go | 4 ++++ 2 files changed, 21 insertions(+) create mode 100644 xmpp/message.go diff --git a/xmpp/message.go b/xmpp/message.go new file mode 100644 index 0000000..58dc657 --- /dev/null +++ b/xmpp/message.go @@ -0,0 +1,17 @@ +package xmpp + +import ( + "log" +) + +type message struct { + Type string `xml:"type,attr"` + From string `xml:"from,attr"` + Body string `xml:"body"` +} + +func handleMessage(s *session, m message) { + if m.Type == "chat" { + log.Printf("Got message '%s' from '%s'.\n", m.Body, m.From) + } +} 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) } -- cgit v1.2.3-70-g09d2 From ce7814012009ba8f5b72d49e2ec96d5594be895e Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 22:51:10 +0200 Subject: Implement echo bot This demonstrates the full one-to-one chat message backend without the need to implement the GUI. --- xmpp/message.go | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/xmpp/message.go b/xmpp/message.go index 58dc657..ba60390 100644 --- a/xmpp/message.go +++ b/xmpp/message.go @@ -2,16 +2,44 @@ package xmpp import ( "log" + "encoding/xml" + "math/rand" + "fmt" ) type message struct { - Type string `xml:"type,attr"` - From string `xml:"from,attr"` - Body string `xml:"body"` + 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" { - log.Printf("Got message '%s' from '%s'.\n", m.Body, m.From) + 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) +} -- cgit v1.2.3-70-g09d2 From 4dc3723f68c390ed05ddeff13f2378da7ebf2b8f Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 5 Jul 2023 21:26:12 +0200 Subject: Apply go fmt --- xmpp/message.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/xmpp/message.go b/xmpp/message.go index ba60390..e1ff8b6 100644 --- a/xmpp/message.go +++ b/xmpp/message.go @@ -1,24 +1,24 @@ package xmpp import ( - "log" "encoding/xml" - "math/rand" "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"` + 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"` + Lang string `xml:"lang,attr,omitempty"` + Body string `xml:"body,omitempty"` } func handleMessage(s *session, m message) { @@ -31,11 +31,11 @@ func handleMessage(s *session, m message) { } } -func (s *session) sendMessage(m,j string) error { +func (s *session) sendMessage(m, j string) error { msg := message{ From: s.jid, - Id: fmt.Sprintf("%016x", rand.Uint64()), - To: j, + Id: fmt.Sprintf("%016x", rand.Uint64()), + To: j, Type: "chat", Lang: "en", Body: m, -- cgit v1.2.3-70-g09d2