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.conversations.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.conversations.get_conversation(self._contactKey)
49 _moduleLogger.info("Nothing in the conversation yet for %r" % (self._contactKey, ))
51 self._report_conversation(mergedConversations)
53 @gtk_toolbox.log_exception(_moduleLogger)
54 def Send(self, messageType, text):
55 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
56 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
58 self._conn.session.backend.send_sms(self._otherHandle.phoneNumber, text)
59 self._conn.session.conversationsStateMachine.reset_timers()
61 self.Sent(int(time.time()), messageType, text)
63 @gtk_toolbox.log_exception(_moduleLogger)
68 self._conn.session.conversations.updateSignalHandler.unregister_sink(
73 telepathy.server.ChannelTypeText.Close(self)
74 if self._manager.channel_exists(self._props):
75 # HACK Older python-telepathy requires doing this manually
76 self._manager.remove_channel(self)
77 self.remove_from_connection()
80 def _contactKey(self):
81 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
85 @gtk_toolbox.log_exception(_moduleLogger)
86 def _on_conversations_updated(self, conv, conversationIds):
87 if self._contactKey not in conversationIds:
89 _moduleLogger.info("Incoming messages from %r for existing conversation" % (self._contactKey, ))
90 mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey)
91 self._report_conversation(mergedConversations)
93 def _report_conversation(self, mergedConversations):
94 newConversations = mergedConversations.conversations
95 newConversations = self._filter_out_reported(newConversations)
96 newConversations = self._filter_out_read(newConversations)
97 newConversations = list(newConversations)
98 if not newConversations:
100 "New messages for %r have already been read externally" % (self._contactKey, )
103 self._lastMessageTimestamp = newConversations[-1].time
107 for newConversation in newConversations
108 for newMessage in newConversation.messages
109 if newMessage.whoFrom != "Me:"
111 if not newConversations:
113 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
117 for newMessage in messages:
118 formattedMessage = self._format_message(newMessage)
119 self._report_new_message(formattedMessage)
121 def _filter_out_reported(self, conversations):
124 for conversation in conversations
125 if self._lastMessageTimestamp < conversation.time
128 def _filter_out_read(self, conversations):
131 for conversation in conversations
132 if not conversation.isRead and not conversation.isArchived
135 def _format_message(self, message):
136 return " ".join(part.text.strip() for part in message.body)
138 def _report_new_message(self, message):
139 currentReceivedId = self._nextRecievedId
140 timestamp = int(time.time())
141 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
143 _moduleLogger.info("Received message from User %r" % self._otherHandle)
144 self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message)
146 self._nextRecievedId += 1