popper 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f134c1ecc38ef413d655eceaa8c8598388514446
4
- data.tar.gz: 4987a54a2cc1d8e8723305513e005a8acc5dc16b
3
+ metadata.gz: 3bf4501b5c03655837fa6f91f609315df3577a21
4
+ data.tar.gz: cc12464e2003de2852826154f3c978f692421e00
5
5
  SHA512:
6
- metadata.gz: 2ef7fa0af414c50b957133212e47e3bdd327764bb98d4fe4f32cef227de5cf93b0f3c0bda116df1904b77e00194b6bc2655d5fb5de6ef8da9330338b076df9f3
7
- data.tar.gz: ff7366f6119d72d7c04f6fa79f7da5d716bf83a0e14f7d374bcb045ebb3289cc3b72b5b086e8afa3b665c8114dfa8d4fd5f147eff3b1f325e3fe3410e53d2854
6
+ metadata.gz: eb915ab4ce61eb8bc11353c848639fa953d41ce85608830696505dee7ebced3602325ee88b3dc2ac5ba1d35e82562b70241582d3cbeb2e9261875a44aaa97e9d
7
+ data.tar.gz: 61a374239c807012c5ff25c137e7237d590cb47f7e63cb9e746af2a5d5f82ac5d56edb8f982f70def8135c9ea3599fdbad23704cd6028de0ff7d307c0e73fdc7
data/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  To post a variety of services by analyzing the email
8
8
  * slack notification
9
9
  * create issue to github.com or ghe
10
+ * exec arbitrary commands
10
11
 
11
12
  # install
12
13
  $ gem install popper
@@ -30,7 +31,6 @@ systmd service config: https://github.com/pyama86/popper/tree/master/init_script
30
31
  ## ~/popper/popper.conf
