~emersion/soju-dev

Implement casemapping v2 SUPERSEDED

Hubert Hirtz: 1
 Implement casemapping

 6 files changed, 292 insertions(+), 89 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/soju-dev/patches/16025/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v2] Implement casemapping Export this patch

TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)

It's not like I want to do it for you but!!! I'll add a contrib script
that merges two log files in another patch to move logs from a directory
to another, in case it's needed (it's needed for me, so... soon™).

What is casemapping?
====================

Casemapping is soooomme kind of case folding, but for IRC.
see <https://modern.ircdocs.horse/#casemapping-parameter>

Basically, without casemapping, soju doesn't understand that `#soju` and
`#SOJU` refer to the same channel, or that `emersion` and `Emersion`
refer to the same user.

This causes issues when the upstream uses one version of a name (e.g.
`#soju`) while the downstream uses another (e.g. `#Soju`).  For example,
consider this exchange (`D` for messages from downstream, `U` for
messages from upstream):

    D: JOIN #Soju
    U: :emersion@soju.im JOIN #soju
    *U sends names and al with "#soju" instead of "#Soju"*
    D: PART #soju
    U: :emersion@soju.im PART #soju

After this exchange, and because soju doesn't know casemapping, it will
keep `#Soju` in its database ("after all, upstream said `emersion`
parted `#soju`, which is a different channel than `#Soju`!"... well,
no).

This also causes issues with history, since messages sent to `#Soju`
will end up in a different log file than messages sent to `#soju`.

The issue with casemapping and multi-upstream
=============================================

If you've read ircdocs' section on CASEMAPPING, you can understand there
is (sadly) more to this.  Indeed, some servers (freenode included)
don't *just* do ASCII lowercase.  They also change some other
characters, like `[` to `{`.

This means that on some servers, `#soju[baka]` and `#soju{baka}` refers
to the same channel, while on others they don't.

Consider the following messages.  Let's say that I am `hhirtz` using
downstream `D`, and I used soju to join two networks: `fn` and `moz`.
`fn` uses the `rfc1459` casemapping (see ircdocs for what it implies),
while `moz` uses the `ascii` casemapping.

    D:   JOIN #Soju[hai]/fn
    fn:  :hhirtz JOIN #Soju{hai}/fn
    D:   PART #Soju{hai}/fn
    fn:  :hhirtz PART #Soju{hai}/fn
    D:   JOIN #Soju[iie]/moz
    moz: :hhirtz JOIN #soju[iie]/moz
    D:   PART #soju{iie}/moz

With this exchange, what are the database changes soju has to make?

1. Add `#soju{hai}/fn`,
2. remove `#soju{hai}/fn`,
3. add `#soju[iie]/moz`.

The last PART is ignored, since `#Soju{iie}/moz` is a different channel!

There is another issue, look at the first two messages: downstream asks
to join `#Soju[hai]/fn` but freenode replies it joined `#Soju{hai}/fn`.
This respects fn's casemapping, but what if moz has two different
channels named `#Soju[iie]` and `#Soju{iie}`?  Downstreams cannot tell
whether they are different or not, since they don't know what
casemapping to use for each upstream (they are not even aware there are
upstreams)!

What does it means?

It means two things:

1. database changes (the `*network` interface) actually depend on the
   upstream CASEMAPPING parameter,
2. soju must expose as few case changes as possible to downstream.  For
   example, it must not forward fn's `#Soju{hai}/fn` as-is.

Solution
========

First, the actual changes:

Entity names in map keys are made into casemapped form, for
upstreamConn, for downstreamConn and for network. Some functions now
require the caller to casemap their argument (to avoid needless calls to
casemapping functions).

Functions which require the entity parameter to be casemapped are for
the most part log related, namely: uc.appendLog and uc.produce.  Why?
Because history for each channel needs to be kept in one folder, and the
best choice to name this folder is to use the casemapped entity (soju
could store history according to the case of the first downstream JOIN
but it adds more logic to find the correct case, should the downstream
PARTs and JOINs again).

Now... what casemapping to use for each entity?

Before soju connects to an upstream, it doesn't know what casemapping to
use.  However, it still has to construct the `*network` structure, with
a map of all channels the downstream wants to join.  In order not to
lose any channel, their names are not casemapped.  Otherwise, soju
might forget to join channels when it connects.

Once the server reports its casemapping (through an ISUPPORT token), the
network channel map is reconstructed, so that its keys are correctly
casemapped. If the server doesn't advertise its casemapping, soju
assumes it is old enough to be developed according to the RFCs only, and
thus uses the rfc1459 casemapping.

In theory, and to be 100% sure, soju *should* join channels only after
it is aware of the casemapping: what if the server replies to those
JOINs before?  It is not an issue in practice, no server do that.

What if I told you... you can casemap a casemapping?

Well, point 2 from last section is yet to be solved.  To address this,
soju must expose a known casemapping to downstreams.  For being the
least destructive one, ascii has been chosen.  soju now advertises the
correct ISUPPORT token and uses ascii casemapping by default.

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.  To ensure it is done every time, the
casemap is done in `dc.marshalEntity`.

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 it replies with the ascii
casemapping in mind.

Casemapping changes?

Changing from one casemapping to another may be lossy: for example,
changing from ascii to rfc1459 will make the network lose channels.
The opposite is possible however, for example freenode plans to.  With
soju's architecture, supporting between-connection casemapping changes
has been trivially implemented in this patch.  The support for this,
however, will not make soju, downstreams or the user aware of the true
case for each channel; this requires the RENAME spec to be finished and
implemented I believe.

In-connection casemapping are not supported by this patch.  For example,
if the upstream switches from rfc1459 to ascii then dc.marshalEntity()
will no longer do partial casemap, and the downstream might end up
being served with different entity names than before.
---
 bridge.go     |   4 +-
 downstream.go |  68 +++++++++++++------
 irc.go        |  92 ++++++++++++++++++++++++-
 service.go    |   6 +-
 upstream.go   | 181 ++++++++++++++++++++++++++++++++++----------------
 user.go       |  30 +++++++--
 6 files changed, 292 insertions(+), 89 deletions(-)

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

	var buf strings.Builder
	for nick, memberships := range ch.Members {
		s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
	for _, member := range ch.Members {
		s := member.Memberships.Format(dc) + dc.marshalEntity(ch.conn.network, member.Nick)

		n := buf.Len() + 1 + len(s)
		if buf.Len() != 0 && n > maxLength {
diff --git a/downstream.go b/downstream.go
index 2f809ec..b5b8b7c 100644
--- a/downstream.go
+++ b/downstream.go
@@ -88,6 +88,7 @@ type downstreamConn struct {
	registered  bool
	user        *user
	nick        string
	nickCM      string // casemapped nickname
	rawUsername string
	networkName string
	clientName  string
@@ -161,16 +162,20 @@ func (dc *downstreamConn) upstream() *upstreamConn {
	return dc.network.conn
}

// isOurNick checks whether the given nick is ours on the given network,
// according to its casemapping.
//
// Therefore nick should not be casemapped beforehand.
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
@@ -182,6 +187,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")
@@ -195,6 +201,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")
@@ -411,13 +418,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
@@ -724,6 +733,7 @@ func (dc *downstreamConn) updateNick() {
			Params:  []string{uc.nick},
		})
		dc.nick = uc.nick
		dc.nickCM = casemapASCII(dc.nick)
	}
}

@@ -885,9 +895,13 @@ func (dc *downstreamConn) welcome() error {
	dc.SendMessage(&irc.Message{
		Prefix:  dc.srv.prefix(),
		Command: irc.RPL_MYINFO,
		Params:  []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
		Params:  []string{dc.nick, dc.srv.Hostname, "soju", "oOwari", "OovaimnqpsrtklbeI"},
	})
	dc.SendMessage(&irc.Message{
		Prefix:  dc.srv.prefix(),
		Command: irc.RPL_ISUPPORT,
		Params:  []string{dc.nick, "CASEMAPPING=ascii", "are simpleyet by this server"},
	})
	// TODO: RPL_ISUPPORT
	// TODO: send CHATHISTORY in RPL_ISUPPORT when implemented
	dc.SendMessage(&irc.Message{
		Prefix:  dc.srv.prefix(),
@@ -1082,6 +1096,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				nick = unmarshaledNick
			}
		}
		nickCM := casemapASCII(nick)

		if strings.ContainsAny(nick, illegalNickChars) {
			return ircError{&irc.Message{
@@ -1089,7 +1104,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  []string{dc.nick, rawNick, "contains illegal characters"},
			}}
		}
		if nick == serviceNick {
		if nickCM == serviceNickCM {
			return ircError{&irc.Message{
				Command: irc.ERR_NICKNAMEINUSE,
				Params:  []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
@@ -1125,6 +1140,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  []string{nick},
			})
			dc.nick = nick
			dc.nickCM = casemapASCII(nick)
		}
	case "JOIN":
		var namesStr string
@@ -1142,6 +1158,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			if err != nil {
				return err
			}
			upstreamNameCM := uc.network.casemap(upstreamName)

			var key string
			if len(keys) > i {
@@ -1159,7 +1176,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {

			var ch *Channel
			var ok bool
			if ch, ok = uc.network.channels[upstreamName]; ok {
			if ch, ok = uc.network.channels[upstreamNameCM]; ok {
				// Don't clear the channel key if there's one set
				// TODO: add a way to unset the channel key
				if key != "" {
@@ -1291,7 +1308,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			modeStr = msg.Params[1]
		}

		if name == dc.nick {
		nameCM := casemapASCII(name)

		if nameCM == dc.nickCM {
			if modeStr != "" {
				dc.forEachUpstream(func(uc *upstreamConn) {
					uc.SendMessageLabeled(dc.id, &irc.Message{
@@ -1329,7 +1348,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  params,
			})
		} else {
			ch, ok := uc.channels[upstreamName]
			upstreamNameCM := uc.network.casemap(upstreamName)
			ch, ok := uc.channels[upstreamNameCM]
			if !ok {
				return ircError{&irc.Message{
					Command: irc.ERR_NOSUCHCHANNEL,
@@ -1378,7 +1398,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Params:  []string{upstreamChannel, topic},
			})
		} else { // getting topic
			ch, ok := uc.channels[upstreamChannel]
			upstreamChannelCM := uc.network.casemap(upstreamChannel)
			ch, ok := uc.channels[upstreamChannelCM]
			if !ok {
				return ircError{&irc.Message{
					Command: irc.ERR_NOSUCHCHANNEL,
@@ -1449,7 +1470,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				return err
			}

			ch, ok := uc.channels[upstreamChannel]
			upstreamChannelCM := uc.network.casemap(upstreamChannel)
			ch, ok := uc.channels[upstreamChannelCM]
			if ok {
				sendNames(dc, ch)
			} else {
@@ -1473,8 +1495,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(),
@@ -1488,7 +1511,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,
@@ -1539,7 +1562,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			mask = mask[:i]
		}

		if mask == dc.nick {
		maskCM := casemapASCII(mask)

		if maskCM == dc.nickCM {
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
				Command: irc.RPL_WHOISUSER,
@@ -1587,7 +1612,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
		tags := copyClientTags(msg.Tags)

		for _, name := range strings.Split(targetsStr, ",") {
			if name == serviceNick {
			nameCM := casemapASCII(name)
			if nameCM == serviceNickCM {
				if dc.caps["echo-message"] {
					echoTags := tags.Copy()
					echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
@@ -1607,7 +1633,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				return err
			}

			if upstreamName == "NickServ" {
			upstreamNameCM := uc.network.casemap(upstreamName)

			if upstreamNameCM == "nickserv" {
				dc.handleNickServPRIVMSG(uc, text)
			}

@@ -1632,7 +1660,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
				Command: "PRIVMSG",
				Params:  []string{upstreamName, text},
			}
			uc.produce(upstreamName, echoMsg, dc)
			uc.produce(upstreamNameCM, echoMsg, dc)

			uc.updateChannelAutoDetach(upstreamName)
		}
@@ -1763,12 +1791,14 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
			}}
		}

		entityCM := uc.network.casemap(entity)

		var history []*irc.Message
		switch subcommand {
		case "BEFORE":
			history, err = dc.user.msgStore.LoadBeforeTime(uc.network, entity, timestamp, limit)
			history, err = dc.user.msgStore.LoadBeforeTime(uc.network, entityCM, timestamp, limit)
		case "AFTER":
			history, err = dc.user.msgStore.LoadAfterTime(uc.network, entity, timestamp, limit)
			history, err = dc.user.msgStore.LoadAfterTime(uc.network, entityCM, timestamp, limit)
		default:
			// TODO: support LATEST, BETWEEN
			return ircError{&irc.Message{
diff --git a/irc.go b/irc.go
index 9a09da4..e6ad6b3 100644
--- a/irc.go
+++ b/irc.go
@@ -118,12 +118,13 @@ outer:
					return nil, fmt.Errorf("malformed modestring %q: missing mode argument for %c%c", modeStr, plusMinus, mode)
				}
				member := arguments[nextArgument]
				if _, ok := ch.Members[member]; ok {
				memberCM := ch.conn.network.casemap(member)
				if _, ok := ch.Members[memberCM]; ok {
					if plusMinus == '+' {
						ch.Members[member].Add(ch.conn.availableMemberships, membership)
						ch.Members[memberCM].Memberships.Add(ch.conn.availableMemberships, membership)
					} else {
						// TODO: for upstreams without multi-prefix, query the user modes again
						ch.Members[member].Remove(membership)
						ch.Members[memberCM].Memberships.Remove(membership)
					}
				}
				needMarshaling[nextArgument] = struct{}{}
@@ -391,3 +392,88 @@ func parseCTCPMessage(msg *irc.Message) (cmd string, params string, ok bool) {

	return cmd, params, true
}

type casemapping func(string) string

// CasemapASCII of name is the canonical representation of name according to the
// ascii casemapping.
func casemapASCII(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
		}
		sb.WriteRune(r)
	}
	return sb.String()
}

// casemapRFC1459 of name is the canonical representation of name according to the
// rfc1459 casemapping.
func casemapRFC1459(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
		} else if r == '[' {
			r = '{'
		} else if r == ']' {
			r = '}'
		} else if r == '\\' {
			r = '|'
		} else if r == '~' {
			r = '^'
		}
		sb.WriteRune(r)
	}
	return sb.String()
}

// casemapRFC1459Strict of name is the canonical representation of name
// according to the rfc1459-strict casemapping.
func casemapRFC1459Strict(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
		} else if r == '[' {
			r = '{'
		} else if r == ']' {
			r = '}'
		} else if r == '\\' {
			r = '|'
		}
		sb.WriteRune(r)
	}
	return sb.String()
}

func parseCasemappingToken(tokenValue string) (casemap casemapping, ok bool) {
	switch tokenValue {
	case "ascii":
		casemap = casemapASCII
	case "rfc1459":
		casemap = casemapRFC1459
	case "rfc1459-strict":
		casemap = casemapRFC1459Strict
	default:
		return nil, false
	}
	return casemap, true
}

func partialCasemap(higher casemapping, name string) string {
	nameFullyCM := higher(name)
	var sb strings.Builder
	sb.Grow(len(name))
	for i, r := range nameFullyCM {
		if 'a' <= r && r <= 'z' {
			r = rune(name[i])
		}
		sb.WriteRune(r)
	}
	return sb.String()
}
diff --git a/service.go b/service.go
index 1c239e1..76d8a20 100644
--- a/service.go
+++ b/service.go
@@ -27,6 +27,7 @@ import (
)

const serviceNick = "BouncerServ"
const serviceNickCM = "bouncerserv"
const serviceRealname = "soju bouncer service"

var servicePrefix = &irc.Prefix{
@@ -767,8 +768,9 @@ func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
	if err != nil {
		return fmt.Errorf("unknown channel %q", name)
	}
	upstreamNameCM := uc.network.casemap(upstreamName)

	ch, ok := uc.network.channels[upstreamName]
	ch, ok := uc.network.channels[upstreamNameCM]
	if !ok {
		return fmt.Errorf("unknown channel %q", name)
	}
@@ -777,7 +779,7 @@ func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
		return err
	}

	uc.updateChannelAutoDetach(upstreamName)
	uc.updateChannelAutoDetach(upstreamNameCM)

	if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
		return fmt.Errorf("failed to update channel: %v", err)
diff --git a/upstream.go b/upstream.go
index aaa3d1f..97474d0 100644
--- a/upstream.go
+++ b/upstream.go
@@ -39,6 +39,11 @@ func (err registrationError) Error() string {
	return fmt.Sprintf("registration error: %v", string(err))
}

type upstreamMember struct {
	Nick        string
	Memberships *memberships
}

type upstreamChannel struct {
	Name         string
	conn         *upstreamConn
@@ -48,7 +53,7 @@ type upstreamChannel struct {
	Status       channelStatus
	modes        channelModes
	creationTime string
	Members      map[string]*memberships
	Members      map[string]upstreamMember
	complete     bool
	detachTimer  *time.Timer
}
@@ -82,9 +87,11 @@ type upstreamConn struct {
	availableChannelModes map[byte]channelModeType
	availableChannelTypes string
	availableMemberships  []membership
	advertisedCasemap     bool

	registered    bool
	nick          string
	nickCM        string // casemapped nickname
	username      string
	realname      string
	modes         userModes
@@ -216,6 +223,7 @@ func (uc *upstreamConn) forEachDownstreamByID(id uint64, f func(*downstreamConn)
	})
}

// name must be casemapped.
func (uc *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
	ch, ok := uc.channels[name]
	if !ok {
@@ -406,11 +414,14 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			}
		}

		if msg.Prefix.Name == serviceNick {
		prefixNameCM := uc.network.casemap(msg.Prefix.Name)
		entityCM := uc.network.casemap(entity)

		if prefixNameCM == serviceNickCM {
			uc.logger.Printf("skipping %v from soju's service: %v", msg.Command, msg)
			break
		}
		if entity == serviceNick {
		if entityCM == serviceNickCM {
			uc.logger.Printf("skipping %v to soju's service: %v", msg.Command, msg)
			break
		}
@@ -418,9 +429,9 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		if msg.Prefix.User == "" && msg.Prefix.Host == "" { // server message
			uc.produce("", msg, nil)
		} else { // regular user message
			target := entity
			if target == uc.nick {
				target = msg.Prefix.Name
			target := entityCM
			if target == uc.nickCM {
				target = prefixNameCM
			}

			if ch, ok := uc.network.channels[target]; ok {
@@ -428,7 +439,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
					uc.handleDetachedMessage(msg.Prefix.Name, text, ch)
				}

				highlight := msg.Prefix.Name != uc.nick && isHighlight(text, uc.nick)
				highlight := prefixNameCM != uc.nickCM && isHighlight(text, uc.nick)
				if ch.DetachOn == FilterMessage || ch.DetachOn == FilterDefault || (ch.DetachOn == FilterHighlight && highlight) {
					uc.updateChannelAutoDetach(target)
				}
@@ -600,6 +611,10 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			dc.updateSupportedCaps()
		})

		// Heuristic: we can join the channels before receiving the
		// eventual CASEMAPPING 005 token, because upstream *should*
		// respond to the JOINs after sending 005 (and after sending the
		// MOTD otherwise).
		if len(uc.network.channels) > 0 {
			var channels, keys []string
			for _, ch := range uc.network.channels {
@@ -634,6 +649,14 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			}
			if !negate {
				switch parameter {
				case "CASEMAPPING":
					casemap, ok := parseCasemappingToken(value)
					if !ok {
						casemap = casemapRFC1459
					}
					uc.network.updateCasemapping(casemap)
					uc.nickCM = uc.network.casemap(uc.nick)
					uc.advertisedCasemap = true
				case "CHANMODES":
					parts := strings.SplitN(value, ",", 5)
					if len(parts) < 4 {
@@ -674,6 +697,11 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
				// TODO: handle ISUPPORT negations
			}
		}
	case irc.RPL_ENDOFMOTD, irc.ERR_NOMOTD:
		if !uc.advertisedCasemap {
			uc.network.updateCasemapping(casemapRFC1459)
			uc.nickCM = uc.network.casemap(uc.nick)
		}
	case "BATCH":
		var tag string
		if err := parseMessageParams(msg, &tag); err != nil {
@@ -718,18 +746,22 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		prefixNameCM := uc.network.casemap(msg.Prefix.Name)
		newNickCM := uc.network.casemap(newNick)

		me := false
		if msg.Prefix.Name == uc.nick {
		if prefixNameCM == uc.nickCM {
			uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
			me = true
			uc.nick = newNick
			uc.nickCM = newNickCM
		}

		for _, ch := range uc.channels {
			if memberships, ok := ch.Members[msg.Prefix.Name]; ok {
				delete(ch.Members, msg.Prefix.Name)
				ch.Members[newNick] = memberships
				uc.appendLog(ch.Name, msg)
		for chName, ch := range uc.channels {
			if member, ok := ch.Members[prefixNameCM]; ok {
				delete(ch.Members, prefixNameCM)
				ch.Members[newNickCM] = member
				uc.appendLog(chName, msg)
			}
		}

@@ -752,13 +784,16 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		prefixNameCM := uc.network.casemap(msg.Prefix.Name)

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

@@ -767,16 +802,19 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
					Params:  []string{ch},
				})
			} else {
				ch, err := uc.getChannel(ch)
				ch, err := uc.getChannel(chCM)
				if err != nil {
					return err
				}
				ch.Members[msg.Prefix.Name] = &memberships{}
				ch.Members[prefixNameCM] = upstreamMember{
					Nick:        msg.Prefix.Name,
					Memberships: &memberships{},
				}
			}

			chMsg := msg.Copy()
			chMsg.Params[0] = ch
			uc.produce(ch, chMsg, nil)
			uc.produce(chCM, chMsg, nil)
		}
	case "PART":
		if msg.Prefix == nil {
@@ -788,24 +826,27 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		prefixNameCM := uc.network.casemap(msg.Prefix.Name)

		for _, ch := range strings.Split(channels, ",") {
			if msg.Prefix.Name == uc.nick {
			chCM := uc.network.casemap(ch)
			if prefixNameCM == uc.nickCM {
				uc.logger.Printf("parted channel %q", ch)
				if uch, ok := uc.channels[ch]; ok {
					delete(uc.channels, ch)
				if uch, ok := uc.channels[chCM]; ok {
					delete(uc.channels, chCM)
					uch.updateAutoDetach(0)
				}
			} else {
				ch, err := uc.getChannel(ch)
				ch, err := uc.getChannel(chCM)
				if err != nil {
					return err
				}
				delete(ch.Members, msg.Prefix.Name)
				delete(ch.Members, prefixNameCM)
			}

			chMsg := msg.Copy()
			chMsg.Params[0] = ch
			uc.produce(ch, chMsg, nil)
			uc.produce(chCM, chMsg, nil)
		}
	case "KICK":
		if msg.Prefix == nil {
@@ -817,36 +858,41 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		if user == uc.nick {
		channelCM := uc.network.casemap(channel)
		userCM := uc.network.casemap(user)

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

		uc.produce(channel, msg, nil)
		uc.produce(channelCM, msg, nil)
	case "QUIT":
		if msg.Prefix == nil {
			return fmt.Errorf("expected a prefix")
		}

		if msg.Prefix.Name == uc.nick {
		prefixNameCM := uc.network.casemap(msg.Prefix.Name)

		if prefixNameCM == uc.nickCM {
			uc.logger.Printf("quit")
		}

		for _, ch := range uc.channels {
			if _, ok := ch.Members[msg.Prefix.Name]; ok {
				delete(ch.Members, msg.Prefix.Name)
		for chName, ch := range uc.channels {
			if _, ok := ch.Members[prefixNameCM]; ok {
				delete(ch.Members, prefixNameCM)

				uc.appendLog(ch.Name, msg)
				uc.appendLog(chName, msg)
			}
		}

		if msg.Prefix.Name != uc.nick {
		if prefixNameCM != uc.nickCM {
			uc.forEachDownstream(func(dc *downstreamConn) {
				dc.SendMessage(dc.marshalMessage(msg, uc.network))
			})
@@ -856,7 +902,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
			return err
		}
		ch, err := uc.getChannel(name)
		nameCM := uc.network.casemap(name)
		ch, err := uc.getChannel(nameCM)
		if err != nil {
			return err
		}
@@ -874,7 +921,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		if err := parseMessageParams(msg, &name); err != nil {
			return err
		}
		ch, err := uc.getChannel(name)
		nameCM := uc.network.casemap(name)
		ch, err := uc.getChannel(nameCM)
		if err != nil {
			return err
		}
@@ -885,21 +933,23 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		} else {
			ch.Topic = ""
		}
		uc.produce(ch.Name, msg, nil)
		uc.produce(nameCM, msg, nil)
	case "MODE":
		var name, modeStr string
		if err := parseMessageParams(msg, &name, &modeStr); err != nil {
			return err
		}

		nameCM := uc.network.casemap(name)

		if !uc.isChannel(name) { // user mode change
			if name != uc.nick {
			if nameCM != uc.nickCM {
				return fmt.Errorf("received MODE message for unknown nick %q", name)
			}
			return uc.modes.Apply(modeStr)
			// TODO: notify downstreams about user mode change?
		} else { // channel mode change
			ch, err := uc.getChannel(name)
			ch, err := uc.getChannel(nameCM)
			if err != nil {
				return err
			}
@@ -909,12 +959,12 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
				return err
			}

			uc.appendLog(ch.Name, msg)
			uc.appendLog(nameCM, msg)

			if ch, ok := uc.network.channels[name]; !ok || !ch.Detached {
			if ch, ok := uc.network.channels[nameCM]; !ok || !ch.Detached {
				uc.forEachDownstream(func(dc *downstreamConn) {
					params := make([]string, len(msg.Params))
					params[0] = dc.marshalEntity(uc.network, name)
					params[0] = dc.marshalEntity(uc.network, ch.Name)
					params[1] = modeStr

					copy(params[2:], msg.Params[2:])
@@ -951,12 +1001,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		if err := parseMessageParams(msg, nil, &channel); err != nil {
			return err
		}
		channelCM := uc.network.casemap(channel)
		modeStr := ""
		if len(msg.Params) > 2 {
			modeStr = msg.Params[2]
		}

		ch, err := uc.getChannel(channel)
		ch, err := uc.getChannel(channelCM)
		if err != nil {
			return err
		}
@@ -967,11 +1018,11 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}
		if firstMode {
			if c, ok := uc.network.channels[channel]; !ok || !c.Detached {
			if c, ok := uc.network.channels[channelCM]; !ok || !c.Detached {
				modeStr, modeParams := ch.modes.Format()

				uc.forEachDownstream(func(dc *downstreamConn) {
					params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
					params := []string{dc.nick, dc.marshalEntity(uc.network, ch.Name), modeStr}
					params = append(params, modeParams...)

					dc.SendMessage(&irc.Message{
@@ -988,7 +1039,9 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		ch, err := uc.getChannel(channel)
		channelCM := uc.network.casemap(channel)

		ch, err := uc.getChannel(channelCM)
		if err != nil {
			return err
		}
@@ -1009,7 +1062,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
			return err
		}
		ch, err := uc.getChannel(name)
		nameCM := uc.network.casemap(name)
		ch, err := uc.getChannel(nameCM)
		if err != nil {
			return err
		}
@@ -1064,11 +1118,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		ch, ok := uc.channels[name]
		nameCM := uc.network.casemap(name)

		ch, ok := uc.channels[nameCM]
		if !ok {
			// NAMES on a channel we have not joined, forward to downstream
			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
				channel := dc.marshalEntity(uc.network, name)
				channel := dc.marshalEntity(uc.network, ch.Name)
				members := splitSpace(members)
				for i, member := range members {
					memberships, nick := uc.parseMembershipPrefix(member)
@@ -1093,7 +1149,11 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {

		for _, s := range splitSpace(members) {
			memberships, nick := uc.parseMembershipPrefix(s)
			ch.Members[nick] = memberships
			nickCM := uc.network.casemap(nick)
			ch.Members[nickCM] = upstreamMember{
				Nick:        nick,
				Memberships: memberships,
			}
		}
	case irc.RPL_ENDOFNAMES:
		var name string
@@ -1101,11 +1161,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
			return err
		}

		ch, ok := uc.channels[name]
		nameCM := uc.network.casemap(name)

		ch, ok := uc.channels[nameCM]
		if !ok {
			// NAMES on a channel we have not joined, forward to downstream
			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
				channel := dc.marshalEntity(uc.network, name)
				channel := dc.marshalEntity(uc.network, ch.Name)

				dc.SendMessage(&irc.Message{
					Prefix:  dc.srv.prefix(),
@@ -1121,7 +1183,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
		}
		ch.complete = true

		if c, ok := uc.network.channels[name]; !ok || !c.Detached {
		if c, ok := uc.network.channels[nameCM]; !ok || !c.Detached {
			uc.forEachDownstream(func(dc *downstreamConn) {
				forwardChannel(dc, ch)
			})
@@ -1393,7 +1455,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
@@ -1437,7 +1499,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
}

func (uc *upstreamConn) handleDetachedMessage(sender string, text string, ch *Channel) {
	highlight := sender != uc.nick && isHighlight(text, uc.nick)
	senderCM := uc.network.casemap(sender)
	highlight := senderCM != uc.nickCM && isHighlight(text, uc.nick)
	if ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight) {
		uc.forEachDownstream(func(dc *downstreamConn) {
			if highlight {
@@ -1559,6 +1622,7 @@ func splitSpace(s string) []string {

func (uc *upstreamConn) register() {
	uc.nick = uc.network.Nick
	uc.nickCM = uc.network.casemap(uc.network.Nick)
	uc.username = uc.network.Username
	if uc.username == "" {
		uc.username = uc.nick
@@ -1658,6 +1722,8 @@ func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message
//
// The internal message ID is returned. If the message isn't recorded in the
// log file, an empty string is returned.
//
// entity must be casemapped.
func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string) {
	if uc.user.msgStore == nil {
		return ""
@@ -1708,6 +1774,8 @@ func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string
//
// If origin is not nil and origin doesn't support echo-message, the message is
// forwarded to all connections except origin.
//
// target must be casemapped.
func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
	var msgID string
	if target != "" {
@@ -1749,6 +1817,7 @@ func (uc *upstreamConn) updateAway() {
	uc.away = away
}

// name must be casemapped.
func (uc *upstreamConn) updateChannelAutoDetach(name string) {
	if uch, ok := uc.channels[name]; ok {
		if ch, ok := uc.network.channels[name]; ok && !ch.Detached {
diff --git a/user.go b/user.go
index 6d54ffc..ed2e76c 100644
--- a/user.go
+++ b/user.go
@@ -66,15 +66,18 @@ type network struct {

	conn           *upstreamConn
	channels       map[string]*Channel
	history        map[string]*networkHistory // indexed by entity
	history        map[string]*networkHistory // indexed by casemapped entity
	offlineClients map[string]struct{}        // indexed by client name
	lastError      error
	casemap        casemapping
}

func newNetwork(user *user, record *Network, channels []Channel) *network {
	m := make(map[string]*Channel, len(channels))
	for _, ch := range channels {
		ch := ch
		// Don't casemap channel names yet, we don't know the
		// casemapping the server is going to use.
		m[ch.Name] = &ch
	}

@@ -85,6 +88,7 @@ func newNetwork(user *user, record *Network, channels []Channel) *network {
		channels:       m,
		history:        make(map[string]*networkHistory),
		offlineClients: make(map[string]struct{}),
		casemap:        casemapRFC1459,
	}
}

@@ -188,8 +192,9 @@ func (net *network) detach(ch *Channel) {
	ch.Detached = true
	net.user.srv.Logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)

	chNameCM := net.casemap(ch.Name)
	if net.conn != nil {
		if uch, ok := net.conn.channels[ch.Name]; ok {
		if uch, ok := net.conn.channels[chNameCM]; ok {
			uch.updateAutoDetach(0)
		}
	}
@@ -213,10 +218,11 @@ func (net *network) attach(ch *Channel) {
	net.user.srv.Logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)

	var uch *upstreamChannel
	chNameCM := net.casemap(ch.Name)
	if net.conn != nil {
		uch = net.conn.channels[ch.Name]
		uch = net.conn.channels[chNameCM]

		net.conn.updateChannelAutoDetach(ch.Name)
		net.conn.updateChannelAutoDetach(chNameCM)
	}

	net.forEachDownstream(func(dc *downstreamConn) {
@@ -230,14 +236,15 @@ func (net *network) attach(ch *Channel) {
			forwardChannel(dc, uch)
		}

		if net.history[ch.Name] != nil {
		if net.history[chNameCM] != nil {
			dc.sendNetworkHistory(net)
		}
	})
}

func (net *network) deleteChannel(name string) error {
	ch, ok := net.channels[name]
	nameCM := net.casemap(name)
	ch, ok := net.channels[nameCM]
	if !ok {
		return fmt.Errorf("unknown channel %q", name)
	}
@@ -250,10 +257,19 @@ func (net *network) deleteChannel(name string) error {
	if err := net.user.srv.db.DeleteChannel(ch.ID); err != nil {
		return err
	}
	delete(net.channels, name)
	delete(net.channels, nameCM)
	return nil
}

func (net *network) updateCasemapping(newCasemap casemapping) {
	net.casemap = newCasemap
	newChannels := make(map[string]*Channel, len(net.channels))
	for _, c := range net.channels {
		newChannels[net.casemap(c.Name)] = c
	}
	net.channels = newChannels
}

type user struct {
	User
	srv *Server
-- 
2.26.2