packages feed

werewolf-slack (empty) → 0.1.0.0

raw patch · 9 files changed

+283/−0 lines, 9 filesdep +aesondep +basedep +bytestringsetup-changed

Dependencies added: aeson, base, bytestring, extra, http-client, http-client-tls, http-types, process, text, wai, warp, werewolf

Files

+ CHANGELOG.md view
@@ -0,0 +1,9 @@+# Changelog++### Upcoming++### v0.1.0.0++*Major*++* Initial version that connects Slack with the werewolf binary. ([#1](https://github.com/hjwylde/werewolf/issues/1))
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2016, Henry J. Wylde+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++* The name of werewolf-slack nor werewolf-slack's contributors may be used to+  endorse or promote products derived from this software without specific+  prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,52 @@+# werewolf-slack++[![Project Status: Wip - Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](http://www.repostatus.org/badges/latest/wip.svg)](http://www.repostatus.org/#wip)+[![Build Status](https://travis-ci.org/hjwylde/werewolf-slack.svg?branch=master)](https://travis-ci.org/hjwylde/werewolf-slack)+[![Release](https://img.shields.io/github/release/hjwylde/werewolf-slack.svg)](https://github.com/hjwylde/werewolf-slack/releases/latest)++A Slack chat client for playing [werewolf](https://github.com/hjwylde/werewolf).+The game engine is based off of the party game Mafia, also known as Werewolf.+See the [Wikipedia article](https://en.wikipedia.org/wiki/Mafia_(party_game)) for a rundown on it's+    gameplay and history.++### Game description++Long has the woods been home to wild creatures, both kind and cruel.+Most have faces and are known to the inhabitants of Fougères in Brittany, France; but no-one from+    the village has yet to lay eyes on the merciless Werewolf.++Each night Werewolves attack the village and devour the innocent.+For centuries no one knew how to fight this scourge, however recently a theory has taken ahold+    that+    maphaps the Werewolves walk among the Villagers themselves...++Objective of the Game:  +For the Loners: complete their own objective.  +For the Villagers: lynch all of the Werewolves.  +For the Werewolves: devour all of the Villagers.++### Installing++Installing werewolf-slack is easiest done using either+    [stack](https://github.com/commercialhaskell/stack) (recommended) or+    [Cabal](https://github.com/haskell/cabal).++**Using stack:**++```bash+stack install werewolf-slack werewolf+export PATH=$PATH:~/.local/bin+```++**Using Cabal:**++```bash+cabal-install werewolf-slack werewolf+export PATH=$PATH:~/.cabal/bin+```++### Usage++#### Commands++See `/werewolf help`.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,19 @@+{-|+Module      : Main++Copyright   : (c) Henry J. Wylde, 2016+License     : BSD3+Maintainer  : public@hjwylde.com+-}++module Main (+    -- * Main+    main,+) where++import System.Environment++import Werewolf.Slack.Application++main :: IO ()+main = getArgs >>= runApplication
+ app/Werewolf/Slack/Application.hs view
@@ -0,0 +1,44 @@+{-|+Module      : Werewolf.Slack.Application++Copyright   : (c) Henry J. Wylde, 2016+License     : BSD3+Maintainer  : public@hjwylde.com+-}++{-# LANGUAGE OverloadedStrings #-}++module Werewolf.Slack.Application (+    -- * Application+    runApplication,+) where++import Control.Concurrent+import Control.Monad.Extra++import qualified Data.ByteString.Char8      as BSC+import qualified Data.ByteString.Lazy.Char8 as BSLC+import           Data.Maybe++import Network.HTTP.Types       (status202, status400)+import Network.Wai              hiding (Response, requestBody, responseStatus)+import Network.Wai.Handler.Warp++import Werewolf.Slack.Werewolf++runApplication :: [String] -> IO ()+runApplication args = run port (application args)+    where+        port = 8080++application :: [String] -> Application+application args request respond = maybe failure (\action -> forkIO action >> success) mAction+    where+        failure = respond $ responseLBS status400 [] "bad request"+        success = respond $ responseLBS status202 [] (BSLC.pack $ fromJust mUserCommand)++        param name      = join . lookup name $ queryString request+        accessToken     = head args+        mUser           = BSC.unpack <$> param "user_name"+        mUserCommand    = BSC.unpack <$> param "text"+        mAction         = executeUserCommand accessToken <$> mUser <*> mUserCommand
+ app/Werewolf/Slack/Slack.hs view
@@ -0,0 +1,42 @@+{-|+Module      : Werewolf.Slack.Slack++Copyright   : (c) Henry J. Wylde, 2016+License     : BSD3+Maintainer  : public@hjwylde.com+-}++{-# LANGUAGE OverloadedStrings #-}++module Werewolf.Slack.Slack (+    -- * Slack+    notify,+) where++import Control.Monad++import           Data.Aeson+import qualified Data.Text  as T++import Network.HTTP.Client+import Network.HTTP.Client.TLS+import Network.HTTP.Types.Method++url :: String -> String+url accessToken = "https://hooks.slack.com/services/" ++ accessToken++notify :: String -> String -> String -> IO ()+notify accessToken to message = do+    manager <- newManager tlsManagerSettings++    initialRequest  <- parseUrl $ url accessToken+    let request     = initialRequest { method = methodPost, requestBody = body }++    void $ httpLbs request manager+    where+        body    = RequestBodyLBS $ encode payload+        payload = object+            [ "channel" .= to+            , "text" .= message+            , "icon_emoji" .= T.unpack ":wolf:"+            ]
+ app/Werewolf/Slack/Werewolf.hs view
@@ -0,0 +1,41 @@+{-|+Module      : Werewolf.Slack.Werewolf++Copyright   : (c) Henry J. Wylde, 2016+License     : BSD3+Maintainer  : public@hjwylde.com+-}++module Werewolf.Slack.Werewolf (+    -- * Werewolf+    executeUserCommand,+) where++import Control.Monad.Extra++import           Data.Aeson+import qualified Data.ByteString.Lazy.Char8 as BSLC+import           Data.Maybe+import qualified Data.Text                  as T++import Game.Werewolf++import System.Process++import Werewolf.Slack.Slack++executeUserCommand :: String -> String -> String -> IO ()+executeUserCommand accessToken user userCommand = do+    stdout          <- readCreateProcess (proc command arguments) ""+    let mResponse   = decode (BSLC.pack stdout) :: Maybe Response++    whenJust mResponse $ \response ->+        forM_ (messages response) $ \(Message mTo message) ->+            notify accessToken (fromMaybe channelName (T.unpack <$> mTo)) (T.unpack message)+    where+        atUser      = if take 1 user == "@" then user else '@':user+        command     = "werewolf"+        arguments   = ["--caller", atUser, "interpret", "--"] ++ words userCommand++channelName :: String+channelName = "#werewolf"
+ werewolf-slack.cabal view
@@ -0,0 +1,47 @@+name:           werewolf-slack+version:        0.1.0.0++author:         Henry J. Wylde+maintainer:     public@hjwylde.com+homepage:       https://github.com/hjwylde/werewolf-slack++synopsis:       A Slack chat client for playing werewolf (https://github.com/hjwylde/werewolf)+description:    The game engine is based off of the party game Mafia, also known as Werewolf.+                See https://github.com/hjwylde/werewolf-slack for help on running the chat client.++license:        BSD3+license-file:   LICENSE++cabal-version:  >= 1.10+category:       Game+build-type:     Simple++extra-source-files: CHANGELOG.md README.md++source-repository head+    type:       git+    location:   git@github.com:hjwylde/werewolf-slack++executable werewolf-slack+    main-is:        Main.hs+    hs-source-dirs: app/+    ghc-options:    -threaded -with-rtsopts=-N+    other-modules:+        Werewolf.Slack.Application+        Werewolf.Slack.Slack+        Werewolf.Slack.Werewolf++    default-language: Haskell2010+    build-depends:+        aeson >= 0.8 && < 0.12,+        base >= 4.8 && < 5,+        bytestring == 0.10.*,+        extra == 1.4.*,+        http-client == 0.4.*,+        http-client-tls == 0.2.*,+        http-types == 0.9.*,+        process == 1.2.*,+        text == 1.2.*,+        wai == 3.2.*,+        warp == 3.2.*,+        werewolf == 0.4.*