Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # Version 3! ivanatora@gmail.com
- use IO::Socket::INET;
- use IO::Select;
- use IO::Handle;
- use POSIX;
- use DBI;
- #$dbuser = "hostserv";
- #$dbpass = "parola";
- #$db = "seen";
- #$dbhost = "localhost";
- #$dbport = "3306";
- #$dsn = "DBI:mysql:database=$db;host=$dbhost;port=$dbport";
- #$dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "$!";
- $dbh = DBI->connect( "dbi:SQLite:data.dbl" ) || die "Cannot connect: $DBI::errstr";
- my $ts = time;
- my $logfile = "/home/ivanatora/eenserv.log";
- rename $logfile,"$logfile$ts";
- my $host = "127.0.0.1";
- #my $host = "www.sou7.org";
- my $port = "6667";
- my $s_ime = "SeenServ.BgChat";
- my $parola = "sv";
- my $mynick = "SeenServ";
- #my $conn = IO::Socket::INET->new("$host:$port");
- my $conn = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Proto => 'tcp');
- @channs = qw/#pleven #perl #help/;
- die "Cant make conn" unless $conn;
- $conn->autoflush(1);
- $| = 1;
- #oops
- $sth = $dbh->prepare("SELECT id FROM seen WHERE when_off='6131735909'");
- $sth->execute();
- $ts = time;
- while (@arr = $sth->fetchrow_array){
- $id = "@arr";
- $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='SeenServ restart' WHERE id='$id'");
- }
- sub fix($){
- my $str = $_[0];
- $str =~ s/[^~a-zA-Z0-9^_\|\-\\`\.\[\]\{\}]//g;
- $str =~ s/\\/\\\\/g;
- chomp $str;
- return $str;
- }
- sub fixm($){
- my $str = "@_";
- $str =~ s/[^a-zA-Z0-9\@\!\\|*^_\-`\.\[\]\{\}]//g;
- chomp $str;
- return $str;
- }
- sub fixc($){
- my $str = "@_";
- $str =~ s/[^a-zA-Z0-9\#\@^_\-`&\.\[\]\{\}\+]//g;
- chomp $str;
- return $str;
- }
- sub fixq($){
- my $str = "@_";
- $str =~ s/[^a-zA-Z0-9 ;:\#^_`\.\[\]\{\}\+]//g;
- chomp $str;
- return $str;
- }
- sub dbg($){
- my $str = "@_";
- # print $conn ":$mynick NOTICE ivanatora :dbg: $str\n";
- }
- sub lst($){
- my $lst = "@_";
- my $now = time;
- my $difference = $now - $lst;
- $seconds = $difference % 60;
- $difference = ($difference - $seconds) / 60;
- $minutes = $difference % 60;
- $difference = ($difference - $minutes) / 60;
- $hours = $difference % 24;
- $difference = ($difference - $hours) / 24;
- $days = $difference % 7;
- $weeks = ($difference - $days) / 7;
- my $str = "";
- if ($weeks > 0) { $str .= "$weeks weeks " }
- if ($days > 0) { $str .= "$days days " }
- # if ($hours > 0) { $str .= "$hours hours " }
- # if ($minutes > 0) { $str .= "$minutes min " }
- $str .= "$hours:$minutes:$seconds";
- return "$str";
- }
- print $conn "PASS $parola :TS\n";
- print $conn "SERVER $s_ime 1 :SeenServ\n";
- print $conn "NICK $mynick 1 $ts +i SeenServ BgChat.SeenServ $s_ime :/msg $mynick help\n";
- $sth = $dbh->prepare("SELECT chan FROM new_chans");
- $sth->execute();
- while ($blaa = $sth->fetchrow_array()){
- push (@channs, $blaa);
- }
- foreach $chana (@channs){
- print $conn ":$s_ime SJOIN $ts $chana + :$mynick\n";
- print $conn ":$s_ime MODE $chana +v $mynick\n";
- }
- $sth = $dbh->prepare("SELECT chan FROM chans WHERE status NOT LIKE 'part'
- AND status NOT LIKE 'leave'");
- $sth->execute();
- while ($arr = $sth->fetchrow_array){
- print $conn ":$s_ime SJOIN $ts $arr + :$mynick\n";
- push (@chans, $arr);
- }
- my $sel = IO::Select->new();
- $sel->add($conn);
- while (1){
- #qaz
- $ts = time;
- my @hands =$sel->can_read(0.5);
- if (@hands){
- foreach my $s (@hands){
- $_ = <$s>;
- #MAIN LOOP HERE, BEWARE MORTALS :P
- if ($_ !~ /PRIVMSG \#/i){
- open (LOG,">> $logfile") or die "$!";
- print LOG "$_";
- close LOG;
- }
- if (/PING :(.*)/){
- my $srv = fix($1);
- print $conn "PONG $srv\n";
- print "GOT PIG !!! 1 = $srv\n";
- }
- if (/:(.+?) PRIVMSG $mynick/i){
- my $sender = fix($1);
- my $line = $_;
- @words = split (/ /,$line);
- if ($words[3] =~ /VERSION/){
- print "got version\n";
- print $conn ":$mynick NOTICE $sender :\001VERSION SeenServ za BgChat! Versiq 3, nova i blestqshta!:)\001\n";
- }
- elsif ($words[3] =~ /PING/){
- print "got pig\n";
- $pingrep = fix($words[4]);
- print $conn ":$mynick NOTICE $sender :\001PING $pingrep\001\n";
- }
- elsif ($words[3] =~ /help/i){
- # print $conn ":$mynick PRIVMSG $sender :/msg $mynick join #channel <= makes $mynick join the specified channel.\n";
- print $conn ":$mynick PRIVMSG $sender :/msg $mynick seen nick OR ident\@host <= wildcards (*) available.\n";
- print $conn ":$mynick PRIVMSG $sender :Channel seen available, too.\n";
- print $conn ":$mynick PRIVMSG $sender :If you want to see last matches, append d<number> to your query\n";
- print $conn ":$mynick PRIVMSG $sender :For example, to see last 5 matches: seen ivo d5\n";
- }
- } #END na proverkite za CTCP
- #:ivanatora PRIVMSG #perl :join #alabala
- if (/:CS PRIVMSG $mynick :!ssctl (.+?) (.+?)$/i){
- $ts = time;
- $status = fix($1);
- $chan = fixc($2);
- $sth = $dbh->prepare("SELECT id FROM chans WHERE chan='$chan'");
- $sth->execute();
- $id = $sth->fetchrow_array();
- if ($id eq ''){
- $dbh->do("INSERT INTO chans (chan,status,ts) VALUES
- ('$chan','$status','$ts')");
- }
- else {
- $dbh->do("UPDATE chans SET status='$status' WHERE id='$id'");
- }
- if ($status eq 'join'){
- print $conn ":$s_ime SJOIN $ts $chan + :$mynick\n";
- }
- elsif (($status eq 'part') || ($status eq 'leave')){
- print $conn ":$mynick PART $chan\n";
- }
- } #END of join cmds
- if (/^NICK/){
- @words = split(/ /,$_);
- my $nick = fix($words[1]);
- my $ident = fix($words[5]);
- my $host = fixm($words[6]);
- $ts = time;
- # dbg("$nick $ident $host $ts");
- if ($ident !~ /^AT/){
- $sth = $dbh->prepare("INSERT INTO seen (nick, user, host, when_on) VALUES
- ('$nick', '$ident', '$host', '$ts')");
- $sth->execute();
- }
- } #new nick introduced
- if (/^:(.+?) NICK (.+?) :.*/ig){
- $ts = time;
- $old = fix($1);
- $new = fix($2);
- #spirame stariq record
- # print "Nickchange: $old -> $new\n";
- $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$old' AND when_off='6131735909'");
- $sth->execute();
- while (@ids = $sth->fetchrow_array){
- $id = "@ids";
- # print "$id\n";
- }
- $dbh->do("UPDATE seen SET when_off='$ts',newnick='$new' WHERE id='$id'");
- #i pushtame noviq, kopirame dannite ot stariq
- $sth = $dbh->prepare("SELECT user,host FROM seen WHERE id='$id'");
- $sth->execute();
- while (@arr = $sth->fetchrow_array){
- $str = "@arr";
- ($chuser,$chhost) = split(/ /,$str);
- print "$chuser : $chhost\n";
- }
- $dbh->do("INSERT INTO seen (nick, user, host, when_on) VALUES ('$new','$chuser','$chhost','$ts')");
- #:ivanatora NICK maudib :1123345694
- } #end of nick change
- #QUITs
- #killed
- if (/:(.+?) KILL (.+?) :.+? \((.+?)\)/ig){
- # dbg("kill");
- $killer = fix($1);
- $nick = fix($2);
- $msg = $3;
- $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$nick' AND when_off='6131735909'");
- $sth->execute();
- @arr = $sth->fetchrow_array;
- $id = "@arr";
- $ts = time;
- $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='Killed by $killer: $msg' WHERE id='$id'");
- }#eok
- #quited
- if (/:(.+?) QUIT :(.+?)$/ig){
- $old = fix($1);
- $msg = fixq($2);
- # print "$old ..=> $msg\n";
- $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$old' AND when_off='6131735909'");
- $sth->execute();
- @arr = $sth->fetchrow_array;
- $id = "@arr";
- $ts = time;
- $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='$msg' WHERE id='$id'");
- }
- #squit comes here,oops, there
- if (/:(.+?) PRIVMSG $mynick :\!?seen (.+?)\s/ig){
- #close or die
- my $who = fix($1);
- my $mask = fixm($2);
- @ws = split (/ /,$_);
- if ($ws[5] =~ /d\d/){
- $ws[5] = fix($ws[5]);
- $ws[5] =~ s/[^\d]//g;
- if ($ws[5] > 5) { $limit = 5; }
- else { $limit = $ws[5]; }
- }
- else { $limit = 1; }
- print "limit = $limit ws[5] = $ws[5]\n";
- sn($who,$mask,undef,$limit);
- }
- if (/:(.+?) PRIVMSG (.+?) :\!?seen (.+?)\s/ig){
- my $who = fix($1);
- my $mask = fixm($3);
- my $chan = fixc($2);
- # @ws = split (/ /,$_);
- # if ($ws[5] =~ /d\d/){
- # $ws[5] = fix($ws[5]);
- # $ws[5] =~ s/d//;
- # if ($ws[5] > 5) { $limit = 5; }
- # else { $limit = $ws[5]; }
- # }
- # else { $limit = 1; }
- # print "limit = $limit ws[5] = $ws[5]\n";
- sn($who,$mask,$chan,1);
- }
- sub sn($$$$){
- my ($who,$mask,$chan,$limit) = ($_[0],$_[1],$_[2],$_[3]);
- if ($chan){
- $chan .= " ";
- }
- if ($chan) {
- $sth = $dbh->prepare("SELECT status FROM chans WHERE chan='$chan'");
- $sth->execute();
- @st = $sth->fetchrow_array;
- $status = "@st";
- if ($status ne 'answerprivate'){
- $public = 1; }
- else { $public = 0; }
- }
- else {
- $public = 0;
- }
- @maskw = split(/ /,$mask);
- $first = $maskw[0];
- $first =~ tr/*/%/;
- print "first- $first ; chan - $chan\n";
- if ($first =~ /\@/){
- ($user,$host) = split(/\@/,$first);
- print "seen: $user $host\n";
- $sth = $dbh->prepare("SELECT nick,user,host,when_off,newnick,quit_msg FROM seen
- WHERE user LIKE '$user' AND host LIKE '$host' ORDER by when_off DESC LIMIT $limit");
- $sth->execute();
- while (@arr = $sth->fetchrow_array){
- #A BIIG WHILE, INDEED
- ($cur,$user,$host,$when,$newnick,$quitmsg) = ($arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5]);
- # dbg(".$when.");
- dbg(".$cur.$user.$host.$when.$newnick.$quitmsg.");
- print "who1 = $who\n";
- if (($when eq '6131735909') & ($cur ne '')){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :$cur ($user\@"."$host) is currently ONLINE!\n";
- }
- elsif ($cur eq ''){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :Sorry, I haven't seen $mask recently.\n";
- }
- elsif ($when ne '6131735909'){
- $dff = lst($when);
- chomp $dff;
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :$cur ($user\@"."$host) was last on IRC $dff ago";
- if ($newnick ne ''){
- print $conn " user changed nick to $newnick\n";
- }
- elsif ($quitmsg ne ''){
- print $conn ", user quit: ($quitmsg)\n";
- }
- else {
- print $conn ".\n";
- }
- }
- $spoken = 1;
- } #end of WHILE @arr
- if ($spoken == 0){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :Sorry, I haven't seen $mask recently.\n";
- }
- $spoken = 0;
- }
- else {
- print "Seen nick: $first\n";
- $sth = $dbh->prepare("SELECT nick,user,host,when_off,newnick,quit_msg FROM seen
- WHERE nick LIKE '$first' ORDER by when_off DESC LIMIT $limit");
- $sth->execute();
- while (@arr = $sth->fetchrow_array){
- # ANOTHER BIG WHALE
- ($cur,$user,$host,$when,$newnick,$quitmsg) = ($arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5]);
- dbg(".$cur.$user.$host.$when.$newnick.$quitmsg.");
- print "who2 = $who\n";
- if (($when eq '6131735909') & ($cur ne '')){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :$cur ($user\@"."$host) is currently ONLINE!\n";
- }
- elsif ($cur eq ''){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :Sorry, I haven't seen $mask recently.\n";
- }
- elsif ($when ne '6131735909'){
- $dff = lst($when);
- chomp $dff;
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :$cur ($user\@"."$host) was last on IRC $dff ago";
- if ($newnick ne ''){
- print $conn ", user changed nick to $newnick\n";
- }
- elsif ($quitmsg ne ''){
- print $conn " , quit: ($quitmsg)\n";
- }
- else {
- print $conn ".\n";
- }
- }
- $spoken = 1;
- } #end of big whale
- if ($spoken == 0){
- print $conn ":$mynick PRIVMSG ";
- if ($public == 1) { print $conn "$chan:";}
- print $conn "$who :Sorry, I haven't seen $mask recently.\n";
- }
- $spoken = 0;
- }
- }
- #} #END OF MAIN LOOP ako twa e while $sock
- } #end of FOREACH!@!@!@!!
- } #end of while 1 ?
- }
Add Comment
Please, Sign In to add comment