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