diff options
author | joe <joe@jerkface.net> | 2013-06-27 22:38:53 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-27 22:38:53 -0400 |
commit | 86e16f72783bcb546dd3ac447237f729e68c710d (patch) | |
tree | 14d195c6ee5770f26120612ce33ca52c337a3b91 /Presence/XMPPTypes.hs | |
parent | 61e76eb99c74aa4c0769e2fe7f280b9a4530171d (diff) |
Started conduits-based XMPP module (will replace XMPPServer)
Diffstat (limited to 'Presence/XMPPTypes.hs')
-rw-r--r-- | Presence/XMPPTypes.hs | 84 |
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 #-} | ||
3 | module XMPPTypes where | ||
4 | |||
5 | import Network.Socket (Socket,SockAddr(..)) | ||
6 | import System.IO (Handle) | ||
7 | import Control.Concurrent.STM (TChan) | ||
8 | import Data.ByteString.Lazy.Char8 as L (ByteString,unpack,pack) | ||
9 | import Text.Show.ByteString as L | ||
10 | import Data.Binary.Builder as B | ||
11 | import Data.Binary.Put | ||
12 | import Control.DeepSeq | ||
13 | import ByteStringOperators | ||
14 | |||
15 | class 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 | |||
24 | class XMPPConfig config where | ||
25 | getBuddies :: config -> ByteString -> IO [ByteString] | ||
26 | getSubscribers :: config -> ByteString -> IO [ByteString] | ||
27 | |||
28 | -- | Jabber ID (JID) datatype | ||
29 | data JID = JID { name :: Maybe ByteString | ||
30 | , peer :: Peer | ||
31 | , resource :: Maybe ByteString | ||
32 | } | ||
33 | deriving (Eq,Ord) | ||
34 | |||
35 | data JabberShow = Offline | ||
36 | | Away | ||
37 | | Available | ||
38 | deriving (Prelude.Show,Enum,Ord,Eq,Read) | ||
39 | |||
40 | data Presence = Presence JID JabberShow | ||
41 | deriving Prelude.Show | ||
42 | |||
43 | data Peer = LocalHost | RemotePeer SockAddr | ||
44 | deriving (Eq,Prelude.Show) | ||
45 | |||
46 | |||
47 | |||
48 | instance 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 | |||
63 | instance L.Show JID where | ||
64 | showp (JID n s r ) = putBuilder . B.fromLazyByteString $ n <$++> "@" <?++> showPeer s <++?> "/" <++$> r | ||
65 | |||
66 | instance Prelude.Show JID where | ||
67 | show jid = L.unpack $ L.show jid | ||
68 | |||
69 | instance NFData JID where | ||
70 | rnf v@(JID n s r) = n `seq` s `seq` r `seq` () | ||
71 | |||
72 | jid user host rsrc = JID (Just user) host (Just rsrc) | ||
73 | |||
74 | |||
75 | showPeer :: Peer -> ByteString | ||
76 | showPeer LocalHost = "localhost" | ||
77 | showPeer (RemotePeer addr@(SockAddrInet _ _)) = pack $ stripColon (Prelude.show addr) | ||
78 | where stripColon s = pre where (pre,port) = break (==':') s | ||
79 | showPeer (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 | |||