#!/usr/bin/perl -wT # # Copyright 2002 Joe Edmonds # # $Id: ceiva,v 1.10 2005/01/09 10:26:24 joe Exp $ use strict; use LWP::UserAgent; use HTTP::Request::Common qw(POST); use HTTP::Cookies; use Getopt::Long; use MIME::Parser; # TBD: Might be nice to "require" this only if needed. use Config::Simple; # # Parse configuration file and parameters # my %config = (); Config::Simple->import_from("$ENV{HOME}/.ceiva",\%config); Getopt::Long::config('no_ignore_case'); GetOptions(\%config, 'username=s', # Ceiva account username 'password=s', # Ceiva account password 'frame-id=i', # "ceiva --help" shows how to get this. ) or die_usage(); my $username = $config{'username'}; my $password = $config{'password'}; my $frame_id = $config{'frame-id'}; die_usage() unless $username and $password and $frame_id; # # Get files # my @files = (); my $parser = undef; my $mime_directory = "/tmp/ceiva.$$"; # from command line if specified there: if (scalar @ARGV) { @files = @ARGV; } # or try to parse them from stdin: else { $parser = new MIME::Parser; mkdir $mime_directory; # Fix treo mail format so MIME::Parser can read it. Feh: my $mime_string = undef; { local undef $/; $mime_string = <>; } $mime_string =~ s/(This is a multi-part message in MIME format.)/$1\n/; my @parts = (); eval { $parser->output_dir($mime_directory); my $entity = $parser->parse_data($mime_string) or die "parse failed\n"; @parts = $entity->parts(); }; die "parsing failed: $@" if ($@); die "parsing failed: no parts" if not @parts; @files = ( $parts[0]->bodyhandle->path ); } my $nfiles = scalar @files; die_usage() if $nfiles < 1 or $nfiles > 10; # # Set up UserAgent # my $ua = new LWP::UserAgent; my $cookie_jar = HTTP::Cookies->new; $ua->cookie_jar($cookie_jar); # # Post login form to get session cookie(s) # my %login_args = ( j_username => $username, j_password => $password, # j_use_cookie_auth=> 'no', _ceiva_form_name => '', _ceiva_page_name => '/public/ceiva_signin.jsp', target_realm => 'member', init_auth => 'true', j_uri => '/member/my_home.jsp', ); my $login_url = 'http://www.ceiva.com/public/j_security_check'; my $req = POST $login_url, \%login_args; my $res = $ua->request($req); die "Login POST to '$login_url' failed (".$res->message.")" if (not $res->is_success and $res->message ne 'Found' and $res->message ne 'Moved Temporarily'); # # Post request to upload the images # my $url = 'http://www.ceiva.com/member-servlet/com.ceiva.servlets.UploadAndSendMultipleImages'; my %args = ( _ceiva_form_name => 'formUploadImages', _ceiva_page_name => '/member/upload_multiple_images.jsp', showthumbs => 'yes', frame_id => $frame_id, date_send => '', date_month => '08', date_day => '04', date_year => '2002', action => '', map { "file".($_+1) => [ $files[$_], undef ] } (0..$#files), ); $req = POST $url, Content_Type => 'form-data', Content => \%args; # ceiva can't handle content-length delete $req->{_headers}->{'content-length'}; # TBD: Is there a better way to remove these? $req->{'_content'} =~ s/Content-Length: *\d*\s*//g; $res = $ua->request($req); die "POST to '$url' failed (".$res->message.")" if not $res->is_success; # Clean up mime directory: if ($parser) { $parser->filer->purge; rmdir $mime_directory; } # # Print a helpful message on stderr # sub die_usage { die "Usage: ceiva --username --password

--frame file1 [file2..file10] You can get your frame_id by logging in and viewing the html on this page: http://www.ceiva.com/member/select_destinations_multiple.jsp "; }