7 import util.coroutines as coroutines
11 _moduleLogger = logging.getLogger("channel.text")
14 class TextChannel(telepathy.server.ChannelTypeText):
16 Look into implementing ChannelInterfaceMessages for rich text formatting
19 def __init__(self, connection, manager, props, contactHandle):
20 self.__manager = manager
24 # HACK Older python-telepathy way
25 telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle)
26 self._requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
27 self._implement_property_get(
28 telepathy.interfaces.CHANNEL_INTERFACE,
29 {"Requested": lambda: self._requested}
32 # HACK Newer python-telepathy way
33 telepathy.server.ChannelTypeText.__init__(self, connection, manager, props)
34 self.__nextRecievedId = 0
35 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
37 self.__otherHandle = contactHandle
39 # HACK Older python-telepathy doesn't provide this
40 self._immutable_properties = {
41 'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
42 'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
43 'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
44 'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
45 'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
46 'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
49 self.__callback = coroutines.func_sink(
50 coroutines.expand_positional(
51 self._on_conversations_updated
54 self._conn.session.voicemails.updateSignalHandler.register_sink(
57 self._conn.session.texts.updateSignalHandler.register_sink(
61 # The only reason there should be anything in the conversation is if
62 # its new, so report it all
64 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
66 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
68 self._report_conversation(mergedConversations)
70 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
72 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
74 self._report_conversation(mergedConversations)
77 # HACK Older python-telepathy doesn't provide this
79 for prop, iface in self._immutable_properties.items():
80 props[iface + '.' + prop] = \
81 self._prop_getters[iface][prop]()
84 @gtk_toolbox.log_exception(_moduleLogger)
85 def Send(self, messageType, text):
86 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
87 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
89 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
90 self._conn.session.textsStateMachine.reset_timers()
92 self.Sent(int(time.time()), messageType, text)
94 @gtk_toolbox.log_exception(_moduleLogger)
99 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
102 self._conn.session.texts.updateSignalHandler.unregister_sink(
105 self.__callback = None
107 telepathy.server.ChannelTypeText.Close(self)
108 if self.__manager.channel_exists(self.__props):
109 # HACK Older python-telepathy requires doing this manually
110 self.__manager.remove_channel(self)
111 self.remove_from_connection()
114 def _contactKey(self):
115 contactKey = self.__otherHandle.contactID, self.__otherHandle.phoneNumber
118 @gtk_toolbox.log_exception(_moduleLogger)
119 def _on_conversations_updated(self, conv, conversationIds):
120 if self._contactKey not in conversationIds:
122 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
123 mergedConversations = conv.get_conversation(self._contactKey)
124 self._report_conversation(mergedConversations)
126 def _report_conversation(self, mergedConversations):
127 newConversations = mergedConversations.conversations
128 newConversations = self._filter_out_reported(newConversations)
129 newConversations = self._filter_out_read(newConversations)
130 newConversations = list(newConversations)
131 if not newConversations:
133 "New messages for %r have already been read externally" % (self._contactKey, )
136 self.__lastMessageTimestamp = newConversations[-1].time
140 for newConversation in newConversations
141 for newMessage in newConversation.messages
142 if newMessage.whoFrom != "Me:"
144 if not newConversations:
146 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
150 for newMessage in messages:
151 formattedMessage = self._format_message(newMessage)
152 self._report_new_message(formattedMessage)
154 def _filter_out_reported(self, conversations):
157 for conversation in conversations
158 if self.__lastMessageTimestamp < conversation.time
161 def _filter_out_read(self, conversations):
164 for conversation in conversations
165 if not conversation.isRead and not conversation.isArchived
168 def _format_message(self, message):
169 return " ".join(part.text.strip() for part in message.body)
171 def _report_new_message(self, message):
172 currentReceivedId = self.__nextRecievedId
173 timestamp = int(time.time())
174 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
176 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
177 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
179 self.__nextRecievedId += 1