Skip to content
Browse files

fixed a bug and more

  • Loading branch information...
1 parent ea18c94 commit bdb126b7107516b77c5960c3b170dbdfb263d1b1 @alivesay committed Jan 20, 2013
View
43 omegadoom/configparser.py
@@ -1,37 +1,46 @@
import json
class OmegaDoomConfigParser(object):
- DEFAULT_SCHEMA = [{'key': 'network', 'type': basestring, 'required': True},
- {'key': 'port', 'type': int, 'required': True},
- {'key': 'nickname', 'type': basestring, 'required': True},
- {'key': 'channels', 'type': basestring, 'required': True, 'multivalued': True},
- {'key': 'admins', 'type': basestring, 'required': True, 'multivalued': True},
- {'key': 'nickserv_pass', 'type': basestring, 'required': False}]
+ DEFAULT_SCHEMA = [{'key': 'network'},
+ {'key': 'port', 'type': int},
+ {'key': 'nickname'},
+ {'key': 'channels', 'multivalued': True},
+ {'key': 'admins', 'multivalued': True},
+ {'key': 'redis_host', 'required': False},
+ {'key': 'redis_port', 'type': int, 'required': False},
+ {'key': 'nickserv_pass', 'required': False},
+ {'key': 'command_char', 'required': True}]
def __init__(self, schema = None):
if schema is None:
schema = self.DEFAULT_SCHEMA
if self._validate_schema(schema):
self._schema = schema
- else:
- raise ValueError('invalid schema')
def _validate_schema(self, schema):
- return isinstance(schema, list) and all((isinstance(i, dict) and 'key' in i and 'type' in i for i in schema))
+ return isinstance(schema, list) and all((isinstance(i, dict) and 'key' in i for i in schema))
def _validate_config(self, config, schema):
for entry in schema:
- if entry['key'] in config:
- if entry.get('multivalued'):
- if not all((isinstance(item, entry['type']) for item in config[entry['key']])):
- raise TypeError("'%s' must be a list of type '%s'" % (entry['key'], entry['type']))
- elif not isinstance(config[entry['key']], entry['type']):
- raise TypeError("'%s' must be of type '%s'" % (entry['key'], entry['type']))
- elif entry.get('required'):
- raise KeyError("missing required attribute '%s'" % entry['key'])
+
+ entry_key = entry['key']
+
+ if entry_key in config:
+ entry_type = entry.get('type', basestring)
+
+ if entry.get('multivalued', False):
+
+ if not all((isinstance(item, entry_type) for item in config[entry_key])):
+ raise TypeError("'%s' must be a list of type '%s'" % (entry_key, entry_type))
+
+ elif not isinstance(config[entry_key], entry_type):
+ raise TypeError("'%s' must be of type '%s'" % (entry_key, entry_type))
+
+ elif entry.get('required', True):
+ raise KeyError("missing required attribute '%s'" % entry_key)
def load(self, filename):
View
5 omegadoom/plugins/admin.py
@@ -3,7 +3,12 @@
from omegadoom.plugins.pluginbase import OmegaDoomPluginBase
class OmegaDoomPlugin(OmegaDoomPluginBase):
+
commands = ['join', 'leave', 'quit']
+
+ def __init__(self, config):
+ OmegaDoomPluginBase.__init__(self, config)
+
def run_command(self, protocol, command, data, privmsg):
if command == 'join':
View
15 omegadoom/plugins/default.py
@@ -5,7 +5,8 @@
class OmegaDoomPlugin(OmegaDoomPluginBase):
- commands = ['ping', 'version']
+
+ commands = ['ping', 'version', 'echo']
def setup(self):
@@ -15,11 +16,15 @@ def setup(self):
def run_command(self, protocol, command, data, privmsg):
prefix, channel, message = privmsg
nick, ident, host = OmegaDoomUtil.parse_prefix(prefix)
+ nick_or_channel = nick if channel == self.config['nickname'] else channel
if command == 'ping':
self._ping_requests[nick] = (privmsg, datetime.now())
protocol.ping(nick)
+ elif command == 'echo':
+ protocol.msg(nick_or_channel, message)
+
def notify(self, protocol, event, *args):
if event == 'pong':
@@ -29,13 +34,13 @@ def notify(self, protocol, event, *args):
def _notify_pong(self, protocol, *args):
prefix, secs = args
nick, ident, host = OmegaDoomUtil.parse_prefix(prefix)
-
+
if nick in self._ping_requests:
privmsg, timestamp = self._ping_requests[nick]
prefix, channel, message = privmsg
- protocol.msg(nick if channel == self.config['nickname'] else channel,
- 'CTCP PING reply in %s secs' % (secs))
+ nick_or_channel = nick if channel == self.config['nickname'] else channel
+ protocol.msg(nick_or_channel, 'CTCP PING reply in %s secs' % (secs))
del(self._ping_requests[nick])
@@ -44,5 +49,3 @@ def _notify_pong(self, protocol, *args):
for k,v in self._ping_requests.items():
if (now - v[1]).seconds > 60:
del(self._ping_requests[k])
-
-
View
14 omegadoom/plugins/pluginbase.py
@@ -1,3 +1,5 @@
+import redis
+
from omegadoom.util import OmegaDoomUtil
class OmegaDoomPluginBase(object):
@@ -7,9 +9,16 @@ class OmegaDoomPluginBase(object):
def __init__(self, config):
self._config = config
+ self._redis = redis.Redis(host='localhost', port=6739, db=0)
self.setup()
-
+
+ def _get_config(self):
+ return self._config
+ config = property(_get_config)
+
+
+ # called by __init__
def setup(self):
pass
@@ -25,7 +34,4 @@ def notify(self, protocol, event, *args):
pass
- def _get_config(self):
- return self._config
- config = property(_get_config)
View
21 omegadoom/plugins/rpg.py
@@ -1,18 +1,33 @@
-import random, re
+import random
+import re
+import redis
from omegadoom.plugins.pluginbase import OmegaDoomPluginBase
from omegadoom.util import OmegaDoomUtil
+
class OmegaDoomPlugin(OmegaDoomPluginBase):
- commands = ['roll', 'exp', 'inventory']
+
+ commands = ['roll', 'exp', 'inventory', 'look']
+
+ def __init__(self, config):
+ OmegaDoomPluginBase.__init__(self, config)
+ self._redis = redis.Redis(config['redis_host'], config['redis_port'])
def run_command(self, protocol, command, data, privmsg):
prefix, channel, message = privmsg
nick, ident, host = OmegaDoomUtil.parse_prefix(prefix)
+ target = nick if channel == self.config['nickname'] else channel
+
+
if command == 'roll':
if data:
# http://stackoverflow.com/questions/1031466/evaluate-dice-rolling-notation-strings
f=lambda s:sum(int(c or 0)+sum(random.randint(1,int(b))for i in[0]*int(a or 1))for a,b,c in re.findall(r'(\d*)d(\d+)(\s*[+-]\s*\d+)?',s))
- protocol.msg(nick if channel == self.config['nickname'] else channel, str(f(data)))
+ protocol.msg(target, str(f(data)))
+
+ if command == 'look':
+ protocol.msg(target, 'You see: an empty room')
+
View
14 omegadoom/responder.py
@@ -8,20 +8,20 @@ def __init__(self, config, protocol, plugin_manager):
self._plugin_manager = plugin_manager
- def privmsg(self, user, channel, msg):
- print user, channel, msg
+ def privmsg(self, prefix, channel, message):
+ print prefix, channel, message
- if msg.startswith(self.COMMAND_CHARACTER):
- command = msg.split(' ')[0][1:]
- data = msg[msg.find(command) + len(command):].strip()
+ if message.startswith(self.COMMAND_CHARACTER):
+ command = message.split(' ')[0][1:]
+ data = message[message.find(command) + len(command):].strip()
if command:
self._plugin_manager.run_command(self._protocol,
command,
data,
- (user, channel, msg))
+ (prefix, channel, message))
-
+ # WTF WHY IS THIS HERE?
def pong(self, user, secs):
self._plugin_manager.notify(self._protocol, 'pong', user, secs)
View
9 omegadoom/transport.py
@@ -0,0 +1,9 @@
+
+class OmegaDoomMessage(object):
+
+ def __init__(self, message, config):
+ if message.startswith(self.COMMAND_CHARACTER):
+ command = message.split(' ')[0][1:]
+ data = message[message.find(command) + len(command):].strip()
+
+

0 comments on commit bdb126b

Please sign in to comment.
Something went wrong with that request. Please try again.