nostril

A C cli tool for creating nostr events
git clone git://jb55.com/nostril
Log | Files | Refs | Submodules | README | LICENSE

commit 74ba45adbc2b018a84050f1f6218c5a784d2cc87
parent 6e1bfcca7e501140469552c5355ea889cad5851f
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 22 Sep 2024 09:50:07 +0900

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 <jb55@jb55.com>

Diffstat:
Mnostril.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+), 0 deletions(-)

diff --git a/nostril.c b/nostril.c @@ -86,6 +86,7 @@ void usage() printf(" --pow <difficulty> number of leading 0 bits of the id to mine\n"); printf(" --mine-pubkey mine a pubkey instead of id\n"); printf(" --tag <key> <value> add a tag\n"); + printf(" --tagn <N> <tags * N...> add a tag with N elements. eg: `--tagn 3 a b c`, `--tagn 0`\n"); printf(" -e <event_id> shorthand for --tag e <event_id>\n"); printf(" -p <pubkey> shorthand for --tag p <pubkey>\n"); printf(" -t <hashtag> shorthand for --tag t <hashtag>\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) {