wcc 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/assets/conf.yml CHANGED
@@ -6,6 +6,7 @@ conf:
6
6
  # tag: wcc
7
7
  # filterd: ./filter.d
8
8
  # templated: ./template.d
9
+ # # default config assuming local mail server
9
10
  # email:
10
11
  # smtp:
11
12
  # from: root@localhost
@@ -47,8 +48,6 @@ sites:
47
48
  # These filters will be executed and every single one has to
48
49
  # return 'true' for the user to be notified
49
50
  filters:
50
- - test
51
- - arg-test: {number: 5, hello: world}
52
51
  - only_changes_of: {at_least: 4, t: lines}
53
52
  # Regex filter that performs matching against <scope> (one of full or diff)
54
53
  - matches: {regex: '(normal|regex)[!]+', flags: i, scope: diff}
@@ -1,5 +1,6 @@
1
1
 
2
2
  WCC::Filters.add 'changes_of' do |data,args|
3
+ next true if data.diff.nil?
3
4
  case args['t'] || args['type']
4
5
  when 'line','lines',nil
5
6
  cmp_val = data.diff.nlinesc
@@ -18,7 +18,7 @@ WCC::Filters.add 'matches' do |data,args|
18
18
  end
19
19
  case args['scope']
20
20
  when 'diff','change',nil
21
- md = r.match(data.diff.to_s)
21
+ md = data.diff.nil? ? nil : r.match(data.diff.to_s)
22
22
  when 'site','full'
23
23
  md = r.match(data.site.content)
24
24
  end
@@ -1,8 +1,9 @@
1
1
 
2
2
  # NOTE: the percentage may easily go above 100% when there are more
3
- # changes than the whole site has lines.
3
+ # changes than the whole site had lines before.
4
4
 
5
5
  WCC::Filters.add 'rel_changes_of' do |data,args|
6
+ next true if data.diffnil?
6
7
  case args['percent_of']
7
8
  when 'all_lines',nil
8
9
  percent = data.diff.nlinesc.to_f / data.site.content.count("\n").+(1).to_f * 100
data/lib/wcc.rb CHANGED
@@ -129,29 +129,57 @@ module WCC
129
129
 
130
130
  if !File.exists?(self[:conf])
131
131
  WCC.logger.fatal "Config file '#{self[:conf]}' does not exist!"
132
- exit 1
132
+ Prog.exit 1
133
133
  end
134
134
 
135
+ # register standard notificators - these are already loaded
136
+ Notificators.map 'email', MailNotificator
137
+ Notificators.map 'jabber', XMPPNotificator
138
+ Notificators.map 'syslog', SyslogNotificator
139
+
135
140
  WCC.logger.debug "Load config from '#{self[:conf]}'"
136
141
 
137
142
  # may be false if file is empty
138
143
  yaml = YAML.load_file(self[:conf])
139
- if yaml.is_a?(Hash) and (yaml = yaml['conf']).is_a?(Hash)
140
- @options[:cache_dir] ||= yaml['cache_dir']
141
- @options[:tag] ||= yaml['tag']
142
- @options[:filter_dir] ||= yaml['filterd']
143
- @options[:template_dir] ||= yaml['templated']
144
+ if yaml.is_a?(Hash) and yaml['conf'].is_a?(Hash)
145
+
146
+ # load MailNotificator and it's defaults even if the key is missing
147
+ # since email has always been the backbone of wcc
148
+ if not yaml['conf'].key?('email')
149
+ yaml['conf']['email'] = nil
150
+ end
144
151
 
145
- MailNotificator.parse_conf(yaml['email']).each { |k,v| @options[k] ||= v }
146
- XMPPNotificator.parse_conf(yaml['jabber']).each { |k,v| @options[k] ||= v }
147
- SyslogNotificator.parse_conf(yaml['syslog']).each { |k,v| @options[k] ||= v }
152
+ yaml['conf'].each do |key,val|
153
+ case key
154
+ when 'cache_dir'
155
+ @options[:cache_dir] ||= val
156
+ when 'tag'
157
+ @options[:tag] ||= val
158
+ when 'filterd'
159
+ @options[:filter_dir] ||= val
160
+ when 'templated'
161
+ @options[:template_dir] ||= val
162
+ else
163
+ if not Notificators.mappings.include?(key)
164
+ plugin_name = "wcc-#{key}-notificator"
165
+ WCC.logger.info "Trying to load plugin #{plugin_name}..."
166
+ begin
167
+ require plugin_name
168
+ rescue LoadError
169
+ WCC.logger.error "Plugin #{plugin_name} not found - maybe try `gem install #{plugin_name}`"
170
+ next
171
+ end
172
+ end
173
+ Notificators.mappings[key].parse_conf(val).each { |k,v| @options[k] ||= v }
174
+ end
175
+ end
148
176
  end
