class JarbasDeltaChatBridge(HiveMindTerminal): protocol = JarbasDeltaChatBridgeProtocol platform = "JarbasDeltaChatBridgeV0.1" def __init__(self, email=None, password=None, *args, **kwargs): super().__init__(*args, **kwargs) self.waiting = False db = join(gettempdir(), "HiveMindDeltaChatBridge_" + str(email)) self.account = Account(db) self.email = email self.pswd = password self.allowed_emails = [] # if empty allow all if not self.account.is_configured(): self.account.set_config("addr", self.email) self.account.set_config("mail_pw", self.pswd) self.account.set_config("mvbox_move", "0") self.account.set_config("mvbox_watch", "0") self.account.set_config("sentbox_watch", "0") configtracker = self.account.configure() configtracker.wait_finish() # map email addresses to deltachat objects self.addr2chat = {} # deltachat events @account_hookimpl def ac_incoming_message(self, message): print("process_incoming message", message) if message.is_system_message(): return # TODO send hivemind event? # check allowed emails addr = message.get_sender_contact().addr if not self.allowed_emails or addr in self.allowed_emails: # accept the chat message.create_chat() else: return # TODO send hivemind event? self.addr2chat[addr] = message.chat utterance = message.text msg = { "data": { "utterances": [utterance], "lang": "en-us" }, "type": "recognizer_loop:utterance", "context": { "source": self.client.peer, "destination": "hive_mind", "deltachat_user": addr, "platform": self.platform } } self.send_to_hivemind_bus(msg) # hivemind bridge functionality def speak(self, utterance, addr): self.addr2chat[addr].send_text(utterance) def run_deltachat(self): self.account.add_account_plugin(self) # start IO threads and configure if necessary self.account.start_io() print("Running deltachat bridge for: " + self.account.get_config("addr")) self.account.wait_shutdown() # parsed hivemind protocol messages def handle_incoming_mycroft(self, message): assert isinstance(message, Message) addr = message.context.get("deltachat_user") if not addr: return if message.msg_type == "speak": utterance = message.data["utterance"] self.speak(utterance, addr) elif message.msg_type == "hive.complete_intent_failure": LOG.error("complete intent failure") self.speak('I don\'t know how to answer that', addr) # hivemind websocket def clientConnectionLost(self, connector, reason): super().clientConnectionLost(connector, reason) # if set to auto reconnect the hivemind connection is reestablished, # otherwise we want to shutdown deltachat if not self.auto_reconnect: self.account.shutdown()
def main() -> None: sys.argv[0] = "curseddelta" argv = sys.argv app_path = os.path.join(os.path.expanduser("~"), ".curseddelta") if not os.path.exists(app_path): os.makedirs(app_path) cfg = get_configuration() parser = argparse.ArgumentParser(prog=argv[0]) parser.add_argument( "--db", action="store", help="database file", default=cfg["general"]["account_path"], ) parser.add_argument( "--show-ffi", action="store_true", help="show low level ffi events" ) parser.add_argument("--email", action="store", help="email address") parser.add_argument("--password", action="store", help="password") parser.add_argument("--set-conf", action="store", help="set config option", nargs=2) parser.add_argument("--get-conf", action="store", help="get config option") parser.add_argument( "--port", action="store", help="port to listen for oauth2 callback", type=int, default="8383", ) args = parser.parse_args(argv[1:]) ac = Account(os.path.expanduser(args.db)) if args.get_conf: print(ac.get_config(args.get_conf)) return if args.show_ffi: log = events.FFIEventLogger(ac, "CursedDelta") ac.add_account_plugin(log) if not ac.is_configured(): assert ( args.email ), "you must specify --email once to configure this database/account" ac.set_config("addr", args.email) if not args.password and is_oauth2(ac, args.email): authz_code = get_authz_code(ac, args.email, args.port) ac.set_config("mail_pw", authz_code) flags = ac.get_config("server_flags") flags = int(flags) if flags else 0 flags |= deltachat.const.DC_LP_AUTH_OAUTH2 ac.set_config("server_flags", str(flags)) else: assert ( args.password ), "you must specify --password once to configure this database/account" ac.set_config("mail_pw", args.password) with ac.temp_plugin(ConfigureTracker(ac)) as tracker: ac.configure() tracker.wait_finish() if args.set_conf: ac.set_config(*args.set_conf) account = AccountPlugin(ac) ac.add_account_plugin(account) ac.start_io() keymap = get_keymap() theme = get_theme() CursedDelta(cfg, keymap, theme, APP_NAME, account) ac.shutdown()