The new logbot, not committed from the wrong terminal window this time.
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.
 
 

141 lines
3.3 KiB

package bot
import (
"log"
"os"
"strings"
"github.com/gissleh/irc"
"git.aiterp.net/rpdata/logbot3/internal/config"
"git.aiterp.net/rpdata/logbot3/internal/util"
)
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()
bot.runCommands(config.Get().Commands.OnReady, bot.client.Status(), nil)
}
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":
{
// The echo-message polyfill will return to sender.
if event.Nick == bot.client.Nick() {
break
}
// Only continue with channel messages, but log the others.
channel := event.ChannelTarget()
if channel == nil {
if event.Verb() == "action" {
log.Printf("Message: * %s %s", event.Nick, event.Text)
} else {
log.Printf("Message: <%s> %s", event.Nick, event.Text)
}
break
}
// Parse account, just in case it'll be useful one day.
account := ""
if user, ok := bot.client.FindUser(event.Nick); ok {
account = user.Account
}
// Get the kind of psot this is.
kind := ""
if strings.HasPrefix(event.Nick, "=") || strings.HasPrefix(event.Nick, "\x1F=") {
kind = "scene"
} else if strings.ToLower(event.Verb()) == "action" {
kind = "action"
} else {
kind = "text"
}
// Format NPC posts' nick and text.
nick := event.Nick
text := event.Text
if strings.HasPrefix(event.Nick, "\x1F") || kind == "scene" {
nick = strings.Replace(event.Nick, "\x1F", "", 2)
text = util.RemoveNPCAttribution(event.Text)
}
// Do not dispatch empty messages.
if len(text) < 1 || len(nick) < 1 || len(kind) < 1 {
log.Println("Ignoring empty message from:", event.Nick)
break
}
// Dispatch it.
bot.handlePost(channel.Name(), ChannelPost{
Account: account,
Kind: kind,
Nick: nick,
Time: event.Time,
Text: text,
})
}
// Log notices
case "packet.notice":
{
log.Printf("Message: -%s- %s", event.Nick, event.Text)
}
// Log initial numerics for debugging
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.cap", "packet.254":
{
log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args[1:], " "), event.Text)
}
case "packet.error":
{
log.Printf("(%s) %s\n", event.Verb(), event.Text)
os.Exit(1)
}
}
// Always log error numerics.
if len(event.Verb()) == 3 && event.Verb()[0] == '4' || event.Verb()[0] == '9' {
log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
}
}