Commit graph

208 commits

Author SHA1 Message Date
delthas 791cf4b887
Add support for downstream WHOIS nick/network nick/network
Many IRC clients use the query `WHOIS nick nick` rather than
`WHOIS nick` when querying a nick. The former command means to
specifically query the WHOIS on the server to which `nick` is connected,
which is useful to get information that is sometimes not propagated
between servers, such as idle time.

In the case where a downstream sends WHOIS nick/network nick/network in
multi-server mode, we need to unmarshal both fields.

Previously, we did not unmarshal those fields, and upstreams would
receive `WHOIS nick/network nick`, which is incorrect.

This adds support for unmarshaling the target field if it is the same as
the mask field, by simply using the unmarshaled nick that is already
computed from the mask.
2020-05-27 23:44:38 +02:00
delthas a64e1d6761
Add support for downstream LIST to a single upstream
Sometimes, doing a LIST on a single upstream can be useful: if a user is
already connected to Rizon and freenode, sending a LIST will contain
tens of thousands of LIST replies that may not be useful if the user is
interested in another upstream.

This adds support for sending `LIST */network`, which follows the ELIST
M mask extension, that will only send LIST to that specific network. No
other masks are supported by this commit.
2020-05-27 23:43:46 +02:00
delthas 2d47d7067f
Add support for downstream NICK to a single upstream
Users often have different nicks on different upstreams, and we should
support changing the user nick on a single upstream.

This adds support for a new trivial extension, `NICK nick/network`,
which will change the nick on the specified network, and do nothing for
the other networks.
2020-05-27 23:43:04 +02:00
delthas b3742cc731
Update downstream nicks in single-server mode and after NICK
Previously, the downstream nick was never changed, even when the
downstream sent a NICK message or was in single-server mode with a
different nick.

This adds support for updating the downstream nick in the following
cases:
- when a downstream sends NICK
- additionally, in single-server mode:
  - when a downstream connects and its single network is connected
  - when an upstream connects
  - when an upstream sends NICK
2020-05-27 23:42:38 +02:00
delthas c88700ef18
Fix parsing MODE messages by updating channel memberships
Previously, we only considered channel modes in the modes of a MODE
messages, which means channel membership changes were ignored. This
resulted in bugs where users channel memberships would not be properly
updated and cached with wrong values. Further, mode arguments
representing entities were not properly marshaled.

This adds support for correctly parsing and updating channel memberships
when processing MODE messages. Mode arguments corresponding to channel
memberships updates are now also properly marshaled.

MODE messages can't be easily sent from history because marshaling these
messages require knowing about the upstream available channel types and
channel membership types, which is currently only possible when
connected. For now this is not an issue since we do not send MODE
messages in history.
2020-05-21 22:36:54 +02:00
delthas 732b581eb2
Add support for multiple user channel memberships
User channel memberships are actually a set of memberships, not a single
value. This introduces memberships, a type representing a set of
memberships, stored as an array of memberships ordered by descending
rank.

