From 49ae78f98bf67d88b548c41225cc87aeba1975f5 Mon Sep 17 00:00:00 2001 From: meera Date: Sat, 18 Jul 2026 20:23:29 +0300 Subject: [PATCH] added server password, and flag in config to enable it --- main.go | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 3c1bcbf..30cd515 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ import ( type Client struct { nick string user string + realname string conn net.Conn host string channels map[string]*Channel @@ -58,22 +59,26 @@ type Channelconfig struct { Description string `json:"description"` } type Config struct { - Port int `json:"port"` - Webapiport int `json:"webapiport"` - Motd string `json:"motd"` - Channels []Channelconfig `json:"channels"` - Ops []string `json:"ops"` + Port int `json:"port"` + Webapiport int `json:"webapiport"` + Motd string `json:"motd"` + Enableserverpasswd bool `json:"enableserverpassword"` + Serverpassword string `json:"serverpassword"` + Channels []Channelconfig `json:"channels"` + Ops []string `json:"ops"` } var ( - channels = make(map[string]*Channel) - clients = make(map[string]*Client) - globalOps = make(map[string]bool) - chMu sync.Mutex // mutex for channels - cliMu sync.RWMutex // mutex for clients - motdString []string - motdFile string - db *sql.DB + channels = make(map[string]*Channel) + clients = make(map[string]*Client) + globalOps = make(map[string]bool) + chMu sync.Mutex // mutex for channels + cliMu sync.RWMutex // mutex for clients + motdString []string + motdFile string + Enableserverpasswd bool + Serverpassword string + db *sql.DB ) func loadConfig() (*Config, error) { @@ -94,6 +99,8 @@ func loadConfig() (*Config, error) { globalOps[cleanedNick] = true } motdFile = conf.Motd + Enableserverpasswd = conf.Enableserverpasswd + Serverpassword = conf.Serverpassword return &conf, nil } func loadMOTD(filename string) error { @@ -203,7 +210,23 @@ func handleConn(conn net.Conn) { client.checkRegistration() } } - + case "PASS": + if !Enableserverpasswd { + client.fwrite(":server 421 * PASS :Server password not enabled\r\n") + continue + } + if len(msg.Params) < 1 { + client.fwrite(":server 461 * PASS :Not enough parameters\r\n") + continue + } + if msg.Params[0] == Serverpassword { + client.saslComplete = true + client.saslRequired = false + client.fwrite(":server NOTICE * :Server password accepted\r\n") + } else { + client.fwrite(":server 464 * :Password incorrect\r\n") + return + } case "CAP": if len(msg.Params) < 1 { client.fwrite(":server 461 %s CAP :Not enough parameters\r\n", client.nick) @@ -271,6 +294,7 @@ func handleConn(conn net.Conn) { if len(msg.Params) >= 4 { uname := msg.Params[0] client.user = uname + client.realname = msg.Params[3] client.checkRegistration() } else { fmt.Fprintf(conn, ":server 461 * USER :Not enough parameters\r\n")