From 9697b0991ba368f712ee80bba07992feea0c2682 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 15 Aug 2016 17:19:34 +0930 Subject: [PATCH] Tweak message ply internals --- meshms.c | 2 +- meshms_cli.c | 19 ++++++++++--------- message_ply.c | 27 ++++++++++++++++++++------- message_ply.h | 3 ++- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/meshms.c b/meshms.c index e1de78c1..e3502f29 100644 --- a/meshms.c +++ b/meshms.c @@ -179,7 +179,7 @@ static enum meshms_status get_database_conversations(const keyring_identity *id, }else{ p=&ptr->my_ply; } - p->found = 1; + p->found = p->known_bid = 1; p->bundle_id = bid; p->version = version; p->tail = tail; diff --git a/meshms_cli.c b/meshms_cli.c index dd2a58c4..653041a2 100644 --- a/meshms_cli.c +++ b/meshms_cli.c @@ -103,6 +103,12 @@ static int app_meshms_send_message(const struct cli_parsed *parsed, struct cli_c || cli_arg(parsed, "payload", &message, NULL, "") == -1) return -1; + sid_t my_sid, their_sid; + if (str_to_sid_t(&my_sid, my_sidhex) == -1) + return WHY("Invalid sender SID"); + if (str_to_sid_t(&their_sid, their_sidhex) == -1) + return WHY("Invalid recipient SID"); + if (create_serval_instance_dir() == -1) return -1; if (!(keyring = keyring_open_instance_cli(parsed))) @@ -113,11 +119,6 @@ static int app_meshms_send_message(const struct cli_parsed *parsed, struct cli_c return -1; } - sid_t my_sid, their_sid; - if (str_to_sid_t(&my_sid, my_sidhex) == -1) - return WHY("invalid sender SID"); - if (str_to_sid_t(&their_sid, their_sidhex) == -1) - return WHY("invalid recipient SID"); // include terminating NUL enum meshms_status status = meshms_send_message(&my_sid, &their_sid, message, strlen(message) + 1); keyring_free(keyring); @@ -175,7 +176,7 @@ static int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_ if (iter.delivered && !marked_delivered){ cli_put_long(context, id++, ":"); cli_put_long(context, iter.latest_ack_offset, ":"); - cli_put_long(context, iter.timestamp?(int)(now - iter.timestamp):-1, ":"); + cli_put_long(context, iter.timestamp ? (now - iter.timestamp):(long)-1, ":"); cli_put_string(context, "ACK", ":"); cli_put_string(context, "delivered", "\n"); marked_delivered = 1; @@ -183,7 +184,7 @@ static int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_ // TODO new message format here cli_put_long(context, id++, ":"); cli_put_long(context, iter.offset, ":"); - cli_put_long(context, iter.timestamp?(int)(now - iter.timestamp):-1, ":"); + cli_put_long(context, iter.timestamp ? (now - iter.timestamp):(long)-1, ":"); cli_put_string(context, ">", ":"); cli_put_string(context, iter.text, "\n"); break; @@ -193,7 +194,7 @@ static int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_ if (iter.read && !marked_read) { cli_put_long(context, id++, ":"); cli_put_long(context, iter.read_offset, ":"); - cli_put_long(context, iter.timestamp?(int)(now - iter.timestamp):-1, ":"); + cli_put_long(context, iter.timestamp ? (now - iter.timestamp):(long)-1, ":"); cli_put_string(context, "MARK", ":"); cli_put_string(context, "read", "\n"); marked_read = 1; @@ -201,7 +202,7 @@ static int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_ // TODO new message format here cli_put_long(context, id++, ":"); cli_put_long(context, iter.offset, ":"); - cli_put_long(context, iter.timestamp?(int)(now - iter.timestamp):-1, ":"); + cli_put_long(context, iter.timestamp ? (now - iter.timestamp):(long)-1, ":"); cli_put_string(context, "<", ":"); cli_put_string(context, iter.text, "\n"); break; diff --git a/message_ply.c b/message_ply.c index c805cbb2..e8dbf56b 100644 --- a/message_ply.c +++ b/message_ply.c @@ -11,9 +11,9 @@ static int message_ply_load_manifest(const keyring_identity *id, struct message_ply *ply, rhizome_manifest *m) { - assert(ply->found); + assert(ply->known_bid); if (rhizome_retrieve_manifest(&ply->bundle_id, m) != RHIZOME_BUNDLE_STATUS_SAME) - return -1; + return 1; rhizome_authenticate_author(m); if (!m->haveSecret || m->authorship != AUTHOR_AUTHENTIC) return -1; @@ -59,7 +59,7 @@ static int message_ply_fill_manifest(const keyring_identity *id, const sid_t *re assert(m->haveSecret); assert(!recipient || m->payloadEncryption == PAYLOAD_ENCRYPTED); ply->bundle_id = m->cryptoSignPublic; - ply->found = 1; + ply->found = ply->known_bid = 1; } return ret; } @@ -72,11 +72,24 @@ int message_ply_append(const keyring_identity *id, const char *service, const si return -1; int ret=-1; - if (ply->found){ - if (message_ply_load_manifest(id, ply, m)!=0) - goto end; - } else { + + if (ply->known_bid){ + switch(message_ply_load_manifest(id, ply, m)){ + case 0: + ply->found = 1; + break; + case 1: + ply->found = 0; + break; + default: + goto end; + } + } + + if (!ply->found){ rhizome_manifest_set_service(m, service); + if (ply->known_bid) + rhizome_manifest_set_id(m, &ply->bundle_id); if (message_ply_fill_manifest(id, recipient, ply, m)!=0) goto end; } diff --git a/message_ply.h b/message_ply.h index 0032aef4..199a4da4 100644 --- a/message_ply.h +++ b/message_ply.h @@ -14,7 +14,8 @@ struct message_ply { uint64_t version; uint64_t tail; uint64_t size; - uint8_t found; + uint8_t found:1; + uint8_t known_bid:1; }; // cursor state for reading one ply