aboutsummaryrefslogtreecommitdiff
path: root/ssh
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-07-28 20:06:57 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-07-28 20:06:57 +0200
commit9c7ddb4ac71e7b4bd298e20500ef66f58db6d329 (patch)
treeb723a701776f9c1f8f67aee06d1d59fb196c9bd7 /ssh
parentc6c2e967979e86690f37a83d30ba9bf9afe47a29 (diff)
clientlib: Update to protocol version 2 with replies
Diffstat (limited to 'ssh')
-rw-r--r--ssh/client.c21
-rw-r--r--ssh/tomsg_clientlib.c10
-rw-r--r--ssh/tomsg_clientlib.h5
3 files changed, 29 insertions, 7 deletions
diff --git a/ssh/client.c b/ssh/client.c
index 52f26c6..ad0f107 100644
--- a/ssh/client.c
+++ b/ssh/client.c
@@ -199,7 +199,7 @@ static bool handle_line(
if (state->focus_room != NULL) {
char *message = NULL;
sv_copy(line, &message);
- enum tomsg_retval ret = tomsg_send(client, state->focus_room, message, NULL);
+ enum tomsg_retval ret = tomsg_send(client, state->focus_room, message, -1, NULL);
free(message);
if (ret != TOMSG_OK) return true;
return false;
@@ -253,7 +253,15 @@ static bool handle_line(
} else if (sv_equals(command, "s") || sv_equals(command, "send")) {
if (parse_args(line, args, num_args = 2, true)) {
autocomplete_roomname(state, &args[0]);
- ret = tomsg_send(client, args[0], args[1], NULL);
+ ret = tomsg_send(client, args[0], args[1], -1, NULL);
+ }
+
+ } else if (sv_equals(command, "r") || sv_equals(command, "reply")) {
+ int64_t replyid;
+ if (parse_args(line, args, num_args = 3, true) &&
+ parse_i64(args[1], &replyid)) {
+ autocomplete_roomname(state, &args[0]);
+ ret = tomsg_send(client, args[0], args[2], replyid, NULL);
}
} else if (sv_equals(command, "ping")) {
@@ -314,6 +322,7 @@ static bool handle_line(
" create_room\n"
" invite <room> <user>\n"
" s/send <room> <message...>\n"
+ " r/reply <room> <msgid> <message...>\n"
" ping\n"
" hist/history <room> <count>\n"
" histb/history_before <room> <count> <before_msgid>\n"
@@ -365,8 +374,12 @@ static const char* event_type_descr(enum tomsg_event_type type) {
}
static void print_history_message(const struct history_message msg) {
- printf("%" PRIi64 " \x1B[90m%" PRIi64 "\x1B[0m <%s> %s\n",
- msg.msgid, msg.timestamp, msg.username, msg.message);
+ printf("%" PRIi64 " \x1B[90m%" PRIi64 "\x1B[0m <%s> ",
+ msg.msgid, msg.timestamp, msg.username);
+ if (msg.replyid != -1) {
+ printf("\x1B[32m[%" PRIi64 "<-]\x1B[0m ", msg.replyid);
+ }
+ printf("%s\n", msg.message);
}
static void handle_event(struct state *state, const struct tomsg_event event) {
diff --git a/ssh/tomsg_clientlib.c b/ssh/tomsg_clientlib.c
index c14d5c5..05ee09d 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 1\n", 14);
+ const enum sshnc_retval retssh = sshnc_send(client->conn, "ver version 2\n", 14);
if (retssh == SSHNC_EOF) return TOMSG_ERR_CLOSED;
if (retssh != SSHNC_OK) return TOMSG_ERR_TRANSPORT;
@@ -691,6 +691,7 @@ static enum tomsg_retval handle_line(
PARSE_WORD(push_message.message.username);
PARSE_I64(push_message.message.timestamp);
PARSE_I64(push_message.message.msgid);
+ PARSE_I64(push_message.message.replyid);
PARSE_RESTSTRING(push_message.message.message);
return TOMSG_OK;
} else if (sv_equals(command, "online")) {
@@ -822,11 +823,12 @@ static enum tomsg_retval handle_line(
// (and everywhere): the messages buffer has been allocated with
// calloc(), so tomsg_event_nullify() will cleanup up precisely
// everything that we haven't filled in yet.
- struct history_message *msg = &inflight.event.history.messages[index];;
+ struct history_message *msg = &inflight.event.history.messages[index];
if (!sv_equals(sv_tokenise_word(&line), inflight.event.history.room_name)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
if (!sv_copy(sv_tokenise_word(&line), &msg->username)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
if (!sv_parse_i64(sv_tokenise_word(&line), &msg->timestamp)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
if (!sv_parse_i64(sv_tokenise_word(&line), &msg->msgid)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
+ if (!sv_parse_i64(sv_tokenise_word(&line), &msg->replyid)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
if (!sv_copy(line, &msg->message)) CLEANUP_RETURN(TOMSG_ERR_PARSE);
// If there are more history_message events coming, re-add the tag for them
@@ -1018,13 +1020,15 @@ enum tomsg_retval tomsg_invite(
enum tomsg_retval tomsg_send(
struct tomsg_client *client, const char *room_name, const char *message,
+ int64_t replyid,
int64_t *tagp //output
) {
if (!client->conn) return TOMSG_ERR_CLOSED;
if (hasspacelf(room_name)) return TOMSG_ERR_SPACE;
if (haslf(message)) return TOMSG_ERR_SPACE;
const int64_t tag = client->next_tag++;
- SEND_FMT(client, tag, "send %s %s", room_name, message);
+ if (replyid < 0) replyid = -1;
+ SEND_FMT(client, tag, "send %s %" PRIi64 " %s", room_name, replyid, message);
const struct tomsg_event event = (struct tomsg_event){
.type = TOMSG_EV_SEND,
.send.tag = tag,
diff --git a/ssh/tomsg_clientlib.h b/ssh/tomsg_clientlib.h
index ab1bde5..b164eb3 100644
--- a/ssh/tomsg_clientlib.h
+++ b/ssh/tomsg_clientlib.h
@@ -139,6 +139,7 @@ struct history_message {
char *username;
int64_t timestamp;
int64_t msgid;
+ int64_t replyid; // -1 if normal message, the replied-to message id otherwise
char *message;
};
@@ -245,8 +246,12 @@ enum tomsg_retval tomsg_invite(
// If 'tag' is not NULL, will write the message request tag to the referenced
// location. This tag will also be given in the TOMSG_EV_SEND response, so that
// the response can be linked to the original message.
+// If 'replyid' is -1, will send a normal message. If not, it must be the id of
+// an earlier message in the same room, and this will send a reply to the
+// indicated message.
enum tomsg_retval tomsg_send(
struct tomsg_client *client, const char *room_name, const char *message,
+ int64_t replyid,
int64_t *tag // output
);