40
40
urwid.curses_display.curses.A_UNDERLINE |= (
41
41
urwid.curses_display.curses.A_BLINK)
43
def isoformat_to_datetime(iso):
44
"Parse an ISO 8601 date string to a datetime.datetime()"
47
d, t = iso.split(u"T", 1)
48
year, month, day = d.split(u"-", 2)
49
hour, minute, second = t.split(u":", 2)
50
second, fraction = divmod(float(second), 1)
51
return datetime.datetime(int(year),
56
int(second), # Whole seconds
57
int(fraction*1000000)) # Microseconds
43
59
class MandosClientPropertyCache(object):
44
60
"""This wraps a Mandos Client D-Bus proxy object, caches the
45
61
properties and calls a hook function when any of them are
57
73
self.properties.update(
58
74
self.proxy.GetAll(client_interface,
59
75
dbus_interface = dbus.PROPERTIES_IFACE))
60
super(MandosClientPropertyCache, self).__init__(
61
proxy_object=proxy_object, *args, **kwargs)
77
#XXX This break good super behaviour!
78
# super(MandosClientPropertyCache, self).__init__(
63
81
def property_changed(self, property=None, value=None):
64
82
"""This is called whenever we get a PropertyChanged signal
113
134
client_interface,
114
135
byte_arrays=True)
136
last_checked_ok = isoformat_to_datetime(self.properties
138
if last_checked_ok is None:
139
self.last_checker_failed = True
141
self.last_checker_failed = ((datetime.datetime.utcnow()
147
if self.last_checker_failed:
148
self._update_timer_callback_tag = (gobject.timeout_add
116
152
def checker_completed(self, exitstatus, condition, command):
117
153
if exitstatus == 0:
118
#self.logger(u'Checker for client %s (command "%s")'
120
# % (self.properties[u"name"], command))
154
if self.last_checker_failed:
155
self.last_checker_failed = False
156
gobject.source_remove(self._update_timer_callback_tag)
157
self._update_timer_callback_tag = None
158
self.logger(u'Checker for client %s (command "%s")'
160
% (self.properties[u"Name"], command))
164
if not self.last_checker_failed:
165
self.last_checker_failed = True
166
self._update_timer_callback_tag = (gobject.timeout_add
122
169
if os.WIFEXITED(condition):
123
170
self.logger(u'Checker for client %s (command "%s")'
124
171
u' failed with exit code %s'
125
% (self.properties[u"name"], command,
172
% (self.properties[u"Name"], command,
126
173
os.WEXITSTATUS(condition)))
128
if os.WIFSIGNALED(condition):
174
elif os.WIFSIGNALED(condition):
129
175
self.logger(u'Checker for client %s (command "%s")'
130
176
u' was killed by signal %s'
131
% (self.properties[u"name"], command,
177
% (self.properties[u"Name"], command,
132
178
os.WTERMSIG(condition)))
134
if os.WCOREDUMP(condition):
179
elif os.WCOREDUMP(condition):
135
180
self.logger(u'Checker for client %s (command "%s")'
137
% (self.properties[u"name"], command))
138
self.logger(u'Checker for client %s completed mysteriously')
182
% (self.properties[u"Name"], command))
184
self.logger(u'Checker for client %s completed'
140
188
def checker_started(self, command):
141
189
#self.logger(u'Client %s started checker "%s"'
142
# % (self.properties[u"name"], unicode(command)))
190
# % (self.properties[u"Name"], unicode(command)))
145
193
def got_secret(self):
194
self.last_checker_failed = False
146
195
self.logger(u'Client %s received its secret'
147
% self.properties[u"name"])
196
% self.properties[u"Name"])
149
198
def need_approval(self, timeout, default):
153
202
message = u'Client %s will get its secret in %s seconds'
154
203
self.logger(message
155
% (self.properties[u"name"], timeout/1000))
204
% (self.properties[u"Name"], timeout/1000))
157
206
def rejected(self, reason):
158
207
self.logger(u'Client %s was rejected; reason: %s'
159
% (self.properties[u"name"], reason))
208
% (self.properties[u"Name"], reason))
161
210
def selectable(self):
162
211
"""Make this a "selectable" widget.
184
233
u"bold-underline-blink":
185
234
u"bold-underline-blink-standout",
188
237
# Rebuild focus and non-focus widgets using current properties
189
self._text = (u'%(name)s: %(enabled)s'
190
% { u"name": self.properties[u"name"],
193
if self.properties[u"enabled"]
239
# Base part of a client. Name!
240
base = (u'%(name)s: '
241
% {u"name": self.properties[u"Name"]})
242
if not self.properties[u"Enabled"]:
243
message = u"DISABLED"
244
elif self.properties[u"ApprovalPending"]:
245
if self.properties[u"ApprovedByDefault"]:
246
message = u"Connection established to client. (d)eny?"
248
message = u"Seeks approval to send secret. (a)pprove?"
249
elif self.last_checker_failed:
250
timeout = datetime.timedelta(milliseconds
253
last_ok = isoformat_to_datetime(
254
max((self.properties[u"LastCheckedOK"]
255
or self.properties[u"Created"]),
256
self.properties[u"LastEnabled"]))
257
timer = timeout - (datetime.datetime.utcnow() - last_ok)
258
message = (u'A checker has failed! Time until client'
260
% unicode(timer).rsplit(".", 1)[0])
263
self._text = "%s%s" % (base, message)
195
265
if not urwid.supports_unicode():
196
266
self._text = self._text.encode("ascii", "replace")
197
267
textlist = [(u"normal", self._text)]
221
299
def keypress(self, (maxcol,), key):
223
301
This overrides the method from urwid.FlowWidget"""
224
if key == u"e" or key == u"+":
226
elif key == u"d" or key == u"-":
303
self.proxy.Enable(dbus_interface = client_interface)
305
self.proxy.Disable(dbus_interface = client_interface)
307
self.proxy.Approve(dbus.Boolean(True, variant_level=1),
308
dbus_interface = client_interface)
310
self.proxy.Approve(dbus.Boolean(False, variant_level=1),
311
dbus_interface = client_interface)
228
312
elif key == u"r" or key == u"_" or key == u"ctrl k":
229
313
self.server_proxy_object.RemoveClient(self.proxy
231
315
elif key == u"s":
232
self.proxy.StartChecker()
316
self.proxy.StartChecker(dbus_interface = client_interface)
233
317
elif key == u"S":
234
self.proxy.StopChecker()
318
self.proxy.StopChecker(dbus_interface = client_interface)
235
319
elif key == u"C":
236
self.proxy.CheckedOK()
320
self.proxy.CheckedOK(dbus_interface = client_interface)
238
322
# elif key == u"p" or key == "=":
239
323
# self.proxy.pause()
266
350
use them as an excuse to shift focus away from this widget.
268
352
def keypress(self, (maxcol, maxrow), key):
269
ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
353
ret = super(ConstrainedListBox, self).keypress((maxcol,
270
355
if ret in (u"up", u"down"):