ivanatora

Untitled

Nov 14th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 12.11 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # Version 3! ivanatora@gmail.com
  3. use IO::Socket::INET;
  4. use IO::Select;
  5. use IO::Handle;
  6. use POSIX;
  7. use DBI;
  8.  
  9. #$dbuser     = "hostserv";
  10. #$dbpass = "parola";
  11. #$db = "seen";
  12. #$dbhost = "localhost";
  13. #$dbport     = "3306";
  14. #$dsn      = "DBI:mysql:database=$db;host=$dbhost;port=$dbport";
  15.  
  16. #$dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "$!";
  17. $dbh = DBI->connect( "dbi:SQLite:data.dbl" ) || die "Cannot connect: $DBI::errstr";
  18.  
  19. my $ts = time;
  20. my $logfile = "/home/ivanatora/eenserv.log";
  21. rename $logfile,"$logfile$ts";
  22. my $host = "127.0.0.1";
  23. #my $host  = "www.sou7.org";
  24. my $port = "6667";
  25. my $s_ime = "SeenServ.BgChat";
  26. my $parola = "sv";
  27. my $mynick = "SeenServ";
  28. #my $conn = IO::Socket::INET->new("$host:$port");
  29. my $conn = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Proto => 'tcp');
  30. @channs = qw/#pleven #perl #help/;
  31. die "Cant make conn" unless $conn;
  32. $conn->autoflush(1);
  33. $| = 1;
  34. #oops
  35. $sth = $dbh->prepare("SELECT id FROM seen WHERE when_off='6131735909'");
  36. $sth->execute();
  37. $ts = time;
  38. while (@arr = $sth->fetchrow_array){
  39.     $id = "@arr";
  40.     $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='SeenServ restart' WHERE id='$id'");
  41. }
  42.  
  43. sub fix($){
  44.     my $str = $_[0];
  45.     $str =~ s/[^~a-zA-Z0-9^_\|\-\\`\.\[\]\{\}]//g;
  46.     $str =~ s/\\/\\\\/g;
  47.     chomp $str;
  48.     return $str;
  49. }
  50. sub fixm($){
  51.     my $str = "@_";
  52.     $str =~ s/[^a-zA-Z0-9\@\!\\|*^_\-`\.\[\]\{\}]//g;
  53.         chomp $str;
  54.         return $str;
  55. }
  56. sub fixc($){
  57.         my $str = "@_";
  58.     $str =~ s/[^a-zA-Z0-9\#\@^_\-`&\.\[\]\{\}\+]//g;
  59.     chomp $str;
  60.     return $str;
  61. }
  62. sub fixq($){
  63.         my $str = "@_";
  64.         $str =~ s/[^a-zA-Z0-9 ;:\#^_`\.\[\]\{\}\+]//g;
  65.         chomp $str;
  66.         return $str;  
  67. }
  68.  
  69. sub dbg($){
  70.     my $str = "@_";
  71. #   print $conn ":$mynick NOTICE ivanatora :dbg: $str\n";
  72. }
  73. sub lst($){
  74.     my $lst = "@_";
  75.     my $now = time;
  76.     my $difference = $now - $lst;
  77.     $seconds    =  $difference % 60;
  78.     $difference = ($difference - $seconds) / 60;
  79.     $minutes    =  $difference % 60;
  80.     $difference = ($difference - $minutes) / 60;
  81.     $hours      =  $difference % 24;
  82.     $difference = ($difference - $hours)   / 24;
  83.     $days       =  $difference % 7;
  84.     $weeks      = ($difference - $days)    /  7;
  85.     my $str = "";
  86.     if ($weeks > 0) { $str .= "$weeks weeks " }
  87.     if ($days > 0) { $str .= "$days days " }
  88. #   if ($hours > 0) { $str .= "$hours hours " }
  89. #   if ($minutes > 0) { $str .= "$minutes min " }
  90.     $str .= "$hours:$minutes:$seconds";
  91.     return "$str";
  92. }
  93.  
  94. print $conn "PASS $parola :TS\n";
  95. print $conn "SERVER $s_ime 1 :SeenServ\n";
  96. print $conn "NICK $mynick 1 $ts +i SeenServ BgChat.SeenServ $s_ime :/msg $mynick help\n";
  97. $sth = $dbh->prepare("SELECT chan FROM new_chans");
  98. $sth->execute();
  99. while ($blaa = $sth->fetchrow_array()){
  100.     push (@channs, $blaa);
  101. }
  102. foreach $chana (@channs){
  103.         print $conn ":$s_ime SJOIN $ts $chana + :$mynick\n";
  104.         print $conn ":$s_ime MODE $chana +v $mynick\n";
  105. }
  106. $sth = $dbh->prepare("SELECT chan FROM chans WHERE status NOT LIKE 'part'
  107. AND status NOT LIKE 'leave'");
  108. $sth->execute();
  109. while ($arr = $sth->fetchrow_array){
  110. print $conn ":$s_ime SJOIN $ts $arr + :$mynick\n";
  111. push (@chans, $arr);
  112. }
  113. my $sel = IO::Select->new();
  114. $sel->add($conn);
  115. while (1){
  116. #qaz
  117. $ts = time;
  118. my @hands =$sel->can_read(0.5);
  119. if (@hands){
  120. foreach my $s (@hands){
  121.         $_ = <$s>;
  122.  
  123. #MAIN LOOP HERE, BEWARE MORTALS :P
  124. if ($_ !~ /PRIVMSG \#/i){
  125.     open (LOG,">> $logfile") or die "$!";
  126.     print LOG "$_";
  127.     close LOG;
  128.     }
  129.  
  130. if (/PING :(.*)/){
  131.     my $srv = fix($1);
  132.         print $conn "PONG $srv\n";
  133.     print "GOT PIG !!! 1 = $srv\n";
  134. }
  135. if (/:(.+?) PRIVMSG $mynick/i){
  136.     my $sender = fix($1);
  137.     my $line = $_;
  138.     @words = split (/ /,$line);
  139.     if ($words[3] =~ /VERSION/){
  140.     print "got version\n";
  141.     print $conn ":$mynick NOTICE $sender :\001VERSION SeenServ za BgChat! Versiq 3, nova i blestqshta!:)\001\n";
  142.     }
  143.     elsif ($words[3] =~ /PING/){
  144.     print "got pig\n";
  145.     $pingrep = fix($words[4]);
  146.     print $conn ":$mynick NOTICE $sender :\001PING $pingrep\001\n";
  147.     }
  148.     elsif ($words[3] =~ /help/i){
  149. #   print $conn ":$mynick PRIVMSG $sender :/msg $mynick join #channel <= makes $mynick join the specified channel.\n";
  150.     print $conn ":$mynick PRIVMSG $sender :/msg $mynick seen nick OR ident\@host <= wildcards (*) available.\n";
  151.     print $conn ":$mynick PRIVMSG $sender :Channel seen available, too.\n";
  152.     print $conn ":$mynick PRIVMSG $sender :If you want to see last matches, append d<number> to your query\n";
  153.     print $conn ":$mynick PRIVMSG $sender :For example, to see last 5 matches: seen ivo d5\n";
  154.     }
  155. } #END na proverkite za CTCP
  156.  
  157. #:ivanatora PRIVMSG #perl :join #alabala
  158. if (/:CS PRIVMSG $mynick :!ssctl (.+?) (.+?)$/i){
  159.         $ts = time;
  160.         $status = fix($1);
  161.         $chan = fixc($2);
  162.         $sth = $dbh->prepare("SELECT id FROM chans WHERE chan='$chan'");
  163.         $sth->execute();
  164.         $id = $sth->fetchrow_array();
  165.         if ($id eq ''){
  166.                 $dbh->do("INSERT INTO chans (chan,status,ts) VALUES
  167. ('$chan','$status','$ts')");
  168.         }
  169.         else {
  170.                 $dbh->do("UPDATE chans SET status='$status' WHERE id='$id'");
  171.         }
  172.         if ($status eq 'join'){
  173.                 print $conn ":$s_ime SJOIN $ts $chan + :$mynick\n";
  174.         }
  175.         elsif (($status eq 'part') || ($status eq 'leave')){
  176.                 print $conn ":$mynick PART $chan\n";
  177.         }
  178. } #END of join cmds
  179.  
  180.  
  181. if (/^NICK/){
  182.     @words = split(/ /,$_);
  183.     my $nick = fix($words[1]);
  184.     my $ident = fix($words[5]);
  185.     my $host = fixm($words[6]);
  186.     $ts = time;
  187. #   dbg("$nick $ident $host $ts");
  188.     if ($ident !~ /^AT/){
  189.         $sth = $dbh->prepare("INSERT INTO seen (nick, user, host, when_on) VALUES
  190. ('$nick', '$ident', '$host', '$ts')");
  191.     $sth->execute();
  192.     }
  193. } #new nick introduced
  194.  
  195. if (/^:(.+?) NICK (.+?) :.*/ig){
  196.     $ts = time;
  197.     $old = fix($1);
  198.     $new = fix($2);
  199. #spirame stariq record
  200. #   print "Nickchange: $old -> $new\n";
  201.     $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$old' AND when_off='6131735909'");
  202.     $sth->execute();
  203.     while (@ids = $sth->fetchrow_array){
  204.         $id = "@ids";
  205. #       print "$id\n";
  206.     }
  207.     $dbh->do("UPDATE seen SET when_off='$ts',newnick='$new' WHERE id='$id'");
  208. #i pushtame noviq, kopirame dannite ot stariq
  209.     $sth = $dbh->prepare("SELECT user,host FROM seen WHERE id='$id'");
  210.     $sth->execute();
  211.     while (@arr = $sth->fetchrow_array){
  212.         $str = "@arr";
  213.         ($chuser,$chhost) = split(/ /,$str);
  214.         print "$chuser : $chhost\n";
  215.     }
  216.     $dbh->do("INSERT INTO seen (nick, user, host, when_on) VALUES ('$new','$chuser','$chhost','$ts')");
  217. #:ivanatora NICK maudib :1123345694
  218. } #end of nick change
  219.  
  220. #QUITs
  221. #killed
  222. if (/:(.+?) KILL (.+?) :.+? \((.+?)\)/ig){
  223. #   dbg("kill");
  224.     $killer = fix($1);
  225.     $nick = fix($2);
  226.     $msg = $3;
  227.     $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$nick' AND when_off='6131735909'");
  228.     $sth->execute();
  229.     @arr = $sth->fetchrow_array;
  230.     $id = "@arr";
  231.     $ts = time;
  232.     $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='Killed by $killer: $msg' WHERE id='$id'");
  233. }#eok
  234. #quited
  235. if (/:(.+?) QUIT :(.+?)$/ig){
  236.     $old = fix($1);
  237.     $msg = fixq($2);
  238. #   print "$old ..=> $msg\n";
  239.     $sth = $dbh->prepare("SELECT id FROM seen WHERE nick LIKE '$old' AND when_off='6131735909'");
  240.     $sth->execute();
  241.     @arr = $sth->fetchrow_array;
  242.     $id = "@arr";
  243.     $ts = time;
  244.     $dbh->do("UPDATE seen SET when_off='$ts',quit_msg='$msg' WHERE id='$id'");
  245. }
  246. #squit comes here,oops, there
  247. if (/:(.+?) PRIVMSG $mynick :\!?seen (.+?)\s/ig){
  248.     #close or die
  249.     my $who = fix($1);
  250.     my $mask = fixm($2);
  251.         @ws = split (/ /,$_);
  252.         if ($ws[5] =~ /d\d/){
  253.                 $ws[5] = fix($ws[5]);
  254.                 $ws[5] =~ s/[^\d]//g;
  255.                 if ($ws[5] > 5) { $limit = 5; }
  256.                 else { $limit = $ws[5]; }
  257.         }
  258.         else { $limit = 1; }
  259.         print "limit = $limit ws[5] = $ws[5]\n";
  260.     sn($who,$mask,undef,$limit);
  261. }
  262. if (/:(.+?) PRIVMSG (.+?) :\!?seen (.+?)\s/ig){
  263.     my $who = fix($1);
  264.     my $mask = fixm($3);
  265.     my $chan = fixc($2);
  266. #   @ws = split (/ /,$_);
  267. #   if ($ws[5] =~ /d\d/){
  268. #       $ws[5] = fix($ws[5]);
  269. #       $ws[5] =~ s/d//;
  270. #       if ($ws[5] > 5) { $limit = 5; }
  271. #       else { $limit = $ws[5]; }
  272. #   }
  273. #   else { $limit = 1; }
  274. #   print "limit = $limit ws[5] = $ws[5]\n";
  275.     sn($who,$mask,$chan,1);
  276. }
  277. sub sn($$$$){
  278.     my ($who,$mask,$chan,$limit) = ($_[0],$_[1],$_[2],$_[3]);
  279.     if ($chan){
  280.         $chan .= " ";
  281.     }
  282.     if ($chan) {
  283.         $sth = $dbh->prepare("SELECT status FROM chans WHERE chan='$chan'");
  284.         $sth->execute();
  285.         @st = $sth->fetchrow_array;
  286.     $status = "@st";
  287.                 if ($status ne 'answerprivate'){
  288.                 $public = 1; }
  289.         else { $public = 0; }
  290.                 }        
  291.     else {
  292.         $public = 0;
  293.     }
  294.     @maskw = split(/ /,$mask);
  295.     $first = $maskw[0];
  296.     $first =~ tr/*/%/;
  297.     print "first- $first ; chan - $chan\n";
  298.     if ($first =~ /\@/){
  299.         ($user,$host) = split(/\@/,$first);
  300.         print "seen: $user $host\n";
  301.         $sth = $dbh->prepare("SELECT nick,user,host,when_off,newnick,quit_msg FROM seen
  302. WHERE user LIKE '$user' AND host LIKE '$host' ORDER by when_off DESC LIMIT $limit");
  303.         $sth->execute();
  304.         while (@arr = $sth->fetchrow_array){
  305.     #A BIIG WHILE, INDEED
  306.         ($cur,$user,$host,$when,$newnick,$quitmsg) = ($arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5]);
  307. #       dbg(".$when.");
  308.         dbg(".$cur.$user.$host.$when.$newnick.$quitmsg.");
  309.         print "who1 = $who\n";
  310.  
  311.         if (($when eq '6131735909') & ($cur ne '')){
  312.             print $conn ":$mynick PRIVMSG ";
  313.             if ($public == 1) { print $conn "$chan:";}
  314.            
  315.             print $conn  "$who :$cur ($user\@"."$host) is currently ONLINE!\n";
  316.         }
  317.         elsif ($cur eq ''){
  318.             print $conn ":$mynick PRIVMSG ";
  319.             if ($public == 1) { print $conn "$chan:";}
  320.             print $conn "$who :Sorry, I haven't seen $mask recently.\n";
  321.         }
  322.         elsif ($when ne '6131735909'){
  323.             $dff = lst($when);
  324.             chomp $dff;
  325.             print $conn ":$mynick PRIVMSG ";
  326.             if ($public == 1) { print $conn "$chan:";}
  327.             print $conn "$who :$cur ($user\@"."$host) was last on IRC $dff ago";
  328.             if ($newnick ne ''){
  329.                         print $conn " user changed nick to $newnick\n";
  330.                         }
  331.                         elsif ($quitmsg ne ''){
  332.                         print $conn ", user quit: ($quitmsg)\n";
  333.                         }
  334.                         else {
  335.                         print $conn ".\n";
  336.                         }
  337.  
  338.         }
  339.         $spoken = 1;
  340.         } #end of WHILE @arr
  341.         if ($spoken == 0){
  342.             print $conn ":$mynick PRIVMSG ";
  343.             if ($public == 1) { print $conn "$chan:";}
  344.             print $conn "$who :Sorry, I haven't seen $mask recently.\n";
  345.         }
  346.         $spoken = 0;
  347.     }
  348.     else {
  349.         print "Seen nick: $first\n";
  350.         $sth = $dbh->prepare("SELECT nick,user,host,when_off,newnick,quit_msg FROM seen
  351. WHERE nick LIKE '$first' ORDER by when_off DESC LIMIT $limit");
  352.         $sth->execute();
  353.         while (@arr = $sth->fetchrow_array){
  354.     # ANOTHER BIG WHALE
  355.         ($cur,$user,$host,$when,$newnick,$quitmsg) = ($arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5]);
  356.         dbg(".$cur.$user.$host.$when.$newnick.$quitmsg.");
  357.         print "who2 = $who\n";
  358.                  if (($when eq '6131735909') & ($cur ne '')){
  359.                         print $conn ":$mynick PRIVMSG ";
  360.             if ($public == 1) { print $conn "$chan:";}
  361.             print $conn "$who :$cur ($user\@"."$host) is currently ONLINE!\n";
  362.                 }
  363.         elsif ($cur eq ''){
  364.                         print $conn ":$mynick PRIVMSG ";
  365.             if ($public == 1) { print $conn "$chan:";}
  366.             print $conn "$who :Sorry, I haven't seen $mask recently.\n";
  367.                 }
  368.                 elsif ($when ne '6131735909'){
  369.             $dff = lst($when);
  370.             chomp $dff;
  371.                         print $conn ":$mynick PRIVMSG ";
  372.             if ($public == 1) { print $conn "$chan:";}
  373.             print $conn "$who :$cur ($user\@"."$host) was last on IRC $dff ago";
  374.             if ($newnick ne ''){
  375.             print $conn ", user changed nick to $newnick\n";
  376.             }
  377.             elsif ($quitmsg ne ''){
  378.             print $conn " , quit: ($quitmsg)\n";
  379.             }
  380.             else {
  381.             print $conn ".\n";
  382.             }
  383.                 }
  384.         $spoken = 1;
  385.         } #end of big whale
  386.         if ($spoken == 0){
  387.             print $conn ":$mynick PRIVMSG ";
  388.             if ($public == 1) { print $conn "$chan:";}
  389.             print $conn "$who :Sorry, I haven't seen $mask recently.\n";
  390.         }
  391.         $spoken = 0;
  392.     }
  393. }
  394.  
  395. #} #END OF MAIN LOOP ako twa e while $sock
  396. } #end of FOREACH!@!@!@!!
  397. } #end of while 1 ?
  398. }
Add Comment
Please, Sign In to add comment