blob: d67f28eeb996a75567c64002e56052d00fd7dd8d (
plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
{-# LANGUAGE OverloadedStrings #-}
module IRC
( connectIRC
) where
import Control.Monad (sequence_)
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as B
import qualified Data.Text as T
import qualified Network.IRC.Client as IRC
import Message
import Plugin (matchPlugin, performPlugin)
import Plugin.Base (queryOnly)
connectIRC :: B.ByteString -> Int -> T.Text -> IO ()
connectIRC host port nick = do
conn <- IRC.connectWithTLS host port 0.2
let cfg = IRC.defaultIRCConf nick
let cfg' = cfg
{
IRC._eventHandlers = handlePrivmsg : IRC._eventHandlers cfg
, IRC._channels = ["#test-chan-13513"]
}
IRC.start conn cfg'
handlePrivmsg :: IRC.EventHandler s
handlePrivmsg = IRC.EventHandler
{ IRC._description = ""
, IRC._matchType = IRC.EPrivmsg
, IRC._eventFunc = \evt -> dispatchEvent evt
}
where
dispatchEvent (IRC.Event _ (IRC.User nick) (IRC.Privmsg _ (Right msg))) = do
let message = Message
{ text = msg
, channel = nick
, nick = nick
}
response <- liftIO $ privmsgFromPlugin message
case response of
Nothing -> return ()
Just r -> sequence_ r
dispatchEvent (IRC.Event
_ (IRC.Channel chan nick) (IRC.Privmsg _ (Right msg))) = do
let message = Message
{ text = msg
, channel = chan
, nick = nick
}
case messageForBot message of
Nothing -> return ()
Just message -> do
response <- liftIO $ privmsgFromPlugin message
case response of
Nothing -> return ()
Just r -> sequence_ r
privmsgFromPlugin :: Message -> IO (Maybe [IRC.StatefulIRC s ()])
privmsgFromPlugin message = do
case matchPlugin message of
Nothing -> return Nothing
Just plugin -> do
response <- liftIO $ performPlugin plugin message
return $ case response of
Left err -> Just $
[IRC.send $ IRC.Privmsg
(toChannel plugin message)
(Right err)]
Right r -> Just $
map (\r ->
IRC.send $ IRC.Privmsg
(toChannel plugin message)
(Right r) )
(splitAtNewlines $ splitLongLines r)
where
-- IRC only permits 512 bytes per line. Use less to allow for protocol
-- information that gets sent in addition to the message content.
splitLongLines txt = T.chunksOf 400 txt
splitAtNewlines lst = foldr (\s acc -> (T.lines s) ++ acc) [] lst
toChannel plugin message = case queryOnly plugin of
False -> channel message
True -> nick message
messageForBot :: Message -> Maybe Message
messageForBot m = case T.stripPrefix "sorbot: " (text m) of
Nothing -> Nothing
Just t -> Just m { text = t }
|