diff options
author | Sean B. Palmer <http://inamidst.com/sbp/> | 2008-02-23 12:16:43 +0000 |
---|---|---|
committer | Sean B. Palmer <http://inamidst.com/sbp/> | 2008-02-23 12:16:43 +0000 |
commit | 2fb00589439a4efb3906d4e681e7ed815dcd180a (patch) | |
tree | 0a6b0ff1a4b5697fc7c3cb0aa3dc934246fcb874 /irc.py | |
parent | 7931fab14599b739c18c8f1ebcc24b75688dbc09 (diff) | |
download | bot-2fb00589439a4efb3906d4e681e7ed815dcd180a.tar.gz bot-2fb00589439a4efb3906d4e681e7ed815dcd180a.tar.bz2 bot-2fb00589439a4efb3906d4e681e7ed815dcd180a.zip |
Lots of fixes, changes, and new goodies.
Diffstat (limited to 'irc.py')
-rwxr-xr-x | irc.py | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -41,11 +41,21 @@ class Bot(asynchat.async_chat): import threading self.sending = threading.RLock() - def write(self, args, text=None): + def __write(self, args, text=None): + # print '%r %r %r' % (self, args, text) if text is not None: self.push(' '.join(args) + ' :' + text + '\r\n') else: self.push(' '.join(args) + '\r\n') + def write(self, args, text=None): + # This is a safe version of __write + try: + args = [arg.encode('utf-8') for arg in args] + if text is not None: + text = text.encode('utf-8') + self.__write(args, text) + except Exception, e: pass + def run(self, host, port=6667): self.initiate_connect(host, port) @@ -103,6 +113,10 @@ class Bot(asynchat.async_chat): try: text = text.encode('utf-8') except UnicodeEncodeError, e: text = e.__class__ + ': ' + str(e) + if isinstance(recipient, unicode): + try: recipient = recipient.encode('utf-8') + except UnicodeEncodeError, e: + return # No messages within the last 3 seconds? Go ahead! # Otherwise, wait so it's been at least 0.8 seconds + penalty @@ -115,14 +129,14 @@ class Bot(asynchat.async_chat): time.sleep(wait - elapsed) # Loop detection - messages = [m[1] for m in self.stack[-5:]] - if messages.count(text) >= 3: + messages = [m[1] for m in self.stack[-8:]] + if messages.count(text) >= 5: text = '...' - if messages.count('...') >= 1: + if messages.count('...') >= 3: self.sending.release() return - self.write(('PRIVMSG', recipient), text) + self.__write(('PRIVMSG', recipient), text) self.stack.append((time.time(), text)) self.stack = self.stack[-10:] |