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
59
43
class MandosClientPropertyCache(object):
60
44
"""This wraps a Mandos Client D-Bus proxy object, caches the
61
45
properties and calls a hook function when any of them are
134
115
client_interface,
135
116
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()
145
self.properties["interval"]))
146
if self.last_checker_failed:
147
self._update_timer_callback_tag = (gobject.timeout_add
151
118
def checker_completed(self, exitstatus, condition, command):
152
119
if exitstatus == 0:
153
if self.last_checker_failed:
154
self.last_checker_failed = False
155
gobject.source_remove(self._update_timer_callback_tag)
156
self._update_timer_callback_tag = None
157
self.logger(u'Checker for client %s (command "%s")'
159
% (self.properties[u"name"], command))
120
#self.logger(u'Checker for client %s (command "%s")'
122
# % (self.properties[u"name"], command))
163
if not self.last_checker_failed:
164
self.last_checker_failed = True
165
self._update_timer_callback_tag = (gobject.timeout_add
168
124
if os.WIFEXITED(condition):
169
125
self.logger(u'Checker for client %s (command "%s")'
170
126
u' failed with exit code %s'
171
127
% (self.properties[u"name"], command,
172
128
os.WEXITSTATUS(condition)))
173
elif os.WIFSIGNALED(condition):
130
if os.WIFSIGNALED(condition):
174
131
self.logger(u'Checker for client %s (command "%s")'
175
132
u' was killed by signal %s'
176
133
% (self.properties[u"name"], command,
177
134
os.WTERMSIG(condition)))
178
elif os.WCOREDUMP(condition):
136
if os.WCOREDUMP(condition):
179
137
self.logger(u'Checker for client %s (command "%s")'
181
139
% (self.properties[u"name"], command))
183
self.logger(u'Checker for client %s completed mysteriously')
140
self.logger(u'Checker for client %s completed mysteriously')
186
142
def checker_started(self, command):
187
143
#self.logger(u'Client %s started checker "%s"'
234
190
# Rebuild focus and non-focus widgets using current properties
236
192
# Base part of a client. Name!
237
base = (u'%(name)s: '
193
self._text = (u'%(name)s: '
238
194
% {u"name": self.properties[u"name"]})
239
if not self.properties[u"enabled"]:
240
message = u"DISABLED"
241
elif self.last_checker_failed:
242
timeout = datetime.timedelta(milliseconds
243
= self.properties[u"timeout"])
244
last_ok = isoformat_to_datetime(
245
max((self.properties["last_checked_ok"]
246
or self.properties["created"]),
247
self.properties[u"last_enabled"]))
248
timer = timeout - (datetime.datetime.utcnow() - last_ok)
250
message = (u'A checker has failed! Time until client gets diabled: %s'
252
elif self.properties[u"approved_pending"]:
196
if self.properties[u"approved_pending"]:
253
197
if self.properties[u"approved_by_default"]:
254
message = u"Connection established to client. (d)eny?"
198
self._text += u"Connection established to client. (d)eny?"
256
message = u"Seeks approval to send secret. (a)pprove?"
200
self._text += u"Seeks approval to send secret. (a)pprove?"
259
self._text = "%s%s" % (base, message)
202
self._text += (u'%(enabled)s'
205
if self.properties[u"enabled"]
261
207
if not urwid.supports_unicode():
262
208
self._text = self._text.encode("ascii", "replace")
263
209
textlist = [(u"normal", self._text)]
274
220
if self.update_hook is not None:
275
221
self.update_hook()
277
def update_timer(self):
280
return True # Keep calling this
282
223
def delete(self):
283
if self._update_timer_callback_tag is not None:
284
gobject.source_remove(self._update_timer_callback_tag)
285
self._update_timer_callback_tag = None
286
224
if self.delete_hook is not None:
287
225
self.delete_hook(self)