class Bot: def __init__(self): config = ConfigParser.ConfigParser() print "Parsing config" config.read('config.ini') self.admin = config.get('connect', 'admin') self.login = config.get('connect', 'jid') self.password = config.get('connect', 'password') self.nick = config.get('connect', 'nick') self.conference = config.get('connect', 'conference') self.presence = xmpp.Presence(to = self.conference + '/' + self.nick) self.bot = None self.manager = Manager() print "Loading plugins" self.loadPlugins() self.connect() print "Bot started" self.process() def connect(self): jid = xmpp.JID(self.login) self.client = xmpp.Client(jid.getDomain(),debug=[]) print "Connecting" self.client.connect() self.client.auth(jid.getNode(), self.password) self.client.sendInitPresence() self.client.RegisterHandler('message', self.message) self.client.send(self.presence) print self.presence.getStatusCode() def loadPlugins(self): for fname in os.listdir('plugins/'): if fname.endswith('.py'): plugin_name = fname.rsplit('.', 1)[0] if plugin_name != '__init__': print "Loading " + plugin_name try: self.manager.load('plugins.%s' % plugin_name) except Exception, e: "Could not load %s: %s" % (plugin_name, e) self.manager.update_functions()
def __init__(self): self.__quit = False signal.signal(signal.SIGTERM, sigTermProcess) signal.signal(signal.SIGHUP, sigHupProcess) print "Parsing config" config = json.loads(open('config.json').read()) self.admin = config.get('admin') self.login = config.get('jid') self.password = config.get('password') self.nick = config.get('nick') self.rooms = config.get('conference') self.client = None self.iq = True self.last = datetime(1, 1, 1) self.__repeats = deque((), 10) self.manager = Manager() self.process()
def __init__(self): config = ConfigParser.ConfigParser() print "Parsing config" config.read('config.ini') self.admin = config.get('connect', 'admin') self.login = config.get('connect', 'jid') self.password = config.get('connect', 'password') self.nick = config.get('connect', 'nick') self.conference = config.get('connect', 'conference') self.presence = xmpp.Presence(to = self.conference + '/' + self.nick) self.bot = None self.manager = Manager() print "Loading plugins" self.loadPlugins() self.connect() print "Bot started" self.process()
class Bot: ''' Core commands: !modules Lists all loaded modules. !functions Lists all available commands. !aliases Lists all aliases for commands. !load [modulename] !reload [modulename] Reloads module modulename or reloads pluggins directory. !help [module name] With module name given prints module info. Without arguments prints this help. ''' version = '0.7.0' def __init__(self): self.__quit = False signal.signal(signal.SIGTERM, sigTermProcess) signal.signal(signal.SIGHUP, sigHupProcess) print "Parsing config" config = json.loads(open('config.json').read()) self.admin = config.get('admin') self.login = config.get('jid') self.password = config.get('password') self.nick = config.get('nick') self.rooms = config.get('conference') self.client = None self.iq = True self.last = datetime(1, 1, 1) self.__repeats = deque((), 10) self.manager = Manager() self.process() def connect(self): self.client = None #remove old client jid = xmpp.JID(self.login) client = xmpp.Client(jid.getDomain(),debug=[]) print 'Connecting' if not client.connect(): raise ConnectError('Unable to connect.') if not client.auth(jid.getNode(), self.password): raise AuthException('Unable to authorize.') self.client = client #Make new main client for room, params in self.rooms.items(): self._joinPresence(room, params) client.RegisterHandler('message', self.messageProcess) client.RegisterHandler('presence', self.presenceProcess) client.RegisterHandler('iq', self.iqProcess, typ='result', ns=xmpp.NS_TIME) client.sendInitPresence() print 'Connected' return True def process(self): print "Bot started" while not self.__quit: try: self.checkReconnect() if self.client: if self.client.Process(1) == 0: self.connect() else: if not self.connect(): raise ConnectError('Unknown connection error.') except xmpp.protocol.XMLNotWellFormed: logging.error('CONNECTION: reconnect (detected not valid XML)') self.conn = None except KeyboardInterrupt: self.exit('EXIT: interrupted by keyboard') except SystemExit: self.exit('EXIT: interrupted by SIGTERM') except ReloadData: print 'Reload: SIGHUP' self.manager.load_dir() self.connect() except AuthException: self.exit('EXIT: auth problems, check config.') except ConnectError, e: print str(e) time.sleep(300) except: