example/interact.go
author Chris Jones <chris@cjones.org>
Sat, 09 Nov 2013 12:09:37 -0700
changeset 178 ccfebbd9f49b
parent 163 3f891f7fe817
permissions -rw-r--r--
Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.
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
func init() {
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 162
diff changeset
    15
	// xmpp.Debug = true
125
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
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    18
// 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
    19
// 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
    20
func main() {
178
ccfebbd9f49b Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.
Chris Jones <chris@cjones.org>
parents: 163
diff changeset
    21
	jidStr := flag.String("jid", "", "JID to log in as")
ccfebbd9f49b Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.
Chris Jones <chris@cjones.org>
parents: 163
diff changeset
    22
	pw := flag.String("pw", "", "password")
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    23
	flag.Parse()
178
ccfebbd9f49b Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.
Chris Jones <chris@cjones.org>
parents: 163
diff changeset
    24
	jid := xmpp.JID(*jidStr)
ccfebbd9f49b Changed the JID type to be an alias of string, rather than a struct. This allows it to be used as a key in a map, among other benefits.
Chris Jones <chris@cjones.org>
parents: 163
diff changeset
    25
	if jid.Domain() == "" || *pw == "" {
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    26
		flag.Usage()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    27
		os.Exit(2)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    28
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    29
159
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    30
	stat := make(chan xmpp.Status)
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    31
	go func() {
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    32
		for s := range stat {
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    33
			log.Printf("connection status %d", s)
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    34
		}
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    35
	}()
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
    36
	tlsConf := tls.Config{InsecureSkipVerify: true}
159
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    37
	c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil, xmpp.Presence{}, stat)
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    38
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
		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
    40
	}
162
7b5586a5e109 Added a Close() function and fixed a couple of shutdown bugs.
Chris Jones <chris@cjones.org>
parents: 159
diff changeset
    41
	defer c.Close()
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    42
137
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    43
	go func(ch <-chan xmpp.Stanza) {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    44
		for obj := range ch {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    45
			fmt.Printf("s: %v\n", obj)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    46
		}
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    47
		fmt.Println("done reading")
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    48
	}(c.Recv)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    49
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    50
	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
    51
	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
    52
	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
    53
		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
    54
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    55
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    56
	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
    57
	for {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    58
		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
    59
		if nr == 0 {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    60
			break
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
		s := string(p)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    63
		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
    64
		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
    65
		if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    66
			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
    67
			break
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
		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
    70
		var ok bool
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    71
		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
    72
			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
    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
		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
    76
		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
    77
		case "iq":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    78
			stan = &xmpp.Iq{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    79
		case "message":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    80
			stan = &xmpp.Message{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    81
		case "presence":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    82
			stan = &xmpp.Presence{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    83
		default:
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    84
			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
    85
			continue
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    86
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    87
		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
    88
		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
    89
			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
    90
		} else {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    91
			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
    92
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    93
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    94
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    95
	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
    96
}