From eb1ab8f5612085a25f3bd25a0de06f1ad368c576 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 26 Sep 2021 16:36:05 +0100 Subject: [PATCH] fix sending notification containing apostrophe on mac, wrap notification in exception handler (#107) --- src/Simplex/Chat/Notification.hs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Simplex/Chat/Notification.hs b/src/Simplex/Chat/Notification.hs index 9c703c8098..6339d62d6c 100644 --- a/src/Simplex/Chat/Notification.hs +++ b/src/Simplex/Chat/Notification.hs @@ -1,9 +1,11 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} module Simplex.Chat.Notification (Notification (..), initializeNotifications) where +import Control.Exception import Control.Monad (void) import Data.List (isInfixOf) import Data.Map (Map, fromList) @@ -19,18 +21,22 @@ import System.Process (readCreateProcess, shell) data Notification = Notification {title :: Text, text :: Text} initializeNotifications :: IO (Notification -> IO ()) -initializeNotifications = case os of - "darwin" -> pure $ notify macScript - "mingw32" -> initWinNotify - "linux" -> - doesFileExist "/proc/sys/kernel/osrelease" >>= \case - False -> pure $ notify linuxScript - True -> do - v <- readFile "/proc/sys/kernel/osrelease" - if "Microsoft" `isInfixOf` v || "WSL" `isInfixOf` v - then initWslNotify - else pure $ notify linuxScript - _ -> pure . const $ pure () +initializeNotifications = + hideException <$> case os of + "darwin" -> pure $ notify macScript + "mingw32" -> initWinNotify + "linux" -> + doesFileExist "/proc/sys/kernel/osrelease" >>= \case + False -> pure $ notify linuxScript + True -> do + v <- readFile "/proc/sys/kernel/osrelease" + if "Microsoft" `isInfixOf` v || "WSL" `isInfixOf` v + then initWslNotify + else pure $ notify linuxScript + _ -> pure . const $ pure () + +hideException :: (a -> IO ()) -> (a -> IO ()) +hideException f a = f a `catch` \(_ :: SomeException) -> pure () notify :: (Notification -> Text) -> Notification -> IO () notify script notification = @@ -46,7 +52,7 @@ macScript :: Notification -> Text macScript Notification {title, text} = "osascript -e 'display notification \"" <> macEscape text <> "\" with title \"" <> macEscape title <> "\"'" macEscape :: Text -> Text -macEscape = replaceAll $ fromList [('"', "\\\"")] +macEscape = replaceAll $ fromList [('"', "\\\""), ('\'', "")] initWslNotify :: IO (Notification -> IO ()) initWslNotify = notify . wslScript <$> savePowershellScript