XmlRpcServer: expose functionality via XML-RPC

this could be fun. provide an XML-RPC web service that allows for
remote access into the bot. in addition to some obvious stuff, this
includes a method that lets you execute any arbitrary module's arbitrary
method (so be careful) and retrieve the results.

as a side effect this has made it apparent that i need to clean up
some of my modules so that functionality and irc responses are decoupled.
This commit is contained in:
Brian S. Stephan 2012-03-30 00:44:25 -05:00
parent 25d0f2f5c3
commit 91faebf33a
1 changed files with 112 additions and 0 deletions

112
modules/XmlRpcServer.py Normal file
View File

@ -0,0 +1,112 @@
"""
XmlRpcServer - access dr.botzo functionality via XML-RPC
Copyright (C) 2012 Brian S. Stephan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import thread
from SimpleXMLRPCServer import SimpleXMLRPCServer
from extlib import irclib
from Module import Module
class DrBotzoMethods:
"""Methods to expose to the XML-RPC server."""
def __init__(self, irc, config, server):
"""Store the same stuff the core module would, since we'll probably need it."""
self.irc = irc
self.config = config
self.server = server
def echo(self, message):
"""
Just reply to the client, for testing purposes.
Arguments: message - the message to echo
"""
return message
def say(self, target, message):
"""
Say a message in a channel/to a nick.
Arguments: target - the nick/channel to privmsg
message - the message to send
"""
self.irc.server.privmsg(target, message)
return "OK"
def execute_module_method(self, modname, method, args):
"""
Execute the method (with arguments) of the specified module.
Arguments: modname - the loaded module to retrieve
method - the method to call from that module
args - the arguments to provide to the method as a pythonic tuple
"""
for module in self.irc.modlist:
if modname == module.__class__.__name__:
try:
func = getattr(module, method)
except AttributeError:
return "couldn't find " + method + " in found module " + modname
if hasattr(func, '__call__'):
return func(*args)
else:
return method + " in found module " + modname + " is not callable"
return "couldn't find " + modname
class XmlRpcServer(Module):
"""A module to expose an XML-RPC service, to achieve various things."""
def __init__(self, irc, config, server):
"""Create the XML-RPC server."""
Module.__init__(self, irc, config, server)
self.xmlrpc = None
self.funcs = DrBotzoMethods(irc, config, server)
self.xmlrpc = SimpleXMLRPCServer(("localhost", 8180))
self.xmlrpc.register_introspection_functions()
self.xmlrpc.register_instance(self.funcs)
thread.start_new_thread(self._listen, ())
def _listen(self):
"""Begin listening. Hopefully this was called in a new thread."""
self.xmlrpc.serve_forever()
def do(self, connection, event, nick, userhost, what, admin_unlocked):
"""Do nothing."""
return
def shutdown(self):
"""Shut down the XML-RPC server."""
if self.xmlrpc is not None:
self.xmlrpc.shutdown()
self.xmlrpc.server_close()
# vi:tabstop=4:expandtab:autoindent