autobrr/test/mockindexer/irc/server.go
soup 0391629862
chore(license): update copyright year in headers (#1929)
* chore: update copyright year in license headers

* Revert "chore: update copyright year in license headers"

This reverts commit 3e58129c431b9a491089ce36b908f9bb6ba38ed3.

* chore: update copyright year in license headers

* fix: sort go imports

* fix: add missing license headers
2025-01-06 22:23:19 +01:00

53 lines
1,007 B
Go

// Copyright (c) 2021 - 2025, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
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", ":6697")
if err != nil {
return nil, err
}
log.Printf("IRC server running on %q", listener.Addr())
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)
}
}