Ejemplo n.º 1
0
    def remove(self, mc, contact, explode=True, cleanup=True):
        if isinstance(mc, int):
            mcname = self.idstr(mc)
            if mc not in self:
                return
            contacts = self[mc].buddies
        else:
            mcname = mc.name
            contacts = mc.mcinfo.buddies

        log.info('removing %r from metacontact %r', contact, mcname)

        cd = contact_dict(contact)

        for d in list(contacts):
            if cd['name'] == d.name and cd['service'] == d.service:
                contacts.remove(d)

        # Contact inherits certain properties from the MetaContact.
        get, set = self.blist.get_contact_info, self.blist.set_contact_info
        sms, email = get(mcname, 'sms'), get(mcname, 'email')

        contact_obj = self.blist.contact_for_id(contact) if isinstance(
            contact, basestring) else contact

        def tolist(s):
            if isinstance(s, basestring): return [s]

        # "inherit" email addresses and sms numbers from the metacontact.
        if sms:
            new_sms = removedupes((tolist(get(mcname, 'sms')) or []) + sms)
            set(contact_obj, 'sms', new_sms)
        if email:
            new_email = removedupes((tolist(get(mcname, 'email')) or []) +
                                    email)
            set(contact_obj, 'email', new_email)

        info('removed %r from %r', contact, mc)
        if hasattr(self.blist, 'new_sorter'):
            on_thread('sorter').call(self.blist.new_sorter.removeContact,
                                     mcname)
        self.rebuild_buddy_mapping()
        self.blist._info_changed()

        if explode and len(contacts) == 1:
            self.remove(mc, contacts[0], True)
            self.remove_mc_entry(mc)
        elif explode and len(contacts) == 0:
            pass
        else:
            if cleanup:
                self.blist.rebuild()
Ejemplo n.º 2
0
    def remove(self, mc, contact, explode = True, cleanup = True):
        if isinstance(mc, int):
            mcname = self.idstr(mc)
            if mc not in self:
                return
            contacts = self[mc].buddies
        else:
            mcname = mc.name
            contacts = mc.mcinfo.buddies

        log.info('removing %r from metacontact %r', contact, mcname)


        cd = contact_dict(contact)

        for d in list(contacts):
            if cd['name'] == d.name and cd['service'] == d.service:
                contacts.remove(d)

        # Contact inherits certain properties from the MetaContact.
        get, set = self.blist.get_contact_info, self.blist.set_contact_info
        sms, email = get(mcname, 'sms'), get(mcname, 'email')

        contact_obj = self.blist.contact_for_id(contact) if isinstance(contact, basestring) else contact
        def tolist(s):
            if isinstance(s, basestring): return [s]

        # "inherit" email addresses and sms numbers from the metacontact.
        if sms:
            new_sms = removedupes((tolist(get(mcname, 'sms')) or []) + sms)
            set(contact_obj, 'sms', new_sms)
        if email:
            new_email = removedupes((tolist(get(mcname, 'email')) or []) + email)
            set(contact_obj, 'email', new_email)

        info('removed %r from %r', contact, mc)
        if hasattr(self.blist, 'new_sorter'):
            on_thread('sorter').call(self.blist.new_sorter.removeContact, mcname)
        self.rebuild_buddy_mapping()
        self.blist._info_changed()

        if explode and len(contacts) == 1:
            self.remove(mc, contacts[0], True)
            self.remove_mc_entry(mc)
        elif explode and len(contacts) == 0:
            pass
        else:
            if cleanup:
                self.blist.rebuild()
Ejemplo n.º 3
0
    def _got_friend_activities(self, f_acts, from_cache = False):
        if f_acts is None:
            return

        if not from_cache:
            activities = []

            entries = getattr(f_acts, 'entry', [])
            for entry in entries:
                activity = objects.Activity()
                try:
                    activity.populate(entry, objects.InputType.XML)
                except Exception:
                    log.error('Error processing this activity: %r', etree.tostring(entry))
                    raise

                if activity.title == (activity.author_name + ' '):
                    continue

                self.fix_title(activity)
                activities.append(activity)

            log.info("got %r activities", len(activities))
            activities = util.removedupes(activities + self.activities, key = lambda x: x.id)
            self._cache('friend_activities', activities[:common.pref('myspace.newsfeed.maxlength', type = int, default = 200)])
        else:
            activities = f_acts

        if self.activities != activities:
            log.info('Got new activities')
            self.activities = activities[:common.pref('myspace.max_activities', type = int, default = 200)]
            self._feed_invalidated()
Ejemplo n.º 4
0
 def ips_from_rdv(self, rtlvs):
     if hasattr(rtlvs, 'proxy_flag'):
         return [ipstr(rtlvs.proxy_ip)]
     else:
         # Try a direct connection with both the client IP and the verified
         # IP that the OSCAR server thinks the sender has.
         return removedupes([ipstr(rtlvs.client_ip), ipstr(rtlvs.verified_ip)])
