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 | {- 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/>.
-}
{-# LANGUAGE OverloadedStrings #-}
-- | Show-opts, enable-history, disable-history, set-history-lines,
-- get-history-lines, erase-opts commands
--
-- Manage user options
module FunBot.Commands.UserOptions
( cmdShowOpts
, cmdEnableHistory
, cmdDisableHistory
, cmdSetLines
, cmdEraseOpts
)
where
import Control.Monad (unless, when)
import Data.List (find, intercalate)
import Data.Monoid ((<>))
import Data.Settings.Types (showOption)
import Data.Text (Text)
import FunBot.History (quote, reportHistory')
import FunBot.Memos (submitMemo)
import FunBot.Settings
import FunBot.Settings.Sections.Channels (addChannel)
import FunBot.Settings.Sections.Feeds (addFeed, deleteFeed)
import FunBot.Settings.Sections.Repos
import FunBot.Settings.Sections.Shortcuts (addShortcut, deleteShortcut)
import FunBot.Types
import FunBot.UserOptions
import FunBot.Util
import Network.IRC.Fun.Bot.Behavior
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types
import Text.Read (readMaybe)
import Network.IRC.Fun.Types.Base
import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Read as TR
priv = MsgContent "That command works only in private conversation with me."
respondShowOpts
:: Maybe Channel
-> Nickname
-> [Text]
-> (MsgContent -> BotSession ())
-> BotSession ()
respondShowOpts (Just _chan) _nick _args send = send priv
respondShowOpts Nothing nick [] _send = sendChannels nick
respondShowOpts Nothing nick [chan] send =
if looksLikeChan $ Channel chan
then sendHistoryOpts nick $ Channel chan
else send $ notchan $ Channel chan
respondShowOpts Nothing nick args _send =
failToUser nick $ WrongNumArgsN (Just $ length args) Nothing
cmdShowOpts = Command
{ cmdNames = cmds ["show-opts", "show-options"]
, cmdRespond = respondShowOpts
, cmdHelp = helps
[ ( "show-opts"
, "list channels for which you set history display options."
)
, ( "show-opts <channel>"
, "show history display options for the given channel."
)
]
, cmdExamples =
[ "show-opts"
, "show-opts #snowdrift"
]
}
respondHistory
:: Bool
-> Maybe Channel
-> Nickname
-> [Text]
-> (MsgContent -> BotSession ())
-> BotSession ()
respondHistory _enable (Just _chan) _nick _args send = send priv
respondHistory enable Nothing nick [chan] send =
if looksLikeChan $ Channel chan
then do
setEnabled nick (Channel chan) enable
hls <- getHistoryLines $ Channel chan
when (enable && hls < 1) $ send $ MsgContent
"Note that while you enabled personal history display, \
\history logging is disabled for that channel, therefore you \
\won’t be getting history messages. Ask the bot maintainer(s) \
\to enable it."
else send $ notchan $ Channel chan
respondHistory _enable Nothing nick args _send =
failToUser nick $ WrongNumArgsN (Just $ length args) (Just 1)
cmdEnableHistory = Command
{ cmdNames = cmds ["history+", "enable-history"]
, cmdRespond = respondHistory True
, cmdHelp = helps
[ ( "history+ <channel>"
, "enable automatic history private display for the given channel."
)
]
, cmdExamples =
[ "history+ #snowdrift"
]
}
cmdDisableHistory = Command
{ cmdNames = cmds ["history-", "disable-history"]
, cmdRespond = respondHistory False
, cmdHelp = helps
[ ( "history- <channel>"
, "disable automatic history private display for the given channel."
)
]
, cmdExamples =
[ "history- #snowdrift-test"
]
}
respondSetLines
:: Maybe Channel
-> Nickname
-> [Text]
-> (MsgContent -> BotSession ())
-> BotSession ()
respondSetLines (Just _chan) _nick _args send = send priv
respondSetLines Nothing nick [chan, len] send =
if looksLikeChan $ Channel chan
then
case TR.decimal len of
Right (n, "") -> setMaxLines nick (Channel chan) n
_ -> badLen
else send $ notchan $ Channel chan
where
badLen = failToUser nick $ InvalidArg (Just 2) (Just len)
respondSetLines Nothing nick args _send =
failToUser nick $ WrongNumArgsN (Just $ length args) (Just 2)
cmdSetLines = Command
{ cmdNames = cmds ["set-history-lines"]
, cmdRespond = respondSetLines
, cmdHelp = helps
[ ( "set-history-lines <channel> <num>"
, "set the maximal number of channel history lines to display."
)
]
, cmdExamples =
[ "set-history-lines #snowdrift 10"
]
}
respondEraseOpts
:: Maybe Channel
-> Nickname
-> [Text]
-> (MsgContent -> BotSession ())
-> BotSession ()
respondEraseOpts (Just _chan) _nick _args send = send priv
respondEraseOpts Nothing nick [] _send = eraseOpts nick
respondEraseOpts Nothing nick args _send =
failToUser nick $ WrongNumArgsN (Just $ length args) (Just 0)
cmdEraseOpts = Command
{ cmdNames = cmds ["erase-opts", "erase-options"]
, cmdRespond = respondEraseOpts
, cmdHelp = helps
[ ( "erase-opts"
, "reset all your options back to defaults."
)
]
, cmdExamples =
[ "erase-opts"
]
}
|