diff options
Diffstat (limited to 'xmpp/message.go')
-rw-r--r-- | xmpp/message.go | 45 |
1 files changed, 45 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) +} |