2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-02-06 21:45:54 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-29 15:57:38 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-20 08:39:23 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
2022-05-09 14:15:00 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~emersion/soju/xirc"
|
2020-02-06 21:45:54 +00:00
|
|
|
)
|
|
|
|
|
2021-01-29 15:57:38 +00:00
|
|
|
func forwardChannel(ctx context.Context, dc *downstreamConn, ch *upstreamChannel) {
|
2020-02-06 21:45:54 +00:00
|
|
|
if !ch.complete {
|
|
|
|
panic("Tried to forward a partial channel")
|
|
|
|
}
|
|
|
|
|
2022-01-10 09:36:45 +00:00
|
|
|
// RPL_NOTOPIC shouldn't be sent on JOIN
|
|
|
|
if ch.Topic != "" {
|
|
|
|
sendTopic(dc, ch)
|
|
|
|
}
|
2021-01-29 15:57:38 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("soju.im/read") {
|
2021-01-29 15:57:38 +00:00
|
|
|
channelCM := ch.conn.network.casemap(ch.Name)
|
|
|
|
r, err := dc.srv.db.GetReadReceipt(ctx, ch.conn.network.ID, channelCM)
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to get the read receipt for %q: %v", ch.Name, err)
|
|
|
|
} else {
|
|
|
|
timestampStr := "*"
|
|
|
|
if r != nil {
|
2022-05-09 14:15:00 +00:00
|
|
|
timestampStr = fmt.Sprintf("timestamp=%s", xirc.FormatServerTime(r.Timestamp))
|
2021-01-29 15:57:38 +00:00
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "READ",
|
|
|
|
Params: []string{dc.marshalEntity(ch.conn.network, ch.Name), timestampStr},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:49:01 +00:00
|
|
|
if !dc.caps.IsEnabled("soju.im/no-implicit-names") {
|
|
|
|
sendNames(dc, ch)
|
|
|
|
}
|
2020-03-25 23:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
|
2020-04-16 15:19:00 +00:00
|
|
|
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
2020-02-19 17:25:19 +00:00
|
|
|
|
2020-02-06 21:45:54 +00:00
|
|
|
if ch.Topic != "" {
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_TOPIC,
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, ch.Topic},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-08-20 08:39:23 +00:00
|
|
|
if ch.TopicWho != nil {
|
|
|
|
topicWho := dc.marshalUserPrefix(ch.conn.network, ch.TopicWho)
|
|
|
|
topicTime := strconv.FormatInt(ch.TopicTime.Unix(), 10)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_TOPICWHOTIME,
|
2020-08-20 08:39:23 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, topicWho.String(), topicTime},
|
|
|
|
})
|
|
|
|
}
|
2020-02-06 21:45:54 +00:00
|
|
|
} else {
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NOTOPIC,
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, "No topic is set"},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-06 21:45:54 +00:00
|
|
|
}
|
2020-03-21 00:24:29 +00:00
|
|
|
}
|
2020-02-06 21:45:54 +00:00
|
|
|
|
2020-03-21 00:24:29 +00:00
|
|
|
func sendNames(dc *downstreamConn, ch *upstreamChannel) {
|
2020-04-16 15:19:00 +00:00
|
|
|
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
2020-03-21 00:24:29 +00:00
|
|
|
|
2022-05-29 15:57:21 +00:00
|
|
|
var members []string
|
Implement casemapping
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.
2021-03-16 09:00:34 +00:00
|
|
|
for _, entry := range ch.Members.innerMap {
|
|
|
|
nick := entry.originalKey
|
|
|
|
memberships := entry.value.(*memberships)
|
2020-04-30 21:39:59 +00:00
|
|
|
s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
|
2022-05-29 15:57:21 +00:00
|
|
|
members = append(members, s)
|
2020-06-30 07:11:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 15:57:21 +00:00
|
|
|
msgs := xirc.GenerateNamesReply(dc.srv.prefix(), dc.nick, downstreamName, ch.Status, members)
|
|
|
|
for _, msg := range msgs {
|
|
|
|
dc.SendMessage(msg)
|
2020-02-06 21:45:54 +00:00
|
|
|
}
|
|
|
|
}
|