From 22216d4d60696c27eb3a57d4be96a59553d47809 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Sun, 16 Apr 2017 11:31:33 +1000 Subject: [PATCH] Fix lots of nits --- irc/accounts.go | 10 +++++----- irc/capability.go | 17 +++++------------ irc/channel.go | 1 + irc/client.go | 9 +++++---- irc/config.go | 23 +++++++++++++++++++++-- irc/net.go | 1 + irc/whowas.go | 2 +- 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/irc/accounts.go b/irc/accounts.go index f7de3f56..7797a6ee 100644 --- a/irc/accounts.go +++ b/irc/accounts.go @@ -318,12 +318,12 @@ func authExternalHandler(server *Server, client *Client, mechanism string, value } // successfulSaslAuth means that a SASL auth attempt completed successfully, and is used to dispatch messages. -func (c *Client) successfulSaslAuth() { - c.Send(nil, c.server.name, RPL_LOGGEDIN, c.nick, c.nickMaskString, c.account.Name, fmt.Sprintf("You are now logged in as %s", c.account.Name)) - c.Send(nil, c.server.name, RPL_SASLSUCCESS, c.nick, "SASL authentication successful") +func (client *Client) successfulSaslAuth() { + client.Send(nil, client.server.name, RPL_LOGGEDIN, client.nick, client.nickMaskString, client.account.Name, fmt.Sprintf("You are now logged in as %s", client.account.Name)) + client.Send(nil, client.server.name, RPL_SASLSUCCESS, client.nick, "SASL authentication successful") // dispatch account-notify - for friend := range c.Friends(AccountNotify) { - friend.Send(nil, c.nickMaskString, "ACCOUNT", c.account.Name) + for friend := range client.Friends(AccountNotify) { + friend.Send(nil, client.nickMaskString, "ACCOUNT", client.account.Name) } } diff --git a/irc/capability.go b/irc/capability.go index d2cf1e55..3214fc03 100644 --- a/irc/capability.go +++ b/irc/capability.go @@ -66,9 +66,12 @@ func (capability Capability) String() string { type CapState uint const ( - CapNone CapState = iota + // CapNone means CAP hasn't been negotiated at all. + CapNone CapState = iota + // CapNegotiating means CAP is being negotiated and registration should be paused. CapNegotiating CapState = iota - CapNegotiated CapState = iota + // CapNegotiated means CAP negotiation has been successfully ended and reg should complete. + CapNegotiated CapState = iota ) // CapVersion is used to select which max version of CAP the client supports. @@ -101,16 +104,6 @@ func (set CapabilitySet) String(version CapVersion) string { return strings.Join(strs, " ") } -func (set CapabilitySet) DisableString() string { - parts := make([]string, len(set)) - index := 0 - for capability := range set { - parts[index] = "-" + capability.String() - index++ - } - return strings.Join(parts, " ") -} - // CAP [] func capHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { subCommand := strings.ToUpper(msg.Params[0]) diff --git a/irc/channel.go b/irc/channel.go index 53446a84..0e1af51a 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -17,6 +17,7 @@ import ( "github.com/tidwall/buntdb" ) +// Channel represents a channel that clients can join. type Channel struct { flags ModeSet lists map[Mode]*UserMaskSet diff --git a/irc/client.go b/irc/client.go index 16304386..684a65dc 100644 --- a/irc/client.go +++ b/irc/client.go @@ -283,7 +283,7 @@ func (client *Client) HasNick() bool { return client.nick != "" && client.nick != "*" } -// HasNick returns true if the client's username is set (used in registration). +// HasUsername returns true if the client's username is set (used in registration). func (client *Client) HasUsername() bool { return client.username != "" && client.username != "*" } @@ -303,11 +303,11 @@ func (client *Client) HasCapabs(capabs ...string) bool { return true } -// -func (c *Client) ModeString() (str string) { +// ModeString returns the mode string for this client. +func (client *Client) ModeString() (str string) { str = "+" - for flag := range c.flags { + for flag := range client.flags { str += flag.String() } @@ -433,6 +433,7 @@ func (client *Client) ChangeNickname(nickname string) error { return err } +// Quit sends the given quit message to the client (but does not destroy them). func (client *Client) Quit(message string) { if !client.quitMessageSent { client.Send(nil, client.nickMaskString, "QUIT", message) diff --git a/irc/config.go b/irc/config.go index e3bee7de..8e370ee1 100644 --- a/irc/config.go +++ b/irc/config.go @@ -21,17 +21,18 @@ import ( "gopkg.in/yaml.v2" ) +// PassConfig holds the connection password. type PassConfig struct { Password string } -// TLSListenConfig defines configuration options for listening on TLS +// TLSListenConfig defines configuration options for listening on TLS. type TLSListenConfig struct { Cert string Key string } -// Certificate returns the TLS certificate assicated with this TLSListenConfig +// Config returns the TLS contiguration assicated with this TLSListenConfig. func (conf *TLSListenConfig) Config() (*tls.Config, error) { cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key) if err != nil { @@ -43,6 +44,7 @@ func (conf *TLSListenConfig) Config() (*tls.Config, error) { }, err } +// PasswordBytes returns the bytes represented by the password hash. func (conf *PassConfig) PasswordBytes() []byte { bytes, err := DecodePasswordHash(conf.Password) if err != nil { @@ -51,6 +53,7 @@ func (conf *PassConfig) PasswordBytes() []byte { return bytes } +// AccountRegistrationConfig controls account registration. type AccountRegistrationConfig struct { Enabled bool EnabledCallbacks []string `yaml:"enabled-callbacks"` @@ -72,10 +75,12 @@ type AccountRegistrationConfig struct { } } +// ChannelRegistrationConfig controls channel registration. type ChannelRegistrationConfig struct { Enabled bool } +// OperClassConfig defines a specific operator class. type OperClassConfig struct { Title string WhoisLine string @@ -83,6 +88,7 @@ type OperClassConfig struct { Capabilities []string } +// OperConfig defines a specific operator's configuration. type OperConfig struct { Class string Vhost string @@ -98,11 +104,13 @@ func (conf *OperConfig) PasswordBytes() []byte { return bytes } +// RestAPIConfig controls the integrated REST API. type RestAPIConfig struct { Enabled bool Listen string } +// ConnectionLimitsConfig controls the automated connection limits. type ConnectionLimitsConfig struct { Enabled bool CidrLenIPv4 int `yaml:"cidr-len-ipv4"` @@ -111,6 +119,7 @@ type ConnectionLimitsConfig struct { Exempted []string } +// ConnectionThrottleConfig controls the automated connection throttling. type ConnectionThrottleConfig struct { Enabled bool CidrLenIPv4 int `yaml:"cidr-len-ipv4"` @@ -124,6 +133,7 @@ type ConnectionThrottleConfig struct { Exempted []string } +// LoggingConfig controls a single logging method. type LoggingConfig struct { Method string MethodStderr bool @@ -136,11 +146,13 @@ type LoggingConfig struct { Level logger.Level `yaml:"level-real"` } +// LineLenConfig controls line lengths. type LineLenConfig struct { Tags int Rest int } +// STSConfig controls the STS configuration/ type STSConfig struct { Enabled bool Duration time.Duration `yaml:"duration-real"` @@ -161,6 +173,7 @@ func (sts *STSConfig) Value() string { return val } +// Config defines the overall configuration. type Config struct { Network struct { Name string @@ -215,12 +228,14 @@ type Config struct { } } +// OperClass defines an assembled operator class. type OperClass struct { Title string WhoisLine string `yaml:"whois-line"` Capabilities map[string]bool // map to make lookups much easier } +// OperatorClasses returns a map of assembled operator classes from the given config. func (conf *Config) OperatorClasses() (*map[string]OperClass, error) { ocs := make(map[string]OperClass) @@ -290,6 +305,7 @@ func (conf *Config) OperatorClasses() (*map[string]OperClass, error) { return &ocs, nil } +// Oper represents a single assembled operator's config. type Oper struct { Class *OperClass WhoisLine string @@ -297,6 +313,7 @@ type Oper struct { Pass []byte } +// Operators returns a map of operator configs from the given OperClass and config. func (conf *Config) Operators(oc *map[string]OperClass) (map[string]Oper, error) { operators := make(map[string]Oper) for name, opConf := range conf.Opers { @@ -327,6 +344,7 @@ func (conf *Config) Operators(oc *map[string]OperClass) (map[string]Oper, error) return operators, nil } +// TLSListeners returns a list of TLS listeners and their configs. func (conf *Config) TLSListeners() map[string]*tls.Config { tlsListeners := make(map[string]*tls.Config) for s, tlsListenersConf := range conf.Server.TLSListeners { @@ -344,6 +362,7 @@ func (conf *Config) TLSListeners() map[string]*tls.Config { return tlsListeners } +// LoadConfig loads the given YAML configuration file. func LoadConfig(filename string) (config *Config, err error) { data, err := ioutil.ReadFile(filename) if err != nil { diff --git a/irc/net.go b/irc/net.go index 04762342..ba2e68a9 100644 --- a/irc/net.go +++ b/irc/net.go @@ -9,6 +9,7 @@ import ( "strings" ) +// IPString returns a simple IP string from the given net.Addr. func IPString(addr net.Addr) string { addrStr := addr.String() ipaddr, _, err := net.SplitHostPort(addrStr) diff --git a/irc/whowas.go b/irc/whowas.go index ddd5e6c8..14aedec5 100644 --- a/irc/whowas.go +++ b/irc/whowas.go @@ -59,7 +59,7 @@ func (list *WhoWasList) Find(nickname string, limit int64) []*WhoWas { } func (list *WhoWasList) prev(index int) int { - index -= 1 + index-- if index < 0 { index += len(list.buffer) }