diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-01 14:03:52 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-01 14:03:52 +0100 |
commit | e55096b063720e0be4ca8d774095d2f6dfb7c086 (patch) | |
tree | 1b3b7c743bf1963bfe7cf11270ad719072a19200 /lib | |
parent | bb5ac2581ce9aa6a1a03c97986e91ad89f67fe3f (diff) | |
parent | fafac666b1d9f1b73fe4330af6b3c14d57ded2cb (diff) |
merge
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dm-sqlite-adapter-with-mutex.rb | 15 | ||||
-rw-r--r-- | lib/mauve/person.rb | 4 | ||||
-rw-r--r-- | lib/mauve/sender.rb | 79 |
3 files changed, 69 insertions, 29 deletions
diff --git a/lib/dm-sqlite-adapter-with-mutex.rb b/lib/dm-sqlite-adapter-with-mutex.rb index 2842c5e..6c5c022 100644 --- a/lib/dm-sqlite-adapter-with-mutex.rb +++ b/lib/dm-sqlite-adapter-with-mutex.rb @@ -5,20 +5,11 @@ require 'dm-sqlite-adapter' require 'monitor' -ADAPTER = DataMapper::Adapters::SqliteAdapter +class DataMapper::Adapters::SqliteAdapter -# better way to alias a private method? (other than "don't"? :) ) -ADAPTER.__send__(:alias_method, :initialize_old, :initialize) -ADAPTER.__send__(:undef_method, :initialize) -ADAPTER.__send__(:alias_method, :with_connection_old, :with_connection) -ADAPTER.__send__(:undef_method, :with_connection) + include MonitorMixin -class ADAPTER - - def initialize(*a) - extend(MonitorMixin) - initialize_old(*a) - end + alias_method :with_connection_old, :with_connection private diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index a4acaf0..c1c12c6 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -169,7 +169,7 @@ module Mauve # notification_method = Configuration.current.notification_methods[name.to_s] - @logger.warn "Notification method '#{name}' not defined (#{@person.username})" if notification_method.nil? + logger.warn "Notification method '#{name}' not defined (#{@person.username})" if notification_method.nil? # # Work out the destination @@ -182,7 +182,7 @@ module Mauve destination = nil end - @logger.warn "#{name} destination for #{@person.username} not set" if destination.nil? + logger.warn "#{name} destination for #{@person.username} not set" if destination.nil? if args.first.is_a?(Hash) conditions = @base_conditions.merge(args.pop) diff --git a/lib/mauve/sender.rb b/lib/mauve/sender.rb index c84dcb0..054313e 100644 --- a/lib/mauve/sender.rb +++ b/lib/mauve/sender.rb @@ -1,8 +1,21 @@ # encoding: UTF-8 require 'ipaddr' require 'socket' +begin + require 'locale' +rescue LoadError + # Do nothing -- these are bonus libraries :) +end + +begin + require 'iconv' +rescue LoadError + # Do nothing -- these are bonus libraries :) +end + require 'mauve/mauve_resolv' require 'mauve/mauve_time' +require 'mauve/proto' module Mauve # @@ -129,32 +142,68 @@ module Mauve end - # Send an update. - # - # @param [Mauve::Proto] update The update to send - # @param [Integer] vebose The verbosity -- higher is more. + # Sanitise all fields in an update, such that when we send, they are + # normal. + # # - # @return [Integer] the number of packets sent. - def send(update, verbose=0) - + def sanitize(update) # # Must have a source, so default to hostname if user doesn't care update.source ||= Socket.gethostname - + + # + # Check the locale charset. This is to maximise the amout of information + # mauve receives, rather than provide proper sanitised data for the server. + # + from_charset = (Locale.current.charset || Locale.charset) if defined?(Locale) + from_charset ||= "UTF-8" + # - # Make sure all alerts default to "-r now" + # # + update.each_field do |field, value| + # + # Make sure all string fields are UTF8 -- to ensure the maximal amount of information is sent. + # + update.__send__("#{field.name}=", Iconv.conv("UTF-8//IGNORE", from_charset, value)) if value.is_a?(String) and defined?(Iconv) + end + update.alert.each do |alert| - next if alert.raise_time || alert.clear_time - alert.raise_time = Time.now.to_i + # + # Make sure all alerts default to "-r now" + # + alert.raise_time = Time.now.to_i unless (alert.raise_time > 0 or alert.clear_time > 0) + + alert.each_field do |field, value| + # + # Make sure all string fields are UTF8 -- to ensure the maximal amount of information is sent. + # + alert.__send__("#{field.name}=", Iconv.conv("UTF-8//IGNORE", from_charset, value)) if value.is_a?(String) and defined?(Iconv) + end end - + + # + # Make sure we set the transmission time and ID. + # + update.transmission_time = Time.now.to_i if update.transmission_time.nil? or update.transmission_time == 0 + update.transmission_id = rand(2**63) if update.transmission_id.nil? or update.transmission_id == 0 + + update + end + + # Send an update. + # + # @param [Mauve::Proto] update The update to send + # @param [Integer] vebose The verbosity -- higher is more. + # + # @return [Integer] the number of packets sent. + def send(update, verbose=0) # - # Make sure we set the transmission time + # Clean up the update, and set any missing fields. # - update.transmission_time = Time.now.to_i + update = sanitise(update) - data = update.serialize_to_string + data = sanitize(update).serialize_to_string if verbose == 1 summary = "#{update.transmission_id} from #{update.source}" |