Adding support for building .debs
[theonering] / src / handle.py
index bcce9d5..55f36fb 100644 (file)
@@ -36,18 +36,54 @@ class ConnectionHandle(TheOneRingHandle):
                self.profile = connection.username
 
 
+def strip_number(prettynumber):
+       """
+       function to take a phone number and strip out all non-numeric
+       characters
+
+       >>> strip_number("+012-(345)-678-90")
+       '01234567890'
+       """
+       import re
+       uglynumber = re.sub('\D', '', prettynumber)
+       return uglynumber
+
+
 class ContactHandle(TheOneRingHandle):
 
-       def __init__(self, connection, id, contactId):
+       def __init__(self, connection, id, contactId, phoneNumber):
                handleType = telepathy.HANDLE_TYPE_CONTACT
-               handleName = contactId
+               handleName = self.to_handle_name(contactId, phoneNumber)
                TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
 
-               self._id = contactId
+               self._contactId = contactId
+               self._phoneNumber = phoneNumber
+
+       @staticmethod
+       def from_handle_name(handleName):
+               parts = handleName.split("#", 1)
+               if len(parts) == 2:
+                       contactId, contactNumber = parts[0:2]
+               elif len(parts) == 1:
+                       contactId, contactNumber = "", handleName
+               else:
+                       raise RuntimeError("Invalid handle: %s" % handleName)
+
+               contactNumber = strip_number(contactNumber)
+               return contactId, contactNumber
+
+       @staticmethod
+       def to_handle_name(contactId, contactNumber):
+               handleName = "#".join((contactId, strip_number(contactNumber)))
+               return handleName
 
        @property
        def contactID(self):
-               return self._id
+               return self._contactId
+
+       @property
+       def phoneNumber(self):
+               return self._phoneNumber
 
        @property
        def contactDetails(self):
@@ -80,6 +116,7 @@ def create_handle_factory():
                        handle = cache[key]
                        isNewHandle = False
                except KeyError:
+                       # The misnamed get_handle_id requests a new handle id
                        handle = Handle(connection, connection.get_handle_id(), *args)
                        cache[key] = handle
                        isNewHandle = True