def test_add_cmd(self): # Without name specified, add_cmd adds a command with the same name # as the function utils.add_cmd(dummyf) utils.add_cmd(dummyf, 'TEST') # All command names should be automatically lowercased. self.assertIn('dummyf', world.commands) self.assertIn('test', world.commands) self.assertNotIn('TEST', world.commands)
import socket, ssl from utils import add_cmd name="ssl" cmds=["ssl"] def getinfo(hostname): try: ctx = ssl.create_default_context() s = ctx.wrap_socket(socket.socket(), server_hostname=hostname) s.connect((hostname, 443)) cert = s.getpeercert() subject = dict(x[0] for x in cert['subject']) issued_to = subject['commonName'] issuer = dict(x[0] for x in cert['issuer']) issued_by = issuer['commonName'] return [issued_to, issued_by] except: return "Error" def ssl_info(irc,event, args): info = getinfo(" ".join(args)) print(" ".join(args)) if info == "Error": irc.reply(event, info) return None irc.reply(event, "The certificate is issued to {0} and issued by {1}".format(info[0], info[1])) add_cmd(ssl_info, "ssl")
for channel in clist: if not utils.isChannel(channel): irc.reply("Error: Invalid channel name %r." % channel) return irc.proto.joinClient(u, channel) irc.callHooks([ u, 'PYLINK_BOTSPLUGIN_JOIN', { 'channel': channel, 'users': [u], 'modes': irc.channels[channel].modes, 'parse_as': 'JOIN' } ]) utils.add_cmd(joinclient, name='join') @utils.add_cmd def nick(irc, source, args): """<target> <newnick> Admin-only. Changes the nick of <target>, a PyLink client, to <newnick>.""" utils.checkAuthenticated(irc, source, allowOper=False) try: nick = args[0] newnick = args[1] except IndexError: irc.reply("Error: Not enough arguments. Needs 2: nick, newnick.") return u = utils.nickToUid(irc, nick)
self.push(code) sys.stdout = sys.__stdout__ self.flush() def showtraceback(self): type, value, lasttb = sys.exc_info() self.irc.msg(self.channel, "{0}: {1}".format(type.__name__, value)) def showsyntaxerror(self, filename): self.showtraceback() def repl(irc, source, msgtarget, args): if utils.isAdmin(irc, source): irc.repls.run(msgtarget, args) utils.add_cmd(repl, ">>") def multirepl(irc, source, msgtarget, args): args = args[0] if utils.isAdmin(irc, source) and irc.multirepl == True and args != '"""': irc.repl+=args+"\n" utils.add_regex(multirepl, "(.*)") def multireplprefix(irc, source, msgtarget, args): if utils.isAdmin(irc, source): if irc.multirepl == True: irc.multirepl = False irc.repls.run(msgtarget, irc.repl) irc.repl = "" else:
return log.info('(%s) Loading plugin %r for %s', irc.name, name, utils.getHostmask(irc, source)) try: world.plugins[name] = pl = utils.loadModuleFromFolder(name, world.plugins_folder) except ImportError as e: if str(e) == ('No module named %r' % name): log.exception('Failed to load plugin %r: The plugin could not be found.', name) else: log.exception('Failed to load plugin %r: ImportError.', name) raise else: if hasattr(pl, 'main'): log.debug('Calling main() function of plugin %r', pl) pl.main(irc) irc.reply("Loaded plugin %r." % name) utils.add_cmd(load) def unload(irc, source, args): """<plugin name>. Unloads a currently loaded plugin.""" utils.checkAuthenticated(irc, source, allowOper=False) try: name = args[0] except IndexError: irc.reply("Error: Not enough arguments. Needs 1: plugin name.") return if name in world.plugins: log.info('(%s) Unloading plugin %r for %s', irc.name, name, utils.getHostmask(irc, source)) pl = world.plugins[name] log.debug('sys.getrefcount of plugin %s is %s', pl, sys.getrefcount(pl))
def _exec(irc, source, args): """<code> Admin-only. Executes <code> in the current PyLink instance. \x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02""" utils.checkAuthenticated(irc, source, allowOper=False) args = ' '.join(args) if not args.strip(): irc.reply('No code entered!') return log.info('(%s) Executing %r for %s', irc.name, args, utils.getHostmask(irc, source)) exec(args, globals(), locals()) utils.add_cmd(_exec, 'exec') def _eval(irc, source, args): """<Python expression> Admin-only. Evaluates the given Python expression and returns the result. \x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02""" utils.checkAuthenticated(irc, source, allowOper=False) args = ' '.join(args) if not args.strip(): irc.reply('No code entered!') return log.info('(%s) Evaluating %r for %s', irc.name, args, utils.getHostmask(irc, source)) irc.reply(eval(args))
def listcommands(irc, source, args): """takes no arguments. Returns a list of available commands PyLink has to offer.""" cmds = list(world.commands.keys()) cmds.sort() for idx, cmd in enumerate(cmds): nfuncs = len(world.commands[cmd]) if nfuncs > 1: cmds[idx] = '%s(x%s)' % (cmd, nfuncs) irc.reply('Available commands include: %s' % ', '.join(cmds)) irc.reply( 'To see help on a specific command, type \x02help <command>\x02.') utils.add_cmd(listcommands, 'list') @utils.add_cmd def help(irc, source, args): """<command> Gives help for <command>, if it is available.""" try: command = args[0].lower() except IndexError: # No argument given, just return 'list' output listcommands(irc, source, args) return if command not in world.commands: irc.msg(source, 'Error: Unknown command %r.' % command) return
from utils import add_cmd import random name = "coin" cmds = ["coin", "cat", "dog"] def flipper(irc,event,args): irc.reply(event, "{0} flips a coin..".format(event.source.nick)) irc.reply(event, "The coin lands on {0}.".format(random.choice(["heads", "tails"]))) def cat(irc,event,args): irc.reply(event, "{0} throws a cat into the air..".format(event.source.nick)) irc.reply(event, "The cat lands on its feet.") def dog(irc,event,args): irc.reply(event, "{0} throws a dog into the air..".format(event.source.nick)) irc.reply(event, "The dog lands on its {0}.".format(random.choice(["tail", "nose"]))) add_cmd(flipper, "coin") add_cmd(cat, "cat") add_cmd(dog, "dog")