149
177
 
150
178
  if self[:show_config]
151
179
  Conf.default.merge(@options).each do |k,v|
152
180
  puts " #{k.to_s} => #{self[k]}"
153
181
  end
154
- exit 0
182
+ Prog.exit 0
155
183
  end
156
184
 
157
185
  @recipients = {}
@@ -167,10 +195,20 @@ module WCC
167
195
  yaml_rec[name].to_a.each do |yaml_way|
168
196
  # TODO: find options and pass them to every notificator
169
197
  if yaml_way.is_a?(Hash)
170
- rec << XMPPNotificator.new(yaml_way['jabber']) if yaml_way.key?('jabber')
171
- rec << MailNotificator.new(yaml_way['email']) if yaml_way.key?('email')
172
- elsif yaml_way == 'syslog'
173
- rec << SyslogNotificator.new
198
+ prim_key = yaml_way.keys.first # and only!
199
+ klass = Notificators.mappings[prim_key]
200
+ if klass.nil?
201
+ WCC.logger.error "Referenced notificator '#{prim_key}' not found!"
202
+ else
203
+ rec << klass.new(yaml_way[prim_key])
204
+ end
205
+ else
206
+ klass = Notificators.mappings[yaml_way]
207
+ if klass.nil?
208
+ WCC.logger.error "Referenced notificator '#{yaml_way}' not found!"
209
+ else
210
+ rec << klass.new
211
+ end
174
212
  end
175
213
  end
176
214
  @recipients[name] = rec
@@ -214,7 +252,6 @@ module WCC
214
252
  cookie = File.open(yaml_site['cookie'], 'r') { |f| f.read }
215
253
  end
216
254
 
217
-
218
255
  @sites << Site.new(
219
256
  yaml_site['url'],
220
257
  yaml_site['strip_html'] || true,
@@ -222,7 +259,8 @@ module WCC
222
259
  frefs,
223
260
  yaml_site['auth'] || {},
224
261
  cookie,
225
- yaml_site['check_interval'] || 5)
262
+ yaml_site['check_interval'] || 5
263
+ )
226
264
  end
227
265
 
228
266
  WCC.logger.debug @sites.length.to_s + (@sites.length == 1 ? ' site' : ' sites') + " loaded\n" +
@@ -239,6 +277,20 @@ module WCC
239
277
  def self.simulate?; self[:simulate] end
240
278
  def self.[](key); Conf.instance[key] end
241
279
  end
280
+
281
+ class Notificators
282
+ @@mappings = {}
283
+
284
+ # API method - add a mapping from conf string to class object
285
+ # @param [String] name the string to be used in conf.yml's conf entry
286
+ # @param [Class] klass the associated notifier class
287
+ def self.map(name, klass)
288
+ WCC.logger.debug "Register notificator #{klass.inspect} for #{name}"
289
+ @@mappings[name] = klass
290
+ end
291
+
292
+ def self.mappings; @@mappings end
293
+ end
242
294
 
243
295
  class LogFormatter
244
296
  def initialize(use_color = true)
@@ -362,11 +414,14 @@ module WCC
362
414
  Dir.mkdir(Conf[:cache_dir]) unless File.directory?(Conf[:cache_dir])
363
415
 
364
416
  if(Conf[:clean])
365
- WCC.logger.warn "Cleanup hash and diff files"
417
+ WCC.logger.warn "Removing hash and diff files..."
366
418
  Dir.foreach(Conf[:cache_dir]) do |f|
367
419
  File.delete(Conf.file(f)) if f =~ /^.*\.(md5|site)$/
368
420
  end
