summaryrefslogtreecommitdiff
path: root/Presence/Util.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Presence/Util.hs')
-rw-r--r--Presence/Util.hs57
1 files changed, 0 insertions, 57 deletions
diff --git a/Presence/Util.hs b/Presence/Util.hs
deleted file mode 100644
index e19b35fd..00000000
--- a/Presence/Util.hs
+++ /dev/null
@@ -1,57 +0,0 @@
1{-# LANGUAGE OverloadedStrings #-}
2module Util where
3
4import qualified Data.ByteString.Lazy as L
5import Data.Monoid
6import qualified Data.Text as Text
7 ;import Data.Text (Text)
8import qualified Data.Text.Encoding as Text
9import qualified Network.BSD as BSD
10import Network.Socket
11
12import Network.Address (setPort)
13
14type UserName = Text
15type ResourceName = Text
16
17stripResource :: Text -> Text
18stripResource jid = let (n,h,_) = splitJID jid
19 in unsplitJID (n,h,Nothing)
20
21unsplitJID :: (Maybe UserName,Text,Maybe ResourceName) -> Text
22unsplitJID (n,h,r) = username <> h <> resource
23 where
24 username = maybe "" (<>"@") n
25 resource = maybe "" ("/"<>) r
26
27splitJID :: Text -> (Maybe UserName,Text,Maybe ResourceName)
28splitJID bjid =
29 let (uATh,slashrsc) = Text.break (=='/') bjid
30 rsrc = if Text.null slashrsc then Nothing
31 else Just $ Text.drop 1 slashrsc
32 (u,atserver) = Text.break (=='@') uATh
33 (name,server) = if Text.null atserver then (Nothing,u)
34 else (Just u,Text.drop 1 atserver)
35 in (name,server,rsrc)
36
37
38textHostName :: IO Text
39textHostName = fmap Text.pack BSD.getHostName
40
41textToLazyByteString :: Text -> L.ByteString
42textToLazyByteString s = L.fromChunks [Text.encodeUtf8 s]
43
44lazyByteStringToText :: L.ByteString -> Text
45lazyByteStringToText = (foldr (<>) mempty . map Text.decodeUtf8 . L.toChunks)
46
47-- | for example: 2001-db8-85a3-8d3-1319-8a2e-370-7348.ipv6-literal.net
48ip6literal :: Text -> Text
49ip6literal addr = Text.map dash addr <> ".ipv6-literal.net"
50 where
51 dash ':' = '-'
52 dash x = x
53
54sameAddress :: SockAddr -> SockAddr -> Bool
55sameAddress laddr addr = setPort 0 laddr == setPort 0 addr
56
57