import ESL
import ConfigParser

class ESLconnection(object):
    """docstring for ESLconnection"""
    def __init__(self, h, p, pw):
        self.h = h
        self.p = p
        self.pw = pw
        self.c = True
        self.f = open('trace.txt')
        
    def connected(self):
        """Check for connection"""
        return self.c
        
    def events(self, e, t):
        """docstring for events"""
        print 'Set %s of type %s' % (e, t)
        return
        
    def recvEvent(self):
        """Must return an event"""
        e = ESL.ESLevent('CUSTOM', 'conference::maintenance')
        for line in self.f:
            if line == '\n':
                return e
            var = line.split(':')[0]
            val = line.split(':')[1].lstrip()
            e.addHeader(var, val.rstrip()) # Removes the newline...
            
            
if __name__ == '__main__':
    config = ConfigParser.RawConfigParser()

    try:
        f = open(sys.argv[1])
        config.readfp(f)
    except Exception:
        f = open('conf_tracker.cfg')
        config.readfp(f)
        
    pw = config.get('host', 'password')
    h = config.get('host', 'host')
    p = config.get('host', 'port')

    print 'Connecting to: %s:%s ' % (h, p)
    import ESL
    con = ESL.ESLconnection(h, p, pw)
    con.events('plain', 'CUSTOM conference::maintenance')
    f = open('trace.txt', 'w')
    while con.connected():
        e = con.recvEvent()
        f.write(e.serialize())
        