Exemplo n.º 1
0
    def token(self, event, token):
        if token in self.tokens:
            (account_id, user, source) = self.tokens[token]
            if event.source.lower() != source.lower(
            ) or event.sender['id'].lower() != user.lower():
                event.addresponse(
                    u'You need to send me this token from %(name)s on %(source)s',
                    {
                        'name': user,
                        'source': source,
                    })
                return

            identity = event.session.query(Identity) \
                    .filter_by(identity=user, source=source).first()
            if not identity:
                identity = Identity(source, user)
            identity.account_id = account_id
            event.session.save_or_update(identity)
            identify_cache.clear()

            del self.tokens[token]
            event.session.commit()

            event.addresponse(u'Identity added')

            log.info(
                u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s",
                identity.id, identity.identity, identity.source, account_id,
                event.account, event.identity, event.sender['connection'],
                token)
Exemplo n.º 2
0
    def token(self, event, token):
        if token in self.tokens:
            (account_id, user, source) = self.tokens[token]
            if event.source.lower() != source.lower() or event.sender["id"].lower() != user.lower():
                event.addresponse(
                    u"You need to send me this token from %(name)s on %(source)s", {"name": user, "source": source}
                )
                return

            identity = event.session.query(Identity).filter_by(identity=user, source=source).first()
            if not identity:
                identity = Identity(source, user)
            identity.account_id = account_id
            event.session.add(identity)
            identify_cache.clear()

            del self.tokens[token]
            event.session.commit()

            event.addresponse(u"Identity added")

            log.info(
                u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s",
                identity.id,
                identity.identity,
                identity.source,
                account_id,
                event.account,
                event.identity,
                event.sender["connection"],
                token,
            )
Exemplo n.º 3
0
    def token(self, event, token):
        if token in self.tokens:
            (account_id, user, source) = self.tokens[token]
            if event.source.lower() != source.lower() or event.sender['id'].lower() != user.lower():
                event.addresponse(u'You need to send me this token from %(name)s on %(source)s', {
                    'name': user,
                    'source': source,
                })
                return

            identity = event.session.query(Identity) \
                    .filter_by(identity=user, source=source).first()
            if not identity:
                identity = Identity(source, user)
            identity.account_id = account_id
            event.session.save_or_update(identity)
            identify_cache.clear()

            del self.tokens[token]
            event.session.commit()

            event.addresponse(u'Identity added')

            log.info(u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s",
                    identity.id, identity.identity, identity.source, account_id, event.account,
                    event.identity, event.sender['connection'], token)
