Adding get_sane_callback to the debug promot
[theonering] / src / channel / debug_prompt.py
1 import cmd
2 import StringIO
3 import time
4 import datetime
5 import logging
6
7 import telepathy
8
9 import tp
10 import gtk_toolbox
11 import gvoice
12
13
14 _moduleLogger = logging.getLogger("channel.text")
15
16
17 class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
18         """
19         @todo Look into implementing ChannelInterfaceMessages for rich text formatting
20         @todo Add a command that initiates a file transfer, giving the log file to the user
21         """
22
23         def __init__(self, connection, manager, props, contactHandle):
24                 self.__manager = manager
25                 self.__props = props
26
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)
32
33                 self.__otherHandle = contactHandle
34
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)
39
40                 self.Sent(int(time.time()), messageType, text)
41
42                 oldStdin, oldStdout = self.stdin, self.stdout
43                 try:
44                         self.stdin = currentStdin = StringIO.StringIO()
45                         self.stdout = currentStdout = StringIO.StringIO()
46                         self.onecmd(text)
47                 finally:
48                         self.stdin, self.stdout = oldStdin, oldStdout
49
50                 self._report_new_message(currentStdout.getvalue())
51
52         @gtk_toolbox.log_exception(_moduleLogger)
53         def Close(self):
54                 self.close()
55
56         def close(self):
57                 _moduleLogger.debug("Closing debug")
58                 tp.ChannelTypeText.Close(self)
59                 self.remove_from_connection()
60
61         def _report_new_message(self, message):
62                 currentReceivedId = self.__nextRecievedId
63
64                 timestamp = int(time.time())
65                 type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
66
67                 self.Received(currentReceivedId, timestamp, self.__otherHandle, type, 0, message.strip())
68
69                 self.__nextRecievedId += 1
70
71         def do_reset_state_machine(self, args):
72                 if args:
73                         self._report_new_message("No arguments supported")
74                         return
75
76                 try:
77                         for machine in self._conn.session.stateMachine._machines:
78                                 machine.reset_timers()
79                 except Exception, e:
80                         self._report_new_message(str(e))
81
82         def help_reset_state_machine(self):
83                 self._report_new_message("Reset the refreshing state machine")
84
85         def do_get_state(self, args):
86                 if args:
87                         self._report_new_message("No arguments supported")
88                         return
89
90                 try:
91                         state = self._conn.session.stateMachine.state
92                         self._report_new_message(str(state))
93                 except Exception, e:
94                         self._report_new_message(str(e))
95
96         def help_get_state(self):
97                 self._report_new_message("Print the current state the refreshing state machine is in")
98
99         def do_is_authed(self, args):
100                 if args:
101                         self._report_new_message("No arguments supported")
102                         return
103
104                 try:
105                         isAuthed = self._conn.session.backend.is_authed()
106                         self._report_new_message(str(isAuthed))
107                 except Exception, e:
108                         self._report_new_message(str(e))
109
110         def help_is_authed(self):
111                 self._report_new_message("Print whether logged in to Google Voice")
112
113         def do_is_dnd(self, args):
114                 if args:
115                         self._report_new_message("No arguments supported")
116                         return
117
118                 try:
119                         isDnd = self._conn.session.backend.is_dnd()
120                         self._report_new_message(str(isDnd))
121                 except Exception, e:
122                         self._report_new_message(str(e))
123
124         def help_is_dnd(self):
125                 self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
126
127         def do_get_account_number(self, args):
128                 if args:
129                         self._report_new_message("No arguments supported")
130                         return
131
132                 try:
133                         number = self._conn.session.backend.get_account_number()
134                         self._report_new_message(number)
135                 except Exception, e:
136                         self._report_new_message(str(e))
137
138         def help_get_account_number(self):
139                 self._report_new_message("Print the Google Voice account number")
140
141         def do_get_callback_numbers(self, args):
142                 if args:
143                         self._report_new_message("No arguments supported")
144                         return
145
146                 try:
147                         numbers = self._conn.session.backend.get_callback_numbers()
148                         numbersDisplay = "\n".join(
149                                 "%s: %s" % (name, number)
150                                 for (number, name) in numbers.iteritems()
151                         )
152                         self._report_new_message(numbersDisplay)
153                 except Exception, e:
154                         self._report_new_message(str(e))
155
156         def help_get_callback_numbers(self):
157                 self._report_new_message("Print a list of all configured callback numbers")
158
159         def do_get_sane_callback_number(self, args):
160                 if args:
161                         self._report_new_message("No arguments supported")
162                         return
163
164                 try:
165                         number = gvoice.backend.get_sane_callback(self._conn.session.backend)
166                         self._report_new_message(number)
167                 except Exception, e:
168                         self._report_new_message(str(e))
169
170         def help_get_sane_callback_number(self):
171                 self._report_new_message("Print the best guess of callback numbers to use")
172
173         def do_get_callback_number(self, args):
174                 if args:
175                         self._report_new_message("No arguments supported")
176                         return
177
178                 try:
179                         number = self._conn.session.backend.get_callback_number()
180                         self._report_new_message(number)
181                 except Exception, e:
182                         self._report_new_message(str(e))
183
184         def help_get_callback_number(self):
185                 self._report_new_message("Print the callback number currently enabled")
186
187         def do_call(self, args):
188                 if len(args) != 1:
189                         self._report_new_message("Must specify the phone number and only the phone nunber")
190                         return
191
192                 try:
193                         number = args[0]
194                         self._conn.session.backend.call(number)
195                 except Exception, e:
196                         self._report_new_message(str(e))
197
198         def help_call(self):
199                 self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
200
201         def do_send_sms(self, args):
202                 if 1 < len(args):
203                         self._report_new_message("Must specify the phone number and then message")
204                         return
205
206                 try:
207                         number = args[0]
208                         message = " ".join(args[1:])
209                         self._conn.session.backend.send_sms(number, message)
210                 except Exception, e:
211                         self._report_new_message(str(e))
212
213         def help_send_sms(self):
214                 self._report_new_message("\n".join(["send_sms NUMBER MESSAGE0 MESSAGE1 ...", "Send an sms to number NUMBER"]))