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