Advertisement
Guest User

goodbyicq.pl

a guest
Feb 19th, 2011
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.50 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use Net::OSCAR;
  4. use Net::SMTP;
  5. use Authen::SASL;
  6. use HTML::Entities;
  7. use Encode;
  8. use strict;
  9. use constant {
  10.     SELFLOG => './run/icq.log', # stderr output goes here when daemon
  11.     SELFPID => './run/icq.pid', # process pid goes here when daemon
  12.     HISTDIR => './history', # directory for icq history
  13.     TZ => 'Asia/Novosibirsk', # If server timezone different from that you prefer
  14.     MAILHOST => 'smtp.list.ru', # smtp mail server
  15.     MAILLOGIN => 'xxx@list.ru', # smtp login
  16.     MAILPASSW => 'xxx', # smtp password
  17.     MAILTO => 'xxx@gmail.com', # send mail here
  18.     ICQLOGIN => '1234567', # icq uin
  19.     ICQPASSW => 'xxxxxxx', # icq password
  20.     STATUS => 'This is Net::OSCAR bot', # icq status
  21.     BLACKLIST => ['3234343', '23444542'], # array of blacklisted uins
  22.     BLACKMSG  => 'I hate you', # message for blacklisted
  23.     MSG => "I can't use this shit anymore. Use email or jabber to contact me. Here it is: xxx\@gmail.com" # massage for others
  24. };
  25.  
  26. if($ARGV[0] eq '-d') {
  27.     require Proc::Daemon;
  28.     Proc::Daemon->VERSION(0.05);
  29.    
  30.     Proc::Daemon->new(
  31.         child_STDERR => SELFLOG ? SELFLOG: '/dev/null',
  32.         pid_file => SELFPID,
  33.         work_dir => '.',
  34.     )->Init();
  35. }
  36.  
  37. $ENV{'TZ'} = TZ;
  38.  
  39. my $CURDATE = date();
  40. my $online = 1;
  41. my $icq = Net::OSCAR->new(capabilities => ['extended_status']);
  42. $icq->set_callback_signon_done(\&signon_done);
  43. $icq->set_callback_im_in(\&im_in);
  44. $icq->set_callback_error(\&error);
  45. $icq->signon(screenname => ICQLOGIN, password => ICQPASSW);
  46.  
  47. $SIG{'INT'} = $SIG{'TERM'} = sub { $online = 0 };
  48.  
  49. while($online) {
  50.     $icq->do_one_loop();
  51.    
  52.     if(HISTDIR && $CURDATE ne date()) {
  53.         send_mail( get_html($icq) );
  54.         $CURDATE = date();
  55.     }
  56. }
  57. $icq->signoff();
  58.  
  59. sub signon_done
  60. {
  61.     my $icq = shift;
  62.     $icq->set_extended_status(STATUS) if STATUS;
  63. }
  64.  
  65. sub im_in
  66. {
  67.     my ($icq, $sender, $msg) = @_;
  68.    
  69.     if(HISTDIR) {
  70.         my ($sec, $min, $hour) = localtime;
  71.        
  72.         my $subdir = HISTDIR . '/' . $CURDATE;
  73.         unless(-e $subdir) {
  74.             mkdir $subdir, 0755;
  75.         }
  76.        
  77.         if($msg =~ /\00|\04/) {
  78.             # icq, qip, other shit
  79.             Encode::from_to($msg, 'ucs-2be', 'windows-1251');
  80.         }
  81.        
  82.         open my $fh, '>>', $subdir . '/' . $sender;
  83.         print $fh "<small>$hour:$min:$sec $CURDATE</small><br><pre>", encode_entities($msg, '\x0-\x40'), "</pre><hr>\n";
  84.         close $fh;
  85.     }
  86.    
  87.     if(BLACKLIST && array_exists(BLACKLIST, $sender)) {
  88.         $icq->send_im($sender, BLACKMSG);
  89.     }
  90.     else {
  91.         $icq->send_im($sender, MSG);
  92.     }
  93. }
  94.  
  95. sub error
  96. {
  97.     if($_[4]) {
  98.         # fatal error
  99.         die $_[3];
  100.     }
  101.     else {
  102.         warn $_[3];
  103.     }
  104. }
  105.  
  106. sub array_exists
  107. {
  108.     my ($array, $needle) = @_;
  109.    
  110.     $needle eq $_ and return 1 foreach @$array;
  111.     return 0;
  112. }
  113.  
  114. sub date
  115. {
  116.     my ($mday, $mon, $year) = (localtime)[3,4,5];
  117.     ++$mon;
  118.     $year += 1900;
  119.     return join('.', $mday, $mon, $year);
  120. }
  121.  
  122. sub get_html
  123. {
  124.     my $icq = shift;
  125.    
  126.     my $subdir = HISTDIR . '/' . $CURDATE;
  127.     opendir my $dh, $subdir
  128.         or return;
  129.     my $html;
  130.     while(defined(my $f = readdir($dh))) {
  131.         if($f ne '.' && $f ne '..') {
  132.             $html .= "<h3>$f - " . $icq->buddy($f)->{alias} . "</h3>";
  133.             open my $fh, $subdir . '/' . $f
  134.                 or next;
  135.             local $/ = undef;
  136.             $html .= <$fh>;
  137.             close $fh;
  138.         }
  139.     }
  140.     close $dh;
  141.    
  142.     return $html;
  143. }
  144.  
  145. sub send_mail
  146. {
  147.     my $body = shift
  148.         or return;
  149.     my $smtp = Net::SMTP->new(MAILHOST);
  150.     $smtp->auth(MAILLOGIN, MAILPASSW)
  151.         or return;
  152.     $smtp->mail(MAILLOGIN);
  153.     $smtp->to(MAILTO);
  154.     $smtp->data(
  155.         "To: " . MAILTO . "\r\n",
  156.         "Content-Type: text/html; charset=windows-1251\r\n",
  157.         "Subject: new icq messages on $CURDATE\r\n\r\n",
  158.         $body
  159.     );
  160.     $smtp->quit();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement