added GetServerInfo shortcut
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
blackbeard420 2021-10-27 21:49:24 -04:00
parent be45f6f2d9
commit eef648ac56
2 changed files with 50 additions and 19 deletions

46
irc_helpers.go Normal file
View File

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

View File

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