#!/usr/bin/perl -w

# sms2irc.pl v0.1
# by Philip Shuman
# philip at shuman dot org
# 
# Usage:
# Add the following to your /etc/aliases or equivilent
#   test:  "| /home/pshuman/tmp/sms2irc.pl example.com 6667 \\#test"
# or
#   test:  "| /home/pshuman/tmp/sms2irc.pl"
# if you hard code the server, port, and channel into the script.

use strict;
use warnings;
use Net::IRC;
use Mail::Internet;

$main::message = new Mail::Internet \*STDIN;
@main::From_lines = $main::message->head->get("From");
$main::from = $main::From_lines[0];
chomp $main::from;
$main::from =~ s/@/ at /g;

$main::mBody = $main::message->body;
$main::line = @$main::mBody[0];
chomp $main::line;

my $irc = new Net::IRC;

my $conn = $irc->newconn(
   Server   => shift || 'example.com',
   Port     => shift || '6667', 
   Nick     => 'sms2irc',
   Ircname  => 'sms2irc V0.1',
   Username => 'sms2irc'
);

$conn->{channel} = shift || '#test2';

sub on_connect {

   # shift in our connection object that is passed automatically
   my $conn = shift;
  
   $conn->join($conn->{channel});
   $conn->privmsg($conn->{channel}, "($main::from) says: $main::line");
   $conn->{connected} = 1;
   sleep 2;
   exit;

}

# The end of MOTD (message of the day), numbered 376 signifies we've connect
# 422 means there was no MOTD file
$conn->add_handler('376', \&on_connect);
$conn->add_handler('422', \&on_connect);

# start IRC
$irc->start();
