var $_sasl_username;
/**
+ * Comman line parser.
+ *
+ * @param Horde_Kolab_Filter_Cli
+ */
+ private $_cli;
+
+ /**
* The log backend that needs to implement the debug(), info() and err()
* methods.
*
* @param mixed $logger The logger.
*/
public function __construct(
+ Horde_Kolab_Filter_Cli $cli,
$logger
) {
+ $this->_cli = $cli;
$this->_logger = $logger;
}
{
global $conf;
- /* Get command line options. */
- $p = new Horde_Kolab_Filter_Argv_Parser(
- array('optionList' =>
- array(
- new Horde_Argv_Option('-s',
- '--sender',
- array('help' => 'The message sender.',
- 'type' => 'string',
- 'nargs' => 1)),
- new Horde_Argv_Option('-r',
- '--recipient',
- array('help' => 'A message recipient.',
- 'action' => 'append',
- 'type' => 'string')),
- new Horde_Argv_Option('-H',
- '--host',
- array('help' => 'The host running this script.')),
- new Horde_Argv_Option('-c',
- '--client',
- array('help' => 'The client sending the message.')),
- new Horde_Argv_Option('-u',
- '--user',
- array('help' => 'ID of the currently authenticated user.',
- 'default' => '')),
- new Horde_Argv_Option('-C',
- '--config',
- array('help' => 'Path to the configuration file for this filter.'))
- )));
-
- try {
- list($values, $args) = $p->parseArgs();
- } catch (InvalidArgumentException $e) {
- $msg = $e->getMessage() . "\n\n" . $p->getUsage();
- return PEAR::raiseError($msg, OUT_STDOUT | EX_USAGE);
- }
+ $values = $this->_cli->getOptions();
if (!empty($values['config']) && file_exists($values['config'])) {
require_once $values['config'];
}
- if (empty($values['recipient'])) {
- throw new Horde_Kolab_Filter_Exception(
- sprintf(
- "Please provide one or more recipients.\n\n%s",
- $p->getUsage()
- ),
- Horde_Kolab_Filter_Exception::OUT_STDOUT |
- Horde_Kolab_Filter_Exception::EX_USAGE
- );
- }
-
$this->_sender = strtolower($values['sender']);
$this->_recipients = array_map('strtolower', $values['recipient']);
$this->_client_address = $values['client'];
}
}
-class Horde_Kolab_Filter_Argv_Parser extends Horde_Argv_Parser
-{
- public function parserError($msg)
- {
- throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
- }
-
- public function parserExit($status = 0, $msg = null)
- {
- throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
- }
-}
--- /dev/null
+<?php
+/**
+ * The command line handling for the Kolab_Filter package.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * The command line handling for the Kolab_Filter package.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli
+{
+ /**
+ * The CLI argument parser.
+ *
+ * @var Horde_Argv_Parser
+ */
+ private $_parser;
+
+ /**
+ * The CLI options.
+ *
+ * @var Horde_Argv_Values
+ */
+ private $_options;
+
+ /**
+ * The CLI arguments.
+ *
+ * @var array
+ */
+ private $_arguments;
+
+ /**
+ * Constructor.
+ */
+ public function __construct()
+ {
+ $this->_parser = new Horde_Kolab_Filter_Cli_Parser(
+ array('optionList' =>
+ array(
+ new Horde_Argv_Option(
+ '-s',
+ '--sender',
+ array(
+ 'help' => 'The message sender.',
+ 'type' => 'string',
+ 'nargs' => 1
+ )
+ ),
+ new Horde_Argv_Option(
+ '-r',
+ '--recipient',
+ array(
+ 'help' => 'A message recipient.',
+ 'action' => 'append',
+ 'type' => 'string'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-H',
+ '--host',
+ array(
+ 'help' => 'The host running this script.'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-c',
+ '--client',
+ array(
+ 'help' => 'The client sending the message.'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-u',
+ '--user',
+ array(
+ 'help' => 'ID of the currently authenticated user.',
+ 'default' => ''
+ )
+ ),
+ new Horde_Argv_Option(
+ '-C',
+ '--config',
+ array(
+ 'help' => 'Path to the configuration file for this filter.'
+ )
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * Parse the command line arguments.
+ *
+ * @return NULL
+ */
+ public function parse()
+ {
+ try {
+ list($this->_options, $this->_arguments) = $this->_parser->parseArgs();
+ } catch (InvalidArgumentException $e) {
+ throw new Horde_Kolab_Filter_Exception_Usage(
+ $e->getMessage() . "\n\n" . $this->_parser->getUsage()
+ );
+ }
+
+ if (empty($this->_options['recipient'])) {
+ throw new Horde_Kolab_Filter_Exception_Usage(
+ sprintf(
+ "Please provide one or more recipients.\n\n%s",
+ $this->_parser->getUsage()
+ )
+ );
+ }
+ }
+
+ /**
+ * Return the command line options.
+ *
+ * @return Horde_Argv_Values The command line values.
+ */
+ public function getOptions()
+ {
+ if ($this->_options === null) {
+ $this->parse();
+ }
+ return $this->_options;
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * A parser variant that does not automatically exit on a parser error.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * A parser variant that does not automatically exit on a parser error.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli_Parser extends Horde_Argv_Parser
+{
+ public function parserError($msg)
+ {
+ return $this->parserExit(0, $msg);
+ }
+
+ public function parserExit($status = 0, $msg = null)
+ {
+ throw new InvalidArgumentException(
+ sprintf("%s: error: %s\n", $this->getProgName(), $msg)
+ );
+ }
+}
* @link http://pear.horde.org/index.php?package=Kolab_Filter
*/
class Horde_Kolab_Filter_Exception
-extends Exception
+extends Horde_Exception
{
/**
* Failure constants from postfix src/global/sys_exits.h
--- /dev/null
+<?php
+/**
+ * This class provides an error thrown when the user supplied invalid command
+ * line parameters.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * This class provides an error thrown when the user supplied invalid command
+ * line parameters.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Exception_Usage
+extends Horde_Kolab_Filter_Exception
+{
+ /**
+ * Construct the exception
+ *
+ * @param string $msg
+ * @param Exception $previous
+ */
+ public function __construct($msg = '', Exception $previous = null)
+ {
+ parent::__construct(
+ $msg,
+ Horde_Kolab_Filter_Exception::OUT_STDOUT |
+ Horde_Kolab_Filter_Exception::EX_USAGE,
+ $previous
+ );
+ }
+}
<file name="Itip.php" role="php" />
</dir> <!-- /lib/Horde/Kolab/Resource -->
<dir name="Filter">
+ <file name="Cli.php" role="php" />
+ <dir name="Cli">
+ <file name="Parser.php" role="php" />
+ </dir> <!-- /lib/Horde/Kolab/Filter/Cli -->
<file name="Content.php" role="php" />
<file name="Base.php" role="php" />
<file name="Exception.php" role="php" />
+ <dir name="Exception">
+ <file name="Usage.php" role="php" />
+ </dir> <!-- /lib/Horde/Kolab/Filter/Exception -->
<file name="Incoming.php" role="php" />
<file name="Outlook.php" role="php" />
<file name="Response.php" role="php" />
<min>1.4.0b1</min>
</pearinstaller>
<package>
- <name>Horde</name>
+ <name>Core</name>
<channel>pear.horde.org</channel>
- <min>0.0.2</min>
</package>
<package>
- <name>Icalendar</name>
+ <name>Horde_iCalendar</name>
<channel>pear.horde.org</channel>
<min>0.0.3</min>
</package>
<channel>pear.horde.org</channel>
</package>
<package>
+ <name>Exception</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>Horde_MIME</name>
<channel>pear.horde.org</channel>
<min>0.0.2</min>
<install name="lib/Horde/Kolab/Resource/Freebusy.php" as="Horde/Kolab/Resource/Freebusy.php" />
<install name="lib/Horde/Kolab/Resource/Freebusy/Kolab.php" as="Horde/Kolab/Resource/Freebusy/Kolab.php" />
<install name="lib/Horde/Kolab/Resource/Freebusy/Mock.php" as="Horde/Kolab/Resource/Freebusy/Mock.php" />
- <install name="lib/Horde/Kolab/Filter/Content.php" as="Horde/Kolab/Filter/Content.php" />
<install name="lib/Horde/Kolab/Test/Filter.php" as="Horde/Kolab/Test/Filter.php" />
+ <install name="lib/Horde/Kolab/Filter/Cli.php" as="Horde/Kolab/Filter/Cli.php" />
+ <install name="lib/Horde/Kolab/Filter/Cli/Parser.php" as="Horde/Kolab/Filter/Cli/Parser.php" />
+ <install name="lib/Horde/Kolab/Filter/Content.php" as="Horde/Kolab/Filter/Content.php" />
<install name="lib/Horde/Kolab/Filter/Base.php" as="Horde/Kolab/Filter/Base.php" />
<install name="lib/Horde/Kolab/Filter/Exception.php" as="Horde/Kolab/Filter/Exception.php" />
+ <install name="lib/Horde/Kolab/Filter/Exception/Usage.php" as="Horde/Kolab/Filter/Exception/Usage.php" />
<install name="lib/Horde/Kolab/Filter/Incoming.php" as="Horde/Kolab/Filter/Incoming.php" />
<install name="lib/Horde/Kolab/Filter/Outlook.php" as="Horde/Kolab/Filter/Outlook.php" />
<install name="lib/Horde/Kolab/Filter/Response.php" as="Horde/Kolab/Filter/Response.php" />
+++ /dev/null
-<?php
-/**
- * Test the base filter class within the Kolab filter implementation.
- *
- * PHP version 5
- *
- * @category Kolab
- * @package Kolab_Filter
- * @subpackage UnitTests
- * @author Gunnar Wrobel <wrobel@pardus.de>
- * @license http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link http://pear.horde.org/index.php?package=Kolab_Filter
- */
-
-/**
- * Prepare the test setup.
- */
-require_once dirname(__FILE__) . '/Autoload.php';
-
-/**
- * Test the filter class.
- *
- * Copyright 2008 Klarälvdalens Datakonsult AB
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- *
- * @category Kolab
- * @package Kolab_Filter
- * @subpackage UnitTests
- * @author Gunnar Wrobel <wrobel@pardus.de>
- * @license http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link http://pear.horde.org/index.php?package=Kolab_Filter
- */
-class Horde_Kolab_Filter_FilterTest extends PHPUnit_Framework_TestCase
-{
-
- /**
- * Set up testing.
- */
- protected function setUp()
- {
- $GLOBALS['conf']['log']['enabled'] = false;
-
- $_SERVER['SERVER_NAME'] = 'localhost';
- $_SERVER['REMOTE_ADDR'] = 'ADDR';
- $_SERVER['REMOTE_HOST'] = 'HOST';
- }
-
-
- /**
- * Test incorrect usage of the Filter
- */
- public function testIncorrectUsage()
- {
- $_SERVER['argv'] = array($_SERVER['argv'][0]);
- $parser = new Horde_Kolab_Filter_Incoming(
- new Horde_Log_Logger(
- new Horde_Log_Handler_Mock()
- )
- );
- $inh = fopen(dirname(__FILE__) . '/fixtures/tiny.eml', 'r');
- try {
- $result = $parser->parse($inh, 'echo');
- } catch (Horde_Kolab_Filter_Exception $e) {
- $this->assertContains('Please provide one or more recipients.', $e->getMessage());
- return;
- }
- $this->assertFail('No exception!');
- }
-}
--- /dev/null
+<?php
+/**
+ * Test the CLI handling.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @subpackage UnitTests
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../Autoload.php';
+
+/**
+ * Test the CLI handling.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @subpackage UnitTests
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Integration_CliTest extends PHPUnit_Framework_TestCase
+{
+
+ /**
+ * Set up testing.
+ */
+ protected function setUp()
+ {
+ $GLOBALS['conf']['log']['enabled'] = false;
+
+ $_SERVER['SERVER_NAME'] = 'localhost';
+ $_SERVER['REMOTE_ADDR'] = 'ADDR';
+ $_SERVER['REMOTE_HOST'] = 'HOST';
+ }
+
+
+ /**
+ * Test incorrect usage of the Filter.
+ */
+ public function testIncorrectUsage()
+ {
+ $_SERVER['argv'] = array($_SERVER['argv'][0]);
+ $parser = new Horde_Kolab_Filter_Incoming(
+ new Horde_Kolab_Filter_Cli(),
+ new Horde_Log_Logger(
+ new Horde_Log_Handler_Mock()
+ )
+ );
+ $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+ try {
+ $result = $parser->parse($inh, 'echo');
+ } catch (Horde_Kolab_Filter_Exception $e) {
+ $this->assertContains(
+ 'Please provide one or more recipients.',
+ $e->getMessage()
+ );
+ return;
+ }
+ $this->assertFail('No exception!');
+ }
+
+ /**
+ * Test incorrect usage of the Filter by providing an invalid option.
+ */
+ public function testIncorrectUsageWithInvalidOption()
+ {
+ $_SERVER['argv'] = array(
+ $_SERVER['argv'][0],
+ '--recipient'
+ );
+ $parser = new Horde_Kolab_Filter_Incoming(
+ new Horde_Kolab_Filter_Cli(),
+ new Horde_Log_Logger(
+ new Horde_Log_Handler_Mock()
+ )
+ );
+ $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+ try {
+ $result = $parser->parse($inh, 'echo');
+ } catch (Horde_Kolab_Filter_Exception $e) {
+ $this->assertContains(
+ 'error: --recipient option requires an argument',
+ $e->getMessage()
+ );
+ return;
+ }
+ $this->assertFail('No exception!');
+ }
+}