]> git.rmz.io Git - dotfiles.git/blob - weechat/python/screen_away.py
qutebrowser: ignore bookmarks
[dotfiles.git] / weechat / python / screen_away.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2009 by xt <xt@bash.no>
4 # Copyright (c) 2009 by penryu <penryu@gmail.com>
5 # Copyright (c) 2010 by Blake Winton <bwinton@latte.ca>
6 # Copyright (c) 2010 by Aron Griffis <agriffis@n01se.net>
7 # Copyright (c) 2010 by Jani Kesänen <jani.kesanen@gmail.com>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 #
24 # (this script requires WeeChat 0.3.0 or newer)
25 #
26 # History:
27 # 2019-03-04, Germain Z. <germanosz@gmail.com>
28 # version 0.16: add option "socket_file", for use with e.g. dtach
29 # : code reformatting for consistency/PEP8
30 # 2017-11-20, Nils Görs <weechatter@arcor.de>
31 # version 0.15: make script python3 compatible
32 # : fix problem with empty "command_on_*" options
33 # : add option "no_output"
34 # 2014-08-02, Nils Görs <weechatter@arcor.de>
35 # version 0.14: add time to detach message. (idea by Mikaela)
36 # 2014-06-19, Anders Bergh <anders1@gmail.com>
37 # version 0.13: Fix a simple typo in an option description.
38 # 2014-01-12, Phyks (Lucas Verney) <phyks@phyks.me>
39 # version 0.12: Added an option to check status of relays to set unaway in
40 # case of a connected relay.
41 # 2013-08-30, Anders Einar Hilden <hildenae@gmail.com>
42 # version: 0.11: Fix reading of set_away
43 # 2013-06-16, Renato Botelho <rbgarga@gmail.com>
44 # version 0.10: add option to don't set away, only change nick
45 # allow multiple commands on attach/dettach
46 # do not add suffix if nick already have it
47 # 2012-12-29, David Flatz <david@upcs.at>
48 # version 0.9: add option to ignore servers and don't set away status for them
49 # add descriptions to config options
50 # 2010-08-07, Filip H.F. "FiXato" Slagter <fixato@gmail.com>
51 # version 0.8: add command on attach feature
52 # 2010-05-07, Jani Kesänen <jani.kesanen@gmail.com>
53 # version 0.7: add command on detach feature
54 # 2010-03-07, Aron Griffis <agriffis@n01se.net>
55 # version 0.6: move socket check to register,
56 # add hook_config for interval,
57 # reduce default interval from 60 to 5
58 # 2010-02-19, Blake Winton <bwinton@latte.ca>
59 # version 0.5: add option to change nick when away
60 # 2010-01-18, xt
61 # version 0.4: only update servers that are connected
62 # 2009-11-30, xt <xt@bash.no>
63 # version 0.3: do not touch servers that are manually set away
64 # 2009-11-27, xt <xt@bash.no>
65 # version 0.2: code for TMUX from penryu
66 # 2009-11-27, xt <xt@bash.no>
67 # version 0.1: initial release
68
69 import os
70 import re
71 import time
72 import weechat as w
73
74
75 SCRIPT_NAME = 'screen_away'
76 SCRIPT_AUTHOR = 'xt <xt@bash.no>'
77 SCRIPT_VERSION = '0.16'
78 SCRIPT_LICENSE = 'GPL3'
79 SCRIPT_DESC = 'Set away status on screen detach'
80
81 SETTINGS = {
82 'message': ('Detached from screen', 'Away message'),
83 'time_format': ('since %Y-%m-%d %H:%M:%S%z',
84 'time format append to away message'),
85 'interval': ('5', 'How often in seconds to check screen status'),
86 'away_suffix': ('', 'What to append to your nick when you\'re away.'),
87 'command_on_attach': ('',
88 ('Commands to execute on attach, separated by '
89 'semicolon')),
90 'command_on_detach': ('',
91 ('Commands to execute on detach, separated by '
92 'semicolon')),
93 'ignore': ('', 'Comma-separated list of servers to ignore.'),
94 'set_away': ('on', 'Set user as away.'),
95 'ignore_relays': ('off',
96 'Only check screen status and ignore relay interfaces'),
97 'no_output': ('off',
98 'no detach/attach information will be displayed in buffer'),
99 'socket_file': ('', 'Socket file to use (leave blank to auto-detect)'),
100 }
101
102 TIMER = None
103 SOCK = None
104 AWAY = False
105 CONNECTED_RELAY = False
106
107
108 def set_timer():
109 '''Update timer hook with new interval.'''
110 global TIMER
111 if TIMER:
112 w.unhook(TIMER)
113 TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000, 0, 0,
114 'screen_away_timer_cb', '')
115
116
117 def screen_away_config_cb(data, option, value):
118 '''Update timer / sock file on config changes.'''
119 global SOCK
120 if SOCK and option.endswith('.interval'):
121 set_timer()
122 elif option.endswith('.socket_file'):
123 SOCK = value
124 if not SOCK:
125 SOCK = get_sock()
126 if SOCK:
127 set_timer()
128 elif TIMER:
129 w.unhook(TIMER)
130 return w.WEECHAT_RC_OK
131
132
133 def get_servers():
134 '''Get the servers that are not away, or were set away by this script.'''
135 ignores = w.config_get_plugin('ignore').split(',')
136 infolist = w.infolist_get('irc_server', '', '')
137 buffers = []
138 while w.infolist_next(infolist):
139 if (not w.infolist_integer(infolist, 'is_connected') == 1 or
140 w.infolist_string(infolist, 'name') in ignores):
141 continue
142 if (not w.config_string_to_boolean(w.config_get_plugin('set_away')) or
143 not w.infolist_integer(infolist, 'is_away') or
144 w.config_get_plugin('message') in w.infolist_string(
145 infolist, 'away_message')):
146 buffers.append((w.infolist_pointer(infolist, 'buffer'),
147 w.infolist_string(infolist, 'nick')))
148 w.infolist_free(infolist)
149 return buffers
150
151
152 def screen_away_timer_cb(buffer, args):
153 '''Check if screen is attached and update awayness.'''
154 global AWAY, SOCK, CONNECTED_RELAY
155
156 set_away = w.config_string_to_boolean(w.config_get_plugin('set_away'))
157 check_relays = not w.config_string_to_boolean(
158 w.config_get_plugin('ignore_relays'))
159 suffix = w.config_get_plugin('away_suffix')
160 attached = os.access(SOCK, os.X_OK) # X bit indicates attached.
161
162 # Check wether a client is connected on relay or not.
163 CONNECTED_RELAY = False
164 if check_relays:
165 infolist = w.infolist_get('relay', '', '')
166 if infolist:
167 while w.infolist_next(infolist):
168 status = w.infolist_string(infolist, 'status_string')
169 if status == 'connected':
170 CONNECTED_RELAY = True
171 break
172 w.infolist_free(infolist)
173
174 if ((attached and AWAY) or
175 (check_relays and CONNECTED_RELAY and not attached and AWAY)):
176 if not w.config_string_to_boolean(w.config_get_plugin('no_output')):
177 w.prnt('', '{}: Screen attached. Clearing away status'.format(
178 SCRIPT_NAME))
179 for server, nick in get_servers():
180 if set_away:
181 w.command(server, '/away')
182 if suffix and nick.endswith(suffix):
183 nick = nick[:-len(suffix)]
184 w.command(server, '/nick {}'.format(nick))
185 AWAY = False
186 if w.config_get_plugin('command_on_attach'):
187 for cmd in w.config_get_plugin('command_on_attach').split(';'):
188 w.command('', cmd)
189
190 elif not attached and not AWAY:
191 if not CONNECTED_RELAY:
192 if (not w.config_string_to_boolean(
193 w.config_get_plugin('no_output'))):
194 w.prnt('', '{}: Screen detached. Setting away status'.format(
195 SCRIPT_NAME))
196 for server, nick in get_servers():
197 if suffix and not nick.endswith(suffix):
198 w.command(server, '/nick {}{}'.format(nick, suffix))
199 if set_away:
200 w.command(server, '/away {} {}'.format(
201 w.config_get_plugin('message'),
202 time.strftime(w.config_get_plugin('time_format'))))
203 AWAY = True
204 if w.config_get_plugin('command_on_detach'):
205 for cmd in w.config_get_plugin('command_on_detach').split(';'):
206 w.command('', cmd)
207
208 return w.WEECHAT_RC_OK
209
210
211 def get_sock():
212 '''Try to get the appropriate sock file for screen/tmux.'''
213 sock = None
214 if 'STY' in os.environ.keys():
215 # We are running under screen.
216 cmd_output = os.popen('env LC_ALL=C screen -ls').read()
217 match = re.search(r'Sockets? in (/.+)\.', cmd_output)
218 if match:
219 sock = os.path.join(match.group(1), os.environ['STY'])
220
221 if not sock and 'TMUX' in os.environ.keys():
222 # We are running under tmux.
223 socket_data = os.environ['TMUX']
224 sock = socket_data.rsplit(',', 2)[0]
225 return sock
226
227
228 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
229 SCRIPT_DESC, '', ''):
230 version = w.info_get('version_number', '') or 0
231 for option, default_desc in SETTINGS.items():
232 if not w.config_is_set_plugin(option):
233 w.config_set_plugin(option, default_desc[0])
234 if int(version) >= 0x00030500:
235 w.config_set_desc_plugin(option, default_desc[1])
236
237 SOCK = w.config_get_plugin('socket_file')
238 if not SOCK:
239 SOCK = get_sock()
240
241 if SOCK:
242 set_timer()
243 w.hook_config('plugins.var.python.{}.*'.format(SCRIPT_NAME),
244 'screen_away_config_cb', '')