]>
git.r.bdr.sh - rbdr/dotfiles/blob - weechat/python/screen_away.py
744638a91ecf2f88372ccad17c76c69fe524b3b8
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 # 2010-08-07, Filip H.F. "FiXato" Slagter <fixato@gmail.com>
28 # version 0.8: add command on attach feature
29 # 2010-05-07, Jani Kesänen <jani.kesanen@gmail.com>
30 # version 0.7: add command on detach feature
31 # 2010-03-07, Aron Griffis <agriffis@n01se.net>
32 # version 0.6: move socket check to register,
33 # add hook_config for interval,
34 # reduce default interval from 60 to 5
35 # 2010-02-19, Blake Winton <bwinton@latte.ca>
36 # version 0.5: add option to change nick when away
38 # version 0.4: only update servers that are connected
39 # 2009-11-30, xt <xt@bash.no>
40 # version 0.3: do not touch servers that are manually set away
41 # 2009-11-27, xt <xt@bash.no>
42 # version 0.2: code for TMUX from penryu
43 # 2009-11-27, xt <xt@bash.no>
44 # version 0.1: initial release
50 SCRIPT_NAME
= "screen_away"
51 SCRIPT_AUTHOR
= "xt <xt@bash.no>"
52 SCRIPT_VERSION
= "0.8"
53 SCRIPT_LICENSE
= "GPL3"
54 SCRIPT_DESC
= "Set away status on screen detach"
57 'message': 'Detached from screen',
58 'interval': '5', # How often in seconds to check screen status
59 'away_suffix': '', # What to append to your nick when you're away.
60 'command_on_attach': '', # Command to execute on attach
61 'command_on_detach': '' # Command to execute on detach
69 '''Update timer hook with new interval'''
74 TIMER
= w
.hook_timer(int(w
.config_get_plugin('interval')) * 1000,
75 0, 0, "screen_away_timer_cb", '')
77 def screen_away_config_cb(data
, option
, value
):
78 if option
.endswith(".interval"):
80 return w
.WEECHAT_RC_OK
83 '''Get the servers that are not away, or were set away by this script'''
85 infolist
= w
.infolist_get('irc_server','','')
87 while w
.infolist_next(infolist
):
88 if not w
.infolist_integer(infolist
, 'is_connected') == 1:
90 if not w
.infolist_integer(infolist
, 'is_away') or \
91 w
.infolist_string(infolist
, 'away_message') == \
92 w
.config_get_plugin('message'):
93 buffers
.append((w
.infolist_pointer(infolist
, 'buffer'),
94 w
.infolist_string(infolist
, 'nick')))
95 w
.infolist_free(infolist
)
98 def screen_away_timer_cb(buffer, args
):
99 '''Check if screen is attached, update awayness'''
103 suffix
= w
.config_get_plugin('away_suffix')
104 attached
= os
.access(SOCK
, os
.X_OK
) # X bit indicates attached
106 if attached
and AWAY
:
107 w
.prnt('', '%s: Screen attached. Clearing away status' % SCRIPT_NAME
)
108 for server
, nick
in get_servers():
109 w
.command(server
, "/away")
110 if suffix
and nick
.endswith(suffix
):
111 nick
= nick
[:-len(suffix
)]
112 w
.command(server
, "/nick %s" % nick
)
114 if w
.config_get_plugin("command_on_attach"):
115 w
.command("", w
.config_get_plugin("command_on_attach"))
117 elif not attached
and not AWAY
:
118 w
.prnt('', '%s: Screen detached. Setting away status' % SCRIPT_NAME
)
119 for server
, nick
in get_servers():
121 w
.command(server
, "/nick %s%s" % (nick
, suffix
));
122 w
.command(server
, "/away %s" % w
.config_get_plugin('message'));
124 if w
.config_get_plugin("command_on_detach"):
125 w
.command("", w
.config_get_plugin("command_on_detach"))
127 return w
.WEECHAT_RC_OK
130 if w
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
,
131 SCRIPT_DESC
, "", ""):
132 for option
, default_value
in settings
.iteritems():
133 if not w
.config_is_set_plugin(option
):
134 w
.config_set_plugin(option
, default_value
)
136 if 'STY' in os
.environ
.keys():
137 # We are running under screen
138 cmd_output
= os
.popen('env LC_ALL=C screen -ls').read()
139 match
= re
.search(r
'Sockets? in (/.+)\.', cmd_output
)
141 SOCK
= os
.path
.join(match
.group(1), os
.environ
['STY'])
143 if not SOCK
and 'TMUX' in os
.environ
.keys():
144 # We are running under tmux
145 socket_data
= os
.environ
['TMUX']
146 SOCK
= socket_data
.rsplit(',',2)[0]
150 w
.hook_config("plugins.var.python." + SCRIPT_NAME
+ ".*",
151 "screen_away_config_cb", "")