summaryrefslogtreecommitdiff
path: root/xmpp/message.go
blob: e1ff8b6ec7a642474ccb7d1e990b8f55b422607c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)
}