diff options
| -rwxr-xr-x | clickclack.py | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/clickclack.py b/clickclack.py index 7ba5c97..e10f5ee 100755 --- a/clickclack.py +++ b/clickclack.py @@ -50,42 +50,62 @@ if __name__ == '__main__': conn_send_cmds = open(CONN_SEND, 'rb').read() conn_send_cmds = conn_send_cmds.replace(b'%s', bytes(str(time.time()).encode('ascii'))) proc.stdin.write(conn_send_cmds) + for line in conn_send_cmds.split(b'\n'): + sys.stdout.buffer.write(b'>'+line+b'\n') proc.stdin.flush() os.set_blocking(proc.stdout.fileno(), False) os.set_blocking(proc.stderr.fileno(), False) child_buffer = bytearray() + fds = [proc.stdout, proc.stderr, sys.stdin.buffer] while True: if DEBUG: print('selecting') - rd = select.select([proc.stdout, proc.stderr, sys.stdin.buffer], [], [])[0] + if len(fds) <= 1: + break + rd = select.select(fds, [], [])[0] for f in rd: if DEBUG: print(f'reading {f!r}') if f is proc.stdout: if DEBUG: print(f'writing to stdout') child_buffer += f.read() + if not len(child_buffer): + print('EOF on child stdout') + fds.remove(f) + continue child_buffer.replace(b'\r',b'') if b'\n' in child_buffer: lines = child_buffer.split(b'\n') for line in lines[:-1]: if line == b'': continue source, cmd, args = parse_irc_line(line) - sys.stdout.buffer.write(line+b'\r\n') + sys.stdout.buffer.write(b'<'+line+b'\r\n') sys.stdout.buffer.flush() if cmd == b'PING': if CLIENT_MODE: pong = b'PONG %s\r\n' % (format_irc_args(args)) else: pong = b'PONG %s\r\n' % (source) - sys.stdout.buffer.write(pong) + sys.stdout.buffer.write(b'>'+pong) sys.stdout.buffer.flush() proc.stdin.write(pong) proc.stdin.flush() child_buffer = lines[-1] if f is proc.stderr: if DEBUG: print(f'writing to stderr') - sys.stderr.buffer.write(f.read()) + output = f.read() + if not len(output): + print('EOF on child stderr') + fds.remove(f) + continue + sys.stderr.buffer.write(b'!'+output) sys.stderr.buffer.flush() if f is sys.stdin.buffer: if DEBUG: print(f'writing to child') - proc.stdin.write(f.readline()) + input = f.readline() + if not len(input): + print('EOF on stdin') + fds.remove(f) + proc.stdin.close() + continue + proc.stdin.write(input) proc.stdin.flush() |
