IRC Bot

From Unallocated Space
Revision as of 12:55, 6 April 2011 by C-P (talk | contribs)
Jump to navigation Jump to search

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.


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)

network = 'irc.servercentral.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()))



botfunc.py

import socket,urllib,time,serial,os

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[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 list_c(data):
	return 'Available commands are: !status !sign !tweet !sign'

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()
                                #print message
        except socket.error:
                message="Failed to update sign"
        return message

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

def update(data):
        reload(botfunc)


commands={'status':status,'tweet':tweet,'site':site,'sign':sign,'mc':mcpipe,'list':list_c,    'twit':tweet,'twat':tweet}