diff --git a/README.md b/README.md index 4b9125b..fffd221 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # irc_bot +About a cats + +```go +go build +``` + +```sh +export CTF2021NICK="HaX0r666" +export CTF2021PASS="passw0rd" +export CTF2021URL="localhost:6667" +./ctf2021_ircbot +``` + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ce3cf6 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/Difrex/ctf2021_ircbot + +go 1.15 + +require ( + github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f + github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d29facb --- /dev/null +++ b/go.sum @@ -0,0 +1,7 @@ +github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f h1:To58XdIp+Sw9QoyhJE3oh2KMZKoj4Ta8W/ltrA+cL68= +github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f/go.mod h1:Ga63x4EC4NFYr/KGzhn8D8fLj89sfJA/dpBsuowiHOQ= +github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb h1:EavwSqheIJl3nb91HhkL73DwnT2Fk8W3yM7T7TuLZvA= +github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb/go.mod h1:I0ZT9x8wStY6VOxtNOrLpnDURFs7HS0z1e1vhuKUEVc= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3ea4d8d --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "log" + "os" + "regexp" + + "github.com/go-chat-bot/plugins/web" + irc "github.com/thoj/go-ircevent" +) + +const channel = "#ctf" + +const ( + pattern = "(?i)\\b(cat|gato|miau|meow|garfield|lolcat)[s|z]{0,1}\\b" + msgPrefix = "[BOT] I love cats! Here's a fact: %s" +) + +type catFact struct { + Fact string `json:"fact"` + Length int `json:"length"` +} + +var ( + re = regexp.MustCompile(pattern) + catFactsURL = "http://catfact.ninja/fact" +) + +func main() { + nick := os.Getenv("CTF2021NICK") + irccon := irc.IRC(nick, nick) + + irccon.VerboseCallbackHandler = false + irccon.Debug = false + irccon.UseTLS = false + + irccon.AddCallback("001", func(e *irc.Event) { + log.Println("Welcome message:", e.Message()) + + e.Connection.Privmsg("nickserv", "identify "+os.Getenv("CTF2021PASS")) + + e.Connection.Join(channel) + e.Connection.Privmsg(channel, `[BOT] I'm a cat facts bot!`) + e.Connection.Privmsg(channel, `[BOT] Inspired from https://github.com/go-chat-bot/plugins/blob/master/catfacts/catfacts.go`) + e.Connection.Privmsg(channel, `[BOT] Try cat /etc/passwd ^_^`) + e.Connection.Privmsg(channel, `[BOT] Source code and issue tracker: https://gitea.difrex.ru/CTF2021/irc_bot`) + }) + + irccon.AddCallback("PRIVMSG", func(e *irc.Event) { + log.Printf("Message from %s received: %s", e.Nick, e.Message()) + catFacts(e.Message(), e.Connection) + }) + + irccon.AddCallback("", func(e *irc.Event) {}) + err := irccon.Connect(os.Getenv("CTF2021URL")) + if err != nil { + fmt.Printf("Err %s", err) + return + } + irccon.Loop() +} + +func catFacts(command string, con *irc.Connection) (string, error) { + if !re.MatchString(command) { + return "", nil + } + data := &catFact{} + err := web.GetJSON(catFactsURL, data) + if err != nil { + return "", err + } + + if len(data.Fact) == 0 { + return "", nil + } + + con.Privmsg(channel, fmt.Sprintf(msgPrefix, data.Fact)) + return fmt.Sprintf(msgPrefix, data.Fact), nil +}