Skip to content
14 changes: 9 additions & 5 deletions zirc/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from string import Template
from .flood import floodProtect
from .loop import EventLoop
from .errors import NoSocket, NoConfig
Expand Down Expand Up @@ -89,17 +90,20 @@ def main_job(self, event):
self.send("NOTICE {0} :{1} {2}".format(event.source.nick, ctcp_message, result))

# Basic client use
def privmsg(self, channel, message, color=None):
def privmsg(self, channel, message, background=None, rainbow=False, style=None):
MSGLEN = 400 - len("PRIVMSG {} :\r\n".format(channel).encode())
strings = [message[i:i + MSGLEN] for i in range(0, len(message), MSGLEN)]
for message in strings:
self.send("PRIVMSG {0} :{1}".format(channel, util.colors.color(message, color)))
msg = Template(message).safe_substitute(**util.colors.colors)
if rainbow:
msg = util.colors.rainbow(msg)
self.send("PRIVMSG {0} :{1}".format(channel, util.colors.stylize(util.colors.background(msg, background), style)))

def reply(self, event, message, color=None):
def reply(self, event, message, background=None, rainbow=False, style=None):
if event.target == self._config['nickname']:
self.privmsg(event.source.nick, message, color=color)
self.privmsg(event.source.nick, message, background=background, rainbow=rainbow, style=style)
else:
self.privmsg(event.target, message, color=color)
self.privmsg(event.target, message, background=background, rainbow=rainbow, style=style)

def listen(self, func, event_name):
self.listeners.append((event_name.lower(), func))
92 changes: 57 additions & 35 deletions zirc/util/colors.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
colors = {
'00': ['white'],
'01': ['black'],
'02': ['navy'],
'03': ['green'],
'04': ['red'],
'05': ['brown', 'maroon'],
'06': ['purple', 'violet'],
'07': ['olive'],
'08': ['yellow'],
'09': ['lightgreen', 'lime'],
'10': ['teal', 'bluecyan'],
'11': ['cyan', 'aqua'],
'12': ['blue', 'royal'],
'13': ['pink', 'lightpurple', 'fuchsia'],
'14': ['gray', 'grey'],
'15': ['lightgray', 'lightgrey', 'silver'],
"WHITE": "\x0300",
"BLACK": "\x0301",
"NAVY": "\x0302",
"GREEN": "\x0303",
"RED": "\x0304",
"BROWN": "\x0305",
"MAROON": "\x0305",
"PURPLE": "\x0306",
"VIOLET": "\x0306",
"ORANGE": "\x0307",
"YELLOW": "\x0308",
"LIGHTGREEN": "\x0309",
"LIME": "\x0309",
"TEAL": "\x0310",
"BLUECYAN": "\x0310",
"CYAN": "\x0311",
"AQUA": "\x0311",
"BLUE": "\x0312",
"ROYAL": "\x0312",
"LIGHTPURPLE": "\x0313",
"PINK": "\x0313",
"FUCHSIA": "\x0313",
"GREY": "\x0314",
"GRAY": "\x0314",
"LIGHTGRAY": "\x0315",
"LIGHTGREY": "\x0315",
"SILVER": "\x0315",

"NORMAL": "\x0F",
"UNDERLINE": "\x1F",
"BOLD": "\x02",
"ITALIC": "\35",
"REVERSE": "\u202E"
}
rainbow = ['red', 'olive', 'yellow', 'green', 'blue', 'navy', 'violet']

def getcolor(color):
for key in colors.keys():
if color in colors[key]:
return key
else:
pass
_rainbow = ["red", "orange", "yellow", "green", "blue", "navy", "violet"]


def rainbow(string):
i = 0
colored = ""

def color(string, c):
if c == 'rainbow':
i = 0
colored = ""
for character in string:
if i > (len(_rainbow) - 1): # We substract one because i starts at 0 and len(rainbow) at 1
i = 0

for character in string:
if i > (len(rainbow) - 1): # We substract one because i starts at 0 and len(rainbow) at 1
i = 0
colored += "{0}{1}".format(colors[_rainbow[i].upper()], character)
i += 1

colored += "\x03{0}{1}".format(getcolor(rainbow[i]), character)
i += 1
return colored + "\x0F"

return colored + "\x0F"
elif c is None:
def background(string, bg):
c = string.find("\x03") != -1
if c and bg is not None:
return "{0},{1}{2}".format(string[:3], colors[bg], string[3:])
elif not c and bg is not None:
return "{0},{1}{2}\x0F".format(colors["black"], colors[bg], string)
elif bg is None:
return string

def stylize(string, style):
if style is not None:
return "{0}{1}".format(colors[style.upper()], string)
else:
return "\x03{}{}\x0F".format(getcolor(c), string)
return string