Now I connected my script to my mysql database, so the script is now lesser understandable, but that is just a plus :)
If someone finds something they made and wants credit for it, please tell me.
Remember that this scrit is not finish yet, som someting is not working proerly yet, and comments, what is that?
#!/usr/bin/perl
#-----------------------------------------------------------------
# Open DC Hub Client Control
# v0.2.8 06 feb 2003
#
# Copyright 2003 AzaToth
#
# The orgin of the split_description() section
# is from Davidator/thirteentwenty
#
#
# Require DBI::MySql
#
# Install the script in ~/.opendchub/scripts
#-----------------------------------------------------------------
#
# ip and prefix control. ip-range an prefix are stored in the database
my $sth = $dbh->prepare("SELECT * FROM ip_range");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref()) {
$local = $ref->{'prefix'} if ((&quad2int($host) > &quad2int($ref->{'low_ip'})) and (&quad2int($host) < &quad2int($ref->{'high_ip'})));
}
odch::data_to_all("<$botname> You shall use prefix: $local|") if $VERBOSE;
$sth->finish();
# If wrong IP, then OUT!
if($local =~ /0/) {
my $sth = $dbh->prepare("SELECT mess FROM messages WHERE type='wrong_ip'");
$sth->execute();
my $text = $sth->fetchrow_hashref();
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> \ $text->{'mess'}|");
$sth->finish();
odch::kick_user($user);
last;
}
else {
my(@prefix) = split(/\]/, $user);
if(lc("@prefix[0]\]") eq "$local") {
#last if (&check_program(@_) == 0); #If wrong client found he's dead now
#&validate_user(@_);
&set_time();
my $name = $dbh->quote($user);
my $ip = odch::get_ip($user);
$dbh->do("REPLACE INTO users VALUES ($name, '$ip', 1)");
# Now he/she officially joined the hub
open(LOG, ">>$PATH/logs/$date.log");
print LOG "[$time] *** $user has joined #Lu-Hubben\n";
close LOG;
}
# And some other checks, now that the prefix is OK
elsif("@prefix[0]" eq "$user") {
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> please add $local before your login name.|");
odch::kick_user($user);
}
else {
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> You are using @prefix[0]\] as prefix, but, you shoud use $local instead!|");
odch::kick_user($user);
}
}
}
sub user_disconnected() {
my($user, $data) = @_;
my $name = $dbh->quote($user);
my $ip = odch::get_ip($user);
$dbh->do("REPLACE INTO users VALUES ($name, '$ip', 0)");
}
sub data_arrival() {
my($user, $data) = @_;
unless($data =~ /\$To:|(\$(Get|My))INFO|\$Key|\$ValidateNick|\$MyPass|\$Version|\$GetNickList|\$ConnectToMe|\$Search|\$Kick/) {
&set_time();
&commands_to_user($user) if $data =~ /\+commands/;
&private_to_user($user, 'rules') if $data =~ /\+rules/;
&private_to_user($user, 'help') if $data =~ /\+help/;
&private_to_user($user, 'hubinfo') if $data =~ /\+hubinfo/;
&private_to_user($user, 'myip') if $data =~ /\+myip/;
my $sth = $dbh->prepare("SELECT * FROM triggers");
$sth->execute();
if ($dcClient eq @CLIENTS[0]) {
#odch::data_to_all("<$botname> using Dcgui klient ($dcClient) and version $dcVersion|");
}
elsif ($dcClient eq @CLIENTS[1]) {
#odch::data_to_all("<$botname> using DC++ klient ($dcClient) and version $dcVersion|");
}
elsif ($dcClient eq @CLIENTS[2]) {
#odch::data_to_all("<$botname> using LuDC++ klient ($dcClient) and version $dcVersion|");
}
else {
#odch::data_to_all("<$botname> using unallowed client ($dcClient) and version $dcVersion|");
}
}
# Date and time in different formats
sub set_time() {
use POSIX qw(strftime);
$full_date = localtime;
$time = strftime "%H:%M", localtime;
$long_time = strftime "%H:%M:%S", localtime;
$date = strftime "%Y-%m-%d", localtime;
}
sub hub_timer() {
odch::data_to_all("<$botname> Hell AzaToth, the king. Long live the king!|");
}
sub int2quad() {
return join('.',unpack('C4', pack("N", $_[0])));
}
sub quad2int() {
return unpack ("N", pack("C4", split(/\./, $_[0])));
}
sub db_message() {
my ($user, $data) = @_;
my $mess = $dbh->quote($data);
my $sth = $dbh->prepare("SELECT mess FROM messages WHERE type=$mess");
$sth->execute();
my $ref = $sth->fetchrow_hashref();
$sth->finish();
my $temp = $ref->{'mess'};
$temp =~ s/(\$\w+)/$1/eeg;
return $temp
}
__END___
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Now I connected my script to my mysql database, so the script is now lesser understandable, but that is just a plus :)
If someone finds something they made and wants credit for it, please tell me.
Remember that this scrit is not finish yet, som someting is not working proerly yet, and comments, what is that?
http://medlem.spray.se/brutalis/odchbot.html
___BEGIN___
#!/usr/bin/perl
#-----------------------------------------------------------------
# Open DC Hub Client Control
# v0.2.8 06 feb 2003
#
# Copyright 2003 AzaToth
#
# The orgin of the split_description() section
# is from Davidator/thirteentwenty
#
#
# Require DBI::MySql
#
# Install the script in ~/.opendchub/scripts
#-----------------------------------------------------------------
#
use DBI;
$dbh = DBI->connect("DBI:mysql:odch","azatoth",'dapass',{ RaiseError => 1, AutoCommit => 0 });
$dbh->do("SET OPTION SQL_BIG_TABLES = 1");
$VERBOSE = 0;
$PATH = "/home/azatoth/.opendchub";
$botname = "AzaBoth";
$WRONG_IP_EXCEPTION = "You are using an ip out of range";
@CLIENTS = ("DCGUI", "++", "++LuDC");
$DCGUI = "0.2.3";
$PLUSPLUS = "0.22";
$PLUSPLUSLUDC = "0.2220";
sub main(){
odch::register_script_name($botname);
}
sub new_user_connected() {
my($user, $data) = @_;
my($host) = odch::get_hostname($user);
my($local) = 0;
my(@tmphost) = split(/\./, $host);
# ip and prefix control. ip-range an prefix are stored in the database
my $sth = $dbh->prepare("SELECT * FROM ip_range");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref()) {
$local = $ref->{'prefix'} if ((&quad2int($host) > &quad2int($ref->{'low_ip'})) and (&quad2int($host) < &quad2int($ref->{'high_ip'})));
}
odch::data_to_all("<$botname> You shall use prefix: $local|") if $VERBOSE;
$sth->finish();
# If wrong IP, then OUT!
if($local =~ /0/) {
my $sth = $dbh->prepare("SELECT mess FROM messages WHERE type='wrong_ip'");
$sth->execute();
my $text = $sth->fetchrow_hashref();
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> \ $text->{'mess'}|");
$sth->finish();
odch::kick_user($user);
last;
}
else {
my(@prefix) = split(/\]/, $user);
if(lc("@prefix[0]\]") eq "$local") {
#last if (&check_program(@_) == 0); #If wrong client found he's dead now
#&validate_user(@_);
&set_time();
my $name = $dbh->quote($user);
my $ip = odch::get_ip($user);
$dbh->do("REPLACE INTO users VALUES ($name, '$ip', 1)");
# Now he/she officially joined the hub
open(LOG, ">>$PATH/logs/$date.log");
print LOG "[$time] *** $user has joined #Lu-Hubben\n";
close LOG;
}
# And some other checks, now that the prefix is OK
elsif("@prefix[0]" eq "$user") {
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> please add $local before your login name.|");
odch::kick_user($user);
}
else {
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> You are using @prefix[0]\] as prefix, but, you shoud use $local instead!|");
odch::kick_user($user);
}
}
}
sub user_disconnected() {
my($user, $data) = @_;
my $name = $dbh->quote($user);
my $ip = odch::get_ip($user);
$dbh->do("REPLACE INTO users VALUES ($name, '$ip', 0)");
}
sub data_arrival() {
my($user, $data) = @_;
unless($data =~ /\$To:|(\$(Get|My))INFO|\$Key|\$ValidateNick|\$MyPass|\$Version|\$GetNickList|\$ConnectToMe|\$Search|\$Kick/) {
&set_time();
&commands_to_user($user) if $data =~ /\+commands/;
&private_to_user($user, 'rules') if $data =~ /\+rules/;
&private_to_user($user, 'help') if $data =~ /\+help/;
&private_to_user($user, 'hubinfo') if $data =~ /\+hubinfo/;
&private_to_user($user, 'myip') if $data =~ /\+myip/;
my $sth = $dbh->prepare("SELECT * FROM triggers");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref()) {
$trigger = $ref->{'text'};
$trigger =~ s/(\$\w+)/$1/eeg;
odch::data_to_all("<$botname> $trigger|") if ($data =~ /$ref->{'trigger'}/);
}
if($data =~/\+mysql/) {
my $sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref()) {
odch::data_to_all("<$botname> \nName: $ref->{'user'}\nIP: $ref->{'ip'}\nOnline: $ref->{'online'}|");
}
}
$sth->finish();
&check_program(@_);
# log creator
open(LOG, ">>$PATH/logs/$date.log");
chop($data);
print LOG "Session start: $full_date \n[$time] *** Now talking in the #Lu-Hubben\n" if -z "$PATH/logs/$date.log";
print LOG "[$time] $data \n";
close LOG;
}
}
sub private_to_user() {
my($user, $data) = @_;
my $text = &db_message($user, $data);
odch::data_to_user($user, "<$botname (private)> \n $text|");
}
sub commands_to_user() {
my($user) = @_;
my($type) = odch::get_type($user);
my(@commands) = ('','','');
my $sth = $dbh->prepare("SELECT mess FROM messages WHERE type=?");
if($type eq 32) {
$sth->execute('admin_commands');
$commands[0] = ($sth->fetchrow_hashref())->{'mess'} . "\n";
}
if($type eq 32 or $type eq 16) {
$sth->execute('op_commands');
$commands[1] = ($sth->fetchrow_hashref())->{'mess'} . "\n";
}
if($type eq 32 or $type eq 16 or $type eq 8 or $type eq 4 or $type eq 2 or $type eq 1) {
$sth->execute('user_commands');
$commands[2] = ($sth->fetchrow_hashref())->{'mess'};
}
$sth->finish();
odch::data_to_user($user, "\$To: $user From: $botname \$<$botname> \n $commands[0] $commands[1] $commands[2] |");
}
# Splitting the description tag
sub split_description() {
my($user) = @_;
my($tmpdata) = odch::get_description($user);
my($pos1) = rindex($tmpdata, "<") +1;
my($pos2) = rindex($tmpdata, ">");
$verdata = substr($tmpdata, $pos1, $pos2 - $pos1);
my(@verdata2) = split(/\ /, $verdata);
odch::data_to_all("<$botname> $verdata @verdata2[0]|") if $VERBOSE;
my($pos1) = rindex($tmpdata, "<@verdata2[0]") + 1 + length(@verdata2[0]);
$tmpdata = substr($tmpdata, $pos1, $pos2 - $pos1);
my(@tmpdata2) = split(/,/, $tmpdata);
@tmp0 = split(/:/, @tmpdata2[0]);
@tmp1 = split(/:/, @tmpdata2[1]);
@tmp2 = split(/:/, @tmpdata2[2]);
@tmp3 = split(/:/, @tmpdata2[3]);
@tmp4 = split(/:/, @tmpdata2[4]);
$dcClient = @verdata2[0];
$dcVersion = @tmp0[1];
$ModeAP = @tmp1[1];
$NbHubs = @tmp2[1];
$NSlots = @tmp3[1];
$UploadLimit = @tmp4[1];
}
# Client controll
sub check_program() {
&split_description(@_);
if ($dcClient eq @CLIENTS[0]) {
#odch::data_to_all("<$botname> using Dcgui klient ($dcClient) and version $dcVersion|");
}
elsif ($dcClient eq @CLIENTS[1]) {
#odch::data_to_all("<$botname> using DC++ klient ($dcClient) and version $dcVersion|");
}
elsif ($dcClient eq @CLIENTS[2]) {
#odch::data_to_all("<$botname> using LuDC++ klient ($dcClient) and version $dcVersion|");
}
else {
#odch::data_to_all("<$botname> using unallowed client ($dcClient) and version $dcVersion|");
}
}
# Date and time in different formats
sub set_time() {
use POSIX qw(strftime);
$full_date = localtime;
$time = strftime "%H:%M", localtime;
$long_time = strftime "%H:%M:%S", localtime;
$date = strftime "%Y-%m-%d", localtime;
}
sub hub_timer() {
odch::data_to_all("<$botname> Hell AzaToth, the king. Long live the king!|");
}
sub int2quad() {
return join('.',unpack('C4', pack("N", $_[0])));
}
sub quad2int() {
return unpack ("N", pack("C4", split(/\./, $_[0])));
}
sub db_message() {
my ($user, $data) = @_;
my $mess = $dbh->quote($data);
my $sth = $dbh->prepare("SELECT mess FROM messages WHERE type=$mess");
$sth->execute();
my $ref = $sth->fetchrow_hashref();
$sth->finish();
my $temp = $ref->{'mess'};
$temp =~ s/(\$\w+)/$1/eeg;
return $temp
}
__END___
@tmp0 = split(/:/, @tmpdata2[0]);
@tmp1 = split(/:/, @tmpdata2[1]);
@tmp2 = split(/:/, @tmpdata2[2]);
@tmp3 = split(/:/, @tmpdata2[3]);
@tmp4 = split(/:/, @tmpdata2[4]);
[...]
$dcVersion = @tmp0[1];
$ModeAP = @tmp1[1];
$NbHubs = @tmp2[1];
$NSlots = @tmp3[1];
$UploadLimit = @tmp4[1];
Could be replaced by:
$dcVersion = split(/:/, @tmpdata2[0]);
$ModeAP = split(/:/, @tmpdata2[1]);
$NbHubs = split(/:/, @tmpdata2[2]);
$NSlots = split(/:/, @tmpdata2[3]);
$UploadLimit = split(/:/, @tmpdata2[4]);
Everyone remembers that when you assign a list to a scalar it discards every value except the last one?
And, since we have separate namespaces, the sane thing to do would be to rename @tmpdata2 to @tmpdata (since the other one is a scalar).
Correct me if I'm wrong.
I had to do it like this:
$dcVersion = (split(/:/, $tmpdata[0]))[1];
$ModeAP = (split(/:/, $tmpdata[1]))[1];
$NbHubs = (split(/:/, $tmpdata[2]))[1];
$NSlots = (split(/:/, $tmpdata[3]))[1];
$UploadLimit = (split(/:/, $tmpdata[4]))[1];