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
35 self._conn.add_logger(self)
37 @misc_utils.log_exception(_moduleLogger)
38 def Send(self, messageType, text):
39 if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
40 raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType)
42 self.Sent(int(time.time()), messageType, text)
44 oldStdin, oldStdout = self.stdin, self.stdout
46 self.stdin = currentStdin = StringIO.StringIO()
47 self.stdout = currentStdout = StringIO.StringIO()
50 self.stdin, self.stdout = oldStdin, oldStdout
52 stdoutData = currentStdout.getvalue().strip()
54 self._report_new_message(stdoutData)
56 @misc_utils.log_exception(_moduleLogger)
61 _moduleLogger.debug("Closing debug")
62 tp.ChannelTypeText.Close(self)
63 self.remove_from_connection()
64 self._conn.remove_logger(self)
66 def log_message(self, component, message):
67 formattedMessage = "LOG: %s\n%s" % (component, message)
68 self._report_new_message(formattedMessage)
70 def _report_new_message(self, message):
71 currentReceivedId = self.__nextRecievedId
73 timestamp = int(time.time())
74 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
76 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message.strip())
78 self.__nextRecievedId += 1
80 def do_reset_state_machine(self, args):
82 args = args.strip().lower()
86 for machine in self._conn.session.stateMachine._machines:
87 machine.reset_timers()
88 elif args == "contacts":
89 self._conn.session.addressbookStateMachine.reset_timers()
90 elif args == "voicemail":
91 self._conn.session.voicemailsStateMachine.reset_timers()
93 self._conn.session.textsStateMachine.reset_timers()
95 self._report_new_message('Unknown machine "%s"' % (args, ))
97 self._report_new_message(str(e))
99 def help_reset_state_machine(self):
100 self._report_new_message("""Reset the refreshing state machine.
101 "reset_state_machine" - resets all
102 "reset_state_machine all"
103 "reset_state_machine contacts"
104 "reset_state_machine voicemail"
105 "reset_state_machine texts"
108 def do_get_state(self, args):
110 self._report_new_message("No arguments supported")
114 state = self._conn.session.stateMachine.state
115 self._report_new_message(str(state))
117 self._report_new_message(str(e))
119 def help_get_state(self):
120 self._report_new_message("Print the current state the refreshing state machine is in")
122 def do_get_polling(self, args):
124 self._report_new_message("No arguments supported")
126 self._report_new_message("\n".join((
127 "Contacts:", repr(self._conn.session.addressbookStateMachine)
129 self._report_new_message("\n".join((
130 "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
132 self._report_new_message("\n".join((
133 "Texts:", repr(self._conn.session.textsStateMachine)
136 def help_get_polling(self):
137 self._report_new_message("Prints the frequency each of the state machines updates")
139 def do_get_state_status(self, args):
141 self._report_new_message("No arguments supported")
143 self._report_new_message("\n".join((
144 "Contacts:", str(self._conn.session.addressbookStateMachine)
146 self._report_new_message("\n".join((
147 "Voicemail:", str(self._conn.session.voicemailsStateMachine)
149 self._report_new_message("\n".join((
150 "Texts:", str(self._conn.session.textsStateMachine)
153 def help_get_state_status(self):
154 self._report_new_message("Prints the current setting for the state machines")
156 def do_is_authed(self, args):
157 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_authed)
160 @misc_utils.log_exception(_moduleLogger)
161 def _is_authed(self, args):
163 self._report_new_message("No arguments supported")
168 self._conn.session.backend.is_authed,
172 self._report_new_message(str(isAuthed))
174 self._report_new_message(str(e))
177 def help_is_authed(self):
178 self._report_new_message("Print whether logged in to Google Voice")
180 def do_is_dnd(self, args):
181 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_dnd)
184 @misc_utils.log_exception(_moduleLogger)
185 def _is_dnd(self, args):
187 self._report_new_message("No arguments supported")
192 self._conn.session.backend.is_dnd,
196 self._report_new_message(str(isDnd))
198 self._report_new_message(str(e))
201 def help_is_dnd(self):
202 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
204 def do_get_account_number(self, args):
206 self._report_new_message("No arguments supported")
210 number = self._conn.session.backend.get_account_number()
211 self._report_new_message(number)
213 self._report_new_message(str(e))
215 def help_get_account_number(self):
216 self._report_new_message("Print the Google Voice account number")
218 def do_get_callback_numbers(self, args):
220 self._report_new_message("No arguments supported")
224 numbers = self._conn.session.backend.get_callback_numbers()
225 numbersDisplay = "\n".join(
226 "%s: %s" % (name, number)
227 for (number, name) in numbers.iteritems()
229 self._report_new_message(numbersDisplay)
231 self._report_new_message(str(e))
233 def help_get_callback_numbers(self):
234 self._report_new_message("Print a list of all configured callback numbers")
236 def do_get_sane_callback_number(self, args):
238 self._report_new_message("No arguments supported")
242 number = gvoice.backend.get_sane_callback(self._conn.session.backend)
243 self._report_new_message(number)
245 self._report_new_message(str(e))
247 def help_get_sane_callback_number(self):
248 self._report_new_message("Print the best guess of callback numbers to use")
250 def do_get_callback_number(self, args):
252 self._report_new_message("No arguments supported")
256 number = self._conn.session.backend.get_callback_number()
257 self._report_new_message(number)
259 self._report_new_message(str(e))
261 def help_get_callback_number(self):
262 self._report_new_message("Print the callback number currently enabled")
264 def do_call(self, args):
265 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
268 @misc_utils.log_exception(_moduleLogger)
269 def _call(self, args):
271 self._report_new_message("Must specify the phone number and only the phone nunber")
277 self._conn.session.backend.call,
282 self._report_new_message(str(e))
285 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
287 def do_send_sms(self, args):
288 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._send_sms)
291 @misc_utils.log_exception(_moduleLogger)
292 def _send_sms(self, args):
293 args = args.split(" ")
295 self._report_new_message("Must specify the phone number and then message")
300 message = " ".join(args[1:])
302 self._conn.session.backend.send_sms,
307 self._report_new_message(str(e))
309 def help_send_sms(self):
310 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))
312 def do_version(self, args):
314 self._report_new_message("No arguments supported")
316 self._report_new_message("%s-%s" % (constants.__version__, constants.__build__))
318 def help_version(self):
319 self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
321 def do_grab_log(self, args):
323 self._report_new_message("No arguments supported")
327 publishProps = self._conn.generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
328 self._conn._channel_manager.channel_for_props(publishProps, signal=True)
330 self._report_new_message(str(e))
332 def help_grab_log(self):
333 self._report_new_message("Download the debug log for including with bug report")
334 self._report_new_message("Warning: this may contain sensitive information")
336 def do_save_log(self, args):
338 self._report_new_message("Must specify a filename to save the log to")
342 filename = os.path.expanduser(args)
343 with open(constants._user_logpath_, "r") as f:
344 logLines = f.xreadlines()
345 log = "".join(logLines)
346 with open(filename, "w") as f:
349 self._report_new_message(str(e))
351 def help_save_log(self):
352 self._report_new_message("Save the log to a specified location")