grim/gobnc
Clone
Summary
Browse
Changes
Graph
Update for the new hosting path
draft
default
tip
2020-04-27, Gary Kramlich
7cd26371cd9c
Update for the new hosting path
package
irc
import
(
"errors"
"regexp"
"strings"
)
var
(
ErrMalformedMessage
=
errors
.
New
(
"malformed message"
)
ErrInvalidNick
=
errors
.
New
(
"invalid nick"
)
regexNick
=
regexp
.
MustCompile
(
`^[A-Za-z][A-Za-z0-9-[\]\^{}]*`
)
)
type
Unmarshaler
interface
{
UnmarshalIRC
([]
byte
)
error
}
type
Marshaller
interface
{
MarshalIRC
()
([]
byte
,
error
)
}
// Message represents a message in the IRC protocol.
type
Message
string
func
(
m
*
Message
)
UnmarshalIRC
(
b
[]
byte
)
error
{
s
:=
string
(
b
)
if
!
strings
.
HasPrefix
(
s
,
":"
)
{
return
ErrMalformedMessage
}
*
m
=
Message
(
s
[
1
:])
return
nil
}
func
(
m
*
Message
)
MarshalIRC
()
([]
byte
,
error
)
{
return
[]
byte
(
":"
+
*
m
),
nil
}
// Nick represents a nick in the IRC protocol.
type
Nick
string
func
(
n
*
Nick
)
UnmarshalIRC
(
b
[]
byte
)
error
{
if
!
regexNick
.
Match
(
b
)
{
return
ErrInvalidNick
}
*
n
=
Nick
(
string
(
b
))
return
nil
}
func
(
n
*
Nick
)
MarshalNick
()
([]
byte
,
error
)
{
if
!
n
.
Valid
()
{
return
nil
,
ErrInvalidNick
}
return
[]
byte
(
*
n
),
nil
}
func
(
n
*
Nick
)
Valid
()
bool
{
return
regexNick
.
MatchString
(
string
(
*
n
))
}