Browse Source

Status and fully working reload.

Douglas William Thrift 14 years ago
parent
commit
fdf7f0f356
2 changed files with 38 additions and 9 deletions
  1. 37 9
      locationbot.py
  2. 1 0
      locationbot.sql

+ 37 - 9
locationbot.py

@@ -71,6 +71,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 			self.ircobj.connections[0] = bot.ircobj.connections[0]
 			self.ircobj.connections[0].irclibobj = irclibobj
 			self.channels = bot.channels
+			self.connection = bot.connection
 			self.__locations = bot.__locations
 			self.__logins = bot.__logins
 			self.__reloading = True
@@ -78,6 +79,11 @@ class LocationBot(ircbot.SingleServerIRCBot):
 	def __admin(self, nickmask):
 		return _or(lambda admin: irclib.mask_matches(nickmask, admin), self.__admins)
 
+	def __db(self):
+		db = psycopg2.connect(self.__dsn)
+
+		return db, db.cursor()
+
 	def __channel(self, nick, exclude = None):
 		if exclude is not None:
 			exclude = irclib.irc_lower(exclude)
@@ -98,7 +104,6 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 		if login:
 			commands.update({
-				'latitude': ('[id]', 'shortcut for !set latitude [id]'),
 				'logout': ('', 'log out as nick'),
 				'set': ('[key [value]]', 'display or set variables'),
 				'unset': ('key', 'unset a variable'),
@@ -135,8 +140,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 			pass
 
 		self.__latitude_next = now.replace(minute = now.minute - now.minute % 5, second = 0, microsecond = 0) + timedelta(minutes = 5)
-		db = psycopg2.connect(self.__dsn)
-		cursor = db.cursor()
+		db, cursor = self.__db()
 
 		cursor.execute('select nick, channels, location, latitude from locationbot.nick where latitude is not null order by latitude')
 
@@ -202,8 +206,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 			return login
 
-		db = psycopg2.connect(self.__dsn)
-		cursor = db.cursor()
+		db, cursor = self.__db()
 
 		def success():
 			connection.privmsg(nick, 'successfully logged in as "%s"' % login)
@@ -238,7 +241,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		connection.privmsg(nick, 'reloading')
 
 	def __restart(self, connection):
-		connection.disconnect('Restarting')
+		connection.disconnect('restarting')
 		os.execvp(sys.argv[0], sys.argv)
 
 	def __say(self, connection, nick, arguments):
@@ -257,14 +260,37 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 		connection.privmsg(nick_channel, message)
 
+	def __status(self, connection, nick, login, arguments):
+		_nick = arguments.split(None, 1)[0] if arguments else None
+		db, cursor = self.__db()
+
+		cursor.execute('select nick, location, updated from locationbot.nick where location is not null' + (' and nick = %s' if _nick is not None else ''), (_nick,))
+
+		if cursor.rowcount == 0:
+			return connection.privmsg(nick, 'no location information for ' + ('"%s"' % _nick if _nick is not None else 'anybody'))
+
+		locations = cursor.fetchall()
+
+		if login:
+			cursor.execute('select timezone from locationbot.nick where timezone is not null and nick = %s', (login,))
+
+			timezone = pytz.timezone(cursor.fetchone()[0]) if cursor.rowcount == 1 else pytz.utc
+		else:
+			timezone = pytz.utc
+
+		connection.privmsg(nick, 'Nick                    Location                             When')
+
+		for _nick, location, updated in locations:
+			connection.privmsg(nick, '%-23s %-36s %s' % (_nick, location, timezone.normalize(updated.astimezone(timezone)).strftime('%Y-%m-%d %H:%M %Z')))
+
 	def __unknown(self, connection, nick, command):
 		connection.privmsg(nick, 'unknown command ("!%s"); try "!help"' % command)
 
 	def __who(self, connection, nick):
-		connection.privmsg(nick, 'Login Nick                      Nick Mask')
+		connection.privmsg(nick, 'Login Nick              Nick Mask')
 
 		for login in sorted(self.__logins.values()):
-			connection.privmsg(nick, '%-31s %s' % login)
+			connection.privmsg(nick, '%-23s %s' % login)
 
 	def _connect(self):
 		password = None
@@ -331,6 +357,8 @@ class LocationBot(ircbot.SingleServerIRCBot):
 				self.__help(connection, nick, admin, login, arguments)
 			elif command == 'login':
 				self.__login(connection, nickmask, nick, arguments)
+			elif command == 'status':
+				self.__status(connection, nick, login, arguments)
 			elif login and command == 'logout':
 				self.__logout(connection, nick)
 			elif admin and command == 'reload':
@@ -397,4 +425,4 @@ if __name__ == '__main__':
 
 			bot.start()
 	except KeyboardInterrupt:
-		bot.connection.disconnect('Oh no!')
+		bot.connection.disconnect('oh no!')

+ 1 - 0
locationbot.sql

@@ -12,6 +12,7 @@ create table locationbot.nick (
 	secret char(32) not null,
 	masks text[],
 	channels text[],
+	timezone text,
 	location text,
 	updated timestamp with time zone,
 	latitude bigint