-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
62 lines (54 loc) · 1.48 KB
/
common.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
package main
import (
"os"
"strings"
"time"
"encoding/xml"
"github.com/shizeeg/xmpp"
)
// SplitJID returns barejid and resource from FullJID
// if jid have no /resource part it returns jid and empty string
func SplitJID(jid string) (barejid, resource string) {
if tmp := strings.SplitN(jid, "/", 2); len(tmp) == 2 {
return tmp[0], tmp[1]
}
return jid, ""
}
// IsValidJID checks if JID is valid
// fullJid == true adds additional check for /resource
func IsValidJID(jid string) bool {
if len(jid) < 5 { // a@b.c
return false
}
atpos := strings.Index(jid, "@")
dotpos := strings.Index(jid, ".")
// slashpos := strings.Index(jid, "/")
// fmt.Println(atpos, dotpos, slashpos)
if atpos > 0 && dotpos > 2 && len(jid) > strings.Index(jid, ".")+1 {
return true
}
return false
}
// GetTimeDate return timezone (tzo) and time (utc) as defined in XEP-0202
// http://xmpp.org/extensions/xep-0202.html
func GetTimeDate() (tzo, utc string) {
now := time.Now()
utc = now.UTC().Format(xmpp.TimeTZ)
tzo = now.Format("-07:00")
return
}
// GetTimeDateOld returns timezone, datetime and display suggestion as defined in
// XEP-0090 http://xmpp.org/extensions/xep-0090.html
func GetTimeDateOld() (tz, utc, display string) {
now := time.Now()
utc = now.UTC().Format(xmpp.TimeOld)
display = now.Format(time.RubyDate)
tz, _ = now.Zone()
return
}
// PrintXML dumps xml to stderr
func PrintXML(s interface{}) error {
enc := xml.NewEncoder(os.Stderr)
enc.Indent("", " ")
return enc.Encode(s)
}