Skip to content

Commit

Permalink
Seems to write to server now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Ratnikov committed Apr 12, 2011
1 parent b63ffb7 commit 9f9ea86
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions connection.go
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"fmt"
"os"
"regexp"
"net"
"xml"
)
Expand All @@ -30,21 +31,21 @@ func NewClient(user, password string) (client *Client, failure os.Error) {
}
}()

c := &Client{}
client = &Client{}


hostname := "talk.google.com"

if conn, err := net.Dial("tcp", "", "talk.google.com:5222"); err != nil {
fmt.Printf("crap!!! %s\n", err)
} else {
c.write(conn, "<?xml version='1.0'?>")
c.write(conn, "<stream:stream to='gmail.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>")
client.write(conn, "<?xml version='1.0'?>")
client.write(conn, "<stream:stream to='gmail.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>")

fmt.Printf("Read: %s\n", client.read(conn))

// assuming need to start tls
c.write(conn, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />")
client.write(conn, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />")

fmt.Printf("Read: %s\n", client.read(conn))
}
Expand All @@ -55,6 +56,8 @@ func NewClient(user, password string) (client *Client, failure os.Error) {
panic(os.NewError(fmt.Sprintf("Failed to connect to %s (%s)", hostname, err)))
}

client.tls = tlsconn

tlsconn.Handshake()

client.write(tlsconn, "<stream:stream to='gmail.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>")
Expand All @@ -68,21 +71,57 @@ func NewClient(user, password string) (client *Client, failure os.Error) {

client.read(tlsconn)

client.write(tlsconn, "<stream:stream to='gmail.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>")

client.read(tlsconn)

client.write(tlsconn, "<iq type='set' id='xmpp-bot1029'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Home</resource></bind></iq>")

client.read(tlsconn)
client.read(tlsconn)
client.read(tlsconn)

client.read(tlsconn)

for {
raw := client.read(tlsconn)

var recipient, received string

if match := regexp.MustCompile("to=\"([^/\"]*)/").FindStringSubmatch(raw); len(match) > 1 {
recipient = match[1]
}

if match := regexp.MustCompile("<body>(.*)</body>").FindStringSubmatch(raw); len(match) > 1 {
received = match[1]
}

message := "You wrote: " + received

fmt.Printf("Sending message %s to %s", message, recipient)

client.Message(recipient, message)
}

return client, nil
}

func (client *Client) Message(recipient, msg string) {
client.write(client.tls, fmt.Sprintf("<message type='chat' id='xmpp-bot1029' to='%s'><body>%s</body></message>", recipient, msg))
}

func (client *Client) read(conn io.Reader) (out string) {
buf := make([]byte, 2048)

num, err := conn.Read(buf)

if err != nil {
panic("Failed to read....")
fmt.Printf("Error occured (%s). Read %d bytes anyway: %s\n", err, num, buf)
} else {
fmt.Printf("Read %d bytes: %s\n", num, buf)
}

return
return bytes.NewBuffer(buf).String()
}


Expand Down

0 comments on commit 9f9ea86

Please sign in to comment.