8 import util.coroutines as coroutines
12 _moduleLogger = logging.getLogger("channel.text")
15 class TextChannel(tp.ChannelTypeText):
17 @todo Look into implementing ChannelInterfaceMessages for rich text formatting
19 @bug Stopped working on Maemo 4.1
22 def __init__(self, connection, manager, props, contactHandle):
23 self.__manager = manager
26 tp.ChannelTypeText.__init__(self, connection, manager, props)
27 self.__nextRecievedId = 0
28 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
30 self.__otherHandle = contactHandle
32 self.__callback = coroutines.func_sink(
33 coroutines.expand_positional(
34 self._on_conversations_updated
37 self._conn.session.voicemails.updateSignalHandler.register_sink(
40 self._conn.session.texts.updateSignalHandler.register_sink(
44 # The only reason there should be anything in the conversation is if
45 # its new, so report it all
47 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
49 _moduleLogger.debug("No voicemails in the conversation yet for %r" % (self._contactKey, ))
51 self._report_conversation(mergedConversations)
53 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
55 _moduleLogger.debug("No texts conversation yet for %r" % (self._contactKey, ))
57 self._report_conversation(mergedConversations)
59 @gtk_toolbox.log_exception(_moduleLogger)
60 def Send(self, messageType, text):
61 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
62 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
64 _moduleLogger.info("Sending message to %r" % (self.__otherHandle, ))
65 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
66 self._conn.session.textsStateMachine.reset_timers()
68 self.Sent(int(time.time()), messageType, text)
70 @gtk_toolbox.log_exception(_moduleLogger)
75 _moduleLogger.debug("Closing text")
76 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
79 self._conn.session.texts.updateSignalHandler.unregister_sink(
82 self.__callback = None
84 tp.ChannelTypeText.Close(self)
85 self.remove_from_connection()
88 def _contactKey(self):
89 contactKey = self.__otherHandle.phoneNumber
92 @gtk_toolbox.log_exception(_moduleLogger)
93 def _on_conversations_updated(self, conv, conversationIds):
94 if self._contactKey not in conversationIds:
96 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
97 mergedConversations = conv.get_conversation(self._contactKey)
98 self._report_conversation(mergedConversations)
100 def _report_conversation(self, mergedConversations):
101 # Can't filter out messages in a texting conversation that came in
102 # before the last one sent because that creates a race condition of two
103 # people sending at about the same time, which happens quite a bit
104 newConversations = mergedConversations.conversations
105 if not newConversations:
107 "No messages ended up existing for %r" % (self._contactKey, )
110 newConversations = self._filter_out_reported(newConversations)
111 newConversations = self._filter_out_read(newConversations)
112 newConversations = list(newConversations)
113 if not newConversations:
115 "New messages for %r have already been read externally" % (self._contactKey, )
118 self.__lastMessageTimestamp = newConversations[-1].time
122 for newConversation in newConversations
123 for newMessage in newConversation.messages
124 if newMessage.whoFrom != "Me:"
126 if not newConversations:
128 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
132 for newMessage in messages:
133 formattedMessage = self._format_message(newMessage)
134 self._report_new_message(formattedMessage)
135 for conv in newConversations:
138 def _filter_out_reported(self, conversations):
141 for conversation in conversations
142 if self.__lastMessageTimestamp < conversation.time
146 def _filter_out_read(conversations):
149 for conversation in conversations
150 if not conversation.isRead and not conversation.isArchived
153 def _format_message(self, message):
154 return " ".join(part.text.strip() for part in message.body)
156 def _report_new_message(self, message):
157 currentReceivedId = self.__nextRecievedId
158 timestamp = int(time.time())
159 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
161 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
162 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
164 self.__nextRecievedId += 1