feat(mockindexer): support feeds and webhooks (#1361)

This commit is contained in:
ze0s 2024-01-21 12:12:34 +01:00 committed by GitHub
parent f488c88f1b
commit c377bc9157
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 346 additions and 49 deletions

View file

@ -49,7 +49,7 @@ func (c *Client) readerLoop() {
line := scanner.Text()
cmd := strings.Split(line, " ")
log.Printf("%s", scanner.Text())
log.Printf("--> %s", scanner.Text())
c.handler(c, cmd)
}
@ -57,6 +57,7 @@ func (c *Client) readerLoop() {
func (c *Client) writerLoop() {
for cmd := range c.writer {
log.Printf("<-- %s", []byte(cmd+"\r\n"))
c.conn.Write([]byte(cmd + "\r\n"))
}
}

View file

@ -7,6 +7,7 @@ import (
"fmt"
"log"
"strings"
"time"
)
func RegistrationHandler(c *Client, cmd []string) {
@ -23,21 +24,35 @@ func RegistrationHandler(c *Client, cmd []string) {
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)
c.writer <- fmt.Sprintf(":localhost 001 %s :Welcome %s", c.nick, c.nick)
c.writer <- fmt.Sprintf(":localhost 002 %s :Your host is localhost, running mock-irc-0.0.1", c.nick)
c.writer <- fmt.Sprintf(":localhost 003 %s :This server was created %s", c.nick, time.Now().String())
c.writer <- fmt.Sprintf(":localhost 004 %s localhost mock-irc-0.0.1 o o o", c.nick)
c.writer <- fmt.Sprintf(":localhost 251 %s :there are 1 users on 1 server", c.nick)
c.writer <- fmt.Sprintf(":localhost 422 %s :MOTD File is missing", c.nick)
}
}
func CommandHandler(c *Client, cmd []string) {
log.Printf("cmd: %+v", cmd)
switch cmd[0] {
case "CAP":
log.Printf("caps: %+v", cmd)
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)
c.writer <- fmt.Sprintf(":localhost 221 %s +Zi", c.nick)
c.writer <- fmt.Sprintf(":localhost 331 %s %s :No topic", c.nick, c.channelName)
c.writer <- fmt.Sprintf(":localhost 353 %s = %s :%s %s", c.nick, c.channelName, c.nick, c.botName)
c.writer <- fmt.Sprintf(":localhost 366 %s %s :End of NAMES list", c.nick, c.channelName)
case "PING":
c.writer <- fmt.Sprintf("PONG n %s", strings.Join(cmd[1:], " "))
c.writer <- fmt.Sprintf(":localhost PONG localhost %s", strings.Join(cmd[1:], " "))
case "PRIVMSG":
c.writer <- fmt.Sprintf("%s PRIVMSG %s %s", fmt.Sprintf(":%s", c.nick), cmd[1], fmt.Sprintf("%s", strings.Join(cmd[2:], " ")))
c.writer <- fmt.Sprintf(":localhost %s PRIVMSG %s %s", fmt.Sprintf(":%s", c.nick), cmd[1], fmt.Sprintf("%s", strings.Join(cmd[2:], " ")))
case "QUIT":
c.writer <- fmt.Sprintf(":%s!%s@localhost QUIT :Quit%s", c.nick, c.nick, strings.Join(cmd[1:], " "))
//c.writer <- fmt.Sprintf(":localhost %s!%s@localhost QUIT :Quit%s", c.nick, c.nick, strings.Join(cmd[1:], " "))
//c.writer <- fmt.Sprintf(":localhost :%s@localhost QUIT :Quit%s", c.nick, strings.Join(cmd[1:], " "))
c.writer <- fmt.Sprintf("ERROR :Quit%s", strings.Join(cmd[1:], " "))
case "ERROR":
c.writer <- fmt.Sprintf("ERROR :Quit%s", strings.Join(cmd[1:], " "))
}
}

View file

@ -21,12 +21,13 @@ type ServerOptions struct {
}
func NewServer(options *ServerOptions) (*Server, error) {
listener, err := net.Listen("tcp", "localhost:6697")
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,
@ -36,7 +37,6 @@ func NewServer(options *ServerOptions) (*Server, error) {
func (s *Server) Run() {
for {
conn, err := s.listener.Accept()
if err != nil {
log.Printf("Failed accept: %v", err)
continue