From 0c95e0543f8e0ffc7e7b3d33799b78e03c1049d2 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 1 May 2012 13:41:01 +0100 Subject: Updated sender to try and get things into UTF8 where poss. --- lib/mauve/sender.rb | 79 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 15 deletions(-) (limited to 'lib/mauve/sender.rb') 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}" -- cgit v1.2.1