Commit graph

188 commits

Author SHA1 Message Date
Simon Ser 42b8dac44e Add user.notifyBouncerNetworkState
DRY.
2022-04-15 10:41:38 +02:00
Simon Ser f3932ab500 Drop user.forEachDownstream
It's just a for loop.
2022-04-15 10:32:28 +02:00
Simon Ser a6636a9b0d Add TODO for upstreamConn.register race 2022-04-15 09:53:30 +02:00
Simon Ser 9fb789ee26 Add timeout for upstream connection registration 2022-04-15 09:49:19 +02:00
Simon Ser 77460dfb97 Split upstream connection handling to separate function
Just a refactoring, no functional change.
2022-04-15 09:37:43 +02:00
delthas f7a468194d bouncer-networks: Add a read-only error attribute
This is useful for clients to display additional info abotu why a
network is disconnected.
2022-04-11 08:38:31 +02:00
Simon Ser a45f9c3274 Use SETNAME when updating user-wide realname 2022-03-30 15:11:25 +02:00
Simon Ser 78d9a84a6f Add support for RPL_VISIBLEHOST 2022-03-21 16:09:45 +01:00
delthas f5b16dc00c Send any welcome error messages to the downstream
Currently, if we fail connecting to a new networking during welcome, we
send no error message to the client, and the connection remains open in
an undefined state.

Given the input:

  NICK nick
  USER user/invalid.xyz s e r
  PASS pass

soju will fail to connect, add a message to its own logs, but will
return no message to the downstream.

This fixes the issue by forwarding the error message if it is an IRC
error message (which it is for connecting to new networks).

We should probably also close the connection after the message is
written, because it leaves the connection in an undefined state. This is
TODO for now because we'd have to wait for the error message to be
written out first, which is non-trivial.
2022-03-21 09:49:37 +01:00
Simon Ser 74fd506fef Use capRegistry for downstreamConn 2022-03-14 19:25:49 +01:00
Simon Ser dc58a70794 msgstore_fs: fix direct message targets
When fetching messages via draft/chathistory from a conversation
with another user, soju would send the following:

    :sender PRIVMSG sender :hey

instead of

    :sender PRIVMSG recipient :hey

because the file-system message store format doesn't contain the
original PRIVMSG target.

Fix this by doing some guesswork.
2022-02-25 21:05:10 +01:00
delthas cae248f672 Add support for the wip soju.im/read capability and READ command
READ lets downstream clients share information between each other about
what messages have been read by other downstreams.

Each target/entity has an optional corresponding read receipt, which is
stored as a timestamp.

- When a downstream sends:
  READ #chan timestamp=2020-01-01T01:23:45.000Z
  the read receipt for that target is set to that date
- soju sends READ to downstreams:
  - on JOIN, if the client uses the soju.im/read capability
  - when the read receipt timestamp is set by any downstream

The read receipt date is clamped by the previous receipt date and the
current time.
2022-02-11 19:41:46 +01:00
Simon Ser e72c896bb4 Add context to upstreamConn.register 2022-02-08 16:38:34 +01:00
Simon Ser 375ac53f74 Add context to upstreamConn.runUntilRegistered 2022-02-08 16:37:31 +01:00
Simon Ser 02dac11308 Forbid empty and flag-looking network names 2022-02-07 21:39:39 +01:00
Simon Ser 49b77d630a Ensure consistent network ordering
Right now there is no consistent ordering in the network list:
no ORDER BY in the DB, and network updates move entries to the end.

Let's always sort by network ID so that users don't see the entries
move around.

I've contemplated sorting by Network.GetName() instead, but:

- Clients have now way to figure out dynamic order changes, e.g.
  when renaming a network.
- Some clients might use ISUPPORT NETWORK when a user hasn't
  explicitly named a network, but soju won't use that for ordering,
  leading to non-alphabetic ordering in the client.

