Script: greentext.py

Colorize imageboard-style text formatting.
Author: AGVXOV — Version: 1 — License: Public-domain
For WeeChat ≥ 0.3.0.
Tags: imageboard, color, py3
Added: 2023-11-11

Download GitHub Repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# # Weechat-Greentext
# Version:          v1
# Min WeeChat
#  version tested:  3.8
# Author:           AGVXOV
# Contact:          agvxov@gmail.com
# Project Home:     https://github.com/agvxov/weechat-greentext
# This script is Public Domain.
#
# Weechat script for applying imageboard formatting to messages. The following are supported:
#  + greentext
#  + purpletext
#  + redtext
# 
# Both inbound and outbound messages are colored.
# Since the coloring uses IRC color codes,
# outbound greentexting will be visible to both you and your friends.
#
import weechat
import re

SCRIPT_NAME    = "greentext"
SCRIPT_AUTHOR  = "AGVXOV"
SCRIPT_VERSION = "1"
SCRIPT_LICENSE = "PD"
SCRIPT_DESC    = "Colorize imageboard-style text formatting."

greentext_re  = re.compile("^\s*>.*$")
purpletext_re = re.compile("^\s*<.*$")
redtext_re    = re.compile("^.*(==.*==).*$")

COLOR_GREEN  = chr(3) + str(3)
COLOR_PURPLE = chr(3) + str(6)
COLOR_RED    = chr(3) + str(4) + chr(2)
COLOR_END    = chr(3) + str(0)

def hi_greentext(modifier, s):
	if greentext_re.search(s):
		if modifier == 'irc_out1_PRIVMSG':
			s = COLOR_GREEN + s
		else:
			s = weechat.color("green") + s
	return s

def hi_purpletext(modifier, s):
	if purpletext_re.search(s):
		if modifier == 'irc_out1_PRIVMSG':
			s = COLOR_PURPLE + s
		else:
			s = weechat.color("magenta") + s
	return s

def hi_redtext(modifier, s):
	if redtext_re.search(s):
		if modifier == 'irc_out1_PRIVMSG':
			m = redtext_re.search(s)
			s = s[:m.start(1)] + COLOR_RED + m.group(1) + COLOR_END + s[m.end(1):]
		else:
			s = weechat.color("red") + s
	return s

def hi(data, modifier, modifier_data, s):
	msg = weechat.info_get_hashtable('irc_message_parse', {'message': s})
	r = msg["text"]
	r = hi_greentext(modifier, r)
	r = hi_purpletext(modifier, r)
	r = hi_redtext(modifier, r)
	r = s[:-len(msg["text"])] + r
	return r
	
def main():
	if not weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
		return
	weechat.hook_modifier('irc_in2_privmsg', 'hi', '')
	weechat.hook_modifier('irc_out1_privmsg', 'hi', '')
	
if __name__ == "__main__":
	main()