| 1 |
-- | Offline mode / RC file / -e support module. Handles spooling lists |
| 2 |
-- of commands (from haskeline, files, or the command line) into the vchat |
| 3 |
-- layer. |
| 4 |
module Lambdabot.Plugin.Core.OfflineRC ( offlineRCPlugin ) where |
| 5 |
|
| 6 |
import Lambdabot.Config.Core |
| 7 |
import Lambdabot.IRC |
| 8 |
import Lambdabot.Monad |
| 9 |
import Lambdabot.Plugin |
| 10 |
import Lambdabot.Util |
| 11 |
|
| 12 |
import Control.Concurrent.Lifted |
| 13 |
import Control.Exception.Lifted ( evaluate, finally ) |
| 14 |
import Control.Monad( void, when ) |
| 15 |
import Control.Monad.State( gets, modify ) |
| 16 |
import Control.Monad.Trans( lift, liftIO ) |
| 17 |
import Data.Char |
| 18 |
import qualified Data.Map as M |
| 19 |
import qualified Data.Set as S |
| 20 |
import System.Console.Haskeline (InputT, Settings(..), runInputT, defaultSettings, getInputLine) |
| 21 |
import System.IO |
| 22 |
import System.Timeout.Lifted |
| 23 |
import Codec.Binary.UTF8.String |
| 24 |
|
| 25 |
-- We need to track the number of active sourcings so that we can |
| 26 |
-- unregister the server (-> allow the bot to quit) when it is not |
| 27 |
-- being used. |
| 28 |
type OfflineRCState = Integer |
| 29 |
type OfflineRC = ModuleT OfflineRCState LB |
| 30 |
|
| 31 |
offlineRCPlugin :: Module OfflineRCState |
| 32 |
offlineRCPlugin = newModule |
| 33 |
{ moduleDefState = return 0 |
| 34 |
, moduleInit = do |
| 35 |
lb . modify $ \s -> s |
| 36 |
{ ircPrivilegedUsers = S.insert (Nick "offlinerc" "null") (ircPrivilegedUsers s) |
| 37 |
} |
| 38 |
-- note: moduleInit is invoked with exceptions masked |
| 39 |
void . forkUnmasked $ do |
| 40 |
waitForInit |
| 41 |
lockRC |
| 42 |
cmds <- getConfig onStartupCmds |
| 43 |
mapM_ feed cmds `finally` unlockRC |
| 44 |
|
| 45 |
, moduleCmds = return |
| 46 |
[ (command "offline") |
| 47 |
{ privileged = True |
| 48 |
, help = say "offline. Start a repl" |
| 49 |
, process = const . lift $ do |
| 50 |
lockRC |
| 51 |
histFile <- lb $ findLBFileForWriting "offlinerc" |
| 52 |
let settings = defaultSettings { historyFile = Just histFile } |
| 53 |
_ <- fork (runInputT settings replLoop `finally` unlockRC) |
| 54 |
return () |
| 55 |
} |
| 56 |
, (command "rc") |
| 57 |
{ privileged = True |
| 58 |
, help = say "rc name. Read a file of commands (asynchronously). TODO: better name." |
| 59 |
, process = \fn -> lift $ do |
| 60 |
txt <- io $ readFile fn |
| 61 |
io $ evaluate $ foldr seq () txt |
| 62 |
lockRC |
| 63 |
_ <- fork (mapM_ feed (lines txt) `finally` unlockRC) |
| 64 |
return () |
| 65 |
} |
| 66 |
] |
| 67 |
} |
| 68 |
|
| 69 |
feed :: String -> OfflineRC () |
| 70 |
feed msg = do |
| 71 |
cmdPrefix <- fmap head (getConfig commandPrefixes) |
| 72 |
let msg' = case msg of |
| 73 |
'>':xs -> cmdPrefix ++ "run " ++ xs |
| 74 |
'!':xs -> xs |
| 75 |
_ -> cmdPrefix ++ dropWhile (== ' ') msg |
| 76 |
-- note that `msg'` is unicode, but lambdabot wants utf-8 lists of bytes |
| 77 |
lb . void . timeout (15 * 1000 * 1000) . received $ |
| 78 |
IrcMessage { ircMsgServer = "offlinerc" |
| 79 |
, ircMsgLBName = "offline" |
| 80 |
, ircMsgPrefix = "null!n=user@null" |
| 81 |
, ircMsgCommand = "PRIVMSG" |
| 82 |
, ircMsgParams = ["offline", ":" ++ encodeString msg' ] } |
| 83 |
|
| 84 |
handleMsg :: IrcMessage -> OfflineRC () |
| 85 |
handleMsg msg = liftIO $ do |
| 86 |
let str = case (tail . ircMsgParams) msg of |
| 87 |
[] -> [] |
| 88 |
(x:_) -> tail x |
| 89 |
-- str contains utf-8 list of bytes; convert to unicode |
| 90 |
hPutStrLn stdout (decodeString str) |
| 91 |
hFlush stdout |
| 92 |
|
| 93 |
replLoop :: InputT OfflineRC () |
| 94 |
replLoop = do |
| 95 |
line <- getInputLine "lambdabot> " |
| 96 |
case line of |
| 97 |
Nothing -> return () |
| 98 |
Just x -> do |
| 99 |
let s' = dropWhile isSpace x |
| 100 |
when (not $ null s') $ do |
| 101 |
lift $ feed s' |
| 102 |
continue <- lift $ lift $ gets (M.member "offlinerc" . ircPersists) |
| 103 |
when continue replLoop |
| 104 |
|
| 105 |
lockRC :: OfflineRC () |
| 106 |
lockRC = do |
| 107 |
withMS $ \ cur writ -> do |
| 108 |
when (cur == 0) $ do |
| 109 |
registerServer "offlinerc" handleMsg |
| 110 |
lift $ modify $ \state' -> |
| 111 |
state' { ircPersists = M.insert "offlinerc" True $ ircPersists state' } |
| 112 |
writ (cur + 1) |
| 113 |
|
| 114 |
unlockRC :: OfflineRC () |
| 115 |
unlockRC = withMS $ \ cur writ -> do |
| 116 |
when (cur == 1) $ unregisterServer "offlinerc" |
| 117 |
writ (cur - 1) |