spacepaste

  1.  
  2. #### todo make nick fetcher into a class so that other parts of the script can access it.
  3. #!/usr/bin/env python
  4. import socket
  5. # This is to identify your user/pass to the IRC server so your bot can be masked
  6. # Thought it was safer to have it on user input rather than hardcoded
  7. user = raw_input("enter user ")
  8. secret = raw_input("enter password ")
  9. # name of the bot, channel and irc network
  10. botnick = raw_input("BOT NICKNAME ")
  11. channel = raw_input("IRC CHANNEL ")
  12. network = 'irc.freenode.net'
  13. # irc protocol
  14. port = 6667
  15. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16. irc.connect((network, port))
  17. print irc.recv(4096)
  18. # bot will identify to the IRC server and join desired channel
  19. irc.send('NICK ' + botnick + '\r\n')
  20. irc.send('USER vitebot vitebot vitebot:Python IRC\r\n')
  21. irc.send('nickserv identify ' + user + ' ' + secret+ '\r\n')
  22. irc.send('JOIN '+ channel +'\r\n')
  23. irc.send('PRIVMSG '+ channel +' :Hello World.\r\n')
  24. irc.send('chanserv op ' + channel + " " + botnick + '\r\n')
  25. while True:
  26. # Creating shortcut variables
  27. data = irc.recv(4096)
  28. find = data.find
  29. send = irc.send
  30. print data
  31. # This fetches the nicks
  32. nick = data.split('!')[0]
  33. nick = nick.replace(':', ' ')
  34. nick = nick.replace(' ', '')
  35. nick = nick.strip(' \t\n\r')
  36. # PING PONG with the IRC server to avoid getting booted
  37. if find('PING') != -1:
  38. send('PONG ' + data.split()[1] + '\r\n')
  39. print "PONG SENT"
  40. print data
  41. # Establishing bot behaviour
  42. elif find(':!' + botnick) != -1:
  43. send('PRIVMSG ' + channel + ' : Hi\r\n')
  44. print "Hi SENT"
  45. print data
  46. # This will only let the user end the script via IRC
  47. elif find(':!op') != -1:
  48. if user == nick:
  49. send('chanserv op ' + channel + " " + user + '\r\n')
  50. print data
  51. elif find('!quit') != -1:
  52. if user == nick:
  53. send(' PRIVMSG ' + channel + ': Good Bye')
  54. send('quit\r\n')
  55. print data
  56. quit()
  57.