1 #include <ccan/array_size/array_size.h>
2 #include <common/json_tok.h>
3 #include <plugins/libplugin.h>
4
5 static u64 cycle_seconds = 0, expired_by = 86400;
6 static struct plugin_timer *cleantimer;
7
8 static void do_clean(void *cb_arg);
9
ignore(struct command * timer,const char * buf,const jsmntok_t * result,void * arg)10 static struct command_result *ignore(struct command *timer,
11 const char *buf,
12 const jsmntok_t *result,
13 void *arg)
14 {
15 struct plugin *p = arg;
16 cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean, p);
17 return timer_complete(p);
18 }
19
do_clean(void * cb_arg)20 static void do_clean(void *cb_arg)
21 {
22 struct plugin *p = cb_arg;
23 /* FIXME: delexpiredinvoice should be in our plugin too! */
24 struct out_req *req = jsonrpc_request_start(p, NULL, "delexpiredinvoice",
25 ignore, ignore, p);
26 json_add_u64(req->js, "maxexpirytime",
27 time_now().ts.tv_sec - expired_by);
28
29 send_outreq(p, req);
30 }
31
json_autocleaninvoice(struct command * cmd,const char * buffer,const jsmntok_t * params)32 static struct command_result *json_autocleaninvoice(struct command *cmd,
33 const char *buffer,
34 const jsmntok_t *params)
35 {
36 u64 *cycle;
37 u64 *exby;
38 struct json_stream *response;
39
40 if (!param(cmd, buffer, params,
41 p_opt_def("cycle_seconds", param_u64, &cycle, 3600),
42 p_opt_def("expired_by", param_u64, &exby, 86400),
43 NULL))
44 return command_param_failed();
45
46 cycle_seconds = *cycle;
47 expired_by = *exby;
48
49 if (cycle_seconds == 0) {
50 response = jsonrpc_stream_success(cmd);
51 json_add_bool(response, "enabled", false);
52 return command_finished(cmd, response);
53 }
54 tal_free(cleantimer);
55 cleantimer = plugin_timer(cmd->plugin, time_from_sec(cycle_seconds),
56 do_clean, cmd->plugin);
57
58 response = jsonrpc_stream_success(cmd);
59 json_add_bool(response, "enabled", true);
60 json_add_u64(response, "cycle_seconds", cycle_seconds);
61 json_add_u64(response, "expired_by", expired_by);
62 return command_finished(cmd, response);
63 }
64
init(struct plugin * p,const char * buf UNUSED,const jsmntok_t * config UNUSED)65 static const char *init(struct plugin *p,
66 const char *buf UNUSED, const jsmntok_t *config UNUSED)
67 {
68 if (cycle_seconds) {
69 plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
70 cleantimer = plugin_timer(p, time_from_sec(cycle_seconds),
71 do_clean, p);
72 } else
73 plugin_log(p, LOG_DBG, "autocleaning not active");
74
75 return NULL;
76 }
77
78 static const struct plugin_command commands[] = { {
79 "autocleaninvoice",
80 "payment",
81 "Set up autoclean of expired invoices. ",
82 "Perform cleanup every {cycle_seconds} (default 3600), or disable autoclean if 0. "
83 "Clean up expired invoices that have expired for {expired_by} seconds (default 86400). ",
84 json_autocleaninvoice
85 }
86 };
87
main(int argc,char * argv[])88 int main(int argc, char *argv[])
89 {
90 setup_locale();
91 plugin_main(argv, init, PLUGIN_STATIC, true, NULL, commands, ARRAY_SIZE(commands),
92 NULL, 0, NULL, 0, NULL, 0,
93 plugin_option("autocleaninvoice-cycle",
94 "string",
95 "Perform cleanup of expired invoices every"
96 " given seconds, or do not autoclean if 0",
97 u64_option, &cycle_seconds),
98 plugin_option("autocleaninvoice-expired-by",
99 "string",
100 "If expired invoice autoclean enabled,"
101 " invoices that have expired for at least"
102 " this given seconds are cleaned",
103 u64_option, &expired_by),
104 NULL);
105 }
106