Skip to content

Commit

Permalink
Added subcommands support ie. !log tail [args] will try to find the
Browse files Browse the repository at this point in the history
method log_tail
  • Loading branch information
gbin committed Jun 14, 2012
1 parent 87304bb commit 3786d6b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,8 @@ Version 1.1.2 (2012-06-)
--------------------------
Don't nag the user for irrelevant settings from the setting-template
Added a message size security in the framework to avoid getting banned from servers when a plugin spills too much
Added subcommands supports like the function log_tail will match !log tail [args]
Added a test mode (-t) to ease plugin development (no need to have XMPP client / server to install and connect to in order to test the bot)

Version 1.1.1 (2012-06-12)
--------------------------
Expand Down
12 changes: 8 additions & 4 deletions errbot/errBot.py
Expand Up @@ -304,13 +304,17 @@ def update(self, mess, args):
return "Done."

@botcmd
def taillog(self, mess, args):
""" Display a tail of the log
use : !log
def log_tail(self, mess, args):
""" Display a tail of the log of n lines or 40 by default
use : !log tail 10
"""
#admin_only(mess) # uncomment if paranoid.
n = 40
if args.isdigit():
n = int(args)

if BOT_LOG_FILE:
with open(BOT_LOG_FILE, 'r') as f:
return tail(f, 40)
return tail(f, n)
return 'No log is configured, please define BOT_LOG_FILE in config.py'

46 changes: 27 additions & 19 deletions errbot/jabberbot.py
Expand Up @@ -398,16 +398,16 @@ def build_message(self, text):
# FIXME unescape " etc.
message = xmpp.protocol.Message(body=text_plain)
# Start creating a xhtml body
html = xmpp.Node('html',\
html = xmpp.Node('html',
{'xmlns': 'http://jabber.org/protocol/xhtml-im'})
try:
html.addChild(node=xmpp.simplexml.XML2Node(\
html.addChild(node=xmpp.simplexml.XML2Node(
"<body xmlns='http://www.w3.org/1999/xhtml'>" +\
text.encode('utf-8') + "</body>"))
message.addChild(node=html)
except Exception, e:
# Didn't work, incorrect markup or something.
self.log.debug('An error while building a xhtml message. '\
self.log.debug('An error while building a xhtml message. '
'Fallback to normal messagebody')
# Fallback - don't sanitize invalid input. User is responsible!
message = None
Expand Down Expand Up @@ -578,23 +578,31 @@ def callback_message(self, conn, mess):
# FIXME i am not threadsafe
self.__threads[jid] = mess.getThread()

if text.startswith('!!'):
text = text[2:]
private = False
elif text.startswith('!'):
text = text[1:]
private = True
else:
if not text.startswith('!'):
return

if ' ' in text:
command, args = text.split(' ', 1)
else:
command, args = text, ''
cmd = command.lower()
self.log.info("received command = %s" % cmd)
text = text[1:]
text_split = text.strip().split(' ')

cmd = None
command = None
args = ''
if len(text_split) > 1:
command = (text_split[0] + '_' + text_split[1]).lower()
if self.commands.has_key(command):
cmd = command
args = ' '.join(text_split[2:])

if not cmd:
command = text_split[0].lower()
if self.commands.has_key(command):
cmd = command
if len(text_split) > 1:
args = ' '.join(text_split[1:])

self.log.info("received command = %s matching [%s] with parameters [%s]" % (command, cmd, args))

if self.commands.has_key(cmd):
if cmd:
def execute_and_send():
try:
reply = self.commands[cmd](mess, args)
Expand All @@ -621,8 +629,8 @@ def execute_and_send():
if type == 'groupchat':
default_reply = None
else:
default_reply = self.MSG_UNKNOWN_COMMAND % {'command': cmd}
reply = self.unknown_command(mess, cmd, args)
default_reply = self.MSG_UNKNOWN_COMMAND % {'command': command}
reply = self.unknown_command(mess, command, args)
if reply is None:
reply = default_reply
if reply:
Expand Down

0 comments on commit 3786d6b

Please sign in to comment.