home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import common
- import hashlib
- import jabber
- from jabber import JID, Presence
- from util import callsback, Storage as S, odict, threaded
- from util.cacheable import urlcacheopen, cproperty
- from common.actions import action
- from jabber.objects.nick import NICK_NS, Nick
- from common import pref
- from gui import skin
- from urllib import quote
- from pyxmpp.jabber.vcard import VCardString
-
- VCardString.decode = lambda s, *a: s.value.decode(*a)
- from jabber import VCard
- from util.observe import ObservableProperty as oproperty
- import logging
- log = logging.getLogger('jabber.buddy')
- GTALK = 'gtalk'
- GTALK_DOMAINS = ('gmail.com', 'googlemail.com')
- JABBER = 'jabber'
-
- def isGTalk(resource):
- if not isinstance(resource, basestring):
- return False
-
- reslower = resource.lower()
- return (any,)((lambda .0: for x in .0:
- reslower.startswith(x))(('talk.', 'gmail.', 'talkgadget', 'ics')))
-
-
- no_widget = lambda self: None if getattr(self, 'iswidget', False) else True
- ADDRESS_KEYS = 'street,extadr,locality,region,pcode,ctry'.split(',')
- WORK_KEYS = 'company department postition role'.split()
- SUBSCRIPTION_TO = ('to', 'both')
- SUBSCRIPTION_FROM = ('from', 'both')
-
- class JabberBuddy(common.buddy):
-
- def __init__(self, jabber_, jid, rosteritem = None):
- uname = JID(jid).bare().as_unicode()
- self.jid = JID(jid).bare()
- self.username = uname
- self.resources = { }
- self.subscription = None if rosteritem else None
- self.rostername = None if rosteritem else None
- self.ask = None if rosteritem else None
- self.groups = None if rosteritem else []
- self.profile = False
- common.buddy.__init__(self, uname, jabber_)
- self._gen_pretty_profile()
- self.pending_adds = set()
-
-
- def __call__(self, item):
- self.subscription = item.subscription
- self.ask = item.ask
- self.rostername = item.name
- self.groups = item.groups
- self.check_subscription()
- return self
-
-
- def sort(self, cmp = cmp, key = (lambda x: x), reverse = False):
- pass
-
-
- def check_subscription(self):
- if self.subscription not in SUBSCRIPTION_TO and self.resources and self is not getattr(self.protocol, 'self_buddy', sentinel):
- self.resources.clear()
- self.notify()
-
-
-
- def service(self):
- if self.jid.domain in GTALK_DOMAINS:
- return GTALK
- else:
- return JABBER
-
- service = property(service)
-
- def serviceicon(self):
- if self.jid.domain in GTALK_DOMAINS or any((lambda .0: for j in .0:
- isGTalk(j.resource))(self.resources)):
- service = GTALK
- else:
- service = JABBER
- return skin.get('serviceicons.' + service)
-
- serviceicon = property(serviceicon)
-
- def id(self):
- return self.jid
-
- id = property(id)
-
- def away(self):
- if self.service == GTALK:
- if self.resources and not (self.idle):
- pass
- return not any((lambda .0: for r in .0:
- r.available)(self.resources.itervalues()))
- elif self.resources:
- pass
- return all((lambda .0: for r in .0:
- r.away)(self.resources.itervalues()))
-
- away = property(away)
-
- def mobile(self):
- return False
-
- mobile = property(mobile)
-
- def blocked(self):
- return False
-
- blocked = property(blocked)
-
- def online(self):
- return bool(self.resources)
-
- online = property(online)
-
- def idle(self):
- if self.service == GTALK:
- if self.resources:
- pass
- return all((lambda .0: for r in .0:
- r.idle)(self.resources.itervalues()))
- else:
- return False
-
- idle = property(idle)
-
- def get_caps(self):
- caps = common.buddy.get_caps(self)
- for r in self.resources.values():
- res = r.jid.resource
- if res and not isGTalk(res):
- break
- continue
-
- return caps
-
- caps = property(get_caps)
-
- def _gen_pretty_profile(self):
- profile = odict()
- ok = False
- fullname = self.vcard_get('fn')
- if fullname:
- profile['Full Name:'] = fullname
- ok = True
-
- mapping = {
- 'birthday': ('bday',),
- 'email_address': ('email', 'address'),
- 'phone': ('tel', 'number'),
- 'company': ('org', 'name'),
- 'department': ('org', 'unit'),
- 'postition': ('title',),
- 'role': ('role',) }
- for key in ('birthday', 'email_address', 'phone'):
- val = self.vcard_get(*mapping[key])
- if val is not None:
- profile[key.replace('_', ' ').title() + ':'] = val
- ok = True
- continue
-
- homepage = self.vcard_get('url')
- if homepage:
- profile['Website'] = (homepage, homepage)
- ok = True
-
- about = self.vcard_get('desc')
- if about:
- if ok:
- profile['sep1'] = 4
-
- profile['Additional Information:'] = ''.join([
- '\n',
- about])
-
-
- def prettyaddy(addict):
- addy = []
-
- add = lambda s: addy.insert(0, s)
- mstr = '%(street)s %(extadr)s %(locality)s %(region)s %(pcode)s %(ctry)s' % addict
- if isinstance(mstr, unicode):
- mstr = mstr.encode('utf-8')
-
- murl = 'http://maps.google.com/maps?q=' + quote(mstr)
- if addict.ctry:
- add('\n' + addict.ctry)
-
- if addict.pcode:
- add(addict.pcode)
-
- if addict.region:
- if addict.pcode:
- add(' ')
-
- add(addict.region)
-
- if addict.locality:
- if addict.region or addict.pcode:
- add(', ')
-
- add(addict.locality)
-
- if addict.extadr:
- if any((addict.locality, addict.region, addict.pcode)):
- add('\n')
-
- add(addict.extadr)
-
- if addict.street:
- if any((addict.locality, addict.region, addict.pcode, addict.extadr)):
- add('\n')
-
- add(addict.street)
-
- if addy:
- add([
- '(',
- (murl, 'map'),
- ')\n'])
-
- return addy
-
- address = self.vcard_get('adr')
- if ok:
- profile['sep3'] = 4
-
- for key in WORK_KEYS:
- val = self.vcard_get(*mapping[key])
- profile[key.title() + ':'] = val
-
- self.last_pretty_profile = profile
- return profile
-
-
- def pretty_profile(self):
- return getattr(self, ('last_pretty_profile',), (lambda : self._gen_pretty_profile()))
-
- pretty_profile = property(pretty_profile)
-
- def get_status_message(self):
-
- try:
- res = self.get_highest_priority_resource()
- if res:
- if not res.status_message:
- pass
- return ''
- else:
- return ''
- except Exception:
- e = None
- log.warning('status_message(self) on %r: %r', self, e)
- raise
-
-
-
- def set_status_message(self, value):
- r = self.get_highest_priority_resource()
- if r:
- old = r.status_message
- r.status_message = value
- self.notify('status_message', old, value)
-
-
- status_message = property(get_status_message, set_status_message)
-
- def stripped_msg(self):
- return self.status_message
-
- stripped_msg = property(stripped_msg)
-
- def status(self):
- if self.subscription not in SUBSCRIPTION_TO and self is not getattr(self.protocol, 'self_buddy', sentinel):
- return 'unknown'
- elif not getattr(self, 'resources', []):
- return 'offline'
- elif not self.away:
- return 'available'
- else:
- return 'away'
-
- status = property(status)
-
- def update_presence(self, presence, notify = True, buddy = None):
- if not presence.get_from():
- pass
- buddy = buddy
- presence_type = presence.get_type()
- if not presence_type or presence_type == 'available':
- old_length = len(self.resources)
- self.resources[buddy] = jabber.resource(self.protocol, buddy, presence)
- children = presence.xmlnode.get_children()
- nicks = jabber.jabber_util.xpath_eval(presence.xmlnode, 'n:nick', {
- 'n': NICK_NS })
- if nicks:
- self.nick = Nick(nicks[0]).nick
-
- if self.jid.domain not in pref('digsby.guest.domains', []):
- self.protocol.get_buddy_icon(self.name)
-
- elif presence_type == 'unavailable':
- if buddy in self.resources:
- del self.resources[buddy]
-
-
- if notify:
- self.notify('status')
-
-
-
- def get_highest_priority_resource(self):
- return None if getattr(self, 'resources', []) else None
-
-
- def set_vcard(self, stanza):
- log.info('incoming vcard for %s', self)
- self._vcard_incoming = False
- q = stanza.get_query()
- if not q:
- return None
-
-
- try:
- vc = self.vcard = jabber.VCard(q)
- except ValueError:
- pass
-
- self._incoming_icon(vc.photo)
- self._gen_pretty_profile()
-
-
- def set_vc(self, vcard):
- self._vcard = vcard
-
-
- def get_vc(self):
- return self._vcard
-
- vcard = property(get_vc, set_vc)
- _vcard = cproperty(None, (lambda o: None if o is not None else None), (lambda o: None if o is not None else None))
-
- def vcard_get(self, *stuffs):
- if self.vcard:
- stuff = stuffs[0]
- stuffs = stuffs[1:]
- thing = getattr(self.vcard, stuff, None)
- if isinstance(thing, list) and thing:
- thing = thing[0]
- while stuffs:
- thing = getattr(thing, stuffs[0])
- stuffs = stuffs[1:]
- elif thing:
- if stuffs:
- log.warning_s('%r', stuffs)
- log.warning_s(self.vcard.as_xml())
-
-
- return None if thing else thing
-
-
- nick = cproperty(None)
-
- def _incoming_icon(self, photos):
- if photos and photos[0].image:
- img = photos[0].image
- self._update_photo_image(img)
- elif photos and photos[0].uri:
-
- def success(result):
- (_response, content) = result
- self._update_photo_image(content)
-
- meth = threaded(urlcacheopen)
- meth.verbose = False
- meth(photos[0].uri, success = success)
-
-
-
- def _update_photo_image(self, data):
- hash = hashlib.sha1(data).hexdigest()
- self.cache_icon(data, hash)
- self.icon_hash = hash
-
-
- def get_remote_alias(self):
- for attr in ('rostername', 'nick'):
- nick = getattr(self, attr, None)
- if nick:
- return unicode(nick)
- continue
-
- for attr in ('nickname', 'fn'):
- nick = self.vcard_get(attr)
- if nick:
- return unicode(nick)
- continue
-
-
- remote_alias = oproperty(get_remote_alias, observe = [
- 'vcard'])
-
- def add_to_group(self, groupname, callback = None):
- log.info('%s add_to_group %s', self, groupname)
- pending = self.pending_adds
- if groupname in pending:
- log.info('ignoring request.')
- else:
- pending.add(groupname)
- item = self.protocol.roster.get_item_by_jid(self.id).clone()
- if groupname not in item.groups:
- item.groups.append(groupname)
- query = item.make_roster_push()
-
- def onsuccess(_s):
- pending.discard(groupname)
- callback.success()
-
-
- def onerror(_s = (None, (None, None, None), None)):
- pending.discard(groupname)
- log.warning('error adding %r to %s', self.id, groupname)
-
- self.protocol.send_cb(query, success = onsuccess, error = onerror, timeout = onerror)
-
-
- add_to_group = action(needs = ((unicode, 'Group name'),))(callsback(add_to_group))
-
- def remove(self, callback = None):
-
- try:
- item = self.protocol.roster.get_item_by_jid(self.id).clone()
- except KeyError:
- return None
-
- item.subscription = 'remove'
- self.protocol.send_cb(item.make_roster_push(), callback = callback)
-
- remove = action()(callsback(remove))
-
- def subscribed(self):
- self.protocol.send_presence(Presence(to_jid = self.id, stanza_type = 'subscribed'))
-
- subscribed = action((lambda self: if no_widget(self) is None:
- passelif self.subscription in SUBSCRIPTION_FROM:
- passTrue))(subscribed)
-
- def unsubscribed(self):
- self.protocol.send_presence(Presence(to_jid = self.id, stanza_type = 'unsubscribed'))
-
- unsubscribed = action((lambda self: if no_widget(self) is None:
- passelif self.subscription not in SUBSCRIPTION_FROM:
- passTrue))(unsubscribed)
-
- def subscribe(self):
- self.protocol.send_presence(Presence(to_jid = self.id, stanza_type = 'subscribe'))
-
- subscribe = action((lambda self: if no_widget(self) is None:
- passelif self.subscription in SUBSCRIPTION_TO:
- passTrue))(subscribe)
-
- def unsubscribe(self):
- self.protocol.send_presence(Presence(to_jid = self.id, stanza_type = 'unsubscribe'))
-
- unsubscribe = action((lambda self: if no_widget(self) is None:
- passelif self.subscription not in SUBSCRIPTION_TO:
- passTrue))(unsubscribe)
-
- def appear_offline_to(self):
- self.protocol.send_presence(Presence(stanza_type = 'unavailable', status = 'Logged Out', to_jid = self.id))
-
-
- def expand(self):
- print 'expand %r' % self
-
- expand = action()(expand)
-
- def sorted_resources(self):
- return sorted(self.resources.itervalues(), key = (lambda r: (r.priority, r.jid)), reverse = True)
-
-
- def __iter__(self):
- return iter(self.sorted_resources())
-
-
- def __len__(self):
- return len(self.resources)
-
-
- def __getitem__(self, index):
- return self.sorted_resources()[index]
-
-
- def __repr__(self):
-
- try:
- res = len(self.resources)
- except:
- res = 0
-
-
- try:
- n = self.name
- except:
- n = ''
-
- return '<JabberBuddy %s %d>' % (n, res)
-
-
-
- def dupe_presence(p):
- return Presence(p.get_node())
-
-