added server password, and flag in config to enable it

This commit is contained in:
meera 2026-07-18 20:23:29 +03:00
parent d0b882f362
commit 49ae78f98b

26
main.go
View File

@ -27,6 +27,7 @@ import (
type Client struct {
nick string
user string
realname string
conn net.Conn
host string
channels map[string]*Channel
@ -61,6 +62,8 @@ type Config struct {
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"`
}
@ -73,6 +76,8 @@ var (
cliMu sync.RWMutex // mutex for clients
motdString []string
motdFile string
Enableserverpasswd bool
Serverpassword string
db *sql.DB
)
@ -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")