#!/usr/bin/perl -w
################################################################
# Ubuntu Live Chat Support 0.3.14 "All you need is Love"       #
################################################################
# File: livechatsupport-register			       #
# Version: 0.3.14                     			       #
# Description: Check if nick is available and register it.     #
#                                                              #
# Author: Joel Bryan T. Juliano <joelbryan.juliano@gmail.com>  #
# License: GPL                                                 #
################################################################

use strict;

use IO::Socket;

my $server = "irc.freenode.net";
my $nick = $ENV{"register_username"};
my $password = $ENV{"register_password"};
my $email = $ENV{"register_email"};
my $website = $ENV{"register_website"};
my $phone = $ENV{"register_phone"};
my $gsm = $ENV{"register_gsm"};
my $user_settings = $ENV{"user_settings"};
my $digits = 0;
my $input = @_;

my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto => 'tcp') or
				die system("echo \"100\" && zenity --error --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 to begin registration. \" | kill -s 9 $$ && ubuntu-live-support $nick $nick register");
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 =~ /NickServ/) {
        if($input =~ /owned/) {
	print $sock "QUIT"."\r"."\n";
        print "100"."\n";
    die system("zenity --error --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\" | kill -s 9 $$ && ubuntu-live-support $nick $nick check");
            }
    }
    elsif ($input =~ /^:?
           \S+?
           \s+?
           433
           \b/x) {
	print $sock "QUIT"."\r"."\n";
        print "100"."\n";
        die system("zenity --error --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\" | kill -s 9 $$ && ubuntu-live-support $nick $nick check");
   }
    elsif ($input =~ /^:?
           \S+?
           \s+?
           465
           \b/x) {
	print $sock "QUIT"."\r"."\n";
        print "100"."\n";
        die system("zenity --error --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. \" | kill -s 9 $$ && ubuntu-live-support $nick $nick check");
   }   
    elsif ($input =~ /Banned/) {
	print $sock "QUIT"."\r"."\n";
        print "100"."\n";
        die system("zenity --error --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. \" | kill -s 9 $$ && ubuntu-live-support $nick $nick check");
   }
    elsif ($input =~ /freenode-connect/) {
	print "69"."\n";
	print $sock "NickServ REGISTER $password"."\r"."\n";
		while (my $input = <$sock>) {
		    if ($input =~ /NickServ/) {
				if ($input =~ /wait/) {
				(my $num)=$input=~/(\d{1,5})/;
				system("sleep 10 && echo 70");
				system("sleep 10 && echo 71");
				system("sleep 10 && echo 72");
				system("sleep 10 && echo 73");
				system("sleep 10 && echo 74");
				system("sleep 10 && echo 75");
				system("sleep 10 && echo 76");
				system("sleep 10 && echo 77");
				system("sleep 10 && echo 78");
				system("sleep 10 && echo 79");
				system("sleep 10 && echo 80");
				sleep($num+20);
				print $sock "NickServ REGISTER $password"."\r"."\n";
				print "80"."\n";
				print $sock "NickServ IDENTIFY $password"."\r"."\n";
			        print "83"."\n";
				print $sock "NickServ SET EMAIL $email"."\r"."\n";
				print "85"."\n";
				print $sock "NickServ SET PHONE $phone"."\r"."\n";
				print "90"."\n";
				print $sock "NickServ SET URL $website"."\r"."\n";
				print "95"."\n";
				print $sock "NickServ SET GSM $gsm"."\r"."\n";
				print "98"."\n";
			if ($user_settings =~ /hide/) { 
				print $sock "NickServ SET HIDE ALL ON"."\r"."\n";
				sleep(10);
				}
				print $sock "QUIT"."\r"."\n";
				system("sleep 30 && echo 99");
				print "100"."\n";
				die system("zenity --info --text=\"Registration Successful!, you can now login using the nickname $nick\" | kill -s 9 $$ && ubuntu-live-support $nick $nick login");
 			}
			else {
				print $sock "NickServ REGISTER $password"."\r"."\n";
				print "80"."\n";
				print $sock "NickServ IDENTIFY $password"."\r"."\n";
			        print "83"."\n";
				print $sock "NickServ SET EMAIL $email"."\r"."\n";
				print "85"."\n";
				print $sock "NickServ SET PHONE $phone"."\r"."\n";
				print "90"."\n";
				print $sock "NickServ SET URL $website"."\r"."\n";
				print "95"."\n";
				print $sock "NickServ SET GSM $gsm"."\r"."\n";
				print "98"."\n";
			if ($user_settings =~ /hide/) { 
				print $sock "NickServ SET HIDE ALL ON"."\r"."\n";
				sleep(10);
				}
				print $sock "QUIT"."\r"."\n";
				system("sleep 30 && echo 99");
				print "100"."\n";
				die system("zenity --info --text=\"Registration Successful!, you can now login using $nick\" | kill -s 9 $$ && ubuntu-live-support $nick $nick login");
		}
	}
}

}

  if($input =~ /PING/) {
        if($input =~/:/) {
                       if(index($input, ":") != -1) {
                         $digits = substr($input, index($input, ":") + 1, (length($input) - index($input, ":")));
                         print $sock "PONG $digits\r\n";
           }
       }
   }
}

	print $sock "QUIT"."\r"."\n";
	die

