-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhenPlugin.hs
More file actions
49 lines (39 loc) · 1.77 KB
/
WhenPlugin.hs
File metadata and controls
49 lines (39 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE OverloadedStrings #-}
module WhenPlugin
( plugin
) where
import Text.XML.HXT.Core((>>>), (/>), runX, getChildren, deep, hasAttrValue, readString, withParseHTML, withWarnings, getText, yes, no)
import Text.HandsomeSoup(css)
import Data.List(isPrefixOf)
import Network.HTTP(simpleHTTP, getRequest, getResponseBody)
import qualified Data.Text as T(Text, isPrefixOf, append, pack)
import IrcUtilities(Plugin(Plugin), Bot(Bot), IrcMsg(Privmsg), bNick, msgTo)
import MyUtils(when)
import Redirection(unwrapRedirectFromMsg)
-- Plugin Info
plugin :: Plugin
plugin = Plugin "WhenPlugin" run helpAvailableUserCmds helpAvailableModCmds helpCmd
spotkaniaUrl = "http://wiki.hswro.org/spotkania"
-- Main run
run :: IrcMsg -> Bot -> IO [T.Text]
run (Privmsg author channel message) bot@(Bot _ config _) = when (",when" `T.isPrefixOf` message') $ do
lastDate <- fetchLastDate
return [msgTo channel target $ T.append "Najbliższe spotkanie odbedzie sie " lastDate]
where
(message', target) = unwrapRedirectFromMsg message author (bNick config)
run _ _ = return []
-- Possible refactorization: Text.HandsomeSoup.fromUrl
fetchLastDate :: IO T.Text
fetchLastDate = do
response <- simpleHTTP (getRequest spotkaniaUrl)
html <- getResponseBody response
let doc = readString [withParseHTML yes, withWarnings no] html
res <- runX $ doc >>> getChildren >>> deep (css "li") /> css "div" /> css "a" >>> hasAttrValue "title" (isPrefixOf "spotkania:") >>> getChildren >>> getText
return $ T.pack $ head res
-- Help
helpAvailableUserCmds :: [T.Text]
helpAvailableUserCmds = ["when"]
helpAvailableModCmds :: [T.Text]
helpAvailableModCmds = []
helpCmd :: T.Text -> [T.Text]
helpCmd "when" = [",when # Wyświetla datę najbliższego spotkania"]