mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(tools): Add a simple MockIndexer helper for IRC announcing and indexing (#555)
* Add a simple SelfIndexer for IRC announcing and indexing * Rename to MockIndexer * fix: close file after reading
This commit is contained in:
parent
ef088c27ad
commit
0f2ce26ba2
7 changed files with 242 additions and 6 deletions
59
test/mockindexer/irc/client.go
Normal file
59
test/mockindexer/irc/client.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn net.Conn
|
||||
writer chan string
|
||||
|
||||
botName string
|
||||
channelName string
|
||||
nick string
|
||||
user string
|
||||
|
||||
handler func(c *Client, cmd []string)
|
||||
}
|
||||
|
||||
type ClientHandler interface {
|
||||
Handle(c Client, cmd []string)
|
||||
}
|
||||
|
||||
func NewClient(conn net.Conn, botName, channelName string) *Client {
|
||||
client := &Client{
|
||||
botName: botName,
|
||||
channelName: channelName,
|
||||
conn: conn,
|
||||
writer: make(chan string),
|
||||
}
|
||||
|
||||
client.handler = RegistrationHandler
|
||||
|
||||
go client.readerLoop()
|
||||
go client.writerLoop()
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func (c *Client) readerLoop() {
|
||||
scanner := bufio.NewScanner(c.conn)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
cmd := strings.Split(line, " ")
|
||||
|
||||
log.Printf("%s", scanner.Text())
|
||||
|
||||
c.handler(c, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) writerLoop() {
|
||||
for cmd := range c.writer {
|
||||
c.conn.Write([]byte(cmd + "\r\n"))
|
||||
}
|
||||
}
|
38
test/mockindexer/irc/handlers.go
Normal file
38
test/mockindexer/irc/handlers.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegistrationHandler(c *Client, cmd []string) {
|
||||
switch cmd[0] {
|
||||
case "NICK":
|
||||
c.nick = cmd[1]
|
||||
break
|
||||
case "USER":
|
||||
c.user = cmd[1]
|
||||
}
|
||||
|
||||
if c.nick != "" && c.user != "" {
|
||||
log.Printf("Logged in\n")
|
||||
|
||||
c.handler = CommandHandler
|
||||
|
||||
c.writer <- fmt.Sprintf(
|
||||
"001 %s :\r\n002 %s :\r\n003 %s :\r\n004 %s n n-d o o\r\n251 %s :\r\n422 %s :",
|
||||
c.nick, c.nick, c.nick, c.nick, c.nick, c.nick)
|
||||
}
|
||||
}
|
||||
|
||||
func CommandHandler(c *Client, cmd []string) {
|
||||
switch cmd[0] {
|
||||
case "JOIN":
|
||||
c.writer <- fmt.Sprintf("331 %s %s :No topic", c.nick, c.channelName)
|
||||
c.writer <- fmt.Sprintf("353 %s = %s :%s %s", c.nick, c.channelName, c.nick, c.botName)
|
||||
c.writer <- fmt.Sprintf("366 %s %s :End", c.nick, c.channelName)
|
||||
case "PING":
|
||||
c.writer <- fmt.Sprintf("PONG n %s", strings.Join(cmd[1:], " "))
|
||||
}
|
||||
}
|
50
test/mockindexer/irc/server.go
Normal file
50
test/mockindexer/irc/server.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
listener net.Listener
|
||||
clients []*Client
|
||||
options *ServerOptions
|
||||
}
|
||||
|
||||
type ServerOptions struct {
|
||||
BotName string
|
||||
Channel string
|
||||
}
|
||||
|
||||
func NewServer(options *ServerOptions) (*Server, error) {
|
||||
listener, err := net.Listen("tcp", "localhost:6697")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Server{
|
||||
listener: listener,
|
||||
options: options,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed accept: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
s.clients = append(s.clients, NewClient(conn, s.options.BotName, s.options.Channel))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) SendAll(line string) {
|
||||
for _, client := range s.clients {
|
||||
client.writer <- fmt.Sprintf(":%s PRIVMSG %s :%s", s.options.BotName, s.options.Channel, line)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue