dispatch/server/irc.go

66 lines
1.4 KiB
Go
Raw Normal View History

2016-01-04 18:26:32 +00:00
package server
import (
2016-01-11 20:04:57 +00:00
"crypto/tls"
2016-01-14 04:56:53 +00:00
"net"
2016-01-11 20:04:57 +00:00
2016-03-01 00:51:26 +00:00
"github.com/spf13/viper"
2016-01-25 00:01:37 +00:00
2016-01-04 18:26:32 +00:00
"github.com/khlieng/dispatch/irc"
"github.com/khlieng/dispatch/storage"
)
func createNickInUseHandler(i *irc.Client, session *Session) func(string) string {
return func(nick string) string {
newNick := nick + "_"
session.print(i.Host, "Nickname", nick, "is already in use, using", newNick, "instead")
go session.user.SetNick(newNick, i.Host)
return newNick
}
}
2016-01-04 18:26:32 +00:00
func reconnectIRC() {
for _, user := range storage.LoadUsers() {
session := NewSession(user)
sessions.set(user.ID, session)
go session.run()
2016-01-04 18:26:32 +00:00
channels := user.GetChannels()
for _, server := range user.GetServers() {
i := irc.NewClient(server.Nick, server.Username)
i.TLS = server.TLS
i.Password = server.Password
i.Realname = server.Realname
i.HandleNickInUse = createNickInUseHandler(i, session)
2016-01-04 18:26:32 +00:00
2016-01-11 23:32:40 +00:00
if cert := user.GetCertificate(); cert != nil {
2016-01-11 20:04:57 +00:00
i.TLSConfig = &tls.Config{
2016-01-25 00:01:37 +00:00
Certificates: []tls.Certificate{*cert},
InsecureSkipVerify: !viper.GetBool("verify_client_certificates"),
2016-01-11 20:04:57 +00:00
}
}
2016-01-14 04:56:53 +00:00
session.setIRC(server.Host, i)
if server.Port != "" {
i.Connect(net.JoinHostPort(server.Host, server.Port))
} else {
i.Connect(server.Host)
}
2016-01-04 18:26:32 +00:00
go newIRCHandler(i, session).run()
var joining []string
for _, channel := range channels {
2016-01-14 04:56:53 +00:00
if channel.Server == server.Host {
2016-01-04 18:26:32 +00:00
joining = append(joining, channel.Name)
}
}
i.Join(joining...)
}
}
}