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)
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, )
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)
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)
def setUp(self): super(PluginTestCase, self).setUp() if self.load_configured is None: self.load_configured = not self.load if self.load_base: if 'core' not in self.load: self.load += ['core'] if 'core.RateLimit' not in self.noload: self.noload += ['core.RateLimit'] self._create_database() ibid.reload_reloader() ibid.reloader.reload_databases() ibid.reloader.reload_dispatcher() ibid.reloader.load_processors(self.load, self.noload, self.load_configured) ibid.auth = TestAuth() self.source = u'test_source_' + unicode(id(self)) ibid.sources[self.source] = TestSource() session = ibid.databases.ibid() self.identity = Identity(self.source, self.username) session.save(self.identity) session.commit() self.identity = session.query(Identity) \ .filter_by(identity=self.username).one() session.close()
def setUp(self): super(PluginTestCase, self).setUp() if sqlalchemy.__version__ > '0.6.0': raise unittest.SkipTest( "PluginTestCase doesn't work with SQLAlchemy 0.6") if self.network and os.getenv('IBID_NETWORKLESS_TEST') is not None: raise unittest.SkipTest('test uses network') ibid.config = FileConfig(locate_resource('ibid.test', 'test.ini')) if self.load_configured is None: self.load_configured = not self.load if self.load_base: if 'core' not in self.load: self.load += ['core'] if 'core.RateLimit' not in self.noload: self.noload += ['core.RateLimit'] self._create_database() ibid.reload_reloader() ibid.reloader.reload_databases() ibid.reloader.reload_dispatcher() ibid.reloader.load_processors(self.load, self.noload, self.load_configured) ibid.auth = TestAuth() self.source = u'test_source_' + unicode(id(self)) ibid.sources[self.source] = TestSource() session = ibid.databases.ibid() self.identity = Identity(self.source, self.username) session.add(self.identity) session.commit() self.identity = session.query(Identity) \ .filter_by(identity=self.username).one() session.close()
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'])
def tell(self, event, how, who, memo): m = re.match(r'^on\s+(\S+?)\s+(.+)$', memo, re.I | re.U) source_specified = bool(m) and m.group(1).lower() in ibid.sources if source_specified: source = m.group(1).lower() memo = m.group(2) else: source = event.source if source.lower() == event.source and \ any(True for name in ibid.config.plugins['core']['names'] if name.lower() == who.lower()): event.addresponse(u"I can't deliver messages to myself") return to = event.session.query(Identity) \ .filter_by(identity=who, source=source).first() if not to and not source_specified: account = event.session.query(Account) \ .filter_by(username=who).first() if account: for identity in account.identities: if identity.source == source: to = identity if not identity: identity = account.identities[0] if not to and not source_specified: event.addresponse( u"I don't know who %(who)s is. " u"Say '%(who)s on %(source)s' and I'll take your word " u'that %(who)s exists', { 'who': who, 'source': source, }) return if not to: if source not in ibid.sources: event.addresponse(u'I am not connected to %s', source) return to = Identity(source, who) event.session.save(to) event.session.commit() log.info(u"Created identity %s for %s on %s", to.id, to.identity, to.source) if permission(u'recvmemo', to.account and to.account.id or None, to.source, event.session) != 'yes': event.addresponse(u'Just tell %s yourself', who) return memo = u' '.join((how, who, memo)) memo = Memo(event.identity, to.id, memo, how.lower() in (u'pm', u'privmsg', u'msg')) event.session.save_or_update(memo) event.session.commit() log.info(u"Stored memo %s for %s (%s) from %s (%s): %s", memo.id, to.id, who, event.identity, event.sender['connection'], memo.memo) event.memo = memo.id nomemos_cache.clear() notified_overlimit_cache.discard(to.id) event.addresponse(u"%(acknowledgement)s, I'll %(action)s " u"%(recipient)s on %(source)s", { 'acknowledgement': choice(('Okay', 'Righto', 'Sure', 'Got it')), 'action': how.lower(), 'recipient': to.identity, 'source': to.source, })
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"], )
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'])