#!/usr/bin/perl
#
# This program is designed to implement twittering. Isn't
# that awesome? It's intended for monitoring purposes,
# and similar activities.
#
# This program doesn't require XML parsers cuz I am cheating
# and not using one. I know what to look for, so I'm
# simply parsing out the response.
#
# Written by Gabriel Cain, <gabriel@dreamingcrow.com>
# Licensed under the GNU GPL v2.
use strict;
use lib '/usr/lib/mon/alert.d';
use Data::Dumper;
use Getopt::Std;
use Twitter;
# optionally support curses
#eval { use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; };
#my $colorEnabled = ($@ ) ? 0 : 1;
#if( $colorEnabled ) {
# print BLUE, "\t\t\t *** ANSI COLOR ENABLED ***\n", RESET;
#}
# Set our options
# -f = alternate config file
# -d = Remove last twitter
# -D <name> = d <Name> message
# -F <name> = follow <Name>
# -r = get recent stuff
# -x = no color
# Process command-line args
my %o;
#getopts('xhrdD:F:f:', \%o);
getopts ("S:s:g:HurD:f:F:", \%o);
my $summary=<STDIN>;
$summary = $o{S
} if (defined $o{S
});
my $ALERT = $o{u} ? "UPALERT" : "ALERT";
my $message = "$ALERT $o{g}/$o{s}: $summary";
#my $DEBUG=1;
#print "DBG: $message\n" if ($DEBUG>0);
#exit;
#if( $o{x} ) {
# $colorEnabled = 0;
#}
# Read our configuration from $HOME/.twitterrc
# first line = username, 2nd line = password
my $tw_rc = $o{f} || "$ENV{HOME}/.twitterrc";
if( ! -f $tw_rc ) {
doCreateTwitterRC();
}
die("$tw_rc: $!") unless -f
$tw_rc;
open(T
,"<$tw_rc") or die("Failed to open $tw_rc: $!");
my $tw_user = <T>;
my $tw_pass = <T>;
# Eat useless newlines.
chomp( $tw_user, $tw_pass );
# Test for -d (delete twit)
if( $o{d}) {
print "Sorry, delete isn't implemented yet.\n";
}
# Direct & empty is contraindicated
print "Empty direct recipient not ok.\n";
}
# fail if we're not fetching & no message supplied
#if( ($#ARGV < 0 && ! $o{r}) || $o{h} ) {
if( $o{H} ) {
-=-=-=-=-=-=< Twitter Command Line Client >-=-=-=-=-=-=-
Written by Gabriel Cain, <gabriel\@dreamingcrow.com>
Hacked for MON alerts by Sotiris Tsimbonis, Apr 2009.
Usage:
twitter.pl <message>
twitter.pl -r
twitter.pl [options]
Options:
-f Read alternate config file,
default is \$HOME/.twitterrc
-D <name> Send a direct message to a user
-F <name> Follow a user
-r Get friends timeline (twenty most recent)
-S <summary> MON alert summary
-s <servicename> MON service
-g <groupname> MON group
-u MON Upalert
This program is licensed under the GNU GPL v2. Enjoy it.
END
}
# Build twitter message.
my $twit;
$twit = "d $o{D} " if $o{D}; # direct message if -D <name>
#$twit .= join ' ', @ARGV;
$twit .= $message;
# Log messages make the baby twitter cry
#warn("Warning: Message longer than 140 characters\n");
}
# Follow
$twit = "follow $o{F}"
}
my $res;
my $T = new Twitter( $tw_user, $tw_pass );
die("Object couldn't be made\n") unless $T;
if( $o{r}) {
$res = $T->whatsup();
}
else {
$res = $T->say( $twit );
}
# Check response, and if we failed, complain about it.
if( ! $res->is_success ) {
print "Request failed: ".$res->status_line."\n";
}
# If we're reading our "friends" page, then we need to spit
# back a list of what our friends say.
if( $o{r}) {
# Parse content for the items and dates.
my @items;
my @content = split /<item>/, $res->content;
foreach my $c (@content) {
# examine each <item> .. </item> block for what we
# need.
my $i = {};
$c =~ /<description>(.*)<\/description>/mi;
$i->{status} = $1;
$c =~ /<pubDate>(.*)<\/pubDate>/mi;
$i->{date} = $1;
next if $i->{date} =~ /Twitter updates from/;
}
# Spit them out in reverse order -- newest at bottom
my ($u,$s) = split /:/, $_->{status
}, 2;
#if( $colorEnabled ) {
# print GREEN, $a, RESET, ":", RED, $u,
# RESET, ":", CYAN, $s, "\n", RESET;
#}
#else {
print $a, ":", $u, ":", $s, "\n";
#}
}
}
sub doCreateTwitterRC {
# create $HOME/.twitterrc
my ($l,$p);
open( R
, ">$ENV{HOME}/.twitterrc") or die("Couldn't create .twitterrc.\n");
$| = 1;
print "What's your twitter login? ";
print "What's your twitter password? ";
# should have login/passwd
}