example/interact.go
author Chris Jones <christian.jones@sri.com>
Mon, 09 Sep 2013 04:16:16 +0100
changeset 136 f35f853a52b6
parent 130 da6f37ae3ffe
child 137 c94a7ce0f4fb
permissions -rw-r--r--
Simplified and debugged the filter logic.
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
// Copyright 2011 The Go Authors.  All rights reserved.
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     4
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     5
package main
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     6
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     7
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
     8
	"../xmpp"
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     9
	"crypto/tls"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    10
	"encoding/xml"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    11
	"flag"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    12
	"fmt"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    13
	"log"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    14
	"os"
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    15
	"strings"
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
type StdLogger struct {
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) Log(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.Println(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 (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
    26
	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
    27
}
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
func init() {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    30
	logger := &StdLogger{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    31
	// xmpp.Debug = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    32
	xmpp.Info = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    33
	xmpp.Warn = logger
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    34
}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    35
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    36
// 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
    37
// 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
    38
func main() {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
	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
    40
	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
    41
	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
    42
	flag.Parse()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    43
	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
    44
		flag.Usage()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    45
		os.Exit(2)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    46
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    47
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
    48
	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
    49
	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
    50
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    51
		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
    52
	}
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
    53
	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
    54
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    55
	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
    56
	if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    57
		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
    58
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    59
	c.Roster.Update()
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    60
	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
    61
	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
    62
	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
    63
		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
    64
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    65
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    66
	go func(ch <-chan xmpp.Stanza) {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    67
		for obj := range ch {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    68
			fmt.Printf("s: %v\n", obj)
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
		fmt.Println("done reading")
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
    71
	}(c.Recv)
125
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    72
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    73
	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
    74
	for {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    75
		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
    76
		if nr == 0 {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    77
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    78
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    79
		s := string(p)
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    80
		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
    81
		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
    82
		if err != nil {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    83
			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
    84
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    85
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    86
		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
    87
		var ok bool
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    88
		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
    89
			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
    90
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    91
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    92
		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
    93
		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
    94
		case "iq":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    95
			stan = &xmpp.Iq{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    96
		case "message":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    97
			stan = &xmpp.Message{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    98
		case "presence":
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    99
			stan = &xmpp.Presence{}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   100
		default:
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   101
			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
   102
			continue
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   103
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   104
		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
   105
		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
   106
			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
   107
		} else {
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   108
			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
   109
			break
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   110
		}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   111
	}
f464f14e39a7 Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
   112
	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
   113
}