diff options
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/ConfigFiles.hs | 35 | ||||
-rw-r--r-- | Presence/main.hs | 11 |
2 files changed, 45 insertions, 1 deletions
diff --git a/Presence/ConfigFiles.hs b/Presence/ConfigFiles.hs index ad0f4c29..32ed8b86 100644 --- a/Presence/ConfigFiles.hs +++ b/Presence/ConfigFiles.hs | |||
@@ -13,6 +13,9 @@ import Control.Exception | |||
13 | import Control.Monad | 13 | import Control.Monad |
14 | import Control.DeepSeq | 14 | import Control.DeepSeq |
15 | import ByteStringOperators () -- For NFData instance | 15 | import ByteStringOperators () -- For NFData instance |
16 | import ControlMaybe | ||
17 | import Data.List (partition) | ||
18 | import Data.Maybe (catMaybes,isJust) | ||
16 | 19 | ||
17 | type User = ByteString | 20 | type User = ByteString |
18 | 21 | ||
@@ -51,6 +54,38 @@ addItem item tag path = | |||
51 | L.hPutStrLn h item | 54 | L.hPutStrLn h item |
52 | in doit | 55 | in doit |
53 | 56 | ||
57 | |||
58 | modifyFile :: | ||
59 | (ByteString,FilePath) | ||
60 | -> ByteString | ||
61 | -> (ByteString -> IO (Maybe ByteString)) | ||
62 | -> Maybe ByteString | ||
63 | -> IO () | ||
64 | modifyFile (tag,file) user test appending = configPath user file >>= doit | ||
65 | where | ||
66 | doit path = do | ||
67 | handle (\e -> when (isDoesNotExistError e) | ||
68 | (createConfigFile tag path >> doit path)) | ||
69 | $ do exists <- fileExist path | ||
70 | if exists | ||
71 | then do | ||
72 | xs <- withFile path ReadMode $ \h -> do | ||
73 | contents <- L.hGetContents h | ||
74 | case L.lines contents of | ||
75 | x:xs -> mapM test xs | ||
76 | _ -> return [] | ||
77 | let (keepers,deleted) = partition isJust xs | ||
78 | withFile path WriteMode $ \h -> do | ||
79 | L.hPutStrLn h tag | ||
80 | forM_ (catMaybes keepers) (L.hPutStrLn h) | ||
81 | withJust appending (L.hPutStrLn h) | ||
82 | else withFile path WriteMode $ \h -> do | ||
83 | L.hPutStrLn h tag | ||
84 | withJust appending (L.hPutStrLn h) | ||
85 | |||
86 | |||
87 | modifySolicited = modifyFile ("<? solicited ?>",solicitedFile) | ||
88 | |||
54 | addBuddy :: User -> ByteString -> IO () | 89 | addBuddy :: User -> ByteString -> IO () |
55 | addBuddy user buddy = | 90 | addBuddy user buddy = |
56 | configPath user buddyFile >>= addItem buddy "<? buddies ?>" | 91 | configPath user buddyFile >>= addItem buddy "<? buddies ?>" |
diff --git a/Presence/main.hs b/Presence/main.hs index a7ff5e5a..bbaac97a 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -105,6 +105,15 @@ data ClientSession = ClientSession { | |||
105 | presence_state :: PresenceState | 105 | presence_state :: PresenceState |
106 | } | 106 | } |
107 | 107 | ||
108 | addJid modify user jid = do | ||
109 | newitem <- parseHostNameJID jid | ||
110 | let cmp jid = do | ||
111 | -- putStrLn $ "Comparing "<++>bshow jid | ||
112 | olditem <- parseHostNameJID jid | ||
113 | if olditem==newitem then return Nothing | ||
114 | else return $ Just jid | ||
115 | modify user cmp (Just jid) | ||
116 | |||
108 | instance JabberClientSession ClientSession where | 117 | instance JabberClientSession ClientSession where |
109 | data XMPPClass ClientSession = ClientSessions PresenceState | 118 | data XMPPClass ClientSession = ClientSessions PresenceState |
110 | 119 | ||
@@ -189,7 +198,7 @@ instance JabberClientSession ClientSession where | |||
189 | 198 | ||
190 | addSolicited s jid = do | 199 | addSolicited s jid = do |
191 | user <- readIORef (unix_uid s) >>= getJabberUserForId | 200 | user <- readIORef (unix_uid s) >>= getJabberUserForId |
192 | ConfigFiles.addSolicited user jid -- (L.show jid) | 201 | addJid ConfigFiles.modifySolicited user jid |
193 | let rchan = rosterChannel . presence_state $ s | 202 | let rchan = rosterChannel . presence_state $ s |
194 | atomically $ do | 203 | atomically $ do |
195 | isempty <- isEmptyTMVar rchan | 204 | isempty <- isEmptyTMVar rchan |