From 12a33b71be08df262a3ad78aff35291bf8f133cf Mon Sep 17 00:00:00 2001 From: rootcoma Date: Tue, 19 Nov 2013 00:11:38 -0800 Subject: [PATCH 1/3] Shortening ctcp ping payload for mirc, using first 5 of sha1 hash --- plugin.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/plugin.py b/plugin.py index afaeffc..a1402b6 100644 --- a/plugin.py +++ b/plugin.py @@ -22,6 +22,7 @@ import random import time import datetime import unicodedata +import hashlib class TriviaTime(callbacks.Plugin): """ @@ -154,12 +155,17 @@ class TriviaTime(callbacks.Plugin): pingMsg = pingMsg.split('*', 1) if len(pingMsg) == 2: pingTime = time.time()-float(pingMsg[0]) - channel = pingMsg[1] - irc.sendMsg( - ircmsgs.privmsg(channel, - '%s Ping reply: %0.2f seconds' % (username, pingTime) - ) - ) + channelHash = pingMsg[1] + channel = '' + for name in irc.state.channels: + if channelHash == self.shortHash(name): + if username in irc.state.channels[name].users: + channel = name + break + if channel == '': + irc.sendMsg(ircmsgs.notice(username, '%s Ping reply: %0.2f seconds' % (username, pingTime))) + else: + irc.sendMsg(ircmsgs.privmsg(channel, '%s Ping reply: %0.2f seconds' % (username, pingTime))) def addZeroWidthSpace(self, text): if len(text) <= 1: @@ -167,6 +173,10 @@ class TriviaTime(callbacks.Plugin): s = u'%s\u200b%s' % (text[:1], text[1:]) return s.encode('utf-8') + def shortHash(self, text): + hashText = hashlib.sha1(text).hexdigest() + return hashText[:5] + def acceptedit(self, irc, msg, arg, user, channel, num): """[] Accept a question edit, and remove edit. @@ -359,11 +369,12 @@ class TriviaTime(callbacks.Plugin): def ping(self, irc, msg, arg): """ - Check your latency to the server. + Check your ping to the bot. Make sure your client correclty responds to ctcp pings """ channel = msg.args[0] + channelHash = self.shortHash(channel) username = msg.nick - irc.sendMsg(ircmsgs.privmsg(username, '\x01PING %s*%s\x01' % (time.time(), channel))) + irc.sendMsg(ircmsgs.privmsg(username, '\x01PING %s*%s\x01' % (time.time(), channelHash))) ping = wrap(ping) def me(self, irc, msg, arg): From a96043f81d59795c82090c53d31279a3fd5b30fb Mon Sep 17 00:00:00 2001 From: rootcoma Date: Tue, 19 Nov 2013 01:15:09 -0800 Subject: [PATCH 2/3] More characters for sha1 hash --- plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index a1402b6..540ca08 100644 --- a/plugin.py +++ b/plugin.py @@ -175,7 +175,7 @@ class TriviaTime(callbacks.Plugin): def shortHash(self, text): hashText = hashlib.sha1(text).hexdigest() - return hashText[:5] + return hashText[:8] def acceptedit(self, irc, msg, arg, user, channel, num): """[] From e415c2278471f4c69e2af0917de7e071650aa6bc Mon Sep 17 00:00:00 2001 From: rootcoma Date: Tue, 19 Nov 2013 15:53:04 -0800 Subject: [PATCH 3/3] More entropy, less characters --- plugin.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugin.py b/plugin.py index 540ca08..f6f1777 100644 --- a/plugin.py +++ b/plugin.py @@ -154,7 +154,7 @@ class TriviaTime(callbacks.Plugin): pingMsg = pingMsg[:-1] pingMsg = pingMsg.split('*', 1) if len(pingMsg) == 2: - pingTime = time.time()-float(pingMsg[0]) + pingTime = time.time()-float(pingMsg[0])-1300000000 channelHash = pingMsg[1] channel = '' for name in irc.state.channels: @@ -175,7 +175,16 @@ class TriviaTime(callbacks.Plugin): def shortHash(self, text): hashText = hashlib.sha1(text).hexdigest() - return hashText[:8] + hashText = self.numToBase94(int(hashText, 16), 8) + return hashText + + def numToBase94(self, n, maxChars): + chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHUJKLMNOPQRSTUVWXYZ~!@#$%^&()_+=-`[]{}\\/|?,.><:;\'" ' + L = [] + for i in range(maxChars): + L.append(chars[n % len(chars)]) + n = int(n / len(chars)) + return ''.join(L) def acceptedit(self, irc, msg, arg, user, channel, num): """[] @@ -374,7 +383,7 @@ class TriviaTime(callbacks.Plugin): channel = msg.args[0] channelHash = self.shortHash(channel) username = msg.nick - irc.sendMsg(ircmsgs.privmsg(username, '\x01PING %s*%s\x01' % (time.time(), channelHash))) + irc.sendMsg(ircmsgs.privmsg(username, '\x01PING %0.2f*%s\x01' % (time.time()-1300000000, channelHash))) ping = wrap(ping) def me(self, irc, msg, arg):