8 import util.coroutines as coroutines
9 import util.misc as misc_utils
12 _moduleLogger = logging.getLogger("channel.text")
15 class TextChannel(tp.ChannelTypeText):
17 NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
19 def __init__(self, connection, manager, props, contactHandle):
20 self.__manager = manager
23 tp.ChannelTypeText.__init__(self, connection, manager, props)
24 self.__nextRecievedId = 0
25 self.__lastMessageTimestamp = self.NULL_TIMESTAMP
27 self.__otherHandle = contactHandle
29 self.__callback = coroutines.func_sink(
30 coroutines.expand_positional(
31 self._on_conversations_updated
34 self._conn.session.voicemails.updateSignalHandler.register_sink(
37 self._conn.session.texts.updateSignalHandler.register_sink(
41 # The only reason there should be anything in the conversation is if
42 # its new, so report it all
44 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
46 _moduleLogger.debug("No voicemails in the conversation yet for %r" % (self._contactKey, ))
48 self._report_conversation(mergedConversations)
50 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
52 _moduleLogger.debug("No texts conversation yet for %r" % (self._contactKey, ))
54 self._report_conversation(mergedConversations)
56 @misc_utils.log_exception(_moduleLogger)
57 def Send(self, messageType, text):
58 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
59 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
61 if self.__lastMessageTimestamp == self.NULL_TIMESTAMP:
62 # Hack: GV marks messages as read when they are replied to. If GV
63 # marks them as read than we ignore them. So reduce the window for
64 # them being marked as read. Oh and Conversations already handles
65 # it if the message was already part of a thread, so we can limit
66 # this to if we are trying to start a thread. You might say a
67 # voicemail could be what is being replied to and that doesn't mean
70 self._conn.session.texts.update(force=True)
72 _moduleLogger.exception(
73 "Update failed when proactively checking for texts"
76 _moduleLogger.info("Sending message to %r" % (self.__otherHandle, ))
77 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
78 self._conn.session.textsStateMachine.reset_timers()
80 self.Sent(int(time.time()), messageType, text)
82 @misc_utils.log_exception(_moduleLogger)
87 _moduleLogger.debug("Closing text")
88 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
91 self._conn.session.texts.updateSignalHandler.unregister_sink(
94 self.__callback = None
96 tp.ChannelTypeText.Close(self)
97 self.remove_from_connection()
100 def _contactKey(self):
101 contactKey = self.__otherHandle.phoneNumber
104 @misc_utils.log_exception(_moduleLogger)
105 def _on_conversations_updated(self, conv, conversationIds):
106 if self._contactKey not in conversationIds:
108 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
109 mergedConversations = conv.get_conversation(self._contactKey)
110 self._report_conversation(mergedConversations)
112 def _report_conversation(self, mergedConversations):
113 # Can't filter out messages in a texting conversation that came in
114 # before the last one sent because that creates a race condition of two
115 # people sending at about the same time, which happens quite a bit
116 newConversations = mergedConversations.conversations
117 if not newConversations:
119 "No messages ended up existing for %r" % (self._contactKey, )
122 newConversations = self._filter_out_reported(newConversations)
123 newConversations = self._filter_out_read(newConversations)
124 newConversations = list(newConversations)
125 if not newConversations:
127 "New messages for %r have already been read externally" % (self._contactKey, )
130 self.__lastMessageTimestamp = newConversations[-1].time
134 for newConversation in newConversations
135 for newMessage in newConversation.messages
136 if newMessage.whoFrom != "Me:"
138 if not newConversations:
140 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
144 for newMessage in messages:
145 formattedMessage = self._format_message(newMessage)
146 self._report_new_message(formattedMessage)
147 for conv in newConversations:
150 def _filter_out_reported(self, conversations):
153 for conversation in conversations
154 if self.__lastMessageTimestamp < conversation.time
158 def _filter_out_read(conversations):
161 for conversation in conversations
162 if not conversation.isRead and not conversation.isArchived
165 def _format_message(self, message):
166 return " ".join(part.text.strip() for part in message.body)
168 def _report_new_message(self, message):
169 currentReceivedId = self.__nextRecievedId
170 timestamp = int(time.time())
171 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
173 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
174 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
176 self.__nextRecievedId += 1