ethermess/ethermess.py

62 lines
1.4 KiB
Python
Raw Normal View History

2019-07-10 19:10:14 +00:00
#!/usr/bin/env python3
libexec_dir = __LIBEXECDIR__
2019-07-10 19:44:20 +00:00
import subprocess
import sys
import time
2019-07-10 20:37:39 +00:00
def writeall(f, b):
written = 0
while written < len(b):
written += f.write(b[written:])
def parse_mac(text):
parts = text.split(':')
if len(parts) != 6:
raise ValueError('Invalid MAC format: %s' % text)
try:
parsed = bytes(int(field, 16) for field in parts)
except ValueError:
raise ValueError('Invalid MAC format %s' % text)
return parsed
2019-07-10 19:44:20 +00:00
nick = input('nick> ').encode('utf-8')
2019-07-10 20:37:39 +00:00
proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', *sys.argv[1:]], stdin = subprocess.PIPE, stdout = sys.stdout, stderr = sys.stderr, bufsize = 0)
2019-07-10 19:44:20 +00:00
2019-07-10 20:37:39 +00:00
writeall(proc.stdin, (bytes([0, len(nick)]) + nick))
2019-07-10 19:44:20 +00:00
print('s - request status, i - request msgid, m - send message, ^D - quit')
2019-07-10 21:00:44 +00:00
while True:
try:
try:
command = input('')
if command == 's':
mac = parse_mac(input('mac> '))
writeall(proc.stdin, b's' + mac)
2019-07-10 20:37:39 +00:00
2019-07-10 21:00:44 +00:00
elif command == 'i':
mac = parse_mac(input('mac> '))
writeall(proc.stdin, b'i' + mac)
2019-07-10 20:37:39 +00:00
2019-07-10 21:00:44 +00:00
elif command == 'm':
mac = parse_mac(input('mac> '))
message = input('message> ').encode('utf-8')
writeall(proc.stdin, b'm' + mac + bytes([len(message) >> 8, len(message) & 0xff]) + message)
2019-07-10 20:37:39 +00:00
2019-07-10 21:00:44 +00:00
else:
print('s - request status, i - request msgid, m - send message, ^D - quit')
2019-07-10 20:37:39 +00:00
2019-07-10 21:00:44 +00:00
except EOFError:
writeall(proc.stdin, b'q')
break
2019-07-10 19:44:20 +00:00
2019-07-10 21:00:44 +00:00
except Exception as err:
print(err)
2019-07-10 19:44:20 +00:00
sys.exit(proc.wait())