summaryrefslogtreecommitdiff
path: root/hs/src/Mail.hs
diff options
context:
space:
mode:
Diffstat (limited to 'hs/src/Mail.hs')
-rw-r--r--hs/src/Mail.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/hs/src/Mail.hs b/hs/src/Mail.hs
new file mode 100644
index 0000000..505931e
--- /dev/null
+++ b/hs/src/Mail.hs
@@ -0,0 +1,60 @@
+{-# LANGUAGE DeriveGeneric
+ , OverloadedStrings #-}
+
+module Mail where
+
+import System.Process
+import Data.Aeson ( eitherDecode
+ , FromJSON
+ , ToJSON
+ , defaultOptions
+ , parseJSON
+ , genericToEncoding
+ , toEncoding
+ , withObject
+ , (.:)
+ )
+import Data.Map (Map)
+import GHC.Generics
+import Data.ByteString (hGet)
+import Data.ByteString.Lazy (fromStrict)
+import System.IO (Handle, hGetLine)
+
+data MailPart = MailPart
+ { filename :: Maybe String
+ , partId :: String
+ , headers :: Map String String
+ , parts :: [MailPart]
+ , contentType :: String
+ , charset :: Maybe String
+ } deriving (Generic, Show)
+
+instance ToJSON MailPart where
+ toEncoding = genericToEncoding defaultOptions
+
+instance FromJSON MailPart where
+ parseJSON = withObject "MailPart" $ \v -> MailPart
+ <$> v .: "filename"
+ <*> v .: "id"
+ <*> v .: "headers"
+ <*> v .: "parts"
+ <*> v .: "content-type"
+ <*> v .: "charset"
+
+type Proc = (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
+
+getMail :: String -> IO (Either String (MailPart, Proc))
+getMail id = do
+ let prgr = "/home/hugo/code/vimwiki-scripts/hs/mail.py" -- TODO
+ let cmd = (proc prgr [id]) { std_out = CreatePipe
+ , std_in = CreatePipe }
+ proc@(_, Just stdout, _, _) <- createProcess cmd
+
+ count <- read <$> hGetLine stdout
+ bytes <- fromStrict <$> hGet stdout count
+ case eitherDecode bytes of
+ Left err -> do
+ cleanupProcess proc
+ print bytes
+ return $ Left err
+ Right mailpart -> return $ Right (mailpart, proc)