#!/usr/bin/perl -w # # takes a .wav, creates a fake .qinfo file, and injects it into # the phonespool for processing by phonepostd. # # if you're debugging phonepostd, run it as root like so: # ~lj/other_sites/phonepost/phonepostd.pl --foreground use strict; use File::Copy; use Getopt::Long; sub usage { my $msg = $_[0] ? "\n$_[0]\n\n" : ""; die "${msg}usage: $0 [OPTS] [] OPTS: --implicit Act like user hung up during the call --security= One of 'public', 'private', 'friends' --runspool Run the spool processor once to inject into LJ --useragent= Optional user agent string to store in logprop " } my $opt_implicit = 0; my $opt_run = 0; my $opt_security = "public"; my $useragent = ""; usage() unless GetOptions( "implicit" => \$opt_implicit, "security=s" => \$opt_security, "runspool" => \$opt_run, "useragent=s" => \$useragent, ); $opt_implicit = 1 if $opt_implicit; usage("Invalid security value. Must be one of 'public', 'private', 'friends'") unless $opt_security =~ /^public|private|friends$/; my ($user, $file) = @ARGV; usage() unless scalar @ARGV == 1 || scalar @ARGV == 2; $file ||= "$ENV{LJHOME}/var/devdata/evillaugh.wav"; if ($file && ! -e $file) { die "File '$file' doesn't exist"; } use lib "$ENV{LJHOME}/cgi-bin"; require "ljlib.pl"; die "\$PHONESPOOL not defined or invalid" unless $LJ::PHONESPOOL && -d $LJ::PHONESPOOL; my $u = LJ::load_user($user) or die "No such user '$user'\n"; my $now = time; # copy wav first, so if phonepostd is running, we don't start processing my $wav = "$LJ::PHONESPOOL/$u->{userid}-$now.wav"; copy ($file, $wav) or die "Unable to copy wav into place: $!\n"; my $size = -s $wav; die "Copied wav '$wav' has no size?" unless $size > 0; # now insert into MogileFS and TheSchwartz so this job # can be picked up by LJ::Worker::PhonePostProcess my $mogc = LJ::mogclient() or die "No mogile client."; my $mogkey = "phonepostwav:$u->{userid}-$now"; my $mogfh = $mogc->new_file($mogkey, "", $size) or die "Failed to get new mogile filehandle"; my $data; { local $/ = undef; open my $fh, $wav or die "Unable to open '$wav': $!"; my $wav_data = <$fh>; print $mogfh $wav_data; close $fh; } unless (close($mogfh)) { die "Failed to write to mogile: " . $mogc->errcode . ": " . $mogc->errstr; } my $sclient = LJ::theschwartz() or die "No Schwartz client"; # make a fake $qinfo so we can send it as args to the Schwartz job my $qparams = { destid => 0, implicit_post => $opt_implicit, posttime => $now, security => $opt_security, userid => $u->id, wavfile => $wav, useragent => $useragent, }; my $job = TheSchwartz::Job->new_from_array("LJ::Worker::PhonePostProcess", { mogkey => $mogkey, qinfo => $qparams, }); my $h = $sclient->insert($job) or die "Failed to insert into theschwartz"; if ($opt_run){ system("$ENV{LJHOME}/bin/worker/phonepost-process", "-v"); } exit 0;