Script: autojoin_on_invite.py

Auto joins channels when invited.
Author: xt — Version: 0.10.1 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: irc, invite, autojoin, py2, py3
Added: 2009-10-28 — Updated: 2026-09-19

Download GitHub Repository

 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
# SPDX-FileCopyrightText: 2009 Tor Hveem <xt@bash.no>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import weechat as w
import re

SCRIPT_NAME    = "autojoin_on_invite"
SCRIPT_AUTHOR  = "xt <xt@bash.no>"
SCRIPT_VERSION = "0.10.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Auto joins channels when invited"

# script options
settings = {
        'whitelist_nicks': '', # comma separated list of nicks,
                               # overrides ignore_nicks
        'whitelist_channels': '', # comma separated list of channels,
                               # overrides ignore_channels
        'ignore_nicks': '', # comma separated list of nicks
                            #that we will not accept auto invite from
        'ignore_channels': '', # comma separated list of channels to not join
        'autojoin_key': 'on', # use channel keys from server's autojoin list
}

if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
                    SCRIPT_DESC, "", ""):
    for option, default_value in settings.items():
        if not w.config_is_set_plugin(option):
            w.config_set_plugin(option, default_value)

    w.hook_signal('*,irc_in2_invite', 'invite_cb', '')

def join(server, channel):
    key = None

    if w.config_string_to_boolean(w.config_get_plugin('autojoin_key')):
        autojoin = w.config_string(w.config_get('irc.server.%s.autojoin' % server)).split(' ', 1)

        if len(autojoin) > 1: # any keys specified
            autojoin_keys = dict(zip(autojoin[0].split(','), autojoin[1].split(',')))
            key = autojoin_keys.get(channel) # defaults to None when not set

    if key:
        w.command('', '/quote -server %s JOIN %s %s' % (server, channel, key))
    else:
        w.command('', '/quote -server %s JOIN %s' % (server, channel))

def invite_cb(data, signal, signal_data):
    server = signal.split(',')[0] # EFNet,irc_in_INVITE
    channel = signal_data.split()[-1].lstrip(':') # :nick!ident@host.name INVITE yournick :#channel
    from_nick = ''
    SearchStr = r'(?:\@.*)?:(?P<nick>.+)!' #@time=2022-03-02T19:00:30.041Z :XX-XXXX!~XX-XXXX@xx.xxxx INVITE yournick :#xxxx-xxx (works also when no message-tag is present)
    from_nick = re.search(SearchStr, signal_data).groups()[0]

    if len(w.config_get_plugin('whitelist_nicks')) > 0 and len(w.config_get_plugin('whitelist_channels')) > 0: # if there's two whitelists, accept both
        if from_nick in w.config_get_plugin('whitelist_nicks').split(',') or channel in w.config_get_plugin('whitelist_channels').split(','):
            w.prnt('', 'Automatically joining %s on server %s, invitation from %s (whitelist).' \
                %(channel, server, from_nick))
            join(server, channel)
        else:
            w.prnt('', 'Ignoring invite from %s to channel %s. Neither inviter nor channel in whitelist.' %(from_nick, channel))

    elif len(w.config_get_plugin('whitelist_nicks')) > 0: # if there's a whitelist, accept nicks in it
        if from_nick in w.config_get_plugin('whitelist_nicks').split(','):
            w.prnt('', 'Automatically joining %s on server %s, invitation from %s (whitelist).' \
                %(channel, server, from_nick))
            join(server, channel)
        else:
            w.prnt('', 'Ignoring invite from %s to channel %s. Inviter not in whitelist.' %(from_nick, channel))

    elif len(w.config_get_plugin('whitelist_channels')) > 0: # if there's a whitelist, accept channels in it
        if channel in w.config_get_plugin('whitelist_channels').split(','):
            w.prnt('', 'Automatically joining %s on server %s, invitation from %s (whitelist).' \
                %(channel, server, from_nick))
            join(server, channel)
        else:
            w.prnt('', 'Ignoring invite from %s to channel %s. Channel not in whitelist.' %(from_nick, channel))

    else: # use the ignore lists to make the decision
        if from_nick in w.config_get_plugin('ignore_nicks').split(','):
            w.prnt('', 'Ignoring invite from %s to channel %s. Invite from ignored inviter.' %(from_nick, channel))
        elif channel in w.config_get_plugin('ignore_channels').split(','):
            w.prnt('', 'Ignoring invite from %s to channel %s. Invite to ignored channel.' %(from_nick, channel))
        else:
            w.prnt('', 'Automatically joining %s on server %s, invitation from %s.' \
                %(channel, server, from_nick))
            join(server, channel)

    return w.WEECHAT_RC_OK