Skip to content

Commit

Permalink
Actually properly implemented nick changing!
Browse files Browse the repository at this point in the history
  • Loading branch information
ori-sky committed Aug 3, 2014
1 parent d55a7dc commit 74faadd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 10 additions & 4 deletions plugins/Nick.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@

module Nick (plugin) where

import qualified Data.Map as M
import Control.Monad.State
import IRCD.Types
import IRCD.Env
import IRCD.Clients

plugin :: Plugin
plugin = defaultPlugin {handlers=[CommandHandler "NICK" nickHandler]}

nickHandler :: HandlerSpec
nickHandler (ClientSrc client) (Message _ _ _ (nick:_)) = do
nickHandler (ClientSrc client) (Message _ _ _ (nick':_)) = do
nicks <- gets (byNick . envClients)
return [GenericAction (liftIO $ print nicks)]
nickHandler (ClientSrc client) _ = return [GenericAction io]
where io = liftIO (putStrLn "No nickname given")
if nick' `M.member` nicks
then return [GenericAction $ liftIO (putStrLn "Nickname is already in use")]
else do
modify $ mapEnvClients (replaceClient client client {nick=Just nick'})
return [GenericAction $ liftIO (putStrLn ("NICK " ++ nick'))]
nickHandler (ClientSrc client) _ = return [GenericAction $ liftIO (putStrLn "No nickname given")]
7 changes: 4 additions & 3 deletions src/IRCD/Clients.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
- limitations under the License.
-}

module IRCD.Clients
(firstAvailableID, insertClient, deleteClient, deleteClientByUid)
where
module IRCD.Clients where

import Data.List (sort)
import qualified Data.Map as M (insert, delete)
Expand Down Expand Up @@ -56,5 +54,8 @@ deleteClient client clients = clients
Nothing -> byNick clients
Just nick' -> M.delete nick' (byNick clients)

replaceClient :: Client -> Client -> Clients -> Clients
replaceClient old new = insertClient new . deleteClient old

deleteClientByUid :: Int -> Clients -> Clients
deleteClientByUid uid' clients = clients {byUid = IM.delete uid' (byUid clients)}

0 comments on commit 74faadd

Please sign in to comment.