def help(self, args): from sip.account import Account def print_help(cmd, fun, long=False): txt = list() txt.append("\n %-10s" % cmd) if fun.__doc__ is not None: doc = fun.__doc__.strip().split('\n') txt.append("- %s" % doc[0]) if long: txt.append('\n') for line in doc[1:]: txt.append(' '*4+line.strip()) return ''.join(txt) if len(args) > 0: if not hasattr(Account, 'do_'+args[0]): repl.error("Unknown *%s* command!" % args[0], place=repl.INBETWEEN); return False fun = getattr(Account, 'do_'+args[0]) repl.info(print_help(args[0], fun, long=True), place=repl.AFTER) return True # list all available commands helps = list() for name, fun in sorted([(name, obj) for (name, obj) in Account.__dict__.iteritems() \ if name.startswith('do_')]): helps.append(print_help(name[3:], fun)) repl.info(''.join(helps), place=repl.AFTER) return True
def handle_request(self, req): """ A request is incoming on a socket used to send data to the server (generic socket). Must be redispatch to correct account (based on To header user) """ username = req.headers['to'].user if username not in self.accounts: repl.error("Unknown targeted '%s' account" % username); return False return getattr(self.accounts[username], 'req_'+req.method.lower())(req)
def handle(self, cmd): """Handle command from CLI i.e: 101 register 101 dial 102 101 ack 458d54ef452a """ #NOTE: filter remove multi-spaces cmd = cmd.strip() if len(cmd) == 0: return True parts = filter(lambda x: len(x) > 0, cmd.split(' ')) if parts[0] == 'help': return self.help(parts[1:]) accnt = self.accounts.get(parts[0], None) if accnt is None: repl.error("Unknown *%s* account" % parts[0], place=repl.INBETWEEN); return False if len(parts) < 2: repl.error("No action specified", place=repl.INBETWEEN); return False action = parts[1].lower() if not hasattr(accnt, 'do_'+action): repl.error("Unknown '%s' action" % action, place=repl.INBETWEEN); return False repl.debug("", place=repl.INBETWEEN) try: return getattr(accnt, 'do_'+action)(*parts[2:]) except TypeError: repl.error("Invalid number of arguments for '%s' action" % action, place=repl.INBETWEEN) return False