1 from __future__ import with_statement
14 import util.misc as misc_utils
15 import util.go_utils as gobject_utils
19 _moduleLogger = logging.getLogger(__name__)
22 class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
24 def __init__(self, connection, manager, props, contactHandle):
25 self.__manager = manager
28 cmd.Cmd.__init__(self, "Debug Prompt")
29 self.use_rawinput = False
30 tp.ChannelTypeText.__init__(self, connection, manager, props)
31 self.__nextRecievedId = 0
32 self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
34 self.__otherHandle = contactHandle
36 @misc_utils.log_exception(_moduleLogger)
37 def Send(self, messageType, text):
38 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
39 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
41 self.Sent(int(time.time()), messageType, text)
43 oldStdin, oldStdout = self.stdin, self.stdout
45 self.stdin = currentStdin = StringIO.StringIO()
46 self.stdout = currentStdout = StringIO.StringIO()
49 self.stdin, self.stdout = oldStdin, oldStdout
51 stdoutData = currentStdout.getvalue().strip()
53 self._report_new_message(stdoutData)
55 @misc_utils.log_exception(_moduleLogger)
60 _moduleLogger.debug("Closing debug")
61 tp.ChannelTypeText.Close(self)
62 self.remove_from_connection()
64 def _report_new_message(self, message):
65 currentReceivedId = self.__nextRecievedId
67 timestamp = int(time.time())
68 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
70 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message.strip())
72 self.__nextRecievedId += 1
74 def do_reset_state_machine(self, args):
76 args = args.strip().lower()
80 for machine in self._conn.session.stateMachine._machines:
81 machine.reset_timers()
82 elif args == "contacts":
83 self._conn.session.addressbookStateMachine.reset_timers()
84 elif args == "voicemail":
85 self._conn.session.voicemailsStateMachine.reset_timers()
87 self._conn.session.textsStateMachine.reset_timers()
89 self._report_new_message('Unknown machine "%s"' % (args, ))
91 self._report_new_message(str(e))
93 def help_reset_state_machine(self):
94 self._report_new_message("""Reset the refreshing state machine.
95 "reset_state_machine" - resets all
96 "reset_state_machine all"
97 "reset_state_machine contacts"
98 "reset_state_machine voicemail"
99 "reset_state_machine texts"
102 def do_get_state(self, args):
104 self._report_new_message("No arguments supported")
108 state = self._conn.session.stateMachine.state
109 self._report_new_message(str(state))
111 self._report_new_message(str(e))
113 def help_get_state(self):
114 self._report_new_message("Print the current state the refreshing state machine is in")
116 def do_get_polling(self, args):
118 self._report_new_message("No arguments supported")
120 self._report_new_message("\n".join((
121 "Contacts:", repr(self._conn.session.addressbookStateMachine)
123 self._report_new_message("\n".join((
124 "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
126 self._report_new_message("\n".join((
127 "Texts:", repr(self._conn.session.textsStateMachine)
130 def help_get_polling(self):
131 self._report_new_message("Prints the frequency each of the state machines updates")
133 def do_get_state_status(self, args):
135 self._report_new_message("No arguments supported")
137 self._report_new_message("\n".join((
138 "Contacts:", str(self._conn.session.addressbookStateMachine)
140 self._report_new_message("\n".join((
141 "Voicemail:", str(self._conn.session.voicemailsStateMachine)
143 self._report_new_message("\n".join((
144 "Texts:", str(self._conn.session.textsStateMachine)
147 def help_get_state_status(self):
148 self._report_new_message("Prints the current setting for the state machines")
150 def do_is_authed(self, args):
151 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_authed)
154 @misc_utils.log_exception(_moduleLogger)
155 def _is_authed(self, args):
157 self._report_new_message("No arguments supported")
162 self._conn.session.backend.is_authed,
166 self._report_new_message(str(isAuthed))
168 self._report_new_message(str(e))
170 def help_is_authed(self):
171 self._report_new_message("Print whether logged in to Google Voice")
173 def do_is_dnd(self, args):
174 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_dnd)
177 @misc_utils.log_exception(_moduleLogger)
178 def _is_dnd(self, args):
180 self._report_new_message("No arguments supported")
185 self._conn.session.backend.is_dnd,
189 self._report_new_message(str(isDnd))
191 self._report_new_message(str(e))
193 def help_is_dnd(self):
194 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
196 def do_get_account_number(self, args):
198 self._report_new_message("No arguments supported")
202 number = self._conn.session.backend.get_account_number()
203 self._report_new_message(number)
205 self._report_new_message(str(e))
207 def help_get_account_number(self):
208 self._report_new_message("Print the Google Voice account number")
210 def do_get_callback_numbers(self, args):
212 self._report_new_message("No arguments supported")
216 numbers = self._conn.session.backend.get_callback_numbers()
217 numbersDisplay = "\n".join(
218 "%s: %s" % (name, number)
219 for (number, name) in numbers.iteritems()
221 self._report_new_message(numbersDisplay)
223 self._report_new_message(str(e))
225 def help_get_callback_numbers(self):
226 self._report_new_message("Print a list of all configured callback numbers")
228 def do_get_sane_callback_number(self, args):
230 self._report_new_message("No arguments supported")
234 number = gvoice.backend.get_sane_callback(self._conn.session.backend)
235 self._report_new_message(number)
237 self._report_new_message(str(e))
239 def help_get_sane_callback_number(self):
240 self._report_new_message("Print the best guess of callback numbers to use")
242 def do_get_callback_number(self, args):
244 self._report_new_message("No arguments supported")
248 number = self._conn.session.backend.get_callback_number()
249 self._report_new_message(number)
251 self._report_new_message(str(e))
253 def help_get_callback_number(self):
254 self._report_new_message("Print the callback number currently enabled")
256 def do_call(self, args):
257 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
260 @misc_utils.log_exception(_moduleLogger)
261 def _call(self, args):
263 self._report_new_message("Must specify the phone number and only the phone nunber")
269 self._conn.session.backend.call,
274 self._report_new_message(str(e))
277 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
279 def do_send_sms(self, args):
280 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._send_sms)
283 @misc_utils.log_exception(_moduleLogger)
284 def _send_sms(self, args):
285 args = args.split(" ")
287 self._report_new_message("Must specify the phone number and then message")
292 message = " ".join(args[1:])
294 self._conn.session.backend.send_sms,
299 self._report_new_message(str(e))
301 def help_send_sms(self):
302 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))
304 def do_version(self, args):
306 self._report_new_message("No arguments supported")
308 self._report_new_message("%s-%s" % (constants.__version__, constants.__build__))
310 def help_version(self):
311 self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
313 def do_grab_log(self, args):
315 self._report_new_message("No arguments supported")
319 publishProps = self._conn.generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
320 self._conn._channel_manager.channel_for_props(publishProps, signal=True)
322 self._report_new_message(str(e))
324 def help_grab_log(self):
325 self._report_new_message("Download the debug log for including with bug report")
326 self._report_new_message("Warning: this may contain sensitive information")
328 def do_save_log(self, args):
330 self._report_new_message("Must specify a filename to save the log to")
334 filename = os.path.expanduser(args)
335 with open(constants._user_logpath_, "r") as f:
336 logLines = f.xreadlines()
337 log = "".join(logLines)
338 with open(filename, "w") as f:
341 self._report_new_message(str(e))
343 def help_save_log(self):
344 self._report_new_message("Save the log to a specified location")