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
24 telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle)
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.conversations.updateSignalHandler.register_sink(
39 # The only reason there should be anything in the conversation is if
40 # its new, so report it all
42 mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey)
46 self._report_conversation(mergedConversations)
48 @gtk_toolbox.log_exception(_moduleLogger)
49 def Send(self, messageType, text):
50 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
51 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
53 self._conn.session.backend.send_sms(self._otherHandle.phoneNumber, text)
54 self._conn.session.stateMachine.reset_timers()
56 self.Sent(int(time.time()), messageType, text)
58 @gtk_toolbox.log_exception(_moduleLogger)
63 self._conn.session.conversations.updateSignalHandler.unregister_sink(
68 telepathy.server.ChannelTypeText.Close(self)
69 if self._manager.channel_exists(self._props):
70 # Older python-telepathy requires doing this manually
71 self._manager.remove_channel(self)
72 self.remove_from_connection()
75 def _contactKey(self):
76 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
80 @gtk_toolbox.log_exception(_moduleLogger)
81 def _on_conversations_updated(self, conv, conversationIds):
82 if self._contactKey not in conversationIds:
84 _moduleLogger.info("Incoming messages from %r for existing conversation" % (self._contactKey, ))
85 mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey)
86 self._report_conversation(mergedConversations)
88 def _report_conversation(self, mergedConversations):
89 newConversations = mergedConversations.conversations
90 newConversations = self._filter_out_reported(newConversations)
91 newConversations = self._filter_out_read(newConversations)
92 newConversations = list(newConversations)
93 if not newConversations:
95 "New messages for %r have already been read externally" % (self._contactKey, )
98 self._lastMessageTimestamp = newConversations[-1].time
102 for newConversation in newConversations
103 for newMessage in newConversation.messages
104 if newMessage.whoFrom != "Me:"
106 if not newConversations:
108 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
112 for newMessage in messages:
113 formattedMessage = self._format_message(newMessage)
114 self._report_new_message(formattedMessage)
116 def _filter_out_reported(self, conversations):
119 for conversation in conversations
120 if self._lastMessageTimestamp < conversation.time
123 def _filter_out_read(self, conversations):
126 for conversation in conversations
127 if not conversation.isRead and not conversation.isArchived
130 def _format_message(self, message):
131 return " ".join(part.text.strip() for part in message.body)
133 def _report_new_message(self, message):
134 currentReceivedId = self._nextRecievedId
136 timestamp = int(time.time())
137 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
139 _moduleLogger.info("Received message from User %r" % self._otherHandle)
140 self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message)
142 self._nextRecievedId += 1