#!/usr/bin/perl -w

################################################################
# Ubuntu Live Chat Support 0.3.14 "All you need is Love"       #
################################################################
# File: livechatsupport-check				       #				
# Version: 0.3.14                           		       #
# Description: Check if nick is available                      #
#                                                              #
# Author: Joel Bryan T. Juliano <joelbryan.juliano@gmail.com>  #
# License: GPL-2                                               #
################################################################

# NOTE: 0.3.14 needs a fully functional NOTICE signal handler, 
#       other than server numeric value 320, because it is only
#       functional if the user who owns the nick is identified to
#       NickServ. 
#
#       NOTICE signal handler is available through Net::IRC perl module
#       but is not included in standard perl modules. I want to use
#	the basic IO::Socket exclusively. If you have a solution,
#	please send me an email.
#
#                                            - joelbryan

use strict;

use IO::Socket;

my $server = "irc.freenode.net";
my $nick = $ENV{"register_username"};

my $input = @_;

my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto => 'tcp') or
				die system("echo \"100\" && zenity --info --title=\"Network Error\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"There was an error connecting to the internet, please make sure you are connected to the internet. \" && ubuntu-live-support $nick $nick check");
print "30"."\n";
print $sock "NICK $nick"."\r"."\n";
print $sock "USER $nick 8 * :Ubuntu Support Registration"."\r"."\n";

while (my $input = <$sock>) {
	if ($input =~ /^:?
	       \S+?
	       \s+?
	       004
	       \b/x) {
		print "50"."\n";
		last;
    } 
	elsif ($input =~ /^:?
	       \S+?
	       \s+?
	       433
	       \b/x) {
		print "100"."\n";
		die system("zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered and \"$nick\" is using it right now, please choose another nick\" && ubuntu-live-support $nick $nick check");
   }
	elsif ($input =~ /^:?
	       .+?
	       \s+?
	       NOTICE
	       \b/x
	       ) {
		print "100"."\n";
		die system("zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered, please choose another nick\" && ubuntu-live-support $nick $nick check");
   }
	elsif ($input =~ /Banned/) {
		print "100"."\n";
		die system("zenity --info --title=\"Registration Error\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"There was an error connecting to Freenode.net, please send an email to staff\@freenode.net. \" && ubuntu-live-support $nick $nick check");
   }
	elsif ((lc $nick eq "nickserv") and $input =~ /nickname/i ) {
		print "100"."\n";
		die system("zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered, please choose another nick\" && ubuntu-live-support $nick $nick check");
   }
	elsif ($input =~ /^:?
	       \S+?
	       \s+?
	       465
	       \b/x) {
		print "100"."\n";
		die system("zenity --info --title=\"Registration Error\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"There was an error connecting to Freenode.net, please send an email to staff\@freenode.net. \" && ubuntu-live-support $nick $nick check");
   }
}

print $sock "WHOIS $nick"."\r"."\n";

while (my $input = <$sock>) {
    chop $input;
    if ($input =~ /^PING(.*)$/i) {
        print $sock "PONG $1"."\r"."\n";
   } 
    elsif
	($input =! /^:?
	       \S+?
	       \s+?
	       320
	       \b/x) {
        print "100"."\n";
        die system("zenity --info --text=\"Nick $nick is available, you can now use register it as your nickname.\" && ubuntu-live-support $nick $nick register");
   } 

    elsif ($input =~ /^:?
	       .+?
	       \s+?
	       NOTICE
	       \b/x
	       ) {
		print "100"."\n";
		die system("zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered, please choose another nick\" && ubuntu-live-support $nick $nick check");
   }
    elsif ((lc $nick eq "nickserv") and $input =~ /nickname/i ) {
		print "100"."\n";
		die system("zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered, please choose another nick\" && ubuntu-live-support $nick $nick check");
   }
    else { 
	if ($input =~ /^:?
	       \S+?
	       \s+?
	       320
	       \b/x) 
   {
	die system("echo 100 && zenity --info --title=\"Nick Unavailable\" --window-icon=/usr/share/icons/gnome/16x16/stock/generic/stock_people.png --text=\"Nickname \"$nick\" is already registered, please choose another nick\" && ubuntu-live-support $nick $nick check");
}

}

}
