File: allquery.py

package info (click to toggle)
weechat-scripts 20120603-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,804 kB
  • sloc: python: 23,181; perl: 16,968; ruby: 1,166; tcl: 199; sh: 8; makefile: 7
file content (116 lines) | stat: -rw-r--r-- 4,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf-8 -*-
#
# Copyright (c) 2011 by F. Besser <fbesser@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
#
# History:
# 2011-09-05, F. Besser <fbesser@gmail.com>:
#     version 0.1: script created
#
# Development is on:
# https://github.com/fbesser/weechat_scripts
#
# (this script requires WeeChat 0.3.0 or newer)
#


SCRIPT_NAME = "allquery"
SCRIPT_AUTHOR = "fbesser"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Executes command on all irc query buffer"

SCRIPT_COMMAND = "allquery"

import_ok = True

try:
    import weechat
except ImportError:
    print('This script must be run under WeeChat.')
    print('Get WeeChat now at: http://www.weechat.org/')
    import_ok = False

try:
    import re
except ImportError, message:
    print('Missing package(s) for %s: %s' % (SCRIPT_NAME, message))
    import_ok = False


def make_list(argument):
    """ Make a list out of argument string of format -argument=value0,value1"""
    arglist = argument.split("=", 1)
    arguments = arglist[1].split(",")
    return arguments

def allquery_command_cb(data, buffer, args):
    """ Callback for /allquery command """
    args = args.strip()
    if args == "":
        weechat.command("", "/help %s" % SCRIPT_COMMAND)
        return weechat.WEECHAT_RC_OK
    argv = args.split(" ")

    exclude_nick = None

    if argv[0].startswith("-exclude="):
        exclude_nick = make_list(argv[0])
        command = " ".join(argv[1::])
    else:
        command = args
    if not command.startswith("/"):
        weechat.command("", "/help %s" % SCRIPT_COMMAND)
        return weechat.WEECHAT_RC_OK

    infolist = weechat.infolist_get("buffer", "", "")
    while weechat.infolist_next(infolist):
        if weechat.infolist_string(infolist, "plugin_name") == "irc":
            ptr = weechat.infolist_pointer(infolist, "pointer")
            server = weechat.buffer_get_string(ptr, "localvar_server")
            query = weechat.buffer_get_string(ptr, "localvar_channel")
            execute_command = re.sub(r'\$nick', query, command)
            if weechat.buffer_get_string(ptr, "localvar_type") == "private":
                if exclude_nick is not None:
                    if not query in exclude_nick:
                        weechat.command(ptr, execute_command)
                else:
                    weechat.command(ptr, execute_command)
    weechat.infolist_free(infolist)
    return weechat.WEECHAT_RC_OK

if __name__ == '__main__' and import_ok:
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
                        SCRIPT_LICENSE, SCRIPT_DESC, "", ""):

        weechat.hook_command(SCRIPT_COMMAND, SCRIPT_DESC,
                             '[-exclude=<nick>,[<nick2>...]] command <arguments>',
                             '   -exclude=nick1,...: exclude some querys from executed command\n'
                             '              command: command executed in query buffers\n'
                             '                $nick: gets replaced by query buffer nick\n\n'
                             'Examples:\n'
                             '  close all query buffers:\n'
                             '    /' + SCRIPT_COMMAND + ' /buffer close\n'
                             '  close all query buffers, but don\'t close FlashCode:\n'
                             '    /' + SCRIPT_COMMAND + ' -exclude=FlashCode /buffer close\n'
                             '  msg to all query buffers:\n'
                             '    /' + SCRIPT_COMMAND + ' /say Hello\n'
                             '  notice to all query buffers:\n'
                             '    /' + SCRIPT_COMMAND + ' /notice $nick Hello',
                             '',
                             'allquery_command_cb', '')