3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-13 07:29:30 +01:00

Fix some notes and add some comments.

This commit is contained in:
Daniel Oaks 2017-10-05 23:29:34 +10:00
parent 797b9f0996
commit eac6a69782
5 changed files with 36 additions and 16 deletions

View File

@ -224,17 +224,21 @@ func (clients *ClientLookupSet) Find(userhost string) *Client {
//TODO(dan): move this over to generally using glob syntax instead?
// kinda more expected in normal ban/etc masks, though regex is useful (probably as an extban?)
// UserMaskSet holds a set of client masks and lets you match hostnames to them.
type UserMaskSet struct {
masks map[string]bool
regexp *regexp.Regexp
}
// NewUserMaskSet returns a new UserMaskSet.
func NewUserMaskSet() *UserMaskSet {
return &UserMaskSet{
masks: make(map[string]bool),
}
}
// Add adds the given mask to this set.
func (set *UserMaskSet) Add(mask string) bool {
casefoldedMask, err := Casefold(mask)
if err != nil {
@ -249,6 +253,7 @@ func (set *UserMaskSet) Add(mask string) bool {
return true
}
// AddAll adds the given masks to this set.
func (set *UserMaskSet) AddAll(masks []string) (added bool) {
for _, mask := range masks {
if !added && !set.masks[mask] {
@ -260,6 +265,7 @@ func (set *UserMaskSet) AddAll(masks []string) (added bool) {
return
}
// Remove removes the given mask from this set.
func (set *UserMaskSet) Remove(mask string) bool {
if !set.masks[mask] {
return false
@ -269,6 +275,7 @@ func (set *UserMaskSet) Remove(mask string) bool {
return true
}
// Match matches the given n!u@h.
func (set *UserMaskSet) Match(userhost string) bool {
if set.regexp == nil {
return false
@ -276,17 +283,18 @@ func (set *UserMaskSet) Match(userhost string) bool {
return set.regexp.MatchString(userhost)
}
// String returns the masks in this set.
func (set *UserMaskSet) String() string {
masks := make([]string, len(set.masks))
index := 0
for mask := range set.masks {
masks[index] = mask
index += 1
index++
}
return strings.Join(masks, " ")
}
// Generate a regular expression from the set of user mask
// setRegexp generates 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

View File

@ -53,7 +53,7 @@ func InitDB(path string) {
}
}
// open an existing database, performing a schema version check
// OpenDatabase returns an existing database, performing a schema version check.
func OpenDatabase(path string) (*buntdb.DB, error) {
// open data store
db, err := buntdb.Open(path)
@ -65,7 +65,7 @@ func OpenDatabase(path string) (*buntdb.DB, error) {
err = db.View(func(tx *buntdb.Tx) error {
version, _ := tx.Get(keySchemaVersion)
if version != latestDbSchema {
return fmt.Errorf("Database must be updated. Expected schema v%s, got v%s.", latestDbSchema, version)
return fmt.Errorf("Database must be updated. Expected schema v%s, got v%s", latestDbSchema, version)
}
return nil
})

View File

@ -12,6 +12,7 @@ import (
"github.com/goshuirc/irc-go/ircmsg"
)
// MonitorManager keeps track of who's monitoring which nicks.
type MonitorManager struct {
sync.RWMutex
// client -> nicks it's watching
@ -21,6 +22,7 @@ type MonitorManager struct {
// (all nicks must be normalized externally by casefolding)
}
// NewMonitorManager returns a new MonitorManager.
func NewMonitorManager() *MonitorManager {
mm := MonitorManager{
watching: make(map[*Client]map[string]bool),
@ -29,7 +31,8 @@ func NewMonitorManager() *MonitorManager {
return &mm
}
var MonitorLimitExceeded = errors.New("Monitor limit exceeded")
// ErrMonitorLimitExceeded is used when the monitor list exceeds our limit.
var ErrMonitorLimitExceeded = errors.New("Monitor limit exceeded")
// AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line.
func (manager *MonitorManager) AlertAbout(client *Client, online bool) {
@ -69,7 +72,7 @@ func (manager *MonitorManager) Add(client *Client, nick string, limit int) error
}
if len(manager.watching[client]) >= limit {
return MonitorLimitExceeded
return ErrMonitorLimitExceeded
}
manager.watching[client][nick] = true
@ -92,7 +95,7 @@ func (manager *MonitorManager) RemoveAll(client *Client) {
manager.Lock()
defer manager.Unlock()
for nick, _ := range manager.watching[client] {
for nick := range manager.watching[client] {
delete(manager.watchedby[nick], client)
}
delete(manager.watching, client)
@ -172,7 +175,7 @@ func monitorAddHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bo
}
err = server.monitorManager.Add(client, casefoldedTarget, limit)
if err == MonitorLimitExceeded {
if err == ErrMonitorLimitExceeded {
client.Send(nil, server.name, ERR_MONLISTFULL, client.getNick(), strconv.Itoa(server.limits.MonitorEntries), strings.Join(targets, ","))
break
} else if err != nil {
@ -206,7 +209,7 @@ func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage) b
monitorList := server.monitorManager.List(client)
var nickList []string
for _, cfnick := range(monitorList) {
for _, cfnick := range monitorList {
replynick := cfnick
// report the uncasefolded nick if it's available, i.e., the client is online
if mclient := server.clients.Get(cfnick); mclient != nil {

View File

@ -1592,6 +1592,7 @@ func (server *Server) setupListeners(config *Config) {
}
}
// GetDefaultChannelModes returns our default channel modes.
func (server *Server) GetDefaultChannelModes() Modes {
server.configurableStateMutex.RLock()
defer server.configurableStateMutex.RUnlock()

View File

@ -64,8 +64,10 @@ func (channels *ChannelNameMap) Len() int {
return len(channels.Chans)
}
// ModeSet holds a set of modes.
type ModeSet map[Mode]bool
// String returns the modes in this set.
func (set ModeSet) String() string {
if len(set) == 0 {
return ""
@ -79,35 +81,44 @@ func (set ModeSet) String() string {
return strings.Join(strs, "")
}
// ClientSet is a set of clients.
type ClientSet map[*Client]bool
// Add adds the given client to this set.
func (clients ClientSet) Add(client *Client) {
clients[client] = true
}
// Remove removes the given client from this set.
func (clients ClientSet) Remove(client *Client) {
delete(clients, client)
}
// Has returns true if the given client is in this set.
func (clients ClientSet) Has(client *Client) bool {
return clients[client]
}
// MemberSet is a set of members with modes.
type MemberSet map[*Client]ModeSet
// Add adds the given client to this set.
func (members MemberSet) Add(member *Client) {
members[member] = make(ModeSet)
}
// Remove removes the given client from this set.
func (members MemberSet) Remove(member *Client) {
delete(members, member)
}
// Has returns true if the given client is in this set.
func (members MemberSet) Has(member *Client) bool {
_, ok := members[member]
return ok
}
// HasMode returns true if the given client is in this set with the given mode.
func (members MemberSet) HasMode(member *Client, mode Mode) bool {
modes, ok := members[member]
if !ok {
@ -116,6 +127,7 @@ func (members MemberSet) HasMode(member *Client, mode Mode) bool {
return modes[mode]
}
// AnyHasMode returns true if any of our clients has the given mode.
func (members MemberSet) AnyHasMode(mode Mode) bool {
for _, modes := range members {
if modes[mode] {
@ -125,19 +137,15 @@ func (members MemberSet) AnyHasMode(mode Mode) bool {
return false
}
// ChannelSet is a set of channels.
type ChannelSet map[*Channel]bool
// Add adds the given channel to this set.
func (channels ChannelSet) Add(channel *Channel) {
channels[channel] = true
}
// Remove removes the given channel from this set.
func (channels ChannelSet) Remove(channel *Channel) {
delete(channels, channel)
}
func (channels ChannelSet) First() *Channel {
for channel := range channels {
return channel
}
return nil
}