diff options
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/LocalChat.hs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Presence/LocalChat.hs b/Presence/LocalChat.hs new file mode 100644 index 00000000..39195fc9 --- /dev/null +++ b/Presence/LocalChat.hs | |||
@@ -0,0 +1,71 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
2 | module LocalChat | ||
3 | ( module Chat | ||
4 | , module LocalChat | ||
5 | ) where | ||
6 | |||
7 | import Debug.Trace | ||
8 | import Control.Concurrent.STM | ||
9 | import Control.Monad | ||
10 | import Data.Function | ||
11 | import Data.List | ||
12 | import qualified Data.Map as Map | ||
13 | ;import Data.Map (Map) | ||
14 | import qualified Data.Text as T | ||
15 | ;import Data.Text (Text) | ||
16 | |||
17 | #ifdef THREAD_DEBUG | ||
18 | import Control.Concurrent.Lifted.Instrument | ||
19 | #else | ||
20 | import Control.Concurrent.Lifted | ||
21 | import GHC.Conc (labelThread) | ||
22 | #endif | ||
23 | |||
24 | import DPut | ||
25 | import DebugTag | ||
26 | import Chat | ||
27 | import MUC | ||
28 | |||
29 | forkUntilSignaled :: String -> STM (IO ()) -> IO (IO ()) | ||
30 | forkUntilSignaled lbl action = do | ||
31 | quitSignal <- newTVarIO False | ||
32 | t <- forkIO $ do | ||
33 | fix $ \loop -> join $ atomically | ||
34 | $ orElse (do readTVar quitSignal >>= check | ||
35 | return $ return ()) | ||
36 | (fmap (>> loop) $ action) | ||
37 | labelThread t lbl | ||
38 | return $ atomically (writeTVar quitSignal True) | ||
39 | |||
40 | |||
41 | chatevents rsvar = do | ||
42 | rs <- readTVar rsvar | ||
43 | if Map.null rs | ||
44 | then retry | ||
45 | else do | ||
46 | ios <- forM rs $ \r -> do | ||
47 | ps <- roomPending r | ||
48 | trace ("roomPending " ++ show ps) $ return () | ||
49 | case Map.toList ps of | ||
50 | (k,t):_ -> do | ||
51 | roomCommit r k t | ||
52 | return $ do | ||
53 | dput XJabber $ "fuck " ++ show (k,t) | ||
54 | _ -> retry | ||
55 | return $ foldl1 (>>) ios | ||
56 | |||
57 | forkLocalChat :: MUC -> IO (IO ()) | ||
58 | forkLocalChat muc = do | ||
59 | (chan, rs) <- atomically $ do | ||
60 | c <- dupTChan (mucChan muc) | ||
61 | rs <- newTVar Map.empty | ||
62 | return (c,rs) | ||
63 | forkUntilSignaled "localchat" $ orElse (chatevents rs) $ do | ||
64 | e <- readTChan chan | ||
65 | case e of | ||
66 | MUCCreate room jid nick r -> modifyTVar' rs $ Map.insert room r | ||
67 | return $ case e of | ||
68 | MUCCreate room jid nick _ -> | ||
69 | dput XJabber $ unwords $ map T.unpack | ||
70 | [ "MUCCreate", room, jid, nick ] | ||
71 | |||