瀏覽代碼

Cleaned up code. (staticcheck)

Steve Thielemann 2 年之前
父節點
當前提交
5ce8c5334b
共有 4 個文件被更改,包括 36 次插入113 次删除
  1. 6 18
      client_test.go
  2. 24 89
      irc-client.go
  3. 2 1
      ircd_test.go
  4. 4 5
      throttle_test.go

+ 6 - 18
client_test.go

@@ -103,9 +103,7 @@ func TestConnect(t *testing.T) {
 	config.Hostname = parts[0]
 	config.Port, _ = strconv.Atoi(parts[1])
 	go ircServer(listen, t, &config)
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()
@@ -188,9 +186,7 @@ func TestConnectNickInUse(t *testing.T) {
 	config.Hostname = parts[0]
 	config.Port, _ = strconv.Atoi(parts[1])
 	go ircServer(listen, t, &config)
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()
@@ -249,9 +245,7 @@ func TestConnectTLS(t *testing.T) {
 	config.Hostname = parts[0]
 	config.Port, _ = strconv.Atoi(parts[1])
 	go ircServer(listen, t, &config)
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()
@@ -305,9 +299,7 @@ func TestConnectAutojoin(t *testing.T) {
 	config.Hostname = parts[0]
 	config.Port, _ = strconv.Atoi(parts[1])
 	go ircServer(listen, t, &config)
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()
@@ -430,9 +422,7 @@ func TestConnectKick(t *testing.T) {
 	abortAfter = 500
 	go ircServer(listen, t, &config)
 
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()
@@ -500,9 +490,7 @@ func TestConnectSignal(t *testing.T) {
 	config.Hostname = parts[0]
 	config.Port, _ = strconv.Atoi(parts[1])
 	go ircServer(listen, t, &config)
-	var FromIRC chan IRCMsg
-
-	FromIRC = make(chan IRCMsg)
+	var FromIRC chan IRCMsg = make(chan IRCMsg)
 	config.ReadChannel = FromIRC
 
 	config.Connect()

+ 24 - 89
irc-client.go

@@ -163,14 +163,14 @@ type IRCConfig struct {
 	Flood_Time     int               `json:"FloodTime"`      // Number of Seconds to track previous messages
 	Flood_Delay    int               `json:"FloodDelay"`     // Delay between sending when flood protection on (Milliseconds)
 	Debug_Output   bool              `json:"Debug"`
-	MyNick         string            `json:"-"`
-	OnExit         func()            `json:"-"`
+	MyNick         string            `json:"-"` // Client's current nick
+	OnExit         func()            `json:"-"` // Called on exit
 	Socket         net.Conn          `json:"-"`
 	Reader         *bufio.Reader     `json:"-"`
 	ReadChannel    chan IRCMsg       `json:"-"`
 	ReadEvents     []string          `json:"-"`
 	WriteChannel   chan IRCWrite     `json:"-"`
-	DelChannel     chan string       `json:"-"`
+	DelChannel     chan string       `json:"-"` // For deleting channel or nicks that are missing.
 	Registered     bool              `json:"-"`
 	ISupport       map[string]string `json:"-"` // 005
 	wg             sync.WaitGroup    `json:"-"`
@@ -189,8 +189,6 @@ func (Config *IRCConfig) SetNick(nick string) {
 	Config.MyNick = nick
 }
 
-// Writer       *bufio.Writer
-
 func (Config *IRCConfig) IsAuto(ch string) bool {
 	return StrInArray(Config.AutoJoin, ch)
 }
@@ -198,15 +196,15 @@ func (Config *IRCConfig) IsAuto(ch string) bool {
 func (Config *IRCConfig) Connect() bool {
 	var err error
 
+	// Set sensible defaults (if not provided),
 	if Config.Flood_Num == 0 {
 		Config.Flood_Num = 5
 	}
 	if Config.Flood_Time == 0 {
 		Config.Flood_Time = 10
 	}
-
 	if Config.Flood_Delay == 0 {
-		Config.Flood_Delay = 1000
+		Config.Flood_Delay = 2000
 	}
 
 	if Config.UseSASL {
@@ -219,13 +217,14 @@ func (Config *IRCConfig) Connect() bool {
 	Config.Registered = false
 
 	if Config.ReadChannel == nil {
-		log.Println("Warning: ReadChannel is nil.")
+		log.Println("Warning: ReadChannel is nil.  You can't received IRCMsg messages.")
 	}
 
 	if Config.UseTLS {
 		var tlsConfig tls.Config
 
 		if !Config.Insecure {
+			// Use system default CA Certificates
 			certPool := x509.NewCertPool()
 			tlsConfig.ClientCAs = certPool
 			tlsConfig.InsecureSkipVerify = false
@@ -237,14 +236,12 @@ func (Config *IRCConfig) Connect() bool {
 		if err != nil {
 			log.Fatal("tls.Dial:", err)
 		}
-		// Config.Writer = bufio.NewWriter(Config.TLSSocket)
 		Config.Reader = bufio.NewReader(Config.Socket)
 	} else {
 		Config.Socket, err = net.Dial("tcp", Config.Hostname+":"+strconv.Itoa(Config.Port))
 		if err != nil {
 			log.Fatal("net.Dial:", err)
 		}
-		// Config.Writer = bufio.NewWriter(Config.Socket)
 		Config.Reader = bufio.NewReader(Config.Socket)
 	}
 
@@ -270,7 +267,6 @@ func (Config *IRCConfig) Connect() bool {
 	Config.MyNick = Config.Nick
 	Config.PriorityWrite(fmt.Sprintf("NICK %s", Config.Nick))
 	Config.PriorityWrite(fmt.Sprintf("USER %s 0 * :%s", Config.Username, Config.Realname))
-	// Config.Writer.Flush()
 
 	go Config.ReaderRoutine()
 	Config.wg.Add(1)
@@ -284,11 +280,6 @@ func (Config *IRCConfig) write(output string) error {
 		log.Println(">>", output)
 	}
 	output += "\r\n"
-	/*
-		if Config.UseTLS {
-			_, err = Config.TLSSocket.Write([]byte(output))
-		} else {
-	*/
 	_, err = Config.Socket.Write([]byte(output))
 
 	return err
@@ -299,11 +290,11 @@ func (Config *IRCConfig) WriterRoutine() {
 	var throttle ThrottleBuffer
 	var Flood FloodTrack
 
+	Flood.Init(Config.Flood_Num, Config.Flood_Time)
+
 	defer Config.wg.Done()
 	throttle.init()
 
-	Flood.Init(Config.Flood_Num, Config.Flood_Time)
-
 	// signal handler
 	var sigChannel chan os.Signal = make(chan os.Signal, 1)
 	signal.Notify(sigChannel, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
@@ -332,19 +323,9 @@ func (Config *IRCConfig) WriterRoutine() {
 				if !gotSignal {
 					gotSignal = true
 					log.Println("SIGNAL")
-					/*
-						// This should be handled now by Close().
-						if Config.OnExit != nil {
-							Config.OnExit()
-						}
-					*/
 					Config.PriorityWrite("QUIT :Received SIGINT")
 				}
-				// return
 				continue
-				// Config.Close()
-				// return
-				//os.Exit(2)
 
 			case remove := <-Config.DelChannel:
 				if Config.Debug_Output {
@@ -353,12 +334,6 @@ func (Config *IRCConfig) WriterRoutine() {
 				throttle.delete(remove)
 
 			case <-time.After(time.Millisecond * time.Duration(Config.Flood_Delay)):
-				// Send from the buffer
-				// debugging the flood buffer
-				// log.Printf("targets: %#v\n", targets)
-				// log.Printf("last: %d\n", last)
-				// log.Printf("buffer: %#v\n", buffer)
-
 				var msg string = throttle.pop()
 				err = Config.write(msg)
 				if err != nil {
@@ -372,19 +347,9 @@ func (Config *IRCConfig) WriterRoutine() {
 				if !gotSignal {
 					gotSignal = true
 					log.Println("SIGNAL")
-					/*
-						// This should now be handled by Close().
-						if Config.OnExit != nil {
-							Config.OnExit()
-						}
-					*/
 					Config.PriorityWrite("QUIT :Received SIGINT")
 				}
-				// return
 				continue
-				// Config.Close()
-				// return
-				// os.Exit(2)
 
 			case remove := <-Config.DelChannel:
 				if Config.Debug_Output {
@@ -415,8 +380,6 @@ func (Config *IRCConfig) WriterRoutine() {
 			}
 		}
 	}
-
-	log.Println("~WriterRoutine")
 }
 
 func (Config *IRCConfig) Close() {
@@ -434,22 +397,27 @@ func RandomNick(nick string) string {
 	return result
 }
 
+// PriorityWrite: Send output command to server immediately.
 func (Config *IRCConfig) PriorityWrite(output string) {
 	Config.WriteChannel <- IRCWrite{To: "", Output: output}
 }
 
+// WriteTo: Send throttled output command using "to" to throttle.
 func (Config *IRCConfig) WriteTo(to string, output string) {
 	Config.WriteChannel <- IRCWrite{To: to, Output: output}
 }
 
+// Msg: Send PRIVMSG, uses WriteTo to throttle.
 func (Config *IRCConfig) Msg(to string, message string) {
 	Config.WriteTo(to, fmt.Sprintf("PRIVMSG %s :%s", to, message))
 }
 
+// Notice: Send NOTICE, uses WriteTo to throttle.
 func (Config *IRCConfig) Notice(to string, message string) {
 	Config.WriteTo(to, fmt.Sprintf("NOTICE %s :%s", to, message))
 }
 
+// Action: Send PRIVMSG CTCP ACTION, uses WriteTo to throttle.
 func (Config *IRCConfig) Action(to string, message string) {
 	Config.WriteTo(to, fmt.Sprintf("PRIVMSG %s :\x01ACTION %s\x01", to, message))
 }
@@ -457,7 +425,6 @@ func (Config *IRCConfig) Action(to string, message string) {
 func (Config *IRCConfig) ReaderRoutine() {
 	defer Config.wg.Done()
 	var registering bool
-	// var identified bool
 
 	for {
 		var line string
@@ -495,6 +462,7 @@ func (Config *IRCConfig) ReaderRoutine() {
 					Config.PriorityWrite("NICK " + newNick)
 				}
 			case "PRIVMSG":
+				// Convert Cmd "PRIVMSG" to "CTCP" or "ACTION".
 				if (results.Msg[0] == '\x01') && (results.Msg[len(results.Msg)-1] == '\x01') {
 					// ACTION
 					results.Cmd = "CTCP"
@@ -537,7 +505,6 @@ func (Config *IRCConfig) ReaderRoutine() {
 								IRCNick(results.From),
 								now.Format(time.ANSIC)))
 					}
-
 				}
 
 			}
@@ -551,19 +518,6 @@ func (Config *IRCConfig) ReaderRoutine() {
 		}
 
 		var msg IRCMsg = results
-		/*
-			IRCMsg{MsgParts: results}
-			if len(results) >= 3 {
-				msg.From = IRCNick(results[0])
-				msg.Cmd = results[1]
-				msg.To = results[2]
-				if len(results) >= 4 {
-					msg.Msg = results[3]
-				}
-			} else {
-				msg.Cmd = results[0]
-			}
-		*/
 
 		if !Config.Debug_Output {
 			if msg.Cmd == "ERROR" || msg.Cmd[0] == '4' || msg.Cmd[0] == '5' {
@@ -613,21 +567,17 @@ func (Config *IRCConfig) ReaderRoutine() {
 			}
 
 			/*
-				2022/04/06 19:12:11 << :[email protected] NOTICE meow :This nickname is registered and protected.  If it is your
 				2022/04/06 19:12:11 << :[email protected] NOTICE meow :nick, type /msg NickServ IDENTIFY password.  Otherwise,
-				2022/04/06 19:12:11 << :[email protected] NOTICE meow :please choose a different nick.
 			*/
 			if (msg.From == "NickServ") && (msg.Cmd == "NOTICE") {
 				if strings.Contains(msg.Msg, "IDENTIFY") && Config.Password != "" {
 					Config.PriorityWrite(fmt.Sprintf("NS IDENTIFY %s", Config.Password))
 				}
-				// :[email protected] NOTICE meow :Password accepted - you are now recognized.
 			}
 
 			if !Config.UseSASL && (msg.Cmd == "900") {
 				Config.Registered = true
 			}
-
 		}
 
 		// This is a better way of knowing when we've identified for services
@@ -635,17 +585,15 @@ func (Config *IRCConfig) ReaderRoutine() {
 			// This should probably be look for + and contains "r"
 			if (msg.Msg[0] == '+') && (strings.Contains(msg.Msg, "r")) {
 				Config.ReadChannel <- IRCMsg{Cmd: "Identified"}
-				// identified = true
-			}
-			if len(Config.AutoJoin) > 0 {
-				Config.PriorityWrite("JOIN " + strings.Join(Config.AutoJoin, ","))
+
+				if len(Config.AutoJoin) > 0 {
+					Config.PriorityWrite("JOIN " + strings.Join(Config.AutoJoin, ","))
+				}
 			}
 		}
 
 		if msg.Cmd == "KICK" {
-			// Were we kicked, is channel in AutoJoin?
-			// 2022/04/13 20:02:52 << :[email protected] KICK #bugz meow-bot :bugz
-			// Msg: ircclient.IRCMsg{MsgParts:[]string{":[email protected]", "KICK", "#bugz", "meow-bot", "bugz"}, From:"bugz", To:"#bugz", Cmd:"KICK", Msg:"meow-bot"}
+			// We were kicked, is channel in AutoJoin?
 			if msg.MsgParts[3] == Config.MyNick {
 				if Config.IsAuto(msg.To) {
 					// Yes, we were kicked from AutoJoin channel
@@ -654,21 +602,6 @@ func (Config *IRCConfig) ReaderRoutine() {
 			}
 		}
 
-		/*
-			// Needs rate limit, it doesn't always work.  (if they're not on the HOP+ list)
-				if msg.Cmd == "474" && identified {
-
-						:irc.red-green.com 474 meow-bot #chat :Cannot join channel (+b)
-						Msg: ircclient.IRCMsg{MsgParts:[]string{":irc.red-green.com", "474", "meow-bot", "#chat", "Cannot join channel (+b)"}, From:"irc.red-green.com", To:"meow-bot", Cmd:"474", Msg:"#chat"}
-					  :[email protected] NOTICE meow-bot :Access denied.
-					  Msg: ircclient.IRCMsg{MsgParts:[]string{":[email protected]", "NOTICE", "meow-bot", "Access denied."}, From:"ChanServ", To:"meow-bot", Cmd:"NOTICE", Msg:"Access denied."}
-					if Config.IsAuto(msg.MsgParts[3]) {
-						Config.PriorityWrite(fmt.Sprintf("CS UNBAN %s", msg.MsgParts[3]))
-						time.AfterFunc(time.Duration(Config.RejoinDelay)*time.Millisecond, func() { Config.WriteTo(msg.To, "JOIN "+msg.MsgParts[3]) })
-					}
-				}
-		*/
-
 		if Config.ReadChannel != nil {
 			Config.ReadChannel <- msg
 		}
@@ -678,7 +611,9 @@ func (Config *IRCConfig) ReaderRoutine() {
 			var reg IRCMsg = IRCMsg{Cmd: "EndMOTD"}
 			Config.ReadChannel <- reg
 			registering = true
+
 			if Config.Password == "" {
+				// We can't register with services, so join the AutJoin channels.
 				if len(Config.AutoJoin) > 0 {
 					Config.PriorityWrite("JOIN " + strings.Join(Config.AutoJoin, ","))
 				}
@@ -686,9 +621,9 @@ func (Config *IRCConfig) ReaderRoutine() {
 		}
 
 		if msg.Cmd == "NICK" {
-			// :meow NICK :meow-bot
+			// Handle nick changes from the server
 
-			if msg.From == Config.MyNick {
+			if Match(msg.From, Config.MyNick) {
 				Config.SetNick(msg.Msg)
 				if Config.Debug_Output {
 					log.Println("Nick is now:", Config.MyNick)

+ 2 - 1
ircd_test.go

@@ -219,7 +219,8 @@ func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 				} else {
 					if config.MyNick == "bad" {
 						// throw bad nick here
-						ircWrite(server, fmt.Sprintf(":irc.red-green.com 433 :Nick already in use."), t)
+						// ircWrite(server, fmt.Sprintf(":irc.red-green.com 433 :Nick already in use."), t)
+						ircWrite(server, ":irc.red-green.com 433 :Nick already in use.", t)
 					}
 					hasNick = true
 				}

+ 4 - 5
throttle_test.go

@@ -2,7 +2,6 @@ package ircclient
 
 import (
 	"bytes"
-	"fmt"
 	"log"
 	"os"
 	"strings"
@@ -21,7 +20,7 @@ func TestFloodTrack(t *testing.T) {
 	for x := 1; x < 3; x++ {
 		Flood.Save()
 		if Flood.Pos != x {
-			t.Error(fmt.Sprintf("Expected Track Pos to be %d", x))
+			t.Errorf("Expected Track Pos to be %d", x)
 		}
 		if Flood.Full() {
 			t.Error("Expected Track to be empty")
@@ -220,7 +219,7 @@ func TestThrottleBuffer(t *testing.T) {
 	for _, expect = range []string{"msg1", "msg3", "msg2"} {
 		str = buff.pop()
 		if str != expect {
-			t.Error(fmt.Sprintf("Expected %s, got %s", expect, str))
+			t.Errorf("Expected %s, got %s", expect, str)
 		}
 	}
 
@@ -241,7 +240,7 @@ func TestThrottleBuffer(t *testing.T) {
 	str = buff.pop()
 	expect = "msg3"
 	if str != expect {
-		t.Error(fmt.Sprintf("Expected %s, got %s", expect, str))
+		t.Errorf("Expected %s, got %s", expect, str)
 	}
 
 	if buff.Life_sucks {
@@ -256,7 +255,7 @@ func TestThrottleBuffer(t *testing.T) {
 	str = buff.pop()
 	expect = "txt1"
 	if str != expect {
-		t.Error(fmt.Sprintf("Expected %s, got %s", expect, str))
+		t.Errorf("Expected %s, got %s", expect, str)
 	}
 
 	if buff.Life_sucks {