hOpenPGP-3.1.1: Data/Conduit/OpenPGP/Filter.hs
-- Filter.hs: OpenPGP (RFC4880) packet filtering
-- Copyright © 2014-2026 Clint Adams
-- This software is released under the terms of the Expat license.
-- (See the LICENSE file).
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Data.Conduit.OpenPGP.Filter
( conduitPktFilter
, conduitPktWithExtraFilter
, conduitTKFilter
, FilterPredicates (..)
, runPredicate
) where
import Control.Monad.Trans.Reader (Reader, runReader)
import Data.Conduit (ConduitT)
import qualified Data.Conduit.List as CL
import Data.Typeable (Typeable, eqT, (:~:) (Refl))
import Data.Void (Void)
import Codec.Encryption.OpenPGP.Types
data FilterPredicates r a
= -- | fp for transferable keys
RTKFilterPredicate (Reader TKUnknown Bool)
| -- | fp for context-less packets
RPFilterPredicate (Reader Pkt Bool)
| -- | generic filter predicate
RFilterPredicate (Reader a Bool)
| -- | generic filter predicate with additional context
RPairFilterPredicate (Reader (r, a) Bool)
{-# DEPRECATED RTKFilterPredicate "Use RFilterPredicate with SomeTK instead" #-}
runPredicate
:: forall r a. Typeable a => FilterPredicates r a -> a -> Bool
runPredicate (RTKFilterPredicate e) = case eqT @a @TKUnknown of
Just Refl -> runReader e
Nothing -> const False
runPredicate (RFilterPredicate e) = runReader e
runPredicate _ = const False
conduitPktFilter
:: Monad m => FilterPredicates Void Pkt -> ConduitT Pkt Pkt m ()
conduitPktFilter = CL.filter . superPredicate
superPredicate :: FilterPredicates Void Pkt -> Pkt -> Bool
superPredicate (RPFilterPredicate e) p = runReader e p
superPredicate (RFilterPredicate e) p = runReader e p
superPredicate _ _ = False -- do not match incorrect type of packet
{-# DEPRECATED
conduitTKFilter
"Use (CL.filter . runPredicate) with RFilterPredicate instead"
#-}
conduitTKFilter
:: Monad m
=> FilterPredicates Void TKUnknown
-> ConduitT TKUnknown TKUnknown m ()
conduitTKFilter = CL.filter . superTKPredicate
superTKPredicate
:: FilterPredicates Void TKUnknown -> TKUnknown -> Bool
superTKPredicate (RTKFilterPredicate e) = runReader e
superTKPredicate (RFilterPredicate e) = runReader e
conduitPktWithExtraFilter
:: Monad m => r -> FilterPredicates r Pkt -> ConduitT Pkt Pkt m ()
conduitPktWithExtraFilter extra = CL.filter . superPairPredicate extra
superPairPredicate :: r -> FilterPredicates r a -> a -> Bool
superPairPredicate r (RPairFilterPredicate e) p = runReader e (r, p)