IRC ACKspace-statusbot

From Hackerspace ACKspace
Revision as of 20:07, 17 January 2013 by StefandeVries (talk | contribs)
Jump to: navigation, search
Project: IRC ACKspace-statusbot
Featured:
State Completed
Members StefandeVries
GitHub No GitHub project defined. Add your project here.
Description Een IRC-bot die op commando de status van de space aangeeft.
Picture
No project picture! Fill in form Picture or Upload a jpeg here

ACKbot zit in ons IRC-kanaal #ackspace op irc.freenode.org. Met het commando !status geeft het aan of de space gesloten of geopend is. De broncode (Python):

# -*- coding: utf-8 -*-
#       ACKbot.py
#       
#       Copyright 2013 Stefan de Vries <stefandevries1994@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
# 
import socket
import urllib2 

def space_open():
	print "Space state called for."
	website = urllib2.urlopen("https://ackspace.nl/spacestate.php") 
	website_html = website.read() 
	if website_html.count("Closed"):
		return 0
	else:
		return 1
			

class IRC:
	def __init__(self, host="irc.freenode.org", port=6667, nick="ACKbot", ident="ACKbot", realname="ACKbot"):		
		self.conn = socket.socket()
		self.conn.settimeout(300.0)	
		self.conn.connect((host, port))
		self.conn.send("NICK %s\r\n" % nick)
		self.conn.send("USER %s %s bla :%s\r\n" % (ident, host, realname))
		self.conn.send("JOIN :#ackspace\r\n:")
				
	def mainLoop(self):
		readbuffer = ""
		while 1:			
			try:
				readbuffer = readbuffer + self.conn.recv(1024)
			except socket.timeout:
				raise ConnError()
				
			message = readbuffer.split("\n")
			readbuffer = message.pop()
			for line in message:
				line = line.rstrip()
				line = line.split()	
				print line
				if line[0] == "PING":
					self.conn.send("PONG %s\r\n" % line[1])
				elif len(line) == 4:
					if line[3] == ":!status":
						ret = space_open()
						if ret:
							self.conn.send("PRIVMSG #ackspace :De space is open!\r\n")
						else:
							self.conn.send("PRIVMSG #ackspace :De space is dicht.\r\n")
						print "Sent response."	

if __name__ == '__main__':
	instance = IRC()
	instance.mainLoop()	


Op dit moment draait het op de VPS van StefandeVries. Wellicht kan in de toekomst gezocht worden naar eigen hosting.