Let's leave it to clients to sort the networks by display name if
they want to.
2022-02-04 15:03:13 +01:00
Simon Ser 0b5da29916 Drop user.forEachNetwork
It's a trivial for loop.
2022-02-04 14:01:27 +01:00
Simon Ser b6c47a517c Use more descriptive errors when aborting pending commands 2021-12-06 22:33:50 +01:00
Simon Ser f75ee272a4 Add context to network.storeClientDeliveryReceipts 2021-12-02 23:32:12 +01:00
Simon Ser 04b0c2fd6b Add context to upstreamConn.handleMessage 2021-12-02 23:27:12 +01:00
Simon Ser a413681253 Cancel pending commands on downstream disconnect
If a client queues a high number of commands and then disconnects,
remove all of the pending commands. This avoids unnecessarily
sending commands whose results won't be used.
2021-12-02 19:29:44 +01:00
Simon Ser fd9a935f3e Don't retry connecting on permanent failure
Closes: https://todo.sr.ht/~emersion/soju/164
2021-12-02 17:33:11 +01:00
Simon Ser 578020e553 Add exponential backoff when re-connecting to upstream
The first reconnection attempt waits for 1min, the second the 2min,
and so on up to 10min. There's a 1min jitter so that multiple failed
connections don't try to reconnect at the exact same time.

Closes: https://todo.sr.ht/~emersion/soju/161
2021-12-02 12:14:35 +01:00
Simon Ser 8acc07d591 Add soju_upstream_connect_errors_total metric 2021-12-02 11:15:51 +01:00
Simon Ser d11216917b Fix upstream gauge metric
It wasn't decremented on error. While at it, increment it when
connecting to the upstream network.
2021-12-02 11:03:56 +01:00
Simon Ser 73287f242e Add context to connectToUpstream 2021-12-02 10:53:43 +01:00
Simon Ser 33a639ecf0 Validate address in user.checkNetwork 2021-12-01 15:57:54 +01:00
Simon Ser 313c6e7f97 Add support for post-connection-registration upstream SASL auth
Once the downstream connection has logged in with their bouncer
credentials, allow them to issue more SASL auths which will be
redirected to the upstream network. This allows downstream clients
to provide UIs to login to transparently login to upstream networks.
2021-11-21 16:10:54 +01:00
Simon Ser c2a5461cb8 Use RPL_LOGGEDIN/OUT to mirror upstream status
This will allow clients to properly show/hide UI to login and
register.
2021-11-19 19:21:48 +01:00
Simon Ser efc4c62725 Add number of upstream connections to metrics 2021-11-17 15:58:19 +01:00
Simon Ser 55840312b4 Add per-user IP addresses
The new upstream-user-ip directive allows bouncer operators to
assign one IP address per user.
2021-11-17 15:07:58 +01:00
Simon Ser 97152191ad Add timeout for downstream connection registration 2021-11-17 14:54:03 +01:00
Simon Ser e28332a5aa Add context arg to downstreamConn.welcome() 2021-11-17 12:33:30 +01:00
Simon Ser 61e6b2efa4 Set isMultiUpstream flag in downstreamConn.welcome()
This will make it easier to globally disable multi-upstream mode.
2021-11-17 11:22:26 +01:00
Simon Ser ff44ea314e Add downstreamConn.isMultiUpstream 2021-11-17 11:17:09 +01:00
Simon Ser 73295e4fa7 Allow most config options to be reloaded
Closes: https://todo.sr.ht/~emersion/soju/42
2021-11-16 00:38:04 +01:00
Simon Ser 5d46dd72a4 Add support for MONITOR
Add support for MONITOR in single-upstream mode.

Each downstream has its own set of monitored targets. These sets
are merged together to compute the MONITOR commands to send to
upstream.

Each upstream has a set of monitored targets accepted by the server
alongside with their status (online/offline). This is used to
directly send replies to downstreams adding a target another
downstream has already added, and send MONITOR S[TATUS] replies.

Co-authored-by: delthas <delthas@dille.cc>
2021-11-15 14:34:04 +01:00
Simon Ser 0b6ff2e61a Add a queue for WHO commands
This has the following upsides:

- We can now routes WHO replies to the correct client, without
  broadcasting them to everybody.
- We are less likely to hit server rate limits when multiple downstreams
  are issuing WHO commands at the same time.
