summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-07-04 22:51:10 +0200
committerxengineering <me@xengineering.eu>2023-07-05 21:24:01 +0200
commitce7814012009ba8f5b72d49e2ec96d5594be895e (patch)
tree6824883f39818e88420f903e3f31cf4da437684d
parent107f97f2cd3ff8dbbdb39269e622462c48b0d41c (diff)
downloadlimox-ce7814012009ba8f5b72d49e2ec96d5594be895e.tar
limox-ce7814012009ba8f5b72d49e2ec96d5594be895e.tar.zst
limox-ce7814012009ba8f5b72d49e2ec96d5594be895e.zip
Implement echo bot
This demonstrates the full one-to-one chat message backend without the need to implement the GUI.
-rw-r--r--xmpp/message.go38
1 files 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)
+}