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:
Viktor Elofsson 2022-12-03 16:55:36 +01:00 committed by GitHub
parent ef088c27ad
commit 0f2ce26ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 242 additions and 6 deletions

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