def connect(self): self.irc = socket.socket() # Trying to bind vhost if self.ipaddr: try: self.irc.bind((self.ipaddr, self.port)) out.info("bound IP-address: %s " % self.ipaddr) except: out.warn("could not bind IP-address: %s " % self.ipaddr) self.irc.connect((self.server, self.port)) self.irc.send(u'NICK %s\n' % (self.nick)) self.irc.send(u'USER %s %s bla :%s\n' % (self.ident, self.server, self.name)) for channel in self.channels: self.irc.send(u'JOIN #%s\n' % channel) out.info("Connected to %s" % self.server) out.newline() self.connected = True stayawake = self.StayAwake(self) stayawake.start()
def run(self): ''' run() Starting the process manager, invoked by self.start(). ''' while self.running: for proc in self.proccesses: duration = proc.duration() maxDuration = self.cfg.get('max_run_time',proc.module.name) try: maxDuration = int(maxDuration) except: try: maxDuration = int(self.cfg.get('module','max_run_time')) except: maxDuration = 5 if not proc.is_alive(): self.proccesses.remove(proc) elif duration >= maxDuration: self.communication.say(proc.data['channel'], 'Running of %s took too long (limit=%ds).' % (proc.module.name, maxDuration)) out.warn('Running of %s took too long (limit=%ds)' % (proc.module.name, maxDuration)) proc.stop() self.proccesses.remove(proc) time.sleep(0.3) out.info('ThreadedManager threadmanager.ThreadedManager().run(self) terminated.')
def download(self,url,verbose=False): ''' Downloading a python module from the web and loads it using the load method. It will try to download the file and write it to disk. If the module is valid then it will be loaded. ''' response = None html = None try: response = urllib2.urlopen(url) html = response.readlines() except: result = emptyResult() result['errors'] = [['Download module','error: could not read file from URL.']] return None, result try: f = open('module/extra/tmp_module.py','w') for line in html: f.write(line) f.close() except: result = emptyResult() result['errors'] = [['Download module','error: could not write to tempfile']] return None, result #def validateModule(self,name): module, result = self.validateModule('tmp_module',verbose) if inspect.ismodule(module) and result['valid']: try: f = open('module/extra/%s.py' % module.name,'w') for line in html: f.write(line) f.close() except: result = emptyResult() result['errors'] = [['Download module','error: could not write module to module/extra/%s.py.' % name]] return None, result else: try: del sys.modules['module.extra.tmp_module'] except: out.warn('Could not clean up tmp_module import') return None, result out.info('Module "%s" downloaded from %s' % (module.name, url)) os.remove('module/extra/tmp_module.py') return module, result
def __init__(self, owners, admins): self.users = [] try: for owner in owners: user, ident, host = owner.replace(' ','').split(',') self.addUser(user,ident,host,1) except: out.error('No owner(s) of the bot defined.') try: for admin in admins: user, ident, host = admin.replace(' ','').split(',') self.addUser(user,ident,host,2) except: out.warn('No owners found.')
def parsecmd(self, nick, ident, host, channel, cmd): ''' Parse a command sent to the bot and decide what to do Keyword arguments: nick -- nick of sender ident -- ident of sender host -- host of sender channel -- channel the message was sent to cmd -- command that was sent ''' command = cmd.split() data = { 'type': 'cmd', 'nick': nick, 'ident': ident, 'host': host, 'channel': channel, 'cmd': command[0] } try: data['argv'] = command[1:] except: data['argv'] = None coreCmd = self.modules.mcore['corecmd'].parse_cmd(data) if not coreCmd: cmdModules = self.modules.listening('cmd') try: module = self.modules.cmdlist[data['cmd']] out.info("running: %s" % module) if module: try: self.threadmanager.runModule(module, data) #thread.start_new_thread(module.main, (data, ) ) except Exception as error: out.verbose(error) out.error("Module '%s %s' failed to run." % (module.name, module.version)) if self.modules.requires(module, 'presist'): module.presist.save() except: out.warn("could not find module that listens to %s." % data['cmd'])
def parsecmd(self, nick, ident, host, channel, cmd): ''' Parse a command sent to the bot and decide what to do Keyword arguments: nick -- nick of sender ident -- ident of sender host -- host of sender channel -- channel the message was sent to cmd -- command that was sent ''' command = cmd.split() data = {'type' :'cmd', 'nick' :nick, 'ident' :ident, 'host' :host, 'channel' :channel, 'cmd' :command[0]} try: data['argv'] = command[1:] except: data['argv'] = None coreCmd = self.modules.mcore['corecmd'].parse_cmd(data) if not coreCmd: cmdModules = self.modules.listening('cmd') try: module = self.modules.cmdlist[data['cmd']] out.info("running: %s" % module) if module: try: self.threadmanager.runModule(module, data) #thread.start_new_thread(module.main, (data, ) ) except Exception as error: out.verbose(error) out.error("Module '%s %s' failed to run." % (module.name, module.version)) if self.modules.requires(module,'presist'): module.presist.save() except: out.warn("could not find module that listens to %s." % data['cmd'])
def run(self): ''' run() Starting the process manager, invoked by self.start(). ''' while self.running: for proc in self.proccesses: duration = proc.duration() maxDuration = self.cfg.get('max_run_time', proc.module.name) try: maxDuration = int(maxDuration) except: try: maxDuration = int( self.cfg.get('module', 'max_run_time')) except: maxDuration = 5 if not proc.is_alive(): self.proccesses.remove(proc) elif duration >= maxDuration: self.communication.say( proc.data['channel'], 'Running of %s took too long (limit=%ds).' % (proc.module.name, maxDuration)) out.warn('Running of %s took too long (limit=%ds)' % (proc.module.name, maxDuration)) proc.stop() self.proccesses.remove(proc) time.sleep(0.3) out.info( 'ThreadedManager threadmanager.ThreadedManager().run(self) terminated.' )
def injectModule(module): ''' injectModule() -> None Injects a brunbot module that should be tested with mock objects for the required modules. ''' import mock.communication import mock.recentdata import mock.presist import mock.auth modules = {} modules['communication'] = mock.communication.Communication('test') modules['recentdata'] = mock.recentdata.Data() modules['auth'] = mock.auth.Auth() if (inspect.ismodule(module)): try: for coremodule in module.require: if not coremodule == 'presist': vars(module)[coremodule] = modules[coremodule] except: out.warn('moduletest.injectModule() - no module.require')