Exemplo n.º 4
0
    def identity(self, event, username, identity, source):
        admin = False
        identity = identity.replace(' ', '')
        reverse_attach = False

        if username is None:
            if event.account:
                account = event.session.query(Account).get(event.account)
            else:
                account = event.session.query(Account) \
                        .join('identities') \
                        .filter(Identity.identity == identity) \
                        .filter(Identity.source == source).first()

                if account:
                    reverse_attach = True
                else:
                    username = event.sender['id']

                    account = event.session.query(Account) \
                            .filter_by(username=username).first()

                    if account:
                        event.addresponse(
                            u'I tried to create the account %s for you, but it already exists. '
                            u"Please use 'create account <name>'", username)
                        return

                    account = Account(username)
                    event.session.save_or_update(account)

                    currentidentity = event.session.query(Identity) \
                            .get(event.identity)
                    currentidentity.account_id = account.id
                    event.session.save_or_update(currentidentity)

                    identify_cache.clear()

                    event.addresponse(u"I've created the account %s for you",
                                      username)

                    event.session.commit()
                    log.info(u"Created account %s (%s) by %s/%s (%s)",
                             account.id, account.username, event.account,
                             event.identity, event.sender['connection'])
                    log.info(
                        u"Attached identity %s (%s on %s) to account %s (%s)",
                        currentidentity.id, currentidentity.identity,
                        currentidentity.source, account.id, account.username)

        else:
            if not auth_responses(event, 'accounts'):
                return
            admin = True
            account = event.session.query(Account) \
                    .filter_by(username=username).first()
            if not account:
                event.addresponse(u"I don't know who %s is", username)
                return

        if reverse_attach:
            ident = event.session.query(Identity).get(event.identity)
        else:
            ident = event.session.query(Identity) \
                    .filter_by(identity=identity, source=source).first()
        if ident and ident.account:
            event.addresponse(
                u'This identity is already attached to account %s',
                ident.account.username)
            return

        if source not in ibid.sources:
            event.addresponse(u'I am not connected to %s', source)
            return
        else:
            source = ibid.sources[source].name

        if not admin:
            token = ''.join([choice(chars) for i in xrange(16)])
            if reverse_attach:
                self.tokens[token] = (account.id, ident.identity, ident.source)
                response = {
                    'reply':
                    u'Please send me this message from %s on %s: %s' %
                    (ident.identity, ident.source, token),
                    'target':
                    identity,
                    'source':
                    source,
                }
                event.addresponse(True)
            else:
                self.tokens[token] = (account.id, identity, source)
                response = {
                    'reply':
                    u'Please send me this message from %s on %s: %s' %
                    (identity, source, token)
                }
                if event.public:
                    response['target'] = event.sender['id']
                    event.addresponse(True)
            event.addresponse(response)
            log.info(u"Sent token %s to %s/%s (%s)", token, event.account,
                     event.identity, event.sender['connection'])

        else:
            if not ident:
                ident = Identity(source, identity)
            ident.account_id = account.id
            event.session.save_or_update(ident)
            event.session.commit()

            identify_cache.clear()

            event.addresponse(True)
            log.info(
                u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)",
                ident.id, ident.identity, ident.source, account.id,
                account.username, event.account, event.identity,
                event.sender['connection'])
Exemplo n.º 5
0
    def identity(self, event, username, identity, source):
        admin = False
        identity = identity.replace(" ", "")
        reverse_attach = False

        if username is None:
            if event.account:
                account = event.session.query(Account).get(event.account)
            else:
                account = (
                    event.session.query(Account)
                    .join("identities")
                    .filter(Identity.identity == identity)
                    .filter(Identity.source == source)
                    .first()
                )

                if account:
                    reverse_attach = True
                else:
                    username = event.sender["id"]

                    account = event.session.query(Account).filter_by(username=username).first()

                    if account:
                        event.addresponse(
                            u"I tried to create the account %s for you, but it already exists. "
                            u"Please use 'create account <name>'",
                            username,
                        )
                        return

                    account = Account(username)
                    event.session.add(account)

                    currentidentity = event.session.query(Identity).get(event.identity)
                    currentidentity.account_id = account.id
                    event.session.add(currentidentity)

                    identify_cache.clear()

                    event.addresponse(u"I've created the account %s for you", username)

                    event.session.commit()
                    log.info(
                        u"Created account %s (%s) by %s/%s (%s)",
                        account.id,
                        account.username,
                        event.account,
                        event.identity,
                        event.sender["connection"],
                    )
                    log.info(
                        u"Attached identity %s (%s on %s) to account %s (%s)",
                        currentidentity.id,
                        currentidentity.identity,
                        currentidentity.source,
                        account.id,
                        account.username,
                    )

        else:
            if not auth_responses(event, "accounts"):
                return
            admin = True
            account = event.session.query(Account).filter_by(username=username).first()
            if not account:
                event.addresponse(u"I don't know who %s is", username)
                return

        if reverse_attach:
            ident = event.session.query(Identity).get(event.identity)
        else:
            ident = event.session.query(Identity).filter_by(identity=identity, source=source).first()
        if ident and ident.account:
            event.addresponse(u"This identity is already attached to account %s", ident.account.username)
            return

        if source not in ibid.sources:
            event.addresponse(u"I am not connected to %s", source)
            return
        else:
            source = ibid.sources[source].name

        if not admin:
            token = "".join([choice(chars) for i in xrange(16)])
            if reverse_attach:
                self.tokens[token] = (account.id, ident.identity, ident.source)
                response = {
                    "reply": u"Please send me this message from %s on %s: %s" % (ident.identity, ident.source, token),
                    "target": identity,
                    "source": source,
                }
                event.addresponse(True)
            else:
                self.tokens[token] = (account.id, identity, source)
                response = {"reply": u"Please send me this message from %s on %s: %s" % (identity, source, token)}
                if event.public:
                    response["target"] = event.sender["id"]
                    event.addresponse(True)
            event.addresponse(response)
            log.info(u"Sent token %s to %s/%s (%s)", token, event.account, event.identity, event.sender["connection"])

        else:
            if not ident:
                ident = Identity(source, identity)
            ident.account_id = account.id
            event.session.add(ident)
            event.session.commit()

            identify_cache.clear()

            event.addresponse(True)
            log.info(
                u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)",
                ident.id,
                ident.identity,
                ident.source,
                account.id,
                account.username,
                event.account,
                event.identity,
                event.sender["connection"],
            )