2021-11-09 22:09:17 +01:00
Simon Ser 0c360d24c5 Remove support for mixed multi-upstream LIST
Multi-upstream connections can still send LIST commands with a
network suffix.
2021-11-09 21:32:26 +01:00
Simon Ser c21202160c Add context support to user and network mutations
References: https://todo.sr.ht/~emersion/soju/141
2021-11-08 19:36:10 +01:00
Simon Ser 2b4f0a870f msgstore: take Network as arg instead of network
The message stores don't need to access the internal network
struct, they just need network metadata such as ID and name.

This can ease moving message stores into a separate package in the
future.
2021-11-03 16:37:01 +01:00
Simon Ser 9ec1f1a5b0 Add context args to Database interface
This is a mecanical change, which just lifts up the context.TODO()
calls from inside the DB implementations to the callers.

Future work involves properly wiring up the contexts when it makes
sense.
2021-10-18 19:15:15 +02:00
Simon Ser 2ce97bcc12 msgstore_fs: rename log dir when network is renamed 2021-10-15 18:11:04 +02:00
Simon Ser 4e9ddf78ab service: allow updating other users 2021-10-12 09:11:14 +02:00
Simon Ser 94dbfff11d Add max-user-networks config option 2021-10-07 20:43:10 +02:00
Simon Ser 79b0fe5de5 Don't send network notification when removed
Closes: https://todo.sr.ht/~emersion/soju/123
2021-09-13 10:33:46 +02:00
Simon Ser acde97ca37 Introduce user.updateUser
Unify updatePassword and updateRealname into a single function. This
allows "user update" to be atomic.
2021-06-28 18:05:03 +02:00
Simon Ser a14f646135 Add per-user realname setting
This allows users to set a default realname used if the per-network
realname isn't set.

A new "user update" command is introduced and can be extended to edit
other user properties and other users in the future.
2021-06-25 20:33:13 +02:00
Simon Ser d7b1c5a9a2 Allow admins to broadcast message to all bouncer users
Typically done via:

    /notice $<bouncer> <message>

Or, for a connection not bound to a specific network:

    /notice $* <message>

The message is broadcast as BouncerServ, because that's the only
user that can be trusted to belong to the bouncer by users. Any
other prefix would conflict with the upstream network.
2021-06-23 19:23:09 +02:00
Simon Ser 4b6a529b69 Fix typo in BOUNCER notifications: s/status/state/ 2021-05-26 11:08:10 +02:00
Simon Ser b0349caada Broadcast BOUNCER notifications to all downstream connections
We weren't sending them to downstreams which weren't bound to a
network.
2021-05-26 11:04:33 +02:00
Simon Ser 7d648f702e Allow networks to be disabled 2021-05-26 10:51:02 +02:00
Simon Ser 47c514a9cf Add support for IRCv3 setname
References: https://todo.sr.ht/~emersion/soju/41
2021-05-25 20:24:45 +02:00
Simon Ser f37a47cc4e Don't send connection status NOTICEs with bouncer-networks 2021-05-25 16:42:51 +02:00
Simon Ser 31f2d28508 Introduce the soju.im/bouncer-networks-notify capability 2021-05-25 16:42:51 +02:00
Simon Ser db0f745193 Implement the soju.im/bouncer-networks extension 2021-05-25 16:42:51 +02:00
Simon Ser fb4a01b023 Add per-network logger 2021-04-13 20:12:49 +02:00
Simon Ser be2825595d Error out on network name conflict
Closes: https://todo.sr.ht/~emersion/soju/29
2021-04-13 19:33:06 +02:00
Simon Ser a2c207d357 Relay detached channel backlog as BouncerServ NOTICE if necessary
Instead of ignoring detached channels wehn replaying backlog,
process them as usual and relay messages as BouncerServ NOTICEs
if necessary. Advance the delivery receipts as if the channel was
attached.

Closes: https://todo.sr.ht/~emersion/soju/98
2021-04-13 19:11:05 +02:00
Simon Ser 424f676254 Store last internal msg ID in DB when detaching
References: https://todo.sr.ht/~emersion/soju/98
2021-04-13 18:15:30 +02:00
Simon Ser 65c58adbd9 Take msg ID in sendTargetBacklog 2021-04-13 17:49:37 +02:00
Simon Ser ef902fdc28 Panic on unknown user event type
This should never happen. Complain loudly if it does.
2021-04-13 13:08:48 +02:00
Simon Ser adb4b300dc Introduce per-user logger
Adds the username to log lines.
2021-04-13 13:04:23 +02:00
Simon Ser 1e4ff49472 Save delivery receipts in DB
This avoids loosing history on restart for clients that don't
support chathistory.

Closes: https://todo.sr.ht/~emersion/soju/80
2021-03-31 18:04:13 +02:00
Simon Ser 5e11e717f1 Rename user.clients to clientNames
This doesn't contain anything other than just the names. Make this
clearer.
2021-03-30 12:44:56 +02:00
Simon Ser 3237bde9f3 Introduce deliveredStore
This hides the double-map complexity behind a dedicated type.
2021-03-29 17:49:50 +02:00
Simon Ser 55274d7213 Move network.clients to user
No need to have this list per-network.
2021-03-29 16:58:56 +02:00
Simon Ser 5a899abaab Simplify network.offlineClients
Replace it with a list of all clients (online or offline).
2021-03-29 16:55:57 +02:00
Simon Ser 6e5a307dc7 Introduce deliveredClientMap
Adds more semantics to map[string]string. Simplifies the complicated
mapStringStringCasemapMap type.
2021-03-26 11:21:14 +01:00
Hubert Hirtz bdd0c7bc06
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-24 18:15:52 +01:00
Simon Ser dab91736db Send NOTICE to downstream when upstream is disconnected
Closes: https://todo.sr.ht/~emersion/soju/76
2021-03-16 09:41:07 +01:00
Simon Ser 31cd56875a Use sendTargetBacklog when re-attaching a channel
No need to attempt to send backlog for all targets in the network.
We're only interested in a single channel.
2021-02-10 13:50:10 +01:00
Simon Ser 7e39f6d663 Rename network.history to network.delivered
"History" is over-loaded with e.g. CHATHISTORY support.
2021-02-10 11:31:34 +01:00
Simon Ser c14118f7f9 Rename sendNetworkHistory to sendNetworkBacklog
"History" is a little bit over-loaded with CHATHISTORY support.
2021-02-10 10:23:51 +01:00
Simon Ser 1110f39227
Add in-memory message store
Uses an in-memory ring buffer.

Closes: https://todo.sr.ht/~emersion/soju/96
2021-01-04 17:18:30 +01:00
Simon Ser 021a4c9474
Turn messageStore into an interface
This allows for other implementations that aren't based on a filesystem.
2021-01-04 14:24:00 +01:00
Hubert Hirtz 7bfa4dafef
Advertise all caps, CAP DEL them on registration
... so that the JOIN/history batch takes into account all capabilities.
Without this commit for example, enabling multi-prefix after the batch
makes the client send NAMES requests for all channels, which generate
needless traffic.
2020-12-25 13:35:20 +01:00
delthas a76b22bf29 Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.

The `FilterDefault` values of the messages filters are currently
hardcoded.

The values of the message filters are not currently user-settable.

This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-12-14 20:54:02 +01:00
Simon Ser 05aafb5edf
Add message store abstraction
Introduce a messageStore type, which will allow for multiple
implementations (e.g. in the DB or in-memory instead of on-disk).

The message store is per-user so that we don't need to deal with locking
and it's easier to implement per-user limits.
2020-10-25 17:47:38 +01:00
Simon Ser fa16337d97
Switch DB API to user IDs
This commit changes the Network schema to use user IDs instead of
usernames. While at it, a new UNIQUE(user, name) constraint ensures
there is no conflict with custom network names.

Closes: https://todo.sr.ht/~emersion/soju/86
References: https://todo.sr.ht/~emersion/soju/29
2020-10-24 15:14:23 +02:00
Hubert Hirtz 7310f67fc7
Make DB.DeleteChannel take the channel ID
... to allow the caller to correctly do any necessary casemapping.
2020-09-07 11:40:18 +02:00
Simon Ser 92fece5cd4
Nuke in-memory ring buffer
Instead, always read chat history from logs. Unify the implicit chat
history (pushing history to clients) and explicit chat history
(via the CHATHISTORY command).

