-
Notifications
You must be signed in to change notification settings - Fork 47
/
Base.php
296 lines (254 loc) · 9.04 KB
/
Base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* LICENSE: This source file is subject to Creative Commons Attribution
* 3.0 License that is available through the world-wide-web at the following URI:
* http://creativecommons.org/licenses/by/3.0/. Basically you are free to adapt
* and use this script commercially/non-commercially. My only requirement is that
* you keep this header as an attribution to my work. Enjoy!
*
* @license http://creativecommons.org/licenses/by/3.0/
*
* @package IRCBot
* @subpackage Library
* @author Daniel Siepmann <coding.layne@me.com>
*
* @encoding UTF-8
* @created 30.12.2011 20:29:55
*
* @filesource
*/
namespace Library\IRC\Command;
/**
* An IRC command.
*
* @package IRCBot
* @subpackage Library
* @author Daniel Siepmann <daniel.siepmann@me.com>
*/
abstract class Base {
/**
* Reference to the IRC Connection.
* @var \Library\IRC\Connection
*/
protected $connection = null;
/**
* Reference to the IRC Bot
* @var \Lirary\IRC\Bot
*/
protected $bot = null;
/**
* Contains all given arguments.
* @var array
*/
protected $arguments = array ( );
/**
* Contains channel or user name
*
* @var string
*/
protected $source = null;
/**
* Original request from server
*
* @var string
*/
private $data;
/**
* The number of arguments the command needs.
*
* You have to define this in the command.
*
* @var integer
*/
protected $numberOfArguments = 0;
/**
* The help string, shown to the user when using the help command.
*
* This is optional to define in the command, but it is recommended you do.
*
* @var string
*/
protected $help = '';
/**
* The usage string, shown to the user if the user calls the command with wrong parameters.
*
* You have to define this in the command.
*
* @var string
*/
protected $usage = '';
/**
* Verify the user before executing a command.
*
* Defaults to false to allow everyone to execute commands
* which do not have this flag set.
*
* This is optional to define in the command.
*
* @var bool
*/
protected $verify = false;
/**
* Executes the command.
*
* @param array $arguments The assigned arguments.
* @param string $source Originating request
* @param string $data Original data from server
*/
public function executeCommand( array $arguments, $source, $data ) {
// Set source
$this->source = $source;
// Set data
$this->data = $data;
// Do we verify the legitimacy of the user executing?
if ($this->needsVerification() && !$this->verifyUser())
{
$this->bot->log('Failed to request permission; aborting command.');
return;
}
elseif ($this->needsVerification())
$this->bot->log('Success; proceeding with command.');
// If a number of arguments is incorrect then run the command, if
// not then show the relevant help text.
// This is fugly, but it works.
// If it's an int...
if (is_numeric($this->numberOfArguments))
{
if (($this->numberOfArguments === -1 && count($arguments) == 0) || ($this->numberOfArguments !== -1 && count($arguments) != $this->numberOfArguments))
{
$this->say('Error: illegal amount of arguments. For help, use ' . $this->bot->commandPrefix . 'help ' . str_replace('Command\\', '', get_class($this)));
return;
}
}
// But if it's an array... An array means this command can take multiple counts of arguments, and react accordingly.
elseif (is_array($this->numberOfArguments))
{
if (!((in_array(count($arguments), $this->numberOfArguments)) || (in_array(-1, $this->numberOfArguments) && count($arguments) >= 1)))
{
$this->say('Error: illegal amount of arguments. For help, use' . $this->bot->commandPrefix . 'help ' . str_replace('Command\\', '', get_class($this)));
return;
}
}
// Some safeguarding here.
else
{
$this->bot->log(get_class($this) . ': No number of arguments variable set. Please add the $numberOfArguments variable to your command file.');
$this->bot->log('This command will not work until fixed.');
return;
}
// Set Arguments
$this->arguments = $arguments;
// Execute the command.
$this->command();
}
/**
* Checks the legitimacy of the user running a command.
*
*/
protected function verifyUser()
{
global $config;
// Get the host.
preg_match("/~([^\s]++)++/", $this->data, $hosts);
// Check if the user has privileges.
$this->bot->log('Requesting privileges for host ' . $hosts[0] . '...');
if (!in_array($hosts[0], $config['hosts']))
{
// Nope. No access for you.
$this->bot->log('Failed; this host is not trusted.');
return false;
}
else
return true;
}
/**
* Sends PRIVMSG to source with $msg
*
* @param string $msg
*/
protected function say($msg) {
$this->connection->sendData(
'PRIVMSG ' . $this->source . ' :' . $msg
);
}
/**
* Get help for the command being run.
*/
public function getHelp() {
if (!empty($this->help))
return array($this->help, $this->usage);
}
/**
* Check if the current commands requires verification.
*/
public function needsVerification()
{
return !empty($this->verify);
}
/**
* Overwrite this method for your needs.
* This method is called if the command get's executed.
*/
public function command() {
echo 'fail';
flush();
throw new Exception( 'You have to overwrite the "command" method and the "executeCommand". Call the parent "executeCommand" and execute your custom "command".' );
}
/**
* Set's the IRC Connection, so we can use it to send data to the server.
* @param \Library\IRC\Connection $ircConnection
*/
public function setIRCConnection( \Library\IRC\Connection $ircConnection ) {
$this->connection = $ircConnection;
}
/**
* Set's the IRC Bot, so we can use it to send data to the server.
*
* @param \Library\IRCBot $ircBot
*/
public function setIRCBot( \Library\IRC\Bot $ircBot ) {
$this->bot = $ircBot;
}
/**
* Returns requesting user IP
*
* @return string
*/
protected function getUserIp() {
// catches from @ to first space
if (preg_match('/@([a-z0-9.-]*) /i', $this->data, $match) === 1) {
$hostname = $match[1];
$ip = gethostbyname($hostname);
// did we really get an IP
if (preg_match( '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $ip ) === 1) {
return $ip;
}
}
return null;
}
/**
* Fetches data from $uri
*
* @param string $uri
* @return string
*/
protected function fetch($uri) {
$this->bot->log("Fetching from URI: " . $uri);
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $uri);
// Set a user agent. Some sites require it (e.g. GitHub API).
curl_setopt($ch, CURLOPT_USERAGENT, 'WildPHP/IRCBot');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$this->bot->log("Data fetched: " . $output);
return $output;
}
}
?>