diff options
Diffstat (limited to 'Presence/Util.hs')
-rw-r--r-- | Presence/Util.hs | 57 |
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 #-} | ||
2 | module Util where | ||
3 | |||
4 | import qualified Data.ByteString.Lazy as L | ||
5 | import Data.Monoid | ||
6 | import qualified Data.Text as Text | ||
7 | ;import Data.Text (Text) | ||
8 | import qualified Data.Text.Encoding as Text | ||
9 | import qualified Network.BSD as BSD | ||
10 | import Network.Socket | ||
11 | |||
12 | import Network.Address (setPort) | ||
13 | |||
14 | type UserName = Text | ||
15 | type ResourceName = Text | ||
16 | |||
17 | stripResource :: Text -> Text | ||
18 | stripResource jid = let (n,h,_) = splitJID jid | ||
19 | in unsplitJID (n,h,Nothing) | ||
20 | |||
21 | unsplitJID :: (Maybe UserName,Text,Maybe ResourceName) -> Text | ||
22 | unsplitJID (n,h,r) = username <> h <> resource | ||
23 | where | ||
24 | username = maybe "" (<>"@") n | ||
25 | resource = maybe "" ("/"<>) r | ||
26 | |||
27 | splitJID :: Text -> (Maybe UserName,Text,Maybe ResourceName) | ||
28 | splitJID 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 | |||
38 | textHostName :: IO Text | ||
39 | textHostName = fmap Text.pack BSD.getHostName | ||
40 | |||
41 | textToLazyByteString :: Text -> L.ByteString | ||
42 | textToLazyByteString s = L.fromChunks [Text.encodeUtf8 s] | ||
43 | |||
44 | lazyByteStringToText :: L.ByteString -> Text | ||
45 | lazyByteStringToText = (foldr (<>) mempty . map Text.decodeUtf8 . L.toChunks) | ||
46 | |||
47 | -- | for example: 2001-db8-85a3-8d3-1319-8a2e-370-7348.ipv6-literal.net | ||
48 | ip6literal :: Text -> Text | ||
49 | ip6literal addr = Text.map dash addr <> ".ipv6-literal.net" | ||
50 | where | ||
51 | dash ':' = '-' | ||
52 | dash x = x | ||
53 | |||
54 | sameAddress :: SockAddr -> SockAddr -> Bool | ||
55 | sameAddress laddr addr = setPort 0 laddr == setPort 0 addr | ||
56 | |||
57 | |||