Applying a fix for some peoples login issue
[theonering] / src / connection.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import constants
7 import gvoice
8 import handle
9 import channel_manager
10
11
12 _moduleLogger = logging.getLogger("connection")
13
14
15 class TheOneRingConnection(telepathy.server.Connection):
16
17         MANDATORY_PARAMETERS = {
18                 'account' : 's',
19                 'password' : 's',
20                 'forward' : 's',
21         }
22         OPTIONAL_PARAMETERS = {
23         }
24         PARAMETER_DEFAULTS = {
25         }
26
27         def __init__(self, manager, parameters):
28                 try:
29                         self.check_parameters(parameters)
30                         account = unicode(parameters['account'])
31
32                         telepathy.server.Connection.__init__(
33                                 self,
34                                 constants._telepathy_protocol_name_,
35                                 account,
36                                 constants._telepathy_implementation_name_
37                         )
38
39                         self._manager = weakref.proxy(manager)
40                         self._credentials = (
41                                 parameters['account'].encode('utf-8'),
42                                 parameters['password'].encode('utf-8'),
43                         )
44                         self._callbackNumber = parameters['forward'].encode('utf-8')
45                         self._channelManager = channel_manager.ChannelManager(self)
46
47                         cookieFilePath = "%s/cookies.txt" % constants._data_path_
48                         self._session = gvoice.session.Session(cookieFilePath)
49
50                         self.set_self_handle(handle.create_handle(self, 'connection'))
51
52                         _moduleLogger.info("Connection to the account %s created" % account)
53                 except Exception, e:
54                         _moduleLogger.exception("Failed to create Connection")
55                         raise
56
57         @property
58         def manager(self):
59                 return self._manager
60
61         @property
62         def session(self):
63                 return self._session
64
65         @property
66         def username(self):
67                 self._credentials[0]
68
69         def handle(self, handleType, handleId):
70                 self.check_handle(handleType, handleId)
71                 return self._handles[handleType, handleId]
72
73         def Connect(self):
74                 """
75                 For org.freedesktop.telepathy.Connection
76                 """
77                 self.StatusChanged(
78                         telepathy.CONNECTION_STATUS_CONNECTING,
79                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
80                 )
81                 try:
82                         self.session.login(*self._credentials)
83                         self.session.backend.set_callback_number(self._callbackNumber)
84                 except gvoice.backend.NetworkError:
85                         self.StatusChanged(
86                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
87                                 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
88                         )
89                 except Exception:
90                         self.StatusChanged(
91                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
92                                 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
93                         )
94                 else:
95                         self.StatusChanged(
96                                 telepathy.CONNECTION_STATUS_CONNECTED,
97                                 telepathy.CONNECTION_STATUS_REASON_REQUESTED
98                         )
99
100         def Disconnect(self):
101                 """
102                 For org.freedesktop.telepathy.Connection
103                 """
104                 try:
105                         self.session.logout()
106                         _moduleLogger.info("Disconnected")
107                 except Exception:
108                         _moduleLogger.exception("Disconnecting Failed")
109                 self.StatusChanged(
110                         telepathy.CONNECTION_STATUS_DISCONNECTED,
111                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
112                 )
113
114         def RequestChannel(self, type, handleType, handleId, suppressHandler):
115                 """
116                 For org.freedesktop.telepathy.Connection
117
118                 @param type DBus interface name for base channel type
119                 @param handleId represents a contact, list, etc according to handleType
120
121                 @returns DBus object path for the channel created or retrieved
122                 """
123                 self.check_connected()
124
125                 channel = None
126                 channelManager = self._channelManager
127                 handle = self.handle(handleType, handleId)
128
129                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
130                         channel = channelManager.channel_for_list(handle, suppressHandler)
131                 elif type == telepathy.CHANNEL_TYPE_TEXT:
132                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
133                                 raise telepathy.NotImplemented("Only Contacts are allowed")
134                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
135                 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
136                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
137                                 raise telepathy.NotImplemented("Only Contacts are allowed")
138                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
139                 else:
140                         raise telepathy.NotImplemented("unknown channel type %s" % type)
141
142                 return channel._object_path
143
144         def RequestHandles(self, handleType, names, sender):
145                 """
146                 For org.freedesktop.telepathy.Connection
147                 """
148                 self.check_connected()
149                 self.check_handle_type(handleType)
150
151                 handles = []
152                 for name in names:
153                         name = name.encode('utf-8')
154                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
155                                 h = self._create_contact_handle(name)
156                         elif handleType == telepathy.HANDLE_TYPE_LIST:
157                                 # Support only server side (immutable) lists
158                                 h = handle.create_handle(self, 'list', name)
159                         else:
160                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
161                         handles.append(h.id)
162                         self.add_client_handle(handle, sender)
163                 return handles
164
165         def _create_contact_handle(self, name):
166                 requestedContactId = name
167
168                 contacts = self.session.addressbook.get_contacts()
169                 contactsFound = [
170                         contactId for contactId in contacts
171                         if contactId == requestedContactId
172                 ]
173
174                 if 0 < len(contactsFound):
175                         contactId = contactsFound[0]
176                         if len(contactsFound) != 1:
177                                 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
178                 else:
179                         contactId = requestedContactId
180                 h = handle.create_handle(self, 'contact', contactId)