| 819 | } |
| 820 | |
| 821 | static const char *check_condition(const tal_t *ctx, |
| 822 | const struct rune *rune, |
| 823 | const struct rune_altern *alt, |
| 824 | struct cond_info *cinfo) |
| 825 | { |
| 826 | const jsmntok_t *ptok; |
| 827 | |
| 828 | if (streq(alt->fieldname, "time")) { |
| 829 | return rune_alt_single_int(ctx, alt, cinfo->now.ts.tv_sec); |
| 830 | } else if (streq(alt->fieldname, "id")) { |
| 831 | if (cinfo->peer) { |
| 832 | const char *id = fmt_node_id(tmpctx, cinfo->peer); |
| 833 | return rune_alt_single_str(ctx, alt, id, strlen(id)); |
| 834 | } |
| 835 | return rune_alt_single_missing(ctx, alt); |
| 836 | } else if (streq(alt->fieldname, "method")) { |
| 837 | if (cinfo->method) { |
| 838 | return rune_alt_single_str(ctx, alt, |
| 839 | cinfo->method, strlen(cinfo->method)); |
| 840 | } |
| 841 | return rune_alt_single_missing(ctx, alt); |
| 842 | } else if (streq(alt->fieldname, "pnum")) { |
| 843 | return rune_alt_single_int(ctx, alt, (cinfo && cinfo->params) ? cinfo->params->size : 0); |
| 844 | } else if (streq(alt->fieldname, "rate")) { |
| 845 | return rate_limit_check(ctx, cinfo->runes, rune, alt, cinfo); |
| 846 | } else if (streq(alt->fieldname, "per")) { |
| 847 | return per_time_check(ctx, cinfo->runes, rune, alt, cinfo); |
| 848 | } |
| 849 | |
| 850 | /* Rest need params lookups: generate this once! */ |
| 851 | if (cinfo->params && strmap_empty(&cinfo->cached_params)) { |
| 852 | const jsmntok_t *t; |
| 853 | size_t i; |
| 854 | |
| 855 | if (cinfo->params->type == JSMN_OBJECT) { |
| 856 | json_for_each_obj(i, t, cinfo->params) { |
| 857 | char *pmemname = tal_fmt(ctx, |
| 858 | "pname%.*s", |
| 859 | t->end - t->start, |
| 860 | cinfo->buf + t->start); |
| 861 | size_t off = strlen("pname"); |
| 862 | |
| 863 | /* First, add version with underscores intact. */ |
| 864 | strmap_add(&cinfo->cached_params, |
| 865 | tal_strdup(ctx, pmemname), t+1); |
| 866 | |
| 867 | /* Now with punctuation removed: */ |
| 868 | for (size_t n = off; pmemname[n]; n++) { |
| 869 | if (cispunct(pmemname[n])) |
| 870 | continue; |
| 871 | pmemname[off++] = pmemname[n]; |
| 872 | } |
| 873 | pmemname[off++] = '\0'; |
| 874 | strmap_add(&cinfo->cached_params, pmemname, t+1); |
| 875 | } |
| 876 | } else if (cinfo->params->type == JSMN_ARRAY) { |
| 877 | json_for_each_arr(i, t, cinfo->params) { |
| 878 | char *pmemname = tal_fmt(tmpctx, "parr%zu", i); |
nothing calls this directly
no test coverage detected