Ejemplo n.º 5
0
def get_state_choices(curstatus = None, account = None):
    'Return state choices for all accounts, or just for one.'

    # Status choices come from connected accounts, unless there are none.
    _profile = profile()
    conn  = list(_profile.account_manager.connected_accounts)
    accts = conn
    if accts != [_profile] and _profile in accts:
        accts.remove(_profile)

    # When no accounts are connected, just use sensible defaults.
    if not accts:
        return DEFAULT_STATUS_CHOICES

    # Sort status messages by "category" in the order they appear in the status
    # message lists in protocolmeta.py
    statuses = []
    for acct in (accts if account in (None, False) else [account]):
        if hasattr(acct, 'protocol_info'):
            proto_statuses = acct.protocol_info().get('statuses', [])
            invis = [StatusMessage.Invisible.title]
            if acct.protocol_info().get('has_invisible', False) and invis not in proto_statuses:
                proto_statuses.append(invis)
        else:
            proto_statuses = []

        for cat_i, cat in enumerate(proto_statuses):
            for status_id, status in enumerate(cat):
                for st in cat:
                    statuses += [(cat_i, status_id, st)]
    statuses.sort()

    statuses = removedupes([(s[0], s[2]) for s in statuses])
    status_strings = [(s[1], _(s[1])) for s in statuses]

    # If the status string itself in our accumulated list of statuses, that means
    # it belongs to another protocol. Search for any protocols which have the
    # status, and append an extra status message to the list.

    if curstatus is not None and curstatus not in [c[0] for c in status_strings]:
        # Accumulate Status -> [Acct1, Acct2, ...]
        from common.protocolmeta import protocols
        status_acct_map = defaultdict(list)
        for k, protocol in protocols.iteritems():
            for cat in protocol.get('statuses', []):
                for st in cat:
                    status_acct_map[st].append(protocol.name)

            if protocol.get('has_invisible', False):
                status_acct_map[StatusMessage.Invisible.title].append(protocol.name)

        # add a string like (MSN/ICQ only) to the status
        accounts = sorted(status_acct_map.get(curstatus, []))

        #Translators: Separator when listing multiple accounts, ex: MSN/ICQ only
        account_types = _('/').join(accounts)
        status_strings.append((curstatus, _('{status} ({account_types} Only)').format(status=curstatus, account_types=account_types)))

    return status_strings or DEFAULT_STATUS_CHOICES
Ejemplo n.º 6
0
    def build_metacontact_info(self, id, mc_alias, contacts, grouppath=None):
        assert isinstance(id, int) and isinstance(mc_alias,
                                                  (basestring, NoneType))
        from .identity import Identity, Personality
        keys = [(c.name, c.service) for c in contacts]
        keys2 = []
        for k in keys:
            if k not in keys2:
                keys2.append(k)
        buddies = [Personality(*k) for k in keys2]
        mc = Identity(id, mc_alias, buddies=buddies)

        # Metacontact gets all emails/sms from buddy
        sms, email = [], []
        get, set_ = self.blist.get_contact_info, self.blist.set_contact_info
        for contact in contacts:
            email += get(contact, 'email') or []
            sms += get(contact, 'sms') or []
            set_(contact, 'email', None)
            set_(contact, 'sms', None)

        set_(id, 'email', removedupes(email))
        set_(id, 'sms', removedupes(sms))

        if grouppath is None:
            grouppaths = [
                None if x is None else x.lower()
                for x in [contact.get_group() for contact in contacts]
            ]
            mc.groups = set([(grouppaths[0], )])
        elif isinstance(grouppath, set):
            mc.groups = set(grouppath)
        else:
            mc.groups = set([tuple(grouppath)])

        # Contacts in the fake root group will return None for get_group.
        # Put the metacontact in that fake root group.
        if mc.groups == set([(None, )]):
            mc.groups = set([(get_fakeroot_name(), )])

        return mc
Ejemplo n.º 7
0
def rdv_ips(rendtlvs):
    'Get a list of possible ips from a rendezvous packet.'

    normal_order = ('client_ip',  'verified_ip', 'proxy_ip')
    proxy_order  = ('proxy_ip',   'client_ip',   'verified_ip')

    # If the proxy_flag is present in the rendezvous TLVs we should try
    # the proxy server first.
    order = proxy_order if hasattr(rendtlvs, 'proxy_flag') else normal_order

    ipif = lambda s: ipstr(rendtlvs[s]) if hasattr(rendtlvs, s) else None
    return removedupes(filter(None, [ipif(s) for s in order]))
Ejemplo n.º 8
0
    def __init__(self, id, alias=None, groups = None, buddies = None):
        if groups is None:  groups = set()
        if buddies is None: buddies = []

        self.id      = id
        self.alias   = alias
        self.groups  = groups

        # None is not a valid group name...for now.
        none_tuple = (None,)
        if any(g == none_tuple for g in groups):
            raise ValueError('groups had a None')

        self.buddies = removedupes(buddies)
