package xmpp import ( "crypto/tls" "crypto/x509" "log" ) type SessionConnect struct{} type SessionDisconnect struct{} type SessionShouldDisconnect struct{} type session struct { in, out chan any jid string transport *tls.Conn } func StartSession(out chan any, jid string, pwd string) chan any { s := session{} s.in = make(chan any) s.out = out s.jid = jid go s.run() return s.in } func (s *session) run() { defer func() { s.out <- SessionDisconnect{} }() err := s.startTransport() if err != nil { return } defer s.transport.Close() s.out <- SessionConnect{} for { select { case data := <-s.in: switch data.(type) { case SessionShouldDisconnect: return default: log.Printf("Unknown data '%d'!\n", data) } } } } func (s *session) startTransport() error { domain := domainpart(s.jid) roots, err := x509.SystemCertPool() if err != nil { log.Println(err) return err } s.transport, err = tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) if err != nil { log.Println(err) return err } return nil }