12 _moduleLogger = logging.getLogger("channel.text")
15 class DebugPromptChannel(telepathy.server.ChannelTypeText, cmd.Cmd):
17 Look into implementing ChannelInterfaceMessages for rich text formatting
20 def __init__(self, connection, manager, props, contactHandle):
21 self.__manager = manager
24 cmd.Cmd.__init__(self, "Debug Prompt")
25 self.use_rawinput = False
27 # HACK Older python-telepathy way
28 telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle)
29 self._requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
30 self._implement_property_get(
31 telepathy.interfaces.CHANNEL_INTERFACE,
32 {"Requested": lambda: self._requested}
35 # HACK Newer python-telepathy way
36 telepathy.server.ChannelTypeText.__init__(self, connection, manager, props)
37 self.__nextRecievedId = 0
38 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
40 self.__otherHandle = contactHandle
43 # HACK Older python-telepathy doesn't provide this
44 _immutable_properties = {
45 'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
46 'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
47 'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
48 'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
49 'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
50 'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
53 for prop, iface in _immutable_properties.items():
54 props[iface + '.' + prop] = \
55 self._prop_getters[iface][prop]()
58 @gtk_toolbox.log_exception(_moduleLogger)
59 def Send(self, messageType, text):
60 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
61 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
63 self.Sent(int(time.time()), messageType, text)
65 oldStdin, oldStdout = self.stdin, self.stdout
67 self.stdin = currentStdin = StringIO.StringIO()
68 self.stdout = currentStdout = StringIO.StringIO()
71 self.stdin, self.stdout = oldStdin, oldStdout
73 self._report_new_message(currentStdout.getvalue())
75 @gtk_toolbox.log_exception(_moduleLogger)
80 telepathy.server.ChannelTypeText.Close(self)
81 if self.__manager.channel_exists(self.__props):
82 # HACK Older python-telepathy requires doing this manually
83 self.__manager.remove_channel(self)
84 self.remove_from_connection()
86 def _report_new_message(self, message):
87 currentReceivedId = self.__nextRecievedId
89 timestamp = int(time.time())
90 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
92 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message.strip())
94 self.__nextRecievedId += 1
96 def do_reset_state_machine(self, args):
98 self._report_new_message("No arguments supported")
102 for machine in self._conn.session.stateMachine._machines:
103 machine.reset_timers()
105 self._report_new_message(str(e))
107 def do_get_state(self, args):
109 self._report_new_message("No arguments supported")
113 state = self._conn.session.stateMachine.state
114 self._report_new_message(str(state))
116 self._report_new_message(str(e))
118 def do_is_authed(self, args):
120 self._report_new_message("No arguments supported")
124 isAuthed = self._conn.session.backend.is_authed()
125 self._report_new_message(str(isAuthed))
127 self._report_new_message(str(e))
129 def do_is_dnd(self, args):
131 self._report_new_message("No arguments supported")
135 isDnd = self._conn.session.backend.is_dnd()
136 self._report_new_message(str(isDnd))
138 self._report_new_message(str(e))
140 def do_get_account_number(self, args):
142 self._report_new_message("No arguments supported")
146 number = self._conn.session.backend.get_account_number()
147 self._report_new_message(number)
149 self._report_new_message(str(e))
151 def do_get_callback_numbers(self, args):
153 self._report_new_message("No arguments supported")
157 numbers = self._conn.session.backend.get_callback_numbers()
158 numbersDisplay = "\n".join(
159 "%s: %s" % (name, number)
160 for (number, name) in numbers.iteritems()
162 self._report_new_message(numbersDisplay)
164 self._report_new_message(str(e))
166 def do_get_callback_number(self, args):
168 self._report_new_message("No arguments supported")
172 number = self._conn.session.backend.get_callback_number()
173 self._report_new_message(number)
175 self._report_new_message(str(e))
177 def do_call(self, args):
179 self._report_new_message("Must specify the phone number and only the phone nunber")
184 self._conn.session.backend.call(number)
186 self._report_new_message(str(e))
188 def do_send_sms(self, args):
190 self._report_new_message("Must specify the phone number and then message")
195 message = " ".join(args[1:])
196 self._conn.session.backend.send_sms(number, message)
198 self._report_new_message(str(e))