IRCBot.py2 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. __Author__ = "Haelwenn Monnier (lanodan) <haelwenn.monnier@gmail.com>"
  4. __License__ = "CC-BY-SA"
  5. import socket, sys, re, time, urllib
  6. tetris=0
  7. urlRegex = re.compile('[hf]t+ps?://')
  8. def get_element(dataInput, elem):
  9. idx1=dataInput.find('<'+elem+'>')
  10. idx2=dataInput.find('</'+elem+'>')
  11. return dataInput[idx1+len('<'+elem+'>'):idx2].strip()
  12. # Output to the channel and console
  13. def printIrc(ircout):
  14. time.sleep(0.5)
  15. irc.send('PRIVMSG '+conf.channel+' :'+ircout+'\x0d\x0a')
  16. print 'PRIVMSG '+conf.channel+' :'+ircout
  17. #line: append to the log (output it to the console too)
  18. def log(line):
  19. print '[LOG]'+line
  20. log = open('IRCBot.log', 'a')
  21. log.write(line+'\n')
  22. log.close()
  23. # Load the config file (config.py)
  24. try:
  25. import conf
  26. except Exception, e:
  27. print 'Exception: '+str(e)
  28. print 'Exiting…'
  29. sys.exit(1)
  30. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  31. irc.connect((conf.server, conf.port))
  32. #if SSLEnabled:
  33. # irc = ssl.wrap_socket(irc)
  34. #
  35. irc.send('PASS '+conf.passwd+'\x0d\x0a')
  36. print 'PASS '+conf.passwd
  37. irc.send('NICK '+conf.nick+'\x0d\x0a')
  38. print 'NICK '+conf.nick
  39. irc.send('USER '+conf.nick+' irc bot :Bot made by lanodan using python!\x0d\x0a') #user authentication
  40. print 'USER '+conf.nick+' irc bot :Bot made by lanodan using python!'
  41. #while 1:
  42. # if irc.recv(2048).find('372') != -1:
  43. irc.send('JOIN '+conf.channel+'\x0d\x0a')
  44. print 'JOIN '+conf.channel
  45. # break
  46. time.sleep(1.5) # Random wait time, feel free to adjust it
  47. #while 1:
  48. # if irc.recv(2048).find('JOIN '+channel) != -1:
  49. printIrc(conf.welcomeMsg)
  50. # break
  51. # Make a while loop to alway parse input
  52. while 1:
  53. data=irc.recv(2048) # text is the socket input (I use text because input is already taken)
  54. text=data.lower() # text is the socket input (I use text because input is already taken)
  55. print data # print the input for debugging purpose
  56. # Below is a bunch of conditions to see if something the bot can deal with is found
  57. if text.find('ping :', 0, 7) != -1:
  58. host = data.split('PING :', 1)[1].strip()
  59. irc.send('PONG '+conf.nick+' '+host+'\x0d\x0a') # Return PONG back (sort of "I'm alive")
  60. print 'PONG '+host+'\x0d\x0a'
  61. if text.find(':!hi') !=-1:
  62. printIrc(data.split(':!hi')[1].strip()+' '+conf.welcomeMsg) # split the message received at the :!hi part
  63. if text.find(':!say') != -1:
  64. printIrc(data.split(':!say')[1].strip())
  65. if text.find(':!action') != -1:
  66. action = data.split(':!action')[1].strip()
  67. printIrc('\x01ACTION '+action+'\x01') # equivalent of /me
  68. if urlRegex.search(text) is not None: # Use httpRegex on input
  69. parse = re.findall('[HhFf][Tt]{1,2}[Pp][Ss]?://[^\{\}\|\\\^\~\[\]\"\'\`\(\)\<\>\ ]+', data) # parse URL
  70. try:
  71. url = str(parse[0]).rstrip() # Take the parsed link
  72. if (len(url) > 8) : # I assume a link is more than 8 characters long
  73. try:
  74. get = urllib.urlopen(url) # Open the link
  75. wget = get.read() # Read the input of the url
  76. print get.info() # Print Headers
  77. mimeType = get.info().type # Get the Content-Type
  78. get.close() # Close the connection
  79. # find the title
  80. if wget.find('<title>') != -1:
  81. title = get_element(wget, 'title')
  82. printIrc('Title: '+title)
  83. log(url+'; '+str(mimeType)+'; '+title)
  84. elif wget.find('<TITLE>') != -1:
  85. title = get_element(wget, 'TITLE')
  86. printIrc('Title: '+title)
  87. log(url+'; '+str(mimeType)+'; '+title)
  88. #If we can't find the title print the Content-Type
  89. else:
  90. printIrc('Content-Type: '+mimeType)
  91. log(url+'; '+str(mimeType))
  92. except Exception, e:
  93. printIrc('Exception: '+str(e))
  94. else:
  95. printIrc('Link too short (not more than 8)')
  96. except Exception, e:
  97. printIrc('Exception: '+str(e))
  98. # Output the link to find the source code
  99. if text.find(':!source') != -1:
  100. printIrc(conf.source)
  101. if text.find('kick '+conf.channel+' '+conf.nick) != -1:
  102. time.sleep(5)
  103. irc.send('JOIN '+conf.channel+'\x0d\x0a') #re-join the chan
  104. printIrc(welcomeMsg) # Say hello
  105. if data[0] == ':': # Commands that do not come directly from the server
  106. if text.find('join') != -1:
  107. username = data[1:].split('!')[0]
  108. if username == 'fuyuneko' or username == 'lanodan':
  109. printIrc(username+' : Okaeri-nasai ouji-sama')
  110. else:
  111. printIrc(username+': '+conf.welcomeMsg)
  112. if text.find('tetris') != -1 and tetris == 0:
  113. printIrc('Never gonna give you up.')
  114. printIrc('Never gonna let you down.')
  115. printIrc('Never gonna run around and desert you.')
  116. printIrc('Never gonna make you cry.')
  117. printIrc('Never gonna say goodbye.')
  118. printIrc('Never gonna tell a lie and hurt you.')
  119. tetris=1 # Prevent another rick roll
  120. if text.find(':!nsidentify') != -1:
  121. irc.send('NS identify '+conf.passwd+'\x0d\x0a')
  122. if text.find(':!msgidentify') != -1:
  123. irc.send('PRIVMSG '+conf.NickServ+' :identify '+conf.passwd+'\x0d\x0a')
  124. if text.find(':!stop in the name of sey') != -1:
  125. irc.send('QUIT :'+conf.quitMsg+'\x0d\x0a')
  126. time.sleep(1)
  127. break # break the loop
  128. if text == "":
  129. irc.send('QUIT :Empty socket input\x0d\x0a')
  130. time.sleep(1)
  131. break # break the loop
  132. irc.close() # Close the socket
  133. sys.exit() # Exit