[vhffs-dev] [886] modobot now use the new moderation API |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 886
Author: gradator
Date: 2007-09-09 04:26:04 +0000 (Sun, 09 Sep 2007)
Log Message:
-----------
modobot now use the new moderation API
Modified Paths:
--------------
trunk/vhffs-irc/modobot.pl
Modified: trunk/vhffs-irc/modobot.pl
===================================================================
--- trunk/vhffs-irc/modobot.pl 2007-09-09 04:20:19 UTC (rev 885)
+++ trunk/vhffs-irc/modobot.pl 2007-09-09 04:26:04 UTC (rev 886)
@@ -1,34 +1,35 @@
-#! %PERL%
+#!/usr/bin/perl
# modobot is an IRC bot which allow you to validate
# VHFFS objects through IRC
-# Written by Florent Bayle
+# Written by Florent Bayle and Sylvain Rochet (this is very important to add my name here, to become famous very soon)
use POSIX qw(locale_h);
use locale;
use strict;
use warnings;
use Locale::gettext;
-use Data::Dumper;
-use lib '%VHFFS_LIB_DIR%';
+use lib '/usr/share/vhffs/api';
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Main;
-use Vhffs::Services::Web;
-use Vhffs::Acl;
use Vhffs::Constants;
use Vhffs::Note;
+use Vhffs::Object;
+use Vhffs::ObjectFactory;
+use Data::Dumper;
+
use Net::IRC;
use Text::Wrapper;
use constant
{
- SERVER => "PUT YOUR IRC SERVER HERE",
- PORT => "6667",
- CHANNEL => "IRC CHANNEL TO JOIN WITH #",
- NAME => "BOT NAME",
+ SERVER => 'PUT YOUR IRC SERVER HERE',
+ PORT => '6667',
+ CHANNEL => 'IRC CHANNEL TO JOIN WITH #',
+ NAME => 'BOT NAME',
};
@@ -41,402 +42,89 @@
my $bot;
my $port = PORT;
-my @oldgroup = ();
-my @oldweb = ();
-my @olddns = ();
-my @oldcvs = ();
-my @oldmailing = ();
-my @oldsvn = ();
-my @oldmail = ();
-my @oldmysql = ();
-my @oldpostgres = ();
-my @oldrepository = ();
+my %oldobjects = ();
my $irc=new Net::IRC;
my $cmpt = 0;
-# Connection
+# Connections to servers
+
+my $vhffs = init Vhffs::Main;
+exit 1 unless $vhffs;
+
my $conn=$irc->newconn(Nick => $botname,
Server => $srv,
Port => $port,
- Username => "pacman",
- Ircname => "Bernard Minet");
+ Username => 'pacman',
+ Ircname => 'Bernard Minet' );
+exit 2 unless $conn;
-my $vhffs = init Vhffs::Main;
-
-
sub deletenl
{
($_) = @_;
$_ =~ s/\n/ /g;
$_ =~ s/\r//g;
return $_;
-} # deletenl
+}
sub list_moderation
{
- #Treat Groups to moderate
- use Vhffs::Panel::Group;
- my $groups = Vhffs::Group::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
- my $temp;
+ my $objects = Vhffs::Object::getall( $vhffs, undef, Vhffs::Constants::WAITING_FOR_VALIDATION );
+ if( defined $objects ) {
+ foreach my $obj ( @{$objects} ) {
+ next if exists( ${%oldobjects}{$obj->get_oid} );
- if( defined $groups )
- {
-GR: foreach $temp ( @{$groups} )
- {
- map { last GR if (($_->get_oid eq $temp->get_oid) && ($_->get_groupname eq $temp->get_groupname)); } @oldgroup;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("Groupe : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_groupname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("Groupe : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_groupname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldgroup = @{$groups};
- }
- else
- {
- @oldgroup = ();
- }
+ my $user = $obj->get_user;
+ my $group = $obj->get_group;
+ my $object = Vhffs::ObjectFactory::fetch_object( $vhffs , $obj->{object_id} );
- if( $vhffs->get_config->get_service_availability('web') == 1 )
- {
- #Treat Website to moderate
- use Vhffs::Services::Web;
- my $webs = Vhffs::Services::Web::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION);
+ my $msg = Vhffs::Functions::type_string_from_type_id( $obj->{type} ).': '.$obj->get_oid.' '.$user->get_username;
+ $msg .= ' ('.Vhffs::Note::get_note( $vhffs , $user ).')' if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' );
+ $msg .= ' ['.$user->get_lang.'] '.$group->get_groupname.' '.$obj->get_description;
+ irc_msg( $msg );
- if( defined $webs )
- {
-WE: foreach $temp ( @{$webs} )
- {
- map { last WE if (($_->get_oid eq $temp->get_oid) && ($_->get_servername eq $temp->get_servername)); } @oldweb;
-
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("Web : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_servername . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("Web : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_servername . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldweb = @{$webs};
- }
- else
- {
- @oldweb = ();
+ ${%oldobjects}{$obj->get_oid} = '';
+ }
}
- }
+}
- if( $vhffs->get_config->get_service_availability('dns') == 1 )
- {
- #Treat DNS to moderate
- use Vhffs::Services::DNS;
- my $nss = Vhffs::Services::DNS::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
- if( defined $nss )
- {
-DN: foreach $temp ( @{$nss} )
- {
- map { last DN if (($_->get_oid eq $temp->get_oid) && ($_->get_domain eq $temp->get_domain)); } @olddns;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("DNS : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("DNS : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
-
- }
- @olddns = @{$nss};
- }
- else
- {
- @olddns = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('cvs') == 1 )
- {
- #Treat CVS to moderate
- use Vhffs::Services::Cvs;
- my $repos = Vhffs::Services::Cvs::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $repos )
- {
-CV: foreach $temp ( @{$repos} )
- {
- map { last CV if (($_->get_oid eq $temp->get_oid) && ($_->get_cvsroot eq $temp->get_cvsroot)); } @oldcvs;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("CVS : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_cvsroot . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("CVS : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_cvsroot . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldcvs = @{$repos};
- }
- else
- {
- @oldcvs = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('mailinglist') == 1 )
- {
- #Treat Mailing lists to moderate
- use Vhffs::Panel::MailingList;
- my $mls = Vhffs::Services::MailingList::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $mls )
- {
-ML: foreach $temp ( @{$mls} )
- {
- map { last ML if (($_->get_oid eq $temp->get_oid) && ($_->get_localpart eq $temp->get_localpart)); } @oldmailing;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("Mailing list : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") " . $temp->get_group->get_groupname . " " . $temp->get_localpart . "@" . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("Mailing list : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_localpart . "@" . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldmailing = @{$mls};
- }
- else
- {
- @oldmailing = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('svn') == 1 )
- {
- #Treat Svn to moderate
- use Vhffs::Services::Svn;
- my $repos = Vhffs::Services::Svn::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $repos )
- {
-SV: foreach $temp ( @{$repos} )
- {
- map { last SV if (($_->get_oid eq $temp->get_oid) && ($_->get_reponame eq $temp->get_reponame)); } @oldsvn;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("SVN : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_reponame . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("SVN : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_reponame . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldsvn = @{$repos};
- }
- else
- {
- @oldsvn = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('mail') == 1 )
- {
- #Treat Mail domains to moderate
- use Vhffs::Panel::Mail;
- my $mails = Vhffs::Services::Mail::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $mails )
- {
-MA: foreach $temp ( @{$mails} )
- {
- map { last MA if (($_->get_oid eq $temp->get_oid) && ($_->get_domain eq $temp->get_domain)); } @oldmail;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("Mail : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("Mail : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_domain . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldmail = @{$mails};
- }
- else
- {
- @oldmail = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('mysql') == 1 )
- {
- #Treat Mysql to moderate
- use Vhffs::Services::Mysql;
- my $dbs = Vhffs::Services::Mysql::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $dbs )
- {
-MY: foreach $temp ( @{$dbs} )
- {
- map { last MY if (($_->get_oid eq $temp->get_oid) && ($_->get_dbname eq $temp->get_dbname)); } @oldmysql;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("MySql : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_dbname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("MySql : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_dbname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldmysql = @{$dbs};
- }
- else
- {
- @oldmysql = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('pgsql') == 1 )
- {
- #Treat Postgres to moderate
- use Vhffs::Services::Pgsql;
- my $dbs = Vhffs::Services::Pgsql::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined @{$dbs} )
- {
-PO: foreach $temp ( @{$dbs} )
- {
- map { last PO if (($_->get_oid eq $temp->get_oid) && ($_->get_dbname eq $temp->get_dbname)); } @oldpostgres;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("Postgres : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_dbname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("Postgres : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_dbname . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldpostgres = @{$dbs};
- }
- else
- {
- @oldpostgres = ();
- }
- }
-
- if( $vhffs->get_config->get_service_availability('repository') == 1 )
- {
- #Treat Repository to moderate
- use Vhffs::Services::Repository;
- my $repos = Vhffs::Services::Repository::getall( $vhffs , Vhffs::Constants::WAITING_FOR_VALIDATION );
-
- if( defined $repos )
- {
-RP: foreach $temp ( @{$repos} )
- {
- map { last RP if (($_->get_oid eq $temp->get_oid) && ($_->get_name eq $temp->get_name)); } @oldrepository;
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- irc_msg ("REPOSITORY : " . $temp->get_oid . " " . $temp->get_user->get_username . "(" . Vhffs::Note::get_note( $vhffs , $temp->get_user ) . ") [".$temp->get_user->get_lang."] " . $temp->get_group->get_groupname . " " . $temp->get_name . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- else
- {
- irc_msg ("REPOSITORY : " . $temp->get_oid . " " . $temp->get_user->get_username . " " . $temp->get_group->get_groupname . " " . $temp->get_name . " " . Vhffs::Functions::stripslashes($temp->get_description));
- }
- }
- @oldrepository = @{$repos};
- }
- else
- {
- @oldrepository = ();
- }
- }
-
-} # list_moderation
-
-
sub moderate
{
- my $oid;
- my $status; # 0 = refuse , 1 = accept
- my $object;
- my $raison;
+ my $oid = shift;
+ my $status = shift; # 0 = refuse , 1 = accept
+ my $reason = shift;
- $oid = shift;
- $status = shift;
+ my $object = Vhffs::ObjectFactory::fetch_object( $vhffs , $oid );
- $object = Vhffs::Object::get_by_oid( $vhffs , $oid );
-
- if( ! defined $object )
- {
- irc_msg ("Error : Cannot fetch object");
- return ( -1 );
- }
- elsif( $object->get_status != Vhffs::Constants::WAITING_FOR_VALIDATION )
- {
- irc_msg ("Error : Object is not waiting for validation");
- return ( -2 );
- }
- else
- {
- if( $status == 1 )
- {
- $object->set_status( Vhffs::Constants::WAITING_FOR_CREATION );
- my $userbis = Vhffs::User::get_by_uid( $vhffs , $object->get_owner_uid );
- if( defined $userbis )
- {
- my $mail;
- $mail = gettext('Your request for a ');
- $mail .= Vhffs::Functions::type_string_from_type_id($object->get_type);
- $mail .= (' on ');
- $mail .= $vhffs->get_config->get_host_name;
- $mail .= gettext(" was accepted\nPlease wait while we are creating your object\n");
- $mail .= gettext("Cheers,\nThe Moderator and Admin team\n");
- $mail .= "-----\n";
- $mail .= $vhffs->get_config->get_host_name()."\n";
- $mail .= $vhffs->get_config->get_panel->{'url'};
-
-
- my $subject = sprintf(gettext("Your request on %s"), $vhffs->get_config->get_host_name());
- Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $userbis->get_mail , $subject , $mail );
-
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- use Vhffs::Note;
- Vhffs::Note::inc_note( $vhffs , $userbis , 1 );
- }
- }
- }
- else
- {
- $raison = shift;
- $object->set_status( Vhffs::Constants::VALIDATION_REFUSED );
- $object->set_description( $raison );
- my $userbis = Vhffs::User::get_by_uid( $vhffs , $object->get_owner_uid );
- if( defined $userbis )
- {
- if( $vhffs->get_config->get_users->{'use_notation'} eq 'yes' )
- {
- use Vhffs::Note;
- Vhffs::Note::inc_note( $vhffs , $userbis , -1 );
- }
- }
- }
-
- if( $object->commit < 0 )
- {
- irc_msg ("Error while commiting changes");
- return ( -3 );
- }
-
- }
- return 0;
+ unless( defined $object )
+ {
+ irc_msg ('Error : Cannot fetch object');
+ return ( -1 );
+ }
+ elsif( $object->get_status != Vhffs::Constants::WAITING_FOR_VALIDATION )
+ {
+ irc_msg ('Error : Object is not waiting for validation');
+ return ( -2 );
+ }
+ else
+ {
+ if( $status == 1 ) {
+ if( $object->moderate_accept < 0 ) {
+ irc_msg('Error while committing changes');
+ }
+ }
+ else {
+ if( $object->moderate_refuse( $reason ) < 0 ) {
+ irc_msg('Error while committing changes');
+ }
+ }
+ delete ${%oldobjects}{$oid};
+ }
+ return 0;
}
@@ -631,17 +319,8 @@
}
elsif ($texte =~ m/^${mynick}: list$/)
{
- @oldgroup = ();
- @oldweb = ();
- @olddns = ();
- @oldcvs = ();
- @oldmailing = ();
- @oldsvn = ();
- @oldmail = ();
- @oldmysql = ();
- @oldpostgres = ();
- @oldrepository = ();
- list_moderation();
+ %oldobjects = ();
+ list_moderation();
}
elsif ($texte =~ m/^${mynick}: desc [a-z0-9]+$/)
{