def f_replace_secret_with_hashes(self, msg): replaced = msg.replace("secret", "######") if msg != replaced: return state.replace(replaced) return state.next(msg)
def cmd_calc(inp): """ Simple calculator plugin @param inp input string """ allowed_chars = " 0123456789.+-*/()" par = 0; for i in range(0, len(inp)): if inp[i] not in allowed_chars: return state.next("Unknown syntax") else: # for's else try: out = eval(inp) except SyntaxError: return state.next("Syntax error") return state.done(str(out))
def cmd_calc(exp): """ Calculates mathematical expression and returns a state with the result. @param exp: expression to be calculated @type exp: str @return: result of the expression @rtype: state.done|state.next """ if exp is None: return state.next(exp) pattern = "\(?\-?\d*.?\d+(\s*[\+\-\*\/]\(*\s*\d*.?\d+\)*)*\)?" if re.match(pattern, exp): try: return state.done(str(eval(exp))) # exp IS VALID math expression except (SyntaxError, ZeroDivisionError): return state.next(exp) # exp IS NOT VALID math expression return state.next(exp) # exp contains non-mathematical symbols
def f_word_count(msg): """ Filter that adds the number of words in processed message. @param msg: message to be parsed @type msg: str @return next state with the original message @rtype: state.next """ global WORDCOUNT WORDCOUNT += len(msg.split(" ")) return state.next(msg)
def f_karma(msg): """ Checks the message for sequence 'something++' and 'something--', if found, it calls the change_karma function. Depending whether the command is valid, returns either done state or next state with appropriate message. @param msg: message to be filtered @type msg: str @return: done state if the information is done parsing or next state if the information is not parseable @rtype: state.next|state.done """ global KARMA msg = msg.strip().lower() if msg == "c++": return state.next(msg) if msg.endswith("++") or msg.endswith("--"): user = msg[:-2].lower() action = msg[-2:] if user == "": return state.next(msg) changes = { "++": "increased", "--": "decreased" } KARMA.setdefault(user, 0) if action == "++": KARMA[user] += 1 else: KARMA[user] -= 1 if KARMA[user] == 0: del KARMA[user] return state.done(user + "'s karma was " + changes[action] + " by 1.")
def filter_karma(msg): """ Implement karma filter. Try catch karma commands and do appropriate action @param msg input string """ if msg.endswith('++') or msg.endswith('--'): # 'C++' exclusive if msg.lower() != 'c++' and msg[:-2].isalnum(): _karma_change(msg[:-2], msg[-2:]) return state.done(None) else: return state.next(msg)
def f_logging(msg, logfile = "ircbot.log"): """ Logs a message. @param msg: message to log @type msg: str @return: next state with the original message @rtype: state.next """ logging.basicConfig(format = "%(asctime)s %(message)s", datefmt = "%d.%m.%Y %H:%M:%S", filename = logfile, level = logging.DEBUG ) logging.info(msg) return state.next(msg)
def cmd_karma(msg): """ Karma command. Return string with karma of detected nick. @param msg input string """ global KARMA msg = msg.strip() if msg == '': return state.next('No name given') if KARMA.has_key(msg): return state.done(msg + "'s karma is " + str(KARMA[msg])) else: return state.done(msg + " has no karma")
def cmd_karma(user): """ Returns a state with number of karma points for given user. @param user: user whose karma is queried @type user: str @return: done state with message '<user> has <number> points of karma' or next statement with message '<user> has no karma' @rtype: state.next|state.done """ global KARMA if user == "": return state.next("karma") user = user.lower() if user not in KARMA: return state.done("'%s' has no karma." % user) return state.done("'%s' has %d points of karma." % (user, KARMA[user]))
def f_karma(msg): """ Checks the message for sequence 'something++' and 'something--', if found, it calls the change_karma function. Depending whether the command is valid, returns either done state or next state with appropriate message. @param msg: message to be filtered @type msg: str @return: done state if the information is done parsing or next state if the information is not parseable @rtype: state.next|state.done """ global KARMA for exp in msg.split(): exp = exp.lower() if exp == "c++": continue if exp.endswith("++") or msg.endswith("--"): user = exp[:-2].lower() action = exp[-2:] if user == "": continue KARMA.setdefault(user, 0) if action == "++": KARMA[user] += 1 else: KARMA[user] -= 1 if KARMA[user] == 0: del KARMA[user] return state.next(msg)
def h_next(self, t): return state.next()
def c_unknown(self, t): return state.next()
def display_next(self): import time self.saveMask.emit(state.now_mask()) state.next() self.update_gui()
def test_next(self): self.assertTrue(state.is_next(state.next(None)))
def test_not_replace(self): self.assertFalse(state.is_replace(state.done(None))) self.assertFalse(state.is_replace(state.next(None)))