Changeset 435 in code for trunk/upstream.go


Ignore:
Timestamp:
Dec 14, 2020, 7:54:02 PM (4 years ago)
Author:
delthas
Message:

Add customizable auto-detaching, auto-reattaching, relaying.

This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.

The FilterDefault values of the messages filters are currently
hardcoded.

The values of the message filters are not currently user-settable.

This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r428 r435  
    5151        Members      map[string]*memberships
    5252        complete     bool
     53        detachTimer  *time.Timer
     54}
     55
     56func (uc *upstreamChannel) updateAutoDetach(dur time.Duration) {
     57        if uc.detachTimer != nil {
     58                uc.detachTimer.Stop()
     59                uc.detachTimer = nil
     60        }
     61
     62        if dur == 0 {
     63                return
     64        }
     65
     66        uc.detachTimer = time.AfterFunc(dur, func() {
     67                uc.conn.network.user.events <- eventChannelDetach{
     68                        uc:   uc.conn,
     69                        name: uc.Name,
     70                }
     71        })
    5372}
    5473
     
    404423                                target = msg.Prefix.Name
    405424                        }
     425
     426                        if ch, ok := uc.network.channels[target]; ok {
     427                                if ch.Detached {
     428                                        uc.handleDetachedMessage(msg.Prefix.Name, text, ch)
     429                                }
     430
     431                                highlight := msg.Prefix.Name != uc.nick && isHighlight(text, uc.nick)
     432                                if ch.DetachOn == FilterMessage || ch.DetachOn == FilterDefault || (ch.DetachOn == FilterHighlight && highlight) {
     433                                        uc.updateChannelAutoDetach(target)
     434                                }
     435                        }
     436
    406437                        uc.produce(target, msg, nil)
    407 
    408                         highlight := msg.Prefix.Name != uc.nick && isHighlight(text, uc.nick)
    409                         if ch, ok := uc.network.channels[target]; ok && ch.Detached && highlight {
    410                                 uc.forEachDownstream(func(dc *downstreamConn) {
    411                                         sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), msg.Prefix.Name, text))
    412                                 })
    413                         }
    414438                }
    415439        case "CAP":
     
    737761                                        Members: make(map[string]*memberships),
    738762                                }
     763                                uc.updateChannelAutoDetach(ch)
    739764
    740765                                uc.SendMessage(&irc.Message{
     
    767792                        if msg.Prefix.Name == uc.nick {
    768793                                uc.logger.Printf("parted channel %q", ch)
    769                                 delete(uc.channels, ch)
     794                                if uch, ok := uc.channels[ch]; ok {
     795                                        delete(uc.channels, ch)
     796                                        uch.updateAutoDetach(0)
     797                                }
    770798                        } else {
    771799                                ch, err := uc.getChannel(ch)
     
    14091437}
    14101438
     1439func (uc *upstreamConn) handleDetachedMessage(sender string, text string, ch *Channel) {
     1440        highlight := sender != uc.nick && isHighlight(text, uc.nick)
     1441        if ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight) {
     1442                uc.forEachDownstream(func(dc *downstreamConn) {
     1443                        if highlight {
     1444                                sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
     1445                        } else {
     1446                                sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
     1447                        }
     1448                })
     1449        }
     1450        if ch.ReattachOn == FilterMessage || (ch.ReattachOn == FilterHighlight && highlight) {
     1451                uc.network.attach(ch)
     1452                if err := uc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
     1453                        uc.logger.Printf("failed to update channel %q: %v", ch.Name, err)
     1454                }
     1455        }
     1456}
     1457
    14111458func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
    14121459        caps := strings.Fields(capsStr)
     
    17021749        uc.away = away
    17031750}
     1751
     1752func (uc *upstreamConn) updateChannelAutoDetach(name string) {
     1753        if uch, ok := uc.channels[name]; ok {
     1754                if ch, ok := uc.network.channels[name]; ok && !ch.Detached {
     1755                        uch.updateAutoDetach(ch.DetachAfter)
     1756                }
     1757        }
     1758}
Note: See TracChangeset for help on using the changeset viewer.