diff options
| author | Teddy Wing | 2017-09-06 23:27:45 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-09-06 23:27:45 +0200 | 
| commit | 712dca5f860529c33ad19e7babf5c471181e2573 (patch) | |
| tree | 35b0c86ca986d2526676a74e96e8a1a82e16a4c0 | |
| parent | 8e5b862c9171af8af7c6c0d0456ae67a9e737633 (diff) | |
| download | sorbot-712dca5f860529c33ad19e7babf5c471181e2573.tar.bz2 | |
tmp; fix/rewordtmp
| -rw-r--r-- | sorbot.cabal | 2 | ||||
| -rw-r--r-- | src/Bot.hs | 15 | ||||
| -rw-r--r-- | src/I18n.hs | 6 | ||||
| -rw-r--r-- | src/Plugin/Base.hs | 7 | ||||
| -rw-r--r-- | src/Plugin/GitHubCommit.hs | 42 | 
5 files changed, 56 insertions, 16 deletions
| diff --git a/sorbot.cabal b/sorbot.cabal index 6761349..26ecb20 100644 --- a/sorbot.cabal +++ b/sorbot.cabal @@ -16,6 +16,7 @@ cabal-version:       >=1.10  library    hs-source-dirs:      src    exposed-modules:     Lib +                     , Bot                       , CliOptions                       , I18n                       , IRC @@ -30,6 +31,7 @@ library    build-depends:       base >= 4.7 && < 5                       , bytestring                       , irc-client +                     , mtl                       , optparse-applicative                       , regex-tdfa                       , sqlite-simple diff --git a/src/Bot.hs b/src/Bot.hs new file mode 100644 index 0000000..a661ea4 --- /dev/null +++ b/src/Bot.hs @@ -0,0 +1,15 @@ +-- {-# LANGUAGE ConstraintKinds #-} +-- {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} + +module Bot +    ( Bot(..) +    ) where + +import Control.Monad.Reader + +import CliOptions (Options) + +newtype Bot a = Bot +    { runBot :: ReaderT Options IO a +    } deriving (Monad, Functor, Applicative, MonadIO) diff --git a/src/I18n.hs b/src/I18n.hs index f7a4571..e587648 100644 --- a/src/I18n.hs +++ b/src/I18n.hs @@ -12,16 +12,20 @@ import qualified Data.Text as T  data Locale = EN | FR deriving (Show)  data Message -    = GitHubCommitRepoURLNotFound +    = GitHubCommitDescription +    | GitHubCommitRepoURLNotFound      | GitRemoteSetOriginUpdatedRepoURL T.Text  translate_en_US :: Message -> T.Text +translate_en_US GitHubCommitDescription = "Generate a commit URL based on the given SHA."  translate_en_US GitHubCommitRepoURLNotFound = "I couldn't find a repo URL for \      \this channel. Try `git remote set origin REPO_URL`."  translate_en_US (GitRemoteSetOriginUpdatedRepoURL url) =      "I updated the channel's repo URL to '" `T.append` url `T.append` "'."  translate_fr_FR :: Message -> T.Text +-- TODO: translate +translate_fr_FR GitHubCommitDescription = "Generate a commit URL based on the given SHA."  translate_fr_FR GitHubCommitRepoURLNotFound = "Je n'ai pas trouvé une URL de \      \repo pour ce channel. Essaye `git remote set origin REPO_URL`."  translate_fr_FR (GitRemoteSetOriginUpdatedRepoURL url) = diff --git a/src/Plugin/Base.hs b/src/Plugin/Base.hs index d4194ee..1ef4cd2 100644 --- a/src/Plugin/Base.hs +++ b/src/Plugin/Base.hs @@ -7,13 +7,18 @@ module Plugin.Base      , defaultPlugin      ) where +import Control.Monad.Trans.Class (lift)  import qualified Data.Text as T  import Database.SQLite.Simple +-- import Config (Config) +import Bot (Bot(Bot))  import Message -type PluginAction = Message -> IO (Either T.Text T.Text) +type PluginAction = Message -> Bot (Either T.Text T.Text) + +-- newtype Foo = Config Plugin  data Plugin = Plugin      { matchRegex  :: String diff --git a/src/Plugin/GitHubCommit.hs b/src/Plugin/GitHubCommit.hs index ae9d68f..454e838 100644 --- a/src/Plugin/GitHubCommit.hs +++ b/src/Plugin/GitHubCommit.hs @@ -4,43 +4,57 @@ module Plugin.GitHubCommit      ( gitHubCommit      ) where +import Control.Monad.IO.Class (liftIO) +import Control.Monad.Trans.Class (lift) +import Control.Monad.Trans.Reader (ask)  import qualified Data.Text as T  import Database.SQLite.Simple  import Database.SQLite.Simple.FromRow  import Text.Regex.TDFA +-- import Config (Config(..)) +import Bot (Bot, runBot)  import I18n  import qualified Message as M  import qualified CliOptions as Cli (lang)  import Plugin.Base -gitHubCommit = defaultPlugin -    { matchRegex  = "^[0-9a-f]{40}$" -    , perform     = gitHubCommitAction -    , command     = "<git_sha>" -    , description = "Generate a commit URL based on the given SHA." -    } +gitHubCommit :: Bot Plugin +gitHubCommit = do +    cfg <- ask +    return $ defaultPlugin +        { matchRegex  = "^[0-9a-f]{40}$" +        , perform     = gitHubCommitAction +        , command     = "<git_sha>" +        , description = translate (lang cfg) GitHubCommitDescription +            -- "Generate a commit URL based on the given SHA." +        }  gitHubCommitAction :: PluginAction  gitHubCommitAction message = do -    dbConn <- open "db/sorbot_development.sqlite3" -    rs <- query dbConn "SELECT repo_url \ +    dbConn <- liftIO $ open "db/sorbot_development.sqlite3" +    rs <- liftIO $ query dbConn "SELECT repo_url \          \ FROM plugin_github_commit_channel_repo_urls \          \ WHERE channel = ? \          \ LIMIT 1"          (Only (M.channel message)) -        :: IO [RepoUrlRow] -    close dbConn +        :: Bot [RepoUrlRow] +    liftIO $ close dbConn -    respond rs +    liftIO $ respond rs    where +    respond :: Bot (Either T.Text T.Text)      respond [] = do          lang <- Cli.lang -        return $ Left $ translate lang GitHubCommitRepoURLNotFound -    respond ((RepoUrlRow r):_) = +        -- TODO: remove need for `lang` +        return $ Left $ translate (lang cfg) GitHubCommitRepoURLNotFound +    respond ((RepoUrlRow r):_) = do +        -- bot <- ask +        -- plugin <- runBot bot >>= gitHubCommit +        plugin <- gitHubCommit          return $ Right $ r `T.append` "/commits/" `T.append` T.pack ( -            M.textStr message =~ matchRegex gitHubCommit) +            M.textStr message =~ matchRegex plugin)  type RepoUrl = T.Text | 
