added GetServerInfo shortcut
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
be45f6f2d9
commit
eef648ac56
46
irc_helpers.go
Normal file
46
irc_helpers.go
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
23
irc_test.go
23
irc_test.go
@ -79,29 +79,14 @@ func TestLusersChannels(t *testing.T) {
|
|||||||
con.Run()
|
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})
|
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) {
|
con.NumericCallback = func(from string, i int, args string) {
|
||||||
if i == RPL_ENDOFMOTD {
|
if i == RPL_ENDOFMOTD {
|
||||||
con2 := NewConnection("irc.libera.chat:6667", "irc-go66543", "irc-go66543", nil, nil)
|
res := GetServerInfo("irc.libera.chat:6667", "irc-go4240")
|
||||||
con2.NumericCallback = func(from string, i int, args string) {
|
con.SendPrivmsg(channel, fmt.Sprintf("Libera has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible))
|
||||||
if i == RPL_LUSERCHANNELS {
|
con.SendQuit("Scan Completed")
|
||||||
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("")
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user