def load_routes(): _routes.clear() while _globals: _globals.pop() globals = get_setting("GLOBAL_CHANNELS") table = get_setting("ROUTES") for addr in globals: if addr.protocol in Globals.protocols: _globals.append(addr) for source, destinations in table.iteritems(): if source.protocol not in Globals.protocols: continue _routes[source] = _routes.get(source, []) for destination in destinations: if destination.protocol in Globals.protocols: _routes.get(source).append(destination) for func in _duplexes: func()
def have_required_settings(*args): """Checks gadget_settings.py for required settings.""" try: for key in args: get_setting(key, Exception) return True except: return False
def build_protocol(): if have_required_settings("MANHOLE_BIND_ADDRESS", "MANHOLE_PASSWORD"): host, port = parse_hostname(get_setting("MANHOLE_BIND_ADDRESS")) manhole = manhole_factory() reactor.listenTCP(port, manhole, interface=host) return manhole
def build_protocol(): if have_required_settings("ECHOER_BIND_ADDRESS"): host, port = parse_hostname(get_setting("ECHOER_BIND_ADDRESS")) echoer = Echoer() reactor.listenUDP(port, echoer, interface=host) return echoer
def manhole_factory(): realm = manhole_ssh.TerminalRealm() realm.chainedProtocolFactory.protocolFactory = lambda x: manhole.Manhole({"__builtins__": __builtins__, "Globals": __import__("gadget.globals").Globals}) portal = Portal.Portal(realm) portal.registerChecker(checkers.InMemoryUsernamePasswordDatabaseDontUse(root=get_setting("MANHOLE_PASSWORD"))) return manhole_ssh.ConchFactory(portal)
def scan(context): name = context.get("name") body = context.get("body") if body in get_setting("SUS_MARKERS"): tmp = get_response_context(context) tmp["body"] = translate_sus(name) send_message(tmp)
def handle_topic(self, cmd, args, context): """!topic [new topic]\nThe [new topic] Tavern""" for dest in get_destinations(): if dest.receiveTopic: command = dest.topicCommand or "/topic %s" newTopic = get_setting("TOPIC_FORMAT", "%s") % (" ".join(args),) msg = command % (newTopic,) send_to_destination(dest, msg)
def translate_sus(name): for k,v in get_setting("SUS_TRANSLATIONS").iteritems(): if "," in k: k = k.split(",") if any([test in name for test in k]): return random.choice(v) else: if k in name: return random.choice(v) return "sus %s" % (name,)
def filter_unicode(str): """Removes blacklisted unicode characters, and encodes as UTF-8.""" for char in get_setting("UNICODE_BLACKLIST"): if type(str) is unicode: str = str.replace(char, "") else: str = str.replace(char.encode("utf-8"), "") if type(str) is unicode: str = str.encode("utf-8") return str
def load_plugins(): """Load all plugins.""" modules = chain() for iterable in ([get_modules_in_package("gadget.default_plugins")] + [get_modules_in_directory(dir) for dir in get_setting("PLUGIN_PATHS")]): modules = chain(modules, iterable) for module in modules: if hasattr(module, "initialize"): print "Loading plugin", module.__name__ try: module.initialize() Globals.plugins.update({module.__name__: module}) except UnsupportedPlugin as e: print "Plugin %s is unsupported: %s" % (module.__name__, e.message) else: print "Warning: plugin %s does not have an initialize function" % (module.__name__,)
def privmsg(self, user, channel, message, action=False): #no privmsgs pls if channel == self.factory.channel: name = user.split("!")[0] if action: #Globals.commands.send_message(u"[Twitch] *\x02%s\x02\u202d %s*" % (name, message), self) return else: pass #Globals.commands.send_message(u"[Twitch] \x02%s\x02\u202d: %s" % (name, message), self) if message.startswith(get_setting("COMMAND_PREFIX")): cmd, args = Globals.commands.parse_args(message) environ = Globals.commands.get_environment(self.factory, channel) environ["NAME"] = name Globals.commands(cmd, args, environ) else: Globals.commands.general(self.factory, user, message)
def load_protocols(): """Load all protocols.""" modules = chain() for iterable in ([get_modules_in_package("gadget.default_protocols")] + [get_modules_in_directory(dir) for dir in get_setting("PROTOCOL_PATHS")]): modules = chain(modules, iterable) for module in modules: if hasattr(module, "build_protocol"): print "Loading protocol", module.__name__ try: protocol = module.build_protocol() if protocol: Globals.protocols.update({module.__name__: protocol}) except UnsupportedProtocol as e: print "Protocol %s is unsupported: %s" % (module.__name__, e.message) else: print "Warning: protocol %s does not have a build_protocol function" % (module.__name__,)
def scan_pls(context): if re.match(get_setting("PLS_REGEX") % (get_setting("NICKNAME"),), context["body"], re.I): reply(context, random.choice(get_setting("PLS_MESSAGES")))
def get_auth_failure_msg(): return random.choice(get_setting("AUTH_FAILURE_MESSAGES"))
def signedOn(self): for channel in get_setting("TWITCH_CHANNELS"): self.join(channel)
def build_protocol(): if have_required_settings("NICKNAME", "IRC_CONNECTIONS"): irc = IRC(get_setting("NICKNAME"), get_setting("IRC_CONNECTIONS")) return irc
def make_skype(self): self.skype = skype4py.Skype(Transport='x11') self.skype.Timeout = 5000 self.skype.FriendlyName = get_setting("NICKNAME") self.skype.OnMessageStatus = (lambda msg, status: reactor.callFromThread(self.message_handler, msg, status)) self.skype.OnAttachmentStatus = (lambda status: reactor.callFromThread(self.attachment_status_handler, status))
def is_authed(self, context): return any([context.get("skypeHandle") in x[0] for x in get_setting("ADMINISTRATORS")])