From 67b9aeeb4e90924bcb1d61dfbe884ff649b49b73 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 29 Aug 2016 11:06:53 +0930 Subject: [PATCH] Add simple meshmb find command --- meshmb.c | 41 ++++++++++++++++++++++++++++++++++------- tests/meshmb | 25 ++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/meshmb.c b/meshmb.c index dfa81bc2..7cc607f5 100644 --- a/meshmb.c +++ b/meshmb.c @@ -139,29 +139,56 @@ static int app_meshmb_read(const struct cli_parsed *parsed, struct cli_context * return ret; } -/* DEFINE_CMD(app_meshmb_find, 0, "Browse available broadcast message feeds", "meshmb", "find", "[]"); static int app_meshmb_find(const struct cli_parsed *parsed, struct cli_context *context) { - + const char *search=NULL; + cli_arg(parsed, "search", &search, NULL, ""); // Ensure the Rhizome database exists and is open if (create_serval_instance_dir() == -1) return -1; if (rhizome_opendb() == -1) return -1; - sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; - sqlite3_stmt *statement = sqlite_prepare(&retry, - "SELECT ROWID, MANIFEST FROM MANIFESTS WHERE SERVICE = '"RHIZOME_SERVICE_MESHMB"' ORDER BY ROWID DESC;"); - while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) { - } + struct rhizome_list_cursor cursor; + bzero(&cursor, sizeof cursor); + cursor.service = RHIZOME_SERVICE_MESHMB; + cursor.name = search && search[0] ? search : NULL; //TODO hide feeds that have been blocked + + if (rhizome_list_open(&cursor) == -1) + return -1; + + const char *names[]={ + "_id", + "id", + "version", + "date", + "name" + }; + cli_columns(context, 5, names); + + unsigned rowcount=0; + int n; + + while ((n = rhizome_list_next(&cursor)) == 1) { + rowcount++; + rhizome_manifest *m = cursor.manifest; + cli_put_long(context, m->rowid, ":"); + cli_put_hexvalue(context, m->cryptoSignPublic.binary, sizeof m->cryptoSignPublic.binary, ":"); + cli_put_long(context, m->version, ":"); + cli_put_long(context, m->has_date ? m->date : 0, ":"); + cli_put_string(context, m->name, "\n"); + } + rhizome_list_release(&cursor); + cli_row_count(context, rowcount); return 0; } +/* DEFINE_CMD(app_meshmb_follow, 0, "", "meshmb", "follow|ignore|block" KEYRING_PIN_OPTIONS, "", ""); diff --git a/tests/meshmb b/tests/meshmb index 2e74088b..c1b0ef0a 100755 --- a/tests/meshmb +++ b/tests/meshmb @@ -8,6 +8,7 @@ rexp_age='[0-9]*' setup_identities() { setup_servald + set_instance +A executeOk_servald config \ set debug.meshms on \ set debug.rhizome on \ @@ -15,7 +16,6 @@ setup_identities() { set debug.rhizome_store on \ set log.console.level debug \ set log.console.show_time on - set_instance +A create_identities $1 } @@ -26,7 +26,7 @@ teardown() { report_all_servald_servers } -doc_meshmbSend="Send broadcast message" +doc_meshmbSend="Send broadcast meshmb message" setup_meshmbSend() { setup_identities 1 } @@ -44,7 +44,7 @@ test_meshmbSend() { } -doc_meshmbRead="Read messages" +doc_meshmbRead="Read meshmb messages" setup_meshmbRead() { setup_identities 1 executeOk_servald meshmb send $IDA1 "Message 1" @@ -57,4 +57,23 @@ test_meshmbRead() { assertStdoutGrep --matches=1 "^1:12:${rexp_age}:Message 1$" assertStdoutLineCount '==' 4 } + +doc_meshmbListFeeds="List meshmb feeds" +setup_meshmbListFeeds() { + setup_identities 3 + executeOk_servald meshmb send $IDA1 "Message 1" + executeOk_servald meshmb send $IDA2 "Message 2" + executeOk_servald meshmb send $IDA3 "Message 3" +} +test_meshmbListFeeds() { + executeOk_servald meshmb find + tfw_cat --stdout + assertStdoutGrep --matches=1 "^_id:id:version:date:name$" + assertStdoutGrep --matches=1 ":${IDA1}:" + assertStdoutGrep --matches=1 ":${IDA2}:" + assertStdoutGrep --matches=1 ":${IDA3}:" + assertStdoutLineCount '==' 5 +} + + runTests "$@"