2014-03-06 22:10:54 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"log"
|
2014-03-07 01:51:33 +01:00
|
|
|
"regexp"
|
2014-03-06 22:10:54 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-03-07 01:51:33 +01:00
|
|
|
var (
|
|
|
|
ErrNickMissing = errors.New("nick missing")
|
|
|
|
ErrNicknameInUse = errors.New("nickname in use")
|
|
|
|
ErrNicknameMismatch = errors.New("nickname mismatch")
|
|
|
|
wildMaskExpr = regexp.MustCompile(`\*|\?`)
|
|
|
|
likeQuoter = strings.NewReplacer(
|
|
|
|
`\`, `\\`,
|
|
|
|
`%`, `\%`,
|
|
|
|
`_`, `\_`,
|
|
|
|
`*`, `%`,
|
|
|
|
`?`, `_`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func HasWildcards(mask string) bool {
|
|
|
|
return wildMaskExpr.MatchString(mask)
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func ExpandUserHost(userhost Name) (expanded Name) {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded = userhost
|
|
|
|
// fill in missing wildcards for nicks
|
2014-03-09 21:45:36 +01:00
|
|
|
if !strings.Contains(expanded.String(), "!") {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded += "!*"
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
if !strings.Contains(expanded.String(), "@") {
|
2014-03-07 01:51:33 +01:00
|
|
|
expanded += "@*"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func QuoteLike(userhost Name) Name {
|
|
|
|
return Name(likeQuoter.Replace(userhost.String()))
|
2014-03-07 01:51:33 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 22:10:54 +01:00
|
|
|
type ClientLookupSet struct {
|
2014-03-09 21:45:36 +01:00
|
|
|
byNick map[Name]*Client
|
2014-03-06 22:10:54 +01:00
|
|
|
db *ClientDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientLookupSet() *ClientLookupSet {
|
|
|
|
return &ClientLookupSet{
|
2014-03-09 21:45:36 +01:00
|
|
|
byNick: make(map[Name]*Client),
|
2014-03-06 22:10:54 +01:00
|
|
|
db: NewClientDB(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (clients *ClientLookupSet) Get(nick Name) *Client {
|
|
|
|
return clients.byNick[nick.ToLower()]
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clients *ClientLookupSet) Add(client *Client) error {
|
|
|
|
if !client.HasNick() {
|
|
|
|
return ErrNickMissing
|
|
|
|
}
|
|
|
|
if clients.Get(client.nick) != nil {
|
|
|
|
return ErrNicknameInUse
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
clients.byNick[client.Nick().ToLower()] = client
|
2014-03-06 22:10:54 +01:00
|
|
|
clients.db.Add(client)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clients *ClientLookupSet) Remove(client *Client) error {
|
|
|
|
if !client.HasNick() {
|
|
|
|
return ErrNickMissing
|
|
|
|
}
|
|
|
|
if clients.Get(client.nick) != client {
|
|
|
|
return ErrNicknameMismatch
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
delete(clients.byNick, client.nick.ToLower())
|
2014-03-06 22:10:54 +01:00
|
|
|
clients.db.Remove(client)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (clients *ClientLookupSet) FindAll(userhost Name) (set ClientSet) {
|
2014-03-06 22:10:54 +01:00
|
|
|
userhost = ExpandUserHost(userhost)
|
|
|
|
set = make(ClientSet)
|
|
|
|
rows, err := clients.db.db.Query(
|
2014-03-07 01:51:33 +01:00
|
|
|
`SELECT nickname FROM client WHERE userhost LIKE ? ESCAPE '\'`,
|
2014-03-06 22:10:54 +01:00
|
|
|
QuoteLike(userhost))
|
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientLookupSet.FindAll.Query:", err)
|
2014-03-06 22:10:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
2014-03-09 21:45:36 +01:00
|
|
|
var nickname Name
|
2014-03-06 22:10:54 +01:00
|
|
|
err := rows.Scan(&nickname)
|
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientLookupSet.FindAll.Scan:", err)
|
2014-03-06 22:10:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
client := clients.Get(nickname)
|
2014-03-07 01:51:33 +01:00
|
|
|
if client == nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientLookupSet.FindAll: missing client:", nickname)
|
2014-03-07 01:51:33 +01:00
|
|
|
continue
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
2014-03-07 01:51:33 +01:00
|
|
|
set.Add(client)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (clients *ClientLookupSet) Find(userhost Name) *Client {
|
2014-03-06 22:10:54 +01:00
|
|
|
userhost = ExpandUserHost(userhost)
|
|
|
|
row := clients.db.db.QueryRow(
|
2014-03-07 01:51:33 +01:00
|
|
|
`SELECT nickname FROM client WHERE userhost LIKE ? ESCAPE '\' LIMIT 1`,
|
2014-03-06 22:10:54 +01:00
|
|
|
QuoteLike(userhost))
|
2014-03-09 21:45:36 +01:00
|
|
|
var nickname Name
|
2014-03-06 22:10:54 +01:00
|
|
|
err := row.Scan(&nickname)
|
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientLookupSet.Find:", err)
|
2014-03-06 22:10:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return clients.Get(nickname)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// client db
|
|
|
|
//
|
|
|
|
|
|
|
|
type ClientDB struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientDB() *ClientDB {
|
|
|
|
db := &ClientDB{
|
|
|
|
db: OpenDB(":memory:"),
|
|
|
|
}
|
2014-03-07 01:51:33 +01:00
|
|
|
stmts := []string{
|
|
|
|
`CREATE TABLE client (
|
|
|
|
nickname TEXT NOT NULL COLLATE NOCASE UNIQUE,
|
|
|
|
userhost TEXT NOT NULL COLLATE NOCASE,
|
|
|
|
UNIQUE (nickname, userhost) ON CONFLICT REPLACE)`,
|
|
|
|
`CREATE UNIQUE INDEX idx_nick ON client (nickname COLLATE NOCASE)`,
|
|
|
|
`CREATE UNIQUE INDEX idx_uh ON client (userhost COLLATE NOCASE)`,
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
2014-03-07 01:51:33 +01:00
|
|
|
for _, stmt := range stmts {
|
|
|
|
_, err := db.db.Exec(stmt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("NewClientDB: ", stmt, err)
|
|
|
|
}
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *ClientDB) Add(client *Client) {
|
|
|
|
_, err := db.db.Exec(`INSERT INTO client (nickname, userhost) VALUES (?, ?)`,
|
|
|
|
client.Nick(), client.UserHost())
|
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientDB.Add:", err)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *ClientDB) Remove(client *Client) {
|
|
|
|
_, err := db.db.Exec(`DELETE FROM client WHERE nickname = ?`,
|
|
|
|
client.Nick())
|
|
|
|
if err != nil {
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.error.Println("ClientDB.Remove:", err)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// usermask to regexp
|
|
|
|
//
|
|
|
|
|
|
|
|
type UserMaskSet struct {
|
2014-03-09 21:45:36 +01:00
|
|
|
masks map[Name]bool
|
2014-03-08 02:09:49 +01:00
|
|
|
regexp *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMaskSet() *UserMaskSet {
|
|
|
|
return &UserMaskSet{
|
2014-03-09 21:45:36 +01:00
|
|
|
masks: make(map[Name]bool),
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (set *UserMaskSet) Add(mask Name) bool {
|
2014-03-08 02:35:58 +01:00
|
|
|
if set.masks[mask] {
|
|
|
|
return false
|
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
set.masks[mask] = true
|
|
|
|
set.setRegexp()
|
2014-03-08 02:35:58 +01:00
|
|
|
return true
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (set *UserMaskSet) AddAll(masks []Name) (added bool) {
|
2014-03-09 04:22:04 +01:00
|
|
|
for _, mask := range masks {
|
|
|
|
if !added && !set.masks[mask] {
|
|
|
|
added = true
|
|
|
|
}
|
|
|
|
set.masks[mask] = true
|
|
|
|
}
|
|
|
|
set.setRegexp()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (set *UserMaskSet) Remove(mask Name) bool {
|
2014-03-08 02:35:58 +01:00
|
|
|
if !set.masks[mask] {
|
|
|
|
return false
|
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
delete(set.masks, mask)
|
|
|
|
set.setRegexp()
|
2014-03-08 02:35:58 +01:00
|
|
|
return true
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (set *UserMaskSet) Match(userhost Name) bool {
|
2014-03-08 02:09:49 +01:00
|
|
|
if set.regexp == nil {
|
|
|
|
return false
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
return set.regexp.MatchString(userhost.String())
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 03:14:02 +01:00
|
|
|
func (set *UserMaskSet) String() string {
|
|
|
|
masks := make([]string, len(set.masks))
|
|
|
|
index := 0
|
|
|
|
for mask := range set.masks {
|
2014-03-09 21:45:36 +01:00
|
|
|
masks[index] = mask.String()
|
2014-03-08 03:14:02 +01:00
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
return strings.Join(masks, " ")
|
|
|
|
}
|
|
|
|
|
2014-03-09 04:22:04 +01:00
|
|
|
// Generate a regular expression from the set of user mask
|
|
|
|
// strings. Masks are split at the two types of wildcards, `*` and
|
|
|
|
// `?`. All the pieces are meta-escaped. `*` is replaced with `.*`,
|
|
|
|
// the regexp equivalent. Likewise, `?` is replaced with `.`. The
|
|
|
|
// parts are re-joined and finally all masks are joined into a big
|
|
|
|
// or-expression.
|
2014-03-08 02:09:49 +01:00
|
|
|
func (set *UserMaskSet) setRegexp() {
|
|
|
|
if len(set.masks) == 0 {
|
|
|
|
set.regexp = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
maskExprs := make([]string, len(set.masks))
|
|
|
|
index := 0
|
|
|
|
for mask := range set.masks {
|
2014-03-09 21:45:36 +01:00
|
|
|
manyParts := strings.Split(mask.String(), "*")
|
2014-03-08 02:09:49 +01:00
|
|
|
manyExprs := make([]string, len(manyParts))
|
|
|
|
for mindex, manyPart := range manyParts {
|
|
|
|
oneParts := strings.Split(manyPart, "?")
|
|
|
|
oneExprs := make([]string, len(oneParts))
|
|
|
|
for oindex, onePart := range oneParts {
|
|
|
|
oneExprs[oindex] = regexp.QuoteMeta(onePart)
|
|
|
|
}
|
|
|
|
manyExprs[mindex] = strings.Join(oneExprs, ".")
|
|
|
|
}
|
|
|
|
maskExprs[index] = strings.Join(manyExprs, ".*")
|
|
|
|
}
|
|
|
|
expr := "^" + strings.Join(maskExprs, "|") + "$"
|
|
|
|
set.regexp, _ = regexp.Compile(expr)
|
|
|
|
}
|