Proper parameters handling of chat commands
authorunc0rr
Tue, 04 Jun 2013 23:58:42 +0400
changeset 9105 18ebb59c89fe
parent 9103 b70352db5675
child 9107 4dde5fecffe2
child 9125 c542f6e3a133
Proper parameters handling of chat commands
gameServer/HWProtoCore.hs
--- a/gameServer/HWProtoCore.hs	Mon Jun 03 23:59:55 2013 -0400
+++ b/gameServer/HWProtoCore.hs	Tue Jun 04 23:58:42 2013 +0400
@@ -4,7 +4,6 @@
 import Control.Monad.Reader
 import Data.Maybe
 import qualified Data.ByteString.Char8 as B
-import qualified Data.List as L
 --------------------------------------
 import CoreTypes
 import Actions
@@ -34,24 +33,24 @@
         else
         return [ModifyClient (\c -> c{pingsQueue = pingsQueue c - 1})]
 
-handleCmd ("CMD" : parameters) =
-    let c = concatMap B.words parameters in
-        if not $ null c then
-            h $ (upperCase . head $ c) : tail c
-            else
-            return []
+handleCmd ["CMD", parameters] = do
+        let (cmd, plist) = B.break (== ' ') parameters
+        let param = B.dropWhile (== ' ') plist
+        h (upperCase cmd) param
     where
-        h ["DELEGATE", n] = handleCmd ["DELEGATE", n]
-        h ["STATS"] = handleCmd ["STATS"]
-        h ("PART":m:ms) = handleCmd ["PART", B.unwords $ m:ms]
-        h ("QUIT":m:ms) = handleCmd ["QUIT", B.unwords $ m:ms]
-        h ("RND":rs) = handleCmd ("RND":rs)
-        h ("GLOBAL":m:ms) = do
+        h "DELEGATE" n | not $ B.null n = handleCmd ["DELEGATE", n]
+        h "STATS" _ = handleCmd ["STATS"]
+        h "PART" m | not $ B.null m = handleCmd ["PART", m]
+                   | otherwise = handleCmd ["PART"]
+        h "QUIT" m | not $ B.null m = handleCmd ["QUIT", m]
+                   | otherwise = handleCmd ["QUIT"]
+        h "RND" p = handleCmd ("RND" : B.words p)
+        h "GLOBAL" p = do
             cl <- thisClient
             rnc <- liftM snd ask
             let chans = map (sendChan . client rnc) $ allClients rnc
-            return [AnswerClients chans ["CHAT", "[global notice]", B.unwords $ m:ms] | isAdministrator cl]
-        h c = return [Warning . B.concat . L.intersperse " " $ "Unknown cmd" : c]
+            return [AnswerClients chans ["CHAT", "[global notice]", p] | isAdministrator cl]
+        h c p = return [Warning $ B.concat ["Unknown cmd: /", c, p]]
 
 handleCmd cmd = do
     (ci, irnc) <- ask