summaryrefslogtreecommitdiff
path: root/Presence/XMPPTypes.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-06-27 22:38:53 -0400
committerjoe <joe@jerkface.net>2013-06-27 22:38:53 -0400
commit86e16f72783bcb546dd3ac447237f729e68c710d (patch)
tree14d195c6ee5770f26120612ce33ca52c337a3b91 /Presence/XMPPTypes.hs
parent61e76eb99c74aa4c0769e2fe7f280b9a4530171d (diff)
Started conduits-based XMPP module (will replace XMPPServer)
Diffstat (limited to 'Presence/XMPPTypes.hs')
-rw-r--r--Presence/XMPPTypes.hs84
1 files changed, 84 insertions, 0 deletions
diff --git a/Presence/XMPPTypes.hs b/Presence/XMPPTypes.hs
new file mode 100644
index 00000000..eb5d349e
--- /dev/null
+++ b/Presence/XMPPTypes.hs
@@ -0,0 +1,84 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE TypeFamilies #-}
3module XMPPTypes where
4
5import Network.Socket (Socket,SockAddr(..))
6import System.IO (Handle)
7import Control.Concurrent.STM (TChan)
8import Data.ByteString.Lazy.Char8 as L (ByteString,unpack,pack)
9import Text.Show.ByteString as L
10import Data.Binary.Builder as B
11import Data.Binary.Put
12import Control.DeepSeq
13import ByteStringOperators
14
15class XMPPSession session where
16 data XMPPClass session
17 newSession :: XMPPClass session -> Socket -> Handle -> IO session
18 setResource :: session -> ByteString -> IO ()
19 getJID :: session -> IO JID
20 closeSession :: session -> IO ()
21 subscribe :: session -> Maybe JID -> IO (TChan Presence)
22 announcePresence :: session -> Presence -> IO ()
23
24class XMPPConfig config where
25 getBuddies :: config -> ByteString -> IO [ByteString]
26 getSubscribers :: config -> ByteString -> IO [ByteString]
27
28-- | Jabber ID (JID) datatype
29data JID = JID { name :: Maybe ByteString
30 , peer :: Peer
31 , resource :: Maybe ByteString
32 }
33 deriving (Eq,Ord)
34
35data JabberShow = Offline
36 | Away
37 | Available
38 deriving (Prelude.Show,Enum,Ord,Eq,Read)
39
40data Presence = Presence JID JabberShow
41 deriving Prelude.Show
42
43data Peer = LocalHost | RemotePeer SockAddr
44 deriving (Eq,Prelude.Show)
45
46
47
48instance Ord Peer where
49 LocalHost <= _
50 = True
51 RemotePeer (SockAddrUnix a) <= RemotePeer (SockAddrUnix b)
52 = a <= b
53 RemotePeer (SockAddrUnix _) <= _
54 = True
55 RemotePeer (SockAddrInet aport a) <= RemotePeer (SockAddrInet bport b)
56 = (a,aport) <= (b,bport)
57 RemotePeer (SockAddrInet aport a) <= _
58 = True
59 RemotePeer (SockAddrInet6 aport aflow a ascope) <= RemotePeer (SockAddrInet6 bport bflow b bscope)
60 = (a,aport,ascope,aflow) <= (b,bport,bscope,bflow)
61 a <= b = False
62
63instance L.Show JID where
64 showp (JID n s r ) = putBuilder . B.fromLazyByteString $ n <$++> "@" <?++> showPeer s <++?> "/" <++$> r
65
66instance Prelude.Show JID where
67 show jid = L.unpack $ L.show jid
68
69instance NFData JID where
70 rnf v@(JID n s r) = n `seq` s `seq` r `seq` ()
71
72jid user host rsrc = JID (Just user) host (Just rsrc)
73
74
75showPeer :: Peer -> ByteString
76showPeer LocalHost = "localhost"
77showPeer (RemotePeer addr@(SockAddrInet _ _)) = pack $ stripColon (Prelude.show addr)
78 where stripColon s = pre where (pre,port) = break (==':') s
79showPeer (RemotePeer addr@(SockAddrInet6 _ _ _ _)) = pack $ stripColon (Prelude.show addr)
80 where stripColon s = if null bracket then pre else pre ++ "]"
81 where
82 (pre,bracket) = break (==']') s
83
84