dispatch/server/irc.go

82 lines
1.6 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"
"encoding/hex"
"fmt"
2016-01-14 04:56:53 +00:00
"net"
2016-01-11 20:04:57 +00:00
"github.com/khlieng/dispatch/pkg/irc"
"github.com/khlieng/dispatch/storage"
2016-01-04 18:26:32 +00:00
)
func createNickInUseHandler(i *irc.Client, state *State) func(string) string {
return func(nick string) string {
newNick := nick + "_"
if newNick == i.GetNick() {
state.sendJSON("nick_fail", NickFail{
Server: i.Host,
})
}
state.sendJSON("error", IRCError{
Server: i.Host,
Message: fmt.Sprintf("Nickname %s is already in use, using %s instead", nick, newNick),
})
return newNick
}
}
func connectIRC(server *storage.Server, state *State, srcIP []byte) *irc.Client {
i := irc.NewClient(server.Nick, server.Username)
i.TLS = server.TLS
i.Realname = server.Realname
i.HandleNickInUse = createNickInUseHandler(i, state)
address := server.Host
if server.Port != "" {
address = net.JoinHostPort(server.Host, server.Port)
}
2018-12-11 09:51:20 +00:00
cfg := state.srv.Config()
if cfg.HexIP {
i.Username = hex.EncodeToString(srcIP)
} else if i.Username == "" {
i.Username = server.Nick
}
if i.Realname == "" {
i.Realname = server.Nick
}
if server.Password == "" &&
2018-12-11 09:51:20 +00:00
cfg.Defaults.Password != "" &&
address == cfg.Defaults.Host {
i.Password = cfg.Defaults.Password
} else {
i.Password = server.Password
}
if i.TLS {
i.TLSConfig = &tls.Config{
2018-12-11 09:51:20 +00:00
InsecureSkipVerify: !cfg.VerifyCertificates,
}
if cert := state.user.GetCertificate(); cert != nil {
i.TLSConfig.Certificates = []tls.Certificate{*cert}
}
}
i.DownloadFolder = cfg.DownloadFolder
i.Autoget = cfg.Autoget
state.setIRC(server.Host, i)
i.Connect(address)
go newIRCHandler(i, state).run()
return i
}