Changeset 584 in code for trunk/downstream.go


Ignore:
Timestamp:
Sep 13, 2021, 9:14:47 AM (4 years ago)
Author:
contact
Message:

Split unmarshalEntity into two functions

Some command handlers need to unmarshal without requiring the
upstream to be connected.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r580 r584  
    305305}
    306306
    307 // unmarshalEntity converts a downstream entity name (ie. channel or nick) into
    308 // an upstream entity name.
     307// unmarshalEntityNetwork converts a downstream entity name (ie. channel or
     308// nick) into an upstream entity name.
    309309//
    310310// This involves removing the "/<network>" suffix.
     311func (dc *downstreamConn) unmarshalEntityNetwork(name string) (*network, string, error) {
     312        if dc.network != nil {
     313                return dc.network, name, nil
     314        }
     315
     316        var net *network
     317        if i := strings.LastIndexByte(name, '/'); i >= 0 {
     318                network := name[i+1:]
     319                name = name[:i]
     320
     321                for _, n := range dc.user.networks {
     322                        if network == n.GetName() {
     323                                net = n
     324                                break
     325                        }
     326                }
     327        }
     328
     329        if net == nil {
     330                return nil, "", ircError{&irc.Message{
     331                        Command: irc.ERR_NOSUCHCHANNEL,
     332                        Params:  []string{name, "Missing network suffix in name"},
     333                }}
     334        }
     335
     336        return net, name, nil
     337}
     338
     339// unmarshalEntity is the same as unmarshalEntityNetwork, but returns the
     340// upstream connection and fails if the upstream is disconnected.
    311341func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, error) {
    312         if uc := dc.upstream(); uc != nil {
    313                 return uc, name, nil
    314         }
    315         if dc.network != nil {
     342        net, name, err := dc.unmarshalEntityNetwork(name)
     343        if err != nil {
     344                return nil, "", err
     345        }
     346
     347        if net.conn == nil {
    316348                return nil, "", ircError{&irc.Message{
    317349                        Command: irc.ERR_NOSUCHCHANNEL,
     
    320352        }
    321353
    322         var conn *upstreamConn
    323         if i := strings.LastIndexByte(name, '/'); i >= 0 {
    324                 network := name[i+1:]
    325                 name = name[:i]
    326 
    327                 dc.forEachUpstream(func(uc *upstreamConn) {
    328                         if network != uc.network.GetName() {
    329                                 return
    330                         }
    331                         conn = uc
    332                 })
    333         }
    334 
    335         if conn == nil {
    336                 return nil, "", ircError{&irc.Message{
    337                         Command: irc.ERR_NOSUCHCHANNEL,
    338                         Params:  []string{name, "Missing network suffix in channel name"},
    339                 }}
    340         }
    341         return conn, name, nil
     354        return net.conn, name, nil
    342355}
    343356
Note: See TracChangeset for help on using the changeset viewer.