aboutsummaryrefslogtreecommitdiff
path: root/src/F2Md/Export.hs
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-05-27 18:57:24 +1000
committerYuchen Pei <hi@ypei.me>2022-05-27 18:57:24 +1000
commitcaf354c7e5bcb5142d8b4358824d22d3de122f34 (patch)
tree7e19673f611424f4b6c44ee5316aad94aab2371a /src/F2Md/Export.hs
parent44a5cd797d72003f580f18871d9e70c06afeb13b (diff)
break apart the code, timestamp read imp.
Diffstat (limited to 'src/F2Md/Export.hs')
-rw-r--r--src/F2Md/Export.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/F2Md/Export.hs b/src/F2Md/Export.hs
new file mode 100644
index 0000000..284ab22
--- /dev/null
+++ b/src/F2Md/Export.hs
@@ -0,0 +1,40 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module F2Md.Export (writeMessage) where
+
+import F2Md.Types
+import Data.Text (Text)
+import qualified Data.Text as T
+import System.Posix.Process
+import System.Posix.Files
+import Data.Time
+import Network.HostName
+import System.Random
+import System.FilePath
+
+
+formatMessage :: Message -> Text
+formatMessage (Message from subject date messageId body) =
+ "MIME-Version: 1.0\n" <> "Date: " <> date <> "\nSubject: " <> subject <>
+ "\nFrom: " <> from <> "\nMessage-ID: " <> messageId <>
+ "\nContent-Type: text/html" <> "\n\n" <> body
+
+genFilename :: FilePath -> IO String
+genFilename root = do
+ epoch <- formatTime defaultTimeLocale "%s" <$> getCurrentTime
+ pid <- getProcessID
+ host <- getHostName
+ rand <- randomRIO (10000, 99999) :: IO Int
+ return $ root </> "new" </> epoch <> "." <> show pid <> "." <>
+ host <> "." <> show rand
+
+genUniqFilename :: FilePath -> IO String
+genUniqFilename root = do
+ filename <- genFilename root
+ exists <- fileExist filename
+ if exists then genUniqFilename root else return filename
+
+writeMessage :: FilePath -> Message -> IO ()
+writeMessage root msg =
+ genUniqFilename root >>=
+ \filename -> writeFile filename $ T.unpack (formatMessage msg)