Limnoria/src/MiscCommands.py

176 lines
6.6 KiB
Python
Raw Normal View History

2003-03-27 07:34:48 +01:00
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Miscellaneous commands.
"""
import os
import pprint
import conf
import world
import debug
import privmsgs
import callbacks
class MiscCommands(callbacks.Privmsg):
def list(self, irc, msg, args):
"""[<module name>]
Lists the commands available in the given module. If no module is
given, lists the public modules available.
"""
name = privmsgs.getArgs(args, needed=0, optional=1)
name = name.lower()
if not name:
names = [cb.__class__.__name__
for cb in irc.callbacks
if hasattr(cb, 'public') and cb.public]
2003-03-28 08:23:12 +01:00
names.sort()
2003-03-27 07:34:48 +01:00
irc.reply(msg, ', '.join(names))
else:
for cb in irc.callbacks:
cls = cb.__class__
if cls.__name__.lower().startswith(name) and \
not issubclass(cls, callbacks.PrivmsgRegexp) and \
issubclass(cls, callbacks.Privmsg):
commands = [x for x in cls.__dict__
if cb.isCommand(x) and \
hasattr(getattr(cb, x), '__doc__')]
2003-03-28 08:23:12 +01:00
commands.sort()
2003-03-27 07:34:48 +01:00
irc.reply(msg, ', '.join(commands))
return
irc.error(msg, 'There is no module named %s, ' \
'or that module has no commands.' % name)
def help(self, irc, msg, args):
"""<command>
Gives the help for a specific command. To find commands,
use the 'list' command to go see the commands offered by a module.
The 'list' command by itself will show you what modules have commands.
"""
command = privmsgs.getArgs(args, needed=0, optional=1)
if not command:
command = 'help'
command = callbacks.canonicalName(command)
cb = irc.findCallback(command)
if cb:
method = getattr(cb, command)
if hasattr(method, '__doc__'):
doclines = method.__doc__.splitlines()
help = doclines.pop(0)
if doclines:
s = '%s %s (for more help use the morehelp command)'
else:
s = '%s %s'
irc.reply(msg, s % (command, help))
else:
irc.reply(msg, 'That command exists, but has no help.')
else:
for cb in irc.callbacks:
if cb.name() == command:
if hasattr(cb, '__doc__'):
doclines = cb.__doc__.splitlines()
help = ' '.join(map(str.strip, doclines))
irc.reply(msg, help)
else:
irc.error(msg, 'That callback has no help.')
else:
irc.error(msg, 'There is no such command')
def morehelp(self, irc, msg, args):
"""<command>
This command gives more help than is provided by the simple argument
list given by the command 'help'.
"""
command = callbacks.canonicalName(privmsgs.getArgs(args))
cb = irc.findCallback(command)
if cb:
method = getattr(cb, command)
if hasattr(method, '__doc__'):
doclines = method.__doc__.splitlines()
simplehelp = doclines.pop(0)
if doclines:
doclines = filter(None, doclines)
doclines = map(str.strip, doclines)
help = ' '.join(doclines)
irc.reply(msg, help)
else:
irc.reply(msg, 'That command has no more help. '\
'The original help is this: %s %s' % \
(command, simplehelp))
else:
irc.error(msg, 'That command has no help at all.')
def bug(self, irc, msg, args):
"""takes no arguments
Log a recent bug. A revent (long) history of the messages received
will be logged, so don't abuse this command or you'll have an upset
admin to deal with.
"""
debug.debugMsg(pprint.pformat(irc.state.history), 'normal')
irc.reply(msg, conf.replySuccess)
def version(self, irc, msg, args):
"""takes no arguments
Returns the version of the current bot.
"""
irc.reply(msg, world.version)
def logfilesize(self, irc, msg, args):
"""takes no arguments
Returns the size of the various logfiles in use.
"""
result = []
for file in os.listdir(conf.logDir):
if file.endswith('.log'):
stats = os.stat(os.path.join(conf.logDir, file))
result.append((file, str(stats.st_size)))
irc.reply(msg, ', '.join(map(': '.join, result)))
2003-03-27 22:34:50 +01:00
def getprefixchar(self, irc, msg, args):
"""takes no arguments
Returns the prefix character(s) the bot is currently using.
"""
irc.reply(msg, repr(conf.prefixChars))
2003-03-27 07:34:48 +01:00
Class = MiscCommands
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: