|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: korvin |
|
5
|
|
|
* Date: 11/12/14 |
|
6
|
|
|
* Time: 6:17 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Buttress\IRC\Action; |
|
10
|
|
|
|
|
11
|
|
|
use Buttress\IRC\Connection\ConnectionInterface; |
|
12
|
|
|
use Buttress\IRC\Message\GenericMessage; |
|
13
|
|
|
use Buttress\IRC\Message\MessageInterface; |
|
14
|
|
|
use Buttress\IRC\Message\PrivmsgMessage; |
|
15
|
|
|
|
|
16
|
|
|
class CTCPAction extends AbstractAction |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $version; |
|
20
|
|
|
|
|
21
|
|
|
function __construct($version = 'buttress/irc v1.0.0') |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$this->version = $version; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Handle messages |
|
28
|
|
|
* |
|
29
|
|
|
* @param MessageInterface $message |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function handleMessage(MessageInterface $message) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($message instanceof PrivmsgMessage) { |
|
35
|
|
|
$string = $message->getMessage(); |
|
36
|
|
|
if (substr($string, 0, 1) === "\001") { |
|
37
|
|
|
$string = trim($string, " \001\n\t"); |
|
38
|
|
|
|
|
39
|
|
|
list($type,) = array_pad(explode(' ', $string, 2), 2, ''); |
|
40
|
|
|
list($nick, $user, $host) = $message->getUser(); |
|
41
|
|
|
|
|
42
|
|
|
$message->getConnection()->log("CTCP \"{$type}\" from {$nick}!{$user}@{$host}", array($message)); |
|
43
|
|
|
$this->handleCTCP($message, $type, $nick); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param MessageInterface $message |
|
50
|
|
|
* @param string $type |
|
51
|
|
|
* @param string $nick |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function handleCTCP($message, $type, $nick) |
|
54
|
|
|
{ |
|
55
|
|
|
$params = array($nick); |
|
56
|
|
|
switch ($type) { |
|
57
|
|
|
case 'VERSION': |
|
58
|
|
|
$params[] = "\001VERSION {$this->version}\001"; |
|
59
|
|
|
break; |
|
60
|
|
|
case 'PING': |
|
61
|
|
|
$params[] = $message->getMessage(); |
|
|
|
|
|
|
62
|
|
|
break; |
|
63
|
|
|
case 'TIME': |
|
64
|
|
|
$time = date(DATE_RFC1123); |
|
65
|
|
|
$params[] = "\001TIME {$time}\001"; |
|
66
|
|
|
break; |
|
67
|
|
|
} |
|
68
|
|
|
if (count($params) > 1) { |
|
69
|
|
|
$response = new GenericMessage('notice', '', $params); |
|
70
|
|
|
$message->getConnection()->sendMessage($response); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.