Don't like ads? PRO users don't see any ads ;-)
Guest

Sotiris Tsimbonis

By: a guest on Apr 27th, 2009  |  syntax: Perl  |  size: 5.26 KB  |  hits: 309  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2. #
  3. # This program is designed to implement twittering.  Isn't
  4. # that awesome?  It's intended for monitoring purposes,
  5. # and similar activities.
  6. #
  7. # This program doesn't require XML parsers cuz I am cheating
  8. # and not using one.  I know what to look for, so I'm
  9. # simply parsing out the response.
  10. #
  11. # Written by Gabriel Cain, <gabriel@dreamingcrow.com>
  12. # Licensed under the GNU GPL v2.
  13.  
  14. use strict;
  15. use lib '/usr/lib/mon/alert.d';
  16. use Data::Dumper;
  17. use Getopt::Std;
  18. use Twitter;
  19.  
  20. # optionally support curses
  21. #eval { use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; };
  22.  
  23. #my $colorEnabled = ($@ ) ? 0 : 1;
  24.  
  25. #if( $colorEnabled ) {
  26. #       print BLUE, "\t\t\t *** ANSI COLOR ENABLED ***\n", RESET;
  27. #}
  28.  
  29. # Set our options
  30. # -f = alternate config file
  31. # -d = Remove last twitter
  32. # -D <name> = d <Name> message
  33. # -F <name> = follow <Name>
  34. # -r = get recent stuff
  35. # -x = no color
  36.  
  37. # Process command-line args
  38. my %o;
  39. #getopts('xhrdD:F:f:', \%o);
  40. getopts ("S:s:g:HurD:f:F:", \%o);
  41.  
  42. my $summary=<STDIN>;
  43. chomp $summary;
  44. $summary = $o{S} if (defined $o{S});
  45. my $ALERT = $o{u} ? "UPALERT" : "ALERT";
  46.  
  47. my $message = "$ALERT $o{g}/$o{s}: $summary";
  48.  
  49. #my $DEBUG=1;
  50. #print "DBG: $message\n" if ($DEBUG>0);
  51. #exit;
  52.  
  53. #if( $o{x} ) {
  54. #       $colorEnabled = 0;
  55. #}
  56.  
  57.  
  58. # Read our configuration from $HOME/.twitterrc
  59. # first line = username, 2nd line = password
  60.  
  61. my $tw_rc = $o{f} || "$ENV{HOME}/.twitterrc";
  62.  
  63. if( ! -f $tw_rc ) {
  64.         doCreateTwitterRC();
  65. }
  66.  
  67. die("$tw_rc: $!") unless -f $tw_rc;
  68. open(T,"<$tw_rc") or die("Failed to open $tw_rc: $!");
  69.  
  70. my $tw_user = <T>;
  71. my $tw_pass = <T>;
  72. close(T);
  73.  
  74. # Eat useless newlines.
  75. chomp( $tw_user, $tw_pass );
  76.  
  77. # Test for -d (delete twit)
  78. if( $o{d}) {
  79.         print "Sorry, delete isn't implemented yet.\n";
  80.         exit 0;
  81. }
  82.  
  83. # Direct & empty is contraindicated
  84. if(  length $o{D} == 0 && defined $o{D} ) {
  85.         print "Empty direct recipient not ok.\n";
  86.         exit;
  87. }
  88.  
  89. # fail if we're not fetching & no message supplied
  90. #if( ($#ARGV < 0 && ! $o{r}) ||  $o{h} ) {
  91. if( $o{H} ) {
  92.         print << "END";
  93. -=-=-=-=-=-=< Twitter Command Line Client >-=-=-=-=-=-=-
  94. Written by Gabriel Cain, <gabriel\@dreamingcrow.com>
  95. Hacked for MON alerts by Sotiris Tsimbonis, Apr 2009.
  96.  
  97. Usage:
  98.         twitter.pl <message>
  99.         twitter.pl -r
  100.         twitter.pl [options]
  101.  
  102. Options:
  103.         -f                      Read alternate config file,
  104.                                 default is \$HOME/.twitterrc
  105.         -D <name>               Send a direct message to a user
  106.         -F <name>               Follow a user
  107.         -r                      Get friends timeline (twenty most recent)
  108.         -S <summary>            MON alert summary
  109.         -s <servicename>        MON service
  110.         -g <groupname>          MON group
  111.         -u                      MON Upalert
  112.  
  113. This program is licensed under the GNU GPL v2.  Enjoy it.
  114.  
  115. END
  116.         exit;
  117. }
  118.  
  119. # Build twitter message.
  120. my $twit;
  121. $twit = "d $o{D} " if $o{D}; # direct message if -D <name>
  122. #$twit .= join ' ', @ARGV;
  123. $twit .= $message;
  124.  
  125. # Log messages make the baby twitter cry
  126. if( length $twit > 140 ) {
  127.         #warn("Warning: Message longer than 140 characters\n");
  128. }
  129.  
  130. # Follow
  131. if( defined $o{F} && length $o{F} > 1 ) {
  132.         $twit = "follow $o{F}"
  133. }
  134.  
  135. my $res;
  136. my $T = new Twitter( $tw_user, $tw_pass );
  137. die("Object couldn't be made\n") unless $T;
  138.  
  139. if( $o{r}) {
  140.         $res = $T->whatsup();
  141. }
  142. else {
  143.         $res = $T->say( $twit );
  144. }
  145.  
  146. # Check response, and if we failed, complain about it.
  147. if( ! $res->is_success ) {
  148.         print "Request failed: ".$res->status_line."\n";
  149.         exit;
  150. }
  151.  
  152. # If we're reading our "friends" page, then we need to spit
  153. # back a list of what our friends say.
  154.  
  155. if( $o{r}) {
  156.         # Parse content for the items and dates.
  157.         my @items;
  158.  
  159.         my @content = split /<item>/, $res->content;
  160.         foreach my $c (@content) {
  161.                 # examine each <item> .. </item> block for what we
  162.                 # need.
  163.                 my $i = {};
  164.                 $c =~ /<description>(.*)<\/description>/mi;
  165.                 $i->{status} = $1;
  166.                 $c =~ /<pubDate>(.*)<\/pubDate>/mi;
  167.                 $i->{date} = $1;
  168.                 next if $i->{date} =~ /Twitter updates from/;
  169.                 push @items, $i;
  170.         }
  171.  
  172.         # Spit them out in reverse order -- newest at bottom
  173.         foreach ( reverse @items ) {
  174.                 my $a = sprintf "%s", $_->{date};
  175.                 my ($u,$s) = split /:/, $_->{status}, 2;
  176.  
  177.                 #if( $colorEnabled ) {
  178.                 #       print GREEN, $a, RESET, ":", RED, $u,
  179.                 #               RESET, ":", CYAN, $s, "\n", RESET;
  180.                 #}
  181.                 #else {
  182.                         print $a, ":", $u, ":", $s, "\n";
  183.                 #}
  184.         }
  185.  
  186. }
  187.  
  188.  
  189.  
  190. sub doCreateTwitterRC {
  191.         # create $HOME/.twitterrc
  192.         my ($l,$p);
  193.         open( R, ">$ENV{HOME}/.twitterrc") or die("Couldn't create .twitterrc.\n");
  194.         $| = 1;
  195.         print "What's your twitter login? ";
  196.  
  197.         chomp( $l = <> );
  198.         print "What's your twitter password? ";
  199.         chomp( $p = <> );
  200.  
  201.         # should have login/passwd
  202.         print R "$l\n$p\n";
  203.         close(R);
  204. }