1 from __future__ import with_statement
14 import util.misc as misc_utils
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 @misc_utils.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 @misc_utils.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_get_polling(self, args):
117 self._report_new_message("No arguments supported")
119 self._report_new_message("\n".join((
120 "Contacts:", repr(self._conn.session.addressbookStateMachine)
122 self._report_new_message("\n".join((
123 "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
125 self._report_new_message("\n".join((
126 "Texts:", repr(self._conn.session.textsStateMachine)
129 def help_get_polling(self):
130 self._report_new_message("Prints the frequency each of the state machines updates")
132 def do_get_state_status(self, args):
134 self._report_new_message("No arguments supported")
136 self._report_new_message("\n".join((
137 "Contacts:", str(self._conn.session.addressbookStateMachine)
139 self._report_new_message("\n".join((
140 "Voicemail:", str(self._conn.session.voicemailsStateMachine)
142 self._report_new_message("\n".join((
143 "Texts:", str(self._conn.session.textsStateMachine)
146 def help_get_state_status(self):
147 self._report_new_message("Prints the current setting for the state machines")
149 def do_is_authed(self, args):
151 self._report_new_message("No arguments supported")
155 isAuthed = self._conn.session.backend.is_authed()
156 self._report_new_message(str(isAuthed))
158 self._report_new_message(str(e))
160 def help_is_authed(self):
161 self._report_new_message("Print whether logged in to Google Voice")
163 def do_is_dnd(self, args):
165 self._report_new_message("No arguments supported")
169 isDnd = self._conn.session.backend.is_dnd()
170 self._report_new_message(str(isDnd))
172 self._report_new_message(str(e))
174 def help_is_dnd(self):
175 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
177 def do_get_account_number(self, args):
179 self._report_new_message("No arguments supported")
183 number = self._conn.session.backend.get_account_number()
184 self._report_new_message(number)
186 self._report_new_message(str(e))
188 def help_get_account_number(self):
189 self._report_new_message("Print the Google Voice account number")
191 def do_get_callback_numbers(self, args):
193 self._report_new_message("No arguments supported")
197 numbers = self._conn.session.backend.get_callback_numbers()
198 numbersDisplay = "\n".join(
199 "%s: %s" % (name, number)
200 for (number, name) in numbers.iteritems()
202 self._report_new_message(numbersDisplay)
204 self._report_new_message(str(e))
206 def help_get_callback_numbers(self):
207 self._report_new_message("Print a list of all configured callback numbers")
209 def do_get_sane_callback_number(self, args):
211 self._report_new_message("No arguments supported")
215 number = gvoice.backend.get_sane_callback(self._conn.session.backend)
216 self._report_new_message(number)
218 self._report_new_message(str(e))
220 def help_get_sane_callback_number(self):
221 self._report_new_message("Print the best guess of callback numbers to use")
223 def do_get_callback_number(self, args):
225 self._report_new_message("No arguments supported")
229 number = self._conn.session.backend.get_callback_number()
230 self._report_new_message(number)
232 self._report_new_message(str(e))
234 def help_get_callback_number(self):
235 self._report_new_message("Print the callback number currently enabled")
237 def do_call(self, args):
239 self._report_new_message("Must specify the phone number and only the phone nunber")
244 self._conn.session.backend.call(number)
246 self._report_new_message(str(e))
249 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
251 def do_send_sms(self, args):
252 args = args.split(" ")
254 self._report_new_message("Must specify the phone number and then message")
259 message = " ".join(args[1:])
260 self._conn.session.backend.send_sms(number, message)
262 self._report_new_message(str(e))
264 def help_send_sms(self):
265 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))
267 def do_version(self, args):
269 self._report_new_message("No arguments supported")
271 self._report_new_message("%s-%s" % (constants.__version__, constants.__build__))
273 def help_version(self):
274 self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
276 def do_grab_log(self, args):
278 self._report_new_message("No arguments supported")
282 publishProps = self._conn.generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
283 self._conn._channel_manager.channel_for_props(publishProps, signal=True)
285 self._report_new_message(str(e))
287 def help_grab_log(self):
288 self._report_new_message("Download the debug log for including with bug report")
289 self._report_new_message("Warning: this may contain sensitive information")
291 def do_save_log(self, args):
293 self._report_new_message("Must specify a filename to save the log to")
297 filename = os.path.expanduser(args)
298 with open(constants._user_logpath_, "r") as f:
299 logLines = f.xreadlines()
300 log = "".join(logLines)
301 with open(filename, "w") as f:
304 self._report_new_message(str(e))
306 def help_save_log(self):
307 self._report_new_message("Save the log to a specified location")