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 self.__callback = coroutines.func_sink(
40 coroutines.expand_positional(
41 self._on_conversations_updated
44 self._conn.session.voicemails.updateSignalHandler.register_sink(
47 self._conn.session.texts.updateSignalHandler.register_sink(
51 # The only reason there should be anything in the conversation is if
52 # its new, so report it all
54 mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
56 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
58 self._report_conversation(mergedConversations)
60 mergedConversations = self._conn.session.texts.get_conversation(self._contactKey)
62 _moduleLogger.debug("Nothing in the conversation yet for %r" % (self._contactKey, ))
64 self._report_conversation(mergedConversations)
67 # HACK Older python-telepathy doesn't provide this
68 _immutable_properties = {
69 'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
70 'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
71 'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
72 'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
73 'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
74 'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
77 for prop, iface in _immutable_properties.items():
78 props[iface + '.' + prop] = \
79 self._prop_getters[iface][prop]()
82 @gtk_toolbox.log_exception(_moduleLogger)
83 def Send(self, messageType, text):
84 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
85 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
87 self._conn.session.backend.send_sms(self.__otherHandle.phoneNumber, text)
88 self._conn.session.textsStateMachine.reset_timers()
90 self.Sent(int(time.time()), messageType, text)
92 @gtk_toolbox.log_exception(_moduleLogger)
97 self._conn.session.voicemails.updateSignalHandler.unregister_sink(
100 self._conn.session.texts.updateSignalHandler.unregister_sink(
103 self.__callback = None
105 telepathy.server.ChannelTypeText.Close(self)
106 if self.__manager.channel_exists(self.__props):
107 # HACK Older python-telepathy requires doing this manually
108 self.__manager.remove_channel(self)
109 self.remove_from_connection()
112 def _contactKey(self):
113 contactKey = self.__otherHandle.contactID, self.__otherHandle.phoneNumber
116 @gtk_toolbox.log_exception(_moduleLogger)
117 def _on_conversations_updated(self, conv, conversationIds):
118 if self._contactKey not in conversationIds:
120 _moduleLogger.debug("Incoming messages from %r for existing conversation" % (self._contactKey, ))
121 mergedConversations = conv.get_conversation(self._contactKey)
122 self._report_conversation(mergedConversations)
124 def _report_conversation(self, mergedConversations):
125 newConversations = mergedConversations.conversations
126 newConversations = self._filter_out_reported(newConversations)
127 newConversations = self._filter_out_read(newConversations)
128 newConversations = list(newConversations)
129 if not newConversations:
131 "New messages for %r have already been read externally" % (self._contactKey, )
134 self.__lastMessageTimestamp = newConversations[-1].time
138 for newConversation in newConversations
139 for newMessage in newConversation.messages
140 if newMessage.whoFrom != "Me:"
142 if not newConversations:
144 "All incoming messages were really outbound messages for %r" % (self._contactKey, )
148 for newMessage in messages:
149 formattedMessage = self._format_message(newMessage)
150 self._report_new_message(formattedMessage)
152 def _filter_out_reported(self, conversations):
155 for conversation in conversations
156 if self.__lastMessageTimestamp < conversation.time
159 def _filter_out_read(self, conversations):
162 for conversation in conversations
163 if not conversation.isRead and not conversation.isArchived
166 def _format_message(self, message):
167 return " ".join(part.text.strip() for part in message.body)
169 def _report_new_message(self, message):
170 currentReceivedId = self.__nextRecievedId
171 timestamp = int(time.time())
172 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
174 _moduleLogger.info("Received message from User %r" % self.__otherHandle)
175 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message)
177 self.__nextRecievedId += 1