Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Tuesday, March 17, 2009

Backend For Panel Reminder

I had already posted about creating a Display Reminders In Gnome Panel. This post is an addition to that script. This is a backend script that writes what to print into the panel_remind file. I use this script to print Birthday Reminder and Things to remember of which I will be posting shortly.
So the Code is simple Perl script.
#!/usr/bin/perl
our $printl;our $i=0;our $r=0;
$| = 1;
while(TRUE)
{
open (RB,"/home/rsrijith/perl/reminder");
my @line=<RB>;
close RB;
if($line[0])
{
while($line[$i])
{
open (WR,">/home/rsrijith/panel_remind");
my @s=split(":",$line[$i]);
my $nowtime=timelocal(localtime());
my $a=$s[2]+300;
if(($s[0]-$nowtime<=$a)&&($s[0]-$nowtime>=0))
{
my $t= to_int((($s[0]-$nowtime)/60));
while($t>0)
{
open (WR,">/home/rsrijith/panel_remind");
print WR "Red:In $t Mins - $s[3]";
close WR;
$r=1;
$t--;
sleep(60);
if( $t == 0)
{ `notify-send "$s[3]" -u critical -t 60000`}
}
}
open (WB,">/home/rsrijith/perl/reminder");
my $nowtime=timelocal(localtime());
if($s[0]-$nowtime>0)
{
print WB "$line[$i]";
close WB;
}
$i++;

}
if($r eq 0)
{
bday();
}
$i=0;
}
else
{
print "Birthday";
bday();
}
}


sub bday
{
open (RB,"/home/rsrijith/shell/birthdays");
my @line=<RB>;
close RB;

if((!$line[0])&&($r==0))
{
print "nothing to remind";
$printl="Black:Nothing to Remind \n:(";
open (WR,">/home/rsrijith/panel_remind");
print WR $printl;
close WR;
sleep(100);
}
while($line[$i])
{
$_=$line[$i];
if(m/Happy/)
{
$printl="Blue:$line[$i]";
}
else
{
$printl="Black:$line[$i]";
}
open (WR,">/home/rsrijith/panel_remind");
print WR $printl;
close WR;
$i++;
sleep(5);
}
$i=0;
return;
}
This script reads from files
  • /home/rsrijith/shell/birthdays: The file contains birthdays of my friends that I need to be reminded of. The method to print the upcoming birthdays into this file is posted at <will be posted soon>.
  • /home/rsrijith/perl/reminder:The file contains reminders that I need to  be reminded. I used another script to write into that file which is posted at <will be posted soon >.
This program needs libnotify  package. The package name is libnotify1 in my computer. It may vary with linux you are in.



So the ordinary panel reminder can be made of some use using this script.
So try it out and tell me how it went.

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.

Saturday, February 7, 2009

Display Reminders In Gnome Panel

 It was a long standing requirement of mine to create a reminder software or something which can keep me reminding of things as I always keep forgetting things. So I made a reminder that displays my reminders on the Gnome Panel.
It looks something like this:

So how do we do it?

1st of all we need to display the program we need in the Add to Panel.

For that we add to the bonobo server list. We do this by adding the following code in the folder  /usr/lib/bonobo/servers/. Let us name the file as GNOME_PerlApplet.server
The content of this file should be :
<oaf_info>
  <oaf_server iid="OAFIID:PerlSampleApplet_Factory" type="exe" location="/home/rsrijith/perl/panel-applet">
    <oaf_attribute name="repo_ids" type="stringv">
      <item value="IDL:Bonobo/GenericFactory:1.0"/>
      <item value="IDL:Bonobo/Unknown:1.0"/>
    </oaf_attribute>
    <oaf_attribute name="name" type="string" value="Panel Reminder"/>
    <oaf_attribute name="description" type="string" value="Perl Program working in Panel"/>
  </oaf_server>

  <oaf_server iid="OAFIID:PerlSampleApplet" type="factory" location="OAFIID:PerlSampleApplet_Factory">
    <oaf_attribute name="repo_ids" type="stringv">
      <item value="IDL:GNOME/Vertigo/PanelAppletShell:1.0"/>
      <item value="IDL:Bonobo/Control:1.0"/>
      <item value="IDL:Bonobo/Unknown:1.0"/>
    </oaf_attribute>
    <oaf_attribute name="name" type="string" value="Panel Reminder"/>
    <oaf_attribute name="description" type="string" value="Perl Program working in Panel:R Srijith"/>
    <oaf_attribute name="panel:category" type="string" value="Amusements"/>
    <oaf_attribute name="panel:icon" type="string" value="bug-buddy.png"/>
  </oaf_server>
</oaf_info>
So this specifies the location of the perl file to run on adding this applet. So here it is /home/rsrijith/perl/panel-applet . This file should print the reminders to be printed on the panel.
The program need the Following packages:

Perl packages:

    • ExtUtils::Depends
    • ExtUtils::PkgConfig
    • Gnome2::GConf
    • Gnome2::PanelApplet
    You install this using
    sudo perl -MCPAN -e 'install <package-name>'

    Linux Package:
      • libpanelappletmm-2.6-1c2
      • libpanelappletmm-2.6-dev
      You install this using
      sudo apt-get install <package-name>

        Thus the perl file content is :
        #!/usr/bin/perl -w

        use warnings;
        use utf8;
        use Gnome2::PanelApplet;
        Gnome2::Program->init ('A Stupid Applet Written in Perl', '0.01', 'libgnomeui',
                               sm_connect => FALSE);

        Gnome2::PanelApplet::Factory->main ('OAFIID:PerlSampleApplet_Factory',
                                            'Gnome2::PanelApplet',
                                            \&fill);


        sub fill {
          my ($applet, $iid, $data) = @_;

          if ($iid ne 'OAFIID:PerlSampleApplet') {
            return FALSE;
          }
         
          my $menu_xml = <<EOX;
        <popup name="button3">
          <menuitem name="Properties Item"
                    verb="Birthdays"
                    _label="Birthdays"
                pixtype="stock"
                pixname="gtk-properties"/>
          <menuitem name="DesktopSwitcher Item"
                verb="DesktopSwitcher"
                _label="Wallpaper Switcher"
                pixtype="stock"
                pixname="gtk-stock"/>
            <menuitem name="Walldelete Item"
                verb="Walldelete"
                _label="Wallpaper Delete"
                pixtype="stock"
                pixname="gtk-stock"/>              
          <menuitem name="About Item"
                verb="About"
                _label="About"
                pixtype="stock"
                pixname="gnome-stock-about"/>
        </popup>
        EOX

          my $cb_mapping = {
            Birthdays => [\&properties_callback, 'default!'],
            DesktopSwitcher => \&deskswitcher_callback,
            Walldelete => \&walldelete_callback,
            About => \&about_callback,
          };

          $applet->setup_menu($menu_xml, $cb_mapping, 'default?');
          our $i=0;our @line;

          our $label = Gtk2::Label->new;
          $label->set_justify("left");
            $label->set_line_wrap(TRUE);
            $label->modify_font(Gtk2::Pango::FontDescription->from_string("Monospace 10"));
            $label->modify_fg("normal", Gtk2::Gdk::Color->parse("Black"));
          $applet->add ($label);
          $applet->show_all;
         
         Glib::Timeout->add (5000, \&update_value);
         #Glib::Timeout->add (500, \&update_list);
         sub update_list{
         open FILE,"/home/rsrijith/panel_remind";
          @line=<FILE>;
          close FILE;
         }
         sub update_value
         {
         
          my @string=split(':',$line[0]);
          $label->modify_fg("normal", Gtk2::Gdk::Color->parse($string[0]));
          $label->set_markup("\n".$string[1]);
          update_list();
         }
        }

        sub deskswitcher_callback
        {
         system('/home/rsrijith/shell/deskback 2>>/home/rsrijith/logs/desktopbackground');
        }

        sub walldelete_callback
        {
         system('/home/rsrijith/shell/deletewall 2>>/home/rsrijith/logs/desktopbackground');
        }

        sub properties_callback {
            my $b="";
                open BDAY,"/home/rsrijith/shell/birthdays1";
            while(<BDAY>)
            { $b.=$_."\n";
            }
          my $dialog = Gtk2::MessageDialog->new (undef, [],
                                                 'info', 'ok',
                                                 $b);
          $dialog->run;
          $dialog->destroy;
        }


        sub about_callback {
          my $about = Gnome2::About->new ('Panel Reminder', '0.1',
                                          'Perl Program working in Panel',
                                          '© 2009 R.Srijith',
                                          'R.Srijith <rsrijith007@gmail.com');
          $about->show;
        }

        This program prints the contents of /home/rsrijith/panel_remind
        An Example of the content of that file should be as

        Blue:Happy Birthday to Mahima
        So that's it!! And Happy Birthday Mahima.


        Comment me if you have any doubts or if it's not working.

        Sunday, November 16, 2008

        Online Version of Perl Invisible FInder

        Here is the online version invisible finder for gmail.
        Felt a lot of people needs this.
        Hope it is helpful
        And please dont believe it's something foolish to get password. I need to login to get the invisible user List.
        And login 2 times atleast and see only the common users in both time. This is because it sometimes gives some false results ie only less than 10%
        Happy Picking Hiding People.
        Now you can Run but cant hide :P
        There is a Problem with the server I had been using the script does'nt work now as the server blocks opening of the 5222 port, which is needed for the script's working.I am trying to find an alternative server.In the mean time you can try running the script on your computer.The script I have used is Find Invisible Users In Gmail

        Sunday, September 21, 2008

        Find Invisible Users In Gmail

        Finding who is invisible is something that I always wanted to do.It's always fun to ping a person who feels none can see him/her online. While studying XMPP protocol I found out that even when the person is offline the chat client knows that person is online.
        The Chat Client should know who all are really online at any given time.This is because even when the person is invisible he/she should have a buddy list showing online users.

        Ah!! Guess you did'nt understand a word till now.Let me try making it simpler.

        Let's assume two users A and B are online now.Both A and B are Available (ie the chat client shows them online).Both A and B has a list of online users in his chat client(eg:google talk,gmail inbox chat,pidgin). Now this online users need to be kept constantly updated. This is done by the XMPP protocol(this is the protocol used by google for it's chat).Now even if B is invisible still it needs to keep it's online users list updated as it needs to show B the online users.
        So whenever B logs into his account it initially sends a presence notification to all the users in it's contacts list saying that B is online as "Please show me online in our online user list".
        All online users respond back with their online status and status message.
        This is where the invisible people shows up.The chat server replies back with their status. The only difference in them is that they respond with their status as "Unavailable".
        So my aim was to find out all users that respond back to me with Unavailable status and display them.
        Ok enough of explanation of how it works let's see the code

        #!/bin/perl

        use strict;
        use Net::XMPP;

        my $sttime=time;
        print "Username:$ARGV[0]\n";
        my $username = "$ARGV[0]";
        my $password = "$ARGV[1]";

        my $resource = "SNIFFER";


        my $hostname = 'talk.google.com';
        my $port = 5222;
        my $componentname = 'gmail.com';
        my $Contype = 'tcpip';
        my $tls = 1;

        my %online;
        my $Con = new Net::XMPP::Client();
        $Con->SetCallBacks(presence=>\&presence_ch);


        my $status = $Con->Connect(
        hostname => $hostname, port => $port,
        componentname => $componentname,
        connectiontype => $Contype, tls => $tls);

        if (!(defined($status))) {
        print "ERROR: XMPP connection failed.\n";
        print " ($!)\n";
        exit(0);
        }



        # Change hostname
        my $sid = $Con->{SESSION}->{id};
        $Con->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

        # Authenticate
        my @result = $Con->AuthSend(
        username => $username, password => $password,
        resource => $resource);

        if ($result[0] ne "ok") {
        print "ERROR: Authorization failed: $result[0] - $result[1]\n";
        exit(0);
        }
        else
        {
        print "Logged in Sucessfull!\nInvisible Users:\n";
        }
        $Con->PresenceSend(show=>"Available");
        sub presence_ch
        {
        my $p=0;
        my $x;
        my $presence = pop;
        my $from = $presence->GetFrom();
        $from =~ s/([a-zA-Z@.]*)\/(.*)/$1/;
        if($presence->GetType() eq "unavailable")
        {
        if (exists $online{$from})
        {
        delete($online{$from});
        }
        else
        {
        $online{$from}=$presence->GetType()."!!" .$presence->GetStatus();
        print $from."\n";
        }
        }
        else
        {
        $online{$from}=$presence->GetType()."!!" .$presence->GetStatus();
        }
        }
        my $currtime;
        while(1)
        {
        $currtime=time;
        if($currtime-$sttime>10)
        {last;}
        my $res=$Con->Process(1);
        if(!(defined($res))){ last;}}



        XMPP protocol needs SSL encryption.So you need to install the perl modules for it.So before trying out the code make sure u have the following modules:
        • IO ::Socket ::SSL (>=0.81)
        • XML ::Stream
        • Net ::XMPP
        • Authen ::SASL
        To install the packages open the CPAN shell using the command
        sudo perl -MCPAN -e 'shell'

        Followed by
        install <module name>

        for each of the module listed above
        Also you need to install the package send-xmpp and it's dependecies
        sudo apt-get install send-xmpp

        Run the program by running it as
        perl invi.pl <username> <password>
        I have also made a online version of this script you can use to find invisible users the link is
        http://sriunplugged.blogspot.com/2008/11/online-version-of-perl-invisible-finder.html
        Worked?? Did'nt work?? Comment me