Instead of keeping track of ring buffer cursors for each client, use
message IDs.

If necessary, the ring buffer could be re-introduced behind a
common MessageStore interface (could be useful when on-disk logs are
disabled).

References: https://todo.sr.ht/~emersion/soju/80
2020-08-20 20:05:01 +02:00
Simon Ser 4dae0da59f
Replace networkHistory.offlineClients with clients
Keep the ring buffer alive even if all clients are connected. Keep the
ID of the latest delivered message even for online clients.

As-is, this is a net downgrade: memory usage increases because ring
buffers aren't free'd anymore. However upcoming commits will replace the
ring buffer with log files. This change makes reading from log files
easier.
2020-08-20 17:38:57 +02:00
Simon Ser 385825d010
Improve registration error messages
- Don't print the raw IRC message, since we already show the original
  error message
- Avoid double-printing "registration failed"
2020-08-19 23:35:12 +02:00
Simon Ser bdb132ad98
Implement rate limiting for upstream messages
Allow up to 10 outgoing messages in a burst, then throttle to 1 message
each 2 seconds.

Closes: https://todo.sr.ht/~emersion/soju/87
2020-08-19 19:42:33 +02:00
Simon Ser 78361f0b1e
Encode idents to hex instead of base64
base64 strings may contain characters rejected by ident clients such
as "+". Use hex encoding and shorten the string a little.
2020-08-19 11:24:25 +02:00
Simon Ser 65302d3c1e
Add an ident server
Closes: https://todo.sr.ht/~emersion/soju/69
2020-08-11 10:59:06 +02:00
Simon Ser 6baa15ba14
Make user.stop block
This allows callers to wait until the user has been stopped.
2020-08-10 15:03:38 +02:00
Simon Ser ef01142a44
Add user.stop 2020-08-10 15:03:38 +02:00
Simon Ser 25d4312e0f
Prevent error handler from falling through in user.run 2020-07-09 14:20:23 +02:00
Simon Ser 998546cdc3
Introduce User.Created
For Network and Channel, the database only needed to define one Store
operation to create/update a record. However since User is missing an ID
we couldn't have a single StoreUser function like other types. We had
CreateUser and UpdatePassword. As new User fields get added (e.g. the
upcoming Admin flag) this isn't sustainable.

We could have CreateUser and UpdateUser, but this wouldn't be consistent
with other types. Instead, introduce User.Created which indicates
whether the record is already stored in the DB. This can be used in a
new StoreUser function to decide whether we need to UPDATE or INSERT
without relying on SQL constraints and INSERT OR UPDATE.

The ListUsers and GetUser functions set User.Created to true.
2020-06-08 11:59:03 +02:00
Simon Ser c709ebfc91
Add network update command
The user.updateNetwork function is a bit involved because we need to
make sure that the upstream connection is closed before re-connecting
(would otherwise cause "Nick already used" errors) and that the
downstream connections' state is kept in sync.

References: https://todo.sr.ht/~emersion/soju/17
2020-06-04 13:04:39 +02:00
Simon Ser cb99e97f5f
Introduce network.isClosed 2020-06-03 17:28:31 +02:00
Simon Ser 9f40925199
Unexport network.Stop
An exported function makes it look like it can be called from any
goroutine.
2020-06-03 16:18:56 +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
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
delthas a99d6dd019 Fix joining only one saved channel per network
This fixes a serious bug added in 276ce12e, where in newNetwork all
channels point to the same channel, which causes soju to only join a
single channel when connecting to an upstream network.

This also adds the same kind of reassignment of a for loop variable in
user.run(), even though that function currently works correctly, as a
sanity improvement in case this function is changed in the future.
2020-05-01 10:49:58 +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 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 276ce12e7c
Add network.channels, remove DB.GetChannel
Store the list of configured channels in the network data structure.
This removes the need for a database lookup and will be useful for
detached channels.
2020-04-23 16:36:20 +02:00