summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Presence/ConfigFiles.hs19
1 files changed, 15 insertions, 4 deletions
diff --git a/Presence/ConfigFiles.hs b/Presence/ConfigFiles.hs
index d0d2ef63..ad0f4c29 100644
--- a/Presence/ConfigFiles.hs
+++ b/Presence/ConfigFiles.hs
@@ -3,6 +3,7 @@ module ConfigFiles where
3 3
4import Data.ByteString.Lazy.Char8 as L 4import Data.ByteString.Lazy.Char8 as L
5import System.Posix.User 5import System.Posix.User
6import System.Posix.Files (fileExist)
6import System.FilePath 7import System.FilePath
7import System.Directory 8import System.Directory
8import System.IO 9import System.IO
@@ -11,7 +12,7 @@ import System.IO.Error
11import Control.Exception 12import Control.Exception
12import Control.Monad 13import Control.Monad
13import Control.DeepSeq 14import Control.DeepSeq
14import ByteStringOperators() -- For NFData instance 15import ByteStringOperators () -- For NFData instance
15 16
16type User = ByteString 17type User = ByteString
17 18
@@ -34,15 +35,20 @@ createConfigFile tag path = do
34 doesDirectoryExist dir >>= flip unless (do 35 doesDirectoryExist dir >>= flip unless (do
35 createDirectory dir 36 createDirectory dir
36 ) 37 )
37 withFile path WriteMode $ \h -> 38 withFile path WriteMode $ \h -> do
38 L.hPutStrLn h tag 39 L.hPutStrLn h tag
39 40
40addItem item tag path = 41addItem item tag path =
41 let doit = do 42 let doit = do
42 handle (\e -> when (isDoesNotExistError e) 43 handle (\e -> when (isDoesNotExistError e)
43 (createConfigFile tag path >> doit)) 44 (createConfigFile tag path >> doit))
44 $ withFile path AppendMode $ \h -> 45 $ do exists <- fileExist path
45 L.hPutStrLn h item 46 if exists
47 then withFile path AppendMode $ \h ->
48 L.hPutStrLn h item
49 else withFile path WriteMode $ \h -> do
50 L.hPutStrLn h tag
51 L.hPutStrLn h item
46 in doit 52 in doit
47 53
48addBuddy :: User -> ByteString -> IO () 54addBuddy :: User -> ByteString -> IO ()
@@ -53,6 +59,11 @@ addSubscriber :: User -> ByteString -> IO ()
53addSubscriber user subscriber = 59addSubscriber user subscriber =
54 configPath user subscriberFile >>= addItem subscriber "<? subscribers ?>" 60 configPath user subscriberFile >>= addItem subscriber "<? subscribers ?>"
55 61
62addSolicited :: User -> ByteString -> IO ()
63addSolicited user solicited =
64 configPath user solicitedFile >>= addItem solicited "<? solicited ?>"
65
66
56getConfigList path = 67getConfigList path =
57 handle (\e -> if isDoesNotExistError e then (return []) else throw e) 68 handle (\e -> if isDoesNotExistError e then (return []) else throw e)
58 $ withFile path ReadMode $ 69 $ withFile path ReadMode $