Last active
August 29, 2015 14:25
-
-
Save 0xallie/8d85f70a82f27e4b3053 to your computer and use it in GitHub Desktop.
WeeChat auto-ghost script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2015, nyuszika7h <nyuszika7h@openmailbox.org> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import fnmatch | |
import string | |
import weechat | |
SCRIPT_NAME = "autoghost" | |
SCRIPT_AUTHOR = "nyuszika7h" | |
SCRIPT_VERSION = "0.2.0" | |
SCRIPT_LICENSE = "MIT" | |
SCRIPT_DESC = "Automatically ghost specific nicks using NickServ" | |
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, | |
SCRIPT_DESC, "", "") | |
opts = { | |
"servers": "", | |
"nicks": "", | |
"trusted_hosts": "", | |
"nickserv_command": "/msg NickServ ghost %s", | |
"debug": "off", | |
} | |
for option, default_value in opts.items(): | |
if weechat.config_is_set_plugin(option): | |
opts[option] = weechat.config_get_plugin(option) | |
else: | |
weechat.config_set_plugin(option, default_value) | |
def debug(buf, message): | |
if weechat.config_get_plugin("debug") == "on": | |
weechat.prnt(buf, "[%s] debug: %s" % (SCRIPT_NAME, message)) | |
def log(buf, message, notify=False): | |
if notify: | |
tags = "notify_private" | |
else: | |
tags = "notify_message" | |
weechat.prnt_date_tags(buf, 0, tags, "[%s] %s" % (SCRIPT_NAME, message)) | |
def irc_lower(s): | |
return s.lower().translate(string.maketrans('[\\]^', '{|}~')) | |
def config_cb(data, option, value): | |
option = option[20 + len(SCRIPT_NAME):] # strip plugins.var.python.SCRIPT_NAME | |
debug("", "option name: %s" % option) | |
if option == "nicks": | |
for server in opts["servers"].split(","): | |
sb = weechat.info_get("irc_buffer", server) | |
old_nicks = [] | |
for nick in opts["nicks"].split(","): | |
if "/" in nick: | |
if nick.startswith(server + "/"): | |
old_nicks.append(nick.split("/")[1]) | |
else: | |
old_nicks.append(nick) | |
raw_nicks = value.split(",") | |
nicks = [] | |
for nick in raw_nicks: | |
if "/" in nick: | |
if nick.startswith(server + "/"): | |
nicks.append(nick.split("/")[1]) | |
else: | |
nicks.append(nick) | |
to_add = ",".join(x for x in nicks if x not in old_nicks) | |
if to_add: | |
debug(sb, "nicks changed; adding nicks to monitor list: %s" % to_add) | |
weechat.command(sb, "/quote MONITOR + %s" % to_add) | |
else: | |
debug(sb, "nicks changed; no new nicks to add") | |
notify_list = weechat.config_string(weechat.config_get("irc.server.%s.notify" % server)).split(",") | |
to_remove = ",".join(x for x in old_nicks if x not in nicks and x not in notify_list) | |
if to_remove: | |
debug(sb, "nicks changed; removing old nicks from monitor list: %s" % to_remove) | |
weechat.command(sb, "/quote MONITOR - %s" % to_remove) | |
else: | |
debug(sb, "nicks changed; no old nicks to remove") | |
elif option == "servers": | |
added_servers = [x for x in value.split(",") if x not in opts["servers"]] | |
if added_servers: | |
for server in added_servers: | |
sb = weechat.info_get("irc_buffer", server) | |
raw_nicks = opts["nicks"].split(",") | |
nicks = [] | |
for nick in raw_nicks: | |
if "/" in nick: | |
if nick.startswith(server + "/"): | |
nicks.append(nick.split("/")[1]) | |
else: | |
nicks.append(nick) | |
to_add = ",".join(nicks) | |
if to_add: | |
debug(sb, "server added; adding nicks to monitor list: %s" % to_add) | |
weechat.command(sb, "/quote MONITOR + %s" % to_add) | |
else: | |
debug(sb, "server added; no new nicks to add") | |
else: | |
debug("", "servers changed; no servers added") | |
removed_servers = [x for x in opts["servers"].split(",") if x not in value.split(",")] | |
if removed_servers: | |
for server in removed_servers: | |
sb = weechat.info_get("irc_buffer", server) | |
raw_nicks = opts["nicks"].split(",") | |
nicks = [] | |
for nick in raw_nicks: | |
if "/" in nick: | |
if nick.startswith(server + "/"): | |
nicks.append(nick.split("/")[1]) | |
else: | |
nicks.append(nick) | |
notify_list = weechat.config_string(weechat.config_get("irc.server.%s.notify" % server)).split(",") | |
to_remove = ",".join(x for x in nicks if x not in notify_list) | |
if to_remove: | |
debug(sb, "server removed; removing nicks from monitor list: %s" % to_remove) | |
weechat.command(sb, "/quote MONITOR - %s" % to_remove) | |
else: | |
debug(sb, "server removed; no nicks to remove") | |
else: | |
debug("", "servers changed; no servers removed") | |
opts[option] = value | |
return weechat.WEECHAT_RC_OK | |
weechat.hook_config("plugins.var.python.%s.*" % SCRIPT_NAME, "config_cb", "") | |
def rpl_mononline_cb(data, signal, signal_data): | |
# :wilhelm.freenode.net 730 * :lyoko1!nyuszika7h@cadoth.net | |
server = signal.split(",")[0] | |
sb = weechat.info_get("irc_buffer", server) | |
hostmask = signal_data.split()[3][1:] | |
(nick, userhost) = hostmask.split("!") | |
nick = irc_lower(nick) | |
servers = weechat.config_get_plugin("servers").split(",") | |
nicks = irc_lower(weechat.config_get_plugin("nicks")).split(",") | |
trusted_hosts = weechat.config_get_plugin("trusted_hosts").split(",") | |
if server in servers: | |
debug(sb, "%s - script is enabled for current server" % hostmask) | |
else: | |
debug(sb, "%s - script is NOT enabled for current server; aborting" % hostmask) | |
return weechat.WEECHAT_RC_OK | |
matched = False | |
for n in nicks: | |
if n.endswith("/" + nick): | |
(sv, n) = n.split("/") | |
if sv == server: | |
debug(sb, "%s - constrained to current server" % hostmask) | |
matched = True | |
else: | |
debug(sb, "%s - constrained to different server (%s); aborting" % (hostmask, sv)) | |
break | |
if nick in nicks or matched: | |
debug(sb, "%s - nick is in list" % hostmask) | |
for uh in trusted_hosts: | |
if fnmatch.fnmatch(userhost, uh): | |
debug(sb, "%s - userhost is in trusted hosts; aborting" % hostmask) | |
return weechat.WEECHAT_RC_OK | |
else: | |
debug(sb, "%s - userhost is NOT in trusted hosts" % hostmask) | |
else: | |
debug(sb, "%s - nick is NOT in list; aborting" % hostmask) | |
return weechat.WEECHAT_RC_OK | |
# If we reached this point, then it's time to ghost the nick. | |
log(sb, "ghosting %s (%s)" % (nick, userhost), notify=True) | |
weechat.command(sb, opts["nickserv_command"] % nick) | |
return weechat.WEECHAT_RC_OK | |
weechat.hook_signal("*,irc_in2_730", "rpl_mononline_cb", "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment