1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phergie plugin for PHP function lookups (https://github.com/chrismou/phergie-irc-plugin-react-php) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/chrismou/phergie-irc-plugin-react-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Chris Chrisostomou (https://mou.me) |
7
|
|
|
* @license http://phergie.org/license New BSD License |
8
|
|
|
* @package Chrismou\Phergie\Plugin\Php |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Chrismou\Phergie\Plugin\Php; |
12
|
|
|
|
13
|
|
|
use Phergie\Irc\Bot\React\AbstractPlugin; |
14
|
|
|
use Phergie\Irc\Bot\React\EventQueueInterface as Queue; |
15
|
|
|
use Phergie\Irc\Plugin\React\Command\CommandEventInterface as Event; |
16
|
|
|
|
17
|
|
|
use Doctrine\DBAL\DriverManager; |
18
|
|
|
use Psr\Log\LoggerAwareInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Plugin class. |
22
|
|
|
* |
23
|
|
|
* @category Chrismou |
24
|
|
|
* @package Chrismou\Phergie\Plugin\Php |
25
|
|
|
*/ |
26
|
|
|
class Plugin extends AbstractPlugin implements LoggerAwareInterface |
27
|
|
|
{ |
28
|
|
|
/** @var */ |
29
|
|
|
protected $db; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Accepts plugin configuration. |
33
|
|
|
* |
34
|
|
|
* Supported keys: |
35
|
|
|
* |
36
|
|
|
* |
37
|
|
|
* |
38
|
|
|
* @param array $config |
39
|
|
|
*/ |
40
|
6 |
|
public function __construct(array $config = array()) |
41
|
|
|
{ |
42
|
6 |
|
if (!isset($config['dbpath']) || !$config['dbpath']) { |
43
|
|
|
$config['dbpath'] = __DIR__ . '/../data/phpdoc.db'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$connectionParams = array( |
48
|
6 |
|
'driver' => 'pdo_sqlite', |
49
|
6 |
|
'path' => $config['dbpath'] |
50
|
6 |
|
); |
51
|
6 |
|
$this->db = DriverManager::getConnection($connectionParams, new \Doctrine\DBAL\Configuration()); |
52
|
6 |
|
$this->db->connect(); |
53
|
6 |
|
} catch (\Exception $e) { |
54
|
1 |
|
$this->db = null; |
55
|
|
|
} |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return an array of event subscriptions |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
1 |
|
public function getSubscribedEvents() |
64
|
|
|
{ |
65
|
|
|
return array( |
66
|
1 |
|
'command.php' => 'handleCommand', |
67
|
|
|
'command.php.help' => 'handleCommandHelp' |
68
|
1 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Handle the main "php" command |
73
|
|
|
* |
74
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
75
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
76
|
|
|
*/ |
77
|
4 |
|
public function handleCommand(Event $event, Queue $queue) |
78
|
|
|
{ |
79
|
4 |
|
if (!$this->db) { |
80
|
1 |
|
$this->handleCommandError($event, $queue); |
81
|
1 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
if (!$this->validateParams($event)) { |
85
|
1 |
|
$this->handleCommandHelp($event, $queue); |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$function = $this->doFunctionLookup($event->getCustomParams()[0]); |
90
|
|
|
|
91
|
2 |
|
$this->doSuccessResponse($event, $queue, $function); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Perform a DB lookup on a provided PHP function name1 |
96
|
|
|
* |
97
|
|
|
* @param string $functionName |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
2 |
|
public function doFunctionLookup($functionName) |
101
|
|
|
{ |
102
|
2 |
|
return $this->db->fetchAssoc('SELECT * FROM function f WHERE name = ?', array($functionName)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Handle the help command |
107
|
|
|
* |
108
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
109
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
110
|
|
|
*/ |
111
|
2 |
|
public function handleCommandHelp(Event $event, Queue $queue) |
112
|
|
|
{ |
113
|
2 |
|
$this->sendIrcResponse($event, $queue, $this->getHelpLines()); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Handle errors |
118
|
|
|
* |
119
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
120
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
121
|
|
|
*/ |
122
|
1 |
|
public function handleCommandError(Event $event, Queue $queue) |
123
|
|
|
{ |
124
|
1 |
|
$this->sendIrcResponse($event, $queue, $this->getErrorLines()); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Process the response from a successful DB query |
129
|
|
|
* |
130
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
131
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
132
|
|
|
* @param array $function |
133
|
|
|
*/ |
134
|
3 |
|
public function doSuccessResponse(Event $event, Queue $queue, $function) |
135
|
|
|
{ |
136
|
2 |
|
if (is_array($function) && count($function)) { |
137
|
1 |
|
$this->sendIrcResponse($event, $queue, $this->getFoundFunctionLines($function)); |
138
|
1 |
|
} else { |
139
|
1 |
|
$this->sendIrcResponse($event, $queue, $this->getUnknownFunctionLines()); |
140
|
|
|
} |
141
|
3 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return an array of sucessful, 'function found' lines |
145
|
|
|
* |
146
|
|
|
* @param array $function |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
2 |
|
public function getFoundFunctionLines($function) |
150
|
|
|
{ |
151
|
|
|
$response = array( |
152
|
1 |
|
sprintf( |
153
|
1 |
|
'%s ( %s )', |
154
|
1 |
|
$function['name'], |
155
|
1 |
|
$function['parameterString'] |
156
|
1 |
|
) |
157
|
1 |
|
); |
158
|
1 |
|
if ($function['description']) { |
159
|
1 |
|
$response[] = $function['description']; |
160
|
1 |
|
} |
161
|
2 |
|
return $response; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return an array of unsucessful, 'function not found' lines |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
1 |
|
public function getUnknownFunctionLines() |
170
|
|
|
{ |
171
|
1 |
|
return array("Function not found"); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return an array of help command response lines |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
2 |
|
public function getHelpLines() |
180
|
|
|
{ |
181
|
|
|
return array( |
182
|
2 |
|
'Usage: php [function] [full]', |
183
|
2 |
|
'[function] - the PHP function you want to search for', |
184
|
|
|
//'[full] (optional) - add "full" after the function name to include the description', |
185
|
|
|
'Returns information about the specified PHP function' |
186
|
2 |
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return a=n array of error response lines |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
1 |
|
public function getErrorLines() |
195
|
|
|
{ |
196
|
1 |
|
return array('Something went wrong... ಠ_ಠ'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Check the supplied parameters are valid |
201
|
|
|
* |
202
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
3 |
|
protected function validateParams(Event $event) |
206
|
|
|
{ |
207
|
3 |
|
return (count($event->getCustomParams()) > 0) ? true : false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Send an array of response lines back to IRC |
212
|
|
|
* |
213
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
214
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
215
|
|
|
* @param array $ircResponse |
216
|
|
|
*/ |
217
|
5 |
|
protected function sendIrcResponse(Event $event, Queue $queue, array $ircResponse) |
218
|
|
|
{ |
219
|
5 |
|
foreach ($ircResponse as $ircResponseLine) { |
220
|
5 |
|
$this->sendIrcResponseLine($event, $queue, $ircResponseLine); |
221
|
5 |
|
} |
222
|
5 |
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Send a single response line back to IRC |
227
|
|
|
* |
228
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event |
229
|
|
|
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
230
|
|
|
* @param string $ircResponseLine |
231
|
|
|
*/ |
232
|
5 |
|
protected function sendIrcResponseLine(Event $event, Queue $queue, $ircResponseLine) |
233
|
|
|
{ |
234
|
5 |
|
$queue->ircPrivmsg($event->getSource(), $ircResponseLine); |
235
|
5 |
|
} |
236
|
|
|
} |
237
|
|
|
|