Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions commando/commando.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import secrets
import string
import runes # type: ignore
import multiprocessing
from typing import Dict, Tuple, Optional

plugin = Plugin()
Expand Down Expand Up @@ -132,6 +133,15 @@ def do_cacherune(plugin, peer_id, runestr):
return {'result': {'rune': runestr}}


def command_run(plugin, peer_id, idnum, method, params):
"""Function to run a command and write the result"""
try:
res = {'result': plugin.rpc.call(method, params)}
except RpcError as e:
res = {'error': e.error}
send_result(plugin, peer_id, idnum, res)


def try_command(plugin, peer_id, idnum, method, params, runestr):
"""Run an arbitrary command and message back the result"""
# You can always set your rune, even if *that rune* wouldn't
Expand All @@ -153,16 +163,17 @@ def try_command(plugin, peer_id, idnum, method, params, runestr):
else:
res = {'error': 'FIXME: Refusing to call inside ourselves'}
else:
try:
res = {'result': plugin.rpc.call(method, params)}
except RpcError as e:
res = {'error': e.error}
# The subprocess does send_result itself: pyln-client doesn't
# support async RPC yet.
multiprocessing.Process(target=command_run,
args=(plugin, peer_id, idnum, method, params)).start()
return

send_result(plugin, peer_id, idnum, res)


@plugin.hook('custommsg')
def on_custommsg(peer_id, payload, plugin, **kwargs):
@plugin.async_hook('custommsg')
def on_custommsg(peer_id, payload, plugin, request, **kwargs):
pbytes = bytes.fromhex(payload)
mtype = int.from_bytes(pbytes[:2], "big")
idnum = int.from_bytes(pbytes[2:10], "big")
Expand Down Expand Up @@ -194,7 +205,7 @@ def on_custommsg(peer_id, payload, plugin, **kwargs):
else:
# Pass through result
finished.req.set_result(ret['result'])
return {'result': 'continue'}
request.set_result({'result': 'continue'})


@plugin.async_method("commando")
Expand Down