Browse Source

Progress!

Douglas William Thrift 14 years ago
parent
commit
96f5daabbd
1 changed files with 100 additions and 42 deletions
  1. 100 42
      locationbot.py

+ 100 - 42
locationbot.py

@@ -6,10 +6,14 @@
 # $Id$
 
 from ConfigParser import NoOptionError, SafeConfigParser
+import geojson
 import getpass
 import ircbot
 import irclib
+import os
 import sys
+import threading
+import urllib, urllib2
 import warnings
 
 class LocationBot(ircbot.SingleServerIRCBot):
@@ -47,6 +51,9 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		except NoOptionError, error:
 			sys.exit(error)
 
+		self.__locations_lock = threading.Lock()
+		self.__locations = []
+
 		ircbot.SingleServerIRCBot.__init__(self, servers, nick, 'Location Bot')
 
 	def __admin(self, nick):
@@ -59,39 +66,26 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 		return False
 
-	def __command(self, connection, event):
-		nick = irclib.nm_to_n(event.source())
+	def __do_latitude(self):
+		with self.__locations_lock:
+			self.__locations.append(('douglaswth', 'Isla Vista, CA, USA'))
 
-		try:
-			command, arguments = event.arguments()[0].split(None, 1)
-		except ValueError:
-			command = event.arguments()[0].strip()
-			arguments = ''
+		#try:
+		#	url = 'http://www.google.com/latitude/apps/badge/api?' + urllib.urlencode({'user': 0, 'type': 'json'})
+		#	response = urllib2.urlopen(url)
+		#	geo = geojson.load(response, object_hook = geojson.GeoJSON.to_instance)
 
-		if command.startswith('!'):
-			command = command[1:]
+		#	print geo.features[0].properties['reverseGeocode']
+		#except urllib2.URLError, error:
+		#	print error
 
-			if command == 'help':
-				commands = [
-					('help', 'show this help message'),
-				]
+		self.__latitude = threading.Timer(5 * 60, self.__do_latitude)
 
-				if self.__admin(event):
-					commands += [
-						('reload', 'reload my code'),
-					]
+		self.__latitude.start()
 
-				for command in commands:
-					connection.privmsg(nick, '!%-7s %s' % command)
-			elif self.__admin(event):
-				if command == 'reload':
-					raise Reload
-				else:
-					self.__unknown(connection, nick, command)
-			else:
-				self.__unknown(connection, nick, command)
-		elif event.eventtype() == 'privmsg':
-			self.__unknown(connection, nick, command)
+	def __restart(self, connection):
+		connection.disconnect('Restarting')
+		os.execvp(sys.argv[0], sys.argv)
 
 	def __unknown(self, connection, nick, command):
 		connection.privmsg(nick, 'unknown command (%s); try "!help"' % command)
@@ -119,30 +113,94 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		connection.nick(connection.get_nickname() + '_')
 
 	def on_privmsg(self, connection, event):
-		self.__command(connection, event)
+		nick = irclib.nm_to_n(event.source())
+		admin = self.__admin(event)
+
+		if not admin:
+			ok = False
+
+			for channel in self.channels.values():
+				if channel.has_user(nick):
+					ok = True
 
-	def on_pubmsg(self, connection, event):
-		self.__command(connection, event)
+					break
+
+			if not ok:
+				return
+
+		try:
+			command, arguments = event.arguments()[0].split(None, 1)
+		except ValueError:
+			command = event.arguments()[0].strip()
+			arguments = ''
+
+		if command.startswith('!'):
+			command = command[1:]
+
+			if command == 'help':
+				connection.privmsg(nick, 'Command     Arguments        Description')
+
+				commands = [
+					('help', '', 'show this help message'),
+					('login', 'user secret', 'login as user with secret'),
+					('register', 'user secret', 'register as user with secret'),
+					('status', '[user]', 'show where everybody or a user is'),
+				]
+
+				if True:
+					commands += [
+						('latitude', '[id]', 'shortcut for !set latitude [id]'),
+						('set', '[key [value]]', 'display or set variables'),
+						('unset', 'key', 'unset a variable'),
+					]
+
+				if admin:
+					commands += [
+						('restart', '', 'quit and join running more up to date code'),
+					]
+
+				commands.sort()
+
+				for command in commands:
+					connection.privmsg(nick, '!%-10s %-16s %s' % command)
+			elif admin:
+				if command == 'restart':
+					self.__restart(connection)
+				else:
+					self.__unknown(connection, nick, command)
+			else:
+				self.__unknown(connection, nick, command)
+		elif event.eventtype() == 'privmsg':
+			self.__unknown(connection, nick, command)
 
 	def on_welcome(self, connection, event):
 		for channel in self.__channels:
 			connection.join(channel)
 
-class Reload(Exception):
-	pass
+	def start(self):
+		self.__latitude = threading.Thread(None, self.__do_latitude)
+		self.__latitude.daemon = True
 
-if __name__ == '__main__':
-	import os.path
+		self.__latitude.start()
+		self._connect()
 
+		while True:
+			self.ircobj.process_once(0.2)
+
+			if self.__locations_lock.acquire(False):
+				if self.__locations:
+					for user, location in self.__locations:
+						for channel in self.channels.values():
+							self.connection.notice(channel, '%s is in %s' % (user, location))
+
+					self.__locations = []
+
+				self.__locations_lock.release()
+
+if __name__ == '__main__':
 	locationbot = LocationBot()
 
 	try:
-		while True:
-			try:
-				locationbot.start()
-			except Reload:
-				locationbot.connection.disconnect('Reloading')
-
-				locationbot = __import__(os.path.splitext(os.path.basename(__file__))[0]).LocationBot()
+		locationbot.start()
 	except KeyboardInterrupt:
 		locationbot.connection.disconnect('Oh no!')