Example #1
0
 def is_allowed(self, name_or_class, mask):  # pragma: no cover
     """Return True is a new connection is allowed"""
     if isclass(name_or_class):
         name = name_or_class.type
     else:
         name = name_or_class
     info = self.connections[name]
     limit = self.config[name + '_limit']
     if limit and info['total'] >= limit:
         msg = (
             "Sorry, there is too much DCC %s active. Please try again "
             "later.") % name.upper()
         self.bot.notice(mask, msg)
         return False
     if mask not in info['masks']:
         return True
     limit = self.config[name + '_user_limit']
     if limit and info['masks'][mask] >= limit:
         msg = (
             "Sorry, you have too many DCC %s active. Close the other "
             "connection(s) or wait a few seconds and try again."
         ) % name.upper()
         self.bot.notice(mask, msg)
         return False
     return True
Example #2
0
    def __init__(self, bot):
        bot.feeds = self
        self.bot = bot

        config = bot.config.get(__name__, {})

        self.directory = os.path.expanduser(
            config.get('directory', '~/.irc3/feeds'))
        if not os.path.isdir(self.directory):
            os.makedirs(self.directory)

        hook = config.get('hook', default_hook)
        hook = irc3.utils.maybedotted(hook)
        if isclass(hook):
            hook = hook(bot)
        self.hook = hook

        dispatcher = config.get('dispatcher', default_dispatcher)
        dispatcher = irc3.utils.maybedotted(dispatcher)
        self.dispatcher = dispatcher(bot)

        self.max_workers = int(config.get('max_workers', 5))
        delay = int(config.get('delay', 5))
        self.delay = delay * 60

        feed_config = dict(
            fmt=config.get('fmt', '[{feed.name}] {entry.title} {entry.link}'),
            delay=delay,
            channels=config.get('channels', ''),
            headers=self.headers,
            time=0,
        )

        self.feeds = {}
        for name, feed in config.items():
            if str(feed).startswith('http'):
                feeds = []
                filenames = []
                for i, feed in enumerate(irc3.utils.as_list(feed)):
                    filename = os.path.join(self.directory,
                                            name.replace('/', '_'))
                    filenames.append('{0}.{1}.feed'.format(filename, i))
                    feeds.append(feed)
                feed = dict(
                    feed_config,
                    name=str(name),
                    feeds=feeds,
                    filenames=filenames,
                    **irc3.utils.extract_config(config, str(name))
                )
                feed['delay'] = feed['delay'] * 60
                channels = irc3.utils.as_list(feed['channels'])
                feed['channels'] = [irc3.utils.as_channel(c) for c in channels]
                self.bot.log.debug(feed)
                self.feeds[name] = feed

        self.imports()
Example #3
0
    def create(self, name_or_class, mask, filepath=None, **kwargs):
        """Create a new DCC connection. Return an ``asyncio.Protocol``"""
        if isclass(name_or_class):
            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']:
            task = asyncio. async (self.loop.create_connection(
                f.factory, f.host, f.port),
                                   loop=self.loop)
            task.add_done_callback(partial(self.created, f))
        else:
            task = asyncio. async (self.loop.create_server(f.factory,
                                                           '0.0.0.0',
                                                           0,
                                                           backlog=1),
                                   loop=self.loop)
            task.add_done_callback(partial(self.created, f))
        return f
Example #4
0
    def create(self, name_or_class, mask, filepath=None, **kwargs):
        """Create a new DCC connection. Return an ``asyncio.Protocol``"""
        if isclass(name_or_class):
            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']:
            task = asyncio.async(
                self.loop.create_connection(f.factory, f.host, f.port),
                loop=self.loop)
            task.add_done_callback(partial(self.created, f))
        else:
            task = asyncio.async(
                self.loop.create_server(
                    f.factory, '0.0.0.0', 0, backlog=1),
                loop=self.loop)
            task.add_done_callback(partial(self.created, f))
        return f
Example #5
0
 def is_allowed(self, name_or_class, mask):  # pragma: no cover
     """Return True is a new connection is allowed"""
     if isclass(name_or_class):
         name = name_or_class.type
     else:
         name = name_or_class
     info = self.connections[name]
     limit = self.config[name + '_limit']
     if limit and info['total'] >= limit:
         msg = ("Sorry, there is too much DCC %s active. Please try again "
                "later.") % name.upper()
         self.bot.notice(mask, msg)
         return False
     if mask not in info['masks']:
         return True
     limit = self.config[name + '_user_limit']
     if limit and info['masks'][mask] >= limit:
         msg = ("Sorry, you have too many DCC %s active. Close the other "
                "connection(s) or wait a few seconds and try again."
                ) % name.upper()
         self.bot.notice(mask, msg)
         return False
     return True