Changeset 245 in code


Ignore:
Timestamp:
Apr 7, 2020, 5:45:29 PM (5 years ago)
Author:
contact
Message:

Centralize logged messages marshaling

This allows messages added to logs to be handled just like messages
added to the ring buffer.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r242 r245  
    223223}
    224224
    225 func (dc *downstreamConn) sendFromUpstream(msg *irc.Message, uc *upstreamConn) {
     225// marshalMessage re-formats a message coming from an upstream connection so
     226// that it's suitable for being sent on this downstream connection. Only
     227// messages that may appear in logs are supported.
     228func (dc *downstreamConn) marshalMessage(msg *irc.Message, uc *upstreamConn) *irc.Message {
    226229        msg = msg.Copy()
     230        msg.Prefix = dc.marshalUserPrefix(uc, msg.Prefix)
     231
    227232        switch msg.Command {
    228233        case "PRIVMSG", "NOTICE":
    229                 msg.Prefix = dc.marshalUserPrefix(uc, msg.Prefix)
    230234                msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
     235        case "NICK":
     236                // Nick change for another user
     237                msg.Params[0] = dc.marshalNick(uc, msg.Params[0])
     238        case "JOIN", "PART":
     239                msg.Params[0] = dc.marshalChannel(uc, msg.Params[0])
     240        case "KICK":
     241                msg.Params[0] = dc.marshalChannel(uc, msg.Params[0])
     242                msg.Params[1] = dc.marshalNick(uc, msg.Params[1])
     243        case "TOPIC":
     244                msg.Params[0] = dc.marshalChannel(uc, msg.Params[0])
     245        case "MODE":
     246                msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
     247        case "QUIT":
     248                // This space is intentinally left blank
    231249        default:
    232250                panic(fmt.Sprintf("unexpected %q message", msg.Command))
    233251        }
    234252
    235         dc.SendMessage(msg)
     253        return msg
    236254}
    237255
     
    685703                        }
    686704
    687                         dc.sendFromUpstream(msg, uc)
     705                        // Don't replay all messages, because that would mess up client
     706                        // state. For instance we just sent the list of users, sending
     707                        // PART messages for one of these users would be incorrect.
     708                        ignore := true
     709                        switch msg.Command {
     710                        case "PRIVMSG", "NOTICE":
     711                                ignore = false
     712                        }
     713                        if ignore {
     714                                continue
     715                        }
     716
     717                        dc.SendMessage(dc.marshalMessage(msg, uc))
    688718                }
    689719        })
  • trunk/upstream.go

    r244 r245  
    563563
    564564                if !me {
     565                        uc.network.ring.Produce(msg)
    565566                        uc.forEachDownstream(func(dc *downstreamConn) {
    566                                 dc.SendMessage(&irc.Message{
    567                                         Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    568                                         Command: "NICK",
    569                                         Params:  []string{dc.marshalEntity(uc, newNick)},
    570                                 })
     567                                dc.SendMessage(dc.marshalMessage(msg, uc))
    571568                        })
    572569                }
     
    602599                        }
    603600
    604                         uc.appendLog(ch, msg)
    605 
    606                         uc.forEachDownstream(func(dc *downstreamConn) {
    607                                 dc.SendMessage(&irc.Message{
    608                                         Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    609                                         Command: "JOIN",
    610                                         Params:  []string{dc.marshalChannel(uc, ch)},
    611                                 })
    612                         })
     601                        chMsg := msg.Copy()
     602                        chMsg.Params[0] = ch
     603                        uc.produce(ch, chMsg, nil)
    613604                }
    614605        case "PART":
     
    620611                if err := parseMessageParams(msg, &channels); err != nil {
    621612                        return err
    622                 }
    623 
    624                 var reason string
    625                 if len(msg.Params) > 1 {
    626                         reason = msg.Params[1]
    627613                }
    628614
     
    639625                        }
    640626
    641                         uc.appendLog(ch, msg)
    642 
    643                         uc.forEachDownstream(func(dc *downstreamConn) {
    644                                 params := []string{dc.marshalChannel(uc, ch)}
    645                                 if reason != "" {
    646                                         params = append(params, reason)
    647                                 }
    648                                 dc.SendMessage(&irc.Message{
    649                                         Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    650                                         Command: "PART",
    651                                         Params:  params,
    652                                 })
    653                         })
     627                        chMsg := msg.Copy()
     628                        chMsg.Params[0] = ch
     629                        uc.produce(ch, chMsg, nil)
    654630                }
    655631        case "KICK":
     
    661637                if err := parseMessageParams(msg, &channel, &user); err != nil {
    662638                        return err
    663                 }
    664 
    665                 var reason string
    666                 if len(msg.Params) > 2 {
    667                         reason = msg.Params[2]
    668639                }
    669640
     
    679650                }
    680651
    681                 uc.appendLog(channel, msg)
    682 
    683                 uc.forEachDownstream(func(dc *downstreamConn) {
    684                         params := []string{dc.marshalChannel(uc, channel), dc.marshalNick(uc, user)}
    685                         if reason != "" {
    686                                 params = append(params, reason)
    687                         }
    688                         dc.SendMessage(&irc.Message{
    689                                 Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    690                                 Command: "KICK",
    691                                 Params:  params,
    692                         })
    693                 })
     652                uc.produce(channel, msg, nil)
    694653        case "QUIT":
    695654                if msg.Prefix == nil {
     
    710669
    711670                if msg.Prefix.Name != uc.nick {
     671                        uc.network.ring.Produce(msg)
    712672                        uc.forEachDownstream(func(dc *downstreamConn) {
    713                                 dc.SendMessage(&irc.Message{
    714                                         Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    715                                         Command: "QUIT",
    716                                         Params:  msg.Params,
    717                                 })
     673                                dc.SendMessage(dc.marshalMessage(msg, uc))
    718674                        })
    719675                }
     
    746702                        ch.Topic = ""
    747703                }
    748                 uc.appendLog(ch.Name, msg)
    749                 uc.forEachDownstream(func(dc *downstreamConn) {
    750                         params := []string{dc.marshalChannel(uc, name)}
    751                         if ch.Topic != "" {
    752                                 params = append(params, ch.Topic)
    753                         }
    754                         dc.SendMessage(&irc.Message{
    755                                 Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    756                                 Command: "TOPIC",
    757                                 Params:  params,
    758                         })
    759                 })
     704                uc.produce(ch.Name, msg, nil)
    760705        case "MODE":
    761706                var name, modeStr string
     
    782727                        }
    783728
    784                         uc.appendLog(ch.Name, msg)
    785 
    786                         uc.forEachDownstream(func(dc *downstreamConn) {
    787                                 params := []string{dc.marshalChannel(uc, name), modeStr}
    788                                 params = append(params, msg.Params[2:]...)
    789 
    790                                 dc.SendMessage(&irc.Message{
    791                                         Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
    792                                         Command: "MODE",
    793                                         Params:  params,
    794                                 })
    795                         })
     729                        uc.produce(ch.Name, msg, nil)
    796730                }
    797731        case irc.RPL_UMODEIS:
     
    13631297}
    13641298
     1299// produce appends a message to the logs, adds it to the history and forwards
     1300// it to connected downstream connections.
     1301//
     1302// If origin is not nil and origin doesn't support echo-message, the message is
     1303// forwarded to all connections except origin.
    13651304func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
    13661305        if target != "" {
     
    13721311        uc.forEachDownstream(func(dc *downstreamConn) {
    13731312                if dc != origin || dc.caps["echo-message"] {
    1374                         dc.sendFromUpstream(msg, uc)
     1313                        dc.SendMessage(dc.marshalMessage(msg, uc))
    13751314                }
    13761315        })
Note: See TracChangeset for help on using the changeset viewer.