From 9a132559c44f025c0a73915b3fe418751294089b Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 22 Mar 2025 02:31:41 +0800 Subject: Add rudimentary IRC logic --- config.go | 5 +++++ go.mod | 1 + go.sum | 2 ++ irc.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 +++ 5 files changed, 57 insertions(+) create mode 100644 irc.go diff --git a/config.go b/config.go index 8cd76c9..9a5647f 100644 --- a/config.go +++ b/config.go @@ -35,6 +35,11 @@ var config struct { Key string `scfg:"key"` Root string `scfg:"root"` } `scfg:"ssh"` + IRC struct { + Net string `scfg:"net"` + Addr string `scfg:"addr"` + TLS bool `scfg:"tls"` + } `scfg:"irc"` General struct { Title string `scfg:"title"` } `scfg:"general"` diff --git a/go.mod b/go.mod index 0907202..d9bbb9c 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/tdewolff/minify/v2 v2.22.3 github.com/yuin/goldmark v1.7.8 go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250321131425-dda3538a9cd4 + go.lindenii.runxiyu.org/lindenii-irc v0.0.0-20250321180239-62045c958b0d golang.org/x/crypto v0.36.0 ) diff --git a/go.sum b/go.sum index 0fef5af..839685e 100644 --- a/go.sum +++ b/go.sum @@ -110,6 +110,8 @@ github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250321131425-dda3538a9cd4 h1:xX6s8+Yo5fRHzVswlJvKQjjN6lZCG7lAh33dTXBqsYE= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250321131425-dda3538a9cd4/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= +go.lindenii.runxiyu.org/lindenii-irc v0.0.0-20250321180239-62045c958b0d h1:ET8nGNEI6IGHAKyB7zSsI1xX3x9uyolUFqA/BP3fb3Y= +go.lindenii.runxiyu.org/lindenii-irc v0.0.0-20250321180239-62045c958b0d/go.mod h1:fE6Ks8GK7PHZGAPkTWG593UmF7FmyugcRcqmey3Nvy0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= diff --git a/irc.go b/irc.go new file mode 100644 index 0000000..fc9e0af --- /dev/null +++ b/irc.go @@ -0,0 +1,46 @@ +package main + +import ( + "crypto/tls" + "net" + + "go.lindenii.runxiyu.org/lindenii-irc" +) + +func ircBotSession() error { + var err error + var underlyingConn net.Conn + if config.IRC.TLS { + underlyingConn, err = tls.Dial(config.IRC.Net, config.IRC.Addr, nil) + } else { + underlyingConn, err = net.Dial(config.IRC.Net, config.IRC.Addr) + } + if err != nil { + return (err) + } + conn := irc.NewConn(underlyingConn) + conn.WriteString("NICK forge\r\nUSER forge 0 * :Forge\r\n") + for { + msg, err := conn.ReadMessage() + if err != nil { + return (err) + } + switch msg.Command { + case "001": + conn.WriteString("JOIN #chat\r\n") + case "PING": + conn.WriteString("PONG :") + conn.WriteString(msg.Args[0]) + conn.WriteString("\r\n") + case "JOIN": + conn.WriteString("PRIVMSG #chat :test\r\n") + default: + } + } +} + +func ircBotLoop() { + for { + _ = ircBotSession() + } +} diff --git a/main.go b/main.go index d92d816..d2b1069 100644 --- a/main.go +++ b/main.go @@ -93,5 +93,8 @@ func main() { } }() + // IRC bot + go ircBotLoop() + select {} } -- cgit v1.2.3