def respond(self, nick, msg): c = self.connection time.sleep(3) if re.search("^hi|^hey|^hello", msg.lower()): self.nextState("_GREETED") if len(self.responses[self.state]) > 0: c.privmsg(self.channel,nick + ": " + self.responses[self.state]) if self.state == 5: self.mTimer = threading.Timer(20.0, self.frustrated, args=(nick,)) self.mTimer.start() else: self.reset() return elif re.search("how[^\w]\w+[^\w]you\??|how[^\w]\w+[^\w]\w+[^\w]\w+\??", msg.lower()): self.nextState("_INQUIRED") if len(self.responses[self.state]) > 0: c.privmsg(self.channel,nick + ": " + self.responses[self.state]) if self.state == 2 or self.state == 6: time.sleep(3) self.nextState("_INQUIRY") c.privmsg(self.channel,nick + ": " + self.responses[self.state]) self.mTimer = threading.Timer(20.0, self.frustrated, args=(nick,)) self.mTimer.start() elif self.state == 12: self.nextState("_DONE") else: self.reset() return elif re.search("^good(.|!)?|^ok(.|!)?|^fine(.|!)?|^all[^\w]right", msg.lower()): self.nextState("_REPLIED") if len(self.responses[self.state]) > 0: c.privmsg(self.channel,nick + ": " + self.responses[self.state]) if self.state == 10: self.nextState("_DONE") self.reset() else: self.reset() return else: m = re.search("(\w+)[^\w](\w+)[^\w](born\??)", msg) if m: name = m.group(1) + " " + m.group(2) c.privmsg(self.channel,nick + ": " + getBirthday.getBirthday(name)) return m = re.search("(\w+)[^\w](\w+)[^\w][s][^\w](birthday\??)", msg) if m: name = m.group(1) + " " + m.group(2) c.privmsg(self.channel,nick + ": " + getBirthday.getBirthday(name)) return c.privmsg(self.channel,nick + ": " + responseFun(msg)) return
def getResponse(msg): for q in birthQueries: match = re.match(q, msg, re.IGNORECASE) if match != None: subject = cleanSubject(match.group(1)) print 'found birthday for ' + subject return getBirthday(subject) for q in subjectQueries: match = re.match(q, msg, re.IGNORECASE) if match != None: subject = cleanSubject(match.group(1)) print 'found subject ' + subject return getSubjectInfo(subject) # Okay, REs didn't work... try something else birth = False words = msg.split() cleanWords = [] for word in words: s = word for c in punct: s = s.replace(c, '') if s in birthWords: birth = True else: cleanWords.append(word) if (len(cleanWords) == 0): return "I don't know what you're talking about." # start from end, grab all cap words to get subject cleanWords.reverse() subj = [] for word in cleanWords: if (word[0] in string.ascii_uppercase or word[0] == '('): subj.append(word) else: break if (len(subj) == 0): return "I don't know what you're talking about." else: subj.reverse() subject = cleanSubject(' '.join(subj)) return getBirthday(subject) if birth else getSubjectInfo(subject)