diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-25 01:02:57 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-25 10:13:59 +0800 |
commit | 31358bfdf570e1b482fe4eefa75b5de4063e0ef1 (patch) | |
tree | 740cc068996c87df0187ac822ab36c479651283c | |
parent | No wonder that didn't work (diff) | |
download | powxy-31358bfdf570e1b482fe4eefa75b5de4063e0ef1.tar.gz powxy-31358bfdf570e1b482fe4eefa75b5de4063e0ef1.tar.zst powxy-31358bfdf570e1b482fe4eefa75b5de4063e0ef1.zip |
configurable irc logging
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | flags.go | 16 | ||||
-rw-r--r-- | irc.go | 23 | ||||
-rw-r--r-- | irclog.go | 2 |
4 files changed, 37 insertions, 18 deletions
@@ -86,6 +86,20 @@ Usage of ./powxy: leading zero bits required for the challenge (default 20) -idle-timeout int idle timeout in seconds, 0 for no timeout + -irc-addr string + irc server address (default "irc.runxiyu.org:6697") + -irc-channel string + irc channel (default "#logs") + -irc-net string + irc network transport (default "tcp") + -irc-nick string + irc nick (default "powxy") + -irc-realname string + irc realname (default "powxy") + -irc-tls + irc tls (default true) + -irc-username string + irc username (default "powxy") -listen string address to listen on (default ":8081") -read-header-timeout int @@ -16,6 +16,14 @@ var ( writeTimeout int idleTimeout int readHeaderTimeout int + ircAddr string + ircNet string + ircTLS bool + ircChannel string + ircNick string + ircUsername string + ircRealname string + ircBuf uint ) // This init parses command line flags. @@ -29,6 +37,14 @@ func init() { flag.IntVar(&writeTimeout, "write-timeout", 0, "write timeout in seconds, 0 for no timeout") flag.IntVar(&idleTimeout, "idle-timeout", 0, "idle timeout in seconds, 0 for no timeout") flag.IntVar(&readHeaderTimeout, "read-header-timeout", 30, "read header timeout in seconds, 0 for no timeout") + flag.StringVar(&ircAddr, "irc-addr", "irc.runxiyu.org:6697", "irc server address") + flag.StringVar(&ircNet, "irc-net", "tcp", "irc network transport") + flag.BoolVar(&ircTLS, "irc-tls", true, "irc tls") + flag.StringVar(&ircChannel, "irc-channel", "#logs", "irc channel") + flag.StringVar(&ircNick, "irc-nick", "powxy", "irc nick") + flag.StringVar(&ircUsername, "irc-username", "powxy", "irc username") + flag.StringVar(&ircRealname, "irc-realname", "powxy", "irc realname") + flag.UintVar(&ircBuf, "irc-buf", 3000, "irc buffer size") flag.Parse() global.NeedBitsReverse = sha256.Size - global.NeedBits } @@ -3,7 +3,6 @@ package main import ( "crypto/tls" "fmt" - "math/rand" irc "go.lindenii.runxiyu.org/lindenii-irc" ) @@ -19,7 +18,7 @@ type errorBack[T any] struct { } func ircBotSession() error { - underlyingConn, err := tls.Dial("tcp", "irc.runxiyu.org:6697", nil) + underlyingConn, err := tls.Dial(ircNet, ircAddr, nil) if err != nil { return err } @@ -31,16 +30,11 @@ func ircBotSession() error { return conn.WriteString(s + "\r\n") } - suffix := "" - for i := 0; i < 5; i++ { - suffix += string(rand.Intn(26) + 97) - } - - _, err = logAndWriteLn("NICK :powxy-" + suffix) + _, err = logAndWriteLn("NICK :" + ircNick) if err != nil { return err } - _, err = logAndWriteLn("USER powxy 0 * :powxy") + _, err = logAndWriteLn("USER " + ircUsername + " 0 * :" + ircRealname) if err != nil { return err } @@ -65,7 +59,7 @@ func ircBotSession() error { switch msg.Command { case "001": - _, err = logAndWriteLn("JOIN #logs") + _, err = logAndWriteLn("JOIN " + ircChannel) if err != nil { readLoopError <- err return @@ -80,14 +74,9 @@ func ircBotSession() error { c, ok := msg.Source.(irc.Client) if !ok { } - if c.Nick != "powxy"+suffix { + if c.Nick != ircNick { continue } - _, err = logAndWriteLn("PRIVMSG #logs :test") - if err != nil { - readLoopError <- err - return - } default: } } @@ -130,7 +119,7 @@ func ircSendDirect(s string) error { } func ircBotLoop() { - ircSendBuffered = make(chan string, 3000) + ircSendBuffered = make(chan string, ircBuf) ircSendDirectChan = make(chan errorBack[string]) for { @@ -28,7 +28,7 @@ func (h *IRCLogHandler) Enabled(_ context.Context, level slog.Level) bool { func (h *IRCLogHandler) Handle(_ context.Context, r slog.Record) error { var sb strings.Builder - sb.WriteString("PRIVMSG #logs :") + sb.WriteString("PRIVMSG " + ircChannel + " :") sb.WriteString(r.Message) |