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

52
main.go
View File

@ -27,6 +27,7 @@ import (
type Client struct { type Client struct {
nick string nick string
user string user string
realname string
conn net.Conn conn net.Conn
host string host string
channels map[string]*Channel channels map[string]*Channel
@ -58,22 +59,26 @@ type Channelconfig struct {
Description string `json:"description"` Description string `json:"description"`
} }
type Config struct { type Config struct {
Port int `json:"port"` Port int `json:"port"`
Webapiport int `json:"webapiport"` Webapiport int `json:"webapiport"`
Motd string `json:"motd"` Motd string `json:"motd"`
Channels []Channelconfig `json:"channels"` Enableserverpasswd bool `json:"enableserverpassword"`
Ops []string `json:"ops"` Serverpassword string `json:"serverpassword"`
Channels []Channelconfig `json:"channels"`
Ops []string `json:"ops"`
} }
var ( var (
channels = make(map[string]*Channel) channels = make(map[string]*Channel)
clients = make(map[string]*Client) clients = make(map[string]*Client)
globalOps = make(map[string]bool) globalOps = make(map[string]bool)
chMu sync.Mutex // mutex for channels chMu sync.Mutex // mutex for channels
cliMu sync.RWMutex // mutex for clients cliMu sync.RWMutex // mutex for clients
motdString []string motdString []string
motdFile string motdFile string
db *sql.DB Enableserverpasswd bool
Serverpassword string
db *sql.DB
) )
func loadConfig() (*Config, error) { func loadConfig() (*Config, error) {
@ -94,6 +99,8 @@ func loadConfig() (*Config, error) {
globalOps[cleanedNick] = true globalOps[cleanedNick] = true
} }
motdFile = conf.Motd motdFile = conf.Motd
Enableserverpasswd = conf.Enableserverpasswd
Serverpassword = conf.Serverpassword
return &conf, nil return &conf, nil
} }
func loadMOTD(filename string) error { func loadMOTD(filename string) error {
@ -203,7 +210,23 @@ func handleConn(conn net.Conn) {
client.checkRegistration() 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": case "CAP":
if len(msg.Params) < 1 { if len(msg.Params) < 1 {
client.fwrite(":server 461 %s CAP :Not enough parameters\r\n", client.nick) 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 { if len(msg.Params) >= 4 {
uname := msg.Params[0] uname := msg.Params[0]
client.user = uname client.user = uname
client.realname = msg.Params[3]
client.checkRegistration() client.checkRegistration()
} else { } else {
fmt.Fprintf(conn, ":server 461 * USER :Not enough parameters\r\n") fmt.Fprintf(conn, ":server 461 * USER :Not enough parameters\r\n")