Saturday, March 14, 2009

Auto Away Notifier for Pidgin in Perl

Whenever I am away from laptop my chat buddies leave me lots message and finally lots of "You there???". So I thought of making a script that would inform my buddies that I am away when someone sends me a message.
This method works with Pigin API.
I have used /proc/acpi/button/lid/C267/state file to find if the lib of the laptop is closed or not. This file may vary in your laptop. So try to find the find the file in your computer that stores the status of the lid. It should be a file in /proc/acpi/button/lid/ folder and replace the file accordingly in your computer.
The Content of /proc/acpi/button/lid/C267/state is state: open and state: closed when the lid was closed.

We save this file as /usr/lib/purple-2/autonotifier.pl and use chmod +x /usr/lib/purple-2/test.pl to give the plugin execute permission. Now restart the Pidgin if it is running or open pidgin and go to Tools->Plugins and you should see a plugin named "Auto Away Plugin". Activate it and that's it.
I have used /sys/class/leds/b43-phy0\:\:radio/brightness file to notify me if there is any messages while i was away. It uses my Wi-fi light to notify me. When there is a message then the Wi-fi light blinks. This too is computer specific so find the file that controls any of the unused leds and replace that file too.
use Purple;
%PLUGIN_INFO = (
perl_api_version => 2,
name => "Auto Away Plugin",
version => "0.1",
summary => "Auto Away Notifier",
description => "Auto Away Notifier in Perl",
author => "R.Srijith <rsrijith007\@gmail.com",
url => "http://sriunplugged.blogspot.com",
load => "plugin_load",
unload => "plugin_unload"
);
sub plugin_init {
return %PLUGIN_INFO;
}

sub im_received {

my ($account, $sender, $message, $conv, $flags) = @_;
Purple::Debug::info("autonotify", "Message recieved\n");
$conv1 = Purple::Conversation->new(1, $account, $sender);
open FILE, ">>/home/rsrijith/logs/autonotifier.txt" or die $!;
open LID, "</proc/acpi/button/lid/C267/state" or die $!;
#$message=~s/<.*>//g;
while(<LID>)
{
if($_ =~ m/closed/)
{
print FILE "$sender:$message\n";
$im = $conv1->get_im_data();
if ($im) { print "ok.\n"; } else { print "fail.\n"; }
system('(if [ ! -e /var/lock/autoremind ]; then while grep -m 1 -q closed /proc/acpi/button/lid/C267/state; do touch /var/lock/autoremind; sleep 1; echo -n 0 >/sys/class/leds/b43-phy0\:\:radio/brightness; sleep 1;echo -n 1 >/sys/class/leds/b43-phy0\:\:radio/brightness; done; if [ -e /var/lock/autoremind ]; then rm /var/lock/autoremind; fi; fi)&');
$im->send("This is an automated response system: R Srijith is not at the desk rite now.");
}
}
close FILE;
}

sub plugin_load {
$plugin = shift;
$conversation_handle = Purple::Conversations::get_handle();
Purple::Signal::connect($conversation_handle, "received-im-msg", $plugin, \&im_received, $data);
Purple::Debug::info("autonotify", "Auto Notify Loaded\n");
}

sub plugin_unload {
Purple::Debug::info("autonotify", "Auto Notify Unloaded\n");
}

So try it out and comment me on how it went.

1 comment:

  1. Perl is the best scripting language for Text processing and handle regex. I have posted few articles related to those at my blog

    http://icfun.blogspot.com/search/label/perl

    Also Perl's Cpan has lots of support that I don't even need to think extra while developing project. I didn't find such help on other programming language except Java and .NET

    ReplyDelete