2017-10-02 14:38:52 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var jidRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
jidRegex = regexp.MustCompile(`^(?:([^@/<>'\" ]+)@)?([^@/<>'\"]+)(?:/([^<>'\" ][^<>'\"]*))?$`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JID struct
|
|
|
|
type JID struct {
|
|
|
|
Local string
|
|
|
|
Domain string
|
|
|
|
Resource string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJID get JID from string
|
|
|
|
func NewJID(jidString string) *JID {
|
|
|
|
jidSplitTmp := jidRegex.FindAllStringSubmatch(jidString, -1)
|
|
|
|
if len(jidSplitTmp) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
jidSplit := jidSplitTmp[0]
|
|
|
|
|
|
|
|
return &JID{
|
|
|
|
Local: jidSplit[1],
|
|
|
|
Domain: jidSplit[2],
|
|
|
|
Resource: jidSplit[3],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bare get the "bare" jid
|
|
|
|
func (jid *JID) Bare() string {
|
|
|
|
if jid.Local != "" {
|
|
|
|
return jid.Local + "@" + jid.Domain
|
|
|
|
}
|
|
|
|
return jid.Domain
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:30:07 +01:00
|
|
|
func (jid *JID) String() string { return jid.Bare() }
|
|
|
|
|
2017-10-02 14:38:52 +02:00
|
|
|
// Full get the "full" jid as string
|
|
|
|
func (jid *JID) Full() string {
|
|
|
|
if jid.Resource != "" {
|
|
|
|
return jid.Bare() + "/" + jid.Resource
|
|
|
|
}
|
|
|
|
return jid.Bare()
|
|
|
|
}
|
|
|
|
|
2017-10-10 00:54:14 +02:00
|
|
|
//MarshalTOML to bytearray
|
|
|
|
func (jid JID) MarshalTOML() ([]byte, error) {
|
2017-10-02 14:38:52 +02:00
|
|
|
return []byte(jid.Full()), nil
|
|
|
|
}
|
|
|
|
|
2017-10-10 00:54:14 +02:00
|
|
|
// UnmarshalTOML from bytearray
|
|
|
|
func (jid *JID) UnmarshalTOML(data []byte) (err error) {
|
2017-10-02 14:38:52 +02:00
|
|
|
newJID := NewJID(string(data))
|
|
|
|
if newJID == nil {
|
|
|
|
return errors.New("not a valid jid")
|
|
|
|
}
|
|
|
|
jid.Local = newJID.Local
|
|
|
|
jid.Domain = newJID.Domain
|
|
|
|
jid.Resource = newJID.Resource
|
|
|
|
return nil
|
|
|
|
}
|