autobrr/test/mockindexer/irc/server.go
Viktor Elofsson 0f2ce26ba2
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
2022-12-03 16:55:36 +01:00

50 lines
838 B
Go

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