aboutsummaryrefslogtreecommitdiff
path: root/Maildir.hs
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2013-09-03 20:24:51 -0300
committerRaúl Benencia <rul@kalgan.cc>2013-09-03 20:24:51 -0300
commit84fa12fef1736d04ee79e40cebaadadda262f063 (patch)
tree4842389a50b1c0f76b3d9a27ef3f562adac87b57 /Maildir.hs
parentc91af8d1f85f876eb7119ce8406385cf570d3886 (diff)
Mark as read functionality
Diffstat (limited to 'Maildir.hs')
-rw-r--r--Maildir.hs31
1 files changed, 25 insertions, 6 deletions
diff --git a/Maildir.hs b/Maildir.hs
index 7e2941d..752f6f0 100644
--- a/Maildir.hs
+++ b/Maildir.hs
@@ -11,8 +11,8 @@ module Maildir where
import Control.Monad.Loops(allM)
import Control.Monad (forM, filterM)
import Data.List(isPrefixOf)
-import System.Directory (doesDirectoryExist, getDirectoryContents)
-import System.FilePath ((</>))
+import System.Directory (doesDirectoryExist, getDirectoryContents, renameFile)
+import System.FilePath ((</>), takeFileName, takeDirectory, splitDirectories, joinPath)
import System.IO(IOMode(..), hGetContents, openFile)
import Types(Maildir, Flag(..), Flags)
@@ -40,10 +40,29 @@ getMessages :: Maildir -> [FilePath] -> IO [(FilePath, Flags, String)]
getMessages mb list = do
messages <- getAll mb
return $ filter (\(id, f, m) -> id `elem` list) messages
-
---
--- | Based on getRecursiveContents from Real World Haskell
---
+
+{- Given a mail in a Maildir, mark it as read -}
+markAsRead :: FilePath -> IO FilePath
+markAsRead fp =
+ case newPath of
+ Nothing -> return fp
+ Just path -> do
+ renameFile fp path
+ return path
+ where newPath =
+ if not $ isNew fp
+ then Just fp
+ else do
+ let fil = takeFileName fp
+ let dir = takeDirectory fp
+ let spl = splitDirectories dir
+ case last spl of
+ "cur" -> Just $ fp ++ "S"
+ "new" -> Just $ (joinPath . init $ spl) </> ("cur" </> (fil ++ "S"))
+ _ -> Nothing
+
+
+-- Based on getRecursiveContents from Real World Haskell
getMaildirsRecursively :: FilePath -> IO [Maildir]
getMaildirsRecursively topdir = do
result <- search topdir
nihil fit ex nihilo