7f538dbc4879acbcc77349fd42a4a05e2eff4e3c
[theonering] / src / channel / contact_list.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import util.go_utils as gobject_utils
7 import util.coroutines as coroutines
8 import handle
9
10
11 _moduleLogger = logging.getLogger("channel.contact_list")
12
13
14 class AbstractListChannel(
15                 telepathy.server.ChannelTypeContactList,
16                 telepathy.server.ChannelInterfaceGroup,
17         ):
18         "Abstract Contact List channels"
19
20         def __init__(self, connection, h):
21                 telepathy.server.ChannelTypeContactList.__init__(self, connection, h)
22                 telepathy.server.ChannelInterfaceGroup.__init__(self)
23
24                 self._conn_ref = weakref.ref(connection)
25                 self._session = connection.session
26
27
28 class AllContactsListChannel(AbstractListChannel):
29         """
30         The group of contacts for whom you receive presence
31         """
32
33         def __init__(self, connection, h):
34                 AbstractListChannel.__init__(self, connection, h)
35                 self._session.addressbook.updateSignalHandler.register_sink(
36                         self._on_contacts_refreshed
37                 )
38                 self.GroupFlagsChanged(0, 0)
39
40         @coroutines.func_sink
41         @coroutines.expand_positional
42         @gobject_utils.async
43         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
44                 """
45                 @todo This currently filters out people not yet added to the contact
46                         list.  Something needs to be done about those
47                 @todo This currently does not handle people with multiple phone
48                         numbers, yay that'll be annoying to resolve
49                 """
50                 connection = self._conn_ref()
51                 handlesAdded = [
52                         handle.create_handle(connection, "contact", contactId)
53                         for contactId in added
54                         if contactId
55                 ]
56                 handlesRemoved = [
57                         handle.create_handle(connection, "contact", contactId)
58                         for contactId in removed
59                         if contactId
60                 ]
61                 message = ""
62                 actor = 0
63                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
64                 self.MembersChanged(
65                         message,
66                         handlesAdded, handlesRemoved,
67                         (), (),
68                         actor,
69                         reason,
70                 )
71
72
73 def create_contact_list_channel(connection, h):
74         if h.get_name() == 'subscribe':
75                 # The group of contacts for whom you receive presence
76                 ChannelClass = AllContactsListChannel
77         elif h.get_name() == 'publish':
78                 # The group of contacts who may receive your presence
79                 ChannelClass = AllContactsListChannel
80         elif h.get_name() == 'hide':
81                 # A group of contacts who are on the publish list but are temporarily
82                 # disallowed from receiving your presence
83                 # This doesn't make sense to support
84                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
85         elif h.get_name() == 'allow':
86                 # A group of contacts who may send you messages
87                 # @todo Allow-List would be cool to support
88                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
89         elif h.get_name() == 'deny':
90                 # A group of contacts who may not send you messages
91                 # @todo Deny-List would be cool to support
92                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
93         elif h.get_name() == 'stored':
94                 # On protocols where the user's contacts are stored, this contact list
95                 # contains all stored contacts regardless of subscription status.
96                 ChannelClass = AllContactsListChannel
97         else:
98                 raise TypeError("Unknown list type : " + h.get_name())
99         return ChannelClass(connection, h)
100
101