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_wordscount(msg): """ Return actual word count @param msg unused, only for keeping consistecy with other commands """ global WORDSCOUNT return state.done("Actual word count is: %d words" % WORDSCOUNT)
def filter_wordscount(msg): """ Count words in input message. @param msg input message """ global WORDSCOUNT WORDSCOUNT += len(msg.split()) return state.done(msg)
def cmd_word_count(msg = None): """ Returns the number of received words so far. @param msg: message associated with the command @type msg: str @return: "state withe the message 'Actual word count is <number> words' @rtype: state.done """ global WORDCOUNT return state.done("Actual word count is %d words." % WORDCOUNT)
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 cmd_help(msg): """ Help message for IrcBot @param msg unused, only for consistency with other commands """ help_msg =\ """ IrcBot Implemented commands: help SHUTDOWN word-count karma calc """ return state.done(help_msg)
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 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_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 h_count(self, t): return state.done("%d" % len(t))
def c_echo(self, t): return state.done("echo " + t)
def c_nothing(self, t): return state.done()
def test_not_next(self): self.assertFalse(state.is_next(state.done(None))) self.assertFalse(state.is_next(state.replace(None)))
def test_done(self): self.assertTrue(state.is_done(state.done(None)))
def cmd_say_hello(self, msg = None): return state.done("hello to you too")