2011-02-03 01:20:37 -05:00
|
|
|
from PyQt4 import QtGui, QtCore
|
|
|
|
from datetime import *
|
|
|
|
import re
|
2011-02-13 04:27:12 -05:00
|
|
|
import random
|
2011-02-03 01:20:37 -05:00
|
|
|
|
|
|
|
from generic import PesterIcon
|
2011-04-14 03:07:05 -04:00
|
|
|
from parsetools import timeDifference, convertTags, lexMessage, parseRegexpFunctions
|
2011-02-13 20:32:02 -05:00
|
|
|
from mispeller import mispeller
|
|
|
|
|
2011-04-10 05:22:06 -04:00
|
|
|
_groupre = re.compile(r"\\([0-9]+)")
|
|
|
|
_upperre = re.compile(r"upper\(([\w<>\\]+)\)")
|
|
|
|
_lowerre = re.compile(r"lower\(([\w<>\\]+)\)")
|
|
|
|
_scramblere = re.compile(r"scramble\(([\w<>\\]+)\)")
|
|
|
|
_reversere = re.compile(r"reverse\(([\w<>\\]+)\)")
|
2011-02-03 01:20:37 -05:00
|
|
|
|
|
|
|
class Mood(object):
|
2011-03-12 02:44:07 -05:00
|
|
|
moods = ["chummy", "rancorous", "offline", "pleasant", "distraught",
|
|
|
|
"pranky", "smooth", "ecstatic", "relaxed", "discontent",
|
2011-02-03 01:20:37 -05:00
|
|
|
"devious", "sleek", "detestful", "mirthful", "manipulative",
|
2011-02-04 16:17:27 -05:00
|
|
|
"vigorous", "perky", "acceptant", "protective", "mystified",
|
|
|
|
"amazed", "insolent", "bemused" ]
|
2011-02-23 06:06:00 -05:00
|
|
|
moodcats = ["chums", "trolls", "other"]
|
2011-03-03 04:03:41 -05:00
|
|
|
revmoodcats = {'discontent': 'trolls', 'insolent': 'chums', 'rancorous': 'chums', 'sleek': 'trolls', 'bemused': 'chums', 'mystified': 'chums', 'pranky': 'chums', 'distraught': 'chums', 'offline': 'chums', 'chummy': 'chums', 'protective': 'other', 'vigorous': 'trolls', 'ecstatic': 'trolls', 'relaxed': 'trolls', 'pleasant': 'chums', 'manipulative': 'trolls', 'detestful': 'trolls', 'smooth': 'chums', 'mirthful': 'trolls', 'acceptant': 'trolls', 'perky': 'trolls', 'devious': 'trolls', 'amazed': 'chums'}
|
2011-02-23 06:06:00 -05:00
|
|
|
|
2011-02-03 01:20:37 -05:00
|
|
|
def __init__(self, mood):
|
|
|
|
if type(mood) is int:
|
|
|
|
self.mood = mood
|
|
|
|
else:
|
|
|
|
self.mood = self.moods.index(mood)
|
|
|
|
def value(self):
|
|
|
|
return self.mood
|
|
|
|
def name(self):
|
|
|
|
try:
|
|
|
|
name = self.moods[self.mood]
|
|
|
|
except IndexError:
|
|
|
|
name = "chummy"
|
|
|
|
return name
|
|
|
|
def icon(self, theme):
|
|
|
|
try:
|
|
|
|
f = theme["main/chums/moods"][self.name()]["icon"]
|
|
|
|
except KeyError:
|
|
|
|
return PesterIcon(theme["main/chums/moods/chummy/icon"])
|
|
|
|
return PesterIcon(f)
|
|
|
|
|
2011-02-13 20:32:02 -05:00
|
|
|
|
2011-02-03 01:20:37 -05:00
|
|
|
class pesterQuirk(object):
|
|
|
|
def __init__(self, quirk):
|
|
|
|
if type(quirk) != dict:
|
|
|
|
raise ValueError("Quirks must be given a dictionary")
|
|
|
|
self.quirk = quirk
|
|
|
|
self.type = self.quirk["type"]
|
2011-04-17 03:46:11 -04:00
|
|
|
if "on" not in self.quirk:
|
|
|
|
self.quirk["on"] = True
|
|
|
|
self.on = self.quirk["on"]
|
2011-02-13 04:27:12 -05:00
|
|
|
def apply(self, string, first=False, last=False):
|
2011-04-17 03:46:11 -04:00
|
|
|
if not self.on:
|
|
|
|
return string
|
|
|
|
elif self.type == "prefix":
|
2011-02-03 01:20:37 -05:00
|
|
|
return self.quirk["value"] + string
|
2011-02-13 04:27:12 -05:00
|
|
|
elif self.type == "suffix":
|
2011-02-03 01:20:37 -05:00
|
|
|
return string + self.quirk["value"]
|
2011-02-13 04:27:12 -05:00
|
|
|
elif self.type == "replace":
|
2011-02-03 01:20:37 -05:00
|
|
|
return string.replace(self.quirk["from"], self.quirk["to"])
|
2011-02-13 04:27:12 -05:00
|
|
|
elif self.type == "regexp":
|
|
|
|
fr = self.quirk["from"]
|
|
|
|
if not first and len(fr) > 0 and fr[0] == "^":
|
|
|
|
return string
|
|
|
|
if not last and len(fr) > 0 and fr[len(fr)-1] == "$":
|
|
|
|
return string
|
2011-04-14 03:07:05 -04:00
|
|
|
to = self.quirk["to"]
|
|
|
|
pt = parseRegexpFunctions(to)
|
|
|
|
return re.sub(fr, pt.expand, string)
|
2011-02-13 04:27:12 -05:00
|
|
|
elif self.type == "random":
|
2011-02-21 18:58:07 -05:00
|
|
|
if len(self.quirk["randomlist"]) == 0:
|
|
|
|
return string
|
2011-02-13 04:27:12 -05:00
|
|
|
fr = self.quirk["from"]
|
|
|
|
if not first and len(fr) > 0 and fr[0] == "^":
|
|
|
|
return string
|
|
|
|
if not last and len(fr) > 0 and fr[len(fr)-1] == "$":
|
|
|
|
return string
|
|
|
|
def randomrep(mo):
|
|
|
|
choice = random.choice(self.quirk["randomlist"])
|
2011-04-14 03:07:05 -04:00
|
|
|
pt = parseRegexpFunctions(choice)
|
|
|
|
return pt.expand(mo)
|
2011-02-13 04:27:12 -05:00
|
|
|
return re.sub(self.quirk["from"], randomrep, string)
|
2011-02-13 20:32:02 -05:00
|
|
|
elif self.type == "spelling":
|
|
|
|
percentage = self.quirk["percentage"]/100.0
|
|
|
|
words = string.split(" ")
|
|
|
|
newl = []
|
|
|
|
for w in words:
|
|
|
|
p = random.random()
|
|
|
|
if p < percentage:
|
|
|
|
newl.append(mispeller(w))
|
|
|
|
else:
|
|
|
|
newl.append(w)
|
|
|
|
return " ".join(newl)
|
2011-02-13 04:27:12 -05:00
|
|
|
|
2011-02-03 01:20:37 -05:00
|
|
|
def __str__(self):
|
|
|
|
if self.type == "prefix":
|
|
|
|
return "BEGIN WITH: %s" % (self.quirk["value"])
|
|
|
|
elif self.type == "suffix":
|
|
|
|
return "END WITH: %s" % (self.quirk["value"])
|
|
|
|
elif self.type == "replace":
|
|
|
|
return "REPLACE %s WITH %s" % (self.quirk["from"], self.quirk["to"])
|
|
|
|
elif self.type == "regexp":
|
|
|
|
return "REGEXP: %s REPLACED WITH %s" % (self.quirk["from"], self.quirk["to"])
|
2011-02-13 04:27:12 -05:00
|
|
|
elif self.type == "random":
|
|
|
|
return "REGEXP: %s RANDOMLY REPLACED WITH %s" % (self.quirk["from"], [str(r) for r in self.quirk["randomlist"]])
|
2011-02-13 20:32:02 -05:00
|
|
|
elif self.type == "spelling":
|
|
|
|
return "MISPELLER: %d%%" % (self.quirk["percentage"])
|
2011-02-03 01:20:37 -05:00
|
|
|
|
|
|
|
class pesterQuirks(object):
|
|
|
|
def __init__(self, quirklist):
|
|
|
|
self.quirklist = []
|
|
|
|
for q in quirklist:
|
2011-02-24 21:15:21 -05:00
|
|
|
self.addQuirk(q)
|
2011-02-03 01:20:37 -05:00
|
|
|
def plainList(self):
|
|
|
|
return [q.quirk for q in self.quirklist]
|
2011-02-24 21:15:21 -05:00
|
|
|
def addQuirk(self, q):
|
|
|
|
if type(q) == dict:
|
|
|
|
self.quirklist.append(pesterQuirk(q))
|
|
|
|
elif type(q) == pesterQuirk:
|
|
|
|
self.quirklist.append(q)
|
2011-02-13 04:27:12 -05:00
|
|
|
def apply(self, lexed, first=False, last=False):
|
|
|
|
prefix = [q for q in self.quirklist if q.type=='prefix']
|
|
|
|
suffix = [q for q in self.quirklist if q.type=='suffix']
|
2011-02-03 01:20:37 -05:00
|
|
|
replace = [q for q in self.quirklist if
|
|
|
|
q.type=='replace' or q.type=='regexp']
|
2011-02-21 14:07:59 -05:00
|
|
|
randomrep = [q for q in self.quirklist if q.type=='random']
|
2011-02-13 20:32:02 -05:00
|
|
|
spelling = [q for q in self.quirklist if q.type=='spelling']
|
2011-03-12 02:44:07 -05:00
|
|
|
|
2011-02-13 04:27:12 -05:00
|
|
|
newlist = []
|
|
|
|
for (i, o) in enumerate(lexed):
|
|
|
|
if type(o) not in [str, unicode]:
|
2011-02-13 05:30:08 -05:00
|
|
|
if i == 0:
|
|
|
|
string = " "
|
|
|
|
for p in prefix:
|
|
|
|
string += p.apply(string)
|
|
|
|
newlist.append(string)
|
2011-02-13 04:27:12 -05:00
|
|
|
newlist.append(o)
|
|
|
|
continue
|
|
|
|
lastStr = (i == len(lexed)-1)
|
|
|
|
string = o
|
2011-02-13 20:32:02 -05:00
|
|
|
for s in spelling:
|
|
|
|
string = s.apply(string)
|
2011-02-21 14:07:59 -05:00
|
|
|
for r in randomrep:
|
2011-02-13 05:30:08 -05:00
|
|
|
string = r.apply(string, first=(i==0), last=lastStr)
|
2011-02-13 04:27:12 -05:00
|
|
|
for r in replace:
|
2011-02-13 05:30:08 -05:00
|
|
|
string = r.apply(string, first=(i==0), last=lastStr)
|
|
|
|
if i == 0:
|
2011-02-21 14:07:59 -05:00
|
|
|
if len(prefix) >= 1:
|
|
|
|
myprefix = random.choice(prefix)
|
|
|
|
string = myprefix.apply(string)
|
2011-02-13 04:27:12 -05:00
|
|
|
if lastStr:
|
2011-02-21 14:07:59 -05:00
|
|
|
if len(suffix) >= 1:
|
|
|
|
mysuffix = random.choice(suffix)
|
|
|
|
string = mysuffix.apply(string)
|
2011-02-13 04:27:12 -05:00
|
|
|
newlist.append(string)
|
|
|
|
|
2011-02-21 14:07:59 -05:00
|
|
|
final = []
|
|
|
|
for n in newlist:
|
|
|
|
if type(n) in [str, unicode]:
|
|
|
|
final.extend(lexMessage(n))
|
|
|
|
else:
|
|
|
|
final.append(n)
|
|
|
|
return final
|
2011-02-03 01:20:37 -05:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for q in self.quirklist:
|
|
|
|
yield q
|
|
|
|
|
|
|
|
class PesterProfile(object):
|
2011-04-14 03:04:33 -04:00
|
|
|
def __init__(self, handle, color=None, mood=Mood("offline"), group=None, chumdb=None):
|
2011-02-03 01:20:37 -05:00
|
|
|
self.handle = handle
|
|
|
|
if color is None:
|
|
|
|
if chumdb:
|
|
|
|
color = chumdb.getColor(handle, QtGui.QColor("black"))
|
|
|
|
else:
|
|
|
|
color = QtGui.QColor("black")
|
|
|
|
self.color = color
|
|
|
|
self.mood = mood
|
2011-04-14 03:04:33 -04:00
|
|
|
if group is None:
|
|
|
|
if chumdb:
|
|
|
|
group = chumdb.getGroup(handle, "Chums")
|
|
|
|
else:
|
|
|
|
group = "Chums"
|
|
|
|
self.group = group
|
2011-02-05 12:17:33 -05:00
|
|
|
def initials(self, time=None):
|
2011-02-03 01:20:37 -05:00
|
|
|
handle = self.handle
|
|
|
|
caps = [l for l in handle if l.isupper()]
|
|
|
|
if not caps:
|
|
|
|
caps = [""]
|
2011-02-05 12:17:33 -05:00
|
|
|
initials = (handle[0]+caps[0]).upper()
|
|
|
|
if hasattr(self, 'time') and time:
|
|
|
|
if self.time > time:
|
|
|
|
return "F"+initials
|
|
|
|
elif self.time < time:
|
|
|
|
return "P"+initials
|
|
|
|
else:
|
|
|
|
return "C"+initials
|
|
|
|
else:
|
2011-03-12 02:44:07 -05:00
|
|
|
return (handle[0]+caps[0]).upper()
|
2011-02-03 01:20:37 -05:00
|
|
|
def colorhtml(self):
|
2011-03-03 04:03:41 -05:00
|
|
|
if self.color:
|
|
|
|
return self.color.name()
|
|
|
|
else:
|
|
|
|
return "#000000"
|
2011-02-03 01:20:37 -05:00
|
|
|
def colorcmd(self):
|
2011-03-03 04:03:41 -05:00
|
|
|
if self.color:
|
|
|
|
(r, g, b, a) = self.color.getRgb()
|
|
|
|
return "%d,%d,%d" % (r,g,b)
|
|
|
|
else:
|
|
|
|
return "0,0,0"
|
2011-02-03 01:20:37 -05:00
|
|
|
def plaindict(self):
|
|
|
|
return (self.handle, {"handle": self.handle,
|
|
|
|
"mood": self.mood.name(),
|
2011-04-14 03:04:33 -04:00
|
|
|
"color": unicode(self.color.name()),
|
|
|
|
"group": unicode(self.group)})
|
2011-02-03 01:20:37 -05:00
|
|
|
def blocked(self, config):
|
|
|
|
return self.handle in config.getBlocklist()
|
|
|
|
|
2011-02-13 04:27:12 -05:00
|
|
|
def memsg(self, syscolor, lexmsg, time=None):
|
|
|
|
suffix = lexmsg[0].suffix
|
|
|
|
msg = convertTags(lexmsg[1:], "text")
|
2011-02-03 04:45:48 -05:00
|
|
|
uppersuffix = suffix.upper()
|
2011-02-05 12:17:33 -05:00
|
|
|
if time is not None:
|
|
|
|
handle = "%s %s" % (time.temporal, self.handle)
|
2011-02-05 22:24:10 -05:00
|
|
|
initials = time.pcf+self.initials()+time.number+uppersuffix
|
2011-02-05 12:17:33 -05:00
|
|
|
else:
|
|
|
|
handle = self.handle
|
|
|
|
initials = self.initials()+uppersuffix
|
|
|
|
return "<c=%s>-- %s%s <c=%s>[%s]</c> %s --</c>" % (syscolor.name(), handle, suffix, self.colorhtml(), initials, msg)
|
2011-02-03 01:20:37 -05:00
|
|
|
def pestermsg(self, otherchum, syscolor, verb):
|
|
|
|
return "<c=%s>-- %s <c=%s>[%s]</c> %s %s <c=%s>[%s]</c> at %s --</c>" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), verb, otherchum.handle, otherchum.colorhtml(), otherchum.initials(), datetime.now().strftime("%H:%M"))
|
2011-02-10 13:00:06 -05:00
|
|
|
def moodmsg(self, mood, syscolor, theme):
|
|
|
|
return "<c=%s>-- %s <c=%s>[%s]</c> changed their mood to %s <img src='%s' /> --</c>" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), mood.name().upper(), theme["main/chums/moods"][mood.name()]["icon"])
|
2011-02-10 20:55:45 -05:00
|
|
|
def idlemsg(self, syscolor, verb):
|
|
|
|
return "<c=%s>-- %s <c=%s>[%s]</c> %s --" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), verb)
|
2011-02-05 12:17:33 -05:00
|
|
|
def memoclosemsg(self, syscolor, timeGrammar, verb):
|
|
|
|
return "<c=%s><c=%s>%s%s%s</c> %s.</c>" % (syscolor.name(), self.colorhtml(), timeGrammar.pcf, self.initials(), timeGrammar.number, verb)
|
2011-02-05 13:56:25 -05:00
|
|
|
def memoopenmsg(self, syscolor, td, timeGrammar, verb, channel):
|
|
|
|
(temporal, pcf, when) = (timeGrammar.temporal, timeGrammar.pcf, timeGrammar.when)
|
|
|
|
timetext = timeDifference(td)
|
|
|
|
initials = pcf+self.initials()
|
|
|
|
return "<c=%s><c=%s>%s</c> %s %s %s.</c>" % \
|
|
|
|
(syscolor.name(), self.colorhtml(), initials, timetext, verb, channel[1:].upper().replace("_", " "))
|
2011-02-06 01:02:39 -05:00
|
|
|
def memobanmsg(self, opchum, opgrammar, syscolor, timeGrammar):
|
|
|
|
initials = timeGrammar.pcf+self.initials()+timeGrammar.number
|
|
|
|
opinit = opgrammar.pcf+opchum.initials()+opgrammar.number
|
|
|
|
return "<c=%s>%s</c> banned <c=%s>%s</c> from responding to memo." % \
|
|
|
|
(opchum.colorhtml(), opinit, self.colorhtml(), initials)
|
2011-02-05 12:17:33 -05:00
|
|
|
def memojoinmsg(self, syscolor, td, timeGrammar, verb):
|
|
|
|
(temporal, pcf, when) = (timeGrammar.temporal, timeGrammar.pcf, timeGrammar.when)
|
2011-02-05 13:56:25 -05:00
|
|
|
timetext = timeDifference(td)
|
2011-02-05 12:17:33 -05:00
|
|
|
initials = pcf+self.initials()+timeGrammar.number
|
|
|
|
return "<c=%s><c=%s>%s %s [%s]</c> %s %s." % \
|
|
|
|
(syscolor.name(), self.colorhtml(), temporal, self.handle,
|
|
|
|
initials, timetext, verb)
|
2011-02-03 01:20:37 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def checkLength(handle):
|
|
|
|
return len(handle) <= 256
|
|
|
|
@staticmethod
|
|
|
|
def checkValid(handle):
|
|
|
|
caps = [l for l in handle if l.isupper()]
|
|
|
|
if len(caps) != 1 or handle[0].isupper():
|
|
|
|
return False
|
|
|
|
if re.search("[^A-Za-z0-9]", handle) is not None:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2011-02-08 17:59:25 -05:00
|
|
|
class PesterHistory(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.history = []
|
2011-02-09 01:26:23 -05:00
|
|
|
self.current = 0
|
|
|
|
self.saved = None
|
|
|
|
def next(self, text):
|
|
|
|
if self.current == 0:
|
|
|
|
return None
|
|
|
|
if self.current == len(self.history):
|
|
|
|
self.save(text)
|
|
|
|
self.current -= 1
|
2011-02-08 17:59:25 -05:00
|
|
|
text = self.history[self.current]
|
|
|
|
return text
|
|
|
|
def prev(self):
|
|
|
|
self.current += 1
|
2011-02-09 01:26:23 -05:00
|
|
|
if self.current >= len(self.history):
|
|
|
|
self.current = len(self.history)
|
|
|
|
return self.retrieve()
|
2011-02-08 17:59:25 -05:00
|
|
|
return self.history[self.current]
|
|
|
|
def reset(self):
|
2011-02-09 01:26:23 -05:00
|
|
|
self.current = len(self.history)
|
|
|
|
self.saved = None
|
|
|
|
def save(self, text):
|
|
|
|
self.saved = text
|
|
|
|
def retrieve(self):
|
|
|
|
return self.saved
|
2011-02-08 17:59:25 -05:00
|
|
|
def add(self, text):
|
2011-02-09 01:26:23 -05:00
|
|
|
if len(self.history) == 0 or text != self.history[len(self.history)-1]:
|
|
|
|
self.history.append(text)
|
|
|
|
self.reset()
|