#!/usr/bin/perl -w
#
# SDServer by dccserv for xchat (v1.8.6+).
# Released under GNU General Public License
#
# Needed to run: Perl 5 (vital) + A computer running SDClone v1.0+
#
# How to setup a client/server section
# Define $ClientNick to your bot nick
# Define $ClientPwd the same as in SDClone
# Set in SDClone $ServerEnable="true"; <--
# That's all !!!
# if your bot nick change do /sdclient newbotnick to retake control
# Once you have control of the client, /sdhelp would works and give any help you need
##########################################################
# Version:
# 1.0          - First version ;-)
# 1.1          - Add a command to get stats of our client
#              - Add a command to get  -slots of our client
#              - Add a command to add a channel to serve
#              - Add a command to stop serve in a channel
#              - Add a command to get -server from our client
#              - Add a command to get -info from our client (info from a file)
#              - Add a command to get -stats from our client
#              - Add buttons
#              - Fix the "can't find sdc_client_connect" in v1.0
#              - Note: It is a pain to do SDClone multi-languages, that's why SDServer isn't for now.
#                      But the client computer will answers in the language you have choose
# 2.0          - Update to add all functions compatibles with SDClone 2.0
##########################################################

###############################################
# imporant variables to define are here
$ClientNick="emacs";               # Our default bot nickname to control - Change to your Own BOT name
$ClientPwd="asimplepassword";      # it's the password we use : must be the same of course as in SDClone
                                   # no spaces in it !
$clientReady="false";              # use to be sure client is the good one, so prevent sending your password to wrong person
$Ver="2.0";
###############################################
# This affect the way the scripts works, not vital but useful
IRC::register("SDServer",$Ver,"sdc_shutdown","");
IRC::print("SDServer: v$Ver by dccserv init... ");
# common to SDClone, try /sdhelp to get the picture
IRC::add_command_handler("sdList","sdc_list");
IRC::add_command_handler("sdQue","sdc_que");
IRC::add_command_handler("sdkillque","sdc_killque");
IRC::add_command_handler("sdLoad","sdc_load");
IRC::add_command_handler("sdSet","sdc_set");
IRC::add_command_handler("sdHelp","sdc_help");
IRC::add_command_handler("sdfriend","sdc_friend");
IRC::add_command_handler("sdSave","sdc_save");
IRC::add_command_handler("sdstatus","sdc_status");
IRC::add_command_handler("sdPub","sdc_pub");
IRC::add_command_handler("sdclone","sdc_sdclone");
IRC::add_command_handler("sdban","sdc_ban");
IRC::add_command_handler("sdunban","sdc_unban");
IRC::add_command_handler("sdbanlist","sdc_banlist");
IRC::add_command_handler("sdsaychannel","sdc_saychannel");
IRC::add_command_handler("sdsay","sdc_say");
IRC::add_command_handler("sdjoin","sdc_join");
IRC::add_command_handler("sdleave","sdc_leave");
IRC::add_command_handler("sdstartserve","sdc_add_serving_channel");
IRC::add_command_handler("sdstopserve","sdc_del_serving_channel");

IRC::add_command_handler("sdcontrolsend","sdc_controlsend");
IRC::add_command_handler("sdsend","sdc_send");
IRC::add_command_handler("sdstopsend","sdc_stopsend");
IRC::add_command_handler("sduserinfo","sdc_userinfo");
IRC::add_command_handler("sdlearusers","sdc_clearusers");
IRC::add_command_handler("sduptime","sdc_uptime");
IRC::add_command_handler("sdcustomads","sdc_customads");

# specific to SDClone follow...
IRC::add_command_handler("sdswitchlanguage","sdc_bad");
IRC::add_command_handler("sdshowword","sdc_bad");
IRC::add_command_handler("sdshowskin","sdc_bad");
# specific to SDServer follow...
IRC::add_command_handler("sdircserver","sdc_irc_server");
IRC::add_command_handler("sdgetslots","sdc_getslots");
IRC::add_command_handler("sdgetstats","sdc_getstats");
IRC::add_command_handler("sdgetinfo","sdc_getinfo");
IRC::add_command_handler("sdclient","sd_new_client");
IRC::add_command_handler("sdconnect","sdc_client_init");
IRC::add_message_handler("PRIVMSG","sd_cmsg");
IRC::add_message_handler("NOTICE","sdc_control");

IRC::command("/addbutton SDServer-ON /sdclone on");
IRC::command("/addbutton SDServer-OFF /sdclone off");
IRC::command("/addbutton SDServer-Help /sdhelp");
IRC::command("/addbutton SDServer-Status /sdstatus");
IRC::print("SDServer: Ready.\n");
IRC::print("Now use : /sdconnect to try connect to $ClientNick");
IRC::print("Or      : /sdclient to change your client to control");

sub sdc_notice
{
$line = shift(@_);
IRC::command("/NOTICE $line");
}

sub sdc_client_init
{
$ClientReady="false";
IRC::command("/QUERY $ClientNick");
&sdc_notice($ClientNick." sdclone version");
}

sub sd_new_client
{
my $line = lc(shift(@_));
$ClientNick=$line;
IRC::print("-> New client to control $ClientNick");
&sdc_client_init;
return 1;
}

