import bson
from dateutil import tz
from nltk.chat.eliza import eliza_chatbot as chatbot
import pymongo

from xmlrpc.server import BaseHandler
from xmlrpc.util import signature


class IRC(BaseHandler):

    MESSAGE = 'msg'
    USERJOIN = 'user_join'
    USERLEFT = 'user_left'
    USERQUIT = 'user_quit'

    @staticmethod
    def nick(user):
        return user.split('!', 1)[0]

    def __init__(self, chatbot):
        self._mongo_conn = pymongo.MongoClient()
        self._chatbot = chatbot

    @property
    def db(self):
        return self._mongo_conn.robinette_db

    def log(self, event, data):
        logmethod = '_log_%s' % event
        if hasattr(self, logmethod):
            getattr(self, logmethod)(event, data)

    def _log_msg(self, event, data):
        self.db.messages.insert({
            '_id': bson.ObjectId(),
            'user': data['user'],
            'channel': data['channel'],
            'msg': data['msg']
        }, w=1)

    def _log_user_join(self, event, data):
        self.db.events.insert({
            '_id': bson.ObjectId(),
            'user': data['user'],
            'channel': data['channel'],
            'event': event
        }, w=1)

    def _log_user_left(self, event, data):
        self.db.events.insert({
            '_id': bson.ObjectId(),
            'user': data['user'],
            'channel': data['channel'],
            'event': event
        }, w=1)

    def _log_user_quit(self, event, data):
        self.db.events.insert({
            '_id': bson.ObjectId(),
            'user': data['user'],
            'quit_msg': data['quit_msg'],
            'event': event
        }, w=1)

    @signature(args=['string'], returns='string')
    def respond(self, msg):
        """
        Respond to the message.

        """
        return self._chatbot.respond(msg)

    @signature(args=['string'], returns='string')
    def seen(self, msg, nick):
        """
        Return the last time a user was seen.

        """
        messages = self.db.messages.find(
            {'user': {'$regex': '^%s' % nick, '$options': 'i'}}
        )
        # Get latest.
        messages = list(messages.sort([('_id', -1)]).limit(1))

        if messages:
            msg = messages[0]
            timestamp = msg['_id'].generation_time.astimezone(tz.tzlocal())
            return '%s was last seen on %s, saying: %s' % (
                nick, timestamp.strftime('%a %b %d %X'), msg['msg']
            )
        else:
            return 'I have not seen %s' % nick


irc = IRC(chatbot)

@@ -755,32 +755,62 @@
 , event, data):%0A
+        _id = bson.ObjectId()%0A
         self.db.
@@ -842,39 +842,27 @@
      '_id': 
-bson.ObjectId()
+_id
 ,%0A          
@@ -951,24 +951,95 @@
  data%5B'msg'%5D
+,%0A            'timestamp': _id.generation_time.astimezone(tz.tzlocal())
 %0A        %7D, 
@@ -2349,54 +2349,19 @@
 sg%5B'
-_id'%5D.generation_time.astimezone(tz.tzlocal())
+timestamp'%5D
 %0A   

Add timestamp to messages