|  | 
|  | 1 | +# SPDX-FileCopyrightText: 2025 D. Bohdan <[email protected]> | 
|  | 2 | +# SPDX-License-Identifier: MIT | 
|  | 3 | +# | 
|  | 4 | +# Censor messages by nick and text without affecting logs using color. | 
|  | 5 | +# This is an alternative to /ignore and triggers that modify logs. | 
|  | 6 | +# | 
|  | 7 | +# --------------------------------------------------------------------------- | 
|  | 8 | +# INSTALLATION & USAGE | 
|  | 9 | +# --------------------------------------------------------------------------- | 
|  | 10 | +# 1. Save this file with WeeChat Python scripts: | 
|  | 11 | +#      ~/.local/share/weechat/python/censor.py | 
|  | 12 | +# | 
|  | 13 | +# 2. Edit the configuration section below to suit your needs: | 
|  | 14 | +#      - Add nick and/or text regexes to CENSORED_NICKS and CENSORED_TEXT. | 
|  | 15 | +#      - Change CENSOR_COLOR if desired. | 
|  | 16 | +# | 
|  | 17 | +# 3. Load it in WeeChat: | 
|  | 18 | +#      /python load censor.py | 
|  | 19 | +# | 
|  | 20 | +# This script only modifies the display of censored messages. | 
|  | 21 | +# The original messages still exists in the buffer and the logs. | 
|  | 22 | +# | 
|  | 23 | +# Example: | 
|  | 24 | +#   CENSORED_NICKS = [r"^AnnoyingUser", r"troll\d+", r"bot$"] | 
|  | 25 | +#   CENSOR_COLOR = "darkgray" | 
|  | 26 | +# | 
|  | 27 | +# --------------------------------------------------------------------------- | 
|  | 28 | + | 
|  | 29 | +import re | 
|  | 30 | + | 
|  | 31 | +import_ok = True | 
|  | 32 | +try: | 
|  | 33 | +    import weechat | 
|  | 34 | +except ImportError: | 
|  | 35 | +    print("This script must be run under WeeChat.") | 
|  | 36 | +    print("Get WeeChat now at: https://weechat.org/") | 
|  | 37 | +    import_ok = False | 
|  | 38 | + | 
|  | 39 | +SCRIPT_NAME = "censor" | 
|  | 40 | +SCRIPT_AUTHOR = "D. Bohdan <[email protected]>" | 
|  | 41 | +SCRIPT_VERSION = "0.2.0" | 
|  | 42 | +SCRIPT_LICENSE = "MIT" | 
|  | 43 | +SCRIPT_DESC = ( | 
|  | 44 | +    "Censor messages by nick and text without affecting logs using color." | 
|  | 45 | +) | 
|  | 46 | + | 
|  | 47 | +# --------------------------------------------------------------------------- | 
|  | 48 | +# CONFIGURATION | 
|  | 49 | +# --------------------------------------------------------------------------- | 
|  | 50 | + | 
|  | 51 | +CENSORED_NICKS = [ | 
|  | 52 | +    # r"troll\d+", | 
|  | 53 | +] | 
|  | 54 | +CENSORED_TEXT = [ | 
|  | 55 | +    # r"hello\?{5}", | 
|  | 56 | +] | 
|  | 57 | + | 
|  | 58 | +CENSOR_COLOR = "darkgray:darkgray" | 
|  | 59 | + | 
|  | 60 | +# --------------------------------------------------------------------------- | 
|  | 61 | +# IMPLEMENTATION | 
|  | 62 | +# --------------------------------------------------------------------------- | 
|  | 63 | + | 
|  | 64 | + | 
|  | 65 | +def tags_nick(tags): | 
|  | 66 | +    """Extract nick from tags.""" | 
|  | 67 | +    if not tags: | 
|  | 68 | +        return "" | 
|  | 69 | + | 
|  | 70 | +    for tag in tags.split(","): | 
|  | 71 | +        if tag.startswith("nick_"): | 
|  | 72 | +            return tag[5:] | 
|  | 73 | + | 
|  | 74 | +    return "" | 
|  | 75 | + | 
|  | 76 | + | 
|  | 77 | +def censor_line(line): | 
|  | 78 | +    color = weechat.color(CENSOR_COLOR) | 
|  | 79 | +    reset = weechat.color("reset") | 
|  | 80 | +    line["message"] = f"{color}{line['message']}{reset}" | 
|  | 81 | + | 
|  | 82 | + | 
|  | 83 | +def censor_line_cb(_data, line): | 
|  | 84 | +    """hook_line callback: style censored message text.""" | 
|  | 85 | +    tags = line.get("tags", "") | 
|  | 86 | +    nick = tags_nick(tags) | 
|  | 87 | + | 
|  | 88 | +    if not nick: | 
|  | 89 | +        return line | 
|  | 90 | + | 
|  | 91 | +    for pat in CENSORED_NICKS: | 
|  | 92 | +        if re.search(pat, nick, flags=re.IGNORECASE): | 
|  | 93 | +            censor_line(line) | 
|  | 94 | +            break | 
|  | 95 | + | 
|  | 96 | +    for pat in CENSORED_TEXT: | 
|  | 97 | +        if re.search(pat, line["message"], flags=re.IGNORECASE): | 
|  | 98 | +            censor_line(line) | 
|  | 99 | +            break | 
|  | 100 | + | 
|  | 101 | +    return line | 
|  | 102 | + | 
|  | 103 | + | 
|  | 104 | +if __name__ == "__main__" and import_ok: | 
|  | 105 | +    weechat.register( | 
|  | 106 | +        SCRIPT_NAME, | 
|  | 107 | +        SCRIPT_AUTHOR, | 
|  | 108 | +        SCRIPT_VERSION, | 
|  | 109 | +        SCRIPT_LICENSE, | 
|  | 110 | +        SCRIPT_DESC, | 
|  | 111 | +        "", | 
|  | 112 | +        "", | 
|  | 113 | +    ) | 
|  | 114 | + | 
|  | 115 | +    weechat.hook_line("", "", "", "censor_line_cb", "") | 
0 commit comments