IRC Bot: Difference between revisions

From Unallocated Space
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
== 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.


== Version 2 ==
 
== bot.py ==


<pre>
<pre>
Line 6: Line 9:
#stupid hacky irc bot
#stupid hacky irc bot


import socket,urllib,sys,threading,time,serial
#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


class pipein (threading.Thread):
    def run (self):
  def run (self):
       global irc
       global irc
       while True:
       while True:
         tmp=sys.stdin.readline()
         tmp=sys.stdin.readline().strip()
         if tmp !=  "":
         if tmp !=  "":
            irc.send ( 'PRIVMSG #unallocatedspace :\001ACTION '+tmp.strip()+'\001\r\n' )
    if tmp == "update":
reload(botfunc)
    else:
    irc.send('PRIVMSG #unallocatedspace :\001ACTION '+tmp.strip()+'\001\r\n')
             print tmp.strip()
             print tmp.strip()
         time.sleep(1)
         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'
network = 'irc.servercentral.net'
Line 74: Line 35:
irc.connect((network,port))
irc.connect((network,port))


 
print irc.recv(4096)
 
print irc.recv ( 4096 )
irc.send('NICK UnalloBot\r\n')
irc.send('NICK UnalloBot\r\n')
irc.send('USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n')
irc.send('USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n')
Line 91: Line 50:
                 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 commands:
if command in botfunc.commands:
send(irc,commands[command](data.strip()))
botfunc.send(irc,botfunc.commands[command](data.strip()))
else:
send(irc,'Not a valid command')


</pre>
</pre>
Line 100: Line 57:




== version 1 ==


== botfunc.py ==
<pre>
<pre>
#!/usr/bin/env python
import socket,urllib,time,serial,os
#stupid hacky irc bot


import socket,urllib2,sys,threading,time,serial
def send(irc,text):
if text.strip()!="":
        irc.send('PRIVMSG #unallocatedspace :\001ACTION '+str(text).strip()+'\001\r\n')


class pipein (threading.Thread):
def status(data):
  def run (self):
return open('/tmp/status').read()[1:]
      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'
def tweet(data):
#network = 'irc.prison.net'
data=urllib.urlopen('https://twitter.com/statuses/user_timeline/165951985.rss?count=1').read()
#network = 'irc.mzima.net'
data= data[data.find('<item>')+31:]
#network = 'irc.shoutcast.com'
return "Last Tweet: "+data[0:data.find('</title>')]
network = 'irc.servercentral.net'


port = 6667
def site(data):
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
        data=urllib.urlopen('http://www.unallocatedspace.org/uas/feed/rss/').read()
irc.connect ( ( network, port ) )
        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


print irc.recv ( 4096 )
def list_c(data):
irc.send ( 'NICK UnalloBot\r\n' )
return 'Available commands are: !status !sign !tweet !sign'
irc.send ( 'USER UnalloBot UnalloBot UnalloBot :Unallocated Bot\r\n' )
irc.send ( 'VERSION 1\r\n' )
irc.send ( 'JOIN #unallocatedspace\r\n' )


pipein().start()
def sign(data):
 
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:
         try:
                 sign=data[data.find("!sign")+5:]
                 if data=="":
                sign=sign.strip()
                        message='The last sign update read as: '+open('/tmp/sign','r').read()
                 if sign != "":
                 else:
                         if sign.find('<FO>')!=-1:
                         if '<FO>' in data:
                                 message="<FO> is not allowed."
                                 message="<FO> is not allowed"
                         else:
                         else:
 
                                s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ser=serial.Serial('/dev/ttyS0', 9600, timeout=1)
                                s.connect(('127.0.0.1',9001))
ser.write("<ID01>\r\n")
                                s.sendall(data)
res=ser.read(10)
                                message=s.recv(1024)
ser.close()
                                s.close()
if ">S" in res:
                                #print message
                                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:
         except socket.error:
                 message="Failed to update sign"
                 message="Failed to update sign"
         irc.send( 'PRIVMSG #unallocatedspace :\001ACTION '+message.strip()+'\001\r\n' )
         return message
  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:]
def mcpipe(data):
title=data[0:data.find("</title>")]
os.system("echo 'say "+data+" (!irc to reply)' > /home/minecraft/minecraft/mcpipe")
return ''


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


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


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




</pre>
</pre>

Revision as of 12:55, 6 April 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.


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}