import logging import logging.config import ostools _datadir = ostools.getDataDir() logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from PyQt5 import QtGui from datetime import datetime import re import random from mood import Mood from parsetools import (timeDifference, convertTags, lexMessage, parseRegexpFunctions, smiledict) from mispeller import mispeller _urlre = re.compile(r"(?i)(?:^|(?<=\s))(?:(?:https?|ftp)://|magnet:)[^\s]+") #_url2re = re.compile(r"(?i)(?\\]+)\)") _lowerre = re.compile(r"lower\(([\w<>\\]+)\)") _scramblere = re.compile(r"scramble\(([\w<>\\]+)\)") _reversere = re.compile(r"reverse\(([\w<>\\]+)\)") _ctagre = re.compile("()", re.I) _smilere = re.compile("|".join(list(smiledict.keys()))) _memore = re.compile(r"(\s|^)(#[A-Za-z0-9_]+)") _handlere = re.compile(r"(\s|^)(@[A-Za-z0-9_]+)") 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"] if "on" not in self.quirk: self.quirk["on"] = True self.on = self.quirk["on"] if "group" not in self.quirk: self.quirk["group"] = "Miscellaneous" self.group = self.quirk["group"] try: self.checkstate = self.quirk["checkstate"] except KeyError: pass def apply(self, string, first=False, last=False, checkstate=0): # This function applies the quirks :3 print("checkstate: " + str(checkstate)) # Try to get a list of breakable links, smilies, # @handle, #memo, in the message. try: # Check for links, store list of links. links = list() for match in re.findall(_urlre, string): links.append(match) # Check for smilies, store list of smilies. smilies = list() for match in re.findall(_smilere, string): smilies.append(match) # Check for @handles, store list of @handles. handles = list() for match in re.findall(_handlere, string): smilies.append(match) # Check for #memos, store list of #memos. memos = list() for match in re.findall(_memore, string): smilies.append(match) except Exception as e: PchumLog.warning("Quirk issue: " + str(e)) if not self.on: return string elif self.type == "prefix": return self.quirk["value"] + string elif self.type == "suffix": return string + self.quirk["value"] elif self.type == "replace": try: # Replace like normal output = string.replace(self.quirk["from"], self.quirk["to"]) # Try to revert links based on list. for link in links: output = output.replace(link.replace(self.quirk["from"], self.quirk["to"]), link) # Try to revert smilies based on list. for smiley in smilies: output = output.replace(smiley.replace(self.quirk["from"], self.quirk["to"]), smiley) # Try to revert @handles based on list. for handle in handles: output = output.replace(handle.replace(self.quirk["from"], self.quirk["to"]), handle) # Try to revert #memos based on list. for memo in memos: output = output.replace(memo.replace(self.quirk["from"], self.quirk["to"]), memo) return output except Exception as e: PchumLog.warning("Replace issue: " + str(e)) return string.replace(self.quirk["from"], self.quirk["to"]) 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 to = self.quirk["to"] pt = parseRegexpFunctions(to) output = re.sub(fr, pt.expand, string) # Reverse sub for links/smilies if enabled. if checkstate == 2: for smiley in smilies: output = output.replace(re.sub(fr, pt.expand, smiley), smiley) for link in links: output = output.replace(re.sub(fr, pt.expand, link), link) for handle in handles: output = output.replace(re.sub(fr, pt.expand, handle), handle) for memo in memos: output = output.replace(re.sub(fr, pt.expand, memo), memo) return output elif self.type == "random": if len(self.quirk["randomlist"]) == 0: return string 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"]) pt = parseRegexpFunctions(choice) return pt.expand(mo) return self.quirk["from"] elif self.type == "spelling": percentage = self.quirk["percentage"]/100.0 words = string.split(" ") newl = [] p = random.random() # Main /word loop for w in words: if re.match(_urlre, w): # Word is an url, don't break. newl.append(w) elif re.match(_memore, w): # Word is an @memo, don't break. newl.append(w) elif re.match(_handlere, w): # Word is an @handle, don't break. newl.append(w) elif re.match(_smilere, w): # Word contains a smiley # Split by ':' and only skip the smiley, # this part is very messy and optional really. stripped_smiles = list() for smiley in smilies: stripped_smiles.append(smiley.strip(':')) denominated = w.split(':') output = '' for part in range(0, len(denominated)): if denominated[part] in stripped_smiles: output += denominated[part] else: if not _ctagre.search(denominated[part]) and p < percentage: output += mispeller(denominated[part]) elif p < percentage: split = _ctagre.split(denominated[part]) tmp = [] for s in split: if s and not _ctagre.search(s): tmp.append(mispeller(s)) else: tmp.append(s) output += tmp else: output += denominated[part] if part != len(denominated)-1: output += ':' newl.append(output) else: if not _ctagre.search(w) and p < percentage: newl.append(mispeller(w)) elif p < percentage: split = _ctagre.split(w) tmp = [] for s in split: if s and not _ctagre.search(s): tmp.append(mispeller(s)) else: tmp.append(s) newl.append("".join(tmp)) else: newl.append(w) return " ".join(newl) 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"]) elif self.type == "random": return "REGEXP: %s RANDOMLY REPLACED WITH %s" % (self.quirk["from"], [r for r in self.quirk["randomlist"]]) elif self.type == "spelling": return "MISPELLER: %d%%" % (self.quirk["percentage"]) class pesterQuirks(object): def __init__(self, quirklist): self.quirklist = [] for q in quirklist: self.addQuirk(q) def plainList(self): return [q.quirk for q in self.quirklist] def addQuirk(self, q): if type(q) == dict: self.quirklist.append(pesterQuirk(q)) elif type(q) == pesterQuirk: self.quirklist.append(q) 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'] newlist = [] for (i, o) in enumerate(lexed): if type(o) not in [str, str]: if i == 0: string = " " for p in prefix: string += p.apply(string) newlist.append(string) newlist.append(o) continue lastStr = (i == len(lexed)-1) string = o for q in self.quirklist: try: checkstate = int(q.checkstate) except Exception: checkstate = 0 if q.type != 'prefix' and q.type != 'suffix': if q.type == 'regexp' or q.type == 'random': string = q.apply(string, first=(i==0), last=lastStr, checkstate=checkstate) else: string = q.apply(string) elif q.type == 'prefix' and i == 0: string = q.apply(string) elif q.type == 'suffix' and lastStr: string = q.apply(string) newlist.append(string) final = [] for n in newlist: if type(n) in [str, str]: final.extend(lexMessage(n)) else: final.append(n) return final def __iter__(self): for q in self.quirklist: yield q class PesterProfile(object): def __init__(self, handle, color=None, mood=Mood("offline"), group=None, notes="", chumdb=None): 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 if group is None: if chumdb: group = chumdb.getGroup(handle, "Chums") else: group = "Chums" self.group = group self.notes = notes def initials(self, time=None): handle = self.handle caps = [l for l in handle if l.isupper()] if not caps: caps = [""] PchumLog.debug("handle = " + str(handle)) PchumLog.debug("caps = " + str(caps)) # Fallback for invalid string try: initials = (handle[0]+caps[0]).upper() except: PchumLog.exception('') initials = "XX" PchumLog.debug("initials = " + str(initials)) 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: return initials def colorhtml(self): if self.color: return self.color.name() else: return "#000000" def colorcmd(self): if self.color: (r, g, b, a) = self.color.getRgb() return "%d,%d,%d" % (r,g,b) else: return "0,0,0" def plaindict(self): return (self.handle, {"handle": self.handle, "mood": self.mood.name(), "color": str(self.color.name()), "group": str(self.group), "notes": str(self.notes)}) def blocked(self, config): return self.handle in config.getBlocklist() def memsg(self, syscolor, lexmsg, time=None): suffix = lexmsg[0].suffix msg = convertTags(lexmsg[1:], "text") uppersuffix = suffix.upper() if time is not None: handle = "%s %s" % (time.temporal, self.handle) initials = time.pcf+self.initials()+time.number+uppersuffix else: handle = self.handle initials = self.initials()+uppersuffix return "-- %s%s [%s] %s --" % (syscolor.name(), handle, suffix, self.colorhtml(), initials, msg) def pestermsg(self, otherchum, syscolor, verb): return "-- %s [%s] %s %s [%s] at %s --" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), verb, otherchum.handle, otherchum.colorhtml(), otherchum.initials(), datetime.now().strftime("%H:%M")) def moodmsg(self, mood, syscolor, theme): return "-- %s [%s] changed their mood to %s --" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), mood.name().upper(), theme["main/chums/moods"][mood.name()]["icon"]) def idlemsg(self, syscolor, verb): return "-- %s [%s] %s --" % (syscolor.name(), self.handle, self.colorhtml(), self.initials(), verb) def memoclosemsg(self, syscolor, initials, verb): if type(initials) == type(list()): return "%s %s." % (syscolor.name(), self.colorhtml(), ", ".join(initials), verb) else: return "%s%s%s %s." % (syscolor.name(), self.colorhtml(), initials.pcf, self.initials(), initials.number, verb) def memonetsplitmsg(self, syscolor, initials): if len(initials) <= 0: return "Netsplit quits: None" % (syscolor.name()) else: return "Netsplit quits: %s" % (syscolor.name(), ", ".join(initials)) def memoopenmsg(self, syscolor, td, timeGrammar, verb, channel): (temporal, pcf, when) = (timeGrammar.temporal, timeGrammar.pcf, timeGrammar.when) timetext = timeDifference(td) PchumLog.debug("pre pcf+self.initials()") initials = pcf+self.initials() PchumLog.debug("post pcf+self.initials()") return "%s %s %s %s." % \ (syscolor.name(), self.colorhtml(), initials, timetext, verb, channel[1:].upper().replace("_", " ")) def memobanmsg(self, opchum, opgrammar, syscolor, initials, reason): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number if type(initials) == type(list()): if opchum.handle == reason: return "%s banned %s from responding to memo." % \ (opchum.colorhtml(), opinit, self.colorhtml(), ", ".join(initials)) else: return "%s banned %s from responding to memo: [%s]." % \ (opchum.colorhtml(), opinit, self.colorhtml(), ", ".join(initials), str(reason)) else: # Is timeGrammar defined? Not sure if this works as intented, added try except block to be safe. try: initials = timeGrammar.pcf+self.initials()+timeGrammar.number if opchum.handle == reason: return "%s banned %s from responding to memo." % \ (opchum.colorhtml(), opinit, self.colorhtml(), initials) else: return "%s banned %s from responding to memo: [%s]." % \ (opchum.colorhtml(), opinit, self.colorhtml(), initials, str(reason)) except: PchumLog.exception('') initials = self.initials() if opchum.handle == reason: return "%s banned %s from responding to memo." % \ (opchum.colorhtml(), opinit, self.colorhtml(), initials) else: return "%s banned %s from responding to memo: [%s]." % \ (opchum.colorhtml(), opinit, self.colorhtml(), initials, str(reason)) """ # Currently unused, might be neat to use in the future? def memopermabanmsg(self, opchum, opgrammar, syscolor, timeGrammar): initials = (timeGrammar.pcf + self.initials() + timeGrammar.number) opinit = (opgrammar.pcf + opchum.initials() + opgrammar.number) return "%s permabanned %s from the memo." % \ (opchum.colorhtml(), opinit, self.colorhtml(), initials) """ def memojoinmsg(self, syscolor, td, timeGrammar, verb): #(temporal, pcf, when) = (timeGrammar.temporal, timeGrammar.pcf, timeGrammar.when) timetext = timeDifference(td) initials = timeGrammar.pcf+self.initials()+timeGrammar.number return "%s %s [%s] %s %s." % \ (syscolor.name(), self.colorhtml(), timeGrammar.temporal, self.handle, initials, timetext, verb) def memoopmsg(self, opchum, opgrammar, syscolor): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number return "%s made %s an OP." % \ (opchum.colorhtml(), opinit, self.colorhtml(), self.initials()) def memodeopmsg(self, opchum, opgrammar, syscolor): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number return "%s took away %s's OP powers." % \ (opchum.colorhtml(), opinit, self.colorhtml(), self.initials()) def memovoicemsg(self, opchum, opgrammar, syscolor): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number return "%s gave %s voice." % \ (opchum.colorhtml(), opinit, self.colorhtml(), self.initials()) def memodevoicemsg(self, opchum, opgrammar, syscolor): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number return "%s took away %s's voice." % \ (opchum.colorhtml(), opinit, self.colorhtml(), self.initials()) def memomodemsg(self, opchum, opgrammar, syscolor, modeverb, modeon): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number if modeon: modeon = "now" else: modeon = "no longer" return "Memo is %s %s by %s" % \ (syscolor.name(), modeon, modeverb, opchum.colorhtml(), opinit) def memoquirkkillmsg(self, opchum, opgrammar, syscolor): opinit = opgrammar.pcf+opchum.initials()+opgrammar.number return "%s turned off your quirk." % \ (syscolor.name(), opchum.colorhtml(), opinit) @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: return (False, "Must have exactly 1 uppercase letter") if handle[0].isupper(): return (False, "Cannot start with uppercase letter") if re.search("[^A-Za-z0-9]", handle) is not None: return (False, "Only alphanumeric characters allowed") return (True,) class PesterHistory(object): def __init__(self): self.history = [] 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 text = self.history[self.current] return text def prev(self): self.current += 1 if self.current >= len(self.history): self.current = len(self.history) return self.retrieve() return self.history[self.current] def reset(self): self.current = len(self.history) self.saved = None def save(self, text): self.saved = text def retrieve(self): return self.saved def add(self, text): if len(self.history) == 0 or text != self.history[len(self.history)-1]: self.history.append(text) self.reset()