module Network.Mail.SMTP.SMTPRaw (
SMTPRaw(..)
, smtpConnect
, smtpSendCommand
, smtpSendCommandAndWait
, smtpSendRaw
, smtpGetReplyLines
, smtpDisconnect
) where
import qualified Data.ByteString as B
import Data.ByteString.Char8 (pack, unpack)
import Network
import Network.Socket
import Data.Attoparsec.ByteString.Char8
import System.IO
import Network.Mail.SMTP.ReplyLine
import Network.Mail.SMTP.Types
data SMTPRaw = SMTPRaw {
smtpPush :: B.ByteString -> IO ()
, smtpPull :: IO B.ByteString
, smtpClose :: IO ()
, smtpHandle :: Handle
}
smtpConnect :: String -> Int -> IO (SMTPRaw, Maybe Greeting)
smtpConnect host port = do
handle <- connectTo host (PortNumber $ fromIntegral port)
greet <- parseWith (B.hGetSome handle 2048) greeting ""
let push = B.hPut handle
let pull = B.hGetSome handle 2048
let close = hClose handle
return $ (SMTPRaw push pull close handle, maybeResult greet)
smtpSendCommandAndWait :: SMTPRaw -> Command -> IO (Maybe [ReplyLine])
smtpSendCommandAndWait smtpraw cmd = do
smtpSendCommand smtpraw cmd
smtpGetReplyLines smtpraw
smtpSendCommand :: SMTPRaw -> Command -> IO ()
smtpSendCommand smtpraw cmd = do
smtpSendRaw smtpraw (toByteString cmd)
smtpSendRaw smtpraw (pack "\r\n")
smtpSendRaw :: SMTPRaw -> B.ByteString -> IO ()
smtpSendRaw = smtpPush
smtpGetReplyLines :: SMTPRaw -> IO (Maybe [ReplyLine])
smtpGetReplyLines smtpraw = do
replies <- parseWith (smtpPull smtpraw) replyLines ""
return $ maybeResult replies
smtpDisconnect :: SMTPRaw -> IO ()
smtpDisconnect = smtpClose