Skip to content

Commit

Permalink
Add max item size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed May 2, 2021
1 parent bf5102b commit 4ab9322
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Clipboard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Graphics.X11.Xlib.Extras

import Data.Binary (Binary)
import qualified Data.ByteString as B
import qualified Data.Text as T
import Lens.Micro

import System.Directory (setCurrentDirectory)
Expand All @@ -33,6 +34,13 @@ data SelectionType = UTF8 Text
| BITMAP ByteString
deriving (Show, Eq, Generic, Binary)

selectionLength :: Selection -> Int
selectionLength (Selection _ (UTF8 a)) = T.length a
selectionLength (Selection _ (PNG a)) = B.length a
selectionLength (Selection _ (JPEG a)) = B.length a
selectionLength (Selection _ (BITMAP a)) = B.length a


data Selection = Selection {
appName :: Text
, selection :: SelectionType
Expand All @@ -48,7 +56,6 @@ data XorgContext = XorgContext {
, defaultMime :: Atom
} deriving (Show)


test :: IO ()
test =
bracket getXorgContext destroyXorgContext $ \ctx -> do
Expand Down
18 changes: 14 additions & 4 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ data Command = DAEMON | PRINT | COPY Text | CLEAR | HELP deriving (Show, Read)

data Config = Config
{ maxHistoryLength :: Int
, maxItemSizeBytes :: Int
, historyPath :: Text
, staticHistoryPath :: Text
, imageCachePath :: Text
Expand All @@ -51,6 +52,7 @@ data Config = Config
configCodec :: TomlCodec Config
configCodec = Config
<$> Toml.int "max_history_length" .= maxHistoryLength
<*> Toml.int "max_item_size_bytes" .= maxItemSizeBytes
<*> Toml.text "history_file" .= historyPath
<*> Toml.text "static_history_file" .= staticHistoryPath
<*> Toml.text "image_cache_directory" .= imageCachePath
Expand Down Expand Up @@ -168,8 +170,16 @@ runDaemon = prepareDirs >> setHistoryFilePermission >> (forever $ go `catchAll`
innerloop :: (MonadIO m, MonadReader Config m) => [(IO (Maybe Clip.Selection), Maybe Clip.Selection)] -> ClipHistory -> m ClipHistory
innerloop getSelections history = do
-- Get selection from enabled clipboards
(getSelections', sel) <- liftIO $ getSelection getSelections

(getSelections', rawSelection) <- liftIO $ getSelection getSelections

-- Do not store selection items above threshold size
maxItemSize <- view (to maxItemSizeBytes)
let sel = case rawSelection of
Nothing -> Nothing
Just selection -> if maxItemSize > 0 && Clip.selectionLength selection >= maxItemSize
then Nothing
else Just selection

-- Do not use selection coming from blacklisted app
liftIO $ when (isJust sel) (print (Clip.appName <$> sel))
blacklist <- view (to blacklistedApps)
Expand Down Expand Up @@ -253,7 +263,7 @@ getConfig = do
when (isLeft tomlRes) $ do
die . toS $ "Error parsing the config file at " <> (show configPath) <> "\n" <> Toml.prettyTomlDecodeErrors (fromLeft mempty tomlRes)

let cfg = fromRight (Config 50 "" "" "" False [] True True) tomlRes
let cfg = fromRight (Config 50 0 "" "" "" False [] True True) tomlRes

-- if it ends with / we don't create a temp directory
-- user is responsible for it
Expand All @@ -268,7 +278,7 @@ getConfig = do
where
defaultConfig = do
homeDir <- toS . fromMaybe mempty . listToMaybe <$> wordexp "~/"
return $ Config 50 (homeDir <> ".cache/greenclip.history") (homeDir <> ".cache/greenclip.staticHistory" ) "/tmp/greenclip" False [] True True
return $ Config 50 0 (homeDir <> ".cache/greenclip.history") (homeDir <> ".cache/greenclip.staticHistory" ) "/tmp/greenclip" False [] True True


parseArgs :: [Text] -> Command
Expand Down

0 comments on commit 4ab9322

Please sign in to comment.