-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHircBot.hs
659 lines (601 loc) · 27.1 KB
/
HircBot.hs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
{-
- HircBot - Simple IRC bot in haskell.
- Copyright (C) 2008-2023 Madis Janson
-
- This file is part of HircBot.
-
- HircBot is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- HircBot is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with HircBot. If not, see <http://www.gnu.org/licenses/>.
-}
import Hirc
import Utf8Conv
import Calculator
import Data.Array (elems)
import Data.Char
import Data.Maybe
import Data.List
import Data.UnixTime
import qualified Data.Map as M
import qualified Data.ByteString.Char8 as C
import Control.Monad
import Control.Concurrent
import Control.Exception as E
import Text.Regex.Posix
import System.Environment
import System.Exit
import System.Random
import System.Posix.IO
import System.Posix.Signals
import System.Posix.Process.ByteString
import System.Posix.Types
import System.IO
data EncodingSpec = Utf8 | Latin1 | Raw
deriving Read
data EventSpec =
Send !String [C.ByteString] |
Say !C.ByteString | SayTo !C.ByteString !C.ByteString |
Join !C.ByteString | Quit !C.ByteString | Perm !C.ByteString |
IfPerm !C.ByteString [EventSpec] [EventSpec] | RandLine !String |
Exec !C.ByteString [C.ByteString] | Plugin [C.ByteString] !C.ByteString |
ExecMaxLines !Int !C.ByteString [C.ByteString] |
ExecTopic !C.ByteString !C.ByteString [C.ByteString] |
Calc !C.ByteString | Append !String !C.ByteString | Rehash |
Call !C.ByteString [C.ByteString] | Next
deriving Read
data AllowSpec = Client !Regex | Group C.ByteString
instance Read Regex where
readsPrec _ ('/':(!s)) =
let parse ('\\':'x':a:b:cs) acc =
parse cs $! chr ((ord a - 48) * 16 + ord b - 48) : acc
parse ('\\':'/':cs) acc = parse cs $! '/':acc
parse ('/':'i':cs) acc = [(regex compIgnoreCase acc, cs)]
parse ('/':cs) acc = [(regex 0 acc, cs)]
parse (c:cs) acc = parse cs $! c:acc
parse "" _ = []
regex opt s = makeRegexOpts (opt + compExtended) execBlank
$! C.reverse $! C.pack s in
parse s ""
readsPrec x (c:cs) | isSpace c = readsPrec x cs
readsPrec _ _ = []
data Config = Config {
servers :: [(String, Int, Bool)],
nick :: String,
encoding :: !EncodingSpec,
define :: [(C.ByteString, [EventSpec])],
messages :: [(Regex, [EventSpec])],
commands :: [(String, [Regex], [EventSpec])],
times :: [(Regex, String, [EventSpec])],
permits :: [(C.ByteString, [C.ByteString])],
nopermit :: [EventSpec]
} deriving Read
type ConfigPatterns = M.Map String [([Regex], [EventSpec])]
data ConfigItem =
Server String !Int |
IPv6Server String !Int |
Nick String |
Encoding !EncodingSpec |
On !Regex [EventSpec] |
Time !Regex String [EventSpec] |
Command String [Regex] [EventSpec] |
Define !C.ByteString [EventSpec] |
Permit !C.ByteString [C.ByteString] |
NoPermit [EventSpec] deriving Read
data PluginId = ExecPlugin [C.ByteString]
deriving (Show, Eq, Ord)
data PluginCmd = PluginMsg C.ByteString C.ByteString | KillPlugin
type Bot a = Irc ConfigSt a
data User = User {
rank :: !Int,
spoke :: !C.ByteString
}
data ConfigSt = ConfigSt {
raw :: !Config,
encodeInput :: String -> String,
patterns :: !ConfigPatterns,
aliasMap :: M.Map C.ByteString [EventSpec],
perms :: M.Map C.ByteString [AllowSpec],
-- Map nick (Map channel User)
users :: M.Map C.ByteString (M.Map C.ByteString User),
plugins :: M.Map PluginId (PluginCmd -> Bot ()),
topics :: M.Map C.ByteString C.ByteString,
timers :: [(Bool, Regex, C.ByteString, [EventSpec])]
}
data EventSrc = EventSrc {
channel :: Maybe C.ByteString,
from :: !C.ByteString,
prefix :: !C.ByteString
}
ioCatch :: IO a -> (IOError -> IO a) -> IO a
ioCatch = E.catch
matchRegex :: Regex -> C.ByteString -> Maybe [C.ByteString]
matchRegex re value = collect . drop 1 . elems <$> matchOnce re value
where collect ((start, len) : rest) =
C.take len (C.drop start value) : collect rest
collect [] = []
bindArg :: EventSrc -> [C.ByteString] -> C.ByteString -> Bot C.ByteString
bindArg src bindings str = C.concat <$> mapM id (format str)
where format str =
let (start, rest) = C.span (/= '$') str in
if C.null rest then
[return start]
else let !rest' = C.tail rest in
if C.null rest' then
[return start, return rest]
else case (C.head rest', C.span (/= '}') (C.tail rest')) of
(':', _) ->
return start : return (prefix src) : format (C.tail rest')
('{', (v, t)) | not (C.null t) ->
let ftail = format (C.drop 1 t)
topic channel = (fromMaybe C.empty . M.lookup channel
. topics <$> ircConfig) : ftail in
case map C.unpack $ C.words v of
["topic"] -> maybe ftail topic (channel src)
["topic", channel] -> topic (C.pack channel)
"time" : _ ->
liftIO (getUnixTime >>=
formatUnixTime (C.drop 5 v)) : ftail
_ -> return start : dollar : format rest'
_ -> case C.readInt rest' of
Just (i, r) | i >= 0 && i < length bindings ->
return start : return (bindings!!i) : format r
_ -> return start : dollar : format rest'
dollar = return $! C.singleton '$'
randLine :: String -> IO C.ByteString
randLine fn =
do l <- C.lines <$> C.readFile fn
n <- randomRIO (0, length l - 1)
parts <- format (C.copy (l !! n))
return $! C.concat parts
where format t = let (before, after) = C.span (/= '{') t
(choice, rest) = C.span (/= '}') after in
if C.null after then return [t]
else do !s <- snippet (C.tail choice) []
!r <- format (C.drop 1 rest)
return $ before : s : r
snippet t a =
let (before, after) = C.span (/= '|') t in
if C.null after then
do n <- randomRIO (0, length a)
return $! (before : a) !! n
else snippet (C.tail after) (before : a)
dropPath p = if C.null s then p else dropPath (C.tail s)
where s = C.dropWhile (/= '/') p
putLog = liftIO . putStrLn
cPutLog s l = liftIO $ C.putStrLn $ C.concat (C.pack s : l)
lower = C.map toLower
{-
- EXEC
-}
sysProcess :: Maybe Fd -> C.ByteString -> [C.ByteString]
-> IO (ProcessID, Handle)
sysProcess input prog argv =
do (rd, wd) <- createPipe
pid <- forkProcess (finally (closeFd rd >> child wd)
(exitImmediately (ExitFailure 127)))
closeFd wd
h <- fdToHandle rd
hSetEncoding h latin1
return (pid, h)
where tryClose fd = ioCatch (closeFd fd) (const (return ()))
child wd =
do dupTo wd stdOutput
case input of
Just inp -> do dupTo inp stdInput
closeFd inp
Nothing -> do dupTo wd stdError
closeFd stdInput
closeFd wd
mapM_ (tryClose . toEnum) [3 .. 255]
ioCatch (executeFile prog False argv Nothing)
(\e -> when (input /= Nothing)
(fdWrite stdError (show e ++ "\n") >> return ()))
fdWrite stdOutput "dead plugin walking"
return ()
readInput :: Handle -> (IO () -> C.ByteString -> IO ()) -> IO () -> IO ()
readInput h f cleanup = ioCatch copy (\_ -> hClose h >> cleanup)
where copy = C.hGetLine h >>= f copy
execSys :: C.ByteString -> (IO () -> C.ByteString -> Bot ())
-> C.ByteString -> [C.ByteString] -> Bot ()
execSys to handler prog argv =
do unlift <- escape
liftIO $ do (pid, h) <- sysProcess Nothing prog argv
let reader cont line = unlift $ handler cont line
forkIO $ readInput h reader (return ())
forkIO $ guard (unlift . say to) pid
return ()
where kill sig pid next =
do dead <- getProcessStatus False False pid
when (dead == Nothing) $
do signalProcess sig pid
next
guard sayTo pid =
do threadDelay 30000000
kill softwareTermination pid $
do sayTo (C.append (C.pack "Terminated ") (dropPath prog))
threadDelay 1000000
kill killProcess pid
(getProcessStatus True False pid >> return ())
execToSay :: C.ByteString -> Int -> C.ByteString -> [C.ByteString] -> Bot ()
execToSay to maxLines prog argv =
do v <- liftIO $ newMVar maxLines
let filter l n = let r = take n l in return (n - length r, r)
sayN continue s =
do say' (liftIO . modifyMVar v . filter) to s
liftIO $ withMVar v (return . (> 0)) >>= (`when` continue)
execSys to sayN prog argv
{-
- SEEN
-}
getUserMap :: C.ByteString -> Bot (M.Map C.ByteString User)
getUserMap nick = fromMaybe M.empty . M.lookup (lower nick) . users <$> ircConfig
getUser :: C.ByteString -> C.ByteString -> Bot (Maybe User)
getUser channel nick = M.lookup channel <$> getUserMap nick
updateUserMap :: (M.Map C.ByteString User -> M.Map C.ByteString User)
-> C.ByteString -> Bot ()
updateUserMap f nick =
ircModifyConfig $ \cfg ->
let !users' = M.alter update (lower nick) (users cfg) in
return cfg { users = users' }
where update user = let !m = f (fromMaybe M.empty user) in
if M.null m then Nothing else Just m
updateUser :: (Maybe User -> Maybe User)
-> C.ByteString -> C.ByteString -> Bot ()
updateUser f !channel nick = updateUserMap (M.alter f channel) nick
updateRank :: (Int -> Int) -> C.ByteString -> C.ByteString -> Bot ()
updateRank f channel nick = updateUser update channel nick
where update u = let !r = f (maybe 0 rank u)
!s = maybe C.empty spoke u in
if r == 0 && C.null s then emptyUser
else Just (User {rank = r, spoke = s})
seenMsg (Just channel) nick !said = updateUser update channel nick
where update u = let !r = maybe 0 rank u in
Just (User {rank = r, spoke = said})
seenMsg Nothing _ _ = return () -- private message
emptyUser = Just (User { rank = 0, spoke = C.empty })
-- XXX sharing seen.dat between channels is probably stupid, but whatever
appendSeen :: [(C.ByteString, Bool)] -> C.ByteString -> Bot ()
appendSeen nicks channel =
do UnixTime t _ <- liftIO getUnixTime
mapM (format (show t)) nicks >>=
liftIO . C.appendFile "seen.dat" . C.unlines
where clear True (Just u) | rank u /= 0 = Just $ u {spoke = C.empty}
clear True _ = emptyUser -- QUIT needs it
clear _ _ = Nothing
format t (nick, alive) =
do user <- getUser channel nick
updateUser (clear alive) channel nick
let said = maybe C.empty spoke user
return $! C.concat
[nick, C.pack ('\t':(if alive then '+':t else t)), tab, said]
tab = C.singleton '\t'
appendSeen' :: C.ByteString -> Bool -> C.ByteString -> Bot ()
appendSeen' nick alive channel = appendSeen [(nick, alive)] channel
seenEvent :: String -> C.ByteString -> [C.ByteString] -> Bot ()
seenEvent "JOIN" nick (channel:_) = appendSeen' nick True channel
seenEvent "PART" nick (channel:_) = appendSeen' nick False channel
seenEvent "KICK" _ (channel:nick:_) = appendSeen' nick False channel
seenEvent "QUIT" nick _ =
getUserMap nick >>= mapM_ (appendSeen [(nick, False)]) . M.keys
seenEvent "NICK" old (new:_) =
do user <- getUserMap old
mapM_ (appendSeen [(old, False), (new, True)]) (M.keys user)
updateUserMap (const user) new
seenEvent "TOPIC" _ (channel:topic:_) = setTopic channel topic
seenEvent "332" _ (_:channel:topic:_) = setTopic channel topic
seenEvent "331" _ (_:channel:_) = setTopic channel C.empty
seenEvent "366" _ (_:channel:_) = ircCmd "TOPIC" channel -- End of NAMES list
seenEvent "353" _ args =
do appendSeen (map stripTag nameList) channel
mapM_ checkMode nameList
where stripTag s =
(if not (C.null s) && C.head s `elem` " +@%" then
C.tail s else s, True)
modeRank '@' = 3
modeRank '%' = 2
modeRank '+' = 1
modeRank _ = 0
checkMode s | C.null s = return ()
checkMode s = updateRank (\_ -> modeRank $ C.head s) channel (C.tail s)
(names:channel:_) = reverse args
nameList = C.words names
-- track mode changes for maintaining ranks
seenEvent "MODE" _ (channel:m:args') = modes False (C.unpack m) args'
where modes _ ('+':m) args = modes True m args
modes _ ('-':m) args = modes False m args
modes set (c:m) args = mode set c args >>= modes set m
modes _ _ _ = return ()
mode _ c (_:args) | elem c "belkIR" = return $! args
mode set c args'@(who:args) =
case elemIndex c "vho" of
Just rank -> do setRank who (if set then rank + 1 else 0)
return $! args
Nothing -> return $! args'
mode _ _ _ = return []
setRank who rank' =
do updateRank (if rank' == 0 then const 0 else max rank')
channel who
user <- getUser channel who
cPutLog "setRank" [channel, C.singleton ' ', who,
C.pack $ " = " ++ show (maybe 0 rank user)]
seenEvent "TERMINATE" _ _ = killPlugins
seenEvent _ _ _ = return ()
setTopic !channel !topic =
ircModifyConfig $ \cfg ->
return cfg { topics = if C.null topic then M.delete channel (topics cfg)
else M.insert channel topic (topics cfg) }
{-
- PLUGIN
-}
removePlugin id =
ircModifyConfig $ \cfg ->
return cfg { plugins = M.delete id (plugins cfg) }
killPlugins = ircConfig >>= mapM_ ($! KillPlugin) . M.elems . plugins
startPlugin :: PluginId -> C.ByteString -> Bot (PluginCmd -> Bot ())
startPlugin id@(ExecPlugin (prog:argv)) replyTo =
do to <- liftIO $ newMVar replyTo
unlift <- escape
(pid, inp, out) <- liftIO $
do (inputRd, inputWd) <- createPipe
(pid, fd) <- sysProcess (Just inputRd) prog argv
closeFd inputRd
h <- fdToHandle inputWd
hSetEncoding h latin1
hSetBuffering h LineBuffering
return (pid, h, fd)
let sayTo cont s = readMVar to >>= unlift . (`say` s) >> cont
output = readInput out sayTo $
do unlift (removePlugin id)
hClose inp
getProcessStatus True False pid
return ()
kill = removePlugin id >>
liftIO (signalProcess softwareTermination pid)
handler KillPlugin = kill
handler (PluginMsg replyTo msg) = liftIO $ ioCatch
(swapMVar to replyTo >> C.hPutStrLn inp msg)
(\e -> putStrLn (show id ++ ": " ++ show e) >>
unlift (kill >> say replyTo ghost))
liftIO $ forkIO $ output
return handler
where ghost = C.pack "\^AACTION has a ghost plugin\^A"
startPlugin id _ = fail ("Illegal plugin id: " ++ show id)
invokePlugin :: PluginId -> C.ByteString -> C.ByteString -> Bot ()
invokePlugin id to msg =
ircConfig >>= maybe start ($! PluginMsg to msg) . M.lookup id . plugins
where start = do p <- startPlugin id to
ircModifyConfig $ \cfg ->
return cfg { plugins = M.insert id p (plugins cfg) }
p (PluginMsg to msg)
{-
- CORE
-}
checkPerm :: EventSrc -> C.ByteString -> Bot Bool
checkPerm src perm =
do cfg <- ircConfig
let hasPerm perm =
case if C.length perm == 1 then C.head perm else ' ' of
'+' -> hasRank 1
'%' -> hasRank 2
'@' -> hasRank 3
_ -> anyPerm $! concat $! maybeToList $! M.lookup perm (perms cfg)
anyPerm (perm:rest) =
do ok <- case perm of
Client re -> return $! (matchOnce re (prefix src)) /= Nothing
Group group -> hasPerm group
if ok then return True else anyPerm rest
anyPerm [] = return False
hasRank expectRank =
-- It's idiotic to give perm based on rank on any channel,
-- but I don't know a better solution now for private chat
case channel src of
Nothing -> any (\u -> rank u >= expectRank) . M.elems
<$> getUserMap (from src)
Just ch -> maybe False ((>= expectRank) . rank)
<$> getUser ch (from src)
hasPerm perm
executeEvent :: EventSrc -> (C.ByteString -> Bot C.ByteString) -> EventSpec -> Bot ()
executeEvent src param event =
case event of
Send evCmd evArg -> mapM param evArg >>= ircSend C.empty evCmd
Say text -> param text >>= mapM_ reply . C.lines
SayTo to text -> do to' <- param to
param text >>= mapM_ (say to') . C.lines
Call alias args ->
let exec event =
if null args then execute param event
else do args' <- mapM param args
execute (bindArg src (from src : args')) event in
ircConfig >>= maybe (error (C.unpack alias ++ " undefined"))
(mapM_ exec) . M.lookup alias . aliasMap
Perm perm -> do ok <- checkPerm src perm
unless ok (fail "NOPERM")
IfPerm perm events evElse ->
do ok <- checkPerm src perm
mapM_ (execute param) (if ok then events else evElse)
Join channel -> do ch <- param channel
ircSend C.empty "JOIN" [ch]
Quit msg -> param msg >>= quitIrc
RandLine fn -> liftIO (randLine fn) >>= param >>= reply
Calc text -> do let reply' = reply . C.pack
textParam <- param text
ircCatch (reply' $ calc $ C.unpack $ textParam) reply'
Rehash -> killPlugins >> ircModifyConfig getConfig
Exec prg args -> mapM param args >>= execToSay replyTo 50 prg
ExecMaxLines limit prg args ->
mapM param args >>= execToSay replyTo limit prg
ExecTopic channel prog args ->
let setTopic _ topic =
do current <- M.lookup channel . topics <$> ircConfig
when (Just topic /= current)
(ircSend C.empty "TOPIC" [channel, topic]) in
mapM param args >>= execSys channel setTopic prog
Plugin prg cmd -> param cmd >>= invokePlugin (ExecPlugin prg) replyTo
Append file str -> param str >>= liftIO . C.appendFile file
Next -> return ()
where replyTo = fromMaybe (from src) (channel src)
reply = say replyTo
execute = executeEvent src
-- wrapper that encodes irc input into desired charset
bot :: (C.ByteString, String, [C.ByteString]) -> Bot ()
bot (prefix, cmd, args) =
do cfg <- ircConfig
bot' (prefix, cmd, map (C.pack . encodeInput cfg . C.unpack) args)
bot' :: (C.ByteString, String, [C.ByteString]) -> Bot ()
bot' msg@(msgPrefix, cmd, args) =
ircCatch (seenEvent cmd (from src) args)
(putLog . (("seenEvent " ++ cmd ++ ": ") ++) . show) >>
ircCatch (ircConfig >>= maybe (liftIO $ print msg) doMatch
. M.lookup cmd . patterns) atErr
where doMatch ((argPattern, events):rest) =
if length args < length argPattern then doMatch rest
else maybe (doMatch rest) (\p -> do
let param = bindArg src (from src : (concat p) ++ [from src])
mapM_ (executeEvent src param) events
let isNext Next = True
isNext _ = False
when (any isNext events) (doMatch rest))
(sequence $ zipWith matchRegex argPattern args)
doMatch [] = do let what = last args
when (cmd == "PRIVMSG")
(seenMsg (channel src) (from src) $! what)
when (args /= [] && C.isPrefixOf (C.singleton '!') what)
(cPutLog "NOMATCH " [showMsg msg])
atErr "NOPERM" = ircConfig >>=
mapM_ (executeEvent src $! bindArg src [from src, from src])
. nopermit . raw
atErr str = putLog str >> ircSend (from src) "NOTICE" [from src, C.pack str]
src = EventSrc {
prefix = msgPrefix,
channel = case args of
s : _ | not (C.null s) && C.head s == '#' -> Just s
_ -> Nothing,
from = C.takeWhile (/= '!') msgPrefix
}
botTicker :: Bot ()
botTicker = do timeStr <- liftIO (getUnixTime >>= formatUnixTime timeFormat)
activity <- ircConfig >>= mapM (run timeStr) . timers
ircModifyConfig (\cfg ->
return cfg { timers = merge activity (timers cfg) })
where merge (active : activity) ((_, re, from, events) : rest) =
(active, re, from, events) : merge activity rest
merge _ timers = timers
run timeStr (wasActive, re, from, events) =
case matchRegex re timeStr of
Just param | not wasActive -> do
let src = source from
mapM_ (executeEvent src (bindArg src param)) events
return True
match -> return $ match /= Nothing
timeFormat = C.pack "%FT%H:%M"
source from =
let channel = if C.head from == '#' then Just from else Nothing in
EventSrc { prefix = C.empty, channel = channel, from = from }
parseConfigItems :: String -> [ConfigItem]
parseConfigItems str = skip parse 1 str
where skip _ line ('\n':s) = skip parse (line + 1) s
skip tr line (c:s) | isSpace c = skip tr line s
skip _ _ "" = []
skip tr line str = tr line str
parse line str = case reads str of
((result, tail):_) ->
let len = length str - length tail in
let !nl = line + length (filter (== '\n') (take len str)) in
result : skip err nl tail
_ -> error (show line ++ ": syntax error")
err line _ = error (show line ++ ": expected newline after definition")
collectConfig :: [ConfigItem] -> Config -> Config
collectConfig (item : items) cfg =
collectConfig items $ case item of
(Server host port) -> cfg { servers = (host, port, False) : servers cfg }
(IPv6Server host port) -> cfg { servers = (host, port, True) : servers cfg }
(Nick nick) -> cfg { nick = nick }
(Encoding spec) -> cfg { encoding = spec }
(On re events) -> cfg { messages = (re, events) : messages cfg }
(Time re from events) -> cfg { times = (re, from, events) : times cfg }
(Command cmd re events) ->
cfg { commands = (cmd, re, events) : commands cfg }
(Define name events) -> cfg { define = (name, events) : define cfg }
(Permit name masks) -> cfg { permits = (name, masks) : permits cfg }
(NoPermit events) -> cfg { nopermit = events ++ nopermit cfg }
collectConfig [] cfg = cfg
createPatterns :: Config -> ConfigPatterns
createPatterns cfg = foldr addCmd M.empty
(commands cfg ++
map (\(pattern, event) -> ("PRIVMSG", [emptyRegex, pattern], event))
(messages cfg))
where addCmd (cmd, args, event) = let bind = (args, event) in
M.alter (Just . maybe [bind] (bind:)) cmd
emptyRegex = makeRegex ""
readConfig :: IO Config
readConfig =
do args <- getArgs
s <- C.readFile (fromMaybe "hircrc" $ listToMaybe args)
let !cfg = parse $! C.unpack $! rmComments s
if null (servers cfg)
then fail "No servers defined in configuration"
else return $! cfg
where rmComments = C.unlines . filter notComment . C.lines
notComment s = let s' = C.dropWhile isSpace s in
C.null s' || C.head s' /= '#'
parse str | "Config " `isPrefixOf` dropWhile isSpace str = read str
parse str = collectConfig (reverse $ parseConfigItems str) Config {
servers = [], nick = "HircB0t", encoding = Utf8, define = [],
messages = [], commands = [], times = [], permits = [], nopermit = []
}
initConfig !users !topics !cfg =
do return $! ConfigSt {
raw = cfg,
encodeInput = case encoding cfg of
Utf8 -> utf8Encode
Latin1 -> utf8Decode
Raw -> id,
patterns = createPatterns cfg,
aliasMap = M.fromList (define cfg),
perms = foldr addPerm M.empty (permits cfg),
plugins = M.empty,
users = users,
topics = topics,
timers = map (\(re, from, events) ->
(False, re, C.pack from, events)) (times cfg)
}
where addPerm (perm, users) = let perms = map getPerm users in
M.alter (Just . maybe perms (perms ++)) perm
getPerm user =
if not (C.null user) && C.head user == ':' then Group (C.tail user)
else Client (makeRegex (permPattern user))
permPattern s = C.concat $! C.singleton '^' : escapePerm s
escapePerm = tr . C.break (\c -> c == '*' || c == '.')
tr (!h, t) | C.null t = [h, C.singleton '$']
tr (h, t) = h : (case C.head t of
'*' -> aster
'.' -> dot
_ -> C.empty) : escapePerm (C.tail t)
aster = C.pack ".*"
dot = C.pack "\\."
getConfig st = readConfig >>= initConfig (users st) (topics st)
main =
do initEnv
hSetBuffering stdout LineBuffering
readConfig >>= connect 0
where connect !nth !cfg =
do let servers' = servers cfg
server = servers' !! (nth `mod` length servers')
config <- initConfig M.empty M.empty cfg >>= newMVar
appendFile "seen.dat" "\n"
ioCatch (connectIrc server (nick cfg) bot botTicker config)
(failed nth config)
failed !nth !config ex =
do putStrLn ("Reconnect after 1 min: Error occured: " ++ show ex)
threadDelay 60000000
putStrLn "Reconnecting..."
readMVar config >>= connect (nth + 1) . raw