weechat-opal/scripts/embellish.py

159 lines
4.3 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
#
# Released into the Public Domain
"""embellish: make your chats noticable"""
# v1.2 - wowaname
# whitelist_buffers added
# message won't be decorated if blank
# changed default ignore_buffers
# v1.1 - wowaname
# ignore_buffers has regex support
# additional profile support
# colours restored on ^C or ^O in message
# v1.0 - wowaname
# Initial release
import random
import re
import weechat
SCRIPT_NAME = "embellish"
SCRIPT_AUTHOR = "The Krusty Krab <wowaname@volatile.ch>"
SCRIPT_VERSION = "1.1"
SCRIPT_LICENSE = "Public domain"
SCRIPT_DESC = "Makes your chats noticable"
# script options
settings = {
"pre": (
"13♥",
"Text to add to the beginning of messages.",
),
"suf": (
"13♥",
"Text to add to the end of messages.",
),
"fgs": (
"04,05,06,13",
"Colour codes to cycle for the foreground. "
"Leave blank for no foreground colours.",
),
"bgs": (
"",
"Colour codes to cycle for the background. "
"Leave blank for no background colours.",
),
"ignore_buffers": (
"bitlbee.*,scripts",
"List of buffers to ignore. Glob matches unless "
"you prefix the name with 're:'.",
),
"whitelist_buffers": (
"",
"List of buffers to whitelist. Glob match unless "
"you prefix the name with 're:'. Useful with "
"ignore_buffers = \"*\"",
),
"whitelist_cmds": (
"me,amsg,say",
"Commands to embellish.",
),
"profiles": (
"> greentext,! alert",
"List of prefix/profile pairs. If you type one of "
"these prefixes at the beginning of your message, "
"the options will switch to (profile)_pre, "
"(profile)_suf, (profile)_fgs, and (profile)_bgs. ",
),
"greentext_pre": ">",
"greentext_suf": "",
"greentext_fgs": "03",
"greentext_bgs": "",
}
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, "", ""):
for opt, val in settings.iteritems():
setting, desc = val if type(val) == tuple else (val, "")
if desc: weechat.config_set_desc_plugin(opt, desc)
if weechat.config_is_set_plugin(opt): continue
weechat.config_set_plugin(opt, setting)
weechat.hook_command_run("/input return", "cb_embellish", "")
def glob_match (haystack, needle):
return re.search("^%s$" %
re.escape(haystack).replace(r"\?", ".").replace(r"\*", ".*?"),
needle)
def cb_embellish(data, buffer, command):
buffer_name = weechat.buffer_get_string(buffer, "name").lower()
output = ""
profile = ""
for pattern in weechat.config_get_plugin("whitelist_buffers").lower().split(","):
if (pattern.startswith("re:") and
re.search(pattern[3:], buffer_name)) or glob_match(pattern, buffer_name):
break
else:
for pattern in weechat.config_get_plugin("ignore_buffers").lower().split(","):
if (pattern.startswith("re:") and
re.search(pattern[3:], buffer_name)) or glob_match(pattern, buffer_name):
return weechat.WEECHAT_RC_OK
if command == "/input return":
input = weechat.buffer_get_string(buffer, "input").rstrip()
if not input:
return weechat.WEECHAT_RC_OK
if input.startswith("/"):
for cmd in weechat.config_get_plugin("whitelist_cmds").lower().split(","):
if not input.startswith("/%s " % cmd): continue
output = "/%s " % cmd
input = input.split(" ",1)[1] if " " in input else ""
break
else:
return weechat.WEECHAT_RC_OK
for profile_pairs in weechat.config_get_plugin("profiles").split(","):
prefix, name = profile_pairs.split()
if not input.startswith("%s " % prefix): continue
profile = "%s_" % name
input = input.split(" ",1)[1] if " " in input else ""
for opt in ("pre", "suf", "fgs", "bgs"):
if weechat.config_is_set_plugin(profile + opt): continue
weechat.config_set_plugin(profile + opt, "")
break
fgs = weechat.config_get_plugin("%sfgs" % profile).split(",")
bgs = weechat.config_get_plugin("%sbgs" % profile).split(",")
base = "%s%s%s" % (
random.choice(fgs),
"," if bgs != [""] else "",
random.choice(bgs),
) if fgs != [""] or bgs != [""] else ""
if base:
input = re.sub(
"\x03([^0-9])",
"\x03%s\\1" % base,
input.replace("\x0f","\x0f%s" % base).replace(
"\r","\r%s" % base).replace(
"\n","\n%s" % base)
)
output += "%s%s%s %s%s %s" % (
base,
weechat.config_get_plugin("%spre" % profile),
base,
input,
base,
weechat.config_get_plugin("%ssuf" % profile),
)
weechat.buffer_set(buffer, "input", output)
return weechat.WEECHAT_RC_OK