def main(): with edi.Manager(name="Actor Service", descr="Controller of all the things") as e: def reply(args, msg): if args.has_key("user"): edi.emit.msg_reply(e.chan, src=args["src"], user=args["user"], msg=msg) @edi.edi_cmd(e, "act", descr="Meta actor command. Try --help", args="COMPLEX") def act(**args): try: c, a = cmd_args(args["cmd"], args["args"]) a = parse_args(a) if a.help in db.keys(): reply(args, db[a.help].help()) elif a.help == None: reply(args, a.help_message) elif a.actor in db.keys(): handle_command(e.chan, c, " ".join(a.args)) except ArgumentParserError, ex: reply(args, ex.message) except UnknownFooException: reply(args, "Unknown Foo") except ParseException: reply(args, "Couldn't parse that")
def describe_cmd(cmd): try: global cmds return [ 'Command "{}"'.format(cmd), cmds[cmd]["descr"], 'Part of app "{}"'.format(cmds[cmd]["app"]), "Arguments: {}".format(cmds[cmd]["args"]) ] except: log.exception("Should not happen") return ["Unknown command"] with edi.Manager( name="Clippy", descr="It looks like you're using EDI. Would you like help?") as e: cmd = edi.Cmd(e.chan, src=MSG_SRC, user=MSG_USER) data = {} @edi.edi_cmd(e, "commands", descr="List available commands") def cmd_commands(**args): cmd.inspect() time.sleep(1) edi.emit.msg_reply(e.chan, src=args["src"], user=args["user"], msg="\n".join(command_list())) @edi.edi_cmd(e, "apps", descr="List running apps")
def mod_karma(thing, operator): thing = thing.strip() if thing in memory: memory[thing] = operator(memory[thing], 1) else: memory[thing] = operator(0, 1) log.info("%s's new karma: %s" % (thing, memory[thing])) with open(jsoned_memory, "wb") as f: json.dump(memory, f) with edi.Manager(name="Karma", descr="Rate what you love and hate.") as e: @edi.edi_cmd(e, "karma", args="TEXT", descr="Get karma of $arg") def get_karma_recv(**args): handle_get_karma(args, args["args"]) @edi.edi_cmd(e, "kinc", args="TEXT", descr="Increment Karma of $arg") def inc_karma(args, **rest): mod_karma(args, operator.add) @edi.edi_cmd(e, "kdec", args="TEXT", descr="Decrement Karma of $arg") def inc_karma(args, **rest): mod_karma(args, operator.sub) @edi.edi_msg(e, "#.recv.*") @edi.edi_filter_matches(r"\b(\S+?)\s?(\+\++|--+)")
cmd=cmd, args=cmd_args, user=args["user"], src=args["rkey"]) def reply_error(args, err): log.info("[UNAUTHORIZED] rkey=%r, user=%r uflags=%r", args["rkey"], args["user"], args["uflags"]) edi.emit.msg_reply(e.chan, src=args["rkey"], user=args["user"], msg=err) with edi.Manager(descr="Parse special strings from messages as EDI commands", name="msg-to-cmd") as e: @edi.edi_msg(e, "#.recv.*") @edi.edi_filter_matches(CMD_REGEX) def parse_cmds(regroups, **args): cmd, cmd_args = regroups is_full = "op" in args["uflags"] is_full = is_full or ("following" in args["uflags"] and args["rkey"].startswith("twitter.sub_edi.recv")) is_guest = "voice" in args["uflags"] cmd_whitelisted = cmd in GUEST_CMD_WHITELIST if is_full or (is_guest and cmd_whitelisted): do_cmd(args, cmd, cmd_args) elif is_guest and not cmd_whitelisted:
write_memory() class FeedUpdater(Thread): def __init__(self): Thread.__init__(self) self.daemon = True def run(self): while True: log.debug("Updating Feeds") update() time.sleep(3 * 60) with edi.Manager(name="RSS", descr="RSS feed client.") as e: @edi.edi_cmd(e, "rss-sub", args="TEXT", descr="Track RSS feed") def add_rss_recv(**args): if len(args["args"].split()) == 2: add_rss(args["src"], args["user"], args["args"].split()[0], args["args"].split()[1]) else: to_send = "Try it like this: !sub <alias> <url>" publish(args["src"], args["user"], to_send) @edi.edi_cmd(e, "rss-unsub", args="TEXT", descr="Un-Track RSS feed") def del_rss_recv(**args): if len(args["args"].split()) == 1: del_rss(args["src"], args["user"], args["args"].split()[0]) else:
logging.basicConfig(level=logging.DEBUG) log = logging.getLogger("actor-service") def setup_gpio(): GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_UP) print "input 23 - schluessel", GPIO.input(23) print "input 24 - rot ", GPIO.input(24) print "input 25 - gruen ", GPIO.input(25) with edi.Manager() as e: cmd = edi.Cmd(e.chan) def process_input(): if GPIO.input(23) == GPIO.LOW and GPIO.input(24) == GPIO.LOW: cmd.telinit("0") time.sleep(5) elif GPIO.input(23) == GPIO.LOW and GPIO.input(25) == GPIO.HIGH: cmd.telinit("4") time.sleep(5) elif GPIO.input(25) == GPIO.HIGH: cmd.sob("chewy") time.sleep(5) elif GPIO.input(24) == GPIO.LOW: cmd.sob("chewy") time.sleep(5)
memory = dict() SUBSTITUTION_RE = r"""^(?:(\S+)[:,]\s*)?s/((?:\\/|[^/])+)/((?:\\/|[^/])+)(?:/(\S*))?""" def publish(inc_msg, message): edi.emit.msg_reply(e.chan, src=inc_msg["rkey"], msg=message) def substitutionFailed(inc_msg): to_send = u"Sorry, %s. I don't know what you are talking about." % (inc_msg["user"]) publish(inc_msg, to_send) with edi.Manager(name="Sed", descr="Sed lets you correct yourself, as well as everybody else.") as e: @edi.edi_msg(e, "#.recv.*") @edi.edi_filter_matches("(?!" + SUBSTITUTION_RE + ")") def collect_message(r, **msg): if re.match(SUBSTITUTION_RE, msg["msg"]): return # The edi_filter_matches decorator doesn't like my inversed regex. user = msg["user"] if user in memory: memory[user] += [msg["msg"]] else: memory[user] = [msg["msg"]] # keep only 50 lines