From 74ba45adbc2b018a84050f1f6218c5a784d2cc87 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 22 Sep 2024 09:50:07 +0900 Subject: [PATCH] add `--tagn 3 a b c` Flexible way to add tag with N elements: eg: empty tag: --tagn 0 protected event: --tagn 1 - Signed-off-by: William Casarin --- nostril.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/nostril.c b/nostril.c index e45ba39..0dec5b5 100644 --- a/nostril.c +++ b/nostril.c @@ -86,6 +86,7 @@ void usage() printf(" --pow number of leading 0 bits of the id to mine\n"); printf(" --mine-pubkey mine a pubkey instead of id\n"); printf(" --tag add a tag\n"); + printf(" --tagn add a tag with N elements. eg: `--tagn 3 a b c`, `--tagn 0`\n"); printf(" -e shorthand for --tag e \n"); printf(" -p shorthand for --tag p \n"); printf(" -t shorthand for --tag t \n"); @@ -418,6 +419,54 @@ static int nostr_add_tag(struct nostr_event *ev, const char *t1, const char *t2) return nostr_add_tag_n(ev, ts, 2); } +static int nostr_add_arg_tags(struct nostr_event *ev, int *argc, const char **argv[]) +{ + struct nostr_tag *tag; + int i; + uint64_t n; + uint16_t small_n; + const char *arg; + + if (*argc == 0) { + fprintf(stderr, "expected more arguments to --tagn\n"); + return 0; + } + + arg = *(*argv)++; (*argc)--; + + if (!parse_num(arg, &n)) { + fprintf(stderr, "expected X to be a number in --tagn X ...\n"); + return 0; + } + + if (n > MAX_TAG_ELEMS) { + fprintf(stderr, "too many --tagn arguments (%"PRIu64" > %d)\n", n, MAX_TAG_ELEMS); + return 0; + } + + small_n = (uint16_t)n; + + if (ev->num_tags + small_n > MAX_TAGS) { + fprintf(stderr, "too many tags (%d + %d > %d)\n", ev->num_tags, small_n, MAX_TAG_ELEMS); + return 0; + } + + tag = &ev->tags[ev->num_tags++]; + tag->num_elems = 0; + + for (i = 0; i < small_n; i++) { + if (*argc <= 0) { + fprintf(stderr, "expected %d tags: --tagn %d ..., got %d args\n", small_n, small_n, i); + return 0; + } + arg = *(*argv)++; (*argc)--; + //fprintf(stderr, "got arg '%s' argc(%d)\n", arg, *argc); + tag->strs[i] = arg; + tag->num_elems++; + } + + return 1; +} static int parse_args(int argc, const char *argv[], struct args *args, struct nostr_event *ev) { @@ -490,6 +539,11 @@ static int parse_args(int argc, const char *argv[], struct args *args, struct no fprintf(stderr, "couldn't add t tag"); return 0; } + } else if (!strcmp(arg, "--tagn")) { + if (!nostr_add_arg_tags(ev, &argc, &argv)) { + return 0; + } + has_added_tags = 1; } else if (!strcmp(arg, "--tag")) { has_added_tags = 1; if (args->tags) {