An IRC bot for learning, fun and collaboration in the Freepost community.
Clone
HTTPS:
git clone https://vervis.peers.community/repos/VvM9v
SSH:
git clone USERNAME@vervis.peers.community:VvM9v
Branches
Tags
UserOptions.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 | {- This file is part of funbot.
-
- Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
-- For JSON field names
{-# LANGUAGE OverloadedStrings #-}
module FunBot.UserOptions
( getUserHistoryOpts
, sendHistoryOpts
, sendChannels
, setEnabled
, setMaxLines
, eraseOpts
, loadUserOptions
, mkSaveUserOptions
, saveUserOptions
)
where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM, mzero)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson
import Data.JsonState
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Text (Text, intercalate)
import Formatting
import FunBot.Config (stateSaveInterval, configuration, userOptsFilename)
import FunBot.Settings.Instances ()
import FunBot.Types
import FunBot.Util (getHistoryLines)
import Network.IRC.Fun.Bot.Chat (sendToUser)
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types (Config (cfgStateRepo))
import Network.IRC.Fun.Types.Base
import qualified Data.HashMap.Lazy as M
-------------------------------------------------------------------------------
-- Utilities
-------------------------------------------------------------------------------
defaultEnabled :: Bool
defaultEnabled = False
defaultMaxLines :: Int
defaultMaxLines = 10
defHistoryDisplay :: HistoryDisplay
defHistoryDisplay = HistoryDisplay
{ hdEnabled = defaultEnabled
, hdMaxLines = defaultMaxLines
}
defUserOpts :: UserOptions
defUserOpts = UserOptions
{ uoHistoryDisplay = M.empty }
getUserHistoryOpts :: Nickname -> Channel -> BotSession HistoryDisplay
getUserHistoryOpts nick chan = do
opts <- getStateS bsUserOptions
let user = M.lookup nick opts
hdmap = fmap uoHistoryDisplay user
mhd = hdmap >>= M.lookup chan
return $ fromMaybe defHistoryDisplay mhd
-------------------------------------------------------------------------------
-- Command Implementation
-------------------------------------------------------------------------------
showEnabled :: Bool -> Text
showEnabled True = "Enabled"
showEnabled False = "Disabled"
showMaxLines :: Int -> Text
showMaxLines n = sformat (int % " lines") n
formatHistoryOpts :: Nickname -> Channel -> Maybe HistoryDisplay -> MsgContent
formatHistoryOpts nick chan mhd =
let defSuffix = " [default]"
(enabled, maxLines) =
case mhd of
Nothing ->
( showEnabled defaultEnabled <> defSuffix
, showMaxLines defaultMaxLines <> defSuffix
)
Just hd ->
( showEnabled $ hdEnabled hd
, showMaxLines $ hdMaxLines hd
)
in MsgContent $
sformat
( "History display of "
% stext
% " for "
% stext
% ": "
% stext
% ", "
% stext
)
(unChannel chan)
(unNickname nick)
enabled
maxLines
modifyOpts
:: Nickname
-> Channel
-> (HistoryDisplay -> HistoryDisplay)
-> BotSession ()
modifyOpts nick chan f = do
opts <- getStateS bsUserOptions
let userPrev = M.lookupDefault defUserOpts nick opts
hdmapPrev = uoHistoryDisplay userPrev
hdPrev = M.lookupDefault defHistoryDisplay chan hdmapPrev
hdNew = f hdPrev
hdmapNew = M.insert chan hdNew hdmapPrev
userNew = userPrev { uoHistoryDisplay = hdmapNew }
optsNew = M.insert nick userNew opts
modifyState $ \ s -> s { bsUserOptions = optsNew }
saveUserOptions
-------------------------------------------------------------------------------
-- Commands
-------------------------------------------------------------------------------
sendHistoryOpts :: Nickname -> Channel -> BotSession ()
sendHistoryOpts nick chan = do
opts <- getStateS bsUserOptions
let user = M.lookup nick opts
hdmap = fmap uoHistoryDisplay user
mhd = hdmap >>= M.lookup chan
sendToUser nick $ formatHistoryOpts nick chan mhd
sendChannels :: Nickname -> BotSession ()
sendChannels nick = do
muser <- fmap (M.lookup nick) $ getStateS bsUserOptions
let mhdmap = fmap uoHistoryDisplay muser
mkeys = fmap M.keys mhdmap
keys = fromMaybe [] mkeys
l = if null keys
then "(none)"
else intercalate ", " $ map unChannel keys
sendToUser nick $ MsgContent $ "Options stored for channels: " <> l
setEnabled :: Nickname -> Channel -> Bool -> BotSession ()
setEnabled nick chan enabled = do
modifyOpts nick chan $ \ hd -> hd { hdEnabled = enabled }
sendToUser nick $ MsgContent $
sformat
( "History display of "
% stext
% ": "
% stext
)
(unChannel chan)
(showEnabled enabled)
setMaxLines :: Nickname -> Channel -> Int -> BotSession ()
setMaxLines nick chan maxLines = do
modifyOpts nick chan $ \ hd -> hd { hdMaxLines = maxLines }
hls <- getHistoryLines chan
sendToUser nick $ MsgContent $
sformat
( "History display length for "
% stext
% ": "
% stext
% "\n(I keep in my logs up to "
% stext
% " for "
% stext
% ")"
)
(unChannel chan)
(showMaxLines maxLines)
(showMaxLines hls)
(unChannel chan)
eraseOpts :: Nickname -> BotSession ()
eraseOpts nick = do
opts <- getStateS bsUserOptions
case M.lookup nick opts of
Nothing ->
sendToUser nick $ MsgContent "You don’t have stored options."
Just user -> do
modifyState $ \ s -> s { bsUserOptions = M.delete nick opts }
saveUserOptions
sendToUser nick $
MsgContent "All your options have been reset to defaults."
-------------------------------------------------------------------------------
-- Persistence
-------------------------------------------------------------------------------
instance FromJSON HistoryDisplay where
parseJSON (Object o) =
HistoryDisplay <$>
o .: "enabled" <*>
o .: "max-lines"
parseJSON _ = mzero
instance ToJSON HistoryDisplay where
toJSON (HistoryDisplay enabled maxLines) = object
[ "enabled" .= enabled
, "max-lines" .= maxLines
]
instance FromJSON UserOptions where
parseJSON (Object o) =
UserOptions <$>
o .: "history-display"
parseJSON _ = mzero
instance ToJSON UserOptions where
toJSON (UserOptions hd) = object
[ "history-display" .= hd
]
loadUserOptions :: IO (M.HashMap Nickname UserOptions)
loadUserOptions = do
r <- loadState $
stateFilePath userOptsFilename (cfgStateRepo configuration)
case r of
Left (False, e) -> error $ "Failed to read user options file: " ++ e
Left (True, e) -> error $ "Failed to parse user options file: " ++ e
Right s -> return s
mkSaveUserOptions :: IO (M.HashMap Nickname UserOptions -> IO ())
mkSaveUserOptions =
mkSaveStateChoose
stateSaveInterval
userOptsFilename
(cfgStateRepo configuration)
"auto commit by funbot"
saveUserOptions :: BotSession ()
saveUserOptions = do
opts <- getStateS bsUserOptions
save <- askEnvS saveUserOpts
liftIO $ save opts
|