3 Commits
5 changed files with 85 additions and 8 deletions
Generated
+1 -1
View File
@@ -10,7 +10,7 @@
"isupport",
"list"
]
revision = "2dd72f9838d99e084c85de0d8fce290bcc547cbe"
revision = "c1e97eb9d2e383147662ddd649fcab1bdaf295b4"
[[projects]]
name = "github.com/dgrijalva/jwt-go"
+17 -1
View File
@@ -3,6 +3,7 @@ package bot
import (
"context"
"log"
"strings"
"time"
"git.aiterp.net/gisle/irc"
@@ -78,7 +79,7 @@ func (bot *Bot) addChannel(channelName string) *Channel {
return bot.channels[channelName]
}
channel := newChannel(bot.ctx, channelName, bot.client)
channel := newChannel(bot, channelName, bot.client)
go channel.loop()
bot.channels[channelName] = channel
@@ -93,6 +94,21 @@ func (bot *Bot) handlePost(channelName string, post ChannelPost) {
channel.ch <- post
}
func (bot *Bot) runCommands(commands []string, target irc.Target, replacers map[string]string) {
if replacers == nil {
replacers = make(map[string]string)
}
replacers["me"] = bot.client.Nick()
for _, command := range commands {
for from, to := range replacers {
command = strings.Replace(command, "%"+from, to, -1)
}
bot.client.EmitInput(command, target)
}
}
func (bot *Bot) loop() {
bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx)
+45 -4
View File
@@ -7,6 +7,7 @@ import (
"time"
"git.aiterp.net/gisle/irc"
"git.aiterp.net/rpdata/logbot3/internal/config"
"git.aiterp.net/rpdata/logbot3/internal/models/channels"
"git.aiterp.net/rpdata/logbot3/internal/models/logs"
"git.aiterp.net/rpdata/logbot3/internal/models/posts"
@@ -23,15 +24,17 @@ type Channel struct {
name string
parentCtx context.Context
parent *Bot
client *irc.Client
}
func newChannel(parentCtx context.Context, name string, client *irc.Client) *Channel {
func newChannel(parent *Bot, name string, client *irc.Client) *Channel {
ctx, cancel := context.WithCancel(context.Background())
return &Channel{
name: name,
parentCtx: parentCtx,
parent: parent,
parentCtx: parent.ctx,
client: client,
ch: make(chan ChannelPost, 8),
@@ -47,6 +50,7 @@ func (channel *Channel) loop() {
defer channel.cancel()
defer minutely.Stop()
// In case of a crash, it should take ownership of sessions when rejoining.
session, err := logs.FindOpen(channel.parentCtx, channel.name)
if err == nil {
minsUntilDeadline := 1 + int(((time.Hour * 2) - time.Since(session.LatestTime())).Minutes())
@@ -60,12 +64,27 @@ func (channel *Channel) loop() {
channel.lastPostTime = session.LatestTime()
}
// If the bot is op, run the commands from the configuration.
go func() {
time.Sleep(time.Second)
commands := config.Get().Commands.OnJoinOp
target := channel.client.Channel(channel.name)
if len(commands) > 0 && target != nil {
me, ok := target.UserList().User(channel.client.Nick())
if ok && strings.ContainsRune(me.Modes, 'o') {
channel.parent.runCommands(commands, target, map[string]string{
"chan": channel.name,
})
}
}
}()
for {
select {
case post := <-channel.ch:
{
log.Printf("Received %s post from %s", post.Kind, post.Nick)
// Handle bot commands, or add to queue otherwise.
if cmd := post.botCommand(); cmd != nil {
switch cmd.Verb {
@@ -107,6 +126,25 @@ func (channel *Channel) loop() {
log.Println("Could not set event name:", err)
}
}
case "register":
{
target := channel.client.Channel(channel.name)
if target != nil {
me, ok := target.UserList().User(channel.client.Nick())
if !ok || !strings.ContainsRune(me.Modes, 'o') {
channel.client.Say(channel.name, "This unit require operator privileges to serve this request.")
break
}
chanServNick := config.Get().Names.ChanServ
if chanServNick == "" {
channel.client.Say(channel.name, "This function is not enabled.")
break
}
channel.client.Sayf(chanServNick, "REGISTER %s", target.Name())
}
}
}
} else {
queue = append(queue, post)
@@ -116,6 +154,9 @@ func (channel *Channel) loop() {
}
}
// Posts after here aren't going to be bot commands, so log its reception.
log.Printf("Received %s post from %s", post.Kind, post.Nick)
// Stop here if there's nothing to post.
if len(queue) == 0 {
break
+15 -2
View File
@@ -2,6 +2,7 @@ package bot
import (
"log"
"os"
"strings"
"git.aiterp.net/rpdata/logbot3/internal/util"
@@ -22,6 +23,8 @@ func handler(event *irc.Event, client *irc.Client) {
{
log.Println("Client is ready!")
go bot.loop()
bot.runCommands(config.Get().Commands.OnReady, bot.client.Status(), nil)
}
case "client.disconnect":
{
@@ -114,10 +117,20 @@ func handler(event *irc.Event, client *irc.Client) {
{
log.Printf("(%s) %s\n", event.Verb(), event.Text)
}
case "packet.005", "packet.254", "packet.cap", "packet.431", "packet.432", "packet.433", "packet.436":
case "packet.005", "packet.cap", "packet.254":
{
log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
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)
}
}
+7
View File
@@ -26,6 +26,13 @@ type Config struct {
Secret string `json:"secret"`
} `json:"key"`
} `json:"api"`
Commands struct {
OnJoinOp []string `json:"onJoinOp"`
OnReady []string `json:"onReady"`
} `json:"commands"`
Names struct {
ChanServ string `json:"chanserv"`
}
}
var mutex sync.Mutex