Class: Popper::MailAccount

Inherits:
Object
  • Object
show all
Defined in:
lib/popper/mail_account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MailAccount

Returns a new instance of MailAccount.



9
10
11
# File 'lib/popper/mail_account.rb', line 9

def initialize(config)
  self.config = config
end

Instance Attribute Details

#complete_listObject

Returns the value of attribute complete_list.



7
8
9
# File 'lib/popper/mail_account.rb', line 7

def complete_list
  @complete_list
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/popper/mail_account.rb', line 7

def config
  @config
end

#current_listObject

Returns the value of attribute current_list.



7
8
9
# File 'lib/popper/mail_account.rb', line 7

def current_list
  @current_list
end

Instance Method Details

#match_rule?(mail) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/popper/mail_account.rb', line 78

def match_rule?(mail)
  config.rule_with_conditions_find do |rule,mail_header,conditions|
    conditions.all? do |condition|
      mail.respond_to?(mail_header) && mail.send(mail_header).to_s.match(/#{condition}/)
    end
  end
end

#pop(conn) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/popper/mail_account.rb', line 23

def pop(conn)
  done_uidls = []
  error_uidls = []

  Popper.log.info "start popper #{config.name}"

  process_uidl_list(conn).each do |m|
    begin
      mail = EncodeMail.new(m.mail)
      Popper.log.info "check mail:#{mail.date.to_s} #{mail.subject}"

      if rule = match_rule?(mail)
        Popper.log.info "do action:#{mail.subject}"
        Popper::Action::Git.run(config.action_by_rule(rule), mail) if config.action_by_rule(rule)
      end
      done_uidls << m.uidl

    rescue Net::POPError => e
      self.complete_list += done_uidls
      Popper.log.warn "pop err write uidl"
      return
    rescue => e
      error_uidls << m.uidl
      Popper.log.warn e
    end
  end

  self.complete_list = current_list - error_uidls
  Popper.log.info "success popper #{config.name}"
end

#process_uidl_list(conn) ⇒ Object



73
74
75
76
# File 'lib/popper/mail_account.rb', line 73

def process_uidl_list(conn)
  uidl_list = current_list - complete_list
  conn.mails.select {|_m|uidl_list.include?(_m.uidl)}
end

#runObject



13
14
15
16
17
18
19
20
21
# File 'lib/popper/mail_account.rb', line 13

def run
  session_start do |conn|
    self.current_list = conn.mails.map(&:uidl)
    self.complete_list = current_list unless complete_list
    pop(conn)
  end
  rescue => e
    Popper.log.warn e
end

#session_start(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/popper/mail_account.rb', line 54

def session_start(&block)
  pop = Net::POP3.new(config..server, config..port || 110)
  pop.enable_ssl if config..respond_to?(:ssl) && config..ssl

  %w(
    open_timeout
    read_timeout
  ).each {|m| pop.instance_variable_set("@#{m}", ENV['POP_TIMEOUT'] || 120) }

  pop.start(
    config..user,
    config..password
  ) do |pop|
    Popper.log.info "connect server #{config.name}"
    block.call(pop)
    Popper.log.info "disconnect server #{config.name}"
  end
end