Ejemplo n.º 9
0
    def build_metacontact_info(self, id, mc_alias, contacts, grouppath = None):
        assert isinstance(id, int) and isinstance(mc_alias, (basestring, NoneType))
        from .identity import Identity, Personality
        keys = [(c.name, c.service) for c in contacts]
        keys2 = []
        for k in keys:
            if k not in keys2:
                keys2.append(k)
        buddies = [Personality(*k) for k in keys2]
        mc = Identity(id, mc_alias, buddies=buddies)

        # Metacontact gets all emails/sms from buddy
        sms, email = [], []
        get, set_ = self.blist.get_contact_info, self.blist.set_contact_info
        for contact in contacts:
            email += get(contact, 'email') or []
            sms   += get(contact, 'sms')   or []
            set_(contact, 'email', None)
            set_(contact, 'sms', None)

        set_(id, 'email', removedupes(email))
        set_(id, 'sms',   removedupes(sms))

        if grouppath is None:
            grouppaths = [None if x is None else x.lower() for x in [contact.get_group() for contact in contacts]]
            mc.groups = set([(grouppaths[0],)])
        elif isinstance(grouppath, set):
            mc.groups = set(grouppath)
        else:
            mc.groups = set([tuple(grouppath)])

        # Contacts in the fake root group will return None for get_group.
        # Put the metacontact in that fake root group.
        if mc.groups == set([(None,)]):
            mc.groups = set([(get_fakeroot_name(),)])

        return mc
Ejemplo n.º 10
0
    def _got_friend_activities(self, f_acts, from_cache=False):
        if f_acts is None:
            return

        if not from_cache:
            activities = []

            entries = getattr(f_acts, 'entry', [])
            for entry in entries:
                activity = objects.Activity()
                try:
                    activity.populate(entry, objects.InputType.XML)
                except Exception:
                    log.error('Error processing this activity: %r',
                              etree.tostring(entry))
                    raise

                if activity.title == (activity.author_name + ' '):
                    continue

                self.fix_title(activity)
                activities.append(activity)

            log.info("got %r activities", len(activities))
            activities = util.removedupes(activities + self.activities,
                                          key=lambda x: x.id)
            self._cache(
                'friend_activities', activities[:common.pref(
                    'myspace.newsfeed.maxlength', type=int, default=200)])
        else:
            activities = f_acts

        if self.activities != activities:
            log.info('Got new activities')
            self.activities = activities[:common.pref(
                'myspace.max_activities', type=int, default=200)]
            self._feed_invalidated()
Ejemplo n.º 11
0
        def empty_status(x):
            return bool(x['status'])

        def status_from_json(x):
            try:
                o = objects.StatusUpdate(self)
                o.populate(x, objects.InputType.JSON)
                return o
            except Exception, e:
                log.error('Error loading status update. exception = %r, data = %r', e, x)
                return None

        if not from_cache:
            updated_status = filter(None, map(status_from_json, filter(empty_status, statuses)))
            _before = updated_status + self.friend_status
            merged_status = util.removedupes(self.friend_status + updated_status, key = lambda x: x.id)
#            for status in merged_status:
#                self.get_comments_for(status.id)
            self._cache('friend_status', merged_status)
        else:
            for status in statuses:
                status.comments = map(objects.MyspaceComment.from_json, getattr(status, 'comments', []))
            merged_status = statuses

        if self.friend_status != merged_status:
            self.friend_status = merged_status[:common.pref('myspace.max_friendstatus', type = int, default = 200)]
            self._feed_invalidated()
            log.info('Got new friend status')

    def _got_indicators(self, indicators):
        indicators.pop('user', None)
Ejemplo n.º 12
0
            try:
                o = objects.StatusUpdate(self)
                o.populate(x, objects.InputType.JSON)
                return o
            except Exception, e:
                log.error(
                    'Error loading status update. exception = %r, data = %r',
                    e, x)
                return None

        if not from_cache:
            updated_status = filter(
                None, map(status_from_json, filter(empty_status, statuses)))
            _before = updated_status + self.friend_status
            merged_status = util.removedupes(self.friend_status +
                                             updated_status,
                                             key=lambda x: x.id)
            #            for status in merged_status:
            #                self.get_comments_for(status.id)
            self._cache('friend_status', merged_status)
        else:
            for status in statuses:
                status.comments = map(objects.MyspaceComment.from_json,
                                      getattr(status, 'comments', []))
            merged_status = statuses

        if self.friend_status != merged_status:
            self.friend_status = merged_status[:common.pref(
                'myspace.max_friendstatus', type=int, default=200)]
            self._feed_invalidated()
            log.info('Got new friend status')