#! /usr/bin/env python

""" Agent 
"""

import string
import sys
import re

class Agent:
    version = "$Date: 2007/05/08 20:21:00 $"
    cmds = {}

    def __init__(self):
	self.registration()

    def registration(self):
	for method in dir(self):
	    if method.startswith("cmd_") and callable(getattr(self, method)):
		regex = eval('self.'+method)({}, {}, [])
		self.cmds[method[4:]] = { 
		    'method': method,
		    'regex': re.compile('^'+regex+'$', re.I),
		}

    def command(self, list, line, m):
	line = line.strip()
	for name in list:
	    cmd = self.cmds[name]
	    regex = cmd['regex']
	    if regex.match(line):
		method = cmd['method']
		args = regex.split(line)
		eval('self.'+method)(m, cmd, args)
		return method

    def on_message(self, m):
	self.command(cmds, m.line, m)
