#!/usr/bin/perl -w
###########################################
# posting-watcher
# Mike Schilli, 2006 (m@perlmeister.com)
###########################################
use strict;
use PhpbbDB;
use Cache::FileCache;
use Digest::MD5;
use Mail::Mailer;
use Mail::Internet;
use Text::ASCIITable;
use Text::Wrap qw(wrap);
use Getopt::Std;
use WWW::Mechanize::Pluggable;
getopts("kc", \my %opts);

my $FORUM_URL  = "http://foo.com/forum";
my $FORUM_USER = "forum_user_id";
my $FORUM_PASS = "XXXXXXXX";

my $TO     = 'moderator@foo.com';
my $REPL   = 'forumcleaner@foo.com';
my $EXPIRE = 14*24*3600;

my $cache = Cache::FileCache->new({
  cache_root => "$ENV{HOME}/phpbb-cache",
  namespace  => "phpbb-watcher",
});

$cache->purge();

if($opts{k}) {
  my @data = <>;
  my $body = join '', @data;
  if($body =~ /\[delete-key (.*?)\]/) {
    my $id = kill_by_key($1);
    my $mail = Mail::Internet->new(\@data);
    if($mail) {
    my $reply = $mail->reply();
    $reply->body(
      ["Deleted posting $id.\n\n", @data]);
    $reply->send() or 
                   die "Reply mail failed";
  } 
    } 
} elsif($opts{c}) {
  check();
} else {
  die "Use -c or -k";
}
###########################################
sub kill_by_key {
###########################################
  my($key) = @_;
  my $id = $cache->get("key$key");
  if(defined $id) {
      msg_remove($id);
  } else {
      die "Invalid key $key";
  }

  return $id;
}

###########################################
sub check {
###########################################
  my $latest = $cache->get("_latest");
  $latest = -1 unless defined $latest;
  my $new_posts = 
    PhpbbPost::Manager->get_phpbb_posts(
      query => 
        [ post_id => { gt => $latest } ]
  );
    
  foreach my $p (@$new_posts) {
    my $id = $p->post_id();
    
    my $key = genkey();
    
    mail($id, format_post($id, 
         $p->text()->post_text(),
         $p->topic()->topic_title(),
         $key), $key
    );
      $cache->set("key$key", $id, $EXPIRE);
    
      $latest = $id;
    }
    $cache->set("_latest", $latest);
}

###########################################
sub genkey {
###########################################
  return Digest::MD5::md5_hex(
        Digest::MD5::md5_hex(
            time(). {}. rand(). $$));
}

###########################################
sub mail {
###########################################
  my($id, $body, $key) = @_;

  my $m = Mail::Mailer->new('sendmail');
  $m->open({
    To      => $TO,
    Subject => "Forum News (#$id) [delete-key $key]",
    From    => $REPL });

  print $m $body;
}

###########################################
sub format_post {
###########################################
  my($id, $text, $topic, $key) = @_;

  my $t = Text::ASCIITable->new(
                       {drawRowLine => 1});

  $t->setCols('Header', 'Content');
  $t->setColWidth("Header", 6);

  $Text::Wrap::columns=60;

  $text =~ s/[^[:print:]]/./g;

  $t->addRow('post', "#$id");
  $t->addRow('topic', $topic);
  $t->addRow('text', wrap("", "", $text));
  $t->addRow('key', "[delete-key $key]");

  return $t->draw();
}

###########################################
sub msg_remove {
###########################################
  my($post_id) = @_;
  my $mech = 
      WWW::Mechanize::Pluggable->new();
  $mech->get($FORUM_URL);

  $mech->phpbb_login($FORUM_USER, 
                     $FORUM_PASS);
  $mech->get(
    "$FORUM_URL/viewtopic.php?p=$post_id");
  $mech->phpbb_post_remove($post_id);
}
