Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #14 from phergie/feature-11
Browse files Browse the repository at this point in the history
Message filtering support
  • Loading branch information
WyriHaximus committed Jan 25, 2016
2 parents b44f6ee + 30ee474 commit 3c9f9af
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .dunitconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// the list of docker images to run against
images="
vectorface/php5.4
vectorface/php5.5
vectorface/php5.6
vectorface/php-nightly
vectorface/hhvm";


Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ return array(

'shortenTimeout' => 15 // If after this amount of seconds no url shortener has come up with a short URL the normal URL will be used. (Not in effect when there are no shorteners listening.)

// or

'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled

)),

)
Expand Down Expand Up @@ -87,6 +91,19 @@ Selection of response headers from: [en.wikipedia.org/wiki/List_of_HTTP_header_f
* `%header-server%`
* `%header-x-powered-by%`

## UrlSectionFilter

This plugin comes with the `UrlSectionFilter` that lets you filter on the different key value pairs coming out of [`parse_url`](http://php.net/parse_url). The following example filter allows `www.phergie.org`, `www2.phergie.org`, and `phergie.org`:

```php
new OrFilter([
new UrlSectionFilter('host', '*.phergie.org'),
new UrlSectionFilter('host', 'phergie.org'),
])
```

The filter comes with a third `strict` parameter where instead of declaring out of scope on missing an URL part it return `false`.

## Tests

To run the unit test suite:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"php": "^5.5|^7.0",
"phergie/phergie-irc-bot-react": "~2",
"nojimage/twitter-text-php": "1.1.1",
"phergie/phergie-irc-plugin-http": "^4",
"react/promise": "~1.0|~2.0"
"phergie/phergie-irc-plugin-http": "^4.0",
"react/promise": "~1.0|~2.0",
"phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0"
},
"require-dev": {
"phake/phake": "dev-VerifierResultConstraint-issue",
Expand Down
78 changes: 61 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions src/Filter/UrlEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Phergie\Irc\Plugin\React\Url\Filter;

use Phergie\Irc\ConnectionInterface;
use Phergie\Irc\Event\EventInterface;

class UrlEvent implements EventInterface
{
/**
* @var string
*/
protected $url;

/**
* @var array|mixed|false
*/
protected $parsedUrl;

/**
* @var EventInterface
*/
protected $event;

/**
* UrlEvent constructor.
* @param string $url
* @param EventInterface $event
*/
public function __construct($url, EventInterface $event)
{
$this->url = $url;
$this->parsedUrl = parse_url($url);
$this->event = $event;
}

public function setMessage($message)
{
return $this->event->setMessage($message);
}

public function getMessage()
{
return $this->event->getMessage();
}

public function setConnection(ConnectionInterface $connection)
{
return $this->event->setConnection($connection);
}

public function getConnection()
{
return $this->event->getConnection();
}

public function setParams(array $params)
{
return $this->event->setParams($params);
}

public function getParams()
{
return $this->event->getParams();
}

public function setCommand($command)
{
return $this->event->setCommand($command);
}

public function getCommand()
{
return $this->event->getCommand();
}

public function getUrlSection($section)
{
if (isset($this->parsedUrl[$section])) {
return $this->parsedUrl[$section];
}

return null;
}
}
61 changes: 61 additions & 0 deletions src/Filter/UrlSectionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Phergie\Irc\Plugin\React\Url\Filter;

use Phergie\Irc\Event\EventInterface;
use Phergie\Irc\Plugin\React\EventFilter\FilterInterface;

class UrlSectionFilter implements FilterInterface
{
/**
* @var string
*/
protected $value;

/**
* @var string
*/
protected $section;

/**
* @var null|false
*/
protected $strictResponse = null;

/**
*
* @param string $section
* @param string $value
*/
public function __construct($section, $value, $strict = false)
{
$this->section = $section;
$this->value = $value;
if ($strict === true) {
$this->strictResponse = false;
}
}

/**
* @param EventInterface $event
* @return bool|null
*/
public function filter(EventInterface $event)
{
if (!($event instanceof UrlEvent)) {
return null;
}

$section = $event->getUrlSection($this->section);
if ($section === null) {
return $this->strictResponse;
}

$pattern = '/^' . str_replace('*', '.*', $this->value) . '$/';
if (preg_match($pattern, $section)) {
return true;
}

return false;
}
}
Loading

0 comments on commit 3c9f9af

Please sign in to comment.