summaryrefslogtreecommitdiff
path: root/Presence/ConfigFiles.hs
blob: ad0f4c29f4f1f57a92af249546620e522e39c1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{-# LANGUAGE OverloadedStrings #-}
module ConfigFiles where

import Data.ByteString.Lazy.Char8 as L
import System.Posix.User
import System.Posix.Files (fileExist)
import System.FilePath
import System.Directory
import System.IO
-- import System.IO.Strict
import System.IO.Error
import Control.Exception
import Control.Monad
import Control.DeepSeq
import ByteStringOperators () -- For NFData instance

type User = ByteString

configDir = ".presence"
buddyFile = "buddies"
subscriberFile = "subscribers"
otherFile = "others"
pendingFile = "pending"
solicitedFile = "solicited"


configPath :: User -> String -> IO String
configPath user filename = do
    ue <- getUserEntryForName (unpack user)
    return $ (++("/"++configDir++"/"++filename)) $ homeDirectory ue


createConfigFile tag path = do
    let dir = dropFileName path
    doesDirectoryExist dir >>= flip unless (do
            createDirectory dir 
        )
    withFile path WriteMode $ \h -> do
        L.hPutStrLn h tag

addItem item tag path =
    let doit = do
         handle (\e -> when (isDoesNotExistError e) 
                       (createConfigFile tag path >> doit))
           $ do exists <- fileExist path
                if exists
                  then withFile path AppendMode $ \h ->
                         L.hPutStrLn h item
                  else withFile path WriteMode $ \h -> do
                         L.hPutStrLn h tag
                         L.hPutStrLn h item
    in doit

addBuddy :: User -> ByteString -> IO ()
addBuddy user buddy =
    configPath user buddyFile >>= addItem buddy "<? buddies ?>"

addSubscriber :: User -> ByteString -> IO ()
addSubscriber user subscriber =
    configPath user subscriberFile >>= addItem subscriber "<? subscribers ?>"

addSolicited :: User -> ByteString -> IO ()
addSolicited user solicited =
    configPath user solicitedFile >>= addItem solicited "<? solicited ?>"


getConfigList path = 
  handle (\e -> if isDoesNotExistError e then (return []) else throw e)
    $ withFile path ReadMode $
        L.hGetContents 
        >=> return . Prelude.tail . L.lines 
        >=> (\a -> seq (rnf a) (return a))

getBuddies :: User -> IO [ByteString]
getBuddies user = configPath user buddyFile >>= getConfigList

getSubscribers :: User -> IO [ByteString]
getSubscribers user = configPath user subscriberFile >>= getConfigList

getOthers :: User -> IO [ByteString]
getOthers user = configPath user otherFile >>= getConfigList

getPending :: User -> IO [ByteString]
getPending user = configPath user pendingFile >>= getConfigList

getSolicited :: User -> IO [ByteString]
getSolicited user = configPath user solicitedFile >>= getConfigList