diff --git a/client.go b/client.go index 60c8d5c..93f51c7 100644 --- a/client.go +++ b/client.go @@ -14,29 +14,19 @@ var ( func main() { - // Parse args. flag.Parse() - jid, _ := xmpp.ParseJID(*jid) - password := *password - // Create stream. - stream, err := xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true}) - if err != nil { - log.Fatal(err) - } - - // Configure stream as a client connection. - x, err := xmpp.NewClientXMPP(stream, jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true}) - if err != nil { - log.Fatal(err) - } + // Create stream and configure it as a client connection. + jid := must(xmpp.ParseJID(*jid)).(xmpp.JID) + stream := must(xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream) + x := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP) log.Printf("Connection established for %s\n", x.JID) // Announce presence. x.Send(xmpp.Presence{}) - // Filter messages into dedicated channel and start a thread to log them. + // Filter messages into dedicated channel and start a goroutine to log them. _, messages := x.AddFilter( func(v interface{}) bool { _, ok := v.(*xmpp.Message) @@ -71,6 +61,13 @@ func main() { select {} } +func must(v interface{}, err error) interface{} { + if err != nil { + log.Fatal(err) + } + return v +} + type DiscoInfo struct { XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"` Identity []DiscoIdentity `xml:"identity"` diff --git a/component.go b/component.go index 2b3f7b3..449c8e4 100644 --- a/component.go +++ b/component.go @@ -13,22 +13,13 @@ var ( ) func main() { + flag.Parse() - addr := *addr - jid, _ := xmpp.ParseJID(*jid) - secret := *secret - // Create stream. - stream, err := xmpp.NewStream(addr, &xmpp.StreamConfig{LogStanzas: true}) - if err != nil { - log.Fatal(err) - } - - // Configure stream as a component connection. - x, err := xmpp.NewComponentXMPP(stream, jid, secret) - if err != nil { - log.Fatal(err) - } + // Create stream and configure it as a component connection. + jid := must(xmpp.ParseJID(*jid)).(xmpp.JID) + stream := must(xmpp.NewStream(*addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream) + x := must(xmpp.NewComponentXMPP(stream, jid, *secret)).(*xmpp.XMPP) for { v, err := x.Recv() @@ -38,3 +29,10 @@ func main() { log.Printf("recv: %v", v) } } + +func must(v interface{}, err error) interface{} { + if err != nil { + log.Fatal(err) + } + return v +}