~marco-gallotta/ibid/ascii-501638

« back to all changes in this revision

Viewing changes to ibid/plugins/identity.py

  • Committer: Marco Gallotta
  • Date: 2010-01-04 10:04:11 UTC
  • mfrom: (808.1.18 trunk)
  • Revision ID: marco@me-20100104100411-1wxdof04tgydys8o
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from random import choice
3
3
import logging
4
4
 
5
 
from sqlalchemy.orm import eagerload
6
 
from sqlalchemy.sql import func
7
 
from sqlalchemy.exceptions import IntegrityError
8
 
 
9
5
import ibid
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
13
11
 
14
12
help = {}
54
52
 
55
53
        if admin:
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()
58
56
            if identity:
59
57
                identity.account_id = account.id
60
58
                event.session.save_or_update(identity)
158
156
            else:
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()
163
161
 
164
162
                if account:
165
163
                    reverse_attach = True
166
164
                else:
167
165
                    username = event.sender['id']
168
166
 
169
 
                    account = event.session.query(Account).filter_by(username=username).first()
 
167
                    account = event.session.query(Account) \
 
168
                            .filter_by(username=username).first()
170
169
 
171
170
                    if account:
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)
178
177
 
179
 
                    currentidentity = event.session.query(Identity).get(event.identity)
 
178
                    currentidentity = event.session.query(Identity) \
 
179
                            .get(event.identity)
180
180
                    currentidentity.account_id = account.id
181
181
                    event.session.save_or_update(currentidentity)
182
182
 
194
194
            if not auth_responses(event, 'accounts'):
195
195
                return
196
196
            admin = True
197
 
            account = event.session.query(Account).filter_by(username=username).first()
 
197
            account = event.session.query(Account) \
 
198
                    .filter_by(username=username).first()
198
199
            if not account:
199
200
                event.addresponse(u"I don't know who %s is", username)
200
201
                return
203
204
            ident = event.session.query(Identity).get(event.identity)
204
205
        else:
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)
262
262
                return
263
263
 
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()
267
266
            if not identity:
268
267
                identity = Identity(source, user)
269
268
            identity.account_id = account_id
286
285
        else:
287
286
            if not auth_responses(event, 'accounts'):
288
287
                return
289
 
            account = event.session.query(Account).filter_by(username=username).first()
 
288
            account = event.session.query(Account) \
 
289
                    .filter_by(username=username).first()
290
290
            if not account:
291
291
                event.addresponse(u"I don't know who %s is", username)
292
292
                return
293
293
 
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()
298
297
        if not identity:
299
298
            event.addresponse(u"I don't know about that identity")
300
299
        else:
328
327
        else:
329
328
            if not auth_responses(event, 'accounts'):
330
329
                return
331
 
            account = event.session.query(Account).filter_by(username=username).first()
 
330
            account = event.session.query(Account) \
 
331
                    .filter_by(username=username).first()
332
332
            if not account:
333
333
                event.addresponse(u"I don't know who %s is", username)
334
334
                return
369
369
            'identities': human_join(u'%s on %s' % (identity.identity, identity.source) for identity in account.identities),
370
370
        })
371
371
 
 
372
help['summon'] = u"Get the attention of a person via different source"
 
373
class Summon(Processor):
 
374
    u"summon <person> [via <source>]"
 
375
    feature = 'summon'
 
376
    permission = u'summon'
 
377
 
 
378
    default_source = Option('default_source',
 
379
            u'Default source to summon people via', u'jabber')
 
380
 
 
381
    @authorise(fallthrough=False)
 
382
    @match(r'^summon\s+(\S+)(?:\s+(?:via|on|using)\s+(\S+))?$')
 
383
    def summon(self, event, who, source):
 
384
        if not source:
 
385
            source = self.default_source
 
386
 
 
387
        if source.lower() not in ibid.sources:
 
388
            event.addresponse(u"I'm afraid that I'm not connected to %s",
 
389
                              source)
 
390
            return
 
391
 
 
392
        account = event.session.query(Account) \
 
393
            .options(eagerload('identities')) \
 
394
            .join(Identity) \
 
395
            .filter(
 
396
                or_(
 
397
                    and_(
 
398
                        Identity.identity == who,
 
399
                        Identity.source == event.source,
 
400
                    ),
 
401
                    Account.username == who,
 
402
                )) \
 
403
            .first()
 
404
 
 
405
        if account:
 
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.',
 
414
                        {
 
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)
 
422
                else:
 
423
                    event.addresponse(
 
424
                        u"Sorry %s doesn't appear to be available right now.",
 
425
                        who)
 
426
                return
 
427
 
 
428
        event.addresponse(
 
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.', {
 
431
                    'who': who,
 
432
                    'source': source,
 
433
        })
 
434
        return
 
435
 
372
436
class Identify(Processor):
373
437
 
374
438
    priority = -1600
 
439
    addressed = False
 
440
    processed = True
 
441
    event_types = (u'message', u'state', u'action', u'notice')
375
442
 
376
 
    def process(self, event):
 
443
    @handler
 
444
    def handle(self, event):
377
445
        if event.sender:
378
446
            if (event.source, event.sender['connection']) in identify_cache:
379
447
                (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]
381
449
 
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']) \
386
454
                    .first()
387
455
            if not identity:
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']) \
402
470
                            .one()
403
471
 
404
472
            event.identity = identity.id
415
483
    else:
416
484
        return (event.identity,)
417
485
 
 
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
 
490
 
418
491
# vi: set et sta sw=4 ts=4: