2013-04-13 20:33:06 +00:00
|
|
|
|
"""
|
|
|
|
|
Opens links in a browser.
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
2013-04-13 20:33:06 +00:00
|
|
|
|
Installation
|
|
|
|
|
------------
|
|
|
|
|
|
|
|
|
|
First use case: local use
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
If you use poezio on your workstation, this is for you.
|
|
|
|
|
You only have to load the plugin: ::
|
|
|
|
|
|
|
|
|
|
/load link
|
|
|
|
|
|
|
|
|
|
Second use case: remote use
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
If you use poezio through SSH, this is for you.
|
|
|
|
|
|
|
|
|
|
.. note:: Small explanation: Poezio will create a `Unix FIFO`_ and send the commands in,
|
|
|
|
|
and you will have to run a dæmon locally with ssh, to get those commands.
|
|
|
|
|
|
|
|
|
|
First, set the :term:`exec_remote` option in the config file to ``true``. Then select
|
|
|
|
|
the directory you want to put the fifo in (default is the current
|
|
|
|
|
directory, :file:`./`), the :file:`poezio.fifo` file will be created there.
|
|
|
|
|
|
|
|
|
|
After that, load the plugin: ::
|
|
|
|
|
|
|
|
|
|
/load link
|
|
|
|
|
|
|
|
|
|
And open a link with :term:`/link` (as described below), this will create the FIFO.
|
|
|
|
|
|
|
|
|
|
You need to grab poezio’s sources on your client computer, or at least the `daemon.py`_
|
|
|
|
|
file.
|
|
|
|
|
|
|
|
|
|
Finally, on your client computer, run the ssh command:
|
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
|
|
ssh toto@example.org "cat ~/poezio/poezio.fifo" | python3 daemon.py
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
|
|
/link
|
2014-09-17 14:16:51 +00:00
|
|
|
|
**Usage:** ``/link [range] [command]``
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
2014-09-17 14:16:51 +00:00
|
|
|
|
This plugin adds a :term:`/link` command that will open the links in
|
|
|
|
|
``firefox``. If you want to use another browser, or any other
|
|
|
|
|
command, you can use the :term:`/set` command to change the
|
|
|
|
|
:term:`browser` option. You can also specify the command to execute
|
|
|
|
|
directly in the arguments. For example `/link "mpv %s"` will open
|
|
|
|
|
the first link found using mpv, instead of the configured browser.
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
|
|
|
2013-10-21 00:25:34 +00:00
|
|
|
|
:term:`/link` without argument will open the last link found
|
|
|
|
|
in the current tab, if any is found. An optional range
|
|
|
|
|
argument can be given, to select one or more links to
|
|
|
|
|
open. Examples:
|
|
|
|
|
``/link 1`` is equivalent to ``/link``
|
|
|
|
|
``/link 3`` will open the third link found in the current tab,
|
|
|
|
|
starting from the bottom.
|
|
|
|
|
``/link 1:5`` will open the last five links in the current tab
|
|
|
|
|
``/link :2`` will open the last two links
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
|
|
Options
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
:term:`exec_remote`
|
|
|
|
|
|
|
|
|
|
To execute the command on your client
|
|
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
|
|
browser
|
|
|
|
|
Set the default browser started by the plugin
|
|
|
|
|
|
|
|
|
|
.. _Unix FIFO: https://en.wikipedia.org/wiki/Named_pipe
|
2013-06-09 18:18:22 +00:00
|
|
|
|
.. _daemon.py: http://dev.louiz.org/projects/poezio/repository/revisions/master/raw/src/daemon.py
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
|
|
"""
|
2014-06-26 13:56:32 +00:00
|
|
|
|
import platform
|
2011-10-29 05:18:19 +00:00
|
|
|
|
import re
|
|
|
|
|
|
2012-05-13 16:45:40 +00:00
|
|
|
|
from plugin import BasePlugin
|
2011-10-29 05:18:19 +00:00
|
|
|
|
from xhtml import clean_text
|
|
|
|
|
import common
|
2012-05-13 16:45:40 +00:00
|
|
|
|
import tabs
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
|
|
url_pattern = re.compile(r'\b(http[s]?://(?:\S+))\b', re.I|re.U)
|
2014-06-26 13:56:32 +00:00
|
|
|
|
app_mapping = {
|
|
|
|
|
'Linux': 'xdg-open',
|
|
|
|
|
'Darwin': 'open',
|
|
|
|
|
}
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
2013-03-01 18:25:31 +00:00
|
|
|
|
for _class in (tabs.MucTab, tabs.PrivateTab, tabs.ConversationTab):
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_tab_command(_class, 'link', self.command_link,
|
2014-09-17 14:16:51 +00:00
|
|
|
|
usage='[num] [command]',
|
|
|
|
|
help='Opens the last link from the conversation into a browser.\n\
|
|
|
|
|
If [num] is given, then it will\open the num-th link displayed. \
|
|
|
|
|
Use a [command] argument to override the configured browser value.',
|
2013-03-01 18:25:31 +00:00
|
|
|
|
short='Open links into a browser')
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
|
|
def find_link(self, nb):
|
2013-03-08 21:53:35 +00:00
|
|
|
|
messages = self.api.get_conversation_messages()
|
2011-10-29 05:18:19 +00:00
|
|
|
|
if not messages:
|
|
|
|
|
return None
|
|
|
|
|
for message in messages[::-1]:
|
2011-10-29 15:20:18 +00:00
|
|
|
|
matches = url_pattern.findall(clean_text(message.txt))
|
|
|
|
|
if matches:
|
|
|
|
|
for url in matches[::-1]:
|
2011-10-29 05:18:19 +00:00
|
|
|
|
if nb == 1:
|
|
|
|
|
return url
|
|
|
|
|
else:
|
|
|
|
|
nb -= 1
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def command_link(self, args):
|
|
|
|
|
args = common.shell_split(args)
|
2014-09-17 14:16:51 +00:00
|
|
|
|
start = 1
|
|
|
|
|
end = 1
|
|
|
|
|
# With two arguments, the first is the range, the second is the command
|
|
|
|
|
# With only one argument, it is a range if it starts with a number
|
|
|
|
|
# or :, otherwise it is a command
|
|
|
|
|
if len(args) == 2 or\
|
|
|
|
|
len(args) == 1 and (args[0][0].isnumeric() or args[0][0] == ":"):
|
2013-10-21 00:25:34 +00:00
|
|
|
|
if args[0].find(':') == -1:
|
|
|
|
|
try:
|
|
|
|
|
start = int(args[0])
|
|
|
|
|
end = start
|
|
|
|
|
except ValueError:
|
|
|
|
|
return self.api.run_command('/help link')
|
|
|
|
|
else:
|
|
|
|
|
start, end = args[0].split(':', 1)
|
|
|
|
|
if start == '':
|
|
|
|
|
start = 1
|
|
|
|
|
try:
|
|
|
|
|
start = int(start)
|
|
|
|
|
end = int(end)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return self.api.information('Invalid range: %s' % (args[0]), 'Error')
|
2014-09-17 14:16:51 +00:00
|
|
|
|
command = None
|
|
|
|
|
if len(args) == 2:
|
|
|
|
|
command = args[1]
|
|
|
|
|
if len(args) == 1 and (not args[0][0].isnumeric() and args[0][0] != ":"):
|
|
|
|
|
command = args[0]
|
2013-10-21 00:25:34 +00:00
|
|
|
|
for nb in range(start, end+1):
|
|
|
|
|
link = self.find_link(nb)
|
|
|
|
|
if not link:
|
|
|
|
|
return self.api.information('No URL found.', 'Warning')
|
2014-06-26 13:56:32 +00:00
|
|
|
|
default = app_mapping.get(platform.system(), 'firefox')
|
2014-09-17 14:16:51 +00:00
|
|
|
|
if command is None:
|
|
|
|
|
self.core.exec_command([self.config.get('browser', default), link])
|
|
|
|
|
else:
|
|
|
|
|
self.core.exec_command([command, link])
|
2011-10-29 05:18:19 +00:00
|
|
|
|
def cleanup(self):
|
|
|
|
|
del self.config
|