81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
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, os.Getenv("CTF2021USER"))
|
|
|
|
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
|
|
}
|