Branch: refs/heads/master Author: Enrico Tröger enrico.troeger@uvena.de Committer: Enrico Tröger enrico.troeger@uvena.de Date: Sun, 07 Jul 2013 08:56:01 UTC Commit: e01d444e54026a03afbff6105e2979f9fc7d43cc https://github.com/geany/infrastructure/commit/e01d444e54026a03afbff6105e297...
Log Message: ----------- Add Geany related Supybot plugins
Modified Paths: -------------- README.md ircbot-plugins/Geany/README.txt ircbot-plugins/Geany/__init__.py ircbot-plugins/Geany/config.py ircbot-plugins/Geany/plugin.py ircbot-plugins/Geany/timer.py ircbot-plugins/SupySocketServer/README.txt ircbot-plugins/SupySocketServer/__init__.py ircbot-plugins/SupySocketServer/config.py ircbot-plugins/SupySocketServer/plugin.py
Modified: README.md 12 files changed, 11 insertions(+), 1 deletions(-) =================================================================== @@ -2,7 +2,7 @@ Geany Infrastructure Scripts ============================
-The scripts in this repository are used for some purposes on geany.org. +The scripts in this repository are used for various purposes on geany.org.
Basically they provide some additional and cool functions like announcing GIT commits to the IRC channel, maintain the GIT mirror repository and similar tasks. @@ -52,3 +52,13 @@ If you want to add or remove a repository maintained by these scripts, follow th
* Open http://git.geany.org/ in your browser and check whether the new repository is visible and has files. + + +IRC Bot Plugins +=============== + +In the directory ircbot-plugins there are two plugins for the IRC bot Supybot +(http://www.supybot.org/). + +The plugins enhance the used Supybot instance on #geany by various useful and funny +features like a bunch of !commands. For details, read the source code.
Modified: ircbot-plugins/Geany/README.txt 5 files changed, 5 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,5 @@ +Miscellaneous fun commands we used to have in #geany on Freenode.net. + +The code is mostly a big hack and not generally usable. +It has hard-coded paths and quite special functionalities. Still, +feel free to use and/or modify it as necessary.
Modified: ircbot-plugins/Geany/__init__.py 61 files changed, 61 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,61 @@ +### +# -*- coding: utf-8 -*- +# Copyright (c) 2003-2005, Jeremiah Fincher +# Copyright (c) 2010, Enrico Tröger +# 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. +### + +""" +Provides various fun commands. +""" + +import supybot +import supybot.world as world + +# Use this for the version of this plugin. You may wish to put a CVS keyword +# in here if you're keeping the plugin in CVS or some similar system. +__version__ = "1.0" + +__author__ = supybot.Author('Enrico Tröger', 'eht16', + 'enrico.troeger@uvena.de') + +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = {} + +import config +import plugin +reload(plugin) # In case we're being reloaded. +# Add more reloads here if you add third-party modules and want them to be +# reloaded when this plugin is reloaded. Don't forget to import them as well! + + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Modified: ircbot-plugins/Geany/config.py 46 files changed, 46 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,46 @@ +### +# -*- coding: utf-8 -*- +# Copyright (c) 2003-2005, Jeremiah Fincher +# Copyright (c) 2010, Enrico Tröger +# 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. +### + +import supybot.conf as conf +import supybot.registry as registry + +def configure(advanced): + # This will be called by supybot to configure this module. advanced is + # a bool that specifies whether the user identified himself as an advanced + # user or not. You should effect your configuration by manipulating the + # registry as appropriate. + conf.registerPlugin('Geany', True) + + +Geany = conf.registerPlugin('Geany') + + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Modified: ircbot-plugins/Geany/plugin.py 306 files changed, 306 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,306 @@ +### +# -*- coding: utf-8 -*- +# Copyright (c) 2003-2005, Jeremiah Fincher +# Copyright (c) 2010, Enrico Tröger +# 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. +### + +from timer import RepeatTimer + +import random +import threading +from ConfigParser import SafeConfigParser + +import supybot.utils as utils +from supybot.commands import * +import supybot.ircmsgs as ircmsgs +import supybot.ircutils as ircutils +import supybot.callbacks as callbacks + +import supybot.conf as conf + + +GOODIES = { + 'coffee': 'A nice sexy waitress brings %s a big cup of coffee!', + 'coke': 'A nice sexy waitress brings %s a cool bottle of coke!', + 'pepsi': 'A nice sexy waitress brings %s a cool bottle of Pepsi!', + 'juice': 'A nice sexy waitress brings %s a glass of fresh juice!', + 'vodka': 'A nice sexy waitress brings %s a shot glass of vodka!', + 'beer': 'A nice sexy waitress brings %s a nice bottle of beer!', + 'tea': 'A nice sexy waitress brings %s a cup of hot tea!', + 'milk': 'A nice sexy waitress brings %s a glass of fresh, cold milk', + 'chocolate': 'A nice sexy waitress brings %s a piece of sweet chocolate', + 'pizza': 'Someone calls Mario, and he brings %s a tasty hawaiian pizza!' +}; + + +class Geany(callbacks.Plugin): + def __init__(self, irc): + self.__parent = super(Geany, self) + self.__parent.__init__(irc) + self.timer = RepeatTimer(180.0, self._write_user_list, self.log, 0, [irc]) + self.timer.start() + self.help_database = {} + self._read_help_database() + + def die(self): + if self.timer: + threading.Thread(target=self.timer.cancel).start() + + def _read_help_database(self): + config = SafeConfigParser() + config.read('help_database.rc') + for key, value in config.items('general'): + self.help_database[key] = value + + def _write_help_database(self): + conv = lambda dic: ['%s: %s' % (k, v) for (k, v) in dic.iteritems()] + data = conv(self.help_database) + data.sort() + f = open('help_database.rc', 'w') + f.write('[general]\n') + f.write('\n'.join(data)) + f.close() + + def _write_user_list(self, irc): + exclude_nicks = [ 'ChanServ', self._get_nick_name() ] + def filter_services(value): + return value not in exclude_nicks + + if hasattr(irc, 'getRealIrc'): + state = irc.getRealIrc().state + elif hasattr(irc, 'state'): + state = irc.state + else: + state = None + + if state: + channel_users = state.channels['#geany'].users + # filter myself and ChanServ + users = filter(filter_services, channel_users) + + f = open('/srv/www/irc.geany.org/irc_userlist', 'w') + f.write('\n'.join(users)) + f.close() + + def _get_nick_name(self): + """ + Return the configured nick name + """ + return str(conf.supybot.nick) + #~ return self.registryValue('nick') + + def _get_command_name(self, msg, fallback='help'): + """ + Parse and return the actual command name + """ + try: + cmd = msg.args[1].split()[0] + if cmd[0] == '!': + cmd = cmd[1:] + except: + cmd = fallback + return cmd + + def _process_help_request(self, irc, text): + if text == 'keywords': + keywords = sorted(self.help_database.keys()) + irc.reply(' '.join(keywords)) + return + + try: + result = self.help_database[text] + if result: + while result[0] == '@': + # read alias + # (The outer while loop could easily cause endless lookups if there are + # circular aliases defined, let's hope users stay nice.) + result = self.help_database[result[1:]] + irc.reply(result) + except KeyError: + pass + + def doPrivmsg(self, irc, msg): + (recipients, text) = msg.args + if text.startswith('?? '): + self._process_help_request(irc, text[3:]) + + def goodie(self, irc, msg, args, text): + """takes no arguments + + Request a goodie + """ + if not text: + rcpt = msg.nick + else: + text = text[0].split() + if len(text) > 1: + if text[0] == 'for': + if text[1] == 'me': + rcpt = msg.nick + else: + rcpt = text[1] + else: + rcpt = text[0] + else: + rcpt = text[0] + + cmd = self._get_command_name(msg, 'tea') + try: + irc.reply(GOODIES[cmd] % rcpt) + except KeyError: + pass + + def listgoodies(self, irc, msg, args, text): + """takes no arguments + + Lists available goodies + """ + available_goodies = sorted(GOODIES.keys()) + available_goodies = ', '.join(available_goodies) + text = 'A nice sexy waitress offers the following goodies for you: %s' % available_goodies + irc.reply(text) + + def hello(self, irc, msg, args, text): + """takes no arguments + + Greetings + """ + cmd = self._get_command_name(msg, 'hi') + text = 'Hi %s. My name is %s and I'm here to offer additional services to you! Try "?? help" for general information.' % (msg.nick, self._get_nick_name()) + irc.reply(text) + + def thanks(self, irc, msg, args, text): + """takes no arguments + + Thanks + """ + cmd = self._get_command_name(msg, 'thanks') + text = '%s, no problem. It was a pleasure to serve you.' % (msg.nick) + irc.reply(text) + + def test(self, irc, msg, args, text): + """takes no arguments + + Bah, tests + """ + irc.reply('I don't like tests!') + + def _learn(self, key, value): + + update = key in self.help_database + + self.help_database[key] = value + + self._write_help_database() + + return update + + def learn(self, irc, msg, args, key, value): + """newKeyword Text... + + With the command !learn you can add new keywords to the database. + Use "!learn newKeyword Text which should be added" to add new keywords. + Use this with care! + """ + update = self._learn(key, value) + + if update: + irc.reply('Existing keyword "%s" was updated' % key) + else: + irc.reply('New keyword "%s" was added' % key) + + def alias(self, irc, msg, args, dest, source): + """newWord existingWord + + Type '!alias newWord existingWord' to create a new alias, e.g. '!alias svn subversion'. + """ + if not source in self.help_database: + irc.reply('Alias "%s" could not be created because the target does not exist' % dest) + return + + update = self._learn(dest, '@%s' % source) + + if update: + irc.reply('Existing alias "%s" was updated' % dest) + else: + irc.reply('New alias "%s" was added' % dest) + + def moo(self, irc, msg, args): + """takes no arguments + + Have you mooed today? + """ + if random.randrange(0, 2): + text = """ ^__^ + (oo) + /-----(__) + / | || + * /\---/\ + ~~ ~~ +.."Have you mooed today?"..""" + for line in text.split('\n'): + irc.reply(line) + else: + irc.reply('I have Super Cow Powers. Have you mooed today?') + + + # "decorate" our commands (wrap is a decorator replacement for old Python versions) + tea = wrap(goodie, [ optional(many('text')) ]) + coffee = wrap(goodie, [ optional(many('text')) ]) + coke = wrap(goodie, [ optional(many('text')) ]) + pepsi = wrap(goodie, [ optional(many('text')) ]) + juice = wrap(goodie, [ optional(many('text')) ]) + vodka = wrap(goodie, [ optional(many('text')) ]) + beer = wrap(goodie, [ optional(many('text')) ]) + pizza = wrap(goodie, [ optional(many('text')) ]) + chocolate = wrap(goodie, [ optional(many('text')) ]) + milk = wrap(goodie, [ optional(many('text')) ]) + goodies = wrap(listgoodies, [ optional(many('text')) ]) + goods = wrap(listgoodies, [ optional(many('text')) ]) + + + hi = wrap(hello, [ optional(many('text')) ]) + hello = wrap(hello, [ optional(many('text')) ]) + hey = wrap(hello, [ optional(many('text')) ]) + + thanks = wrap(thanks, [ optional(many('text')) ]) + thankyou = wrap(thanks, [ optional(many('text')) ]) + thx = wrap(thanks, [ optional(many('text')) ]) + + learn = wrap(learn, [ 'something', 'text' ]) + alias = wrap(alias, [ 'something', 'something' ]) + + test = wrap(test, [ optional(many('text')) ]) + moo = wrap(moo) + + + +Class = Geany + + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Modified: ircbot-plugins/Geany/timer.py 59 files changed, 59 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,59 @@ +### +# -*- coding: utf-8 -*- +# Copyright (c) 2003-2005, Jeremiah Fincher +# Copyright (c) 2010, Enrico Tröger +# 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. +### + +from threading import Event, Thread + + +class RepeatTimer(Thread): + def __init__(self, interval, function, logger, iterations=0, args=[], kwargs={}): + Thread.__init__(self) + self.interval = interval + self.function = function + self.iterations = iterations + self.args = args + self.kwargs = kwargs + self.finished = Event() + self.logger = logger + + def run(self): + count = 0 + self.logger.info(u'start') + while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations): + self.finished.wait(self.interval) + if not self.finished.isSet(): + self.function(*self.args, **self.kwargs) + count += 1 + + def cancel(self): + self.logger.info(u'cancelled') + self.finished.set() + +
Modified: ircbot-plugins/SupySocketServer/README.txt 4 files changed, 4 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,4 @@ + +External Control + +Version: 1.0
Modified: ircbot-plugins/SupySocketServer/__init__.py 66 files changed, 66 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,66 @@ +### +# -*- coding: utf-8 -*- +# Copyright (c) 2007, Ali Afshar +# 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. + +### + +""" +Add a description of the plugin (to be presented to the user inside the wizard) +here. This should describe *what* the plugin does. +""" + +import supybot +import supybot.world as world + +# Use this for the version of this plugin. You may wish to put a CVS keyword +# in here if you're keeping the plugin in CVS or some similar system. +__version__ = "" + +# XXX Replace this with an appropriate author or supybot.Author instance. +__author__ = supybot.Author('Enrico Tröger', 'eht16', + 'enrico.troeger@uvena.de') + +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = {} + +# This is a url where the most recent plugin package can be downloaded. +__url__ = '' # 'http://supybot.com/Members/yourname/ExternalControl/download' + +import config +import plugin +reload(plugin) # In case we're being reloaded. +# Add more reloads here if you add third-party modules and want them to be +# reloaded when this plugin is reloaded. Don't forget to import them as well! + + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
Modified: ircbot-plugins/SupySocketServer/config.py 58 files changed, 58 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,58 @@ +### +# Copyright (c) 2007, Ali Afshar +# 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. + +### + +import supybot.conf as conf +import supybot.registry as registry + +def configure(advanced): + # This will be called by supybot to configure this module. advanced is + # a bool that specifies whether the user identified himself as an advanced + # user or not. You should effect your configuration by manipulating the + # registry as appropriate. + conf.registerPlugin('SupySocketServer', True) + + +SupySocketServer = conf.registerPlugin('SupySocketServer') +# This is where your configuration variables (if any) should go. For example: +# conf.registerGlobalValue(ExternalControl, 'someConfigVariableName', +# registry.Boolean(False, """Help for someConfigVariableName.""")) + +conf.registerGlobalValue(SupySocketServer, 'port', + registry.Integer(7766, + """The port that the external control server + should wait for requests on.""")) + +conf.registerGlobalValue(SupySocketServer, 'host', + registry.String('localhost', + """The host that the external control server + should wait for requests on.""")) + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
Modified: ircbot-plugins/SupySocketServer/plugin.py 245 files changed, 245 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,245 @@ +### +# Copyright (c) 2007, Ali Afshar +# 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. + +### + +import select +import threading +import socket +import SocketServer + + +import supybot.world as world +from supybot.commands import * +from supybot.log import getPluginLogger +import supybot.ircmsgs as ircmsgs +import supybot.callbacks as callbacks + + + +class RequestHandler(SocketServer.StreamRequestHandler): + + def handle(self): + # data should be: 'network channel message', e.g. + # 'Freenode #geany blah blah' + data = self.rfile.readline().strip() + self.server.logger.debug(u'got data from socket: %s' % data) + network, channel, message = data.split(' ', 2) + ci = ControlInstance() + ci.privmsg(network, channel, message) + + +class ControlInstance(object): + + def privmsg(self, network, personorchannel, message): + target_irc, target = self._get_irc_and_target(network, personorchannel) + msg = ircmsgs.privmsg(target, message) + target_irc.sendMsg(msg) + + def _get_irc(self, network): + for irc in world.ircs: + if irc.network == network: + return irc + + def _get_person_or_channel(self, irc, personorchannel): + if personorchannel.startswith('#'): + for channel in irc.state.channels: + if channel == personorchannel: + return channel + else: + return personorchannel + + def _get_irc_and_target(self, network, personorchannel): + target_irc = self._get_irc(network) + if target_irc is None: + raise Exception('Not on Network: %s' % network) + target = self._get_person_or_channel(target_irc, personorchannel) + if target is None: + raise Exception('Not on Channel: %s' % personorchannel) + return target_irc, target + + +class SocketServerImpl(SocketServer.TCPServer): + + timeout = None + allow_reuse_address = True + address_family = socket.AF_INET + socket_type = socket.SOCK_STREAM + request_queue_size = 5 + + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): + """Constructor. May be extended, do not override.""" + SocketServer.BaseServer.__init__(self, server_address, RequestHandlerClass) + self.socket = socket.socket(self.address_family, self.socket_type) + self.__is_shut_down = threading.Event() + self.__serving = False + if bind_and_activate: + self.server_bind() + self.server_activate() + + def server_bind(self): + """Called by constructor to bind the socket. + + May be overridden. + + """ + if self.allow_reuse_address: + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.bind(self.server_address) + self.server_address = self.socket.getsockname() + + def server_activate(self): + """Called by constructor to activate the server. + + May be overridden. + + """ + self.socket.listen(self.request_queue_size) + + def serve_forever(self, poll_interval=0.5): + """Handle one request at a time until shutdown. + + Polls for shutdown every poll_interval seconds. Ignores + self.timeout. If you need to do periodic tasks, do them in + another thread. + """ + self.logger.info(u'listen') + self.__serving = True + self.__is_shut_down.clear() + while self.__serving: + # XXX: Consider using another file descriptor or + # connecting to the socket to wake this up instead of + # polling. Polling reduces our responsiveness to a + # shutdown request and wastes cpu at all other times. + r = select.select([self], [], [], poll_interval)[0] + if r: + self._handle_request_noblock() + self.__is_shut_down.set() + self.logger.info(u'sucessfully shut down') + + def shutdown(self): + """Stops the serve_forever loop. + + Blocks until the loop has finished. This must be called while + serve_forever() is running in another thread, or it will + deadlock. + """ + self.logger.info(u'shutdown called') + self.__serving = False + self.__is_shut_down.wait() + + def _handle_request_noblock(self): + """Handle one request, without blocking. + + I assume that select.select has returned that the socket is + readable before this function was called, so there should be + no risk of blocking in get_request(). + """ + try: + request, client_address = self.get_request() + except socket.error: + return + if self.verify_request(request, client_address): + try: + self.process_request(request, client_address) + except: + self.handle_error(request, client_address) + self.close_request(request) + + def handle_request(self): + """Handle one request, possibly blocking. + + Respects self.timeout. + """ + # Support people who used socket.settimeout() to escape + # handle_request before self.timeout was available. + timeout = self.socket.gettimeout() + if timeout is None: + timeout = self.timeout + elif self.timeout is not None: + timeout = min(timeout, self.timeout) + fd_sets = select.select([self], [], [], timeout) + if not fd_sets[0]: + self.handle_timeout() + return + self._handle_request_noblock() + + +class SupySocketServer(callbacks.Plugin): + """Add the help for "@plugin help SupySocketServer" here + This should describe *how* to use this plugin.""" + threaded = True + + def __init__(self, irc): + callbacks.Plugin.__init__(self, irc) + self._server = None + self._server_thread = None + self._start_server_in_thread() + + def _start_server_in_thread(self): + self._server_thread = threading.Thread(target=self._start_server) + self._server_thread.daemon = True + self._server_thread.start() + + def _start_server(self): + host = self.registryValue('host') + port = self.registryValue('port') + self._server = SocketServerImpl((host, port), RequestHandler) + self._server.logger = self.log + self._server.serve_forever() + + def die(self): + if self._server: + self._server.shutdown() + self._server_thread.join() + + + def outFilter(self, irc, msg): + if msg.inReplyTo: + if msg.inReplyTo.supysocketserver: + target_irc, target, notice = msg.inReplyTo.supysocketserver + self._reply_command(target_irc, target, msg, notice) + return None + else: + return msg + else: + return msg + + def _reply_command(self, target_irc, target, msg, notice): + if notice: + factory = ircmsgs.notice + else: + factory = ircmsgs.privmsg + reply_msg = factory(target, msg.args[1]) + target_irc.sendMsg(reply_msg) + + +Class = SupySocketServer + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).