def __init__(self, context): self.context = context module = self.__class__.__module__ self.config = config = context.config.get(module, {}) self.log = logging.getLogger(module) self.log.debug('Config: %r', config) if 'cmd' in context.config: # in case of config['cmd'] = context.config['cmd'] context.config['cmd'] = self.cmd = config.get('cmd', '!') context.config['re_cmd'] = re.escape(self.cmd) self.use_shlex = self.config.get('use_shlex', True) self.antiflood = self.config.get('antiflood', False) self.case_sensitive = self.config.get('casesensitive', self.case_sensitive) guard = utils.maybedotted(config.get('guard', self.default_policy)) self.log.debug('Guard: %s', guard.__name__) self.guard = guard(context) self.error_format = utils.maybedotted( config.get('error_format', "Invalid arguments.".format)) self.handles = defaultdict(Done) self.tasks = defaultdict(Done) self.aliases = {}
def __init__(self, context): self.context = context module = self.__class__.__module__ self.config = config = context.config.get(module, {}) self.log = logging.getLogger(module) self.log.debug('Config: %r', config) if 'cmd' in context.config: # in case of config['cmd'] = context.config['cmd'] context.config['cmd'] = self.cmd = config.get('cmd', '!') context.config['re_cmd'] = re.escape(self.cmd) self.use_shlex = self.config.get('use_shlex', True) self.antiflood = self.config.get('antiflood', False) self.case_sensitive = self.config.get('casesensitive', self.case_sensitive) guard = utils.maybedotted(config.get('guard', self.default_policy)) self.log.debug('Guard: %s', guard.__name__) self.guard = guard(context) self.error_format = utils.maybedotted(config.get('error_format', "Invalid arguments.".format)) self.handles = defaultdict(Done) self.tasks = defaultdict(Done) self.aliases = {}
def test_maybedotted(self): self.assertTrue(maybedotted('irc3.plugins') is irc3.plugins) self.assertTrue(maybedotted('irc3.utils.IrcString') is IrcString) self.assertTrue(maybedotted(IrcString) is IrcString) self.assertRaises(LookupError, maybedotted, 'irc3.none.none') self.assertRaises(LookupError, maybedotted, 'irc3.none') self.assertRaises(LookupError, maybedotted, None) self.assertRaises(LookupError, maybedotted, '')
def test_maybedotted(self): self.assertTrue( maybedotted('irc3.plugins') is irc3.plugins) self.assertTrue( maybedotted('irc3.utils.IrcString') is IrcString) self.assertTrue( maybedotted(IrcString) is IrcString) self.assertRaises(LookupError, maybedotted, 'irc3.none.none') self.assertRaises(LookupError, maybedotted, 'irc3.none') self.assertRaises(LookupError, maybedotted, None) self.assertRaises(LookupError, maybedotted, '')
def __init__(self, bot): self.bot = bot self.loop = bot.loop self.config = cfg = bot.config.get('dcc', {}) self.config.update(send_limit_rate=int(cfg.get('send_limit_rate', 0)), send_block_size=int( cfg.get('send_block_size', DCCSend.block_size))) self.connections = {} self.protocols = {} for klass in (DCCChat, DCCGet, DCCSend): n = klass.type self.config.update({ n + '_limit': int(cfg.get(n + '_limit', 100)), n + '_user_limit': int(cfg.get(n + '_user_limit', 1)), n + '_accept_timeout': int(cfg.get(n + '_accept_timeout', 60)), n + '_idle_timeout': int(cfg.get(n + '_idle_timeout', 60 * 5)), }) klass = maybedotted(self.config.get(n + '_protocol', klass)) self.connections[n] = {'total': 0, 'masks': defaultdict(dict)} self.protocols[n] = klass self.seeks = {}
def __init__(self, context): self.context = context module = self.__class__.__module__ self.config = config = context.config.get(module, {}) self.log = logging.getLogger(module) self.log.debug('Config: %r', config) if 'cmd' in context.config: # in case of config['cmd'] = context.config['cmd'] context.config['cmd'] = self.cmd = config.get('cmd', '!') if self.cmd in WEIRD_CHARS: context.config['re_cmd'] = r'\%s' % self.cmd else: context.config['re_cmd'] = self.cmd self.antiflood = self.config.get('antiflood', False) self.case_sensitive = self.config.get('casesensitive', self.case_sensitive) guard = utils.maybedotted(config.get('guard', self.default_policy)) self.log.debug('Guard: %s', guard.__name__) self.guard = guard(context) self.handles = defaultdict(Done) self.tasks = defaultdict(Done)
def reconnect(bot, mask, target, args): """force reconnect %%reconnect """ plugin = bot.get_plugin(utils.maybedotted('irc3.plugins.core.Core')) bot.loop.call_soon(plugin.reconnect)
def __init__(self, bot): self.bot = bot self.config = config = bot.config.get(__name__, {}) self.log = logging.getLogger(__name__) self.log.debug('Config: %r', config) bot.config['cmd'] = self.cmd = config.get('cmd', '!') guard = utils.maybedotted(config.get('guard', free_policy)) self.log.debug('Guard: %s', guard.__name__) self.guard = guard(bot)
def callback(context, name, ob): obj = context.context if info.scope == 'class': callback = func.__get__(obj.get_plugin(ob), ob) else: callback = utils.wraps_with_context(func, obj) plugin = obj.get_plugin(utils.maybedotted(commands)) predicates.update(module=func.__module__) cmd_name = predicates.get('name', func.__name__) plugin[cmd_name] = (predicates, callback) obj.log.debug('Register command %r', cmd_name)
def callback(context, name, ob): obj = context.context if info.scope == 'class': callback = func.__get__(obj.get_plugin(ob), ob) else: @functools.wraps(func) def wrapper(*args, **kwargs): return func(obj, *args, **kwargs) callback = wrapper plugin = obj.get_plugin(utils.maybedotted(commands)) predicates.update(module=func.__module__) plugin[func.__name__] = (predicates, callback) obj.log.debug('Register command %r', func.__name__)
def callback(context, name, ob): obj = context.context if info.scope == 'class': callback = func.__get__(obj.get_plugin(ob), ob) else: callback = utils.wraps_with_context(func, obj) plugin = obj.get_plugin(utils.maybedotted(commands)) predicates.update(module=func.__module__) cmd_name = predicates.get('name', func.__name__) if not plugin.case_sensitive: cmd_name = cmd_name.lower() plugin[cmd_name] = (predicates, callback) obj.log.debug('Register command %r', cmd_name)
def create(self, name_or_class, mask, filepath=None, **kwargs): """Create a new DCC connection. Return an ``asyncio.Protocol``""" if isinstance(name_or_class, type): name = name_or_class.type protocol = name_or_class else: name = name_or_class protocol = self.protocols[name] assert name in DCC_TYPES if filepath: kwargs.setdefault('limit_rate', self.config['send_limit_rate']) kwargs['filepath'] = filepath if protocol.type == DCCSend.type: kwargs.setdefault('offset', 0) kwargs.update( filename_safe=slugify(os.path.basename(filepath)), filesize=os.path.getsize(filepath), ) elif protocol.type == DCCGet.type: try: offset = os.path.getsize(filepath) except OSError: offset = 0 kwargs.setdefault('offset', offset) kwargs.setdefault('resume', False) kwargs.setdefault('port', None) f = protocol(mask=mask, ip=int(self.bot.ip), bot=self.bot, loop=self.loop, **kwargs) if kwargs['port']: if self.bot.config.get('dcc_sock_factory'): sock_factory = maybedotted(self.bot.config.dcc_sock_factory) args = dict(sock=sock_factory(self.bot, f.host, f.port)) else: args = dict(host=f.host, port=f.port) task = self.bot.create_task( self.loop.create_connection(f.factory, **args)) task.add_done_callback(partial(self.created, f)) else: task = self.bot.create_task( self.loop.create_server(f.factory, '0.0.0.0', 0, backlog=1)) task.add_done_callback(partial(self.created, f)) return f
def create(self, name_or_class, mask, filepath=None, **kwargs): """Create a new DCC connection. Return an ``asyncio.Protocol``""" if isinstance(name_or_class, type): name = name_or_class.type protocol = name_or_class else: name = name_or_class protocol = self.protocols[name] assert name in DCC_TYPES if filepath: kwargs.setdefault('limit_rate', self.config['send_limit_rate']) kwargs['filepath'] = filepath if protocol.type == DCCSend.type: kwargs.setdefault('offset', 0) kwargs.update( filename_safe=slugify(os.path.basename(filepath)), filesize=os.path.getsize(filepath), ) elif protocol.type == DCCGet.type: try: offset = os.path.getsize(filepath) except OSError: offset = 0 kwargs.setdefault('offset', offset) kwargs.setdefault('resume', False) kwargs.setdefault('port', None) f = protocol( mask=mask, ip=int(self.bot.ip), bot=self.bot, loop=self.loop, **kwargs) if kwargs['port']: if self.bot.config.get('dcc_sock_factory'): sock_factory = maybedotted(self.bot.config.dcc_sock_factory) args = dict(sock=sock_factory(self.bot, f.host, f.port)) else: args = dict(host=f.host, port=f.port) task = self.bot.create_task( self.loop.create_connection(f.factory, **args)) task.add_done_callback(partial(self.created, f)) else: task = self.bot.create_task( self.loop.create_server( f.factory, '0.0.0.0', 0, backlog=1)) task.add_done_callback(partial(self.created, f)) return f
def __init__(self, bot): self.bot = bot self.config = config = bot.config.get(__name__, {}) self.log = logging.getLogger(__name__) self.log.debug('Config: %r', config) if 'cmd' in bot.config: # in case of config['cmd'] = bot.config['cmd'] bot.config['cmd'] = self.cmd = config.get('cmd', '!') self.antiflood = self.config.get('antiflood', False) guard = utils.maybedotted(config.get('guard', free_policy)) self.log.debug('Guard: %s', guard.__name__) self.guard = guard(bot) self.handles = defaultdict(Done)
def callback(context, name, ob): obj = context.context if info.scope == 'class': callback = func.__get__(obj.get_plugin(ob), ob) else: callback = utils.wraps_with_context(func, obj) plugin = obj.get_plugin(utils.maybedotted(commands)) predicates.update(module=func.__module__) cmd_name = predicates.get('name', func.__name__) if not plugin.case_sensitive: cmd_name = cmd_name.lower() plugin[cmd_name] = (predicates, callback) aliases = predicates.get('aliases', None) if aliases is not None: for alias in aliases: plugin.aliases[alias] = cmd_name obj.log.debug('Register command %r %r', cmd_name, aliases) else: obj.log.debug('Register command %r', cmd_name)
def __init__(self, bot): self.bot = bot self.loop = bot.loop self.config = cfg = bot.config.get('dcc', {}) self.config.update( send_limit_rate=int(cfg.get('send_limit_rate', 0)), send_block_size=int(cfg.get('send_block_size', DCCSend.block_size)) ) self.connections = {} self.protocols = {} for klass in (DCCChat, DCCGet, DCCSend): n = klass.type self.config.update({ n + '_limit': int(cfg.get(n + '_limit', 100)), n + '_user_limit': int(cfg.get(n + '_user_limit', 1)), n + '_accept_timeout': int(cfg.get(n + '_accept_timeout', 60)), n + '_idle_timeout': int(cfg.get(n + '_idle_timeout', 60 * 5)), }) klass = maybedotted(self.config.get(n + '_protocol', klass)) self.connections[n] = {'total': 0, 'masks': defaultdict(dict)} self.protocols[n] = klass self.seeks = {}