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 conversation = self._conn.session.conversations.get_conversation(self._contactKey)
43 self._report_conversation(conversation)
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)
52 self.Sent(int(time.time()), messageType, text)
54 @gtk_toolbox.log_exception(_moduleLogger)
57 # Clear since the user has seen it all and it should start a new conversation
58 self._conn.session.conversations.clear_conversation(self._contactKey)
60 self._conn.session.conversations.updateSignalHandler.unregister_sink(
64 telepathy.server.ChannelTypeText.Close(self)
65 self.remove_from_connection()
68 def _contactKey(self):
69 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
73 @gtk_toolbox.log_exception(_moduleLogger)
74 def _on_conversations_updated(self, conv, conversationIds):
75 if self._contactKey not in conversationIds:
77 _moduleLogger.info("Incoming messages from %r for existing conversation" % (self._contactKey, ))
78 conversation = self._conn.session.conversations.get_conversation(self._contactKey)
79 self._report_conversation(conversation)
81 def _report_conversation(self, conversation):
82 # @bug? Check if messages sent need to be filtered out
83 completeMessageHistory = conversation["messageParts"]
84 messages = self._filter_seen_messages(completeMessageHistory)
85 self._lastMessageTimestamp = messages[-1][0]
86 formattedMessage = self._format_messages(messages)
87 self._report_new_message(formattedMessage)
89 def _filter_seen_messages(self, messages):
92 for message in messages
93 if self._lastMessageTimestamp < message[0]
96 def _format_messages(self, messages):
97 return "\n".join(message[1] for message in messages)
99 def _report_new_message(self, message):
100 currentReceivedId = self._nextRecievedId
102 timestamp = int(time.time())
103 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
105 _moduleLogger.info("Received message from User %r" % self._otherHandle)
106 self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message)
108 self._nextRecievedId += 1