From 207c1074df89d8b47daf94f8f84306eedcd9216d Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Thu, 5 Oct 2017 23:47:43 +1000 Subject: [PATCH] Split utils out to a separate subpackage --- irc/client.go | 7 ++++--- irc/config.go | 3 ++- irc/monitor.go | 7 ++++--- irc/server.go | 9 +++++---- irc/{util.go => utils/args.go} | 6 +++--- irc/{ => utils}/net.go | 2 +- irc/{ => utils}/net_test.go | 2 +- 7 files changed, 20 insertions(+), 16 deletions(-) rename irc/{util.go => utils/args.go} (81%) rename irc/{ => utils}/net.go (99%) rename irc/{ => utils}/net_test.go (98%) diff --git a/irc/client.go b/irc/client.go index d3be21db..241c8a51 100644 --- a/irc/client.go +++ b/irc/client.go @@ -21,6 +21,7 @@ import ( ident "github.com/oragono/go-ident" "github.com/oragono/oragono/irc/caps" "github.com/oragono/oragono/irc/sno" + "github.com/oragono/oragono/irc/utils" ) const ( @@ -153,7 +154,7 @@ func (client *Client) IP() net.IP { return net.ParseIP(client.proxiedIP) } - return net.ParseIP(IPString(client.socket.conn.RemoteAddr())) + return net.ParseIP(utils.IPString(client.socket.conn.RemoteAddr())) } // IPString returns the IP address of this client as a string. @@ -197,7 +198,7 @@ func (client *Client) run() { // Set the hostname for this client // (may be overridden by a later PROXY command from stunnel) - client.rawHostname = AddrLookupHostname(client.socket.conn.RemoteAddr()) + client.rawHostname = utils.AddrLookupHostname(client.socket.conn.RemoteAddr()) for { line, err = client.socket.Read() @@ -451,7 +452,7 @@ func (client *Client) AllNickmasks() []string { masks = append(masks, mask) } - mask2, err := Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, IPString(client.socket.conn.RemoteAddr()))) + mask2, err := Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, utils.IPString(client.socket.conn.RemoteAddr()))) if err == nil && mask2 != mask { masks = append(masks, mask2) } diff --git a/irc/config.go b/irc/config.go index d3274cc1..a476b35d 100644 --- a/irc/config.go +++ b/irc/config.go @@ -16,6 +16,7 @@ import ( "github.com/oragono/oragono/irc/custime" "github.com/oragono/oragono/irc/logger" + "github.com/oragono/oragono/irc/utils" "code.cloudfoundry.org/bytefmt" @@ -383,7 +384,7 @@ func LoadConfig(filename string) (config *Config, err error) { if config.Server.Name == "" { return nil, errors.New("Server name missing") } - if !IsHostname(config.Server.Name) { + if !utils.IsHostname(config.Server.Name) { return nil, errors.New("Server name must match the format of a hostname") } if config.Datastore.Path == "" { diff --git a/irc/monitor.go b/irc/monitor.go index 5b3aa347..3674a845 100644 --- a/irc/monitor.go +++ b/irc/monitor.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/goshuirc/irc-go/ircmsg" + "github.com/oragono/oragono/irc/utils" ) // MonitorManager keeps track of who's monitoring which nicks. @@ -218,7 +219,7 @@ func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage) b nickList = append(nickList, replynick) } - for _, line := range argsToStrings(maxLastArgLength, nickList, ",") { + for _, line := range utils.ArgsToStrings(maxLastArgLength, nickList, ",") { client.Send(nil, server.name, RPL_MONLIST, client.getNick(), line) } @@ -243,12 +244,12 @@ func monitorStatusHandler(server *Server, client *Client, msg ircmsg.IrcMessage) } if len(online) > 0 { - for _, line := range argsToStrings(maxLastArgLength, online, ",") { + for _, line := range utils.ArgsToStrings(maxLastArgLength, online, ",") { client.Send(nil, server.name, RPL_MONONLINE, client.getNick(), line) } } if len(offline) > 0 { - for _, line := range argsToStrings(maxLastArgLength, offline, ",") { + for _, line := range utils.ArgsToStrings(maxLastArgLength, offline, ",") { client.Send(nil, server.name, RPL_MONOFFLINE, client.getNick(), line) } } diff --git a/irc/server.go b/irc/server.go index f3273400..54e95999 100644 --- a/irc/server.go +++ b/irc/server.go @@ -27,6 +27,7 @@ import ( "github.com/oragono/oragono/irc/isupport" "github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/sno" + "github.com/oragono/oragono/irc/utils" "github.com/tidwall/buntdb" ) @@ -267,7 +268,7 @@ func (server *Server) Run() { case conn := <-server.newConns: // check IP address - ipaddr := net.ParseIP(IPString(conn.Conn.RemoteAddr())) + ipaddr := net.ParseIP(utils.IPString(conn.Conn.RemoteAddr())) if ipaddr == nil { conn.Conn.Write([]byte(couldNotParseIPMsg)) conn.Conn.Close() @@ -1034,7 +1035,7 @@ func (client *Client) getWhoisOf(target *Client) { client.Send(nil, client.server.name, RPL_WHOISOPERATOR, client.nick, target.nick, target.whoisLine) } if client.flags[Operator] || client == target { - client.Send(nil, client.server.name, RPL_WHOISACTUALLY, client.nick, target.nick, fmt.Sprintf("%s@%s", target.username, LookupHostname(target.IPString())), target.IPString(), "Actual user@host, Actual IP") + client.Send(nil, client.server.name, RPL_WHOISACTUALLY, client.nick, target.nick, fmt.Sprintf("%s@%s", target.username, utils.LookupHostname(target.IPString())), target.IPString(), "Actual user@host, Actual IP") } if target.flags[TLS] { client.Send(nil, client.server.name, RPL_WHOISSECURE, client.nick, target.nick, "is using a secure connection") @@ -2177,7 +2178,7 @@ func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { return false } - clientAddress := IPString(client.socket.conn.RemoteAddr()) + clientAddress := utils.IPString(client.socket.conn.RemoteAddr()) clientHostname := client.hostname for _, address := range server.proxyAllowedFrom { if clientHostname == address || clientAddress == address { @@ -2198,7 +2199,7 @@ func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool { // override the client's regular IP client.proxiedIP = msg.Params[1] - client.rawHostname = LookupHostname(msg.Params[1]) + client.rawHostname = utils.LookupHostname(msg.Params[1]) client.hostname = client.rawHostname return false } diff --git a/irc/util.go b/irc/utils/args.go similarity index 81% rename from irc/util.go rename to irc/utils/args.go index 999463a0..8f087423 100644 --- a/irc/util.go +++ b/irc/utils/args.go @@ -1,11 +1,11 @@ // Copyright (c) 2016-2017 Daniel Oaks // released under the MIT license -package irc +package utils -// argsToStrings takes the arguments and splits them into a series of strings, +// ArgsToStrings takes the arguments and splits them into a series of strings, // each argument separated by delim and each string bounded by maxLength. -func argsToStrings(maxLength int, arguments []string, delim string) []string { +func ArgsToStrings(maxLength int, arguments []string, delim string) []string { var messages []string var buffer string diff --git a/irc/net.go b/irc/utils/net.go similarity index 99% rename from irc/net.go rename to irc/utils/net.go index 6ead2471..e56ab8de 100644 --- a/irc/net.go +++ b/irc/utils/net.go @@ -2,7 +2,7 @@ // Copyright (c) 2016 Daniel Oaks // released under the MIT license -package irc +package utils import ( "net" diff --git a/irc/net_test.go b/irc/utils/net_test.go similarity index 98% rename from irc/net_test.go rename to irc/utils/net_test.go index 04def7cb..0094d143 100644 --- a/irc/net_test.go +++ b/irc/utils/net_test.go @@ -2,7 +2,7 @@ // Copyright (c) 2016 Daniel Oaks // released under the MIT license -package irc +package utils import "testing"