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