3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00

Misc refactoring

This commit is contained in:
Daniel Oaks 2018-02-03 21:15:07 +10:00
parent d854bac78e
commit 2ecec25d28
9 changed files with 55 additions and 59 deletions

View File

@ -1,31 +0,0 @@
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// 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
)

View File

@ -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
)

View File

@ -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,

View File

@ -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 {

View File

@ -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,

View File

@ -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 <target>{,<target>} <message>
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 <target>{,<target>} <message>
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 <target>{,<target>}
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

View File

@ -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)

View File

@ -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
}

View File

@ -1,7 +1,7 @@
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
package utils
import "github.com/goshuirc/irc-go/ircmsg"