Disabling 'compress' and adding lots of debug help
[theonering] / src / connection.py
1 import os
2 import weakref
3 import logging
4
5 import telepathy
6
7 import constants
8 import tp
9 import util.misc as misc_utils
10
11 import gvoice
12 import handle
13
14 import aliasing
15 import avatars
16 import capabilities
17 import contacts
18 import presence
19 import requests
20 import simple_presence
21
22 import autogv
23 import channel_manager
24
25
26 _moduleLogger = logging.getLogger(__name__)
27
28
29 class TheOneRingOptions(object):
30
31         useGVContacts = True
32
33         assert gvoice.session.Session._DEFAULTS["contacts"][1] == "hours"
34         contactsPollPeriodInHours = gvoice.session.Session._DEFAULTS["contacts"][0]
35
36         assert gvoice.session.Session._DEFAULTS["voicemail"][1] == "minutes"
37         voicemailPollPeriodInMinutes = gvoice.session.Session._DEFAULTS["voicemail"][0]
38
39         assert gvoice.session.Session._DEFAULTS["texts"][1] == "minutes"
40         textsPollPeriodInMinutes = gvoice.session.Session._DEFAULTS["texts"][0]
41
42         def __init__(self, parameters = None):
43                 if parameters is None:
44                         return
45                 self.useGVContacts = parameters["use-gv-contacts"]
46                 self.contactsPollPeriodInHours = parameters['contacts-poll-period-in-hours']
47                 self.voicemailPollPeriodInMinutes = parameters['voicemail-poll-period-in-minutes']
48                 self.textsPollPeriodInMinutes = parameters['texts-poll-period-in-minutes']
49
50
51 class TheOneRingConnection(
52         tp.Connection,
53         aliasing.AliasingMixin,
54         avatars.AvatarsMixin,
55         capabilities.CapabilitiesMixin,
56         contacts.ContactsMixin,
57         presence.PresenceMixin,
58         requests.RequestsMixin,
59         simple_presence.SimplePresenceMixin,
60 ):
61
62         # overiding base class variable
63         _mandatory_parameters = {
64                 'account': 's',
65                 'password': 's',
66         }
67         # overiding base class variable
68         _optional_parameters = {
69                 'forward': 's',
70                 'use-gv-contacts': 'b',
71                 'contacts-poll-period-in-hours': 'i',
72                 'voicemail-poll-period-in-minutes': 'i',
73                 'texts-poll-period-in-minutes': 'i',
74         }
75         _parameter_defaults = {
76                 'forward': '',
77                 'use-gv-contacts': TheOneRingOptions.useGVContacts,
78                 'contacts-poll-period-in-hours': TheOneRingOptions.contactsPollPeriodInHours,
79                 'voicemail-poll-period-in-minutes': TheOneRingOptions.voicemailPollPeriodInMinutes,
80                 'texts-poll-period-in-minutes': TheOneRingOptions.textsPollPeriodInMinutes,
81         }
82         _secret_parameters = set((
83                 "password",
84         ))
85
86         @misc_utils.log_exception(_moduleLogger)
87         def __init__(self, manager, parameters):
88                 self.check_parameters(parameters)
89                 account = unicode(parameters['account'])
90                 encodedAccount = parameters['account'].encode('utf-8')
91                 encodedPassword = parameters['password'].encode('utf-8')
92                 encodedCallback = misc_utils.normalize_number(parameters['forward'].encode('utf-8'))
93                 if encodedCallback and not misc_utils.is_valid_number(encodedCallback):
94                         raise telepathy.errors.InvalidArgument("Invalid forwarding number")
95
96                 # Connection init must come first
97                 self.__options = TheOneRingOptions(parameters)
98                 self.__session = gvoice.session.Session(
99                         cookiePath = None,
100                         defaults = {
101                                 "contacts": (self.__options.contactsPollPeriodInHours, "hours"),
102                                 "voicemail": (self.__options.voicemailPollPeriodInMinutes, "minutes"),
103                                 "texts": (self.__options.textsPollPeriodInMinutes, "minutes"),
104                         },
105                 )
106                 tp.Connection.__init__(
107                         self,
108                         constants._telepathy_protocol_name_,
109                         account,
110                         constants._telepathy_implementation_name_
111                 )
112                 aliasing.AliasingMixin.__init__(self)
113                 avatars.AvatarsMixin.__init__(self)
114                 capabilities.CapabilitiesMixin.__init__(self)
115                 contacts.ContactsMixin.__init__(self)
116                 presence.PresenceMixin.__init__(self)
117                 requests.RequestsMixin.__init__(self)
118                 simple_presence.SimplePresenceMixin.__init__(self)
119
120                 self.__manager = weakref.proxy(manager)
121                 self.__credentials = (
122                         encodedAccount,
123                         encodedPassword,
124                 )
125                 self.__callbackNumberParameter = encodedCallback
126                 self.__channelManager = channel_manager.ChannelManager(self)
127
128                 self.__cachePath = os.sep.join((constants._data_path_, "cache", self.username))
129                 try:
130                         os.makedirs(self.__cachePath)
131                 except OSError, e:
132                         if e.errno != 17:
133                                 raise
134
135                 self.set_self_handle(handle.create_handle(self, 'connection'))
136                 self._plumbing = [
137                         autogv.NewGVConversations(weakref.ref(self)),
138                         autogv.RefreshVoicemail(weakref.ref(self)),
139                         autogv.AutoDisconnect(weakref.ref(self)),
140                         autogv.DelayEnableContactIntegration(constants._telepathy_implementation_name_),
141                 ]
142
143                 _moduleLogger.info("Connection to the account %s created" % account)
144                 self._timedDisconnect = autogv.TimedDisconnect(weakref.ref(self))
145                 self._timedDisconnect.start()
146
147         @property
148         def manager(self):
149                 return self.__manager
150
151         @property
152         def session(self):
153                 return self.__session
154
155         @property
156         def options(self):
157                 return self.__options
158
159         @property
160         def username(self):
161                 return self.__credentials[0]
162
163         @property
164         def callbackNumberParameter(self):
165                 return self.__callbackNumberParameter
166
167         def get_handle_by_name(self, handleType, handleName):
168                 requestedHandleName = handleName.encode('utf-8')
169                 if handleType == telepathy.HANDLE_TYPE_CONTACT:
170                         h = handle.create_handle(self, 'contact', requestedHandleName)
171                 elif handleType == telepathy.HANDLE_TYPE_LIST:
172                         # Support only server side (immutable) lists
173                         h = handle.create_handle(self, 'list', requestedHandleName)
174                 else:
175                         raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
176                 return h
177
178         @property
179         def _channel_manager(self):
180                 return self.__channelManager
181
182         @misc_utils.log_exception(_moduleLogger)
183         def Connect(self):
184                 """
185                 For org.freedesktop.telepathy.Connection
186                 """
187                 if self._status != telepathy.CONNECTION_STATUS_DISCONNECTED:
188                         _moduleLogger.info("Attempting connect when not disconnected")
189                         return
190                 _moduleLogger.info("Connecting...")
191                 self.StatusChanged(
192                         telepathy.CONNECTION_STATUS_CONNECTING,
193                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
194                 )
195                 self._timedDisconnect.stop()
196                 self.session.login(
197                         self.__credentials[0],
198                         self.__credentials[1],
199                         self._on_login,
200                         self._on_login_error,
201                 )
202
203         @misc_utils.log_exception(_moduleLogger)
204         def _on_login(self, *args):
205                 _moduleLogger.info("Connected, setting up...")
206                 try:
207                         self.__session.load(self.__cachePath)
208
209                         for plumber in self._plumbing:
210                                 plumber.start()
211                         if not self.__callbackNumberParameter:
212                                 callback = gvoice.backend.get_sane_callback(
213                                         self.session.backend
214                                 )
215                                 self.__callbackNumberParameter = misc_utils.normalize_number(callback)
216                         self.session.backend.set_callback_number(self.__callbackNumberParameter)
217
218                         subscribeHandle = self.get_handle_by_name(telepathy.HANDLE_TYPE_LIST, "subscribe")
219                         subscribeProps = self.generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, subscribeHandle, False)
220                         self.__channelManager.channel_for_props(subscribeProps, signal=True)
221                         publishHandle = self.get_handle_by_name(telepathy.HANDLE_TYPE_LIST, "publish")
222                         publishProps = self.generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, publishHandle, False)
223                         self.__channelManager.channel_for_props(publishProps, signal=True)
224                 except Exception:
225                         _moduleLogger.exception("Setup failed")
226                         self.disconnect(telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED)
227                         return
228
229                 _moduleLogger.info("Connected and set up")
230                 self.StatusChanged(
231                         telepathy.CONNECTION_STATUS_CONNECTED,
232                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
233                 )
234
235         @misc_utils.log_exception(_moduleLogger)
236         def _on_login_error(self, error):
237                 _moduleLogger.error(error)
238                 if isinstance(error, gvoice.backend.NetworkError):
239                         self.disconnect(telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR)
240                 else:
241                         self.disconnect(telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED)
242
243         @misc_utils.log_exception(_moduleLogger)
244         def Disconnect(self):
245                 """
246                 For org.freedesktop.telepathy.Connection
247                 """
248                 _moduleLogger.info("Kicking off disconnect")
249                 self.disconnect(telepathy.CONNECTION_STATUS_REASON_REQUESTED)
250
251         @misc_utils.log_exception(_moduleLogger)
252         def RequestChannel(self, type, handleType, handleId, suppressHandler):
253                 """
254                 For org.freedesktop.telepathy.Connection
255
256                 @param type DBus interface name for base channel type
257                 @param handleId represents a contact, list, etc according to handleType
258
259                 @returns DBus object path for the channel created or retrieved
260                 """
261                 self.check_connected()
262                 self.check_handle(handleType, handleId)
263
264                 h = self.get_handle_by_id(handleType, handleId) if handleId != 0 else None
265                 props = self.generate_props(type, h, suppressHandler)
266                 self._validate_handle(props)
267
268                 chan = self.__channelManager.channel_for_props(props, signal=True)
269                 path = chan._object_path
270                 _moduleLogger.info("RequestChannel Object Path (%s): %s" % (type.rsplit(".", 1)[-1], path))
271                 return path
272
273         def generate_props(self, channelType, handleObj, suppressHandler, initiatorHandle=None):
274                 targetHandle = 0 if handleObj is None else handleObj.get_id()
275                 targetHandleType = telepathy.HANDLE_TYPE_NONE if handleObj is None else handleObj.get_type()
276                 props = {
277                         telepathy.CHANNEL_INTERFACE + '.ChannelType': channelType,
278                         telepathy.CHANNEL_INTERFACE + '.TargetHandle': targetHandle,
279                         telepathy.CHANNEL_INTERFACE + '.TargetHandleType': targetHandleType,
280                         telepathy.CHANNEL_INTERFACE + '.Requested': suppressHandler
281                 }
282
283                 if initiatorHandle is not None:
284                         props[telepathy.CHANNEL_INTERFACE + '.InitiatorHandle'] = initiatorHandle.id
285
286                 return props
287
288         def disconnect(self, reason):
289                 _moduleLogger.info("Disconnecting")
290
291                 self._timedDisconnect.stop()
292
293                 # Not having the disconnect first can cause weird behavior with clients
294                 # including not being able to reconnect or even crashing
295                 self.StatusChanged(
296                         telepathy.CONNECTION_STATUS_DISCONNECTED,
297                         reason,
298                 )
299
300                 for plumber in self._plumbing:
301                         plumber.stop()
302
303                 self.__channelManager.close()
304                 self.manager.disconnected(self)
305
306                 self.session.save(self.__cachePath)
307                 self.session.logout()
308                 self.session.close()
309
310                 # In case one of the above items takes too long (which it should never
311                 # do), we leave the starting of the shutdown-on-idle counter to the
312                 # very end
313                 self.manager.disconnect_completed()
314
315                 _moduleLogger.info("Disconnected")