7 import util.go_utils as gobject_utils
8 import util.coroutines as coroutines
12 _moduleLogger = logging.getLogger("channel.text")
15 class TextChannel(telepathy.server.ChannelTypeText):
17 Look into implementing ChannelInterfaceMessages for rich text formatting
20 def __init__(self, connection, manager, props, contactHandle):
21 self._manager = manager
25 # HACK Older python-telepathy way
26 telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle)
28 # HACK Newer python-telepathy way
29 telepathy.server.ChannelTypeText.__init__(self, connection, manager, props)
30 self._nextRecievedId = 0
31 self._lastMessageTimestamp = datetime.datetime(1, 1, 1)
33 self._otherHandle = contactHandle
35 self._callback = coroutines.func_sink(
36 coroutines.expand_positional(
37 self._on_conversations_updated
40 self._conn.session.voicemails.updateSignalHandler.register_sink(
43 self._conn.session.texts.updateSignalHandler.register_sink(
47 # The only reason there should be anything in the conversation is if
48 # its new, so report it all
50 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
52 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
54 self._report_conversation(mergedConversations)
56 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
58 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
60 self._report_conversation(mergedConversations)
62 @gtk_toolbox.log_exception(_moduleLogger)
63 def Send(self, messageType, text):
64 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
65 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
67 self._conn.session.backend.send_sms(self._otherHandle.phoneNumber, text)
68 self._conn.session.textsStateMachine.reset_timers()
70 self.Sent(int(time.time()), messageType, text)
72 @gtk_toolbox.log_exception(_moduleLogger)
77 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
80 self._conn.session.texts.updateSignalHandler.unregister_sink(
85 telepathy.server.ChannelTypeText.Close(self)
86 if self._manager.channel_exists(self._props):
87 # HACK Older python-telepathy requires doing this manually
88 self._manager.remove_channel(self)
89 self.remove_from_connection()
92 def _contactKey(self):
93 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
96 @gtk_toolbox.log_exception(_moduleLogger)
97 def _on_conversations_updated(self, conv, conversationIds):
98 if self._contactKey not in conversationIds:
100 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
101 mergedConversations = conv.get_conversation(self._contactKey)
102 self._report_conversation(mergedConversations)
104 def _report_conversation(self, mergedConversations):
105 newConversations = mergedConversations.conversations
106 newConversations = self._filter_out_reported(newConversations)
107 newConversations = self._filter_out_read(newConversations)
108 newConversations = list(newConversations)
109 if not newConversations:
111 "New messages for %r have already been read externally" % (self._contactKey, )
114 self._lastMessageTimestamp = newConversations[-1].time
118 for newConversation in newConversations
119 for newMessage in newConversation.messages
120 if newMessage.whoFrom != "Me:"
122 if not newConversations:
124 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
128 for newMessage in messages:
129 formattedMessage = self._format_message(newMessage)
130 self._report_new_message(formattedMessage)
132 def _filter_out_reported(self, conversations):
135 for conversation in conversations
136 if self._lastMessageTimestamp < conversation.time
139 def _filter_out_read(self, conversations):
142 for conversation in conversations
143 if not conversation.isRead and not conversation.isArchived
146 def _format_message(self, message):
147 return " ".join(part.text.strip() for part in message.body)
149 def _report_new_message(self, message):
150 currentReceivedId = self._nextRecievedId
151 timestamp = int(time.time())
152 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
154 _moduleLogger.info("Received message from User %r" % self._otherHandle)
155 self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message)
157 self._nextRecievedId += 1