From 57e9bf601e38f16e7e545f0b1b7a42ea278e5c48 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 19 Jul 2015 14:59:51 -0700 Subject: [PATCH] Log command usage, 'exec' usage, successful logins, and access denied to commands in admin.py Closes #66. --- coreplugin.py | 3 ++- plugins/admin.py | 10 +++++++++- plugins/commands.py | 6 ++++-- tests/test_utils.py | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/coreplugin.py b/coreplugin.py index 7fc9b83..32871e3 100644 --- a/coreplugin.py +++ b/coreplugin.py @@ -29,9 +29,10 @@ def handle_commands(irc, source, command, args): utils.msg(irc, source, 'Unknown command %r.' % cmd) return try: + log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source)) func(irc, source, cmd_args) 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))) return utils.add_hook(handle_commands, 'PRIVMSG') diff --git a/plugins/admin.py b/plugins/admin.py index 62e956d..7394c01 100644 --- a/plugins/admin.py +++ b/plugins/admin.py @@ -1,13 +1,20 @@ # 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__)))) + import utils +from log import log class NotAuthenticatedError(Exception): pass def checkauthenticated(irc, source): + lastfunc = inspect.stack()[1][3] 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!") def _exec(irc, source, args): @@ -20,6 +27,7 @@ def _exec(irc, source, args): if not args.strip(): utils.msg(irc, source, 'No code entered!') return + log.info('(%s) Executing %r for %s', irc.name, args, utils.getHostmask(irc, source)) exec(args, globals(), locals()) utils.add_cmd(_exec, 'exec') diff --git a/plugins/commands.py b/plugins/commands.py index 7e620ad..a7b7b75 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -34,11 +34,13 @@ def identify(irc, source, args): realuser = conf['login']['user'] irc.users[source].identified = 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: utils.msg(irc, source, 'Incorrect credentials.') u = irc.users[source] - log.warning("(%s) Failed login to %r from user '%s!%s@%s' (UID %r).", - irc.name, username, u.nick, u.ident, u.host, u.uid) + log.warning("(%s) Failed login to %r from %s.", + irc.name, username, utils.getHostmask(irc, source)) def listcommands(irc, source, args): """takes no arguments. diff --git a/tests/test_utils.py b/tests/test_utils.py index 8a9ef3e..36c178f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -95,5 +95,6 @@ class TestUtils(unittest.TestCase): ('-m', None), ('+k', 'hello'), ('+b', '*!*@*.badisp.net')]) self.assertEqual(res, '-o+l-nm+kb 9PYAAAAAA 50 hello *!*@*.badisp.net') + if __name__ == '__main__': unittest.main()