]> git.r.bdr.sh - rbdr/dotfiles/blob - weechat/python/screen_away.py
Update weechat config
[rbdr/dotfiles] / 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 # 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
37 # 2010-01-18, xt
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
45
46 import weechat as w
47 import re
48 import os
49
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"
55
56 settings = {
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
62 }
63
64 TIMER = None
65 SOCK = None
66 AWAY = False
67
68 def set_timer():
69 '''Update timer hook with new interval'''
70
71 global TIMER
72 if TIMER:
73 w.unhook(TIMER);
74 TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000,
75 0, 0, "screen_away_timer_cb", '')
76
77 def screen_away_config_cb(data, option, value):
78 if option.endswith(".interval"):
79 set_timer()
80 return w.WEECHAT_RC_OK
81
82 def get_servers():
83 '''Get the servers that are not away, or were set away by this script'''
84
85 infolist = w.infolist_get('irc_server','','')
86 buffers = []
87 while w.infolist_next(infolist):
88 if not w.infolist_integer(infolist, 'is_connected') == 1:
89 continue
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)
96 return buffers
97
98 def screen_away_timer_cb(buffer, args):
99 '''Check if screen is attached, update awayness'''
100
101 global AWAY, SOCK
102
103 suffix = w.config_get_plugin('away_suffix')
104 attached = os.access(SOCK, os.X_OK) # X bit indicates attached
105
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)
113 AWAY = False
114 if w.config_get_plugin("command_on_attach"):
115 w.command("", w.config_get_plugin("command_on_attach"))
116
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():
120 if suffix:
121 w.command(server, "/nick %s%s" % (nick, suffix));
122 w.command(server, "/away %s" % w.config_get_plugin('message'));
123 AWAY = True
124 if w.config_get_plugin("command_on_detach"):
125 w.command("", w.config_get_plugin("command_on_detach"))
126
127 return w.WEECHAT_RC_OK
128
129
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)
135
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)
140 if match:
141 SOCK = os.path.join(match.group(1), os.environ['STY'])
142
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]
147
148 if SOCK:
149 set_timer()
150 w.hook_config("plugins.var.python." + SCRIPT_NAME + ".*",
151 "screen_away_config_cb", "")