Esempio n. 1
0
    def handle(self, event):
        if event.sender:
            if (event.source, event.sender['connection']) in identify_cache:
                (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]
                return

            identity = event.session.query(Identity) \
                    .options(eagerload('account')) \
                    .filter_by(source=event.source,
                               identity=event.sender['id']) \
                    .first()
            if not identity:
                identity = Identity(event.source, event.sender['id'])
                event.session.save_or_update(identity)
                try:
                    event.session.commit()
                    log.info(u'Created identity %s for %s on %s', identity.id, identity.identity, identity.source)
                except IntegrityError:
                    event.session.rollback()
                    event.session.close()
                    del event['session']
                    log.debug(u'Race encountered creating identity for %s on %s', event.sender['id'], event.source)
                    identity = event.session.query(Identity) \
                            .options(eagerload('account')) \
                            .filter_by(source=event.source,
                                       identity=event.sender['id']) \
                            .one()

            event.identity = identity.id
            if identity.account:
                event.account = identity.account.id
            else:
                event.account = None
            identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account)
Esempio n. 2
0
    def handle(self, event):
        if event.sender:
            if (event.source, event.sender['connection']) in identify_cache:
                (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]
                return

            identity = event.session.query(Identity) \
                    .options(eagerload('account')) \
                    .filter_by(source=event.source,
                               identity=event.sender['id']) \
                    .first()
            if not identity:
                identity = Identity(event.source, event.sender['id'])
                event.session.add(identity)
                try:
                    event.session.commit()
                    log.info(u'Created identity %s for %s on %s', identity.id, identity.identity, identity.source)
                except IntegrityError:
                    event.session.rollback()
                    event.session.close()
                    del event['session']
                    log.debug(u'Race encountered creating identity for %s on %s', event.sender['id'], event.source)
                    identity = event.session.query(Identity) \
                            .options(eagerload('account')) \
                            .filter_by(source=event.source,
                                       identity=event.sender['id']) \
                            .one()

            event.identity = identity.id
            if identity.account:
                event.account = identity.account.id
            else:
                event.account = None
            identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account)
Esempio n. 3
0
    def _get_usaco_user(self, event, user):
        account = event.session.query(Account) \
            .options(eagerload('attributes')) \
            .filter(Account.username == user) \
            .first()
        if account is None:
            account = event.session.query(Account) \
                .options(eagerload('attributes')) \
                .join('identities') \
                .filter(Identity.identity == user) \
                .filter(Identity.source == event.source) \
                .first()
            if account is None:
                raise UsacoException(
                    u'Sorry, %s has not been linked to a USACO account yet' %
                    user)

        usaco_account = [
            attr.value for attr in account.attributes
            if attr.name == 'usaco_account'
        ]
        if len(usaco_account) == 0:
            raise UsacoException(
                u'Sorry, %s has not been linked to a USACO account yet' % user)
        return usaco_account[0]
Esempio n. 4
0
    def _get_usaco_user(self, event, user):
        account = event.session.query(Account).options(eagerload("attributes")).filter(Account.username == user).first()
        if account is None:
            account = (
                event.session.query(Account)
                .options(eagerload("attributes"))
                .join("identities")
                .filter(Identity.identity == user)
                .filter(Identity.source == event.source)
                .first()
            )
            if account is None:
                raise UsacoException(u"Sorry, %s has not been linked to a USACO account yet" % user)

        usaco_account = [attr.value for attr in account.attributes if attr.name == "usaco_account"]
        if len(usaco_account) == 0:
            raise UsacoException(u"Sorry, %s has not been linked to a USACO account yet" % user)
        return usaco_account[0]
Esempio n. 5
0
    def _get_usaco_user(self, event, user):
        account = event.session.query(Account) \
            .options(eagerload('attributes')) \
            .filter(Account.username == user) \
            .first()
        if account is None:
            account = event.session.query(Account) \
                .options(eagerload('attributes')) \
                .join('identities') \
                .filter(Identity.identity == user) \
                .filter(Identity.source == event.source) \
                .first()
            if account is None:
                raise UsacoException(u'Sorry, %s has not been linked to a USACO account yet' % user)

        usaco_account = [attr.value for attr in account.attributes if attr.name == 'usaco_account']
        if len(usaco_account) == 0:
            raise UsacoException(u'Sorry, %s has not been linked to a USACO account yet' % user)
        return usaco_account[0]
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
    def handle(self, event):
        if event.sender:
            if (event.source, event.sender["connection"]) in identify_cache:
                (event.identity, event.account) = identify_cache[(event.source, event.sender["connection"])]
                return

            identity = (
                event.session.query(Identity)
                .options(eagerload("account"))
                .filter_by(source=event.source, identity=event.sender["id"])
                .first()
            )
            if not identity:
                identity = Identity(event.source, event.sender["id"])
                event.session.add(identity)
                try:
                    event.session.commit()
                    log.info(u"Created identity %s for %s on %s", identity.id, identity.identity, identity.source)
                except IntegrityError:
                    event.session.rollback()
                    event.session.close()
                    del event["session"]
                    log.debug(u"Race encountered creating identity for %s on %s", event.sender["id"], event.source)
                    identity = (
                        event.session.query(Identity)
                        .options(eagerload("account"))
                        .filter_by(source=event.source, identity=event.sender["id"])
                        .one()
                    )

            event.identity = identity.id
            if identity.account:
                event.account = identity.account.id
            else:
                event.account = None
            identify_cache[(event.source, event.sender["connection"])] = (event.identity, event.account)
Esempio n. 9
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