gitea_push2qq/cmds.py

140 lines
6.6 KiB
Python
Raw Normal View History

2023-12-20 20:47:07 +08:00
import secrets
2023-12-21 01:11:02 +08:00
from casbin import AsyncEnforcer
2023-12-21 00:14:39 +08:00
from sanic import SanicException
from models import User, GiteaUser
2023-12-22 00:14:06 +08:00
from sio_model import SioDecorator, Message, SioRequest, ReplyMessage
2023-12-20 20:47:07 +08:00
def cmds(app, data):
# 此处会检查注册的指令是否重复
sio_decorator = SioDecorator(app, data)
@sio_decorator.cmd('ping')
async def ping(sqt: SioRequest):
msg = Message(content='pong', room_id=sqt.room_id)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
@sio_decorator.cmd('gitea')
async def gitea(sqt: SioRequest):
parser = sqt.parser
parser.add_argument('-vd', '--validate', help='绑定QQ与gitea',
action="store_true")
2023-12-21 00:14:39 +08:00
parser.add_argument('-ust', '--ustatus', help='查询Gitea绑定状态',
action="store_true")
2023-12-20 20:47:07 +08:00
args = parser.parse_args(sqt.args)
2023-12-22 00:14:06 +08:00
reply = ReplyMessage(id=sqt.message_id)
2023-12-20 20:47:07 +08:00
if args.validate:
state = secrets.token_urlsafe(16)
user = await User.filter(id=sqt.sender_id).get_or_none()
if user:
user.name = sqt.sender_name
user.state = state
await user.save()
else:
user = User(id=sqt.sender_id, name=sqt.sender_name, state=state)
await user.save()
2023-12-20 23:29:54 +08:00
url = (f'{app.ctx.sio_config.gitea_host}/login/oauth/authorize?'
2023-12-20 20:47:07 +08:00
f'client_id={app.ctx.sio_config.client_id}&'
f'redirect_uri={app.ctx.sio_config.localhost}/redirect&'
f'response_type=code&state={state}')
2023-12-22 00:14:06 +08:00
msg = Message(content=f'click: \n{url}', room_id=sqt.room_id, reply_to=reply)
2023-12-20 20:47:07 +08:00
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
2023-12-21 00:14:39 +08:00
if args.ustatus:
g_user = await GiteaUser.filter(qid_id=sqt.sender_id).get_or_none()
if g_user:
2023-12-22 00:14:06 +08:00
msg = Message(content=f'您的Gitea账号是{g_user.name}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 00:14:39 +08:00
else:
try:
task = await sqt.app.get_task(str(sqt.sender_id))
result = task.result()
2023-12-22 00:14:06 +08:00
msg = Message(content=f'绑定状态:{result}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 00:14:39 +08:00
except SanicException:
2023-12-22 00:14:06 +08:00
msg = Message(content=f'你还没有绑定呢', room_id=sqt.room_id, reply_to=reply)
2023-12-21 00:14:39 +08:00
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
2023-12-20 20:47:07 +08:00
if len(sqt.args) == 0:
parser.parse_args(["-h"])
2023-12-21 01:11:02 +08:00
@sio_decorator.cmd('authadd')
async def auth_add(sqt: SioRequest):
parser = sqt.parser
2023-12-21 03:05:17 +08:00
parser.add_argument('-ag', '--addgroup', help='添加用户组与其权限',
2023-12-21 01:11:02 +08:00
action="store_true")
parser.add_argument('-g', '--group', help='组名')
parser.add_argument('-m', '--command', help='命令名')
2023-12-21 03:05:17 +08:00
parser.add_argument('-u', '--user', help='用户名')
2023-12-21 01:32:12 +08:00
parser.add_argument('-au', '--adduser', help='添加用户/群到组',
2023-12-21 01:11:02 +08:00
action="store_true")
args = parser.parse_args(sqt.args)
reply = ReplyMessage(id=sqt.message_id)
2023-12-21 01:11:02 +08:00
e: AsyncEnforcer = sqt.app.ctx.e
msg = ''
if args.addgroup:
if args.group and args.command:
if await e.add_policy(args.group, args.command):
msg = Message(content=f'添加成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
else:
msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
if args.adduser:
2023-12-21 01:32:12 +08:00
if args.group:
2023-12-21 03:05:17 +08:00
if await e.add_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'添加成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
else:
msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
if len(sqt.args) == 0:
parser.parse_args(["-h"])
else:
if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 01:11:02 +08:00
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
2023-12-21 01:25:47 +08:00
await e.save_policy()
2023-12-21 01:11:02 +08:00
2023-12-21 03:05:17 +08:00
@sio_decorator.cmd('authrm')
async def auth_add(sqt: SioRequest):
parser = sqt.parser
parser.add_argument('-rg', '--rmgroup', help='移除用户组的权限',
action="store_true")
parser.add_argument('-g', '--group', help='组名')
parser.add_argument('-u', '--user', help='用户名')
parser.add_argument('-ru', '--rmuser', help='从组移除用户/群',
action="store_true")
args = parser.parse_args(sqt.args)
e: AsyncEnforcer = sqt.app.ctx.e
msg = ''
if args.rmgroup:
if args.group and args.command:
if await e.remove_policy(args.group, args.command):
msg = Message(content=f'移除成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
else:
msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
if args.rmuser:
if args.group:
if await e.delete_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'移除成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
else:
msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
if len(sqt.args) == 0:
parser.parse_args(["-h"])
else:
if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
2023-12-21 03:05:17 +08:00
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy()
2023-12-20 20:47:07 +08:00
return sio_decorator