mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +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"))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue