#!/usr/bin/perl -w
################################################################
# Ubuntu Live Chat Support 0.3.14 "All you need is Love"       #
################################################################
# File: livechatsupport-register-cleanup		       #
# Version: 0.3.14                     			       #
# Description: Drop the previous cancelled registration        #
#                                                              #
# Author: Joel Bryan T. Juliano <joelbryan.juliano@gmail.com>  #
# License: GPL                                                 #
################################################################

use strict;

use IO::Socket;

my $server = "irc.freenode.net";
my $nick = $ENV{"last_register_nick"};
my $password = $ENV{"last_register_pass"};
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 "70"."\n";
print $sock "NICK $nick"."\r"."\n";
print $sock "USER $nick 8 * :Ubuntu Support Registration"."\r"."\n";

while (my $input = <$sock>) {

	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");
	print $sock "NICKSERV DROP $nick $password"."\r"."\n";
	print $sock "QUIT"."\r"."\n";
	system("sleep 30 && echo 99");
	die print "100"."\n";

  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";
           }
       }
   }
}


