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

#check_and_action(m) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/popper/mail_account.rb', line 47

def check_and_action(m)
  mail = EncodeMail.new(m.mail)
  Popper.log.info "check mail:#{mail.date.to_s} #{mail.subject}"
  match_rules?(mail).each do |rule|
    Popper.log.info "do action:#{mail.subject}"
    Popper::Action::Webhook.run(config.action_by_rule(rule), mail) if config.action_by_rule(rule)
  end

  m.uidl
end

#match_rules?(mail) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/popper/mail_account.rb', line 88

def match_rules?(mail)
  config.rule_with_conditions_select 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
# 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
      done_uidls << check_and_action(m)
      m.delete if config..respond_to?(:delete_after) && config..delete_after
    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



83
84
85
86
# File 'lib/popper/mail_account.rb', line 83

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



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

def session_start(&block)
  pop = Net::POP3.new(config..server, config..port || 110)
  pop = set_pop_option(pop)

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

#set_pop_option(pop) ⇒ Object



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

def set_pop_option(pop)
  pop.enable_ssl if config..respond_to?(:ssl) && config..ssl
  %w(
    open_timeout
    read_timeout
  ).each do |m|
    pop.instance_variable_set("@#{m}", ENV['POP_TIMEOUT'] || 120)
  end
  pop
end