3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 20:22:45 +01:00

Log command usage, 'exec' usage, successful logins, and access denied to commands in admin.py

Closes #66.
This commit is contained in:
James Lu 2015-07-19 14:59:51 -07:00
parent 1b09a00ea9
commit 57e9bf601e
4 changed files with 16 additions and 4 deletions

View File

@ -29,9 +29,10 @@ def handle_commands(irc, source, command, args):
utils.msg(irc, source, 'Unknown command %r.' % cmd) utils.msg(irc, source, 'Unknown command %r.' % cmd)
return return
try: try:
log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source))
func(irc, source, cmd_args) func(irc, source, cmd_args)
except Exception as e: except Exception as e:
log.exception('Unhandled exception caught in command %r' % cmd) log.exception('Unhandled exception caught in command %r', cmd)
utils.msg(irc, source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e))) utils.msg(irc, source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
return return
utils.add_hook(handle_commands, 'PRIVMSG') utils.add_hook(handle_commands, 'PRIVMSG')

View File

@ -1,13 +1,20 @@
# admin.py: PyLink administrative commands # admin.py: PyLink administrative commands
import sys, os import sys
import os
import inspect
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import utils import utils
from log import log
class NotAuthenticatedError(Exception): class NotAuthenticatedError(Exception):
pass pass
def checkauthenticated(irc, source): def checkauthenticated(irc, source):
lastfunc = inspect.stack()[1][3]
if not irc.users[source].identified: if not irc.users[source].identified:
log.warning('(%s) Access denied for %s calling %r', irc.name,
utils.getHostmask(irc, source), lastfunc)
raise NotAuthenticatedError("You are not authenticated!") raise NotAuthenticatedError("You are not authenticated!")
def _exec(irc, source, args): def _exec(irc, source, args):
@ -20,6 +27,7 @@ def _exec(irc, source, args):
if not args.strip(): if not args.strip():
utils.msg(irc, source, 'No code entered!') utils.msg(irc, source, 'No code entered!')
return return
log.info('(%s) Executing %r for %s', irc.name, args, utils.getHostmask(irc, source))
exec(args, globals(), locals()) exec(args, globals(), locals())
utils.add_cmd(_exec, 'exec') utils.add_cmd(_exec, 'exec')

View File

@ -34,11 +34,13 @@ def identify(irc, source, args):
realuser = conf['login']['user'] realuser = conf['login']['user']
irc.users[source].identified = realuser irc.users[source].identified = realuser
utils.msg(irc, source, 'Successfully logged in as %s.' % realuser) utils.msg(irc, source, 'Successfully logged in as %s.' % realuser)
log.info("(%s) Successful login to %r by %s.",
irc.name, username, utils.getHostmask(irc, source))
else: else:
utils.msg(irc, source, 'Incorrect credentials.') utils.msg(irc, source, 'Incorrect credentials.')
u = irc.users[source] u = irc.users[source]
log.warning("(%s) Failed login to %r from user '%s!%s@%s' (UID %r).", log.warning("(%s) Failed login to %r from %s.",
irc.name, username, u.nick, u.ident, u.host, u.uid) irc.name, username, utils.getHostmask(irc, source))
def listcommands(irc, source, args): def listcommands(irc, source, args):
"""takes no arguments. """takes no arguments.

View File

@ -95,5 +95,6 @@ class TestUtils(unittest.TestCase):
('-m', None), ('+k', 'hello'), ('-m', None), ('+k', 'hello'),
('+b', '*!*@*.badisp.net')]) ('+b', '*!*@*.badisp.net')])
self.assertEqual(res, '-o+l-nm+kb 9PYAAAAAA 50 hello *!*@*.badisp.net') self.assertEqual(res, '-o+l-nm+kb 9PYAAAAAA 50 hello *!*@*.badisp.net')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()