This also adds multi-prefix to the permanent downstream and upstream
capabilities, so that we try to get all possible channel memberships.
2020-05-11 12:25:49 +02:00
Simon Ser 81e4930931
Add time tag to echo messages
Closes: https://todo.sr.ht/~emersion/soju/59
2020-05-05 16:52:50 +02:00
delthas 40ee5e8405 Fix not properly marshaling self in single-server mode
In single-server mode, we don't need to add a /network suffix when
marshaling, but we still need to replace our nick with the downstream
nick.
2020-05-01 21:56:40 +02:00
Simon Ser aa6f3a9954
Don't clear channel key on JOIN
Closes: https://todo.sr.ht/~emersion/soju/50
2020-05-01 17:39:53 +02:00
Simon Ser d9186e994d
Add support for detached channels
Channels can now be detached by leaving them with the reason "detach",
and re-attached by joining them again. Upon detaching the channel is
no longer forwarded to downstream connections. Upon re-attaching the
history buffer is sent.
2020-05-01 15:18:14 +02:00
Simon Ser e7e4311160
Remove network.upstream
This is an artifact from when we used locks. No need for this anymore.
2020-04-30 10:25:16 +02:00
Simon Ser c4655f1492
Add upstreamConn.caps
Instead of adding one field per capability, let's just have a map, just
like downstreamConn.
2020-04-29 19:45:37 +02:00
Simon Ser 0c549d68c4
Add support for away-notify
This makes use of cap-notify to dynamically advertise support for
away-notify. away-notify is advertised to downstream connections if all
upstreams support it.
2020-04-29 19:34:44 +02:00
Simon Ser 394f2853ad
Add downstream support for cap-notify 2020-04-29 19:34:38 +02:00
delthas 19854b7ec7 Unmarshal nicks in texts of PRIVMSG and NOTICE from downstreams
When writing a PRIVMSG or NOTICE on a channel, it is very common to use
autocompletion to mention other users on that channel. When using soju
in multi-network mode, all users will have their nicked suffixed by
`/network`. This suffix should be removed before sending it upstream.

This adds support for removing all `/network` suffixes in messages sent
to a channel of that network.
2020-04-24 18:26:44 +02:00
Simon Ser 9c463b61ec
Fix typo 2020-04-16 17:25:39 +02:00
Simon Ser 3e80573765
Support sending history when upstream is disconnected
Previously, we were dropping the history.
2020-04-16 17:23:35 +02:00
Simon Ser 45e897c1c1
Make downstreamConn.marshal{Entity,UserPrefix} take a network
This will be used when sending history while upstream is disconnected.
2020-04-16 17:19:00 +02:00
Simon Ser 5cf876cb89
Kill downstreamConn.marshal{Nick,Channel}
We can just use downstreamConn.marshalEntity instead.
2020-04-16 16:57:33 +02:00
Simon Ser 96be0b5945
Disallow marshalling for anotehr network 2020-04-16 16:54:13 +02:00
Simon Ser e508f2ca81
Unify downstreamConn.marshal{Entity,Nick,Channel}
We don't actually need to check if the entity is a channel.
2020-04-16 16:33:56 +02:00
Simon Ser 8e852cc7e4
Add support for downstream batch & chathistory 2020-04-15 11:29:15 +02:00
Simon Ser db198335aa
Per-entity ring buffers
Instead of having one ring buffer per network, each network has one ring
buffer per entity (channel or nick). This allows history to be more
fair: if there's a lot of activity in a channel, it won't prune activity
in other channels.

We now track history sequence numbers per client and per network in
networkHistory. The overall list of offline clients is still tracked in
network.offlineClients.

When all clients have received history, the ring buffer can be released.

In the future, we should get rid of too-old offline clients to avoid
having to maintain history for them forever. We should also add a
per-user limit on the number of ring buffers.
2020-04-10 19:22:47 +02:00
Simon Ser 9f6e59195c
Document downstreamConn.{,un}marshalEntity 2020-04-07 22:30:54 +02:00
Simon Ser 3195809c30
Centralize logged messages marshaling
This allows messages added to logs to be handled just like messages
added to the ring buffer.
2020-04-07 19:45:29 +02:00
Simon Ser dd08acc3ea
Make Ring.NewConsumer seq argument mandatory
There's no point in supporting a nil argument anymore.
2020-04-07 14:45:08 +02:00
Simon Ser 9692114f37
Make upstreamConn.produce log messages 2020-04-06 21:42:55 +02:00
Simon Ser baadb964bc
Add origin argument to upstreamConn.produce 2020-04-06 21:34:45 +02:00
Simon Ser 1c17d25731
Remove downstreamConn.ourMessages
We can just do the filtering when dispatching the message.
2020-04-06 19:08:43 +02:00
Simon Ser e1ea0d4fb5
Simplify ring consumer loop
No need to use Peek here.
2020-04-06 18:34:33 +02:00
Simon Ser f0bc919885
Remove downstreamConn.ringConsumers
We no longer need long-lived ring buffer consumers now that
upstreamConn.produce dispatches messages to downstream connections.
2020-04-06 18:31:48 +02:00
Simon Ser 40ff14ec6c
Remove downstreamConn.lock
Everything is now accessed from the user goroutine now that the
per-network ring buffer goroutine is gone.
2020-04-06 18:23:39 +02:00
Simon Ser d541587701
Remove channel from ring buffer consumers
This is unused.
2020-04-06 18:13:46 +02:00
Simon Ser ad2c142c36
Remove per-network ring buffer goroutines
Just dispatch from the user goroutine. This allows removes a lot of complexity.
2020-04-06 18:05:36 +02:00
Simon Ser 0c06142ae9
Introduce upstreamConn.produce
The logic in this function is about to get more complicated. It'll soon
also dispatch messages in connected downstreams.
2020-04-06 17:28:49 +02:00
Simon Ser e70bf9c42a
Add network.{createUpdate,delete}Channel
These are about to get more complicated as we move towards per-entity
ring buffers.
2020-04-05 15:04:52 +02:00
Simon Ser ee8aae7a96
Add NOTICE messages to ring buffer
References: https://todo.sr.ht/~emersion/soju/33
2020-04-03 21:00:19 +02:00
Simon Ser 3d142cae9f
Add time tag to all messages 2020-04-03 20:48:23 +02:00
Simon Ser 27e0906450
Introduce messageLogger
This centralizes formatting related to message logging in a single
place.
2020-04-03 18:59:17 +02:00
Simon Ser 3a89152346
Log self-messages too
Closes: https://todo.sr.ht/~emersion/soju/44
2020-04-03 17:15:12 +02:00
Simon Ser e1931649e4
Use conn.ReadMessage instead of irc.Conn.ReadMessage 2020-04-03 17:01:25 +02:00
Simon Ser 0dc007cd45
Add support for downstream echo-message extension 2020-04-03 16:55:49 +02:00
Simon Ser 2a0696b6bb
Introduce conn for common connection logic
This centralizes the common upstream & downstream bits.
2020-04-03 16:35:08 +02:00
Simon Ser 8c6328207b
Fix writer goroutine races
Any SendMessage call after Close could potentially block forever if the
outgoing channel was filled up. Now the channel is drained before the
writer goroutine exits.
2020-04-03 16:15:25 +02:00
Simon Ser 7c10535bfd
Fix SQL error logged on JOIN
Closes: https://todo.sr.ht/~emersion/soju/40
2020-04-01 17:34:22 +02:00
Simon Ser 29f2e93ab7
Set write deadlines
References: https://todo.sr.ht/~emersion/soju/26
2020-04-01 16:27:53 +02:00
Simon Ser d4de60a869
Simplify ring consumer goroutine
Since network.history is now only accessed from the user goroutine, a
lock becomes unnecessary.
2020-04-01 16:02:31 +02:00
Simon Ser 10da094259
Stop ring consumers when deleting network 2020-04-01 15:48:56 +02:00
Simon Ser 743540665b
Add downstream support for server-time 2020-03-31 19:50:31 +02:00
Simon Ser cbadb64748
Expose message-tags capability downstream
Strip tags if the client doesn't support them.
2020-03-31 19:39:06 +02:00
Simon Ser 73ee7d237f
Make "@" and "/" indicate client name and network, respectively
This allows both kinds "<username>@<client>/<network>" and
"<username>/<network>@<client>".
2020-03-31 19:02:02 +02:00
Simon Ser d748ff269c
Consume ring messages outside of writer goroutine
This fixes out-of-order JOIN and PRIVMSG messages.

Closes: https://todo.sr.ht/~emersion/soju/36
2020-03-31 18:16:54 +02:00
Simon Ser b1fd943ad6
Use clientName to decide whether or not history should be sent
Closes: https://todo.sr.ht/~emersion/soju/31
2020-03-28 17:36:09 +01:00
Simon Ser 6ff26e6640
Remove downstreamConn.username
Replace it with downstreamConn.user.Username
2020-03-28 17:28:28 +01:00
Simon Ser b7e442670d
Add downstreamConn.clientName 2020-03-28 17:25:48 +01:00
Simon Ser 5a8adc93fe
Update password on NickServ SET PASSWORD message 2020-03-28 10:40:33 +01:00
Simon Ser 53d4f9f71b
s/List/LIST/ when referring to the command 2020-03-28 01:03:00 +01:00
Simon Ser 2ae43be525
Document functions safe to call from any goroutine 2020-03-28 00:54:42 +01:00
Simon Ser 1c3da31f2e
Introduce eventUpstreamDisconnected
This allows us to perform cleanup actions in the user goroutine. This
removes the need for pendingLISTsLock.
2020-03-28 00:51:58 +01:00
delthas 10ea698022 Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.

Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.

This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)

When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.

Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.

When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.

Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-28 00:07:20 +01:00
delthas 72a285aaeb Make upstream.SendMessageLabeled use an uint64 id
This commit is preparatory work for code that will call
SendMessageLabeled with a direct downstream id rather than a
downstreamConnection pointer.
2020-03-28 00:07:20 +01:00
Simon Ser 2fff645129
Stop accessing user data in downstreamConn.authenticate
This becomes racy once user.Password is updated on-the-fly.
2020-03-27 22:38:38 +01:00
Simon Ser 08bb06c164
Nuke user.lock
Split user.register into two functions, one to make sure the user is
authenticated, the other to send our current state. This allows to get
rid of data races by doing the second part in the user goroutine.

Closes: https://todo.sr.ht/~emersion/soju/22
2020-03-27 19:17:58 +01:00
Simon Ser c0f5850e5b
Add eventDownstreamDisconnected
This should remove the need for protecting user.downstreamConns with a
mutex.
2020-03-27 17:55:03 +01:00
Simon Ser 36ab6ece09
Add eventDownstreamConnected
In a later commit, we'll be able to move part of downstreamConn.register
into the user goroutine to prevent races.

References: https://todo.sr.ht/~emersion/soju/22
2020-03-27 17:21:05 +01:00
Simon Ser 474f2889d9
Introduce a user.events channel
This allows to easily add new events, and also guarantees ordering
between different event types.
2020-03-27 16:33:19 +01:00
delthas 764b54da59
Add downstream NOTICE support 2020-03-27 09:40:51 +01:00
delthas 9530df5db0
Add downstream INVITE support 2020-03-26 22:43:48 +01:00
delthas 3b6e175365
Add upstream RPL_CREATIONTIME support 2020-03-26 22:33:44 +01:00
delthas a018f35c42
Add downstream TOPIC support 2020-03-26 22:27:46 +01:00
delthas bab26c7a6f
Add KICK support
Downstream and upstream message handling are slightly different because
downstreams can send KICK messages with multiple channels or users,
while upstreams can only send KICK messages with one channel and one
user (according to the RFC).
2020-03-26 00:15:26 +01:00
delthas 9b7309d4c9
Simplify error handling for downstream JOIN/PART
downstreamConnection.unmarshalEntity already returns an ircError of
command ERR_NOSUCHCHANNEL, so there's no need to explicitly return
another ircError of that type.
2020-03-26 00:12:53 +01:00
delthas 6e72071716
Route NAMES, WHO, WHOIS replies to the requesting downstream
Using labeled-response, the replies to several commands such as NAMES,
WHO, WHOIS can be routed back to a specific downstream, rather than
being broadcast to all downstreams.

For example, after this commit, if the server supports labeled-response,
if a downstream requests the NAMES or WHO or WHOIS of a channel, the
replies of the upstream will only be sent back to that downstream, and
the other downstreams won't receive these messages.
2020-03-25 23:21:14 +01:00
delthas d0917f0fa1
Add a server-unique id to each downstream
Adding a simple uint64 id to each downstream is preparatory work
for labeled-responses tags targeting a specific downstream.
2020-03-25 23:17:46 +01:00
Simon Ser 33dacc4fb0
Add support for channel keys 2020-03-25 11:54:08 +01:00
Simon Ser 146906ef6b
Handle downstream JOIN/PART with multiple channel names
Closes: https://todo.sr.ht/~emersion/soju/19
2020-03-25 11:32:44 +01:00
Simon Ser 21241c2009
Consume ring buffer for networks added on-the-fly 2020-03-25 11:28:25 +01:00
Simon Ser 293a0e8e20
Move upstreamConn.ring to network
This handles upstream disconnection and re-connection better.
2020-03-25 10:53:08 +01:00
delthas 9486d657c5
Add downstream self WHO and WHOIS support 2020-03-25 09:51:22 +01:00
delthas a958a01446
Add user hostname to downstream user prefix 2020-03-25 09:48:00 +01:00
delthas 9ff8429a53
Add downstream NAMES support
NAMES reply for channels currently joined will be returned from cache;
requests for channels not joined will be forwarded from upstream.
2020-03-25 09:47:26 +01:00
delthas 98a95e9955
Add MODE arguments support
- Add RPL_ISUPPORT support with CHANMODES, CHANTYPES, PREFIX parsing
- Add support for channel mode state with mode arguments
- Add upstream support for RPL_UMODEIS, RPL_CHANNELMODEIS
- Request channel MODE on upstream channel JOIN
- Use sane default channel mode and channel mode types
2020-03-25 09:40:08 +01:00
Simon Ser 015281ed35
Add user.{add,remove}Downstream 2020-03-23 16:05:00 +01:00
Simon Ser c511a3c895
Add network.upstream helper 2020-03-21 10:24:38 +01:00
Simon Ser 4b9168196a
Fix typo 2020-03-21 08:44:03 +01:00
Simon Ser ec98c6f04f
Fix empty SASL challenge message
We were sending an empty string. We need to send "+" instead.
2020-03-21 08:41:14 +01:00
Simon Ser 7114c8a1e9
Avoid sending JOIN twice for the same channel 2020-03-20 22:53:05 +01:00
Simon Ser 45d118dd12
Move upstreamConn.history to network 2020-03-20 22:48:17 +01:00
Simon Ser 7714c84669
Fix deadlocks in single-upstream mode 2020-03-20 10:42:17 +01:00
delthas aedf66c73d Fix MODE downstream support
- Fix replies without client as first argument
- Replace wrong prefix check with a proper entity type check
2020-03-20 10:11:27 +01:00
delthas b3ad960529 Add WHOIS support 2020-03-20 10:11:27 +01:00
delthas 54275c25ac Add WHO support 2020-03-20 00:29:03 +01:00
Simon Ser 166d8b0626
Remove some IRCv3 constants
go-irc v3.1.2 adds some missing IRCv3 constants.
2020-03-19 14:51:45 +01:00
Simon Ser 7dc2d4d9b1
Fix history name
dc.username contains the raw account username, not the username supplied
by the client.
2020-03-19 14:44:49 +01:00
delthas 37eb162b75 Add create-network bouncer service command 2020-03-19 01:14:44 +01:00
delthas 37e56a01b2 Add suffixing for multi-upstream connections 2020-03-19 00:06:48 +01:00
Simon Ser e3d97bb164
Add basic infrastructure for bouncer service 2020-03-18 12:23:08 +01:00
Simon Ser 0db032d267
Fix typo 2020-03-17 16:17:39 +01:00
Simon Ser dc26908466
Fix echo PRIVMSG messages 2020-03-17 16:15:54 +01:00
Simon Ser 651e936913
Add downstream SASL support 2020-03-16 16:16:27 +01:00