]> git.rmz.io Git - dotfiles.git/blob - weechat/python/allquery.py
zsh: make sure to set bindings at the beginning
[dotfiles.git] / weechat / python / allquery.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2011-2013 by F. Besser <fbesser@gmail.com>
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 #
19 # History:
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
25 #
26 # 2011-09-05, F. Besser <fbesser@gmail.com>:
27 # version 0.1: script created
28 #
29 # Development is on:
30 # https://github.com/fbesser/weechat_scripts
31 #
32 # (this script requires WeeChat 0.3.0 or newer)
33 #
34
35
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"
41
42 SCRIPT_COMMAND = "allquery"
43
44 import_ok = True
45
46 try:
47 import weechat
48 except ImportError:
49 print('This script must be run under WeeChat.')
50 print('Get WeeChat now at: http://www.weechat.org/')
51 import_ok = False
52
53 try:
54 import re
55 except ImportError, message:
56 print('Missing package(s) for %s: %s' % (SCRIPT_NAME, message))
57 import_ok = False
58
59
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(",")
64 return arguments
65
66 def allquery_command_cb(data, buffer, args):
67 """ Callback for /allquery command """
68 args = args.strip()
69 if args == "":
70 weechat.command("", "/help %s" % SCRIPT_COMMAND)
71 return weechat.WEECHAT_RC_OK
72 argv = args.split(" ")
73
74 exclude_nick = None
75 current_server = None
76
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", "")
81 args = args.lstrip()
82 argv.remove("-current")
83
84 # search for "-exclude" in arguments
85 i = 0
86 for entry in argv[0:]:
87 if entry.startswith("-exclude="):
88 exclude_nick = make_list(argv[i])
89 command = " ".join(argv[i+1::])
90 break
91 i +=1
92 else:
93 command = args
94
95 # no command found.
96 if not command:
97 return weechat.WEECHAT_RC_OK
98
99 if not command.startswith("/"):
100 command = "/%s" % command
101
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)
113 else:
114 exclude_nick_and_server(ptr,query,server,exclude_nick,execute_command)
115 weechat.infolist_free(infolist)
116 return weechat.WEECHAT_RC_OK
117
118
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)
124 else:
125 weechat.command(ptr, execute_command)
126
127
128 if __name__ == '__main__' and import_ok:
129 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
130 SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
131
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'
138 'Examples:\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',
149 '%(commands)',
150 'allquery_command_cb', '')