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

10
room.go
View File

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