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
Channels.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 | {- 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 #-}
module FunBot.Settings.Sections.Channels
( chanSec
, addChannel
, addLocalLocation
, removeLocalLocation
)
where
import Control.Monad (unless, void)
import Data.Bool (bool)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Sequence (Seq, (|>), (><), ViewL (..))
import Data.Settings.Section
import Data.Settings.Types
import FunBot.Settings.MkOption
import FunBot.Settings.Persist
import FunBot.Types
import Network.IRC.Fun.Bot.IrcLog
import Network.IRC.Fun.Bot.MsgCount
import Network.IRC.Fun.Bot.Nicks
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Types.Base (Channel (..), Nickname (..))
import qualified Data.CaseInsensitive as CI
import qualified Data.HashMap.Lazy as M
import qualified Data.HashSet as S
import qualified Data.Sequence as Q
import qualified Data.Text as T
defChan = ChanSettings True False [] "(?)" M.empty S.empty Nothing
locationOption chan l@(LocationLabel t) =
let defl = "(?)"
getl l sets = fromMaybe defl $ do
cs <- M.lookup chan $ stChannels sets
loc <- M.lookup l $ csLocations cs
return $ unLocation loc
setl l v sets =
let chans = stChannels sets
cs = M.lookupDefault defChan chan chans
locs = csLocations cs
locs' = M.insert l (Location v) locs
cs' = cs { csLocations = locs' }
chans' = M.insert chan cs' chans
in sets { stChannels = chans' }
f l = mkOptionF (getl l) (setl l) defl
in (CI.original t, f l)
-- | Create a section for a channel.
chanSec :: Channel -> [LocationLabel] -> SettingsTree
chanSec chan lls = Section
{ secOpts = M.fromList
[ ( "track"
, mkOptionB
(channelIsTracked chan)
(bool (stopTrackingChannel chan) (startTrackingChannel chan))
False
)
, ( "count"
, mkOptionB
(chanIsCounted chan)
(bool (stopCountingChan chan) (startCountingChan chan))
False
)
, ( "log"
, mkOptionB
(channelIsLogged chan)
(bool (stopLoggingChannel chan) (startLoggingChannel chan))
False
)
, ( "def-response"
, mkOptionB
(defRespEnabled chan)
(void . setDefResp chan)
True
)
, ( "say-titles"
, mkOptionF
(getf True csSayTitles)
(setf $ \ cs say -> cs { csSayTitles = say })
True
)
, ( "welcome"
, mkOptionF
(getf False csWelcome)
(setf $ \ cs w -> cs { csWelcome = w })
False
)
, ( "folks"
, mkOptionF
(getf [] $ map unNickname . csFolks)
(setf $ \ cs fs -> cs { csFolks = map Nickname fs })
[]
)
, ( "email"
, mkOptionF
(getf "(?)" csEmail)
(setf $ \ cs s -> cs { csEmail = s })
"(?)"
)
, ( "browse"
, mkOptionF
(getf "" $ fromMaybe "" . csBrowse)
(setf $ \ cs url ->
if T.null url
then cs { csBrowse = Nothing }
else cs { csBrowse = Just url }
)
""
)
]
, secSubs = M.fromList
[ ( "locations"
, Section
{ secOpts = M.fromList $ map (locationOption chan) lls
, secSubs = M.empty
}
)
]
}
where
getf e f = maybe e f . M.lookup chan . stChannels
setf f v s =
let chans = stChannels s
cs = M.lookupDefault defChan chan chans
cs' = f cs v
chans' = M.insert chan cs' chans
in s { stChannels = chans' }
-- | Add a new channel to state and tree and to be joined from now on. If
-- already exists, nothing happens.
addChannel :: Channel -> BotSession ()
addChannel chan = do
selectChannel chan
addChannelState chan
sets <- getSTree
let route = ["channels", unChannel chan]
unless (route `memberSub` sets) $ do
let sec = chanSec chan []
ins = insertSub route sec
modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }
-- | Add a new location item to a channel's settings and tree. Return 'Nothing'
-- on success. Otherwise return whether the channel isn't selected ('False') or
-- the location label already exists ('True').
addLocalLocation
:: Channel
-> LocationLabel
-> Location
-> BotSession (Maybe Bool)
addLocalLocation chan label location = do
sel <- channelSelected chan
if sel
then do
chans <- fmap stChannels getSettings
let cs = M.lookupDefault defChan chan chans
locs = csLocations cs
case M.lookup label locs of
Just _ -> return $ Just True
Nothing -> do
let locs' = M.insert label location locs
cs' = cs { csLocations = locs' }
chans' = M.insert chan cs' chans
modifySettings $ \ s -> s { stChannels = chans' }
saveBotSettings
let (t, opt) = locationOption chan label
path = ["channels", unChannel chan, "locations", t]
ins = insertOpt path opt
modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }
return Nothing
else return $ Just False
-- | Remove a channel-specific location from settings and tree. Return whether
-- success, i.e. whether the location did exist and indeed has been deleted.
removeLocalLocation :: Channel -> LocationLabel -> BotSession Bool
removeLocalLocation chan label = do
chans <- fmap stChannels getSettings
case M.lookup chan chans of
Nothing -> return False
Just cs ->
let locs = csLocations cs
in if M.member label locs
then do
let locs' = M.delete label locs
cs' = cs { csLocations = locs' }
chans' = M.insert chan cs' chans
modifySettings $ \ s -> s { stChannels = chans' }
saveBotSettings
let t = CI.original $ unLocationLabel label
path = ["channels", unChannel chan, "locations", t]
del = deleteOpt path
modifyState $ \ s -> s { bsSTree = del $ bsSTree s }
return True
else return False
|