|
1 // Copyright 2011 The Go Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style |
|
3 // license that can be found in the LICENSE file. |
|
4 |
|
5 // This package implements a simple XMPP client according to RFCs 3920 |
|
6 // and 3921, plus the various XEPs at http://xmpp.org/protocols/. |
|
7 package xmpp |
|
8 |
|
9 import ( |
|
10 "fmt" |
|
11 "net" |
|
12 "os" |
|
13 ) |
|
14 |
|
15 const ( |
|
16 serverSrv = "xmpp-server" |
|
17 clientSrv = "xmpp-client" |
|
18 ) |
|
19 |
|
20 // The client in a client-server XMPP connection. |
|
21 type Client struct { |
|
22 //In <-chan *Stanza |
|
23 //Out chan<- *Stanza |
|
24 tcp *net.TCPConn |
|
25 } |
|
26 |
|
27 // Connect to the appropriate server and authenticate as the given JID |
|
28 // with the given password. |
|
29 func NewClient(jid *JID, password string) (*Client, os.Error) { |
|
30 // Resolve the domain in the JID. |
|
31 _, srvs, err := net.LookupSRV(clientSrv, "tcp", jid.Domain) |
|
32 if err != nil { |
|
33 return nil, os.NewError("LookupSrv " + jid.Domain + |
|
34 ": " + err.String()) |
|
35 } |
|
36 |
|
37 var c *net.TCPConn |
|
38 for _, srv := range srvs { |
|
39 addrStr := fmt.Sprintf("%s:%d", srv.Target, srv.Port) |
|
40 addr, err := net.ResolveTCPAddr("tcp", addrStr) |
|
41 if err != nil { |
|
42 err = os.NewError(fmt.Sprintf("ResolveTCPAddr(%s): %s", |
|
43 addrStr, err.String())) |
|
44 continue |
|
45 } |
|
46 c, err = net.DialTCP("tcp", nil, addr) |
|
47 if err != nil { |
|
48 err = os.NewError(fmt.Sprintf("DialTCP(%s): %s", |
|
49 addr, err.String())) |
|
50 continue |
|
51 } |
|
52 } |
|
53 if c == nil { |
|
54 return nil, err |
|
55 } |
|
56 |
|
57 cl := Client{} |
|
58 cl.tcp = c |
|
59 return &cl, nil |
|
60 } |