weechat-opal/scripts/colo.py

180 lines
5.1 KiB
Python
Raw Normal View History

2016-02-17 01:12:49 +00:00
# -*- coding: utf-8 -*-
#
# Released into the Public Domain
2016-08-09 06:55:47 +00:00
"""colo: make your chats noticable"""
2016-02-17 01:12:49 +00:00
2016-08-09 06:55:47 +00:00
# v2.0 - wowaname
# format strings instead of pre/suf
# rename
2016-07-26 00:29:52 +00:00
# v1.3 - wowaname
# changed hook so script doesn't modify the input buffer
2016-02-17 01:12:49 +00:00
# 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
2016-08-09 06:55:47 +00:00
SCRIPT_NAME = "colo"
2016-02-17 01:12:49 +00:00
SCRIPT_AUTHOR = "The Krusty Krab <wowaname@volatile.ch>"
2016-08-09 06:55:47 +00:00
SCRIPT_VERSION = "2.0"
2016-02-17 01:12:49 +00:00
SCRIPT_LICENSE = "Public domain"
SCRIPT_DESC = "Makes your chats noticable"
# script options
settings = {
2016-08-09 06:55:47 +00:00
"fmt": (
"%c13♥ %0%s%o %c13♥",
"Format string for text. %0 - %9 are different colours, %s is text, "
"%c %b %u %r %o are ^C ^B ^U ^R ^O respectively, and %% is a literal "
2016-08-12 11:01:14 +00:00
"percent sign. %0 is the primary colour that should be used with %s.",
2016-02-17 01:12:49 +00:00
),
"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",
2016-08-09 06:55:47 +00:00
"Commands to colour.",
2016-02-17 01:12:49 +00:00
),
"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. ",
),
2016-08-09 06:55:47 +00:00
"greentext_fmt": "%c3> %s",
2016-08-12 11:01:14 +00:00
"alert_fmt": "%c1,8/!\\%c8,1 %s %o%c1,8/!\\"
2016-02-17 01:12:49 +00:00
}
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)
2016-08-09 06:55:47 +00:00
weechat.hook_modifier("input_text_for_buffer", "cb_colo", "")
2016-02-17 01:12:49 +00:00
2016-08-12 11:01:14 +00:00
# prevent looping
nest = False
2016-02-17 01:12:49 +00:00
def glob_match (haystack, needle):
return re.search("^%s$" %
re.escape(haystack).replace(r"\?", ".").replace(r"\*", ".*?"),
needle)
2016-08-12 11:01:14 +00:00
def is_command (string):
return (string.startswith("/") and not string.startswith("/ ") and
string != "/" and string.split(" ", 1)[0].split("\n", 1)[0].find("/", 1)
< 0)
2016-08-09 06:55:47 +00:00
def cb_colo (data, mod, buf, input):
2016-08-12 11:01:14 +00:00
global nest
if nest:
nest = False
return input
2016-07-26 00:29:52 +00:00
buffer_name = weechat.buffer_get_string(buf, "name").lower()
2016-02-17 01:12:49 +00:00
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):
2016-07-26 00:29:52 +00:00
return input
2016-02-17 01:12:49 +00:00
if not input:
2016-07-26 00:29:52 +00:00
return input
2016-02-17 01:12:49 +00:00
2016-08-12 11:01:14 +00:00
if is_command(input):
2016-02-17 01:12:49 +00:00
for cmd in weechat.config_get_plugin("whitelist_cmds").lower().split(","):
if not input.startswith("/%s " % cmd): continue
output = "/%s " % cmd
2016-07-26 00:29:52 +00:00
input = input.split(" ", 1)[1] if " " in input else ""
2016-02-17 01:12:49 +00:00
break
else:
2016-07-26 00:29:52 +00:00
return input
if input.startswith("//"): input = input[1:]
2016-02-17 01:12:49 +00:00
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 ""
2016-08-09 06:55:47 +00:00
for opt in ("fmt", "fgs", "bgs"):
2016-02-17 01:12:49 +00:00
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(",")
2016-08-09 06:55:47 +00:00
fmt = weechat.config_get_plugin("%sfmt" % profile).split("%%")
for i in xrange(len(fmt)):
fmt[i] = fmt[i].replace("%c", "\x03").replace("%b",
"\x02").replace("%u", "\x1f").replace("%r",
"\x16").replace("%o", "\x0f")
if fgs == [""] and bgs == [""]: continue
for j in xrange(10):
base = "\x0f\x03%s%s%s" % (
random.choice(fgs),
"," if bgs != [""] else "",
random.choice(bgs),
)
fmt[i] = fmt[i].replace("%%%d" % j, base)
if j: continue
input = re.sub(
"\x03([^0-9])",
"\x03%s\\1" % base,
input.replace("\x0f","\x0f%s" % base))
fmt = "%".join(fmt)
2016-08-12 11:01:14 +00:00
nest = is_command(fmt)
2016-08-09 06:55:47 +00:00
l = (409 - len(fmt) -
int( weechat.info_get("irc_server_isupport_value", "%s,NICKLEN" %
weechat.buffer_get_string(buf, "localvar_server")) )
)
2016-07-26 00:29:52 +00:00
o = []
2016-08-12 11:01:14 +00:00
for line in input.replace("\r", "\n").split("\n"):
if not line: continue
2016-07-26 00:29:52 +00:00
for i in xrange(0, len(line), l):
2016-08-09 06:55:47 +00:00
o.append(fmt.replace("%s", line[i:i+l].rstrip()))
2016-07-26 00:29:52 +00:00
return output + "\n".join(o)