Aluf

Python spam bot

Jan 20th, 2015
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1.  
  2.  
  3. import urllib2
  4. import random
  5. import re
  6. import socket
  7. import thread
  8. import time
  9. from HTMLParser import HTMLParser
  10.  
  11.  
  12. #-- IRC Setup --#
  13.  
  14. def sendmsg(msg, channel):
  15. irc.send ( 'PRIVMSG ' + channel + ' :' + msg + '\r\n' )
  16.  
  17. def sendcmd(cmd, data):
  18. irc.send ( cmd + ' ' + data + '\r\n' )
  19.  
  20. def pingpong(data):
  21. if data.find ( 'PING' ) != -1:
  22. irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
  23.  
  24. def pongping(network, data):
  25. sendcmd('PING', network)
  26. if 'PING' in data:
  27. return True
  28. print 'Connected to ' + network
  29. else:
  30. return False
  31.  
  32. def rcvdata(data):
  33. global channel
  34.  
  35. if data.find ( 'PRIVMSG' ) != -1:
  36. nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
  37. message = ':'.join ( data.split ( ':' ) [ 2: ] )
  38. print nick + ':', message.rstrip("\n\r")
  39. if message.find ('!start') == 0:
  40. sendmsg("Starting senbot at " + message[7:], channel)
  41. thread.start_new_thread(fgetse, (message[7:],))
  42. if message.find ('!quit') == 0:
  43. sendcmd('QUIT', message[5:])
  44.  
  45.  
  46. #---------------#
  47.  
  48.  
  49. class getlinks(HTMLParser): #Parses html to find links
  50. def handle_starttag(self, tag, attrs): #Handles any opening tag
  51. global nurl
  52. if tag == 'a': #If the tag is a link
  53. for atr in attrs:
  54. if atr[0] == 'href': #Get it's href sttribute
  55. if atr[1][0:7] == 'http://' or atr[1][0:8] == 'https://': #Make sure it's http of https
  56. if random.randrange(0, 8) == 1: #There's a one in nine chance
  57. nurl = atr[1] #That this it will go to this link next
  58.  
  59. class striphtml(HTMLParser): #Strips html tags from html data. Ripped from stackoverflow
  60. def __init__(self):
  61. self.reset()
  62. self.fed = []
  63. def handle_data(self, d): #if it's data (not a tag)
  64. self.fed.append(d) #add it to fed[].
  65. def get_data(self): #return fed[]
  66. return ''.join(self.fed)
  67.  
  68.  
  69. def striptags(html): #Get html without all that annoying markup!
  70. s = striphtml() #call the parser
  71. s.feed(html)
  72. return s.get_data() #Get the data
  73.  
  74. def getse(text): #Gets a sentence form 'text'
  75. sens = re.findall(r'[A-Z][A-Za-z ,]+[!.?]', text) #Starts with a capital, has 1 or more letters, commas, or spaces, and ends in a !, . , or ?
  76. if len(sens) != 0: #If the
  77. rtn = sens[len(sens)-random.randrange(0, len(sens))]
  78. if len(rtn) > 20:
  79. return rtn
  80.  
  81. def fgetse(surl):
  82. global nurl
  83. global channel
  84. nurl = surl
  85. user_agent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' #Set as googlebot to get access to forums/etc.
  86. headers = { 'User-Agent' : user_agent }
  87. while True:
  88. try:
  89. response = urllib2.urlopen(nurl) #Open link
  90. html = response.read()
  91. linkp = getlinks()
  92. linkp.feed(html)
  93. #print nurl
  94. sen = getse(striptags(html))
  95. if sen != None:
  96. sendmsg(sen, channel)
  97. except: #Oh noes!
  98. nurl = surl #Go back to lulzy place
  99.  
  100. nurl = ''
  101.  
  102. network = 'irc.hackthissite.org' #raw_input('What network do you want to join?\n')
  103. channel = raw_input('What channel do you want to join?\n')
  104. nick = 'senbot'
  105. port = 6667
  106. connected = False
  107.  
  108. irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  109. irc.connect ( ( network, port ) )
  110.  
  111. sendcmd('NICK', nick)
  112. sendcmd( 'USER', nick + ' foo bar ' + nick )
  113.  
  114. conn = False
  115.  
  116. while True:
  117. data = irc.recv(4096)
  118. pingpong(data)
  119.  
  120. if connected:
  121. if not conn:
  122. sendcmd('PRIVMSG', 'NickServ IDENTIFY mypassword')
  123. data = irc.recv ( 4096 )
  124. while data.find(nick) == -1:
  125. time.sleep(1)
  126. sendcmd('JOIN', channel)
  127. sendcmd('/mode', nick + ' +B')
  128. sendmsg('ready!', channel)
  129. conn = True
  130. else:
  131. thread.start_new_thread(rcvdata, (data,))
  132. else:
  133. connected = pongping(network, data)
Advertisement
Add Comment
Please, Sign In to add comment