2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
This plugin will set your status to **away** if you detach your screen.
|
|
|
|
|
2014-12-10 22:21:58 +00:00
|
|
|
The default behaviour is to check for both tmux and screen (in that order).
|
|
|
|
|
|
|
|
Configuration options
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
use_screen
|
|
|
|
**Default:** ``true``
|
|
|
|
|
|
|
|
Try to find an attached screen.
|
|
|
|
|
|
|
|
use_tmux
|
|
|
|
**Default:** ``true``
|
|
|
|
|
|
|
|
Try to find and attached tmux.
|
|
|
|
|
2016-06-04 21:23:33 +00:00
|
|
|
use_csi
|
|
|
|
**Default:** ``false``
|
|
|
|
|
|
|
|
Use `client state indication`_ to limit bandwidth (thus CPU) usage when detached. WARNING: using CSI together with chatrooms will result in inaccurate logs due to presence filtering or other inaccuracies.
|
|
|
|
|
|
|
|
.. _client state indication: https://xmpp.org/extensions/xep-0352.html
|
2013-04-13 20:33:06 +00:00
|
|
|
"""
|
2014-12-10 22:21:58 +00:00
|
|
|
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2011-09-23 15:43:01 +00:00
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import pyinotify
|
2014-12-10 22:21:58 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
DEFAULT_CONFIG = {
|
2015-01-19 12:35:49 +00:00
|
|
|
'screen_detach': {
|
|
|
|
'use_tmux': True,
|
2016-06-04 21:23:33 +00:00
|
|
|
'use_screen': True,
|
|
|
|
'use_csi': False
|
2015-01-19 12:35:49 +00:00
|
|
|
}
|
2014-12-10 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# overload if this is not how your stuff
|
|
|
|
# is configured
|
|
|
|
try:
|
|
|
|
LOGIN = os.getlogin()
|
2015-01-19 12:35:49 +00:00
|
|
|
LOGIN_TMUX = os.getuid()
|
2014-12-10 22:21:58 +00:00
|
|
|
except Exception:
|
|
|
|
LOGIN = os.getenv('USER')
|
|
|
|
LOGIN_TMUX = os.getuid()
|
|
|
|
|
2015-01-05 23:01:21 +00:00
|
|
|
SCREEN_DIR = '/var/run/screens/S-%s' % LOGIN
|
2014-12-10 22:21:58 +00:00
|
|
|
TMUX_DIR = '/tmp/tmux-%s' % LOGIN_TMUX
|
|
|
|
|
|
|
|
def find_screen(path):
|
2015-01-19 12:35:49 +00:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
return
|
2014-12-10 22:21:58 +00:00
|
|
|
for f in os.listdir(path):
|
|
|
|
path = os.path.join(path, f)
|
|
|
|
if screen_attached(path):
|
|
|
|
return path
|
|
|
|
|
|
|
|
def screen_attached(socket):
|
|
|
|
return (os.stat(socket).st_mode & stat.S_IXUSR) != 0
|
|
|
|
|
|
|
|
class Plugin(BasePlugin, pyinotify.Notifier):
|
|
|
|
|
|
|
|
default_config = DEFAULT_CONFIG
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
def init(self):
|
|
|
|
sock_path = None
|
2014-12-10 22:21:58 +00:00
|
|
|
if self.config.get('use_tmux'):
|
|
|
|
sock_path = find_screen(TMUX_DIR)
|
2015-01-05 23:01:21 +00:00
|
|
|
if sock_path is None and self.config.get('use_screen'):
|
2014-12-10 22:21:58 +00:00
|
|
|
sock_path = find_screen(SCREEN_DIR)
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
# Only actually do something if we found an attached screen (assuming only one)
|
|
|
|
if sock_path:
|
2014-12-10 22:21:58 +00:00
|
|
|
self.attached = True
|
2011-09-23 15:43:01 +00:00
|
|
|
wm = pyinotify.WatchManager()
|
|
|
|
wm.add_watch(sock_path, pyinotify.EventsCodes.ALL_FLAGS['IN_ATTRIB'])
|
2014-12-10 22:21:58 +00:00
|
|
|
pyinotify.Notifier.__init__(self, wm, default_proc_fun=HandleScreen(plugin=self))
|
|
|
|
asyncio.get_event_loop().add_reader(self._fd, self.process)
|
2015-01-19 12:35:49 +00:00
|
|
|
else:
|
|
|
|
self.api.information('screen_detach plugin: No tmux or screen found',
|
|
|
|
'Warning')
|
|
|
|
self.attached = False
|
2014-12-10 22:21:58 +00:00
|
|
|
|
|
|
|
def process(self):
|
|
|
|
self.read_events()
|
|
|
|
self.process_events()
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
2014-12-10 22:21:58 +00:00
|
|
|
asyncio.get_event_loop().remove_reader(self._fd)
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
def update_screen_state(self, socket):
|
|
|
|
attached = screen_attached(socket)
|
|
|
|
if attached != self.attached:
|
|
|
|
self.attached = attached
|
|
|
|
status = 'available' if self.attached else 'away'
|
2016-03-31 22:24:58 +00:00
|
|
|
self.core.command.status(status)
|
2016-06-04 22:08:49 +00:00
|
|
|
if self.config.get('use_csi'):
|
2016-06-04 21:23:33 +00:00
|
|
|
if self.attached:
|
|
|
|
self.core.xmpp.plugin['xep_0352'].send_active()
|
|
|
|
else:
|
|
|
|
self.core.xmpp.plugin['xep_0352'].send_inactive()
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
class HandleScreen(pyinotify.ProcessEvent):
|
|
|
|
def my_init(self, **kwargs):
|
|
|
|
self.plugin = kwargs['plugin']
|
|
|
|
|
|
|
|
def process_IN_ATTRIB(self, event):
|
|
|
|
self.plugin.update_screen_state(event.path)
|