diff options
Diffstat (limited to 'ssh/tomsg_clientlib.c')
-rw-r--r-- | ssh/tomsg_clientlib.c | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/ssh/tomsg_clientlib.c b/ssh/tomsg_clientlib.c index c14d5c5..e6d3684 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; @@ -635,6 +635,11 @@ void tomsg_event_nullify(struct tomsg_event event) { free(event.history.messages); break; + case TOMSG_EV_GET_MESSAGE: + free(event.get_message.room_name); + history_message_nullify(event.get_message.message); + break; + case TOMSG_EV_PUSH_MESSAGE: free(event.push_message.room_name); history_message_nullify(event.push_message.message); @@ -691,6 +696,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")) { @@ -809,6 +815,11 @@ static enum tomsg_retval handle_line( inflight.event.history.messages = calloc(count, sizeof(struct history_message)); if (!inflight.event.history.messages) CLEANUP_RETURN(TOMSG_ERR_MEMORY); + // If there are no history_message's coming, complete early + if (count == 0) { + SUCCESS_RETURN(); + } + // Re-add the tag for the history_message events enum tomsg_retval ret = add_inflight(client, tag, inflight.event); if (ret != TOMSG_OK) return ret; @@ -822,11 +833,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 @@ -841,6 +853,16 @@ static enum tomsg_retval handle_line( CLEANUP_RETURN(TOMSG_ERR_PARSE); } + case TOMSG_EV_GET_MESSAGE: + if (!sv_equals(command, "message")) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_copy(sv_tokenise_word(&line), &inflight.event.get_message.room_name)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_copy(sv_tokenise_word(&line), &inflight.event.get_message.message.username)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_parse_i64(sv_tokenise_word(&line), &inflight.event.get_message.message.timestamp)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_parse_i64(sv_tokenise_word(&line), &inflight.event.get_message.message.msgid)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_parse_i64(sv_tokenise_word(&line), &inflight.event.get_message.message.replyid)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + if (!sv_copy(line, &inflight.event.get_message.message.message)) CLEANUP_RETURN(TOMSG_ERR_PARSE); + SUCCESS_RETURN(); + case TOMSG_EV_PING: if (!sv_equals(command, "pong")) CLEANUP_RETURN(TOMSG_ERR_PARSE); if (!sv_is_empty(line)) CLEANUP_RETURN(TOMSG_ERR_PARSE); @@ -1018,13 +1040,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, @@ -1055,6 +1079,17 @@ enum tomsg_retval tomsg_history( return add_inflight(client, tag, event); } +enum tomsg_retval tomsg_get_message(struct tomsg_client *client, int64_t msgid) { + if (!client->conn) return TOMSG_ERR_CLOSED; + if (msgid < -1) msgid = -1; + const int64_t tag = client->next_tag++; + const struct tomsg_event event = (struct tomsg_event){ + .type = TOMSG_EV_GET_MESSAGE, + }; + SEND_FMT(client, tag, "get_message %" PRIi64, msgid); + return add_inflight(client, tag, event); +} + enum tomsg_retval tomsg_ping(struct tomsg_client *client) { if (!client->conn) return TOMSG_ERR_CLOSED; const int64_t tag = client->next_tag++; |