2016-01-04 18:26:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-01-11 20:04:57 +00:00
|
|
|
"crypto/tls"
|
2018-04-26 19:32:21 +00:00
|
|
|
"log"
|
2016-01-14 04:56:53 +00:00
|
|
|
"net"
|
2016-01-11 20:04:57 +00:00
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
"github.com/khlieng/dispatch/irc"
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2017-06-30 05:20:38 +00:00
|
|
|
"github.com/spf13/viper"
|
2016-01-04 18:26:32 +00:00
|
|
|
)
|
|
|
|
|
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 + "_"
|
2017-07-02 23:07:55 +00:00
|
|
|
|
|
|
|
if newNick == i.GetNick() {
|
|
|
|
session.sendJSON("nick_fail", NickFail{
|
|
|
|
Server: i.Host,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-28 22:42:00 +00:00
|
|
|
session.printError("Nickname", nick, "is already in use, using", newNick, "instead")
|
2017-04-06 21:26:58 +00:00
|
|
|
|
|
|
|
return newNick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
func reconnectIRC() {
|
|
|
|
for _, user := range storage.LoadUsers() {
|
2018-04-26 19:32:21 +00:00
|
|
|
session, err := NewSession(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sessions.set(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() {
|
2017-07-02 01:31:00 +00:00
|
|
|
i := connectIRC(server, session)
|
2016-01-04 18:26:32 +00:00
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-02 01:31:00 +00:00
|
|
|
|
|
|
|
func connectIRC(server storage.Server, session *Session) *irc.Client {
|
|
|
|
i := irc.NewClient(server.Nick, server.Username)
|
|
|
|
i.TLS = server.TLS
|
|
|
|
i.Realname = server.Realname
|
|
|
|
i.HandleNickInUse = createNickInUseHandler(i, session)
|
|
|
|
|
|
|
|
address := server.Host
|
|
|
|
if server.Port != "" {
|
|
|
|
address = net.JoinHostPort(server.Host, server.Port)
|
|
|
|
}
|
|
|
|
|
2018-05-18 01:14:22 +00:00
|
|
|
if i.Username == "" {
|
|
|
|
i.Username = server.Nick
|
|
|
|
}
|
|
|
|
if i.Realname == "" {
|
|
|
|
i.Realname = server.Nick
|
|
|
|
}
|
|
|
|
|
2017-07-02 01:31:00 +00:00
|
|
|
if server.Password == "" &&
|
|
|
|
viper.GetString("defaults.password") != "" &&
|
2018-05-18 01:37:33 +00:00
|
|
|
address == viper.GetString("defaults.host") {
|
2017-07-02 01:31:00 +00:00
|
|
|
i.Password = viper.GetString("defaults.password")
|
|
|
|
} else {
|
|
|
|
i.Password = server.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.TLS {
|
|
|
|
i.TLSConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: !viper.GetBool("verify_certificates"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert := session.user.GetCertificate(); cert != nil {
|
|
|
|
i.TLSConfig.Certificates = []tls.Certificate{*cert}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session.setIRC(server.Host, i)
|
|
|
|
i.Connect(address)
|
|
|
|
go newIRCHandler(i, session).run()
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|