Changeset 155 in code for trunk/upstream.go


Ignore:
Timestamp:
Mar 25, 2020, 10:20:56 PM (5 years ago)
Author:
delthas
Message:

Add upstream labeled-response capability support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r153 r155  
    44        "crypto/tls"
    55        "encoding/base64"
     6        "errors"
    67        "fmt"
    78        "io"
     
    5253        batches    map[string]batch
    5354
    54         tagsSupported bool
     55        tagsSupported   bool
     56        labelsSupported bool
     57        nextLabelId     uint64
    5558
    5659        saslClient  sasl.Client
     
    128131}
    129132
     133func (uc *upstreamConn) forEachDownstreamById(id uint64, f func(*downstreamConn)) {
     134        uc.forEachDownstream(func(dc *downstreamConn) {
     135                if id != 0 && id != dc.id {
     136                        return
     137                }
     138                f(dc)
     139        })
     140}
     141
    130142func (uc *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
    131143        ch, ok := uc.channels[name]
     
    153165
    154166func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
     167        var label string
     168        if l, ok := msg.GetTag("label"); ok {
     169                label = l
     170        }
     171
    155172        var msgBatch *batch
    156173        if batchName, ok := msg.GetTag("batch"); ok {
     
    160177                }
    161178                msgBatch = &b
     179                if label == "" {
     180                        label = msgBatch.Label
     181                }
     182        }
     183
     184        var downstreamId uint64 = 0
     185        if label != "" {
     186                var labelOffset uint64
     187                n, err := fmt.Sscanf(label, "sd-%d-%d", &downstreamId, &labelOffset)
     188                if err == nil && n < 2 {
     189                        err = errors.New("not enough arguments")
     190                }
     191                if err != nil {
     192                        return fmt.Errorf("unexpected message label: invalid downstream reference for label %q: %v", label, err)
     193                }
    162194        }
    163195
     
    205237
    206238                        requestCaps := make([]string, 0, 16)
    207                         for _, c := range []string{"message-tags", "batch"} {
     239                        for _, c := range []string{"message-tags", "batch", "labeled-response"} {
    208240                                if _, ok := uc.caps[c]; ok {
    209241                                        requestCaps = append(requestCaps, c)
     
    432464                                return err
    433465                        }
     466                        label := label
     467                        if label == "" && msgBatch != nil {
     468                                label = msgBatch.Label
     469                        }
    434470                        uc.batches[tag] = batch{
    435471                                Type:   batchType,
    436472                                Params: msg.Params[2:],
    437473                                Outer:  msgBatch,
     474                                Label:  label,
    438475                        }
    439476                } else if strings.HasPrefix(tag, "-") {
     
    950987        case "TAGMSG":
    951988                // TODO: relay to downstream connections that accept message-tags
     989        case "ACK":
     990                // Ignore
    952991        case irc.RPL_YOURHOST, irc.RPL_CREATED:
    953992                // Ignore
     
    10481087        case "message-tags":
    10491088                uc.tagsSupported = ok
     1089        case "labeled-response":
     1090                uc.labelsSupported = ok
    10501091        }
    10511092        return nil
     
    10741115        uc.outgoing <- msg
    10751116}
     1117
     1118func (uc *upstreamConn) SendMessageLabeled(dc *downstreamConn, msg *irc.Message) {
     1119        if uc.labelsSupported {
     1120                if msg.Tags == nil {
     1121                        msg.Tags = make(map[string]irc.TagValue)
     1122                }
     1123                msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", dc.id, uc.nextLabelId))
     1124                uc.nextLabelId++
     1125        }
     1126        uc.SendMessage(msg)
     1127}
Note: See TracChangeset for help on using the changeset viewer.