MCPcopy Create free account
hub / github.com/ElementsProject/lightning / check_condition

Function check_condition

plugins/commando.c:261–319  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

259}
260
261static const char *check_condition(const tal_t *ctx,
262 const struct rune *rune,
263 const struct rune_altern *alt,
264 struct cond_info *cinfo)
265{
266 const jsmntok_t *ptok;
267
268 if (streq(alt->fieldname, "time")) {
269 return rune_alt_single_int(ctx, alt, time_now().ts.tv_sec);
270 } else if (streq(alt->fieldname, "id")) {
271 const char *id = node_id_to_hexstr(tmpctx, cinfo->peer);
272 return rune_alt_single_str(ctx, alt, id, strlen(id));
273 } else if (streq(alt->fieldname, "method")) {
274 return rune_alt_single_str(ctx, alt,
275 cinfo->buf + cinfo->method->start,
276 cinfo->method->end - cinfo->method->start);
277 } else if (streq(alt->fieldname, "pnum")) {
278 return rune_alt_single_int(ctx, alt, cinfo->params->size);
279 } else if (streq(alt->fieldname, "rate")) {
280 return rate_limit_check(ctx, rune, alt, cinfo);
281 }
282
283 /* Rest are params looksup: generate this once! */
284 if (strmap_empty(&cinfo->cached_params)) {
285 const jsmntok_t *t;
286 size_t i;
287
288 if (cinfo->params->type == JSMN_OBJECT) {
289 json_for_each_obj(i, t, cinfo->params) {
290 char *pmemname = tal_fmt(tmpctx,
291 "pname%.*s",
292 t->end - t->start,
293 cinfo->buf + t->start);
294 size_t off = strlen("pname");
295 /* Remove punctuation! */
296 for (size_t n = off; pmemname[n]; n++) {
297 if (cispunct(pmemname[n]))
298 continue;
299 pmemname[off++] = pmemname[n];
300 }
301 pmemname[off++] = '\0';
302 strmap_add(&cinfo->cached_params, pmemname, t+1);
303 }
304 } else if (cinfo->params->type == JSMN_ARRAY) {
305 json_for_each_arr(i, t, cinfo->params) {
306 char *pmemname = tal_fmt(tmpctx, "parr%zu", i);
307 strmap_add(&cinfo->cached_params, pmemname, t);
308 }
309 }
310 }
311
312 ptok = strmap_get(&cinfo->cached_params, alt->fieldname);
313 if (!ptok)
314 return rune_alt_single_missing(ctx, alt);
315
316 return rune_alt_single_str(ctx, alt,
317 cinfo->buf + ptok->start,
318 ptok->end - ptok->start);

Callers

nothing calls this directly

Calls 7

rune_alt_single_intFunction · 0.85
time_nowFunction · 0.85
rune_alt_single_strFunction · 0.85
rate_limit_checkFunction · 0.85
cispunctFunction · 0.85
rune_alt_single_missingFunction · 0.85
node_id_to_hexstrFunction · 0.50

Tested by

no test coverage detected