Add sample ubot plugin written in haskell
authorunc0rr
Thu, 17 Jun 2021 19:32:26 +0200
changeset 15794 7598960819a1
parent 15793 96443d9b48c9
child 15795 40929af15167
Add sample ubot plugin written in haskell
tools/ubot-plugins/hs-echo/app/Main.hs
tools/ubot-plugins/hs-echo/hs-ping.cabal
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/ubot-plugins/hs-echo/app/Main.hs	Thu Jun 17 19:32:26 2021 +0200
@@ -0,0 +1,48 @@
+{-# LANGUAGE OverloadedStrings #-}
+module Main where
+
+import Text.Megaparsec (Parsec, parseMaybe)
+import Text.URI
+import System.Environment (getEnv)
+import Data.Text (Text, pack, unpack)
+import Data.Maybe
+import Control.Monad (when)
+import Network.AMQP
+import qualified Data.ByteString.Lazy.Char8 as BL
+
+assert :: String -> Bool -> a -> a
+assert message False x = error message
+assert _ _ x = x
+
+unRpack = unpack . unRText
+
+main :: IO ()
+main = do
+    amqpUri <- getEnv "AMQP_URL"
+    let uri = fromJust $ parseMaybe (parser :: Parsec Int Text URI) $ pack amqpUri
+    when (uriScheme uri /= mkScheme "amqp") $ error "AMQP_URL environment variable scheme should be amqp"
+    let Right (Authority (Just (UserInfo username (Just password))) rHost maybePort) = uriAuthority uri
+ 
+    conn <- openConnection' (unRpack rHost) (fromInteger . toInteger $ fromMaybe 5672 maybePort) "/" (unRText username) (unRText password)
+    chan <- openChannel conn
+
+    (queueName, messageCount, consumerCount) <- declareQueue chan newQueue
+    bindQueue chan queueName "irc" "cmd.echo.hedgewars"
+
+    -- subscribe to the queue
+    consumeMsgs chan queueName Ack (myCallback chan)
+
+    getLine -- wait for keypress
+    closeConnection conn
+    putStrLn "connection closed"
+
+
+myCallback :: Channel -> (Message,Envelope) -> IO ()
+myCallback chan (msg, env) = do
+    let message = BL.tail . BL.dropWhile (/= '\n') $ msgBody msg
+    putStrLn $ "received message: " ++ (BL.unpack $ message)
+
+    publishMsg chan "irc" "say.hedgewars"
+        newMsg {msgBody = message}
+
+    ackEnv env
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/ubot-plugins/hs-echo/hs-ping.cabal	Thu Jun 17 19:32:26 2021 +0200
@@ -0,0 +1,39 @@
+cabal-version:      2.4
+name:               hs-ubot-ping-plugin
+version:            0.1.0.0
+
+-- A short (one-line) description of the package.
+-- synopsis:
+
+-- A longer description of the package.
+-- description:
+
+-- A URL where users can report bugs.
+-- bug-reports:
+
+-- The license under which the package is released.
+-- license:
+author:             Andrey Korotaev
+maintainer:         a.korotaev@hedgewars.org
+
+-- A copyright notice.
+-- copyright:
+-- category:
+extra-source-files: CHANGELOG.md
+
+executable hs-ubot-ping-plugin
+    main-is:          Main.hs
+
+    -- Modules included in this executable, other than Main.
+    -- other-modules:
+
+    -- LANGUAGE extensions used by modules in this package.
+    -- other-extensions:
+    build-depends:    base ^>=4.14.1.0,
+                      megaparsec >= 9.0,
+                      modern-uri >= 0.3,
+                      text >= 1.2,
+                      bytestring,
+                      amqp >= 0.22
+    hs-source-dirs:   app
+    default-language: Haskell2010