-
Notifications
You must be signed in to change notification settings - Fork 2
/
addr_spec.go
67 lines (60 loc) · 1.42 KB
/
addr_spec.go
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
package main
import (
"errors"
"io"
"strings"
)
type AddrSpec struct {
sipURI *SIPURI
absoluteURI *AbsoluteURI
}
func NewAddrSpec() *AddrSpec {
return &AddrSpec{sipURI: nil, absoluteURI: nil}
}
func ParseAddrSpec(addrSpec string) (*AddrSpec, error) {
if strings.HasPrefix(addrSpec, "sip:") || strings.HasPrefix(addrSpec, "sips:") {
sipUri, err := ParseSipURI(addrSpec)
if err == nil {
return &AddrSpec{sipURI: sipUri, absoluteURI: nil}, nil
} else {
return nil, err
}
} else {
absoluteURI, err := ParseAbsoluteURI(addrSpec)
if err != nil {
return nil, err
}
return &AddrSpec{sipURI: nil, absoluteURI: absoluteURI}, nil
}
}
func (as *AddrSpec) IsSIPURI() bool {
return as.sipURI != nil
}
func (as *AddrSpec) GetSIPURI() (*SIPURI, error) {
if as.sipURI == nil {
return nil, errors.New("addr-spec is not SIP URI")
}
return as.sipURI, nil
}
func (as *AddrSpec) GetAbsoluteURI() (*AbsoluteURI, error) {
if as.absoluteURI == nil {
return nil, errors.New("addr-spec is not absoluteURI")
}
return as.absoluteURI, nil
}
func (as *AddrSpec) Write(writer io.Writer) (int, error) {
if as.sipURI != nil {
return as.sipURI.Write(writer)
} else if as.absoluteURI != nil {
return as.absoluteURI.Writer(writer)
}
return 0, nil
}
func (as *AddrSpec) String() string {
if as.sipURI != nil {
return as.sipURI.String()
} else if as.absoluteURI != nil {
return as.absoluteURI.String()
}
return ""
}