2021-01-16 00:20:50 +03:00
|
|
|
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"
|
2021-01-16 11:06:44 +03:00
|
|
|
msgPrefix = "I love cats! Here's a fact: %s"
|
2021-01-16 00:20:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2021-01-16 10:49:40 +03:00
|
|
|
irccon := irc.IRC(nick, os.Getenv("CTF2021USER"))
|
2021-01-16 00:20:50 +03:00
|
|
|
|
|
|
|
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)
|
2021-01-16 11:06:44 +03:00
|
|
|
e.Connection.Privmsg(channel, `I'm a cat facts 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 ^_^`)
|
|
|
|
e.Connection.Privmsg(channel, `Source code and issue tracker: https://gitea.difrex.ru/CTF2021/irc_bot`)
|
2021-01-16 00:20:50 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|