Allow sending messages
This commit is contained in:
parent
d9d8c3657f
commit
206440659f
|
@ -436,7 +436,7 @@ void read_status(void) {
|
|||
unsigned char length;
|
||||
readallx(0, &length, 1);
|
||||
|
||||
unsigned char nick[256];
|
||||
unsigned char nick[length];
|
||||
readallx(0, nick, length);
|
||||
|
||||
if (!check_utf8(nick, length, false)) {
|
||||
|
@ -447,6 +447,31 @@ void read_status(void) {
|
|||
own_nick_length = length;
|
||||
}
|
||||
|
||||
void read_message(void) {
|
||||
unsigned char mac[6];
|
||||
readallx(0, mac, sizeof(mac));
|
||||
|
||||
unsigned char length_raw[2];
|
||||
readallx(0, length_raw, 2);
|
||||
size_t length = (length_raw[0] << 8) | length_raw[1];
|
||||
|
||||
if (length > EM_MESSAGE_MAX_LENGTH) {
|
||||
errx(1, "Frontend sent a message that is too long (%zuB, max is %uB)", length, EM_MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
unsigned char message[length];
|
||||
readallx(0, message, length);
|
||||
|
||||
if (!check_utf8(message, length, true)) {
|
||||
errx(1, "Frontend sent a nick with malformed utf-8 or control characters other than newline");
|
||||
}
|
||||
|
||||
memcpy(own_message_destination_mac, mac, sizeof(own_message_destination_mac));
|
||||
memcpy(own_message, message, length);
|
||||
own_message_length = length;
|
||||
own_message_send_state = QUEUED;
|
||||
}
|
||||
|
||||
void read_command(void) {
|
||||
unsigned char cmd;
|
||||
readallx(0, &cmd, 1);
|
||||
|
@ -457,17 +482,14 @@ void read_command(void) {
|
|||
unsigned char mac[6];
|
||||
readallx(0, mac, sizeof(mac));
|
||||
send_status_request(mac);
|
||||
} else if (cmd == 'S') {
|
||||
read_status();
|
||||
} else if (cmd == 'i') {
|
||||
unsigned char mac[6];
|
||||
readallx(0, mac, sizeof(mac));
|
||||
send_msgid_request(mac);
|
||||
} else if (cmd == 'm') {
|
||||
readallx(0, own_message_destination_mac, sizeof(own_message_destination_mac));
|
||||
memcpy(own_message, "Hello, world!", 13);
|
||||
own_message_length = 13;
|
||||
own_message_send_state = QUEUED;
|
||||
} else if (cmd == '\n') {
|
||||
// Ignore
|
||||
read_message();
|
||||
} else {
|
||||
fprintf(stderr, "?"); //debg
|
||||
}
|
||||
|
@ -1026,6 +1048,9 @@ void eventloop(void) {
|
|||
if (pollfds[0].revents & POLLIN) {
|
||||
// Read a command
|
||||
read_command();
|
||||
} else if (pollfds[0].revents & POLLHUP) {
|
||||
// Quit
|
||||
running = false;
|
||||
} else {
|
||||
errx(1, "Got poll event %hd on stdin\n", pollfds[0].revents);
|
||||
}
|
||||
|
|
|
@ -44,7 +44,8 @@ try:
|
|||
|
||||
elif command == 'm':
|
||||
mac = parse_mac(input('mac> '))
|
||||
writeall(proc.stdin, b'm' + mac)
|
||||
message = input('message> ').encode('utf-8')
|
||||
writeall(proc.stdin, b'm' + mac + bytes([len(message) >> 8, len(message) & 0xff]) + message)
|
||||
|
||||
else:
|
||||
print('s - request status, i - request msgid, m - send message, ^D - quit')
|
||||
|
|
Loading…
Reference in New Issue