Advertisement
amigojapan

Python simple bot

Apr 25th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. #Copyright 2014 Usmar Padow (amigojapan) usmpadow@gmail.com
  2. import socket
  3. import string
  4. import time
  5. from random import randint
  6.  
  7. #Configuration
  8. HOST="irc.dal.net"
  9. PORT=6667
  10. NICK="DataBot"
  11. PASS="enterprise"
  12. IDENT="DataBot"
  13. REALNAME="DataBot"
  14. CHAN="#depression"
  15. ECHO=True
  16. TIMEOUT=200
  17. #DELAY_FOR_MOTD=30#this is the delay that I estimate may take for MOTD to come out( quick and dirty fix, fix later)
  18.  
  19.  
  20. readbuffer=""
  21. def IRCconnect(s):
  22.     s.settimeout(TIMEOUT)#this value seems to be in seconds
  23.     s.connect((HOST, PORT))
  24.     ##join with a random nickname, release the real nick then identify
  25.     RANDNICK="RANDNICK"+str(randint(100,1000))
  26.     s.send("PASS testpassword\r\n")
  27.     s.send("NICK %s\r\n" % RANDNICK)   
  28.     s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
  29.     #print "Delaying "+str(DELAY_FOR_MOTD)+" Seconds in hope MOTD has gone by"
  30.     #time.sleep(DELAY_FOR_MOTD)
  31.     s.send("PRIVMSG NickServ@services.dal.net :RELEASE "+NICK+" "+PASS+"\r\n")
  32.     s.send("NICK "+NICK+"\r\n")
  33.     s.send("PRIVMSG NickServ@services.dal.net :IDENTIFY "+PASS+"\r\n")
  34.     s.send("JOIN :%s\r\n" % CHAN)
  35.     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "bot activated"))
  36.  
  37. s=socket.socket( )
  38. IRCconnect(s)
  39.  
  40.  
  41. while 1:
  42.     try:
  43.         readbuffer=readbuffer+s.recv(1024)
  44.         if ECHO:
  45.             print readbuffer
  46.         temp=string.split(readbuffer, "\n")
  47.         readbuffer=temp.pop( )
  48.         #put all parameters after the commands in the params variable and command in command variable
  49.         for line in temp:
  50.             line=string.rstrip(line)
  51.             line=string.split(line)
  52.         if len(line)>3:
  53.             command= line[3][1:len(line[3])]
  54.             params=""
  55.             for a in range(4, len(line)):
  56.                 params=params+line[a]+" "
  57.             params=params[:-1]#strip the extra space
  58.         #Bot commands
  59.         #if command=="!say":
  60.         #   s.send("PRIVMSG %s :%s\r\n" % (CHAN, params))
  61.         if command=="!help":
  62.             s.send("PRIVMSG %s :%s\r\n" % (CHAN, "Sorry, help is not available"))
  63.             command=""
  64.            
  65.         if(line[0]=="PING"):
  66.             s.send("PONG %s\r\n" % line[1])
  67.     except socket.timeout:
  68.         s.shutdown(socket.SHUT_RDWR)
  69.         s=socket.socket( )
  70.         print "Connection timed out, reconnecting..."
  71.         IRCconnect(s)
  72.     except socket.error:
  73.         s.shutdown(socket.SHUT_RDWR)
  74.         s=socket.socket( ) 
  75.         print "Connection reset by peer, reconnecting..."
  76.         IRCconnect(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement