2022-06-16 10:23:11 +09:00
|
|
|
# SPDX-FileCopyrightText: 2022 n9k <https://git.076.ne.jp/ninya9k>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2022-06-16 10:05:18 +09:00
|
|
|
from anonstream.control.spec import ParseException, Parsed
|
2022-06-15 14:39:54 +09:00
|
|
|
from anonstream.control.spec.common import Str
|
2022-06-25 12:18:57 +09:00
|
|
|
from anonstream.control.spec.methods.allowedness import SPEC as SPEC_ALLOWEDNESS
|
2022-06-15 17:49:42 +09:00
|
|
|
from anonstream.control.spec.methods.chat import SPEC as SPEC_CHAT
|
2022-08-01 09:42:04 +09:00
|
|
|
from anonstream.control.spec.methods.emote import SPEC as SPEC_EMOTE
|
2022-06-15 14:39:54 +09:00
|
|
|
from anonstream.control.spec.methods.help import SPEC as SPEC_HELP
|
2022-08-01 08:09:52 +09:00
|
|
|
from anonstream.control.spec.methods.quit import SPEC as SPEC_QUIT
|
2022-06-15 17:49:42 +09:00
|
|
|
from anonstream.control.spec.methods.title import SPEC as SPEC_TITLE
|
|
|
|
from anonstream.control.spec.methods.user import SPEC as SPEC_USER
|
2022-06-14 17:29:32 +09:00
|
|
|
|
2022-06-15 14:39:54 +09:00
|
|
|
SPEC = Str({
|
|
|
|
'help': SPEC_HELP,
|
2022-08-01 08:09:52 +09:00
|
|
|
'quit': SPEC_QUIT,
|
2022-06-15 17:49:42 +09:00
|
|
|
'title': SPEC_TITLE,
|
|
|
|
'chat': SPEC_CHAT,
|
|
|
|
'user': SPEC_USER,
|
2022-06-25 12:18:57 +09:00
|
|
|
'allowednesss': SPEC_ALLOWEDNESS,
|
2022-08-01 09:42:04 +09:00
|
|
|
'emote': SPEC_EMOTE,
|
2022-06-15 14:39:54 +09:00
|
|
|
})
|
2022-06-14 17:29:32 +09:00
|
|
|
|
2022-06-15 14:39:54 +09:00
|
|
|
async def parse(request):
|
|
|
|
words = request.split()
|
|
|
|
if not words:
|
|
|
|
normal, response = None, ''
|
2022-06-14 17:29:32 +09:00
|
|
|
else:
|
2022-06-15 14:39:54 +09:00
|
|
|
spec = SPEC
|
|
|
|
index = 0
|
|
|
|
args = []
|
|
|
|
while True:
|
2022-06-14 17:29:32 +09:00
|
|
|
try:
|
2022-06-15 14:39:54 +09:00
|
|
|
spec, n_consumed, more_args = spec.consume(words, index)
|
2022-06-16 10:05:18 +09:00
|
|
|
except ParseException as e:
|
2022-06-15 14:39:54 +09:00
|
|
|
normal, response = None, e.args[0] + '\n'
|
|
|
|
break
|
|
|
|
except Parsed as e:
|
|
|
|
fn, *_ = e.args
|
|
|
|
normal, response = await fn(*args)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
index += n_consumed
|
|
|
|
args.extend(more_args)
|
2022-06-14 17:29:32 +09:00
|
|
|
return normal, response
|