From 80b91d872d3e251073e1ec6795dde3a3862251f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 14 Jul 2019 23:25:10 +0300 Subject: [PATCH] Add commands for changing status and nick --- ethermess-backend.c | 3 +++ ethermess.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ethermess-backend.c b/ethermess-backend.c index 5199001..812ddf1 100644 --- a/ethermess-backend.c +++ b/ethermess-backend.c @@ -432,6 +432,8 @@ void read_status(void) { errx(1, "Frontend sent a nick with malformed utf-8 or control characters"); } + own_status = status; + memcpy(own_nick, nick, length); own_nick_length = length; } @@ -473,6 +475,7 @@ void read_command(void) { send_status_request(mac); } else if (cmd == 's') { read_status(); + send_status(broadcast_mac); } else if (cmd == 'm') { read_message(); } else { diff --git a/ethermess.py b/ethermess.py index 0b4b7fa..2a41ce3 100644 --- a/ethermess.py +++ b/ethermess.py @@ -5,6 +5,9 @@ import select import subprocess import sys +own_nick = None +own_status = None + def writeall(f, b): written = 0 while written < len(b): @@ -55,7 +58,13 @@ def send_message(backend, mac, message): def send_status_request(backend, mac): writeall(backend, b'r' + mac) +def set_status_nick(backend, status, nick): + encoded = nick.encode('utf-8') + writeall(backend, b's' + bytes([status, len(encoded)]) + encoded) + def handle_user_command(backend, line): + global own_nick, own_status + if len(line) > 0 and line[0] == '/': command, _, rest = line.partition(' ') if command == '/msg': @@ -69,13 +78,25 @@ def handle_user_command(backend, line): mac = parse_mac(rest) send_status_request(backend, mac) + elif command == '/available' and rest == '': + own_status = 0 + set_status_nick(backend, own_status, own_nick) + + elif command == '/unavailable' and rest == '': + own_status = 1 + set_status_nick(backend, own_status, own_nick) + + elif command == '/nick': + own_nick = rest + set_status_nick(backend, own_status, own_nick) + else: # Display usage - print('/msg ; /status ') + print('/msg ; /status ; /available; /unavailable; /nick') else: # Display usage - print('/msg ; /status ') + print('/msg ; /status ; /available; /unavailable; /nick') def eventloop(proc): # Create unbuffered version of stdin @@ -163,14 +184,16 @@ def eventloop(proc): else: raise Exception('Unreachable') def main(): - _, interface, nick = sys.argv + global own_nick, own_status + + _, interface, own_nick = sys.argv proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', interface], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = sys.stderr, bufsize = 0) # Tell the backend the status and nick - status = 0 - nick = nick.encode('utf-8') - writeall(proc.stdin, bytes([status, len(nick)]) + nick) + own_status = 0 + encoded = own_nick.encode('utf-8') + writeall(proc.stdin, bytes([own_status, len(encoded)]) + encoded) # Read our MAC mac = readall(proc.stdout, 6)