This repository was archived by the owner on Feb 9, 2020. It is now read-only.
forked from sheenobu/go-xco
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddress.go
More file actions
133 lines (102 loc) · 2.6 KB
/
address.go
File metadata and controls
133 lines (102 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package xco
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"strings"
)
// Address is an XMPP JID address
type Address struct {
LocalPart string
DomainPart string
ResourcePart string
}
// ParseAddress parses the address from the given string
func ParseAddress(s string) (Address, error) {
var addr Address
err := addr.parse(s)
return addr, err
}
// Equals compares the given address
func (a *Address) Equals(o *Address) bool {
return (a == o) || ((a != nil && o != nil) && (a.LocalPart == o.LocalPart && a.DomainPart == o.DomainPart && a.ResourcePart == o.ResourcePart))
}
// String formats the address as an XMPP JID
func (a *Address) String() string {
buf := bytes.NewBufferString("")
if a.LocalPart != "" {
buf.WriteString(a.LocalPart)
buf.WriteString("@")
}
buf.WriteString(a.DomainPart)
if a.ResourcePart != "" {
buf.WriteString("/")
buf.WriteString(a.ResourcePart)
}
return buf.String()
}
// Bare returns a copy of this address with the resource part made
// blank.
func (a *Address) Bare() *Address {
b := *a
b.ResourcePart = ""
return &b
}
// UnmarshalXMLAttr marks the Address struct as being able to be parsed as an XML attribute
func (a *Address) UnmarshalXMLAttr(attr xml.Attr) error {
return a.parse(attr.Value)
}
// MarshalXMLAttr marks the Address struct as being able to be written as an XML attribute
func (a *Address) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
errs := a.validate()
if len(errs) != 0 {
return xml.Attr{}, fmt.Errorf("Malformed Address for Attribute %s: %s", name, errs)
}
return xml.Attr{
Name: name,
Value: a.String(),
}, nil
}
func (a *Address) validate() []error {
var errs []error
if a.DomainPart == "" {
errs = append(errs, errors.New("Domain is empty"))
}
return errs
}
func (a *Address) parse(s string) error {
// normalization
s = strings.TrimSpace(s)
if len(s) == 0 {
return errors.New("Address is empty")
}
// parsing
domainStart := 0
domainEnd := len(s)
if idx := strings.IndexAny(s, "@"); idx != -1 {
a.LocalPart = s[0:idx]
domainStart = idx + 1
}
if idx := strings.IndexAny(s, "/"); idx != -1 {
a.ResourcePart = s[idx+1:]
domainEnd = idx
}
if domainStart != domainEnd {
a.DomainPart = s[domainStart:domainEnd]
}
// validation
errs := a.validate()
if a.LocalPart == "" && domainStart != 0 {
errs = append(errs, errors.New("Localpart is empty"))
}
if a.ResourcePart == "" && domainEnd != len(s) {
errs = append(errs, errors.New("Resourcepart is empty"))
}
if len(errs) == 1 {
return errs[0]
} else if len(errs) > 1 {
return fmt.Errorf("Multiple errors: %v", errs)
}
return nil
}