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, h):
21 telepathy.server.ChannelTypeText.__init__(self, connection, h)
22 self._nextRecievedId = 0
23 self._lastMessageTimestamp = datetime.datetime(1, 1, 1)
27 self._callback = coroutines.func_sink(
28 coroutines.expand_positional(
29 self._on_conversations_updated
32 self._conn.session.conversations.updateSignalHandler.register_sink(
36 # The only reason there should be anything in the conversation is if
37 # its new, so report it all
39 mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey)
43 self._report_conversation(mergedConversations)
45 @gtk_toolbox.log_exception(_moduleLogger)
46 def Send(self, messageType, text):
47 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
48 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
50 self._conn.session.backend.send_sms(self._otherHandle.phoneNumber, text)
51 self._conn.session.stateMachine.reset_timers()
53 self.Sent(int(time.time()), messageType, text)
55 @gtk_toolbox.log_exception(_moduleLogger)
60 _moduleLogger.info("Closing text channel for %r" % (self._otherHandle, ))
61 self._conn.session.conversations.updateSignalHandler.unregister_sink(
66 telepathy.server.ChannelTypeText.Close(self)
67 self.remove_from_connection()
68 self._prop_getters = None # HACK to get around python-telepathy memory leaks
71 def _contactKey(self):
72 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
76 @gtk_toolbox.log_exception(_moduleLogger)
77 def _on_conversations_updated(self, conv, conversationIds):
78 if self._contactKey not in conversationIds:
80 _moduleLogger.info("Incoming messages from %r for existing conversation" % (self._contactKey, ))
81 mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey)
82 self._report_conversation(mergedConversations)
84 def _report_conversation(self, mergedConversations):
85 newConversations = mergedConversations.conversations
86 newConversations = self._filter_out_reported(newConversations)
87 newConversations = self._filter_out_read(newConversations)
88 newConversations = list(newConversations)
89 if not newConversations:
91 "New messages for %r have already been read externally" % (self._contactKey, )
94 self._lastMessageTimestamp = newConversations[-1].time
98 for newConversation in newConversations
99 for newMessage in newConversation.messages
100 if newMessage.whoFrom != "Me:"
102 if not newConversations:
104 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
108 for newMessage in messages:
109 formattedMessage = self._format_message(newMessage)
110 self._report_new_message(formattedMessage)
112 def _filter_out_reported(self, conversations):
115 for conversation in conversations
116 if self._lastMessageTimestamp < conversation.time
119 def _filter_out_read(self, conversations):
122 for conversation in conversations
123 if not conversation.isRead and not conversation.isArchived
126 def _format_message(self, message):
127 return " ".join(part.text.strip() for part in message.body)
129 def _report_new_message(self, message):
130 currentReceivedId = self._nextRecievedId
132 timestamp = int(time.time())
133 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
135 _moduleLogger.info("Received message from User %r" % self._otherHandle)
136 self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message)
138 self._nextRecievedId += 1