369
- # TODO: delete timestamps on clean?
421
+ cache_file = Conf.file('cache.yml')
422
+ WCC.logger.warn "Removing timestamp cache..."
423
+ File.delete(cache_file) if File.exists?(cache_file)
424
+ Prog.exit 1
370
425
  end
371
426
 
372
427
  # read filter.d
@@ -409,16 +464,24 @@ module WCC
409
464
  File.open(cache_file, 'w+') do |f| YAML.dump({"timestamps" => @@timestamps}, f) end
410
465
 
411
466
  # shut down notificators
412
- MailNotificator.shut_down
413
- XMPPNotificator.shut_down
414
- SyslogNotificator.shut_down
467
+ Notificators.mappings.each do |name,klass|
468
+ WCC.logger.debug "Shut down #{klass}"
469
+ klass.shut_down
470
+ end
415
471
  end
416
472
 
417
473
  def self.load_template(name)
418
474
  t_path = File.join(Conf[:template_dir], name)
419
- t = File.open(t_path, 'r') { |f| f.read }
420
- # <> omit newline for lines starting with <% and ending in %>
421
- ERB.new(t, 0, "<>")
475
+ if File.exists?(t_path)
476
+ t = File.open(t_path, 'r') { |f| f.read }
477
+ # <> omit newline for lines starting with <% and ending in %>
478
+ return ERB.new(t, 0, "<>")
479
+ end
480
+ nil
481
+ end
482
+
483
+ def self.exit(errno)
484
+ Kernel::exit errno
422
485
  end
423
486
 
424
487
  private
data/lib/wcc/diff.rb CHANGED
@@ -112,7 +112,7 @@ module WCC
112
112
  md = s.match(/(@|_)di(@|_)/)
113
113
  while not md.nil?
114
114
  mds << md
115
- s = s.substring(md.begin(2)+1)
115
+ s = s.substring(md.begin(2))
116
116
  md = s.match(/(@|_)di(@|_)/)
117
117
  end
118
118
 
data/lib/wcc/mail.rb CHANGED
@@ -91,6 +91,7 @@ module WCC
91
91
  end
92
92
  end
93
93
  # default is smtp
94
+ WCC.logger.debug "Using default SMTP configuration for MailNotificator"
94
95
  return {
95
96
  :mailer => 'smtp',
96
97
  :from_mail => MailAddress.new("#{Etc.getlogin}@localhost"),
data/lib/wcc/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module WCC
3
- VERSION = "2.0.2"
3
+ VERSION = "2.0.3"
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 2
10
- version: 2.0.2
9
+ - 3
10
+ version: 2.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christian Nicolai
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-18 00:00:00 Z
18
+ date: 2011-11-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: htmlentities
@@ -75,13 +75,11 @@ extra_rdoc_files:
75
75
  files:
76
76
  - assets/conf.yml
77
77
  - assets/filter.d/and.rb
78
- - assets/filter.d/arg-test.rb
79
78
  - assets/filter.d/changes_of.rb
80
79
  - assets/filter.d/matches.rb
81
80
  - assets/filter.d/not.rb
82
81
  - assets/filter.d/or.rb
83
82
  - assets/filter.d/rel_changes_of.rb
84
- - assets/filter.d/test.rb
85
83
  - assets/template.d/mail.alt.erb
86
84
  - assets/template.d/mail-body.html.erb
87
85
  - assets/template.d/mail-body.plain.erb
@@ -104,7 +102,7 @@ files:
104
102
  homepage: https://github.com/cmur2/wcc
105
103
  licenses:
106
104
  - Apache License Version 2.0
107
- post_install_message: "NOTE: Remember to \xC2\xB4wcc-upgrade\xC2\xB4 your conf.yml directory!"
105
+ post_install_message: "NOTE: Remember to `wcc-upgrade` your conf.yml directory!"
108
106
  rdoc_options: []
109
107
 
110
108
  require_paths:
@@ -1,9 +0,0 @@
1
-
2
- WCC::Filters.add 'arg-test' do |data,args|
3
- puts "arg-test:"
4
- puts " #arguments = #{args.size}"
5
- args.each do |arg|
6
- puts " - #{arg.inspect}"
7
- end
8
- true
9
- end
@@ -1,4 +0,0 @@
1
-
2
- WCC::Filters.add 'test' do |data|
3
- true
4
- end