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

Commit 3c9f9af

Browse files
committed
Merge pull request #14 from phergie/feature-11
Message filtering support
2 parents b44f6ee + 30ee474 commit 3c9f9af

10 files changed

Lines changed: 440 additions & 23 deletions

File tree

.dunitconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// the list of docker images to run against
22
images="
3-
vectorface/php5.4
43
vectorface/php5.5
54
vectorface/php5.6
5+
vectorface/php-nightly
66
vectorface/hhvm";
77

88

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ return array(
4040

4141
'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.)
4242

43+
// or
44+
45+
'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled
46+
4347
)),
4448

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

94+
## UrlSectionFilter
95+
96+
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`:
97+
98+
```php
99+
new OrFilter([
100+
new UrlSectionFilter('host', '*.phergie.org'),
101+
new UrlSectionFilter('host', 'phergie.org'),
102+
])
103+
```
104+
105+
The filter comes with a third `strict` parameter where instead of declaring out of scope on missing an URL part it return `false`.
106+
90107
## Tests
91108

92109
To run the unit test suite:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"php": "^5.5|^7.0",
1717
"phergie/phergie-irc-bot-react": "~2",
1818
"nojimage/twitter-text-php": "1.1.1",
19-
"phergie/phergie-irc-plugin-http": "^4",
20-
"react/promise": "~1.0|~2.0"
19+
"phergie/phergie-irc-plugin-http": "^4.0",
20+
"react/promise": "~1.0|~2.0",
21+
"phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0"
2122
},
2223
"require-dev": {
2324
"phake/phake": "dev-VerifierResultConstraint-issue",

composer.lock

Lines changed: 61 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Filter/UrlEvent.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Phergie\Irc\Plugin\React\Url\Filter;
4+
5+
use Phergie\Irc\ConnectionInterface;
6+
use Phergie\Irc\Event\EventInterface;
7+
8+
class UrlEvent implements EventInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $url;
14+
15+
/**
16+
* @var array|mixed|false
17+
*/
18+
protected $parsedUrl;
19+
20+
/**
21+
* @var EventInterface
22+
*/
23+
protected $event;
24+
25+
/**
26+
* UrlEvent constructor.
27+
* @param string $url
28+
* @param EventInterface $event
29+
*/
30+
public function __construct($url, EventInterface $event)
31+
{
32+
$this->url = $url;
33+
$this->parsedUrl = parse_url($url);
34+
$this->event = $event;
35+
}
36+
37+
public function setMessage($message)
38+
{
39+
return $this->event->setMessage($message);
40+
}
41+
42+
public function getMessage()
43+
{
44+
return $this->event->getMessage();
45+
}
46+
47+
public function setConnection(ConnectionInterface $connection)
48+
{
49+
return $this->event->setConnection($connection);
50+
}
51+
52+
public function getConnection()
53+
{
54+
return $this->event->getConnection();
55+
}
56+
57+
public function setParams(array $params)
58+
{
59+
return $this->event->setParams($params);
60+
}
61+
62+
public function getParams()
63+
{
64+
return $this->event->getParams();
65+
}
66+
67+
public function setCommand($command)
68+
{
69+
return $this->event->setCommand($command);
70+
}
71+
72+
public function getCommand()
73+
{
74+
return $this->event->getCommand();
75+
}
76+
77+
public function getUrlSection($section)
78+
{
79+
if (isset($this->parsedUrl[$section])) {
80+
return $this->parsedUrl[$section];
81+
}
82+
83+
return null;
84+
}
85+
}

src/Filter/UrlSectionFilter.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Phergie\Irc\Plugin\React\Url\Filter;
4+
5+
use Phergie\Irc\Event\EventInterface;
6+
use Phergie\Irc\Plugin\React\EventFilter\FilterInterface;
7+
8+
class UrlSectionFilter implements FilterInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $value;
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $section;
19+
20+
/**
21+
* @var null|false
22+
*/
23+
protected $strictResponse = null;
24+
25+
/**
26+
*
27+
* @param string $section
28+
* @param string $value
29+
*/
30+
public function __construct($section, $value, $strict = false)
31+
{
32+
$this->section = $section;
33+
$this->value = $value;
34+
if ($strict === true) {
35+
$this->strictResponse = false;
36+
}
37+
}
38+
39+
/**
40+
* @param EventInterface $event
41+
* @return bool|null
42+
*/
43+
public function filter(EventInterface $event)
44+
{
45+
if (!($event instanceof UrlEvent)) {
46+
return null;
47+
}
48+
49+
$section = $event->getUrlSection($this->section);
50+
if ($section === null) {
51+
return $this->strictResponse;
52+
}
53+
54+
$pattern = '/^' . str_replace('*', '.*', $this->value) . '$/';
55+
if (preg_match($pattern, $section)) {
56+
return true;
57+
}
58+
59+
return false;
60+
}
61+
}

0 commit comments

Comments
 (0)