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"
|
|
|
|
)
|
|
|
|
|
2017-04-06 21:26:58 +00:00
|
|
|
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() {
|
2016-01-15 01:27:30 +00:00
|
|
|
session := NewSession(user)
|
2016-01-25 05:01:40 +00:00
|
|
|
sessions.set(user.ID, session)
|
2016-01-19 21:02:12 +00:00
|
|
|
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
|
2017-04-06 21:26:58 +00:00
|
|
|
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)
|
2016-01-15 01:27:30 +00:00
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|