Class: Popper::MailAccount

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MailAccount

Returns a new instance of MailAccount.



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

def initialize(config)
  @config = config
end

Instance Method Details

#match_rule?(mail) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'lib/popper/mail_account.rb', line 74

def match_rule?(mail)
  @config.rules.to_h.keys.find do |rule|
    @config.condition_by_rule(rule).to_h.all? do |mail_header,conditions|
      conditions.all? do |condition|
        mail.respond_to?(mail_header) && mail.send(mail_header).to_s.match(/#{condition}/)
      end
    end
  end
end

#pop(conn) ⇒ Object



21
22
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
# File 'lib/popper/mail_account.rb', line 21

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
      @complete_uidl_list += done_uidls
      Popper.log.warn "pop err write uidl"
      return
    rescue => e
      error_uidls << m.uidl
      Popper.log.warn e
    end
  end

  @complete_uidl_list = @current_uidl_list - error_uidls
  Popper.log.info "success popper #{@config.name}"
end

#process_uidl_list(conn) ⇒ Object



69
70
71
72
# File 'lib/popper/mail_account.rb', line 69

def process_uidl_list(conn)
  uidl_list = @current_uidl_list - @complete_uidl_list
  conn.mails.select {|_m|uidl_list.include?(_m.uidl)}
end

#runObject



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

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

#session_start(&block) ⇒ Object



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

def session_start(&block)
  pop = Net::POP3.new(@config..server, @config..port || 110)
  %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