[sork] sqlprocmail forwards driver

Carlos Alberto Barcenilla barce at dearriba.com
Mon Apr 7 23:10:05 PDT 2003


#!/usr/bin/perl
use POSIX;
use Pg;
use Sys::Syslog qw(:DEFAULT setlogsock);

# CONFIGURACION
*REALM = \'frlp.utn.edu.ar';

#   PROGRAMA PRINCIPAL  #

setlogsock('unix');
openlog("forwards","cons,pid","user");

# conecta a la base de datos 
$conn = Pg::connectdb("dbname=horde user=horde password=mypassword host=database.frlp.utn.edu.ar port=5432");
if ($conn->status != PGRES_CONNECTION_OK) {
    print "Conexion Fallida\n";
    syslog("err","Error: No fue posible abrir la base de datos");
    exit 1;
}
    $query_quotas = "BEGIN WORK";
    $result = $conn->exec($query_quotas);
    if ($result->resultStatus!=PGRES_COMMAND_OK) {
     syslog("err","Error: Fallo en BEGIN WORK");
     exit 1;
    }

    $query_quotas = "SELECT * FROM horde_forwards WHERE newentry='y' ".
    "AND username LIKE '%@".$REALM."'";

    $result = $conn->exec($query_quotas);
    if ($result->resultStatus!=PGRES_TUPLES_OK) {
     syslog("err","Error: Fallo en SELECT");
     exit 1;
    }

 $cantidad_usuarios = $result->ntuples;
 for ($i=0 ; $i<$cantidad_usuarios; $i++) {
    $username = $result->getvalue($i, $result->fnumber('username'));
    $forwardto = $result->getvalue($i, $result->fnumber('forwardto'));
    $keeplocal = $result->getvalue($i, $result->fnumber('keeplocal')) eq 'y';
    $forward = $result->getvalue($i, $result->fnumber('forward')) eq 'y';

    $username =~ /(.+)@(.+)/ ;
    $user=$1;
    $domain=$2;

    ($name,$passwd,$uid,$gid, $quota, 
    $comment,$gcos,$dir,$shell,$expire)  = getpwnam($user);
    open PROCMAILRC, '>'.$dir.'/.procmailrc';
    if ($forward) {
       print PROCMAILRC ":0 ".($keeplocal ? 'c':'')."\n";
       print PROCMAILRC "*\n";
       print PROCMAILRC "!$forwardto\n";
    }
    close PROCMAILRC;

 }

    $query_quotas = "UPDATE horde_forwards SET newentry='n' ".
    "WHERE username LIKE '%@".$REALM."'";
    $result = $conn->exec($query_quotas);
    if ($result->resultStatus!=PGRES_COMMAND_OK) {
     syslog("err","Error: Fallo en UPDATE");
     exit 1;
    }

    $query_quotas = "COMMIT";
    $result = $conn->exec($query_quotas);
    if ($result->resultStatus!=PGRES_COMMAND_OK) {
     syslog("err","Error: Fallo en COMMIT");
     exit 1;
    }

closelog();  # cierra la conexion con syslog




More information about the sork mailing list