Lots of work in prep for conversations to work
[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_message_received
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[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.clear_conversation(self._contactKey)
54
55                         self._conn.session.conversations.updateSignalHandler.unregister_sink(
56                                 self._on_message_received
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[self._contactKey]
74                 self._report_conversation(conversation)
75
76         def _report_conversation(self, conversation):
77                 completeMessageHistory = conversation["messageParts"]
78                 messages = self._filter_seen_messages(completeMessageHistory)
79                 self._lastMessageTimestamp = messages[-1][0]
80                 formattedMessage = self._format_messages(messages)
81                 self._report_new_message(formattedMessage)
82
83         def _filter_seen_messages(self, messages):
84                 return (
85                         message
86                         for message in messages
87                         if self._lastMessageTimestamp < message[0]
88                 )
89
90         def _format_messages(self, messages):
91                 return "\n".join(message[1] for message in messages)
92
93         def _report_new_message(self, message):
94                 currentReceivedId = self._nextRecievedId
95
96                 timestamp = int(time.time())
97                 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
98                 message = message.content
99
100                 _moduleLogger.info("Received message from User %r" % self._otherHandle)
101                 self.Received(id, timestamp, self._otherHandle, type, 0, message)
102
103                 self._nextRecievedId += 1