add support for op 2

This commit is contained in:
Luna Mendes 2017-12-02 11:24:55 -03:00
parent d40b8dfcaa
commit d725403509
4 changed files with 58 additions and 5 deletions

View file

@ -82,7 +82,6 @@ class Admin(Cog):
m = ctx.send(f':ok_hand: Reloaded `{ext}`') m = ctx.send(f':ok_hand: Reloaded `{ext}`')
self.bot.loop.create_task(m) self.bot.loop.create_task(m)
@commands.command(typing=True) @commands.command(typing=True)
async def sql(self, ctx, *, statement: no_codeblock): async def sql(self, ctx, *, statement: no_codeblock):
"""Execute SQL.""" """Execute SQL."""

30
bot/ext/rsudo.py Normal file
View file

@ -0,0 +1,30 @@
import logging
import discord
from .common import Cog
log = logging.getLogger(__name__)
class Rsudo(Cog):
def __init__(self, bot):
super().__init__(bot)
async def create_request(self, message):
# parse it, follows format command,uid
uid = message.split(',')[-1]
command = ','.join(message.split(',')[:-1])
log.info(f'[rsudo] {uid!r} {command!r}')
if not self.command_channel:
return
e = discord.Embed(title=f'rsudo from uid {uid}')
e.add_field(name='command', value=f'`{command}`')
await self.command_channel.send(embed=e)
def setup(bot):
bot.add_cog(Rsudo(bot))

View file

@ -7,3 +7,5 @@ db = {
bot_token = 'Mzg2Mjc1MDc3MzY2NDgwODk4.DQNi9A.BnyE5MnKaIaVMBbWiW9rVDwkrSs' bot_token = 'Mzg2Mjc1MDc3MzY2NDgwODk4.DQNi9A.BnyE5MnKaIaVMBbWiW9rVDwkrSs'
owner_id = 162819866682851329 owner_id = 162819866682851329
bot_token = None

View file

@ -14,9 +14,20 @@ import asyncpg
import config import config
from bot import schedule_bot from bot import schedule_bot
SOCKFILE = './memed.succ'
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
db = None db = None
bot = None
# for writer:
# - op 0 : hello
# - op 1 : request response
# for reader:
# - op 1 : log
# - op 2 : rsudo
def parse_logstr(string): def parse_logstr(string):
@ -38,6 +49,7 @@ def parse_logstr(string):
cwd = [e[1] for e in s if e[0] == 'cwd'][0] cwd = [e[1] for e in s if e[0] == 'cwd'][0]
return uid, cwd, command return uid, cwd, command
async def read_msg(reader): async def read_msg(reader):
header = await reader.read(8) header = await reader.read(8)
length, op = struct.unpack('Ii', header) length, op = struct.unpack('Ii', header)
@ -64,7 +76,8 @@ async def send_msg(writer, op: int, data: str):
writer.write(msg) writer.write(msg)
await writer.drain() await writer.drain()
async def process(op: int, message: str):
async def process(reader, writer, op: int, message: str):
"""Process messages given through the socket""" """Process messages given through the socket"""
if op == 1: if op == 1:
uid, cwd, command = parse_logstr(message) uid, cwd, command = parse_logstr(message)
@ -72,6 +85,14 @@ async def process(op: int, message: str):
await db.execute(""" await db.execute("""
INSERT INTO logs (uid, cwd, cmd) VALUES ($1, $2, $3) INSERT INTO logs (uid, cwd, cmd) VALUES ($1, $2, $3)
""", uid, cwd, command) """, uid, cwd, command)
elif op == 2:
rsudo = bot.get_cog('Rsudo')
if not rsudo:
return await send_msg(writer, 1, 'no rsudo cog')
await rsudo.create_request(message)
return await send_msg(writer, 1, 'ok')
async def handle_client(reader, writer): async def handle_client(reader, writer):
"""Handle clients""" """Handle clients"""
@ -84,7 +105,7 @@ async def handle_client(reader, writer):
addr = writer.get_extra_info('peername') addr = writer.get_extra_info('peername')
log.debug('received %r from %s', message, addr) log.debug('received %r from %s', message, addr)
await process(op, message) await process(reader, writer, op, message)
writer.close() writer.close()
except Exception as e: except Exception as e:
@ -95,13 +116,14 @@ async def handle_client(reader, writer):
if __name__ == '__main__': if __name__ == '__main__':
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
coro = asyncio.start_unix_server(handle_client, './log.suck', coro = asyncio.start_unix_server(handle_client, SOCKFILE,
loop=loop) loop=loop)
db = loop.run_until_complete(asyncpg.create_pool(**config.db)) db = loop.run_until_complete(asyncpg.create_pool(**config.db))
server = loop.run_until_complete(coro) server = loop.run_until_complete(coro)
schedule_bot(loop, config, db) if config.bot_token:
schedule_bot(loop, config, db)
log.info(f'Serving on {server.sockets[0].getsockname()}') log.info(f'Serving on {server.sockets[0].getsockname()}')
try: try: