2016-01-04 18:26:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-01-11 20:04:57 +00:00
|
|
|
"crypto/tls"
|
2018-08-10 18:24:29 +00:00
|
|
|
"encoding/hex"
|
2019-01-25 10:02:31 +00:00
|
|
|
"fmt"
|
2016-01-14 04:56:53 +00:00
|
|
|
"net"
|
2016-01-11 20:04:57 +00:00
|
|
|
|
2018-05-31 21:24:59 +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
|
|
|
)
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func createNickInUseHandler(i *irc.Client, state *State) func(string) string {
|
2017-04-06 21:26:58 +00:00
|
|
|
return func(nick string) string {
|
|
|
|
newNick := nick + "_"
|
2017-07-02 23:07:55 +00:00
|
|
|
|
|
|
|
if newNick == i.GetNick() {
|
2018-05-31 21:24:59 +00:00
|
|
|
state.sendJSON("nick_fail", NickFail{
|
2017-07-02 23:07:55 +00:00
|
|
|
Server: i.Host,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:02:31 +00:00
|
|
|
state.sendJSON("error", IRCError{
|
|
|
|
Server: i.Host,
|
2020-05-23 00:30:48 +00:00
|
|
|
Message: fmt.Sprintf("Nickname %s is unavailable, trying %s instead", nick, newNick),
|
2019-01-25 10:02:31 +00:00
|
|
|
})
|
2017-04-06 21:26:58 +00:00
|
|
|
|
|
|
|
return newNick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:24:29 +00:00
|
|
|
func connectIRC(server *storage.Server, state *State, srcIP []byte) *irc.Client {
|
2017-07-02 01:31:00 +00:00
|
|
|
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"
|
2018-05-31 21:24:59 +00:00
|
|
|
i.HandleNickInUse = createNickInUseHandler(i, state)
|
2017-07-02 01:31:00 +00:00
|
|
|
|
|
|
|
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 {
|
2018-08-10 18:24:29 +00:00
|
|
|
i.Username = hex.EncodeToString(srcIP)
|
|
|
|
} else if i.Username == "" {
|
2018-05-18 01:14:22 +00:00
|
|
|
i.Username = server.Nick
|
|
|
|
}
|
2018-08-10 18:24:29 +00:00
|
|
|
|
2018-05-18 01:14:22 +00:00
|
|
|
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
|
2017-07-02 01:31:00 +00:00
|
|
|
} 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,
|
|
|
|
}
|
2017-07-02 01:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if i.TLS {
|
|
|
|
i.TLSConfig = &tls.Config{
|
2018-12-11 09:51:20 +00:00
|
|
|
InsecureSkipVerify: !cfg.VerifyCertificates,
|
2017-07-02 01:31:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
if cert := state.user.GetCertificate(); cert != nil {
|
2017-07-02 01:31:00 +00:00
|
|
|
i.TLSConfig.Certificates = []tls.Certificate{*cert}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
state.setIRC(server.Host, i)
|
2017-07-02 01:31:00 +00:00
|
|
|
i.Connect(address)
|
2018-05-31 21:24:59 +00:00
|
|
|
go newIRCHandler(i, state).run()
|
2017-07-02 01:31:00 +00:00
|
|
|
|
|
|
|
return i
|
|
|
|
}
|