Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2d029f918 | ||
|
|
293102c1c6 | ||
|
|
de04d6069b |
@@ -4,5 +4,5 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/gissleh/irc v0.0.0-20200720173331-512ba5aa6fca
|
||||
github.com/gissleh/irc v0.0.0-20200720191144-af5dc31ff33d
|
||||
)
|
||||
|
||||
@@ -2,3 +2,5 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/gissleh/irc v0.0.0-20200720173331-512ba5aa6fca h1:vTL3kM7PkVnTy77i2/ewaC0VDmSEF7sAOMDTNIs6rKY=
|
||||
github.com/gissleh/irc v0.0.0-20200720173331-512ba5aa6fca/go.mod h1:h2/fPlbnb11gFpitk0HGxYLgK+bCKGh55QSYNo36X8M=
|
||||
github.com/gissleh/irc v0.0.0-20200720191144-af5dc31ff33d h1:PbjXoijU290Y6qTD/LM9yyFTWgX8iJtPMQ4Adwsn954=
|
||||
github.com/gissleh/irc v0.0.0-20200720191144-af5dc31ff33d/go.mod h1:h2/fPlbnb11gFpitk0HGxYLgK+bCKGh55QSYNo36X8M=
|
||||
|
||||
+27
-34
@@ -6,9 +6,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gissleh/irc"
|
||||
"git.aiterp.net/rpdata/logbot3/internal/models"
|
||||
"git.aiterp.net/rpdata/logbot3/internal/models/channels"
|
||||
"github.com/gissleh/irc"
|
||||
)
|
||||
|
||||
var botKey = "git.aiterp.net/rpdata/logbot.Bot.key"
|
||||
@@ -54,6 +53,8 @@ func (bot *Bot) Connect(server string, ssl bool, maxRetries int) (err error) {
|
||||
}
|
||||
bot.ctx, bot.ctxCancel = context.WithCancel(bot.client.Context())
|
||||
|
||||
bot.channels = make(map[string]*Channel)
|
||||
|
||||
retries := 0
|
||||
for maxRetries == 0 || retries < maxRetries {
|
||||
err = bot.client.Connect(server, ssl)
|
||||
@@ -118,45 +119,37 @@ func (bot *Bot) runCommands(commands []string, target irc.Target, replacers map[
|
||||
func (bot *Bot) loop() {
|
||||
bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx)
|
||||
|
||||
channelChanges, err := channels.SubscribeLogged(bot.ctx)
|
||||
if err != nil {
|
||||
log.Println("Failed to get channel changes:", err)
|
||||
return
|
||||
}
|
||||
ticker := time.NewTicker(time.Second * 10)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case channel := <-channelChanges:
|
||||
case <-ticker.C:
|
||||
{
|
||||
channels := []models.Channel{channel}
|
||||
deadline := time.After(time.Second * 1)
|
||||
buffering := true
|
||||
for buffering {
|
||||
select {
|
||||
case channel := <-channelChanges:
|
||||
channels = append(channels, channel)
|
||||
case <-deadline:
|
||||
buffering = false
|
||||
}
|
||||
loggedChannels, err := channels.ListLogged(bot.ctx)
|
||||
if err != nil {
|
||||
log.Println("Failed to list logged channels:", err)
|
||||
continue
|
||||
}
|
||||
loggedMap := make(map[string]bool)
|
||||
for _, channel := range loggedChannels {
|
||||
loggedMap[channel.Name] = true
|
||||
}
|
||||
clientMap := make(map[string]bool)
|
||||
for _, channel := range bot.client.Channels() {
|
||||
clientMap[channel.Name()] = true
|
||||
}
|
||||
|
||||
decisions := make(map[string]bool)
|
||||
for _, channel := range channels {
|
||||
decisions[channel.Name] = channel.Logged
|
||||
joins := make([]string, 0, len(loggedMap))
|
||||
parts := make([]string, 0, len(clientMap))
|
||||
for key := range loggedMap {
|
||||
if !clientMap[key] {
|
||||
joins = append(joins, key)
|
||||
}
|
||||
}
|
||||
joins := make([]string, 0, len(decisions))
|
||||
parts := make([]string, 0, len(decisions))
|
||||
for channelName, logged := range decisions {
|
||||
if logged {
|
||||
if bot.client.Channel(channelName) != nil {
|
||||
continue
|
||||
}
|
||||
joins = append(joins, channelName)
|
||||
} else {
|
||||
if bot.client.Channel(channelName) == nil {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, channelName)
|
||||
for key := range clientMap {
|
||||
if !loggedMap[key] {
|
||||
parts = append(parts, key)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ type Channel struct {
|
||||
}
|
||||
|
||||
func newChannel(parent *Bot, name string, client *irc.Client) *Channel {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(parent.ctx)
|
||||
|
||||
return &Channel{
|
||||
name: name,
|
||||
@@ -266,7 +266,7 @@ func (channel *Channel) loop() {
|
||||
channel.lastPostSessionID = ""
|
||||
}
|
||||
|
||||
case <-channel.parentCtx.Done():
|
||||
case <-channel.ctx.Done():
|
||||
{
|
||||
// Time to pack up shop.
|
||||
return
|
||||
|
||||
+15
-1
@@ -31,7 +31,17 @@ func handler(event *irc.Event, client *irc.Client) {
|
||||
bot.stopLoop()
|
||||
|
||||
conf := config.Get().Server
|
||||
go bot.Connect(conf.Address, conf.SSL, 0)
|
||||
go bot.Connect(conf.Address, conf.SSL, 100)
|
||||
}
|
||||
|
||||
// Log Connections
|
||||
case "client.connecting":
|
||||
{
|
||||
log.Println("Client is connecting...")
|
||||
}
|
||||
case "client.connect":
|
||||
{
|
||||
log.Println("Client connected!")
|
||||
}
|
||||
|
||||
// Log joins and leaves
|
||||
@@ -133,6 +143,10 @@ func handler(event *irc.Event, client *irc.Client) {
|
||||
}
|
||||
}
|
||||
|
||||
if event.Kind() == "error" {
|
||||
log.Printf("Client Error (%s) %s\n", event.Verb(), event.Text)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user