|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Servers\IRCBouncer; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
|
5
|
|
|
use PHPDaemon\Network\Server; |
|
6
|
|
|
use PHPDaemon\Structures\ObjectStorage; |
|
7
|
|
|
|
|
8
|
|
|
class Pool extends Server |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var Pool |
|
13
|
|
|
*/ |
|
14
|
|
|
public $client; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Connection |
|
18
|
|
|
*/ |
|
19
|
|
|
public $conn; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var boolean |
|
23
|
|
|
*/ |
|
24
|
|
|
public $protologging = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Pool |
|
28
|
|
|
*/ |
|
29
|
|
|
public $db; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var object |
|
33
|
|
|
*/ |
|
34
|
|
|
public $messages; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var object |
|
38
|
|
|
*/ |
|
39
|
|
|
public $channels; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @TODO DESCR |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function init() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->client = Pool::getInstance(); |
|
|
|
|
|
|
47
|
|
|
$this->client->protologging = $this->protologging; |
|
48
|
|
|
$this->db = Pool::getInstance(); |
|
|
|
|
|
|
49
|
|
|
$this->messages = $this->db->{$this->config->dbname->value . '.messages'}; |
|
|
|
|
|
|
50
|
|
|
$this->channels = $this->db->{$this->config->dbname->value . '.channels'}; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Setting default config options |
|
55
|
|
|
* Overriden from ConnectionPool::getConfigDefaults |
|
56
|
|
|
* @return array|bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getConfigDefaults() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
/* [string|array] Listen addresses */ |
|
62
|
|
|
'listen' => '0.0.0.0', |
|
63
|
|
|
|
|
64
|
|
|
/* [integer] Listen port */ |
|
65
|
|
|
'port' => 6667, |
|
66
|
|
|
|
|
67
|
|
|
/* [string] URL */ |
|
68
|
|
|
'url' => 'irc://user@host/nickname/realname', |
|
69
|
|
|
|
|
70
|
|
|
/* [string] Server name */ |
|
71
|
|
|
'servername' => 'bnchost.tld', |
|
72
|
|
|
|
|
73
|
|
|
/* [string] Default channels */ |
|
74
|
|
|
'defaultchannels' => '', |
|
75
|
|
|
|
|
76
|
|
|
/* [boolean] Logging? */ |
|
77
|
|
|
'protologging' => 0, |
|
78
|
|
|
|
|
79
|
|
|
/* [string] Database name */ |
|
80
|
|
|
'dbname' => 'bnc', |
|
81
|
|
|
|
|
82
|
|
|
/* [string] Password */ |
|
83
|
|
|
'password' => 'SecretPwd', |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @TODO DESCR |
|
89
|
|
|
*/ |
|
90
|
|
|
public function applyConfig() |
|
91
|
|
|
{ |
|
92
|
|
|
parent::applyConfig(); |
|
93
|
|
|
$this->protologging = (bool)$this->config->protologging->value; |
|
|
|
|
|
|
94
|
|
|
if (isset($this->client)) { |
|
95
|
|
|
$this->client->protologging = $this->protologging; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @TODO DESCR |
|
101
|
|
|
*/ |
|
102
|
|
|
public function onReady() |
|
103
|
|
|
{ |
|
104
|
|
|
parent::onReady(); |
|
105
|
|
|
$this->client->onReady(); |
|
106
|
|
|
$this->getConnection($this->config->url->value); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @TODO DESCR |
|
111
|
|
|
* @param string $url |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getConnection($url) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->client->getConnection($url, function ($conn) use ($url) { |
|
|
|
|
|
|
116
|
|
|
$this->conn = $conn; |
|
117
|
|
|
$conn->attachedClients = new ObjectStorage; |
|
118
|
|
|
if ($conn->connected) { |
|
119
|
|
|
Daemon::log('IRC bot connected at ' . $url); |
|
120
|
|
|
$conn->join($this->config->defaultchannels->value); |
|
|
|
|
|
|
121
|
|
|
$conn->bind('motd', function ($conn) { |
|
|
|
|
|
|
122
|
|
|
//Daemon::log($conn->motd); |
|
123
|
|
|
}); |
|
124
|
|
|
foreach ($this as $bouncerConn) { |
|
125
|
|
|
if (!$bouncerConn->attachedServer) { |
|
126
|
|
|
$bouncerConn->attachTo($conn); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
$conn->bind('command', function ($conn, $from, $cmd, $args) { |
|
130
|
|
|
if ($cmd === 'PONG') { |
|
131
|
|
|
return; |
|
132
|
|
|
} elseif ($cmd === 'PING') { |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
if ($from['orig'] === $conn->servername) { |
|
136
|
|
|
$from['orig'] = $this->config->servername->value; |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
$conn->attachedClients->each('commandArr', $from['orig'], $cmd, $args); |
|
139
|
|
|
}); |
|
140
|
|
|
$conn->bind('privateMsg', function ($conn, $msg) { |
|
141
|
|
|
Daemon::log('IRCBouncer: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\''); |
|
142
|
|
|
}); |
|
143
|
|
|
$conn->bind('msg', function ($conn, $msg) { |
|
144
|
|
|
$msg['ts'] = microtime(true); |
|
145
|
|
|
$msg['dir'] = 'i'; |
|
146
|
|
|
$this->messages->insert($msg); |
|
147
|
|
|
}); |
|
148
|
|
|
$conn->bind('disconnect', function ($conn) use ($url) { |
|
149
|
|
|
foreach ($this as $bouncerConn) { |
|
150
|
|
|
if ($bouncerConn->attachedServer === $conn) { |
|
151
|
|
|
$bouncerConn->detach(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
$this->getConnection($url); |
|
155
|
|
|
}); |
|
156
|
|
|
} else { |
|
157
|
|
|
Daemon::log('IRCBouncer: unable to connect (' . $url . ')'); |
|
158
|
|
|
} |
|
159
|
|
|
}); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..