sub sdc_control
{
my $line=lc(shift(@_));
my @userJ = split "!",$line;
my @userK = split ":",@userJ[0];
my $C_Nick=@userK[1];
@userJ = split "notice",$line;
@userK = split ":",@userJ[1];
@userJ = split " ",@userK[1],3;
my $C_state=@userJ[0];
my $C_Cmd=@userJ[1];
if ($C_state eq "sdclone")
        {
        $ClientReady="true";
        IRC::print($ClientNick." use version ".$C_Cmd);
        }
}

sub sdc_controlsend
{
my $line = shift(@_);
&sd_to_client("sdcontrolsend ".$line);
return 1;
}

sub sdc_clearusers
{
my $line = shift(@_);
&sd_to_client("sdclearusers ".$line);
return 1;
}

sub sdc_send
{
my $line = shift(@_);
&sd_to_client("sdsend ".$line);
return 1;
}

sub sdc_stopsend
{
my $line = shift(@_);
&sd_to_client("sdstopsend ".$line);
return 1;
}

sub sdc_userinfo
{
my $line = shift(@_);
&sd_to_client("sduserinfo ".$line);
return 1;
}

sub sdc_uptime
{
my $line = shift(@_);
&sd_to_client("sduptime ".$line);
return 1;
}

sub sdc_customads
{
my $line = shift(@_);
&sd_to_client("sdcustomads ".$line);
return 1;
}


sub sdc_irc_server
{
my $line = shift(@_);
&sd_to_client("sdircserver ".$line);
return 1;
}

sub sdc_bad
{
IRC::print("SDServer: This command is not use in SDServer");
return 1;
}

sub sdc_getinfo
{
my $line = shift(@_);
&sd_to_client("sdgetinfo ".$line);
return 1;
}

sub sdc_getslots
{
my $line = shift(@_);
&sd_to_client("sdgetslots ".$line);
return 1;
}

sub sdc_getstats
{
my $line = shift(@_);
&sd_to_client("sdgetstats ".$line);
return 1;
}

sub sdc_add_serving_channel
{
my $line = shift(@_);
&sd_to_client("sdstartserve ".$line);
return 1;
}

sub sdc_del_serving_channel
{
my $line = shift(@_);
&sd_to_client("sdstopserve ".$line);
return 1;
}

sub sdc_list
{
my $line = shift(@_);
&sd_to_client("sdlist ".$line);
return 1;
}

sub sdc_que
{
my $line = shift(@_);
&sd_to_client("sdque ".$line);
return 1;
}

sub sdc_killque
{
my $line = shift(@_);
&sd_to_client("sdkillque ".$line);
return 1;
}
sub sdc_load
{
my $line = shift(@_);
&sd_to_client("sdload ".$line);
return 1;
}

sub sdc_set
{
my $line = shift(@_);
&sd_to_client("sdset ".$line);
return 1;
}

sub sdc_help
{
my $line = shift(@_);
&sd_to_client("sdhelp ".$line);
return 1;
}
sub sdc_friend
{
my $line = shift(@_);
&sd_to_client("sdfriend ".$line);
return 1;
}

sub sdc_save
{
my $line = shift(@_);
&sd_to_client("sdsave ".$line);
return 1;
}

sub sdc_status
{
my $line = shift(@_);
&sd_to_client("sdstatus ".$line);
return 1;
}

sub sdc_pub
{
my $line = shift(@_);
&sd_to_client("sdpub ".$line);
return 1;
}

sub sdc_sdclone
{
my $line = shift(@_);
&sd_to_client("sdclone ".$line);
return 1;
}

sub sdc_ban
{
my $line = shift(@_);
&sd_to_client("sdban ".$line);
return 1;
}

sub sdc_unban
{
my $line = shift(@_);
&sd_to_client("sdunban ".$line);
return 1;
}

sub sdc_banlist
{
my $line = shift(@_);
&sd_to_client("sdbanlist ".$line);
return 1;
}

sub sdc_join
{
my $line = shift(@_);
&sd_to_client("sdjoin ".$line);
return 1;
}

sub sdc_leave
{
my $line = shift(@_);
&sd_to_client("sdleave ".$line);
return 1;
}

sub sdc_say
{
my $line = shift(@_);
&sd_to_client("sdsay ".$line);
return 1;
}

sub sdc_saychannel
{
my $line = shift(@_);
&sd_to_client("sdsaychannel ".$line);
return 1;
}

sub sd_to_client
{
my $line=shift(@_);
if ($ClientReady eq "true")     { sdc_notice("$ClientNick $ClientPwd $line"); }
        else            { IRC::print("$ClientNick not ready ! Use /sdconnect to enable it"); }
}

sub sd_cmsg
{
        my $line = shift;
        $line =~ /:.*?\s*([^!]*)!.*? +PRIVMSG +(.*?)\s*:(.*)/;
        my $nick = $1;
        my @ctcp = split " ",lc($line);     # version reply
        if (lc(@ctcp[3]) eq ":\001version\001") { sdc_notice($nick." \001VERSION SDServer $Ver for Linux by dccserv\001"); }
        return 0;
}

sub sdc_shutdown
# call on shutdown
{
IRC::command("/delbutton SDServer-ON");
IRC::command("/delbutton SDServer-OFF");
IRC::command("/delbutton SDServer-Help");
IRC::command("/delbutton SDServer-Status");
}
