Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save floehopper/919319 to your computer and use it in GitHub Desktop.
Save floehopper/919319 to your computer and use it in GitHub Desktop.
class Jobs::IncomingMessage
class << self
def encode(content)
content = content.gsub(/\215./) { |match| GSM_ESCAPED_CHARACTERS[match[1]] }
content = Iconv.conv("UTF-8", "HP-ROMAN8", content)
content.gsub!(EURO_TOKEN, "\342\202\254")
content
end
def decode(content)
content = content.gsub("\342\202\254", EURO_TOKEN)
content = Iconv.conv("HP-ROMAN8", "UTF-8", content)
GSM_ESCAPED_CHARACTERS.invert.each do |(character, replacement)|
content.gsub!(character, "\215#{replacement.chr}")
end
content
end
end
end
def convert_message_into_message_part(subscriber, message, message_id, part, total_parts, content)
message_part = MessagePart.new(
:subscriber_id => subscriber.id,
:message_id => message_id,
:part => part,
:total_parts => total_parts,
:content => content,
:contact_msisdn => message.contact_msisdn,
:timestamp => message.timestamp,
:sent => message.sent
)
puts "#{'%5d' % message_id} - saving part #{part} of #{total_parts}"
if message_part.save
message.destroy
else
puts "*** Error saving MessagePart: " + message_part.errors.full_messages.join("\n")
end
end
Subscriber.all.each do |subscriber|
messages_with_long_udh = subscriber.messages.where(:content => %r{^(\330\210\320|\330\210\321|\330\210\322|\330\210\323)})
messages_with_long_udh.each do |message|
decoded_content = Iconv.conv("UTF-16BE", "UTF-8", message.content)
udl, iei, iedl, message_id, total_parts, part = decoded_content.slice!(0..6).unpack("CCCSCC")
content = Iconv.conv("UTF-8", "UTF-16BE", decoded_content)
convert_message_into_message_part(subscriber, message, message_id, part, total_parts, content)
end
messages_with_long_udh = subscriber.messages.where(:content => %r{^\006\010\040})
messages_with_long_udh.each do |message|
decoded_content = Jobs::IncomingMessage.decode(message.content)
udl, iei, iedl, message_id, total_parts, part = decoded_content.slice!(0..6).unpack("CCCSCC")
content = Jobs::IncomingMessage.encode(decoded_content)
convert_message_into_message_part(subscriber, message, message_id, part, total_parts, content)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment