2011-02-27 18:44:24 -08:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// TODO(rsc):
|
|
|
|
// More precise error handling.
|
|
|
|
// Presence functionality.
|
|
|
|
// TODO(mattn):
|
|
|
|
// Add proxy authentication.
|
|
|
|
|
|
|
|
// Package xmpp implements a simple Google Talk client
|
|
|
|
// using the XMPP protocol described in RFC 3920 and RFC 3921.
|
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2013-05-14 19:24:35 -07:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
2011-02-27 18:44:24 -08:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
2014-04-22 09:05:35 -07:00
|
|
|
"encoding/binary"
|
2011-11-09 06:18:51 -08:00
|
|
|
"encoding/xml"
|
2011-11-04 06:40:10 -07:00
|
|
|
"errors"
|
2011-02-27 18:44:24 -08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-05-14 19:24:35 -07:00
|
|
|
"math/big"
|
2011-02-27 18:44:24 -08:00
|
|
|
"net"
|
2011-11-09 06:18:51 -08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2011-02-27 18:44:24 -08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-04-22 09:05:35 -07:00
|
|
|
nsStream = "http://etherx.jabber.org/streams"
|
|
|
|
nsTLS = "urn:ietf:params:xml:ns:xmpp-tls"
|
|
|
|
nsSASL = "urn:ietf:params:xml:ns:xmpp-sasl"
|
|
|
|
nsBind = "urn:ietf:params:xml:ns:xmpp-bind"
|
|
|
|
nsClient = "jabber:client"
|
2014-12-13 06:28:57 -08:00
|
|
|
nsSession = "urn:ietf:params:xml:ns:xmpp-session"
|
2011-02-27 18:44:24 -08:00
|
|
|
)
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// Default TLS configuration options
|
2011-02-27 18:44:24 -08:00
|
|
|
var DefaultConfig tls.Config
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// Cookie is a unique XMPP session identifier
|
2014-04-22 09:05:35 -07:00
|
|
|
type Cookie uint64
|
|
|
|
|
|
|
|
func getCookie() Cookie {
|
|
|
|
var buf [8]byte
|
|
|
|
if _, err := rand.Reader.Read(buf[:]); err != nil {
|
|
|
|
panic("Failed to read random bytes: " + err.Error())
|
|
|
|
}
|
|
|
|
return Cookie(binary.LittleEndian.Uint64(buf[:]))
|
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// Client holds XMPP connection opitons
|
2011-02-27 18:44:24 -08:00
|
|
|
type Client struct {
|
2013-08-12 15:33:50 -07:00
|
|
|
conn net.Conn // connection to server
|
|
|
|
jid string // Jabber ID for our connection
|
2013-05-14 19:24:35 -07:00
|
|
|
domain string
|
2013-08-12 15:33:50 -07:00
|
|
|
p *xml.Decoder
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:24:35 -07:00
|
|
|
func connect(host, user, passwd string) (net.Conn, error) {
|
2011-02-27 18:44:24 -08:00
|
|
|
addr := host
|
|
|
|
|
|
|
|
if strings.TrimSpace(host) == "" {
|
2011-06-27 18:53:04 -07:00
|
|
|
a := strings.SplitN(user, "@", 2)
|
2011-02-27 18:44:24 -08:00
|
|
|
if len(a) == 2 {
|
|
|
|
host = a[1]
|
|
|
|
}
|
|
|
|
}
|
2011-06-27 18:53:04 -07:00
|
|
|
a := strings.SplitN(host, ":", 2)
|
2011-02-27 18:44:24 -08:00
|
|
|
if len(a) == 1 {
|
|
|
|
host += ":5222"
|
|
|
|
}
|
|
|
|
proxy := os.Getenv("HTTP_PROXY")
|
|
|
|
if proxy == "" {
|
|
|
|
proxy = os.Getenv("http_proxy")
|
|
|
|
}
|
|
|
|
if proxy != "" {
|
2011-09-28 12:26:19 -07:00
|
|
|
url, err := url.Parse(proxy)
|
2011-02-27 18:44:24 -08:00
|
|
|
if err == nil {
|
|
|
|
addr = url.Host
|
|
|
|
}
|
|
|
|
}
|
2011-04-05 02:31:59 -07:00
|
|
|
c, err := net.Dial("tcp", addr)
|
2011-02-27 18:44:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if proxy != "" {
|
|
|
|
fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\n", host)
|
|
|
|
fmt.Fprintf(c, "Host: %s\r\n", host)
|
|
|
|
fmt.Fprintf(c, "\r\n")
|
|
|
|
br := bufio.NewReader(c)
|
2011-05-17 17:31:47 -07:00
|
|
|
req, _ := http.NewRequest("CONNECT", host, nil)
|
|
|
|
resp, err := http.ReadResponse(br, req)
|
2011-02-27 18:44:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2011-06-27 18:53:04 -07:00
|
|
|
f := strings.SplitN(resp.Status, " ", 2)
|
2011-11-04 06:40:10 -07:00
|
|
|
return nil, errors.New(f[1])
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 19:24:35 -07:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2013-08-12 15:33:50 -07:00
|
|
|
// Options are used to specify additional options for new clients, such as a Resource.
|
|
|
|
type Options struct {
|
2013-08-12 16:04:39 -07:00
|
|
|
// Host specifies what host to connect to, as either "hostname" or "hostname:port"
|
|
|
|
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
|
|
|
|
// Default the port to 5222.
|
|
|
|
Host string
|
|
|
|
|
|
|
|
// User specifies what user to authenticate to the remote server.
|
|
|
|
User string
|
|
|
|
|
|
|
|
// Password supplies the password to use for authentication with the remote server.
|
|
|
|
Password string
|
|
|
|
|
2013-08-12 15:33:50 -07:00
|
|
|
// Resource specifies an XMPP client resource, like "bot", instead of accepting one
|
|
|
|
// from the server. Use "" to let the server generate one for your client.
|
|
|
|
Resource string
|
2013-08-12 16:04:39 -07:00
|
|
|
|
2014-09-14 23:21:06 -07:00
|
|
|
// TLS Config
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
|
2014-10-28 16:32:19 -07:00
|
|
|
// InsecureAllowUnencryptedAuth permits authentication over a TCP connection that has not been promoted to
|
|
|
|
// TLS by STARTTLS; this could leak authentication information over the network, or permit man in the middle
|
|
|
|
// attacks.
|
|
|
|
InsecureAllowUnencryptedAuth bool
|
|
|
|
|
|
|
|
// NoTLS directs go-xmpp to not use TLS initially to contact the server; instead, a plain old unencrypted
|
|
|
|
// TCP connection should be used. (Can be combined with StartTLS to support STARTTLS-based servers.)
|
2013-08-12 16:04:39 -07:00
|
|
|
NoTLS bool
|
2013-10-18 00:49:41 -07:00
|
|
|
|
2014-10-28 16:32:19 -07:00
|
|
|
// StartTLS directs go-xmpp to STARTTLS if the server supports it; go-xmpp will automatically STARTTLS
|
|
|
|
// if the server requires it regardless of this option.
|
|
|
|
StartTLS bool
|
|
|
|
|
2013-10-18 00:49:41 -07:00
|
|
|
// Debug output
|
|
|
|
Debug bool
|
2014-04-22 09:05:35 -07:00
|
|
|
|
2014-10-04 09:22:05 -07:00
|
|
|
// Use server sessions
|
2014-04-22 09:05:35 -07:00
|
|
|
Session bool
|
2014-10-04 09:22:05 -07:00
|
|
|
|
2014-10-04 09:29:19 -07:00
|
|
|
// Presence Status
|
2014-10-04 09:22:05 -07:00
|
|
|
Status string
|
2014-10-04 09:29:19 -07:00
|
|
|
|
|
|
|
// Status message
|
|
|
|
StatusMessage string
|
2013-08-12 15:33:50 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
// NewClient establishes a new Client connection based on a set of Options.
|
|
|
|
func (o Options) NewClient() (*Client, error) {
|
|
|
|
host := o.Host
|
|
|
|
c, err := connect(host, o.User, o.Password)
|
2013-05-14 19:24:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-02-27 18:44:24 -08:00
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
client := new(Client)
|
|
|
|
if o.NoTLS {
|
|
|
|
client.conn = c
|
|
|
|
} else {
|
2014-09-14 23:15:56 -07:00
|
|
|
var tlsconn *tls.Conn
|
2014-09-14 23:21:06 -07:00
|
|
|
if o.TLSConfig != nil {
|
|
|
|
tlsconn = tls.Client(c, o.TLSConfig)
|
2014-09-14 23:15:56 -07:00
|
|
|
} else {
|
2014-11-09 05:51:33 -08:00
|
|
|
DefaultConfig.ServerName = strings.Split(o.User, "@")[1]
|
2014-09-14 23:15:56 -07:00
|
|
|
tlsconn = tls.Client(c, &DefaultConfig)
|
|
|
|
}
|
2013-08-12 16:04:39 -07:00
|
|
|
if err = tlsconn.Handshake(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if strings.LastIndex(o.Host, ":") > 0 {
|
|
|
|
host = host[:strings.LastIndex(o.Host, ":")]
|
|
|
|
}
|
2014-10-30 15:45:15 -07:00
|
|
|
insecureSkipVerify := DefaultConfig.InsecureSkipVerify
|
|
|
|
if o.TLSConfig != nil {
|
|
|
|
insecureSkipVerify = o.TLSConfig.InsecureSkipVerify
|
|
|
|
}
|
|
|
|
if !insecureSkipVerify {
|
|
|
|
if err = tlsconn.VerifyHostname(host); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-12 16:04:39 -07:00
|
|
|
}
|
|
|
|
client.conn = tlsconn
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
if err := client.init(&o); err != nil {
|
2013-05-14 19:24:35 -07:00
|
|
|
client.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-12 16:04:39 -07:00
|
|
|
|
2013-05-14 19:24:35 -07:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
// NewClient creates a new connection to a host given as "hostname" or "hostname:port".
|
|
|
|
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
|
|
|
|
// Default the port to 5222.
|
2013-10-18 00:49:41 -07:00
|
|
|
func NewClient(host, user, passwd string, debug bool) (*Client, error) {
|
2013-08-12 16:04:39 -07:00
|
|
|
opts := Options{
|
|
|
|
Host: host,
|
|
|
|
User: user,
|
|
|
|
Password: passwd,
|
2013-10-18 00:49:41 -07:00
|
|
|
Debug: debug,
|
2014-04-22 09:05:35 -07:00
|
|
|
Session: false,
|
2013-05-14 19:24:35 -07:00
|
|
|
}
|
2013-08-12 16:04:39 -07:00
|
|
|
return opts.NewClient()
|
|
|
|
}
|
2013-05-14 19:24:35 -07:00
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// NewClientNoTLS creates a new client without TLS
|
2013-10-18 00:49:41 -07:00
|
|
|
func NewClientNoTLS(host, user, passwd string, debug bool) (*Client, error) {
|
2013-08-12 16:04:39 -07:00
|
|
|
opts := Options{
|
|
|
|
Host: host,
|
|
|
|
User: user,
|
|
|
|
Password: passwd,
|
|
|
|
NoTLS: true,
|
2013-10-18 00:49:41 -07:00
|
|
|
Debug: debug,
|
2014-04-22 09:05:35 -07:00
|
|
|
Session: false,
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
2013-08-12 16:04:39 -07:00
|
|
|
return opts.NewClient()
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// Close closes the XMPP connection
|
2011-11-04 06:40:10 -07:00
|
|
|
func (c *Client) Close() error {
|
2014-12-11 02:41:59 -08:00
|
|
|
if c.conn != (*tls.Conn)(nil) {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
2014-12-13 06:28:57 -08:00
|
|
|
return nil
|
2013-05-14 19:24:35 -07:00
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, authenticate, digestURI, nonceCountStr string) string {
|
2013-08-12 15:33:50 -07:00
|
|
|
h := func(text string) []byte {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(text))
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
|
|
|
hex := func(bytes []byte) string {
|
|
|
|
return fmt.Sprintf("%x", bytes)
|
|
|
|
}
|
|
|
|
kd := func(secret, data string) []byte {
|
|
|
|
return h(secret + ":" + data)
|
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
a1 := string(h(username+":"+realm+":"+passwd)) + ":" + nonce + ":" + cnonceStr
|
|
|
|
a2 := authenticate + ":" + digestURI
|
|
|
|
response := hex(kd(hex(h(a1)), nonce+":"+nonceCountStr+":"+cnonceStr+":auth:"+hex(h(a2))))
|
2013-08-12 15:33:50 -07:00
|
|
|
return response
|
2013-05-14 19:24:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func cnonce() string {
|
|
|
|
randSize := big.NewInt(0)
|
|
|
|
randSize.Lsh(big.NewInt(1), 64)
|
|
|
|
cn, err := rand.Int(rand.Reader, randSize)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%016x", cn)
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
func (c *Client) init(o *Options) error {
|
2013-10-18 00:49:41 -07:00
|
|
|
if o.Debug {
|
2014-12-13 06:28:57 -08:00
|
|
|
// For debugging: the following causes the plaintext of the connection to be duplicated to stderr.
|
|
|
|
c.p = xml.NewDecoder(tee{c.conn, os.Stderr})
|
|
|
|
} else {
|
|
|
|
c.p = xml.NewDecoder(c.conn)
|
2013-10-18 00:49:41 -07:00
|
|
|
}
|
2011-02-27 18:44:24 -08:00
|
|
|
|
2013-08-12 16:04:39 -07:00
|
|
|
a := strings.SplitN(o.User, "@", 2)
|
2011-02-27 18:44:24 -08:00
|
|
|
if len(a) != 2 {
|
2013-08-12 16:04:39 -07:00
|
|
|
return errors.New("xmpp: invalid username (want user@domain): " + o.User)
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
domain := a[1]
|
|
|
|
|
2014-10-28 16:32:19 -07:00
|
|
|
// Declare intent to be a jabber client and gather stream features.
|
|
|
|
f, err := c.startStream(o, domain)
|
2011-02-27 18:44:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-28 16:32:19 -07:00
|
|
|
|
|
|
|
// If the server requires we STARTTLS, attempt to do so.
|
2014-12-13 06:28:57 -08:00
|
|
|
if f, err = c.startTLSIfRequired(f, o, domain); err != nil {
|
2014-10-28 16:32:19 -07:00
|
|
|
return err
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:32:19 -07:00
|
|
|
// Even digest forms of authentication are unsafe if we do not know that the host
|
|
|
|
// we are talking to is the actual server, and not a man in the middle playing
|
|
|
|
// proxy.
|
|
|
|
if !c.IsEncrypted() && !o.InsecureAllowUnencryptedAuth {
|
|
|
|
return errors.New("refusing to authenticate over unencrypted TCP connection")
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
2014-10-28 16:32:19 -07:00
|
|
|
|
2013-05-14 19:24:35 -07:00
|
|
|
mechanism := ""
|
2011-02-27 18:44:24 -08:00
|
|
|
for _, m := range f.Mechanisms.Mechanism {
|
2014-09-14 23:12:12 -07:00
|
|
|
if m == "ANONYMOUS" {
|
|
|
|
mechanism = m
|
|
|
|
fmt.Fprintf(c.conn, "<auth xmlns='%s' mechanism='ANONYMOUS' />\n", nsSASL)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
a := strings.SplitN(o.User, "@", 2)
|
|
|
|
if len(a) != 2 {
|
|
|
|
return errors.New("xmpp: invalid username (want user@domain): " + o.User)
|
|
|
|
}
|
|
|
|
user := a[0]
|
|
|
|
domain := a[1]
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
if m == "PLAIN" {
|
2013-05-14 19:24:35 -07:00
|
|
|
mechanism = m
|
|
|
|
// Plain authentication: send base64-encoded \x00 user \x00 password.
|
2013-08-12 16:04:39 -07:00
|
|
|
raw := "\x00" + user + "\x00" + o.Password
|
2013-05-14 19:24:35 -07:00
|
|
|
enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
|
|
|
|
base64.StdEncoding.Encode(enc, []byte(raw))
|
2014-12-13 06:28:57 -08:00
|
|
|
fmt.Fprintf(c.conn, "<auth xmlns='%s' mechanism='PLAIN'>%s</auth>\n", nsSASL, enc)
|
2013-05-14 19:24:35 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if m == "DIGEST-MD5" {
|
|
|
|
mechanism = m
|
|
|
|
// Digest-MD5 authentication
|
2014-12-13 06:28:57 -08:00
|
|
|
fmt.Fprintf(c.conn, "<auth xmlns='%s' mechanism='DIGEST-MD5'/>\n", nsSASL)
|
2013-05-14 19:24:35 -07:00
|
|
|
var ch saslChallenge
|
|
|
|
if err = c.p.DecodeElement(&ch, nil); err != nil {
|
|
|
|
return errors.New("unmarshal <challenge>: " + err.Error())
|
|
|
|
}
|
|
|
|
b, err := base64.StdEncoding.DecodeString(string(ch))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokens := map[string]string{}
|
|
|
|
for _, token := range strings.Split(string(b), ",") {
|
|
|
|
kv := strings.SplitN(strings.TrimSpace(token), "=", 2)
|
|
|
|
if len(kv) == 2 {
|
|
|
|
if kv[1][0] == '"' && kv[1][len(kv[1])-1] == '"' {
|
2013-08-12 15:33:50 -07:00
|
|
|
kv[1] = kv[1][1 : len(kv[1])-1]
|
2013-05-14 19:24:35 -07:00
|
|
|
}
|
|
|
|
tokens[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
realm, _ := tokens["realm"]
|
|
|
|
nonce, _ := tokens["nonce"]
|
|
|
|
qop, _ := tokens["qop"]
|
|
|
|
charset, _ := tokens["charset"]
|
|
|
|
cnonceStr := cnonce()
|
2014-12-13 06:28:57 -08:00
|
|
|
digestURI := "xmpp/" + domain
|
2013-05-14 19:24:35 -07:00
|
|
|
nonceCount := fmt.Sprintf("%08x", 1)
|
2014-12-13 06:28:57 -08:00
|
|
|
digest := saslDigestResponse(user, realm, o.Password, nonce, cnonceStr, "AUTHENTICATE", digestURI, nonceCount)
|
|
|
|
message := "username=\"" + user + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", cnonce=\"" + cnonceStr +
|
|
|
|
"\", nc=" + nonceCount + ", qop=" + qop + ", digest-uri=\"" + digestURI + "\", response=" + digest + ", charset=" + charset
|
2014-04-16 08:25:20 -07:00
|
|
|
|
2013-05-14 19:24:35 -07:00
|
|
|
fmt.Fprintf(c.conn, "<response xmlns='%s'>%s</response>\n", nsSASL, base64.StdEncoding.EncodeToString([]byte(message)))
|
|
|
|
|
|
|
|
var rspauth saslRspAuth
|
|
|
|
if err = c.p.DecodeElement(&rspauth, nil); err != nil {
|
|
|
|
return errors.New("unmarshal <challenge>: " + err.Error())
|
|
|
|
}
|
|
|
|
b, err = base64.StdEncoding.DecodeString(string(rspauth))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(c.conn, "<response xmlns='%s'/>\n", nsSASL)
|
2011-02-27 18:44:24 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-05-14 19:24:35 -07:00
|
|
|
if mechanism == "" {
|
2014-12-13 06:28:57 -08:00
|
|
|
return fmt.Errorf("PLAIN authentication is not an option: %v", f.Mechanisms.Mechanism)
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next message should be either success or failure.
|
|
|
|
name, val, err := next(c.p)
|
2013-11-03 23:13:55 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-02-27 18:44:24 -08:00
|
|
|
switch v := val.(type) {
|
|
|
|
case *saslSuccess:
|
|
|
|
case *saslFailure:
|
|
|
|
// v.Any is type of sub-element in failure,
|
|
|
|
// which gives a description of what failed.
|
2011-11-04 06:40:10 -07:00
|
|
|
return errors.New("auth failure: " + v.Any.Local)
|
2011-02-27 18:44:24 -08:00
|
|
|
default:
|
2011-11-04 06:40:10 -07:00
|
|
|
return errors.New("expected <success> or <failure>, got <" + name.Local + "> in " + name.Space)
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're authenticated, we're supposed to start the stream over again.
|
|
|
|
// Declare intent to be a jabber client.
|
2014-10-28 16:32:19 -07:00
|
|
|
if f, err = c.startStream(o, domain); err != nil {
|
2011-02-27 18:44:24 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// Generate a uniqe cookie
|
2014-04-22 09:05:35 -07:00
|
|
|
cookie := getCookie()
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
// Send IQ message asking to bind to the local user name.
|
2013-08-12 16:04:39 -07:00
|
|
|
if o.Resource == "" {
|
2014-04-22 09:05:35 -07:00
|
|
|
fmt.Fprintf(c.conn, "<iq type='set' id='%x'><bind xmlns='%s'></bind></iq>\n", cookie, nsBind)
|
2013-08-12 15:33:50 -07:00
|
|
|
} else {
|
2014-04-22 09:05:35 -07:00
|
|
|
fmt.Fprintf(c.conn, "<iq type='set' id='%x'><bind xmlns='%s'><resource>%s</resource></bind></iq>\n", cookie, nsBind, o.Resource)
|
2013-08-12 15:33:50 -07:00
|
|
|
}
|
2011-02-27 18:44:24 -08:00
|
|
|
var iq clientIQ
|
2012-02-08 21:44:44 -08:00
|
|
|
if err = c.p.DecodeElement(&iq, nil); err != nil {
|
2011-11-04 06:40:10 -07:00
|
|
|
return errors.New("unmarshal <iq>: " + err.Error())
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
2011-05-17 16:51:59 -07:00
|
|
|
if &iq.Bind == nil {
|
2011-11-04 06:40:10 -07:00
|
|
|
return errors.New("<iq> result missing <bind>")
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
c.jid = iq.Bind.Jid // our local id
|
|
|
|
|
2014-04-22 09:05:35 -07:00
|
|
|
if o.Session {
|
|
|
|
//if server support session, open it
|
2014-12-13 06:28:57 -08:00
|
|
|
fmt.Fprintf(c.conn, "<iq to='%s' type='set' id='%x'><session xmlns='%s'/></iq>", xmlEscape(domain), cookie, nsSession)
|
2014-04-22 09:05:35 -07:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
// We're connected and can now receive and send messages.
|
2014-10-04 09:29:19 -07:00
|
|
|
fmt.Fprintf(c.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", o.Status, o.StatusMessage)
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-28 16:32:19 -07:00
|
|
|
// startTlsIfRequired examines the server's stream features and, if STARTTLS is required or supported, performs the TLS handshake.
|
|
|
|
// f will be updated if the handshake completes, as the new stream's features are typically different from the original.
|
2014-12-13 06:28:57 -08:00
|
|
|
func (c *Client) startTLSIfRequired(f *streamFeatures, o *Options, domain string) (*streamFeatures, error) {
|
2014-10-28 16:32:19 -07:00
|
|
|
// whether we start tls is a matter of opinion: the server's and the user's.
|
|
|
|
switch {
|
|
|
|
case f.StartTLS == nil:
|
|
|
|
// the server does not support STARTTLS
|
|
|
|
return f, nil
|
|
|
|
case f.StartTLS.Required != nil:
|
|
|
|
// the server requires STARTTLS.
|
|
|
|
case !o.StartTLS:
|
|
|
|
// the user wants STARTTLS and the server supports it.
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
fmt.Fprintf(c.conn, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\n")
|
|
|
|
var k tlsProceed
|
|
|
|
if err = c.p.DecodeElement(&k, nil); err != nil {
|
|
|
|
return f, errors.New("unmarshal <proceed>: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := o.TLSConfig
|
|
|
|
if tc == nil {
|
|
|
|
tc = new(tls.Config)
|
|
|
|
*tc = DefaultConfig
|
|
|
|
//TODO(scott): we should consider using the server's address or reverse lookup
|
|
|
|
tc.ServerName = domain
|
|
|
|
}
|
|
|
|
t := tls.Client(c.conn, tc)
|
|
|
|
|
|
|
|
if err = t.Handshake(); err != nil {
|
|
|
|
return f, errors.New("starttls handshake: " + err.Error())
|
|
|
|
}
|
|
|
|
c.conn = t
|
|
|
|
|
|
|
|
// restart our declaration of XMPP stream intentions.
|
|
|
|
tf, err := c.startStream(o, domain)
|
|
|
|
if err != nil {
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
return tf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// startStream will start a new XML decoder for the connection, signal the start of a stream to the server and verify that the server has
|
2014-12-13 06:28:57 -08:00
|
|
|
// also started the stream; if o.Debug is true, startStream will tee decoded XML data to stderr. The features advertised by the server
|
2014-10-28 16:32:19 -07:00
|
|
|
// will be returned.
|
|
|
|
func (c *Client) startStream(o *Options, domain string) (*streamFeatures, error) {
|
|
|
|
if o.Debug {
|
2014-12-13 06:28:57 -08:00
|
|
|
c.p = xml.NewDecoder(tee{c.conn, os.Stderr})
|
|
|
|
} else {
|
|
|
|
c.p = xml.NewDecoder(c.conn)
|
2014-10-28 16:32:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(c.conn, "<?xml version='1.0'?>\n"+
|
|
|
|
"<stream:stream to='%s' xmlns='%s'\n"+
|
|
|
|
" xmlns:stream='%s' version='1.0'>\n",
|
|
|
|
xmlEscape(domain), nsClient, nsStream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the server to start a <stream>.
|
|
|
|
se, err := nextStart(c.p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if se.Name.Space != nsStream || se.Name.Local != "stream" {
|
|
|
|
return nil, fmt.Errorf("expected <stream> but got <%v> in %v", se.Name.Local, se.Name.Space)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we're in the stream and can use Unmarshal.
|
|
|
|
// Next message should be <features> to tell us authentication options.
|
|
|
|
// See section 4.6 in RFC 3920.
|
|
|
|
f := new(streamFeatures)
|
|
|
|
if err = c.p.DecodeElement(f, nil); err != nil {
|
|
|
|
return f, errors.New("unmarshal <features>: " + err.Error())
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 16:53:23 -08:00
|
|
|
// IsEncrypted will return true if the client is connected using a TLS transport, either because it used.
|
2014-12-28 16:16:23 -08:00
|
|
|
// TLS to connect from the outset, or because it successfully used STARTTLS to promote a TCP connection to TLS.
|
2014-10-28 16:32:19 -07:00
|
|
|
func (c *Client) IsEncrypted() bool {
|
|
|
|
_, ok := c.conn.(*tls.Conn)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2014-12-28 16:16:23 -08:00
|
|
|
// Chat is an incoming or outgoing XMPP chat message.
|
2011-02-27 18:44:24 -08:00
|
|
|
type Chat struct {
|
|
|
|
Remote string
|
|
|
|
Type string
|
|
|
|
Text string
|
2013-10-30 02:13:02 -07:00
|
|
|
Other []string
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2014-12-28 16:16:23 -08:00
|
|
|
// Presence is an XMPP presence notification.
|
2013-01-18 16:48:50 -08:00
|
|
|
type Presence struct {
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
Type string
|
|
|
|
Show string
|
|
|
|
}
|
|
|
|
|
2014-12-28 16:16:23 -08:00
|
|
|
// Recv waits to receive the next XMPP stanza.
|
|
|
|
// Return type is either a presence notification or a chat message.
|
|
|
|
func (c *Client) Recv() (stanza interface{}, err error) {
|
2011-02-27 18:44:24 -08:00
|
|
|
for {
|
|
|
|
_, val, err := next(c.p)
|
|
|
|
if err != nil {
|
|
|
|
return Chat{}, err
|
|
|
|
}
|
2013-01-18 16:48:50 -08:00
|
|
|
switch v := val.(type) {
|
|
|
|
case *clientMessage:
|
2013-10-30 02:13:02 -07:00
|
|
|
return Chat{v.From, v.Type, v.Body, v.Other}, nil
|
2013-01-18 16:48:50 -08:00
|
|
|
case *clientPresence:
|
|
|
|
return Presence{v.From, v.To, v.Type, v.Show}, nil
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2014-12-28 16:16:23 -08:00
|
|
|
// Send sends the message wrapped inside an XMPP message stanza body.
|
2014-11-15 10:43:43 -08:00
|
|
|
func (c *Client) Send(chat Chat) (n int, err error) {
|
2014-12-28 16:16:23 -08:00
|
|
|
return fmt.Fprintf(c.conn, "<message to='%s' type='%s' xml:lang='en'>"+"<body>%s</body></message>",
|
2013-01-18 16:49:47 -08:00
|
|
|
xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text))
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
2014-12-13 06:28:57 -08:00
|
|
|
// SendOrg sends the original text without being wrapped in an XMPP message stanza.
|
2014-11-15 10:43:43 -08:00
|
|
|
func (c *Client) SendOrg(org string) (n int, err error) {
|
|
|
|
return fmt.Fprint(c.conn, org)
|
2013-10-18 00:52:01 -07:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
// RFC 3920 C.1 Streams name space
|
|
|
|
type streamFeatures struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
|
2014-10-28 16:32:19 -07:00
|
|
|
StartTLS *tlsStartTLS
|
2011-05-17 16:51:59 -07:00
|
|
|
Mechanisms saslMechanisms
|
|
|
|
Bind bindBind
|
2011-02-27 18:44:24 -08:00
|
|
|
Session bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type streamError struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"`
|
2011-02-27 18:44:24 -08:00
|
|
|
Any xml.Name
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3920 C.3 TLS name space
|
|
|
|
type tlsStartTLS struct {
|
2014-10-28 16:32:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"`
|
|
|
|
Required *string `xml:"required"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tlsProceed struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tlsFailure struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls failure"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3920 C.4 SASL name space
|
|
|
|
type saslMechanisms struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
|
2012-04-03 09:46:01 -07:00
|
|
|
Mechanism []string `xml:"mechanism"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type saslAuth struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
|
2012-04-03 09:46:01 -07:00
|
|
|
Mechanism string `xml:",attr"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type saslChallenge string
|
|
|
|
|
2013-05-14 19:24:35 -07:00
|
|
|
type saslRspAuth string
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
type saslResponse string
|
|
|
|
|
|
|
|
type saslAbort struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl abort"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type saslSuccess struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type saslFailure struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
|
2011-02-27 18:44:24 -08:00
|
|
|
Any xml.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3920 C.5 Resource binding name space
|
|
|
|
type bindBind struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
|
2011-02-27 18:44:24 -08:00
|
|
|
Resource string
|
2013-01-18 16:14:09 -08:00
|
|
|
Jid string `xml:"jid"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3921 B.1 jabber:client
|
|
|
|
type clientMessage struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"jabber:client message"`
|
2012-06-16 12:39:22 -07:00
|
|
|
From string `xml:"from,attr"`
|
2014-12-13 06:28:57 -08:00
|
|
|
ID string `xml:"id,attr"`
|
2012-06-16 12:39:22 -07:00
|
|
|
To string `xml:"to,attr"`
|
|
|
|
Type string `xml:"type,attr"` // chat, error, groupchat, headline, or normal
|
2011-02-27 18:44:24 -08:00
|
|
|
|
2014-12-29 16:53:23 -08:00
|
|
|
// These should technically be []clientText, but string is much more convenient.
|
2012-06-16 12:39:22 -07:00
|
|
|
Subject string `xml:"subject"`
|
|
|
|
Body string `xml:"body"`
|
|
|
|
Thread string `xml:"thread"`
|
2013-10-30 02:13:02 -07:00
|
|
|
|
2013-10-30 02:14:11 -07:00
|
|
|
// Any hasn't matched element
|
2013-10-30 02:13:02 -07:00
|
|
|
Other []string `xml:",any"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientText struct {
|
2012-04-03 09:46:01 -07:00
|
|
|
Lang string `xml:",attr"`
|
2011-09-28 12:26:19 -07:00
|
|
|
Body string `xml:"chardata"`
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientPresence struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"jabber:client presence"`
|
2012-06-16 19:47:48 -07:00
|
|
|
From string `xml:"from,attr"`
|
2014-12-13 06:28:57 -08:00
|
|
|
ID string `xml:"id,attr"`
|
2012-06-16 19:47:48 -07:00
|
|
|
To string `xml:"to,attr"`
|
|
|
|
Type string `xml:"type,attr"` // error, probe, subscribe, subscribed, unavailable, unsubscribe, unsubscribed
|
|
|
|
Lang string `xml:"lang,attr"`
|
2011-02-27 18:44:24 -08:00
|
|
|
|
2013-01-18 16:48:50 -08:00
|
|
|
Show string `xml:"show"` // away, chat, dnd, xa
|
|
|
|
Status string `xml:"status,attr"` // sb []clientText
|
|
|
|
Priority string `xml:"priority,attr"`
|
2011-02-27 18:44:24 -08:00
|
|
|
Error *clientError
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientIQ struct { // info/query
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"jabber:client iq"`
|
2012-04-03 09:46:01 -07:00
|
|
|
From string `xml:",attr"`
|
2014-12-13 06:28:57 -08:00
|
|
|
ID string `xml:",attr"`
|
2012-04-03 09:46:01 -07:00
|
|
|
To string `xml:",attr"`
|
|
|
|
Type string `xml:",attr"` // error, get, result, set
|
2011-05-17 16:51:59 -07:00
|
|
|
Error clientError
|
|
|
|
Bind bindBind
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientError struct {
|
2011-09-28 12:26:19 -07:00
|
|
|
XMLName xml.Name `xml:"jabber:client error"`
|
2012-04-03 09:46:01 -07:00
|
|
|
Code string `xml:",attr"`
|
|
|
|
Type string `xml:",attr"`
|
2011-02-27 18:44:24 -08:00
|
|
|
Any xml.Name
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan XML token stream to find next StartElement.
|
2012-02-08 21:44:44 -08:00
|
|
|
func nextStart(p *xml.Decoder) (xml.StartElement, error) {
|
2011-02-27 18:44:24 -08:00
|
|
|
for {
|
|
|
|
t, err := p.Token()
|
2013-11-03 23:13:55 -08:00
|
|
|
if err != nil && err != io.EOF {
|
2013-11-04 18:03:26 -08:00
|
|
|
return xml.StartElement{}, err
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
switch t := t.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan XML token stream for next element and save into val.
|
|
|
|
// If val == nil, allocate new element based on proto map.
|
|
|
|
// Either way, return val.
|
2012-02-08 21:44:44 -08:00
|
|
|
func next(p *xml.Decoder) (xml.Name, interface{}, error) {
|
2011-02-27 18:44:24 -08:00
|
|
|
// Read start element to find out what type we want.
|
|
|
|
se, err := nextStart(p)
|
|
|
|
if err != nil {
|
|
|
|
return xml.Name{}, nil, err
|
|
|
|
}
|
2011-05-10 23:11:14 -07:00
|
|
|
|
|
|
|
// Put it in an interface and allocate one.
|
|
|
|
var nv interface{}
|
2011-11-04 06:40:10 -07:00
|
|
|
switch se.Name.Space + " " + se.Name.Local {
|
|
|
|
case nsStream + " features":
|
|
|
|
nv = &streamFeatures{}
|
|
|
|
case nsStream + " error":
|
|
|
|
nv = &streamError{}
|
|
|
|
case nsTLS + " starttls":
|
|
|
|
nv = &tlsStartTLS{}
|
|
|
|
case nsTLS + " proceed":
|
|
|
|
nv = &tlsProceed{}
|
|
|
|
case nsTLS + " failure":
|
|
|
|
nv = &tlsFailure{}
|
|
|
|
case nsSASL + " mechanisms":
|
|
|
|
nv = &saslMechanisms{}
|
|
|
|
case nsSASL + " challenge":
|
|
|
|
nv = ""
|
|
|
|
case nsSASL + " response":
|
|
|
|
nv = ""
|
|
|
|
case nsSASL + " abort":
|
|
|
|
nv = &saslAbort{}
|
|
|
|
case nsSASL + " success":
|
|
|
|
nv = &saslSuccess{}
|
|
|
|
case nsSASL + " failure":
|
|
|
|
nv = &saslFailure{}
|
|
|
|
case nsBind + " bind":
|
|
|
|
nv = &bindBind{}
|
|
|
|
case nsClient + " message":
|
|
|
|
nv = &clientMessage{}
|
|
|
|
case nsClient + " presence":
|
|
|
|
nv = &clientPresence{}
|
|
|
|
case nsClient + " iq":
|
|
|
|
nv = &clientIQ{}
|
|
|
|
case nsClient + " error":
|
|
|
|
nv = &clientError{}
|
2011-05-10 23:11:14 -07:00
|
|
|
default:
|
2011-11-04 06:40:10 -07:00
|
|
|
return xml.Name{}, nil, errors.New("unexpected XMPP message " +
|
2011-02-27 18:44:24 -08:00
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal into that storage.
|
2012-02-08 21:44:44 -08:00
|
|
|
if err = p.DecodeElement(nv, &se); err != nil {
|
2011-02-27 18:44:24 -08:00
|
|
|
return xml.Name{}, nil, err
|
|
|
|
}
|
2011-05-10 23:11:14 -07:00
|
|
|
return se.Name, nv, err
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var xmlSpecial = map[byte]string{
|
|
|
|
'<': "<",
|
|
|
|
'>': ">",
|
|
|
|
'"': """,
|
|
|
|
'\'': "'",
|
|
|
|
'&': "&",
|
|
|
|
}
|
|
|
|
|
|
|
|
func xmlEscape(s string) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
if s, ok := xmlSpecial[c]; ok {
|
|
|
|
b.WriteString(s)
|
|
|
|
} else {
|
|
|
|
b.WriteByte(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type tee struct {
|
|
|
|
r io.Reader
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
2011-11-04 06:40:10 -07:00
|
|
|
func (t tee) Read(p []byte) (n int, err error) {
|
2011-02-27 18:44:24 -08:00
|
|
|
n, err = t.r.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
t.w.Write(p[0:n])
|
2013-10-18 00:49:41 -07:00
|
|
|
t.w.Write([]byte("\n"))
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|