From 434e722f92b0e97ad79f893607de418c3a429375 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 1 Apr 2020 12:16:32 +0200 Subject: [PATCH] Auto away Closes: https://todo.sr.ht/~emersion/soju/13 --- upstream.go | 24 ++++++++++++++++++++++++ user.go | 11 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/upstream.go b/upstream.go index efbed31..341dc03 100644 --- a/upstream.go +++ b/upstream.go @@ -54,6 +54,7 @@ type upstreamConn struct { channels map[string]*upstreamChannel caps map[string]string batches map[string]batch + away bool tagsSupported bool labelsSupported bool @@ -1269,6 +1270,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { // TODO: relay to downstream connections that accept message-tags case "ACK": // Ignore + case irc.RPL_NOWAWAY, irc.RPL_UNAWAY: + // Ignore case irc.RPL_YOURHOST, irc.RPL_CREATED: // Ignore case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME: @@ -1483,3 +1486,24 @@ func (uc *upstreamConn) appendLog(entity string, format string, a ...interface{} uc.logger.Printf("failed to log message to %q: %v", log.name, err) } } + +func (uc *upstreamConn) updateAway() { + away := true + uc.forEachDownstream(func(*downstreamConn) { + away = false + }) + if away == uc.away { + return + } + if away { + uc.SendMessage(&irc.Message{ + Command: "AWAY", + Params: []string{"Auto away"}, + }) + } else { + uc.SendMessage(&irc.Message{ + Command: "AWAY", + }) + } + uc.away = away +} diff --git a/user.go b/user.go index dc84bcc..f6f1cdd 100644 --- a/user.go +++ b/user.go @@ -175,7 +175,8 @@ func (u *user) run() { for e := range u.events { switch e := e.(type) { case eventUpstreamConnected: - // Nothing to do + uc := e.uc + uc.updateAway() case eventUpstreamDisconnected: uc := e.uc for _, log := range uc.logs { @@ -200,6 +201,10 @@ func (u *user) run() { } u.downstreamConns = append(u.downstreamConns, dc) + + u.forEachUpstream(func(uc *upstreamConn) { + uc.updateAway() + }) case eventDownstreamDisconnected: dc := e.dc for i := range u.downstreamConns { @@ -208,6 +213,10 @@ func (u *user) run() { break } } + + u.forEachUpstream(func(uc *upstreamConn) { + uc.updateAway() + }) case eventDownstreamMessage: msg, dc := e.msg, e.dc if dc.isClosed() {