From eef648ac56e5f6ae5f360f932d5ebb9721880a10 Mon Sep 17 00:00:00 2001 From: blackbeard420 Date: Wed, 27 Oct 2021 21:49:24 -0400 Subject: [PATCH] added GetServerInfo shortcut --- irc_helpers.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ irc_test.go | 23 ++++------------------- 2 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 irc_helpers.go diff --git a/irc_helpers.go b/irc_helpers.go new file mode 100644 index 0000000..1341155 --- /dev/null +++ b/irc_helpers.go @@ -0,0 +1,46 @@ +package irc + +import ( + "fmt" + "strconv" + "strings" +) + +type ServerInfo struct { + Users int + Invisible int + Channels int + Servers int +} + +func GetServerInfo(server, nick string) ServerInfo { + users := 0 + invis := 0 + channels := 0 + servers := 0 + + con := NewConnection(server, nick, nick, nil, nil) + + con.NumericCallback = func(from string, i int, args string) { + switch i { + case RPL_LUSERCLIENT: + s := strings.Split(args, ":") + if len(s) > 1 { + fmt.Sscanf(s[1], "There are %d users and %d invisible on %d servers", &users, &invis, &servers) + } + case RPL_LUSERCHANNELS: + s := strings.TrimSpace(strings.Split(args, " ")[1]) + channels, _ = strconv.Atoi(s) + con.SendQuit("") + } + } + + con.Run() + + return ServerInfo{ + Users: users, + Invisible: invis, + Channels: channels, + Servers: servers, + } +} diff --git a/irc_test.go b/irc_test.go index f2b9744..1385fbb 100644 --- a/irc_test.go +++ b/irc_test.go @@ -79,29 +79,14 @@ func TestLusersChannels(t *testing.T) { con.Run() } -func TestMultiClient(t *testing.T) { +func TestGetServerInfo(t *testing.T) { con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel}) - chancnt := make(chan int) - con.NumericCallback = func(from string, i int, args string) { if i == RPL_ENDOFMOTD { - con2 := NewConnection("irc.libera.chat:6667", "irc-go66543", "irc-go66543", nil, nil) - con2.NumericCallback = func(from string, i int, args string) { - if i == RPL_LUSERCHANNELS { - s := strings.TrimSpace(strings.Split(args, " ")[1]) - val, _ := strconv.Atoi(s) - chancnt <- val - } - } - go con2.Run() - - go func() { - resp := <-chancnt - con.SendPrivmsg(channel, fmt.Sprintf("There are %d channels formed on libera", resp)) - con2.SendQuit("") - con.SendQuit("") - }() + res := GetServerInfo("irc.libera.chat:6667", "irc-go4240") + con.SendPrivmsg(channel, fmt.Sprintf("Libera has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible)) + con.SendQuit("Scan Completed") } }