From 2ecec25d28295ea5c14d51128537fd6385cb20e2 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Sat, 3 Feb 2018 21:15:07 +1000 Subject: [PATCH] Misc refactoring --- irc/capability.go | 31 ------------------------------- irc/caps/constants.go | 12 ++++++++++++ irc/channelreg.go | 7 +++++-- irc/chanserv.go | 12 +++++++----- irc/client.go | 4 ++-- irc/handlers.go | 24 ++++++++++++------------ irc/nickserv.go | 12 +++++++----- irc/server.go | 10 +++++++++- irc/{ => utils}/message_tags.go | 2 +- 9 files changed, 55 insertions(+), 59 deletions(-) delete mode 100644 irc/capability.go rename irc/{ => utils}/message_tags.go (97%) diff --git a/irc/capability.go b/irc/capability.go deleted file mode 100644 index 3a6dcf84..00000000 --- a/irc/capability.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2012-2014 Jeremy Latt -// Copyright (c) 2016-2017 Daniel Oaks -// released under the MIT license - -package irc - -import ( - "github.com/oragono/oragono/irc/caps" -) - -var ( - // SupportedCapabilities are the caps we advertise. - // MaxLine, SASL and STS are set during server startup. - SupportedCapabilities = caps.NewSet(caps.AccountTag, caps.AccountNotify, caps.AwayNotify, caps.CapNotify, caps.ChgHost, caps.EchoMessage, caps.ExtendedJoin, caps.InviteNotify, caps.Languages, caps.MessageTags, caps.MultiPrefix, caps.Rename, caps.Resume, caps.ServerTime, caps.UserhostInNames) - - // CapValues are the actual values we advertise to v3.2 clients. - // actual values are set during server startup. - CapValues = caps.NewValues() -) - -// CapState shows whether we're negotiating caps, finished, etc for connection registration. -type CapState uint - -const ( - // 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 means CAP negotiation has been successfully ended and reg should complete. - CapNegotiated CapState = iota -) diff --git a/irc/caps/constants.go b/irc/caps/constants.go index 7845ff61..fee059dc 100644 --- a/irc/caps/constants.go +++ b/irc/caps/constants.go @@ -63,3 +63,15 @@ const ( // Cap302 refers to the IRCv3.2 CAP spec. Cap302 Version = 302 ) + +// State shows whether we're negotiating caps, finished, etc for connection registration. +type State uint + +const ( + // NoneState means CAP hasn't been negotiated at all. + NoneState State = iota + // NegotiatingState means CAP is being negotiated and registration should be paused. + NegotiatingState State = iota + // NegotiatedState means CAP negotiation has been successfully ended and reg should complete. + NegotiatedState State = iota +) diff --git a/irc/channelreg.go b/irc/channelreg.go index 2443721b..1e4d669f 100644 --- a/irc/channelreg.go +++ b/irc/channelreg.go @@ -67,16 +67,19 @@ type RegisteredChannel struct { Invitelist []string } +// ChannelRegistry manages registered channels. type ChannelRegistry struct { - // this serializes operations of the form (read channel state, synchronously persist it); + // This serializes operations of the form (read channel state, synchronously persist it); // this is enough to guarantee eventual consistency of the database with the // ChannelManager and Channel objects, which are the source of truth. - // Wwe could use the buntdb RW transaction lock for this purpose but we share + // + // We could use the buntdb RW transaction lock for this purpose but we share // that with all the other modules, so let's not. sync.Mutex // tier 2 server *Server } +// NewChannelRegistry returns a new ChannelRegistry. func NewChannelRegistry(server *Server) *ChannelRegistry { return &ChannelRegistry{ server: server, diff --git a/irc/chanserv.go b/irc/chanserv.go index c4e6cd53..2c9e3b73 100644 --- a/irc/chanserv.go +++ b/irc/chanserv.go @@ -12,16 +12,18 @@ import ( "github.com/oragono/oragono/irc/sno" ) -func (server *Server) chanservReceiveNotice(client *Client, message string) { - // do nothing -} - // ChanServNotice sends the client a notice from ChanServ. func (client *Client) ChanServNotice(text string) { client.Send(nil, fmt.Sprintf("ChanServ!services@%s", client.server.name), "NOTICE", client.nick, text) } -func (server *Server) chanservReceivePrivmsg(client *Client, message string) { +// chanservReceiveNotice handles NOTICEs that ChanServ receives. +func (server *Server) chanservNoticeHandler(client *Client, message string) { + // do nothing +} + +// chanservReceiveNotice handles NOTICEs that ChanServ receives. +func (server *Server) chanservPrivmsgHandler(client *Client, message string) { var params []string for _, p := range strings.Split(message, " ") { if len(p) > 0 { diff --git a/irc/client.go b/irc/client.go index d289deb3..fcd90418 100644 --- a/irc/client.go +++ b/irc/client.go @@ -44,7 +44,7 @@ type Client struct { authorized bool awayMessage string capabilities *caps.Set - capState CapState + capState caps.State capVersion caps.Version certfp string channels ChannelSet @@ -92,7 +92,7 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) *Client { atime: now, authorized: server.Password() == nil, capabilities: caps.NewSet(), - capState: CapNone, + capState: caps.NoneState, capVersion: caps.Cap301, channels: make(ChannelSet), ctime: now, diff --git a/irc/handlers.go b/irc/handlers.go index 4b3682ed..61c62a47 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -533,7 +533,7 @@ func capHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { switch subCommand { case "LS": if !client.registered { - client.capState = CapNegotiating + client.capState = caps.NegotiatingState } if len(msg.Params) > 1 && msg.Params[1] == "302" { client.capVersion = 302 @@ -549,7 +549,7 @@ func capHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { case "REQ": if !client.registered { - client.capState = CapNegotiating + client.capState = caps.NegotiatingState } // make sure all capabilities actually exist @@ -564,7 +564,7 @@ func capHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { case "END": if !client.registered { - client.capState = CapNegotiated + client.capState = caps.NegotiatedState server.tryRegister(client) } @@ -576,7 +576,7 @@ func capHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // csHandler handles the /CS and /CHANSERV commands func csHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { - server.chanservReceivePrivmsg(client, strings.Join(msg.Params, " ")) + server.chanservPrivmsgHandler(client, strings.Join(msg.Params, " ")) return false } @@ -1676,7 +1676,7 @@ func nickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // NOTICE {,} func noticeHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { - clientOnlyTags := GetClientOnlyTags(msg.Tags) + clientOnlyTags := utils.GetClientOnlyTags(msg.Tags) targets := strings.Split(msg.Params[0], ",") message := msg.Params[1] @@ -1710,10 +1710,10 @@ func noticeHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { continue } if target == "chanserv" { - server.chanservReceiveNotice(client, message) + server.chanservNoticeHandler(client, message) continue } else if target == "nickserv" { - server.nickservReceiveNotice(client, message) + server.nickservNoticeHandler(client, message) continue } @@ -1778,7 +1778,7 @@ func npcaHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // nsHandler handles the /NS and /NICKSERV commands func nsHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { - server.nickservReceivePrivmsg(client, strings.Join(msg.Params, " ")) + server.nickservPrivmsgHandler(client, strings.Join(msg.Params, " ")) return false } @@ -1901,7 +1901,7 @@ func pongHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // PRIVMSG {,} func privmsgHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { - clientOnlyTags := GetClientOnlyTags(msg.Tags) + clientOnlyTags := utils.GetClientOnlyTags(msg.Tags) targets := strings.Split(msg.Params[0], ",") message := msg.Params[1] @@ -1937,10 +1937,10 @@ func privmsgHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool } else { target, err = CasefoldName(targetString) if target == "chanserv" { - server.chanservReceivePrivmsg(client, message) + server.chanservPrivmsgHandler(client, message) continue } else if target == "nickserv" { - server.nickservReceivePrivmsg(client, message) + server.nickservPrivmsgHandler(client, message) continue } user := server.clients.Get(target) @@ -2159,7 +2159,7 @@ func sceneHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // TAGMSG {,} func tagmsgHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { - clientOnlyTags := GetClientOnlyTags(msg.Tags) + clientOnlyTags := utils.GetClientOnlyTags(msg.Tags) // no client-only tags, so we can drop it if clientOnlyTags == nil { return false diff --git a/irc/nickserv.go b/irc/nickserv.go index 8f92cfe7..7141c86d 100644 --- a/irc/nickserv.go +++ b/irc/nickserv.go @@ -27,10 +27,6 @@ To login to an account: Leave out [username password] to use your client certificate fingerprint. Otherwise, the given username and password will be used.` -func (server *Server) nickservReceiveNotice(client *Client, message string) { - // do nothing -} - // extractParam extracts a parameter from the given string, returning the param and the rest of the string. func extractParam(line string) (string, string) { rawParams := strings.SplitN(strings.TrimSpace(line), " ", 2) @@ -42,7 +38,13 @@ func extractParam(line string) (string, string) { return param0, param1 } -func (server *Server) nickservReceivePrivmsg(client *Client, message string) { +// nickservNoticeHandler handles NOTICEs that NickServ receives. +func (server *Server) nickservNoticeHandler(client *Client, message string) { + // do nothing +} + +// nickservPrivmsgHandler handles PRIVMSGs that NickServ receives. +func (server *Server) nickservPrivmsgHandler(client *Client, message string) { command, params := extractParam(message) command = strings.ToLower(command) diff --git a/irc/server.go b/irc/server.go index c704aad4..7da27b69 100644 --- a/irc/server.go +++ b/irc/server.go @@ -49,6 +49,14 @@ var ( supportedUserModesString = modes.SupportedUserModes.String() // supportedChannelModesString acts as a cache for when we introduce users supportedChannelModesString = modes.SupportedChannelModes.String() + + // SupportedCapabilities are the caps we advertise. + // MaxLine, SASL and STS are set during server startup. + SupportedCapabilities = caps.NewSet(caps.AccountTag, caps.AccountNotify, caps.AwayNotify, caps.CapNotify, caps.ChgHost, caps.EchoMessage, caps.ExtendedJoin, caps.InviteNotify, caps.Languages, caps.MessageTags, caps.MultiPrefix, caps.Rename, caps.Resume, caps.ServerTime, caps.UserhostInNames) + + // CapValues are the actual values we advertise to v3.2 clients. + // actual values are set during server startup. + CapValues = caps.NewValues() ) // Limits holds the maximum limits for various things such as topic lengths. @@ -422,7 +430,7 @@ func (server *Server) generateMessageID() string { func (server *Server) tryRegister(c *Client) { if c.registered || !c.HasNick() || !c.HasUsername() || - (c.capState == CapNegotiating) { + (c.capState == caps.NegotiatingState) { return } diff --git a/irc/message_tags.go b/irc/utils/message_tags.go similarity index 97% rename from irc/message_tags.go rename to irc/utils/message_tags.go index 1197b92f..d2df94b5 100644 --- a/irc/message_tags.go +++ b/irc/utils/message_tags.go @@ -1,7 +1,7 @@ // Copyright (c) 2016-2017 Daniel Oaks // released under the MIT license -package irc +package utils import "github.com/goshuirc/irc-go/ircmsg"