-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.go
82 lines (72 loc) · 1.54 KB
/
core.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/xml"
"fmt"
"net"
)
type Query struct {
XMLName xml.Name `xml:"query"`
Xmlns string `xml:"xmlns,attr"`
Username string `xml:"username"`
Password string `xml:"password"`
Digest string `xml:"digest"`
Resource string `xml:"resource"`
}
type Iq struct {
XMLName xml.Name `xml:"iq"`
Type string `xml:"type,attr"`
Id string `xml:"id,attr"`
Query Query `xml:"query"`
}
type Stream struct {
XMLName xml.Name `xml:"stream:stream"`
Iq Iq `xml:"iq"`
}
type LoginReq struct {
Iq Iq `xml:"iq"`
}
func Serve(addr string) {
ln, err := net.Listen("tcp", addr)
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
b := xml.NewDecoder(conn)
fmt.Fprintf(conn, `<?xml version='1.0'?>
<stream:stream
from='localhost'
id='abc123'
to='james@localhost'
version='1.0'
xml:lang='en'
xmlns='jabber:server'
xmlns:stream='http://etherx.jabber.org/streams'>`)
fmt.Fprintf(conn, "<stream:features/>")
for {
iqData := new(Iq)
b.Decode(iqData)
switch iqData.Type {
case "get":
r := &Iq{Id: iqData.Id, Type: "result"}
r.Query = Query{Xmlns: "jabber:iq:auth"}
output, _ := xml.Marshal(r)
fmt.Fprintf(conn, string(output))
case "set":
// Need to perform auth lookup here
i := Iq{Id: iqData.Id, Type: "result"}
output, _ := xml.Marshal(i)
fmt.Fprintf(conn, string(output))
default:
// Nothing
}
}
}