From 4690f0cec21669eb5c54a4dfec78bef7e568beed Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 3 Oct 2020 19:58:54 +0200 Subject: ssh: Update clientlib for protocol version 3 --- ssh/client.c | 17 +++++++++++++++++ ssh/tomsg_clientlib.c | 32 +++++++++++++++++++++++++++++++- ssh/tomsg_clientlib.h | 13 +++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/ssh/client.c b/ssh/client.c index e6376cd..b293a33 100644 --- a/ssh/client.c +++ b/ssh/client.c @@ -244,6 +244,12 @@ static bool handle_line( } else if (sv_equals(command, "create_room")) { ret = tomsg_create_room(client); + } else if (sv_equals(command, "leave") || sv_equals(command, "leave_room")) { + if (parse_args(line, args, num_args = 1, false)) { + autocomplete_roomname(state, &args[0]); + ret = tomsg_leave_room(client, args[0]); + } + } else if (sv_equals(command, "invite")) { if (parse_args(line, args, num_args = 2, false)) { autocomplete_roomname(state, &args[0]); @@ -368,6 +374,7 @@ static const char* event_type_descr(enum tomsg_event_type type) { case TOMSG_EV_LIST_ROOMS: return "list_rooms"; case TOMSG_EV_LIST_MEMBERS: return "list_members"; case TOMSG_EV_CREATE_ROOM: return "create_room"; + case TOMSG_EV_LEAVE_ROOM: return "leave_room"; case TOMSG_EV_INVITE: return "invite"; case TOMSG_EV_SEND: return "send"; case TOMSG_EV_HISTORY: return "history"; @@ -379,6 +386,7 @@ static const char* event_type_descr(enum tomsg_event_type type) { case TOMSG_EV_PUSH_MESSAGE: return "push_message"; case TOMSG_EV_PUSH_INVITE: return "push_invite"; case TOMSG_EV_PUSH_JOIN: return "push_join"; + case TOMSG_EV_PUSH_LEAVE: return "push_leave"; } return "?unknown type?"; @@ -445,6 +453,10 @@ static void handle_event(struct state *state, const struct tomsg_event event) { printf(" Created room %s\n", event.create_room.room_name); break; + case TOMSG_EV_LEAVE_ROOM: + printf(" Left room %s\n", event.leave_room.room_name); + break; + case TOMSG_EV_INVITE: printf(" Invited %s to %s\n", event.join.username, event.join.room_name); @@ -504,6 +516,11 @@ static void handle_event(struct state *state, const struct tomsg_event event) { printf(" %s has joined %s\n", event.join.username, event.join.room_name); break; + + case TOMSG_EV_PUSH_LEAVE: + printf(" %s has left room %s\n", + event.push_leave.username, event.push_leave.room_name); + break; } } diff --git a/ssh/tomsg_clientlib.c b/ssh/tomsg_clientlib.c index e6d3684..df22a1e 100644 --- a/ssh/tomsg_clientlib.c +++ b/ssh/tomsg_clientlib.c @@ -178,7 +178,7 @@ const char* tomsg_strerror(enum tomsg_retval code) { static enum tomsg_retval version_negotiation(struct tomsg_client *client) { if (!client->conn) return TOMSG_ERR_CLOSED; - const enum sshnc_retval retssh = sshnc_send(client->conn, "ver version 2\n", 14); + const enum sshnc_retval retssh = sshnc_send(client->conn, "ver version 3\n", 14); if (retssh == SSHNC_EOF) return TOMSG_ERR_CLOSED; if (retssh != SSHNC_OK) return TOMSG_ERR_TRANSPORT; @@ -607,6 +607,10 @@ void tomsg_event_nullify(struct tomsg_event event) { free(event.create_room.room_name); break; + case TOMSG_EV_LEAVE_ROOM: + free(event.leave_room.room_name); + break; + case TOMSG_EV_LIST_ROOMS: if (event.list_rooms.rooms != NULL) { for (int64_t i = 0; i < event.list_rooms.count; i++) { @@ -649,6 +653,11 @@ void tomsg_event_nullify(struct tomsg_event event) { free(event.push_invite.room_name); free(event.push_invite.inviter); break; + + case TOMSG_EV_PUSH_LEAVE: + free(event.push_leave.room_name); + free(event.push_leave.username); + break; } } @@ -717,6 +726,12 @@ static enum tomsg_retval handle_line( PARSE_WORD(join.username); EXPECT_FINISH(); return TOMSG_OK; + } else if (sv_equals(command, "leave")) { + eventp->type = TOMSG_EV_PUSH_LEAVE; + PARSE_WORD(push_leave.room_name); + PARSE_WORD(push_leave.username); + EXPECT_FINISH(); + return TOMSG_OK; } else { // Unknown command, let's just ignore return TOMSG_ERR_AGAIN; @@ -757,6 +772,7 @@ static enum tomsg_retval handle_line( case TOMSG_EV_LOGIN: case TOMSG_EV_LOGOUT: case TOMSG_EV_INVITE: + case TOMSG_EV_LEAVE_ROOM: if (!sv_equals(command, "ok")) CLEANUP_RETURN(TOMSG_ERR_PARSE); SUCCESS_RETURN(); @@ -883,6 +899,7 @@ static enum tomsg_retval handle_line( case TOMSG_EV_PUSH_MESSAGE: case TOMSG_EV_PUSH_INVITE: case TOMSG_EV_PUSH_JOIN: + case TOMSG_EV_PUSH_LEAVE: // What? Why did we put a push event in the inflight list? That's a bug. assert(false); } @@ -1023,6 +1040,19 @@ enum tomsg_retval tomsg_create_room(struct tomsg_client *client) { return add_inflight(client, tag, event); } +enum tomsg_retval tomsg_leave_room( + struct tomsg_client *client, const char *room_name) { + if (!client->conn) return TOMSG_ERR_CLOSED; + if (hasspacelf(room_name)) return TOMSG_ERR_SPACE; + const int64_t tag = client->next_tag++; + SEND_FMT(client, tag, "leave_room %s", room_name); + const struct tomsg_event event = (struct tomsg_event){ + .type = TOMSG_EV_LEAVE_ROOM, + .leave_room.room_name = strdup(room_name), + }; + return add_inflight(client, tag, event); +} + enum tomsg_retval tomsg_invite( struct tomsg_client *client, const char *room_name, const char *username) { if (!client->conn) return TOMSG_ERR_CLOSED; diff --git a/ssh/tomsg_clientlib.h b/ssh/tomsg_clientlib.h index 8a4aae4..20eb96e 100644 --- a/ssh/tomsg_clientlib.h +++ b/ssh/tomsg_clientlib.h @@ -123,6 +123,7 @@ enum tomsg_event_type { TOMSG_EV_LIST_ROOMS, // list_rooms TOMSG_EV_LIST_MEMBERS, // list_members TOMSG_EV_CREATE_ROOM, // create_room + TOMSG_EV_LEAVE_ROOM, // leave_room TOMSG_EV_INVITE, // join TOMSG_EV_SEND, // send TOMSG_EV_HISTORY, // history @@ -134,6 +135,7 @@ enum tomsg_event_type { TOMSG_EV_PUSH_MESSAGE, // push_message TOMSG_EV_PUSH_INVITE, // push_invite TOMSG_EV_PUSH_JOIN, // join + TOMSG_EV_PUSH_LEAVE, // push_leave }; struct history_message { @@ -168,6 +170,9 @@ struct tomsg_event { struct { char *room_name; } create_room; + struct { + char *room_name; + } leave_room; struct { char *room_name; char *username; @@ -200,6 +205,10 @@ struct tomsg_event { char *room_name; char *inviter; } push_invite; + struct { + char *room_name; + char *username; + } push_leave; }; }; @@ -243,6 +252,10 @@ enum tomsg_retval tomsg_list_members(struct tomsg_client *client, const char *ro // Send a create_room command to the server. enum tomsg_retval tomsg_create_room(struct tomsg_client *client); +// Send a leave_room command to the server. +enum tomsg_retval tomsg_leave_room( + struct tomsg_client *client, const char *room_name); + // Send an invite command to the server. enum tomsg_retval tomsg_invite( struct tomsg_client *client, const char *room_name, const char *username); -- cgit v1.2.3-70-g09d2