def __init__(self, config): # Extract bot details self.name = config.get('client', 'name'); self.gcpi = config.get('client', 'owner_gpci'); self.psk = config.get('client', 'psk'); # Create a new connection and connect to it self.connection = Connection(); self.connection.connect(config); self.echobot_parser = EchobotParser(self); # Regex compiling self.valid_user_regex = re.compile(r':(\w+)!\w+@\S+'); self.strip_regex = re.compile(r'(\x03\d+(?:,\d+)?)|\x0f'); # Terminated? self.terminated = False; self.logged_in = False;
class Bot: def __init__(self, config): # Extract bot details self.name = config.get('client', 'name'); self.gcpi = config.get('client', 'owner_gpci'); self.psk = config.get('client', 'psk'); # Create a new connection and connect to it self.connection = Connection(); self.connection.connect(config); self.echobot_parser = EchobotParser(self); # Regex compiling self.valid_user_regex = re.compile(r':(\w+)!\w+@\S+'); self.strip_regex = re.compile(r'(\x03\d+(?:,\d+)?)|\x0f'); # Terminated? self.terminated = False; self.logged_in = False; def add_channels(self, channels): self.channels = channels; def add_echobots(self, echobots): self.echobots = echobots; def poll_loop(self): while not self.terminated: try: dataString = self.connection.receive().decode(); except: #self.connection.send("PING :" + self.connection.server); #print("nothing received"); continue; chunks = dataString.split(' '); #print(dataString); if "Nick" in dataString and not self.logged_in: self.logged_in = True; self.connection.send("MODE " + self.name + " +iwx"); self.connection.send("PRIVMSG nickserv :identify %s %s" % (self.name, self.psk)); self.connection.send("PRIVMSG hostserv :on"); if chunks[0] == 'ERROR': break; elif chunks[0] == 'PING': self.on_ping(chunks); elif self.is_user_message(chunks[0]): self.on_user_message(chunks); if len(chunks) > 2: if chunks[2] == 'Auth': self.process_auth(chunks); def is_user_message(self, author): return re.search(self.valid_user_regex, author); def strip_colors(self, message): return self.strip_regex.sub('', message); def on_user_message(self, chunks): if chunks[1] != 'PRIVMSG': return; author = re.search(self.valid_user_regex, ''.join(chunks)).groups(1)[0]; channel = chunks[2]; message = " ".join(chunks[3:])[1:]; if author in self.echobots: self.echobot_parser.parse(author, channel, self.strip_colors(message)); def process_auth(self, chunks): if chunks[1] == 'NOTICE' and chunks[6] == 'hostname': self.on_connect(); def close(self): self.connection.send("QUIT :Adios!"); self.connection.close(); self.terminated = True; def on_ping(self, chunks): print(" ".join(chunks)); sendValue = " ".join(chunks[1:]); self.connection.send("PONG " + sendValue); def on_connect(self): self.connection.send("PASS *"); message = "USER " + self.name + " 8 * : choo choo!"; self.connection.send(message); message = "NICK " + self.name; self.connection.send(message); sleep(2); for channel in self.channels: self.connection.send("JOIN " + channel); sleep(1);