thomas: utils/admin kolab_vacation_tool, NONE, 1.1 kolab_vacation_tool.README, NONE, 1.1

cvs at intevation.de cvs at intevation.de
Thu Jan 19 16:35:20 CET 2006


Author: thomas

Update of /kolabrepository/utils/admin
In directory doto:/tmp/cvs-serv18899

Added Files:
	kolab_vacation_tool kolab_vacation_tool.README 
Log Message:
Added PHP script to change vacation settings for users.
Currently only changing the vacation domain is supported.


--- NEW FILE: kolab_vacation_tool ---
#!/kolab/bin/php
<?php
/*
 *  Copyright (c) 2005 Intevation GmbH
 *
 *    Writen by Thomas Arendsen Hein <thomas at intevation.de>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

$include_path = ini_get('include_path');
ini_set( 'include_path',
         '.:/kolab/var/kolab/php:/kolab/var/kolab/php/pear:'.$include_path);

require_once 'admin/include/sieveutils.class.php';
require_once 'PEAR.php';

include_once 'admin/include/Sieve.php';

function load_kolab_conf () {
  $kolab_conf = array();
  foreach(file('/kolab/etc/kolab/kolab.conf') as $line) {
    list($key, $value) = split(':', $line, 2);
    $kolab_conf[trim($key)] = trim($value);
  }
  $kolab_conf['manager'] = 'manager'; // XXX
  return $kolab_conf;
}


function user_homeserver ( $kolab_conf, $user_list ) {
  $users = array();

  // Connect to the LDAP server
  $ldap = ldap_connect($kolab_conf['ldap_uri']);
  if (!ldap_bind($ldap, $kolab_conf['bind_dn'], $kolab_conf['bind_pw'])) {
      fwrite(STDOUT, 'Unable to contact LDAP server: ' . ldap_error($ldap));
      exit(1);
  }

  foreach($user_list as $user) {
    $result = ldap_search($ldap, $kolab_conf['base_dn'],
                          "(&(objectClass=kolabInetOrgPerson)(uid=*)(mail=*)(sn=*)(kolabHomeServer=*)(|(mail=$user)(uid=$user)))",
                          array("mail", "kolabHomeServer"));
    if (!$result) {
      fwrite(STDOUT, 'Unable to perform LDAP search: ' . ldap_error($ldap));
      exit(1);
    }
    $entries = ldap_get_entries($ldap, $result);
    unset($entries['count']);
    if( count($entries) > 0 ) {
      foreach($entries as $entry) {
        $users[$entry['mail'][0]] = $entry['kolabhomeserver'][0];
      }
    } else {
      fwrite(STDOUT, "$user: Not found!\n");
    }
  }

  return $users;
}


function sieve_maildomain ( $auth, $pass, $host, $user, $new_maildomain ) {
  $errors = array();

  $scriptname = 'kolab-vacation.siv';

  $sieve =& new Net_Sieve( $auth, $pass, $host, 2000, '', $user );
  if( PEAR::isError( $sieve->_error ) ) {
    $errors[] = $sieve->_error->getMessage();
    return $errors;
  }

  $addresses = $days = $text = false;
  if( PEAR::isError( $scripts = $sieve->listScripts() ) ) {
    $errors[] = $scripts->getMessage();
    return $errors;
  }
  if( in_array( $scriptname, $scripts ) ) {
    $script = $sieve->getScript($scriptname);
    $maildomain = SieveUtils::getMailDomain( $script );
    $reacttospam = SieveUtils::getReactToSpam( $script );
    $addresses = SieveUtils::getVacationAddresses( $script );
    $days = SieveUtils::getVacationDays( $script );
    $text = SieveUtils::getVacationText( $script );
  } else $reacttospam = true;
  if( $addresses === false ) $addresses = array( "" );
  $active = ( $sieve->getActive() === $scriptname );

  if( $maildomain != $new_maildomain ) {

    $script = 
      "require \"vacation\";\r\n\r\n".
      (!empty($new_maildomain)?"if not address :domain :contains \"From\" \"".$new_maildomain."\" { keep; stop; }\r\n":"").
      ($reacttospam?"if header :contains \"X-Spam-Flag\" \"YES\" { keep; stop; }\r\n":"").
      ($addresses && $text && $days ?
      "vacation :addresses [ \"".join('", "', $addresses )."\" ] :days ".$days." text:\r\n".
      SieveUtils::dotstuff($text).".\r\n;":"")."\r\n\r\n";

    if( PEAR::isError( $res = $sieve->installScript( $scriptname, $script, $active ) ) ) {
      $errors[] = $res->getMessage();
      $errors[] = 'Script was:';
      $errors[] = $script;
      return $errors;
    }

    fwrite(STDOUT, "$user: \"$maildomain\" -> \"$new_maildomain\" on $host\n");
  } else {
    fwrite(STDOUT, "$user: already set to \"$new_maildomain\" on $host\n");
  }
}


// main

if( count($argv) < 3 ) {
  fwrite(STDOUT, "Usage: $argv[0] MAILDOMAIN USERPATTERN...\n");
  fwrite(STDOUT, "Set vacation parameter \"Only react to mail coming from domain\"\n");
  fwrite(STDOUT, "for Kolab users matching a given pattern. Wildcard is \"*\".\n");
  exit(1);
}

$kolab_conf = load_kolab_conf();

$argv = $_SERVER['argv'];
unset($argv[0]);
$new_maildomain = $argv[1];
unset($argv[1]);

$users = user_homeserver($kolab_conf, $argv);
if( count($users) < 1 ) {
  fwrite(STDOUT, "No matching users found.\n");
  exit(0);
}

foreach($users as $user => $homeserver) {
  $errors = sieve_maildomain ( $kolab_conf['manager'], $kolab_conf['bind_pw'], $homeserver, $user, $new_maildomain );
  if( $errors ) {
    fwrite(STDOUT, "Error while setting vacation maildomain for user ".$user.":\n");
    foreach($errors as $line) {
      fwrite(STDOUT, $line."\n");
    }
  }
}

exit(0);

/*
  Local variables:
  mode: php
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8 sts=2 sw=2 et:
 */
?>

--- NEW FILE: kolab_vacation_tool.README ---
kolab_vacation_tool
===================

Update vacation domain ("Only react to mail coming from domain XYZ"):


Installation
------------

as user 'root':
# cp kolab_vacation_tool /kolab/sbin/
# chown kolab /kolab/sbin/kolab_vacation_tool
# chmod 744 /kolab/sbin/kolab_vacation_tool

If you have many users you might need to increase the memory available
to PHP scripts in /kolab/etc/php/php.ini, e.g.:

  memory_limit = 32M


Usage
-----

as user 'kolab':
$ kolab_vacation_tool DOMAIN USER_PATTERN...

For selecting users you can use the primary email address or the uid.
Wildcard character is '*'.


Examples
--------

Set vacation domain for selected users to 'example.com':
$ kolab_vacation_tool example.com '*smith*' user at sub.example.com 'foo*'

Set vacation domain for _all_ users to 'sub.example.net':
$ kolab_vacation_tool sub.example.net '*'

Remove vacation domain setting for a selected user:
$ kolab_vacation_tool '' user at sub.example.com






More information about the commits mailing list