7 import util.coroutines as coroutines
11 _moduleLogger = logging.getLogger("channel.text")
14 class TextChannel(telepathy.server.ChannelTypeText):
16 Look into implementing ChannelInterfaceMessages for rich text formatting
19 def __init__(self, connection, manager, props, contactHandle):
20 self.__manager = manager
24 # HACK Older python-telepathy way
25 telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle)
26 self._requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
27 self._implement_property_get(
28 telepathy.interfaces.CHANNEL_INTERFACE,
29 {"Requested": lambda: self._requested}
32 # HACK Newer python-telepathy way
33 telepathy.server.ChannelTypeText.__init__(self, connection, manager, props)
34 self.__nextRecievedId = 0
35 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
37 self.__otherHandle = contactHandle
39 # HACK Older python-telepathy doesn't provide this
40 self._immutable_properties = {
41 'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
42 'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
43 'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
44 'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
45 'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
46 'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
49 self.__callback = coroutines.func_sink(
50 coroutines.expand_positional(
51 self._on_conversations_updated
54 self._conn.session.voicemails.updateSignalHandler.register_sink(
57 self._conn.session.texts.updateSignalHandler.register_sink(
61 # The only reason there should be anything in the conversation is if
62 # its new, so report it all
64 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
66 _moduleLogger.debug("No voicemails in the conversation yet for %r" % (self._contactKey, ))
68 self._report_conversation(mergedConversations)
70 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
72 _moduleLogger.debug("No texts conversation yet for %r" % (self._contactKey, ))
74 self._report_conversation(mergedConversations)
77 # HACK Older python-telepathy doesn't provide this
79 for prop, iface in self._immutable_properties.items():
80 props[iface + '.' + prop] = \
81 self._prop_getters[iface][prop]()
84 @gtk_toolbox.log_exception(_moduleLogger)
85 def Send(self, messageType, text):
86 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
87 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
89 _moduleLogger.info("Sending message to %r" % (self.__otherHandle, ))
90 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
91 self._conn.session.textsStateMachine.reset_timers()
93 self.Sent(int(time.time()), messageType, text)
95 @gtk_toolbox.log_exception(_moduleLogger)
100 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
103 self._conn.session.texts.updateSignalHandler.unregister_sink(
106 self.__callback = None
108 telepathy.server.ChannelTypeText.Close(self)
109 if self.__manager.channel_exists(self.__props):
110 # HACK Older python-telepathy requires doing this manually
111 self.__manager.remove_channel(self)
112 self.remove_from_connection()
115 def _contactKey(self):
116 contactKey = self.__otherHandle.contactID, self.__otherHandle.phoneNumber
119 @gtk_toolbox.log_exception(_moduleLogger)
120 def _on_conversations_updated(self, conv, conversationIds):
121 if self._contactKey not in conversationIds:
123 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
124 mergedConversations = conv.get_conversation(self._contactKey)
125 self._report_conversation(mergedConversations)
127 def _report_conversation(self, mergedConversations):
128 # Can't filter out messages in a texting conversation that came in
129 # before the last one sent because that creates a race condition of two
130 # people sending at about the same time, which happens quite a bit
131 newConversations = mergedConversations.conversations
132 if not newConversations:
134 "No messages ended up existing for %r" % (self._contactKey, )
137 newConversations = self._filter_out_reported(newConversations)
138 newConversations = self._filter_out_read(newConversations)
139 newConversations = list(newConversations)
140 if not newConversations:
142 "New messages for %r have already been read externally" % (self._contactKey, )
145 self.__lastMessageTimestamp = newConversations[-1].time
149 for newConversation in newConversations
150 for newMessage in newConversation.messages
151 if newMessage.whoFrom != "Me:"
153 if not newConversations:
155 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
159 for newMessage in messages:
160 formattedMessage = self._format_message(newMessage)
161 self._report_new_message(formattedMessage)
163 def _filter_out_reported(self, conversations):
166 for conversation in conversations
167 if self.__lastMessageTimestamp < conversation.time
170 def _filter_out_read(self, conversations):
173 for conversation in conversations
174 if not conversation.isRead and not conversation.isArchived
177 def _format_message(self, message):
178 return " ".join(part.text.strip() for part in message.body)
180 def _report_new_message(self, message):
181 currentReceivedId = self.__nextRecievedId
182 timestamp = int(time.time())
183 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
185 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
186 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
188 self.__nextRecievedId += 1