class Tuersteherin(object): # Constructor # def __init__(self): # load config self.config = ConfigParser.ConfigParser() if not self.config.read("config.ini"): print "Error: your config.ini could not be read" exit(1) # load plugins importdir.do("plugins", globals()) self.plugins = [ module(config=self.config) for module in Plugin.__subclasses__() ] # load required config self.server = self.config.get('IRC', 'server') self.port = int(self.config.get('IRC', 'port')) self.nick = self.config.get('IRC', 'nick') self.ircchan = self.config.get('IRC', 'ircchan').split(",") self.debugchan = self.config.get('IRC', 'debugchan') # optional config try: self.ignore = self.config.get('IRC', 'ignore').split(',') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): self.ignore = [] try: self.joindelay = int(self.config.get('IRC', 'joindelay')) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): self.joindelay = 0 self.irc = IRCBot(self.server, self.port, self.nick) # Helper Methods # def debug(self, msg): logging.debug(msg) def send_message(self, recipient, msg, srcuser=''): if srcuser not in self.ignore: self.irc.msg(recipient, "\x0F" + msg) def send_command(self, recipient, cmd): self.irc.msg(recipient, cmd) # IRC Handlers def on_msg(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_msg(self, user_nick, host, channel, message) def on_privmsg(self, irc, user_nick, host, message): for plugin in self.plugins: plugin.on_privmsg(self, user_nick, host, message) def on_join(self, irc, user_nick, host, channel): for plugin in self.plugins: plugin.on_join(self, user_nick, host, channel) def on_notice(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_notice(self, user_nick, host, channel, message) # Operations def run(self): # Assign event handlers self.irc.on_msg(self.on_msg) self.irc.on_privmsg(self.on_privmsg) self.irc.on_join(self.on_join) self.irc.on_notice(self.on_notice) # Start Bot self.irc.start() time.sleep(self.joindelay) for channel in self.ircchan: self.irc.join(channel) self.irc.join(self.debugchan) # Run Eventloop try: while self.irc.running: time.sleep(1) except KeyboardInterrupt: print("Received exit command") finally: self.irc.stop()
raw_input = input if len(sys.argv) < 4: print("Usage: %s <hostname> <port> <nickname>" % sys.argv[0]) sys.exit(1) logging.basicConfig(level=logging.DEBUG) irc = IRCBot(sys.argv[1], int(sys.argv[2]), sys.argv[3]) @irc.on_join def on_join(self, nick, host, channel): self.msg(channel, 'Hello') @irc.on_msg def on_msg(self, nick, host, channel, message): if message.lower().startswith('!help'): self.msg(nick, 'some help, idk') irc.start() irc.join("#luna") try: while irc.running: irc.send_raw(raw_input("")) except KeyboardInterrupt: print("Received exit command") finally: irc.stop()
class Rezeptionistin(object): # Constructor # def __init__(self): # load config self.config = ConfigParser.ConfigParser() self.translations = ConfigParser.ConfigParser() if not self.config.read("config.ini"): print "Error: your config.ini could not be read" exit(1) if not self.translations.read("language.ini"): print "Error: your language.ini could not be read" exit(1) # load plugins importdir.do("plugins", globals()) self.plugins = [module(config=self.config) for module in Plugin.__subclasses__()] # load required config self.server=self.config.get('IRC','server') self.port=int(self.config.get('IRC', 'port')) self.nick=self.config.get('IRC', 'nick') self.ircchan=self.config.get('IRC', 'ircchan').split(",") self.debugchan=self.config.get('IRC', 'debugchan') self.useragent=self.config.get('HTTP', 'useragent') self.language=self.config.get('Language','language') self.spacestatus=self.config.get('SpaceStatus', 'url') try: self.ignore=self.config.get('IRC','ignore').split(',') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): self.ignore = [] # load translation keys # load optional config try: self.nickservpassword=self.config.get('IRC', 'nickservpassword') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): pass self.identified = False self.httpregex = re.compile(r'https?://') self.irc = IRCBot(self.server, self.port, self.nick) # Helper Methods # def translate(self, language_key): return self.translations.get(self.language, language_key) def setlanguage(self, language): if not self.translations.has_section(language): return False self.language = language return True def getlanguage(self): return self.language def nickserv_identify(): if not self.identified: self.identified = True if hasattr(self, "nickservpassword") and (self.ircchan == "#" + channel): self.send_command("NickServ", "identify " + self.nickservpassword) def netcat(self, hostname, port, content): try: s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.connect((hostname, port)) s.shutdown(socket.SHUT_WR) while 1: data = s.recv(1024) if data == "": break f = data except: f = "" finally: s.close() return f def debug(self, msg): logging.debug(msg) def geturlsfrommsg(self, message): url = re.findall("(?P<url>https?://[^\s]+)", message) return url def getpage(self, url): req = urllib2.Request(url, headers={ 'User-Agent': self.useragent }) soup = BeautifulSoup(urllib2.urlopen(req),"html.parser") return soup def get_spacestatus_data(self): data = None try: data = self.getpage(self.spacestatus) data = self.sanitize(data) data = json.loads(data) self.debug(data) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): print "SpaceStatus was not properly configured in your config.ini" return data def geturltitle(self, url): try: page = self.getpage(url) title = self.sanitize(page.title.string.lstrip()) except: title = "" return title def split_by_nth(self, text, n, seperator=' '): groups = text.split(seperator) res_l = list(seperator.join(groups[:n])) res_r = list(seperator.join(groups[n:])) res_l.extend(res_r) return res_l def send_message(self, recipient, msg, srcuser=''): if srcuser not in self.ignore: self.irc.msg(recipient, "\x0F" + msg) def send_command(self, recipient, cmd): self.irc.msg(recipient, cmd) def sanitize(self, s): return str(s).translate(string.maketrans("\n\r",' ')) def get_plugin(self, name): for plugin in self.plugins: if type(plugin).__name__ == name: return plugin def check_user_authentication(self, user_nick, success_callback, failure_callback=None): authentication = self.get_plugin("Authentication") if authentication: authentication.check_user_authentication(self, user_nick, success_callback, failure_callback) # IRC Handlers def on_msg(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_msg(self, user_nick, host, channel, message) def on_privmsg(self, irc, user_nick, host, message): for plugin in self.plugins: plugin.on_privmsg(self, user_nick, host, message) def on_join(self, irc, user_nick, host, channel): for plugin in self.plugins: plugin.on_join(self, user_nick, host, channel) def on_notice(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_notice(self, user_nick, host, channel, message[1:]) # Operations def run(self): # Assign event handlers self.irc.on_msg(self.on_msg) self.irc.on_privmsg(self.on_privmsg) self.irc.on_join(self.on_join) self.irc.on_notice(self.on_notice) # Start Bot self.irc.start() for channel in self.ircchan: self.irc.join(channel) self.irc.join(self.debugchan) # Run Eventloop try: while self.irc.running: time.sleep(1) except KeyboardInterrupt: print("Received exit command") finally: self.irc.stop()
class Rezeptionistin(object): # Constructor # def __init__(self): # load config config = ConfigParser.ConfigParser() if not config.read("config.ini"): print "Error: your config.ini could not be read" exit(1) # load plugins importdir.do("plugins", globals()) self.plugins = [module() for module in Plugin.__subclasses__()] self.server=config.get('IRC','server') self.port=int(config.get('IRC', 'port')) self.nick=config.get('IRC', 'nick') self.ircchan=config.get('IRC', 'ircchan') self.debugchan=config.get('IRC', 'debugchan') self.useragent=config.get('HTTP', 'useragent') self.site = wiki.Wiki(config.get('MediaWiki', 'wikiapiurl')) self.site.login(config.get('MediaWiki', 'user'), config.get('MediaWiki', 'password')) self.httpregex=re.compile(r'https?://') self.irc = IRCBot(self.server, self.port, self.nick) # Helper Methods # def netcat(self, hostname, port, content): s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.connect((hostname, port)) s.shutdown(socket.SHUT_WR) while 1: data = s.recv(1024) if data == "": break f = data s.close() return f def wikiupdate(self, title, url): cat = category.Category(self.site, "Linklist") for article in cat.getAllMembersGen(namespaces=[0]): print article.edit(appendtext="\n* {title} - {url} \n".format(title=title, url=url)) def geturlfrommsg(self, message): url = re.search("(?P<url>https?://[^\s]+)", message).group("url") return url def getpage(self, url): req = urllib2.Request(url, headers={ 'User-Agent': self.useragent }) soup = BeautifulSoup(urllib2.urlopen(req)) return soup def geturltitle(self, url): try: page = self.getpage(url) title = self.sanitize(page.title.string.lstrip()) except: title = "" return title def split_by_nth(self, text, n, seperator=' '): groups = text.split(seperator) res_l = list(seperator.join(groups[:n])) res_r = list(seperator.join(groups[n:])) res_l.extend(res_r) return res_l def send_message(self, recipient, msg): self.irc.msg(recipient, "\x0F" + msg) def send_command(self, recipient, cmd): self.irc.msg(recipient, cmd) def sanitize(self, s): return str(s).translate(string.maketrans("\n\r",' ')) # IRC Handlers def on_msg(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_msg(self, user_nick, host, channel, message) def on_privmsg(self, irc, user_nick, host, message): for plugin in self.plugins: plugin.on_privmsg(self, user_nick, host, message) def on_join(self, irc, user_nick, host, channel): for plugin in self.plugins: plugin.on_join(self, user_nick, host, channel) # Operations def run(self): # Assign event handlers self.irc.on_msg(self.on_msg) self.irc.on_privmsg(self.on_privmsg) self.irc.on_join(self.on_join) # Start Bot self.irc.start() self.irc.join(self.ircchan) self.irc.join(self.debugchan) # Run Eventloop try: while self.irc.running: self.irc.send_raw(raw_input("")) except KeyboardInterrupt: print("Received exit command") finally: self.irc.stop()
class Rezeptionistin(object): # Constructor # def __init__(self): # load config config = ConfigParser.ConfigParser() if not config.read("config.ini"): print "Error: your config.ini could not be read" exit(1) # load plugins importdir.do("plugins", globals()) self.plugins = [module() for module in Plugin.__subclasses__()] self.server = config.get('IRC', 'server') self.port = int(config.get('IRC', 'port')) self.nick = config.get('IRC', 'nick') self.ircchan = config.get('IRC', 'ircchan') self.debugchan = config.get('IRC', 'debugchan') self.useragent = config.get('HTTP', 'useragent') self.site = wiki.Wiki(config.get('MediaWiki', 'wikiapiurl')) self.site.login(config.get('MediaWiki', 'user'), config.get('MediaWiki', 'password')) self.httpregex = re.compile(r'https?://') self.irc = IRCBot(self.server, self.port, self.nick) # Helper Methods # def netcat(self, hostname, port, content): s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.connect((hostname, port)) s.shutdown(socket.SHUT_WR) while 1: data = s.recv(1024) if data == "": break f = data s.close() return f def wikiupdate(self, title, url): cat = category.Category(self.site, "Linklist") for article in cat.getAllMembersGen(namespaces=[0]): print article.edit(appendtext="\n* {title} - {url} \n".format( title=title, url=url)) def geturlfrommsg(self, message): url = re.search("(?P<url>https?://[^\s]+)", message).group("url") return url def getpage(self, url): req = urllib2.Request(url, headers={'User-Agent': self.useragent}) soup = BeautifulSoup(urllib2.urlopen(req)) return soup def geturltitle(self, url): try: page = self.getpage(url) title = self.sanitize(page.title.string.lstrip()) except: title = "" return title def split_by_nth(self, text, n, seperator=' '): groups = text.split(seperator) res_l = list(seperator.join(groups[:n])) res_r = list(seperator.join(groups[n:])) res_l.extend(res_r) return res_l def send_message(self, recipient, msg): self.irc.msg(recipient, "\x0F" + msg) def send_command(self, recipient, cmd): self.irc.msg(recipient, cmd) def sanitize(self, s): return str(s).translate(string.maketrans("\n\r", ' ')) # IRC Handlers def on_msg(self, irc, user_nick, host, channel, message): for plugin in self.plugins: plugin.on_msg(self, user_nick, host, channel, message) def on_privmsg(self, irc, user_nick, host, message): for plugin in self.plugins: plugin.on_privmsg(self, user_nick, host, message) def on_join(self, irc, user_nick, host, channel): for plugin in self.plugins: plugin.on_join(self, user_nick, host, channel) # Operations def run(self): # Assign event handlers self.irc.on_msg(self.on_msg) self.irc.on_privmsg(self.on_privmsg) self.irc.on_join(self.on_join) # Start Bot self.irc.start() self.irc.join(self.ircchan) self.irc.join(self.debugchan) # Run Eventloop try: while self.irc.running: self.irc.send_raw(raw_input("")) except KeyboardInterrupt: print("Received exit command") finally: self.irc.stop()
if len(message.split()) >= 2: send_message(self, channel, message.split()[1] + ", " + random.choice(list(open('lists/flattery.txt')))) if message.lower().startswith('!utf8'): send_message(self, channel, "hällö") if httpregex.search(message.lower()) is not None: url = geturlfrommsg(message) title = geturltitle(url) if not title == "": send_message(self, channel, "{title}".format(title=title)) @irc.on_privmsg def on_privmsg(self, nick, host, message): if message.lower().startswith('!gt'): send_message(self, nick, "Ich lebe noch, {nick}".format(nick=nick)) # Start Bot irc.start() if ircchan: irc.join(ircchan) if debugchan: irc.join(debugchan) # Run Eventloop try: while irc.running: irc.send_raw(raw_input("")) except KeyboardInterrupt: print("Received exit command") finally: irc.stop()