Difference between revisions of "IRC Bot"

From Unallocated Space
Jump to navigation Jump to search
(Created page with "Needs to be re-written. "What a friggen mess." --C-P <pre> #!/usr/bin/env python #stupid hacky irc bot import socket,urllib2,sys,threading,time,serial class pipein (threading....")
 
Line 1: Line 1:
Needs to be re-written. "What a friggen mess." --C-P
+
 
 +
== Version 2 ==
 +
 
 +
<pre>
 +
#!/usr/bin/env python
 +
#stupid hacky irc bot
 +
 
 +
import socket,urllib,sys,threading,time,serial
 +
 
 +
class pipein (threading.Thread):
 +
  def run (self):
 +
      global irc
 +
      while True:
 +
        tmp=sys.stdin.readline()
 +
        if tmp !=  "":
 +
            irc.send ( 'PRIVMSG #unallocatedspace :\001ACTION '+tmp.strip()+'\001\r\n' )
 +
            print tmp.strip()
 +
        time.sleep(1)
 +
 
 +
def send(irc,text):
 +
        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 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:
 +
ser=serial.Serial('/dev/ttyS0', 9600, timeout=1)
 +
ser.write("<ID01>\r\n")
 +
                      res=ser.read(10)
 +
                      ser.close()
 +
                      if ">S" in res:
 +
                        host="127.0.0.1"
 +
                              port=9001
 +
                                s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 +
                                s.connect((host,port))
 +
                                s.sendall(data)
 +
                                s.close()
 +
                                print data
 +
                                message="Updating sign to "+data
 +
                        else:
 +
                        message="The sign is turned off"
 +
except socket.error:
 +
message="Failed to update sign"
 +
return message
 +
 
 +
 
 +
commands={'status':status,'tweet':tweet,'site':site,'sign':sign}
 +
 
 +
 
 +
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 commands:
 +
send(irc,commands[command](data.strip()))
 +
else:
 +
send(irc,'Not a valid command')
 +
 
 +
</pre>
 +
 
 +
 
 +
 
 +
== version 1 ==
  
 
<pre>
 
<pre>

Revision as of 13:11, 30 March 2011

Version 2

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

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

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

def send(irc,text):
        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 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:
				ser=serial.Serial('/dev/ttyS0', 9600, timeout=1)
				ser.write("<ID01>\r\n")
       		                res=ser.read(10)
       	        	        ser.close()
                       		if ">S" in res:
                        		host="127.0.0.1"
                               		port=9001
                                	s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                                	s.connect((host,port))
                                	s.sendall(data)
                                	s.close()
                                	print data
                                	message="Updating sign to "+data
                        	else:
                        		message="The sign is turned off"
	except socket.error:
		message="Failed to update sign"
	return message


commands={'status':status,'tweet':tweet,'site':site,'sign':sign}


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 commands:
			send(irc,commands[command](data.strip()))
		else:
			send(irc,'Not a valid command')


version 1

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

import socket,urllib2,sys,threading,time,serial

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

#network = 'irc.efnet.org'
#network = 'irc.prison.net'
#network = 'irc.mzima.net'
#network = 'irc.shoutcast.com'
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 1\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' )
   if data.find ( 'PRIVMSG #unallocatedspace :!status') != -1:
      status=open('/tmp/status').read()
      irc.send ( 'PRIVMSG #unallocatedspace :\001ACTION '+status[1:]+'\001\r\n' )
      print data,':',status[1:]
   if data.find( 'PRIVMSG #unallocatedspace :!sign' ) != -1:
        try:
                sign=data[data.find("!sign")+5:]
                sign=sign.strip()
                if sign != "":
                        if sign.find('<FO>')!=-1:
                                message="<FO> is not allowed."
                        else:

				ser=serial.Serial('/dev/ttyS0', 9600, timeout=1)
				ser.write("<ID01>\r\n")
				res=ser.read(10)
				ser.close()
				if ">S" in res:
	                                host="127.0.0.1"
	                                port=9001
	                                s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	                                s.connect((host,port))
	                                s.sendall(sign)
	                                s.close()
	                                print sign
        	                        message="Updating sign to "+sign
				else:
					message="The sign is turned off"
                else:
                        message = 'The last update read as: '+open('/tmp/sign','r').read()
        except socket.error:
                message="Failed to update sign"
        irc.send( 'PRIVMSG #unallocatedspace :\001ACTION '+message.strip()+'\001\r\n' )
   if data.find( 'PRIVMSG #unallocatedspace :!tweet' ) != -1:
        usock = urllib2.urlopen('https://twitter.com/statuses/user_timeline/165951985.rss?count=1')
        data = usock.read()
        usock.close()

        data= data[data.find('<item>')+31:]
        irc.send ( 'PRIVMSG #unallocatedspace :\001ACTION Last Tweet: '+data[0:data.find('</title>')]+'\001\r\n' )

   if data.find( 'PRIVMSG #unallocatedspace :!site' ) != -1:
	usock = urllib2.urlopen('http://www.unallocatedspace.org/uas/feed/rss/')
	data = usock.read()
	usock.close()

	data=data[data.find('<item>')+16:]
	title=data[0:data.find("</title>")]

	data=data[data.find('<link>')+6:]
	data=data[0:data.find("</link>")]

	irc.send ( 'PRIVMSG #unallocatedspace :\001ACTION Last Post: '+title+" - "+data+'\001\r\n' )

   print data