267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/wcc.rb', line 267
def self.checkForUpdate(site)
WCC.logger.info "Requesting '#{site.uri.to_s}'"
begin
res = site.fetch
rescue Timeout::Error => ex
return false
rescue => ex
WCC.logger.error "Cannot connect to #{site.uri.to_s} : #{ex.to_s}"
return false
end
if not res.kind_of?(Net::HTTPOK)
WCC.logger.error "Site #{site.uri.to_s} returned #{res.code} code, skipping it."
return false
end
new_content = res.body
encoding = (res['content-type'].to_s.match(/;\s*charset=([A-Za-z0-9-]*)/i).to_a[1] ||
new_content.match(/<meta.*charset=([a-zA-Z0-9-]*).*/i).to_a[1]).to_s.downcase || 'utf-8'
WCC.logger.info "Encoding is '#{encoding}'"
begin
new_content = Iconv.conv('utf-8', encoding, new_content)
rescue => ex
WCC.logger.error "Cannot convert site from '#{encoding}': #{ex.to_s}"
return false
end
new_content = new_content.strip_html if site.strip_html?
new_hash = Digest::MD5.hexdigest(new_content)
WCC.logger.debug "Compare hashes\n old: #{site.hash.to_s}\n new: #{new_hash.to_s}"
return false if new_hash == site.hash
if site.new?
site.hash, site.content = new_hash, new_content
diff = "Site was first checked so no diff was possible."
else
old_site_file = Tempfile.new("wcc-#{site.id}-")
old_site_file.write(site.content)
old_site_file.close
old_label = "OLD (%s)" % File.mtime(Conf.file(site.id + ".md5")).strftime(DIFF_TIME_FMT)
new_label = "NEW (%s)" % Time.now.strftime(DIFF_TIME_FMT)
site.hash, site.content = new_hash, new_content
diff = %x[diff -U 1 --label "#{old_label}" --label "#{new_label}" #{old_site_file.path} #{Conf.file(site.id + '.site')}]
end
return false if not Filter.accept(diff, site.filters)
data = OpenStruct.new
data.title = "[#{Conf[:tag]}] #{site.uri.host} changed"
data.message = "Change at #{site.uri.to_s} - diff follows:\n\n#{diff}"
Conf.mailer.send(data, @@mail_plain, MailAddress.new(Conf[:from_mail]), site.emails)
system("logger -t '#{Conf[:tag]}' 'Change at #{site.uri.to_s} (tag #{site.id}) detected'") if Conf[:syslog]
true
end
|