Add commands for changing status and nick

This commit is contained in:
Juhani Krekelä 2019-07-14 23:25:10 +03:00
parent c2d021534f
commit 80b91d872d
2 changed files with 32 additions and 6 deletions

View File

@ -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 {

View File

@ -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 <MAC> <message>; /status <MAC>')
print('/msg <MAC> <message>; /status <MAC>; /available; /unavailable; /nick')
else:
# Display usage
print('/msg <MAC> <message>; /status <MAC>')
print('/msg <MAC> <message>; /status <MAC>; /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)