~emersion/soju-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH v7] Implement casemapping

Details
Message ID
<20210218103043.13146-1-hubert@hirtz.pm>
DKIM signature
pass
Download raw message
Patch: +123 -83
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)

== What is casemapping? ==

see <https://modern.ircdocs.horse/#casemapping-parameter>

== Casemapping and multi-upstream ==

Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],

1. soju must also update the database accordingly to upstreams'
   casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
   that is a subset of all supported casemappings (here, ascii).

[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.

Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.

downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.

Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).

== Message forwarding and casemapping ==

downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user.  This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).

marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters.  ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).

== Casemapping changes ==

Casemapping changes are not fully supported by this patch and will
result in loss of history.  This is a limitation of the protocol and
should be solved by the RENAME spec.

---

This patch uses a casemapMap to avoid missing calls to network.casemap,
as advised in the previous revision thread.

So.. contrary to what i said in the previous mail, it is not trivial to
use the original upstream names instead of calling partialCasemap in
marshal{Entity,UserPrefix}, so this patch keeps this behavior.

Indeed, the caller of marshal.* would have to ensure it uses the same
casemapped form every time, meaning we would have to inspect every
message (passed to marshalMessage), lookup channels or users, retrieve
their original names if we know them, add them to upstreamConn if we
don't, etc.

 bridge.go     |   4 +-
 downstream.go |  83 +++++++++++++++++++++----------------
 service.go    |   6 +--
 upstream.go   | 111 +++++++++++++++++++++++++++++++-------------------
 user.go       |   2 +-
 5 files changed, 123 insertions(+), 83 deletions(-)

diff --git a/bridge.go b/bridge.go
index f707e33..509f1fd 100644
--- a/bridge.go
@@ -54,7 +54,9 @@ func sendNames(dc *downstreamConn, ch *upstreamChannel) {
	maxLength := maxMessageLength - len(emptyNameReply.String())

	var buf strings.Builder
	for nick, memberships := range ch.Members {
	for _, entry := range ch.Members.innerMap {
		nick := entry.originalKey
		memberships := entry.value.(*memberships)
		s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)

		n := buf.Len() + 1 + len(s)
diff --git a/downstream.go b/downstream.go
index 2d3784e..18a8a34 100644
--- a/downstream.go
+++ b/downstream.go
@@ -89,6 +89,7 @@ type downstreamConn struct {
	registered  bool
	user        *user
	nick        string
	nickCM      string
	rawUsername string
	networkName string
	clientName  string
@@ -165,13 +166,13 @@ func (dc *downstreamConn) upstream() *upstreamConn {
func isOurNick(net *network, nick string) bool {
	// TODO: this doesn't account for nick changes
	if net.conn != nil {
		return nick == net.conn.nick
		return net.casemap(nick) == net.conn.nickCM
	}
	// We're not currently connected to the upstream connection, so we don't
	// know whether this name is our nickname. Best-effort: use the network's
	// configured nickname and hope it was the one being used when we were
	// connected.
	return nick == net.Nick
	return net.casemap(nick) == net.casemap(net.Nick)
}

// marshalEntity converts an upstream entity name (ie. channel or nick) into a
@@ -183,6 +184,7 @@ func (dc *downstreamConn) marshalEntity(net *network, name string) string {
	if isOurNick(net, name) {
		return dc.nick
	}
	name = partialCasemap(net.casemap, name)
	if dc.network != nil {
		if dc.network != net {
			panic("soju: tried to marshal an entity for another network")
@@ -196,6 +198,7 @@ func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *i
	if isOurNick(net, prefix.Name) {
		return dc.prefix()
	}
	prefix.Name = partialCasemap(net.casemap, prefix.Name)
	if dc.network != nil {
		if dc.network != net {
			panic("soju: tried to marshal a user prefix for another network")
@@ -325,8 +328,8 @@ func (dc *downstreamConn) ackMsgID(id string) {
		return
	}

	delivered, ok := network.delivered[entity]
	if !ok {
	delivered := network.delivered.Value(entity).(map[string]string)
	if delivered == nil {
		return
	}

@@ -412,13 +415,15 @@ func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
				Params:  []string{dc.nick, nick, "contains illegal characters"},
			}}
		}
		if nick == serviceNick {
		nickCM := casemapASCII(nick)
		if nickCM == serviceNickCM {
			return ircError{&irc.Message{
				Command: irc.ERR_NICKNAMEINUSE,
				Params:  []string{dc.nick, nick, "Nickname reserved for bouncer service"},
			}}
		}
		dc.nick = nick
		dc.nickCM = nickCM
	case "USER":
		if err := parseMessageParams(msg, &dc.rawUsername, nil, nil, &dc.realname); err != nil {
			return err
@@ -733,6 +738,7 @@ func (dc *downstreamConn) updateNick() {
			Params:  []string{uc.nick},
		})
		dc.nick = uc.nick
		dc.nickCM = casemapASCII(dc.nick)
	}
}

@@ -878,6 +884,7 @@ func (dc *downstreamConn) welcome() error {

	isupport := []string{
		fmt.Sprintf("CHATHISTORY=%v", dc.srv.HistoryLimit),
		"CASEMAPPING=ascii",
	}

	if uc := dc.upstream(); uc != nil && uc.networkName != "" {
@@ -920,11 +927,13 @@ func (dc *downstreamConn) welcome() error {
	dc.updateSupportedCaps()

	dc.forEachUpstream(func(uc *upstreamConn) {
		for _, ch := range uc.channels {
		for _, entry := range uc.channels.innerMap {
			ch := entry.value.(*upstreamChannel)
			if !ch.complete {
				continue
			}
			if record, ok := uc.network.channels[ch.Name]; ok && record.Detached {
			record := uc.network.channels.Value(ch.Name).(*Channel)
			if record != nil && record.Detached {
				continue
			}

@@ -947,8 +956,10 @@ func (dc *downstreamConn) welcome() error {
		}

		// Fast-forward history to last message
		for target, delivered := range net.delivered {
			if ch, ok := net.channels[target]; ok && ch.Detached {
		for target, entry := range net.delivered.innerMap {
			delivered := entry.value.(map[string]string)
			ch := net.channels.Value(target).(*Channel)
			if ch != nil && ch.Detached {
				continue
			}

@@ -982,8 +993,10 @@ func (dc *downstreamConn) sendNetworkBacklog(net *network) {
	if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
		return
	}
	for target, delivered := range net.delivered {
		if ch, ok := net.channels[target]; ok && ch.Detached {
	for target, entry := range net.delivered.innerMap {
		delivered := entry.value.(map[string]string)
		ch := net.channels.Value(target).(*Channel)
		if ch != nil && ch.Detached {
			continue
		}

@@ -1111,7 +1124,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  []string{dc.nick, rawNick, "contains illegal characters"},
			}}
		}
		if nick == serviceNick {
		if casemapASCII(nick) == serviceNickCM {
			return ircError{&irc.Message{
				Command: irc.ERR_NICKNAMEINUSE,
				Params:  []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
@@ -1147,6 +1160,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  []string{nick},
			})
			dc.nick = nick
			dc.nickCM = casemapASCII(dc.nick)
		}
	case "JOIN":
		var namesStr string
@@ -1179,9 +1193,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  params,
			})

			var ch *Channel
			var ok bool
			if ch, ok = uc.network.channels[upstreamName]; ok {
			ch := uc.network.channels.Value(upstreamName).(*Channel)
			if ch != nil {
				// Don't clear the channel key if there's one set
				// TODO: add a way to unset the channel key
				if key != "" {
@@ -1193,7 +1206,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
					Name: upstreamName,
					Key:  key,
				}
				uc.network.channels[upstreamName] = ch
				uc.network.channels.SetValue(upstreamName, ch)
			}
			if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
				dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
@@ -1217,16 +1230,15 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			}

			if strings.EqualFold(reason, "detach") {
				var ch *Channel
				var ok bool
				if ch, ok = uc.network.channels[upstreamName]; ok {
				ch := uc.network.channels.Value(upstreamName).(*Channel)
				if ch != nil {
					uc.network.detach(ch)
				} else {
					ch = &Channel{
						Name:     name,
						Detached: true,
					}
					uc.network.channels[upstreamName] = ch
					uc.network.channels.SetValue(upstreamName, ch)
				}
				if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
					dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
@@ -1313,7 +1325,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			modeStr = msg.Params[1]
		}

		if name == dc.nick {
		if casemapASCII(name) == dc.nickCM {
			if modeStr != "" {
				dc.forEachUpstream(func(uc *upstreamConn) {
					uc.SendMessageLabeled(dc.id, &irc.Message{
@@ -1351,8 +1363,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  params,
			})
		} else {
			ch, ok := uc.channels[upstreamName]
			if !ok {
			ch := uc.channels.Value(upstreamName).(*upstreamChannel)
			if ch == nil {
				return ircError{&irc.Message{
					Command: irc.ERR_NOSUCHCHANNEL,
					Params:  []string{dc.nick, name, "No such channel"},
@@ -1388,7 +1400,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			return err
		}

		uc, upstreamChannel, err := dc.unmarshalEntity(channel)
		uc, upstreamName, err := dc.unmarshalEntity(channel)
		if err != nil {
			return err
		}
@@ -1397,14 +1409,14 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			topic := msg.Params[1]
			uc.SendMessageLabeled(dc.id, &irc.Message{
				Command: "TOPIC",
				Params:  []string{upstreamChannel, topic},
				Params:  []string{upstreamName, topic},
			})
		} else { // getting topic
			ch, ok := uc.channels[upstreamChannel]
			if !ok {
			ch := uc.channels.Value(upstreamName).(*upstreamChannel)
			if ch == nil {
				return ircError{&irc.Message{
					Command: irc.ERR_NOSUCHCHANNEL,
					Params:  []string{dc.nick, upstreamChannel, "No such channel"},
					Params:  []string{dc.nick, upstreamName, "No such channel"},
				}}
			}
			sendTopic(dc, ch)
@@ -1466,19 +1478,19 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {

		channels := strings.Split(msg.Params[0], ",")
		for _, channel := range channels {
			uc, upstreamChannel, err := dc.unmarshalEntity(channel)
			uc, upstreamName, err := dc.unmarshalEntity(channel)
			if err != nil {
				return err
			}

			ch, ok := uc.channels[upstreamChannel]
			if ok {
			ch := uc.channels.Value(upstreamName).(*upstreamChannel)
			if ch != nil {
				sendNames(dc, ch)
			} else {
				// NAMES on a channel we have not joined, ask upstream
				uc.SendMessageLabeled(dc.id, &irc.Message{
					Command: "NAMES",
					Params:  []string{upstreamChannel},
					Params:  []string{upstreamName},
				})
			}
		}
@@ -1495,8 +1507,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {

		// TODO: support WHO masks
		entity := msg.Params[0]
		entityCM := casemapASCII(entity)

		if entity == dc.nick {
		if entityCM == dc.nickCM {
			// TODO: support AWAY (H/G) in self WHO reply
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
@@ -1510,7 +1523,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			})
			return nil
		}
		if entity == serviceNick {
		if entityCM == serviceNickCM {
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
				Command: irc.RPL_WHOREPLY,
@@ -1561,7 +1574,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			mask = mask[:i]
		}

		if mask == dc.nick {
		if casemapASCII(mask) == dc.nickCM {
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
				Command: irc.RPL_WHOISUSER,
diff --git a/service.go b/service.go
index e5ac17e..15b7dd9 100644
--- a/service.go
+++ b/service.go
@@ -409,7 +409,7 @@ func handleServiceNetworkStatus(dc *downstreamConn, params []string) error {
			} else {
				statuses = append(statuses, "connected")
			}
			details = fmt.Sprintf("%v channels", len(uc.channels))
			details = fmt.Sprintf("%v channels", uc.channels.Len())
		} else {
			statuses = append(statuses, "disconnected")
			if net.lastError != nil {
@@ -769,8 +769,8 @@ func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
		return fmt.Errorf("unknown channel %q", name)
	}

	ch, ok := uc.network.channels[upstreamName]
	if !ok {
	ch := uc.network.channels.Value(upstreamName).(*Channel)
	if ch == nil {
		return fmt.Errorf("unknown channel %q", name)
	}

diff --git a/upstream.go b/upstream.go
index 5c3615d..5e1b13f 100644
--- a/upstream.go
+++ b/upstream.go
@@ -101,6 +101,8 @@ type upstreamConn struct {
	saslClient  sasl.Client
	saslStarted bool

	casemapIsSet bool

	// set of LIST commands in progress, per downstream
	pendingLISTDownstreamSet map[uint64]struct{}
}
@@ -235,6 +237,7 @@ func (uc *upstreamConn) isChannel(entity string) bool {
}

func (uc *upstreamConn) isOurNick(nick string) bool {
	return uc.nickCM == uc.network.casemap(nick)
}

func (uc *upstreamConn) getPendingLIST() *pendingLIST {
@@ -426,11 +429,12 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			uc.produce("", msg, nil)
		} else { // regular user message
			target := entity
			if target == uc.nick {
			if uc.isOurNick(target) {
				target = msg.Prefix.Name
			}

			if ch, ok := uc.network.channels[target]; ok {
			ch := uc.network.channels.Value(target).(*Channel)
			if ch != nil {
				if ch.Detached {
					uc.handleDetachedMessage(msg.Prefix.Name, text, ch)
				}
@@ -607,9 +611,10 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			dc.updateSupportedCaps()
		})

		if len(uc.network.channels) > 0 {
		if uc.network.channels.Len() > 0 {
			var channels, keys []string
			for _, ch := range uc.network.channels {
			for _, entry := range uc.network.channels.innerMap {
				ch := entry.value.(*Channel)
				channels = append(channels, ch.Name)
				keys = append(keys, ch.Key)
			}
@@ -648,7 +653,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
					}
					uc.network.updateCasemapping(casemap)
					uc.nickCM = uc.network.casemap(uc.nick)
					uc.advertisedCasemap = true
					uc.casemapIsSet = true
				case "CHANMODES":
					parts := strings.SplitN(value, ",", 5)
					if len(parts) < 4 {
@@ -691,6 +696,14 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
				// TODO: handle ISUPPORT negations
			}
		}
	case irc.ERR_NOMOTD, irc.RPL_ENDOFMOTD:
		if !uc.casemapIsSet {
			// upstream did not send any CASEMAPPING token, thus
			// we assume it implements the old RFCs with rfc1459.
			uc.casemapIsSet = true
			uc.network.updateCasemapping(casemapRFC1459)
			uc.nickCM = uc.network.casemap(uc.nick)
		}
	case "BATCH":
		var tag string
		if err := parseMessageParams(msg, &tag); err != nil {
@@ -736,7 +749,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		}

		me := false
		if uc.network.casemap(msg.Prefix.Name) == uc.nickCM {
		if uc.isOurNick(msg.Prefix.Name) {
			uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
			me = true
			uc.nick = newNick
@@ -773,13 +786,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		}

		for _, ch := range strings.Split(channels, ",") {
			if msg.Prefix.Name == uc.nick {
			if uc.isOurNick(msg.Prefix.Name) {
				uc.logger.Printf("joined channel %q", ch)
				uc.channels[ch] = &upstreamChannel{
				uc.channels.SetValue(ch, &upstreamChannel{
					Name:    ch,
					conn:    uc,
					Members: make(map[string]*memberships),
				}
					Members: newCasemapMap(0),
				})
				uc.updateChannelAutoDetach(ch)

				uc.SendMessage(&irc.Message{
@@ -791,7 +804,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
				if err != nil {
					return err
				}
				ch.Members[msg.Prefix.Name] = &memberships{}
				ch.Members.SetValue(msg.Prefix.Name, &memberships{})
			}

			chMsg := msg.Copy()
@@ -809,10 +822,11 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		}

		for _, ch := range strings.Split(channels, ",") {
			if msg.Prefix.Name == uc.nick {
			if uc.isOurNick(msg.Prefix.Name) {
				uc.logger.Printf("parted channel %q", ch)
				if uch, ok := uc.channels[ch]; ok {
					delete(uc.channels, ch)
				uch := uc.channels.Value(ch).(*upstreamChannel)
				if uch != nil {
					uc.channels.Delete(ch)
					uch.updateAutoDetach(0)
				}
			} else {
@@ -820,7 +834,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
				if err != nil {
					return err
				}
				delete(ch.Members, msg.Prefix.Name)
				ch.Members.Delete(msg.Prefix.Name)
			}

			chMsg := msg.Copy()
@@ -837,15 +851,15 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		if user == uc.nick {
		if uc.isOurNick(user) {
			uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
			delete(uc.channels, channel)
			uc.channels.Delete(channel)
		} else {
			ch, err := uc.getChannel(channel)
			if err != nil {
				return err
			}
			delete(ch.Members, user)
			ch.Members.Delete(user)
		}

		uc.produce(channel, msg, nil)
@@ -854,13 +868,14 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return fmt.Errorf("expected a prefix")
		}

		if msg.Prefix.Name == uc.nick {
		if uc.isOurNick(msg.Prefix.Name) {
			uc.logger.Printf("quit")
		}

		for _, ch := range uc.channels {
			if _, ok := ch.Members[msg.Prefix.Name]; ok {
				delete(ch.Members, msg.Prefix.Name)
		for _, entry := range uc.channels.innerMap {
			ch := entry.value.(*upstreamChannel)
			if ch.Members.Has(msg.Prefix.Name) {
				ch.Members.Delete(msg.Prefix.Name)

				uc.appendLog(ch.Name, msg)
			}
@@ -931,7 +946,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {

			uc.appendLog(ch.Name, msg)

			if ch, ok := uc.network.channels[name]; !ok || !ch.Detached {
			c := uc.network.channels.Value(name).(*Channel)
			if c == nil || !c.Detached {
				uc.forEachDownstream(func(dc *downstreamConn) {
					params := make([]string, len(msg.Params))
					params[0] = dc.marshalEntity(uc.network, name)
@@ -987,7 +1003,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}
		if firstMode {
			if c, ok := uc.network.channels[channel]; !ok || !c.Detached {
			c := uc.network.channels.Value(channel).(*Channel)
			if c == nil || !c.Detached {
				modeStr, modeParams := ch.modes.Format()

				uc.forEachDownstream(func(dc *downstreamConn) {
@@ -1084,8 +1101,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		ch, ok := uc.channels[name]
		if !ok {
		ch := uc.channels.Value(name).(*upstreamChannel)
		if ch == nil {
			// NAMES on a channel we have not joined, forward to downstream
			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
				channel := dc.marshalEntity(uc.network, name)
@@ -1113,7 +1130,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {

		for _, s := range splitSpace(members) {
			memberships, nick := uc.parseMembershipPrefix(s)
			ch.Members[nick] = memberships
			ch.Members.SetValue(nick, memberships)
		}
	case irc.RPL_ENDOFNAMES:
		var name string
@@ -1121,8 +1138,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		ch, ok := uc.channels[name]
		if !ok {
		ch := uc.channels.Value(name).(*upstreamChannel)
		if ch == nil {
			// NAMES on a channel we have not joined, forward to downstream
			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
				channel := dc.marshalEntity(uc.network, name)
@@ -1141,7 +1158,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		}
		ch.complete = true

		if c, ok := uc.network.channels[name]; !ok || !c.Detached {
		c := uc.network.channels.Value(name).(*Channel)
		if c == nil || !c.Detached {
			uc.forEachDownstream(func(dc *downstreamConn) {
				forwardChannel(dc, ch)
			})
@@ -1295,7 +1313,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		weAreInvited := nick == uc.nick
		weAreInvited := uc.isOurNick(nick)

		uc.forEachDownstream(func(dc *downstreamConn) {
			if !weAreInvited && !dc.caps["invite-notify"] {
@@ -1418,7 +1436,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		// Ignore
	case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
		// Ignore
	case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
	case irc.RPL_MOTDSTART, irc.RPL_MOTD:
		// Ignore
	case irc.RPL_LISTSTART:
		// Ignore
@@ -1584,6 +1602,7 @@ func splitSpace(s string) []string {

func (uc *upstreamConn) register() {
	uc.nick = uc.network.Nick
	uc.nickCM = uc.network.casemap(uc.nick)
	uc.username = uc.network.Username
	if uc.username == "" {
		uc.username = uc.nick
@@ -1689,20 +1708,21 @@ func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string
	}

	detached := false
	if ch, ok := uc.network.channels[entity]; ok {
	if ch := uc.network.channels.Value(entity).(*Channel); ch != nil {
		detached = ch.Detached
	}

	delivered, ok := uc.network.delivered[entity]
	if !ok {
		lastID, err := uc.user.msgStore.LastMsgID(uc.network, entity, time.Now())
	delivered := uc.network.delivered.Value(entity).(map[string]string)
	entityCM := uc.network.casemap(entity)
	if delivered == nil {
		lastID, err := uc.user.msgStore.LastMsgID(uc.network, entityCM, time.Now())
		if err != nil {
			uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
			return ""
		}

		delivered = make(map[string]string)
		uc.network.delivered[entity] = delivered
		uc.network.delivered.SetValue(entity, delivered)

		for clientName, _ := range uc.network.offlineClients {
			delivered[clientName] = lastID
@@ -1717,7 +1737,7 @@ func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string
		}
	}

	msgID, err := uc.user.msgStore.Append(uc.network, entity, msg)
	msgID, err := uc.user.msgStore.Append(uc.network, entityCM, msg)
	if err != nil {
		uc.logger.Printf("failed to log message: %v", err)
		return ""
@@ -1738,7 +1758,8 @@ func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstr
	}

	// Don't forward messages if it's a detached channel
	if ch, ok := uc.network.channels[target]; ok && ch.Detached {
	ch := uc.network.channels.Value(target).(*Channel)
	if ch != nil && ch.Detached {
		return
	}

@@ -1773,9 +1794,13 @@ func (uc *upstreamConn) updateAway() {
}

func (uc *upstreamConn) updateChannelAutoDetach(name string) {
	if uch, ok := uc.channels[name]; ok {
		if ch, ok := uc.network.channels[name]; ok && !ch.Detached {
			uch.updateAutoDetach(ch.DetachAfter)
		}
	uch := uc.channels.Value(name).(*upstreamChannel)
	if uch == nil {
		return
	}
	ch := uc.network.channels.Value(name).(*Channel)
	if ch == nil || ch.Detached {
		return
	}
	uch.updateAutoDetach(ch.DetachAfter)
}
diff --git a/user.go b/user.go
index 04d7513..9a90253 100644
--- a/user.go
+++ b/user.go
@@ -61,7 +61,7 @@ type network struct {
	stopped chan struct{}

	conn           *upstreamConn
	channels       casemapMap          // values are of type Channel
	channels       casemapMap          // values are of type *Channel
	delivered      casemapMap          // values are of type map[string]string (entity -> client name -> msg ID)
	offlineClients map[string]struct{} // indexed by client name
	lastError      error
--
2.26.2
Reply to thread Export thread (mbox)