Exemplo n.º 6
0
    def identity(self, event, username, identity, source):
        admin = False
        identity = identity.replace(' ', '')
        reverse_attach = False

        if username is None:
            if event.account:
                account = event.session.query(Account).get(event.account)
            else:
                account = event.session.query(Account) \
                        .join('identities') \
                        .filter(Identity.identity == identity) \
                        .filter(Identity.source == source).first()

                if account:
                    reverse_attach = True
                else:
                    username = event.sender['id']

                    account = event.session.query(Account) \
                            .filter_by(username=username).first()

                    if account:
                        event.addresponse(u'I tried to create the account %s for you, but it already exists. '
                            u"Please use 'create account <name>'", username)
                        return

                    account = Account(username)
                    event.session.save_or_update(account)

                    currentidentity = event.session.query(Identity) \
                            .get(event.identity)
                    currentidentity.account_id = account.id
                    event.session.save_or_update(currentidentity)

                    identify_cache.clear()

                    event.addresponse(u"I've created the account %s for you", username)

                    event.session.commit()
                    log.info(u"Created account %s (%s) by %s/%s (%s)",
                            account.id, account.username, event.account, event.identity, event.sender['connection'])
                    log.info(u"Attached identity %s (%s on %s) to account %s (%s)",
                            currentidentity.id, currentidentity.identity, currentidentity.source, account.id, account.username)

        else:
            if not auth_responses(event, 'accounts'):
                return
            admin = True
            account = event.session.query(Account) \
                    .filter_by(username=username).first()
            if not account:
                event.addresponse(u"I don't know who %s is", username)
                return

        if reverse_attach:
            ident = event.session.query(Identity).get(event.identity)
        else:
            ident = event.session.query(Identity) \
                    .filter_by(identity=identity, source=source).first()
        if ident and ident.account:
            event.addresponse(u'This identity is already attached to account %s',
                    ident.account.username)
            return

        if source not in ibid.sources:
            event.addresponse(u'I am not connected to %s', source)
            return
        else:
            source = ibid.sources[source].name

        if not admin:
            token = ''.join([choice(chars) for i in xrange(16)])
            if reverse_attach:
                self.tokens[token] = (account.id, ident.identity, ident.source)
                response = {
                        'reply': u'Please send me this message from %s on %s: %s' % (ident.identity, ident.source, token),
                        'target': identity,
                        'source': source,
                }
                event.addresponse(True)
            else:
                self.tokens[token] = (account.id, identity, source)
                response = {'reply': u'Please send me this message from %s on %s: %s' % (identity, source, token)}
                if event.public:
                    response['target'] = event.sender['id']
                    event.addresponse(True)
            event.addresponse(response)
            log.info(u"Sent token %s to %s/%s (%s)",
                    token, event.account, event.identity, event.sender['connection'])

        else:
            if not ident:
                ident = Identity(source, identity)
            ident.account_id = account.id
            event.session.save_or_update(ident)
            event.session.commit()

            identify_cache.clear()

            event.addresponse(True)
            log.info(u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)",
                    ident.id, ident.identity, ident.source, account.id, account.username,
                    event.account, event.identity, event.sender['connection'])