example/interact.go
author Chris Jones <christian.jones@sri.com>
Sun, 15 Sep 2013 11:34:35 -0600
changeset 142 0ff033eed887
parent 137 c94a7ce0f4fb
child 153 bbd4166df95d
permissions -rw-r--r--
Removed copyright statements.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     1
package main
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     2
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     3
import (
130
da6f37ae3ffe Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents: 125
diff changeset
     4
	"../xmpp"
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     5
	"crypto/tls"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     6
	"encoding/xml"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     7
	"flag"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     8
	"fmt"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     9
	"log"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    10
	"os"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    11
	"strings"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    12
)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    13
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    14
type StdLogger struct {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    15
}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    16
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    17
func (s *StdLogger) Log(v ...interface{}) {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    18
	log.Println(v...)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    19
}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    20
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    21
func (s *StdLogger) Logf(fmt string, v ...interface{}) {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    22
	log.Printf(fmt, v...)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    23
}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    24
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    25
func init() {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    26
	logger := &StdLogger{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    27
	// xmpp.Debug = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    28
	xmpp.Info = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    29
	xmpp.Warn = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    30
}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    31
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    32
// Demonstrate the API, and allow the user to interact with an XMPP
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    33
// server via the terminal.
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    34
func main() {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    35
	var jid xmpp.JID
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    36
	flag.Var(&jid, "jid", "JID to log in as")
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    37
	var pw *string = flag.String("pw", "", "password")
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    38
	flag.Parse()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
	if jid.Domain == "" || *pw == "" {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    40
		flag.Usage()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    41
		os.Exit(2)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    42
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    43
130
da6f37ae3ffe Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents: 125
diff changeset
    44
	tlsConf := tls.Config{InsecureSkipVerify: true}
da6f37ae3ffe Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents: 125
diff changeset
    45
	c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil)
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    46
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    47
		log.Fatalf("NewClient(%v): %v", jid, err)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    48
	}
130
da6f37ae3ffe Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents: 125
diff changeset
    49
	defer close(c.Send)
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    50
137
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    51
	go func(ch <-chan xmpp.Stanza) {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    52
		for obj := range ch {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    53
			fmt.Printf("s: %v\n", obj)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    54
		}
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    55
		fmt.Println("done reading")
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    56
	}(c.Recv)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    57
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    58
	err = c.StartSession(&xmpp.Presence{})
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    59
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    60
		log.Fatalf("StartSession: %v", err)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    61
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    62
	c.Roster.Update()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    63
	roster := c.Roster.Get()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    64
	fmt.Printf("%d roster entries:\n", len(roster))
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    65
	for i, entry := range roster {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    66
		fmt.Printf("%d: %v\n", i, entry)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    67
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    68
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    69
	p := make([]byte, 1024)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    70
	for {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    71
		nr, _ := os.Stdin.Read(p)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    72
		if nr == 0 {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    73
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    74
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    75
		s := string(p)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    76
		dec := xml.NewDecoder(strings.NewReader(s))
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    77
		t, err := dec.Token()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    78
		if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    79
			fmt.Printf("token: %s\n", err)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    80
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    81
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    82
		var se *xml.StartElement
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    83
		var ok bool
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    84
		if se, ok = t.(*xml.StartElement); !ok {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    85
			fmt.Println("Couldn't find start element")
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    86
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    87
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    88
		var stan xmpp.Stanza
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    89
		switch se.Name.Local {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    90
		case "iq":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    91
			stan = &xmpp.Iq{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    92
		case "message":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    93
			stan = &xmpp.Message{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    94
		case "presence":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    95
			stan = &xmpp.Presence{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    96
		default:
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    97
			fmt.Println("Can't parse non-stanza.")
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    98
			continue
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    99
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   100
		err = dec.Decode(stan)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   101
		if err == nil {
130
da6f37ae3ffe Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents: 125
diff changeset
   102
			c.Send <- stan
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   103
		} else {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   104
			fmt.Printf("Parse error: %v\n", err)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   105
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   106
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   107
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   108
	fmt.Println("done sending")
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   109
}