Home

Script: force_nick.py

Force nick change on channels which disallow it. (for WeeChat ≥ 0.3.0)
Author: Simmo Saan, version 0.4, GPL3 — added: 2017-02-04
  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 by Simmo Saan <simmo.saan@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:
#
# 2015-08-07, Simmo Saan <simmo.saan@gmail.com>
#   version 0.4: option for invite-only channels
# 2015-08-07, Simmo Saan <simmo.saan@gmail.com>
#   version 0.3: options to control risky channel cycling
# 2015-07-03, Simmo Saan <simmo.saan@gmail.com>
#   version 0.2: ability to rejoin passworded channels
# 2015-07-01, Simmo Saan <simmo.saan@gmail.com>
#   version 0.1: initial script
#

"""
Force nick change on channels which disallow it
"""

from __future__ import print_function

SCRIPT_NAME = "force_nick"
SCRIPT_AUTHOR = "Simmo Saan <simmo.saan@gmail.com>"
SCRIPT_VERSION = "0.4"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Force nick change on channels which disallow it"

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

SETTINGS = {
    "cycle_detach": (
        "off",
        "automatically cycle channels which are not open in WeeChat"),
    "cycle_key": (
        "on",
        "automatically cycle passworded channels (+k)"),
    "cycle_invite": (
        "off",
        "automatically cycle invite-only channels (+i)")
}

import re

servers = {}

def parse_message(signal_data):
    hashtable = weechat.info_get_hashtable("irc_message_parse", {"message": signal_data})

    # parse arguments string into usable pieces
    args = hashtable["arguments"].split(":", 1)
    hashtable["args"] = args[0].split()
    if len(args) > 1:
        hashtable["text"] = args[1]

    return hashtable

def channel_block(server, channel):
    fail = None
    config_cycle = lambda opt: weechat.config_string_to_boolean(weechat.config_get_plugin("cycle_%s" % opt))

    channels = weechat.infolist_get("irc_channel", "", "%s,%s" % (server, channel))
    if weechat.infolist_next(channels):
        modes = weechat.infolist_string(channels, "modes")

        if not config_cycle("key") and weechat.infolist_string(channels, "key") != "":
            fail = "cycle_key"
        elif not config_cycle("invite") and "i" in modes:
            fail = "cycle_invite"
    elif not config_cycle("detach"):
        fail = "cycle_detach"

    weechat.infolist_free(channels)

    if fail:
        weechat.prnt("", "%s: won't automatically cycle %s.%s: %s" % (SCRIPT_NAME, server, channel, fail))
    else:
        servers[server]["channels"].append(channel)
        buffer = weechat.buffer_search("irc", server)
        weechat.command(buffer, "/part %s" % channel)
        weechat.command(buffer, "/nick %s" % servers[server]["nick"])

def nick_out_cb(data, signal, signal_data):
    server = signal.split(",")[0]
    parsed = parse_message(signal_data)
    nick = parsed["args"][0]

    if server not in servers: # initialize new nickchange
        servers[server] = {}
        servers[server]["channels"] = []

    servers[server]["nick"] = nick

    return weechat.WEECHAT_RC_OK

def nick_in_cb(data, signal, signal_data):
    server = signal.split(",")[0]
    parsed = parse_message(signal_data)
    mynick = weechat.info_get("irc_nick", server)

    if parsed["nick"] == mynick: # nick change worked
        channels = weechat.infolist_get("irc_channel", "", server)
        keys = {}
        while weechat.infolist_next(channels):
            keys[weechat.infolist_string(channels, "name")] = weechat.infolist_string(channels, "key")

        buffer = weechat.buffer_search("irc", server)
        for channel in servers[server]["channels"]:
            weechat.command(buffer, "/join -noswitch %s %s" % (channel, keys.get(channel, "")))

        weechat.infolist_free(channels)

        servers.pop(server)

    return weechat.WEECHAT_RC_OK

def unreal_cb(data, signal, signal_data):
    server = signal.split(",")[0]
    parsed = parse_message(signal_data)

    match = re.match(r"Can not change nickname while on (#\w+) \(\+N\)", parsed["text"])
    if match:
        channel = match.group(1)
        channel_block(server, channel)

    return weechat.WEECHAT_RC_OK

def freenode_cb(data, signal, signal_data):
    server = signal.split(",")[0]
    parsed = parse_message(signal_data)

    channel_block(server, parsed["args"][2])

    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_signal("*,irc_out1_nick", "nick_out_cb", "")
        weechat.hook_signal("*,irc_in_nick", "nick_in_cb", "")
        weechat.hook_signal("*,irc_in_447", "unreal_cb", "")
        weechat.hook_signal("*,irc_in_435", "freenode_cb", "")

        for option, value in SETTINGS.items():
            if not weechat.config_is_set_plugin(option):
                weechat.config_set_plugin(option, value[0])

            weechat.config_set_desc_plugin(option, "%s (default: \"%s\")" % (value[1], value[0]))