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))
171 def help_is_authed(self):
172 self._report_new_message("Print whether logged in to Google Voice")
174 def do_is_dnd(self, args):
175 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_dnd)
178 @misc_utils.log_exception(_moduleLogger)
179 def _is_dnd(self, args):
181 self._report_new_message("No arguments supported")
186 self._conn.session.backend.is_dnd,
190 self._report_new_message(str(isDnd))
192 self._report_new_message(str(e))
195 def help_is_dnd(self):
196 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
198 def do_get_account_number(self, args):
200 self._report_new_message("No arguments supported")
204 number = self._conn.session.backend.get_account_number()
205 self._report_new_message(number)
207 self._report_new_message(str(e))
209 def help_get_account_number(self):
210 self._report_new_message("Print the Google Voice account number")
212 def do_get_callback_numbers(self, args):
214 self._report_new_message("No arguments supported")
218 numbers = self._conn.session.backend.get_callback_numbers()
219 numbersDisplay = "\n".join(
220 "%s: %s" % (name, number)
221 for (number, name) in numbers.iteritems()
223 self._report_new_message(numbersDisplay)
225 self._report_new_message(str(e))
227 def help_get_callback_numbers(self):
228 self._report_new_message("Print a list of all configured callback numbers")
230 def do_get_sane_callback_number(self, args):
232 self._report_new_message("No arguments supported")
236 number = gvoice.backend.get_sane_callback(self._conn.session.backend)
237 self._report_new_message(number)
239 self._report_new_message(str(e))
241 def help_get_sane_callback_number(self):
242 self._report_new_message("Print the best guess of callback numbers to use")
244 def do_get_callback_number(self, args):
246 self._report_new_message("No arguments supported")
250 number = self._conn.session.backend.get_callback_number()
251 self._report_new_message(number)
253 self._report_new_message(str(e))
255 def help_get_callback_number(self):
256 self._report_new_message("Print the callback number currently enabled")
258 def do_call(self, args):
259 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
262 @misc_utils.log_exception(_moduleLogger)
263 def _call(self, args):
265 self._report_new_message("Must specify the phone number and only the phone nunber")
271 self._conn.session.backend.call,
276 self._report_new_message(str(e))
279 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
281 def do_send_sms(self, args):
282 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._send_sms)
285 @misc_utils.log_exception(_moduleLogger)
286 def _send_sms(self, args):
287 args = args.split(" ")
289 self._report_new_message("Must specify the phone number and then message")
294 message = " ".join(args[1:])
296 self._conn.session.backend.send_sms,
301 self._report_new_message(str(e))
303 def help_send_sms(self):
304 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))
306 def do_version(self, args):
308 self._report_new_message("No arguments supported")
310 self._report_new_message("%s-%s" % (constants.__version__, constants.__build__))
312 def help_version(self):
313 self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
315 def do_grab_log(self, args):
317 self._report_new_message("No arguments supported")
321 publishProps = self._conn.generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
322 self._conn._channel_manager.channel_for_props(publishProps, signal=True)
324 self._report_new_message(str(e))
326 def help_grab_log(self):
327 self._report_new_message("Download the debug log for including with bug report")
328 self._report_new_message("Warning: this may contain sensitive information")
330 def do_save_log(self, args):
332 self._report_new_message("Must specify a filename to save the log to")
336 filename = os.path.expanduser(args)
337 with open(constants._user_logpath_, "r") as f:
338 logLines = f.xreadlines()
339 log = "".join(logLines)
340 with open(filename, "w") as f:
343 self._report_new_message(str(e))
345 def help_save_log(self):
346 self._report_new_message("Save the log to a specified location")