Module: MMS2R
- Defined in:
- lib/mms2r.rb,
lib/mms2r/media.rb,
lib/mms2r/version.rb,
lib/mms2r/media/sprint.rb
Overview
Synopsis
MMS2R is a library to collect media files from MMS messages. MMS messages are multipart emails and mobile carriers often inject branding into these messages. MMS2R strips the advertising from an MMS leaving the actual user generated media.
The Tracker for MMS2R is located at rubyforge.org/tracker/?group_id=3065 Please submit bugs and feature requests using the Tracker.
If MMS from a carrier not known by MMS2R is encountered please submit a sample to the author for inclusion in this project.
Stand Alone Example
begin
require 'mms2r'
rescue LoadError
require 'rubygems'
require 'mms2r'
end
mail = Mail.read("sample-MMS.file")
mms = MMS2R::Media.new(mail)
subject = mms.subject
number = mms.number
file = mms.default_media
mms.purge
Rails ActionMailer#receive w/ AttachmentFu Example
def receive(mail)
mms = MMS2R::Media.new(mail)
picture = Picture.new # picture is an attachemnt_fu model
picture.title = mms.subject
picture.uploaded_data = mms.default_media
picture.save!
mms.purge
end
More Examples
See the README.rdoc file for more examples
Built In Configuration
A custom configuration can be created for processing the MMS from carriers that are not currently known by MMS2R. In the conf/ directory create a YAML file named by combining the domain name of the MMS sender plus a .yml extension. For instance the configuration of senders from AT&T’s cellular service with a Sender pattern of [email protected] have a configuration named conf/mms.att.net.yml
The YAML configuration contains a Hash with instructions for determining what is content generated by the user and what is content inserted by the carrier.
The root hash itself has two hashes under the keys ‘ignore’ and ‘transform’, and an array under the ‘number’ key. Each hash is itself keyed by mime-type. The value pointed to by the mime-type key is an array. The ignore arrays are first inspected as regular expressions else are used as a equality for a string filename. Ignores # work by filename for the multi-part of the MMS that is being inspected. The array pointed to by the ‘number’ key represents an alternate mail header where the sender’s number can be found with a regular expression and replacement value for a gsub.
The transform arrays are themselves an array of two element arrays. The elements are regexp parameters for gsub.
Ignore instructions are honored first then transform instructions. In the sample, masthead.jpg is ignored as a regular expression, and spacer.gif is ignored as a filename comparison. The transform has a match and a replacement, see the gsub documentation for more information about match and replace.
– ignore:
image/jpeg:
- /^masthead.jpg$/i
image/gif:
- spacer.gif
text/plain:
- /\AThis message was sent using PIX-FLIX Messaging service from .*/m
transform:
text/plain:
- - /\A(.+?)\s+This message was sent using PIX-FLIX Messaging .*/m
- "\1"
number:
- from
- /^([^\s]+)\s.*/
- "\1"
Carriers often provide their services under many different domain names. The conf/aliases.yml is a YAML file with a hash that maps alternative or legacy carrier names to the most common name of their service. For example in terms of MMS2R txt.att.net is an alias for mms.att.net. Therefore when an MMS with a Sender of txt.att.net is processed MMS2R will use the mms.att.net configuration to process the message.
Defined Under Namespace
Constant Summary collapse
- CARRIERS =
A hash of MMS2R processors keyed by MMS carrier domain.
{}
- EXT =
A hash of file extensions for common mime-types
{ 'text/plain' => 'text', 'text/plain' => 'txt', 'text/html' => 'html', 'image/png' => 'png', 'image/gif' => 'gif', 'image/jpeg' => 'jpeg', 'image/jpeg' => 'jpg', 'video/quicktime' => 'mov', 'video/3gpp2' => '3g2' }
Class Method Summary collapse
-
.debug(thing, options = {}) ⇒ Object
Compare original MMS2R results with original mail values and other metrics.
-
.parse(thing, options = {}) ⇒ Object
Simple convenience function to make it a one-liner: MMS2R.parse raw_mail or MMS2R.parse File.load(file) MMS2R.parse File.load(path_to_file) Combined w/ the method_missing delegation, this should behave as an enhanced Mail object, more or less.
-
.register(domain, processor_class) ⇒ Object
Registers a class as a processor for a MMS domain.
Class Method Details
.debug(thing, options = {}) ⇒ Object
Compare original MMS2R results with original mail values and other metrics.
Takes a file path, mms2r object, mail object, or mail text blob.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mms2r.rb', line 68 def self.debug(thing, = {}) mms = case thing when MMS2R::Media; thing when Mail::Message; MMS2R::Media.new(thing, ) else self.parse(thing, ) end <<OUT #{'-' * 80} original mail #{'from:'.ljust(15)} #{mms.mail.from} #{'to:'.ljust(15)} #{mms.mail.to} #{'subject:'.ljust(15)} #{mms.mail.subject} mms2r #{'from:'.ljust(15)} #{mms.from} #{'to:'.ljust(15)} #{mms.to} #{'subject:'.ljust(15)} #{mms.subject} #{'number:'.ljust(15)} #{mms.number} default media #{mms.default_media.inspect} default text #{mms.default_text.inspect} #{mms.default_text.read if mms.default_text} all plain text #{(mms.media['text/plain']||[]).each {|t| open(t).read}.join("\n\n")} media hash #{mms.media.inspect} OUT end |
.parse(thing, options = {}) ⇒ Object
Simple convenience function to make it a one-liner: MMS2R.parse raw_mail or MMS2R.parse File.load(file) MMS2R.parse File.load(path_to_file) Combined w/ the method_missing delegation, this should behave as an enhanced Mail object, more or less.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mms2r.rb', line 52 def self.parse thing, = {} mail = case when File.exist?(thing); Mail.new(open(thing).read) when thing.respond_to?(:read); Mail.new(thing.read) else Mail.new(thing) end MMS2R::Media.new(mail, ) end |
.register(domain, processor_class) ⇒ Object
Registers a class as a processor for a MMS domain. Should only be used in special cases such as MMS2R::Media::Sprint for ‘pm.sprint.com’
18 19 20 |
# File 'lib/mms2r.rb', line 18 def self.register(domain, processor_class) MMS2R::CARRIERS[domain] = processor_class end |