]>
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 # 2014-08-02, Nils Görs <weechatter@arcor.de>
28 # version 0.14: add time to detach message. (idea by Mikaela)
29 # 2014-06-19, Anders Bergh <anders1@gmail.com>
30 # version 0.13: Fix a simple typo in an option description.
31 # 2014-01-12, Phyks (Lucas Verney) <phyks@phyks.me>
32 # version 0.12: Added an option to check status of relays to set unaway in
33 # case of a connected relay.
34 # 2013-08-30, Anders Einar Hilden <hildenae@gmail.com>
35 # version: 0.11: Fix reading of set_away
36 # 2013-06-16, Renato Botelho <rbgarga@gmail.com>
37 # version 0.10: add option to don't set away, only change nick
38 # allow multiple commands on attach/dettach
39 # do not add suffix if nick already have it
40 # 2012-12-29, David Flatz <david@upcs.at>
41 # version 0.9: add option to ignore servers and don't set away status for them
42 # add descriptions to config options
43 # 2010-08-07, Filip H.F. "FiXato" Slagter <fixato@gmail.com>
44 # version 0.8: add command on attach feature
45 # 2010-05-07, Jani Kesänen <jani.kesanen@gmail.com>
46 # version 0.7: add command on detach feature
47 # 2010-03-07, Aron Griffis <agriffis@n01se.net>
48 # version 0.6: move socket check to register,
49 # add hook_config for interval,
50 # reduce default interval from 60 to 5
51 # 2010-02-19, Blake Winton <bwinton@latte.ca>
52 # version 0.5: add option to change nick when away
54 # version 0.4: only update servers that are connected
55 # 2009-11-30, xt <xt@bash.no>
56 # version 0.3: do not touch servers that are manually set away
57 # 2009-11-27, xt <xt@bash.no>
58 # version 0.2: code for TMUX from penryu
59 # 2009-11-27, xt <xt@bash.no>
60 # version 0.1: initial release
67 SCRIPT_NAME
= "screen_away"
68 SCRIPT_AUTHOR
= "xt <xt@bash.no>"
69 SCRIPT_VERSION
= "0.14"
70 SCRIPT_LICENSE
= "GPL3"
71 SCRIPT_DESC
= "Set away status on screen detach"
74 'message': ('Detached from screen', 'Away message'),
75 'time_format': ('since %Y-%m-%d %H:%M:%S%z', 'time format append to away message'),
76 'interval': ('5', 'How often in seconds to check screen status'),
77 'away_suffix': ('', 'What to append to your nick when you\'re away.'),
78 'command_on_attach': ('', 'Commands to execute on attach, separated by semicolon'),
79 'command_on_detach': ('', 'Commands to execute on detach, separated by semicolon'),
80 'ignore': ('', 'Comma-separated list of servers to ignore.'),
81 'set_away': ('on', 'Set user as away.'),
82 'ignore_relays': ('off', 'Only check screen status and ignore relay interfaces'),
88 CONNECTED_RELAY
= False
91 '''Update timer hook with new interval'''
96 TIMER
= w
.hook_timer(int(w
.config_get_plugin('interval')) * 1000,
97 0, 0, "screen_away_timer_cb", '')
99 def screen_away_config_cb(data
, option
, value
):
100 if option
.endswith(".interval"):
102 return w
.WEECHAT_RC_OK
105 '''Get the servers that are not away, or were set away by this script'''
107 ignores
= w
.config_get_plugin('ignore').split(',')
108 infolist
= w
.infolist_get('irc_server','','')
110 while w
.infolist_next(infolist
):
111 if not w
.infolist_integer(infolist
, 'is_connected') == 1 or \
112 w
.infolist_string(infolist
, 'name') in ignores
:
114 if not w
.config_string_to_boolean(w
.config_get_plugin('set_away')) or \
115 not w
.infolist_integer(infolist
, 'is_away') or \
116 w
.config_get_plugin('message') in w
.infolist_string(infolist
, 'away_message'):
117 # w.infolist_string(infolist, 'away_message') == \
118 # w.config_get_plugin('message'):
119 buffers
.append((w
.infolist_pointer(infolist
, 'buffer'),
120 w
.infolist_string(infolist
, 'nick')))
121 w
.infolist_free(infolist
)
124 def screen_away_timer_cb(buffer, args
):
125 '''Check if screen is attached, update awayness'''
127 global AWAY
, SOCK
, CONNECTED_RELAY
129 set_away
= w
.config_string_to_boolean(w
.config_get_plugin('set_away'))
130 check_relays
= not w
.config_string_to_boolean(w
.config_get_plugin('ignore_relays'))
131 suffix
= w
.config_get_plugin('away_suffix')
132 attached
= os
.access(SOCK
, os
.X_OK
) # X bit indicates attached
134 # Check wether a client is connected on relay or not
135 CONNECTED_RELAY
= False
137 infolist
= w
.infolist_get('relay', '', '')
139 while w
.infolist_next(infolist
):
140 status
= w
.infolist_string(infolist
, 'status_string')
141 if status
== 'connected':
142 CONNECTED_RELAY
= True
144 w
.infolist_free(infolist
)
146 if (attached
and AWAY
) or (check_relays
and CONNECTED_RELAY
and not attached
and AWAY
):
147 w
.prnt('', '%s: Screen attached. Clearing away status' % SCRIPT_NAME
)
148 for server
, nick
in get_servers():
150 w
.command(server
, "/away")
151 if suffix
and nick
.endswith(suffix
):
152 nick
= nick
[:-len(suffix
)]
153 w
.command(server
, "/nick %s" % nick
)
155 for cmd
in w
.config_get_plugin("command_on_attach").split(";"):
158 elif not attached
and not AWAY
:
159 if not CONNECTED_RELAY
:
160 w
.prnt('', '%s: Screen detached. Setting away status' % SCRIPT_NAME
)
161 for server
, nick
in get_servers():
162 if suffix
and not nick
.endswith(suffix
):
163 w
.command(server
, "/nick %s%s" % (nick
, suffix
));
165 w
.command(server
, "/away %s %s" % (w
.config_get_plugin('message'), time
.strftime(w
.config_get_plugin('time_format'))))
167 for cmd
in w
.config_get_plugin("command_on_detach").split(";"):
170 return w
.WEECHAT_RC_OK
173 if w
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
,
174 SCRIPT_DESC
, "", ""):
175 version
= w
.info_get('version_number', '') or 0
176 for option
, default_desc
in settings
.iteritems():
177 if not w
.config_is_set_plugin(option
):
178 w
.config_set_plugin(option
, default_desc
[0])
179 if int(version
) >= 0x00030500:
180 w
.config_set_desc_plugin(option
, default_desc
[1])
182 if 'STY' in os
.environ
.keys():
183 # We are running under screen
184 cmd_output
= os
.popen('env LC_ALL=C screen -ls').read()
185 match
= re
.search(r
'Sockets? in (/.+)\.', cmd_output
)
187 SOCK
= os
.path
.join(match
.group(1), os
.environ
['STY'])
189 if not SOCK
and 'TMUX' in os
.environ
.keys():
190 # We are running under tmux
191 socket_data
= os
.environ
['TMUX']
192 SOCK
= socket_data
.rsplit(',',2)[0]
196 w
.hook_config("plugins.var.python." + SCRIPT_NAME
+ ".*",
197 "screen_away_config_cb", "")