example/interact.go
author Chris Jones <chris@cjones.org>
Sun, 29 Sep 2013 21:55:08 -0600
changeset 160 ba2428230f71
parent 159 fd4089f717b2
child 162 7b5586a5e109
permissions -rw-r--r--
to-do update.
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
159
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    44
	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
    45
	go func() {
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    46
		for s := range stat {
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    47
			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
    48
		}
fd4089f717b2 Listen for XMPP connection status changes, and print them out.
Chris Jones <chris@cjones.org>
parents: 153
diff changeset
    49
	}()
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
    50
	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
    51
	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
    52
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    53
		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
    54
	}
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
    55
	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
    56
137
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    57
	go func(ch <-chan xmpp.Stanza) {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    58
		for obj := range ch {
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    59
			fmt.Printf("s: %v\n", obj)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    60
		}
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    61
		fmt.Println("done reading")
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    62
	}(c.Recv)
c94a7ce0f4fb Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents: 130
diff changeset
    63
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    64
	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
    65
	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
    66
	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
    67
		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
    68
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    69
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    70
	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
    71
	for {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    72
		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
    73
		if nr == 0 {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    74
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    75
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    76
		s := string(p)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    77
		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
    78
		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
    79
		if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    80
			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
    81
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    82
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    83
		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
    84
		var ok bool
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    85
		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
    86
			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
    87
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    88
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    89
		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
    90
		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
    91
		case "iq":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    92
			stan = &xmpp.Iq{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    93
		case "message":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    94
			stan = &xmpp.Message{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    95
		case "presence":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    96
			stan = &xmpp.Presence{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    97
		default:
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    98
			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
    99
			continue
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   100
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   101
		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
   102
		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
   103
			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
   104
		} else {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   105
			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
   106
			break
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
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   109
	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
   110
}