2018-11-17 15:18:34 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Simple plugin to show how to build new plugins for c-lightning
|
|
|
|
|
2018-11-26 18:47:27 +01:00
|
|
|
It demonstrates how a plugin communicates with c-lightning, how it
|
|
|
|
registers command line arguments that should be passed through and how
|
|
|
|
it can register JSON-RPC commands. We communicate with the main daemon
|
|
|
|
through STDIN and STDOUT, reading and writing JSON-RPC requests.
|
2018-11-17 15:18:34 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
greeting = "World"
|
|
|
|
|
|
|
|
|
2018-11-26 18:47:27 +01:00
|
|
|
def json_hello(request, name):
|
|
|
|
greeting = "Hello {}".format(name)
|
2018-11-17 15:18:34 +01:00
|
|
|
return greeting
|
|
|
|
|
|
|
|
|
2018-11-26 19:47:52 +01:00
|
|
|
def json_fail(request):
|
|
|
|
raise ValueError("This will fail")
|
|
|
|
|
|
|
|
|
2018-11-17 15:18:34 +01:00
|
|
|
def json_getmanifest(request):
|
|
|
|
global greeting
|
|
|
|
return {
|
|
|
|
"options": [
|
|
|
|
{"name": "greeting",
|
|
|
|
"type": "string",
|
|
|
|
"default": greeting,
|
|
|
|
"description": "What name should I call you?"},
|
|
|
|
],
|
|
|
|
"rpcmethods": [
|
|
|
|
{
|
|
|
|
"name": "hello",
|
|
|
|
"description": "Returns a personalized greeting for {name}",
|
|
|
|
},
|
2018-11-26 19:47:52 +01:00
|
|
|
{
|
|
|
|
"name": "fail",
|
|
|
|
"description": "Always returns a failure for testing",
|
|
|
|
},
|
2018-11-17 15:18:34 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-06 12:30:10 +01:00
|
|
|
def json_init(request, options, configuration):
|
2018-11-17 15:18:34 +01:00
|
|
|
"""The main daemon is telling us the relevant cli options
|
|
|
|
"""
|
|
|
|
global greeting
|
|
|
|
|
|
|
|
greeting = request['params']['options']['greeting']
|
|
|
|
return "ok"
|
|
|
|
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
'hello': json_hello,
|
2018-11-26 19:47:52 +01:00
|
|
|
'fail': json_fail,
|
2018-11-17 15:18:34 +01:00
|
|
|
'getmanifest': json_getmanifest,
|
|
|
|
'init': json_init,
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:47:27 +01:00
|
|
|
|
2018-11-17 15:18:34 +01:00
|
|
|
partial = ""
|
|
|
|
for l in sys.stdin:
|
|
|
|
try:
|
2018-11-26 19:47:52 +01:00
|
|
|
partial += l
|
2018-11-17 15:18:34 +01:00
|
|
|
request = json.loads(partial)
|
2018-11-26 19:47:52 +01:00
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
|
|
|
|
result = None
|
|
|
|
method = methods[request['method']]
|
|
|
|
params = request['params']
|
|
|
|
try:
|
2018-11-26 18:47:27 +01:00
|
|
|
if isinstance(params, dict):
|
|
|
|
result = method(request, **params)
|
|
|
|
else:
|
|
|
|
result = method(request, *params)
|
2018-11-17 15:18:34 +01:00
|
|
|
result = {
|
|
|
|
"jsonrpc": "2.0",
|
2018-11-26 18:47:27 +01:00
|
|
|
"result": result,
|
2018-11-17 15:18:34 +01:00
|
|
|
"id": request['id']
|
|
|
|
}
|
2018-11-26 19:47:52 +01:00
|
|
|
except Exception as e:
|
|
|
|
result = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"error": "Error while processing {}".format(request['method']),
|
|
|
|
"id": request['id']
|
|
|
|
}
|
|
|
|
|
|
|
|
json.dump(result, fp=sys.stdout)
|
|
|
|
sys.stdout.write('\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
partial = ""
|