Class: EmailAuthentication::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/email-authentication/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addressObject

Returns the value of attribute address.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def address
  @address
end

#domainObject

Returns the value of attribute domain.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def domain
  @domain
end

#fromObject

Returns the value of attribute from.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def from
  @from
end

#fromdomainObject

Returns the value of attribute fromdomain.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def fromdomain
  @fromdomain
end

#messageObject

Returns the value of attribute message.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def message
  @message
end

#mxObject

Returns the value of attribute mx.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def mx
  @mx
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/email-authentication/base.rb', line 10

def name
  @name
end

Class Method Details

.check(address, from) ⇒ Object



14
15
16
17
# File 'lib/email-authentication/base.rb', line 14

def self.check(address,from)
  tmp=self.new
  return tmp.check(address,from)
end

Instance Method Details

#check(address, from) ⇒ Object

run all the checks



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/email-authentication/base.rb', line 120

def check(address,from)
  self.set_address(address,from)
  @message=[]
  puts "checking #{@address}"
  ['format','mx','smtp'].each { |cmd| 
      cmdstring="check_#{cmd}"
      res,msg= self.send(cmdstring)
       @flag=@flag && res
       @message << msg }
  [@flag,@message.join(',').to_s]
end

#check_formatObject

this needs work. Anyone who can improve the regex i would be happy to put in their changes see alsothe validate_email_format gem for rails



28
29
30
31
32
33
34
35
36
37
# File 'lib/email-authentication/base.rb', line 28

def check_format
  @@email_regex = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Z‌​a-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i
  res=(@address =~ @@email_regex)
  #puts " res is #{res}"
  if res
    [true,"format ok"]
  else
    [false,"format failed"]
  end
end

#check_mxObject

check the mx domain



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/email-authentication/base.rb', line 44

def check_mx
  domain=self.address.split('@')
  @domain = domain[1]
  @name=domain[0]
  #puts "domain is #{domain}"
  flag=false
  if @domain!=nil
        begin
         ret = self.resolver.query(@domain, Types.MX)
          if ret.answer!=nil and ret.rcode=='NOERROR'
            @mx=ret.answer.first.exchange.to_s if ret.answer!=nil 
            @mx=@mx.downcase
            msg= "mx record #{self.mx}"
            puts msg
            flag=true
          end
         rescue Dnsruby::NXDomain 
           msg="non existing domain #{@domain}"
           puts msg
         rescue Exception => e
              msg="exception #{e.message}"
              puts msg
         end
      
  else
    msg="nil domain"
  end
  # puts "ret is #{ret.inspect}"
  [flag,msg]
end

#check_smtpObject

need to think about this and check the domain via telnet S: 220 smtp.example.com ESMTP Postfix C: HELO relay.example.org S: 250 Hello relay.example.org, I am glad to meet you C: MAIL FROM:<[email protected]> S: 250 Ok C: RCPT TO:<[email protected]> S: 250 Ok



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/email-authentication/base.rb', line 83

def check_smtp
   flag=false
   msg='smtp not ok'
   domain=self.from.split('@')
   @fromdomain = domain[1]
   if @mx.include?('google') or @mx.include?('live.com')
      # DO I Need to do this?
      flag=true
      msg="smtp not checked since google or live: #{@mx}"
   else
     begin 
       smtp = Net::Telnet::new("Host" => @mx, 'Port' => 25, "Telnetmode" => false, "Prompt" => /^\+OK/)
       c=""
       msg=c
       cmd="HELO " + @fromdomain
       smtp.cmd('String' => cmd, 'Match'=> /^250/) { |c| #print "CMD: #{cmd} RESP: #{c}" 
              msg << c}
       cmd="MAIL FROM:<" +@from+ ">"
       sleep 0.5
       smtp.cmd('String' => cmd, 'Match'=> /^250/ ) { |c| #print "CMD: #{cmd} RESP: #{c}" 
                msg << c}
       cmd="RCPT TO:<" +@address+ ">"
       sleep 0.5
       smtp.cmd('String' => cmd, 'Match'=> /^250/ ) { |c| print "CMD: #{cmd} RESP: #{c}" 
                        msg= "smtp test: #{cmd} resp: #{c}"
                        flag=true if c.include?('250') }
       cmd='quit'
       smtp.cmd('String' => cmd, 'Match'=> /^221/ ) { |c| print "CMD: #{cmd} RESP: #{c}"           }
     rescue Exception => e
       @flag=false
       msg= "smtp exception #{e.message}"
     end
   end if @mx!=nil
   
  [flag,msg]
end

#debugObject



11
12
13
# File 'lib/email-authentication/base.rb', line 11

def debug
  true
end

#resolverObject

cache the dns resolver



39
40
41
42
# File 'lib/email-authentication/base.rb', line 39

def resolver
  @resolver = Dnsruby::Resolver.new if @resolver==nil
  @resolver
end

#set_address(address, from = "") ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/email-authentication/base.rb', line 18

def set_address(address,from="")
  raise "address nil" if address==nil
  raise "address blank" if address==""
  raise "from address blank" if from==""
  self.address=address.to_s
  self.from=from
  @flag=true
end