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

66 lines
1.0 KiB
Go

package models
import (
"regexp"
"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"
}
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
if jid.Local != "" {
str = jid.Local + "@" + str
}
return str
}
func (jid *JID) IsDomain() bool {
return jid != nil && jid.Local == "" && jid.Domain != ""
}
func GetJID(jidStr string) (jid *JID) {
jidS := ParseJID(jidStr)
err := database.Read.Where(jidS).First(jid).Error
if err != nil {
return nil
}
return
}
var jidRegex *regexp.Regexp
func init() {
jidRegex = regexp.MustCompile(`^(?:([^@/<>'\" ]+)@)?([^@/<>'\"]+)(?:/([^<>'\" ][^<>'\"]*))?$`)
database.AddModel(&JID{})
}