Code
This commit is contained in:
parent
630bcd51de
commit
9c0f715238
13
README.md
13
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
|
||||
```
|
||||
|
||||
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -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
|
||||
)
|
7
go.sum
Normal file
7
go.sum
Normal file
@ -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=
|
80
main.go
Normal file
80
main.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user