1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @package Examples |
6
|
|
|
* @subpackage Base |
7
|
|
|
* |
8
|
|
|
* @author Vasily Zorin <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class ExampleIRCBot extends \PHPDaemon\Core\AppInstance |
12
|
|
|
{ |
13
|
|
|
public $client; |
14
|
|
|
public $conn; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor. |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function init() |
21
|
|
|
{ |
22
|
|
|
if ($this->isEnabled()) { |
23
|
|
|
$this->client = \PHPDaemon\Clients\IRC\Pool::getInstance(); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Called when the worker is ready to go. |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function onReady() |
32
|
|
|
{ |
33
|
|
|
if ($this->client) { |
34
|
|
|
$this->client->onReady(); |
35
|
|
|
$this->connect(); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function connect() |
40
|
|
|
{ |
41
|
|
|
$app = $this; |
42
|
|
|
$r = $this->client->getConnection($this->config->url->value, function ($conn) use ($app) { |
|
|
|
|
43
|
|
|
$app->conn = $conn; |
44
|
|
|
if ($conn->connected) { |
45
|
|
|
\PHPDaemon\Core\Daemon::log('IRC bot connected at ' . $this->config->url->value); |
46
|
|
|
$conn->join('#botwar_phpdaemon'); |
47
|
|
|
$conn->bind('motd', function ($conn) { |
|
|
|
|
48
|
|
|
//\PHPDaemon\Daemon::log($conn->motd); |
49
|
|
|
}); |
50
|
|
|
$conn->bind('privateMsg', function ($conn, $msg) { |
51
|
|
|
\PHPDaemon\Core\Daemon::log('IRCBot: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\''); |
52
|
|
|
$conn->message($msg['from']['nick'], 'You just wrote: ' . $msg['body']); // send the message back |
53
|
|
|
}); |
54
|
|
|
$conn->bind('disconnect', function () use ($app) { |
55
|
|
|
$app->connect(); |
56
|
|
|
}); |
57
|
|
|
} else { |
58
|
|
|
\PHPDaemon\Core\Daemon::log('IRCBot: unable to connect (' . $this->config->url->value . ')'); |
59
|
|
|
} |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Called when worker is going to update configuration. |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function onConfigUpdated() |
68
|
|
|
{ |
69
|
|
|
if ($this->client) { |
70
|
|
|
$this->client->config = $this->config; |
71
|
|
|
$this->client->onConfigUpdated(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Called when application instance is going to shutdown. |
77
|
|
|
* @return boolean Ready to shutdown? |
78
|
|
|
*/ |
79
|
|
|
public function onShutdown() |
80
|
|
|
{ |
81
|
|
|
if ($this->client) { |
82
|
|
|
return $this->client->onShutdown(); |
83
|
|
|
} |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setting default config options |
89
|
|
|
* Overriden from AppInstance::getConfigDefaults |
90
|
|
|
* Uncomment and return array with your default options |
91
|
|
|
* @return array|false |
92
|
|
|
*/ |
93
|
|
|
protected function getConfigDefaults() |
94
|
|
|
{ |
95
|
|
|
$random = sprintf('%x', crc32(posix_getpid() . "\x00" . microtime(true))); |
96
|
|
|
return [ |
97
|
|
|
'url' => 'irc://guest_' . $random . ':[email protected]/Bot_phpDaemon' |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.