#!/usr/bin/env python

# functions common to the various kinds of daemon

# TODO: define a log function that sends a pubsub message to the
# logger daemon

import time, binascii
from Crypto import Random

def signal_handler(a, b):
    global clean_shutdown
    clean_shutdown = True
    r.publish("exit", "clean-exit")
    r.lpush("exit", "clean-exit")

def short(session):
    """Return the first 12 bytes of a session ID, or, for a
    challenge ID, the challenge ID with the session ID truncated."""
    tmp = session.partition(":")
    return tmp[0][:12] + "..." + tmp[1] + tmp[2]

def random():
    """Return 64 hex digits representing a new 32-byte random number."""
    return binascii.hexlify(Random.get_random_bytes(32))

def random_raw():
    """Return 32 random bytes."""
    return Random.get_random_bytes(32)

@@ -71,90 +71,8 @@
 on%0A%0A
-# TODO: define a log function that sends a pubsub message to the%0A# logger daemon%0A%0A
 impo
@@ -733,8 +733,167 @@
 tes(32)%0A
+%0Adef log(msg, session = None):%0A    if session:%0A        r.publish(%22logs%22, %22%25s: %25s%22 %25 (short(session), msg))%0A    else:%0A        r.publish(%22logs%22, %22%25s%22 %25 session)%0A

add logging function