37
30
urwid.curses_display.curses.A_UNDERLINE |= (
38
31
urwid.curses_display.curses.A_BLINK)
40
def isoformat_to_datetime(iso):
41
"Parse an ISO 8601 date string to a datetime.datetime()"
44
d, t = iso.split(u"T", 1)
45
year, month, day = d.split(u"-", 2)
46
hour, minute, second = t.split(u":", 2)
47
second, fraction = divmod(float(second), 1)
48
return datetime.datetime(int(year),
53
int(second), # Whole seconds
54
int(fraction*1000000)) # Microseconds
56
33
class MandosClientPropertyCache(object):
57
34
"""This wraps a Mandos Client D-Bus proxy object, caches the
58
35
properties and calls a hook function when any of them are
61
def __init__(self, proxy_object=None, *args, **kwargs):
38
def __init__(self, proxy_object=None, properties=None, *args,
62
40
self.proxy = proxy_object # Mandos Client proxy object
64
self.properties = dict()
65
self.proxy.connect_to_signal(u"PropertyChanged",
42
if properties is None:
43
self.properties = dict()
45
self.properties = properties
46
self.proxy.connect_to_signal("PropertyChanged",
66
47
self.property_changed,
70
self.properties.update(
71
self.proxy.GetAll(client_interface,
72
dbus_interface = dbus.PROPERTIES_IFACE))
51
if properties is None:
52
self.properties.update(self.proxy.GetAll(client_interface,
54
dbus.PROPERTIES_IFACE))
73
55
super(MandosClientPropertyCache, self).__init__(
74
proxy_object=proxy_object, *args, **kwargs)
56
proxy_object=proxy_object,
57
properties=properties, *args, **kwargs)
76
59
def property_changed(self, property=None, value=None):
77
60
"""This is called whenever we get a PropertyChanged signal
88
71
def __init__(self, server_proxy_object=None, update_hook=None,
89
delete_hook=None, logger=None, *args, **kwargs):
72
delete_hook=None, *args, **kwargs):
91
74
self.update_hook = update_hook
93
76
self.delete_hook = delete_hook
94
77
# Mandos Server proxy object
95
78
self.server_proxy_object = server_proxy_object
99
self._update_timer_callback_tag = None
100
self.last_checker_failed = False
102
80
# The widget shown normally
103
self._text_widget = urwid.Text(u"")
81
self._text_widget = urwid.Text("")
104
82
# The widget shown when we have focus
105
self._focus_text_widget = urwid.Text(u"")
83
self._focus_text_widget = urwid.Text("")
106
84
super(MandosClientWidget, self).__init__(
107
85
update_hook=update_hook, delete_hook=delete_hook,
110
88
self.opened = False
111
self.proxy.connect_to_signal(u"CheckerCompleted",
112
self.checker_completed,
115
self.proxy.connect_to_signal(u"CheckerStarted",
116
self.checker_started,
119
self.proxy.connect_to_signal(u"GotSecret",
123
self.proxy.connect_to_signal(u"Rejected",
127
last_checked_ok = isoformat_to_datetime(self.properties
129
if last_checked_ok is None:
130
self.last_checker_failed = True
132
self.last_checker_failed = ((datetime.datetime.utcnow()
136
self.properties["interval"]))
137
if self.last_checker_failed:
138
self._update_timer_callback_tag = (gobject.timeout_add
142
def checker_completed(self, exitstatus, condition, command):
144
if self.last_checker_failed:
145
self.last_checker_failed = False
146
gobject.source_remove(self._update_timer_callback_tag)
147
self._update_timer_callback_tag = None
148
self.logger(u'Checker for client %s (command "%s")'
150
% (self.properties[u"name"], command))
154
if not self.last_checker_failed:
155
self.last_checker_failed = True
156
self._update_timer_callback_tag = (gobject.timeout_add
159
if os.WIFEXITED(condition):
160
self.logger(u'Checker for client %s (command "%s")'
161
u' failed with exit code %s'
162
% (self.properties[u"name"], command,
163
os.WEXITSTATUS(condition)))
164
elif os.WIFSIGNALED(condition):
165
self.logger(u'Checker for client %s (command "%s")'
166
u' was killed by signal %s'
167
% (self.properties[u"name"], command,
168
os.WTERMSIG(condition)))
169
elif os.WCOREDUMP(condition):
170
self.logger(u'Checker for client %s (command "%s")'
172
% (self.properties[u"name"], command))
174
self.logger(u'Checker for client %s completed mysteriously')
177
def checker_started(self, command):
178
self.logger(u'Client %s started checker "%s"'
179
% (self.properties[u"name"], unicode(command)))
181
def got_secret(self):
182
self.logger(u'Client %s received its secret'
183
% self.properties[u"name"])
186
self.logger(u'Client %s was rejected'
187
% self.properties[u"name"])
189
90
def selectable(self):
190
91
"""Make this a "selectable" widget.
216
117
# Rebuild focus and non-focus widgets using current properties
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
218
% { u"name": self.properties[u"name"],
221
if self.properties[u"enabled"]
223
u"timer": (unicode(datetime.timedelta
229
- isoformat_to_datetime
230
(max((self.properties
235
self.properties[u"last_enabled"]))))
236
if (self.last_checker_failed
118
self._text = (u'name="%(name)s", enabled=%(enabled)s'
240
120
if not urwid.supports_unicode():
241
121
self._text = self._text.encode("ascii", "replace")
242
textlist = [(u"normal", self._text)]
122
textlist = [(u"normal", u"BLARGH: "), (u"bold", self._text)]
243
123
self._text_widget.set_text(textlist)
244
124
self._focus_text_widget.set_text([(with_standout[text[0]],
378
250
self.log_wrap = u"any"
381
self.log_message_raw((u"bold",
382
u"Mandos Monitor version " + version))
383
self.log_message_raw((u"bold",
253
self.log_message(u"Message")
254
self.log_message(u"Message0 Message1 Message2 Message3 Message4 Message5 Message6 Message7 Message8 Message9")
255
self.log_message(u"Message10 Message11 Message12 Message13 Message14 Message15 Message16 Message17 Message18 Message19")
256
self.log_message(u"Message20 Message21 Message22 Message23 Message24 Message25 Message26 Message27 Message28 Message29")
386
258
self.busname = domain + '.Mandos'
387
259
self.main_loop = gobject.MainLoop()
398
270
mandos_clients = dbus.Dictionary()
400
272
(self.mandos_serv
401
.connect_to_signal(u"ClientRemoved",
273
.connect_to_signal("ClientRemoved",
402
274
self.find_and_remove_client,
403
275
dbus_interface=server_interface,
404
276
byte_arrays=True))
405
277
(self.mandos_serv
406
.connect_to_signal(u"ClientAdded",
278
.connect_to_signal("ClientAdded",
407
279
self.add_new_client,
408
280
dbus_interface=server_interface,
409
281
byte_arrays=True))
411
.connect_to_signal(u"ClientNotFound",
412
self.client_not_found,
413
dbus_interface=server_interface,
415
282
for path, client in mandos_clients.iteritems():
416
283
client_proxy_object = self.bus.get_object(self.busname,
448
308
self.topwidget = urwid.Pile(self.uilist)
450
def log_message(self, message):
451
timestamp = datetime.datetime.now().isoformat()
452
self.log_message_raw(timestamp + u": " + message)
454
def log_message_raw(self, markup):
310
def log_message(self, markup):
455
311
"""Add a log message to the log buffer."""
456
312
self.log.append(urwid.Text(markup, wrap=self.log_wrap))
457
313
if (self.max_log_length
458
314
and len(self.log) > self.max_log_length):
459
315
del self.log[0:len(self.log)-self.max_log_length-1]
460
self.logbox.set_focus(len(self.logbox.body.contents),
461
coming_from=u"above")
464
317
def toggle_log_display(self):
465
318
"""Toggle visibility of the log buffer."""
605
439
self.topwidget.set_focus(self.logbox)
607
#elif (key == u"end" or key == u"meta >" or key == u"G"
609
# pass # xxx end-of-buffer
610
#elif (key == u"home" or key == u"meta <" or key == u"g"
612
# pass # xxx beginning-of-buffer
613
#elif key == u"ctrl e" or key == u"$":
614
# pass # xxx move-end-of-line
615
#elif key == u"ctrl a" or key == u"^":
616
# pass # xxx move-beginning-of-line
617
#elif key == u"ctrl b" or key == u"meta (" or key == u"h":
619
#elif key == u"ctrl f" or key == u"meta )" or key == u"l":
622
# pass # scroll up log
624
# pass # scroll down log
441
elif (key == u"end" or key == u"meta >" or key == u"G"
443
pass # xxx end-of-buffer
444
elif (key == u"home" or key == u"meta <" or key == u"g"
446
pass # xxx beginning-of-buffer
447
elif key == u"ctrl e" or key == u"$":
448
pass # xxx move-end-of-line
449
elif key == u"ctrl a" or key == u"^":
450
pass # xxx move-beginning-of-line
451
elif key == u"ctrl b" or key == u"meta (" or key == u"h":
453
elif key == u"ctrl f" or key == u"meta )" or key == u"l":
458
pass # scroll down log
625
459
elif self.topwidget.selectable():
626
460
self.topwidget.keypress(self.size, key)