Overall, got sending a text to an arbitrary number working
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6
7 _moduleLogger = logging.getLogger("handle")
8
9
10 class TheOneRingHandle(telepathy.server.Handle):
11         """
12         Instances are memoized
13         """
14
15         def __init__(self, connection, id, handleType, name):
16                 telepathy.server.Handle.__init__(self, id, handleType, name)
17                 self._conn = weakref.proxy(connection)
18
19         def __repr__(self):
20                 return "<%s id=%u name='%s'>" % (
21                         type(self).__name__, self.id, self.name
22                 )
23
24         id = property(telepathy.server.Handle.get_id)
25         type = property(telepathy.server.Handle.get_type)
26         name = property(telepathy.server.Handle.get_name)
27
28
29 class ConnectionHandle(TheOneRingHandle):
30
31         def __init__(self, connection, id):
32                 handleType = telepathy.HANDLE_TYPE_CONTACT
33                 handleName = connection.username
34                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
35
36                 self.profile = connection.username
37
38
39 def strip_number(prettynumber):
40         """
41         function to take a phone number and strip out all non-numeric
42         characters
43
44         >>> strip_number("+012-(345)-678-90")
45         '01234567890'
46         """
47         import re
48         uglynumber = re.sub('\D', '', prettynumber)
49         return uglynumber
50
51
52 class ContactHandle(TheOneRingHandle):
53
54         def __init__(self, connection, id, contactId, phoneNumber):
55                 handleType = telepathy.HANDLE_TYPE_CONTACT
56                 handleName = self.to_handle_name(contactId, phoneNumber)
57                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
58
59                 self._contactId = contactId
60                 self._phoneNumber = phoneNumber
61
62         @staticmethod
63         def from_handle_name(handleName):
64                 parts = handleName.split("#", 1)
65                 if len(parts) == 2:
66                         contactId, contactNumber = parts[0:2]
67                 elif len(parts) == 1:
68                         contactId, contactNumber = "", handleName
69                 else:
70                         raise RuntimeError("Invalid handle: %s" % handleName)
71
72                 contactNumber = strip_number(contactNumber)
73                 return contactId, contactNumber
74
75         @staticmethod
76         def to_handle_name(contactId, contactNumber):
77                 handleName = "#".join((contactId, strip_number(contactNumber)))
78                 return handleName
79
80         @property
81         def contactID(self):
82                 return self._contactId
83
84         @property
85         def phoneNumber(self):
86                 return self._phoneNumber
87
88         @property
89         def contactDetails(self):
90                 return self._conn.addressbook.get_contact_details(self._id)
91
92
93 class ListHandle(TheOneRingHandle):
94
95         def __init__(self, connection, id, listName):
96                 handleType = telepathy.HANDLE_TYPE_LIST
97                 handleName = listName
98                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
99
100
101 _HANDLE_TYPE_MAPPING = {
102         'connection': ConnectionHandle,
103         'contact': ContactHandle,
104         'list': ListHandle,
105 }
106
107
108 def create_handle_factory():
109
110         cache = weakref.WeakValueDictionary()
111
112         def create_handle(connection, type, *args):
113                 Handle = _HANDLE_TYPE_MAPPING[type]
114                 key = Handle, connection.username, args
115                 try:
116                         handle = cache[key]
117                         isNewHandle = False
118                 except KeyError:
119                         # The misnamed get_handle_id requests a new handle id
120                         handle = Handle(connection, connection.get_handle_id(), *args)
121                         cache[key] = handle
122                         isNewHandle = True
123                 connection._handles[handle.get_type(), handle.get_id()] = handle
124                 handleStatus = "Is New!" if isNewHandle else "From Cache"
125                 _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
126                 return handle
127
128         return create_handle
129
130
131 create_handle = create_handle_factory()