2
2
from random import choice
5
from sqlalchemy.orm import eagerload
6
from sqlalchemy.sql import func
7
from sqlalchemy.exceptions import IntegrityError
10
from ibid.plugins import Processor, match, auth_responses
11
from ibid.models import Account, Identity, Attribute
6
from ibid.config import Option
7
from ibid.db import eagerload, IntegrityError, and_, or_
8
from ibid.db.models import Account, Identity, Attribute
9
from ibid.plugins import Processor, match, handler, auth_responses, authorise
12
10
from ibid.utils import human_join
56
54
identity = event.session.query(Identity) \
57
.filter_by(identity=username, source=event.source.lower()).first()
55
.filter_by(identity=username, source=event.source).first()
59
57
identity.account_id = account.id
60
58
event.session.save_or_update(identity)
159
157
account = event.session.query(Account) \
160
158
.join('identities') \
161
.filter(func.lower(Identity.identity) == identity.lower()) \
162
.filter(func.lower(Identity.source) == source.lower()).first()
159
.filter(Identity.identity == identity) \
160
.filter(Identity.source == source).first()
165
163
reverse_attach = True
167
165
username = event.sender['id']
169
account = event.session.query(Account).filter_by(username=username).first()
167
account = event.session.query(Account) \
168
.filter_by(username=username).first()
172
171
event.addresponse(u'I tried to create the account %s for you, but it already exists. '
176
175
account = Account(username)
177
176
event.session.save_or_update(account)
179
currentidentity = event.session.query(Identity).get(event.identity)
178
currentidentity = event.session.query(Identity) \
180
180
currentidentity.account_id = account.id
181
181
event.session.save_or_update(currentidentity)
194
194
if not auth_responses(event, 'accounts'):
197
account = event.session.query(Account).filter_by(username=username).first()
197
account = event.session.query(Account) \
198
.filter_by(username=username).first()
199
200
event.addresponse(u"I don't know who %s is", username)
203
204
ident = event.session.query(Identity).get(event.identity)
205
206
ident = event.session.query(Identity) \
206
.filter(func.lower(Identity.identity) == identity.lower()) \
207
.filter(func.lower(Identity.source) == source.lower()).first()
207
.filter_by(identity=identity, source=source).first()
208
208
if ident and ident.account:
209
209
event.addresponse(u'This identity is already attached to account %s',
210
210
ident.account.username)
264
264
identity = event.session.query(Identity) \
265
.filter(func.lower(Identity.identity) == user.lower()) \
266
.filter(func.lower(Identity.source) == source.lower()).first()
265
.filter_by(identity=user, source=source).first()
268
267
identity = Identity(source, user)
269
268
identity.account_id = account_id
287
286
if not auth_responses(event, 'accounts'):
289
account = event.session.query(Account).filter_by(username=username).first()
288
account = event.session.query(Account) \
289
.filter_by(username=username).first()
291
291
event.addresponse(u"I don't know who %s is", username)
294
294
identity = event.session.query(Identity) \
295
.filter_by(account_id=account.id) \
296
.filter(func.lower(Identity.identity) == user.lower()) \
297
.filter(func.lower(Identity.source) == source.lower()).first()
295
.filter_by(account_id=account.id, identity=user,
296
source=source).first()
299
298
event.addresponse(u"I don't know about that identity")
329
328
if not auth_responses(event, 'accounts'):
331
account = event.session.query(Account).filter_by(username=username).first()
330
account = event.session.query(Account) \
331
.filter_by(username=username).first()
333
333
event.addresponse(u"I don't know who %s is", username)
369
369
'identities': human_join(u'%s on %s' % (identity.identity, identity.source) for identity in account.identities),
372
help['summon'] = u"Get the attention of a person via different source"
373
class Summon(Processor):
374
u"summon <person> [via <source>]"
376
permission = u'summon'
378
default_source = Option('default_source',
379
u'Default source to summon people via', u'jabber')
381
@authorise(fallthrough=False)
382
@match(r'^summon\s+(\S+)(?:\s+(?:via|on|using)\s+(\S+))?$')
383
def summon(self, event, who, source):
385
source = self.default_source
387
if source.lower() not in ibid.sources:
388
event.addresponse(u"I'm afraid that I'm not connected to %s",
392
account = event.session.query(Account) \
393
.options(eagerload('identities')) \
398
Identity.identity == who,
399
Identity.source == event.source,
401
Account.username == who,
406
for other_identity in [id for id
407
in account.identities
408
if id.source.lower() == source.lower()]:
409
if any(True for channel
410
in ibid.channels[other_identity.source].itervalues()
411
if other_identity.id in channel):
412
event.addresponse(u'Your presence has been requested by '
413
u'%(who)s in %(channel)s on %(source)s.',
415
'who': event.sender['nick'],
416
'channel': (not event.public)
417
and u'private' or event.channel,
418
'source': event.source,
419
}, target=other_identity.identity,
420
source=other_identity.source, address=False)
421
event.addresponse(True)
424
u"Sorry %s doesn't appear to be available right now.",
429
u"Sorry, I don't know how to find %(who)s on %(source)s. "
430
u'%(who)s must first link an identity on %(source)s.', {
372
436
class Identify(Processor):
441
event_types = (u'message', u'state', u'action', u'notice')
376
def process(self, event):
444
def handle(self, event):
378
446
if (event.source, event.sender['connection']) in identify_cache:
379
447
(event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]
382
450
identity = event.session.query(Identity) \
383
451
.options(eagerload('account')) \
384
.filter(func.lower(Identity.source) == event.source.lower()) \
385
.filter(func.lower(Identity.identity) == event.sender['id'].lower()) \
452
.filter_by(source=event.source,
453
identity=event.sender['id']) \
388
456
identity = Identity(event.source, event.sender['id'])
397
465
log.debug(u'Race encountered creating identity for %s on %s', event.sender['id'], event.source)
398
466
identity = event.session.query(Identity) \
399
467
.options(eagerload('account')) \
400
.filter(func.lower(Identity.source) == event.source.lower()) \
401
.filter(func.lower(Identity.identity) == event.sender['id'].lower()) \
468
.filter_by(source=event.source,
469
identity=event.sender['id']) \
404
472
event.identity = identity.id
416
484
return (event.identity,)
486
def identify(session, source, id):
487
identity = session.query(Identity) \
488
.filter_by(source=source, identity=id).first()
489
return identity and identity.id
418
491
# vi: set et sta sw=4 ts=4: