home · contact · privacy
Whitelist legal input characters.
[plomrogue2] / plomrogue / parser.py
index 2a87e7d10ea221336cde9dae391b14d5b774b024..2820c186778365384be548929c2a1128f587d490 100644 (file)
@@ -78,6 +78,14 @@ class Parser:
 
         Respects function signature defined in function's .argtypes attribute.
         """
+        import string
+        msg = msg.rstrip()
+        legal_chars = string.digits + string.ascii_letters +\
+            string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô'
+        for c in msg:
+            if not c in legal_chars:
+                raise ArgError('Command/message contains illegal character(s), '
+                               'may only contain ones of: %s' % legal_chars)
         tokens = self.tokenize(msg)
         if len(tokens) == 0:
             return None, ()
@@ -101,8 +109,8 @@ class Parser:
         tmpl_tokens = signature.split()
         if len(tmpl_tokens) != len(args_tokens):
             raise ArgError('Number of arguments (' + str(len(args_tokens)) +
-                           ') not expected number (' + str(len(tmpl_tokens))
-                           ').')
+                           ') not expected number (' + str(len(tmpl_tokens)) +
+                           ').')
         args = []
         string_string = 'string'
         for i in range(len(tmpl_tokens)):
@@ -116,6 +124,10 @@ class Parser:
                 if not arg.isdigit() or int(arg) < 1:
                     raise ArgError('Argument must be positive integer.')
                 args += [int(arg)]
+            elif tmpl == 'bool':
+                if not arg.isdigit() or int(arg) not in (0, 1):
+                    raise ArgError('Argument must be 0 or 1.')
+                args += [bool(int(arg))]
             elif tmpl == 'char':
                 try:
                     ord(arg)