def run(self): #this function is called when the bot starts, it does error detection and stuff self.thread_connect.start() self.thread_handle.start() self.thread_detectTimeout.start() #self.thread_consInp.start() while not self.stop: try: if self.breakstuff: self.breakstuff = False raise I_like_trains if self.brokenPipe != False or self.pingTimeout != False: self.echo("Broken Pipe: {}".format(self.brokenPipe), "warn") self.echo("PingTimeout: {}".format(self.pingTimeout), "warn") self.echo("exiting...", "warn") self.exit() self.echo("reconnecting in 10 seconds") time.sleep(4) if self.stop == True: break time.sleep(0.42) except Exception as e: self.echo(traceback.format_exc(), "warn") time.sleep(4) return self.echo("waiting for threads to stop...") for t in threading.enumerate(): try: omitted = False if t.ident == None: omitted = "not started" elif t == threading.main_thread(): omitted = "main thread" elif t == threading.current_thread(): omitted = "current thread" elif t.daemon: omitted = "daemon" if omitted: self.echo("omitted {omitted} thread {thread}".format(thread=t.name, omitted=omitted)) else: self.echo("joining thread {thread}".format(thread=t.name)) t.join(5) if t.is_alive(): self.echo("thread {thread} did not exit within 5 seconds!".format(thread=t.name)) else: self.echo("thread {thread} exited!".format(thread=t.name)) except: self.echo(traceback.format_exc(), "warn") self.echo("all threads stopped!") self.echo("exiting in 2 seconds") time.sleep(2) Echo.end(self.screen) return
import Echo as otherEcho name = sys.argv[1] if len(sys.argv) >=3: joinChansOnStart = str(sys.argv[2]).lower() == "true" else: joinChansOnStart = True restart = True while restart: try: exec(compile(open("IRC.py").read(), "IRC.py", "exec"), globals(), locals()) server = IRCServer( name, joinChansOnStart) server.start() while server.is_alive(): time.sleep(1) restart = server.restart joinChansOnStart = server.joinChansOnStart del server except Exception as e: otherEcho.end() print(traceback.format_exc()) print() if restart:# and not self.end and not self.allowRestart or self.restartOnExc: print("restart in 2 seconds") time.sleep(2) print("restarting now...") else: restart = False print("exiting...") break
def __init__(self, name, dojoin = True, *, doMsgHandle = True, buffer = 8192, encoding = 'UTF-8'): super(IRCServer, self).__init__() self.screen = Echo.start() self.name = name self.path = self.name + "/" self.win_log = Echo.ScrollText(scrollback = 200, posy = 0, posx = 0, height = curses.LINES - 1, width = curses.COLS - 1, logFile = self.path+self.name+"-logs.txt") self.win_log.addPref("warn", "\r{warn}", curses.color_pair(11)) #these define custom prefixes for the console output self.win_log.addPref("ping", "\r(ping)", curses.color_pair(11)) self.win_log.addPref("recv", "\r[recv]", curses.color_pair(15)) self.win_log.addPref("send", "\r[send]", curses.color_pair(16)) self.win_log.addPref("pipo", "\r[pipo]", curses.color_pair(17)) self.echo = self.win_log.echo self.echo("-----SETTING EVERYTHING UP-----") signal.signal(signal.SIGINT, self.handle_keybInt) signal.signal(signal.SIGWINCH, self.handle_resize) self.connected = False #is True when connected to the server, False otherwise self.stop = False #is True when the bot is going down, False when everythings fine self.restart = True #is True when the bot is set to restart, False otherwise self.pingTimeout = False #is False when no Ping Timeout occured, otherwise it holds time in seconds as float self.brokenPipe = False #is False when no Broken Pipe Error occured, True otherwise self.connectionReset = False #is False when no Connection Reset Error occured, True otherwise self.doPiong = True #True when responses to Ping requests from the server self.echoPiong = False #if True PING / PONG messages are logged, False otherwise self.maxInactivity = 20 #maximum amount of inactivity until PING request self.timeout = 10 #maximum amount of time until a PING Timeout is indicated self.doDetectTimeout = True #if True the bot restarts or shuts down when the bot detects a Ping Timeout self.blockMOTD = True #if True MOTD is not logged self.echoCmd = False #for debugging, if True it logs the splitted messages received from the server self.breakstuff = False #for debugging, raises somewhere a error self.serverName = name #the name (folder) of the server/bot, each must be unique self.joinChansOnStart = dojoin #if True the bot joins channels on start up self.doMsgHandle = doMsgHandle #if True extensions are executed self.buffer = buffer #the buffer size self.encoding = encoding #prefered encoding\ self.nick = None #the bot's nick self.oldNick = None #the bot's previous nick #=====config file, holds general data about the bot and the connection self.configFile = File("", self.name.lower() + ".json", echo = self.echo) self.config = self.configFile.data #=====holds which user (<nick>!<identity>@<hostmask>) has what roles self.permuserFile = File(self.path, "permuser.json", echo = self.echo) self.permuser = self.permuserFile.data #===== holds which role has access to which commands self.rolesFile = File(self.path, "roles.json", echo = self.echo) self.roles = self.rolesFile.data self.identity = self.config["ident"] #bot's identity self.realname = self.config["realname"] #bot's realname self.network = self.config["network"] #the network the bot will be connected to, should be equal to the where the bot connects to. this is usefull when the server is running on the same host (localhost) self.port = int(self.config["port"]) #the port the bot connects to self.ssl = str(self.config["ssl"]).lower() == "true" #if True ssl will be used self.greetMsg = self.config["greetMsg"] #the default greet message self.quitMsg = self.config["quitMsg"] #the default quit message self.__pswFile = File("", "pw.json", echo = self.echo).data #the password file where all passwords are stored, i seperated them for easier code sharing :) self.lines = [] #all messages will be stored in this list split in lines self.search = {} #phrases the bot listens to. if found the complete line will be put as value self.chans = {} #information returned to a names request self.users = {} #information returned to a who request #every message line the client receives will be split on these self.patServerMsg = re.compile(r":(?P<all>((?P<server>[a-z0-9.-]+)\s(?P<reply>\d+|PONG)\s(?P<nick>\w+)(?:[= ]+)?(?P<target>#?\w+)?)(?:[\s: ]*)(?P<suffix>.*))(?P<hostmask>)(?P<hgl>)(?P<cmd>)(?P<args>)", re.I) #self.patUserMsg = re.compile(r":((?P<nick>\w+)!~?(?P<hostmask>[a-z0-9.@-]+)\s(?P<type>\w+)[\s:]*(?P<target>#?\w+))(?:\s:)?(?P<omnom>\([#O]\)<\w+>)\s?(?P<suffix>(?:(?P<hgl>\w*):)*\s?(?P<cmd>\S*)\s?(?P<args>.*))(?P<reply>)(?P<server>)", re.I) self.patMsgWHO = re.compile(r"~?(?P<ident>\w+)\s(?P<ip>[a-z0-9\.\-]+)\s(?P<server>[a-z0-9.-]+)\s(?P<nick>\w+)\s(?P<away>H|G)(?P<registered>r?)(?P<bot>B?)(?P<prefix>\*?[%~+@&]?)\s(?P<steps>:[0-9])\s(?P<realname>.*)", re.I) self.patNoneMsg = re.compile(r"((?P<nick>)(?P<hostmask>)(?P<type>)(?P<target>))(?P<suffix>(?:(?:(?P<hgl>))(?P<cmd>)(?P<args>))?)(?P<reply>)(?P<server>).*?", re.I) self.patUMPre = re.compile(r":((?P<nick>\w+)!~?(?P<hostmask>[a-z0-9.@-]+)\s(?P<type>\w+)[\s:]+(?P<target>#?[\w-]+))(?:\s:)(?P<suffix>.*)", re.I) #self.patUMnorm = re.compile(r"(?:(?P<hgl>\w+):)?\s?(?P<cmd>:\S*)\s?(?P<args>.*)(?P<reply>)(?P<server>)", re.I) self.patUMnorm = re.compile(r"(?:(?P<hgl>\w+):\s)?(?P<cmd>[!:~@#$%^&*()_+?|]?\w*)\s?(?P<args>.*)(?P<reply>)(?P<server>)", re.I) self.patUMomnom = re.compile(r"(?:\((?P<omnom>[O#]*)\)<(?P<omnomnick>\w+)>)\s?(?P<suffix>(?:(?P<hgl>\w*):)?\s?(?P<cmd>\S*)\s?(?P<args>.*))(?P<reply>)(?P<server>)", re.I) self.patUMvoid = re.compile(r"(?P<hgl>)(?P<cmd>)(?P<args>)(?P<reply>)(?P<server>).*", re.I) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #the socket the bot uses to connect #self.socket.settimeout(10) if self.ssl: self.socket = ssl.wrap_socket(self.socket) #make the socket ssl #the handler for the extensions self.extHandler = ExtensionHandler(mainPath = self.path, fileName = "extensions.json", echo = self.echo) self.thread_connect = threading.Thread(None, self.connect, "connect") #thread for the connect function self.thread_recv = threading.Thread(None, self.receive, "receive") #thread for the receive function self.thread_handle = threading.Thread(None, self.handle, "handle") #thread for the message handling function self.thread_detectTimeout = threading.Thread(None, self.detectTimeout, "dTimeout") #thread for the Ping Timeout detection
import Command import Echo if __name__ == "__main__": args = Command.paramDecode() send_mail = args.email From = args.From password = args.password print("send_mail:%s\nFrom:%s\n" % (send_mail, From)) Echo.echo(send_mail, password, From)