IRC Bot

From Unallocated Space
Revision as of 19:47, 5 March 2011 by C-P (talk | contribs) (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....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Needs to be re-written. "What a friggen mess." --C-P

#!/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