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

Automatic Desktop Wallpaper Changer

I have a lot of wallpaper that I wanted to switch automatically. After a lot of search i found that there a command way to control all the gnome settings. So I the command I used is "gconftool-2".
So code is like:
#!/bin/bash
folder="/home/rsrijith/Pics" #folder where the pics lie in
find $folder -iregex ".*.jp.?g" >$folder/temp_back #list all images in the folder
no=$(cat $folder/temp_back|wc -l)
rand=$(echo `expr $RANDOM % $no`)
while ( grep -q $rand $folder/.last50 )
do
rand=$(echo `expr $RANDOM % $no`)
done
echo $rand >> $folder/.last50
tail -50 $folder/.last50 >$folder/temp
cat $folder/temp >$folder/.last50
rm $folder/temp
file=$(sed -n $rand'p' $folder/temp_back)
ON_USER=rsrijith #your username here

export DBUS_SESSION=$(grep -v "^#" /home/$ON_USER/.dbus/session-bus/`cat /var/lib/dbus/machine-id`-0)

sudo -u $ON_USER $DBUS_SESSION /usr/bin/gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$file" && echo "Worked On:"$(date) >>/home/$ON_USER/logs/desktopbackground
echo "Set Wallpaper as:"$file >>/home/$ON_USER/logs/desktopbackground
rm $folder/temp_back

I have used the DBUS_SESSION variable to make it compatible with Ubuntu 8.10. The Dbus session is a bit bit different from earlier versions. The change is that it does'nt accept the change until the gnome is restarted. It's due to the new session that is made each time you update that value.

So the folder variable should the folder where the wallpapers should lie. You can put pictures in folders inside that folder. All of it will be taken. But I have specified to take only jpg and jpeg only.Now a random image is picked from that using the environment variable $RANDOM. Next we update the gnome settings using gconftool-2 if that image was'nt used in the last 50 changes.

Thus this file picks an image and puts it as wallpaper. So the next part is making this change automatically. For this we use cron (linux scheduler).
Use the command below to enter the crontab
sudo crontab -e
Enter the crontab entry as
*/15 * * * * /home/rsrijith/shell/deskback 2>>/home/rsrijith/logs/deskbackground
Check the entry using
sudo crontab -l
That's all!!!
Anyone tried? Worked? Comment me !!!

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

Sunday, September 7, 2008

Automatic Command Archive

The default command storing size of ubuntu is 500 commands. It stores the commands in home folder in the file ".bash_history" .
The problem is 500 commands is too short of a size.
One method is to increase the command size to a large value by changing the $HISTFILESIZE variable using the following command
export HISTFILESIZE=1000

But this will last only till that terminal is closed.

Do make tat permanent or rather make that command execute every time u open a terminal we can put that command in .bashrc file in the home folder itself
The commands in this folder will be executed each time you open a terminal. So adding that command is one way to solve tat problem.

Or there is another way. Make a script tat will automatically archive the old commands to a file and compress it and save if it bigger than a specified size. That's a better method i feel.
Also i try to remove the repeated commands and store only unique ones there by not wasting space storing copies. This is a better method i feel than making it accumulate in the .bashrc.
So let's see how to do it.
File name:logarchive

echo "Ran at:"$(date) >>/home/$1/logs/commandrotate
echo "Size of History: "$(cat /home/$1/.bash_history|wc -l) >>/home/rsrijith/logs/commandrotate
if [ $(cat /home/$1/.bash_history|wc -l) -gt 400 ]
then
echo "Archived" >>/home/rsrijith/logs/commandrotate
cat -n /home/$1/.bash_history|sort -ru -k2|sort -n -k1|sed 's/[ ]*[0-9]*\t//' > /home/$1/.bash_history
cat /home/$1/.bash_history >>/home/$1/history/cmd_history
touch /home/$1/history/temp
cat -n /home/$1/history/cmd_history|sort -ru -k2|sort -n -k1|sed 's/[ ]*[0-9]*\t//' >/home/$1/history/cmd_history
tail -100 /home/$1/.bash_history >/home/$1/history/temp
cat /home/$1/history/temp >/home/$1/.bash_history
rm /home/$1/history/temp
fi
logrotate /home/$1/history/logrot.conf
if [ -e /home/$1/history/cmd_history.1 ]
then
gunzip "/home/$1/history/cmd_archive.gz"
cat /home/$1/history/cmd_history.1 >>/home/$1/history/cmd_archive
gzip -f "/home/$1/history/cmd_archive"
rm /home/$1/history/cmd_history.1
rm /home/$1/history/cmd_archive
fi

This script also reduces the current .bashrc file to only unique entry.Although there is a command (or rather environment variable) to store only unique values. But that only keeps the first entry in the file.So only the initial entry stays. I usually use the recently used commands using the up arrow in bash. So I need to maintain the order in which I have typed the command too. That's why I have used cat -n to get the order and have used it to order it in the end.
I have made the script run when the commands exceeds 400.

Once the cmd_history file goes over 1MB ,I try to compress it and store in as an archive. This is why i have used logrotate file

File name:logrot.conf

/home/rsrijith/history/cmd_history {
missingok
size 1M
rotate 1
}

Now we need to run it every 30mins or 1hr according to how much you use the commands.I have used 30mins just in case.So we use crontab (A scheduler in Linux).The crontab file is /etc/crontab.We can edit it using the command.
crontab -e

As we need to run it as root.Run the command as root.
So the crontab entry is as

*/30 * * * * root /home/rsrijith/history/logarchive rsrijith 2>>/home/rsrijith/logs/commandrotate

Saturday, September 6, 2008

"Send To" in Linux

It's been a common practice among the windows users to right click and search for "Send To" option when a file needs to copied to the USB or any other Drive.
Why spare me, even i used to do this while using windows, when to my astonishment i found that there exists no such option in the default window ("Nautilus") in Ubuntu. So i thought of making one for my convenience.
Nautilus supports user scripts inside it. This can be easily embedded into the manager by inserting it into the folder ~/.gnome2/nautilus-scripts/ folder where ~ stands for the shortcut for home folder.
I used zenity ( a in build gnome dialog box) to get the radio box for selecting the drive to copy to.
The Send to function looks like this



The Code :
#!/bin/bash
destination='Choose Destination'
title_destination='Send files to:'

copy='Copying'
title_copy='Please wait...'

success='Files successfully copied'
title_success='Success'

errors='Something went wrong'
title_errors='Error'

no_writable='Destination is either not existant or writable'
title_no_writable='Error'
vd=$(ls -tm /media/sed 's/cdrom[0-9]\?[,]\?[ ]\?//g')
#options=${vd//, / FALSE /media/}
IFS=$'\t\n'
options=$(echo $vdsed 's_[ ]\?\([^,]*\),_\/media\/\1\tFALSE\t_g'sed 's_\(.*\)\tFALSE\t_\1_g');
destinazione=$(zenity --list --title "$title_destination" --text "$destination" --radiolist --column " " --column "Device" TRUE $options);

if [[ "$destinazione" = "" ]]; then
exit
fi

if [[ -w $destinazione ]]; then
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS=$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
destinazione=$(echo $destinazione)
cp -R "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" "$destinazione" zenity --progress --pulsate --auto-close --title="$title_copy" --text="$copy"
if (( $? == 0 )); then
zenity --info --text="$success" --title "$title_success";
else zenity --info --text="$errors" --title "$title_errors";
fi
else zenity --info --text="$no_writable" --title "$title_no_writable";
fi