IRC Bot: Difference between revisions

From Unallocated Space
Jump to navigation Jump to search
No edit summary
No edit summary
Line 30: Line 30:
         time.sleep(1)
         time.sleep(1)


network = 'irc.servercentral.net'


port=6667
while True:
irc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
irc.connect((network,port))
                network = 'irc.mzima.net'
network = 'irc.prison.net'


print irc.recv(4096)
irc.send('NICK UnalloBot\r\n')
port=6667
irc.send('USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n')
irc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
irc.send('VERSION 2\r\n')
irc.connect((network,port))
irc.send('JOIN #unallocatedspace\r\n')
print irc.recv(4096)
irc.send('NICK UnalloBot\r\n')
irc.send('USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n')
irc.send('VERSION 2\r\n')
irc.send('JOIN #unallocatedspace\r\n')


pipein().start()
pipein().start()


while True:
while True:
data=irc.recv(4096)
data=irc.recv(4096)
if data.find('PING')!=-1:
if data.find('PING')!=-1:
irc.send ('PONG '+data.split()[1]+'\r\n')
irc.send ('PONG '+data.split()[1]+'\r\n')
elif data.find('PRIVMSG #unallocatedspace :!')!=-1:
elif data.find('PRIVMSG #unallocatedspace :!')!=-1:
                data=data[data.find(' :!')+3:].strip()
                data=data[data.find(' :!')+3:].strip()
                command,u,data=data.partition(" ")
                command,u,data=data.partition(" ")
if command in botfunc.commands:
if command in botfunc.commands:
botfunc.send(irc,botfunc.commands[command](data.strip()))
botfunc.send(irc,botfunc.commands[command](data.strip()))
except:
print "something fucked up"
time.sleep(30)


</pre>
</pre>
Line 61: Line 69:
== botfunc.py ==
== botfunc.py ==
<pre>
<pre>
import socket,urllib,time,serial,os
import socket,urllib,time,serial,os,string


def send(irc,text):
def send(irc,text):
Line 71: Line 79:


def tweet(data):
def tweet(data):
data=urllib.urlopen('https://twitter.com/statuses/user_timeline/165951985.rss?count=1').read()
        data=urllib.urlopen('https://twitter.com/statuses/user_timeline/165951985.rss?count=1').read()
data= data[data.find('<item>')+31:]
        data=data.replace('\n',' ')
return "Last Tweet: "+data[0:data.find('</title>')]
        data= data[data.find('<item>')+31:]
        return "Last Tweet: "+data[0:data.find('</title>')]


def site(data):
def site(data):
Line 83: Line 92:
         return 'Last Post: '+title+" - "+data
         return 'Last Post: '+title+" - "+data


def list_c(data):
def help(data):
return 'Available commands are: !status !sign !tweet !sign'
data=data.replace('!','')
if data=='status':
return '!status returns the current status of the space. (open/closed)'
elif data=='sign':
return '!sign returns or updates the text displayed on the prolite LED sign.'
elif data=='tweet':
return '!tweet returns the latest tweet on the @Unallocated twitter account.'
elif data=='site':
return '!site returns the latest blog post on http://unallocatedspace.org/'
elif data=='mc':
return '!mc is a hidden command that facilitates live communication with people playing on the Unallocated Minecraft server.'
 
elif data=='rollcall':
return '!rollcall lists Unallocated Space members that have checked into the space with their UAS member smart cards during the current session (opening to closing)'
elif data=='phone':
return '!phone prints the phone number of the space. 512-943-2827. ooh how meta.'
 
else:
return 'Available commands are: !status !rollcall !sign !tweet !site !phone'


def sign(data):
def sign(data):
         try:
         try:
                 if data=="":
                 if data=="":
Line 99: Line 126:
                                 message=s.recv(1024)
                                 message=s.recv(1024)
                                 s.close()
                                 s.close()
                                #print message
         except socket.error:
         except socket.error:
                 message="Failed to update sign"
                 message="Failed to update sign"
Line 105: Line 132:


def mcpipe(data):
def mcpipe(data):
return 'Not right now.'
os.system("echo 'say "+data+" (!irc to reply)' > /home/minecraft/minecraft/mcpipe")
os.system("echo 'say "+data+" (!irc to reply)' > /home/minecraft/minecraft/mcpipe")
return ''
return ''
def rollcall(data):
        status=open("/tmp/status").read()
        if status[0:1]=="+":
                peps=open("/uas/smart/whoishere").read().strip()
                peeps = ', '.join(list(set(peps.split('\n'))))
                if peeps.strip() != "":
                        return "The following people have checked into the space this session. "+peeps[0:]
                else:
                        return "No one has checked into the space this session."
        else:
                return "The space is closed, Rollcall is not allowed"
def trollcall(data):
trollface=rollcall(data)
return trollface.replace('the space this session. ','your mom. ')
def pronto(data):
return "damnit pronto....."
def phone(data):
return "Call us at 512-943-2827!"


def update(data):
def update(data):
Line 112: Line 164:




commands={'status':status,'tweet':tweet,'site':site,'sign':sign,'mc':mcpipe,'list':list_c,    'twit':tweet,'twat':tweet}
commands={'status':status,'rollcall':rollcall,'tweet':tweet,'site':site,'sign':sign,'mc':mcpipe,'phone':phone,'help':help,    'blog':site,'twit':tweet,'twat':tweet,'brocall':rollcall,'trollcall':trollcall,'bagels':pronto}
 




