Initial commit with enhanced IRC join/part info

This commit is contained in:
Kufat 2022-08-06 17:39:30 -04:00
parent 3c4192ebf6
commit a8d99c119e
2 changed files with 103 additions and 8 deletions

View File

@ -80,33 +80,66 @@ func (b *Birc) handleInvite(client *girc.Client, event girc.Event) {
}
}
func isKill(quitmsg string) bool {
return strings.HasPrefix(quitmsg, "Killed") || strings.HasPrefix(quitmsg, "Local kill") || strings.HasPrefix(quitmsg[1:], "-lined")
}
func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
if len(event.Params) == 0 {
b.Log.Debugf("handleJoinPart: empty Params? %#v", event)
return
}
channel := strings.ToLower(event.Params[0])
if event.Command == "KICK" && event.Params[1] == b.Nick {
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
time.Sleep(time.Duration(b.GetInt("RejoinDelay")) * time.Second)
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EventRejoinChannels}
if event.Command == "KICK" {
if event.Params[1] == b.Nick {
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
time.Sleep(time.Duration(b.GetInt("RejoinDelay")) * time.Second)
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EventRejoinChannels}
} else {
msg := config.Message{Username: "system",
Text: event.Source.Name + " kicked " + event.Params[1] + " with message: " + event.Last(),
Channel: channel,
Account: b.Account,
Event: config.EventJoinLeave}
b.Log.Debugf("<= Message is %#v", msg)
b.Remote <- msg
}
return
}
if event.Command == "QUIT" {
if event.Source.Name == b.Nick && strings.Contains(event.Last(), "Ping timeout") {
b.Log.Infof("%s reconnecting ..", b.Account)
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EventFailure}
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: b.getPseudoChannel(), Account: b.Account, Event: config.EventFailure}
return
} else if b.GetBool("nosendjoinpart") {
return
} else if b.isUserActive(event.Source.Name) || isKill(event.Last()) {
verbosequit := ""
quitmsg := ""
if len(event.Params) >= 1 {
quitmsg = " with message: " + event.Last()
}
if b.GetBool("verbosejoinpart") {
verbosequit = " (" + event.Source.Ident + "@" + event.Source.Host + ")"
}
msg := config.Message{Username: "system", Text: event.Source.Name + verbosequit + " quit" + quitmsg, Channel: b.getPseudoChannel(), Account: b.Account, Event: config.EventJoinLeave}
b.Log.Debugf("<= Message is %#v", msg)
b.Remote <- msg
return
}
}
if event.Source.Name != b.Nick {
if b.GetBool("nosendjoinpart") {
if b.GetBool("nosendjoinpart") || !b.isUserActive(event.Source.Name) {
return
}
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
partmsg := ""
if event.Command == "PART" && len(event.Params) >= 2 {
partmsg = " with message: " + event.Last()
}
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s" + partmsg, Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
if b.GetBool("verbosejoinpart") {
b.Log.Debugf("<= Sending verbose JOIN_LEAVE event from %s to gateway", b.Account)
msg = config.Message{Username: "system", Text: event.Source.Name + " (" + event.Source.Ident + "@" + event.Source.Host + ") " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
msg = config.Message{Username: "system", Text: event.Source.Name + " (" + event.Source.Ident + "@" + event.Source.Host + ") " + strings.ToLower(event.Command) + "s" + partmsg, Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
} else {
b.Log.Debugf("<= Sending JOIN_LEAVE event from %s to gateway", b.Account)
}
@ -130,9 +163,29 @@ func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
i.Handlers.AddBg("PART", b.handleJoinPart)
i.Handlers.AddBg("QUIT", b.handleJoinPart)
i.Handlers.AddBg("KICK", b.handleJoinPart)
i.Handlers.AddBg("NICK", b.handleNick)
i.Handlers.Add("INVITE", b.handleInvite)
}
func (b *Birc) handleNick(client *girc.Client, event girc.Event) {
if len(event.Params) != 1 {
b.Log.Debugf("handleJoinPart: malformed nick change? %#v", event)
return
} else if b.isUserActive(event.Source.Name) {
msg := config.Message{Username: "system",
Text: event.Source.Name + " changed nick to " + event.Params[0],
Channel: b.getPseudoChannel(),
Account: b.Account,
Event: config.EventJoinLeave}
b.Log.Debugf("<= Message is %#v", msg)
b.Remote <- msg
if b.ActivityTimeout != 0 {
// This doesn't count as new activity, but it does preserve the value
b.activeUsers[event.Params[0]] = b.activeUsers[event.Source.Name]
}
}
}
func (b *Birc) handleNickServ() {
if !b.GetBool("UseSASL") && b.GetString("NickServNick") != "" && b.GetString("NickServPassword") != "" {
b.Log.Debugf("Sending identify to nickserv %s", b.GetString("NickServNick"))
@ -238,6 +291,10 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
}
b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account)
if b.ActivityTimeout > 0 {
b.Log.Debugf("<= Updating last-active time for user %s", event.Source.Name)
b.activeUsers[event.Source.Name] = time.Now().Unix()
}
b.Remote <- rmsg
}

View File

@ -27,10 +27,12 @@ type Birc struct {
i *girc.Client
Nick string
names map[string][]string
activeUsers map[string]int64
connected chan error
Local chan config.Message // local queue for flood control
FirstConnection, authDone bool
MessageDelay, MessageQueue, MessageLength int
ActivityTimeout int64
channels map[string]bool
*bridge.Config
@ -41,6 +43,7 @@ func New(cfg *bridge.Config) bridge.Bridger {
b.Config = cfg
b.Nick = b.GetString("Nick")
b.names = make(map[string][]string)
b.activeUsers = make(map[string]int64)
b.connected = make(chan error)
b.channels = make(map[string]bool)
@ -59,6 +62,15 @@ func New(cfg *bridge.Config) bridge.Bridger {
} else {
b.MessageLength = b.GetInt("MessageLength")
}
if b.GetBool("ShowActiveUserEvents") {
if b.GetInt("ActivityTimeout") == 0 {
b.ActivityTimeout = 1800 // 30 minutes
} else {
b.ActivityTimeout = int64(b.GetInt("ActivityTimeout"))
}
} else {
b.ActivityTimeout = 0 // Disable
}
b.FirstConnection = true
return b
}
@ -413,3 +425,29 @@ func (b *Birc) getTLSConfig() (*tls.Config, error) {
return tlsConfig, nil
}
func (b *Birc) isUserActive(nick string) bool {
b.Log.Debugf("checking activity for %s", nick)
if b.ActivityTimeout == 0 {
return true
} else if activeTime, ok := b.activeUsers[nick]; ok {
now := time.Now().Unix()
b.Log.Debugf("last activity for %s was %d, currently %d", nick, activeTime, now)
if now < activeTime {
b.Log.Errorf("User %s has active time in the future: %d", nick, activeTime)
return true // err on the side of caution
}
return (now - activeTime) < b.ActivityTimeout
}
return false
}
func (b *Birc) getPseudoChannel() string {
for channelname, active := range b.channels {
if active {
return channelname
}
}
b.Log.Warningf("Bot not active in any channels!")
return ""
}