From 875282e9ecb67f0a2e8999e6142f18de10e686a0 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Tue, 6 Jun 2023 14:17:14 +0400 Subject: [PATCH] core: fix parsing multiple servers passed as cli argument (#2549) --- src/Simplex/Chat.hs | 6 ++--- src/Simplex/Chat/Options.hs | 16 +++++++++--- tests/ChatTests/Direct.hs | 50 +++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 5e293d828..8c10d4922 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -355,9 +355,9 @@ processChatCommand = \case asks currentUser >>= readTVarIO >>= \case Nothing -> throwChatError CENoActiveUser Just user -> do - smpServers <- withStore' (`getProtocolServers` user) + servers <- withStore' (`getProtocolServers` user) cfg <- asks config - pure (activeAgentServers cfg protocol smpServers, smpServers) + pure (activeAgentServers cfg protocol servers, servers) | otherwise = do defServers <- asks $ defaultServers . config pure (cfgServers protocol defServers, []) @@ -4989,7 +4989,7 @@ chatCommandP = onOffP = ("on" $> True) <|> ("off" $> False) profileNames = (,) <$> displayName <*> fullNameP newUserP = do - sameServers <- "same_smp=" *> onOffP <* A.space <|> pure False + sameServers <- "same_servers=" *> onOffP <* A.space <|> pure False (cName, fullName) <- profileNames let profile = Just Profile {displayName = cName, fullName, image = Nothing, contactLink = Nothing, preferences = Nothing} pure NewUser {profile, sameServers, pastTimestamp = False} diff --git a/src/Simplex/Chat/Options.hs b/src/Simplex/Chat/Options.hs index 57b653ca9..e16beb9d4 100644 --- a/src/Simplex/Chat/Options.hs +++ b/src/Simplex/Chat/Options.hs @@ -25,7 +25,7 @@ import Simplex.Chat.Controller (ChatLogLevel (..), updateStr, versionNumber, ver import Simplex.Messaging.Client (NetworkConfig (..), defaultNetworkConfig) import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers (parseAll) -import Simplex.Messaging.Protocol (ProtocolTypeI, ProtoServerWithAuth, SMPServerWithAuth, XFTPServerWithAuth) +import Simplex.Messaging.Protocol (ProtoServerWithAuth, ProtocolTypeI, SMPServerWithAuth, XFTPServerWithAuth) import Simplex.Messaging.Transport.Client (SocksProxy, defaultSocksProxy) import System.FilePath (combine) @@ -88,7 +88,11 @@ coreChatOptsP appDir defaultDbFileName = do ( long "server" <> short 's' <> metavar "SERVER" - <> help "Semicolon-separated list of SMP server(s) to use (each server can have more than one hostname)" + <> help + ( ("Space-separated list of SMP server(s) to use (each server can have more than one hostname)." <> "\n") + <> ("If you pass multiple servers, surround the entire list in quotes." <> "\n") + <> "Examples: smp1.example.com, \"smp1.example.com smp2.example.com smp3.example.com\"" + ) <> value [] ) xftpServers <- @@ -96,7 +100,11 @@ coreChatOptsP appDir defaultDbFileName = do parseProtocolServers ( long "xftp-server" <> metavar "SERVER" - <> help "Semicolon-separated list of XFTP server(s) to use (each server can have more than one hostname)" + <> help + ( ("Space-separated list of XFTP server(s) to use (each server can have more than one hostname)." <> "\n") + <> ("If you pass multiple servers, surround the entire list in quotes." <> "\n") + <> "Examples: xftp1.example.com, \"xftp1.example.com xftp2.example.com xftp3.example.com\"" + ) <> value [] ) socksProxy <- @@ -270,7 +278,7 @@ serverPortP :: A.Parser (Maybe String) serverPortP = Just . B.unpack <$> A.takeWhile A.isDigit protocolServersP :: ProtocolTypeI p => A.Parser [ProtoServerWithAuth p] -protocolServersP = strP `A.sepBy1` A.char ';' +protocolServersP = strP `A.sepBy1` A.char ' ' parseLogLevel :: ReadM ChatLogLevel parseLogLevel = eitherReader $ \case diff --git a/tests/ChatTests/Direct.hs b/tests/ChatTests/Direct.hs index b1751bafe..63ad8ca59 100644 --- a/tests/ChatTests/Direct.hs +++ b/tests/ChatTests/Direct.hs @@ -521,8 +521,9 @@ testGetSetSMPServers = alice #$> ("/smp", id, "smp://1234-w==@smp1.example.im") alice #$> ("/smp smp://1234-w==:password@smp1.example.im", id, "ok") alice #$> ("/smp", id, "smp://1234-w==:password@smp1.example.im") - alice #$> ("/smp smp://2345-w==@smp2.example.im;smp://3456-w==@smp3.example.im:5224", id, "ok") - alice #$> ("/smp", id, "smp://2345-w==@smp2.example.im") + alice #$> ("/smp smp://2345-w==@smp2.example.im smp://3456-w==@smp3.example.im:5224", id, "ok") + alice ##> "/smp" + alice <## "smp://2345-w==@smp2.example.im" alice <## "smp://3456-w==@smp3.example.im:5224" alice #$> ("/smp default", id, "ok") alice #$> ("/smp", id, "smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7001") @@ -551,8 +552,9 @@ testGetSetXFTPServers = alice #$> ("/xftp", id, "xftp://1234-w==@xftp1.example.im") alice #$> ("/xftp xftp://1234-w==:password@xftp1.example.im", id, "ok") alice #$> ("/xftp", id, "xftp://1234-w==:password@xftp1.example.im") - alice #$> ("/xftp xftp://2345-w==@xftp2.example.im;xftp://3456-w==@xftp3.example.im:5224", id, "ok") - alice #$> ("/xftp", id, "xftp://2345-w==@xftp2.example.im") + alice #$> ("/xftp xftp://2345-w==@xftp2.example.im xftp://3456-w==@xftp3.example.im:5224", id, "ok") + alice ##> "/xftp" + alice <## "xftp://2345-w==@xftp2.example.im" alice <## "xftp://3456-w==@xftp3.example.im:5224" alice #$> ("/xftp default", id, "ok") alice #$> ("/xftp", id, "xftp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7002") @@ -1135,39 +1137,55 @@ testCreateUserDefaultServers :: HasCallStack => FilePath -> IO () testCreateUserDefaultServers = testChat2 aliceProfile bobProfile $ \alice _ -> do - alice #$> ("/smp smp://2345-w==@smp2.example.im;smp://3456-w==@smp3.example.im:5224", id, "ok") - alice #$> ("/smp", id, "smp://2345-w==@smp2.example.im") - alice <## "smp://3456-w==@smp3.example.im:5224" + alice #$> ("/smp smp://2345-w==@smp2.example.im smp://3456-w==@smp3.example.im:5224", id, "ok") + alice #$> ("/xftp xftp://2345-w==@xftp2.example.im xftp://3456-w==@xftp3.example.im:5224", id, "ok") + checkCustomServers alice alice ##> "/create user alisa" showActiveUser alice "alisa" alice #$> ("/smp", id, "smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7001") + alice #$> ("/xftp", id, "xftp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7002") - -- with same_smp=off + -- with same_servers=off alice ##> "/user alice" showActiveUser alice "alice (Alice)" - alice #$> ("/smp", id, "smp://2345-w==@smp2.example.im") - alice <## "smp://3456-w==@smp3.example.im:5224" + checkCustomServers alice - alice ##> "/create user same_smp=off alisa2" + alice ##> "/create user same_servers=off alisa2" showActiveUser alice "alisa2" alice #$> ("/smp", id, "smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7001") + alice #$> ("/xftp", id, "xftp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:7002") + where + checkCustomServers alice = do + alice ##> "/smp" + alice <## "smp://2345-w==@smp2.example.im" + alice <## "smp://3456-w==@smp3.example.im:5224" + alice ##> "/xftp" + alice <## "xftp://2345-w==@xftp2.example.im" + alice <## "xftp://3456-w==@xftp3.example.im:5224" testCreateUserSameServers :: HasCallStack => FilePath -> IO () testCreateUserSameServers = testChat2 aliceProfile bobProfile $ \alice _ -> do - alice #$> ("/smp smp://2345-w==@smp2.example.im;smp://3456-w==@smp3.example.im:5224", id, "ok") - alice #$> ("/smp", id, "smp://2345-w==@smp2.example.im") - alice <## "smp://3456-w==@smp3.example.im:5224" + alice #$> ("/smp smp://2345-w==@smp2.example.im smp://3456-w==@smp3.example.im:5224", id, "ok") + alice #$> ("/xftp xftp://2345-w==@xftp2.example.im xftp://3456-w==@xftp3.example.im:5224", id, "ok") + checkCustomServers alice - alice ##> "/create user same_smp=on alisa" + alice ##> "/create user same_servers=on alisa" showActiveUser alice "alisa" - alice #$> ("/smp", id, "smp://2345-w==@smp2.example.im") + checkCustomServers alice + where + checkCustomServers alice = do + alice ##> "/smp" + alice <## "smp://2345-w==@smp2.example.im" alice <## "smp://3456-w==@smp3.example.im:5224" + alice ##> "/xftp" + alice <## "xftp://2345-w==@xftp2.example.im" + alice <## "xftp://3456-w==@xftp3.example.im:5224" testDeleteUser :: HasCallStack => FilePath -> IO () testDeleteUser =