def compile_bf(bfcode): output = "Output: " bf_index = 0 bfarr = [0] * 3000 i = 0 j = 0 # build brace map i.e. return addresses bracestack, bracemap = [], {} while j < len(bfcode): if bfcode[j] == "[": bracestack.append(j) if bfcode[j] == "]": if len(bracestack) == 0: output = "Missing '['" return output start = bracestack.pop() bracemap[start] = j bracemap[j] = start j += 1 if len(bracestack) != 0: output = "Missing ']'" return output signal.alarm(10) while i < len(bfcode): if variables.time_counter - variables.init_counter > 0: irc.send_msg(variables.too_much_timer_err, variables.channel) signal.alarm(0) return "[NORESULT]" if bfcode[i] == '<': if bf_index > 0: bf_index -= 1 elif bf_index == 0: bf_index = 2999 elif bfcode[i] == '>': if bf_index < 3000: bf_index += 1 elif bf_index >= 2999: bf_index = 0 elif bfcode[i] == '+': if bfarr[bf_index] == 255: bfarr[bf_index] = 0 else: bfarr[bf_index] += 1 elif bfcode[i] == '-': if bfarr[bf_index] == 0: bfarr[bf_index] = 255 else: bfarr[bf_index] -= 1 elif bfcode[i] == '.': if bfarr[bf_index] > 0 and bfarr[bf_index] < 255: output = output + chr(bfarr[bf_index]) else: output = output + "[NON EXISTENT CHARACTER: " + str(bfarr[bf_index]) +"]" elif bfcode[i] == '[' and bfarr[bf_index] == 0: i = bracemap[i] elif bfcode[i] == ']' and bfarr[bf_index] != 0: i = bracemap[i] i += 1 signal.alarm(0) output = irc.process_output(output) return output
def compile_df(dfcode): accumulator = 0 i = 0 output = "" signal.alarm(10) while(i < len(dfcode)): if variables.time_counter - variables.init_counter > 0: signal.alarm(0) irc.send_msg(variables.too_much_time_err, variables.channel) return "[NORESULT]" if dfcode[i] == 'x' or dfcode[i] == 'i': accumulator += 1 elif dfcode[i] == 'd': accumulator -= 1 elif dfcode[i] == 's' or dfcode[i] == 'k': accumulator *= accumulator elif dfcode[i] == 'o' or dfcode[i] == 'c': if accumulator == 0 or accumulator == 256: output += "0 " else: output += str(accumulator) + " " i += 1 signal.alarm(0) output = irc.process_output(output) return output
def bin_to_decimal(arg): arg = (arg.replace(".b2d", "")).replace(" ", "") arg = arg.replace("0B", "").replace("0b", "") arg = arg.replace("B", "").replace("b", "") try: irc.send_msg("Output: " + str(int((arg), 2)), variables.channel) except (TypeError, ValueError): irc.send_msg("Error while converting bin to decimal", variables.channel)
def bin_to_hex(arg): arg = (arg.replace(".b2h", "")).replace(" ", "") arg = arg.replace("0B", "").replace("0b", "") arg = arg.replace("B", "").replace("b", "") try: irc.send_msg("Output: " + hex(int(arg, 2)), variables.channel) except (TypeError, ValueError): irc.send_msg("Error while converting bin to hex", variables.channel)
def hex_to_bin(arg): arg = (arg.replace(".h2b", "")).replace(" ", "") arg = ( arg.replace("A", "a").replace("B", "b").replace("C", "c").replace("D", "d").replace("E", "e").replace("F", "f") ) arg = arg.replace("0x", "").replace("0X", "") arg = arg.replace("X", "").replace("x", "") try: irc.send_msg("Output: " + bin(int((arg), 16)), variables.channel) except (TypeError, ValueError): irc.send_msg("Error while converting hex to bin", variables.channel)
def main_loop(): while 1: try: main_func() except: err = sys.exc_info() exception = sys.exc_info()[0] if exception == KeyboardInterrupt: quit_bot() irc.send_msg("Error! INFO: " + str(err), variables.head_user) irc.send_msg("An error has been reported to the head user. Further details would be disclosed to the public in future. [RESTARTING]", variables.channel)
def main_func(): irc_message = variables.ircsock.recv(8192) irc_message = irc_message.strip('\n\r') signal.alarm(0) variables.time_counter = 0 print(irc_message) output = "Output: " mystr = irc.parse_msg(irc_message) if mystr['COMMAND'] == "PRIVMSG": if len(mystr['ARGS']) == 0: variables.channel = "#rxvm" else: variables.channel = mystr['ARGS'][0] if variables.channel == variables.botnick: priv_user = mystr['PREFIX'] priv_user = priv_user.split('!') variables.channel = priv_user[0] myinput = "" if mystr['ARGS'][1].startswith("ath", 0, len("ath")) == True: mycode = mystr['ARGS'][1].replace("ath", "") if mycode.startswith(".reload", 0, 7): mycode = mycode.replace(".reload", "").replace(" ", "") user = mystr['PREFIX'] user = user.split('!') if user[0] != variables.head_user: irc.send_msg("You need to be: " + variables.head_user + " to make me reload a module", variables.channel) else: try: reload(sys.modules[mycode]) irc.send_msg("Reloaded module: " + mycode, variables.channel) except (KeyError): irc.send_msg("Module not found", variables.channel) else: parser.parse_ath_message(mycode, mystr) # check for any links if variables.channel == "#osdev-offtopic": urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', mystr['ARGS'][1]) if len(urls) > 0: i = 0 while i < len(urls): try: irc.send_msg("Title: " + utils.get_page_title(urls[i]), variables.channel) i += 1 except: irc.send_msg("An error occurred while processing the link.", variables.channel) break if mystr['COMMAND'] == "PING": print "PING" irc.ping() return 0
def hex_to_decimal(arg): arg = (arg.replace(".h2d", "")).replace(" ", "") try: arg = ( arg.replace("A", "a") .replace("B", "b") .replace("C", "c") .replace("D", "d") .replace("E", "e") .replace("F", "f") ) arg = arg.replace("0x", "").replace("0X", "") arg = arg.replace("X", "").replace("x", "") irc.send_msg("Output: " + str(int(arg, 16)), variables.channel) except (TypeError, ValueError): irc.send_msg("Error while converting hex to int!", variables.channel)
def get_time(arg): try: arg = arg.replace(".time", "").replace(" ", "") mytimezone = timezone(arg) the_time = datetime.now(mytimezone) irc.send_msg( "Output: Time (In " + arg + ") " + the_time.strftime("%Y/%m/%d - %H:%M:%S") + ": UTC " + pytz.timezone(arg) .localize(datetime(date.today().year, date.today().month, date.today().day)) .strftime("%z"), variables.channel, ) except (pytz.exceptions.UnknownTimeZoneError): irc.send_msg("Unknown Time zone.", variables.channel)
def search_xkcd(comic_string): global comics_titles, total_current_titles, latest_index_num load_comics() # grab all comic titles and their image URLs # first get the latest comic index # now let's find the most matching string try: comic_title = difflib.get_close_matches(comic_string, comics_titles, 1)[0] comic_index = 0 comic_index = comics_titles.index(comic_title) except: irc.send_msg( str(sys.exc_info()), variables.head_user) return "Error. Unable to find comic title" target = open("xkcdtitles.txt", 'a') i = total_current_titles+1 while i < len(comics_titles): target.write("%s\n" % comics_titles[i].encode('ascii', errors='backslashreplace').replace("\n", "").replace("\r", "")) i += 1 return xkcd_url + str(comic_index+1)
def compile_ul(ulcode): output = "Output: " ulstack = ["NULL"] i = 0 signal.alarm(10) while i < len(ulcode): if variables.time_counter - variables.init_counter > 0: irc.send_msg(variables.too_much_timer_err, variables.channel) signal.alarm(0) return "[NORESULT]" if ulcode[i] == '~': if len(ulstack) < 2: output = variables.bestack_2item_err return output a = ulstack.pop() b = ulstack.pop() ulstack.append(a) ulstack.append(b) elif ulcode[i] == ':': if len(ulstack) < 1: output = variables.bestack_1item_err return output a = ulstack.pop() ulstack.append(a) ulstack.append(a) elif ulcode[i] == '!': if len(ulstack) < 1: output = variables.bestack_1item_err return output ulstack.pop() elif ulcode[i] == '*': if len(ulstack) < 2: output = variables.bestack_2item_err return output a = ulstack.pop() b = ulstack.pop() ulstack.append(b+a) elif ulcode[i] == 'a': if len(ulstack) < 1: output = variables.bestack_1item_err return output a = ulstack.pop() ulstack.append("(" + a + ")") elif ulcode[i] == '^': if len(ulstack) < 1: output = variables.bestack_1item_err return output a = ulstack.pop() ulcode = insert_string(ulcode, a, i+1) elif ulcode[i] == 'S': if len(ulstack) < 1: output = variables.bestack_1item_err return output output = output + ulstack.pop() elif ulcode[i] == '(': a = "" level = 0 i += 1 level += 1 while(level > 0): if i > len(ulcode): output = "Error! Unterminated parentheses." return output if ulcode[i] == '(': level += 1 elif ulcode[i] == ')': level -= 1 if level > 0: a += ulcode[i] i += 1 i -= 1 ulstack.append(a) i += 1 signal.alarm(0) output = irc.process_output(output) return output
def decimal_to_hex(arg): arg = (arg.replace(".d2h", "")).replace(" ", "") try: irc.send_msg("Output: " + hex(int(arg)), variables.channel) except (TypeError, ValueError): irc.send_msg("Error while converting decimal to hex!", variables.channel)
def eval_expr(expr): try: a = eval_(ast.parse(expr, mode="eval").body) return a except (RuntimeError, TypeError, NameError, KeyError, SyntaxError, ZeroDivisionError): irc.send_msg(variables.eval_op_error_str, channel)
def test(): irc.send_msg("Acknowledge.", variables.channel)
def compile_be(becode): output = "Output: " bestack = [0] pc_dir = variables.right i = 0 signal.alarm(10) while becode[i] != '@': if variables.time_counter - variables.init_counter > 0: signal.alarm(0) irc.send_msg(variables.too_much_time_err, variables.channel) return "[NORESULT]" if becode[i] == '+' or becode[i] == '-' or becode[i] == '/' or becode[i] == '*' or becode[i] == '%': if len(bestack) < 2: output = variables.bestack_2item_err return output else: a = bestack.pop() b = bestack.pop() if becode[i] == '+': bestack.append(a+b) elif becode[i] == '-': bestack.append(b-a) elif becode[i] == '*': bestack.append(a*b) elif becode[i] == '/': bestack.append(b/a) elif becode[i] == '%': bestack.append(b%a) else: output = variables.bestack_2item_err return output elif becode[i] == '!': if len(bestack) < 1: output = bestack_1item_err return output else: a = bestack.pop() if a == 0: bestack.append(1) else: bestack.append(0) elif becode[i] == '`': if len(bestack) < 2: output = variables.bestack_2item_err return output else: a = bestack.pop() b = bestack.pop() if(b > a): bestack.append(1) else: bestack.append(0) elif becode[i] == '<': pc_dir = variables.left elif becode[i] == '>': pc_dir = variables.right elif becode[i] == '?': pc_dir = randint(variables.left,variables.right) elif becode[i] == '_': if len(bestack) < 1: output = variables.bestack_1item_err return output else: a = bestack.pop() if a == 0: pc_dir = variables.right else: pc_dir = variables.left elif becode[i] == '|': if len(bestack) < 1: output = variables.bestack_1item_err return output else: a = bestack.pop() if a == 0: pc_dir = variables.down else: pc_dir = variables.up elif becode[i] == '"': j = i+1 mark_found = False while j < len(becode): if becode[j] == '"': mark_found = True break else: try: bestack.append(ord(becode[j])) except (TypeError): bestack.append(ord(' ')) j += 1 if mark_found == False: output = variables.be_string_err return output i = j elif becode[i] == ':': if len(bestack) < 1: output = variables.bestack_1item_err return output else: a = bestack.pop() bestack.append(a) bestack.append(a) elif becode[i] == '\\': if len(bestack) < 2: output = variables.bestack_2item_err return output else: a = bestack.pop() b = bestack.pop() bestack.append(a) bestack.append(b) elif becode[i] == '.': if len(bestack) < 1: output = variables.bestack_1item_err return output else: a = bestack.pop() output = output + str(a) elif becode[i] == ',': if len(bestack) < 1: output = variables.bestack_1item_err return output else: a = bestack.pop() if (a) > 0 and (a) < 255: output = output + chr((a)) else: output = output + "[NON-ASCII CHAR: " + str(a) + "]" elif becode[i] == '#': if pc_dir == variables.right: i += 1 elif pc_dir == variables.left: i -= 1 else: output = variables.be_internal_err return output elif becode[i] == '$': if len(becode) < 1: output = "Stack underflow error!" return output else: bestack.pop() elif becode[i] == 'g': if len(bestack) < 2: output = variables.bestack_2item_err return output y_pos = bestack.pop() x_pos = bestack.pop() array_pos = x_pos if array_pos < 0 or array_pos > len(becode): bestack.append(0) else: bestack.append(ord(becode[array_pos])) elif becode[i] == '0' or becode[i] == '1' or becode[i] == '2' or becode[i] == '3' or becode[i] == '4' or becode[i] == '5' or becode[i] == '6' or becode[i] == '7' or becode[i] == '8' or becode[i] == '9': bestack.append(int(becode[i])) if pc_dir == variables.right: i += 1 elif pc_dir == variables.left: i -= 1 else: output = variables.be_internal_err return output if i < 0 or i >= len(becode): output = variables.be_oerflow_err return output signal.alarm(0) output = irc.process_output(output) return output
def parse_ath_message(mycode, mystr): if mycode.startswith(".bf ", 0, 4) == True: mycode = mycode.replace(".bf ", "") irc.send_msg(bfc.compile_bf(mycode), variables.channel) signal.alarm(0) elif mycode.startswith(".be ", 0, 4) == True: mycode = mycode.replace(".be ", "") irc.send_msg(bec.compile_be(mycode), variables.channel) signal.alarm(0) elif mycode.startswith(".ul ", 0, 4) == True: print mycode mycode = mycode.replace(".ul ", "") irc.send_msg(ulc.compile_ul(mycode), variables.channel) signal.alarm(0) elif mycode.startswith(".test", 0, 5) == True: utils.test() elif mycode.startswith(".eval", 0, 5) == True: output = "Result: " + utils.evaluate_expression(mycode) irc.send_msg(output, variables.channel) elif mycode.startswith(".d2h", 0, 4) == True: utils.decimal_to_hex(mycode) elif mycode.startswith(".h2d", 0, 4) == True: utils.hex_to_decimal(mycode) elif mycode.startswith(".d2b", 0, 4) == True: utils.decimal_to_bin(mycode) elif mycode.startswith(".h2b", 0, 4) == True: utils.hex_to_bin(mycode) elif mycode.startswith(".b2h", 0, 4) == True: utils.bin_to_hex(mycode) elif mycode.startswith(".b2d", 0, 4) == True: utils.bin_to_decimal(mycode) elif mycode.startswith(".join", 0, 5) == True: user = mystr['PREFIX'] user = user.split('!') if user[0] != variables.head_user: irc.send_msg("You need to be: " + variables.head_user + " to make me join a channel", variables.channel) else: mychan = mycode.replace(".join", "") mychan = mychan.replace(" ", "") if mychan == "0": irc.send_msg(variables.i_hate_you, variables.channel) else: irc.join_channel(mychan) elif mycode.startswith(".leave", 0, 6): user = mystr['PREFIX'] user = user.split('!') if user[0] != variables.head_user: irc.send_msg("You need to be: " + variables.head_user + " to make me leave a channel", variables.channel) else: mychan = mycode.replace(".leave", "") mychan = mychan.replace(" ", "") if mychan == "0": irc.send_msg(variables.i_hate_you, variables.channel) else: irc.leave_channel(mychan) elif mycode.startswith(".time ", 0, 6) == True: utils.get_time(mycode) elif mycode.startswith(".ccount ", 0, 8) == True: mycode = mycode.replace(".ccount ", "") length = 0 if check_if_unicode(mycode) == True: length = unicode_len(mycode) else: length = len(mycode) irc.send_msg("Length: " + str(length), variables.channel) elif mycode.startswith(".help", 0, 5) == True: irc.send_msg(help.athena_help, variables.channel) elif mycode.startswith(".list", 0, 5): irc.send_msg("List of modules: " + str(list(imports())), variables.channel) elif mycode.startswith(".xkcd ", 0, 6): irc.send_msg(u'\u200b' + xkcd.search_xkcd(mycode.replace(".xkcd ", "")), variables.channel) elif mycode.startswith(".tr ", 0, 4): mycode = mycode.replace(".tr ", "") lang = mycode[:2] mycode = mycode.replace(lang, "") irc.send_msg(utils.translate_lang(lang, mycode), variables.channel) elif mycode.startswith(".bftxt ", 0, 7): mycode = mycode.replace(".bftxt ", "") irc.send_msg("Output: " + bftxt.bf(mycode), variables.channel) elif mycode.startswith(".df ", 0, 4): mycode = mycode.replace(".df ", "") irc.send_msg("Output: " + deadfish.compile_df(mycode), variables.channel) elif mycode.startswith(".source", 0, 7): irc.send_msg(u"\u200bhttps://github.com/Benderx2/athena_bot", variables.channel)