Cleaner nickname and channelname validation, not correlated together

This commit is contained in:
Sergey Matveev 2014-05-13 23:59:09 +04:00
parent 1331f317fb
commit 043730c355
2 changed files with 20 additions and 21 deletions

View File

@ -35,6 +35,10 @@ const (
ALIVENESS_CHECK = time.Second * 10 // Client's aliveness check period
)
var (
RE_NICKNAME = regexp.MustCompile("^[a-zA-Z0-9-]{1,9}$")
)
type Daemon struct {
hostname string
motd string
@ -153,19 +157,16 @@ func (daemon *Daemon) ClientRegister(client *Client, command string, cols []stri
client.ReplyParts("431", "No nickname given")
return
}
nickname := strings.ToLower(cols[1])
nickname_found := false
nickname := cols[1]
for client := range daemon.clients {
if client.nickname == nickname {
nickname_found = true
}
}
if nickname_found {
client.ReplyParts("433", "*", cols[1], "Nickname is already in use")
client.ReplyParts("433", "*", nickname, "Nickname is already in use")
return
}
if ok, _ := regexp.MatchString("^[^_-][_a-z0-9-]{1,50}$", nickname); !ok {
}
if !RE_NICKNAME.MatchString(nickname) {
client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
return
}
client.nickname = nickname
case "USER":
@ -213,8 +214,7 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
keys = []string{}
}
for n, room := range rooms {
room, valid := RoomNameSanitize(room)
if !valid {
if !RoomNameValid(room) {
client.ReplyNoChannel(room)
continue
}
@ -326,7 +326,7 @@ func (daemon *Daemon) Processor(events chan ClientEvent) {
}
continue
}
room, _ := RoomNameSanitize(cols[0])
room := cols[0]
r, found := daemon.rooms[room]
if !found {
client.ReplyNoChannel(room)
@ -345,7 +345,6 @@ func (daemon *Daemon) Processor(events chan ClientEvent) {
continue
}
for _, room := range strings.Split(cols[1], ",") {
room, _ = RoomNameSanitize(room)
r, found := daemon.rooms[room]
if !found {
client.ReplyNoChannel(room)
@ -382,7 +381,6 @@ func (daemon *Daemon) Processor(events chan ClientEvent) {
if msg != "" {
continue
}
target, _ = RoomNameSanitize(target)
r, found := daemon.rooms[target]
if !found {
client.ReplyNoNickChan(target)
@ -394,10 +392,9 @@ func (daemon *Daemon) Processor(events chan ClientEvent) {
continue
}
cols = strings.SplitN(cols[1], " ", 2)
room, _ := RoomNameSanitize(cols[0])
r, found := daemon.rooms[room]
r, found := daemon.rooms[cols[0]]
if !found {
client.ReplyNoChannel(room)
client.ReplyNoChannel(cols[0])
}
var change string
if len(cols) > 1 {
@ -411,7 +408,7 @@ func (daemon *Daemon) Processor(events chan ClientEvent) {
client.ReplyNotEnoughParameters("WHO")
continue
}
room, _ := RoomNameSanitize(strings.Split(cols[1], " ")[0])
room := strings.Split(cols[1], " ")[0]
r, found := daemon.rooms[room]
if !found {
client.ReplyNoChannel(room)

10
room.go
View File

@ -25,12 +25,14 @@ import (
"strings"
)
var (
RE_ROOM = regexp.MustCompile("^#[^\x00\x07\x0a\x0d ,:/]{1,200}$")
)
// Sanitize room's name. It can consist of 1 to 50 ASCII symbols
// with some exclusions. All room names will have "#" prefix.
func RoomNameSanitize(name string) (n string, valid bool) {
n = strings.TrimLeft(strings.ToLower(name), "&#+!")
valid, _ = regexp.MatchString("^[^\x00\x07\x0a\x0d ,:/]{1,50}$", n)
return "#" + n, valid
func RoomNameValid(name string) bool {
return RE_ROOM.MatchString(name)
}
type Room struct {