#!@python@/bin/python from os import environ from os.path import expanduser import socket import ssl from shlex import quote from sys import argv, stdin, stdout from multiprocessing import Process from select import select port = environ.get('NOTMUCH_TCP') host = environ.get('NOTMUCH_HOST') hostname = socket.gethostname() if port is None: port = 2010 if host is None: host = "odin.asgard.yggdrasil" sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) sslcontext.load_verify_locations(cafile = expanduser('~/.notmuch-tcp/ca.pem')) sslcontext.load_cert_chain(certfile = expanduser(f"~/.notmuch-tcp/{hostname}.pem"), keyfile = expanduser(f"~/.notmuch-tcp/{hostname}.key")) with socket.create_connection((host, port)) as sock: with sslcontext.wrap_socket(sock, server_hostname = host) as ssock: def send_args(): escaped_args = ' '.join(map(quote, argv[1:])) ssock.sendall(f"{escaped_args}\n".encode()) def send_stdin(): with open(0, 'rb') as stdin_bin: while True: to_send = stdin_bin.read(256) if to_send: ssock.sendall(to_send) else: break def recv_stdout(): with open(1, 'wb') as stdout_bin: while True: ready = select([ssock], [], [], 5) if ready[0]: received = ssock.recv(256) if len(received) <= 0: break stdout_bin.write(received) else: break send_args() send = Process(target = send_stdin) recv = Process(target = recv_stdout) send.start() recv.start() recv.join() send.terminate()