]>
git.rmz.io Git - dotfiles.git/blob - weechat/python/screen_away.py
1 # -*- coding: utf-8 -*-
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>
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.
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.
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/>.
24 # (this script requires WeeChat 0.3.0 or newer)
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
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
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'
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 '
90 'command_on_detach': ('',
91 ('Commands to execute on detach, separated by '
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'),
98 'no detach/attach information will be displayed in buffer'),
99 'socket_file': ('', 'Socket file to use (leave blank to auto-detect)'),
105 CONNECTED_RELAY
= False
109 '''Update timer hook with new interval.'''
113 TIMER
= w
.hook_timer(int(w
.config_get_plugin('interval')) * 1000, 0, 0,
114 'screen_away_timer_cb', '')
117 def screen_away_config_cb(data
, option
, value
):
118 '''Update timer / sock file on config changes.'''
120 if SOCK
and option
.endswith('.interval'):
122 elif option
.endswith('.socket_file'):
130 return w
.WEECHAT_RC_OK
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', '', '')
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
):
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
)
152 def screen_away_timer_cb(buffer, args
):
153 '''Check if screen is attached and update awayness.'''
154 global AWAY
, SOCK
, CONNECTED_RELAY
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.
162 # Check wether a client is connected on relay or not.
163 CONNECTED_RELAY
= False
165 infolist
= w
.infolist_get('relay', '', '')
167 while w
.infolist_next(infolist
):
168 status
= w
.infolist_string(infolist
, 'status_string')
169 if status
== 'connected':
170 CONNECTED_RELAY
= True
172 w
.infolist_free(infolist
)
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(
179 for server
, nick
in get_servers():
181 w
.command(server
, '/away')
182 if suffix
and nick
.endswith(suffix
):
183 nick
= nick
[:-len(suffix
)]
184 w
.command(server
, '/nick {}'.format(nick
))
186 if w
.config_get_plugin('command_on_attach'):
187 for cmd
in w
.config_get_plugin('command_on_attach').split(';'):
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(
196 for server
, nick
in get_servers():
197 if suffix
and not nick
.endswith(suffix
):
198 w
.command(server
, '/nick {}{}'.format(nick
, suffix
))
200 w
.command(server
, '/away {} {}'.format(
201 w
.config_get_plugin('message'),
202 time
.strftime(w
.config_get_plugin('time_format'))))
204 if w
.config_get_plugin('command_on_detach'):
205 for cmd
in w
.config_get_plugin('command_on_detach').split(';'):
208 return w
.WEECHAT_RC_OK
212 '''Try to get the appropriate sock file for screen/tmux.'''
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
)
219 sock
= os
.path
.join(match
.group(1), os
.environ
['STY'])
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]
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])
237 SOCK
= w
.config_get_plugin('socket_file')
243 w
.hook_config('plugins.var.python.{}.*'.format(SCRIPT_NAME
),
244 'screen_away_config_cb', '')