f9c00ae8c4a67276d1243a29e4eb28f0dd04762d
[theonering] / src / channel / text.py
1 import time
2 import datetime
3 import logging
4
5 import telepathy
6
7 import util.go_utils as gobject_utils
8 import util.coroutines as coroutines
9 import gtk_toolbox
10
11
12 _moduleLogger = logging.getLogger("channel.text")
13
14
15 class TextChannel(telepathy.server.ChannelTypeText):
16         """
17         Look into implementing ChannelInterfaceMessages for rich text formatting
18         """
19
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)
24
25                 self._otherHandle = h
26
27                 self._conn.session.conversations.updateSignalHandler.register_sink(
28                         self._on_conversations_updated
29                 )
30
31                 # The only reason there should be anything in the conversation is if
32                 # its new, so report it all
33                 try:
34                         conversation = self._conn.session.conversations.get_conversation(self._contactKey)
35                 except KeyError:
36                         pass
37                 else:
38                         self._report_conversation(conversation)
39
40         @gtk_toolbox.log_exception(_moduleLogger)
41         def Send(self, messageType, text):
42                 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
43                         raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
44
45                 self._conn.session.backend.send_sms(self._otherHandle.phoneNumber, text)
46
47                 self.Sent(int(time.time()), messageType, text)
48
49         @gtk_toolbox.log_exception(_moduleLogger)
50         def Close(self):
51                 try:
52                         # Clear since the user has seen it all and it should start a new conversation
53                         self._conn.session.conversations.clear_conversation(self._contactKey)
54
55                         self._conn.session.conversations.updateSignalHandler.unregister_sink(
56                                 self._on_conversations_updated
57                         )
58                 finally:
59                         telepathy.server.ChannelTypeText.Close(self)
60                         self.remove_from_connection()
61
62         @property
63         def _contactKey(self):
64                 contactKey = self._otherHandle.contactID, self._otherHandle.phoneNumber
65                 return contactKey
66
67         @coroutines.func_sink
68         @coroutines.expand_positional
69         @gobject_utils.async
70         def _on_conversations_updated(self, conversationIds):
71                 if self._contactKey not in conversationIds:
72                         return
73                 conversation = self._conn.session.conversations.get_conversation(self._contactKey)
74                 self._report_conversation(conversation)
75
76         def _report_conversation(self, conversation):
77                 # @bug Check if messages sent need to be filtered out
78                 completeMessageHistory = conversation["messageParts"]
79                 messages = self._filter_seen_messages(completeMessageHistory)
80                 self._lastMessageTimestamp = messages[-1][0]
81                 formattedMessage = self._format_messages(messages)
82                 self._report_new_message(formattedMessage)
83
84         def _filter_seen_messages(self, messages):
85                 return (
86                         message
87                         for message in messages
88                         if self._lastMessageTimestamp < message[0]
89                 )
90
91         def _format_messages(self, messages):
92                 return "\n".join(message[1] for message in messages)
93
94         def _report_new_message(self, message):
95                 currentReceivedId = self._nextRecievedId
96
97                 timestamp = int(time.time())
98                 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
99                 message = message.content
100
101                 _moduleLogger.info("Received message from User %r" % self._otherHandle)
102                 self.Received(id, timestamp, self._otherHandle, type, 0, message)
103
104                 self._nextRecievedId += 1