Module: Mailpot::Mail

Defined in:
lib/mailpot/mail.rb

Class Method Summary collapse

Class Method Details

.add_message(message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mailpot/mail.rb', line 26

def add_message(message)
 if !@initialized
  initialize
 end
 msg = Hash.new
 msg[:source] = message[:source]
 msg[:sender] = message[:sender]
 msg[:source_ip] = message[:ip]
 msg[:recipients] = message[:recipients]
 msg[:probe] = detect_probe(message)
 # we must at this point detect probes because we need them to be sent
 encoded_message = Marshal.dump(msg)
 digest = Digest::MD5.hexdigest(encoded_message)
 store_message(digest, encoded_message)
end

.detect_probe(msg) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mailpot/mail.rb', line 42

def detect_probe(msg)
 mail = Mail.new(msg[:source])
 # Probes only have one recipient
 if msg[:recipients].length > 1
  return [false, false]
 end
 # probes are short
 if msg[:source].length > 1024
  return [false, false]
 end
 # ip address will be in the subject sometimes
 if mail.subject.include? Mailpot::Configuration.smtp_ip
  return [true, forward_probe(msg)]
 end
 return [false, false]
end

.forward_probe(msg) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/mailpot/mail.rb', line 59

def forward_probe(msg)
 r = Regexp.new(/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+?\.[a-zA-Z.]*\b/)
 sender = msg[:sender]
 sender = sender.scan(r)
 recipient = msg[:recipients].to_s
 recipient = recipient.scan(r)
 Net::SMTP.start('localhost') do | smtp|
  smtp.send_message msg[:source], sender, recipient
 end
end

.initializeObject

setup connections etc



17
18
19
20
21
22
23
24
# File 'lib/mailpot/mail.rb', line 17

def initialize
 yml = Mailpot::Configuration.key_data
 @bucket = yml['bucket']
 @queue = yml['queue']
 @s3 = AWS::S3.new(yml)
 @sqs = AWS::SQS.new(yml)
 @initialized = true
end

.store_message(key, value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mailpot/mail.rb', line 70

def store_message(key, value)
 Thread.new {
  mail = @s3.buckets[@bucket].objects[key]
  options = {
   :storage_class => :reduced_redundancy
  }
  mail.write(value, options)
  q = @sqs.queues[@queue]
  msg = q.send_message(key)
 }
end