implement IsValidNick()

This commit is contained in:
Liam Stanley 2016-11-18 15:17:09 -05:00
parent b37fa0e70d
commit 7694378be0
4 changed files with 83 additions and 25 deletions

@ -27,13 +27,15 @@
- [ ] client should support ping tracking (sending `PING`'s to the server)
- [ ] users need to be exposed in state somehow (other than `GetChannels()`)
- [ ] ip/host binding?
- [ ] `IsValidNick(nick string)`?
- [ ] `User.Age()`? (`FirstActive()`?) (time since first seen)
- [ ] cleanup docs in conn.go & event.go
- [ ] add `DISCONNECTED` command state
- [ ] add `Client.IsInChannel()`? and/or basic channel list
- [ ] add `Client.Topic(topic string)`
- [ ] `MODE` tracking on a per-channel basis
- [ ] `Event.PrettyString()`?
- [ ] `Client.AddTmpCallback()` for one time use callbacks?
- [ ] implement id's for callbacks, for allowance of removal?
## Installing

@ -292,27 +292,3 @@ func (e *Event) String() string {
// contains '*', even though this isn't RFC compliant, it's commonly used
var validChannelPrefixes = [...]string{"&", "#", "+", "!", "*"}
// IsValidChannel checks if channel is an RFC complaint channel or not
func IsValidChannel(channel string) bool {
if len(channel) <= 1 || len(channel) > 50 {
return false
}
var validprefix bool
for i := 0; i < len(validChannelPrefixes); i++ {
if string(channel[0]) == validChannelPrefixes[i] {
validprefix = true
break
}
}
if !validprefix {
return false
}
if strings.Contains(channel, " ") || strings.Contains(channel, ",") {
return false
}
return true
}

49
main.go

@ -23,6 +23,7 @@ import (
"io/ioutil"
"log"
"net"
"strings"
"time"
)
@ -414,3 +415,51 @@ func (c *Client) SendRaw(raw string) {
func (c *Client) SendRawf(format string, a ...interface{}) {
c.SendRaw(fmt.Sprintf(format, a...))
}
// IsValidChannel checks if channel is an RFC complaint channel or not
func IsValidChannel(channel string) bool {
if len(channel) <= 1 || len(channel) > 50 {
return false
}
var validprefix bool
for i := 0; i < len(validChannelPrefixes); i++ {
if string(channel[0]) == validChannelPrefixes[i] {
validprefix = true
break
}
}
if !validprefix {
return false
}
if strings.Contains(channel, " ") || strings.Contains(channel, ",") {
return false
}
return true
}
// IsValidNick valids an IRC nickame. Note that this does not valid IRC
// nickname length.
func IsValidNick(nick string) bool {
if len(nick) <= 0 {
return false
}
// Check the first index. Some characters aren't allowed for the first
// index of an IRC nickname.
if nick[0] < 0x41 || nick[0] > 0x7D {
// a-z, A-Z, and _\[]{}^|
return false
}
for i := 1; i < len(nick); i++ {
if (nick[i] < 0x41 || nick[i] > 0x7D) && (nick[i] < 0x30 || nick[i] > 0x39) && nick[i] != 0x2D {
// a-z, A-Z, 0-9, -, and _\[]{}^|
return false
}
}
return true
}

31
main_test.go Normal file

@ -0,0 +1,31 @@
package girc
import "testing"
func TestIsValidNick(t *testing.T) {
type args struct {
nick string
}
tests := []struct {
name string
args args
want bool
}{
{name: "normal", args: args{nick: "test"}, want: true},
{name: "empty", args: args{nick: ""}, want: false},
{name: "hyphen and special", args: args{nick: "test[-]"}, want: true},
{name: "invalid middle", args: args{nick: "test!test"}, want: false},
{name: "invalid dot middle", args: args{nick: "test.test"}, want: false},
{name: "end", args: args{nick: "test!"}, want: false},
{name: "invalid start", args: args{nick: "!test"}, want: false},
{name: "backslash and numeric", args: args{nick: "test[\\0"}, want: true},
{name: "long", args: args{nick: "test123456789AZBKASDLASMDLKM"}, want: true},
{name: "index 0 dash", args: args{nick: "-test"}, want: false},
{name: "index 0 numeric", args: args{nick: "0test"}, want: false},
}
for _, tt := range tests {
if got := IsValidNick(tt.args.nick); got != tt.want {
t.Errorf("%q. IsValidNick() = %v, want %v", tt.name, got, tt.want)
}
}
}