{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module Network.Mail.Assumpta.Internal.Net
where
import Control.Monad.Catch (MonadMask)
import Control.Monad.IO.Class
import Data.Default (def)
import qualified Network.Connection as NC
import Network.Mail.Assumpta.Connection as C
type Port = Int
type HostName = String
data Handle = Handle {
hConn :: !NC.Connection
, hContext :: !NC.ConnectionContext
}
instance Connection Handle where
type Cstrt Handle = MonadIO
type Params Handle = NC.ConnectionParams
open params = do ctx <- liftIO NC.initConnectionContext
conn <- liftIO $ NC.connectTo ctx params
return $ Handle conn ctx
close = liftIO . NC.connectionClose . hConn
send h bs = liftIO $ NC.connectionPut (hConn h) bs
recv h = liftIO $ NC.connectionGet (hConn h) 2048
upgrade h = let conn = hConn h
ctx = hContext h
tls = defaultTLSSettings
in liftIO $ NC.connectionSetSecure ctx conn tls
defaultTLSSettings :: NC.TLSSettings
defaultTLSSettings = def
mkParams ::
HostName -> Int -> NC.ConnectionParams
mkParams hostname port =
NC.ConnectionParams {
NC.connectionHostname = hostname
, NC.connectionPort = fromIntegral port
, NC.connectionUseSecure = Nothing
, NC.connectionUseSocks = Nothing
}
openParams ::
MonadIO m => NC.ConnectionParams -> m Handle
openParams =
C.open
open :: MonadIO m => HostName -> Port -> m Handle
open host port =
let p = mkParams host port
in openParams p
openTls :: MonadIO m => HostName -> Port -> m Handle
openTls host port =
let p = (mkParams host port) { NC.connectionUseSecure = Just defaultTLSSettings }
in openParams p
close :: MonadIO m => Handle -> m ()
close =
C.close
withHandleParams ::
(MonadMask m, MonadIO m) => NC.ConnectionParams -> (Handle -> m b) -> m b
withHandleParams =
C.withConnection
withHandle ::
(MonadMask m, MonadIO m) => HostName -> Port -> (Handle -> m b) -> m b
withHandle host port =
let p = mkParams host port
in withHandleParams p
withSecureHandle ::
(MonadMask m, MonadIO m) => HostName -> Port -> (Handle -> m b) -> m b
withSecureHandle host port =
let p = (mkParams host port) {
NC.connectionUseSecure = Just defaultTLSSettings
}
in withHandleParams p