CTF2021/irc_bot
Archived
4
0
This repository has been archived on 2021-02-12. You can view files and clone it, but cannot push or open issues or pull requests.
irc_bot/main.go

223 lines
4.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"sync"
"time"
"log"
irc "github.com/thoj/go-ircevent"
)
var channel = "#ctf"
var sendHi = ""
const (
pattern = "(?i)\\b(cat|gato|miau|meow|garfield|lolcat|кот|кошк)[s|z]{0,1}\\b"
nyastat = "nyastat"
msgPrefix = "I love cats! Here's a fact: %s ^_^"
gifPrefix = "Meow! Here's a gif: %s ^_^"
)
// environment variables
const (
userEnv = "IRC_USER"
nickEnv = "IRC_NICK"
himsgEnv = "IRC_SEY_HI"
passEnv = "IRC_PASS"
chanEnv = "IRC_CHAN"
urlEnv = "IRC_URL"
)
type catFact struct {
Fact string `json:"fact"`
Length int `json:"length"`
}
var (
re = regexp.MustCompile(pattern)
catFactsURL = "http://catfact.ninja/fact"
)
type stats struct {
userMessages map[string]uint
mux *sync.Mutex
}
func newStats() *stats {
return &stats{
userMessages: make(map[string]uint),
mux: &sync.Mutex{},
}
}
func (s *stats) addUser(user string) {
s.mux.Lock()
defer s.mux.Unlock()
if v, ok := s.userMessages[user]; ok {
log.Printf("User %s writes %d message", user, v+1)
s.userMessages[user]++
return
}
log.Printf("First message from user %s", user)
s.userMessages[user] = 1
}
func (s *stats) online(e *irc.Event) int {
e.Connection.Mutex.Lock()
defer e.Connection.Mutex.Unlock()
return -1
}
func (s *stats) realStats(e *irc.Event) {
e.Connection.Privmsg(channel, "🙀🙀🙀 WoW 🙀🙀🙀 NYAStat 🙀🙀🙀")
t := "User %s sent %d messages"
for u, v := range s.userMessages {
e.Connection.Privmsg(channel, fmt.Sprintf(t, u, v))
}
}
func (s *stats) printStats(e *irc.Event) {
s.online(e)
s.realStats(e)
}
func addCallbacks(irccon *irc.Connection, s *stats) {
irccon.AddCallback("001", func(e *irc.Event) {
log.Println("Welcome message:", e.Message())
// Login
e.Connection.Privmsg("nickserv", "identify "+os.Getenv(passEnv))
// Join to channel
e.Connection.Join(channel)
// Greetings
if sendHi != "" {
e.Connection.Privmsg(channel, `Hi!`)
e.Connection.Privmsg(channel, `I'm a cat facts bot!`)
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())
s.addUser(e.Nick)
doCommand(e.Message(), e, s)
})
}
func main() {
nick := os.Getenv(nickEnv)
irccon := irc.IRC(nick, os.Getenv(userEnv))
s := newStats()
irccon.VerboseCallbackHandler = false
irccon.Debug = false
irccon.UseTLS = false
addCallbacks(irccon, s)
err := irccon.Connect(os.Getenv(urlEnv))
if err != nil {
fmt.Printf("Err %s", err)
return
}
irccon.Loop()
}
func doCommand(command string, e *irc.Event, s *stats) {
// if ok := statCommand(command); ok {
// s.printStats(e)
// return
// }
if ok := checkCommad(command, e.Connection); !ok {
return
}
rand.Seed(time.Now().UnixNano())
i := rand.Intn(50)
fmt.Println(i)
if i > 30 {
catGif(e.Connection)
return
}
catFacts(e.Connection)
}
func statCommand(command string) bool {
if command == nyastat {
return true
}
return false
}
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 getJson(url string, v interface{}) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return json.Unmarshal(body, v)
}
func catFacts(con *irc.Connection) (string, error) {
data := &catFact{}
err := 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
}
func init() {
ch := os.Getenv(chanEnv)
if ch != "" {
channel = ch
}
sendHi = os.Getenv(himsgEnv)
}