summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Presence/ConfigFiles.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/Presence/ConfigFiles.hs b/Presence/ConfigFiles.hs
new file mode 100644
index 00000000..ee0d5b85
--- /dev/null
+++ b/Presence/ConfigFiles.hs
@@ -0,0 +1,66 @@
1{-# LANGUAGE OverloadedStrings #-}
2module ConfigFiles where
3
4import Data.ByteString.Lazy.Char8 as L
5import System.Posix.User
6import System.FilePath
7import System.Directory
8import System.IO
9-- import System.IO.Strict
10import System.IO.Error
11import Control.Exception
12import Control.Monad
13import Todo
14import Control.DeepSeq
15import ByteStringOperators
16
17type User = ByteString
18
19configDir = ".presence"
20buddyFile = "buddies"
21subscriberFile = "subscribers"
22
23buddyPath :: User -> IO String
24buddyPath user = do
25 ue <- getUserEntryForName (unpack user)
26 return $ (++("/"++configDir++"/"++buddyFile)) $ homeDirectory ue
27
28subscriberPath :: User -> IO String
29subscriberPath user = do
30 ue <- getUserEntryForName (unpack user)
31 return $ (++("/"++configDir++"/"++subscriberFile)) $ homeDirectory ue
32
33createConfigFile tag path = do
34 let dir = dropFileName path
35 doesDirectoryExist dir >>= flip unless (do
36 createDirectory dir
37 )
38 withFile path WriteMode $ \h ->
39 L.hPutStrLn h tag
40
41addItem item tag path =
42 let doit = do
43 handle (\e -> when (isDoesNotExistError e)
44 (createConfigFile tag path >> doit))
45 $ withFile path AppendMode $ \h ->
46 L.hPutStrLn h item
47 in doit
48
49addBuddy :: User -> ByteString -> IO ()
50addBuddy user buddy =
51 buddyPath user >>= addItem buddy "<? buddies ?>"
52
53addSubscriber :: User -> ByteString -> IO ()
54addSubscriber user subscriber =
55 subscriberPath user >>= addItem subscriber "<? subscribers ?>"
56
57getConfigList path = withFile path ReadMode $
58 L.hGetContents
59 >=> return . Prelude.tail . L.lines
60 >=> (\a -> seq (rnf a) (return a))
61
62getBuddies :: User -> IO [ByteString]
63getBuddies user = buddyPath user >>= getConfigList
64
65getSubscribers :: User -> IO [ByteString]
66getSubscribers user = subscriberPath user >>= getConfigList