| Path: | lib/mms2r.rb |
| Last Update: | Sun Dec 20 13:05:02 -0800 2009 |
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.
require 'rubygems'
require 'mms2r'
mail = TMail::Mail.parse(IO.readlines("sample-MMS.file").join)
mms = MMS2R::Media.new(mail)
subject = mms.subject
number = mms.number
file = mms.default_media
mms.purge
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
See the README.txt file for more examples
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 2065551212@mms.att.net 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.