| 852 | } |
| 853 | |
| 854 | static const char *check_condition(const tal_t *ctx, |
| 855 | const struct rune *rune, |
| 856 | const struct rune_altern *alt, |
| 857 | struct cond_info *cinfo) |
| 858 | { |
| 859 | const jsmntok_t *ptok; |
| 860 | |
| 861 | if (streq(alt->fieldname, "time")) { |
| 862 | return rune_alt_single_int(ctx, alt, cinfo->now.ts.tv_sec); |
| 863 | } else if (streq(alt->fieldname, "id")) { |
| 864 | if (cinfo->peer) { |
| 865 | const char *id = fmt_node_id(tmpctx, cinfo->peer); |
| 866 | return rune_alt_single_str(ctx, alt, id, strlen(id)); |
| 867 | } |
| 868 | return rune_alt_single_missing(ctx, alt); |
| 869 | } else if (streq(alt->fieldname, "method")) { |
| 870 | if (cinfo->method) { |
| 871 | return rune_alt_single_str(ctx, alt, |
| 872 | cinfo->method, strlen(cinfo->method)); |
| 873 | } |
| 874 | return rune_alt_single_missing(ctx, alt); |
| 875 | } else if (streq(alt->fieldname, "pnum")) { |
| 876 | return rune_alt_single_int(ctx, alt, (cinfo && cinfo->params) ? cinfo->params->size : 0); |
| 877 | } else if (streq(alt->fieldname, "rate")) { |
| 878 | return rate_limit_check(ctx, cinfo->runes, rune, alt, cinfo); |
| 879 | } else if (streq(alt->fieldname, "per")) { |
| 880 | return per_time_check(ctx, cinfo->runes, rune, alt, cinfo); |
| 881 | } |
| 882 | |
| 883 | /* Rest need params lookups: generate this once! */ |
| 884 | if (cinfo->params && strmap_empty(&cinfo->cached_params)) { |
| 885 | const jsmntok_t *t; |
| 886 | size_t i; |
| 887 | |
| 888 | if (cinfo->params->type == JSMN_OBJECT) { |
| 889 | json_for_each_obj(i, t, cinfo->params) { |
| 890 | char *pmemname = tal_fmt(tmpctx, |
| 891 | "pname%.*s", |
| 892 | t->end - t->start, |
| 893 | cinfo->buf + t->start); |
| 894 | size_t off = strlen("pname"); |
| 895 | |
| 896 | /* First, add version with underscores intact. */ |
| 897 | cache_param(cinfo, tal_strdup(tmpctx, pmemname), t+1); |
| 898 | |
| 899 | /* Now with punctuation removed: */ |
| 900 | for (size_t n = off; pmemname[n]; n++) { |
| 901 | if (cispunct(pmemname[n])) |
| 902 | continue; |
| 903 | pmemname[off++] = pmemname[n]; |
| 904 | } |
| 905 | pmemname[off++] = '\0'; |
| 906 | cache_param(cinfo, pmemname, t+1); |
| 907 | } |
| 908 | } else if (cinfo->params->type == JSMN_ARRAY) { |
| 909 | json_for_each_arr(i, t, cinfo->params) { |
| 910 | char *pmemname = tal_fmt(tmpctx, "parr%zu", i); |
| 911 | strmap_add(&cinfo->cached_params, pmemname, t); |
nothing calls this directly
no test coverage detected