Script: slock_away.py

WARNING: this script has been disabled (not compatible with Python 3).

Set away status if slock (X display locker) is running.
Author: Peter A. Shevtsov — Version: 0.1 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0, requires: slock.
Tags: away, py2
Added: 2011-10-14

Download GitHub Repository

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Peter A. Shevtsov <pshevtsov@severusweb.ru>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Set away status if slock is running
#
# History:
#
# 2011-10-14, Peter A. Shevtsov <pshevtsov@severusweb.ru>:
#     version 0.1: initial release
#

SCRIPT_NAME = "slock_away"
SCRIPT_AUTHOR = "Peter A. Shevtsov <pshevtsov@severusweb.ru>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Set away status if slock is running"

SCRIPT_COMMAND = "slock_away"

import_ok = True

try:
    import weechat
except ImportError:
    print "This script must be run under WeeChat."
    print "Get WeeChat now at: http://www.weechat.org/"
    import_ok = False

try:
    import subprocess
except ImportError:
    print "Missing package(s) for %s: %s" % (SCRIPT_NAME, message)
    import_ok = False

TIMER = None

settings = {
        'away_message': 'Away',
        'interval': '20',  # How often to check for inactivity (in seconds)
        'away': '0'
}


def set_back(overridable_messages):
    """Removes away status for servers
    where one of the overridable_messages is set"""
    if (weechat.config_get_plugin('away') == '0'):
        return  # No need to come back again
    serverlist = weechat.infolist_get('irc_server', '', '')
    if serverlist:
        buffers = []
        while weechat.infolist_next(serverlist):
            if (weechat.infolist_string(serverlist, 'away_message')
                    in overridable_messages):
                ptr = weechat.infolist_pointer(serverlist, 'buffer')
                if ptr:
                    buffers.append(ptr)
        weechat.infolist_free(serverlist)
        for buffer in buffers:
            weechat.command(buffer, "/away")
    weechat.config_set_plugin('away', '0')


def set_away(message, overridable_messages=[]):
    """Sets away status, but respectfully
    (so it doesn't change already set statuses"""
    if (weechat.config_get_plugin('away') == '1'):
        return  # No need to go away again
                # (this prevents some repeated messages)
    serverlist = weechat.infolist_get('irc_server', '', '')
    if serverlist:
        buffers = []
        while weechat.infolist_next(serverlist):
            if weechat.infolist_integer(serverlist, 'is_away') == 0:
                ptr = weechat.infolist_pointer(serverlist, 'buffer')
                if ptr:
                    buffers.append(ptr)
            elif (weechat.infolist_string(serverlist, 'away_message')
                    in overridable_messages):
                buffers.append(weechat.infolist_pointer(serverlist, 'buffer'))
        weechat.infolist_free(serverlist)
        for buffer in buffers:
            weechat.command(buffer, "/away %s" % message)
    weechat.config_set_plugin('away', '1')


def slock_away_cb(data, buffer, args):
    """Callback for /slock_away command"""
    response = {
            'msg': lambda status:
                        weechat.config_set_plugin('away_message', status)
    }
    if args:
        words = args.strip().partition(' ')
        if words[0] in response:
            response[words[0]](words[2])
        else:
            weechat.prnt('', "slock_away error: %s not a recognized command. "
                       "Try /help slock_away" % words[0])
    weechat.prnt('', "slock_away: away message: \"%s\"" %
                      weechat.config_get_plugin('away_message'))
    return weechat.WEECHAT_RC_OK


def auto_check(data, remaining_calls):
    """Callback from timer"""
    check()
    return weechat.WEECHAT_RC_OK


def check():
    """Check for existance of process and set away if it isn't there"""
    pidof = subprocess.Popen("pidof slock",
            shell=True, stdout=subprocess.PIPE)
    pidof.wait()
    if pidof.returncode == 0:
        set_away(weechat.config_get_plugin('away_message'), [])
    else:
        set_back([weechat.config_get_plugin('away_message')])


def check_timer():
    """Sets or unsets the timer
    based on whether or not the plugin is enabled"""
    global TIMER
    if TIMER:
        weechat.unhook(TIMER)
    TIMER = weechat.hook_timer(
            int(weechat.config_get_plugin('interval')) * 1000,
            0, 0, "auto_check", "")

if __name__ == "__main__" and import_ok:
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
                  SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
        for option, default_value in settings.iteritems():
            if not weechat.config_is_set_plugin(option):
                weechat.config_set_plugin(option, default_value)

        weechat.hook_command(SCRIPT_COMMAND,
                             SCRIPT_DESC,
                             "msg <status>",
                             "msg: set the away message\n",
                             "", "slock_away_cb", "")
        check_timer()