Example #1
0
    def summon(self, event, who, source):
        if not source:
            source = self.default_source

        if source.lower() not in ibid.sources:
            event.addresponse(u"I'm afraid that I'm not connected to %s",
                              source)
            return

        account = event.session.query(Account) \
            .options(eagerload('identities')) \
            .join('identities') \
            .filter(
                or_(
                    and_(
                        Identity.identity == who,
                        Identity.source == event.source,
                    ),
                    Account.username == who,
                )) \
            .first()

        if account:
            for other_identity in [
                    id for id in account.identities
                    if id.source.lower() == source.lower()
            ]:
                if any(True for channel in ibid.channels[
                        other_identity.source].itervalues()
                       if other_identity.id in channel):
                    event.addresponse(
                        u'Your presence has been requested by '
                        u'%(who)s in %(channel)s on %(source)s.', {
                            'who': event.sender['nick'],
                            'channel':
                            (not event.public) and u'private' or event.channel,
                            'source': event.source,
                        },
                        target=other_identity.identity,
                        source=other_identity.source,
                        address=False)
                    event.addresponse(True)
                else:
                    event.addresponse(
                        u"Sorry %s doesn't appear to be available right now.",
                        who)
                return

        event.addresponse(
            u"Sorry, I don't know how to find %(who)s on %(source)s. "
            u'%(who)s must first link an identity on %(source)s.', {
                'who': who,
                'source': source,
            })
        return
Example #2
0
    def search(self, event, limit, search_type, pattern, start):
        limit = limit and min(int(limit), self.limit) or self.default
        start = start and max(int(start) - 1, 0) or 0

        search_type = search_type and search_type.lower() or u""

        origpattern = pattern
        m = self.regex_re.match(pattern)
        is_regex = False
        if m:
            pattern = m.group(1)
            is_regex = bool(m.group(2))

        # Hack: We replace $arg with _%, but this won't match a partial
        # "$arg" string
        if is_regex:
            filter_op = get_regexp_op(event.session)
            name_pattern = pattern.replace(r'\$arg', '_%')
        else:
            filter_op = lambda x, y: x.like(y, escape='#')
            pattern = '%%%s%%' % escape_like_re.sub(r'#\1', pattern)
            name_pattern = pattern.replace('$arg', '#_#%')

        query = event.session.query(Factoid)\
                             .join(Factoid.names).add_entity(FactoidName)\
                             .join(Factoid.values)

        if search_type.startswith('fact'):
            query = query.filter(filter_op(FactoidName.name, name_pattern))
        elif search_type.startswith('value'):
            query = query.filter(filter_op(FactoidValue.value, pattern))
        else:
            query = query.filter(
                or_(filter_op(FactoidName.name, name_pattern),
                    filter_op(FactoidValue.value, pattern)))

        # Pre-evalute the iterable or the if statement will be True in SQLAlchemy 0.4. LP: #383286
        matches = [match for match in query.all()]

        bounded_matches = matches[start:start + limit]
        if bounded_matches:
            event.addresponse(u'; '.join(
                u'%s [%s]' % (fname.name, len(factoid.values))
                for factoid, fname in bounded_matches))
        elif len(matches):
            event.addresponse(
                u"I could only find %(number)d things that matched '%(pattern)s'",
                {
                    u'number': len(matches),
                    u'pattern': origpattern,
                })
        else:
            event.addresponse(u"I couldn't find anything that matched '%s'" %
                              origpattern)
