archive- Random tools & helpful resources for IRC |
git clone git://git.acid.vegas/archive.git |
Log | Files | Refs | Archive |
colo.py (5067B)
1 # -*- coding: utf-8 -*- 2 # 3 # Released into the Public Domain 4 5 """colo: make your chats noticable""" 6 7 import random 8 import re 9 import weechat 10 11 SCRIPT_NAME = "colo" 12 SCRIPT_AUTHOR = "The Krusty Krab <wowaname@volatile.ch>" 13 SCRIPT_VERSION = "2.2" 14 SCRIPT_LICENSE = "Public domain" 15 SCRIPT_DESC = "Makes your chats noticable" 16 17 # script options 18 settings = { 19 "fmt": ( 20 "%c13♥ %0%s%o %c13♥", 21 "Format string for text. %0 - %9 are different colours, %s is text, " 22 "%c %b %u %r %o are ^C ^B ^U ^R ^O respectively, and %% is a literal " 23 "percent sign. %0 is the primary colour that should be used with %s.", 24 ), 25 "fgs": ( 26 "04,05,06,13", 27 "Colour codes to cycle for the foreground. " 28 "Leave blank for no foreground colours.", 29 ), 30 "bgs": ( 31 "", 32 "Colour codes to cycle for the background. " 33 "Leave blank for no background colours.", 34 ), 35 "ignore_buffers": ( 36 "bitlbee.*,scripts", 37 "List of buffers to ignore. Glob matches unless " 38 "you prefix the name with 're:'.", 39 ), 40 "whitelist_buffers": ( 41 "", 42 "List of buffers to whitelist. Glob match unless " 43 "you prefix the name with 're:'. Useful with " 44 "ignore_buffers = \"*\"", 45 ), 46 "whitelist_cmds": ( 47 "me,amsg,say", 48 "Commands to colour.", 49 ), 50 "profiles": ( 51 "> greentext,! alert", 52 "List of prefix/profile pairs. If you type one of " 53 "these prefixes at the beginning of your message, " 54 "the options will switch to (profile)_pre, " 55 "(profile)_suf, (profile)_fgs, and (profile)_bgs. ", 56 ), 57 "greentext_fmt": "%c3> %s", 58 "alert_fmt": "%c1,8/!\\%c8,1 %s %o%c1,8/!\\" 59 } 60 61 62 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, 63 SCRIPT_DESC, "", ""): 64 for opt, val in settings.iteritems(): 65 setting, desc = val if type(val) == tuple else (val, "") 66 if desc: weechat.config_set_desc_plugin(opt, desc) 67 if weechat.config_is_set_plugin(opt): continue 68 weechat.config_set_plugin(opt, setting) 69 70 weechat.hook_modifier("input_text_for_buffer", "cb_colo", "") 71 72 # prevent looping 73 nest = False 74 75 def glob_match (haystack, needle): 76 return re.search("^%s$" % 77 re.escape(haystack).replace(r"\?", ".").replace(r"\*", ".*?"), 78 needle) 79 80 def is_command (string): 81 return (string.startswith("/") and not string.startswith("/ ") and 82 string != "/" and string.split(" ", 1)[0].split("\n", 1)[0].find("/", 1) 83 < 0) 84 85 def cb_colo (data, mod, buf, input): 86 global nest 87 if nest: 88 nest = False 89 # return input 90 buffer_name = weechat.buffer_get_string(buf, "name").lower() 91 output = "" 92 profile = "" 93 94 for pattern in weechat.config_get_plugin("whitelist_buffers").lower().split(","): 95 if (pattern.startswith("re:") and 96 re.search(pattern[3:], buffer_name)) or glob_match(pattern, buffer_name): 97 break 98 else: 99 for pattern in weechat.config_get_plugin("ignore_buffers").lower().split(","): 100 if (pattern.startswith("re:") and 101 re.search(pattern[3:], buffer_name)) or glob_match(pattern, buffer_name): 102 return input 103 104 if not input: 105 return input 106 107 if is_command(input): 108 for cmd in weechat.config_get_plugin("whitelist_cmds").lower().split(","): 109 if not input.startswith("/%s " % cmd): continue 110 output = "/%s " % cmd 111 input = input.split(" ", 1)[1] if " " in input else "" 112 break 113 else: 114 # XXX 115 return input.replace('\r','') 116 117 if input.startswith("//"): input = input[1:] 118 119 for profile_pairs in weechat.config_get_plugin("profiles").split(","): 120 prefix, name = profile_pairs.split() 121 if not input.startswith("%s " % prefix): continue 122 profile = "%s_" % name 123 input = input.split(" ",1)[1] if " " in input else "" 124 for opt in ("fmt", "fgs", "bgs"): 125 if weechat.config_is_set_plugin(profile + opt): continue 126 weechat.config_set_plugin(profile + opt, "") 127 break 128 129 fgs = weechat.config_get_plugin("%sfgs" % profile).split(",") 130 bgs = weechat.config_get_plugin("%sbgs" % profile).split(",") 131 fmt = weechat.config_get_plugin("%sfmt" % profile).split("%%") 132 133 for i in xrange(len(fmt)): 134 fmt[i] = fmt[i].replace("%c", "\x03").replace("%b", 135 "\x02").replace("%u", "\x1f").replace("%r", 136 "\x16").replace("%o", "\x0f") 137 if fgs == [""] and bgs == [""]: continue 138 for j in xrange(10): 139 base = "\x03%s%s%s" % ( 140 random.choice(fgs), 141 "," if bgs != [""] else "", 142 random.choice(bgs), 143 ) 144 fmt[i] = fmt[i].replace("%%%d" % j, base) 145 if j: continue 146 input = re.sub( 147 "\x03([^0-9])", 148 "\x03%s\\1" % base, 149 input.replace("\x0f","\x0f%s" % base)) 150 151 fmt = "%".join(fmt) 152 nest = is_command(fmt) 153 servername = weechat.buffer_get_string(buf, "localvar_server") 154 iptr = weechat.infolist_get("irc_server", "", servername) 155 weechat.infolist_next(iptr) 156 long_lines = weechat.infolist_integer(iptr, "cap_long_lines") 157 weechat.infolist_free(iptr) 158 159 nicklen = weechat.info_get("irc_server_isupport_value", "%s,NICKLEN" % 160 servername) 161 if not nicklen: nicklen = 9 162 163 l = ((512 if long_lines else 0) + 409 - len(fmt) - int(nicklen)) 164 o = [] 165 for line in input.replace("\r", "\n").split("\n"): 166 if not line: continue 167 for i in xrange(0, len(line), l): 168 o.append(fmt.replace("%s", line[i:i+l].rstrip())) 169 170 return output + "\n".join(o)