86 lines
3.3 KiB
Haskell
86 lines
3.3 KiB
Haskell
-- | This module contains pure functions for transforming the state of
|
|
-- 'IrcUser' structures while processing IRC commands.
|
|
module Pipes.IRC.Server.User
|
|
( newUser
|
|
, userAddChan
|
|
, userDelChan
|
|
, userAddMode
|
|
, userDelMode
|
|
, userAddInvite
|
|
, userDelInvite
|
|
, userHasMode
|
|
, userInChan
|
|
)
|
|
where
|
|
|
|
import Control.Lens
|
|
import Data.ByteString.Char8 (ByteString)
|
|
import Data.Set (delete, empty, insert, member)
|
|
|
|
import Pipes.IRC.Server.Types
|
|
|
|
-- | Create a new 'IrcUser' record with the given server name and connection
|
|
-- id.
|
|
newUser :: ByteString -- ^ a 'ByteString' containing the user's server name
|
|
-> Int -- ^ the user's connection id
|
|
-> IrcUser -- ^ the resulting new 'IrcUser' structure
|
|
newUser srvname cid =
|
|
IrcUser { _userServerName = srvname
|
|
, _userModes = empty
|
|
, _userChannels = empty
|
|
, _userConn = cid
|
|
, _userInvites = empty
|
|
}
|
|
|
|
userAddChan, userDelChan :: ChanKey -- ^ channel to add or remove from user
|
|
-> IrcUser -- ^ user of which to modify channel list
|
|
-> IrcUser -- ^ new user with channel list changed
|
|
|
|
-- | Add a channel to the user's set of channels. This does not change the
|
|
-- set of users in the channel or perform any checking; this just performs
|
|
-- the low-level change to the user.
|
|
userAddChan cn = userChannels %~ insert cn
|
|
|
|
-- | Delete a channel from the user's set of channels. This does not change
|
|
-- the set of users in the channel or perform any checking; this just performs
|
|
-- the low-level change to the user.
|
|
userDelChan cn = userChannels %~ delete cn
|
|
|
|
|
|
userAddMode, userDelMode :: IrcUserMode -- ^ mode to add or remove from user
|
|
-> IrcUser -- ^ user to perform the mode change on
|
|
-> IrcUser -- ^ new user after mode change
|
|
|
|
-- | Add the mode to the user's set of modes.
|
|
userAddMode um = userModes %~ insert um
|
|
|
|
-- | Remove the mode from the user's set of modes.
|
|
userDelMode um = userModes %~ delete um
|
|
|
|
|
|
userAddInvite, userDelInvite :: ChanKey -- ^ channel name of invitation
|
|
-> IrcUser -- ^ user of which to modify invites
|
|
-> IrcUser -- ^ new user after invite changes
|
|
|
|
-- | Add a channel invite to the user's set of active invites. This does not do
|
|
-- any change to the channel; it just performs the low-level change to the user.
|
|
userAddInvite cn = userInvites %~ insert cn
|
|
|
|
-- | Delete a channel invite from the user's set of active invites. This does
|
|
-- not do any change to the channel; it just performs the low-level change to
|
|
-- the user.
|
|
userDelInvite cn = userInvites %~ delete cn
|
|
|
|
-- | Check whether the user has the indicated mode.
|
|
userHasMode :: IrcUserMode -- ^ mode to check for presence of
|
|
-> IrcUser -- ^ user to check the mode of
|
|
-> Bool -- ^ does user have the mode?
|
|
userHasMode um usr = member um (usr ^. userModes)
|
|
|
|
-- | Check whether the user has the named channel in its set of channels. Note
|
|
-- that this does not check anything other than the user's set of channels.
|
|
userInChan :: ChanKey -- ^ name of the channel to check for
|
|
-> IrcUser -- ^ user to check the channel set of
|
|
-> Bool -- ^ is the user on the channel?
|
|
userInChan cn usr = member cn (usr ^. userChannels)
|