packages feed

irc-ctcp 0.1.2.1 → 0.1.3.0

raw patch · 3 files changed

+81/−60 lines, 3 filesdep ~base

Dependency ranges changed: base

Files

LICENSE view
@@ -1,13 +1,20 @@-        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE -                    Version 2, December 2004 -- Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> +Copyright (c) 2014, Michael Walker <mike@barrucadu.co.uk> - Everyone is permitted to copy and distribute verbatim or modified - copies of this license document, and changing it is allowed as long - as the name is changed. +Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions: -            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE -   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION +The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software. -  0. You just DO WHAT THE FUCK YOU WANT TO.+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Network/IRC/CTCP.hs view
@@ -1,24 +1,25 @@ -- |Functions for encoding and decoding CTCPs. module Network.IRC.CTCP-    ( -- *Types-      CTCPByteString-    , getUnderlyingByteString+  ( -- *Types+    CTCPByteString+  , getUnderlyingByteString -    -- *Encoding and decoding-    , toCTCP-    , fromCTCP-    , encodeCTCP-    , decodeCTCP+  -- *Encoding and decoding+  , toCTCP+  , fromCTCP+  , encodeCTCP+  , decodeCTCP -    -- *Utilities-    , isCTCP-    , asCTCP-    , orCTCP-    ) where+  -- *Utilities+  , isCTCP+  , asCTCP+  , orCTCP+  ) where  import Data.ByteString    (ByteString, pack, singleton, unpack) import Data.List          (mapAccumL) import Data.Maybe         (catMaybes, fromMaybe)+import Data.Monoid        ((<>)) import Data.Text          (Text, splitOn) import Data.Text.Encoding (decodeUtf8, encodeUtf8) import Data.Tuple         (swap)@@ -28,7 +29,7 @@  -- |Type representing a CTCP-encoded bytestring. newtype CTCPByteString = CBS ByteString-    deriving (Eq, Show)+  deriving (Eq, Show)  -- |Get the underlying (encoded) bytestring from a CTCP bytestring. getUnderlyingByteString :: CTCPByteString -> ByteString@@ -43,20 +44,19 @@  -- |Encode a bytestring according to the CTCP spec. encodeCTCP :: ByteString -> CTCPByteString-encodeCTCP bs = CBS $ B.concat [ singleton soh-                               , escape bs-                               , singleton soh-                               ]+encodeCTCP bs = CBS $ singleton soh <> escape bs <> singleton soh -    where escape = B.concatMap escape'-          escape' x = case lookup x encodings of-                        -- If there is an encoding, escape it and use-                        -- that.-                        Just x' -> pack [esc, x']+-- |Escape a bytestring according to the CTCP spec.+escape :: ByteString -> ByteString+escape = B.concatMap escape'+  where+    escape' x =+      case lookup x encodings of+        -- If there is an encoding, escape it and use that.+        Just x' -> pack [esc, x'] -                        -- Otherwise, just return the original-                        -- character.-                        Nothing -> singleton x+        -- Otherwise, just return the original character.+        Nothing -> singleton x  -- |Decode a CTCP-encoded bytestring and turn it into a command name -- and arguments.@@ -64,24 +64,26 @@ -- This decodes the text with UTF-8. If another encoding is desired, -- 'decodeCTCP' should be used directly. fromCTCP :: CTCPByteString -> (Text, [Text])-fromCTCP bs = case splitOn (T.pack " ") . decodeUtf8 . decodeCTCP $ bs of-                (cmd : args) -> (cmd, args)-                _            -> (T.pack "", [])+fromCTCP bs =+  case splitOn (T.pack " ") . decodeUtf8 . decodeCTCP $ bs of+    (cmd : args) -> (cmd, args)+    _            -> (T.pack "", [])  -- |Decode a CTCP bytestring. Extraeneous escapes are dropped. decodeCTCP :: CTCPByteString -> ByteString decodeCTCP (CBS bs) = unescape . B.tail . B.init $ bs -    where unescape = pack . catMaybes . snd . mapAccumL step False . unpack--          -- If we fail to find a decoding, ignore the escape.-          step True x = (False, Just . fromMaybe x $ lookup x decodings)+-- |Unescape a bytestring according to the CTCP spec. Extraeneous escapes are dropped.+unescape :: ByteString -> ByteString+unescape = pack . catMaybes . snd . mapAccumL step False . unpack+  where+    -- If we fail to find a decoding, ignore the escape.+    step True x = (False, Just . fromMaybe x $ lookup x decodings) -          -- Enter escape mode, this doesn't add a character to the-          -- output.-          step False 0o020 = (True, Nothing)+    -- Enter escape mode, this doesn't add a character to the output.+    step False 0o020 = (True, Nothing) -          step _ x = (False, Just x)+    step _ x = (False, Just x)  soh :: Integral i => i soh = 0o001@@ -90,11 +92,12 @@ esc = 0o020  encodings :: Integral i => [(i, i)]-encodings = [ (0o000, 0o060)-            , (0o012, 0o156)-            , (0o015, 0o162)-            , (0o020, 0o020)-            ]+encodings =+  [ (0o000, 0o060)+  , (0o012, 0o156)+  , (0o015, 0o162)+  , (0o020, 0o020)+  ]  decodings :: Integral i => [(i, i)] decodings = map swap encodings@@ -113,9 +116,10 @@ -- -- This uses 'isCTCP', and so is lenient with escapes. asCTCP :: ByteString -> Maybe CTCPByteString-asCTCP bs = if isCTCP bs-            then Just $ CBS bs-            else Nothing+asCTCP bs =+  if isCTCP bs+  then Just $ CBS bs+  else Nothing  -- |Apply one of two functions depending on whether the bytestring -- looks like a CTCP or not.
irc-ctcp.cabal view
@@ -10,7 +10,7 @@ -- PVP summary:      +-+------- breaking API changes --                   | | +----- non-breaking API additions --                   | | | +--- code changes with no API change-version:             0.1.2.1+version:             0.1.3.0  -- A short (one-line) description of the package. synopsis:            A CTCP encoding and decoding library for IRC clients.@@ -56,10 +56,13 @@   details.  -- URL for the project homepage or repository.-homepage:            https://github.com/barrucadu/yukibot+homepage:            https://github.com/barrucadu/irc-ctcp +-- URL where users should direct bug reports.+bug-reports:         https://github.com/barrucadu/irc-ctcp/issues+ -- The license under which the package is released.-license:             OtherLicense+license:             MIT  -- The file containing the license text. license-file:        LICENSE@@ -85,7 +88,6 @@ -- Constraint on the version of Cabal needed to build this package. cabal-version:       >=1.10 - library   -- Modules exported by the library.   exposed-modules:     Network.IRC.CTCP@@ -97,7 +99,7 @@   -- other-extensions:          -- Other library packages from which modules are imported.-  build-depends:       base       >=4.7 && <5+  build-depends:       base       >=4.5 && <5                      , bytestring >=0.10                      , text       >=1.1   @@ -107,3 +109,11 @@   -- Base language which the package is written in.   default-language:    Haskell2010   +source-repository head+  type:     git+  location: https://github.com/barrucadu/irc-ctcp.git++source-repository this+  type:     git+  location: https://github.com/barrucadu/irc-ctcp.git+  tag:      0.1.3.0