#!/usr/bin/perl

use strict q(vars);
use warnings;

use Net::Twitter;

our $consumer_key          = q(stuff);
our $consumer_secret       = q(andThings);
our $token;
our $token_secret;

sub get_quote {
  my (@quotes) = grep { length() < 141 } map { chomp; $_ } <DATA>;
  return $quotes[rand @quotes];
}

sub restore_tokens {
  open my $fh, '<', ".oauth" or die qq(failed to create file; $!);
  my($access_token, $access_token_secret) = map
  {
    chomp;
    s/^[^:]+:// if defined and length;
    $_
  } <$fh>;
  close $fh;
  return ($access_token, $access_token_secret);
}

# just show a quote and be done if we're called from a browser
if (exists $ENV{QUERY_STRING}) {
  eval "use CGI";
  my $q = new CGI;
  print $q->header;
  print $q->h1( "Random Steven Brust Quote" );
  print $q->h2( get_quote);
  exit( 0 );
}

($token,$token_secret) = (restore_tokens);
die "Not Authorized" unless $token && $token_secret;

my $nt = Net::Twitter->new
(
 traits   => [qw/OAuth API::REST/],
 consumer_key        => $consumer_key,
 consumer_secret     => $consumer_secret,
 access_token        => $token,
 access_token_secret => $token_secret,
);

my $result = $nt->update( get_quote );
#use Data::Dumper; print Dumper( $result );
exit( 0 );

__DATA__
Maybe it's just me, but... there's nothing like having someone try to kill you to take your mind off your problems.
No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style.
Everybody generalizes from one example. At least, I do.
You connect yourself to the viewer by sharing something that is inside of you that connects with something inside of him.
Staring into the dragon's maw, one quickly learns wisdom.
Plan. Yes. Good idea. I should come up with a plan.
Most people seem to take pleasure in feeling superior to someone. I'm not like that, which pleases me because it makes me feel superior.
The struggle is always worthwhile, if the end be worthwhile and the means honorable...
You can't put something together again unless you've torn it apart first.
True heroics must be carefully planned - and strenuously avoided.
It is said: 'By his home u shall know him'; & we all know that we must pay attention 2 anyone who reverses his subject & auxiliary verb
