1 from __future__ import with_statement
18 _moduleLogger = logging.getLogger("channel.debug_prompt")
21 class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
23 def __init__(self, connection, manager, props, contactHandle):
24 self.__manager = manager
27 cmd.Cmd.__init__(self, "Debug Prompt")
28 self.use_rawinput = False
29 tp.ChannelTypeText.__init__(self, connection, manager, props)
30 self.__nextRecievedId = 0
31 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
33 self.__otherHandle = contactHandle
35 @gtk_toolbox.log_exception(_moduleLogger)
36 def Send(self, messageType, text):
37 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
38 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
40 self.Sent(int(time.time()), messageType, text)
42 oldStdin, oldStdout = self.stdin, self.stdout
44 self.stdin = currentStdin = StringIO.StringIO()
45 self.stdout = currentStdout = StringIO.StringIO()
48 self.stdin, self.stdout = oldStdin, oldStdout
50 stdoutData = currentStdout.getvalue().strip()
52 self._report_new_message(stdoutData)
54 @gtk_toolbox.log_exception(_moduleLogger)
59 _moduleLogger.debug("Closing debug")
60 tp.ChannelTypeText.Close(self)
61 self.remove_from_connection()
63 def _report_new_message(self, message):
64 currentReceivedId = self.__nextRecievedId
66 timestamp = int(time.time())
67 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
69 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message.strip())
71 self.__nextRecievedId += 1
73 def do_reset_state_machine(self, args):
75 args = args.strip().lower()
79 for machine in self._conn.session.stateMachine._machines:
80 machine.reset_timers()
81 elif args == "contacts":
82 self._conn.session.addressbookStateMachine.reset_timers()
83 elif args == "voicemail":
84 self._conn.session.voicemailsStateMachine.reset_timers()
86 self._conn.session.textsStateMachine.reset_timers()
88 self._report_new_message('Unknown machine "%s"' % (args, ))
90 self._report_new_message(str(e))
92 def help_reset_state_machine(self):
93 self._report_new_message("""Reset the refreshing state machine.
94 "reset_state_machine" - resets all
95 "reset_state_machine all"
96 "reset_state_machine contacts"
97 "reset_state_machine voicemail"
98 "reset_state_machine texts"
101 def do_get_state(self, args):
103 self._report_new_message("No arguments supported")
107 state = self._conn.session.stateMachine.state
108 self._report_new_message(str(state))
110 self._report_new_message(str(e))
112 def help_get_state(self):
113 self._report_new_message("Print the current state the refreshing state machine is in")
115 def do_is_authed(self, args):
117 self._report_new_message("No arguments supported")
121 isAuthed = self._conn.session.backend.is_authed()
122 self._report_new_message(str(isAuthed))
124 self._report_new_message(str(e))
126 def help_is_authed(self):
127 self._report_new_message("Print whether logged in to Google Voice")
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 help_is_dnd(self):
141 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
143 def do_get_account_number(self, args):
145 self._report_new_message("No arguments supported")
149 number = self._conn.session.backend.get_account_number()
150 self._report_new_message(number)
152 self._report_new_message(str(e))
154 def help_get_account_number(self):
155 self._report_new_message("Print the Google Voice account number")
157 def do_get_callback_numbers(self, args):
159 self._report_new_message("No arguments supported")
163 numbers = self._conn.session.backend.get_callback_numbers()
164 numbersDisplay = "\n".join(
165 "%s: %s" % (name, number)
166 for (number, name) in numbers.iteritems()
168 self._report_new_message(numbersDisplay)
170 self._report_new_message(str(e))
172 def help_get_callback_numbers(self):
173 self._report_new_message("Print a list of all configured callback numbers")
175 def do_get_sane_callback_number(self, args):
177 self._report_new_message("No arguments supported")
181 number = gvoice.backend.get_sane_callback(self._conn.session.backend)
182 self._report_new_message(number)
184 self._report_new_message(str(e))
186 def help_get_sane_callback_number(self):
187 self._report_new_message("Print the best guess of callback numbers to use")
189 def do_get_callback_number(self, args):
191 self._report_new_message("No arguments supported")
195 number = self._conn.session.backend.get_callback_number()
196 self._report_new_message(number)
198 self._report_new_message(str(e))
200 def help_get_callback_number(self):
201 self._report_new_message("Print the callback number currently enabled")
203 def do_call(self, args):
205 self._report_new_message("Must specify the phone number and only the phone nunber")
210 self._conn.session.backend.call(number)
212 self._report_new_message(str(e))
215 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
217 def do_send_sms(self, args):
218 args = args.split(" ")
220 self._report_new_message("Must specify the phone number and then message")
225 message = " ".join(args[1:])
226 self._conn.session.backend.send_sms(number, message)
228 self._report_new_message(str(e))
230 def help_send_sms(self):
231 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))
233 def do_version(self, args):
235 self._report_new_message("No arguments supported")
237 self._report_new_message("%s-%s" % (constants.__version__, constants.__build__))
239 def help_version(self):
240 self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
242 def do_get_polling(self, args):
244 self._report_new_message("No arguments supported")
246 self._report_new_message("\n".join((
247 "Contacts:", repr(self._conn.session.addressbookStateMachine)
249 self._report_new_message("\n".join((
250 "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
252 self._report_new_message("\n".join((
253 "Texts:", repr(self._conn.session.textsStateMachine)
256 def help_get_polling(self):
257 self._report_new_message("Prints the frequency each of the state machines updates")
259 def do_grab_log(self, args):
261 self._report_new_message("No arguments supported")
265 publishProps = self._conn._generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
266 self._conn._channel_manager.channel_for_props(publishProps, signal=True)
268 self._report_new_message(str(e))
270 def help_grab_log(self):
271 self._report_new_message("Download the debug log for including with bug report")
272 self._report_new_message("Warning: this may contain sensitive information")
274 def do_save_log(self, args):
276 self._report_new_message("Must specify a filename to save the log to")
280 filename = os.path.expanduser(args)
281 with open(constants._user_logpath_, "r") as f:
282 logLines = f.xreadlines()
283 log = "".join(logLines)
284 with open(filename, "w") as f:
287 self._report_new_message(str(e))
289 def help_save_log(self):
290 self._report_new_message("Save the log to a specified location")