structs.go
changeset 3 6121aa2f21b1
parent 2 4dabfef08c8c
child 5 faef59c8db05
--- a/structs.go	Sat Dec 24 09:55:26 2011 -0700
+++ b/structs.go	Sat Dec 24 11:05:54 2011 -0700
@@ -8,9 +8,11 @@
 
 import (
 	"bytes"
+	"flag"
 	"fmt"
 	"io"
 	"os"
+	"regexp"
 	"xml"
 )
 
@@ -31,6 +33,7 @@
 	Resource *string
 }
 var _ fmt.Stringer = &JID{}
+var _ flag.Value = &JID{}
 
 // XMPP's <stream:stream> XML element
 type Stream struct {
@@ -70,6 +73,26 @@
 	return result
 }
 
+func (jid *JID) Set(val string) bool {
+	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
+	parts := r.FindStringSubmatch(val)
+	if parts == nil {
+		return false
+	}
+	if parts[2] == "" {
+		jid.Node = nil
+	} else {
+		jid.Node = &parts[2]
+	}
+	jid.Domain = parts[3]
+	if parts[5] == "" {
+		jid.Resource = nil
+	} else {
+		jid.Resource = &parts[5]
+	}
+	return true
+}
+
 func (s *Stream) MarshalXML() ([]byte, os.Error) {
 	buf := bytes.NewBuffer(nil)
 	buf.WriteString("<stream:stream")