</pre>
</pre>

Revision as of 15:02, 6 September 2011

Version 2.5

Major addition to 2.5 is the ability to add new commands and edit old commands on the fly by editing botfunc.py then echoing "update" into the irc named pipe.

Related: IRC & Minecraft

bot.py

#!/usr/bin/env python
#stupid hacky irc bot

#import socket,urllib,sys,threading,time,serial
import socket,urllib,sys,threading,time,serial,botfunc

class pipein(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
	threading.Thread.daemon = True

    def run (self):
      global irc
      while True:
         tmp=sys.stdin.readline().strip()
         if tmp !=  "":
	    if tmp == "update":
		reload(botfunc)
	    else:
	    	irc.send('PRIVMSG #unallocatedspace :\001ACTION '+tmp.strip()+'\001\r\n')
            print tmp.strip()
         time.sleep(1)


while True:
	try:
                network = 'irc.mzima.net'
		network = 'irc.prison.net'

	
		port=6667
		irc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		irc.connect((network,port))
	
		print irc.recv(4096)
		irc.send('NICK UnalloBot\r\n')
		irc.send('USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n')
		irc.send('VERSION 2\r\n')
		irc.send('JOIN #unallocatedspace\r\n')

		pipein().start()

		while True:
			data=irc.recv(4096)
			if data.find('PING')!=-1:
				irc.send ('PONG '+data.split()[1]+'\r\n')
			elif data.find('PRIVMSG #unallocatedspace :!')!=-1:
		                data=data[data.find(' :!')+3:].strip()
		                command,u,data=data.partition(" ")
				if command in botfunc.commands:
					botfunc.send(irc,botfunc.commands[command](data.strip()))
	except:
		print "something fucked up"
	time.sleep(30)



botfunc.py

import socket,urllib,time,serial,os,string

def send(irc,text):
	if text.strip()!="":
	        irc.send('PRIVMSG #unallocatedspace :\001ACTION '+str(text).strip()+'\001\r\n')

def status(data):
	return open('/tmp/status').read()[1:]

def tweet(data):
        data=urllib.urlopen('https://twitter.com/statuses/user_timeline/165951985.rss?count=1').read()
        data=data.replace('\n',' ')
        data= data[data.find('<item>')+31:]
        return "Last Tweet: "+data[0:data.find('</title>')]

def site(data):
        data=urllib.urlopen('http://www.unallocatedspace.org/uas/feed/rss/').read()
        data=data[data.find('<item>')+16:]
        title=data[0:data.find("</title>")]
        data=data[data.find('<link>')+6:]
        data=data[0:data.find("</link>")]
        return 'Last Post: '+title+" - "+data

def help(data):
	data=data.replace('!','')
	if data=='status':
		return '!status returns the current status of the space. (open/closed)'
	elif data=='sign':
		return '!sign returns or updates the text displayed on the prolite LED sign.'
	elif data=='tweet':
		return '!tweet returns the latest tweet on the @Unallocated twitter account.'
	elif data=='site':
		return '!site returns the latest blog post on http://unallocatedspace.org/'
	elif data=='mc':
		return '!mc is a hidden command that facilitates live communication with people playing on the Unallocated Minecraft server.'

	elif data=='rollcall':
		return '!rollcall lists Unallocated Space members that have checked into the space with their UAS member smart cards during the current session (opening to closing)'
	elif data=='phone':
		return '!phone prints the phone number of the space. 512-943-2827. ooh how meta.'

	else:
		return 'Available commands are: !status !rollcall !sign !tweet !site !phone'

def sign(data):	
        try:
                if data=="":
                        message='The last sign update read as: '+open('/tmp/sign','r').read()
                else:
                        if '<FO>' in data:
                                message="<FO> is not allowed"
                        else:
                                s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                                s.connect(('127.0.0.1',9001))
                                s.sendall(data)
                                message=s.recv(1024)
                                s.close()
					
        except socket.error:
                message="Failed to update sign"
        return message

def mcpipe(data):
	return 'Not right now.'
	os.system("echo 'say "+data+" (!irc to reply)' > /home/minecraft/minecraft/mcpipe")
	return ''

def rollcall(data):
        status=open("/tmp/status").read()
        if status[0:1]=="+":
                peps=open("/uas/smart/whoishere").read().strip()
                peeps = ', '.join(list(set(peps.split('\n'))))
                if peeps.strip() != "":
                        return "The following people have checked into the space this session. "+peeps[0:]
                else:
                        return "No one has checked into the space this session."
        else:
                return "The space is closed, Rollcall is not allowed"

def trollcall(data):
	trollface=rollcall(data)
	return trollface.replace('the space this session. ','your mom. ')	

def pronto(data):
	return "damnit pronto....."

def phone(data):
	return "Call us at 512-943-2827!"



def update(data):
        reload(botfunc)


commands={'status':status,'rollcall':rollcall,'tweet':tweet,'site':site,'sign':sign,'mc':mcpipe,'phone':phone,'help':help,    'blog':site,'twit':tweet,'twat':tweet,'brocall':rollcall,'trollcall':trollcall,'bagels':pronto}