import shelve
import array

database = shelve.open('./plugins/log/database', writeback=True)

def umsg(event, server):
    global database

    nick = event['nicka']
    msg = event['msg']

    if not database.has_key(nick):
        database[nick] = array.array('u')

    database[nick].extend(('<%s>%s\n' % (nick, msg)).decode('utf-8'))
    database.sync()

def chmsg(event, server):
    global database

    nick = event['nicka']
    channel = event['channel']
    msg = event['msg']
   
    if not database.has_key(channel):
        database[channel] = array.array('u')

    database[channel].extend(('<%s>%s\n' % (nick, msg)).decode('utf-8'))
    database.sync()

def uerror(event, server):
    global database
    database.close()

def uquit(event, server):
    global database

    nicka = event['nicka']

    if nicka == server.nick:
        database.close()

   
