Browse Source

Checkpoint!

Douglas William Thrift 14 years ago
commit
91d522307a
3 changed files with 151 additions and 0 deletions
  1. 1 0
      .gitattributes
  2. 2 0
      .gitignore
  3. 148 0
      locationbot.py

+ 1 - 0
.gitattributes

@@ -0,0 +1 @@
+*.py	ident

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+*.py?
+locationbot.ini

+ 148 - 0
locationbot.py

@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+# Location Bot
+#
+# Douglas Thrift
+#
+# $Id$
+
+from ConfigParser import NoOptionError, SafeConfigParser
+import getpass
+import ircbot
+import irclib
+import sys
+import warnings
+
+class LocationBot(ircbot.SingleServerIRCBot):
+	def __init__(self):
+		config = SafeConfigParser({'hostname': ''})
+
+		config.read('locationbot.ini')
+
+		try:
+			nick = config.sections()[0]
+		except IndexError:
+			sys.exit('No nick configured')
+
+		servers = []
+
+		try:
+			for server in config.get(nick, 'servers').split():
+				server = server.split(':', 2)
+
+				if len(server) == 1:
+					servers.append((server[0], 6667))
+				else:
+					host = server[0]
+					port = int(server[1])
+					ssl = server[1].startswith('+')
+
+					if len(server) == 3:
+						servers.append((host, port, ssl, server[2]))
+					else:
+						servers.append((host, port, ssl))
+
+			self.__admins = config.get(nick, 'admins').split()
+			self.__channels = config.get(nick, 'channels').split()
+			self.__hostname = config.get(nick, 'hostname')
+		except NoOptionError, error:
+			sys.exit(error)
+
+		ircbot.SingleServerIRCBot.__init__(self, servers, nick, 'Location Bot')
+
+	def __admin(self, nick):
+		if isinstance(nick, irclib.Event):
+			nick = nick.source()
+
+		for admin in self.__admins:
+			if irclib.mask_matches(nick, admin):
+				return True
+
+		return False
+
+	def __command(self, connection, event):
+		nick = irclib.nm_to_n(event.source())
+
+		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':
+				commands = [
+					('help', 'show this help message'),
+				]
+
+				if self.__admin(event):
+					commands += [
+						('reload', 'reload my code'),
+					]
+
+				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 __unknown(self, connection, nick, command):
+		connection.privmsg(nick, 'unknown command (%s); try "!help"' % command)
+
+	def _connect(self):
+		password = None
+
+		if len(self.server_list[0]) != 2:
+			ssl = self.server_list[0][2]
+
+		if len(self.server_list[0]) == 4:
+			password = self.server_list[0][3]
+
+		try:
+			with warnings.catch_warnings():
+				warnings.filterwarnings('ignore', r'socket\.ssl\(\) is deprecated\.  Use ssl\.wrap_socket\(\) instead\.', DeprecationWarning)
+				self.connect(self.server_list[0][0], self.server_list[0][1], self._nickname, password, getpass.getuser(), self._realname, localaddress = self.__hostname, ssl = ssl)
+		except irclib.ServerConnectionError:
+			pass
+
+	def get_version(self):
+		return 'locationbot ' + sys.platform
+
+	def on_nicknameinuse(self, connection, event):
+		connection.nick(connection.get_nickname() + '_')
+
+	def on_privmsg(self, connection, event):
+		self.__command(connection, event)
+
+	def on_pubmsg(self, connection, event):
+		self.__command(connection, event)
+
+	def on_welcome(self, connection, event):
+		for channel in self.__channels:
+			connection.join(channel)
+
+class Reload(Exception):
+	pass
+
+if __name__ == '__main__':
+	import os.path
+
+	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()
+	except KeyboardInterrupt:
+		locationbot.connection.disconnect('Oh no!')