Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of ssh://github.com/Detegr/HBot

  • Loading branch information...
commit d48f167c436a177bc51b29040c2d2fe9fe0ffe71 2 parents 9d03f7f + 0293f30
Antti Keränen authored
2  Makefile
View
@@ -1,6 +1,6 @@
SRC=HBot
CSRC=Config/config.c
-PLUGINNAMES=Admin Wikla Unicafe Random Random3 Lanit Arabianranta Util/Random Util/DatabasePath UrlAnalyzer Help Logger
+PLUGINNAMES=Admin Wikla Unicafe Random Random3 Lanit Arabianranta Util/Random Util/DatabasePath UrlAnalyzer Help Logger Trivia
PLUGINS=$(addprefix Plugin/,$(PLUGINNAMES))
PLUGINOBJECTS=$(addsuffix .o,$(PLUGINS))
COBJECTS=$(CSRC:.c=.o)
86 Plugin/Btc.hs
View
@@ -0,0 +1,86 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Plugin.Btc(btc) where
+
+import qualified Data.ByteString.Char8 as C
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as L
+import Network.HTTP.Conduit
+import Data.Aeson
+import Data.Aeson.Types (Parser)
+import Control.Applicative
+import qualified Data.Text as T
+import Safe (readMay)
+import Text.Printf
+import PluginData
+
+data BtcData = BtcData
+ { high :: Double
+ , last :: Double
+ , timestamp :: Int
+ , bid :: Double
+ , volume :: Double
+ , low :: Double
+ , ask :: Double
+ } deriving Show
+
+data ConversionData = ConversionData
+ { sell :: Double
+ , buy :: Double
+ } deriving Show
+
+readField :: (Read a) => Object -> T.Text -> Parser a
+readField o f = do
+ v <- o .: f
+ case readMay (T.unpack v) of
+ Nothing -> fail"Bad field: " ++ T.unpack f
+ Just r -> return r
+
+instance FromJSON BtcData where
+ parseJSON (Object o) = do
+ BtcData <$> readField o "high" <*>
+ readField o "last" <*>
+ readField o "timestamp" <*>
+ readField o "bid" <*>
+ readField o "volume" <*>
+ readField o "low" <*>
+ readField o "ask"
+
+instance FromJSON ConversionData where
+ parseJSON (Object o) = do
+ ConversionData <$> readField o "sell" <*>
+ readField o "buy"
+
+valueStr :: String
+valueStr = "http://www.bitstamp.net/api/ticker/"
+
+conversionStr :: String
+conversionStr = "https://www.bitstamp.net/api/eur_usd/"
+
+value :: IO L.ByteString
+value = simpleHttp valueStr
+
+conversion :: IO L.ByteString
+conversion = simpleHttp conversionStr
+
+parseBtc :: L.ByteString -> Either String BtcData
+parseBtc = eitherDecode'
+
+parseRate :: L.ByteString -> Either String ConversionData
+parseRate = eitherDecode'
+
+btcStr :: BtcData -> ConversionData -> String
+btcStr b c = "High: $" ++ (printf "%.2f" $ high b) ++ (" (" ++ (printf "%.2f" $ high b / buy c) ++ "e)") ++
+ ", Low: $" ++ (printf "%.2f" $ low b) ++ (" (" ++ (printf "%.2f" $ low b / buy c) ++ "e)") ++
+ ", Last: $" ++ (printf "%.2f"Plugin.Btc.last b) ++ (" (" ++ (printf "%.2f" $ Plugin.Btc.last b / buy c) ++ "e)")
+
+btc :: PluginData a -> IO (PluginResult a)
+btc pd = do
+ btc <- fmap parseBtc value
+ rate <- fmap parseRate conversion
+ case rate of
+ Left err -> msgToChannel pd err
+ Right rate ->
+ case btc of
+ Left err -> msgToChannel pd err
+ Right btc -> msgToChannel pd $ btcStr btc rate
46 Plugin/Trivia.hs
View
@@ -0,0 +1,46 @@
+module Plugin.Util.Trivia(trivia) where
+
+import PluginData
+import Plugin.Util.Random
+import Data.Char (toLower)
+
+parseUserAndMessage :: String -> (String, String)
+parseUserAndMessage s = (parseUser s, parseMessage s)
+
+parseMessage :: String -> String
+parseMessage = drop 2 . dropWhile (/= '>')
+
+parseUser :: String -> String
+parseUser = takeWhile (/= '>') . tail . dropWhile (/= '<')
+
+question :: String
+question = "Guess the author of this line: "
+
+correct :: String -> String -> String
+correct w u = "The winner is " ++ w ++ ", that line was written by " ++ u ++ "."
+
+incorrect :: String
+incorrect = "Nope."
+
+cancel :: String -> String
+cancel u = "Cancelling trivia, the correct answer was: " ++ u
+
+trivia :: PluginData (String, String) -> IO (PluginResult (String, String))
+trivia pd = do
+ if (head $ head (params pd)) /= '#'
+ then msgTo (getNick pd) "Dude, leave me alone!" (state pd)
+ else do
+ let a = arguments pd
+ case (state pd) of
+ Just (u,msg) ->
+ if length a > 0
+ then if (map toLower (head a) == map toLower u)
+ then msgToChannel' pd (correct (getNick pd) u) Nothing
+ else msgToChannel' pd incorrect (state pd)
+ else msgToChannel' pd (cancel u) Nothing
+ Nothing ->
+ if length a > 0
+ then msgToChannel' pd "Launch trivia without arguments." Nothing
+ else do
+ (u,msg) <- fmap parseUserAndMessage $ getRandom Nothing
+ msgToChannel' pd (question ++ msg) $ Just (u,msg)
3  Plugin/UrlAnalyzer.hs
View
@@ -12,6 +12,7 @@ import Data.Maybe(catMaybes)
import Control.Exception(try,SomeException)
import Network.URI
import Data.List (isPrefixOf)
+import Data.String.Utils (strip)
import PluginData
@@ -26,7 +27,7 @@ getTitle uri = do
parseTagsT . B.concat . L.toChunks $ src)
if mbtitle == []
then return "No title"
- else return $ "Title: " ++ (take 500 . head $ mbtitle)
+ else return $ "Title: " ++ (strip . take 500 . head $ mbtitle)
analyzeUrl :: PluginData a -> IO (PluginResult a)
analyzeUrl pd = do
Please sign in to comment.
Something went wrong with that request. Please try again.