Guest User

Untitled

a guest
Feb 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1.  
  2. class Email
  3. @@blacklist = Array.new
  4. @@reg = /\A([-a-z0-9]+[\w\.\-\+]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  5.  
  6. def initialize(mailbox, host)
  7. @host = host
  8. @address = "#{mailbox}@#{host}"
  9. end
  10.  
  11. def blacklisted?
  12. return true if @@blacklist.include? @host
  13. end
  14.  
  15. def host_exists?
  16. puts "hosting #{@host}"
  17. if @host
  18. return true if `host #{@host}`.include? @host
  19. add_to_blacklist!
  20. return false
  21. end
  22. return false
  23. end
  24.  
  25. def valid?
  26. puts "is #{@address} valid?"
  27. return true if @@reg.match @address
  28. false
  29. end
  30.  
  31. def to_s
  32. @address
  33. end
  34.  
  35. def add_to_blacklist!
  36. @@blacklist << @host
  37. end
  38.  
  39. end
  40.  
  41. class Message
  42. require 'net/smtp'
  43.  
  44. def initialize(from, subject, body)
  45. @from = from
  46. @subject = subject
  47. @body = body
  48. end
  49.  
  50. def send(to, server)
  51. begin
  52. if @from.host_exists? && @from.valid? && !@from.blacklisted?
  53. Net::SMTP.start(server, 25) do |smtp|
  54. smtp.send_message msg, @from, to.to_s
  55. end
  56. end
  57. rescue Net::SMTPFatalError
  58. to.add_to_blacklist!
  59. rescue Net::SMTPServerBusy
  60. to.add_to_blacklist!
  61. end
  62. end
  63.  
  64. def msg
  65. "Subject:#{@subject}\r\n#{@body.to_s}"
  66. end
  67. end
  68.  
  69. class IMAP
  70. require 'net/imap'
  71.  
  72. def initialize(login, password)
  73. @connection = Net::IMAP.new('imap.gmail.com', 993, true)
  74. @connection.login(login, password)
  75. end
  76.  
  77. def select_folder(folder)
  78. @connection.select(folder)
  79. end
  80.  
  81. def all_mail
  82. @connection.uid_search(['ALL'])
  83. end
  84.  
  85. def get_message(id)
  86. @connection.uid_fetch(id, "(UID RFC822.SIZE ENVELOPE BODY[TEXT])")[0]
  87. end
  88.  
  89. def get_message_in_array(id)
  90. orig = self.get_message(id)
  91. return [Email.new( orig.attr["ENVELOPE"].from.first.mailbox ,orig.attr["ENVELOPE"].from.first.host), orig.attr["ENVELOPE"].subject, orig.attr["BODY[TEXT]"] ]
  92. end
  93.  
  94. def logout
  95. @connection.logout
  96. end
  97. end
  98. if ARGV[0].to_s == '-h'
  99. puts "spamer.rb username password to server -f folder"
  100. exit
  101. end
  102. username = ARGV.shift
  103. password = ARGV.shift
  104. to = ARGV.shift.split("@")
  105. server = ARGV.shift
  106. folder = "[Gmail]/Spam"
  107. while opt = ARGV.shift
  108. case opt.to_s
  109. when "-f"
  110. folder = ARGV.shift
  111. end
  112. end
  113.  
  114.  
  115. begin
  116. im = IMAP.new(username, password)
  117. im.select_folder(folder)
  118. to = Email.new(to[0], to[1])
  119. for id in im.all_mail
  120. mail = im.get_message_in_array(id)
  121. msg = Message.new(mail[0], mail[1], mail[2])
  122. puts "Sending from: #{mail[0]}"
  123. msg.send(to, server)
  124. end
  125. rescue Net::IMAP::ByeResponseError
  126. retry
  127. end
Add Comment
Please, Sign In to add comment