31
32
  ```
32
33
  include = ["/etc/popper/*.conf"]
33
- [global]
34
34
  interval = 60 # fetch interbal default:60
35
35
 
36
36
  [default.condition]
@@ -74,9 +74,11 @@ mentions = ["@user"]
74
74
  message = "webmailer error mail"
75
75
 
76
76
  [example.rules.normal_log.action.git]
77
-
78
77
  repo = "example/fuu"
79
78
 
79
+ [example.rules.normal_log.action.exec_cmd]
80
+ repo = "/path/to/other_command.rb"
81
+
80
82
  [example2.login]
81
83
  user = "example2@example.com"
82
84
  ...
data/lib/popper/action.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Popper::Action
2
- autoload :Base, "popper/action/base"
3
- autoload :Slack, "popper/action/slack"
4
- autoload :Ghe, "popper/action/ghe"
5
- autoload :Git, "popper/action/git"
2
+ autoload :Base, "popper/action/base"
3
+ autoload :Slack, "popper/action/slack"
4
+ autoload :Ghe, "popper/action/ghe"
5
+ autoload :Git, "popper/action/git"
6
+ autoload :ExecCmd, "popper/action/exec_cmd"
6
7
  end
@@ -1,19 +1,18 @@
1
1
  module Popper::Action
2
2
  class Base
3
3
  @next_action = nil
4
- @action = nil
5
4
  @action_config = nil
6
5
 
7
6
  def self.run(config, mail, params={})
8
- @action_config = config.send(self.action) if config.respond_to?(self.action)
7
+ @action_config = config.send(action_name) if config.respond_to?(action_name)
9
8
  begin
10
- Popper.log.info "run action #{self.action}"
9
+ Popper.log.info "run action #{action_name}"
11
10
  params = task(mail, params)
12
- Popper.log.info "exit action #{self.action}"
11
+ Popper.log.info "exit action #{action_name}"
13
12
  rescue => e
14
13
  Popper.log.warn e
15
14
  Popper.log.warn e.backtrace
16
- end if action?
15
+ end if do_action?
17
16
  next_run(config, mail, params)
18
17
  end
19
18
 
@@ -22,18 +21,20 @@ module Popper::Action
22
21
  @next_action
23
22
  end
24
23
 
25
- def self.action(action=nil)
26
- @action = action if action
27
- @action
28
- end
29
-
30
24
  def self.next_run(config, mail, params={})
31
25
  @next_action.run(config, mail, params) if @next_action
32
26
  end
33
27
 
34
- def self.action?
28
+ def self.do_action?
35
29
  @action_config && check_params
36
30
  end
31
+
32
+ def self.action_name
33
+ self.name.split('::').last.downcase.to_sym
34
+ end
35
+
36
+
37
+
37
38
  def self.check_params; end
38
39
  end
39
40
  end
@@ -0,0 +1,18 @@
1
+ module Popper::Action
2
+ class ExecCmd < Base
3
+ def self.task(mail, params={})
4
+ system(@action_config.cmd, mail.subject, mail.utf_body)
5
+ params
6
+ end
7
+
8
+ def self.check_params
9
+ @action_config.respond_to?(:cmd)
10
+ end
11
+
12
+ def self.action_name
13
+ :exec_cmd
14
+ end
15
+
16
+ next_action(Git)
17
+ end
18
+ end
@@ -16,6 +16,5 @@ module Popper::Action
16
16
  end
17
17
 
18
18
  next_action(Slack)
19
- action(:ghe)
20
19
  end
21
20
  end
@@ -8,7 +8,7 @@ module Popper::Action
8
8
  mail.subject,
9
9
  mail.utf_body
10
10
  )
11
- params["#{self.action}_url".to_sym] = url[:html_url] if url
11
+ params["#{action_name}_url".to_sym] = url[:html_url] if url
12
12
  params
13
13
  end
14
14
 
@@ -23,6 +23,5 @@ module Popper::Action
23
23
  end
24
24
 
25
25
  next_action(Ghe)
26
- action(:git)
27
26
  end
28
27
  end
@@ -32,6 +32,5 @@ module Popper::Action
32
32
  @action_config.respond_to?(:webhook_url)
33
33
  end
34
34
 
35
- action(:slack)
36
35
  end
37
36
  end
data/lib/popper/cli.rb CHANGED
@@ -20,16 +20,13 @@ module Popper
20
20
 
21
21
  Popper.load_config(options)
22
22
 
23
- accounts = Popper.configure.accounts.map {|account| MailAccount.new(account)}
24
- interval = case
25
- when Popper.configure.global.respond_to?(:interval)
26
- Popper.configure.global.interval
27
- else
28
- 60
29
- end
23
+ accounts = Popper.configure.accounts.map do |account|
24
+ MailAccount.new(account)
25
+ end.compact
26
+
30
27
  while true
31
28
  accounts.each(&:run)
32
- sleep(interval)
29
+ sleep(Popper.configure.interval)
33
30
  end
34
31
 
35
32
  rescue => e
data/lib/popper/config.rb CHANGED
@@ -3,45 +3,56 @@ require 'ostruct'
3
3
  require 'logger'
4
4
  module Popper
5
5
  class Config
6
- attr_reader :global, :default, :accounts
6
+ attr_reader :default, :accounts, :interval
7
7
  def initialize(config_path)
8
8
  raise "configure not fond #{config_path}" unless File.exist?(config_path)
9
- config = TOML.load_file(config_path)
10
- if config.key?("include")
11
- content = config["include"].map {|p| Dir.glob(p).map {|f|File.read(f)}}.join("\n")
12
- config.delete("include")
13
- config.deep_merge!(TOML::Parser.new(content).parsed)
14
- end
15
-
16
- @global = AccountAttributes.new(config["global"]) if config["global"]
17
- @default = AccountAttributes.new(config["default"]) if config["default"]
18
- @accounts = []
9
+ config = read_file(config_path)
19
10
 
20
- config.select {|k,v| !%w(default global).include?(k) }.each do |account|
11
+ @interval = config.key?("interval") ? config["interval"].to_i : 60
12
+ @default = config["default"] if config["default"]
13
+ @accounts = config.select {|k,v| v.is_a?(Hash) && v.key?("login") }.map do |account|
21
14
  _account = AccountAttributes.new(account[1])
22
15
  _account.name = account[0]
23
- self.accounts << _account
16
+ _account
24
17
  end
25
18
  end
26
19
 
20
+ def read_file(file)
21
+ config = TOML.load_file(file)
22
+ if config.key?("include")
23
+ content = config["include"].map {|p| Dir.glob(p).map {|f|File.read(f)}}.join("\n")
24
+ config.deep_merge!(TOML::Parser.new(content).parsed)
25
+ end
26
+ config
27
+ end
28
+
29
+ %w(
30
+ condition
31
+ action
32
+ ).each do |name|
33
+ define_method("default_#{name}") {
34
+ begin
35
+ default[name]
36
+ rescue
37
+ {}
38
+ end
39
+ }
40
+ end
27
41
  end
28
42
 
29
43
  class AccountAttributes < OpenStruct
30
44
  def initialize(hash=nil)
31
45
  @table = {}
32
- @hash_table = {}
46
+ @hash = hash
33
47
 
34
- if hash
35
- hash.each do |k,v|
36
- @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
37
- @hash_table[k.to_sym] = v
38
- new_ostruct_member(k)
39
- end
40
- end
48
+ hash.each do |k,v|
49
+ @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
50
+ new_ostruct_member(k)
51
+ end if hash
41
52
  end
42
53
 
43
54
  def to_h
44
- @hash_table
55
+ @hash
45
56
  end
46
57
 
47
58
  [
@@ -49,7 +60,7 @@ module Popper
49
60
  %w(each each),
50
61
  ].each do |arr|
51
62
  define_method("rule_with_conditions_#{arr[0]}") do |&blk|
52
- self.rules.to_h.keys.send(arr[0]) do |rule|
63
+ @hash["rules"].keys.send(arr[0]) do |rule|
53
64
  self.condition_by_rule(rule).to_h.send(arr[1]) do |mail_header,conditions|
54
65
  blk.call(rule, mail_header, conditions)
55
66
  end
@@ -61,32 +72,33 @@ module Popper
61
72
  condition
62
73
  action
63
74
  ).each do |name|
64
- define_method("global_default_#{name}") {
65
- begin
66
- Popper.configure.default.send(name).to_h
67
- rescue
68
- {}
69
- end
70
- }
71
-
72
75
  define_method("account_default_#{name}") {
73
76
  begin
74
- self.default.send(name).to_h
77
+ @hash["default"][name]
75
78
  rescue
76
79
  {}
77
80
  end
78
81
  }
79
82
 
80
- # merge global default and account default
83
+ # merge default and account default
81
84
  define_method("#{name}_by_rule") do |rule|
82
- hash = self.send("global_default_#{name}")
83
- hash = hash.deep_merge(self.send("account_default_#{name}").to_h) if self.send("account_default_#{name}")
84
- hash = hash.deep_merge(self.rules.send(rule).send(name).to_h) if rules.send(rule).respond_to?(name.to_sym)
85
+ hash = Popper.configure.send("default_#{name}")
86
+ hash = hash.deep_merge(self.send("account_default_#{name}")) if self.send("account_default_#{name}")
87
+ hash = hash.deep_merge(rule_by_name(rule)[name]) if rule_by_name(rule).key?(name)
85
88
 
86
89
  # replace body to utf_body
87
90
  AccountAttributes.new(Hash[hash.map {|k,v| [k.to_s.gsub(/^body$/, "utf_body").to_sym, v]}])
88
91
  end
89
92
  end
93
+
94
+ def rule_by_name(name)
95
+ begin
96
+ @hash["rules"][name]
97
+ rescue
98
+ {}
99
+ end
100
+ end
101
+
90
102
  end
91
103
 
92
104
  def self.load_config(options)
data/lib/popper/init.rb CHANGED
@@ -12,7 +12,6 @@ module Popper
12
12
 
13
13
  def self.sample_config
14
14
  <<-EOS
15
- [global]
16
15
  interval = 60 # fetch interbal default:60
17
16
 
18
17
  [default.condition]
@@ -28,15 +28,7 @@ module Popper
28
28
 
29
29
  process_uidl_list(conn).each do |m|
30
30
  begin
31
- mail = EncodeMail.new(m.mail)
32
- Popper.log.info "check mail:#{mail.date.to_s} #{mail.subject}"
33
-
34
- if rule = match_rule?(mail)
35
- Popper.log.info "do action:#{mail.subject}"
36
- Popper::Action::Git.run(config.action_by_rule(rule), mail) if config.action_by_rule(rule)
37
- end
38
- done_uidls << m.uidl
39
-
31
+ done_uidls << check_and_action(m)
40
32
  rescue Net::POPError => e
41
33
  self.complete_list += done_uidls
42
34
  Popper.log.warn "pop err write uidl"
@@ -51,25 +43,42 @@ module Popper
51
43
  Popper.log.info "success popper #{config.name}"
52
44
  end
53
45
 
46
+ def check_and_action(m)
47
+ mail = EncodeMail.new(m.mail)
48
+ Popper.log.info "check mail:#{mail.date.to_s} #{mail.subject}"
49
+ if rule = match_rule?(mail)
50
+ Popper.log.info "do action:#{mail.subject}"
51
+ Popper::Action::ExecCmd.run(config.action_by_rule(rule), mail) if config.action_by_rule(rule)
52
+ end
53
+
54
+ m.uidl
55
+ end
56
+
54
57
  def session_start(&block)
55
58
  pop = Net::POP3.new(config.login.server, config.login.port || 110)
56
- pop.enable_ssl if config.login.respond_to?(:ssl) && config.login.ssl
57
-
58
- %w(
59
- open_timeout
60
- read_timeout
61
- ).each {|m| pop.instance_variable_set("@#{m}", ENV['POP_TIMEOUT'] || 120) }
59
+ pop = set_pop_option(pop)
62
60
 
63
61
  pop.start(
64
62
  config.login.user,
65
63
  config.login.password
66
- ) do |pop|
64
+ ) do |conn|
67
65
  Popper.log.info "connect server #{config.name}"
68
- block.call(pop)
66
+ block.call(conn)
69
67
  Popper.log.info "disconnect server #{config.name}"
70
68
  end
71
69
  end
72
70
 
71
+ def set_pop_option(pop)
72
+ pop.enable_ssl if config.login.respond_to?(:ssl) && config.login.ssl
73
+ %w(
74
+ open_timeout
75
+ read_timeout
76
+ ).each do |m|
77
+ pop.instance_variable_set("@#{m}", ENV['POP_TIMEOUT'] || 120)
78
+ end
79
+ pop
80
+ end
81
+
73
82
  def process_uidl_list(conn)
74
83
  uidl_list = current_list - complete_list
75
84
  conn.mails.select {|_m|uidl_list.include?(_m.uidl)}
@@ -1,3 +1,3 @@
1
1
  module Popper
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: popper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - pyama86
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-01 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -143,6 +143,7 @@ files:
143
143
  - lib/popper.rb
144
144
  - lib/popper/action.rb
145
145
  - lib/popper/action/base.rb
146
+ - lib/popper/action/exec_cmd.rb
146
147
  - lib/popper/action/ghe.rb
147
148
  - lib/popper/action/git.rb
148
149
  - lib/popper/action/slack.rb
@@ -172,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
173
  version: '0'
173
174
  requirements: []
174
175
  rubyforge_project:
175
- rubygems_version: 2.2.2
176
+ rubygems_version: 2.4.5.1
176
177
  signing_key:
177
178
  specification_version: 4
178
179
  summary: email notification tool