structs.go
changeset 3 6121aa2f21b1
parent 2 4dabfef08c8c
child 5 faef59c8db05
equal deleted inserted replaced
2:4dabfef08c8c 3:6121aa2f21b1
     6 
     6 
     7 // This file contains data structures.
     7 // This file contains data structures.
     8 
     8 
     9 import (
     9 import (
    10 	"bytes"
    10 	"bytes"
       
    11 	"flag"
    11 	"fmt"
    12 	"fmt"
    12 	"io"
    13 	"io"
    13 	"os"
    14 	"os"
       
    15 	"regexp"
    14 	"xml"
    16 	"xml"
    15 )
    17 )
    16 
    18 
    17 const (
    19 const (
    18 	// Version of RFC 3920 that we implement.
    20 	// Version of RFC 3920 that we implement.
    29 	Node *string
    31 	Node *string
    30 	Domain string
    32 	Domain string
    31 	Resource *string
    33 	Resource *string
    32 }
    34 }
    33 var _ fmt.Stringer = &JID{}
    35 var _ fmt.Stringer = &JID{}
       
    36 var _ flag.Value = &JID{}
    34 
    37 
    35 // XMPP's <stream:stream> XML element
    38 // XMPP's <stream:stream> XML element
    36 type Stream struct {
    39 type Stream struct {
    37 	to string `xml:"attr"`
    40 	to string `xml:"attr"`
    38 	from string `xml:"attr"`
    41 	from string `xml:"attr"`
    66 	}
    69 	}
    67 	if jid.Resource != nil {
    70 	if jid.Resource != nil {
    68 		result = result + "/" + *jid.Resource
    71 		result = result + "/" + *jid.Resource
    69 	}
    72 	}
    70 	return result
    73 	return result
       
    74 }
       
    75 
       
    76 func (jid *JID) Set(val string) bool {
       
    77 	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
       
    78 	parts := r.FindStringSubmatch(val)
       
    79 	if parts == nil {
       
    80 		return false
       
    81 	}
       
    82 	if parts[2] == "" {
       
    83 		jid.Node = nil
       
    84 	} else {
       
    85 		jid.Node = &parts[2]
       
    86 	}
       
    87 	jid.Domain = parts[3]
       
    88 	if parts[5] == "" {
       
    89 		jid.Resource = nil
       
    90 	} else {
       
    91 		jid.Resource = &parts[5]
       
    92 	}
       
    93 	return true
    71 }
    94 }
    72 
    95 
    73 func (s *Stream) MarshalXML() ([]byte, os.Error) {
    96 func (s *Stream) MarshalXML() ([]byte, os.Error) {
    74 	buf := bytes.NewBuffer(nil)
    97 	buf := bytes.NewBuffer(nil)
    75 	buf.WriteString("<stream:stream")
    98 	buf.WriteString("<stream:stream")