dispatch/server/irc.go

89 lines
1.9 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"
2020-05-20 02:19:40 +00:00
"github.com/khlieng/dispatch/version"
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 unavailable, trying %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
2020-05-20 02:19:40 +00:00
i.Version = fmt.Sprintf("Dispatch %s (git: %s)", version.Tag, version.Commit)
i.Source = "https://github.com/khlieng/dispatch"
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
}
2020-05-23 06:05:37 +00:00
if server.ServerPassword == "" &&
cfg.Defaults.ServerPassword != "" &&
2018-12-11 09:51:20 +00:00
address == cfg.Defaults.Host {
2020-05-23 06:05:37 +00:00
i.Password = cfg.Defaults.ServerPassword
} else {
2020-05-23 06:05:37 +00:00
i.Password = server.ServerPassword
}
if server.Account != "" && server.Password != "" {
i.SASL = &irc.SASLPlain{
Username: server.Account,
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}
}
}
state.setIRC(server.Host, i)
i.Connect(address)
go newIRCHandler(i, state).run()
return i
}