Module: MMS2R

Defined in:
lib/mms2r.rb,
lib/mms2r/media.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.txt 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 evaluated as a regular expressions and if the evaluation fails 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 eval.

The transform arrays are themselves an array of two element arrays. The elements are parameters for gsub and will be evaluated from within the ruby code.

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

Classes: Media

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

Class Method Details

.parse(raw_mail) ⇒ Object

Simple convenience function to make it a one-liner: MMS2R.parse raw_mail or MMS2R.parse File.load(raw_mail) Combined w/ the method_missing delegation, this should behave as an enhanced Mail object, more or less.



49
50
51
# File 'lib/mms2r.rb', line 49

def self.parse raw_mail
mail = Mail.new raw_mail
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