Index: /trunk/bridge.go
===================================================================
--- /trunk/bridge.go	(revision 259)
+++ /trunk/bridge.go	(revision 260)
@@ -17,5 +17,5 @@
 
 func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
-	downstreamName := dc.marshalEntity(ch.conn, ch.Name)
+	downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
 
 	if ch.Topic != "" {
@@ -37,8 +37,8 @@
 	// TODO: send multiple members in each message
 
-	downstreamName := dc.marshalEntity(ch.conn, ch.Name)
+	downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
 
 	for nick, membership := range ch.Members {
-		s := membership.String() + dc.marshalEntity(ch.conn, nick)
+		s := membership.String() + dc.marshalEntity(ch.conn.network, nick)
 
 		dc.SendMessage(&irc.Message{
Index: /trunk/downstream.go
===================================================================
--- /trunk/downstream.go	(revision 259)
+++ /trunk/downstream.go	(revision 260)
@@ -122,4 +122,16 @@
 }
 
+func isOurNick(net *network, nick string) bool {
+	// TODO: this doesn't account for nick changes
+	if net.conn != nil {
+		return nick == net.conn.nick
+	}
+	// 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
+}
+
 // marshalEntity converts an upstream entity name (ie. channel or nick) into a
 // downstream entity name.
@@ -127,23 +139,23 @@
 // This involves adding a "/<network>" suffix if the entity isn't the current
 // user.
-func (dc *downstreamConn) marshalEntity(uc *upstreamConn, name string) string {
+func (dc *downstreamConn) marshalEntity(net *network, name string) string {
 	if dc.network != nil {
-		if dc.network != uc.network {
+		if dc.network != net {
 			panic("soju: tried to marshal an entity for another network")
 		}
 		return name
 	}
-	if name == uc.nick {
+	if isOurNick(net, name) {
 		return dc.nick
 	}
-	return name + "/" + uc.network.GetName()
-}
-
-func (dc *downstreamConn) marshalUserPrefix(uc *upstreamConn, prefix *irc.Prefix) *irc.Prefix {
-	if prefix.Name == uc.nick {
+	return name + "/" + net.GetName()
+}
+
+func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *irc.Prefix {
+	if isOurNick(net, prefix.Name) {
 		return dc.prefix()
 	}
 	if dc.network != nil {
-		if dc.network != uc.network {
+		if dc.network != net {
 			panic("soju: tried to marshal a user prefix for another network")
 		}
@@ -151,5 +163,5 @@
 	}
 	return &irc.Prefix{
-		Name: prefix.Name + "/" + uc.network.GetName(),
+		Name: prefix.Name + "/" + net.GetName(),
 		User: prefix.User,
 		Host: prefix.Host,
@@ -229,21 +241,21 @@
 func (dc *downstreamConn) marshalMessage(msg *irc.Message, uc *upstreamConn) *irc.Message {
 	msg = msg.Copy()
-	msg.Prefix = dc.marshalUserPrefix(uc, msg.Prefix)
+	msg.Prefix = dc.marshalUserPrefix(uc.network, msg.Prefix)
 
 	switch msg.Command {
 	case "PRIVMSG", "NOTICE":
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
 	case "NICK":
 		// Nick change for another user
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
 	case "JOIN", "PART":
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
 	case "KICK":
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
-		msg.Params[1] = dc.marshalEntity(uc, msg.Params[1])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
+		msg.Params[1] = dc.marshalEntity(uc.network, msg.Params[1])
 	case "TOPIC":
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
 	case "MODE":
-		msg.Params[0] = dc.marshalEntity(uc, msg.Params[0])
+		msg.Params[0] = dc.marshalEntity(uc.network, msg.Params[0])
 	case "QUIT":
 		// This space is intentinally left blank
@@ -663,5 +675,5 @@
 					Prefix:  dc.prefix(),
 					Command: "JOIN",
-					Params:  []string{dc.marshalEntity(ch.conn, ch.Name)},
+					Params:  []string{dc.marshalEntity(ch.conn.network, ch.Name)},
 				})
 
@@ -714,5 +726,5 @@
 				Prefix:  dc.srv.prefix(),
 				Command: "BATCH",
-				Params:  []string{"+" + batchRef, "chathistory", dc.marshalEntity(uc, target)},
+				Params:  []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
 			})
 		}
Index: /trunk/upstream.go
===================================================================
--- /trunk/upstream.go	(revision 259)
+++ /trunk/upstream.go	(revision 260)
@@ -765,5 +765,5 @@
 
 			uc.forEachDownstream(func(dc *downstreamConn) {
-				params := []string{dc.nick, dc.marshalEntity(uc, channel), modeStr}
+				params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
 				params = append(params, modeParams...)
 
@@ -827,5 +827,5 @@
 				Prefix:  dc.srv.prefix(),
 				Command: irc.RPL_LIST,
-				Params:  []string{dc.nick, dc.marshalEntity(uc, channel), clients, topic},
+				Params:  []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
 			})
 		})
@@ -845,9 +845,9 @@
 			// NAMES on a channel we have not joined, forward to downstream
 			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-				channel := dc.marshalEntity(uc, name)
+				channel := dc.marshalEntity(uc.network, name)
 				members := splitSpace(members)
 				for i, member := range members {
 					membership, nick := uc.parseMembershipPrefix(member)
-					members[i] = membership.String() + dc.marshalEntity(uc, nick)
+					members[i] = membership.String() + dc.marshalEntity(uc.network, nick)
 				}
 				memberStr := strings.Join(members, " ")
@@ -882,5 +882,5 @@
 			// NAMES on a channel we have not joined, forward to downstream
 			uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-				channel := dc.marshalEntity(uc, name)
+				channel := dc.marshalEntity(uc.network, name)
 
 				dc.SendMessage(&irc.Message{
@@ -923,7 +923,7 @@
 			channel := channel
 			if channel != "*" {
-				channel = dc.marshalEntity(uc, channel)
-			}
-			nick := dc.marshalEntity(uc, nick)
+				channel = dc.marshalEntity(uc.network, channel)
+			}
+			nick := dc.marshalEntity(uc.network, nick)
 			dc.SendMessage(&irc.Message{
 				Prefix:  dc.srv.prefix(),
@@ -942,5 +942,5 @@
 			if name != "*" {
 				// TODO: support WHO masks
-				name = dc.marshalEntity(uc, name)
+				name = dc.marshalEntity(uc.network, name)
 			}
 			dc.SendMessage(&irc.Message{
@@ -957,5 +957,5 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			dc.SendMessage(&irc.Message{
 				Prefix:  dc.srv.prefix(),
@@ -971,5 +971,5 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			dc.SendMessage(&irc.Message{
 				Prefix:  dc.srv.prefix(),
@@ -985,5 +985,5 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			dc.SendMessage(&irc.Message{
 				Prefix:  dc.srv.prefix(),
@@ -999,5 +999,5 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			params := []string{dc.nick, nick}
 			params = append(params, msg.Params[2:]...)
@@ -1016,9 +1016,9 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			channelList := make([]string, len(channels))
 			for i, channel := range channels {
 				prefix, channel := uc.parseMembershipPrefix(channel)
-				channel = dc.marshalEntity(uc, channel)
+				channel = dc.marshalEntity(uc.network, channel)
 				channelList[i] = prefix.String() + channel
 			}
@@ -1037,5 +1037,5 @@
 
 		uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
-			nick := dc.marshalEntity(uc, nick)
+			nick := dc.marshalEntity(uc.network, nick)
 			dc.SendMessage(&irc.Message{
 				Prefix:  dc.srv.prefix(),
@@ -1077,7 +1077,7 @@
 		uc.forEachDownstream(func(dc *downstreamConn) {
 			dc.SendMessage(&irc.Message{
-				Prefix:  dc.marshalUserPrefix(uc, msg.Prefix),
+				Prefix:  dc.marshalUserPrefix(uc.network, msg.Prefix),
 				Command: "INVITE",
-				Params:  []string{dc.marshalEntity(uc, nick), dc.marshalEntity(uc, channel)},
+				Params:  []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
 			})
 		})
@@ -1093,5 +1093,5 @@
 				Prefix:  dc.srv.prefix(),
 				Command: irc.RPL_INVITING,
-				Params:  []string{dc.nick, dc.marshalEntity(uc, nick), dc.marshalEntity(uc, channel)},
+				Params:  []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
 			})
 		})