Example #3
0
    def summon(self, event, who, source):
        if not source:
            source = self.default_source

        if source.lower() not in ibid.sources:
            event.addresponse(u"I'm afraid that I'm not connected to %s",
                              source)
            return

        account = event.session.query(Account) \
            .options(eagerload('identities')) \
            .join('identities') \
            .filter(
                or_(
                    and_(
                        Identity.identity == who,
                        Identity.source == event.source,
                    ),
                    Account.username == who,
                )) \
            .first()

        if account:
            for other_identity in [id for id
                    in account.identities
                    if id.source.lower() == source.lower()]:
                if any(True for channel
                        in ibid.channels[other_identity.source].itervalues()
                        if other_identity.id in channel):
                    event.addresponse(u'Your presence has been requested by '
                                      u'%(who)s in %(channel)s on %(source)s.',
                        {
                            'who': event.sender['nick'],
                            'channel': (not event.public)
                                    and u'private' or event.channel,
                            'source': event.source,
                        }, target=other_identity.identity,
                        source=other_identity.source, address=False)
                    event.addresponse(True)
                else:
                    event.addresponse(
                        u"Sorry %s doesn't appear to be available right now.",
                        who)
                return

        event.addresponse(
                u"Sorry, I don't know how to find %(who)s on %(source)s. "
                u'%(who)s must first link an identity on %(source)s.', {
                    'who': who,
                    'source': source,
        })
        return
Example #4
0
    def search(self, event, limit, search_type, pattern, start):
        limit = limit and min(int(limit), self.limit) or self.default
        start = start and max(int(start) - 1, 0) or 0

        search_type = search_type and search_type.lower() or u""

        origpattern = pattern
        m = self.regex_re.match(pattern)
        is_regex = False
        if m:
            pattern = m.group(1)
            is_regex = bool(m.group(2))

        # Hack: We replace $arg with _%, but this won't match a partial
        # "$arg" string
        if is_regex:
            filter_op = get_regexp_op(event.session)
            name_pattern = pattern.replace(r'\$arg', '_%')
        else:
            filter_op = lambda x, y: x.like(y, escape='#')
            pattern = '%%%s%%' % escape_like_re.sub(r'#\1', pattern)
            name_pattern = pattern.replace('$arg', '#_#%')

        query = event.session.query(Factoid)\
                             .join(Factoid.names).add_entity(FactoidName)\
                             .join(Factoid.values)

        if search_type.startswith('fact'):
            query = query.filter(filter_op(FactoidName.name, name_pattern))
        elif search_type.startswith('value'):
            query = query.filter(filter_op(FactoidValue.value, pattern))
        else:
            query = query.filter(or_(filter_op(FactoidName.name, name_pattern),
                                     filter_op(FactoidValue.value, pattern)))

        # Pre-evalute the iterable or the if statement will be True in SQLAlchemy 0.4. LP: #383286
        matches = [match for match in query.all()]

        bounded_matches = matches[start:start+limit]
        if bounded_matches:
            event.addresponse(u'; '.join(
                u'%s [%s]' % (fname.name, len(factoid.values))
                for factoid, fname in bounded_matches))
        elif len(matches):
            event.addresponse(u"I could only find %(number)d things that matched '%(pattern)s'", {
                u'number': len(matches),
                u'pattern': origpattern,
            })
        else:
            event.addresponse(u"I couldn't find anything that matched '%s'" % origpattern)
Example #5
0
    def summon(self, event, who, source):
        if not source:
            source = self.default_source

        if source.lower() not in ibid.sources:
            event.addresponse(u"I'm afraid that I'm not connected to %s", source)
            return

        account = (
            event.session.query(Account)
            .options(eagerload("identities"))
            .join("identities")
            .filter(or_(and_(Identity.identity == who, Identity.source == event.source), Account.username == who))
            .first()
        )

        if account:
            for other_identity in [id for id in account.identities if id.source.lower() == source.lower()]:
                if any(
                    True
                    for channel in ibid.channels[other_identity.source].itervalues()
                    if other_identity.id in channel
                ):
                    event.addresponse(
                        u"Your presence has been requested by " u"%(who)s in %(channel)s on %(source)s.",
                        {
                            "who": event.sender["nick"],
                            "channel": (not event.public) and u"private" or event.channel,
                            "source": event.source,
                        },
                        target=other_identity.identity,
                        source=other_identity.source,
                        address=False,
                    )
                    event.addresponse(True)
                else:
                    event.addresponse(u"Sorry %s doesn't appear to be available right now.", who)
                return

        event.addresponse(
            u"Sorry, I don't know how to find %(who)s on %(source)s. "
            u"%(who)s must first link an identity on %(source)s.",
            {"who": who, "source": source},
        )
        return