diff options
| author | Teddy Wing | 2017-08-17 00:35:24 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-08-17 00:35:24 +0200 | 
| commit | f4028e21f8a138961927b8e794b349f209b00dc7 (patch) | |
| tree | f1099c1cd6ebd511a0a6d52c99c0545238be1a21 | |
| parent | e9278eb0a1c0126e6903f333ed4efa5dd00ff5cc (diff) | |
| download | sorbot-f4028e21f8a138961927b8e794b349f209b00dc7.tar.bz2 | |
Change `Plugin` and `Message` from `String` to `Data.Text`
Use the `Data.Text` type instead of `String` in most of the places we
use it in `Plugin` and `Message`.
This allows us to more easily pass data between the IRC package. No more
kludgy `pack`s and `unpack`s in our IRC message handler.
The one thing we couldn't convert was our regex. From what I understand
(https://stackoverflow.com/questions/14922579/haskell-regular-expressions-and-data-text#14922626),
the regex library I'm using doesn't support `Data.Text`, so use
`String`s for that instead.
| -rw-r--r-- | src/IRC.hs | 20 | ||||
| -rw-r--r-- | src/Message.hs | 8 | ||||
| -rw-r--r-- | src/Plugin.hs | 4 | ||||
| -rw-r--r-- | src/Plugin/Base.hs | 4 | ||||
| -rw-r--r-- | src/Plugin/GitHubCommit.hs | 7 | 
5 files changed, 26 insertions, 17 deletions
| @@ -32,9 +32,9 @@ handlePrivmsg = IRC.EventHandler    where      dispatchEvent (IRC.Event _ (IRC.User nick) (IRC.Privmsg _ (Right msg))) = do          let message = Message -                { text    = T.unpack msg -                , channel = T.unpack nick -                , nick    = T.unpack nick +                { text    = msg +                , channel = nick +                , nick    = nick                  }          response <- liftIO $ privmsgFromPlugin message          case response of @@ -43,9 +43,9 @@ handlePrivmsg = IRC.EventHandler      dispatchEvent (IRC.Event        _ (IRC.Channel chan nick) (IRC.Privmsg _ (Right msg))) = do          let message = Message -                { text    = T.unpack msg -                , channel = T.unpack chan -                , nick    = T.unpack nick +                { text    = msg +                , channel = chan +                , nick    = nick                  }          response <- liftIO $ privmsgFromPlugin message          case response of @@ -60,8 +60,8 @@ privmsgFromPlugin message = do              response <- liftIO $ performPlugin plugin message              return $ case response of                  Left err -> Just $ IRC.send $ IRC.Privmsg -                    (T.pack (channel message)) -                    (Right (T.pack err)) +                    (channel message) +                    (Right err)                  Right r  -> Just $ IRC.send $ IRC.Privmsg -                    (T.pack (channel message)) -                    (Right (T.pack r)) +                    (channel message) +                    (Right r) diff --git a/src/Message.hs b/src/Message.hs index 6a95638..db582e9 100644 --- a/src/Message.hs +++ b/src/Message.hs @@ -4,12 +4,14 @@ module Message      , Nick      ) where -type Channel = String +import qualified Data.Text as T -type Nick = String +type Channel = T.Text + +type Nick = T.Text  data Message = Message -    { text    :: String +    { text    :: T.Text      , channel :: Channel      , nick    :: Nick      } diff --git a/src/Plugin.hs b/src/Plugin.hs index 73df169..26432d9 100644 --- a/src/Plugin.hs +++ b/src/Plugin.hs @@ -4,6 +4,8 @@ module Plugin      , plugins      ) where +import qualified Data.Text as T +  import Text.Regex.TDFA  import qualified Message as M @@ -20,7 +22,7 @@ matchPlugin message = firstPlugin $ matchPlugins message plugins  -- | Filter the list of plugins to those that match the given message.  matchPlugins :: M.Message -> [Plugin] -> [Plugin]  matchPlugins message plugins = -    [p | p <- plugins, M.text message =~ matchRegex p] +    [p | p <- plugins, (T.unpack $ M.text message) =~ matchRegex p]  -- | Run the action belonging to the plugin, stored in its `perform` field.  performPlugin :: Plugin -> PluginAction diff --git a/src/Plugin/Base.hs b/src/Plugin/Base.hs index 6ba4ca5..62900c7 100644 --- a/src/Plugin/Base.hs +++ b/src/Plugin/Base.hs @@ -4,11 +4,13 @@ module Plugin.Base      , Plugin(..)      ) where +import qualified Data.Text as T +  import Database.SQLite.Simple  import Message -type PluginAction = Message -> IO (Either String String) +type PluginAction = Message -> IO (Either T.Text T.Text)  data Plugin = Plugin      { matchRegex :: String diff --git a/src/Plugin/GitHubCommit.hs b/src/Plugin/GitHubCommit.hs index 9773690..ee694b4 100644 --- a/src/Plugin/GitHubCommit.hs +++ b/src/Plugin/GitHubCommit.hs @@ -4,6 +4,8 @@ module Plugin.GitHubCommit      ( gitHubCommit      ) where +import qualified Data.Text as T +  import Database.SQLite.Simple  import Database.SQLite.Simple.FromRow  import Text.Regex.TDFA @@ -33,11 +35,12 @@ gitHubCommitAction message = do          Left "I couldn't find a repo URL for this channel. \              \Try `git remote set origin REPO_URL`."      respond ((RepoUrlRow r):_) = -        Right $ r ++ "/commits/" ++ M.text message =~ matchRegex gitHubCommit +        Right $ r `T.append` "/commits/" `T.append` T.pack ( +            (T.unpack $ M.text message) =~ matchRegex gitHubCommit)  type Id = Int -type RepoUrl = String +type RepoUrl = T.Text  -- | A type to match the database table for this plugin.  data RepoUrlRow = RepoUrlRow RepoUrl | 
