From bac0f5b6711e34d55f28fdaa478cacfa615c61ac Mon Sep 17 00:00:00 2001 From: Rebecca Kelly Date: Sat, 3 Feb 2024 16:04:59 -0500 Subject: [PATCH] Set LINELEN to 8k for clients that support long messages This matches the value already negotiated for cap multiline. I think we could go up to 16k without difficulty, but 8k is probably sufficient. This also tells gen_tcp to use a 16k buffer; with the default settings, long commands sometimes get split across multiple reads, with unfortunate results. --- lib/irc/handler.ex | 2 ++ lib/irc_server.ex | 9 ++++++++- test/irc/handler_test.exs | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/irc/handler.ex b/lib/irc/handler.ex index ca99223..87fec72 100644 --- a/lib/irc/handler.ex +++ b/lib/irc/handler.ex @@ -607,6 +607,8 @@ defmodule M51.IrcConn.Handler do "CHANMODES=b,,,i", "CHANTYPES=#!", "CHATHISTORY=100", + # Matrix limit is 64k for the whole event, so this is fairly conservative. + "LINELEN=#{@multiline_max_bytes}", "MAXTARGETS=1", # https://github.com/ircv3/ircv3-specifications/pull/510 "MSGREFTYPES=msgid", diff --git a/lib/irc_server.ex b/lib/irc_server.ex index 948c417..4269707 100644 --- a/lib/irc_server.ex +++ b/lib/irc_server.ex @@ -40,7 +40,14 @@ defmodule M51.IrcServer do end defp accept(port, retries_left \\ 10) do - case :gen_tcp.listen(port, [:binary, :inet6, packet: :line, active: false, reuseaddr: true]) do + opts = [ + :binary, :inet6, + packet: :line, + active: false, + reuseaddr: true, + buffer: M51.IrcConn.Handler.multiline_max_bytes * 2 + ] + case :gen_tcp.listen(port, opts) do {:ok, server_sock} -> Logger.info("Listening on port #{port}") loop_accept(server_sock) diff --git a/test/irc/handler_test.exs b/test/irc/handler_test.exs index 113ff46..3c35eb7 100644 --- a/test/irc/handler_test.exs +++ b/test/irc/handler_test.exs @@ -20,7 +20,7 @@ defmodule M51.IrcConn.HandlerTest do @cap_ls_302 ":server. CAP * LS :account-tag batch draft/account-registration=before-connect draft/channel-rename draft/chathistory draft/message-redaction draft/multiline=max-bytes=8192 draft/no-implicit-names draft/sasl-ir echo-message extended-join labeled-response message-tags sasl=PLAIN server-time soju.im/account-required standard-replies userhost-in-names\r\n" @cap_ls ":server. CAP * LS :account-tag batch draft/account-registration draft/channel-rename draft/chathistory draft/message-redaction draft/multiline draft/no-implicit-names draft/sasl-ir echo-message extended-join labeled-response message-tags sasl server-time soju.im/account-required standard-replies userhost-in-names\r\n" - @isupport "CASEMAPPING=rfc3454 CLIENTTAGDENY=*,-draft/react,-draft/reply CHANLIMIT= CHANMODES=b,,,i CHANTYPES=#! CHATHISTORY=100 MAXTARGETS=1 MSGREFTYPES=msgid PREFIX= TARGMAX=JOIN:1,PART:1 UTF8ONLY :are supported by this server\r\n" + @isupport "CASEMAPPING=rfc3454 CLIENTTAGDENY=*,-draft/react,-draft/reply CHANLIMIT= CHANMODES=b,,,i CHANTYPES=#! CHATHISTORY=100 LINELEN=8192 MAXTARGETS=1 MSGREFTYPES=msgid PREFIX= TARGMAX=JOIN:1,PART:1 UTF8ONLY :are supported by this server\r\n" setup do start_supervised!({MockMatrixClient, {self()}})