| 1 |
-- | System module : IRC control functions |
| 2 |
module Lambdabot.Plugin.Core.System (systemPlugin) where |
| 3 |
|
| 4 |
import Lambdabot.Bot |
| 5 |
import Lambdabot.Compat.AltTime |
| 6 |
import Lambdabot.Compat.FreenodeNick |
| 7 |
import Lambdabot.IRC |
| 8 |
import Lambdabot.Module |
| 9 |
import Lambdabot.Monad |
| 10 |
import Lambdabot.Plugin |
| 11 |
import Lambdabot.Util |
| 12 |
|
| 13 |
import Control.Monad.Reader |
| 14 |
import Control.Monad.State (gets, modify) |
| 15 |
import qualified Data.Map as M |
| 16 |
import qualified Data.Set as S |
| 17 |
|
| 18 |
type SystemState = (ClockTime, TimeDiff) |
| 19 |
type System = ModuleT SystemState LB |
| 20 |
|
| 21 |
systemPlugin :: Module SystemState |
| 22 |
systemPlugin = newModule |
| 23 |
{ moduleDefState = flip (,) noTimeDiff `fmap` io getClockTime |
| 24 |
, moduleSerialize = Just stdSerial |
| 25 |
|
| 26 |
, moduleInit = do |
| 27 |
(_, d) <- readMS |
| 28 |
t <- io getClockTime |
| 29 |
writeMS (t, d) |
| 30 |
, moduleExit = do |
| 31 |
(initial, d) <- readMS |
| 32 |
now <- liftIO getClockTime |
| 33 |
writeMS (initial, max d (diffClockTimes now initial)) |
| 34 |
|
| 35 |
, moduleCmds = return $ |
| 36 |
[ (command "listchans") |
| 37 |
{ help = say "Show channels bot has joined" |
| 38 |
, process = \_ -> listKeys (M.mapKeysMonotonic (FreenodeNick . getCN) . ircChannels) |
| 39 |
} |
| 40 |
, (command "listmodules") |
| 41 |
{ help = say "listmodules. Show available plugins" |
| 42 |
, process = \_ -> say . showClean =<< lb listModules |
| 43 |
} |
| 44 |
, (command "listservers") |
| 45 |
{ help = say "listservers. Show current servers" |
| 46 |
, process = \_ -> listKeys ircServerMap |
| 47 |
} |
| 48 |
, (command "list") |
| 49 |
{ help = say "list [module|command]. Show commands for [module] or the module providing [command]." |
| 50 |
, process = doList |
| 51 |
} |
| 52 |
, (command "echo") |
| 53 |
{ help = say "echo <msg>. echo irc protocol string" |
| 54 |
, process = doEcho |
| 55 |
} |
| 56 |
, (command "uptime") |
| 57 |
{ help = say "uptime. Show uptime" |
| 58 |
, process = \_ -> do |
| 59 |
(uptime, maxUptime) <- lift getUptime |
| 60 |
say ("uptime: " ++ timeDiffPretty uptime ++ |
| 61 |
", longest uptime: " ++ timeDiffPretty maxUptime) |
| 62 |
} |
| 63 |
|
| 64 |
, (command "listall") |
| 65 |
{ privileged = True |
| 66 |
, help = say "list all commands" |
| 67 |
, process = \_ -> mapM_ doList =<< lb listModules |
| 68 |
} |
| 69 |
, (command "join") |
| 70 |
{ privileged = True |
| 71 |
, help = say "join <channel>" |
| 72 |
, process = \rest -> do |
| 73 |
chan <- readNick rest |
| 74 |
lb $ send (joinChannel chan) |
| 75 |
} |
| 76 |
, (command "part") |
| 77 |
{ privileged = True |
| 78 |
, help = say "part <channel>" |
| 79 |
, aliases = ["leave"] |
| 80 |
, process = \rest -> do |
| 81 |
chan <- readNick rest |
| 82 |
lb $ send (partChannel chan) |
| 83 |
} |
| 84 |
, (command "msg") |
| 85 |
{ privileged = True |
| 86 |
, help = say "msg <nick or channel> <msg>" |
| 87 |
, process = \rest -> do |
| 88 |
-- writes to another location: |
| 89 |
let (tgt, txt) = splitFirstWord rest |
| 90 |
tgtNick <- readNick tgt |
| 91 |
lb $ ircPrivmsg tgtNick txt |
| 92 |
} |
| 93 |
, (command "codepage") |
| 94 |
{ privileged = True |
| 95 |
, help = say "codepage <server> <CP-name>" |
| 96 |
, process = \rest -> do |
| 97 |
let (server, cp) = splitFirstWord rest |
| 98 |
lb $ ircCodepage server cp |
| 99 |
} |
| 100 |
, (command "quit") |
| 101 |
{ privileged = True |
| 102 |
, help = say "quit [msg], have the bot exit with msg" |
| 103 |
, process = \rest -> do |
| 104 |
server <- getServer |
| 105 |
lb (ircQuit server $ if null rest then "requested" else rest) |
| 106 |
} |
| 107 |
, (command "disconnect") |
| 108 |
{ privileged = True |
| 109 |
, help = say "disconnect <server> [msg], disconnect from a server with msg" |
| 110 |
, process = \rest -> do |
| 111 |
let (server, msg) = splitFirstWord rest |
| 112 |
lb (ircQuit server $ if null msg then "requested" else msg) |
| 113 |
} |
| 114 |
, (command "flush") |
| 115 |
{ privileged = True |
| 116 |
, help = say "flush. flush state to disk" |
| 117 |
, process = \_ -> lb (withAllModules writeGlobalState) |
| 118 |
|
| 119 |
} |
| 120 |
, (command "admin") |
| 121 |
{ privileged = True |
| 122 |
, help = say "admin [+|-] nick. change a user's admin status." |
| 123 |
, process = doAdmin |
| 124 |
} |
| 125 |
, (command "ignore") |
| 126 |
{ privileged = True |
| 127 |
, help = say "ignore [+|-] nick. change a user's ignore status." |
| 128 |
, process = doIgnore |
| 129 |
} |
| 130 |
, (command "reconnect") |
| 131 |
{ privileged = True |
| 132 |
, help = say "reconnect to server" |
| 133 |
, process = \rest -> do |
| 134 |
server <- getServer |
| 135 |
lb (ircReconnect server $ if null rest then "reconnect requested" else rest) |
| 136 |
} |
| 137 |
] |
| 138 |
} |
| 139 |
|
| 140 |
------------------------------------------------------------------------ |
| 141 |
|
| 142 |
doList :: String -> Cmd System () |
| 143 |
doList "" = say "What module? Try @listmodules for some ideas." |
| 144 |
doList m = say =<< lb (listModule m) |
| 145 |
|
| 146 |
doEcho :: String -> Cmd System () |
| 147 |
doEcho rest = do |
| 148 |
rawMsg <- withMsg (return . show) |
| 149 |
target <- showNick =<< getTarget |
| 150 |
say (concat ["echo; msg:", rawMsg, " target:" , target, " rest:", show rest]) |
| 151 |
|
| 152 |
doAdmin :: String -> Cmd System () |
| 153 |
doAdmin = toggleNick $ \op nck s -> s { ircPrivilegedUsers = op nck (ircPrivilegedUsers s) } |
| 154 |
|
| 155 |
doIgnore :: String -> Cmd System () |
| 156 |
doIgnore = toggleNick $ \op nck s -> s { ircIgnoredUsers = op nck (ircIgnoredUsers s) } |
| 157 |
|
| 158 |
------------------------------------------------------------------------ |
| 159 |
|
| 160 |
-- | Print map keys |
| 161 |
listKeys :: Show k => (IRCRWState -> M.Map k v) -> Cmd System () |
| 162 |
listKeys f = say . showClean . M.keys =<< lb (gets f) |
| 163 |
|
| 164 |
getUptime :: System (TimeDiff, TimeDiff) |
| 165 |
getUptime = do |
| 166 |
(loaded, m) <- readMS |
| 167 |
now <- io getClockTime |
| 168 |
let diff = now `diffClockTimes` loaded |
| 169 |
return (diff, max diff m) |
| 170 |
|
| 171 |
toggleNick :: (Ord a, MonadLB m) => |
| 172 |
((a -> S.Set a -> S.Set a) -> Nick -> IRCRWState -> IRCRWState) |
| 173 |
-> String -> Cmd m () |
| 174 |
toggleNick edit rest = do |
| 175 |
let (op, tgt) = splitAt 2 rest |
| 176 |
|
| 177 |
f <- case op of |
| 178 |
"+ " -> return S.insert |
| 179 |
"- " -> return S.delete |
| 180 |
_ -> fail "invalid usage" |
| 181 |
|
| 182 |
nck <- readNick tgt |
| 183 |
lb . modify $ edit f nck |
| 184 |
|
| 185 |
listModule :: String -> LB String |
| 186 |
listModule s = inModuleNamed s fromCommand printProvides |
| 187 |
where |
| 188 |
fromCommand = withCommand s |
| 189 |
(return $ "No module \""++s++"\" loaded") (const printProvides) |
| 190 |
|
| 191 |
printProvides :: ModuleT st LB String |
| 192 |
printProvides = do |
| 193 |
cmds <- moduleCmds =<< asks theModule |
| 194 |
let cmds' = filter (not . privileged) cmds |
| 195 |
name' <- asks moduleName |
| 196 |
return . concat $ if null cmds' |
| 197 |
then [name', " has no visible commands"] |
| 198 |
else [name', " provides: ", showClean (concatMap cmdNames cmds')] |