Controlling Leo remotely

53 views
Skip to first unread message

Terry Brown

unread,
Jul 29, 2015, 11:26:24 PM7/29/15
to leo-editor
Prompted by the IRC discussion today, I've added remote code execution
abilities to mod_http.py Relevant part of the doc.s pasted below.

This is a plus for a couple of reasons - previously you had to use
the leoremote plug-in which used sockets and was really python only,
this new approach just uses HTTP GET requests which can be generated by
almost anything (language, link in a doc., browser bookmark, etc.).
Also leoremote doesn't work in Python 3.x.

This code to load a file into Leo from the command line:
https://github.com/leo-editor/snippets/blob/master/utils/led.py

Can be replaced with this version:
https://github.com/leo-editor/snippets/blob/master/utils/led.sh

Executing code remotely
-----------------------

.. warning::

Allowing remote code execution is a **HUGE SECURITY HOLE**, you need to
be sure that the url from which you access Leo (typically
http://localhost:8130/) is accessible only by people and software you trust.

Remote execution is turned off by default, you need to manually / locally
change the @setting ``@bool http_allow_remote_exec = False`` to ``True``
to enable it.

Commands to be executed are submitted via HTTP GET requests, which can
be generated in almost any language and also triggered from shortcuts,
links in other documents or applications, etc. etc.

The basic form is::

http://localhost:8130/_/exec/?cmd=<python code for Leo to execute>

The query parameters are:

``cmd`` (required)
A valid python snippet for Leo to execute. Executed by
the ``vs-eval`` command in the ``valuespace`` plug-in. Can be
specified multiple times, each is executed in order. May contain
newlines, see examples.
``c`` (optional)
Which currently loaded outline to use, can be an integer, starting
from zero, or the full path+filename, or just the base filename.
Defaults to 0 (zero), i.e. the "first" open outline.
``enc`` (optional)
Encoding for response, 'str', 'repr', or 'json'. Used to render
the returned value.
``mime_type`` (optional)
Defaults to ``text/plain``. Could be useful to use ``text/html`` etc.

A special variant url is::

http://localhost:8130/_/exec/commanders/

which returns a list of open outlines.

Examples
========

This command::

curl http://localhost:8130/_/exec/?cmd='c.bringToFront()' >/dev/null

will raise the Leo window, or at least make the window manager signal the
need to raise it.

::

curl --get --data-urlencode \
cmd='g.handleUrl("file:///home/tbrown/.leo/.contacts.leo#Contacts", c)' \
http://localhost:8130/_/exec/ >/dev/null

will cause a running Leo instance to open ``/some/path/contacts.leo`` and select
the ``Contacts`` node. A desktop icon link, browser bookmark, or link in a
spread-sheet or other document could be used the same way.

In the ``bash`` shell language, this code::

TEXT="$@"
curl --silent --show-error --get --data-urlencode cmd="
nd = c.rootPosition().insertAfter()
nd.h = 'TODO: $TEXT'
import time
nd.b = '# created %s' % time.asctime()
c.selectPosition(nd)
c.redraw()
'To do item created\n'
" http://localhost:8130/_/exec/

could be written in a file called ``td``, and then, assuming that file is
executable and on the shell's path, entering::

td remember to vacuum the cat

on the command line would create a node at the top of the first open outline in
Leo with a headline ``TODO: remember to vacuum the cat`` and a body text ``#
created Wed Jul 29 16:42:26 2015``. The command ``vs-eval`` returns the value of
the last expression in a block, so the trailing ``'To do item created\n'`` gives
better feedback than ``None`` generated by ``c.redraw()``.
``c.selectPosition(nd)`` is important ant to stop Leo getting confused about
which node is selected.

Edward K. Ream

unread,
Aug 7, 2015, 7:31:32 AM8/7/15
to leo-editor
On Wed, Jul 29, 2015 at 10:26 PM, 'Terry Brown' via leo-editor <leo-e...@googlegroups.com> wrote:
Prompted by the IRC discussion today, I've added remote code execution
abilities to mod_http.py 

​Thanks Terry!

Edward
Reply all
Reply to author
Forward
0 new messages