diff options
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/ConfigFiles.hs | 19 |
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 | ||
4 | import Data.ByteString.Lazy.Char8 as L | 4 | import Data.ByteString.Lazy.Char8 as L |
5 | import System.Posix.User | 5 | import System.Posix.User |
6 | import System.Posix.Files (fileExist) | ||
6 | import System.FilePath | 7 | import System.FilePath |
7 | import System.Directory | 8 | import System.Directory |
8 | import System.IO | 9 | import System.IO |
@@ -11,7 +12,7 @@ import System.IO.Error | |||
11 | import Control.Exception | 12 | import Control.Exception |
12 | import Control.Monad | 13 | import Control.Monad |
13 | import Control.DeepSeq | 14 | import Control.DeepSeq |
14 | import ByteStringOperators() -- For NFData instance | 15 | import ByteStringOperators () -- For NFData instance |
15 | 16 | ||
16 | type User = ByteString | 17 | type 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 | ||
40 | addItem item tag path = | 41 | addItem 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 | ||
48 | addBuddy :: User -> ByteString -> IO () | 54 | addBuddy :: User -> ByteString -> IO () |
@@ -53,6 +59,11 @@ addSubscriber :: User -> ByteString -> IO () | |||
53 | addSubscriber user subscriber = | 59 | addSubscriber user subscriber = |
54 | configPath user subscriberFile >>= addItem subscriber "<? subscribers ?>" | 60 | configPath user subscriberFile >>= addItem subscriber "<? subscribers ?>" |
55 | 61 | ||
62 | addSolicited :: User -> ByteString -> IO () | ||
63 | addSolicited user solicited = | ||
64 | configPath user solicitedFile >>= addItem solicited "<? solicited ?>" | ||
65 | |||
66 | |||
56 | getConfigList path = | 67 | getConfigList 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 $ |