-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxmpp.go
65 lines (49 loc) · 1.14 KB
/
xmpp.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package xmpp
import (
"os"
"regexp"
)
type Message struct {
mtype, raw, to, from, body string
}
func NewMessage(raw string) *Message {
var err os.Error
msg := new(Message)
msg.raw = raw
if msg.mtype, err = parse(raw, "type=\"([^\"]+)\""); err != nil {
return nil // failed to parse what type it is
}
if msg.to, err = parse(raw, "to=\"([^\"]+)\""); err != nil {
return nil // failed to parse to
}
if msg.from, err = parse(raw, "from=\"([^\"]+)\""); err != nil {
return nil // failed to parse from
}
if msg.body, err = parse(raw, "<body>(.*)</body>"); err != nil {
return nil // failed to parse body
}
return msg
}
func (m *Message) Raw() string {
return m.raw
}
func (m *Message) Type() string {
return m.mtype
}
func (m *Message) From() (from string) {
return m.from
}
func (m *Message) To() string {
return m.to
}
func (m *Message) Body() string {
return m.body
}
func parse(raw, regex string) (out string, err os.Error) {
if match := regexp.MustCompile(regex).FindStringSubmatch(raw); len(match) > 0 {
out = match[1]
} else {
err = os.NewError("Failed to parse message")
}
return
}