This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/models/jid.go

57 lines
904 B
Go
Raw Normal View History

2019-05-31 10:07:40 +02:00
package models
import (
2019-05-31 18:26:15 +02:00
"regexp"
2019-05-31 10:07:40 +02:00
"github.com/jinzhu/gorm"
"dev.sum7.eu/genofire/golang-lib/database"
)
type JID struct {
gorm.Model
Local string
Domain string
}
func (j *JID) TableName() string {
return "jid"
}
2019-05-31 18:26:15 +02:00
func ParseJID(jidString string) (jid *JID) {
jidSplitTmp := jidRegex.FindAllStringSubmatch(jidString, -1)
if len(jidSplitTmp) != 1 {
return nil
}
jidSplit := jidSplitTmp[0]
return &JID{
Local: jidSplit[1],
Domain: jidSplit[2],
}
}
func (jid *JID) String() string {
if jid == nil {
return ""
}
str := jid.Domain
2019-06-01 02:59:00 +02:00
if str != "" && jid.Local != "" {
2019-05-31 18:26:15 +02:00
str = jid.Local + "@" + str
}
return str
}
func (jid *JID) IsDomain() bool {
return jid != nil && jid.Local == "" && jid.Domain != ""
}
var jidRegex *regexp.Regexp
2019-05-31 10:07:40 +02:00
func init() {
2019-05-31 18:26:15 +02:00
jidRegex = regexp.MustCompile(`^(?:([^@/<>'\" ]+)@)?([^@/<>'\"]+)(?:/([^<>'\" ][^<>'\"]*))?$`)
2019-05-31 10:07:40 +02:00
database.AddModel(&JID{})
}