package main import ( "log" "crypto/tls" "crypto/x509" ) type XmppEvent uint8 const ( XmppDisconnect XmppEvent = iota XmppConnect ) func (l *Limox) xmpp(jid string, pwd string) { log.Printf("JID: '%s' PWD: '%s'\n", jid, pwd) domain := domainpart(jid) log.Printf("Domain: '%s'\n", domain) roots, err := x509.SystemCertPool() if err != nil { l.XmppEvents <- err return } conn, err := tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) if err != nil { l.XmppEvents <- err return } l.XmppEvents <- XmppConnect var closing bool = false for { ev := <-l.GuiEvents switch ev { case Disconnect: closing = true default: log.Fatalf("Unknown GuiEvent '%d'!\n", ev) } if closing { break } } conn.Close() l.XmppEvents <- XmppDisconnect } // domainpart extracts the domain name from a JID / XMPP address. See // https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. func domainpart(jid string) string { list := []rune(jid) for i, v := range list { if v == '/' { list = list[:i] break } } for i, v := range list { if v == '@' { list = list[i+1:] break } } return string(list) }