예제 #1
0
    def __build_contact(self, contact=None, memberships=Membership.NONE):
        external_email = None
        is_messenger_enabled = False
        for email in contact.Emails:
            if email.Type == ContactEmailType.EXTERNAL:
                external_email = email
            if email.IsMessengerEnabled:
                is_messenger_enabled = True

        if (not contact.IsMessengerUser) and (external_email is not None):
            display_name = contact.DisplayName
            if not display_name:
                display_name = external_email.Email

            if not is_messenger_enabled:
                memberships = Membership.NONE

            c = profile.Contact(contact.Id,
                    NetworkID.EXTERNAL,
                    external_email.Email.encode("utf-8"),
                    display_name.encode("utf-8"),
                    contact.CID,
                    memberships,
                    contact.Type)
            contact_infos = contact.contact_infos
            c._server_infos_changed(contact_infos)

            for group in self.groups:
                if group.id in contact.Groups:
                    c._add_group_ownership(group)

            return c

        elif contact.PassportName == "":
            # FIXME : mobile phone and mail contacts here
            return None
        else:
            display_name = contact.DisplayName
            if not display_name:
                display_name = contact.QuickName
            if not display_name:
                display_name = contact.PassportName

            if not contact.IsMessengerUser:
                memberships = Membership.NONE
            c = profile.Contact(contact.Id,
                    NetworkID.MSN,
                    contact.PassportName.encode("utf-8"),
                    display_name.encode("utf-8"),
                    contact.CID,
                    memberships)
            contact_infos = contact.contact_infos
            c._server_infos_changed(contact_infos)

            for group in self.groups:
                if group.id in contact.Groups:
                    c._add_group_ownership(group)

            return c
        return None
예제 #2
0
 def search_or_build_contact(self, account, network_id, display_name=None):
     contact = self.search_contact(account, network_id)
     if contact is None:
         if not display_name:
             display_name = account
         contact = profile.Contact(None, network_id, account, display_name)
     return contact
예제 #3
0
 def __build_or_update_contact(self, account, network_id=NetworkID.MSN,
         memberships=None, infos=None):
     contact = self.search_contact(account, network_id)
     if contact is not None:
         self.__update_contact(contact, memberships, infos)
     else:
         if infos is None:
             display_name = ""
             contact = profile.Contact(None, network_id, account,
                     display_name, memberships)
         else:
             contact = self.__build_contact(infos, memberships)
         self.contacts.add(contact)
         self.emit('contact-added', contact)
     return contact
예제 #4
0
    def __update_memberships(self, members):
        role_to_membership = {
            "Allow"   : Membership.ALLOW,
            "Block"   : Membership.BLOCK,
            "Reverse" : Membership.REVERSE,
            "Pending" : Membership.PENDING
        }

        for member in members:
            if isinstance(member, sharing.PassportMember):
                network = NetworkID.MSN
            elif isinstance(member, sharing.EmailMember):
                network = NetworkID.EXTERNAL
            else:
                continue

            contact = self.search_contact(member.Account, network)
            new_contact = False
            if contact is None:
                new_contact = True
                cid = getattr(member, "CID", None)
                account = member.Account.encode("utf-8")
                display_name = (member.DisplayName or member.Account).encode("utf-8")
                msg = member.Annotations.get('MSN.IM.InviteMessage', u'')
                c = profile.Contact(None, network, account, display_name, cid)
                c._server_attribute_changed('invite_message', msg.encode("utf-8"))
                self.contacts.add(c)
                contact = c

            for role in member.Roles:
                membership = role_to_membership.get(role, None)
                if membership is None:
                    raise NotImplementedError("Unknown Membership:" + membership)
                contact._add_membership(membership)

            if new_contact and self.state == AddressBookState.SYNCHRONIZED:
                self.emit('contact-added', contact)
예제 #5
0
    def __update_memberships(self, members):
        role_to_membership = {
            "Allow"   : Membership.ALLOW,
            "Block"   : Membership.BLOCK,
            "Reverse" : Membership.REVERSE,
            "Pending" : Membership.PENDING
        }

        membership_conflicts = {
            Membership.ALLOW: Membership.BLOCK,
            Membership.BLOCK: Membership.ALLOW,
            Membership.FORWARD: Membership.PENDING,
            Membership.PENDING: Membership.FORWARD
        }

        for member in members:
            if isinstance(member, sharing.PassportMember):
                network = NetworkID.MSN
            elif isinstance(member, sharing.EmailMember):
                network = NetworkID.EXTERNAL
            else:
                continue

            if network == NetworkID.MSN and member.IsPassportNameHidden:
                continue # ignore contacts with hidden passport name

            contact = self.search_contact(member.Account, network)

            new_contact = False
            if contact is None:
                member_deleted = True
                for role, deleted in member.Roles.items():
                    if not deleted:
                        member_deleted = False
                        break
                if member_deleted:
                    continue

                new_contact = True
                cid = getattr(member, "CID", None)
                account = member.Account.encode("utf-8")
                display_name = (member.DisplayName or member.Account).encode("utf-8")
                msg = member.Annotations.get('MSN.IM.InviteMessage', u'')
                contact = profile.Contact(None, network, account, display_name, cid)
                contact.freeze_notify()
                contact._server_attribute_changed('invite_message', msg.encode("utf-8"))
                self.contacts.add(contact)

            if contact is self._client.profile:
                continue # don't update our own memberships

            # TODO: Check whether the contact's membership was changed
            # after member.LastChanged and if so ignore this member.
            # To implement this papyon has to save full membership info
            # for contacts.

            deleted_memberships = Membership.NONE
            for role, deleted in member.Roles.items():
                membership = role_to_membership.get(role, None)
                if membership is None:
                    raise NotImplementedError("Unknown Membership:" + membership)

                if deleted:
                    deleted_memberships |= membership
                else:
                    conflicting_memberships = membership_conflicts.get(membership, Membership.NONE)
                    contact._remove_membership(conflicting_memberships)
                    contact._add_membership(membership)

            if deleted_memberships:
                self.__remove_contact(contact, deleted_memberships)
            if self.state != AddressBookState.INITIAL_SYNC:
                if contact.is_member(Membership.PENDING):
                    self.emit('contact-pending', contact)
                if new_contact:
                    self.emit('contact-added', contact)
예제 #6
0
 def search_or_build_contact(self, account, network_id):
     contact = self.search_contact(account, network_id)
     if contact is None:
         contact = profile.Contact(None, network_id, account, account)
     return contact