8 import util.coroutines as coroutines
12 _moduleLogger = logging.getLogger("channel.text")
15 class TextChannel(tp.ChannelTypeText):
17 Look into implementing ChannelInterfaceMessages for rich text formatting
20 def __init__(self, connection, manager, props, contactHandle):
21 self.__manager = manager
24 tp.ChannelTypeText.__init__(self, connection, manager, props)
25 self.__nextRecievedId = 0
26 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
28 self.__otherHandle = contactHandle
30 self.__callback = coroutines.func_sink(
31 coroutines.expand_positional(
32 self._on_conversations_updated
35 self._conn.session.voicemails.updateSignalHandler.register_sink(
38 self._conn.session.texts.updateSignalHandler.register_sink(
42 # The only reason there should be anything in the conversation is if
43 # its new, so report it all
45 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
47 _moduleLogger.debug("No voicemails in the conversation yet for %r" % (self._contactKey, ))
49 self._report_conversation(mergedConversations)
51 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
53 _moduleLogger.debug("No texts conversation yet for %r" % (self._contactKey, ))
55 self._report_conversation(mergedConversations)
57 @gtk_toolbox.log_exception(_moduleLogger)
58 def Send(self, messageType, text):
59 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
60 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
62 _moduleLogger.info("Sending message to %r" % (self.__otherHandle, ))
63 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
64 self._conn.session.textsStateMachine.reset_timers()
66 self.Sent(int(time.time()), messageType, text)
68 @gtk_toolbox.log_exception(_moduleLogger)
73 _moduleLogger.debug("Closing text")
74 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
77 self._conn.session.texts.updateSignalHandler.unregister_sink(
80 self.__callback = None
82 tp.ChannelTypeText.Close(self)
83 self.remove_from_connection()
86 def _contactKey(self):
87 contactKey = self.__otherHandle.phoneNumber
90 @gtk_toolbox.log_exception(_moduleLogger)
91 def _on_conversations_updated(self, conv, conversationIds):
92 if self._contactKey not in conversationIds:
94 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
95 mergedConversations = conv.get_conversation(self._contactKey)
96 self._report_conversation(mergedConversations)
98 def _report_conversation(self, mergedConversations):
99 # Can't filter out messages in a texting conversation that came in
100 # before the last one sent because that creates a race condition of two
101 # people sending at about the same time, which happens quite a bit
102 newConversations = mergedConversations.conversations
103 if not newConversations:
105 "No messages ended up existing for %r" % (self._contactKey, )
108 newConversations = self._filter_out_reported(newConversations)
109 newConversations = self._filter_out_read(newConversations)
110 newConversations = list(newConversations)
111 if not newConversations:
113 "New messages for %r have already been read externally" % (self._contactKey, )
116 self.__lastMessageTimestamp = newConversations[-1].time
120 for newConversation in newConversations
121 for newMessage in newConversation.messages
122 if newMessage.whoFrom != "Me:"
124 if not newConversations:
126 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
130 for newMessage in messages:
131 formattedMessage = self._format_message(newMessage)
132 self._report_new_message(formattedMessage)
134 def _filter_out_reported(self, conversations):
137 for conversation in conversations
138 if self.__lastMessageTimestamp < conversation.time
142 def _filter_out_read(conversations):
145 for conversation in conversations
146 if not conversation.isRead and not conversation.isArchived
149 def _format_message(self, message):
150 return " ".join(part.text.strip() for part in message.body)
152 def _report_new_message(self, message):
153 currentReceivedId = self.__nextRecievedId
154 timestamp = int(time.time())
155 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
157 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
158 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
160 self.__nextRecievedId += 1