2016-12-02 22:24:19 +08:00
|
|
|
import os
|
2017-01-02 13:13:25 +08:00
|
|
|
import random
|
|
|
|
from datetime import datetime
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
from config import config
|
|
|
|
|
|
|
|
|
2016-12-03 17:16:16 +08:00
|
|
|
class SkipException(Exception):
|
2016-12-02 22:24:19 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _mkdir_if_not_exists_and_return_path(path):
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def get_root_dir():
|
|
|
|
return os.path.split(os.path.realpath(__file__))[0]
|
|
|
|
|
|
|
|
|
2016-12-08 21:58:49 +08:00
|
|
|
def get_filters_dir():
|
|
|
|
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'filters'))
|
|
|
|
|
|
|
|
|
2016-12-02 22:24:19 +08:00
|
|
|
def get_commands_dir():
|
|
|
|
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'commands'))
|
|
|
|
|
|
|
|
|
2017-01-02 23:51:19 +08:00
|
|
|
def get_nl_processors_dir():
|
|
|
|
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'nl_processors'))
|
|
|
|
|
|
|
|
|
2016-12-02 22:24:19 +08:00
|
|
|
def get_db_dir():
|
|
|
|
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'data', 'db'))
|
|
|
|
|
|
|
|
|
|
|
|
def get_default_db_path():
|
2016-12-03 17:16:16 +08:00
|
|
|
return os.path.join(get_db_dir(), 'default.sqlite')
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_tmp_dir():
|
|
|
|
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'data', 'tmp'))
|
|
|
|
|
|
|
|
|
|
|
|
def get_source(ctx_msg):
|
|
|
|
"""
|
|
|
|
Source is used to distinguish the interactive sessions.
|
2016-12-29 23:45:34 +08:00
|
|
|
Note: This value may change after restarting the bot.
|
|
|
|
|
2017-01-02 13:13:25 +08:00
|
|
|
:return: an unique value representing a source, or a random value if something strange happened
|
2016-12-02 22:24:19 +08:00
|
|
|
"""
|
2016-12-29 23:45:34 +08:00
|
|
|
if ctx_msg.get('via') == 'qq':
|
2017-01-02 13:13:25 +08:00
|
|
|
if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid') and ctx_msg.get('sender_uid'):
|
2016-12-29 23:45:34 +08:00
|
|
|
return 'g' + ctx_msg.get('group_uid') + 'p' + ctx_msg.get('sender_uid')
|
2017-01-02 13:13:25 +08:00
|
|
|
elif ctx_msg.get('type') == 'discuss_message' and ctx_msg.get('discuss_id') and ctx_msg.get('sender_uid'):
|
2016-12-29 23:45:34 +08:00
|
|
|
return 'd' + ctx_msg.get('discuss_id') + 'p' + ctx_msg.get('sender_uid')
|
2017-01-02 13:13:25 +08:00
|
|
|
elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_uid'):
|
|
|
|
return 'p' + ctx_msg.get('sender_uid')
|
2016-12-29 23:45:34 +08:00
|
|
|
elif ctx_msg.get('via') == 'wx':
|
2017-01-02 13:13:25 +08:00
|
|
|
if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_id') and ctx_msg.get('sender_id'):
|
2016-12-29 23:45:34 +08:00
|
|
|
return 'g' + ctx_msg.get('group_id') + 'p' + ctx_msg.get('sender_id')
|
2017-01-02 13:13:25 +08:00
|
|
|
elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_id'):
|
2016-12-29 23:45:34 +08:00
|
|
|
return 'p' + ctx_msg.get('sender_id')
|
2017-01-02 13:13:25 +08:00
|
|
|
return str(int(datetime.now().timestamp())) + str(random.randint(100, 999))
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_target(ctx_msg):
|
|
|
|
"""
|
|
|
|
Target is used to distinguish the records in database.
|
2016-12-29 23:45:34 +08:00
|
|
|
Note: This value will not change after restarting the bot.
|
|
|
|
|
|
|
|
:return: an unique value representing a target, or None if there is no persistent unique value
|
2016-12-02 22:24:19 +08:00
|
|
|
"""
|
2016-12-29 23:45:34 +08:00
|
|
|
if ctx_msg.get('via') == 'qq':
|
2017-01-02 13:13:25 +08:00
|
|
|
if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid'):
|
|
|
|
return 'g' + ctx_msg.get('group_uid')
|
2016-12-29 23:45:34 +08:00
|
|
|
elif ctx_msg.get('type') == 'discuss_message':
|
|
|
|
# TODO: 看看讨论组 ID 重新启动会不会变
|
|
|
|
pass
|
2017-01-02 13:13:25 +08:00
|
|
|
elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_uid'):
|
|
|
|
return 'p' + ctx_msg.get('sender_uid')
|
2016-12-29 23:45:34 +08:00
|
|
|
elif ctx_msg.get('via') == 'wx':
|
|
|
|
if ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_account'):
|
2017-01-02 13:13:25 +08:00
|
|
|
return 'p' + ctx_msg.get('sender_account')
|
2016-12-29 23:45:34 +08:00
|
|
|
return None
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_command_start_flags():
|
|
|
|
return tuple(sorted(config['command_start_flags'], reverse=True))
|
|
|
|
|
|
|
|
|
|
|
|
def get_command_name_separators():
|
|
|
|
return tuple(sorted(config['command_name_separators'], reverse=True))
|
|
|
|
|
|
|
|
|
|
|
|
def get_command_args_start_flags():
|
|
|
|
return tuple(sorted(('[ \t\n]',) + config['command_args_start_flags'], reverse=True))
|