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("Nothing 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("Nothing in the 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 newConversations = self._filter_out_reported(newConversations)
133 newConversations = self._filter_out_read(newConversations)
134 newConversations = list(newConversations)
135 if not newConversations:
137 "New messages for %r have already been read externally" % (self._contactKey, )
140 self.__lastMessageTimestamp = newConversations[-1].time
144 for newConversation in newConversations
145 for newMessage in newConversation.messages
146 if newMessage.whoFrom != "Me:"
148 if not newConversations:
150 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
154 for newMessage in messages:
155 formattedMessage = self._format_message(newMessage)
156 self._report_new_message(formattedMessage)
158 def _filter_out_reported(self, conversations):
161 for conversation in conversations
162 if self.__lastMessageTimestamp < conversation.time
165 def _filter_out_read(self, conversations):
168 for conversation in conversations
169 if not conversation.isRead and not conversation.isArchived
172 def _format_message(self, message):
173 return " ".join(part.text.strip() for part in message.body)
175 def _report_new_message(self, message):
176 currentReceivedId = self.__nextRecievedId
177 timestamp = int(time.time())
178 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
180 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
181 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
183 self.__nextRecievedId += 1