-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandparser.py
More file actions
73 lines (66 loc) · 3.24 KB
/
commandparser.py
File metadata and controls
73 lines (66 loc) · 3.24 KB
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
# Copyright Beatrice Tohni 2021
""" This file is part of wiggleVerse.
wiggleVerse is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
wiggleVerse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wiggleVerse. If not, see <https://www.gnu.org/licenses/>."""
import logging
class CommandParser:
def __init__(self):
pass
def parse_command(self, command):
if command[0] != '/':
# This is an attempted PRIVMSG attempt to the default channel
return ('no_slash', command)
else: # This is an attempted client command
command = command[1:] # Remove the slash
words = command.split(' ')
# Remove empty strings
for word in words:
words = [i for i in words if i]
logging.debug(f'{words}')
first_word = words[0]
opt_args = ['quit', 'disconnect', 'easter', 'list', 'reconnect', 'names', 'help', 'version']
one_arg = ['raw', 'whois', 'nick', 'join', 'part', 'topic', 'switch']
if opt_args.count(first_word) == 1:
return (first_word, ' '.join(words[-len(words)+1:]))
elif one_arg.count(first_word) == 1:
try:
if first_word == 'raw':
return (first_word, ' '.join(words[1:]))
else:
return (first_word, words[1])
except IndexError:
return ('error', f'{first_word}: Incorrect number of arguments given')
elif first_word == 'msg':
try:
return (first_word, words[1], ' '.join(words[2:]))
except IndexError:
return ('error', f'{first_word}: Incorrect number of arguments given')
elif first_word == 'connect':
if len(words) == 2:
return (first_word, words[1], 6667) # Default port
elif len(words) == 3:
try:
return (first_word, words[1], int(words[2]))
except ValueError:
return ('error', 'Connect: Non numeric port number given')
else:
return ('error', f'{first_word}: Incorrect number of arguments given')
elif first_word == 'set':
if len(words) == 1: # get settings
return ('getset', 'all')
elif len(words) == 2: # get a setting
return ('getset', words[1])
elif len(words) >= 3: # set a setting
return ('setset', words[1], words[2:])
else:
return ('error', 'Set: incorrect paramters')
else:
return ('error', f'{first_word}: No such command')