]>
git.rmz.io Git - dotfiles.git/blob - weechat/python/allquery.py
e789bd6a2a86c334e77e9f21f438f278b971fcd6
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2011-2013 by F. Besser <fbesser@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # 2013-09-01, nils_2@freenode.#weechat:
21 # version 0.2: add support of servername for "-exclude"
22 # : make script behave like /allchan and /allserver command
23 # : add function "-current"
24 # : case-insensitive search for query/server
26 # 2011-09-05, F. Besser <fbesser@gmail.com>:
27 # version 0.1: script created
30 # https://github.com/fbesser/weechat_scripts
32 # (this script requires WeeChat 0.3.0 or newer)
36 SCRIPT_NAME
= "allquery"
37 SCRIPT_AUTHOR
= "fbesser"
38 SCRIPT_VERSION
= "0.2"
39 SCRIPT_LICENSE
= "GPL3"
40 SCRIPT_DESC
= "Executes command on all irc query buffer"
42 SCRIPT_COMMAND
= "allquery"
49 print('This script must be run under WeeChat.')
50 print('Get WeeChat now at: http://www.weechat.org/')
55 except ImportError, message
:
56 print('Missing package(s) for %s: %s' % (SCRIPT_NAME
, message
))
60 def make_list(argument
):
61 """ Make a list out of argument string of format -argument=value0,value1"""
62 arglist
= argument
.lower().split("=", 1)
63 arguments
= arglist
[1].split(",")
66 def allquery_command_cb(data
, buffer, args
):
67 """ Callback for /allquery command """
70 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
71 return weechat
.WEECHAT_RC_OK
72 argv
= args
.split(" ")
77 if '-current' in argv
:
78 current_server
= weechat
.buffer_get_string(weechat
.current_buffer(), "localvar_server")
79 # remove "-current" + whitespace from argumentlist
80 args
= args
.replace("-current", "")
82 argv
.remove("-current")
84 # search for "-exclude" in arguments
86 for entry
in argv
[0:]:
87 if entry
.startswith("-exclude="):
88 exclude_nick
= make_list(argv
[i
])
89 command
= " ".join(argv
[i
+1::])
97 return weechat
.WEECHAT_RC_OK
99 if not command
.startswith("/"):
100 command
= "/%s" % command
102 infolist
= weechat
.infolist_get("buffer", "", "")
103 while weechat
.infolist_next(infolist
):
104 if weechat
.infolist_string(infolist
, "plugin_name") == "irc":
105 ptr
= weechat
.infolist_pointer(infolist
, "pointer")
106 server
= weechat
.buffer_get_string(ptr
, "localvar_server")
107 query
= weechat
.buffer_get_string(ptr
, "localvar_channel")
108 execute_command
= re
.sub(r
'\$nick', query
, command
)
109 if weechat
.buffer_get_string(ptr
, "localvar_type") == "private":
110 if current_server
is not None:
111 if server
== current_server
:
112 exclude_nick_and_server(ptr
,query
,server
,exclude_nick
,execute_command
)
114 exclude_nick_and_server(ptr
,query
,server
,exclude_nick
,execute_command
)
115 weechat
.infolist_free(infolist
)
116 return weechat
.WEECHAT_RC_OK
119 def exclude_nick_and_server(ptr
, query
, server
, exclude_nick
, execute_command
):
120 server
= "%s.*" % server
# servername + ".*"
121 if exclude_nick
is not None:
122 if not query
.lower() in exclude_nick
and not server
.lower() in exclude_nick
:
123 weechat
.command(ptr
, execute_command
)
125 weechat
.command(ptr
, execute_command
)
128 if __name__
== '__main__' and import_ok
:
129 if weechat
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
,
130 SCRIPT_LICENSE
, SCRIPT_DESC
, "", ""):
132 weechat
.hook_command(SCRIPT_COMMAND
, SCRIPT_DESC
,
133 '[-current] [-exclude=<nick|server>[,<nick2|server>...]] <command> [<arguments>]',
134 ' -current: execute command for query of current server only\n'
135 ' -exclude: exclude some querys and/or server from executed command\n'
136 ' command: command executed in query buffers\n'
137 ' arguments: arguments for command (special variables $nick will be replaced by its value)\n\n'
139 ' close all query buffers:\n'
140 ' /' + SCRIPT_COMMAND
+ ' buffer close\n'
141 ' close all query buffers, but don\'t close FlashCode:\n'
142 ' /' + SCRIPT_COMMAND
+ ' -exclude=FlashCode buffer close\n'
143 ' close all query buffers, except for server freenode:\n'
144 ' /' + SCRIPT_COMMAND
+ ' -exclude=freenode.* buffer close\n'
145 ' msg to all query buffers:\n'
146 ' /' + SCRIPT_COMMAND
+ ' say Hello\n'
147 ' notice to all query buffers:\n'
148 ' /' + SCRIPT_COMMAND
+ ' notice $nick Hello',
150 'allquery_command_cb', '')