Changeset 436 in code for trunk/service.go


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

service: Introduce channel update

This adds the channel update service command, which is used to set the
auto-detach, auto-reattach, and message relaying settings of a channel.

Of note is that currently the parser parses # as a comment, which
means any channel update #foo ... will actually need to be escaped to
channel update "#foo" ...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/service.go

    r421 r436  
    219219                        handle: handlePasswordChange,
    220220                },
     221                "channel": {
     222                        children: serviceCommandSet{
     223                                "update": {
     224                                        usage:  "<name> [-relay-detached <default|none|highlight|message>] [-reattach-on <default|none|highlight|message>] [-detach-after <duration>] [-detach-on <default|none|highlight|message>]",
     225                                        desc:   "update a channel",
     226                                        handle: handleServiceChannelUpdate,
     227                                },
     228                        },
     229                },
    221230        }
    222231}
     
    428437func handleServiceNetworkUpdate(dc *downstreamConn, params []string) error {
    429438        if len(params) < 1 {
    430                 return fmt.Errorf("expected exactly one argument")
     439                return fmt.Errorf("expected at least one argument")
    431440        }
    432441
     
    697706        return nil
    698707}
     708
     709type channelFlagSet struct {
     710        *flag.FlagSet
     711        RelayDetached, ReattachOn, DetachAfter, DetachOn *string
     712}
     713
     714func newChannelFlagSet() *channelFlagSet {
     715        fs := &channelFlagSet{FlagSet: newFlagSet()}
     716        fs.Var(stringPtrFlag{&fs.RelayDetached}, "relay-detached", "")
     717        fs.Var(stringPtrFlag{&fs.ReattachOn}, "reattach-on", "")
     718        fs.Var(stringPtrFlag{&fs.DetachAfter}, "detach-after", "")
     719        fs.Var(stringPtrFlag{&fs.DetachOn}, "detach-on", "")
     720        return fs
     721}
     722
     723func (fs *channelFlagSet) update(channel *Channel) error {
     724        if fs.RelayDetached != nil {
     725                filter, err := parseFilter(*fs.RelayDetached)
     726                if err != nil {
     727                        return err
     728                }
     729                channel.RelayDetached = filter
     730        }
     731        if fs.ReattachOn != nil {
     732                filter, err := parseFilter(*fs.ReattachOn)
     733                if err != nil {
     734                        return err
     735                }
     736                channel.ReattachOn = filter
     737        }
     738        if fs.DetachAfter != nil {
     739                dur, err := time.ParseDuration(*fs.DetachAfter)
     740                if err != nil || dur < 0 {
     741                        return fmt.Errorf("unknown duration for -detach-after %q (duration format: 0, 300s, 22h30m, ...)", *fs.DetachAfter)
     742                }
     743                channel.DetachAfter = dur
     744        }
     745        if fs.DetachOn != nil {
     746                filter, err := parseFilter(*fs.DetachOn)
     747                if err != nil {
     748                        return err
     749                }
     750                channel.DetachOn = filter
     751        }
     752        return nil
     753}
     754
     755func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
     756        if len(params) < 1 {
     757                return fmt.Errorf("expected at least one argument")
     758        }
     759        name := params[0]
     760
     761        fs := newChannelFlagSet()
     762        if err := fs.Parse(params[1:]); err != nil {
     763                return err
     764        }
     765
     766        uc, upstreamName, err := dc.unmarshalEntity(name)
     767        if err != nil {
     768                return fmt.Errorf("unknown channel %q", name)
     769        }
     770
     771        ch, ok := uc.network.channels[upstreamName]
     772        if !ok {
     773                return fmt.Errorf("unknown channel %q", name)
     774        }
     775
     776        if err := fs.update(ch); err != nil {
     777                return err
     778        }
     779
     780        uc.updateChannelAutoDetach(upstreamName)
     781
     782        if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
     783                return fmt.Errorf("failed to update channel: %v", err)
     784        }
     785
     786        sendServicePRIVMSG(dc, fmt.Sprintf("updated channel %q", name))
     787        return nil
     788}
Note: See TracChangeset for help on using the changeset viewer.