You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
1.9 KiB
103 lines
1.9 KiB
package bot
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
|
|
"git.aiterp.net/gisle/irc"
|
|
"git.aiterp.net/rpdata/logbot3/internal/config"
|
|
)
|
|
|
|
func handler(event *irc.Event, client *irc.Client) {
|
|
bot, ok := client.Value(botKey).(*Bot)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
switch event.Name() {
|
|
// Handle bot loop
|
|
case "hook.ready":
|
|
{
|
|
log.Println("Client is ready!")
|
|
go bot.loop()
|
|
}
|
|
case "client.disconnect":
|
|
{
|
|
log.Println("Client disconnected!")
|
|
bot.stopLoop()
|
|
|
|
conf := config.Get().Server
|
|
go bot.Connect(conf.Address, conf.SSL, 0)
|
|
}
|
|
|
|
// Log joins and leaves
|
|
case "packet.join":
|
|
{
|
|
if client.Nick() == event.Nick {
|
|
bot.addChannel(event.Arg(0))
|
|
}
|
|
|
|
log.Println("(JOIN)", event.Nick, "joined", event.Arg(0))
|
|
}
|
|
case "packet.part":
|
|
{
|
|
log.Println("(PART)", event.Nick, "left", event.Arg(0))
|
|
}
|
|
case "packet.quit":
|
|
{
|
|
log.Println("(QUIT)", event.Nick, "quit")
|
|
}
|
|
|
|
case "ctcp.action", "packet.privmsg":
|
|
{
|
|
if event.Nick == bot.client.Nick() {
|
|
break
|
|
}
|
|
|
|
channel := event.ChannelTarget()
|
|
if channel == nil {
|
|
break
|
|
}
|
|
|
|
account := ""
|
|
if user, ok := bot.client.FindUser(event.Nick); ok {
|
|
account = user.Account
|
|
}
|
|
|
|
kind := ""
|
|
if event.Verb() == "action" {
|
|
kind = "action"
|
|
} else {
|
|
if strings.HasPrefix(event.Nick, "=") {
|
|
kind = "scene"
|
|
} else {
|
|
kind = "text"
|
|
}
|
|
}
|
|
|
|
post := ChannelPost{
|
|
Account: account,
|
|
Kind: kind,
|
|
Nick: event.Nick,
|
|
Time: event.Time,
|
|
Text: event.Text,
|
|
}
|
|
|
|
bot.handlePost(channel.Name(), post)
|
|
}
|
|
|
|
// Log initial numerics for debugging's sake
|
|
case "packet.001", "packet.002", "packet.003", "packet.251", "packet.255", "packet.265", "packet.266", "packet.250", "packet.375", "packet.372", "packet.376":
|
|
{
|
|
log.Printf("(%s) %s\n", event.Verb(), event.Text)
|
|
}
|
|
case "packet.005", "packet.254":
|
|
{
|
|
log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
irc.Handle(handler)
|
|
}
|