dispatch/server/irc.go

79 lines
1.7 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-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{
2020-05-23 07:42:20 +00:00
Server: i.Host(),
})
}
state.sendJSON("error", IRCError{
2020-05-23 07:42:20 +00:00
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 {
2018-12-11 09:51:20 +00:00
cfg := state.srv.Config()
2020-05-23 07:42:20 +00:00
ircCfg := irc.Config{
Host: server.Host,
Port: server.Port,
TLS: server.TLS,
Nick: server.Nick,
Username: server.Username,
Realname: server.Realname,
2020-06-04 00:28:41 +00:00
Account: server.Account,
Password: server.Password,
2020-05-23 07:42:20 +00:00
Version: fmt.Sprintf("Dispatch %s (git: %s)", version.Tag, version.Commit),
Source: "https://github.com/khlieng/dispatch",
}
2020-05-23 07:42:20 +00:00
if server.TLS {
ircCfg.TLSConfig = &tls.Config{
InsecureSkipVerify: !cfg.VerifyCertificates,
}
if cert := state.user.GetCertificate(); cert != nil {
ircCfg.TLSConfig.Certificates = []tls.Certificate{*cert}
}
}
2020-05-23 07:42:20 +00:00
if cfg.HexIP {
ircCfg.Username = hex.EncodeToString(srcIP)
2020-05-23 06:05:37 +00:00
}
2020-05-23 07:42:20 +00:00
if server.ServerPassword == "" &&
cfg.Defaults.ServerPassword != "" &&
server.Host == cfg.Defaults.Host {
2020-06-04 00:28:41 +00:00
ircCfg.ServerPassword = cfg.Defaults.ServerPassword
2020-05-23 07:42:20 +00:00
} else {
2020-06-04 00:28:41 +00:00
ircCfg.ServerPassword = server.ServerPassword
}
i := irc.NewClient(&ircCfg)
2020-05-23 07:42:20 +00:00
i.Config.HandleNickInUse = createNickInUseHandler(i, state)
state.setIRC(server.Host, i)
2020-05-23 07:42:20 +00:00
i.Connect()
go newIRCHandler(i, state).run()
return i
}