package main import ( "fmt" "log" "math/rand" "net/http" "os" "regexp" "time" "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 = "I love cats! Here's a fact: %s ^_^" gifPrefix = "Meow! Here's a gif: %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, `I'm a cat bot!`) e.Connection.Privmsg(channel, `Inspired from https://github.com/go-chat-bot/plugins/blob/master/catfacts/catfacts.go`) e.Connection.Privmsg(channel, `Try cat /etc/passwd :p`) e.Connection.Privmsg(channel, `Source code and issue tracker: https://gitea.difrex.ru/CTF2021/irc_bot`) e.Connection.Privmsg(channel, `Now with gifs! ^_^`) }) irccon.AddCallback("PRIVMSG", func(e *irc.Event) { log.Printf("Message from %s received: %s", e.Nick, e.Message()) doCommand(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 doCommand(command string, con *irc.Connection) { if ok := checkCommad(command, con); !ok { return } rand.Seed(time.Now().UnixNano()) i := rand.Intn(50) fmt.Println(i) if i > 30 { catGif(con) return } catFacts(con) } func checkCommad(command string, con *irc.Connection) bool { if !re.MatchString(command) { return false } return true } func catGif(con *irc.Connection) (string, error) { res, err := http.Get("http://thecatapi.com/api/images/get?format=src&type=gif") if err != nil { return "", err } con.Privmsg(channel, fmt.Sprintf(gifPrefix, res.Request.URL.String())) return fmt.Sprintf(gifPrefix, res.Request.URL.String()), nil } func catFacts(con *irc.Connection) (string, error) { 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 }