Skip to content

Commit

Permalink
Convert Ingo to use Horde\ManageSieve.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 28, 2015
1 parent 4bdbcdd commit eda1beb
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 160 deletions.
1 change: 1 addition & 0 deletions ingo/docs/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
v3.3.0-git
----------

[jan] Use Horde\ManageSieve instead of Net_Sieve.
[mms] Rearrange the default order of filter rules.


Expand Down
14 changes: 0 additions & 14 deletions ingo/docs/INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ To function properly, Ingo **requires** the following:

.. _`Horde Application Framework`: http://www.horde.org/apps/horde

2. The following PEAR modules:
(See `horde/docs/INSTALL`_ for instructions on installing PEAR modules)

.. Important:: If you are going to install Ingo the recommended way,
i.e. using the PEAR installer, you can skip the remainder of
this section. Installing Ingo through PEAR will
automatically download and install all required PEAR modules.

a. Net_Sieve [OPTIONAL]

Ingo uses the Net_Sieve class for communicating with timsieved running
on Cyrus mail servers. You will only need to install this class if you
are using Sieve for filtering.


Installing Ingo
===============
Expand Down
40 changes: 0 additions & 40 deletions ingo/lib/Exception/Pear.php

This file was deleted.

9 changes: 1 addition & 8 deletions ingo/lib/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ class Ingo_Test extends Horde_Test
*
* @var array
*/
protected $_pearList = array(
'Net_Socket' => array(
'error' => 'If you will be using Sieve scripts, make sure you are using a version of PEAR which includes the Net_Socket class, or that you have installed the Net_Socket package seperately.'
),
'Net_Sieve' => array(
'error' => 'If you will be using Sieve scripts, make sure you are using a version of PEAR which includes the Net_Sieve class, or that you have installed the Net_Sieve package seperately.'
)
);
protected $_pearList = array();

/**
* Inter-Horde application dependencies.
Expand Down
73 changes: 35 additions & 38 deletions ingo/lib/Transport/Sivtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @package Ingo
*/

use \Horde\ManageSieve;

/**
* Ingo_Transport_Sivtest implements an Ingo transport driver to allow scripts
* to be installed and set active via the Cyrus sivtest command line utility.
Expand Down Expand Up @@ -62,21 +64,17 @@ protected function _connect()
$this->_params['password'],
$this->_params['hostspec']);

$this->_sieve = new Net_Sieve(
$this->_params['username'],
$this->_params['password'],
'unix://' . $this->_params['socket'],
0,
null,
null,
false,
true,
$this->_params['usetls']);

$res = $this->_sieve->getError();
if ($res instanceof PEAR_Error) {
unset($this->_sieve);
throw new Ingo_Exception($res);
try {
$this->_sieve = new ManageSieve(array(
'user' => $this->_params['username'],
'password' => $this->_params['password'],
'host' => 'unix://' . $this->_params['socket'],
'port' => null,
'bypassauth' => true,
'usetls' => $this->_params['usetls']
));
} catch (ManageSieve\Exception $e) {
throw new Ingo_Exception($e);
}
}

Expand All @@ -94,16 +92,16 @@ protected function _connect()
public function sivtestSocket($username, $password, $hostspec)
{
$command = '';
$error_return = '';
$error_return = null;

if (strtolower($this->_params['logintype']) == 'gssapi' &&
if (Horde_String::lower($this->_params['logintype']) == 'gssapi' &&
isset($_SERVER['KRB5CCNAME'])) {
$command .= 'KRB5CCNAME=' . $_SERVER['KRB5CCNAME'];
$command .= 'KRB5CCNAME=' . $_SERVER['KRB5CCNAME'] . ' ';
}

$domain_socket = 'unix://' . $this->_params['socket'];

$command .= ' ' . $this->_params['command']
$command .= $this->_params['command']
. ' -m ' . $this->_params['logintype']
. ' -u ' . $username
. ' -a ' . $username
Expand All @@ -121,47 +119,46 @@ public function sivtestSocket($username, $password, $hostspec)
while (!file_exists($this->_params['socket'])) {
usleep(200000);
if ($attempts++ > 5) {
$error_return = ': No socket after 10 seconds of trying!';
$error_return = _("No socket after 10 seconds of trying");
continue 2;
}
}
}
$socket = new Net_Socket();
$error = $socket->connect($domain_socket, 0, true, 30);
if (!($error instanceof PEAR_Error)) {
try {
$socket = new \Horde\Socket\Client($domain_socket, 0, 30);
} catch (Horde_Exception $error_return) {
break;
}

// We failed, break this connection.
unlink($this->_params['socket']);
}

if (!empty($error_return)) {
if ($error_return) {
throw new Ingo_Exception($error_return);
}

$status = $socket->getStatus();
if ($status instanceof PEAR_Error || $status['eof']) {
throw new Ingo_Exception(_("Failed to write to socket: (connection lost!)"));
}

$error = $socket->writeLine("CAPABILITY");
if ($error instanceof PEAR_Error) {
throw new Ingo_Exception(_("Failed to write to socket: " . $error->getMessage()));
try {
$status = $socket->getStatus();
if ($status['eof']) {
throw new Ingo_Exception(_("Failed to write to socket: (connection lost!)"));
}
$socket->write("CAPABILITY\r\n");
} catch (Horde_Exception $e) {
throw new Ingo_Exception(sprintf(_("Failed to write to socket:"), $e->getMessage()));
}

$result = $socket->readLine();
if ($result instanceof PEAR_Error) {
throw new Ingo_Exception(_("Failed to read from socket: " . $error->getMessage()));
try {
$result = rtrim($socket->gets(), "\r\n");
} catch (Horde_Exception $e) {
throw new Ingo_Exception(sprintf(_("Failed to read from socket:"), $e->getMessage()));
}
$socket->close();

if (preg_match('|^bye \(referral "(sieve://)?([^"]+)|i',
$result, $matches)) {
$socket->disconnect();

$this->sivtestSocket($username, $password, $matches[2]);
} else {
$socket->disconnect();
exec($command . ' > /dev/null 2>&1');
sleep(1);
}
Expand Down
92 changes: 47 additions & 45 deletions ingo/lib/Transport/Timsieved.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @package Ingo
*/

use \Horde\ManageSieve;

/**
* Ingo_Transport_Timsieved implements an Ingo transport driver to allow
* scripts to be installed and set active via a Cyrus timsieved server.
Expand All @@ -25,9 +27,9 @@
class Ingo_Transport_Timsieved extends Ingo_Transport_Base
{
/**
* The Net_Sieve object.
* The ManageSieve object.
*
* @var Net_Sieve
* @var \Horde\ManageSieve
*/
protected $_sieve;

Expand Down Expand Up @@ -59,6 +61,8 @@ public function __construct(array $params = array())
*/
protected function _connect()
{
global $injector;

if (!empty($this->_sieve)) {
return;
}
Expand All @@ -67,38 +71,24 @@ protected function _connect()
? $this->_params['username']
: $this->_params['admin'];

$this->_sieve = new Net_Sieve(
$auth,
$this->_params['password'],
$this->_params['hostspec'],
$this->_params['port'],
$this->_params['logintype'],
$this->_params['euser'],
!empty($this->_params['debug']),
false,
$this->_params['usetls'],
null,
array($this, 'debug')
);

$res = $this->_sieve->getError();
if ($res instanceof PEAR_Error) {
unset($this->_sieve);
throw new Ingo_Exception($res);
try {
$this->_sieve = new ManageSieve(array(
'user' => $auth,
'password' => $this->_params['password'],
'host' => $this->_params['hostspec'],
'port' => $this->_params['port'],
'authmethod' => $this->_params['logintype'],
'euser' => $this->_params['euser'],
'usetls' => $this->_params['usetls'],
'logger' => $this->_params['debug']
? $injector->getInstance('Horde_Log_Logger')
: null,
));
} catch (ManageSieve\Exception $e) {
throw new Ingo_Exception($e);
}
}

/**
* Routes the Sieve protocol log to the Horde log.
*
* @param Net_Sieve $sieve A Net_Sieve object.
* @param string $message The tracked Sieve communication.
*/
public function debug($sieve, $message)
{
Horde::log($message, 'DEBUG');
}

/**
* Sets a script running on the backend.
*
Expand All @@ -113,14 +103,22 @@ public function setScriptActive($script)
{
$this->_connect();

if (!strlen($script['script'])) {
Ingo_Exception_Pear::catchError($this->_sieve->setActive(''));
Ingo_Exception_Pear::catchError($this->_sieve->removeScript($script['name']));
return;
}
try {
if (!strlen($script['script'])) {
$this->_sieve->setActive('');
$this->_sieve->removeScript($script['name']);
return;
}

Ingo_Exception_Pear::catchError($this->_sieve->haveSpace($script['name'], strlen($script['script'])));
Ingo_Exception_Pear::catchError($this->_sieve->installScript($script['name'], $script['script'], true));
if (!$this->_sieve->hasSpace($script['name'], strlen($script['script']))) {
throw new Ingo_Exception(_("Not enough free space on the server."));
}
$this->_sieve->installScript(
$script['name'], $script['script'], true
);
} catch (ManageSieve\Exception $e) {
throw new Ingo_Exception($e);
}
}

/**
Expand All @@ -133,13 +131,17 @@ public function setScriptActive($script)
public function getScript()
{
$this->_connect();
$active = Ingo_Exception_Pear::catchError($this->_sieve->getActive());
if (!strlen($active)) {
throw new Horde_Exception_NotFound();
try {
$active = $this->_sieve->getActive();
if (!strlen($active)) {
throw new Horde_Exception_NotFound();
}
return array(
'name' => $active,
'script' => $this->_sieve->getScript($active)
);
} catch (ManageSieve\Exception $e) {
throw new Ingo_Exception($e);
}
return array(
'name' => $active,
'script' => Ingo_Exception_Pear::catchError($this->_sieve->getScript($active))
);
}
}

0 comments on commit eda1beb

Please sign in to comment.