Tuesday, June 16, 2009

How i wasted a tuesday evening

My old Dell XPS notebook runs at home while I go to work and I occasionally RDP back to it. I usually see some IMs on my Pidgin IM when I log in, thought it would be a good idea to script something in Python+ D-BUS to send mail to my gmail account once I get any new messages, so I can check them on my work desktop, or Wince mobile. Till I realized the Win32 Pidgin doesn't have D-BUS.

The next best alternative calls for a quick and dirty scripting using Perl(ActivePerl 5.10). Boy the documentation is pretty sparse. After googling around(http://sriunplugged.blogspot.com/2009/03/autoreminder-in-pidgin.html) I've managed to come up with a script that works. Phew. Todo: presense + stripping html tags from the messages.

Bedtime now.



use Purple;
use Pidgin;
use Net::SMTP;

%PLUGIN_INFO = (
perl_api_version => 2,
name => "Email New Message Plugin",
version => "0.3",
summary => "script to mail to user any message received",
description => "Sends an email when a new message is recieved",
author => "Alvin Ng (Rebooting at gmail) ",
url => "http://pidgin.im",
load => "plugin_load",
unload => "plugin_unload"
);
sub plugin_init {
return %PLUGIN_INFO;
}
sub plugin_load {
my $plugin = shift;
Purple::Debug::info("testplugin", "plugin_load() - Test Plugin Loaded.\n");
Purple::Signal::connect(Purple::Conversations::get_handle(),'received-im-msg',$plugin,\&im_received,

'received-im-msg');

}


sub im_received {
Purple::Debug::info('incoming', "im_received\n");
# SMTP details here
my $smtp_server="smtp server";
my $sender='sender@domain';
my $recp = 'recipient@domain';

my ($account, $imsender, $message, $conv, $flags) = @_;

#log it

#send it, TODO: mail it when the client is set to any other status but online

$smtp = Net::SMTP->new($smtp_server);
$smtp->mail($sender);
$smtp->to($recp);

$smtp->data();
$smtp->datasend("To: $recp \n");
$smtp->datasend("From: $imsender\n");
$smtp->datasend("Subject: New Message from $account::$imsender \n\n");
$smtp->datasend("$account : $imsender \n");
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit;

}
sub plugin_unload {
my $plugin = shift;
Purple::Debug::info("testplugin", "plugin_unload() - Test Plugin Unloaded.\n");
}

No comments: