From 526151e0314a10364413a41b034c522c3fa5e27d Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 13 May 2012 01:15:55 -0400 Subject: [PATCH] !geolocate command, plus some cleanup to other commands --- earwigbot/commands/_old.py | 14 -------- earwigbot/commands/crypt.py | 4 +-- earwigbot/commands/editcount.py | 4 +-- earwigbot/commands/geolocate.py | 68 ++++++++++++++++++++++++++++++++++++++ earwigbot/commands/langcode.py | 2 +- earwigbot/commands/link.py | 8 ----- earwigbot/commands/registration.py | 4 +-- earwigbot/commands/remind.py | 4 +-- earwigbot/commands/replag.py | 3 +- earwigbot/commands/rights.py | 4 +-- earwigbot/commands/threads.py | 4 +-- earwigbot/config.py | 5 ++- 12 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 earwigbot/commands/geolocate.py diff --git a/earwigbot/commands/_old.py b/earwigbot/commands/_old.py index b49577e..64fcd74 100644 --- a/earwigbot/commands/_old.py +++ b/earwigbot/commands/_old.py @@ -367,17 +367,3 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): reply("NotImplementedError", chan, nick) elif action == "report": reply("NotImplementedError", chan, nick) - if command == "lookup" or command == "ip": - try: - hexIP = line2[4] - except Exception: - reply("Please specify a hex IP address.", chan, nick) - return - hexes = [hexIP[:2], hexIP[2:4], hexIP[4:6], hexIP[6:8]] - hashes = [] - for hexHash in hexes: - newHex = int(hexHash, 16) - hashes.append(newHex) - normalizedIP = "%s.%s.%s.%s" % (hashes[0], hashes[1], hashes[2], hashes[3]) - reply(normalizedIP, chan, nick) - return diff --git a/earwigbot/commands/crypt.py b/earwigbot/commands/crypt.py index 6a57c8c..ccb5300 100644 --- a/earwigbot/commands/crypt.py +++ b/earwigbot/commands/crypt.py @@ -33,9 +33,7 @@ class Command(BaseCommand): def check(self, data): commands = ["crypt", "hash", "encrypt", "decrypt"] - if data.is_command and data.command in commands: - return True - return False + return data.is_command and data.command in commands def process(self, data): if data.command == "crypt": diff --git a/earwigbot/commands/editcount.py b/earwigbot/commands/editcount.py index 9182877..517a8e3 100644 --- a/earwigbot/commands/editcount.py +++ b/earwigbot/commands/editcount.py @@ -31,9 +31,7 @@ class Command(BaseCommand): def check(self, data): commands = ["ec", "editcount"] - if data.is_command and data.command in commands: - return True - return False + return data.is_command and data.command in commands def process(self, data): if not data.args: diff --git a/earwigbot/commands/geolocate.py b/earwigbot/commands/geolocate.py new file mode 100644 index 0000000..73dee91 --- /dev/null +++ b/earwigbot/commands/geolocate.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2009-2012 by Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import json +import urllib2 + +from earwigbot.commands import BaseCommand + +class Command(BaseCommand): + """Geolocate an IP address (via http://ipinfodb.com/).""" + name = "geolocate" + + def check(self, data): + commands = ["geolocate", "locate", "geo", "ip"] + return data.is_command and data.command in commands + + def process(self, data): + if not data.args: + self.reply(data, "please specify an IP to lookup.") + return + + try: + key = config.tasks[self.name]["apiKey"] + except KeyError: + msg = 'I need an API key for http://ipinfodb.com/ stored as \x0303config.tasks["{0}"]["apiKey"]\x0301.' + log = 'Need an API key for http://ipinfodb.com/ stored as config.tasks["{0}"]["apiKey"]' + self.reply(data, msg.format(self.name) + ".") + self.logger.error(log.format(self.name)) + return + + address = data.args[0] + url = "http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json" + query = urllib2.urlopen(url.format(key, address)).read() + res = json.loads(query) + + try: + country = res["countryName"] + region = res["regionName"] + city = res["cityName"] + latitude = res["latitude"] + longitude = res["longitude"] + utcoffset = res["timeZone"] + except KeyError: + self.reply(data, "IP \x0302{0}\x0301 not found.".format(address)) + return + + msg = "{0}, {1}, {2} ({3}, {4}), UTC {5}" + geo = msg.format(country, region, city, latitude, longitude, utcoffset) + self.reply(data, geo) diff --git a/earwigbot/commands/langcode.py b/earwigbot/commands/langcode.py index 4630780..6695980 100644 --- a/earwigbot/commands/langcode.py +++ b/earwigbot/commands/langcode.py @@ -45,7 +45,7 @@ class Command(BaseCommand): if site["code"] == code: name = site["name"] sites = ", ".join([s["url"] for s in site["site"]]) - msg = "\x0302{0}\x0302 is {1} ({2})".format(code, name, sites) + msg = "\x0302{0}\x0301 is {1} ({2})".format(code, name, sites) self.reply(data, msg) return diff --git a/earwigbot/commands/link.py b/earwigbot/commands/link.py index cb2e154..152fa08 100644 --- a/earwigbot/commands/link.py +++ b/earwigbot/commands/link.py @@ -29,14 +29,6 @@ class Command(BaseCommand): """Convert a Wikipedia page name into a URL.""" name = "link" - def check(self, data): - # if ((data.is_command and data.command == "link") or - # (("[[" in data.msg and "]]" in data.msg) or - # ("{{" in data.msg and "}}" in data.msg))): - if data.is_command and data.command == "link": - return True - return False - def process(self, data): msg = data.msg diff --git a/earwigbot/commands/registration.py b/earwigbot/commands/registration.py index 44592ef..3ef641c 100644 --- a/earwigbot/commands/registration.py +++ b/earwigbot/commands/registration.py @@ -31,9 +31,7 @@ class Command(BaseCommand): def check(self, data): commands = ["registration", "reg", "age"] - if data.is_command and data.command in commands: - return True - return False + return data.is_command and data.command in commands def process(self, data): if not data.args: diff --git a/earwigbot/commands/remind.py b/earwigbot/commands/remind.py index 05360e6..4a7d6ed 100644 --- a/earwigbot/commands/remind.py +++ b/earwigbot/commands/remind.py @@ -30,9 +30,7 @@ class Command(BaseCommand): name = "remind" def check(self, data): - if data.is_command and data.command in ["remind", "reminder"]: - return True - return False + return data.is_command and data.command in ["remind", "reminder"] def process(self, data): if not data.args: diff --git a/earwigbot/commands/replag.py b/earwigbot/commands/replag.py index fce0240..ab81319 100644 --- a/earwigbot/commands/replag.py +++ b/earwigbot/commands/replag.py @@ -41,7 +41,8 @@ class Command(BaseCommand): conn = oursql.connect(**args) with conn.cursor() as cursor: - query = "SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) FROM recentchanges ORDER BY rc_timestamp DESC LIMIT 1" + query = """SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) + FROM recentchanges ORDER BY rc_timestamp DESC LIMIT 1""" cursor.execute(query) replag = int(cursor.fetchall()[0][0]) conn.close() diff --git a/earwigbot/commands/rights.py b/earwigbot/commands/rights.py index a2ad76d..63357c7 100644 --- a/earwigbot/commands/rights.py +++ b/earwigbot/commands/rights.py @@ -29,9 +29,7 @@ class Command(BaseCommand): def check(self, data): commands = ["rights", "groups", "permissions", "privileges"] - if data.is_command and data.command in commands: - return True - return False + return data.is_command and data.command in commands def process(self, data): if not data.args: diff --git a/earwigbot/commands/threads.py b/earwigbot/commands/threads.py index 8d1ed17..e1bbb22 100644 --- a/earwigbot/commands/threads.py +++ b/earwigbot/commands/threads.py @@ -32,9 +32,7 @@ class Command(BaseCommand): def check(self, data): commands = ["tasks", "task", "threads", "tasklist"] - if data.is_command and data.command in commands: - return True - return False + return data.is_command and data.command in commands def process(self, data): self.data = data diff --git a/earwigbot/config.py b/earwigbot/config.py index ab9eecd..cee3570 100644 --- a/earwigbot/config.py +++ b/earwigbot/config.py @@ -273,7 +273,10 @@ class BotConfig(object): >>> config.decrypt(config.irc, "frontend", "nickservPassword") # decrypts config.irc["frontend"]["nickservPassword"] """ - self._decryptable_nodes.append((node, nodes)) + signature = (node, nodes) + if signature in self._decryptable_nodes: + return # Already decrypted + self._decryptable_nodes.append(signature) if self.is_encrypted(): self._decrypt(node, nodes)