Home

Script: detach_away.py

Automatically set away message based on number of relays connected. (for WeeChat ≥ 1.5)
Author: p3lim, version 0.1.0, MIT — added: 2017-05-08
 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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 p3lim <weechat@p3lim.net>
#
# https://github.com/p3lim/weechat-detach-away

try:
	import weechat
except ImportError:
	from sys import exit
	print('This script has to run under WeeChat (https://weechat.org/).')
	exit(1)

from urllib import urlencode

SCRIPT_NAME = 'detach_away'
SCRIPT_AUTHOR = 'p3lim'
SCRIPT_VERSION = '0.1.0'
SCRIPT_LICENSE = 'MIT'
SCRIPT_DESC = 'Automatically sets away message based on number of relays connected'

SETTINGS = {
	'message': (
		'I am away',
		'away message'),
	'debugging': (
		'off',
		'debug flag'),
}

num_relays = 0

def DEBUG():
	return weechat.config_get_plugin('debug') == 'on'

def set_away(is_away, message=''):
	if is_away:
		message = weechat.config_get_plugin('message')

	weechat.command('', '/away -all ' + message)

def relay_connected(data, signal, signal_data):
	global num_relays

	if DEBUG():
		weechat.prnt('', 'DETACH_AWAY: last #relays: ' + str(num_relays))

	if int(num_relays) == 0:
		set_away(False)

	num_relays = weechat.info_get('relay_client_count', 'connected')
	return weechat.WEECHAT_RC_OK

def relay_disconnected(data, signal, signal_data):
	global num_relays

	if DEBUG():
		weechat.prnt('', 'DETACH_AWAY: last #relays: ' + str(num_relays))

	if int(num_relays) > 0:
		set_away(True)

	num_relays = weechat.info_get('relay_client_count', 'connected')
	return weechat.WEECHAT_RC_OK

# register plugin
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '')

# register for relay status
weechat.hook_signal('relay_client_connected', 'relay_connected', '')
weechat.hook_signal('relay_client_disconnected', 'relay_disconnected', '')

# register configuration defaults
for option, value in SETTINGS.items():
	if not weechat.config_is_set_plugin(option):
		weechat.config_set_plugin(option, value[0])

	weechat.config_set_desc_plugin(option, '%s (default: "%s")' % (value[1], value[0]))